Documentation
¶
Overview ¶
Package turnbridge drains the supervised claude session's unified tui-driver Events() stream and maps each event into the neutral internal turn-event model (internal/turnevent, #606). It is the producer half of the structured-event bridge (EPIC #596, ADR 025 § Phase 2 structured streaming); the consumer half (#616) attaches an OnEvent callback and fans the mapped stream out to capability-advertising phones. This slice ships the producer with unit tests, deliberately unwired to a live fan-out.
Index ¶
- func BuildTurnState(conversationID string, state TurnState) (typ string, payload protocol.TurnStatePayload)
- func MapEvent(ev turnevent.Event, tc TurnContext) (typ string, payload any, ok bool)
- type Config
- type Producer
- type SessionHost
- type Subscriber
- type Target
- type TargetResolver
- type TurnContext
- type TurnState
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildTurnState ¶
func BuildTurnState(conversationID string, state TurnState) (typ string, payload protocol.TurnStatePayload)
BuildTurnState shapes a turn_state payload for the given conversation and target state. The consumer's lifecycle machine decides WHICH state applies (e.g. observing a ThoughtChunk means "thinking") and calls this; the adapter only shapes the payload. The return type is concrete (not any) because it is monomorphic — the consumer needs no type assertion.
func MapEvent ¶
MapEvent maps one neutral turnevent.Event plus explicit turn context to the matching v2 interactive wire payload and its envelope type discriminant.
ok is false for events with no wire representation: ThoughtChunk (ADR 025 — #607 defines no thought-text envelope; thinking surfaces as a turn_state transition the consumer drives via BuildTurnState, and the thought text is NOT forwarded) and any nil/unknown Event. The consumer drops + debug-logs those. Pure; safe on a zero-value Event.
payload is one of the four protocol.*Payload value structs, or nil when ok is false. It is any because the four payloads share no marker interface; the consumer json.Marshals it directly (same path as protocol.MessagePayload). The consumer owns the envelope ID, TS, marshal, and seal — none happen here.
Types ¶
type Config ¶
type Config struct {
// Subscribe yields the live event stream; required.
Subscribe Subscriber
// OnEvent receives each mapped internal event. nil ⇒ the producer drains
// the source and does nothing else (AC 4's "no-op beyond draining").
OnEvent func(turnevent.Event)
// FlushSignal, if non-nil, is selected in the drain loop; each receive
// invokes OnFlush on the single Run goroutine. The owning consumer drives
// the timer behind this channel (arm/reset/stop), so a "reset on flush"
// policy stays single-goroutine-safe. nil ⇒ no periodic-flush arm (#609).
FlushSignal <-chan time.Time
// OnFlush, if non-nil and FlushSignal fires, runs on the Run goroutine — the
// same goroutine as OnEvent — so a consumer may flush state it mutates across
// OnEvent calls without a lock. nil ⇒ ignored.
OnFlush func()
// Logger; nil ⇒ slog.Default().
Logger *slog.Logger
}
Config configures a Producer.
type Producer ¶
type Producer struct {
// contains filtered or unexported fields
}
Producer drains a tui-driver event stream into the neutral turn-event model. The OnEvent callback runs on the single Run goroutine, so the consumer's callback (#616) must not block it indefinitely — its own queue owns backpressure (ADR 025 § Backpressure).
func (*Producer) Run ¶
Run drives the outer re-subscribe loop until ctx is cancelled. Each iteration subscribes (blocking until a live stream exists), drains it until the channel closes (session restart) or ctx is done, then re-subscribes. Re-subscribing after a channel close is "no leaked goroutine across a session restart": the prior session's merge goroutine already closed its channel. Returns ctx.Err() on cancellation — the only error Subscribe yields per its contract.
type SessionHost ¶
SessionHost is the supervisor seam the production Subscriber drives. *supervisor.Supervisor satisfies it structurally.
type Subscriber ¶
Subscriber yields a live session's tui-driver event stream. The returned channel closes when that session ends (supervisor restart) or ctx is done. Returns a non-nil error ONLY on ctx cancellation; transient resolution failures are retried internally, so the channel-or-ctx-done contract holds.
func NewTargetSubscriber ¶
func NewTargetSubscriber( resolve TargetResolver, tr *tuidriver.Tracker, log *slog.Logger, ) Subscriber
NewTargetSubscriber builds the production Subscriber for the follow-active producer lifecycle (#679). resolve yields the current Target — which session host to stream over, which JSONL to tail, and the optional switch channel that forces a re-subscribe when the active conversation changes. tr is required by Session.Events (it drives only the dropped stall arm); pass tuidriver.NewTracker(tuidriver.TrackerOpts{}).
Structure: an OUTER loop calls resolve once per (re)subscription to snapshot a Target, and an INNER loop runs the WaitForPTY → resolve-JSONL → Events sequence for that Target. The inner loop's transient retries reuse target.Resolve so its per-subscription cold/warm offset state (resolvedOnce/sawEmpty) persists across a not-yet-present JSONL — the #671 cold-start gate, now per bound session. A switch (target.Switch fires) cancels the per-subscription ctx, which aborts any in-flight pre-stream wait and breaks back to the outer loop to re-snapshot the now-active Target; this switch-abortable wait is the one new invariant beyond the single-host body and is load-bearing for AC3 — without it the producer can wedge forever in WaitForPTY of a stale evicted session after the operator switches conversations.
Like the single-host subscriber it owns a per-subscription ctx cancelled by sess.Wait() (session end → the merge loop closes the Events channel so Producer.Run re-subscribes) — "no leaked goroutine across a session restart". The switch watcher exits on the same ctx, so it never outlives its subscription; both watchers call the idempotent cancel().
type Target ¶
type Target struct {
// Host is the supervisor seam to WaitForPTY on and capture Session() from.
Host SessionHost
// Resolve yields which JSONL to tail and from what offset, re-evaluated per
// transient retry within a subscription so a not-yet-present file is gated
// then picked up (the #671 cold-start rule). Same contract as the resolver
// the single-host subscriber took.
Resolve func(ctx context.Context) (path string, startOffset int64, err error)
// Switch, if non-nil, tears the subscription down when it fires so
// Producer.Run re-subscribes onto the now-current Target (follow-active). A
// nil Switch means session-end and parent-ctx cancel are the only teardown
// triggers — the pre-#679 single-host behavior.
Switch <-chan struct{}
}
Target is everything one (re)subscription needs, resolved fresh per subscription by a TargetResolver: which session host to wait on + capture the live session from, which JSONL to tail (and from what offset), and an optional channel that forces a re-subscribe when it fires. It is the follow-active generalisation (#679) of the single fixed host + resolver the subscriber used to bake in — turnbridge stays neutral about how a Target is chosen.
type TargetResolver ¶
TargetResolver yields the current Target. Called once at the top of each (re)subscription. A non-nil error is retried (subscribeRetryDelay backoff) unless ctx is done — the existing transient-resolve backoff, now also covering the rare "active conversation names an unresolvable bound session" case.
type TurnContext ¶
type TurnContext struct {
ConversationID string
TurnID string
// Seq is the per-turn assistant-delta ordering counter. It is consumed
// ONLY by the TextChunk -> assistant_delta mapping and ignored for every
// other event kind; the consumer advances it.
Seq int
}
TurnContext is the per-event turn addressing the consumer supplies to MapEvent. The adapter never derives these — which conversation / turn / seq applies to a given event is a turn-lifecycle decision owned by the consumer.