2026-04-26 07:51:25 +01:00
use codex_utils_absolute_path ::AbsolutePathBuf ;
2026-05-19 14:04:07 +01:00
use notify ::RecursiveMode ;
use notify ::Watcher ;
2026-04-26 07:51:25 +01:00
use std ::collections ::HashMap ;
use std ::path ::PathBuf ;
use std ::sync ::Arc ;
use std ::time ::SystemTime ;
use std ::time ::UNIX_EPOCH ;
use tokio ::sync ::Mutex as AsyncMutex ;
2026-05-19 14:04:07 +01:00
use tokio ::sync ::mpsc ;
2026-04-26 07:51:25 +01:00
use tokio ::sync ::oneshot ;
use tracing ::warn ;
2026-05-14 06:42:39 +01:00
use crate ::reorient ::EpiphanyFreshnessWatcherSnapshot ;
2026-04-26 07:51:25 +01:00
#[ derive(Clone, Debug, PartialEq, Eq) ]
2026-05-13 12:30:24 +01:00
pub struct EpiphanyInvalidationSnapshot {
2026-04-26 07:51:25 +01:00
pub available : bool ,
pub workspace_root : Option < AbsolutePathBuf > ,
pub observed_at_unix_seconds : Option < i64 > ,
pub changed_paths : Vec < PathBuf > ,
}
2026-05-14 06:42:39 +01:00
pub fn epiphany_freshness_watcher_snapshot (
snapshot : & EpiphanyInvalidationSnapshot ,
) -> EpiphanyFreshnessWatcherSnapshot < '_ > {
EpiphanyFreshnessWatcherSnapshot {
available : snapshot . available ,
workspace_root : snapshot . workspace_root . as_ref ( ) . map ( | path | path . as_path ( ) ) ,
observed_at_unix_seconds : snapshot . observed_at_unix_seconds ,
changed_paths : snapshot . changed_paths . as_slice ( ) ,
}
}
2026-04-26 07:51:25 +01:00
#[ derive(Clone) ]
2026-05-13 12:30:24 +01:00
pub struct EpiphanyInvalidationManager {
2026-04-26 07:51:25 +01:00
state : Arc < AsyncMutex < HashMap < String , WatchEntry > > > ,
}
struct WatchEntry {
workspace_root : AbsolutePathBuf ,
latest : Arc < AsyncMutex < LatestInvalidation > > ,
terminate_tx : oneshot ::Sender < oneshot ::Sender < ( ) > > ,
2026-05-19 14:04:07 +01:00
_watcher : notify ::RecommendedWatcher ,
2026-04-26 07:51:25 +01:00
}
#[ derive(Clone, Debug, Default, PartialEq, Eq) ]
struct LatestInvalidation {
observed_at_unix_seconds : Option < i64 > ,
changed_paths : Vec < PathBuf > ,
}
impl EpiphanyInvalidationManager {
2026-05-13 12:30:24 +01:00
pub fn new ( ) -> Self {
2026-05-19 14:04:07 +01:00
Self {
state : Arc ::new ( AsyncMutex ::new ( HashMap ::new ( ) ) ) ,
2026-04-26 07:51:25 +01:00
}
}
2026-05-13 12:30:24 +01:00
pub async fn ensure_thread_watch ( & self , thread_id : & str , workspace_root : & AbsolutePathBuf ) {
2026-04-26 07:51:25 +01:00
let existing_entry = {
let mut state = self . state . lock ( ) . await ;
match state . get ( thread_id ) {
Some ( entry ) if & entry . workspace_root = = workspace_root = > return ,
Some ( _ ) = > state . remove ( thread_id ) ,
None = > None ,
}
} ;
if let Some ( entry ) = existing_entry {
stop_watch_entry ( entry ) . await ;
}
let latest = Arc ::new ( AsyncMutex ::new ( LatestInvalidation ::default ( ) ) ) ;
2026-05-19 14:04:07 +01:00
let ( event_tx , mut event_rx ) = mpsc ::unbounded_channel ::< Vec < PathBuf > > ( ) ;
let mut watcher =
match notify ::recommended_watcher ( move | result : notify ::Result < notify ::Event > | {
match result {
Ok ( event ) = > {
let _ = event_tx . send ( event . paths ) ;
}
Err ( err ) = > warn! ( " epiphany invalidation watcher event failed: {err} " ) ,
}
} ) {
Ok ( watcher ) = > watcher ,
Err ( err ) = > {
warn! ( " epiphany invalidation manager could not create watcher: {err} " ) ;
return ;
}
} ;
if let Err ( err ) = watcher . watch ( workspace_root . as_path ( ) , RecursiveMode ::Recursive ) {
warn! (
" epiphany invalidation manager could not watch `{}`: {err} " ,
workspace_root . display ( )
) ;
return ;
}
2026-04-26 07:51:25 +01:00
let ( terminate_tx , terminate_rx ) = oneshot ::channel ( ) ;
let workspace_root_path = workspace_root . to_path_buf ( ) ;
let entry = WatchEntry {
workspace_root : workspace_root . clone ( ) ,
latest : Arc ::clone ( & latest ) ,
terminate_tx ,
2026-05-19 14:04:07 +01:00
_watcher : watcher ,
2026-04-26 07:51:25 +01:00
} ;
self . state . lock ( ) . await . insert ( thread_id . to_string ( ) , entry ) ;
tokio ::spawn ( async move {
let mut terminate_rx = terminate_rx ;
let mut done_tx = None ;
loop {
2026-05-19 14:04:07 +01:00
let paths = tokio ::select! {
2026-04-26 07:51:25 +01:00
biased ;
result = & mut terminate_rx = > {
done_tx = result . ok ( ) ;
break ;
}
2026-05-19 14:04:07 +01:00
paths = event_rx . recv ( ) = > match paths {
Some ( paths ) = > paths ,
2026-04-26 07:51:25 +01:00
None = > break ,
} ,
} ;
let observed_at_unix_seconds = unix_now ( ) ;
let mut latest = latest . lock ( ) . await ;
latest . observed_at_unix_seconds = Some ( observed_at_unix_seconds ) ;
latest . changed_paths =
2026-05-19 14:04:07 +01:00
normalize_changed_paths ( paths , workspace_root_path . as_path ( ) ) ;
2026-04-26 07:51:25 +01:00
}
if let Some ( done_tx ) = done_tx {
let _ = done_tx . send ( ( ) ) ;
}
} ) ;
}
2026-05-13 12:30:24 +01:00
pub async fn snapshot ( & self , thread_id : & str ) -> EpiphanyInvalidationSnapshot {
2026-04-26 07:51:25 +01:00
let ( workspace_root , latest ) = {
let state = self . state . lock ( ) . await ;
match state . get ( thread_id ) {
Some ( entry ) = > (
Some ( entry . workspace_root . clone ( ) ) ,
Some ( Arc ::clone ( & entry . latest ) ) ,
) ,
None = > ( None , None ) ,
}
} ;
let Some ( latest ) = latest else {
return EpiphanyInvalidationSnapshot {
available : false ,
workspace_root : None ,
observed_at_unix_seconds : None ,
changed_paths : Vec ::new ( ) ,
} ;
} ;
let latest = latest . lock ( ) . await . clone ( ) ;
EpiphanyInvalidationSnapshot {
available : true ,
workspace_root ,
observed_at_unix_seconds : latest . observed_at_unix_seconds ,
changed_paths : latest . changed_paths ,
}
}
2026-05-13 12:30:24 +01:00
pub async fn remove_thread ( & self , thread_id : & str ) {
2026-04-26 07:51:25 +01:00
let entry = self . state . lock ( ) . await . remove ( thread_id ) ;
if let Some ( entry ) = entry {
stop_watch_entry ( entry ) . await ;
}
}
}
async fn stop_watch_entry ( entry : WatchEntry ) {
let ( done_tx , done_rx ) = oneshot ::channel ( ) ;
let _ = entry . terminate_tx . send ( done_tx ) ;
let _ = done_rx . await ;
}
fn unix_now ( ) -> i64 {
SystemTime ::now ( )
. duration_since ( UNIX_EPOCH )
. map ( | duration | duration . as_secs ( ) as i64 )
. unwrap_or_default ( )
}
fn normalize_changed_paths ( paths : Vec < PathBuf > , workspace_root : & std ::path ::Path ) -> Vec < PathBuf > {
let mut normalized = paths
. into_iter ( )
. filter_map ( | path | match path . strip_prefix ( workspace_root ) {
Ok ( relative ) if relative . as_os_str ( ) . is_empty ( ) = > None ,
Ok ( relative ) = > Some ( relative . to_path_buf ( ) ) ,
Err ( _ ) = > Some ( path ) ,
} )
. collect ::< Vec < _ > > ( ) ;
normalized . sort ( ) ;
normalized . dedup ( ) ;
normalized
}