@@ -27,10 +27,12 @@ import {
2727} from "@t3tools/contracts" ;
2828import * as DateTime from "effect/DateTime" ;
2929import * as Deferred from "effect/Deferred" ;
30+ import * as Duration from "effect/Duration" ;
3031import * as Effect from "effect/Effect" ;
3132import * as Fiber from "effect/Fiber" ;
3233import * as Layer from "effect/Layer" ;
3334import * as Option from "effect/Option" ;
35+ import * as Queue from "effect/Queue" ;
3436import * as Ref from "effect/Ref" ;
3537import * as Scheduler from "effect/Scheduler" ;
3638import * as Schema from "effect/Schema" ;
@@ -65,6 +67,8 @@ const storesProvided = Layer.mergeAll(databaseLayer, eventStoreProvided, project
6567const eventSinkProvided = EventSink . layer . pipe ( Layer . provide ( storesProvided ) ) ;
6668const effectOutboxProvided = EffectOutbox . layer . pipe ( Layer . provide ( databaseLayer ) ) ;
6769const commandReceiptStoreProvided = CommandReceiptStore . layer . pipe ( Layer . provide ( databaseLayer ) ) ;
70+ // Its own database, for tests that act on every row in the outbox table.
71+ const isolatedOutboxLayer = Layer . fresh ( EffectOutbox . layer . pipe ( Layer . provideMerge ( databaseLayer ) ) ) ;
6872const projectionMaintenanceProvided = ProjectionMaintenance . layer . pipe (
6973 Layer . provide ( storesProvided ) ,
7074) ;
@@ -2223,6 +2227,95 @@ it.layer(TestLayer)("orchestration V2 foundation persistence", (it) => {
22232227 } ) ,
22242228 ) ;
22252229
2230+ it . effect ( "prunes settled effects past retention in batches and keeps the rest" , ( ) =>
2231+ Effect . gen ( function * ( ) {
2232+ const outbox = yield * EffectOutbox . EffectOutboxV2 ;
2233+ const sql = yield * SqlClient . SqlClient ;
2234+ const now = yield * DateTime . now ;
2235+ const insert = (
2236+ prefix : string ,
2237+ count : number ,
2238+ status : EffectOutbox . OrchestrationEffectStatusV2 ,
2239+ completedAgo : Duration . Duration | null ,
2240+ ) => {
2241+ const completedAt =
2242+ completedAgo === null
2243+ ? null
2244+ : DateTime . formatIso ( DateTime . subtractDuration ( now , completedAgo ) ) ;
2245+ const createdAt = DateTime . formatIso ( now ) ;
2246+ return sql `
2247+ WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${ count } )
2248+ INSERT INTO orchestration_v2_effect_outbox (
2249+ effect_id, command_id, thread_id, effect_type, payload_json, status,
2250+ available_at, created_at, updated_at, completed_at
2251+ )
2252+ SELECT ${ prefix } || i, 'command:prune', 'thread:prune', 'terminal.cleanup',
2253+ '{"type":"terminal.cleanup"}', ${ status } , ${ createdAt } , ${ createdAt } , ${ createdAt } ,
2254+ ${ completedAt }
2255+ FROM n
2256+ ` ;
2257+ } ;
2258+ const old = Duration . sum ( EffectOutbox . SETTLED_EFFECT_RETENTION , Duration . minutes ( 1 ) ) ;
2259+ const recent = Duration . subtract ( EffectOutbox . SETTLED_EFFECT_RETENTION , Duration . minutes ( 1 ) ) ;
2260+ // More expired rows than one delete batch.
2261+ yield * insert ( "succeeded-old:" , 1_201 , "succeeded" , old ) ;
2262+ yield * insert ( "cancelled-old:" , 2 , "cancelled" , old ) ;
2263+ yield * insert ( "succeeded-recent:" , 1 , "succeeded" , recent ) ;
2264+ yield * insert ( "failed-old:" , 1 , "failed" , old ) ;
2265+ yield * insert ( "pending:" , 1 , "pending" , null ) ;
2266+ yield * insert ( "running:" , 1 , "running" , null ) ;
2267+
2268+ assert . equal ( yield * outbox . pruneSettled , 1_203 ) ;
2269+
2270+ const remaining = yield * sql < { readonly effect_id : string } > `
2271+ SELECT effect_id FROM orchestration_v2_effect_outbox ORDER BY effect_id
2272+ ` ;
2273+ assert . deepEqual (
2274+ remaining . map ( ( row ) => row . effect_id ) ,
2275+ [ "failed-old:1" , "pending:1" , "running:1" , "succeeded-recent:1" ] ,
2276+ ) ;
2277+ assert . equal ( yield * outbox . pruneSettled , 0 ) ;
2278+ } ) . pipe ( Effect . provide ( isolatedOutboxLayer ) ) ,
2279+ ) ;
2280+
2281+ it . effect ( "prunes settled effects hourly from the layer-owned worker" , ( ) =>
2282+ Effect . gen ( function * ( ) {
2283+ const outbox = yield * EffectOutbox . EffectOutboxV2 ;
2284+ const commandId = CommandId . make ( "command:foundation-prune-worker" ) ;
2285+ const threadId = ThreadId . make ( "thread:foundation-prune-worker" ) ;
2286+ const workerId = "prune-worker" ;
2287+ const request = { type : "terminal.cleanup" } as const ;
2288+ yield * outbox . enqueue ( [ { id : "effect:prune-worker:done" , commandId, threadId, request } ] ) ;
2289+ yield * outbox . claimNext ( { workerId, leaseDurationMs : 30_000 } ) ;
2290+ assert . isTrue ( yield * outbox . succeed ( { effectId : "effect:prune-worker:done" , workerId } ) ) ;
2291+ yield * outbox . enqueue ( [ { id : "effect:prune-worker:pending" , commandId, threadId, request } ] ) ;
2292+ // Observe each run so the test waits for it instead of racing the clock.
2293+ const runs = yield * Queue . unbounded < number > ( ) ;
2294+ const observed = EffectOutbox . EffectOutboxV2 . of ( {
2295+ ...outbox ,
2296+ pruneSettled : outbox . pruneSettled . pipe ( Effect . tap ( ( pruned ) => Queue . offer ( runs , pruned ) ) ) ,
2297+ } ) ;
2298+ const ids = Effect . map ( outbox . listByCommandId ( commandId ) , ( rows ) =>
2299+ rows . map ( ( row ) => row . id ) . toSorted ( ) ,
2300+ ) ;
2301+
2302+ yield * TestClock . adjust (
2303+ Duration . subtract ( EffectOutbox . SETTLED_EFFECT_RETENTION , Duration . minutes ( 30 ) ) ,
2304+ ) ;
2305+ yield * Layer . build (
2306+ EffectOutbox . pruneWorkerLive . pipe (
2307+ Layer . provide ( Layer . succeed ( EffectOutbox . EffectOutboxV2 , observed ) ) ,
2308+ ) ,
2309+ ) ;
2310+ assert . equal ( yield * Queue . take ( runs ) , 0 ) ;
2311+ assert . deepEqual ( yield * ids , [ "effect:prune-worker:done" , "effect:prune-worker:pending" ] ) ;
2312+
2313+ yield * TestClock . adjust ( "1 hour" ) ;
2314+ assert . equal ( yield * Queue . take ( runs ) , 1 ) ;
2315+ assert . deepEqual ( yield * ids , [ "effect:prune-worker:pending" ] ) ;
2316+ } ) . pipe ( Effect . scoped , Effect . provide ( isolatedOutboxLayer ) ) ,
2317+ ) ;
2318+
22262319 it . effect ( "does not emit a SQL span for an empty safety claim" , ( ) =>
22272320 Effect . gen ( function * ( ) {
22282321 const outbox = yield * EffectOutbox . EffectOutboxV2 ;
0 commit comments