Rinomina: crate akode-*, cartelle akode-rs/ e akode-cli/, binario akode, ~/.akode, AKODE_* (A)
Riapplica la fase 5 di aKode (86e7c72e) sulla base ripulita:
- crate codex-* -> akode-*, codex-rs -> akode-rs, codex-cli -> akode-cli, binario e ausiliari akode;
- ~/.codex -> ~/.akode, CODEX_HOME -> AKODE_HOME, variabili CODEX_* -> AKODE_*, .codex/ -> .akode/ di progetto;
nessun fallback a ~/.codex o a CODEX_*;
- testi per l'utente "Codex" -> "aKode"; testi verso il modello e istruzioni di sistema invariati;
- snapshot insta rinominati (non ancora rigenerati).
I conflitti con la base ripulita sono stati risolti riapplicando la rinomina ai file senza le
funzioni di imeAI (script di mappa dei token per file).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
185
akode-rs/utils/git-discovery/src/git_root_discovery_tests.rs
Normal file
185
akode-rs/utils/git-discovery/src/git_root_discovery_tests.rs
Normal file
@@ -0,0 +1,185 @@
|
||||
//! Verifies shared probe results, capacity retention after cancellation, and
|
||||
//! fresh discovery after repository changes. Blocked probes must not delay runtime shutdown.
|
||||
|
||||
use super::GitRootDiscovery;
|
||||
use akode_utils_absolute_path::AbsolutePathBuf;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::mpsc;
|
||||
use std::time::Duration;
|
||||
use tokio::time::timeout;
|
||||
|
||||
const PROBE_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
/// Simulates a filesystem call that stays blocked until the test releases it.
|
||||
fn blocked_root_discovery(
|
||||
capacity: usize,
|
||||
) -> (Arc<GitRootDiscovery>, mpsc::Receiver<()>, mpsc::Sender<()>) {
|
||||
let (entered_tx, entered_rx) = mpsc::channel();
|
||||
let (release_tx, release_rx) = mpsc::channel();
|
||||
let release_rx = Mutex::new(release_rx);
|
||||
let discovery = Arc::new(GitRootDiscovery {
|
||||
capacity,
|
||||
find_root: Arc::new(move |cwd| {
|
||||
entered_tx.send(()).expect("signal probe entry");
|
||||
let _ = release_rx
|
||||
.lock()
|
||||
.expect("release receiver")
|
||||
.recv_timeout(PROBE_TIMEOUT);
|
||||
Some(cwd.to_path_buf())
|
||||
}),
|
||||
..Default::default()
|
||||
});
|
||||
(discovery, entered_rx, release_tx)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn root_discovery_shares_probes_across_callers_and_cancellation() {
|
||||
const TIMEOUT: Duration = Duration::from_secs(5);
|
||||
let workspace = tempfile::tempdir().expect("workspace");
|
||||
let first_cwd = AbsolutePathBuf::from_absolute_path(workspace.path().join("first"))
|
||||
.expect("absolute first cwd");
|
||||
let second_cwd = AbsolutePathBuf::from_absolute_path(workspace.path().join("second"))
|
||||
.expect("absolute second cwd");
|
||||
let third_cwd = AbsolutePathBuf::from_absolute_path(workspace.path().join("third"))
|
||||
.expect("absolute third cwd");
|
||||
let (discovery, entered_rx, release_tx) = blocked_root_discovery(/*capacity*/ 2);
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("runtime");
|
||||
|
||||
runtime.block_on(async {
|
||||
let mut first = Box::pin(discovery.discover(first_cwd.clone()));
|
||||
let mut duplicate = Box::pin(discovery.discover(first_cwd.clone()));
|
||||
let mut second = Box::pin(discovery.discover(second_cwd.clone()));
|
||||
assert!(futures::poll!(first.as_mut()).is_pending());
|
||||
assert!(futures::poll!(duplicate.as_mut()).is_pending());
|
||||
// Sharing the first cwd leaves room for a different directory.
|
||||
assert!(futures::poll!(second.as_mut()).is_pending());
|
||||
entered_rx
|
||||
.recv_timeout(TIMEOUT)
|
||||
.expect("first probe started");
|
||||
entered_rx
|
||||
.recv_timeout(TIMEOUT)
|
||||
.expect("second probe started");
|
||||
drop(first);
|
||||
drop(duplicate);
|
||||
// Cancelling every waiter does not free capacity while its worker is blocked.
|
||||
let mut third = Box::pin(discovery.discover(third_cwd.clone()));
|
||||
assert!(futures::poll!(third.as_mut()).is_pending());
|
||||
let mut cancelled = Box::pin(discovery.discover(first_cwd.join("cancelled")));
|
||||
assert!(futures::poll!(cancelled.as_mut()).is_pending());
|
||||
drop(cancelled);
|
||||
assert!(entered_rx.try_recv().is_err());
|
||||
let mut retry = Box::pin(discovery.discover(first_cwd.clone()));
|
||||
// The same directory can still join its probe when capacity is full.
|
||||
assert!(futures::poll!(retry.as_mut()).is_pending());
|
||||
let mut follower = Box::pin(discovery.discover(first_cwd.clone()));
|
||||
assert!(futures::poll!(follower.as_mut()).is_pending());
|
||||
|
||||
release_tx.send(()).expect("release first probe");
|
||||
release_tx.send(()).expect("release second probe");
|
||||
release_tx.send(()).expect("release queued probe");
|
||||
let roots = timeout(TIMEOUT, async {
|
||||
(retry.await, follower.await, second.await, third.await)
|
||||
})
|
||||
.await
|
||||
.expect("running and queued root probes finished");
|
||||
assert_eq!(
|
||||
roots,
|
||||
(
|
||||
Some(first_cwd.to_path_buf()),
|
||||
Some(first_cwd.to_path_buf()),
|
||||
Some(second_cwd.to_path_buf()),
|
||||
Some(third_cwd.to_path_buf()),
|
||||
)
|
||||
);
|
||||
entered_rx.try_recv().expect("queued probe started");
|
||||
assert!(entered_rx.try_recv().is_err());
|
||||
});
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn completed_root_discoveries_are_not_cached() {
|
||||
let workspace = tempfile::tempdir().expect("workspace");
|
||||
let cwd = AbsolutePathBuf::from_absolute_path(workspace.path()).expect("absolute cwd");
|
||||
let discovery = Arc::new(GitRootDiscovery {
|
||||
capacity: 1,
|
||||
..Default::default()
|
||||
});
|
||||
assert_eq!(discovery.discover(cwd.clone()).await, None);
|
||||
|
||||
std::fs::create_dir(cwd.join(".git")).expect("git directory");
|
||||
std::fs::write(cwd.join(".git/HEAD"), "ref: refs/heads/main\n").expect("git HEAD");
|
||||
assert_eq!(
|
||||
discovery.discover(cwd.clone()).await,
|
||||
Some(cwd.to_path_buf())
|
||||
);
|
||||
|
||||
std::fs::remove_dir_all(cwd.join(".git")).expect("remove Git metadata");
|
||||
assert_eq!(discovery.discover(cwd).await, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn saturated_discovery_does_not_block_other_instances() {
|
||||
let workspace = tempfile::tempdir().expect("workspace");
|
||||
let cwd = AbsolutePathBuf::from_absolute_path(workspace.path()).expect("absolute cwd");
|
||||
let (saturated, entered_rx, release_tx) = blocked_root_discovery(/*capacity*/ 1);
|
||||
let mut blocked = Box::pin(saturated.discover(cwd.clone()));
|
||||
assert!(futures::poll!(blocked.as_mut()).is_pending());
|
||||
entered_rx
|
||||
.recv_timeout(PROBE_TIMEOUT)
|
||||
.expect("probe started");
|
||||
|
||||
let available = Arc::new(GitRootDiscovery::default());
|
||||
let root = timeout(Duration::from_secs(5), available.discover(cwd.clone())).await;
|
||||
release_tx.send(()).expect("release probe");
|
||||
assert_eq!(root.expect("independent discovery finishes"), None);
|
||||
assert_eq!(blocked.await, Some(cwd.to_path_buf()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocked_root_discovery_does_not_delay_runtime_shutdown() {
|
||||
const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
const WORKER_THREADS: usize = 1;
|
||||
let workspace = tempfile::tempdir().expect("workspace");
|
||||
let cwd = AbsolutePathBuf::from_absolute_path(workspace.path()).expect("absolute cwd");
|
||||
let (discovery, entered_rx, release_tx) = blocked_root_discovery(/*capacity*/ 1);
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(WORKER_THREADS)
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("runtime");
|
||||
let lookup = runtime.spawn({
|
||||
let discovery = Arc::clone(&discovery);
|
||||
async move { discovery.discover(cwd).await }
|
||||
});
|
||||
entered_rx
|
||||
.recv_timeout(PROBE_TIMEOUT)
|
||||
.expect("probe started");
|
||||
lookup.abort();
|
||||
assert!(
|
||||
runtime
|
||||
.block_on(lookup)
|
||||
.expect_err("cancelled lookup")
|
||||
.is_cancelled()
|
||||
);
|
||||
|
||||
let (shutdown_tx, shutdown_rx) = mpsc::channel();
|
||||
let shutdown = std::thread::spawn(move || {
|
||||
drop(runtime);
|
||||
let _ = shutdown_tx.send(());
|
||||
});
|
||||
let shutdown_result = shutdown_rx.recv_timeout(SHUTDOWN_TIMEOUT);
|
||||
let outstanding = discovery
|
||||
.in_flight
|
||||
.lock()
|
||||
.expect("outstanding probes")
|
||||
.len();
|
||||
// Release the probe even if shutdown timed out so a regression cannot hang the test.
|
||||
release_tx.send(()).expect("release probe");
|
||||
shutdown.join().expect("shutdown thread");
|
||||
assert_eq!((shutdown_result, outstanding), (Ok(()), 1));
|
||||
}
|
||||
120
akode-rs/utils/git-discovery/src/lib.rs
Normal file
120
akode-rs/utils/git-discovery/src/lib.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
//! Bounded, shared Git-root discovery for optional metadata.
|
||||
//!
|
||||
//! Each instance bounds detached probes and shares one outstanding probe per
|
||||
//! supplied cwd. Workers retain their entries through caller cancellation and remove
|
||||
//! them when filesystem work ends; completed results are not cached. Capacity waits
|
||||
//! are cancellable; keys are compared without filesystem canonicalization.
|
||||
//! Probes never enter Tokio's blocking pool, so a stuck filesystem cannot keep its
|
||||
//! runtime shutdown waiting for optional metadata work.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use akode_git_utils::get_git_repo_root;
|
||||
use akode_utils_absolute_path::AbsolutePathBuf;
|
||||
use futures::FutureExt;
|
||||
use futures::future::BoxFuture;
|
||||
use futures::future::Shared;
|
||||
use tokio::sync::Notify;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
const MAX_CONCURRENT_ROOT_PROBES: usize = 8;
|
||||
|
||||
type SharedRootProbe = Shared<BoxFuture<'static, Option<PathBuf>>>;
|
||||
type RootFinder = dyn Fn(&Path) -> Option<PathBuf> + Send + Sync;
|
||||
|
||||
/// Shares outstanding metadata probes by cwd and bounds probes across directories.
|
||||
/// Entries survive caller cancellation but are not cached after filesystem work ends.
|
||||
pub struct GitRootDiscovery {
|
||||
capacity: usize,
|
||||
in_flight: Mutex<HashMap<AbsolutePathBuf, SharedRootProbe>>,
|
||||
find_root: Arc<RootFinder>,
|
||||
capacity_changed: Notify,
|
||||
}
|
||||
|
||||
impl Default for GitRootDiscovery {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
capacity: MAX_CONCURRENT_ROOT_PROBES,
|
||||
in_flight: Mutex::new(HashMap::new()),
|
||||
find_root: Arc::new(get_git_repo_root),
|
||||
capacity_changed: Notify::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GitRootDiscovery {
|
||||
/// Joins the cwd's existing probe, waiting for capacity to start a detached
|
||||
/// worker when necessary. Dropping the future cancels only the wait.
|
||||
pub async fn discover(self: &Arc<Self>, cwd: AbsolutePathBuf) -> Option<PathBuf> {
|
||||
let (result, new_probe) = loop {
|
||||
// notify_waiters reaches futures created before the capacity check,
|
||||
// even if they have not been polled yet.
|
||||
let capacity_changed = self.capacity_changed.notified();
|
||||
{
|
||||
let mut in_flight = self
|
||||
.in_flight
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if let Some(result) = in_flight.get(&cwd) {
|
||||
break (result.clone(), None);
|
||||
}
|
||||
if in_flight.len() < self.capacity {
|
||||
let (result_tx, result_rx) = oneshot::channel();
|
||||
let result = async move { result_rx.await.unwrap_or_default() }
|
||||
.boxed()
|
||||
.shared();
|
||||
in_flight.insert(cwd.clone(), result.clone());
|
||||
let probe = ProbeGuard {
|
||||
cwd,
|
||||
discovery: Arc::clone(self),
|
||||
};
|
||||
break (result, Some((probe, result_tx)));
|
||||
}
|
||||
}
|
||||
capacity_changed.await;
|
||||
};
|
||||
if let Some((probe, result_tx)) = new_probe {
|
||||
// Other waiters for this cwd can now join the new probe.
|
||||
self.capacity_changed.notify_waiters();
|
||||
// A failed spawn drops the closure and its guard, so do not hold the map lock.
|
||||
// Dropping the handle detaches the thread; Tokio never owns or joins it.
|
||||
let worker = std::thread::Builder::new()
|
||||
.name("codex-git-root".to_string())
|
||||
.spawn(move || {
|
||||
let root = (probe.discovery.find_root)(probe.cwd.as_path());
|
||||
drop(probe);
|
||||
let _ = result_tx.send(root);
|
||||
});
|
||||
if let Err(error) = worker {
|
||||
tracing::warn!(%error, "failed to start optional Git root probe");
|
||||
}
|
||||
}
|
||||
result.await
|
||||
}
|
||||
}
|
||||
|
||||
/// Keeps an outstanding entry owned by its blocking worker, including during
|
||||
/// cancellation or unwinding, rather than by any caller waiting for the result.
|
||||
struct ProbeGuard {
|
||||
cwd: AbsolutePathBuf,
|
||||
discovery: Arc<GitRootDiscovery>,
|
||||
}
|
||||
|
||||
impl Drop for ProbeGuard {
|
||||
fn drop(&mut self) {
|
||||
self.discovery
|
||||
.in_flight
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.remove(&self.cwd);
|
||||
self.discovery.capacity_changed.notify_waiters();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "git_root_discovery_tests.rs"]
|
||||
mod tests;
|
||||
Reference in New Issue
Block a user