Skip to content

Commit 493a0d7

Browse files
fix(server): a steer that raced Stop stays held even with nothing else queued
The requeued message waits held behind the run Stop reached, whether or not other messages were queued and whether or not that run has already ended. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 78095ef commit 493a0d7

2 files changed

Lines changed: 70 additions & 21 deletions

File tree

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5438,7 +5438,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54385438
projection.thread.worktreePath === null
54395439
? latestRun
54405440
: undefined;
5441-
const queueBehindRun = activeRun ?? worktreeMissingRun;
5441+
// A steer re-dispatched after it could not join its turn was sent before any Stop since, and
5442+
// was meant to go before the queue. If a Stop reached its run, it waits held, even once that
5443+
// run has ended.
5444+
const redispatchedMessage = projection.messages.find(
5445+
(message) => message.id === command.messageId,
5446+
);
5447+
const redispatchedSteer = redispatchedMessage !== undefined;
5448+
const steerRunId = redispatchedMessage?.runId ?? null;
5449+
const stoppedSteerRun =
5450+
steerRunId !== null && (yield* stopReachedRun(command, command.threadId, steerRunId))
5451+
? projection.runs.find((run) => run.id === steerRunId)
5452+
: undefined;
5453+
const queueBehindRun = activeRun ?? worktreeMissingRun ?? stoppedSteerRun;
54425454
const pendingMergeBackTransfers = pendingMergeBackTransfersForThread(projection);
54435455
const startsOrQueuesRun =
54445456
dispatchMode.type === "defer_start" ||
@@ -5455,11 +5467,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54555467
(run.status === "failed" || run.status === "interrupted") &&
54565468
run.workspacePreparation?.type === "worktree",
54575469
);
5458-
// A steer re-dispatched after it could not join its turn was sent before any Stop since, and
5459-
// was meant to go before the queue.
5460-
const redispatchedSteer = projection.messages.some(
5461-
(message) => message.id === command.messageId,
5462-
);
54635470
const sentByUserFromClient =
54645471
command.createdBy === "user" &&
54655472
(command.creationSource === "web" || command.creationSource === "mobile") &&
@@ -5604,6 +5611,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
56045611
activeAttemptId: attemptId,
56055612
status: "queued",
56065613
...(worktreeMissingRun !== undefined ||
5614+
stoppedSteerRun !== undefined ||
56075615
(heldRunsToRelease.length === 0 && queueIsHeld && !wakeSkipsHold)
56085616
? { queueHeld: true }
56095617
: {}),

‎apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts‎

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -943,10 +943,14 @@ it.effect("queues a steer the provider can only run as its own turn behind the r
943943
),
944944
);
945945

946-
it.effect("keeps Stop's held queue when a steer it raced has to wait for a new turn", () =>
946+
it.effect.each([
947+
{ queued: true, settled: false, label: "behind a queued message" },
948+
{ queued: false, settled: false, label: "alone" },
949+
{ queued: false, settled: true, label: "after the stopped turn ended" },
950+
])("holds a steer that raced Stop and has to wait for a new turn, $label", ({ queued, settled }) =>
947951
Effect.scoped(
948952
Effect.gen(function* () {
949-
const { started, layer, startFirstTurn, steerEntered, releaseSteer } =
953+
const { events, started, layer, startFirstTurn, steerEntered, releaseSteer } =
950954
yield* nextTurnSelectionHarness("steering-requires-new-turn-stop", false, true);
951955
yield* Effect.gen(function* () {
952956
const orchestrator = yield* Orchestrator.OrchestratorV2;
@@ -966,17 +970,19 @@ it.effect("keeps Stop's held queue when a steer it raced has to wait for a new t
966970
createdBy: "user",
967971
creationSource: "web",
968972
});
969-
yield* orchestrator.dispatch({
970-
type: "message.dispatch",
971-
commandId: CommandId.make("queue-later"),
972-
threadId,
973-
messageId: laterId,
974-
text: "later",
975-
attachments: [],
976-
dispatchMode: { type: "queue_after_active" },
977-
createdBy: "user",
978-
creationSource: "web",
979-
});
973+
if (queued) {
974+
yield* orchestrator.dispatch({
975+
type: "message.dispatch",
976+
commandId: CommandId.make("queue-later"),
977+
threadId,
978+
messageId: laterId,
979+
text: "later",
980+
attachments: [],
981+
dispatchMode: { type: "queue_after_active" },
982+
createdBy: "user",
983+
creationSource: "web",
984+
});
985+
}
980986
const steering = yield* worker.runOnce.pipe(Effect.forkScoped);
981987
yield* Deferred.await(steerEntered);
982988
yield* orchestrator.dispatch({
@@ -986,18 +992,53 @@ it.effect("keeps Stop's held queue when a steer it raced has to wait for a new t
986992
runId: first.runId,
987993
holdQueue: true,
988994
});
995+
if (settled) {
996+
const interrupted = yield* orchestrator.streamDomainEvents.pipe(
997+
Stream.filter(
998+
(event) =>
999+
event.type === "run.updated" &&
1000+
event.payload.id === first.runId &&
1001+
event.payload.status === "interrupted",
1002+
),
1003+
Stream.take(1),
1004+
Stream.runDrain,
1005+
Effect.forkScoped,
1006+
);
1007+
const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!;
1008+
yield* Queue.offer(events, {
1009+
type: "provider_turn.updated",
1010+
driver,
1011+
providerTurn: { ...turn, status: "interrupted", completedAt: yield* DateTime.now },
1012+
});
1013+
yield* Queue.offer(events, {
1014+
type: "turn.terminal",
1015+
driver,
1016+
providerThreadId: turn.providerThreadId,
1017+
providerTurnId: turn.id,
1018+
runOrdinal: first.runOrdinal,
1019+
status: "interrupted",
1020+
failure: null,
1021+
threadDisposition: "reusable",
1022+
});
1023+
yield* Fiber.join(interrupted);
1024+
}
9891025
yield* Deferred.succeed(releaseSteer, undefined);
9901026
yield* Fiber.join(steering);
1027+
yield* worker.drain();
1028+
yield* orchestrator.resumeQueuedRuns;
1029+
yield* worker.drain();
9911030

9921031
const projection = yield* orchestrator.getThreadProjection(threadId);
993-
for (const messageId of [laterId, skillId]) {
1032+
assert.equal(started.length, 1, "nothing may start after Stop");
1033+
const waiting = queued ? [skillId, laterId] : [skillId];
1034+
for (const messageId of waiting) {
9941035
const run = projection.runs.find((candidate) => candidate.userMessageId === messageId);
9951036
assert.equal(run?.status, "queued");
9961037
assert.isTrue(run?.queueHeld, `${messageId} must stay held after Stop`);
9971038
}
9981039
assert.deepEqual(
9991040
queuedRunsInDeliveryOrder(projection).map((run) => run.userMessageId),
1000-
[skillId, laterId],
1041+
waiting,
10011042
);
10021043
}).pipe(Effect.provide(layer));
10031044
}),

0 commit comments

Comments
 (0)