Skip to content
Source
Sign inSign up
pingdotgg/t3code

feat(orchestrator): archive and delete owned subagent subtrees #3878

ClosedPixPMusic(GitHub profile, opens in a new tab)wants to merge 1 commit intot3code/codex-turn-mapping fromrefs/pull/3878/headopened View on GitHub

Applying archive, unarchive, or delete to a thread now cascades through the subagent threads it recursively owns, entirely server-side:

  • OwnedSubagentTree walks the recursively owned subtree (subagent lineage only — forks and context transfers are not ownership edges; malformed cycles are bounded).
  • The orchestrator intercepts thread.archive / thread.unarchive / thread.delete, applies the lifecycle transition to every subtree member with a single timestamp (so "archived together" is exact), and cancels active runs and pending attempts across the subtree.
  • Unarchiving a root restores only the members archived together with it; independently archived descendants stay archived.
  • OwnedSubagentLifecycleRepair repairs half-applied cascades on startup.
  • CheckpointCleanupService removes checkpoints orphaned by deleted threads.

Server-only: no client, web, or mobile changes. The sidebar (#3861) and mobile (#3871) UI work layer on top of this behavior and should land after it.

All 282 orchestration-v2 tests pass (276 passed, 6 pre-existing skips), including new coverage for the cascade, repair, cleanup, turn-start gating on archived/deleted threads, and foundation persistence.


Note

High Risk Large orchestration-v2 changes to thread lifecycle, event commit guards, effect cancellation, and checkpoint deletion—areas where races or incomplete cascades could leave inconsistent state or drop in-flight provider work.

Overview Thread archive, unarchive, and delete now drive a server-side cascade over recursively owned subagent threads (subagent lineage only—not forks), using a shared archive timestamp so “archived together” is exact and unarchive restores only that cohort. The orchestrator cancels active work across the subtree, detaches sessions, enqueues terminal/attachment cleanup, and on delete adds checkpoint.cleanup via new CheckpointCleanupService and deleteScopeRefs (including deterministic ordinals through interrupted baselines).

Concurrency and repair: dispatch is serialized under a root-thread graph lock; archive/delete cancel process-bound (and capture) effects across all affected thread IDs. OwnedSubagentLifecycleRepair plus getThreadLifecycleRecords reconcile inconsistent parent/child lifecycle on startup and when new subagent threads appear under archived/deleted parents.

Persistence races: writeIfRunCurrent rejects events for archived/deleted threads (including routed child-thread events), accepts multiple expected run statuses, and optionally admits terminal artifacts from superseded attempts. Provider turn start and run execution detach or gate when lifecycle wins; delegated-task and subagent finalization skip inactive parents.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 225ba2d994ea5bedeb5cc74922731afdca96fdb5. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Archive and delete owned subagent subtrees with cascading lifecycle propagation

  • Thread archive, unarchive, and delete commands now propagate through the entire owned subagent subtree (via BFS traversal in OwnedSubagentTree.ts), cancelling runs, attempts, nodes, provider turns, and effects for all affected threads.
  • A new CheckpointCleanupService deletes checkpoint refs for each scope in a thread up to the latest known ordinal; it is enqueued as a checkpoint.cleanup effect during archive/delete.
  • writeIfRunCurrent in EventSink.ts now rejects writes if any referenced thread is archived/deleted, and can accept multiple expected run statuses and terminal artifacts for superseded attempts.
  • On startup and on new thread.created events, the orchestrator runs lifecycle repair via OwnedSubagentLifecycleRepair.ts to align child threads whose lifecycle disagrees with an archived/deleted parent.
  • All command dispatches and startNextQueuedRun are now serialized by a graph-level lock keyed on rootThreadId, preventing races across related threads.
  • Risk: archiving or deleting any thread now cascades to its entire owned subagent subtree and cancels all associated in-flight effects, which is a significant behavioral change for multi-thread agent graphs.
📊 Macroscope summarized 225ba2d. 13 files reviewed, 0 issues evaluated, 0 issues filtered, 0 comments posted

🗂️ Filtered Issues

No issues evaluated.

    1. Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro

      Run ID: 0078d2e8-d8e2-4542-a264-f2bc45bd60e5

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. Folded into #3861 — the server-side cascade belongs with the first PR in the sequence (no mobile code there); mobile UI follows in #3871.

    4. @@ -78,3 +79,8 @@
      7879 if (run === undefined) {
      7980 return yield* new ProviderTurnStartError({ runId, cause: `Run ${runId} was not found.` });
      8081 }
      82+ const providerThread = projection.providerThreads.find(
      83+ (candidate) => candidate.id === run.providerThreadId,
      84+ );
      85+ if (projection.thread.archivedAt !== null || projection.thread.deletedAt !== null) {
      86+ if (
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium orchestration-v2/ProviderTurnStartService.ts:86

      The early archived/deleted fast path detaches the provider session based on the initially-read projection, without revalidating before the call. If the thread is unarchived and a new turn reattaches the same providerSessionId before this outbox effect runs, providerSessions.detach removes the now-current attachment and may release the single-thread session, disrupting the in-flight turn. Consider rechecking the thread lifecycle and current attachment state transactionally before detaching, or conditioning detachment on a lifecycle generation so it only applies to the archived/deleted state it was scheduled for.

      🤖 Copy this AI Prompt to have your agent fix this:
      In file @apps/server/src/orchestration-v2/ProviderTurnStartService.ts around line 86:
      
      The early archived/deleted fast path detaches the provider session based on the initially-read projection, without revalidating before the call. If the thread is unarchived and a new turn reattaches the same `providerSessionId` before this outbox effect runs, `providerSessions.detach` removes the now-current attachment and may release the single-thread session, disrupting the in-flight turn. Consider rechecking the thread lifecycle and current attachment state transactionally before detaching, or conditioning detachment on a lifecycle generation so it only applies to the archived/deleted state it was scheduled for.
      @@ -648,13 +653,4 @@
      648653 runId: input.run.id,
      649654 nodeId: input.rootNode.id,
      650655 event,
      651- ...(event.type === "provider_thread.updated" &&
      652- event.providerThread.id === input.providerThread.id
      653- ? {
      654- writeIfRunCurrent: {
      655- runId: input.run.id,
      656- activeAttemptId: input.attempt.id,
      657- expectedStatus: "running" as const,
      658- },
      659- }
      660- : {}),
      656+ writeIfRunCurrent: {
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium orchestration-v2/RunExecutionService.ts:656

      expectedStatus: ["running", "waiting"] is applied to every routed provider event, not just child-drain events. When a child keeps ingestion open after the root turn.terminal, a late mutable root event arriving after the run has moved to waiting is still committed and can overwrite terminal root provider state — for example, reverting the provider turn back to running. The waiting admission should be restricted to post-terminal child artifacts only, not applied unconditionally to all events.

      🤖 Copy this AI Prompt to have your agent fix this:
      In file @apps/server/src/orchestration-v2/RunExecutionService.ts around line 656:
      
      `expectedStatus: ["running", "waiting"]` is applied to every routed provider event, not just child-drain events. When a child keeps ingestion open after the root `turn.terminal`, a late mutable root event arriving after the run has moved to `waiting` is still committed and can overwrite terminal root provider state — for example, reverting the provider turn back to `running`. The `waiting` admission should be restricted to post-terminal child artifacts only, not applied unconditionally to all events.
      @@ -0,0 +27,21 @@
      27+>()("t3/orchestration-v2/CheckpointCleanupService/CheckpointCleanupServiceV2") {}
      28+
      29+export const layer: Layer.Layer<
      30+ CheckpointCleanupServiceV2,
      31+ never,
      32+ CheckpointServiceV2 | ProjectionStoreV2
      33+> = Layer.effect(
      34+ CheckpointCleanupServiceV2,
      35+ Effect.gen(function* () {
      36+ const checkpoints = yield* CheckpointServiceV2;
      37+ const projections = yield* ProjectionStoreV2;
      38+
      39+ const cleanup = Effect.fn("orchestrationV2.checkpointCleanup.cleanup")(function* (
      40+ threadId: ThreadId,
      41+ ) {
      42+ const projection = yield* projections.getThreadProjection(threadId);
      43+ const throughOrdinal = Math.max(
      44+ 0,
      45+ ...projection.runs.map((run) => run.ordinal),
      46+ ...projection.checkpoints.map((checkpoint) => checkpoint.ordinalWithinScope),
      47+ );
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium orchestration-v2/CheckpointCleanupService.ts:43

      cleanup computes throughOrdinal by spreading every run and checkpoint ordinal into Math.max, so a thread with enough runs and checkpoints exceeds the JavaScript engine's function-argument limit and throws RangeError before any scope refs are removed. Compute the maximum iteratively instead of spreading the arrays into Math.max.

      Suggested change
      43- const throughOrdinal = Math.max(
      44- 0,
      45- ...projection.runs.map((run) => run.ordinal),
      46- ...projection.checkpoints.map((checkpoint) => checkpoint.ordinalWithinScope),
      47- );
      43+ const throughOrdinal = [
      44+ ...projection.runs.map((run) => run.ordinal),
      45+ ...projection.checkpoints.map((checkpoint) => checkpoint.ordinalWithinScope),
      46+ 0,
      47+ ].reduce((a, b) => Math.max(a, b), 0);
      🤖 Copy this AI Prompt to have your agent fix this:
      In file @apps/server/src/orchestration-v2/CheckpointCleanupService.ts around lines 43-47:
      
      `cleanup` computes `throughOrdinal` by spreading every run and checkpoint ordinal into `Math.max`, so a thread with enough runs and checkpoints exceeds the JavaScript engine's function-argument limit and throws `RangeError` before any scope refs are removed. Compute the maximum iteratively instead of spreading the arrays into `Math.max`.
    5. Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot). Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 225ba2d994ea5bedeb5cc74922731afdca96fdb5. Configure [here](https://www.cursor.com/dashboard/bugbot).
      @@ -781,0 +859,21 @@
      859+ command: Extract<
      860+ OrchestrationV2Command,
      861+ { readonly type: "thread.archive" | "thread.unarchive" | "thread.delete" }
      862+ >,
      863+ rootProjection: OrchestrationV2ThreadProjection,
      864+ events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
      865+ effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
      866+ ) {
      867+ const shell = yield* projectionStore.getShellSnapshot().pipe(
      868+ Effect.mapError(
      869+ (cause) =>
      870+ new OrchestratorProjectionError({
      871+ threadId: command.threadId,
      872+ cause,
      873+ }),
      874+ ),
      875+ );
      876+ const descendants = ownedSubagentDescendants(command.threadId, [
      877+ ...shell.threads,
      878+ ...shell.archivedThreads,
      879+ ]);

      Delete cascade misses shell gaps

      Medium Severity

      dispatchOwnedThreadLifecycle builds the owned subtree only from getShellSnapshot() threads, which omit deleted rows. If an intermediate owned subagent was already deleted, the walk cannot reach deeper descendants, so a later thread.delete on an ancestor may leave active grandchild threads undeleted and off the affectedThreadIds list used to cancel process-bound effects. planOwnedSubagentLifecycleRepairs runs only at orchestrator startup, so those orphans can keep running until restart.

      Additional Locations (2)
      Fix in Cursor Fix in Web
      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 225ba2d994ea5bedeb5cc74922731afdca96fdb5. Configure [here](https://www.cursor.com/dashboard/bugbot).
    6. Approvability

      Verdict: Needs human review

      3 blocking correctness issues found. This PR introduces a significant new feature for cascading archive/delete operations across owned subagent subtrees. The changes involve substantial new orchestration logic, new services, and modifications to core event handling. Four unresolved medium-severity review comments identify potential correctness issues including race conditions and orphaned thread scenarios.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).

Sign in to join this conversation.