Skip to content

Commit 1d57b7e

Browse files
committed
fix(server): reject busy merge-back targets before blocking their queue
1 parent 8d3b061 commit 1d57b7e

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
@@ -3963,7 +3963,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
39633963
// command copies, not an earlier read the source's user could outrun.
39643964
yield* refuseAboveDispatchModeLimit(command, command.sourceThreadId, sourceProjection.thread);
39653965
const targetProjection = yield* projectionStore
3966-
.getThreadRecords(command.targetThreadId, ["contextTransfers"])
3966+
.getThreadRecords(command.targetThreadId, ["runs", "contextTransfers"])
39673967
.pipe(
39683968
Effect.mapError(
39693969
(cause) =>
@@ -4015,6 +4015,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
40154015
});
40164016
}
40174017

4018+
// Queued runs cannot consume merge-back context. Refuse under the target's
4019+
// command lock before a pending transfer can block its ordinary messages.
4020+
if (targetProjection.runs.some((run) => isBlockingRun(run) || run.status === "queued")) {
4021+
return yield* new OrchestratorDispatchError({
4022+
commandId: command.commandId,
4023+
commandType: command.type,
4024+
cause:
4025+
"Finish or stop the target thread's active work and clear its queue before merging back.",
4026+
});
4027+
}
4028+
40184029
const sourceProviderThread = providerThreadForRun(sourceProjection, sourceRun);
40194030
const now = command.createdAt ?? (yield* DateTime.now);
40204031
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
@@ -1373,7 +1373,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
13731373
}),
13741374
);
13751375

1376-
it.effect("merges an explicit provider-finished run while checkpoint capture is pending", () =>
1376+
it.effect("refuses busy merge-back targets without blocking the queue", () =>
13771377
Effect.gen(function* () {
13781378
const orchestrator = yield* Orchestrator.OrchestratorV2;
13791379
const eventSink = yield* EventSink.EventSinkV2;
@@ -1388,6 +1388,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
13881388
);
13891389
const forkTransferId = ContextTransferId.make("runtime-layer-waiting-merge-fork-transfer");
13901390

1391+
yield* seedProject({
1392+
projectId,
1393+
title: "Merge-back project",
1394+
workspaceRoot: process.cwd(),
1395+
defaultModelSelection: modelSelection,
1396+
createdAt: DateTime.formatIso(now),
1397+
});
13911398
yield* orchestrator.dispatch({
13921399
type: "thread.create",
13931400
createdBy: "user",
@@ -1515,6 +1522,87 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
15151522
],
15161523
});
15171524

1525+
yield* orchestrator.dispatch({
1526+
type: "message.dispatch",
1527+
createdBy: "user",
1528+
creationSource: "web",
1529+
commandId: CommandId.make("runtime-layer-merge-target-start"),
1530+
threadId: targetThreadId,
1531+
messageId: MessageId.make("runtime-layer-merge-target-message"),
1532+
text: "Keep working in the parent.",
1533+
attachments: [],
1534+
dispatchMode: { type: "start_immediately" },
1535+
});
1536+
const activeTarget = yield* orchestrator.getThreadProjection(targetThreadId);
1537+
const targetRun = activeTarget.runs[0]!;
1538+
for (const status of ["preparing", "starting", "waiting", "queued", "running"] as const) {
1539+
yield* eventSink.write({
1540+
commandId: CommandId.make(`runtime-layer-merge-target-${status}`),
1541+
events: [
1542+
{
1543+
id: EventId.make(`runtime-layer-merge-target-${status}-event`),
1544+
type: "run.updated",
1545+
threadId: targetThreadId,
1546+
runId: targetRun.id,
1547+
occurredAt: now,
1548+
payload: { ...targetRun, status },
1549+
},
1550+
],
1551+
});
1552+
const error = yield* orchestrator
1553+
.dispatch({
1554+
type: "thread.merge_back",
1555+
createdBy: "user",
1556+
creationSource: "mobile",
1557+
commandId: CommandId.make(`runtime-layer-merge-refused-${status}`),
1558+
sourceThreadId,
1559+
targetThreadId,
1560+
sourcePoint: { type: "run", runId: sourceRunId },
1561+
})
1562+
.pipe(Effect.flip);
1563+
assert.instanceOf(error, Orchestrator.OrchestratorDispatchError);
1564+
assert.equal(
1565+
error.cause,
1566+
"Finish or stop the target thread's active work and clear its queue before merging back.",
1567+
);
1568+
const refusedTarget = yield* orchestrator.getThreadProjection(targetThreadId);
1569+
assert.deepEqual(refusedTarget.contextTransfers, activeTarget.contextTransfers);
1570+
assert.equal(refusedTarget.runs[0]?.status, status);
1571+
}
1572+
yield* orchestrator.dispatch({
1573+
type: "message.dispatch",
1574+
createdBy: "user",
1575+
creationSource: "web",
1576+
commandId: CommandId.make("runtime-layer-merge-target-queue"),
1577+
threadId: targetThreadId,
1578+
messageId: MessageId.make("runtime-layer-merge-target-queued-message"),
1579+
text: "Run this after the current turn.",
1580+
attachments: [],
1581+
dispatchMode: { type: "queue_after_active" },
1582+
});
1583+
const queuedTarget = yield* orchestrator.getThreadProjection(targetThreadId);
1584+
const queuedRun = queuedTarget.runs.find((run) => run.status === "queued");
1585+
assert.isDefined(queuedRun);
1586+
yield* orchestrator.dispatch({
1587+
type: "queued-run.cancel",
1588+
commandId: CommandId.make("runtime-layer-merge-target-cancel-queue"),
1589+
threadId: targetThreadId,
1590+
runId: queuedRun.id,
1591+
});
1592+
yield* eventSink.write({
1593+
commandId: CommandId.make("runtime-layer-merge-target-complete"),
1594+
events: [
1595+
{
1596+
id: EventId.make("runtime-layer-merge-target-completed-event"),
1597+
type: "run.updated",
1598+
threadId: targetThreadId,
1599+
runId: targetRun.id,
1600+
occurredAt: now,
1601+
payload: { ...targetRun, status: "completed", completedAt: now },
1602+
},
1603+
],
1604+
});
1605+
15181606
yield* orchestrator.dispatch({
15191607
type: "thread.merge_back",
15201608
createdBy: "user",

0 commit comments

Comments
 (0)