Skip to content

Commit 954bc76

Browse files
committed
fix(server): a follow-up promoted to Steer no longer reads as stopped
Fixes #19351
1 parent 9a01524 commit 954bc76

2 files changed

Lines changed: 196 additions & 6 deletions

File tree

‎apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts‎

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,3 +1837,168 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
18371837
}),
18381838
);
18391839
});
1840+
1841+
it.layer(layerTest)("delegated follow-ups promoted to Steer", (it) => {
1842+
it.effect("settles a promoted follow-up with the turn it steered, not as cancelled", () =>
1843+
Effect.gen(function* () {
1844+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1845+
const eventSink = yield* EventSink.EventSinkV2;
1846+
const now = yield* DateTime.now;
1847+
const threadId = ThreadId.make("thread:steer-parent");
1848+
const projectId = ProjectId.make("project:steer-parent");
1849+
const runId = RunId.make("run:steer-parent");
1850+
const rootNodeId = NodeId.make("node:steer-parent-root");
1851+
yield* seedParentWithTerminalTask({
1852+
threadId,
1853+
projectId,
1854+
runId,
1855+
rootNodeId,
1856+
taskId: NodeId.make("node:steer-parent-settled"),
1857+
deliveryState: "delivered",
1858+
now,
1859+
});
1860+
const child = yield* seedRestartCancelledChild({
1861+
parentThreadId: threadId,
1862+
projectId,
1863+
parentRunId: runId,
1864+
rootNodeId,
1865+
name: "steer-child",
1866+
completionWake: "always",
1867+
continuationPending: false,
1868+
runStatus: "completed",
1869+
now,
1870+
});
1871+
const steeredRunId = RunId.make("run:steer-child:2");
1872+
const followUpTaskId = NodeId.make("node:steer-child-follow-up");
1873+
const followUpMessageId = MessageId.make("message:steer-child-follow-up");
1874+
const followUpRun = runEvent({
1875+
threadId: child.childThreadId,
1876+
runId: RunId.make("run:steer-child:3"),
1877+
ordinal: 3,
1878+
status: "cancelled",
1879+
userMessageId: followUpMessageId,
1880+
now,
1881+
});
1882+
// The follow-up was queued as its own task, then promoted into the running turn.
1883+
yield* eventSink.write({
1884+
commandId: CommandId.make("command:steer-child:promoted"),
1885+
events: [
1886+
runEvent({
1887+
threadId: child.childThreadId,
1888+
runId: steeredRunId,
1889+
ordinal: 2,
1890+
status: "running",
1891+
now,
1892+
}),
1893+
{
1894+
...followUpRun,
1895+
payload: { ...followUpRun.payload, startedAt: null, delegatedTaskId: followUpTaskId },
1896+
},
1897+
{
1898+
id: EventId.make("event:steer-child:follow-up-message"),
1899+
type: "message.updated",
1900+
threadId: child.childThreadId,
1901+
runId: steeredRunId,
1902+
occurredAt: now,
1903+
payload: {
1904+
id: followUpMessageId,
1905+
threadId: child.childThreadId,
1906+
runId: steeredRunId,
1907+
nodeId: null,
1908+
role: "user",
1909+
text: "Also merge the fixture branch.",
1910+
attachments: [],
1911+
streaming: false,
1912+
createdBy: "agent",
1913+
creationSource: "mcp",
1914+
createdAt: now,
1915+
updatedAt: now,
1916+
},
1917+
},
1918+
{
1919+
id: EventId.make("event:steer-child:follow-up-task"),
1920+
type: "subagent.updated",
1921+
threadId,
1922+
runId,
1923+
nodeId: followUpTaskId,
1924+
driver,
1925+
providerInstanceId: modelSelection.instanceId,
1926+
occurredAt: now,
1927+
payload: {
1928+
id: followUpTaskId,
1929+
threadId,
1930+
runId,
1931+
parentNodeId: rootNodeId,
1932+
origin: "app_owned",
1933+
createdBy: "agent",
1934+
driver,
1935+
providerInstanceId: modelSelection.instanceId,
1936+
providerThreadId: null,
1937+
childThreadId: child.childThreadId,
1938+
nativeTaskRef: null,
1939+
prompt: "Also merge the fixture branch.",
1940+
title: null,
1941+
model: null,
1942+
completionWake: "always",
1943+
status: "pending",
1944+
result: null,
1945+
startedAt: now,
1946+
completedAt: null,
1947+
updatedAt: now,
1948+
},
1949+
},
1950+
],
1951+
});
1952+
1953+
yield* orchestrator.recoverDelegatedTask(child.childThreadId, followUpRun.payload.id);
1954+
1955+
const steering = yield* orchestrator.getThreadProjection(threadId);
1956+
assert.equal(steering.subagents.find((row) => row.id === followUpTaskId)?.status, "pending");
1957+
assert.isTrue(
1958+
yield* orchestrator.delegatedTaskResultPending(child.childThreadId, followUpTaskId),
1959+
);
1960+
1961+
yield* eventSink.write({
1962+
commandId: CommandId.make("command:steer-child:completed"),
1963+
events: [
1964+
{
1965+
id: EventId.make("event:steer-child:result"),
1966+
type: "message.updated",
1967+
threadId: child.childThreadId,
1968+
runId: steeredRunId,
1969+
occurredAt: now,
1970+
payload: {
1971+
id: MessageId.make("message:steer-child:result"),
1972+
threadId: child.childThreadId,
1973+
runId: steeredRunId,
1974+
nodeId: null,
1975+
role: "assistant",
1976+
text: "Merged both branches.",
1977+
attachments: [],
1978+
streaming: false,
1979+
createdBy: "agent",
1980+
creationSource: "server",
1981+
createdAt: now,
1982+
updatedAt: now,
1983+
},
1984+
},
1985+
runEvent({
1986+
threadId: child.childThreadId,
1987+
runId: steeredRunId,
1988+
ordinal: 2,
1989+
status: "completed",
1990+
now,
1991+
}),
1992+
],
1993+
});
1994+
yield* orchestrator.recoverDelegatedTask(child.childThreadId, steeredRunId);
1995+
1996+
const settled = yield* orchestrator.getThreadProjection(threadId);
1997+
const original = settled.subagents.find((row) => row.id === child.taskId);
1998+
const followUp = settled.subagents.find((row) => row.id === followUpTaskId);
1999+
assert.equal(original?.result, "Merged both branches.");
2000+
assert.equal(followUp?.status, "completed");
2001+
assert.equal(followUp?.result, "Merged both branches.");
2002+
}),
2003+
);
2004+
});

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,25 @@ export class OrchestratorV2 extends Context.Service<OrchestratorV2, Orchestrator
387387
"t3/orchestration-v2/Orchestrator/OrchestratorV2",
388388
) {}
389389

