fix(server): failed threads no longer scan every tool item for errors (#18628)

This commit is contained in:
Hasan Shoaib authored and GitHub committed 2026-10-13 13:21:47 -07:00
1 parent fb8d0fc19c
commit 84cb13412e
7 files changed
+32 -4

No files matched your search

@@ -520,7 +520,7 @@ it.effect("shell failure lookups stay on the thread's own turn items", () =>
// node_id, so a node_ordinal lookup walks the whole history once per thread.
const itemLookups = plan.filter((row) => row.detail.startsWith("SEARCH item "));
assert.lengthOf(itemLookups, 2);
assert.isTrue(itemLookups.every((row) => row.detail.includes("turn_items_thread_run_idx")));
assert.isTrue(itemLookups.every((row) => row.detail.includes("turn_items_failed_error_idx")));
// The pending secret request lookup is bounded the same way.
const secretLookups = plan.filter((row) => row.detail.startsWith("SEARCH secret "));
assert.lengthOf(secretLookups, 1);
@@ -3426,6 +3426,15 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
);
assert.isAbove(sessionLookup, bindingLookup, marker);
assert.include(plan[sessionLookup], "(provider_session_id=?)");
const errorLookups = plan.filter(
(detail) =>
/^SEARCH (?:item|error) /.test(detail) && !detail.includes("(turn_item_id=?)"),
);
assert.lengthOf(errorLookups, marker.includes("blocking") ? 2 : 1, marker);
for (const errorLookup of errorLookups) {
assert.include(errorLookup, "turn_items_failed_error_idx");
assert.include(errorLookup, "(thread_id=? AND run_id=? AND node_id=?)");
}
}
}),
);
@@ -5214,7 +5214,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
(
SELECT item.payload_json
FROM orchestration_v2_projection_turn_items item
INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id
WHERE r.run_id = presented.run_id
AND r.status = 'failed'
@@ -5231,7 +5230,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
(
SELECT item.payload_json
FROM orchestration_v2_projection_turn_items item
INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
WHERE item.thread_id = t.thread_id
AND item.run_id = blocked.run_id
AND item.type = 'error' AND item.status = 'failed'
@@ -75,6 +75,7 @@ import Migration0058 from "./Migrations/058_WebhookRelayDeliveries.ts";
import Migration0059 from "./Migrations/059_McpAppModelContext.ts";
import Migration0060 from "./Migrations/060_ThreadSnapshotWindowIndexes.ts";
import Migration0061 from "./Migrations/061_PullRequestReviewGuides.ts";
import Migration0062 from "./Migrations/062_ThreadFailureLookupIndex.ts";
/**
* Migration loader with all migrations defined inline.
@@ -150,6 +151,7 @@ export const migrationEntries = [
[59, "McpAppModelContext", Migration0059],
[60, "ThreadSnapshotWindowIndexes", Migration0060],
[61, "PullRequestReviewGuides", Migration0061],
[62, "ThreadFailureLookupIndex", Migration0062],
] as const;
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
@@ -13,7 +13,7 @@ layer("055_OrchestrationV2", (it) => {
Effect.sync(() => {
assert.deepStrictEqual(
migrationEntries.map(([id]) => id),
Array.from({ length: 61 }, (_, index) => index + 1),
Array.from({ length: 62 }, (_, index) => index + 1),
);
}),
);
@@ -33,6 +33,7 @@ layer("055_OrchestrationV2", (it) => {
[59, "McpAppModelContext"],
[60, "ThreadSnapshotWindowIndexes"],
[61, "PullRequestReviewGuides"],
[62, "ThreadFailureLookupIndex"],
]);
assert.deepStrictEqual(yield* runMigrations(), []);
@@ -60,6 +61,7 @@ layer("055_OrchestrationV2", (it) => {
{ migration_id: 59, name: "McpAppModelContext" },
{ migration_id: 60, name: "ThreadSnapshotWindowIndexes" },
{ migration_id: 61, name: "PullRequestReviewGuides" },
{ migration_id: 62, name: "ThreadFailureLookupIndex" },
]);
const tables = yield* sql<{ readonly name: string }>`
@@ -0,0 +1,15 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";
export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
// Shell and limit-recovery reads need one root error, not every item in a run.
yield* sql`
CREATE INDEX orchestration_v2_projection_turn_items_failed_error_idx
ON orchestration_v2_projection_turn_items(
thread_id, run_id, node_id, updated_at DESC, ordinal DESC, turn_item_id DESC
)
WHERE type = 'error' AND status = 'failed'
`;
});
@@ -42,6 +42,7 @@ describe("V2 preview upgrade", () => {
[59, "McpAppModelContext"],
[60, "ThreadSnapshotWindowIndexes"],
[61, "PullRequestReviewGuides"],
[62, "ThreadFailureLookupIndex"],
]);
assert.deepStrictEqual(yield* runMigrations(), []);
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -126,6 +127,7 @@ describe("V2 preview upgrade", () => {
[59, "McpAppModelContext"],
[60, "ThreadSnapshotWindowIndexes"],
[61, "PullRequestReviewGuides"],
[62, "ThreadFailureLookupIndex"],
]);
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
);