55 NodeId ,
66 type OrchestrationV2AppThread ,
77 type OrchestrationV2Run ,
8+ type OrchestrationV2RuntimeRequest ,
89 ProjectId ,
910 ProviderDriverKind ,
1011 ProviderInstanceId ,
@@ -23,7 +24,11 @@ import * as Statement from "effect/sql/Statement";
2324
2425import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
2526import * as ProjectionStore from "./ProjectionStore.ts" ;
26- import { isAutoSettlementCandidate , resolveAutoSettlementAt } from "./ThreadSettlementService.ts" ;
27+ import {
28+ isAutoSettlementCandidate ,
29+ isSnoozed ,
30+ resolveAutoSettlementAt ,
31+ } from "./ThreadSettlementService.ts" ;
2732
2833const layerSql = ProjectionStore . layer . pipe ( Layer . provideMerge ( SqlitePersistence . layerMemory ) ) ;
2934const now = DateTime . makeUnsafe ( "2026-09-04T12:00:00Z" ) ;
@@ -140,6 +145,78 @@ const createItem = Effect.fn(function* (
140145 } ) ;
141146} ) ;
142147
148+ it . effect . each ( [
149+ [ "sql" , layerSql ] ,
150+ [ "memory" , ProjectionStore . layerMemory ] ,
151+ ] as const ) (
152+ "%s: pending approvals wake a snoozed question even with older creation times" ,
153+ ( [ , testLayer ] ) =>
154+ Effect . gen ( function * ( ) {
155+ const store = yield * ProjectionStore . ProjectionStoreV2 ;
156+ const threadId = yield * createThread ( "snoozed-question-approval" , {
157+ snoozedAt : now ,
158+ snoozedUntil : DateTime . add ( now , { days : 1 } ) ,
159+ } ) ;
160+ const question : OrchestrationV2RuntimeRequest = {
161+ id : RuntimeRequestId . make ( "question" ) ,
162+ nodeId : NodeId . make ( "node:question" ) ,
163+ providerTurnId : null ,
164+ nativeRequestRef : null ,
165+ kind : "user_input" ,
166+ status : "pending" ,
167+ responseCapability : { type : "message" } ,
168+ createdAt : DateTime . subtract ( now , { hours : 1 } ) ,
169+ resolvedAt : null ,
170+ } ;
171+ const approval : OrchestrationV2RuntimeRequest = {
172+ ...question ,
173+ id : RuntimeRequestId . make ( "approval" ) ,
174+ kind : "command" ,
175+ createdAt : old ,
176+ } ;
177+ const newerApproval = {
178+ ...approval ,
179+ id : RuntimeRequestId . make ( "newer-approval" ) ,
180+ createdAt : DateTime . add ( old , { hours : 1 } ) ,
181+ } ;
182+ const setRequest = ( request : OrchestrationV2RuntimeRequest ) =>
183+ store . apply ( {
184+ id : EventId . make ( `event:${ request . id } :${ request . status } ` ) ,
185+ type : "runtime-request.updated" ,
186+ threadId,
187+ occurredAt : now ,
188+ payload : request ,
189+ } ) ;
190+ const assertPending = ( expected : RuntimeRequestId , snoozed : boolean ) =>
191+ Effect . gen ( function * ( ) {
192+ const snapshot = yield * store . getShellSnapshot ( ) ;
193+ const shell = snapshot . threads . find ( ( thread ) => thread . id === threadId ) ! ;
194+ const projection = yield * store . getThreadProjection ( threadId ) ;
195+ for ( const candidate of [ shell , ProjectionStore . threadShellFromProjection ( projection ) ] ) {
196+ assert . equal ( candidate . pendingRuntimeRequest ?. id , expected ) ;
197+ assert . equal ( isSnoozed ( candidate , DateTime . toEpochMillis ( now ) ) , snoozed ) ;
198+ }
199+ } ) ;
200+ yield * setRequest ( {
201+ ...question ,
202+ id : RuntimeRequestId . make ( "older-question" ) ,
203+ createdAt : old ,
204+ } ) ;
205+ yield * setRequest ( question ) ;
206+ yield * assertPending ( question . id , true ) ;
207+
208+ // A request becoming pending can retain a creation time older than the snooze.
209+ yield * setRequest ( approval ) ;
210+ yield * assertPending ( approval . id , false ) ;
211+ yield * setRequest ( newerApproval ) ;
212+ yield * assertPending ( newerApproval . id , false ) ;
213+ yield * setRequest ( { ...newerApproval , status : "resolved" , resolvedAt : now } ) ;
214+ yield * assertPending ( approval . id , false ) ;
215+ yield * setRequest ( { ...approval , status : "resolved" , resolvedAt : now } ) ;
216+ yield * assertPending ( question . id , true ) ;
217+ } ) . pipe ( Effect . provide ( testLayer ) ) ,
218+ ) ;
219+
143220it . effect . each ( [
144221 [ "sql" , layerSql ] ,
145222 [ "memory" , ProjectionStore . layerMemory ] ,
0 commit comments