The best way to control Claude Code, Codex and more from any computer or phone
fix(server): deferred-steer check and queued cancel no longer read a thread's whole history (#36100)
Julius Marminge committed
Nov 13, 2026 at 01:49 UTC
10fbff5ab6d31c494b5c6cfd759d78c12aa731f6
2 files changed
+131
-3
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+121
-1
@@ -2468,6 +2468,19 @@ it.effect("queued-message commands read a long thread's history without walking
2468
}),
2469
);
2470
walks.push(...(yield* historyWalks("queued-message.promote-to-steer")));
2471
+ yield* dispatch("cancelled");
2472
+ const cancelledRun = (yield* projections.getThreadProjection(threadId)).runs.at(-1)!;
2473
+ assert.equal(cancelledRun.status, "queued");
2474
+ statements.splice(0);
2475
+ yield* observed(
2476
+ orchestrator.dispatch({
2477
+ type: "queued-run.cancel",
2478
+ commandId: CommandId.make("cancel:long-history-reads"),
2479
+ threadId,
2480
+ runId: cancelledRun.id,
2481
+ }),
2482
+ );
2483
+ walks.push(...(yield* historyWalks("queued-run.cancel")));
2484
yield* observed(
2485
orchestrator.dispatch({
2486
type: "run.interrupt",
@@ -2481,7 +2494,14 @@ it.effect("queued-message commands read a long thread's history without walking
2494
const after = yield* projections.getThreadProjection(threadId);
2495
assert.deepEqual(
2496
after.messages.filter((message) => message.role === "user").map((message) => message.text),
2484
- ["Request 1", "Request 2", "Request 3", "/compact", "Request queued, edited"],
2497
+ [
2498
+ "Request 1",
2499
+ "Request 2",
2500
+ "Request 3",
2501
+ "/compact",
2502
+ "Request cancelled",
2503
+ "Request queued, edited",
2504
+ ],
2505
);
2506
assert.deepEqual(
2507
after.providerThreads.map((providerThread) => providerThread.id),
@@ -2490,3 +2510,103 @@ it.effect("queued-message commands read a long thread's history without walking
2510
assert.deepEqual(walks, [], walks.join("\n"));
2511
}).pipe(Effect.provide(layerTest)),
2512
);
2513
+
2514
+it.effect("a running provider-turn update reads only the thread's queued and running runs", () =>
2515
+ Effect.gen(function* () {
2516
+ const runsReads = yield* Deferred.make<ReturnType<Statement.Statement<unknown>["compile"]>>();
2517
+ let armed = false;
2518
+ // The deferred-steer reactor runs in a fiber forked by the layer, so observe at layer level.
2519
+ const observe: Statement.Transformer = (statement) => {
2520
+ const compiled = statement.compile();
2521
+ return armed && /FROM orchestration_v2_projection_runs\b/.test(compiled[0])
2522
+ ? Deferred.succeed(runsReads, compiled).pipe(Effect.as(statement))
2523
+ : Effect.succeed(statement);
2524
+ };
2525
+ yield* Effect.gen(function* () {
2526
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
2527
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
2528
+ const eventSink = yield* EventSink.EventSinkV2;
2529
+ const sql = yield* SqlClient.SqlClient;
2530
+ const threadId = ThreadId.make("thread:deferred-steer-reads");
2531
+ const driver = ProviderDriverKind.make("codex");
2532
+ const now = yield* DateTime.now;
2533
+ yield* orchestrator.dispatch({
2534
+ type: "thread.create",
2535
+ commandId: CommandId.make("create-deferred-steer-reads"),
2536
+ threadId,
2537
+ projectId: ProjectId.make("project:deferred-steer-reads"),
2538
+ title: "Deferred steer reads",
2539
+ modelSelection,
2540
+ runtimeMode: "full-access",
2541
+ interactionMode: "default",
2542
+ branch: null,
2543
+ worktreePath: null,
2544
+ createdBy: "user",
2545
+ creationSource: "web",
2546
+ });
2547
+ yield* orchestrator.dispatch({
2548
+ type: "message.dispatch",
2549
+ commandId: CommandId.make("dispatch:deferred-steer-reads"),
2550
+ threadId,
2551
+ messageId: MessageId.make("message:deferred-steer-reads"),
2552
+ text: "Request",
2553
+ attachments: [],
2554
+ dispatchMode: { type: "defer_start" },
2555
+ createdBy: "user",
2556
+ creationSource: "web",
2557
+ });
2558
+ const run = (yield* projections.getThreadProjection(threadId)).runs.at(-1)!;
2559
+ const providerThreadId = ProviderThreadId.make("provider-thread:deferred-steer-reads");
2560
+ yield* eventSink.write({
2561
+ events: [
2562
+ {
2563
+ id: EventId.make("event:deferred-steer-reads:run"),
2564
+ type: "run.updated",
2565
+ threadId,
2566
+ runId: run.id,
2567
+ occurredAt: now,
2568
+ payload: { ...run, providerThreadId, status: "running", queuePosition: null },
2569
+ },
2570
+ ],
2571
+ });
2572
+ armed = true;
2573
+ yield* eventSink.write({
2574
+ events: [
2575
+ {
2576
+ id: EventId.make("event:deferred-steer-reads:turn"),
2577
+ type: "provider-turn.updated",
2578
+ threadId,
2579
+ runId: run.id,
2580
+ occurredAt: now,
2581
+ payload: {
2582
+ id: ProviderTurnId.make("provider-turn:deferred-steer-reads"),
2583
+ providerThreadId,
2584
+ nodeId: run.rootNodeId!,
2585
+ runAttemptId: run.activeAttemptId!,
2586
+ nativeTurnRef: {
2587
+ driver,
2588
+ nativeId: "native:deferred-steer-reads",
2589
+ strength: "strong",
2590
+ },
2591
+ ordinal: 1,
2592
+ status: "running",
2593
+ startedAt: now,
2594
+ completedAt: null,
2595
+ },
2596
+ },
2597
+ ],
2598
+ });
2599
+ const [query, params] = yield* Deferred.await(runsReads);
2600
+ const plan = yield* sql.unsafe<{ detail: string }>(`EXPLAIN QUERY PLAN ${query}`, params);
2601
+ // Every run the thread ever had, when only queued steers and the running run matter.
2602
+ assert.isFalse(
2603
+ plan.some(({ detail }) => /^SEARCH .*runs.* \(thread_id=\?\)$/.test(detail)),
2604
+ `${plan.map(({ detail }) => detail).join("\n")}\n in: ${query.replace(/\s+/g, " ").trim()}`,
2605
+ );
2606
+ }).pipe(
2607
+ Effect.provide(
2608
+ layerTest.pipe(Layer.provide(Layer.succeed(Statement.CurrentTransformer, observe))),
2609
+ ),
2610
+ );
2611
+ }),
2612
+);
apps/server/src/orchestration-v2/Orchestrator.ts
+10
-2
@@ -11236,7 +11236,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
11236
const projection = yield* loadProjectionForCommand(
11237
command,
11238
["runs", "messages", "nodes", "attempts", "subagents", "turnItems"],
11239
- { turnItemTypes: [], messageRoles: ["user"] },
11239
+ // Cancelling reads the queued run's own message and root node, never a settled leaf.
11240
+ {
11241
+ turnItemTypes: [],
11242
+ messageRoles: ["user"],
11243
+ messageRunIds: [command.runId],
11244
+ omitSettledLeafNodes: true,
11245
+ },
11246
);
11247
const queuedRun = projection.runs.find((candidate) => candidate.id === command.runId);
11248
if (queuedRun === undefined || queuedRun.status !== "queued") {
@@ -15348,7 +15354,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15354
if (stored.event.type !== "provider-turn.updated") return;
15355
const threadId = stored.event.threadId;
15356
const turn = stored.event.payload;
15351
- const projection = yield* projectionStore.getThreadRecords(threadId, ["runs"]);
15357
+ const projection = yield* projectionStore.getThreadRecords(threadId, ["runs"], {
15358
+ runStatuses: ["queued", "running"],
15359
+ });
15360
const run = projection.runs.find(
15361
(candidate) =>
15362
candidate.activeAttemptId === turn.runAttemptId && candidate.status === "running",