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 22, 00:45 UTC
perf(server): opening a long thread reads from its latest segments, not its whole history (#33305)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Nov 10, 2026 at 03:29 UTC
61cb845d9699d0ecce5a9b660d3b32907914df8b
3 files changed
+188
-4
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+140
@@ -1,3 +1,5 @@
1
+import * as NodeSqlite from "node:sqlite";
2
+
3
import { assert, it, vi } from "@effect/vitest";
4
import {
5
EnvironmentId,
@@ -57,6 +59,13 @@ import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
59
60
import { layerReaderDatabase, traceSqlRouting } from "../persistence/testkit/SqliteRouting.ts";
61
62
+declare module "node:sqlite" {
63
+ interface StatementSync {
64
+ // Node 26 has this; @types/node 24 does not declare it yet.
65
+ stat(counter: "vmStep"): number;
66
+ }
67
+}
68
+
69
const layerTest = Layer.mergeAll(
70
ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
71
SqlitePersistence.layerMemory,
@@ -3356,6 +3365,137 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3365
yield* sql`UPDATE orchestration_v2_projection_turn_items SET ordinal = 999,
3366
payload_json = json_set(payload_json, '$.ordinal', 999)
3367
WHERE turn_item_id = 'turn-item:bounded-sql-history:999'`;
3368
+ // Work folded below the newest 502 visible rows still counts its whole group, so the
3369
+ // 100 commands after the messages stay hidden in the 398-item group.
3370
+ yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'assistant_message',
3371
+ payload_json = json_set(payload_json, '$.type', 'assistant_message', '$.text', 'Working',
3372
+ '$.messageId', turn_item_id, '$.streaming', json('false'))
3373
+ WHERE thread_id = ${threadId} AND ordinal BETWEEN 300 AND 899`;
3374
+ const straddledWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
3375
+ rowLimit: sqlPageLimit,
3376
+ userTurnLimit: 10,
3377
+ deferWork: true,
3378
+ });
3379
+ const straddledOrdinals = [
3380
+ ...Array.from({ length: sqlPageLimit }, (_, index) => 900 - sqlPageLimit + index),
3381
+ 1000,
3382
+ ];
3383
+ assert.deepEqual(
3384
+ straddledWindow.projection.turnItems.map((item) => item.ordinal),
3385
+ straddledOrdinals,
3386
+ );
3387
+ // A run that starts with work (a wake) has no segment start, so the group begins at its first item.
3388
+ const moveFirstItemToRun = (runId: string) => sql`
3389
+ UPDATE orchestration_v2_projection_turn_items SET run_id = ${runId},
3390
+ payload_json = json_set(payload_json, '$.runId', ${runId})
3391
+ WHERE thread_id = ${threadId} AND ordinal = 1`;
3392
+ yield* moveFirstItemToRun("run:bounded-sql-history:2");
3393
+ const wakeWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
3394
+ rowLimit: sqlPageLimit,
3395
+ userTurnLimit: 10,
3396
+ deferWork: true,
3397
+ });
3398
+ assert.deepEqual(
3399
+ wakeWindow.projection.turnItems.map((item) => item.ordinal),
3400
+ straddledOrdinals,
3401
+ );
3402
+ yield* moveFirstItemToRun(sharedRunId);
3403
+ // A failed error below the tail still blocks folding for its run. Only the SQL columns
3404
+ // change; the row sits below the window, so its payload is never decoded.
3405
+ // A steer at 100 starts a later segment, so the error sits below the scan start too.
3406
+ const setItem = (ordinal: number, type: string, status: string) => sql`
3407
+ UPDATE orchestration_v2_projection_turn_items SET type = ${type}, status = ${status}
3408
+ WHERE thread_id = ${threadId} AND ordinal = ${ordinal}`;
3409
+ yield* setItem(100, "user_message", "completed");
3410
+ yield* setItem(2, "error", "failed");
3411
+ const blockedWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
3412
+ rowLimit: sqlPageLimit,
3413
+ userTurnLimit: 10,
3414
+ deferWork: true,
3415
+ });
3416
+ assert.isFalse(
3417
+ blockedWindow.projection.visibleTurnItems.some((row) => row.item.deferredWork),
3418
+ );
3419
+ assert.deepEqual(
3420
+ blockedWindow.projection.turnItems.map((item) => item.ordinal),
3421
+ Array.from({ length: sqlPageLimit }, (_, index) => 1001 - sqlPageLimit + index),
3422
+ );
3423
+ yield* setItem(2, "command_execution", "completed");
3424
+ yield* setItem(100, "command_execution", "completed");
3425
+ yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
3426
+ payload_json = json_set(payload_json, '$.type', 'command_execution')
3427
+ WHERE thread_id = ${threadId} AND ordinal BETWEEN 300 AND 899`;
3428
+
3429
+ // Runless rows below a run that starts after the newest 502 visible rows stay in the window.
3430
+ yield* sql`UPDATE orchestration_v2_projection_turn_items
3431
+ SET run_id = CASE WHEN ordinal > 990 THEN 'run:bounded-sql-history:991' END,
3432
+ type = CASE WHEN ordinal = 991 THEN 'user_message' ELSE 'assistant_message' END,
3433
+ payload_json = CASE WHEN ordinal = 991
3434
+ THEN json_set(payload_json, '$.type', 'user_message', '$.runId', 'run:bounded-sql-history:991',
3435
+ '$.createdBy', 'user', '$.creationSource', 'web', '$.inputIntent', 'turn_start',
3436
+ '$.messageId', turn_item_id, '$.text', 'Turn 991', '$.attachments', json('[]'))
3437
+ ELSE json_set(payload_json, '$.type', 'assistant_message',
3438
+ '$.runId', CASE WHEN ordinal > 990 THEN 'run:bounded-sql-history:991' END,
3439
+ '$.text', 'Working', '$.messageId', turn_item_id, '$.streaming', json('false'))
3440
+ END
3441
+ WHERE thread_id = ${threadId} AND ordinal > 1`;
3442
+ const windowStatements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
3443
+ const runlessWindow = yield* projectionStore
3444
+ .getThreadSnapshotWindow(threadId, {
3445
+ rowLimit: sqlPageLimit,
3446
+ userTurnLimit: 10,
3447
+ deferWork: true,
3448
+ })
3449
+ .pipe(
3450
+ Effect.provideService(Statement.CurrentTransformer, (statement) => {
3451
+ windowStatements.push(statement.compile());
3452
+ return Effect.succeed(statement);
3453
+ }),
3454
+ );
3455
+ // The window read must not grow with history below the newest visible rows. VM steps are
3456
+ // deterministic; reading the whole history here cost 65% more steps.
3457
+ const copyPath = "file:/thread-window-steps?vfs=memdb";
3458
+ {
3459
+ using copy = new NodeSqlite.DatabaseSync(copyPath);
3460
+ yield* sql`VACUUM INTO ${copyPath}`;
3461
+ assert.isNotEmpty(windowStatements);
3462
+ const windowSteps = () =>
3463
+ windowStatements.reduce((steps, [query, params]) => {
3464
+ const statement = copy.prepare(query);
3465
+ statement.all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>));
3466
+ return steps + statement.stat("vmStep");
3467
+ }, 0);
3468
+ const fullSteps = windowSteps();
3469
+ assert.isAbove(fullSteps, 0);
3470
+ copy.exec(
3471
+ "DELETE FROM orchestration_v2_projection_turn_items WHERE ordinal BETWEEN 2 AND 498",
3472
+ );
3473
+ assert.isAtMost(fullSteps, windowSteps() * 1.1);
3474
+ }
3475
+ assert.deepEqual(
3476
+ runlessWindow.projection.turnItems.map((item) => item.ordinal),
3477
+ Array.from({ length: THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT }, (_, index) => 499 + index),
3478
+ );
3479
+ const runlessPage = buildBoundedThreadProjection({
3480
+ projection: runlessWindow.projection,
3481
+ snapshotSequence: runlessWindow.snapshotSequence,
3482
+ });
3483
+ assert.isTrue(runlessPage.hasMoreHistory);
3484
+ assert.isNotNull(runlessPage.historyCursor);
3485
+ // Limits SQLite reads differently from `tailRows` skip the tail bound and match main.
3486
+ const windowOrdinalCount = (rowLimit: number) =>
3487
+ projectionStore
3488
+ .getThreadSnapshotWindow(threadId, { rowLimit, userTurnLimit: 10, deferWork: true })
3489
+ .pipe(Effect.map((window) => window.projection.turnItems.length));
3490
+ assert.strictEqual(yield* windowOrdinalCount(502.5), THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT);
3491
+ yield* sql`UPDATE orchestration_v2_projection_turn_items
3492
+ SET payload_json = json_set(payload_json, '$.inputIntent', 'steer')
3493
+ WHERE thread_id = ${threadId} AND ordinal = 991`;
3494
+ assert.strictEqual(yield* windowOrdinalCount(-1), 1000);
3495
+ yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = ${sharedRunId},
3496
+ type = 'command_execution',
3497
+ payload_json = json_set(payload_json, '$.type', 'command_execution', '$.runId', ${sharedRunId})
3498
+ WHERE thread_id = ${threadId} AND ordinal > 1`;
3499
yield* sql`
3500
UPDATE orchestration_v2_projection_turn_items
3501
SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution')
apps/server/src/orchestration-v2/ProjectionStore.ts
+47
-3
@@ -101,6 +101,7 @@ import {
101
isConversationHistoryItem,
102
THREAD_HISTORY_MAX_RAW_TURNS,
103
missingInterruptRequests,
104
+ DEFERRED_WORK_ITEM_TYPES,
105
deferWorkHistoryRows,
106
selectDeferredWorkPage,
107
encodeThreadHistoryCursor,
@@ -3032,6 +3033,48 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3033
SELECT ordinal, (SELECT MAX(ordinal) FROM user_anchors) AS latest FROM boundary
3034
`;
3035
const boundary = anchors[0]!;
3036
+ // Folding needs whole segments, so the window would walk every item since the boundary.
3037
+ // Non-deferrable types always keep their own row, so the window stays above the newest
3038
+ // `tailRows` of them. Start at the latest segment start of each run that reaches that
3039
+ // point, clamped to it so runless rows below stay in the scan.
3040
+ const tailRows = Math.max(window.rowLimit, THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT);
3041
+ const tailWorkStart = sql<{ readonly ordinal: number | null }>`
3042
+ ${eligible}, visible_tail AS (
3043
+ SELECT ordinal FROM eligible
3044
+ WHERE ordinal >= ${boundary.ordinal}
3045
+ AND type NOT IN ${sql.in(DEFERRED_WORK_ITEM_TYPES)}
3046
+ ORDER BY ordinal DESC
3047
+ LIMIT ${tailRows}
3048
+ ), tail AS (
3049
+ SELECT MIN(ordinal) AS ordinal FROM visible_tail HAVING COUNT(*) >= ${tailRows}
3050
+ ), tail_runs AS (
3051
+ SELECT DISTINCT item.run_id
3052
+ FROM tail CROSS JOIN eligible AS item
3053
+ WHERE item.ordinal >= tail.ordinal AND item.run_id IS NOT NULL
3054
+ )
3055
+ SELECT MIN(MIN(COALESCE(
3056
+ (
3057
+ SELECT start.ordinal FROM eligible AS start
3058
+ WHERE start.run_id = tail_runs.run_id AND start.type = 'user_message'
3059
+ AND start.ordinal <= tail.ordinal
3060
+ ORDER BY start.ordinal DESC LIMIT 1
3061
+ ),
3062
+ (
3063
+ SELECT MIN(first.ordinal) FROM orchestration_v2_projection_turn_items AS first
3064
+ WHERE first.thread_id = ${threadId} AND first.run_id = tail_runs.run_id
3065
+ ),
3066
+ tail.ordinal
3067
+ ), tail.ordinal)) AS ordinal
3068
+ FROM tail LEFT JOIN tail_runs
3069
+ `;
3070
+ // A negative or fractional limit makes SQLite's LIMIT disagree with `tailRows`; keep the old start.
3071
+ const workStart =
3072
+ window.deferWork === true &&
3073
+ boundary.latest !== null &&
3074
+ Number.isSafeInteger(window.rowLimit) &&
3075
+ window.rowLimit >= 0
3076
+ ? Math.max(boundary.ordinal, (yield* tailWorkStart)[0]?.ordinal ?? 0)
3077
+ : boundary.ordinal;
3078
return sql`
3079
${eligible}, blocked_runs AS MATERIALIZED (
3080
SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items
@@ -3039,7 +3082,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3082
AND ${window.deferWork ? 1 : 0} = 1
3083
AND run_id IN (
3084
SELECT run_id FROM eligible
3042
- WHERE ordinal >= ${boundary.ordinal} AND run_id IS NOT NULL
3085
+ WHERE ordinal >= ${workStart} AND run_id IS NOT NULL
3086
)
3087
AND (type IN ('run_interrupt_request', 'run_interrupt_result')
3088
OR (type = 'error' AND status = 'failed' AND parent_item_id IS NULL))
@@ -3048,14 +3091,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3091
MAX(CASE WHEN type = 'user_message' THEN ordinal ELSE 0 END)
3092
OVER (PARTITION BY run_id ORDER BY ordinal) AS segment,
3093
CASE WHEN status = 'completed' AND (run_status = 'completed' OR attempt_status = 'superseded')
3051
- AND type IN ('command_execution', 'reasoning', 'file_change', 'file_search', 'web_search', 'dynamic_tool')
3094
+ AND type IN ${sql.in(DEFERRED_WORK_ITEM_TYPES)}
3095
AND NOT (${inlineWorkOutput})
3096
AND run_id NOT IN (SELECT run_id FROM blocked_runs)
3097
THEN 1 ELSE 0 END AS can_defer
3098
FROM eligible AS item
3099
WHERE ${window.deferWork ? 1 : 0} = 1
3100
AND ${boundary.latest === null ? 0 : 1} = 1
3058
- AND ordinal >= ${boundary.ordinal}
3101
+ AND ordinal >= ${workStart}
3102
), deferred_work AS (
3103
SELECT run_id, segment, attempt_id, MIN(ordinal) AS first_ordinal,
3104
MAX(ordinal) AS last_ordinal, COUNT(*) AS item_count
@@ -3090,6 +3133,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3133
FROM window_items
3134
WHERE ordinal >= ${boundary.ordinal}
3135
ORDER BY ordinal DESC, turn_item_id DESC
3136
+ -- Every LIMIT here and in latest_user_turn must stay <= tailRows, or rows below workStart go missing.
3137
LIMIT CASE
3138
WHEN ${window.rowLimit} = 0 THEN 0
3139
WHEN (SELECT row_count FROM latest_user_turn)
apps/server/src/orchestration-v2/threadHistoryPaging.ts
+1
-1
@@ -183,7 +183,7 @@ function isThreadHistoryTurnStart(item: HistoryRow["item"]): boolean {
183
);
184
}
185
186
-const DEFERRED_WORK_ITEM_TYPES = [
186
+export const DEFERRED_WORK_ITEM_TYPES = [
187
"command_execution",
188
"reasoning",
189
"file_change",