Skip to content

Commit e825f5a

Browse files
fix(server): background wakes start beside a held queue again
The branch queued and held every provider wake carrying a notification whenever any queued run was held, so a background reply after a failed run or Stop waited for the user. Remove that hold: wakes start beside a held queue as on main. Waiting account switches still drain buffered output first through the busy-session queueing and nextQueuedRun order. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent acea3a9 commit e825f5a

3 files changed

Lines changed: 88 additions & 33 deletions

File tree

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6431,9 +6431,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
64316431
command.createdBy === "agent" &&
64326432
command.creationSource === "provider" &&
64336433
command.notification !== undefined;
6434-
const bufferedWakeHeld =
6435-
bufferedProviderWake &&
6436-
projection.runs.some((run) => run.status === "queued" && run.queueHeld === true);
64376434
// Buffered output is part of the busy session; only its owning turn can drain it.
64386435
const queueWaitingOnBackgroundWork =
64396436
!bufferedProviderWake &&
@@ -6443,7 +6440,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
64436440
const queueBehindRun =
64446441
activeRun ??
64456442
worktreeMissingRun ??
6446-
(waitsForBackgroundWork || queueWaitingOnBackgroundWork || bufferedWakeHeld
6443+
(waitsForBackgroundWork || queueWaitingOnBackgroundWork
64476444
? (latestRun ?? undefined)
64486445
: undefined);
64496446
const pendingMergeBackTransfers = pendingMergeBackTransfersForThread(projection);

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6956,6 +6956,85 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
69566956
}),
69576957
);
69586958

