Repository navigation
Backlog
Labels
status/repro-plausibleLikely from reading the code, not observed
Description
Follow-up to #66278 (agent() and llm() in Code Mode scripts). Related: #66478, #66479, #66555.
Problem
A Code Mode script that calls agent() awaits each child itself, but the children still behave like subagent tool children toward the parent session. Observed in a script with 78 agent() calls (explore agents, schema output):
- The parent received a
<background-result kind="subagent">notice for every child, mostly "Subagent completed without a text response", and the transcript showed one "Explore finished" row per child. - 4 children returned their result while a shell command of theirs was still running.
agent()resolved, the script finished, and when those commands exited the children woke on their own and reported to the parent again. Two of those started new parent turns after the script had ended. - The children's commands outlived both the child run and the script.
Current state
agent()resolves at the child's first idle:draininpackages/core/src/tool/plugin/codemode-agents.tsprompts withresume: false, awaitssessions.resume(childID), and never looks at the child's outstanding background jobs. On interrupt it only callssessions.interrupt(childID); the child's jobs are not cancelled.- Every child with a
parentIDreports to its parent when it settles. The guard inpackages/core/src/session/execution/subagent-report.tsexemptsAutomation.isRunSessionchildren but not children carrying thecodemodeAgentmarker. packages/core/src/session/compaction-subagents.tshas the same automation-only filter, so finishedagent()children take up the idle subagent slots in compaction checkpoints.packages/core/src/tool/plugin/shell.tsmoves a foreground command to the background after the default 2 minute timeout unless the session is an automation run (timeoutBackgrounds). Anagent()child can therefore end up with a background job it never asked for, and the job's completion wakes the child (sessions.syntheticwithoutresume: false).- A schema child that goes idle without calling
structured_output(for example while waiting on that backgrounded command) is immediately sent "You ended without calling structured_output. Call it now", which pushes it to answer before the command finishes.
What's needed
- Do not report
codemodeAgentchildren to the parent session: extend the guard insubagent-report.tsand the filter incompaction-subagents.tsthe way automation run children are handled. The script already has the result. - Do not move foreground commands to the background on timeout inside
agent()children; fail with a timeout as automation runs do. Explicitbackground: truekeeps working. - Give one-shot
agent()structured concurrency: when the call returns, fails or is interrupted, cancel the child's jobs and stop its descendants (same cascade assession-interrupt.ts). - On restart, do not reattach shell jobs owned by
codemodeAgentchildren whose script is gone. - Tests in
packages/core/test/codemode-agents-e2e.test.ts: the parent gets zero synthetic messages and zero extra wakes after Nagent()calls; a background shell started by a child is cancelled whenagent()returns or the script is interrupted; a foreground command past its timeout fails instead of backgrounding.
All behind the existing codemode_agents flag. No script API change.
Out of scope, for a later issue
- Continuing an earlier child and keeping a long-running process alive across calls. Waiting for all of a child's background jobs is not an option, because a dev server never exits; this needs an explicit handle (for example
agent.open()withsend/jobs/wait/close) where the script, not a job completion, resumes the child. - UI: one
executerow with running/done/failed counts and links to the children, instead of a row per child.
Metadata
Metadata
Assignees
Labels
status/repro-plausibleLikely from reading the code, not observed
Activity
opencodejr commentedon Nov 26, 2026
Not observed; this looks likely from reading the code.
I couldn't run this.
agent()in Code Mode isn't in any release yet (it landed after v2.4.2), and I didn't have time to build a simulated nested Code Mode run from source. Reading the code ondev(7290a57), each gap you list is there:subagent-report.tsprepare: the quiet path covers only sessions without a parent orAutomation.isRunSession.agent()children have aparentIDand thecodemodeAgentmarker, so they report to and wake the parent each time they settle.compaction-subagents.ts: the same automation-only filter, so finishedagent()children stay in checkpoint subagent entries.shell.ts:timeoutBackgrounds = !background && context.automation === undefinedis true foragent()children, so a foreground command that times out is moved to the background.codemode-agents.tsdrain: it awaitssessions.resume(childID)and on interrupt only callssessions.interrupt(childID). Nothing checks or cancels the child's background jobs or descendants, soagent()can resolve while a child's command is still running.structured_outputreminder is sent at the first idle, even when that idle was only a wait on a backgrounded command.