Issue: pingdotgg/t3code#26413 (open, no labels, no maintainer comment: NOT triaged). Related: #30148 (ordinary threads, not addressed), #25624 (open PR, configurable idle timeout), #16917, #15173.
Worktree: <SCRATCH>\t3code-26413, branch fix/release-finished-delegate-sessions from origin/main 8b44f42b14. Commit 5e79b4b632ebe70d87193252d913f4a21d4d3d45 (not pushed).
PR draft: ...\RAM\26413\PR.md. Raw measurement rows and the scratch measurement test: ...\RAM\26413\measure-main.jsonl, measure-branch.jsonl, measure26413.test.ts.txt.
- Size: +80 source lines in 4 files, 322 insertions / 2 deletions total with tests (9 files).
- Impact: removes up to 30 minutes of one claude process plus MCP children per finished delegate (0.86-0.94 GB each on this PC; #26413 saw 12 such sessions, about 5 GB).
- State: committed locally, tested, not pushed. Confidence: medium-high on correctness of the guards; real Claude adapter end to end is unmeasured.
- Upstream: needs a triage or maintainer approval first (see PR.md "Scope and approval"). Recommendation: comment on #26413 asking maintainers to confirm direction (release on completion versus a shorter delegate timeout versus #25624) before opening a PR. Not a "very small obvious bug" fix: it changes lifecycle behaviour and adds an outbox effect type.
delegated_task.request(Orchestrator.tsdispatchDelegatedTaskRequest, ~7716) creates an ordinary child thread (relationshipsubagent,forkedFromthe task node) and dispatches its first message withstart_immediately; the child gets its own provider session like any thread.- When the child's run reaches a terminal status,
handleRunUpdate(~13800) takes the parent lock and callsfinalizeAppOwnedSubagent->finalizeAppOwnedSubagentTask(~12434). That writes the task result, thesubagent_resultcontext transfer and the completion delivery to the parent. It never touches the child's provider session. Settle of the child is a separate, user or whole-day action (thread.settlequeuesprovider-session.detach). - The session is only released by the manager's idle timer (30 min, restarted by every provider event), plus detach paths. The turn's
turn.terminalcallsmarkIdleand reschedules the same 30 minute timer, so nothing distinguishes a finished delegate from an idle ordinary thread. delegatedTaskProgress(provider-coresubagentProjection.ts) already defines "result available, nothing active, no children, no background work", which is the "nothing can resume it" predicate reused here.
Orchestrator.ts:finalizeAppOwnedSubagentTaskpasses aprovider-session.release-finished-delegateeffect (threadId = child, only for acompletedoutcome) to the samewriteSystemEventstransaction that records the result. Effect id derives from the child run id, so it is idempotent.EffectOutbox.ts: new request type, listed inPROCESS_BOUND_EFFECT_TYPES(cancelled after process loss, since the session is gone).EffectWorker.ts: dispatches it toproviderSessions.releaseFinishedDelegate.ProviderSessionManager.ts: newreleaseFinishedDelegate({ threadId }). Considers only sessions withsupportsMultipleProviderThreads === falseattached to exactly this thread; requires thread relationshipsubagentanddelegatedTaskProgress(...).state === "result_available"(unreadable thread counts as not finished); skips on busy turns or adapterhasPendingBackgroundWork(wake messages, running native subagents; probe failure counts as pending); releases viareleaseEntry({reason:"idle_timeout", onlyIfIdleGeneration}), the generation taken before the probes. Anything else leaves the 30 minute timer in charge.- Follow-up: unchanged path; with the session gone,
opencreates a new one (test: openCount 2). - Three test fakes of
ProviderSessionManagerV2ShapegainedreleaseFinishedDelegate: () => Effect.void.
- Added in
ProviderSessionManager.test.ts: release at once with a 60 min idle timeout and no clock advance, projected statusstopped, follow-up reopens (1 test); kept for an ordinary thread, a still-running delegate, a delegate with a queued follow-up, a delegate with pending background work (4 cases). - Added in
DelegatedCompletionDelivery.test.ts(layer nowprovideMerges the in-memory SQL so the outbox can be read): completed child queues the effect, cancelled child does not. - Results (Node 26.12.1): 7 touched/related files, 212 tests pass; delegate integration files (Orchestrator.control-reads, RemoteDelegatedTask, ClaudeAutomaticDelivery.integration, BackgroundWorkStop.integration, SubagentProjection), 44 pass. Server
tsc --noEmitclean;vp lintandvp fmt --checkon the 9 changed files clean. - Fails on main (source stashed, tests kept): the release test and the orchestrator test. The four "keeps" tests also fail on main, but only with "not a function", so they prove nothing there.
Each fake session spawns an idle node process (about 51 MB; stand-in for claude.exe, so absolute MB is not Claude's). 6 delegated sessions with completed runs, 10 runs per variant (2 invocations x 5 reps, alternating main/branch), Windows 11.
| variant | alive before | alive after | total MB before -> after (median) | time until all released |
|---|---|---|---|---|
| main | 6 | 6 (1.5 s later, 10/10 runs) | 305.9 -> 305.9 | 30:00 on the virtual clock (6 alive at 29:59, 0 at 30:00) |
| branch | 6 | 0 | 303.6 -> 0 | median 20.5 ms, min 11, max 30 (call to all 6 exited) |
| Scale at this PC's 0.86-0.94 GB per session: 6 finished delegates = about 5.2-5.6 GB held up to 30 min on main. | ||||
Unmeasured: real Claude adapter and a real delegate_task end to end; outbox worker latency between result recorded and effect run; wall-clock cost of the next follow-up's session start (spawn plus MCP registration, up to the 90 s timeout). |
- Follow-ups within the old 30 min window now pay a session start instead of reusing a warm process.
- Failed, cancelled, interrupted delegates keep the 30 min behaviour.
- New persisted outbox effect type (older builds cannot decode a pending row after a downgrade).
- Ordinary threads (#30148) unchanged; composes with #25624.
- Failed/cancelled delegate release: rarer and the user may be about to inspect or continue them.
- Real Claude session start cost after release not measured. Shared-process providers (Codex) intentionally skipped. Not covered: delegates run by remote peers (
RemoteDelegation), which have no local child session. - Housekeeping: while releasing my bench.lock I ran
rmdir ... || rm -rfonce;owner.txtwas already gone and the directory was mine, but I cannot rule out that another agent had briefly taken the lock. Lock is released. - Kept: worktree
t3code-26413with its node_modules (holds the commit, no push), and the small files inRAM\26413. Removed: scratch test from the worktree, stash entries popped.