6959+
it.effect("starts a background-work wake beside a queue held after a failed run", () =>
6960+
Effect.gen(function* () {
6961+
const orchestrator = yield* Orchestrator.OrchestratorV2;
6962+
const events = yield* EventSink.EventSinkV2;
6963+
const threadId = ThreadId.make("queue-hold-background-wake");
6964+
yield* orchestrator.dispatch({
6965+
type: "thread.create",
6966+
createdBy: "user",
6967+
creationSource: "web",
6968+
commandId: CommandId.make(`${threadId}:create`),
6969+
threadId,
6970+
projectId: ProjectId.make(`${threadId}:project`),
6971+
title: "Background wake beside a held queue",
6972+
modelSelection,
6973+
runtimeMode: "full-access",
6974+
interactionMode: "default",
6975+
branch: null,
6976+
worktreePath: process.cwd(),
6977+
});
6978+
const send = (index: number, text: string) =>
6979+
orchestrator.dispatch({
6980+
type: "message.dispatch",
6981+
createdBy: "user",
6982+
creationSource: "web",
6983+
commandId: CommandId.make(`${threadId}:message:${index}`),
6984+
threadId,
6985+
messageId: MessageId.make(`${threadId}:message:${index}`),
6986+
text,
6987+
attachments: [],
6988+
modelSelection,
6989+
dispatchMode: { type: index === 0 ? "start_immediately" : "queue_after_active" },
6990+
});
6991+
yield* send(0, "Active");
6992+
yield* send(1, "Queued");
6993+
const before = yield* orchestrator.getThreadProjection(threadId);
6994+
const [failed, held] = [before.runs[0]!, before.runs[1]!];
6995+
const now = yield* DateTime.now;
6996+
yield* events.write({
6997+
events: [
6998+
{
6999+
id: EventId.make(`${threadId}:failed`),
7000+
type: "run.updated",
7001+
threadId,
7002+
runId: failed.id,
7003+
occurredAt: now,
7004+
payload: { ...failed, status: "failed", startedAt: now, completedAt: now },
7005+
},
7006+
{
7007+
id: EventId.make(`${threadId}:held`),
7008+
type: "run.updated",
7009+
threadId,
7010+
runId: held.id,
7011+
occurredAt: now,
7012+
payload: { ...held, queueHeld: true },
7013+
},
7014+
],
7015+
});
7016+
yield* orchestrator.dispatch({
7017+
type: "message.dispatch",
7018+
createdBy: "agent",
7019+
creationSource: "provider",
7020+
commandId: CommandId.make(`${threadId}:wake`),
7021+
threadId,
7022+
messageId: MessageId.make(`${threadId}:wake`),
7023+
text: "Background task completed.",
7024+
attachments: [],
7025+
dispatchMode: { type: "queue_after_active" },
7026+
notification: {
7027+
source: { kind: "background_task" },
7028+
outcome: "updated",
7029+
summary: "Background task completed.",
7030+
},
7031+
});
7032+
const after = yield* orchestrator.getThreadProjection(threadId);
7033+
assert.equal(after.runs[2]?.status, "starting");
7034+
assert.isTrue(after.runs.find((run) => run.id === held.id)?.queueHeld);
7035+
}),
7036+
);
7037+
69597038
it.effect("edits and removes queued runs", () =>
69607039
Effect.gen(function* () {
69617040
const orchestrator = yield* Orchestrator.OrchestratorV2;

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

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,37 +2550,33 @@ describe("orchestration v2 provider switching", () => {
25502550
it.live.each([
25512551
[
25522552
"detaches the previous instance's session when a send hands off to another instance",
2553-
{ background: false, queued: false, sharedHistory: false, buffered: false, held: false },
2553+
{ background: false, queued: false, sharedHistory: false, buffered: false },
25542554
],
25552555
[
25562556
"keeps a busy previous session running when a direct send hands off",
2557-
{ background: true, queued: false, sharedHistory: false, buffered: false, held: false },
2557+
{ background: true, queued: false, sharedHistory: false, buffered: false },
25582558
],
25592559
[
25602560
"resumes a direct account switch when a background tool finishes",
2561-
{ background: true, queued: false, sharedHistory: true, buffered: false, held: false },
2561+
{ background: true, queued: false, sharedHistory: true, buffered: false },
25622562
],
25632563
[
25642564
"keeps a busy previous session running when a queued hand-off starts",
2565-
{ background: true, queued: true, sharedHistory: false, buffered: false, held: false },
2565+
{ background: true, queued: true, sharedHistory: false, buffered: false },
25662566
],
25672567
[
25682568
"waits for background work before resuming a queued account switch",
2569-
{ background: true, queued: true, sharedHistory: true, buffered: false, held: false },
2569+
{ background: true, queued: true, sharedHistory: true, buffered: false },
25702570
],
25712571
[
25722572
"drains buffered provider output before resuming a waiting account switch",
2573-
{ background: true, queued: false, sharedHistory: true, buffered: true, held: false },
2573+
{ background: true, queued: false, sharedHistory: true, buffered: true },
25742574
],
25752575
[
25762576
"drains buffered output queued before the predecessor settles",
2577-
{ background: true, queued: true, sharedHistory: true, buffered: true, held: false },
2577+
{ background: true, queued: true, sharedHistory: true, buffered: true },
25782578
],
2579-
[
2580-
"keeps buffered provider wakes held when Stop holds the waiting account switch",
2581-
{ background: true, queued: false, sharedHistory: true, buffered: true, held: true },
2582-
],
2583-
] as const)("%s", ([, { background, queued, sharedHistory, buffered, held }]) =>
2579+
] as const)("%s", ([, { background, queued, sharedHistory, buffered }]) =>
25842580
Effect.scoped(
25852581
Effect.gen(function* () {
25862582
const cwd = yield* checkpointWorkspace("handoff-releases-session");
@@ -2767,25 +2763,8 @@ describe("orchestration v2 provider switching", () => {
27672763
yield* Ref.set(bufferedWake, buffered);
27682764
yield* Ref.set(backgroundWork, false);
27692765
if (buffered) {
2770-
if (held) {
2771-
yield* orchestrator.dispatch({
2772-
type: "thread.stop",
2773-
commandId: CommandId.make("command:handoff-buffered-stop"),
2774-
threadId: handoffThreadId,
2775-
});
2776-
yield* worker.drain();
2777-
}
27782766
yield* dispatchBufferedWake();
27792767
yield* worker.drain();
2780-
if (held) {
2781-
const stopped = yield* orchestrator.getThreadProjection(handoffThreadId);
2782-
assert.deepEqual(
2783-
stopped.runs.filter((run) => run.status === "queued").map((run) => run.queueHeld),
2784-
[true, true, true],
2785-
);
2786-
assert.lengthOf(yield* Ref.get(capturedTurns), 1);
2787-
return;
2788-
}
27892768
assert.isFalse(
27902769
yield* Ref.get(bufferedWake),
27912770
"The owning session must drain buffered output before the account switch",

0 commit comments

Comments
 (0)