diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 61d076a779..bec7d5a07e 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -1355,6 +1355,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 78d04a6b6c..bdea5401cf 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -5227,10 +5227,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 6f4c8ee128..6d531d5913 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -370,6 +370,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, @@ -6042,22 +6065,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,