fix(cli): emit user prompt as a user event in run --format json#29998
Open
Kurry wants to merge 1 commit into
Open
fix(cli): emit user prompt as a user event in run --format json#29998Kurry wants to merge 1 commit into
Kurry wants to merge 1 commit into
Conversation
`opencode run --format json` began the event stream at step_start and never emitted the prompt itself, so anything rebuilding a transcript from the stream lost the user turn — it was only recoverable via `opencode export`. The streamed message.part events only cover the assistant reply, and a user text part has no time.end, so the existing text handler skipped it. Emit the prompt as a `user` event (carrying the same parts passed to session.prompt) before the assistant turn begins. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
alexgshaw
pushed a commit
to harbor-framework/harbor
that referenced
this pull request
May 30, 2026
…jectory (#1759) OpenCode trajectories had no source="user" step: _convert_events_to_trajectory only emitted agent steps, so the prompt was missing (the docstring even claimed a user step was synthesised, but the code never added one). OpenCode's `run --format=json` stream omits the prompt entirely (anomalyco/opencode#29997); it is only recoverable via `opencode export`. Capture the rendered instruction in run() and prepend a source="user" step, preferring OpenCode's own `user` event when present (forward-compatible with anomalyco/opencode#29998) and falling back to the instruction otherwise. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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.
Issue for this PR
Closes #29997
Type of change
What does this PR do?
opencode run --format jsonnever emits the user's prompt. The stream begins atstep_start, so anything reconstructing a transcript from it (eval harnesses, loggers, trajectory exporters) loses the user turn.The prompt is recorded — it's just dropped from the stream. Take one session on
dev:run --format jsonomits the prompt, butopencode exportof that same session contains it.opencode run --format json -- say helloondev(the real format is one compact JSON object per line; pretty-printed here, IDs shortened):{ "type": "step_start", "sessionID": "ses_…", "part": { "id": "prt_…", "type": "step-start", "snapshot": "…" } } { "type": "text", "sessionID": "ses_…", "part": { "type": "text", "text": "Hello! How can I assist you today?", "time": { "start": 1780155478703, "end": 1780155478922 } } } { "type": "step_finish", "sessionID": "ses_…", "part": { "reason": "stop", "type": "step-finish", "tokens": { "total": 14759, "input": 14748, "output": 11 }, "cost": 0.0022188 } }The first event is the assistant's
step_start— the prompt is nowhere in the stream.opencode export <same session>ondev(excerpt) — the prompt is right there:{ "messages": [ { "info": { "role": "user" }, "parts": [{ "type": "text", "text": "say hello" }] }, { "info": { "role": "assistant" }, "parts": [{ "type": "text", "text": "Hello! How can I assist you today?" }] } ] }So the data exists; the
--format jsonstream just never emits it.Why the stream drops it. The event loop receives a
message.part.updatedfor every part, including the user's prompt, but thetextbranch only emits oncepart.time?.endis set:That gate is deliberate: assistant text streams in as several updates, so it waits for the finished one (the part that has a
time.end). But the user's prompt is input, not streamed generation — its text part arrives once with notimefield at all, sopart.time?.endisundefined, the branch is skipped, and nothing else in the loop handles it. The same guard that dedupes streaming assistant text silently drops the prompt. Logging every event the loop sees confirms it:The fix. Emit a
userevent carrying the same parts passed tosession.prompt, just before the assistant turn starts. It reuses the existingemit()helper (a no-op unless--format json), so default output is unchanged, and it's additive — consumers that don't know theusertype ignore it.With the fix,
opencode run --format json -- say helloproduces the full trajectory below — the newuserevent leads and the rest is identical todev:{ "type": "user", "timestamp": 1780155194554, "sessionID": "ses_1867b634affeNm9y7CZN9ojxPl", "parts": [{ "type": "text", "text": "say hello" }] } { "type": "step_start", "sessionID": "ses_…", "part": { "id": "prt_…", "type": "step-start", "snapshot": "…" } } { "type": "text", "sessionID": "ses_…", "part": { "type": "text", "text": "Hello! How can I assist you today?", "time": { "start": 1780155478703, "end": 1780155478922 } } } { "type": "step_finish", "sessionID": "ses_…", "part": { "reason": "stop", "type": "step-finish", "tokens": { "total": 14759, "input": 14748, "output": 11 }, "cost": 0.0022188 } }How did you verify your code works?
dev(same session):run --format jsonhas no user turn,exportdoes.test/cli/run/run-process.test.ts(drives the real CLI with--format json) asserting auserevent carries the prompt and precedes the assistanttext. It fails ondev(nouserevent →expect(user).toBeDefined()fails) and passes with the fix.bun test test/cli/run/run-process.test.ts→ 5 pass.bun typecheckclean; prettier + oxlint clean on the changed files.Screenshots / recordings
N/A — not a UI change.
Checklist