Skip to content

Commit e059b62

Browse files
fix(server): Claude background-task wakes start when the queue is held
After a server restart, recovery holds unstarted queued runs. A Claude background-task wake that arrives while the previous run is finalizing queues, inherits that hold, and never starts. The CLI runs the turn anyway, so every T3 MCP call in it fails with parent_not_active. A provider wake no longer inherits queueHeld, and the queue can start an unheld run that sits behind held ones. Only provider wakes are queued unheld beside held runs, so ordinary queues behave as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dacd2cb commit e059b62

3 files changed

Lines changed: 80 additions & 10 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,14 @@ function delegatedTaskTerminalStatus(
604604
}
605605
}
606606

607+
/**
608+
* The next queued run that may start. Held runs wait for the user; the only
609+
* runs queued unheld beside them are provider wakes, which never wait.
610+
*/
607611
function nextQueuedRun(
608612
projection: Pick<OrchestrationV2ThreadProjection, "runs" | "messages">,
609613
): OrchestrationV2Run | undefined {
610-
return queuedRunsInDeliveryOrder(projection)[0];
614+
return queuedRunsInDeliveryOrder(projection).find((run) => run.queueHeld !== true);
611615
}
612616

613617
function latestStableRun(
@@ -1278,8 +1282,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12781282
if (
12791283
projection.thread.archivedAt !== null ||
12801284
projection.thread.deletedAt !== null ||
1281-
projection.runs.some(isBlockingRun) ||
1282-
projection.runs.some((run) => run.status === "queued" && run.queueHeld === true)
1285+
projection.runs.some(isBlockingRun)
12831286
) {
12841287
return;
12851288
}
@@ -4974,6 +4977,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
49744977
}),
49754978
),
49764979
);
4980+
const isProviderWake =
4981+
command.createdBy === "agent" && command.creationSource === "provider";
49774982
const run: OrchestrationV2Run = {
49784983
id: runId,
49794984
threadId: command.threadId,
@@ -4985,7 +4990,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
49854990
rootNodeId,
49864991
activeAttemptId: attemptId,
49874992
status: "queued",
4988-
...(projection.runs.some(
4993+
// A provider wake is not held with the queue: the provider is already
4994+
// running that turn, and this run only attaches its output. Held, the
4995+
// turn would run with no T3 run, so its T3 tool calls fail.
4996+
...(!isProviderWake &&
4997+
projection.runs.some(
49894998
(candidate) => candidate.status === "queued" && candidate.queueHeld === true,
49904999
)
49915000
? { queueHeld: true }

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4686,11 +4686,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
46864686
SELECT 1 AS ready FROM orchestration_v2_projection_threads AS thread
46874687
WHERE thread.thread_id = ${threadId} AND thread.archived_at IS NULL AND thread.deleted_at IS NULL
46884688
AND EXISTS (SELECT 1 FROM orchestration_v2_projection_runs
4689-
WHERE thread_id = ${threadId} AND status = 'queued')
4689+
WHERE thread_id = ${threadId} AND status = 'queued'
4690+
AND json_extract(payload_json, '$.queueHeld') IS NOT 1)
46904691
AND NOT EXISTS (SELECT 1 FROM orchestration_v2_projection_runs
46914692
WHERE thread_id = ${threadId}
4692-
AND (status IN ('preparing', 'starting', 'running', 'waiting')
4693-
OR (status = 'queued' AND json_extract(payload_json, '$.queueHeld') = 1)))`;
4693+
AND status IN ('preparing', 'starting', 'running', 'waiting'))`;
46944694
return rows.length > 0;
46954695
}),
46964696
)
@@ -6456,14 +6456,13 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
64566456
return (
64576457
projection.thread.archivedAt === null &&
64586458
projection.thread.deletedAt === null &&
6459-
projection.runs.some((run) => run.status === "queued") &&
6459+
projection.runs.some((run) => run.status === "queued" && run.queueHeld !== true) &&
64606460
!projection.runs.some(
64616461
(run) =>
64626462
run.status === "preparing" ||
64636463
run.status === "starting" ||
64646464
run.status === "running" ||
6465-
run.status === "waiting" ||
6466-
(run.status === "queued" && run.queueHeld === true),
6465+
run.status === "waiting",
64676466
)
64686467
);
64696468
}),

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4286,6 +4286,68 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
42864286
}),
42874287
);
42884288

4289+
it.effect("starts a provider wake queued beside a held queue", () =>
4290+
Effect.gen(function* () {
4291+
const orchestrator = yield* Orchestrator.OrchestratorV2;
4292+
const recovery = yield* ProviderRuntimeRecoveryService.ProviderRuntimeRecoveryService;
4293+
const events = yield* EventSink.EventSinkV2;
4294+
const threadId = ThreadId.make("queue-hold-provider-wake");
4295+
yield* orchestrator.dispatch({
4296+
type: "thread.create",
4297+
createdBy: "user",
4298+
creationSource: "web",
4299+
commandId: CommandId.make(`${threadId}:create`),
4300+
threadId,
4301+
projectId: ProjectId.make(`${threadId}:project`),
4302+
title: "Wake beside a held queue",
4303+
modelSelection,
4304+
runtimeMode: "full-access",
4305+
interactionMode: "default",
4306+
branch: null,
4307+
worktreePath: process.cwd(),
4308+
});
4309+
const send = (index: number, text: string, provider = false) =>
4310+
orchestrator.dispatch({
4311+
type: "message.dispatch",
4312+
createdBy: provider ? "agent" : "user",
4313+
creationSource: provider ? "provider" : "web",
4314+
commandId: CommandId.make(`${threadId}:message:${index}`),
4315+
threadId,
4316+
messageId: MessageId.make(`${threadId}:message:${index}`),
4317+
text,
4318+
attachments: [],
4319+
modelSelection,
4320+
dispatchMode: { type: index === 0 ? "start_immediately" : "queue_after_active" },
4321+
});
4322+
yield* send(0, "Active");
4323+
yield* send(1, "Queued before the restart");
4324+
yield* recovery.reconcile("startup");
4325+
yield* send(2, "After the restart");
4326+
yield* send(3, "Background task completed.", true);
4327+
const queued = yield* orchestrator.getThreadProjection(threadId);
4328+
const [held, active, wake] = [queued.runs[1]!, queued.runs[2]!, queued.runs[3]!];
4329+
assert.isTrue(held.queueHeld);
4330+
assert.notEqual(wake.queueHeld, true);
4331+
const now = yield* DateTime.now;
4332+
yield* events.write({
4333+
events: [
4334+
{
4335+
id: EventId.make(`${threadId}:active-done`),
4336+
type: "run.updated",
4337+
threadId,
4338+
runId: active.id,
4339+
occurredAt: now,
4340+
payload: { ...active, status: "completed", completedAt: now },
4341+
},
4342+
],
4343+
});
4344+
yield* orchestrator.resumeQueuedRuns;
4345+
const after = yield* orchestrator.getThreadProjection(threadId);
4346+
assert.equal(after.runs.find((run) => run.id === wake.id)?.status, "starting");
4347+
assert.equal(after.runs.find((run) => run.id === held.id)?.status, "queued");
4348+
}),
4349+
);
4350+
42894351
it.effect("edits and removes queued runs", () =>
42904352
Effect.gen(function* () {
42914353
const orchestrator = yield* Orchestrator.OrchestratorV2;

0 commit comments

Comments
 (0)