obs

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package obs is the local observability layer: a structured event bus that feeds an append-only JSONL audit log and a local IPC host.

It exists because MCP stdio is a private channel between the AI client and jms: without it there is no independent, real-time, inspectable view of what the AI executed (DESIGN.md「可观测性」).

Index

Constants

View Source
const (
	// EnvAudit disables auditing when set to "off".
	EnvAudit = "JMS_AUDIT"
	// EnvAuditDir relocates the audit directory.
	EnvAuditDir = "JMS_AUDIT_DIR"
	// EnvAuditFull stores the untruncated output preview when set to "1".
	EnvAuditFull = "JMS_AUDIT_FULL"
	// EnvAuditPreviewBytes tunes the preview truncation limit.
	EnvAuditPreviewBytes = "JMS_AUDIT_PREVIEW_BYTES"
)

Environment switches (DESIGN.md「审计日志」).

View Source
const (
	// DefaultRingSize bounds the in-memory replay buffer.
	DefaultRingSize = 512
	// DefaultPreviewBytes truncates a captured output preview.
	DefaultPreviewBytes = 4096
)

Defaults.

Variables

This section is empty.

Functions

func MarshalLine

func MarshalLine(e Event) ([]byte, error)

MarshalLine renders an event as one JSONL line, without a trailing newline.

It is the single place the on-disk encoding is decided, so the writer and any reader agree by construction.

func NewBusWithAudit

func NewBusWithAudit(ctx context.Context, opts AuditOptions) (*Bus, *AuditWriter, error)

NewBusWithAudit wires a bus to an audit writer when auditing is enabled.

JMS_AUDIT=off disables it; JMS_AUDIT_DIR relocates the directory; JMS_AUDIT_FULL=1 stores full output; JMS_AUDIT_PREVIEW_BYTES tunes the preview. An explicit AuditOptions field wins over its environment switch, so an injected test or a caller that already resolved the config does not have to unset the environment.

The returned bus is never nil, even when the writer is: a failed audit file still leaves IPC observation and `--last` replay working. On failure the error is returned together with the usable bus and a nil writer, leaving it to the caller to decide whether a missing audit log is fatal for the command it is about to run.

ctx is accepted for symmetry with the rest of the runtime and is unused: opening a file is synchronous, and the writer's lifetime ends at Close rather than at cancellation.

Types

type AuditOptions

type AuditOptions struct {
	// Dir is the audit directory; the empty value means <config_dir>/audit.
	Dir string
	// PreviewBytes truncates Preview; 0 means DefaultPreviewBytes.
	PreviewBytes int
	// Full disables truncation.
	Full bool
}

AuditOptions tunes an AuditWriter.

type AuditWriter

type AuditWriter struct {
	// contains filtered or unexported fields
}

AuditWriter appends events as JSONL, one file per process.

Per-pid files avoid the Windows lock contention of several MCP instances appending to one file (DESIGN.md「审计日志」).

func NewAuditWriter

func NewAuditWriter(opts AuditOptions) (*AuditWriter, error)

NewAuditWriter opens the per-pid JSONL file.

The directory and the file are created 0600/0700 on Unix.

func (*AuditWriter) Close

func (w *AuditWriter) Close() error

Close flushes and closes the file.

It is idempotent: every call reports the same outcome as the first one, and later calls never touch the closed file. A write failure recorded by Publish is returned here, because a Subscriber has nowhere else to report it. It is safe on a nil writer, which is what NewBusWithAudit returns when auditing is disabled.

func (*AuditWriter) Path

func (w *AuditWriter) Path() string

Path returns the file path.

It returns "" for a nil writer (auditing disabled), so callers can print it without a nil check.

func (*AuditWriter) Publish

func (w *AuditWriter) Publish(e Event)

Publish implements Subscriber.

The writer owns the per-pid file, so it stamps its own pid on every line: a record whose pid contradicts the file name would make `jms tail`'s per-file attribution wrong. A write failure cannot be reported to the publisher, so it is remembered and returned by Close.

type Bus

