Skip to content

Commit 080173b

Browse files
perf(server): seek failed-run root errors without scanning tool history
1 parent 58d2532 commit 080173b

6 files changed

Lines changed: 81 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ it.effect("shell failure lookups stay on the thread's own turn items", () =>
520520
// node_id, so a node_ordinal lookup walks the whole history once per thread.
521521
const itemLookups = plan.filter((row) => row.detail.startsWith("SEARCH item "));
522522
assert.lengthOf(itemLookups, 2);
523-
assert.isTrue(itemLookups.every((row) => row.detail.includes("turn_items_thread_run_idx")));
523+
assert.isTrue(itemLookups.every((row) => row.detail.includes("turn_items_failed_error_idx")));
524524
// The pending secret request lookup is bounded the same way.
525525
const secretLookups = plan.filter((row) => row.detail.startsWith("SEARCH secret "));
526526
assert.lengthOf(secretLookups, 1);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,6 +3426,15 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
34263426
);
34273427
assert.isAbove(sessionLookup, bindingLookup, marker);
34283428
assert.include(plan[sessionLookup], "(provider_session_id=?)");
3429+
const errorLookups = plan.filter(
3430+
(detail) =>
3431+
/^SEARCH (?:item|error) /.test(detail) && !detail.includes("(turn_item_id=?)"),
3432+
);
3433+
assert.lengthOf(errorLookups, marker.includes("blocking") ? 2 : 1, marker);
3434+
for (const errorLookup of errorLookups) {
3435+
assert.include(errorLookup, "turn_items_failed_error_idx");
3436+
assert.include(errorLookup, "(thread_id=? AND run_id=? AND node_id=?)");
3437+
}
34293438
}
34303439
}),
34313440
);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5214,7 +5214,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
52145214
(
52155215
SELECT item.payload_json
52165216
FROM orchestration_v2_projection_turn_items item
5217-
INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
52185217
INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id
52195218
WHERE r.run_id = presented.run_id
52205219
AND r.status = 'failed'
@@ -5231,7 +5230,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
52315230
(
52325231
SELECT item.payload_json
52335232
FROM orchestration_v2_projection_turn_items item
5234-
INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
52355233
WHERE item.thread_id = t.thread_id
52365234
AND item.run_id = blocked.run_id
52375235
AND item.type = 'error' AND item.status = 'failed'

‎apps/server/src/persistence/Migrations.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import Migration0058 from "./Migrations/058_WebhookRelayDeliveries.ts";
7575
import Migration0059 from "./Migrations/059_McpAppModelContext.ts";
7676
import Migration0060 from "./Migrations/060_ThreadSnapshotWindowIndexes.ts";
7777
import Migration0061 from "./Migrations/061_PullRequestReviewGuides.ts";
78+
import Migration0062 from "./Migrations/062_ThreadFailureLookupIndex.ts";
7879

7980
/**
8081
* Migration loader with all migrations defined inline.
@@ -150,6 +151,7 @@ export const migrationEntries = [
150151
[59, "McpAppModelContext", Migration0059],
151152
[60, "ThreadSnapshotWindowIndexes", Migration0060],
152153
[61, "PullRequestReviewGuides", Migration0061],
154+
[62, "ThreadFailureLookupIndex", Migration0062],
153155
] as const;
154156

155157
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { assert, it } from "@effect/vitest";
2+
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3+
import * as Effect from "effect/Effect";
4+
import * as SqlClient from "effect/sql/SqlClient";
5+
6+
import { runMigrations } from "../Migrations.ts";
7+
8+
it.effect.each([null, "root"])("seeks ordered root errors when node is %s", (nodeId) =>
9+
Effect.gen(function* () {
10+
const sql = yield* SqlClient.SqlClient;
11+
yield* runMigrations({ toMigrationInclusive: 61 });
12+
yield* sql`
13+
WITH RECURSIVE items(ordinal) AS (
14+
SELECT 1 UNION ALL SELECT ordinal + 1 FROM items WHERE ordinal < 512
15+
)
16+
INSERT INTO orchestration_v2_projection_turn_items
17+
(turn_item_id, thread_id, run_id, node_id, ordinal, type, status, updated_at, payload_json)
18+
SELECT 'command-' || ordinal, 'thread', 'run', ${nodeId}, ordinal,
19+
'command_execution', 'completed', '2026-10-01', '{}'
20+
FROM items
21+
`;
22+
yield* sql`
23+
INSERT INTO orchestration_v2_projection_turn_items
24+
(turn_item_id, thread_id, run_id, node_id, ordinal, type, status, updated_at, payload_json)
25+
VALUES
26+
('older', 'thread', 'run', ${nodeId}, 513, 'error', 'failed', '2026-10-02', '{}'),
27+
('tie-a', 'thread', 'run', ${nodeId}, 514, 'error', 'failed', '2026-10-02', '{}'),
28+
('tie-b', 'thread', 'run', ${nodeId}, 514, 'error', 'failed', '2026-10-02', '{}'),
29+
('child', 'thread', 'run', 'child', 515, 'error', 'failed', '2026-10-03', '{}'),
30+
('other-run', 'thread', 'other', ${nodeId}, 516, 'error', 'failed', '2026-10-03', '{}')
31+
`;
32+
yield* runMigrations({ toMigrationInclusive: 62 });
33+
const lookup = sql<{ readonly turn_item_id: string }>`
34+
SELECT turn_item_id FROM orchestration_v2_projection_turn_items
35+
WHERE thread_id = 'thread' AND run_id = 'run'
36+
AND node_id IS ${nodeId} AND type = 'error' AND status = 'failed'
37+
ORDER BY updated_at DESC, ordinal DESC, turn_item_id DESC LIMIT 1
38+
`;
39+
assert.deepEqual(yield* lookup, [{ turn_item_id: "tie-b" }]);
40+
const [query, parameters] = lookup.compile();
41+
const plan = yield* sql.unsafe<{ readonly detail: string }>(
42+
`EXPLAIN QUERY PLAN ${query}`,
43+
parameters,
44+
);
45+
assert.isTrue(plan.some((row) => row.detail.includes("turn_items_failed_error_idx")));
46+
assert.isFalse(plan.some((row) => row.detail.includes("TEMP B-TREE")));
47+
48+
yield* sql`
49+
UPDATE orchestration_v2_projection_turn_items SET status = 'completed'
50+
WHERE turn_item_id = 'tie-b'
51+
`;
52+
assert.deepEqual(yield* lookup, [{ turn_item_id: "tie-a" }]);
53+
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
54+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
// Shell and limit-recovery reads need one root error, not every item in a run.
8+
yield* sql`
9+
CREATE INDEX orchestration_v2_projection_turn_items_failed_error_idx
10+
ON orchestration_v2_projection_turn_items(
11+
thread_id, run_id, node_id, updated_at DESC, ordinal DESC, turn_item_id DESC
12+
)
13+
WHERE type = 'error' AND status = 'failed'
14+
`;
15+
});

0 commit comments

Comments
 (0)