Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): finished delegated tasks release their provider session #25288

Closedonly21mil(GitHub profile, opens in a new tab)wants to merge 12 commits intomain fromrefs/pull/25288/headopened View on GitHub

Problem

A delegate_task child keeps its provider session open after its task completes or fails. Nothing on the completion path releases it, and child threads are rarely settled, so the session waits out the 30-minute idle timer. For Claude and OpenCode that is one live process per finished child. On one busy machine, 30 of 47 agent processes were finished children, holding 7.1 GiB.

Change

When the orchestrator finalizes a delegated task that ended completed or failed, it dispatches the existing provider-session.detach command for each live session bound to the child thread, with the reason "Delegated task finished." A single-thread session closes its process. A shared one unloads the child's thread (Codex sends thread/unsubscribe).

  • The detach takes the child's lock inside finalize's parent lock, the order follow-up intake uses.
  • It skips the child while a run is preparing, starting, running or waiting, or a queued run is free to start, so a follow-up that just started keeps its session. Queued runs that a provider failure held do not count.
  • A monitor wake free to start or running when the task finishes keeps the session; a held wake does not, and its later start reattaches the session. When a kept wake ends, finalize retries only the release, not the result. A later wake reattaches the session, and its end releases it again, even when the wake fails before its turn starts.
  • A cancelled or interrupted task keeps its session until the idle timer, even after a later run on the child completes. After a follow-up task that left the queue, that follow-up's outcome decides; one cancelled before it started does not count.
  • Other threads keep the idle timer.

A later follow-up resumes the child's native thread, as after an idle release today. The delegated_task_status Codex replay fixture now records an unsubscribe after the first task, then a thread/resume before the follow-up.

On web and desktop, a child's Lineage section shows a "More thread actions" menu with "Disconnect agent session" while the child holds a live session. The menu now disappears when the task completes or fails, not 30 minutes later at the idle release. Mobile has no such menu.

Base web: the completed mock child shows its response and a Lineage actions control.

Base web: the completed mock child shows its response and a Lineage actions control

Base web: the completed mock child’s Lineage menu offers Disconnect agent session.

Base web: the completed mock child’s Lineage menu offers Disconnect agent session

Base desktop: the completed mock child shows its response and a Lineage actions control.

Base desktop: the completed mock child shows its response and a Lineage actions control

Base desktop: the completed mock child’s Lineage menu offers Disconnect agent session.

Base desktop: the completed mock child’s Lineage menu offers Disconnect agent session

Candidate web: the completed mock child shows its response and no Lineage actions control.

Candidate web: the completed mock child shows its response and no Lineage actions control

Candidate desktop: the completed mock child shows its response and no Lineage actions control.

Candidate desktop: the completed mock child shows its response and no Lineage actions control

Candidate web: the failed mock child has no Lineage actions control.

Candidate web: the failed mock child has no Lineage actions control

The screenshots above come from an earlier commit and show only the first finish. The four below were captured on this commit and show later runs. They use a scheduled run as the later non-task run, since the mock provider emits no monitor wakes; the monitor case is covered by the release table.

Web, this branch: after a held run resumes on the released session and completes, the child's Lineage has no actions control.

Web, this branch: after a held run resumes on the released session and completes, the child's Lineage has no actions control

Web, this branch: after a stopped follow-up and a later completed run, the child keeps its session and Lineage offers Disconnect agent session.

Web, this branch: after a stopped follow-up and a later completed run, the child keeps its session and Lineage offers Disconnect agent session

Desktop, this branch: after a held run resumes on the released session and completes, the child's Lineage has no actions control.

Desktop, this branch: after a held run resumes on the released session and completes, the child's Lineage has no actions control

Desktop, this branch: after a stopped follow-up and a later completed run, the child keeps its session and Lineage offers Disconnect agent session.

Desktop, this branch: after a stopped follow-up and a later completed run, the child keeps its session and Lineage offers Disconnect agent session

Scope and approval

No issue or maintainer discussion yet; the fix is small. It reuses the provider-session.detach command that settle and archive use and changes no contract, setting or timer. Behavior for maintainers to confirm: a finished delegated child's session is released at once on completed or failed, with no grace period. The release does not check for background work. A background command still running when a task finishes stops with the session: after any follow-up task, and for Codex children after the first task too, since Codex does not report background commands to the task's progress. The parent then gets no wake when that command would have finished. A grace period or a background-work check would be a small change on top if you prefer one; #21877 adds that check for follow-ups.

Verification

  • vp test run src/orchestration-v2/Orchestrator.control-reads.test.ts -t "releases the session": 16 table cases with a fake single-thread adapter, the real session manager and worker drains; the clock moves only past start retries. Released: a completed task, a failed one, one with a held queue, monitor wakes (running at the finish, a second wake, a held wake resumed later, with or without a cancelled message behind it, a wake that fails before its turn starts, a wake that fails with a follow-up behind it), and a held follow-up cancelled before a later run completes. Kept live: an interrupted task, alone and after a later run completes; an interrupted or cancelled follow-up after a later run completes; a top-level run; and a follow-up that just started.
    • On main: 10 cases fail at isLive and the monitor and monitor again cases time out before the first release. 4 pass.
    • On this branch: all 16 pass. Reverting any one guard fails only the cases it covers.

