Skip to content

fix(cli): emit synthetic background notices and completed compactions in json run output - #64852

Open
zsicheng0214 wants to merge 3 commits into
anomalyco:devfrom
TraceLite-AI:run-json-events
Open

zsicheng0214 wants to merge 3 commits into
anomalyco:devfrom
TraceLite-AI:run-json-events

Conversation

@zsicheng0214

@zsicheng0214 zsicheng0214 commented Nov 19, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #64851

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Emits synthetic and completed compaction session messages in opencode run --format json (packages/cli/src/run/noninteractive.ts), keeping default human output unchanged:

  • Handles session.synthetic and session.compaction.started / session.compaction.ended / session.compaction.failed in renderer.event so background <subagent> / <shell> completion notices (including description and metadata) and completed compactions (including reason, summary, cost, and tokens) stream live in JSON mode.
  • Reconciles message.type === "synthetic" and message.type === "compaction" (status === "completed") in renderer.reconcile, deduplicated by message ID via printed so messages seen live are not emitted twice.

How did you verify your code works?

Ran in packages/cli and repo root:

cd packages/cli
bun test test/run/noninteractive.test.ts
bun typecheck
bun run typecheck:tsc
cd ../..
bun x oxlint packages/cli/src/run/noninteractive.ts packages/cli/test/run/noninteractive.test.ts

Screenshots / recordings

N/A (CLI --format json stream handling)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Public API: adds synthetic and compaction event types to opencode run --format json output (additive, backwards compatible)

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 synthetic event appears only as the very last line, after the woken turn's step_finish. Its part.id is the projectedPartID(..., "synthetic") form, so it came from reconcile, 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") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@zsicheng0214

Copy link
Copy Markdown
Author

Thanks for catching the live inbox path! Updated noninteractive.ts to record synthetic items on session.inbox.enqueued (clearing on session.inbox.cancelled) and emit them live on session.inbox.delivered before the woken turn starts, keyed by inboxID so reconcile still deduplicates cleanly. Also updated the test to drive session.inbox.enqueued + session.inbox.delivered and verify synthetic arrives before the woken reply's text.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@r266-tech

Copy link
Copy Markdown
Contributor

All 25 noninteractive.test.ts cases pass on 0d3b3d93. I used the same mocked SDK to cover a report that was already queued before run --continue subscribed. The stream sees session.inbox.delivered, then the woken step and text. The final projection has the notice before that reply.

The JSON order was step_start, text, synthetic. Without the live enqueue event, the renderer emits the notice only during final reconciliation. This ordering can matter on an existing session because the run's steer prompt can be promoted ahead of an older queued report. If existing-session reports are in scope for live ordering, this is a useful regression case.

…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.
@zsicheng0214

Copy link
Copy Markdown
Author

Great catch, @r266-tech — thank you for testing that scenario!

Addressed in b02c0123: when session.inbox.delivered fires in --format json for an inboxID other than this run's prompt and session.inbox.enqueued wasn't observed live (because the report was already queued before run --continue subscribed), renderer now falls back to looking up the projected message and emits synthetic right away before the woken turn's step_start and text events (still deduplicated on reconcile, and with zero extra calls in default mode or when enqueued was seen live). Also added the existing-session regression test to noninteractive.test.ts (26/26 pass).

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cli(run): --format json omits synthetic background completion notices and completed compactions

2 participants