Skip to content

Commit 0804c60

Browse files
authored
fix(server): reject busy merge-back targets before blocking their queue (#23554)
1 parent b1b3c10 commit 0804c60

2 files changed

Lines changed: 101 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4340,7 +4340,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
43404340
// The source is unlocked; validate the modes copied here, not an earlier read.
43414341
yield* refuseAboveDispatchModeLimit(command, command.sourceThreadId, sourceProjection.thread);
43424342
const targetProjection = yield* projectionStore
4343-
.getThreadRecords(command.targetThreadId, ["contextTransfers"])
4343+
.getThreadRecords(command.targetThreadId, ["runs", "contextTransfers"])
43444344
.pipe(
43454345
Effect.mapError(
43464346
(cause) =>
@@ -4392,6 +4392,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
43924392
});
43934393
}
43944394

4395+
// Queued runs cannot consume merge-back context. Refuse under the target's
4396+
// command lock before a pending transfer can block its ordinary messages.
4397+
if (targetProjection.runs.some((run) => isBlockingRun(run) || run.status === "queued")) {
4398+
return yield* new OrchestratorDispatchError({
4399+
commandId: command.commandId,
4400+
commandType: command.type,
4401+
cause:
4402+
"Finish or stop the target thread's active work and clear its queue before merging back.",
4403+
});
4404+
}
4405+
43954406
const sourceProviderThread = providerThreadForRun(sourceProjection, sourceRun);
43964407
const now = command.createdAt ?? (yield* DateTime.now);
43974408
const emitEvent = emit(events, command);

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

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
17261726
}),
17271727
);
17281728

1729-
it.effect("merges an explicit provider-finished run while checkpoint capture is pending", () =>
1729+
it.effect("refuses busy merge-back targets without blocking the queue", () =>
17301730
Effect.gen(function* () {
17311731
const orchestrator = yield* Orchestrator.OrchestratorV2;
17321732
const eventSink = yield* EventSink.EventSinkV2;
@@ -1741,6 +1741,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
17411741
);
17421742
const forkTransferId = ContextTransferId.make("runtime-layer-waiting-merge-fork-transfer");
17431743

1744+
yield* seedProject({
1745+
projectId,
1746+
title: "Merge-back project",
1747+
workspaceRoot: process.cwd(),
1748+
defaultModelSelection: modelSelection,
1749+
createdAt: DateTime.formatIso(now),
1750+
});
17441751
yield* orchestrator.dispatch({
17451752
type: "thread.create",
17461753
createdBy: "user",
@@ -1868,6 +1875,87 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
18681875
],
18691876
});
18701877

1878+
yield* orchestrator.dispatch({
1879+
type: "message.dispatch",
1880+
createdBy: "user",
1881+
creationSource: "web",
1882+
commandId: CommandId.make("runtime-layer-merge-target-start"),
1883+
threadId: targetThreadId,
1884+
messageId: MessageId.make("runtime-layer-merge-target-message"),
1885+
text: "Keep working in the parent.",
1886+
attachments: [],
1887+
dispatchMode: { type: "start_immediately" },
1888+
});
1889+
const activeTarget = yield* orchestrator.getThreadProjection(targetThreadId);
1890+
const targetRun = activeTarget.runs[0]!;
1891+
for (const status of ["preparing", "starting", "waiting", "queued", "running"] as const) {
1892+
yield* eventSink.write({
1893+
commandId: CommandId.make(`runtime-layer-merge-target-${status}`),
1894+
events: [
1895+
{
1896+
id: EventId.make(`runtime-layer-merge-target-${status}-event`),
1897+
type: "run.updated",
1898+
threadId: targetThreadId,
1899+
runId: targetRun.id,
1900+
occurredAt: now,
1901+
payload: { ...targetRun, status },
1902+
},
1903+
],
1904+
});
1905+
const error = yield* orchestrator
1906+
.dispatch({
1907+
type: "thread.merge_back",
1908+
createdBy: "user",
1909+
creationSource: "mobile",
1910+
commandId: CommandId.make(`runtime-layer-merge-refused-${status}`),
1911+
sourceThreadId,
1912+
targetThreadId,
1913+
sourcePoint: { type: "run", runId: sourceRunId },
1914+
})
1915+
.pipe(Effect.flip);
1916+
assert.instanceOf(error, Orchestrator.OrchestratorDispatchError);
1917+
assert.equal(
1918+
error.cause,
1919+
"Finish or stop the target thread's active work and clear its queue before merging back.",
1920+
);
1921+
const refusedTarget = yield* orchestrator.getThreadProjection(targetThreadId);
1922+
assert.deepEqual(refusedTarget.contextTransfers, activeTarget.contextTransfers);
1923+
assert.equal(refusedTarget.runs[0]?.status, status);
1924+
}
1925+
yield* orchestrator.dispatch({
1926+
type: "message.dispatch",
1927+
createdBy: "user",
1928+
creationSource: "web",
1929+
commandId: CommandId.make("runtime-layer-merge-target-queue"),
1930+
threadId: targetThreadId,
1931+
messageId: MessageId.make("runtime-layer-merge-target-queued-message"),
1932+
text: "Run this after the current turn.",
1933+
attachments: [],
1934+
dispatchMode: { type: "queue_after_active" },
1935+
});
1936+
const queuedTarget = yield* orchestrator.getThreadProjection(targetThreadId);
1937+
const queuedRun = queuedTarget.runs.find((run) => run.status === "queued");
1938+
assert.isDefined(queuedRun);
1939+
yield* orchestrator.dispatch({
1940+
type: "queued-run.cancel",
1941+
commandId: CommandId.make("runtime-layer-merge-target-cancel-queue"),
1942+
threadId: targetThreadId,
1943+
runId: queuedRun.id,
1944+
});
1945+
yield* eventSink.write({
1946+
commandId: CommandId.make("runtime-layer-merge-target-complete"),
1947+
events: [
1948+
{
1949+
id: EventId.make("runtime-layer-merge-target-completed-event"),
1950+
type: "run.updated",
1951+
threadId: targetThreadId,
1952+
runId: targetRun.id,
1953+
occurredAt: now,
1954+
payload: { ...targetRun, status: "completed", completedAt: now },
1955+
},
1956+
],
1957+
});
1958+
18711959
yield* orchestrator.dispatch({
18721960
type: "thread.merge_back",
18731961
createdBy: "user",

0 commit comments

Comments
 (0)