Skip to content

Commit 9a0ab90

Browse files
committed
perf(server): compute blocked history runs once per snapshot
1 parent 30ba916 commit 9a0ab90

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,7 +2829,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
28292829
`;
28302830
const boundary = anchors[0]!;
28312831
return sql`
2832-
${eligible}, work_segments AS (
2832+
${eligible}, blocked_runs AS MATERIALIZED (
2833+
SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items
2834+
WHERE thread_id = ${threadId} AND run_id IS NOT NULL
2835+
AND ${window.deferWork ? 1 : 0} = 1
2836+
AND (type IN ('run_interrupt_request', 'run_interrupt_result')
2837+
OR (type = 'error' AND status = 'failed' AND parent_item_id IS NULL))
2838+
), work_segments AS (
28332839
SELECT item_rowid, ordinal, turn_item_id, run_id, type,
28342840
MAX(CASE WHEN type = 'user_message' THEN ordinal ELSE 0 END)
28352841
OVER (PARTITION BY run_id ORDER BY ordinal) AS segment,
@@ -2838,12 +2844,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
28382844
AND (type <> 'dynamic_tool' OR (
28392845
payload_json NOT LIKE '%htmlRender%' AND payload_json NOT LIKE ${`%${MCP_APP_OUTPUT_KEY}%`}
28402846
AND payload_json NOT LIKE '%"viewedImagePath"%'))
2841-
AND NOT EXISTS (
2842-
SELECT 1 FROM orchestration_v2_projection_turn_items blocked
2843-
WHERE blocked.run_id = eligible.run_id
2844-
AND (blocked.type IN ('run_interrupt_request', 'run_interrupt_result')
2845-
OR (blocked.type = 'error' AND blocked.status = 'failed' AND blocked.parent_item_id IS NULL))
2846-
) THEN 1 ELSE 0 END AS can_defer
2847+
AND run_id NOT IN (SELECT run_id FROM blocked_runs)
2848+
THEN 1 ELSE 0 END AS can_defer
28472849
FROM eligible
28482850
WHERE ${window.deferWork ? 1 : 0} = 1
28492851
AND ${boundary.latest === null ? 0 : 1} = 1

0 commit comments

Comments
 (0)