The release table with main's Orchestrator.ts: 12 of 16 cases fail, monitor and monitor again by timeout.

The release table with main's Orchestrator.ts: 12 of 16 cases fail

The release table on this branch: all 16 cases pass.

The release table on this branch: all 16 cases pass

  • The 10 server suites that finalize delegated tasks, ThreadStop and RestartContinuation among them: 207 tests pass.
  • With the old fixture, OrchestratorMcpToolkit.integration fails: the replay expected turn/start and received thread/unsubscribe.
  • vp run typecheck in apps/server, plus vp lint and vp fmt --check on the changed files: clean.

Not checked:

  • A real Claude, Codex or OpenCode process end to end. The tests use a fake adapter and a constructed Codex replay.
  • A monitor wake in the app. The fresh captures use a scheduled run in its place, since the mock provider emits no monitor wakes.

Prior PRs

None make this fix. Nearby work addresses different causes: #20048 by Ben Davis unloads Codex native subagent subtrees; #20361 by Andrea Simone Costa keeps child subscriptions when a parent settles; #19665 by Michael Huo settles child threads after their parent settles. #21877 by Scott Norteman, still open, makes a follow-up task wait for its nested work and the child's background tasks before it finishes. It edits the same spot in finalizeAppOwnedSubagentTask, so whichever PR lands second needs a small rebase. With it, a follow-up's release also waits for that work.

Production fix, tests, test images and PR description written with Claude Opus 5.5; first-finish screenshots captured with GPT-6.1 Sol and later-run screenshots with Claude Opus 5.5, all in T3 Code Nightly.

🤖 Generated with Claude Opus 5.5 and GPT-6.1 Sol in T3 Code Nightly

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    2. Approvability

      Verdict: Needs Human Approval

      Macroscope's review found this PR requires human approval — This changes delegated-task finalization to detach provider sessions and potentially stop or unload provider runtime resources, with nuanced handling for active runs, queued work, monitor wakes, and follow-ups. The production lifecycle logic is sufficiently complex and behaviorally significant to warrant human review.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. Review in Change Stack →

      No actionable comments were generated in the recent review.

      Recent review info
      Run configuration
      Commits

      Reviewing files that changed from the base of the PR and between 0a3b561bc0901d8fa47e430fc9d8593918cde978 and 8aa6d84ead7261458481a318d30f3f2ed61308f5.

      Files selected for processing: 3
      • apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
      • apps/server/src/orchestration-v2/Orchestrator.ts
      • apps/server/src/orchestration-v2/testkit/fixtures/delegated_task_status/codex_transcript.ndjson

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

      Walkthrough

      The orchestrator now selects the relevant delegated task outcome before finalization. It releases provider sessions only for eligible outcomes when queued work does not block release. New tests cover outcome selection, queue states, recovery, and session reattachment.

      Changes: Delegated task session release

      Layer / File(s)Summary
      Finalized task outcome
      apps/server/src/orchestration-v2/Orchestrator.ts
      Finalization handles tasks whose result transfer already exists without creating another transfer. It retains the existing task status unless a qualifying delegated follow-up determines the status.
      Provider-session release and validation
      apps/server/src/orchestration-v2/Orchestrator.ts, apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts, apps/server/src/orchestration-v2/testkit/fixtures/delegated_task_status/codex_transcript.ndjson
      The orchestrator checks task outcomes and queued runs before detaching live provider sessions. The tests cover completion, failure, interruption, queue states, retries, and recovery. The transcript fixture adds active and interrupted follow-up turns.
      Priority: Normal

      Estimated code review effort: 4 (Complex) | ~45 minutes

      Change: Bug fix

      Suggested labels: bugfix, size:L

      Suggested reviewers: t3dotgg, macroscopeapp

      Merge Risk: Minimal · up to 8aa6d

      Finished delegated tasks now release their provider session instead of holding it until the idle timeout, and sessions stay attached while follow-up work is running or about to run. No concrete merge-blocking issue was found. The deferred background-work check and the lack of testing with real providers are worth knowing about but do not block merging.

      Pre-merge checks: Passed 4
      Check nameStatusExplanation
      Title checkPassedThe title clearly and concisely describes the main server change: releasing provider sessions for finished delegated tasks.
      Description checkPassedThe description covers the problem, implementation, scope, approval rationale, verification results, screenshots, limitations, and related work. It clearly reports passing tests and unchecked real-pro…
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Full details: Description check

      Explanation

      The description covers the problem, implementation, scope, approval rationale, verification results, screenshots, limitations, and related work. It clearly reports passing tests and unchecked real-provider and monitor-wake scenarios.

      Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Comment `@coderabbitai help` to get the list of available commands.
    4. closing at @StiensWout's request under Establish the problem and scope first.

      Changes delegated-task completion from the existing idle-release policy to immediate session detachment. The author explicitly notes this can stop still-running background commands and prevent their parent wake, without any issue or maintainer discussion. That is an intentional lifecycle tradeoff requiring prior direction.

      Discuss the child-session lifetime and background-work behavior with maintainers and link explicit approval before implementing that policy.

      thanks for trying to help improve t3 code. if you think this closure was a mistake, please open a fresh pr addressing the requirement above, and link back to this one.

Sign in to join this conversation.