Skip to content

Commit f4b2cdd

Browse files
t3dotggclaude
andcommitted
perf(server): opening a long thread reads only its latest window
Snapshot windows with deferred work grouped every item since the user-turn boundary. With few user turns that boundary is the start of the thread, so the cost grew with the whole thread. Start the work grouping at the newest 502 rows that can never fold, moved back to the latest segment start of each run that reaches them, so folded groups stay whole and the output does not change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1a43f48 commit f4b2cdd

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,6 +3356,24 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
33563356
yield* sql`UPDATE orchestration_v2_projection_turn_items SET ordinal = 999,
33573357
payload_json = json_set(payload_json, '$.ordinal', 999)
33583358
WHERE turn_item_id = 'turn-item:bounded-sql-history:999'`;
3359+
// Work folded below the newest 502 visible rows still counts its whole group, so the
3360+
// 100 commands after the messages stay hidden in the 398-item group.
3361+
yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'assistant_message',
3362+
payload_json = json_set(payload_json, '$.type', 'assistant_message', '$.text', 'Working',
3363+
'$.messageId', turn_item_id, '$.streaming', json('false'))
3364+
WHERE thread_id = ${threadId} AND ordinal BETWEEN 300 AND 899`;
3365+
const straddledWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
3366+
rowLimit: sqlPageLimit,
3367+
userTurnLimit: 10,
3368+
deferWork: true,
3369+
});
3370+
assert.deepEqual(
3371+
straddledWindow.projection.turnItems.map((item) => item.ordinal),
3372+
[...Array.from({ length: sqlPageLimit }, (_, index) => 900 - sqlPageLimit + index), 1000],
3373+
);
3374+
yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
3375+
payload_json = json_set(payload_json, '$.type', 'command_execution')
3376+
WHERE thread_id = ${threadId} AND ordinal BETWEEN 300 AND 899`;
33593377
yield* sql`
33603378
UPDATE orchestration_v2_projection_turn_items
33613379
SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution')

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,16 @@ export class ProjectionStoreV2 extends Context.Service<ProjectionStoreV2, Projec
735735

736736
export const ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION = 2;
737737

738+
// Completed items of these types can fold into one deferred-work row in snapshot windows.
739+
const DEFERRABLE_WORK_TYPES = [
740+
"command_execution",
741+
"reasoning",
742+
"file_change",
743+
"file_search",
744+
"web_search",
745+
"dynamic_tool",
746+
] as const;
747+
738748
function upsertById<T extends { readonly id: string }>(items: ReadonlyArray<T>, next: T): Array<T> {
739749
const index = items.findIndex((item) => item.id === next.id);
740750
if (index === -1) {
@@ -3032,14 +3042,54 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30323042
SELECT ordinal, (SELECT MAX(ordinal) FROM user_anchors) AS latest FROM boundary
30333043
`;
30343044
const boundary = anchors[0]!;
3045+
// Folding work needs whole segments, so the window would otherwise walk every item
3046+
// since the boundary. Types outside the deferrable set always keep their own row,
3047+
// so the window cannot reach below the newest `tailRows` of them; start at the
3048+
// latest segment of each run that reaches that point so its groups stay whole.
3049+
const tailRows = Math.max(window.rowLimit, THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT);
3050+
const workStart =
3051+
window.deferWork !== true || boundary.latest === null
3052+
? boundary.ordinal
3053+
: Math.max(
3054+
boundary.ordinal,
3055+
(yield* sql<{ readonly ordinal: number | null }>`
3056+
${eligible}, visible_tail AS (
3057+
SELECT ordinal FROM eligible
3058+
WHERE ordinal >= ${boundary.ordinal}
3059+
AND type NOT IN ${sql.in(DEFERRABLE_WORK_TYPES)}
3060+
ORDER BY ordinal DESC
3061+
LIMIT ${tailRows}
3062+
), tail AS (
3063+
SELECT MIN(ordinal) AS ordinal FROM visible_tail HAVING COUNT(*) >= ${tailRows}
3064+
), tail_runs AS (
3065+
SELECT DISTINCT item.run_id
3066+
FROM tail CROSS JOIN eligible AS item
3067+
WHERE item.ordinal >= tail.ordinal AND item.run_id IS NOT NULL
3068+
)
3069+
SELECT MIN(COALESCE(
3070+
(
3071+
SELECT start.ordinal FROM eligible AS start
3072+
WHERE start.run_id = tail_runs.run_id AND start.type = 'user_message'
3073+
AND start.ordinal <= tail.ordinal
3074+
ORDER BY start.ordinal DESC LIMIT 1
3075+
),
3076+
(
3077+
SELECT MIN(first.ordinal) FROM orchestration_v2_projection_turn_items AS first
3078+
WHERE first.run_id = tail_runs.run_id
3079+
),
3080+
tail.ordinal
3081+
)) AS ordinal
3082+
FROM tail LEFT JOIN tail_runs
3083+
`)[0]?.ordinal ?? 0,
3084+
);
30353085
return sql`
30363086
${eligible}, blocked_runs AS MATERIALIZED (
30373087
SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items
30383088
WHERE thread_id = ${threadId} AND run_id IS NOT NULL
30393089
AND ${window.deferWork ? 1 : 0} = 1
30403090
AND run_id IN (
30413091
SELECT run_id FROM eligible
3042-
WHERE ordinal >= ${boundary.ordinal} AND run_id IS NOT NULL
3092+
WHERE ordinal >= ${workStart} AND run_id IS NOT NULL
30433093
)
30443094
AND (type IN ('run_interrupt_request', 'run_interrupt_result')
30453095
OR (type = 'error' AND status = 'failed' AND parent_item_id IS NULL))
@@ -3048,14 +3098,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30483098
MAX(CASE WHEN type = 'user_message' THEN ordinal ELSE 0 END)
30493099
OVER (PARTITION BY run_id ORDER BY ordinal) AS segment,
30503100
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')
3101+
AND type IN ${sql.in(DEFERRABLE_WORK_TYPES)}
30523102
AND NOT (${inlineWorkOutput})
30533103
AND run_id NOT IN (SELECT run_id FROM blocked_runs)
30543104
THEN 1 ELSE 0 END AS can_defer
30553105
FROM eligible AS item
30563106
WHERE ${window.deferWork ? 1 : 0} = 1
30573107
AND ${boundary.latest === null ? 0 : 1} = 1
3058-
AND ordinal >= ${boundary.ordinal}
3108+
AND ordinal >= ${workStart}
30593109
), deferred_work AS (
30603110
SELECT run_id, segment, attempt_id, MIN(ordinal) AS first_ordinal,
30613111
MAX(ordinal) AS last_ordinal, COUNT(*) AS item_count

0 commit comments

Comments
 (0)