fix(orchestration-v2): target live subagent sessions

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Shivam SharmaandClaude Fable 5 authored and T3 Code Test committed 2026-08-14 15:04:12 +05:30
1 parent 9099ca6007
commit 6894fe9f6f
4 files changed
+102 -19

No files matched your search

@@ -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* () {
@@ -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(
@@ -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 },
],
}),
);
});
@@ -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<Pick<OrchestrationV2Run, "id" | "providerThreadId">>;
readonly providerThreads: ReadonlyArray<
Pick<OrchestrationV2ProviderThread, "id" | "providerSessionId">
>;
}) {
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,