Skip to content

Commit afc12ab

Browse files
ScottN-PVclaude
andauthored
fix(server): a steered send no longer leaves a subagent task running (pingdotgg#19019)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent beadfe5 commit afc12ab

3 files changed

Lines changed: 149 additions & 2 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ function makeDeterministicAdapter(input: {
198198
turn: ProviderAdapter.ProviderAdapterV2TurnInput,
199199
) => Deferred.Deferred<void> | undefined;
200200
readonly response: (turn: ProviderAdapter.ProviderAdapterV2TurnInput) => string;
201+
readonly steerReceived?: (
202+
steer: ProviderAdapter.ProviderAdapterV2SteerInput,
203+
) => Deferred.Deferred<void> | undefined;
201204
}): ProviderAdapter.ProviderAdapterV2["Service"] {
202205
return {
203206
instanceId: input.instanceId,
@@ -365,7 +368,12 @@ function makeDeterministicAdapter(input: {
365368
},
366369
]);
367370
}),
368-
steerTurn: () => Effect.void,
371+
steerTurn: (steer) => {
372+
const received = input.steerReceived?.(steer);
373+
return received === undefined
374+
? Effect.void
375+
: Deferred.succeed(received, undefined).pipe(Effect.asVoid);
376+
},
369377
interruptTurn: ({ providerThread, providerTurnId }) =>
370378
Effect.gen(function* () {
371379
const turnInput = turnInputs.get(providerTurnId);
@@ -529,6 +537,7 @@ describe("orchestrator MCP toolkit", () => {
529537
Effect.gen(function* () {
530538
const cwd = yield* checkpointWorkspace("orchestrator-mcp-toolkit");
531539
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
540+
const steerReceivedGates = new Map<string, Deferred.Deferred<void>>();
532541
const parentTerminalGates = new Map<ThreadId, Deferred.Deferred<void>>();
533542
const deliveryTerminalGates = new Map<ThreadId, Deferred.Deferred<void>>();
534543
const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
@@ -564,6 +573,7 @@ describe("orchestrator MCP toolkit", () => {
564573
turn.message.text === delegatedPrompt
565574
? delegatedResult
566575
: `Claude completed: ${turn.message.text}`,
576+
steerReceived: (steer) => steerReceivedGates.get(steer.message.text),
567577
}),
568578
]);
569579
const continuationOffers = yield* Ref.make<
@@ -1902,6 +1912,134 @@ describe("orchestrator MCP toolkit", () => {
19021912
yield* invoke("task_status", { taskId: activeChildFollowup.taskId });
19031913
yield* Ref.set(continuationOffers, []);
19041914

1915+
// A send into a child run the server started has no task to join.
1916+
// It gets its own task only when it starts a new attempt; a steer
1917+
// into the running attempt leaves no run that could settle one.
1918+
const childSteerText = "Include the parent's latest guidance before finishing.";
1919+
const steerIntoUntaskedChildRun = (
1920+
target: { readonly providerInstanceId: ProviderInstanceId; readonly model: string },
1921+
name: string,
1922+
) =>
1923+
Effect.gen(function* () {
1924+
const delegateCall = yield* invoke("delegate_task", {
1925+
task: "Inspect the steering boundary and return the result.",
1926+
target,
1927+
mode: "wait",
1928+
timeoutMs: 10_000,
1929+
clientRequestId: `${name}-delegate`,
1930+
});
1931+
const child = yield* decodeDelegateTaskResult(delegateCall.structuredContent).pipe(
1932+
Effect.orDie,
1933+
);
1934+
expect(child.status).toBe("completed");
1935+
const messageId = MessageId.make(`message:mcp-child:${name}`);
1936+
yield* orchestrator.dispatch({
1937+
type: "message.dispatch",
1938+
createdBy: "agent",
1939+
creationSource: "server",
1940+
commandId: CommandId.make(`command:mcp-child:${name}`),
1941+
threadId: child.childThreadId,
1942+
messageId,
1943+
text: cancellationPrompt,
1944+
attachments: [],
1945+
modelSelection: { instanceId: target.providerInstanceId, model: target.model },
1946+
dispatchMode: { type: "start_immediately" },
1947+
});
1948+
const running = yield* waitForProjection(
1949+
orchestrator,
1950+
child.childThreadId,
1951+
(projection) => {
1952+
const run = projection.runs.find((row) => row.userMessageId === messageId);
1953+
return (
1954+
run?.status === "running" &&
1955+
projection.providerTurns.some(
1956+
(turn) => turn.nodeId === run.rootNodeId && turn.status === "running",
1957+
)
1958+
);
1959+
},
1960+
);
1961+
const run = running.runs.find((row) => row.userMessageId === messageId)!;
1962+
expect(run.delegatedTaskId).toBeUndefined();
1963+
const sendCall = yield* invoke("t3_thread_send", {
1964+
threadId: child.childThreadId,
1965+
message: childSteerText,
1966+
mode: "steer",
1967+
clientRequestId: `${name}-steer`,
1968+
});
1969+
const sent = yield* decodeThreadSendResult(sendCall.structuredContent).pipe(
1970+
Effect.orDie,
1971+
);
1972+
expect(sent).toMatchObject({ runId: run.id, delivery: "steered" });
1973+
return { child, sent };
1974+
});
1975+
const waitForChildRun = (
1976+
childThreadId: ThreadId,
1977+
runId: string,
1978+
status: "completed" | "interrupted",
1979+
) =>
1980+
waitForProjection(orchestrator, childThreadId, (projection) =>
1981+
projection.runs.some((run) => run.id === runId && run.status === status),
1982+
);
1983+
1984+
// Claude steers into the running turn, which then ends.
1985+
const steerReceived = yield* Deferred.make<void>();
1986+
steerReceivedGates.set(childSteerText, steerReceived);
1987+
const steeredSend = yield* steerIntoUntaskedChildRun(
1988+
{ providerInstanceId: claudeInstanceId, model: claudeModel },
1989+
"steer-untasked-run",
1990+
);
1991+
// A steer that misses the turn is sent again as its own run, so
1992+
// end the turn only once the steer reached it.
1993+
yield* Deferred.await(steerReceived);
1994+
steerReceivedGates.delete(childSteerText);
1995+
yield* invoke("t3_thread_interrupt", {
1996+
threadId: steeredSend.child.childThreadId,
1997+
runId: steeredSend.sent.runId,
1998+
clientRequestId: "steer-untasked-run-interrupt",
1999+
});
2000+
yield* waitForChildRun(
2001+
steeredSend.child.childThreadId,
2002+
steeredSend.sent.runId,
2003+
"interrupted",
2004+
);
2005+
expect(
2006+
(yield* orchestrator.getThreadProjection(parentThreadId)).subagents.filter(
2007+
(task) =>
2008+
task.origin === "app_owned" &&
2009+
task.childThreadId === steeredSend.child.childThreadId &&
2010+
task.status === "running",
2011+
),
2012+
).toEqual([]);
2013+
expect(steeredSend.sent.taskId).toBeUndefined();
2014+
2015+
// This Codex session has active steering off, so the send restarts the
2016+
// turn as a new attempt that owns the follow-up task and settles it.
2017+
const restarted = yield* steerIntoUntaskedChildRun(
2018+
{ providerInstanceId: codexInstanceId, model: codexModel },
2019+
"restart-untasked-run",
2020+
);
2021+
const restartedTaskId = restarted.sent.taskId;
2022+
expect(restartedTaskId).toBeDefined();
2023+
yield* waitForChildRun(
2024+
restarted.child.childThreadId,
2025+
restarted.sent.runId,
2026+
"completed",
2027+
);
2028+
yield* waitForProjection(orchestrator, parentThreadId, (projection) =>
2029+
projection.subagents.some(
2030+
(task) => task.id === restartedTaskId && task.status === "completed",
2031+
),
2032+
);
2033+
expect(
2034+
(yield* orchestrator.getThreadProjection(restarted.child.childThreadId)).runs.find(
2035+
(run) => run.id === restarted.sent.runId,
2036+
)?.delegatedTaskId,
2037+
).toBe(restartedTaskId);
2038+
yield* invoke("task_status", { taskId: steeredSend.child.taskId });
2039+
yield* invoke("task_status", { taskId: restarted.child.taskId });
2040+
yield* invoke("task_status", { taskId: restartedTaskId });
2041+
yield* Ref.set(continuationOffers, []);
2042+
19052043
const repeatedDelegatedCall = yield* invoke("delegate_task", {
19062044
task: delegatedPrompt,
19072045
target: {

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export const ThreadUpdateTool = Tool.make("t3_thread_update", {
234234

235235
const ThreadSendTool = Tool.make("t3_thread_send", {
236236
description:
237-
"Send a message to any T3 thread in this environment. The target cannot have broader permission modes than the caller. Use delegate_task with the full review context and a new clientRequestId for independent review rounds. For a follow-up that needs the same child's conversation, send to its childThreadId. After the original task completes, a requested follow-up creates a fresh task in that child thread and returns taskId; its completion notifies the parent, and the original result stays unchanged. mode='auto' starts an idle thread, steers a fully active turn, or queues behind a turn that is not yet steerable. Use queue for a separate follow-up turn, steer for an in-flight update, or restart to interrupt-and-restart the active turn. clientRequestId makes retries idempotent.",
237+
"Send a message to any T3 thread in this environment. The target cannot have broader permission modes than the caller. Use delegate_task with the full review context and a new clientRequestId for independent review rounds. For a follow-up that needs the same child's conversation, send to its childThreadId. After the original task completes, a requested follow-up creates a fresh task in that child thread and returns taskId when it starts a turn or restarts a turn without its own task; its completion notifies the parent, and the original result stays unchanged. A steer that joins a running turn returns that turn's task, if it has one. mode='auto' starts an idle thread, steers a fully active turn, or queues behind a turn that is not yet steerable. Use queue for a separate follow-up turn, steer for an in-flight update, or restart to interrupt-and-restart the active turn. clientRequestId makes retries idempotent.",
238238
parameters: OrchestratorMcpThreadSendInput,
239239
success: OrchestratorMcpThreadSendResult,
240240
failure: OrchestratorMcpFailure,

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6838,6 +6838,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
68386838
)
68396839
.at(-1);
68406840
if (message?.runId == null) return;
6841+
// Only a message that starts a run or a new attempt can own a task. A
6842+
// steer joins the running attempt, which finalizes its run from the
6843+
// snapshot taken when it started, so a task bound to it would never settle.
6844+
const startsAttempt = pending.some(
6845+
(event) =>
6846+
(event.type === "run.created" && event.payload.id === message.runId) ||
6847+
(event.type === "run-attempt.created" && event.payload.runId === message.runId),
6848+
);
6849+
if (!startsAttempt) return;
68416850
const child = yield* projectionStore.getThread(command.threadId);
68426851
const parentThreadId = child.lineage.parentThreadId;
68436852
if (

0 commit comments

Comments
 (0)