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, 22:15 UTC
perf(server): very long threads open about 10x faster (#34490)
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Theo Browne committed
Nov 11, 2026 at 12:12 UTC
b015ffd7c2d360aed7debb52a3b64483ab4466ab
5 files changed
+306
-40
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+151
-1
@@ -1562,7 +1562,9 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
1562
["settled-reasoning", settledRun, "reasoning", "cancelled"],
1563
["settled-subagent", settledRun, "subagent", "completed"],
1564
["settled-run-live-tool", settledRun, "tool_call", "running"],
1565
+ ["settled-run-todo", settledRun, "todo_list", "running"],
1566
["active-run-done-tool", activeRun, "tool_call", "completed"],
1567
+ ["active-run-todo", activeRun, "todo_list", "running"],
1568
] as const;
1569
for (const [name, run, kind, status] of nodes) {
1570
const id = NodeId.make(`node:settled-leaf-nodes:${name}`);
@@ -1592,7 +1594,49 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
1594
completedAt: status === "running" ? null : now,
1595
},
1596
});
1597
+ if (kind === "todo_list") {
1598
+ yield* store.apply({
1599
+ id: EventId.make(`event:settled-leaf-nodes:${name}:plan`),
1600
+ type: "plan.updated",
1601
+ threadId,
1602
+ runId: run.id,
1603
+ nodeId: id,
1604
+ driver,
1605
+ occurredAt: now,
1606
+ payload: {
1607
+ id: PlanId.make(`plan:settled-leaf-nodes:${name}`),
1608
+ threadId,
1609
+ runId: run.id,
1610
+ nodeId: id,
1611
+ status: "active",
1612
+ kind: "todo_list",
1613
+ steps: [],
1614
+ },
1615
+ });
1616
+ }
1617
}
1618
+ // No row or node of this run is in the window.
1619
+ const settledCheckpointId = CheckpointId.make("checkpoint:settled-leaf-nodes:earlier-run");
1620
+ yield* store.apply({
1621
+ id: EventId.make("event:settled-leaf-nodes:earlier-run-checkpoint"),
1622
+ type: "checkpoint.captured",
1623
+ threadId,
1624
+ occurredAt: now,
1625
+ payload: {
1626
+ id: settledCheckpointId,
1627
+ threadId,
1628
+ scopeId: CheckpointScopeId.make("scope:settled-leaf-nodes:root"),
1629
+ runId: RunId.make("run:settled-leaf-nodes:earlier"),
1630
+ nodeId: NodeId.make("node:settled-leaf-nodes:earlier-root"),
1631
+ parentCheckpointId: null,
1632
+ ordinalWithinScope: 1,
1633
+ appRunOrdinal: 1,
1634
+ ref: CheckpointRef.make("refs/t3/settled-leaf-nodes/1"),
1635
+ status: "ready",
1636
+ files: [],
1637
+ capturedAt: now,
1638
+ },
1639
+ });
1640
const nodeNames = (records: { readonly nodes: ReadonlyArray<{ readonly id: string }> }) =>
1641
records.nodes
1642
.map((node) => node.id.replace("node:settled-leaf-nodes:", ""))
@@ -1601,12 +1645,32 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
1645
1646
assert.deepEqual(
1647
nodeNames(yield* store.getThreadRecords(threadId, ["nodes"], { omitSettledLeafNodes: true })),
1604
- ["active-run-done-tool", "settled-root", "settled-run-live-tool", "settled-subagent"],
1648
+ [
1649
+ "active-run-done-tool",
1650
+ "active-run-todo",
1651
+ "settled-root",
1652
+ "settled-run-live-tool",
1653
+ "settled-run-todo",
1654
+ "settled-subagent",
1655
+ ],
1656
);
1657
assert.deepEqual(
1658
nodeNames(yield* store.getThreadRecords(threadId, ["nodes"])),
1659
nodes.map(([name]) => name).toSorted(),
1660
);
1661
+ // A todo list stays running after its run ends. Counted as live work, every old run of a
1662
+ // long thread would ride along with each window, with its nodes and subagents.
1663
+ const liveWindow = (yield* store.getThreadSnapshotWindow(threadId, { rowLimit: 1 })).projection;
1664
+ assert.deepEqual(nodeNames(liveWindow), ["active-run-todo", "settled-run-live-tool"]);
1665
+ assert.deepEqual(
1666
+ liveWindow.plans.map((plan) => plan.id),
1667
+ [PlanId.make("plan:settled-leaf-nodes:active-run-todo")],
1668
+ );
1669
+ // Clients show the diff of the last finished turn from this record.
1670
+ assert.deepEqual(
1671
+ liveWindow.checkpoints.map((checkpoint) => checkpoint.id),
1672
+ [settledCheckpointId],
1673
+ );
1674
1675
const subagents = [
1676
["delivered", settledRun, "completed", "delivered"],
@@ -3663,6 +3727,92 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3727
SET type = 'user_message', payload_json = json_set(payload_json, '$.type', 'user_message')
3728
WHERE thread_id = ${threadId} AND ordinal = 1
3729
`;
3730
+ // A running run folds nothing, so its newest rows are the window. Reading them must not
3731
+ // walk the rest of the turn: 999 commands here, tens of thousands after a day of work.
3732
+ const setSharedRunStatus = (status: "running" | "completed") => sql`
3733
+ UPDATE orchestration_v2_projection_runs
3734
+ SET status = ${status}, payload_json = json_set(payload_json, '$.status', ${status})
3735
+ WHERE run_id = ${sharedRunId}`;
3736
+ yield* setSharedRunStatus("running");
3737
+ // Another thread's rows that carry this run's id must not count toward the tail.
3738
+ const foreignThreadId = "thread:bounded-sql-history:foreign";
3739
+ yield* sql`
3740
+ INSERT INTO orchestration_v2_projection_turn_items (
3741
+ turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
3742
+ parent_item_id, ordinal, type, status, updated_at, payload_json
3743
+ )
3744
+ SELECT turn_item_id || ':foreign', ${foreignThreadId}, run_id, NULL, NULL, NULL,
3745
+ NULL, ordinal + 5000, type, status, updated_at, payload_json
3746
+ FROM orchestration_v2_projection_turn_items
3747
+ WHERE thread_id = ${threadId} AND ordinal > 400`;
3748
+ const liveStatements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
3749
+ const liveWindow = yield* projectionStore
3750
+ .getThreadSnapshotWindow(threadId, {
3751
+ rowLimit: sqlPageLimit,
3752
+ userTurnLimit: 10,
3753
+ deferWork: true,
3754
+ })
3755
+ .pipe(
3756
+ Effect.provideService(Statement.CurrentTransformer, (statement) => {
3757
+ liveStatements.push(statement.compile());
3758
+ return Effect.succeed(statement);
3759
+ }),
3760
+ );
3761
+ assert.deepEqual(
3762
+ liveWindow.projection.turnItems.map((item) => item.ordinal),
3763
+ Array.from({ length: sqlPageLimit }, (_, index) => 1001 - sqlPageLimit + index),
3764
+ );
3765
+ {
3766
+ const livePath = "file:/thread-window-live-steps?vfs=memdb";
3767
+ using copy = new NodeSqlite.DatabaseSync(livePath);
3768
+ yield* sql`VACUUM INTO ${livePath}`;
3769
+ const liveSteps = (statements = liveStatements) =>
3770
+ statements.reduce((steps, [query, params]) => {
3771
+ const statement = copy.prepare(query);
3772
+ statement.all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>));
3773
+ return steps + statement.stat("vmStep");
3774
+ }, 0);
3775
+ const deleteOrdinals = (from: number, to: number) =>
3776
+ copy.exec(
3777
+ `DELETE FROM orchestration_v2_projection_turn_items WHERE ordinal BETWEEN ${from} AND ${to}`,
3778
+ );
3779
+ // Nothing below the newest 502 rows is read to find the tail.
3780
+ const fullSteps = liveSteps();
3781
+ assert.isAbove(fullSteps, 0);
3782
+ deleteOrdinals(2, 498);
3783
+ assert.isAtMost(fullSteps, liveSteps() * 1.1);
3784
+ // The rows themselves are read only from the newest page, not from all 502.
3785
+ const rowStatements = liveStatements.filter(([query]) => query.includes("work_segments"));
3786
+ assert.lengthOf(rowStatements, 1);
3787
+ const pageSteps = liveSteps(rowStatements);
3788
+ deleteOrdinals(499, 900);
3789
+ assert.isAtMost(pageSteps, liveSteps(rowStatements) * 1.1);
3790
+ // The tail search reads conversation rows from their own index, not past the work rows.
3791
+ const tailSearch = liveStatements.find(([query]) => query.includes("visible_tail"));
3792
+ assert.isDefined(tailSearch);
3793
+ const plan = copy
3794
+ .prepare(`EXPLAIN QUERY PLAN ${tailSearch![0]}`)
3795
+ .all(...(tailSearch![1] as ReadonlyArray<NodeSqlite.SQLInputValue>))
3796
+ .map((row) => String(row.detail));
3797
+ assert.isTrue(
3798
+ plan.some((detail) => detail.includes("turn_items_conversation_idx")),
3799
+ plan.join("\n"),
3800
+ );
3801
+ }
3802
+ const olderLivePage = yield* projectionStore.getThreadSnapshotWindow(threadId, {
3803
+ rowLimit: sqlPageLimit,
3804
+ userTurnLimit: 10,
3805
+ deferWork: true,
3806
+ anchorItemId: TurnItemId.make("turn-item:bounded-sql-history:600"),
3807
+ });
3808
+ // Rows above the anchor must not set the tail, or the page would start above its end.
3809
+ assert.deepEqual(
3810
+ olderLivePage.projection.turnItems.map((item) => item.ordinal),
3811
+ Array.from({ length: sqlPageLimit }, (_, index) => 601 - sqlPageLimit + index),
3812
+ );
3813
+ yield* sql`DELETE FROM orchestration_v2_projection_turn_items
3814
+ WHERE thread_id = ${foreignThreadId}`;
3815
+ yield* setSharedRunStatus("completed");
3816
yield* sql`
3817
UPDATE orchestration_v2_projection_turn_items
3818
SET run_id = 'run:bounded-sql-history:' || ordinal,
apps/server/src/orchestration-v2/ProjectionStore.ts
+135
-39
@@ -2023,6 +2023,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2023
OR item.payload_json LIKE '%"viewedImagePath"%'
2024
OR item.payload_json LIKE '%"toolName":"%delegate_task"%')`;
2025
2026
+ // SQLite uses the partial conversation index of migration 082 only for this exact predicate.
2027
+ const CONVERSATION_INDEX_EXCLUDED_TYPES = sql.literal(
2028
+ `(${DEFERRED_WORK_ITEM_TYPES.map((type) => `'${type}'`).join(", ")})`,
2029
+ );
2030
+
2031
+ // A todo list is never closed when its run ends, so a long thread holds an active one per
2032
+ // old run. Counting those as live work drags each run's nodes and subagents into every window.
2033
+ const todoListOfSettledRun = (row: Statement.Fragment) => sql`(
2034
+ ${row}.kind = 'todo_list' AND ${row}.run_id IS NOT NULL AND NOT EXISTS (
2035
+ SELECT 1 FROM orchestration_v2_projection_runs AS todo_run
2036
+ WHERE todo_run.run_id = ${row}.run_id
2037
+ AND todo_run.status IN ('queued','preparing','starting','running','waiting')
2038
+ ))`;
2039
+
2040
// Start from the few owned provider threads; a thread can have a large settled node history.
2041
const nodesOwningProviderThreads = (threadId: ThreadId) => sql`
2042
SELECT owner.node_id
@@ -3075,55 +3089,123 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3089
`;
3090
const boundary = anchors[0]!;
3091
// Folding needs whole segments, so the window would walk every item since the boundary.
3078
- // Non-deferrable types always keep their own row, so the window stays above the newest
3079
- // `tailRows` of them. Start at the latest segment start of each run that reaches that
3080
- // point, clamped to it so runless rows below stay in the scan.
3092
+ // It stays above the newest `tailRows` rows that are sure to keep their own row. Start at
3093
+ // the latest segment start of each run that reaches that point, clamped to it so runless
3094
+ // rows below stay in the scan.
3095
const tailRows = Math.max(window.rowLimit, THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT);
3082
- const tailWorkStart = sql<{ readonly ordinal: number | null }>`
3083
- ${eligible}, visible_tail AS (
3084
- SELECT ordinal FROM eligible
3085
- WHERE ordinal >= ${boundary.ordinal}
3086
- AND type NOT IN ${sql.in(DEFERRED_WORK_ITEM_TYPES)}
3096
+ // The newest rows sure to keep their own row: conversation rows, and every row of the
3097
+ // running run, which folds nothing. More of them than a whole turn may hold means the
3098
+ // window pages from inside the newest turn with the row limit, so the tail shrinks to
3099
+ // that limit. A turn of thousands of rows is then not walked to return the newest few.
3100
+ const tailWorkStart = sql<{
3101
+ readonly ordinal: number | null;
3102
+ readonly long_turn: number;
3103
+ }>`
3104
+ ${eligible}, live_run AS (
3105
+ -- The oldest live run is the one at work; queued runs follow it.
3106
+ SELECT live.run_id FROM orchestration_v2_projection_runs AS live
3107
+ WHERE live.thread_id = ${threadId}
3108
+ AND live.status IN ('queued','preparing','starting','running','waiting')
3109
+ AND ${window.requiredRunId ?? null} IS NULL
3110
+ AND NOT EXISTS (
3111
+ SELECT 1 FROM orchestration_v2_projection_run_attempts
3112
+ WHERE run_id = live.run_id AND status = 'superseded'
3113
+ )
3114
+ ORDER BY +live.ordinal ASC LIMIT 1
3115
+ ), visible_tail AS (
3116
+ SELECT ordinal FROM (
3117
+ SELECT ordinal FROM eligible
3118
+ WHERE ordinal >= ${boundary.ordinal}
3119
+ AND type NOT IN ${CONVERSATION_INDEX_EXCLUDED_TYPES}
3120
+ ORDER BY ordinal DESC
3121
+ LIMIT ${tailRows}
3122
+ )
3123
+ UNION
3124
+ SELECT ordinal FROM (
3125
+ SELECT live_item.ordinal
3126
+ FROM orchestration_v2_projection_turn_items AS live_item
3127
+ -- The unary plus keeps this on the run index, whose ordinal order ends
3128
+ -- the walk at the limit.
3129
+ WHERE live_item.run_id = (SELECT run_id FROM live_run)
3130
+ AND +live_item.thread_id = ${threadId}
3131
+ AND live_item.ordinal >= ${boundary.ordinal}
3132
+ AND live_item.ordinal <= COALESCE(
3133
+ (
3134
+ SELECT ordinal FROM orchestration_v2_projection_turn_items
3135
+ WHERE thread_id = ${threadId}
3136
+ AND turn_item_id = ${window.anchorItemId ?? null}
3137
+ LIMIT 1
3138
+ ),
3139
+ 9223372036854775807
3140
+ )
3141
+ ORDER BY live_item.ordinal DESC
3142
+ LIMIT ${tailRows}
3143
+ )
3144
ORDER BY ordinal DESC
3145
LIMIT ${tailRows}
3146
+ ), turn AS (
3147
+ SELECT (
3148
+ SELECT COUNT(*) FROM visible_tail WHERE ordinal >= ${boundary.latest}
3149
+ ) > ${THREAD_HISTORY_PAGE_POLICY.maxTurnItems} AS long_turn
3150
), tail AS (
3090
- SELECT MIN(ordinal) AS ordinal FROM visible_tail HAVING COUNT(*) >= ${tailRows}
3151
+ SELECT visible_tail.ordinal FROM visible_tail
3152
+ ORDER BY visible_tail.ordinal DESC
3153
+ LIMIT 1 OFFSET (
3154
+ SELECT MAX(CASE WHEN long_turn THEN ${window.rowLimit} ELSE ${tailRows} END, 1) - 1
3155
+ FROM turn
3156
+ )
3157
), tail_runs AS (
3158
SELECT DISTINCT item.run_id
3159
FROM tail CROSS JOIN eligible AS item
3160
WHERE item.ordinal >= tail.ordinal AND item.run_id IS NOT NULL
3161
)
3096
- SELECT MIN(MIN(COALESCE(
3097
- (
3098
- SELECT start.ordinal FROM eligible AS start
3099
- WHERE start.run_id = tail_runs.run_id AND start.type = 'user_message'
3100
- AND start.ordinal <= tail.ordinal
3101
- ORDER BY start.ordinal DESC LIMIT 1
3102
- ),
3103
- (
3104
- SELECT MIN(first.ordinal) FROM orchestration_v2_projection_turn_items AS first
3105
- WHERE first.thread_id = ${threadId} AND first.run_id = tail_runs.run_id
3106
- ),
3107
- tail.ordinal
3108
- ), tail.ordinal)) AS ordinal
3162
+ SELECT MIN(MIN(CASE
3163
+ -- Only a completed run or a superseded attempt folds work, and folding is
3164
+ -- what needs the whole segment. Any other run keeps every row.
3165
+ WHEN (
3166
+ SELECT status FROM orchestration_v2_projection_runs
3167
+ WHERE run_id = tail_runs.run_id
3168
+ ) IS NOT 'completed' AND NOT EXISTS (
3169
+ SELECT 1 FROM orchestration_v2_projection_run_attempts
3170
+ WHERE run_id = tail_runs.run_id AND status = 'superseded'
3171
+ ) THEN tail.ordinal
3172
+ ELSE COALESCE(
3173
+ (
3174
+ SELECT start.ordinal FROM eligible AS start
3175
+ WHERE start.run_id = tail_runs.run_id AND start.type = 'user_message'
3176
+ AND start.ordinal <= tail.ordinal
3177
+ ORDER BY start.ordinal DESC LIMIT 1
3178
+ ),
3179
+ (
3180
+ SELECT MIN(first.ordinal) FROM orchestration_v2_projection_turn_items AS first
3181
+ WHERE first.thread_id = ${threadId} AND first.run_id = tail_runs.run_id
3182
+ ),
3183
+ tail.ordinal
3184
+ )
3185
+ END, tail.ordinal)) AS ordinal, (SELECT long_turn FROM turn) AS long_turn
3186
FROM tail LEFT JOIN tail_runs
3187
`;
3188
// A negative or fractional limit makes SQLite's LIMIT disagree with `tailRows`; keep the old start.
3112
- const workStart =
3189
+ const tail =
3190
window.deferWork === true &&
3191
boundary.latest !== null &&
3192
Number.isSafeInteger(window.rowLimit) &&
3193
window.rowLimit >= 0
3117
- ? Math.max(boundary.ordinal, (yield* tailWorkStart)[0]?.ordinal ?? 0)
3118
- : boundary.ordinal;
3194
+ ? (yield* tailWorkStart)[0]
3195
+ : undefined;
3196
+ const longTurn = tail?.long_turn === 1;
3197
+ const workStart = Math.max(boundary.ordinal, tail?.ordinal ?? 0);
3198
return sql`
3199
${eligible}, blocked_runs AS MATERIALIZED (
3200
SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items
3201
WHERE thread_id = ${threadId} AND run_id IS NOT NULL
3202
AND ${window.deferWork ? 1 : 0} = 1
3203
+ -- Only a run that can fold asks whether it is blocked. A live run cannot,
3204
+ -- and this check would read every row it ever wrote.
3205
AND run_id IN (
3206
SELECT run_id FROM eligible
3207
WHERE ordinal >= ${workStart} AND run_id IS NOT NULL
3208
+ AND (run_status = 'completed' OR attempt_status = 'superseded')
3209
)
3210
AND (type IN ('run_interrupt_request', 'run_interrupt_result')
3211
OR (type = 'error' AND status = 'failed' AND parent_item_id IS NULL))
@@ -3177,6 +3259,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3259
-- Every LIMIT here and in latest_user_turn must stay <= tailRows, or rows below workStart go missing.
3260
LIMIT CASE
3261
WHEN ${window.rowLimit} = 0 THEN 0
3262
+ WHEN ${longTurn ? 1 : 0} = 1 THEN ${window.rowLimit}
3263
WHEN (SELECT row_count FROM latest_user_turn)
3264
BETWEEN 1 AND ${THREAD_HISTORY_PAGE_POLICY.maxTurnItems}
3265
THEN ${THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT}
@@ -3326,8 +3409,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3409
WITH RECURSIVE retained(node_id) AS (
3410
SELECT value FROM json_each(${cohortJson("nodeId")})
3411
UNION
3329
- SELECT node_id FROM orchestration_v2_projection_nodes
3412
+ SELECT node_id FROM orchestration_v2_projection_nodes AS live
3413
WHERE thread_id = ${threadId} AND status IN ('pending','starting','running','waiting')
3414
+ AND NOT ${todoListOfSettledRun(sql`live`)}
3415
UNION
3416
SELECT root_node_id FROM orchestration_v2_projection_run_attempts
3417
WHERE thread_id = ${threadId} AND (
@@ -3697,18 +3781,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3781
ORDER BY created_at ASC, message_id ASC
3782
`
3783
: sql<PayloadRow>`
3700
- SELECT payload_json FROM orchestration_v2_projection_messages AS message
3701
- WHERE message.thread_id = ${threadId}
3702
- AND (
3703
- message.message_id IN (
3704
- SELECT value FROM json_each(${cohortMessageIds})
3705
- )
3706
- OR message.run_id IN (
3707
- SELECT run_id FROM orchestration_v2_projection_runs
3708
- WHERE thread_id = ${threadId}
3709
- AND status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
3710
- )
3711
- )
3784
+ -- Look the window's messages up by key and the live runs' by run. One filter
3785
+ -- over the thread reads every message the thread ever had.
3786
+ SELECT payload_json FROM (
3787
+ SELECT message.payload_json, message.created_at, message.message_id
3788
+ FROM json_each(${cohortMessageIds}) AS wanted
3789
+ CROSS JOIN orchestration_v2_projection_messages AS message
3790
+ ON message.message_id = wanted.value
3791
+ WHERE message.thread_id = ${threadId}
3792
+ UNION
3793
+ SELECT message.payload_json, message.created_at, message.message_id
3794
+ FROM orchestration_v2_projection_runs AS run
3795
+ CROSS JOIN orchestration_v2_projection_messages AS message
3796
+ ON message.run_id = run.run_id
3797
+ WHERE run.thread_id = ${threadId}
3798
+ AND run.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
3799
+ AND message.thread_id = ${threadId}
3800
+ )
3801
ORDER BY created_at ASC, message_id ASC
3802
`,
3803
fields !== undefined && !fields.includes("plans")
@@ -3722,7 +3811,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3811
SELECT payload_json FROM orchestration_v2_projection_plans AS plan
3812
WHERE plan.thread_id = ${threadId}
3813
AND (
3725
- plan.status = 'active'
3814
+ (plan.status = 'active' AND NOT ${todoListOfSettledRun(sql`plan`)})
3815
OR plan.plan_id = ${latestTodoList?.plan_id ?? null}
3816
OR plan.plan_id IN (
3817
SELECT value FROM json_each(${cohortPlanIds})
@@ -3763,6 +3852,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3852
WHERE thread_id = ${threadId}
3853
AND (status IN ('pending','capturing')
3854
OR (run_id IS NULL AND ordinal_within_scope = 0 AND status = 'ready')
3855
+ -- The diff of the last finished turn stays in reach while a long run is at work.
3856
+ OR checkpoint_id = (
3857
+ SELECT checkpoint_id FROM orchestration_v2_projection_checkpoints
3858
+ WHERE thread_id = ${threadId} AND status = 'ready'
3859
+ AND app_run_ordinal IS NOT NULL
3860
+ ORDER BY app_run_ordinal DESC LIMIT 1
3861
+ )
3862
OR checkpoint_id IN (SELECT value FROM json_each(${cohortCheckpointIds}))
3863
OR run_id IN (SELECT value FROM json_each(${cohortRunIds}))
3864
OR node_id IN (SELECT value FROM json_each(${cohortNodeIds})))
apps/server/src/persistence/Migrations.ts
+2
@@ -84,6 +84,7 @@ import Migration0078 from "./Migrations/078_DropOrchestrationEventCorrelationInd
84
import Migration0079 from "./Migrations/079_DelegatedRecoveryIndexes.ts";
85
import Migration0080 from "./Migrations/080_ScheduledTaskThreadMode.ts";
86
import Migration0081 from "./Migrations/081_ProjectionMessageSearchText.ts";
87
+import Migration0082 from "./Migrations/082_TurnItemConversationIndex.ts";
88
89
const migrationEntries = [
90
[1, "OrchestrationEvents", Migration0001],
@@ -168,6 +169,7 @@ const migrationEntries = [
169
[79, "DelegatedRecoveryIndexes", Migration0079],
170
[80, "ScheduledTaskThreadMode", Migration0080],
171
[81, "ProjectionMessageSearchText", Migration0081],
172
+ [82, "TurnItemConversationIndex", Migration0082],
173
] as const;
174
175
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/082_TurnItemConversationIndex.ts
new
+16
@@ -0,0 +1,16 @@
1
+import * as Effect from "effect/Effect";
2
+import * as SqlClient from "effect/sql/SqlClient";
3
+
4
+export default Effect.gen(function* () {
5
+ const sql = yield* SqlClient.SqlClient;
6
+
7
+ // The thread window finds its newest conversation rows here instead of walking the tool calls
8
+ // between them. The read in ProjectionStore must spell this type list the same way.
9
+ yield* sql`
10
+ CREATE INDEX IF NOT EXISTS orchestration_v2_projection_turn_items_conversation_idx
11
+ ON orchestration_v2_projection_turn_items(thread_id, ordinal, type, run_id)
12
+ WHERE type NOT IN (
13
+ 'command_execution', 'reasoning', 'file_change', 'file_search', 'web_search', 'dynamic_tool'
14
+ )
15
+ `;
16
+});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -61,6 +61,7 @@ describe("V2 preview upgrade", () => {
61
[79, "DelegatedRecoveryIndexes"],
62
[80, "ScheduledTaskThreadMode"],
63
[81, "ProjectionMessageSearchText"],
64
+ [82, "TurnItemConversationIndex"],
65
]);
66
assert.deepStrictEqual(yield* runMigrations(), []);
67
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -165,6 +166,7 @@ describe("V2 preview upgrade", () => {
166
[79, "DelegatedRecoveryIndexes"],
167
[80, "ScheduledTaskThreadMode"],
168
[81, "ProjectionMessageSearchText"],
169
+ [82, "TurnItemConversationIndex"],
170
]);
171
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
172
);