From d935c9cc625495434c2448094b59e16bcfad8825 Mon Sep 17 00:00:00 2001 From: Shivam Sharma <91240327+shivamhwp@users.noreply.github.com> Date: Sat, 1 Aug 2026 13:57:29 +0530 Subject: [PATCH] fix(orchestration-v2): target live subagent sessions Co-Authored-By: Claude Fable 5 --- .../Adapters/ClaudeAdapterV2.test.ts | 12 ++++ .../Adapters/ClaudeAdapterV2.ts | 7 ++- .../Orchestrator.subagentStop.test.ts | 57 +++++++++++++++++++ .../src/orchestration-v2/Orchestrator.ts | 45 +++++++++------ .../state/orchestrationV2Subagents.test.ts | 2 +- .../src/state/orchestrationV2Subagents.ts | 18 +++--- 6 files changed, 110 insertions(+), 31 deletions(-) create mode 100644 apps/server/src/orchestration-v2/Orchestrator.subagentStop.test.ts diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index c028f7b3e0..04ec45cf0f 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -1331,6 +1331,18 @@ describe("ClaudeAdapterV2 background wake turns", () => { }); const makeWakeHarness = makeWakeHarnessWithOptions(); + it.effect("treats a stop after query release as already complete", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + if (harness.runtime.stopTask === undefined) { + return yield* Effect.die("Claude adapter runtime must expose stopTask."); + } + yield* harness.runtime.stopTask({ nativeTaskId: WAKE_TASK_ID }); + }).pipe(Effect.provide(Layer.merge(idAllocatorLayer, NodeServices.layer))), + ), + ); + it.effect("projects API retries and resolves the same item after recovery", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 81cdb4225d..c5b1c47cbb 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -4484,10 +4484,11 @@ export function makeClaudeAdapterV2( }) { const existing = yield* Ref.get(queryContext); if (existing === null) { - return yield* new ProviderAdapterProtocolError({ - driver: CLAUDE_PROVIDER, - detail: `Claude provider session ${input.providerSessionId} has no live query to stop task ${stopInput.nativeTaskId}.`, + yield* Effect.logWarning("orchestration-v2.claude-stop-task-without-live-query", { + providerSessionId: input.providerSessionId, + nativeTaskId: stopInput.nativeTaskId, }); + return; } yield* existing.query.stopTask(stopInput.nativeTaskId).pipe( Effect.mapError( diff --git a/apps/server/src/orchestration-v2/Orchestrator.subagentStop.test.ts b/apps/server/src/orchestration-v2/Orchestrator.subagentStop.test.ts new file mode 100644 index 0000000000..fbd3b42a7c --- /dev/null +++ b/apps/server/src/orchestration-v2/Orchestrator.subagentStop.test.ts @@ -0,0 +1,57 @@ +import { ProviderSessionId, ProviderThreadId, RunId } from "@t3tools/contracts"; +import { assert, it } from "@effect/vitest"; + +import { providerSessionIdForSubagentStop } from "./Orchestrator.ts"; + +const runId = RunId.make("run-subagent-stop"); +const runThreadId = ProviderThreadId.make("provider-thread-run"); +const activeThreadId = ProviderThreadId.make("provider-thread-active"); +const staleThreadId = ProviderThreadId.make("provider-thread-stale"); +const runSessionId = ProviderSessionId.make("provider-session-run"); +const activeSessionId = ProviderSessionId.make("provider-session-active"); +const staleSessionId = ProviderSessionId.make("provider-session-stale"); + +it("prefers the run session when stopping a subagent", () => { + assert.equal( + providerSessionIdForSubagentStop({ + runId, + activeProviderThreadId: activeThreadId, + runs: [{ id: runId, providerThreadId: runThreadId }], + providerThreads: [ + { id: activeThreadId, providerSessionId: activeSessionId }, + { id: runThreadId, providerSessionId: runSessionId }, + ], + }), + runSessionId, + ); +}); + +it("falls back only to the explicitly active provider thread", () => { + assert.equal( + providerSessionIdForSubagentStop({ + runId, + activeProviderThreadId: activeThreadId, + runs: [{ id: runId, providerThreadId: runThreadId }], + providerThreads: [ + { id: staleThreadId, providerSessionId: staleSessionId }, + { id: runThreadId, providerSessionId: null }, + { id: activeThreadId, providerSessionId: activeSessionId }, + ], + }), + activeSessionId, + ); +}); + +it("returns null when neither the run nor active thread has a session", () => { + assert.isNull( + providerSessionIdForSubagentStop({ + runId, + activeProviderThreadId: null, + runs: [{ id: runId, providerThreadId: runThreadId }], + providerThreads: [ + { id: staleThreadId, providerSessionId: staleSessionId }, + { id: runThreadId, providerSessionId: null }, + ], + }), + ); +}); diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 6e23fc01e1..fee751ac06 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -380,6 +380,29 @@ function providerThreadForRun( : projection.providerThreads.find((candidate) => candidate.id === run.providerThreadId); } +export function providerSessionIdForSubagentStop(input: { + readonly runId: OrchestrationV2Subagent["runId"]; + readonly activeProviderThreadId: OrchestrationV2AppThread["activeProviderThreadId"]; + readonly runs: ReadonlyArray>; + readonly providerThreads: ReadonlyArray< + Pick + >; +}) { + const run = + input.runId === null ? undefined : input.runs.find((candidate) => candidate.id === input.runId); + const runSessionId = + run?.providerThreadId === null || run?.providerThreadId === undefined + ? null + : (input.providerThreads.find((candidate) => candidate.id === run.providerThreadId) + ?.providerSessionId ?? null); + if (runSessionId !== null) return runSessionId; + if (input.activeProviderThreadId === null) return null; + return ( + input.providerThreads.find((candidate) => candidate.id === input.activeProviderThreadId) + ?.providerSessionId ?? null + ); +} + function providerTurnForRun( projection: OrchestrationV2ThreadProjection, run: OrchestrationV2Run, @@ -5353,22 +5376,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio cause: `Subagent ${command.subagentId} has no native task reference to stop.`, }); } - // The stop call goes through the session the subagent's run executes - // under; a workflow that outlived its launch run falls back to the - // thread's bound session, which owns background tasks between turns. - const run = - subagent.runId === null - ? undefined - : projection.runs.find((candidate) => candidate.id === subagent.runId); - const runProviderThread = - run?.providerThreadId === null - ? undefined - : projection.providerThreads.find((candidate) => candidate.id === run?.providerThreadId); - const providerSessionId = - runProviderThread?.providerSessionId ?? - projection.providerThreads.find((candidate) => candidate.providerSessionId !== null) - ?.providerSessionId ?? - null; + const providerSessionId = providerSessionIdForSubagentStop({ + runId: subagent.runId, + activeProviderThreadId: projection.thread.activeProviderThreadId, + runs: projection.runs, + providerThreads: projection.providerThreads, + }); if (providerSessionId === null) { return yield* new OrchestratorDispatchError({ commandId: command.commandId, diff --git a/packages/client-runtime/src/state/orchestrationV2Subagents.test.ts b/packages/client-runtime/src/state/orchestrationV2Subagents.test.ts index 3249df2dce..f52e9e4084 100644 --- a/packages/client-runtime/src/state/orchestrationV2Subagents.test.ts +++ b/packages/client-runtime/src/state/orchestrationV2Subagents.test.ts @@ -91,7 +91,7 @@ describe("deriveOrchestrationV2SubagentPanelState", () => { }); const researcher = agent("researcher", { kind: "workflow_agent", - usage: { totalTokens: 400 }, + usage: { totalTokens: 600 }, workflowMembership: { workflowSubagentId: workflowId, agentIndex: 1, diff --git a/packages/client-runtime/src/state/orchestrationV2Subagents.ts b/packages/client-runtime/src/state/orchestrationV2Subagents.ts index f1e8050022..fe62b78f8c 100644 --- a/packages/client-runtime/src/state/orchestrationV2Subagents.ts +++ b/packages/client-runtime/src/state/orchestrationV2Subagents.ts @@ -97,10 +97,6 @@ export function deriveOrchestrationV2SubagentPanelState(input: { if (direct.length > 0) groups.push({ workflow: null, phases: [], agents: direct }); const workers = input.subagents.filter((agent) => agent.kind !== "workflow"); - // A coordinator's usage already includes its members', so counting both - // double-reports the thread. Only drop it when the members actually account - // for it: a provider that reports workflow usage but omits per-agent tokens - // would otherwise erase the whole workflow from the total. const memberTotalsByWorkflow = new Map(); for (const agent of workers) { const workflowId = agent.workflowMembership?.workflowSubagentId; @@ -110,15 +106,15 @@ export function deriveOrchestrationV2SubagentPanelState(input: { (memberTotalsByWorkflow.get(workflowId) ?? 0) + (agent.usage?.totalTokens ?? 0), ); } + const workflowIdsWithReportedUsage = new Set( + workflows.flatMap((workflow) => (workflow.usage === null ? [] : [workflow.id])), + ); const reportedUsage = input.subagents.flatMap((agent) => { if (agent.usage === null) return []; - if (agent.kind !== "workflow") return [agent.usage.totalTokens]; - const memberTotal = memberTotalsByWorkflow.get(agent.id); - if (memberTotal === undefined) return [agent.usage.totalTokens]; - // Keep whatever the members did not account for. Dropping the coordinator - // outright erases the whole workflow whenever a provider reports its usage - // but omits per-agent tokens. - return memberTotal >= agent.usage.totalTokens ? [] : [agent.usage.totalTokens - memberTotal]; + const workflowId = agent.workflowMembership?.workflowSubagentId; + return workflowId !== undefined && workflowIdsWithReportedUsage.has(workflowId) + ? [] + : [agent.usage.totalTokens]; }); // Count the workers, plus any coordinator that has no members yet. A // coordinator with members is represented by them and would double-count;