@@ -27,54 +27,60 @@ export const make = Effect.gen(function* () {
2727 const projections = yield * ProjectionStore . ProjectionStoreV2 ;
2828 const settings = yield * ServerSettings . ServerSettingsService ;
2929
30- const compactWhenDue = ( threadId : ThreadId , runId : RunId ) : Effect . Effect < void > =>
31- Effect . gen ( function * ( ) {
32- const current = yield * settings . getSettings ;
33- if ( ! current . compactIdleThreads ) return ;
34- const records = yield * projections . getThreadRecords (
35- threadId ,
36- [
37- "runs" ,
38- "messages" ,
39- "providerThreads" ,
40- "providerTurns" ,
41- "runtimeRequests" ,
42- "turnItems" ,
43- "contextTransfers" ,
44- ] ,
45- { messageRunIds : [ runId ] , turnItemStatuses : [ "pending" , "running" , "waiting" ] } ,
46- ) ;
47- if (
48- ! resolveProjectProviderInstanceEnabled (
49- current ,
50- records . thread . projectId ,
51- records . thread . providerInstanceId ,
30+ const compactWhenDue = Effect . fn ( "IdleCompactionService.compactWhenDue" ) (
31+ function * ( threadId : ThreadId , runId : RunId ) {
32+ // Loops once after the wait: the thread may have moved on.
33+ while ( true ) {
34+ const current = yield * settings . getSettings ;
35+ if ( ! current . compactIdleThreads ) return ;
36+ const records = yield * projections . getThreadRecords (
37+ threadId ,
38+ [
39+ "runs" ,
40+ "messages" ,
41+ "providerThreads" ,
42+ "providerTurns" ,
43+ "runtimeRequests" ,
44+ "turnItems" ,
45+ "contextTransfers" ,
46+ ] ,
47+ { messageRunIds : [ runId ] , turnItemStatuses : [ "pending" , "running" , "waiting" ] } ,
48+ ) ;
49+ if (
50+ ! resolveProjectProviderInstanceEnabled (
51+ current ,
52+ records . thread . projectId ,
53+ records . thread . providerInstanceId ,
54+ )
5255 )
53- )
56+ return ;
57+ const nowMs = DateTime . toEpochMillis ( yield * DateTime . now ) ;
58+ const window = idleCompactionWindow ( records , nowMs ) ;
59+ if ( window ?. runId !== runId ) return ;
60+ if ( nowMs < window . startMs ) {
61+ yield * Effect . sleep ( Duration . millis ( window . startMs - nowMs ) ) ;
62+ continue ;
63+ }
64+ yield * orchestrator . dispatch ( {
65+ type : "message.dispatch" ,
66+ // One compaction per completed run, even across duplicate events.
67+ commandId : CommandId . make ( `server:idle-compact:${ threadId } :${ runId } ` ) ,
68+ messageId : MessageId . make ( `idle-compact:${ threadId } :${ runId } ` ) ,
69+ threadId,
70+ text : "/compact" ,
71+ attachments : [ ] ,
72+ createdBy : "agent" ,
73+ creationSource : "server" ,
74+ dispatchMode : { type : "start_immediately" } ,
75+ } ) ;
5476 return ;
55- const nowMs = DateTime . toEpochMillis ( yield * DateTime . now ) ;
56- const window = idleCompactionWindow ( records , nowMs ) ;
57- if ( window ?. runId !== runId ) return ;
58- if ( nowMs < window . startMs ) {
59- // Recheck after the wait: the thread may have moved on.
60- yield * Effect . sleep ( Duration . millis ( window . startMs - nowMs ) ) ;
61- return yield * compactWhenDue ( threadId , runId ) ;
6277 }
63- yield * orchestrator . dispatch ( {
64- type : "message.dispatch" ,
65- // One compaction per completed run, even across duplicate events.
66- commandId : CommandId . make ( `server:idle-compact:${ threadId } :${ runId } ` ) ,
67- messageId : MessageId . make ( `idle-compact:${ threadId } :${ runId } ` ) ,
68- threadId,
69- text : "/compact" ,
70- attachments : [ ] ,
71- createdBy : "agent" ,
72- creationSource : "server" ,
73- dispatchMode : { type : "start_immediately" } ,
74- } ) ;
75- } ) . pipe (
76- Effect . catch ( ( cause ) => Effect . logWarning ( "idle compaction skipped" , { threadId, cause } ) ) ,
77- ) ;
78+ } ,
79+ ( effect , threadId ) =>
80+ effect . pipe (
81+ Effect . catch ( ( cause ) => Effect . logWarning ( "idle compaction skipped" , { threadId, cause } ) ) ,
82+ ) ,
83+ ) ;
7884
7985 const start = Effect . fn ( "IdleCompactionService.start" ) ( function * ( ) {
8086 // Keyed by thread: a newer completed run replaces the waiting timer.
0 commit comments