Skip to content

Commit f00b5c8

Browse files
Alyx-MLclaude
andcommitted
fix(server): usage-limit sweep stops scanning every run each tick
The usage-limit recovery sweep runs every scheduler tick (5 s). Its latest-run lookup sorted every run of every thread, about 40 ms per tick with one long thread, which was most of the server's idle CPU. Only threads with a usage-limit failure can qualify, so filter to those first. Same rows, 20.9 ms to 0.004 ms on a real database copy. Done with Claude Opus 5.5 in Claude Code. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9c16146 commit f00b5c8

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3580,6 +3580,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
35803580
LIMIT 1
35813581
)
35823582
WHERE t.deleted_at IS NULL
3583+
-- The sweep runs every scheduler tick. Without this list the latest-run
3584+
-- lookup above sorts every run of every thread, tens of milliseconds
3585+
-- for one long thread; only threads with a usage-limit failure qualify.
3586+
AND t.thread_id IN (
3587+
SELECT failure.thread_id FROM orchestration_v2_projection_turn_items failure
3588+
WHERE failure.type = 'error' AND failure.status = 'failed'
3589+
AND json_extract(failure.payload_json, '$.failure.class') = 'usage_limit'
3590+
)
35833591
AND json_extract(t.payload_json, '$.archivedAt') IS NULL
35843592
AND json_extract(t.payload_json, '$.settledOverride') IS NOT 'settled'
35853593
AND json_extract(item.payload_json, '$.failure.class') = 'usage_limit'

0 commit comments

Comments
 (0)