@@ -3,7 +3,7 @@ use std::convert::Infallible;
33use std:: net:: { Ipv4Addr , SocketAddr , SocketAddrV4 , TcpListener as StdTcpListener } ;
44use std:: sync:: Arc ;
55use std:: thread;
6- use std:: time:: { Duration , SystemTime , UNIX_EPOCH } ;
6+ use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
77
88use async_stream:: stream;
99use axum:: extract:: { Path , State } ;
@@ -90,8 +90,15 @@ impl JobEvent {
9090struct JobState {
9191 history : Vec < JobEvent > ,
9292 sender : broadcast:: Sender < JobEvent > ,
93+ /// Set once the job completes or fails; until then the job is live and kept.
94+ finished_at : Option < Instant > ,
9395}
9496
97+ /// How long a finished job stays subscribable. Long enough that a reloaded page
98+ /// can still collect the result it was waiting on, short enough that completed
99+ /// jobs — whose history holds the full result payload — do not accumulate.
100+ const JOB_RETENTION : Duration = Duration :: from_secs ( 30 * 60 ) ;
101+
95102#[ derive( Default ) ]
96103pub struct ActivityManager {
97104 jobs : RwLock < HashMap < String , JobState > > ,
@@ -124,12 +131,15 @@ impl ActivityManager {
124131 timestamp_ms : timestamp_ms ( ) ,
125132 } ;
126133
134+ self . evict_finished ( JOB_RETENTION ) . await ;
135+
127136 let mut jobs = self . jobs . write ( ) . await ;
128137 jobs. insert (
129138 job_id. clone ( ) ,
130139 JobState {
131140 history : vec ! [ started] ,
132141 sender,
142+ finished_at : None ,
133143 } ,
134144 ) ;
135145
@@ -148,9 +158,22 @@ impl ActivityManager {
148158 Some ( ( job. history . clone ( ) , job. sender . subscribe ( ) ) )
149159 }
150160
161+ /// Drop finished jobs older than `retention`. Called when a new job starts,
162+ /// which keeps the map bounded without a timer task: nothing is created, so
163+ /// nothing grows.
164+ pub async fn evict_finished ( & self , retention : Duration ) {
165+ self . jobs
166+ . write ( )
167+ . await
168+ . retain ( |_, job| job. finished_at . is_none_or ( |at| at. elapsed ( ) < retention) ) ;
169+ }
170+
151171 async fn push_event ( & self , job_id : & str , event : JobEvent ) {
152172 let mut jobs = self . jobs . write ( ) . await ;
153173 if let Some ( job) = jobs. get_mut ( job_id) {
174+ if event. is_terminal ( ) {
175+ job. finished_at = Some ( Instant :: now ( ) ) ;
176+ }
154177 job. history . push ( event. clone ( ) ) ;
155178 let _ = job. sender . send ( event) ;
156179 }
@@ -312,3 +335,70 @@ fn timestamp_ms() -> u64 {
312335 . map ( |duration| duration. as_millis ( ) as u64 )
313336 . unwrap_or ( 0 )
314337}
338+
339+ #[ cfg( test) ]
340+ mod tests {
341+ use super :: * ;
342+
343+ async fn finished_job ( manager : & Arc < ActivityManager > ) -> String {
344+ let handle = manager
345+ . create_job ( "refinement" , "codex" , "default" , "Refining" )
346+ . await ;
347+ handle
348+ . complete ( "refinement" , serde_json:: json!( { "refined_groups" : [ ] } ) )
349+ . await ;
350+ handle. job_id ( ) . to_string ( )
351+ }
352+
353+ #[ tokio:: test]
354+ async fn evicts_finished_jobs_past_retention_and_keeps_live_ones ( ) {
355+ let manager = Arc :: new ( ActivityManager :: new ( ) ) ;
356+ let done = finished_job ( & manager) . await ;
357+ let live = manager
358+ . create_job ( "refinement" , "codex" , "default" , "Still going" )
359+ . await
360+ . job_id ( )
361+ . to_string ( ) ;
362+
363+ manager. evict_finished ( Duration :: ZERO ) . await ;
364+
365+ assert ! (
366+ manager. subscribe( & done) . await . is_none( ) ,
367+ "finished job should be dropped once past retention"
368+ ) ;
369+ assert ! (
370+ manager. subscribe( & live) . await . is_some( ) ,
371+ "a job that has not reached a terminal event must survive eviction"
372+ ) ;
373+ }
374+
375+ #[ tokio:: test]
376+ async fn keeps_finished_jobs_within_retention_so_a_reload_can_collect_them ( ) {
377+ let manager = Arc :: new ( ActivityManager :: new ( ) ) ;
378+ let done = finished_job ( & manager) . await ;
379+
380+ manager. evict_finished ( JOB_RETENTION ) . await ;
381+
382+ let ( history, _) = manager. subscribe ( & done) . await . expect ( "job still retained" ) ;
383+ assert ! ( history. iter( ) . any( JobEvent :: is_terminal) ) ;
384+ }
385+
386+ #[ tokio:: test]
387+ async fn starting_a_job_sweeps_the_map ( ) {
388+ let manager = Arc :: new ( ActivityManager :: new ( ) ) ;
389+ let done = finished_job ( & manager) . await ;
390+ manager
391+ . jobs
392+ . write ( )
393+ . await
394+ . get_mut ( & done)
395+ . unwrap ( )
396+ . finished_at = Some ( Instant :: now ( ) - JOB_RETENTION - Duration :: from_secs ( 1 ) ) ;
397+
398+ let _ = manager
399+ . create_job ( "refinement" , "codex" , "default" , "Next" )
400+ . await ;
401+
402+ assert ! ( manager. subscribe( & done) . await . is_none( ) ) ;
403+ }
404+ }
0 commit comments