The best way to control Claude Code, Codex and more from any computer or phone
perf(server): index historical usage-limit recovery lookups (#27896)
Co-authored-by: Omar <76748259+omar9848492@users.noreply.github.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Omar committed
Nov 19, 2026 at 01:04 UTC
0796279c1cf000a4e6de72b1858aec75e3301059
5 files changed
+45
-1
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+17
@@ -8653,6 +8653,16 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
8653
),
8654
);
8655
yield* sql`UPDATE orchestration_v2_projection_threads SET deleted_at = NULL WHERE thread_id = ${threadId}`;
8656
+ yield* sql`INSERT INTO orchestration_v2_projection_turn_items
8657
+ (turn_item_id, thread_id, run_id, node_id, ordinal, type, status, updated_at, payload_json)
8658
+ VALUES ('limit-shell:malformed-error', ${threadId}, ${original.id}, ${original.rootNodeId},
8659
+ 99, 'error', 'failed', '2999-01-01T00:00:00.000Z', '{broken')`;
8660
+ assert.isUndefined(
8661
+ (yield* store.getLimitRecoveryCandidates({ now, autoResume: true, snooze: false })).find(
8662
+ (row) => row.id === threadId,
8663
+ ),
8664
+ );
8665
+ yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = 'limit-shell:malformed-error'`;
8666
const recoveryOptions = { now, autoResume: false, snooze: false };
8667
assert.isUndefined(
8668
(yield* store.getLimitRecoveryCandidates(recoveryOptions)).find(
@@ -9261,6 +9271,13 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
9271
const plan = (yield* sql.unsafe<{ readonly detail: string }>(
9272
`EXPLAIN QUERY PLAN ${statement}`,
9273
)).map((row) => row.detail);
9274
+ if (marker === "AS failure_payload_json") {
9275
+ assert.include(
9276
+ plan.join("\n"),
9277
+ "orchestration_v2_projection_turn_items_usage_limit_idx",
9278
+ marker,
9279
+ );
9280
+ }
9281
const bindingLookup = plan.findIndex((detail) => detail.startsWith("SEARCH binding "));
9282
const sessionLookup = plan.findIndex((detail) => detail.startsWith("SEARCH session "));
9283
assert.isAtLeast(bindingLookup, 0, marker);
apps/server/src/orchestration-v2/ProjectionStore.ts
+5
-1
@@ -4839,11 +4839,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4839
-- for one long thread; only threads with a usage-limit failure qualify.
4840
AND t.thread_id IN (
4841
SELECT failure.thread_id FROM orchestration_v2_projection_turn_items failure
4842
+ -- Spelled as in 099_BackgroundRecoveryIndexes so the partial index serves it.
4843
WHERE failure.type = 'error' AND failure.status = 'failed'
4843
- AND json_extract(failure.payload_json, '$.failure.class') = 'usage_limit'
4844
+ AND CASE WHEN json_valid(failure.payload_json) THEN
4845
+ json_extract(failure.payload_json, '$.failure.class') = 'usage_limit'
4846
+ ELSE 0 END
4847
)
4848
AND t.archived_at IS NULL
4849
AND json_extract(t.payload_json, '$.settledOverride') IS NOT 'settled'
4850
+ AND json_valid(item.payload_json)
4851
AND json_extract(item.payload_json, '$.failure.class') = 'usage_limit'
4852
AND json_extract(item.payload_json, '$.failure.resetAt') IS NOT NULL
4853
AND julianday(json_extract(item.payload_json, '$.failure.resetAt')) > julianday(COALESCE(r.completed_at, json_extract(t.payload_json, '$.updatedAt')))
apps/server/src/persistence/Migrations.ts
+2
@@ -106,6 +106,7 @@ import Migration0095 from "./Migrations/095_TurnItemInlineWorkOutput.ts";
106
import Migration0096 from "./Migrations/096_RepairStaleTitleRegeneration.ts";
107
import Migration0097 from "./Migrations/097_IssueTrackerBindings.ts";
108
import Migration0098 from "./Migrations/098_ActiveShellIndexIndexes.ts";
109
+import Migration0099 from "./Migrations/099_BackgroundRecoveryIndexes.ts";
110
111
const migrationEntries = [
112
[1, "OrchestrationEvents", Migration0001],
@@ -207,6 +208,7 @@ const migrationEntries = [
208
[96, "RepairStaleTitleRegeneration", Migration0096],
209
[97, "IssueTrackerBindings", Migration0097],
210
[98, "ActiveShellIndexIndexes", Migration0098],
211
+ [99, "BackgroundRecoveryIndexes", Migration0099],
212
] as const;
213
214
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/099_BackgroundRecoveryIndexes.ts
new
+19
@@ -0,0 +1,19 @@
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
+ // Usage-limit recovery scans every thread's failures. The latest executed run lookup it
8
+ // joins is already served by 084's orchestration_v2_projection_runs_last_executed_idx.
9
+ // Keep this predicate identical to ProjectionStore's usage-limit recovery filter.
10
+ // json_valid keeps a malformed payload writable, as the other payload indexes do.
11
+ yield* sql`
12
+ CREATE INDEX IF NOT EXISTS orchestration_v2_projection_turn_items_usage_limit_idx
13
+ ON orchestration_v2_projection_turn_items(thread_id)
14
+ WHERE type = 'error' AND status = 'failed'
15
+ AND CASE WHEN json_valid(payload_json) THEN
16
+ json_extract(payload_json, '$.failure.class') = 'usage_limit'
17
+ ELSE 0 END
18
+ `;
19
+});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -78,6 +78,7 @@ describe("V2 preview upgrade", () => {
78
[96, "RepairStaleTitleRegeneration"],
79
[97, "IssueTrackerBindings"],
80
[98, "ActiveShellIndexIndexes"],
81
+ [99, "BackgroundRecoveryIndexes"],
82
]);
83
assert.deepStrictEqual(yield* runMigrations(), []);
84
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -199,6 +200,7 @@ describe("V2 preview upgrade", () => {
200
[96, "RepairStaleTitleRegeneration"],
201
[97, "IssueTrackerBindings"],
202
[98, "ActiveShellIndexIndexes"],
203
+ [99, "BackgroundRecoveryIndexes"],
204
]);
205
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
206
);