Skip to content

Instantly share code, notes, and snippets.

@Pyseph
Created November 5, 2026 12:54
Show Gist options
  • Select an option

  • Save Pyseph/3a6801b14d2909a7ba8b01aab9173cf0 to your computer and use it in GitHub Desktop.

Select an option

Save Pyseph/3a6801b14d2909a7ba8b01aab9173cf0 to your computer and use it in GitHub Desktop.
t3code: finished delegated sessions, measurement notes for the release-on-completion fix

RAM-26413: release a finished delegated child's provider session (worker win-3ee5c4c, V scope)

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.

Candidate: release a completed delegate's session at once (ranked 1)

  • 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.

Trace: why the child's session stays alive

  • delegated_task.request (Orchestrator.ts dispatchDelegatedTaskRequest, ~7716) creates an ordinary child thread (relationship subagent, forkedFrom the task node) and dispatches its first message with start_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 calls finalizeAppOwnedSubagent -> finalizeAppOwnedSubagentTask (~12434). That writes the task result, the subagent_result context 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.settle queues provider-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.terminal calls markIdle and reschedules the same 30 minute timer, so nothing distinguishes a finished delegate from an idle ordinary thread.
  • delegatedTaskProgress (provider-core subagentProjection.ts) already defines "result available, nothing active, no children, no background work", which is the "nothing can resume it" predicate reused here.

Change

  • Orchestrator.ts: finalizeAppOwnedSubagentTask passes a provider-session.release-finished-delegate effect (threadId = child, only for a completed outcome) to the same writeSystemEvents transaction that records the result. Effect id derives from the child run id, so it is idempotent.
  • EffectOutbox.ts: new request type, listed in PROCESS_BOUND_EFFECT_TYPES (cancelled after process loss, since the session is gone).
  • EffectWorker.ts: dispatches it to providerSessions.releaseFinishedDelegate.
  • ProviderSessionManager.ts: new releaseFinishedDelegate({ threadId }). Considers only sessions with supportsMultipleProviderThreads === false attached to exactly this thread; requires thread relationship subagent and delegatedTaskProgress(...).state === "result_available" (unreadable thread counts as not finished); skips on busy turns or adapter hasPendingBackgroundWork (wake messages, running native subagents; probe failure counts as pending); releases via releaseEntry({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, open creates a new one (test: openCount 2).
  • Three test fakes of ProviderSessionManagerV2Shape gained releaseFinishedDelegate: () => Effect.void.

Tests

  • Added in ProviderSessionManager.test.ts: release at once with a 60 min idle timeout and no clock advance, projected status stopped, 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 now provideMerges 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 --noEmit clean; vp lint and vp fmt --check on 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.

Measurements (fake provider, real stand-in processes)

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).

Remaining behaviour differences

  • 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.

Too small to pursue

  • Failed/cancelled delegate release: rarer and the user may be about to inspect or continue them.

Coverage and gaps

  • 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 -rf once; owner.txt was 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-26413 with its node_modules (holds the commit, no push), and the small files in RAM\26413. Removed: scratch test from the worktree, stash entries popped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment