Skip to content

feat(codemode): agent() children should not notify the parent or leave background jobs running #66691

Backlog

Description

@hearnadam

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: drain in packages/core/src/tool/plugin/codemode-agents.ts prompts with resume: false, awaits sessions.resume(childID), and never looks at the child's outstanding background jobs. On interrupt it only calls sessions.interrupt(childID); the child's jobs are not cancelled.
  • Every child with a parentID reports to its parent when it settles. The guard in packages/core/src/session/execution/subagent-report.ts exempts Automation.isRunSession children but not children carrying the codemodeAgent marker.
  • packages/core/src/session/compaction-subagents.ts has the same automation-only filter, so finished agent() children take up the idle subagent slots in compaction checkpoints.
  • packages/core/src/tool/plugin/shell.ts moves a foreground command to the background after the default 2 minute timeout unless the session is an automation run (timeoutBackgrounds). An agent() child can therefore end up with a background job it never asked for, and the job's completion wakes the child (sessions.synthetic without resume: 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

  1. Do not report codemodeAgent children to the parent session: extend the guard in subagent-report.ts and the filter in compaction-subagents.ts the way automation run children are handled. The script already has the result.
  2. Do not move foreground commands to the background on timeout inside agent() children; fail with a timeout as automation runs do. Explicit background: true keeps working.
  3. 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 as session-interrupt.ts).
  4. On restart, do not reattach shell jobs owned by codemodeAgent children whose script is gone.
  5. Tests in packages/core/test/codemode-agents-e2e.test.ts: the parent gets zero synthetic messages and zero extra wakes after N agent() calls; a background shell started by a child is cancelled when agent() 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() with send / jobs / wait / close) where the script, not a job completion, resumes the child.
  • UI: one execute row with running/done/failed counts and links to the children, instead of a row per child.

Activity

  1. opencodejr commented on Nov 26, 2026

    @opencodejr
    Contributor

    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 on dev (7290a57), each gap you list is there:

    • subagent-report.ts prepare: the quiet path covers only sessions without a parent or Automation.isRunSession. agent() children have a parentID and the codemodeAgent marker, so they report to and wake the parent each time they settle.
    • compaction-subagents.ts: the same automation-only filter, so finished agent() children stay in checkpoint subagent entries.
    • shell.ts: timeoutBackgrounds = !background && context.automation === undefined is true for agent() children, so a foreground command that times out is moved to the background.
    • codemode-agents.ts drain: it awaits sessions.resume(childID) and on interrupt only calls sessions.interrupt(childID). Nothing checks or cancels the child's background jobs or descendants, so agent() can resolve while a child's command is still running.
    • For schema children, the structured_output reminder is sent at the first idle, even when that idle was only a wait on a backgrounded command.
  2. added
    status/repro-plausible
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/repro-plausible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions