run --format json omits every subagent event (still in 2.x, see #49300)
Description
opencode run --format json prints nothing a subagent does. Only events from the root session reach stdout: the subagent's shell calls, text and steps are dropped, and the root's subagent tool_use appears only once the subagent has finished. A headless consumer can't show progress, or see where a long subagent got stuck.
#49300 reported this against 1.18 and was closed for 2.0. It is still there in 2.2.1 and 2.2.3. packages/cli/src/run/noninteractive.ts skips every event whose sessionID isn't the root one (:236 at v2.2.1, :239 at v2.2.3), while ownsSession a few lines up already follows parentID so that subagent permission prompts are answered. On dev after #61911 the same check is at :172.
Plugins
none
OpenCode version
2.2.3 (same on 2.2.1)
Steps to reproduce
- Point the
openaiprovider at a stub Responses endpoint (OPENCODE_CONFIG_CONTENTwithbaseURL) and runopencode run --standalone --format json --auto --model openai/gpt-test -- "go"with an empty HOME. - The stub answers the root's first turn with a
subagentcall (agent: general). It answers the child's first turn withshellecho child-ran, and every turn that carries a tool result with plain text. - The stub receives the child's requests, and the child's shell result (
child-ran) comes back to it, so the subagent ran. - stdout has only root-session lines:
step_start,tool_use(subagent, whosemetadata.sessionIDnames the child),step_finish,step_start,text,step_finish. Nothing from the child session.
Operating System
Linux Mint 22.3 (Ubuntu 24.04 base), x64
Terminal
none (headless)
Downstream: alpha-omega-security/scrutineer#1318, where an 8-hour run timed out inside a subagent with no trace of what it was doing.
Thanks for the detailed report and for tracing it to the code.
This looks like a feature request rather than a regression. In v2, opencode run --format json only prints events from the root session, and that filter was kept on purpose when the noninteractive runner was rewritten in #61911. ownsSession follows parentID only so that subagent permission prompts get answered; it was never meant to include subagent output. So the request is: stream subagent (child-session) events in run --format json, marked with the session they come from, so headless consumers can show progress and see where a long subagent is stuck. Maintainers will consider it.
For now, if you need live subagent activity, you can run a server and subscribe to its event stream through the SDK. That stream includes child-session events, and each child session links back to its parent through parentID.
Related:
- #49300: the same report against v1.18, closed as part of the v1 cleanup
- #33396 / #33397: earlier requests to expose child-session events in the
opencode runNDJSON stream - #46685: telling child-session events apart from root-session ones (closed v1 report)
If the intent of the sessionID filter in packages/cli/src/run/noninteractive.ts is to avoid event duplication for headless consumers, why is the parentID logic used for permission prompts not extended to stream child events into the JSON output?
I'd like to work on this. Proposal:
- Behind an opt-in flag, also emit subagent events, tagged with their
sessionIDandparentID. - At the end, emit one
resultevent with total tokens and cost across all sessions, plus step count, duration and outcome.
Existing output doesn't change. Does this direction work, and should I build on #60508? Happy to be assigned.