@@ -29,6 +29,7 @@ import type {
2929 OrchestrationV2ConversationMessage ,
3030 OrchestrationV2ContextTransfer ,
3131 OrchestrationV2DomainEvent ,
32+ OrchestrationV2LimitRecovery ,
3233 OrchestrationV2OlderSettledCount ,
3334 OrchestrationV2OlderSettledCursor ,
3435 OrchestrationV2ProjectedTurnItem ,
@@ -190,6 +191,11 @@ export type ProjectionLimitRecoveryCandidate = Pick<
190191 | "snoozedUntil"
191192> ;
192193
194+ export interface ProjectionAwaitedLimitRecovery {
195+ readonly threadId : ThreadId ;
196+ readonly limitRecovery : OrchestrationV2LimitRecovery ;
197+ }
198+
193199export type ProjectionThreadPullRequests = Pick <
194200 OrchestrationV2AppThread ,
195201 "id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pullRequests"
@@ -558,6 +564,12 @@ export interface ProjectionStoreV2Shape {
558564 readonly getLimitRecoveryWakeAt : (
559565 now : DateTime . Utc ,
560566 ) => Effect . Effect < number | null , ProjectionStoreV2Error > ;
567+ /** Auto-resume or snooze recoveries not yet reset, for runs these instances failed by `failedBefore`. */
568+ readonly getLimitRecoveriesAwaitingReset : ( options : {
569+ readonly now : DateTime . Utc ;
570+ readonly providerInstanceIds : ReadonlyArray < ProviderInstanceId > ;
571+ readonly failedBefore : DateTime . Utc ;
572+ } ) => Effect . Effect < ReadonlyArray < ProjectionAwaitedLimitRecovery > , ProjectionStoreV2Error > ;
561573 readonly getSettlementCandidates : (
562574 threadId ?: ThreadId ,
563575 ) => Effect . Effect < ReadonlyArray < ProjectionSettlementCandidate > , ProjectionStoreV2Error > ;
@@ -4135,7 +4147,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
41354147 json_extract(t.payload_json, '$.limitRecovery.runId') IS r.run_id
41364148 AND json_extract(t.payload_json, '$.limitRecovery.resetAt') IS json_extract(item.payload_json, '$.failure.resetAt')
41374149 AND json_extract(t.payload_json, '$.limitRecovery.autoResume') = 1
4138- AND julianday(json_extract(item.payload_json, '$.failure.resetAt')) <= julianday(${ DateTime . formatIso ( options . now ) } )
4150+ AND (
4151+ json_extract(t.payload_json, '$.limitRecovery.resetEarly') = 1
4152+ OR julianday(json_extract(item.payload_json, '$.failure.resetAt')) <= julianday(${ DateTime . formatIso ( options . now ) } )
4153+ )
41394154 AND (
41404155 json_extract(t.payload_json, '$.snoozedUntil') IS NULL
41414156 OR julianday(json_extract(t.payload_json, '$.snoozedUntil')) <= julianday(${ DateTime . formatIso ( options . now ) } )
@@ -4227,6 +4242,43 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
42274242 Effect . mapError ( ( cause ) => new ProjectionStoreSetupError ( { cause } ) ) ,
42284243 ) ;
42294244
4245+ const getLimitRecoveriesAwaitingReset = Effect . fn (
4246+ "ProjectionStore.getLimitRecoveriesAwaitingReset" ,
4247+ ) (
4248+ function * (
4249+ options : Parameters < ProjectionStoreV2Shape [ "getLimitRecoveriesAwaitingReset" ] > [ 0 ] ,
4250+ ) {
4251+ if ( options . providerInstanceIds . length === 0 ) return [ ] ;
4252+ const rows = yield * sql < { readonly payload_json : string } > `
4253+ SELECT t.payload_json
4254+ FROM orchestration_v2_projection_threads t
4255+ INNER JOIN orchestration_v2_projection_runs r
4256+ ON r.run_id = json_extract(t.payload_json, '$.limitRecovery.runId')
4257+ WHERE t.deleted_at IS NULL
4258+ AND json_extract(t.payload_json, '$.archivedAt') IS NULL
4259+ AND json_extract(t.payload_json, '$.settledOverride') IS NOT 'settled'
4260+ AND r.status = 'failed'
4261+ AND r.provider_instance_id IN ${ sql . in ( options . providerInstanceIds ) }
4262+ AND julianday(COALESCE(r.completed_at, json_extract(t.payload_json, '$.updatedAt'))) <= julianday(${ DateTime . formatIso ( options . failedBefore ) } )
4263+ AND julianday(json_extract(t.payload_json, '$.limitRecovery.resetAt')) > julianday(${ DateTime . formatIso ( options . now ) } )
4264+ AND json_extract(t.payload_json, '$.limitRecovery.resetEarly') IS NOT 1
4265+ AND (
4266+ json_extract(t.payload_json, '$.limitRecovery.autoResume') = 1
4267+ OR json_extract(t.payload_json, '$.limitRecovery.snooze') = 1
4268+ )
4269+ ORDER BY t.thread_id
4270+ ` ;
4271+ const waiting : Array < ProjectionAwaitedLimitRecovery > = [ ] ;
4272+ for ( const row of rows ) {
4273+ const thread = yield * decodeThreadPayload ( row . payload_json ) ;
4274+ if ( thread . limitRecovery != null )
4275+ waiting . push ( { threadId : thread . id , limitRecovery : thread . limitRecovery } ) ;
4276+ }
4277+ return waiting ;
4278+ } ,
4279+ Effect . mapError ( ( cause ) => new ProjectionStoreSetupError ( { cause } ) ) ,
4280+ ) ;
4281+
42304282 const getRecoveryThreadIds = Effect . fn ( "ProjectionStore.getRecoveryThreadIds" ) (
42314283 function * (
42324284 kind : ProjectionRecoveryKind ,
@@ -7657,6 +7709,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
76577709 getProviderControlContext : readOnReader ( getProviderControlContext ) ,
76587710 getLimitRecoveryCandidates : readOnReader ( getLimitRecoveryCandidates ) ,
76597711 getLimitRecoveryWakeAt : readOnReader ( getLimitRecoveryWakeAt ) ,
7712+ getLimitRecoveriesAwaitingReset : readOnReader ( getLimitRecoveriesAwaitingReset ) ,
76607713 getRecoveryThreadIds : readOnReader ( getRecoveryThreadIds ) ,
76617714 getUnreadableThreadIds : readOnReader ( getUnreadableThreadIds ) ,
76627715 getThreadSnapshot : readOnReader ( getThreadSnapshot ) ,
0 commit comments