fix: preserve orchestration task identity

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex authored and maria-rcks committed 2026-08-03 11:45:13 -04:00
1 parent b65e09cdd8
commit b4bc329f73
4 files changed
+134 -5

No files matched your search

+24 -2
View File
@@ -5,6 +5,7 @@ import {
type ModelSelection,
NodeId,
type OrchestrationV2Run,
type OrchestrationV2Subagent,
type OrchestrationV2ThreadProjection,
type OrchestrationV2ThreadShell,
type OrchestrationV2TurnItem,
@@ -279,6 +280,25 @@ function taskStatusForRun(
}
}
function delegatedTaskRun(
childProjection: OrchestrationV2ThreadProjection,
task: OrchestrationV2Subagent,
): OrchestrationV2Run | undefined {
const spawnTransfer = childProjection.contextTransfers.find(
(transfer) =>
transfer.type === "subagent_spawn" &&
transfer.sourceThreadId === task.threadId &&
transfer.targetThreadId === task.childThreadId,
);
if (spawnTransfer === undefined) {
// Legacy delegated-task projections predate the durable spawn transfer.
return latestRun(childProjection);
}
return spawnTransfer.targetRunId === null
? undefined
: childProjection.runs.find((run) => run.id === spawnTransfer.targetRunId);
}
function isTerminalTaskStatus(
status: OrchestratorMcpDelegateTaskResult["status"],
): status is TerminalTaskStatus {
@@ -773,7 +793,7 @@ const make = Effect.gen(function* () {
);
}
const childProjection = yield* loadProjection(task.childThreadId);
const childRun = latestRun(childProjection);
const childRun = delegatedTaskRun(childProjection, task);
const status = taskStatusForRun(childRun);
const derivedResult =
task.result !== null
@@ -1139,7 +1159,9 @@ const make = Effect.gen(function* () {
} satisfies OrchestratorMcpTaskCancelResult;
}
const child = yield* loadProjection(current.childThreadId);
const activeRun = child.runs.find(isActiveRun);
const activeRun = child.runs.find(
(run) => run.id === current.childRunId && isActiveRun(run),
);
if (activeRun === undefined) {
return yield* failure(
"task_not_cancellable",
@@ -408,7 +408,7 @@ describe("orchestrator MCP toolkit", () => {
driver: ProviderDriverKind.make("claudeAgent"),
capabilities: ClaudeProviderCapabilitiesV2,
capturedTurns,
shouldComplete: () => true,
shouldComplete: (turn) => turn.message.text !== cancellationPrompt,
response: (turn) =>
turn.message.text === delegatedPrompt
? delegatedResult
@@ -845,9 +845,59 @@ describe("orchestrator MCP toolkit", () => {
delegatedStatusAfterFollowupCall.structuredContent,
).pipe(Effect.orDie);
expect(delegatedStatusAfterFollowup).toMatchObject({
childRunId: childFollowup.runId,
childRunId: delegated.childRunId,
status: "completed",
summary: delegatedResult,
});
const activeChildFollowupCall = yield* invoke("t3_thread_send", {
threadId: delegated.childThreadId,
message: cancellationPrompt,
clientRequestId: "delegated-child-active-followup-1",
});
const activeChildFollowup = yield* decodeThreadSendResult(
activeChildFollowupCall.structuredContent,
).pipe(Effect.orDie);
yield* waitForProjection(orchestrator, delegated.childThreadId, (projection) =>
projection.runs.some(
(run) => run.id === activeChildFollowup.runId && run.status === "running",
),
);
const completedTaskCancelCall = yield* invoke("task_cancel", {
taskId: delegated.taskId,
reason: "Must not interrupt a later unrelated child run.",
clientRequestId: "cancel-completed-delegated-task-1",
});
const completedTaskCancel = yield* decodeTaskCancelResult(
completedTaskCancelCall.structuredContent,
).pipe(Effect.orDie);
expect(completedTaskCancel).toEqual({
taskId: delegated.taskId,
status: "completed",
});
expect(
(yield* orchestrator.getThreadProjection(delegated.childThreadId)).runs.find(
(run) => run.id === activeChildFollowup.runId,
)?.status,
).toBe("running");
const activeChildCleanupCall = yield* invoke("t3_thread_interrupt", {
threadId: delegated.childThreadId,
runId: activeChildFollowup.runId,
reason: "Clean up the active follow-up after verifying task cancellation isolation.",
clientRequestId: "interrupt-delegated-child-followup-1",
});
const activeChildCleanup = yield* decodeThreadInterruptResult(
activeChildCleanupCall.structuredContent,
).pipe(Effect.orDie);
expect(activeChildCleanup).toMatchObject({
runId: activeChildFollowup.runId,
status: "interrupt_requested",
});
yield* waitForProjection(orchestrator, delegated.childThreadId, (projection) =>
projection.runs.some(
(run) => run.id === activeChildFollowup.runId && run.status === "interrupted",
),
);
// A wait-mode child (completionWake settled_only) that completes
// while the parent run is live does not offer a wake: the
@@ -6,6 +6,7 @@ import * as NodeFS from "node:fs";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it } from "@effect/vitest";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Option from "effect/Option";
@@ -785,6 +786,56 @@ describe("AcpSessionRuntime", () => {
),
);
it.effect("keeps active-session updates while loading a different session", () =>
Effect.gen(function* () {
const loadStarted = yield* Deferred.make<void>();
const runtime = yield* AcpSessionRuntime.make({
authMethodId: "test",
spawn: {
command: mockAgentCommand,
args: mockAgentArgs,
env: {
T3_ACP_DELAY_LOAD_SESSION_AFTER_REPLAY: "1",
T3_ACP_LOAD_SESSION_DELAY_MS: "250",
},
},
cwd: process.cwd(),
sessionLoadTimeout: "2 seconds",
clientInfo: { name: "t3-test", version: "0.0.0" },
requestLogger: (event) =>
event.method === "session/load" && event.status === "started"
? Deferred.succeed(loadStarted, undefined).pipe(Effect.asVoid)
: Effect.void,
});
yield* runtime.start();
const eventsFiber = yield* Stream.runCollect(Stream.take(runtime.getEvents(), 4)).pipe(
Effect.forkChild({ startImmediately: true }),
);
const loadFiber = yield* runtime
.loadSession("mock-session-2")
.pipe(Effect.forkChild({ startImmediately: true }));
yield* Deferred.await(loadStarted);
yield* runtime.prompt({
prompt: [{ type: "text", text: "active session prompt" }],
});
const events = Array.from(yield* Fiber.join(eventsFiber).pipe(Effect.timeout("2 seconds")));
expect(events.map((event) => event._tag)).toEqual([
"PlanUpdated",
"AssistantItemStarted",
"ContentDelta",
"AssistantItemCompleted",
]);
expect(
events.some((event) => event._tag === "ContentDelta" && event.text === "hello from mock"),
).toBe(true);
const loaded = yield* Fiber.join(loadFiber).pipe(Effect.timeout("2 seconds"));
expect(loaded.sessionId).toBe("mock-session-2");
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer), TestClock.withLive),
);
it.effect("completes session/load after replay becomes idle while its RPC stays pending", () =>
Effect.gen(function* () {
const runtime = yield* AcpSessionRuntime.AcpSessionRuntime;
@@ -1597,7 +1597,13 @@ export const make = (
yield* acp.handleSessionUpdate((notification) =>
Effect.gen(function* () {
const gate = yield* Ref.get(sessionLoadGateRef);
if (Option.isSome(gate) && gate.value.active) {
// A different session can still have an in-flight prompt while this
// load replays history, so quarantine only the loading session.
if (
Option.isSome(gate) &&
gate.value.active &&
notification.sessionId === gate.value.sessionId
) {
if (sessionUpdateCountsAsLoadReplayActivity(notification, gate.value.sessionId)) {
const lastActivityAtMillis = yield* Clock.currentTimeMillis;
yield* Ref.set(