# Proof: subagents inherit their parent's lifecycle

Replacement for closed PR pingdotgg/t3code#20074, rebased onto `13f89ae0a7fcecbdcd06ac1551d5ff549bb76926`.

- Branch: `Quicksaver:fix/subagent-lifecycle-inheritance`, head `91555bd53d04c731b7c8fc4b52389ca03312202b`.
- Baseline `1cf5f5d3a5`: the squashed #20074 head `c5e91fb122` (against merge base `071239804e`), applied to the target without conflicts.
- Adaptation `91555bd53d`: one test adjusted for upstream's archive guard. Production code is unchanged from the #20074 head.
- Diff against the target: 305 additions, 5 deletions, in 4 server files. `source.patch` is the exact diff and `sha256.txt` has the file hashes.

Host: Windows 11, Node v24.15.0, Vite+ 1.4.0, pnpm 12.14.0. Everything below ran at the head above.

## What the change does

1. When a provider reports a new native subagent thread (`app_thread.created`), the child takes `archivedAt` from its persisted parent. It also takes `deletedAt` from the parent, unless the child already has its own. A restored parent therefore yields an active child. Metadata refreshes of an existing child, forks, and non-subagent threads keep their own lifecycle.
2. That write holds the parent's thread lock, then the child's. Archive and delete use the same order, so a concurrent archive or delete cannot interleave with the inheritance read. A lineage whose parent id equals its own id takes only the child lock.
3. `delegated_task.request` under an archived or deleted parent fails with `Parent thread <id> is archived.` (or `deleted.`) through the normal receipt path. Replaying the rejected command returns `OrchestratorCommandPreviouslyRejectedError`. Replaying an accepted command returns its original sequence and events.

## Failing first, then passing

`before.log` runs the branch's tests against the target's production `Orchestrator.ts` and `ProviderEventIngestor.ts`. 7 of 68 tests fail:

- both delegation rejection cases (archive and delete),
- the three lifecycle inheritance cases,
- the two archive/delete serialization races.

`after.log` runs the same two test files at the head. All 68 pass.

`control-orchestrator*.log` and `control-ingestor*.log` run the target's own copies of both test files on the same host. Both pass with exit 0.

## Why the archive test changed

Upstream now refuses `thread.archive` while an app-owned delegated task is running ("has running delegated tasks"). The #20074 test accepted a delegation and then archived the parent, which upstream now rejects. `after-focused.log` shows that failure on the unadapted baseline.

In the adapted test, only the delete case accepts a delegation before the lifecycle change and then checks that the accepted receipt replays unchanged. The archive case archives first. It then checks the rejection, unarchives, and checks that the rejected receipt still replays as rejected and that a fresh request is accepted. The receipt lookup runs before the lifecycle guard, so the delete case covers accepted replay for both states.

## Runtime transcript

`runtime-lifecycle.ts` boots the real server (`apps/server/src/bin.ts start`) on a fresh temporary T3 home with a fresh git project, over real HTTP and WebSocket. It authenticates with a one-off bootstrap token, which the log redacts. It launches a parent thread on the free `Replay (dev)` provider and waits for the parent run to be active. It archives the parent, then sends `delegated_task.request` through `orchestration.dispatchCommand`. The server is stopped and the temporary home deleted after each run.

Head (`runtime-after.log`):

```text
parent run run:thread:parent-72379c1d:ordinal:1 status=running
thread.archive parent -> accepted sequence=23
parent archivedAt=DateTime.Utc(2026-11-14T10:55:51.890Z) run status=running
delegated_task.request -> rejected ... "message":"Parent thread parent-72379c1d is archived." ...
parent subagents=0
```

Target production files swapped in (`runtime-before.log`):

```text
thread.archive parent -> accepted sequence=23
parent archivedAt=DateTime.Utc(2026-11-14T10:56:21.219Z) run status=running
delegated_task.request -> rejected ... "message":"No orchestration projection exists for thread thread:delegated-task:edfe1ee6-...." ...
```

Control at the head without archiving (`runtime-control-no-archive.log`): the same request fails with the same `No orchestration projection exists` error.

What this shows: in a running server, the new guard rejects the request before any other delegation work, with the lifecycle error. It does not show the target accepting a child under an archived parent. A client-dispatched `delegated_task.request` fails later on the target for an unrelated, pre-existing reason, with or without archiving. Agents normally delegate through MCP, which already refuses callers in archived threads (`OrchestratorMcpService` `notActive`). The orchestrator guard is the authoritative check for every other command source. The deterministic before/after for both paths is in the tests above.

The native subagent inheritance path needs a provider that reports native child threads. The replay provider does not, so that path is covered by the `ProviderEventIngestor` tests only.

## Checks

- `check-changed.log`: `vp run check:changed` passes format, lint, knip, graphql, typecheck (3 packages), and tests (7 files, 72 tests).
- `typecheck.log`: server typecheck, exit 0, zero errors. Its suggestions are on pre-existing lines.

## Review

- `magi-20261114T1038-7765bb-finish.json`: Magi review of the adaptation `1cf5f5d3a5..91555bd53d` by GPT 6 Astra, GPT 6.1 Sol, Claude Fable 5.2 and Claude Opus 5.5. Consensus on turn 1 at full weight, with no proposals.
- `coderabbit-adaptation.ndjson`: CodeRabbit on the same range, zero findings.

## Limits

- Server only. No web, desktop or mobile client behavior changes, so there is no integrated client pass.
- Windows only. No macOS or Linux run. The change is platform-independent server logic.
- The full test suites and the build did not run locally. CI runs them.
- No real provider CLI ran. The runtime transcript uses the replay provider.
