Move Epiphany invalidation into bridge
This commit is contained in:
parent
f63a882f73
commit
9d2b8540f5
11 changed files with 23 additions and 16 deletions
|
|
@ -4,7 +4,6 @@ use crate::codex_message_processor::maybe_run_epiphany_pre_compaction_checkpoint
|
|||
use crate::codex_message_processor::read_rollout_items_from_rollout;
|
||||
use crate::codex_message_processor::read_summary_from_rollout;
|
||||
use crate::codex_message_processor::summary_to_thread;
|
||||
use crate::epiphany_invalidation::EpiphanyInvalidationManager;
|
||||
use crate::error_code::INTERNAL_ERROR_CODE;
|
||||
use crate::error_code::INVALID_REQUEST_ERROR_CODE;
|
||||
use crate::outgoing_message::ClientRequestResult;
|
||||
|
|
@ -148,6 +147,7 @@ use codex_protocol::request_user_input::RequestUserInputResponse as CoreRequestU
|
|||
use codex_sandboxing::policy_transforms::intersect_permission_profiles;
|
||||
use codex_shell_command::parse_command::shlex_join;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use epiphany_codex_bridge::invalidation::EpiphanyInvalidationManager;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
|
|
|||
|
|
@ -11,8 +11,6 @@ use crate::bespoke_event_handling::maybe_emit_hook_prompt_item_completed;
|
|||
use crate::command_exec::CommandExecManager;
|
||||
use crate::command_exec::StartCommandExecParams;
|
||||
use crate::config_manager::ConfigManager;
|
||||
use crate::epiphany_invalidation::EpiphanyInvalidationManager;
|
||||
use crate::epiphany_invalidation::EpiphanyInvalidationSnapshot;
|
||||
use crate::error_code::INPUT_TOO_LARGE_ERROR_CODE;
|
||||
use crate::error_code::INTERNAL_ERROR_CODE;
|
||||
use crate::error_code::INVALID_PARAMS_ERROR_CODE;
|
||||
|
|
@ -327,6 +325,8 @@ use codex_thread_store::ThreadStoreError;
|
|||
use codex_thread_store::UpdateThreadMetadataParams as StoreUpdateThreadMetadataParams;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_pty::DEFAULT_OUTPUT_BYTES_CAP;
|
||||
use epiphany_codex_bridge::invalidation::EpiphanyInvalidationManager;
|
||||
use epiphany_codex_bridge::invalidation::EpiphanyInvalidationSnapshot;
|
||||
use epiphany_codex_bridge::reorient::EpiphanyFreshnessWatcherSnapshot;
|
||||
use epiphany_codex_bridge::state::live_thread_epiphany_state;
|
||||
use std::collections::HashMap;
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ use epiphany_codex_bridge::coordinator::epiphany_reorient_finding_already_accept
|
|||
use epiphany_codex_bridge::coordinator::map_epiphany_crrc_recommendation;
|
||||
use epiphany_codex_bridge::coordinator::map_epiphany_roles;
|
||||
use epiphany_codex_bridge::coordinator::select_epiphany_coordinator_automation_action;
|
||||
use epiphany_codex_bridge::invalidation::EpiphanyInvalidationManager;
|
||||
use epiphany_codex_bridge::jobs::map_epiphany_jobs;
|
||||
use epiphany_codex_bridge::launch::EPIPHANY_REORIENT_LAUNCH_BINDING_ID;
|
||||
use epiphany_codex_bridge::launch::build_epiphany_reorient_launch_request;
|
||||
|
|
@ -31,7 +32,6 @@ use tokio::sync::Mutex;
|
|||
use tracing::warn;
|
||||
|
||||
use super::epiphany_freshness_watcher_snapshot;
|
||||
use crate::epiphany_invalidation::EpiphanyInvalidationManager;
|
||||
use crate::outgoing_message::ThreadScopedOutgoingMessageSender;
|
||||
use crate::thread_state::ThreadState;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,210 +0,0 @@
|
|||
use codex_core::file_watcher::FileWatcher;
|
||||
use codex_core::file_watcher::FileWatcherSubscriber;
|
||||
use codex_core::file_watcher::ThrottledWatchReceiver;
|
||||
use codex_core::file_watcher::WatchPath;
|
||||
use codex_core::file_watcher::WatchRegistration;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::time::SystemTime;
|
||||
use std::time::UNIX_EPOCH;
|
||||
use tokio::sync::Mutex as AsyncMutex;
|
||||
use tokio::sync::oneshot;
|
||||
use tracing::warn;
|
||||
|
||||
const INVALIDATION_WATCH_DEBOUNCE: Duration = Duration::from_millis(200);
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct EpiphanyInvalidationSnapshot {
|
||||
pub available: bool,
|
||||
pub workspace_root: Option<AbsolutePathBuf>,
|
||||
pub observed_at_unix_seconds: Option<i64>,
|
||||
pub changed_paths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct EpiphanyInvalidationManager {
|
||||
available: bool,
|
||||
file_watcher: Arc<FileWatcher>,
|
||||
state: Arc<AsyncMutex<HashMap<String, WatchEntry>>>,
|
||||
}
|
||||
|
||||
struct WatchEntry {
|
||||
workspace_root: AbsolutePathBuf,
|
||||
latest: Arc<AsyncMutex<LatestInvalidation>>,
|
||||
terminate_tx: oneshot::Sender<oneshot::Sender<()>>,
|
||||
_subscriber: FileWatcherSubscriber,
|
||||
_registration: WatchRegistration,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
struct LatestInvalidation {
|
||||
observed_at_unix_seconds: Option<i64>,
|
||||
changed_paths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl EpiphanyInvalidationManager {
|
||||
pub(crate) fn new() -> Self {
|
||||
match FileWatcher::new() {
|
||||
Ok(file_watcher) => Self {
|
||||
available: true,
|
||||
file_watcher: Arc::new(file_watcher),
|
||||
state: Arc::new(AsyncMutex::new(HashMap::new())),
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("epiphany invalidation manager falling back to noop core watcher: {err}");
|
||||
Self {
|
||||
available: false,
|
||||
file_watcher: Arc::new(FileWatcher::noop()),
|
||||
state: Arc::new(AsyncMutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn ensure_thread_watch(
|
||||
&self,
|
||||
thread_id: &str,
|
||||
workspace_root: &AbsolutePathBuf,
|
||||
) {
|
||||
if !self.available {
|
||||
return;
|
||||
}
|
||||
|
||||
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 (subscriber, rx) = self.file_watcher.add_subscriber();
|
||||
let registration = subscriber.register_paths(vec![WatchPath {
|
||||
path: workspace_root.to_path_buf(),
|
||||
recursive: true,
|
||||
}]);
|
||||
let latest = Arc::new(AsyncMutex::new(LatestInvalidation::default()));
|
||||
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,
|
||||
_subscriber: subscriber,
|
||||
_registration: registration,
|
||||
};
|
||||
self.state.lock().await.insert(thread_id.to_string(), entry);
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut rx = ThrottledWatchReceiver::new(rx, INVALIDATION_WATCH_DEBOUNCE);
|
||||
let mut terminate_rx = terminate_rx;
|
||||
let mut done_tx = None;
|
||||
|
||||
loop {
|
||||
let event = tokio::select! {
|
||||
biased;
|
||||
result = &mut terminate_rx => {
|
||||
done_tx = result.ok();
|
||||
break;
|
||||
}
|
||||
event = rx.recv() => match event {
|
||||
Some(event) => event,
|
||||
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 =
|
||||
normalize_changed_paths(event.paths, workspace_root_path.as_path());
|
||||
}
|
||||
|
||||
if let Some(done_tx) = done_tx {
|
||||
let _ = done_tx.send(());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) async fn snapshot(&self, thread_id: &str) -> EpiphanyInvalidationSnapshot {
|
||||
if !self.available {
|
||||
return EpiphanyInvalidationSnapshot {
|
||||
available: false,
|
||||
workspace_root: None,
|
||||
observed_at_unix_seconds: None,
|
||||
changed_paths: Vec::new(),
|
||||
};
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn remove_thread(&self, thread_id: &str) {
|
||||
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
|
||||
}
|
||||
1
vendor/codex/codex-rs/app-server/src/lib.rs
vendored
1
vendor/codex/codex-rs/app-server/src/lib.rs
vendored
|
|
@ -74,7 +74,6 @@ mod config_manager;
|
|||
mod config_manager_service;
|
||||
mod device_key_api;
|
||||
mod dynamic_tools;
|
||||
mod epiphany_invalidation;
|
||||
mod error_code;
|
||||
mod filters;
|
||||
mod fs_api;
|
||||
|
|
|
|||
Reference in a new issue