type Bus struct {
	// contains filtered or unexported fields
}

Bus is a non-blocking event bus with a bounded replay ring.

A nil *Bus is usable and discards everything: callers carry an optional bus and "nil disables publication" is part of their contract (mcpserver.Options, cli.Runtime).

func NewBus

func NewBus(ringSize int) *Bus

NewBus returns an empty bus with a ring of size ringSize (0 means DefaultRingSize).

func (*Bus) Dropped

func (b *Bus) Dropped() int64

Dropped reports how many deliveries were skipped.

func (*Bus) Last

func (b *Bus) Last(n int) []Event

Last returns up to n most recent events, oldest first.

func (*Bus) Publish

func (b *Bus) Publish(e Event)

Publish delivers e to every subscriber and records it in the ring.

A subscriber that blocks is skipped and counted: the execution path must never stall on a slow observer. Delivery is asynchronous, so a subscriber may observe an event after Publish has returned.

func (*Bus) Subscribe

func (b *Bus) Subscribe(s Subscriber)

Subscribe registers a subscriber.

Every call registers a delivery stream, so subscribing the same value twice delivers twice. Subscribers are identified for Unsubscribe by their interface value (see Unsubscribe).

A delivery goroutine drains each registration until Unsubscribe removes it. A Bus is therefore process-scoped, like the pools it feeds: a caller that registers a short-lived subscriber must unsubscribe it to release that goroutine.

func (*Bus) Unsubscribe

func (b *Bus) Unsubscribe(s Subscriber)

Unsubscribe removes every registration of s.

Events already queued for s are still delivered: they were published before the unsubscribe. No event published after Unsubscribe returns reaches s.

Subscribers are compared by their interface value: pointer-shaped subscribers are matched by identity. A SubscriberFunc is matched by its code pointer, because Go panics when an interface holding a function type is compared — two closures built from the same function literal are indistinguishable, so register a pointer-typed subscriber when that matters.

type Event

type Event struct {
	TS          time.Time `json:"ts"`
	PID         int       `json:"pid"`
	Actor       string    `json:"actor,omitempty"`
	Kind        Kind      `json:"kind"`
	Server      string    `json:"server,omitempty"`
	Asset       string    `json:"asset,omitempty"`
	Backend     string    `json:"backend,omitempty"`
	Endpoint    string    `json:"endpoint,omitempty"`
	Command     string    `json:"command,omitempty"`
	ExitCode    *int      `json:"exit_code,omitempty"`
	DurationMS  int64     `json:"duration_ms,omitempty"`
	OutputBytes int       `json:"output_bytes,omitempty"`
	Preview     string    `json:"preview,omitempty"`
	Pool        string    `json:"pool,omitempty"`
	IdleMS      int64     `json:"idle_ms,omitempty"`
	Reason      string    `json:"reason,omitempty"`
	Error       string    `json:"error,omitempty"`
}

Event is one audit record. The JSON shape is the on-disk contract.

type Kind

type Kind string

Kind enumerates the audit event types (DESIGN.md「审计日志」).

const (
	KindExecStart      Kind = "exec.start"
	KindExecEnd        Kind = "exec.end"
	KindPoolCold       Kind = "pool.cold"
	KindPoolHit        Kind = "pool.hit"
	KindPoolEvict      Kind = "pool.evict"
	KindPoolReap       Kind = "pool.reap"
	KindSessionLogin   Kind = "session.login"
	KindSessionRelogin Kind = "session.relogin"
	KindEndpointSelect Kind = "endpoint.select"
	KindError          Kind = "error"
)

Event kinds.

type Subscriber

type Subscriber interface {
	// Publish delivers one event. It must not block indefinitely.
	Publish(Event)
}

Subscriber receives events. A slow subscriber must never block the publisher (DESIGN.md「可观测性」).

type SubscriberFunc

type SubscriberFunc func(Event)

SubscriberFunc adapts a function to Subscriber.

func (SubscriberFunc) Publish

func (f SubscriberFunc) Publish(e Event)

Publish calls f.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL