The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 13:30 UTC
fix(server): a steer that misses its turn moves into the run it starts (#36855)
Julius Marminge committed
Nov 14, 2026 at 10:14 UTC
8cbaf750558fbffc2f26d33aca60e01bd2da14f3
4 files changed
+108
-20
apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
+55
-1
@@ -1390,11 +1390,32 @@ it.effect("starts a steer that missed the turn on the saved next-turn selection"
1390
assert.equal(started[1]?.message.messageId, MessageId.make("message:steer"));
1391
assert.deepEqual(started[1]?.modelSelection, composerSelection);
1392
assert.deepEqual(projection.thread.modelSelection, composerSelection);
1393
+ const message = projection.turnItems.find(
1394
+ (item) =>
1395
+ item.type === "user_message" && item.messageId === MessageId.make("message:steer"),
1396
+ );
1397
+ assert.equal(message?.runId, started[1]!.runId);
1398
+ assert.isAbove(
1399
+ message!.ordinal,
1400
+ Math.max(
1401
+ ...projection.turnItems
1402
+ .filter((item) => item.runId === first.runId)
1403
+ .map((item) => item.ordinal),
1404
+ ),
1405
+ );
1406
}).pipe(Effect.provide(layer));
1407
}),
1408
);
1409
1397
-it.effect.each(["complete", "stop-before-decline", "stop-after-decline", "queued-model"] as const)(
1410
+const declinedSteerTimings = [
1411
+ "complete",
1412
+ "stop-before-decline",
1413
+ "stop-after-decline",
1414
+ "queued-model",
1415
+ "two-steers",
1416
+] as const;
1417
+
1418
+it.effect.each(declinedSteerTimings)(
1419
"persists a declined steer without blocking the worker: %s",
1420
(timing) =>
1421
Effect.gen(function* () {
@@ -1465,6 +1486,26 @@ it.effect.each(["complete", "stop-before-decline", "stop-after-decline", "queued
1486
}),
1487
).pipe(Effect.asVoid, Effect.orDie);
1488
}
1489
+ if (timing === "two-steers") {
1490
+ // The second steer arrives before the first is declined.
1491
+ beforeDecline = orchestrator
1492
+ .dispatch({
1493
+ type: "message.dispatch",
1494
+ commandId: CommandId.make("steer-second"),
1495
+ threadId,
1496
+ messageId: MessageId.make("message:steer-second"),
1497
+ text: "second",
1498
+ attachments: [],
1499
+ dispatchMode: { type: "steer_active", targetRunId: first.runId },
1500
+ createdBy: "user",
1501
+ creationSource: "web",
1502
+ })
1503
+ .pipe(
1504
+ Effect.andThen(Effect.sync(() => (beforeDecline = Effect.void))),
1505
+ Effect.asVoid,
1506
+ Effect.orDie,
1507
+ );
1508
+ }
1509
const messageId = MessageId.make("message:declined");
1510
yield* orchestrator.dispatch({
1511
type: "message.dispatch",
@@ -1517,6 +1558,19 @@ it.effect.each(["complete", "stop-before-decline", "stop-after-decline", "queued
1558
);
1559
return;
1560
}
1561
+ if (timing === "two-steers") {
1562
+ assert.deepEqual(
1563
+ pending.runs
1564
+ .filter((run) => run.status === "queued")
1565
+ .toSorted(
1566
+ (left, right) =>
1567
+ (left.queuePosition ?? left.ordinal) - (right.queuePosition ?? right.ordinal),
1568
+ )
1569
+ .map((run) => run.userMessageId),
1570
+ [messageId, MessageId.make("message:steer-second")],
1571
+ );
1572
+ return;
1573
+ }
1574
if (timing === "stop-after-decline") {
1575
yield* stop;
1576
yield* worker.drain();
apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts
+1
-1
@@ -48,7 +48,7 @@ it.effect("reuses an item's position without attempting another allocation inser
48
runId: RunId.make("run:moved"),
49
runOrdinal: 2,
50
}),
51
- first,
51
+ 2_000_001,
52
);
53
assert.equal(
54
yield* positions.allocate({ ...input, threadId: ThreadId.make("thread:other") }),
apps/server/src/orchestration-v2/TurnItemPositionStore.ts
+44
-18
@@ -24,6 +24,8 @@ export interface TurnItemPositionStoreV2Shape {
24
readonly runOrdinal?: number;
25
// `run_start` takes the run's unused first slot, ahead of its items, and appends once it is taken.
26
readonly placement?: "append" | "run_start";
27
+ // Keep an existing position even when it lies outside the run's range.
28
+ readonly keepExisting?: boolean;
29
}) => Effect.Effect<number, TurnItemPositionStoreError>;
30
readonly normalize: (
31
item: OrchestrationV2TurnItem,
@@ -50,6 +52,7 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
52
runId,
53
runOrdinal: suppliedRunOrdinal,
54
placement = "append",
55
+ keepExisting = false,
56
}) {
57
const existing = yield* sql<{ readonly ordinal: number }>`
58
SELECT ordinal
@@ -57,10 +60,8 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
60
WHERE thread_id = ${threadId} AND turn_item_id = ${turnItemId}
61
LIMIT 1
62
`;
60
- if (existing[0] !== undefined) return existing[0].ordinal;
61
-
63
const runRows =
63
- runId === null
64
+ runId === null || suppliedRunOrdinal !== undefined
65
? []
66
: yield* sql<{ readonly ordinal: number }>`
67
SELECT ordinal
@@ -71,22 +72,45 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
72
const runOrdinal = suppliedRunOrdinal ?? runRows[0]?.ordinal ?? null;
73
const lowerBound = runOrdinal === null ? 0 : runOrdinal * 1_000_000;
74
const upperBound = runOrdinal === null ? 999_999 : lowerBound + 999_999;
74
- yield* sql`
75
- INSERT INTO orchestration_v2_turn_item_positions (thread_id, turn_item_id, ordinal)
76
- SELECT
77
- ${threadId},
78
- ${turnItemId},
79
- ${
80
- placement === "run_start"
81
- ? sql`CASE WHEN COALESCE(MIN(ordinal), ${upperBound}) > ${lowerBound} THEN ${lowerBound} ELSE MAX(ordinal) + 1 END`
82
- : sql`COALESCE(MAX(ordinal), ${lowerBound}) + 1`
75
+ const current = existing[0]?.ordinal;
76
+ if (current !== undefined) {
77
+ // A steer that missed its turn starts the next run; it moves into that run's range.
78
+ if (
79
+ keepExisting ||
80
+ runOrdinal === null ||
81
+ (current >= lowerBound && current <= upperBound)
82
+ ) {
83
+ return current;
84
}
84
- FROM orchestration_v2_turn_item_positions
85
- WHERE thread_id = ${threadId}
86
- AND ordinal >= ${lowerBound}
87
- AND ordinal <= ${upperBound}
88
- ON CONFLICT(thread_id, turn_item_id) DO NOTHING
89
- `;
85
+ yield* sql`
86
+ UPDATE orchestration_v2_turn_item_positions
87
+ SET ordinal = (
88
+ SELECT COALESCE(MAX(ordinal), ${lowerBound}) + 1
89
+ FROM orchestration_v2_turn_item_positions
90
+ WHERE thread_id = ${threadId}
91
+ AND ordinal >= ${lowerBound}
92
+ AND ordinal <= ${upperBound}
93
+ )
94
+ WHERE thread_id = ${threadId} AND turn_item_id = ${turnItemId}
95
+ `;
96
+ } else {
97
+ yield* sql`
98
+ INSERT INTO orchestration_v2_turn_item_positions (thread_id, turn_item_id, ordinal)
99
+ SELECT
100
+ ${threadId},
101
+ ${turnItemId},
102
+ ${
103
+ placement === "run_start"
104
+ ? sql`CASE WHEN COALESCE(MIN(ordinal), ${upperBound}) > ${lowerBound} THEN ${lowerBound} ELSE MAX(ordinal) + 1 END`
105
+ : sql`COALESCE(MAX(ordinal), ${lowerBound}) + 1`
106
+ }
107
+ FROM orchestration_v2_turn_item_positions
108
+ WHERE thread_id = ${threadId}
109
+ AND ordinal >= ${lowerBound}
110
+ AND ordinal <= ${upperBound}
111
+ ON CONFLICT(thread_id, turn_item_id) DO NOTHING
112
+ `;
113
+ }
114
const rows = yield* sql<{ readonly ordinal: number }>`
115
SELECT ordinal
116
FROM orchestration_v2_turn_item_positions
@@ -121,6 +145,8 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
145
threadId: item.threadId,
146
turnItemId: item.id,
147
runId: item.runId,
148
+ // Queued steers keep their submission order until their run starts (see Orchestrator).
149
+ keepExisting: item.status === "pending",
150
...(runOrdinal === undefined ? {} : { runOrdinal }),
151
}).pipe(Effect.map((ordinal) => (item.ordinal === ordinal ? item : { ...item, ordinal })));
152
}),
apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+8
@@ -4075,6 +4075,8 @@ describe("orchestration v2 provider switching", () => {
4075
if (restored?.type === "turn-item.updated") {
4076
assert.deepEqual(restored.payload, {
4077
...queuedItem,
4078
+ // Its run is now the second one, so it moves into that run's positions.
4079
+ ordinal: 2_000_001,
4080
status: "failed",
4081
completedAt: restored.occurredAt,
4082
updatedAt: restored.occurredAt,
@@ -4129,6 +4131,12 @@ describe("orchestration v2 provider switching", () => {
4131
failureItem?.type === "error" ? failureItem.failure.code : null,
4132
"context_handoff_unsupported",
4133
);
4134
+ assert.isBelow(
4135
+ projection.turnItems.find(
4136
+ (item) => item.type === "user_message" && item.messageId === rejectedMessageId,
4137
+ )!.ordinal,
4138
+ failureItem!.ordinal,
4139
+ );
4140
assert.isTrue(
4141
projection.visibleTurnItems.some(
4142
({ item }) => item.type === "user_message" && item.messageId === rejectedMessageId,