@@ -1071,6 +1071,7 @@ const encodeTimelineSources = Schema.encodeEffect(
10711071 Schema . Array ( Schema . Struct ( { threadId : Schema . String , id : Schema . String } ) ) ,
10721072 ) ,
10731073) ;
1074+ const LIMIT_EVIDENCE_MAX_RUNS = 200 ;
10741075const encodeIdList = Schema . encodeSync ( Schema . fromJsonString ( Schema . Array ( Schema . String ) ) ) ;
10751076
10761077// Empty filters still scan SQLite rows; short-circuit them, including turnItemTypes: [].
@@ -6066,6 +6067,99 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60666067 ) ,
60676068 ) ;
60686069
6070+ // A window reads only the newest rows, so an older usage-limited run and the notification
6071+ // starts of the wakes after it can be missing. Clients need them (usageLimitBlockedRun)
6072+ // to resume it. They join runs and turnItems, never visibleTurnItems.
6073+ const withLimitEvidence = Effect . fn ( "ProjectionStore.withLimitEvidence" ) ( function * (
6074+ threadId : ThreadId ,
6075+ projection : OrchestrationV2ThreadProjection ,
6076+ ) {
6077+ const rows = yield * sql < { readonly kind : string ; readonly id : string } > `
6078+ WITH blocked AS (
6079+ SELECT r.run_id, r.ordinal, item.turn_item_id AS error_id
6080+ FROM orchestration_v2_projection_threads t
6081+ ${ latestFailureJoins }
6082+ INNER JOIN orchestration_v2_projection_runs r ON r.run_id = (
6083+ SELECT latest.run_id FROM orchestration_v2_projection_runs latest
6084+ WHERE latest.thread_id = t.thread_id
6085+ AND latest.status <> 'queued'
6086+ AND NOT (
6087+ latest.status = 'cancelled'
6088+ AND json_extract(latest.payload_json, '$.startedAt') IS NULL
6089+ )
6090+ AND NOT ${ isLimitedWake ( "latest" ) }
6091+ ORDER BY latest.completed_at IS NULL DESC, latest.completed_at DESC,
6092+ latest.ordinal DESC, latest.run_id DESC
6093+ LIMIT 1
6094+ ) AND r.status = 'failed'
6095+ INNER JOIN orchestration_v2_projection_turn_items item ON item.turn_item_id = (
6096+ SELECT error.turn_item_id FROM orchestration_v2_projection_turn_items error
6097+ WHERE error.thread_id = t.thread_id AND error.run_id = r.run_id
6098+ AND error.type = 'error' AND error.status = 'failed'
6099+ AND error.node_id IS json_extract(r.payload_json, '$.rootNodeId')
6100+ ORDER BY error.updated_at DESC, error.ordinal DESC, error.turn_item_id DESC
6101+ LIMIT 1
6102+ )
6103+ WHERE t.thread_id = ${ threadId }
6104+ AND EXISTS (
6105+ SELECT 1 FROM orchestration_v2_projection_turn_items limited
6106+ WHERE limited.thread_id = t.thread_id
6107+ AND limited.type = 'error' AND limited.status = 'failed'
6108+ AND json_extract(limited.payload_json, '$.failure.class') = 'usage_limit'
6109+ )
6110+ AND json_extract(item.payload_json, '$.failure.class') = 'usage_limit'
6111+ ), later AS (
6112+ -- Runs omitted here are not loaded at all, which leaves the result unchanged.
6113+ SELECT run.run_id, run.payload_json
6114+ FROM orchestration_v2_projection_runs run, blocked
6115+ WHERE run.thread_id = ${ threadId } AND run.ordinal > blocked.ordinal
6116+ ORDER BY run.ordinal DESC
6117+ LIMIT ${ LIMIT_EVIDENCE_MAX_RUNS }
6118+ )
6119+ SELECT 'run' AS kind, run_id AS id FROM blocked
6120+ UNION ALL SELECT 'item', error_id FROM blocked
6121+ UNION ALL SELECT 'run', run_id FROM later
6122+ UNION ALL
6123+ SELECT 'item', start.turn_item_id
6124+ FROM later
6125+ CROSS JOIN orchestration_v2_projection_turn_items start ON start.turn_item_id = (
6126+ SELECT first_input.turn_item_id FROM orchestration_v2_projection_turn_items first_input
6127+ WHERE first_input.run_id = later.run_id
6128+ AND first_input.node_id IS json_extract(later.payload_json, '$.rootNodeId')
6129+ AND first_input.type IN ('user_message', 'notification')
6130+ ORDER BY first_input.ordinal
6131+ LIMIT 1
6132+ )
6133+ WHERE start.type = 'notification'
6134+ ` ;
6135+ const loadedRunIds = new Set < string > ( projection . runs . map ( ( run ) => run . id ) ) ;
6136+ const loadedItemIds = new Set < string > ( projection . turnItems . map ( ( item ) => item . id ) ) ;
6137+ const runIds = rows . flatMap ( ( row ) =>
6138+ row . kind === "run" && ! loadedRunIds . has ( row . id ) ? [ row . id ] : [ ] ,
6139+ ) ;
6140+ const itemIds = rows . flatMap ( ( row ) =>
6141+ row . kind === "item" && ! loadedItemIds . has ( row . id ) ? [ row . id ] : [ ] ,
6142+ ) ;
6143+ if ( runIds . length === 0 && itemIds . length === 0 ) return projection ;
6144+ const runRows = yield * sql < PayloadRow > `
6145+ SELECT payload_json FROM orchestration_v2_projection_runs
6146+ WHERE thread_id = ${ threadId }
6147+ AND run_id IN (SELECT value FROM json_each(${ encodeIdList ( runIds ) } ))
6148+ ` ;
6149+ const itemRows = yield * sql < PayloadRow > `
6150+ SELECT payload_json FROM orchestration_v2_projection_turn_items
6151+ WHERE thread_id = ${ threadId }
6152+ AND turn_item_id IN (SELECT value FROM json_each(${ encodeIdList ( itemIds ) } ))
6153+ ` ;
6154+ const runs = yield * decodeRows ( decodeRunPayload , threadId ) ( runRows ) ;
6155+ const turnItems = yield * decodeRows ( decodeTurnItemPayload , threadId ) ( itemRows ) ;
6156+ return {
6157+ ...projection ,
6158+ runs : [ ...projection . runs , ...runs ] . toSorted ( ( left , right ) => left . ordinal - right . ordinal ) ,
6159+ turnItems : [ ...projection . turnItems , ...turnItems ] ,
6160+ } ;
6161+ } ) ;
6162+
60696163 const getThreadSnapshotWindow : ProjectionStoreV2Shape [ "getThreadSnapshotWindow" ] = (
60706164 threadId ,
60716165 options ,
@@ -6107,7 +6201,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
61076201 return {
61086202 schemaVersion : ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION ,
61096203 snapshotSequence : rows [ 0 ] ?. snapshot_sequence ?? 0 ,
6110- projection,
6204+ projection : yield * withLimitEvidence ( threadId , projection ) ,
61116205 } ;
61126206 } ) ,
61136207 )
0 commit comments