390+
/**
391+
* The run whose outcome settles a delegated task. Promoting a queued follow-up
392+
* to Steer cancels its run and moves its message into the running turn, so the
393+
* task settles with that turn instead of reading as stopped.
394+
*/
395+
function delegatedTaskResultRun(
396+
child: Pick<OrchestrationV2ThreadProjection, "runs" | "messages">,
397+
taskId: NodeId,
398+
): OrchestrationV2Run | undefined {
399+
const run = child.runs
400+
.filter((candidate) => candidate.delegatedTaskId === taskId)
401+
.toSorted((a, b) => b.ordinal - a.ordinal)[0];
402+
if (run?.status !== "cancelled") return run;
403+
const steeredRunId = child.messages.find((message) => message.id === run.userMessageId)?.runId;
404+
return (
405+
child.runs.find((candidate) => candidate.id === steeredRunId && candidate.id !== run.id) ?? run
406+
);
407+
}
408+
390409
function nextRunOrdinal(projection: Pick<OrchestrationV2ThreadProjection, "runs">): number {
391410
return projection.runs.length + 1;
392411
}
@@ -10154,16 +10173,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1015410173
? options.taskId === forkedFrom.nodeId
1015510174
? options.resultRun
1015610175
: undefined
10157-
: taskRuns.toSorted((a, b) => b.ordinal - a.ordinal)[0];
10176+
: delegatedTaskResultRun(childControls, options.taskId);
1015810177
if (childRun === undefined) return;
10178+
const steered = taskRuns.length > 0 && !taskRuns.includes(childRun);
1015910179
const outcome = runOutcome(childRun);
1016010180
const terminalStatus = delegatedTaskTerminalStatus(outcome);
1016110181
if (terminalStatus === null) {
1016210182
return;
1016310183
}
1016410184
if (
1016510185
yield* childAwaitsRestartContinuation(
10166-
taskRuns.length === 0 ? childControls.runs : taskRuns,
10186+
taskRuns.length === 0 || steered ? childControls.runs : taskRuns,
1016710187
childRun,
1016810188
options?.settledContinuationOf,
1016910189
)
@@ -10218,7 +10238,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1021810238
(transfer.sourcePoint.runId === childRun.id ||
1021910239
(options.taskId === forkedFrom.nodeId && transfer.sourcePoint.runId === undefined)),
1022010240
);
10221-
if (existingResultTransfer !== undefined) {
10241+
// The steered run's own task may already have published that run's result.
10242+
if (steered ? task.result !== null : existingResultTransfer !== undefined) {
1022210243
return;
1022310244
}
1022410245

@@ -11428,10 +11449,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1142811449
{ messageRoles: ["user"] },
1142911450
);
1143011451
if (taskId !== undefined) {
11431-
const runs = child.runs.filter((run) => run.delegatedTaskId === taskId);
11432-
const run = runs.toSorted((a, b) => b.ordinal - a.ordinal)[0];
11452+
const run = delegatedTaskResultRun(child, taskId);
1143311453
if (run === undefined || delegatedTaskTerminalStatus(run.status) === null) return true;
11434-
return yield* childAwaitsRestartContinuation(runs, run);
11454+
return yield* childAwaitsRestartContinuation(
11455+
run.delegatedTaskId === taskId
11456+
? child.runs.filter((candidate) => candidate.delegatedTaskId === taskId)
11457+
: child.runs,
11458+
run,
11459+
);
1143511460
}
1143611461
const progress = delegatedTaskProgress(child);
1143711462
// A caller's older read saw a result; newer work since then means it is not final.

0 commit comments

Comments
 (0)