Repository navigation
fix(cli): emit synthetic background notices and completed compactions in json run output - #64852
zsicheng0214 wants to merge 3 commits into
Conversation
… in json run output
There was a problem hiding this comment.
Thanks for picking this up. As it stands, background completion notices don't stream live; they only show up at the end of the run, after the turn they woke.
I ran opencode run --format json --auto from source against a simulated model. While the first turn was still running, I posted a queued synthetic <shell> notice through session.synthetic, which is the same path subagent-completion.ts and the shell result code take. The notice woke a second turn.
- On the base, the JSON stream has no event for the notice.
- With this PR, a
syntheticevent appears only as the very last line, after the woken turn'sstep_finish. Itspart.idis theprojectedPartID(..., "synthetic")form, so it came fromreconcile, not the live handler.
The reason is that background reports go through the inbox. The projector turns them into messages on session.inbox.delivered. A session.synthetic event is only published directly for a few internal notices (output-limit continuation, restarts, instructions). The live path needs to catch inbox deliveries of synthetic items, for example by remembering the payload from session.inbox.enqueued and emitting it on session.inbox.delivered. The test should feed that same event sequence too; right now it uses a hand-made session.synthetic event the real flow never sends.
The live compaction path uses the same message ID as the server (inputID ?? fromEvent(started.id)), so the dedup against reconcile holds. I couldn't get a compaction to run inside a run to see it end to end.
Public API: run --format json gains two new event types, synthetic (part: id, sessionID, messageID, text, optional description/metadata, time.created) and compaction (part: reason, summary, optional cost/tokens/metadata, time.created). Both are additions. Consumers that ignore unknown types are unaffected; strict parsers will see new types. No other API changes.
Tests in packages/cli/test/run/noninteractive.test.ts pass and tsc -b passes.
|
|
||
| event: async (event: Event) => { | ||
| const time = "created" in event ? event.created : Date.now() | ||
| if (event.type === "session.synthetic") { |
There was a problem hiding this comment.
Background subagent/shell reports never reach this branch. SessionSynthetic puts them in the inbox, and they become messages on session.inbox.delivered. A direct session.synthetic event only comes from internal notices (output-limit continuation, restarts, instructions). In a real run I saw the notice only from reconcile, after the turn it woke had already finished.
To stream it in place, this probably needs to handle session.inbox.delivered for synthetic items, for example by remembering the payload from session.inbox.enqueued by inboxID. The inbox ID is also the message ID, so the printed dedup would still work.
| { | ||
| id: "evt_subagent_report", | ||
| created: 2, | ||
| type: "session.synthetic", |
There was a problem hiding this comment.
This feeds a hand-made session.synthetic event, but real background reports arrive as session.inbox.enqueued + session.inbox.delivered. That's why the test passes while the live path misses real notices. Please drive the test with the event sequence the server actually sends.
|
Thanks for catching the live inbox path! Updated |
There was a problem hiding this comment.
Both earlier points are fixed. The renderer now saves queued synthetic notices when they are enqueued and prints them when the inbox delivers them, and the test sends the real enqueue-then-deliver events.
I re-ran the end-to-end check from my last review: opencode run --format json against a simulated model, posting a queued <shell> notice while the first turn was still running. The synthetic event now appears once, in place, between the first turn's step_finish and the woken turn's step_start, and reconcile doesn't print it again. On the base it doesn't appear at all.
Public API: unchanged since the last review. run --format json still gains the additive synthetic and compaction event types.
The tests in noninteractive.test.ts pass and tsc -b passes. No new concerns.
|
All 25 The JSON order was |
…ssions When `opencode run --continue` attaches to a session with an older synthetic report already queued before event subscription, `session.inbox.enqueued` was not observed live. Fall back to reading the projected synthetic message on `session.inbox.delivered` so it streams before the woken turn's steps.
|
Great catch, @r266-tech — thank you for testing that scenario! Addressed in |
There was a problem hiding this comment.
The new commit works. Synthetic notices that were queued before the run started now stream in place on an existing session.
I checked it end to end: I created a session with a busy first turn, queued a <shell> notice on it, then ran opencode run --session <id> --format json. On the base, no event appears for the notice. With this PR, one synthetic event appears between the first turn's step_finish and the woken turn's step_start, and reconcile doesn't print it again. I also re-ran the earlier case, a notice queued during the run, and it still appears once, in place.
Public API: unchanged since the last review. run --format json still gains the additive synthetic and compaction event types.
The tests in noninteractive.test.ts pass and tsc -b passes. No new concerns.
Issue for this PR
Closes #64851
Type of change
What does this PR do?
Emits
syntheticand completedcompactionsession messages inopencode run --format json(packages/cli/src/run/noninteractive.ts), keeping default human output unchanged:session.syntheticandsession.compaction.started/session.compaction.ended/session.compaction.failedinrenderer.eventso background<subagent>/<shell>completion notices (includingdescriptionandmetadata) and completed compactions (includingreason,summary,cost, andtokens) stream live in JSON mode.message.type === "synthetic"andmessage.type === "compaction"(status === "completed") inrenderer.reconcile, deduplicated by message ID viaprintedso messages seen live are not emitted twice.How did you verify your code works?
Ran in
packages/cliand repo root:Screenshots / recordings
N/A (CLI
--format jsonstream handling)Checklist
Added by the opencode bot (AI-generated).
Public API: adds
syntheticandcompactionevent types toopencode run --format jsonoutput (additive, backwards compatible)