Documentation
¶
Overview ¶
Package streamjson re-emits the parsed claude session-JSONL Event stream onto a writer (typically pyry's stdout) and composes a single `type:"result"` trailer line when the run terminates. Output shape mirrors `claude -p --output-format stream-json` so the dispatcher's stream-json parser keeps working unchanged after the agent-run migration.
MUST NOT log Event content. The package logs only counts, durations, and error messages — never Event.Raw bytes nor per-event Usage values.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Writer is where stream-json lines are written. Required; production
// passes os.Stdout.
Writer io.Writer
// SessionID is the UUIDv4 the caller minted and passed to claude via
// --session-id. Stamped into the trailer's `session_id` field. Required.
SessionID string
// Now is a clock seam; defaults to time.Now. New captures Now() at
// construction; Close calls Now() again to compute duration_ms.
Now func() time.Time
// Logger is optional and defaults to slog.Default().
Logger *slog.Logger
}
Config configures Emitter.
type Emitter ¶
type Emitter struct {
// contains filtered or unexported fields
}
Emitter re-emits jsonl.Events to its Writer and composes a single `type:"result"` trailer line on Close. Safe for concurrent use across Emit, SetExitReason, and Close.
func (*Emitter) Close ¶
Close writes the final `type:"result"` trailer line. MUST be called exactly once after Emit calls have stopped; calling Close while Emit is still firing is a race.
Default exit-reason classification when SetExitReason was not called:
- end-of-turn was observed during Emit → ExitReasonCompletion
- end-of-turn was NOT observed → ExitReasonError
Idempotent: second and subsequent calls return the first call's error verbatim without re-writing the trailer.
func (*Emitter) Emit ¶
Emit re-emits ev.Raw verbatim followed by '\n', aggregates ev.Usage, and counts assistant entries. Safe for concurrent use.
Once an underlying write fails, the error is sticky: subsequent Emit calls no-op (returning nil) so the watcher can keep parsing until end-of-turn or ctx cancel without thrashing on a broken pipe. Close still attempts to write the trailer (best-effort).
Emit after Close is a no-op returning nil — late events from a slow watcher goroutine must not panic.
func (*Emitter) SetExitReason ¶
func (e *Emitter) SetExitReason(r ExitReason)
SetExitReason overrides the default exit-reason classification used by Close. Pluggable seam for the future budget integration (#334), which calls SetExitReason(ExitReasonMaxTurns) from its Terminate hook before SIGTERMing claude.
Idempotent: only the first non-empty value sticks; later calls are no-ops. Safe for concurrent use.
type ExitReason ¶
type ExitReason string
ExitReason classifies how the run terminated. The internal value is translated into wire fields (`subtype` / `terminal_reason` / `is_error`) at trailer-composition time; see closeLocked.
const ( // ExitReasonCompletion signals a clean, deterministic end-of-turn // observation from the watcher. ExitReasonCompletion ExitReason = "completion" // ExitReasonMaxTurns signals the budget Counter forcibly stopped the // run. Set via SetExitReason from #334's future Terminate hook. ExitReasonMaxTurns ExitReason = "max_turns" // ExitReasonError signals an unrecoverable failure — claude exited // non-zero, the watcher failed, or the run was torn down before // end-of-turn fired. ExitReasonError ExitReason = "error" )