feat(orchestration-v2): attribute reused subagents to the run driving them (2/4) #4662
Closedshivamhwp(GitHub profile, opens in a new tab)wants to merge 8 commits into
subagent-obs/01-contracts fromsubagent-obs/03-reuseopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/scripts/record-codex-app-server-replay-fixture.ts line 469OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -443,3 +449,19 @@443449 },444450 ],445451 },452+ {453+ name: "subagent_reuse_after_interrupt",454+ fileName: "subagent_reuse_after_interrupt.ndjson",455+ description:456+ "A root turn spawns a native Codex subagent and is interrupted while it works, then a later root turn re-activates the same child thread.",457+ runs: [458+ {459+ name: "reuse-after-interrupt",460+ description:461+ "Interrupt the spawning turn mid-subagent, then message the same subagent from a new root turn.",462+ turnDefaults: {463+ approvalPolicy: "never",464+ sandboxPolicy: workspaceWriteSandbox(),465+ },466+ steps: [467+ {
- apps/server/src/orchestration-v2/ProviderTurnStartService.ts line 434OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -408,0 +414,21 @@414+ // Broader than the child-thread list above: an interrupted agent is still415+ // resumable, so it stays re-activatable even though its thread must not416+ // be adopted up front.417+ const reactivatableSubagents = projection.subagents.filter((subagent) =>418+ canReactivateSubagent(subagent.status),419+ );420+ // Adapters keep their subagent registry in memory, so it is empty after a421+ // session reap or a server restart. Hand back the reusable identities422+ // from the projection so a returning agent thread is recognised instead423+ // of being spawned again as a duplicate.424+ const existingSubagents = yield* Effect.forEach(425+ reactivatableSubagents,426+ (subagent, index): Effect.Effect<ReadonlyArray<ProviderAdapterV2ExistingSubagent>> =>427+ Effect.gen(function* () {428+ const childThreadId = subagent.childThreadId;429+ if (childThreadId === null) return [];430+ const turnItem = projection.turnItems.find(431+ (item) => item.type === "subagent" && item.subagentId === subagent.id,432+ );433+ if (turnItem === undefined) return [];434+ const childProjection = yield* projectionStore
- apps/server/src/orchestration-v2/ProviderTurnStartService.ts lines 436 to 438OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -408,0 +418,21 @@418+ );419+ // Adapters keep their subagent registry in memory, so it is empty after a420+ // session reap or a server restart. Hand back the reusable identities421+ // from the projection so a returning agent thread is recognised instead422+ // of being spawned again as a duplicate.423+ const existingSubagents = yield* Effect.forEach(424+ reactivatableSubagents,425+ (subagent, index) =>426+ Effect.gen(function* () {427+ const childThreadId = subagent.childThreadId;428+ if (childThreadId === null) return [];429+ const turnItem = projection.turnItems.find(430+ (item) => item.type === "subagent" && item.subagentId === subagent.id,431+ );432+ if (turnItem === undefined) return [];433+ const childProjection = yield* projectionStore434+ .getThreadProjection(childThreadId)435+ .pipe(436+ Effect.catchIf(Schema.is(ProjectionStoreThreadNotFoundError), () =>437+ Effect.succeed(null),438+ ),
- @@ -387,3 +389,18 @@387389 },388390 ],389391 },392+ {393+ name: "subagent_reuse_after_idle",394+ buildInput: subagentReuseAfterIdleInput,395+ providers: [396+ {397+ driver: ProviderDriverKind.make("codex"),398+ transcriptFile: new URL(399+ "./subagent_reuse_after_idle/codex_transcript.ndjson",400+ import.meta.url,401+ ),402+ modelSelection: CODEX_MODEL_SELECTION,403+ assertOutput: assertSubagentReuseAfterIdleOutput,404+ },405+ ],406+ },apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts line 1624Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1557,0 +1597,21 @@1597+ entry.subagent.status === "pending" ||1598+ entry.subagent.status === "running" ||1599+ entry.subagent.status === "waiting"1600+ ? "idle"1601+ : entry.subagent.status,1602+ currentActivationId: null,1603+ },1604+ });1605+ }1606+ return updated;1607+ });1608+ // Turn ordinals live in the same lost registry. Restarting them at1609+ // 1 would re-derive childRootNodeId for a different native turn, so1610+ // continue past the activations already recorded.1611+ yield* Ref.update(nextProviderTurnOrdinals, (current) => {1612+ const updated = new Map(current);1613+ for (const entry of existing) {1614+ const key = String(entry.childProviderThread.id);1615+ if (updated.has(key)) continue;1616+ updated.set(key, Math.max(1, entry.subagent.activationCount));1617+ }
- apps/server/src/orchestration-v2/RunExecutionService.ts line 464Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -359,5 +447,18 @@359447 }360448 return [true, addProviderThread(event.node.providerThreadId)];361449 }362- case "subagent.updated":363- return [ownsRun(event.subagent.runId) || ownsChildThread(event.subagent.threadId), state];450+ case "subagent.updated": {451+ const belongs =452+ ownsRun(event.subagent.runId) ||453+ ownsChildThread(event.subagent.threadId) ||454+ ownsSubagent(event.subagent.id);455+ return belongs456+ ? [457+ true,458+ addSubagent(459+ event.subagent.id,460+ event.subagent.childThreadId,461+ !isTerminalSubagentStatus(event.subagent.status),462+ ),463+ ]464+ : [false, state];
shivamhwp(GitHub profile, opens in a new tab) changed the title
- @@ -447,0 +474,21 @@474+ },475+ {476+ type: "turn",477+ label: "reuse-interrupted-subagent",478+ prompt: SUBAGENT_REUSE_AFTER_INTERRUPT_RESUME_PROMPT,479+ },480+ ],481+ },482+ ],483+ },484+ {485+ name: "subagent_reuse_after_idle",486+ fileName: "subagent_reuse_after_idle.ndjson",487+ description:488+ "A root turn spawns a native Codex subagent, then a later root turn re-activates it. Replay advances past the session idle timeout between turns so the adapter registry is empty on reuse.",489+ runs: [490+ {491+ name: "reuse-after-idle",492+ description:493+ "Second root turn messages the same subagent after the session would have been reaped.",494+ steps: [
shivamhwp(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.



Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
8c30b74b-f26f-4a50-85c4-3515e634a341You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
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