feat(opencode): turn lifecycle events (Turn.Started, Turn.Ended) - #81
Merged
Conversation
Adds `SessionEvent.Turn.Started` and `SessionEvent.Turn.Ended` (live events, types `session.next.turn.started`/`session.next.turn.ended`) carrying `{ sessionID, timestamp }` (+ `finished` on Ended). Fires `Turn.Started` once per turn at the top of `runLoop` and `Turn.Ended` once when the loop exits.
First two events of the agent-loop decomposition. They mark the natural lifecycle boundary of a turn (a user prompt may span multiple steps) and give V2 plugins / telemetry a clean entry/exit point without depending on the internal `while (true)` structure. Same shim pattern as the rest: V2 surface alongside the existing V1 flow, no breaking change.
Next decomposition steps (separate PRs): `Step.Started`/`Step.Ended` per-step events, `LLM.Stream` already wired in schema, `Tools.Execute` event around the per-tool pipeline, and finally the per-step services replaceable via the plugin runtime.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First two events of the agent-loop decomposition. Adds
SessionEvent.Turn.StartedandSessionEvent.Turn.Ended(live events) and fires them around therunLoopinpackages/opencode/src/session/prompt.ts.What this PR does
Turn.Started/Turn.Endedto the schema (live, non-durable).Turn.Startedfires once at the top ofrunLoop, before any step runs.Turn.Endedfires once when the loop exits (after the finalbreak), before the prune fork.Why this matters
A turn is the natural boundary of agent work (one user prompt may span multiple steps). Marking it explicitly gives V2 plugins / telemetry a clean entry/exit point without depending on the internal
while (true)structure. dsh usesturn/startandturn/endingfor the same reason.Pattern
Same as the rest: V2 surface alongside the existing V1 flow, no breaking change. V2 plugins can subscribe via
events.subscribe/events.subscribeAllwithout registering a V1 plugin.Out of scope (next steps in the decomposition)
Step.Started/Step.Endedper-step events (already in schema, just need wiring)LLM.Streamevent around the AI-SDK callTools.Executeevent wrapping the per-tool pipelineNeed help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.