@@ -16,6 +16,7 @@ import * as Effect from "effect/Effect";
1616import * as Stream from "effect/Stream" ;
1717import * as TestClock from "effect/testing/TestClock" ;
1818import * as SqlClient from "effect/sql/SqlClient" ;
19+ import * as Statement from "effect/sql/Statement" ;
1920
2021import * as OrchestrationEventStore from "../persistence/OrchestrationEventStore.ts" ;
2122import * as ThreadManagement from "./ThreadManagementService.ts" ;
@@ -281,3 +282,38 @@ it.effect("retention keeps a deleted thread tombstone for old cursors", () =>
281282 assert . equal ( resumed [ 0 ] ?. kind === "event" ? resumed [ 0 ] . event . type : null , "thread.deleted" ) ;
282283 } ) . pipe ( Effect . provide ( layerTest ) ) ,
283284) ;
285+
286+ it . effect ( "a prompt reference added before deletion does not advance the replay floor" , ( ) =>
287+ Effect . gen ( function * ( ) {
288+ const sink = yield * EventSink . EventSinkV2 ;
289+ const maintenance = yield * ProjectionMaintenance . ProjectionMaintenanceV2 ;
290+ const applicationEvents = yield * OrchestrationEventStore . OrchestrationEventStore ;
291+ const sql = yield * SqlClient . SqlClient ;
292+ const oldAt = yield * DateTime . now ;
293+ const thread = makeThread ( ThreadId . make ( "thread:prompt-reference-race" ) , oldAt ) ;
294+ yield * sink . write ( {
295+ events : [ createdEvent ( thread , oldAt ) , ...promptEvents ( thread . id , oldAt , 1 ) ] ,
296+ } ) ;
297+ yield * TestClock . adjust ( Duration . hours ( 25 ) ) ;
298+ const now = yield * DateTime . now ;
299+ yield * sink . write ( { events : [ visitedEvent ( thread , "recent" , now ) ] } ) ;
300+ let inserted = false ;
301+ const addReference : Statement . Transformer = ( statement ) => {
302+ const [ query ] = statement . compile ( ) ;
303+ if ( ! inserted && query . includes ( "DELETE FROM orchestration_events AS candidate" ) ) {
304+ inserted = true ;
305+ return sink
306+ . write ( { events : promptEvents ( thread . id , now , 2 ) } )
307+ . pipe ( Effect . orDie , Effect . as ( statement ) ) ;
308+ }
309+ return Effect . succeed ( statement ) ;
310+ } ;
311+ const result = yield * maintenance
312+ . compactEventStore ( )
313+ . pipe ( Effect . provideService ( Statement . CurrentTransformer , addReference ) ) ;
314+ assert . isTrue ( inserted ) ;
315+ assert . equal ( result . deletedEventCount , 0 ) ;
316+ assert . equal ( yield * applicationEvents . replayFloor ( thread . id ) , 0 ) ;
317+ assert . deepEqual ( yield * sql `SELECT * FROM orchestration_v2_event_retention` , [ ] ) ;
318+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
319+ ) ;
0 commit comments