From cc356fd384b9d3c46350758dacd37774b8f6a088 Mon Sep 17 00:00:00 2001 From: ningyi-oai Date: Tue, 17 Nov 2026 01:18:26 +0000 Subject: [PATCH] Preserve partial Git metadata when worktree status is slow (#61939) ## Why Memory requests have a one-second Git metadata deadline. Waiting for all Git lookups together discards an already observed HEAD or remote URL when the worktree scan exceeds that deadline. Shared status scans can also describe a different HEAD from the independent lookup. ## What changed - Publish HEAD, verified remote URLs, and worktree status incrementally, retaining available metadata at the memory request deadline. - Read HEAD and change status together using Git porcelain v2, and reject status observations that conflict with an independently observed HEAD. - Run a positive-only change probe over up to four recently modified root directories during memory extraction preparation. Use a completed probe only when full status is unknown and its HEAD matches the request metadata, without waiting for it before sending the request. ## Testing Add coverage for partial metadata at the deadline, delayed lookups, stale and unborn HEAD observations, positive-only probes, shared scans, and porcelain parsing. Extend remote credential redaction coverage to partial clones. GitOrigin-RevId: e7b7c250bfc0e23fb8962fcd5cd5febd7c69e2bd --- codex-rs/core/src/responses_metadata.rs | 25 ++ codex-rs/core/src/responses_metadata_tests.rs | 28 ++ codex-rs/core/src/turn_metadata.rs | 161 +++++--- codex-rs/core/src/turn_metadata_tests.rs | 352 ++++++++++++++++++ codex-rs/core/tests/suite/git_enrichment.rs | 23 +- codex-rs/git-utils/src/info.rs | 41 +- codex-rs/git-utils/src/lib.rs | 5 + codex-rs/git-utils/src/status.rs | 174 ++++++++- codex-rs/git-utils/src/status_tests.rs | 285 +++++++++++--- codex-rs/memories/write/src/phase1.rs | 7 +- codex-rs/memories/write/src/runtime.rs | 9 +- codex-rs/memories/write/src/startup_tests.rs | 1 + 12 files changed, 960 insertions(+), 151 deletions(-) diff --git a/codex-rs/core/src/responses_metadata.rs b/codex-rs/core/src/responses_metadata.rs index fd83de8aa5..aa2b873c29 100644 --- a/codex-rs/core/src/responses_metadata.rs +++ b/codex-rs/core/src/responses_metadata.rs @@ -284,6 +284,31 @@ pub struct CodexResponsesMetadata { } impl CodexResponsesMetadata { + /// Adds a Git-confirmed positive observation to an otherwise unknown Memory status. + /// A completed full scan wins, and an independently observed HEAD must match. + pub fn apply_memory_git_change_observation( + &mut self, + repo_root: &std::path::Path, + observed: codex_git_utils::GitWorktreeStatus, + ) { + if !matches!(self.request_kind, Some(CodexResponsesRequestKind::Memory)) + || !observed.has_changes + { + return; + } + let Some(workspace) = self + .workspaces + .get_mut(repo_root.to_string_lossy().as_ref()) + else { + return; + }; + if workspace.has_changes.is_none() + && let Some(head) = observed.head_commit_hash + && workspace.latest_git_commit_hash.as_deref() == Some(head.0.as_str()) + { + workspace.has_changes = Some(true); + } + } pub(crate) fn new( installation_id: String, session_id: String, diff --git a/codex-rs/core/src/responses_metadata_tests.rs b/codex-rs/core/src/responses_metadata_tests.rs index 654a8e7da3..7c8a900ad0 100644 --- a/codex-rs/core/src/responses_metadata_tests.rs +++ b/codex-rs/core/src/responses_metadata_tests.rs @@ -27,6 +27,34 @@ fn metadata_with_remote(url: &str) -> CodexResponsesMetadata { metadata } +#[test] +fn memory_positive_status_requires_matching_head_and_unknown_full_status() { + let root = std::path::Path::new("/workspace/東京"); + let mut metadata = metadata_with_remote("https://example.invalid/repo"); + metadata.request_kind = Some(CodexResponsesRequestKind::Memory); + let observation = |head: &str| codex_git_utils::GitWorktreeStatus { + head_commit_hash: Some(codex_git_utils::GitSha::new(head)), + has_changes: true, + }; + metadata.apply_memory_git_change_observation(root, observation("abc123")); + assert_eq!( + metadata.workspaces["/workspace/東京"].has_changes, + Some(false) + ); + metadata + .workspaces + .get_mut("/workspace/東京") + .unwrap() + .has_changes = None; + metadata.apply_memory_git_change_observation(root, observation("other")); + assert_eq!(metadata.workspaces["/workspace/東京"].has_changes, None); + metadata.apply_memory_git_change_observation(root, observation("abc123")); + assert_eq!( + metadata.workspaces["/workspace/東京"].has_changes, + Some(true) + ); +} + #[test] fn turn_metadata_keeps_the_exact_boundary_and_omits_an_overlong_remote() { let prefix = "https://example.invalid/"; diff --git a/codex-rs/core/src/turn_metadata.rs b/codex-rs/core/src/turn_metadata.rs index 70e76f8713..245d56cac0 100644 --- a/codex-rs/core/src/turn_metadata.rs +++ b/codex-rs/core/src/turn_metadata.rs @@ -9,6 +9,8 @@ use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; use serde_json::Value; +#[cfg(test)] +use tokio::sync::oneshot; use tokio::sync::watch; use tokio_util::task::AbortOnDropHandle; @@ -26,10 +28,12 @@ use crate::sandbox_tags::SandboxTags; use crate::sandbox_tags::record_policy_metadata; use crate::session::step_settings::ResolvedStepSettings; use codex_file_system::WindowsSandboxSelection; +use codex_git_utils::GitHeadState; +use codex_git_utils::GitWorktreeStatus; use codex_git_utils::SanitizedGitUrl; -use codex_git_utils::get_git_remote_urls_assume_git_repo; -use codex_git_utils::get_has_changes_in_repo; -use codex_git_utils::get_head_commit_hash; +use codex_git_utils::get_head_state; +use codex_git_utils::get_worktree_status_in_repo; +use codex_git_utils::observe_git_remote_urls; use codex_protocol::AgentPath; use codex_protocol::ThreadId; use codex_protocol::models::PermissionProfile; @@ -40,6 +44,8 @@ use codex_protocol::protocol::ThreadSource; use codex_protocol::turn_input::ResponsesApiClientMetadataUpdate; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_git_discovery::GitRootDiscovery; +use futures::FutureExt; +use futures::future::FusedFuture; // Memory requests await this optional metadata before sending their model request. const MEMORY_GIT_METADATA_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(1); @@ -98,31 +104,6 @@ impl<'a> ExecutionMetadata<'a> { } } -#[derive(Clone, Debug, Default)] -struct WorkspaceGitMetadata { - associated_remote_urls: Option>, - latest_git_commit_hash: Option, - has_changes: Option, -} - -impl WorkspaceGitMetadata { - fn is_empty(&self) -> bool { - self.associated_remote_urls.is_none() - && self.latest_git_commit_hash.is_none() - && self.has_changes.is_none() - } -} - -impl From for TurnMetadataWorkspace { - fn from(value: WorkspaceGitMetadata) -> Self { - Self { - associated_remote_urls: value.associated_remote_urls, - latest_git_commit_hash: value.latest_git_commit_hash, - has_changes: value.has_changes, - } - } -} - #[allow(clippy::too_many_arguments)] pub async fn detached_memory_responses_metadata( thread_manager: &crate::ThreadManager, @@ -585,42 +566,110 @@ impl TurnMetadataState { } } -async fn memory_workspaces( - cwd: &AbsolutePathBuf, - repo_root: impl Future>, -) -> BTreeMap { - tokio::time::timeout(MEMORY_GIT_METADATA_TIMEOUT, git_workspaces(cwd, repo_root)) - .await - .unwrap_or_default() -} - async fn git_workspaces( cwd: &AbsolutePathBuf, repo_root: impl Future>, ) -> BTreeMap { - let Some(repo_root) = repo_root.await else { - return BTreeMap::new(); - }; - let (head_commit_hash, associated_remote_urls, has_changes) = tokio::join!( - get_head_commit_hash(cwd), - get_git_remote_urls_assume_git_repo(cwd), - get_has_changes_in_repo(cwd, &repo_root), - ); - let workspace_git_metadata = WorkspaceGitMetadata { - associated_remote_urls, - latest_git_commit_hash: head_commit_hash.map(|sha| sha.0), - has_changes, - }; let mut workspaces = BTreeMap::new(); - if !workspace_git_metadata.is_empty() { - workspaces.insert( - repo_root.to_string_lossy().into_owned(), - workspace_git_metadata.into(), - ); - } + collect_git_workspaces(cwd, repo_root, |observed| workspaces = observed).await; workspaces } +async fn memory_workspaces( + cwd: &AbsolutePathBuf, + repo_root: impl Future>, +) -> BTreeMap { + let mut workspaces = BTreeMap::new(); + let collect = collect_git_workspaces(cwd, repo_root, |observed| workspaces = observed); + let _ = tokio::time::timeout(MEMORY_GIT_METADATA_TIMEOUT, collect).await; + workspaces +} + +async fn collect_git_workspaces( + cwd: &AbsolutePathBuf, + repo_root: impl Future>, + mut publish: impl FnMut(BTreeMap), +) { + let Some(repo_root) = repo_root.await else { + return; + }; + collect_workspace_git_metadata( + get_head_state(cwd), + |updates| async move { observe_git_remote_urls(cwd, &updates).await }, + get_worktree_status_in_repo(cwd, &repo_root), + |workspace| { + publish(BTreeMap::from([( + repo_root.to_string_lossy().into_owned(), + workspace, + )])); + }, + ) + .await; +} + +async fn collect_workspace_git_metadata>( + head: impl Future, + collect_remotes: impl FnOnce(watch::Sender>) -> R, + status: impl Future>, + mut publish: impl FnMut(TurnMetadataWorkspace), +) { + let (updates, mut remotes) = watch::channel(BTreeMap::new()); + let observe = async { + let mut head = std::pin::pin!(head.fuse()); + let mut status = std::pin::pin!(status.fuse()); + let mut head_state = GitHeadState::Unavailable; + let mut observed_status = None; + let mut latest_git_commit_hash = None; + let mut has_changes = None; + let mut observed_remote_urls = false; + loop { + let mut changed = tokio::select! { + result = &mut head, if !head.is_terminated() => { + head_state = result; + if let GitHeadState::Commit(sha) = &head_state { + latest_git_commit_hash = Some(sha.0.clone()); + } + latest_git_commit_hash.is_some() + } + Ok(()) = remotes.changed() => { + observed_remote_urls = true; + true + } + result = &mut status, if !status.is_terminated() => { + observed_status = result; + false + } + else => break, + }; + // A shared scan can be stale. Use its HEAD and status together when + // the independent lookup failed, but reject a conflicting known HEAD. + if head.is_terminated() + && let Some(observed) = observed_status.take() + { + let matches_head = match &head_state { + GitHeadState::Commit(sha) => observed.head_commit_hash.as_ref() == Some(sha), + GitHeadState::Missing => observed.head_commit_hash.is_none(), + GitHeadState::Unavailable => true, + }; + if matches_head { + latest_git_commit_hash = observed.head_commit_hash.map(|sha| sha.0); + has_changes = Some(observed.has_changes); + changed = true; + } + } + if changed { + publish(TurnMetadataWorkspace { + associated_remote_urls: observed_remote_urls + .then(|| remotes.borrow_and_update().clone()), + latest_git_commit_hash: latest_git_commit_hash.clone(), + has_changes, + }); + } + } + }; + tokio::join!(collect_remotes(updates), observe); +} + #[cfg(test)] #[path = "turn_metadata_tests.rs"] mod tests; diff --git a/codex-rs/core/src/turn_metadata_tests.rs b/codex-rs/core/src/turn_metadata_tests.rs index 2d8aad6e15..1427d46a32 100644 --- a/codex-rs/core/src/turn_metadata_tests.rs +++ b/codex-rs/core/src/turn_metadata_tests.rs @@ -28,6 +28,8 @@ use codex_analytics::CompactionPhase; use codex_analytics::CompactionReason; use codex_analytics::CompactionTrigger; use codex_analytics::TurnAnalyticsMetadata; +use codex_git_utils::GitSha; +use codex_git_utils::get_head_commit_hash; use codex_models_manager::model_info::model_info_from_slug; use codex_protocol::AgentPath; use codex_protocol::config_types::WindowsSandboxLevel; @@ -268,6 +270,105 @@ async fn memory_workspaces_times_out_pending_git_discovery() { assert!(workspaces.is_empty()); } +#[cfg(unix)] +#[tokio::test] +async fn memory_workspaces_keep_observed_refs_when_status_exceeds_the_deadline() { + let (_temp_dir, repo) = create_clean_git_repo("repo").await; + let head = get_head_commit_hash(&repo).await.expect("read HEAD"); + let url = "https://example.invalid/repo.git"; + assert!( + Command::new("git") + .args(["remote", "add", "origin", url]) + .current_dir(&repo) + .status() + .await + .expect("add remote") + .success() + ); + + // Only status reads the index; block it without delaying HEAD or remote discovery. + let index = repo.join(".git/index"); + std::fs::remove_file(&index).expect("remove index"); + assert!( + Command::new("mkfifo") + .arg(index.as_path()) + .status() + .await + .expect("create index FIFO") + .success() + ); + + let started = tokio::time::Instant::now(); + let workspaces = memory_workspaces( + &repo, + std::future::ready(Some(repo.as_path().to_path_buf())), + ) + .await; + assert!(started.elapsed() >= MEMORY_GIT_METADATA_TIMEOUT); + assert_eq!( + serde_json::to_value(workspaces).expect("serialize workspaces"), + serde_json::json!({ + (repo.to_string_lossy().into_owned()): { + "associated_remote_urls": {"origin": url}, + "latest_git_commit_hash": head.0, + } + }), + ); +} + +#[test_case::test_case(true, false; "pending remotes")] +#[test_case::test_case(false, true; "pending HEAD")] +#[tokio::test(start_paused = true)] +async fn git_collection_keeps_independently_observed_refs_until_the_deadline( + head_ready: bool, + remotes_ready: bool, +) { + let head = "0123456789abcdef0123456789abcdef01234567"; + let url = "https://example.invalid/repo.git"; + let (send_head, head_probe) = tokio::sync::oneshot::channel(); + let (send_remotes, remotes_probe) = tokio::sync::oneshot::channel(); + let (send_status, status_probe) = tokio::sync::oneshot::channel(); + if head_ready { + send_head.send(GitSha::new(head)).expect("send HEAD"); + } + if remotes_ready { + let remotes = + BTreeMap::from([("origin".to_string(), url.try_into().expect("valid remote"))]); + send_remotes.send(remotes).expect("send remotes"); + } + + let start = tokio::time::Instant::now(); + let mut latest = None; + let collect = collect_workspace_git_metadata( + async { GitHeadState::Commit(head_probe.await.expect("HEAD probe")) }, + |updates| async move { + if let Ok(urls) = remotes_probe.await { + updates.send_replace(urls); + } + }, + async { status_probe.await.ok() }, + |workspace| latest = Some(workspace), + ); + let _ = tokio::time::timeout(MEMORY_GIT_METADATA_TIMEOUT, collect).await; + + assert_eq!(start.elapsed(), MEMORY_GIT_METADATA_TIMEOUT); + assert!(send_status.is_closed()); + let workspace = latest.expect("workspace before deadline"); + assert_eq!( + workspace.latest_git_commit_hash.as_deref(), + head_ready.then_some(head) + ); + assert_eq!( + workspace + .associated_remote_urls + .as_ref() + .and_then(|urls| urls.get("origin")) + .map(SanitizedGitUrl::as_str), + remotes_ready.then_some(url), + ); + assert_eq!(workspace.has_changes, None); +} + #[test] fn turn_metadata_state_includes_sandbox_metadata() { let temp_dir = TempDir::new().expect("temp dir"); @@ -1249,6 +1350,257 @@ fn responses_api_metadata_accepts_previously_valid_reserved_keys() { } } +#[tokio::test] +async fn git_enrichment_publishes_refs_before_status_is_available() { + let (_temp_dir, repo) = create_clean_git_repo("repo").await; + let url = "https://example.invalid/repo.git"; + assert!( + Command::new("git") + .args(["remote", "add", "origin", url]) + .current_dir(&repo) + .status() + .await + .expect("add remote") + .success() + ); + let head = get_head_commit_hash(&repo).await.expect("read HEAD"); + let refs = serde_json::json!({ + "associated_remote_urls": {"origin": url}, + "latest_git_commit_hash": head.0, + }); + let mut clean = refs.clone(); + clean["has_changes"] = Value::Bool(false); + + let updates = + collect_with_status_after_refs(&repo, get_worktree_status_in_repo(&repo, &repo)).await; + assert!( + updates + .first() + .expect("early refs") + .get("has_changes") + .is_none() + ); + assert_eq!(updates.last(), Some(&clean)); + assert_eq!( + collect_with_status_after_refs(&repo, std::future::ready(None)) + .await + .last(), + Some(&refs), + ); +} + +#[tokio::test] +async fn git_enrichment_keeps_verified_remotes_while_ambiguous_lookups_are_pending() { + let urls = BTreeMap::from([( + "origin".to_string(), + SanitizedGitUrl::try_from("https://example.invalid/repo").unwrap(), + )]); + let initial = urls.clone(); + let mut all_urls = urls.clone(); + all_urls.insert( + "upstream".to_string(), + SanitizedGitUrl::try_from("https://example.invalid/upstream").unwrap(), + ); + let final_update = all_urls.clone(); + let (resume_lookup, wait_for_publication) = oneshot::channel(); + let mut resume_lookup = Some(resume_lookup); + let mut observed = Vec::new(); + let collect = collect_workspace_git_metadata( + std::future::ready(GitHeadState::Unavailable), + |verified| async move { + verified.send_replace(initial); + wait_for_publication.await.unwrap(); + verified.send_replace(final_update); + }, + std::future::ready(None), + |workspace| { + observed.push(workspace.associated_remote_urls); + if let Some(ack) = resume_lookup.take() { + ack.send(()).unwrap(); + } + }, + ); + tokio::time::timeout(Duration::from_secs(5), collect) + .await + .unwrap(); + assert_eq!(observed, vec![Some(urls), Some(all_urls)]); +} + +#[tokio::test] +async fn git_enrichment_publishes_verified_status_while_remotes_are_pending() { + let (_temp_dir, repo) = create_clean_git_repo("repo").await; + let head = get_head_commit_hash(&repo).await.expect("read HEAD"); + let (release_remotes, wait_for_status) = tokio::sync::oneshot::channel(); + let mut release_remotes = Some(release_remotes); + let collect = collect_workspace_git_metadata( + get_head_state(&repo), + |updates| { + let repo = &repo; + async move { + wait_for_status.await.expect("verified worktree status"); + observe_git_remote_urls(repo, &updates).await; + } + }, + get_worktree_status_in_repo(&repo, &repo), + |workspace| { + if let Some(dirty) = workspace.has_changes + && let Some(release) = release_remotes.take() + { + assert_eq!(workspace.latest_git_commit_hash.as_ref(), Some(&head.0)); + assert_eq!(workspace.associated_remote_urls, None); + assert!(!dirty); + release.send(()).expect("release remote lookup"); + } + }, + ); + tokio::time::timeout(Duration::from_secs(5), collect) + .await + .expect("verified status should be published before remotes return"); +} + +#[test_case::test_case(true; "confirmed unborn HEAD")] +#[test_case::test_case(false; "HEAD still pending")] +#[tokio::test(start_paused = true)] +async fn git_enrichment_requires_confirmed_head_for_unborn_status(head_ready: bool) { + let mut published_dirty = None; + let collect = collect_workspace_git_metadata( + async { + if !head_ready { + std::future::pending::<()>().await; + } + GitHeadState::Missing + }, + |_| std::future::ready(()), + std::future::ready(Some(GitWorktreeStatus { + head_commit_hash: None, + has_changes: true, + })), + |workspace| published_dirty = workspace.has_changes, + ); + let _ = tokio::time::timeout(MEMORY_GIT_METADATA_TIMEOUT, collect).await; + assert_eq!(published_dirty, head_ready.then_some(true)); +} + +#[tokio::test] +async fn git_enrichment_keeps_dirty_status_when_an_unborn_repo_contains_a_head_file() { + let temp_dir = TempDir::new().expect("temp dir"); + let repo = temp_dir.path().abs(); + assert!( + Command::new("git") + .args(["init", "-q"]) + .current_dir(&repo) + .status() + .await + .expect("git init") + .success() + ); + std::fs::write(repo.join("HEAD"), "untracked").expect("write HEAD file"); + let workspaces = git_workspaces( + &repo, + std::future::ready(Some(repo.as_path().to_path_buf())), + ) + .await; + let workspace = workspaces + .get(repo.to_string_lossy().as_ref()) + .expect("workspace"); + assert_eq!(workspace.latest_git_commit_hash, None); + assert_eq!(workspace.has_changes, Some(true)); +} + +#[tokio::test] +async fn git_enrichment_recovers_commit_and_status_when_independent_head_lookup_fails() { + let (_temp_dir, repo) = create_clean_git_repo("repo").await; + let head = get_head_commit_hash(&repo).await.expect("read HEAD"); + std::fs::write(repo.join("untracked"), "dirty").expect("write untracked file"); + let mut latest = None; + collect_workspace_git_metadata( + std::future::ready(GitHeadState::Unavailable), + |_| std::future::ready(()), + get_worktree_status_in_repo(&repo, &repo), + |workspace| latest = Some(workspace), + ) + .await; + let workspace = latest.expect("workspace"); + assert_eq!( + workspace.latest_git_commit_hash.as_deref(), + Some(head.0.as_str()) + ); + assert_eq!(workspace.has_changes, Some(true)); +} + +#[tokio::test] +async fn git_enrichment_rejects_a_committed_status_when_head_is_missing() { + let head = GitSha::new("0123456789abcdef0123456789abcdef01234567"); + let mut latest = None; + collect_workspace_git_metadata( + std::future::ready(GitHeadState::Missing), + |_| std::future::ready(()), + std::future::ready(Some(GitWorktreeStatus { + head_commit_hash: Some(head), + has_changes: false, + })), + |workspace| latest = Some(workspace), + ) + .await; + assert!(latest.is_none()); +} + +#[tokio::test] +async fn git_enrichment_does_not_mark_an_old_head_clean() { + let (_temp_dir, repo) = create_clean_git_repo("repo").await; + let head = get_head_commit_hash(&repo) + .await + .expect("read original HEAD"); + let status = async { + assert!( + Command::new("git") + .args(["commit", "--allow-empty", "-qm", "new HEAD"]) + .current_dir(&repo) + .status() + .await + .expect("commit") + .success() + ); + let status = get_worktree_status_in_repo(&repo, &repo) + .await + .expect("read clean status for the new HEAD"); + assert!(!status.has_changes); + Some(status) + }; + assert_eq!( + collect_with_status_after_refs(&repo, status).await, + vec![serde_json::json!({"latest_git_commit_hash": head.0})], + ); +} + +async fn collect_with_status_after_refs( + repo: &AbsolutePathBuf, + status: impl Future>, +) -> Vec { + let (send_refs_ready, refs_ready) = tokio::sync::oneshot::channel(); + let mut send_refs_ready = Some(send_refs_ready); + let mut updates = Vec::new(); + let status = async { + refs_ready.await.expect("refs published"); + status.await + }; + let collect = collect_workspace_git_metadata( + get_head_state(repo), + |updates| async move { observe_git_remote_urls(repo, &updates).await }, + status, + |workspace| { + updates.push(serde_json::to_value(workspace).expect("serialize workspace")); + if let Some(ready) = send_refs_ready.take() { + ready.send(()).expect("release status scan"); + } + }, + ); + tokio::time::timeout(Duration::from_secs(5), collect) + .await + .expect("refs should be published while status is pending"); + updates +} + #[tokio::test] async fn turn_metadata_state_preserves_subagent_parent_after_git_enrichment() { let (_temp_dir, repo_path) = create_clean_git_repo("repo").await; diff --git a/codex-rs/core/tests/suite/git_enrichment.rs b/codex-rs/core/tests/suite/git_enrichment.rs index bb06c8bda5..49668e8618 100644 --- a/codex-rs/core/tests/suite/git_enrichment.rs +++ b/codex-rs/core/tests/suite/git_enrichment.rs @@ -166,11 +166,30 @@ async fn startup_prewarm_skips_git_enrichment_and_user_turn_observes_fresh_state } /// Repository credentials must never cross the outbound model-request boundary. +#[test_case(false; "ordinary clone")] +#[test_case(true; "partial clone")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn user_turn_git_enrichment_redacts_remote_credentials() -> Result<()> { +async fn user_turn_git_enrichment_redacts_remote_credentials(partial_clone: bool) -> Result<()> { skip_if_no_network!(Ok(())); - let (repo, head) = create_git_repo()?; + let (source, head) = create_git_repo()?; + let repo = if partial_clone { + run_git(source.path(), &["config", "uploadpack.allowFilter", "true"])?; + let clone = TempDir::new()?; + run_git( + clone.path(), + &[ + "clone", + "--no-local", + "--filter=blob:none", + source.path().to_str().context("source repo path")?, + ".", + ], + )?; + clone + } else { + source + }; run_git( repo.path(), &[ diff --git a/codex-rs/git-utils/src/info.rs b/codex-rs/git-utils/src/info.rs index 875677449c..b710fe3d7f 100644 --- a/codex-rs/git-utils/src/info.rs +++ b/codex-rs/git-utils/src/info.rs @@ -5,6 +5,7 @@ pub use remotes::get_git_remote_urls_assume_git_repo; pub use remotes::observe_git_remote_urls; use std::collections::HashSet; +use std::ffi::OsStr; use std::path::Path; use std::path::PathBuf; @@ -129,18 +130,36 @@ pub async fn collect_git_info(cwd: &Path) -> Option { /// Return the current HEAD commit hash without checking whether `cwd` is in a git repo. pub async fn get_head_commit_hash(cwd: &Path) -> Option { - let output = run_git_command_with_timeout(&["rev-parse", "HEAD"], cwd).await?; - if !output.status.success() { - return None; + match get_head_state(cwd).await { + GitHeadState::Commit(hash) => Some(hash), + GitHeadState::Missing | GitHeadState::Unavailable => None, } +} - let stdout = String::from_utf8(output.stdout).ok()?; - let hash = stdout.trim(); - if hash.is_empty() { - None - } else { - Some(GitSha::new(hash)) +/// Distinguishes a missing HEAD reference from an unavailable Git lookup. +#[derive(Clone, Debug, PartialEq)] +pub enum GitHeadState { + Commit(GitSha), + Missing, + Unavailable, +} + +pub async fn get_head_state(cwd: &Path) -> GitHeadState { + let Some(output) = + run_git_command_with_timeout(&["rev-parse", "--verify", "--quiet", "HEAD"], cwd).await + else { + return GitHeadState::Unavailable; + }; + if output.status.code() == Some(1) { + return GitHeadState::Missing; } + if output.status.success() + && let Ok(stdout) = str::from_utf8(&output.stdout) + && !stdout.trim().is_empty() + { + return GitHeadState::Commit(GitSha::new(stdout.trim())); + } + GitHeadState::Unavailable } pub fn canonicalize_git_remote_url(url: &str) -> Option { @@ -363,7 +382,7 @@ pub(crate) async fn detect_local_fsmonitor_override( pub(crate) async fn run_git_command_with_timeout_from( git: &Path, - args: &[&str], + args: &[impl AsRef], cwd: &Path, fsmonitor: crate::FsmonitorOverride, ) -> Option { @@ -373,7 +392,7 @@ pub(crate) async fn run_git_command_with_timeout_from( pub(crate) fn local_git_command( git: &Path, - args: &[&str], + args: &[impl AsRef], cwd: &Path, fsmonitor: crate::FsmonitorOverride, ) -> Command { diff --git a/codex-rs/git-utils/src/lib.rs b/codex-rs/git-utils/src/lib.rs index 1beb1f7e5c..58df009426 100644 --- a/codex-rs/git-utils/src/lib.rs +++ b/codex-rs/git-utils/src/lib.rs @@ -36,6 +36,7 @@ pub use fsmonitor::FsmonitorProbeRunner; pub use fsmonitor::detect_fsmonitor_override; pub use info::CommitLogEntry; pub use info::GitDiffToRemote; +pub use info::GitHeadState; pub use info::GitInfo; pub use info::canonicalize_git_remote_url; pub use info::collect_git_info; @@ -46,13 +47,17 @@ pub use info::get_git_remote_urls; pub use info::get_git_remote_urls_assume_git_repo; pub use info::get_git_repo_root; pub use info::get_head_commit_hash; +pub use info::get_head_state; pub use info::git_diff_to_remote; pub use info::local_git_branches; pub use info::observe_git_remote_urls; pub use info::recent_commits; pub use operations::git_config_override_env; pub use platform::create_symlink; +pub use status::GitWorktreeStatus; pub use status::get_has_changes_in_repo; +pub use status::get_worktree_status_in_repo; +pub use status::probe_worktree_changes; pub use trust::resolve_root_git_project_for_trust; pub use trust::resolve_root_git_project_uri_for_trust; pub use worktree::RepositoryIdentity; diff --git a/codex-rs/git-utils/src/status.rs b/codex-rs/git-utils/src/status.rs index 849197630f..a642b34cea 100644 --- a/codex-rs/git-utils/src/status.rs +++ b/codex-rs/git-utils/src/status.rs @@ -1,4 +1,7 @@ +use std::cmp::Reverse; +use std::collections::BinaryHeap; use std::collections::HashMap; +use std::ffi::OsString; use std::future::Future; use std::path::Path; use std::path::PathBuf; @@ -9,58 +12,191 @@ use futures::FutureExt; use futures::future::BoxFuture; use futures::future::WeakShared; +use crate::GitSha; +use crate::get_git_repo_root; use crate::info::detect_local_fsmonitor_override; use crate::info::run_git_command_with_timeout_from; -#[derive(Clone, Debug, Eq, Hash, PartialEq)] -struct GitStatusKey { - git: PathBuf, - repo_root: PathBuf, +/// The HEAD and worktree changes observed by the same Git status command. +#[derive(Clone, Debug, PartialEq)] +pub struct GitWorktreeStatus { + pub head_commit_hash: Option, + pub has_changes: bool, } -type GitStatusFuture = BoxFuture<'static, Option>; +type GitStatusFuture = BoxFuture<'static, Option>; +type GitStatusRuns = Mutex>>; -fn git_status_runs() -> &'static Mutex>> { - static RUNS: OnceLock>>> = - OnceLock::new(); +const MAX_PROBE_DIRECTORIES: usize = 4; + +fn git_status_runs() -> &'static GitStatusRuns { + static RUNS: OnceLock = OnceLock::new(); + RUNS.get_or_init(|| Mutex::new(HashMap::new())) +} + +fn change_probe_runs() -> &'static GitStatusRuns { + static RUNS: OnceLock = OnceLock::new(); RUNS.get_or_init(|| Mutex::new(HashMap::new())) } /// Reports repository changes without inspecting uncommitted submodule contents. pub async fn get_has_changes_in_repo(cwd: &Path, repo_root: &Path) -> Option { + get_worktree_status_in_repo(cwd, repo_root) + .await + .map(|status| status.has_changes) +} + +/// Reads HEAD and worktree changes together, skipping uncommitted submodule contents. +pub async fn get_worktree_status_in_repo( + cwd: &Path, + repo_root: &Path, +) -> Option { let git = PathBuf::from("git"); let cwd = cwd.to_path_buf(); - let key = git_status_key(git.clone(), repo_root).await; + let key = canonical_worktree_root(repo_root).await; share_git_status_run(key, move || async move { let fsmonitor = detect_local_fsmonitor_override(&git, &cwd).await; // Submodule worktree scans can execute filters selected by task-writable // Git configuration. Skip those scans while retaining gitlink changes. let output = run_git_command_with_timeout_from( &git, - &["status", "--porcelain", "--ignore-submodules=dirty"], + &[ + "status", + "--porcelain=v2", + "--branch", + "--no-ahead-behind", + "--ignore-submodules=dirty", + ], &cwd, fsmonitor, ) .await?; - output.status.success().then_some(!output.stdout.is_empty()) + if !output.status.success() { + return None; + } + parse_worktree_status(&output.stdout) }) .await } -async fn git_status_key(git: PathBuf, repo_root: &Path) -> GitStatusKey { - let repo_root = tokio::fs::canonicalize(repo_root) - .await - .unwrap_or_else(|_| repo_root.to_path_buf()); - GitStatusKey { git, repo_root } +/// Returns a positive change observation from a few recently modified root +/// directories. A clean, failed, or incomplete probe cannot establish that the +/// complete worktree is clean and returns `None`. +pub async fn probe_worktree_changes(cwd: &Path) -> Option<(PathBuf, GitWorktreeStatus)> { + let root = get_git_repo_root(cwd)?; + let probe_root = root.clone(); + let cwd = cwd.to_path_buf(); + // Relative Git environment paths can resolve differently in sibling cwds. + let status = share_git_run(change_probe_runs(), cwd.clone(), move || async move { + run_change_probe(&cwd, &probe_root).await + }) + .await?; + Some((root, status)) } -async fn share_git_status_run(key: GitStatusKey, run: F) -> Option +async fn run_change_probe(cwd: &Path, root: &Path) -> Option { + let mut entries = tokio::fs::read_dir(root).await.ok()?; + let mut recent = BinaryHeap::new(); + while let Some(entry) = entries.next_entry().await.ok()? { + if entry.file_name() == ".git" || !entry.file_type().await.is_ok_and(|kind| kind.is_dir()) { + continue; + } + if let Ok(metadata) = entry.metadata().await + && let Ok(modified) = metadata.modified() + { + recent.push(Reverse((modified, entry.file_name()))); + if recent.len() > MAX_PROBE_DIRECTORIES { + recent.pop(); + } + } + } + if recent.is_empty() { + return None; + } + + let mut args: Vec = [ + "status", + "--porcelain=v2", + "--branch", + "--no-ahead-behind", + "--ignore-submodules=dirty", + "--", + ] + .into_iter() + .map(OsString::from) + .collect(); + args.extend(recent.into_iter().map(|Reverse((_, name))| { + let mut pathspec = OsString::from(":(top,literal)"); + pathspec.push(name); + pathspec + })); + + let git = Path::new("git"); + let fsmonitor = detect_local_fsmonitor_override(git, cwd).await; + let output = run_git_command_with_timeout_from(git, &args, cwd, fsmonitor).await?; + if !output.status.success() { + return None; + } + parse_worktree_status(&output.stdout).filter(|status| status.has_changes) +} + +fn parse_worktree_status(output: &[u8]) -> Option { + let mut oid = None; + let mut has_changes = false; + // Without -z, Git quotes newlines in paths, including both paths in a rename. + for line in output.strip_suffix(b"\n")?.split(|&byte| byte == b'\n') { + let line = line.strip_suffix(b"\r").unwrap_or(line); + if let Some(value) = line.strip_prefix(b"# branch.oid ") { + if oid.replace(value).is_some() { + return None; + } + } else { + // Porcelain v2: # header; 1 ordinary, 2 rename/copy, u unmerged, ? untracked. + match line { + [b'#', b' ', ..] => {} + [b'1' | b'2' | b'u' | b'?', b' ', ..] => has_changes = true, + _ => return None, + } + } + } + let head_commit_hash = match oid? { + b"(initial)" => None, + hash if matches!(hash.len(), 40 | 64) && hash.iter().all(u8::is_ascii_hexdigit) => { + Some(GitSha::new(str::from_utf8(hash).ok()?)) + } + _ => return None, + }; + Some(GitWorktreeStatus { + head_commit_hash, + has_changes, + }) +} + +async fn canonical_worktree_root(repo_root: &Path) -> PathBuf { + tokio::fs::canonicalize(repo_root) + .await + .unwrap_or_else(|_| repo_root.to_path_buf()) +} + +async fn share_git_status_run(key: PathBuf, run: F) -> Option where F: FnOnce() -> Fut, - Fut: Future> + Send + 'static, + Fut: Future> + Send + 'static, +{ + share_git_run(git_status_runs(), key, run).await +} + +async fn share_git_run( + runs: &'static GitStatusRuns, + key: PathBuf, + run: F, +) -> Option +where + F: FnOnce() -> Fut, + Fut: Future> + Send + 'static, { let result = { - let mut runs = git_status_runs() + let mut runs = runs .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); if let Some(result) = runs diff --git a/codex-rs/git-utils/src/status_tests.rs b/codex-rs/git-utils/src/status_tests.rs index 0336e27e95..852296cb5a 100644 --- a/codex-rs/git-utils/src/status_tests.rs +++ b/codex-rs/git-utils/src/status_tests.rs @@ -1,4 +1,3 @@ -use std::path::PathBuf; use std::sync::Arc; use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; @@ -9,11 +8,51 @@ use futures::future::join_all; use pretty_assertions::assert_eq; use tempfile::TempDir; -use super::GitStatusKey; +use super::GitWorktreeStatus; +use super::canonical_worktree_root; +use super::change_probe_runs; use super::get_has_changes_in_repo; -use super::git_status_key; +use super::get_worktree_status_in_repo; use super::git_status_runs; +use super::parse_worktree_status; +use super::probe_worktree_changes; +use super::share_git_run; use super::share_git_status_run; +use crate::GitSha; + +const CLEAN_STATUS: Option = Some(GitWorktreeStatus { + head_commit_hash: None, + has_changes: false, +}); +const DIRTY_STATUS: Option = Some(GitWorktreeStatus { + head_commit_hash: None, + has_changes: true, +}); + +#[tokio::test] +async fn head_probe_distinguishes_missing_commit_and_unavailable_repository() { + let repository = TempDir::new().expect("create repository"); + assert_eq!( + crate::get_head_state(repository.path()).await, + crate::GitHeadState::Unavailable + ); + gix::init(repository.path()).expect("initialize repository"); + std::fs::write(repository.path().join("HEAD"), "untracked").expect("write HEAD file"); + assert_eq!( + crate::get_head_state(repository.path()).await, + crate::GitHeadState::Missing + ); + crate::ensure_git_baseline_repository(repository.path()) + .await + .expect("create commit"); + let hash = crate::get_head_commit_hash(repository.path()) + .await + .expect("read commit"); + assert_eq!( + crate::get_head_state(repository.path()).await, + crate::GitHeadState::Commit(hash) + ); +} #[tokio::test] async fn metadata_status_does_not_execute_submodule_clean_filter() { @@ -126,11 +165,156 @@ async fn status_requests_from_sibling_directories_use_the_known_worktree_root() std::fs::create_dir(&second_directory).expect("create second directory"); let results = tokio::join!( - get_has_changes_in_repo(&first_directory, repository.path()), + get_worktree_status_in_repo(&first_directory, repository.path()), get_has_changes_in_repo(&second_directory, repository.path()), ); - assert_eq!(results, (Some(false), Some(false))); + assert_eq!(results, (CLEAN_STATUS, Some(false))); +} + +#[tokio::test] +async fn status_reports_the_commit_for_clean_and_dirty_worktrees() { + let repository = TempDir::new().expect("create repository"); + let root = repository.path(); + crate::ensure_git_baseline_repository(root) + .await + .expect("create initial commit"); + let head = crate::get_head_commit_hash(root).await.expect("read HEAD"); + let mut expected = GitWorktreeStatus { + head_commit_hash: Some(head), + has_changes: false, + }; + assert_eq!( + get_worktree_status_in_repo(root, root).await, + Some(expected.clone()) + ); + + std::fs::write(root.join("untracked"), "worktree change").expect("create untracked file"); + expected.has_changes = true; + assert_eq!( + get_worktree_status_in_repo(root, root).await, + Some(expected) + ); + + assert!( + std::process::Command::new("git") + .args(["config", "status.renames", "invalid"]) + .current_dir(root) + .status() + .expect("configure invalid status setting") + .success() + ); + assert_eq!(get_worktree_status_in_repo(root, root).await, None); +} + +#[tokio::test] +async fn change_probe_only_returns_git_verified_positive_status() { + let repository = TempDir::new().expect("create repository"); + let root = repository.path(); + crate::ensure_git_baseline_repository(root) + .await + .expect("create initial commit"); + let head = crate::get_head_commit_hash(root).await.expect("read HEAD"); + assert_eq!(probe_worktree_changes(root).await, None); + + // The pathspec must treat names containing Git pathspec syntax literally. + let candidate = root.join("[recent]"); + std::fs::create_dir(&candidate).expect("create candidate directory"); + std::fs::write(candidate.join("leaf"), "new file").expect("create untracked leaf"); + assert_eq!( + probe_worktree_changes(root).await, + Some(( + root.to_path_buf(), + GitWorktreeStatus { + head_commit_hash: Some(head), + has_changes: true, + } + )) + ); + + assert!( + std::process::Command::new("git") + .args(["config", "status.showUntrackedFiles", "no"]) + .current_dir(root) + .status() + .expect("configure hidden untracked files") + .success() + ); + assert_eq!(probe_worktree_changes(root).await, None); +} + +#[tokio::test] +async fn concurrent_change_probes_share_one_scan() { + let repository = TempDir::new().expect("create repository"); + let key = repository.path().to_path_buf(); + let scans = AtomicUsize::new(0); + let (release, gate) = tokio::sync::oneshot::channel(); + let first = share_git_run(change_probe_runs(), key.clone(), || async move { + gate.await.ok()?; + DIRTY_STATUS + }); + futures::pin_mut!(first); + assert!(futures::poll!(&mut first).is_pending()); + let second = share_git_run(change_probe_runs(), key, || { + scans.fetch_add(1, Ordering::Relaxed); + async { CLEAN_STATUS } + }); + futures::pin_mut!(second); + assert!(futures::poll!(&mut second).is_pending()); + release.send(()).expect("release shared probe"); + assert_eq!(tokio::join!(first, second), (DIRTY_STATUS, DIRTY_STATUS)); + assert_eq!(scans.load(Ordering::Relaxed), 0); +} + +#[tokio::test] +async fn change_probes_do_not_share_sibling_cwds_or_full_status() { + let repository = TempDir::new().expect("create repository"); + let root = repository.path().to_path_buf(); + let nested = root.join("nested"); + let (first, second, full) = tokio::join!( + share_git_run(change_probe_runs(), root.clone(), || async { + tokio::task::yield_now().await; + CLEAN_STATUS + }), + share_git_run(change_probe_runs(), nested, || async { + tokio::task::yield_now().await; + DIRTY_STATUS + }), + share_git_status_run(root, || async { + tokio::task::yield_now().await; + DIRTY_STATUS + }), + ); + assert_eq!( + (first, second, full), + (CLEAN_STATUS, DIRTY_STATUS, DIRTY_STATUS) + ); +} + +#[test] +fn porcelain_status_accepts_sha256_and_non_utf8_path_bytes() { + let hash = "ab".repeat(32); + let mut output = format!("# branch.head (detached)\n# branch.oid {hash}\n").into_bytes(); + output.extend_from_slice(b"? file-\xff\n"); + assert_eq!( + parse_worktree_status(&output), + Some(GitWorktreeStatus { + head_commit_hash: Some(GitSha::new(&hash)), + has_changes: true + }) + ); +} + +#[test] +fn porcelain_status_requires_one_valid_head_observation() { + for output in [ + b"# branch.head main\n".as_slice(), + b"# branch.oid abc123\n", + b"# branch.oid xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + b"# branch.oid (initial)\n# branch.oid (initial)\n", + ] { + assert_eq!(parse_worktree_status(output), None); + } } #[cfg(unix)] @@ -142,8 +326,8 @@ async fn status_keys_coalesce_symlink_aliases_of_the_same_worktree() { std::fs::create_dir(&repository).expect("create repository"); std::os::unix::fs::symlink(&repository, &repository_alias).expect("create repository alias"); - let key = git_status_key(PathBuf::from("git"), &repository).await; - let alias_key = git_status_key(PathBuf::from("git"), &repository_alias).await; + let key = canonical_worktree_root(&repository).await; + let alias_key = canonical_worktree_root(&repository_alias).await; assert_eq!(key, alias_key); } @@ -151,10 +335,7 @@ async fn status_keys_coalesce_symlink_aliases_of_the_same_worktree() { #[tokio::test] async fn concurrent_status_requests_share_one_repository_scan() { let repository = TempDir::new().expect("create repository"); - let key = GitStatusKey { - git: PathBuf::from("git"), - repo_root: repository.path().to_path_buf(), - }; + let key = repository.path().to_path_buf(); let scan_count = Arc::new(AtomicUsize::new(0)); let results = join_all((0..32).map(|_| { @@ -163,12 +344,12 @@ async fn concurrent_status_requests_share_one_repository_scan() { share_git_status_run(key, move || async move { scan_count.fetch_add(1, Ordering::Relaxed); tokio::time::sleep(Duration::from_millis(25)).await; - Some(true) + DIRTY_STATUS }) })) .await; - assert_eq!(results, vec![Some(true); 32]); + assert_eq!(results, vec![DIRTY_STATUS; 32]); assert_eq!(scan_count.load(Ordering::Relaxed), 1); } @@ -177,8 +358,8 @@ async fn status_requests_do_not_share_different_repositories() { let first_repository = TempDir::new().expect("create first repository"); let second_repository = TempDir::new().expect("create second repository"); let scan_count = Arc::new(AtomicUsize::new(0)); - let first_key = git_status_key(PathBuf::from("git"), first_repository.path()).await; - let second_key = git_status_key(PathBuf::from("git"), second_repository.path()).await; + let first_key = canonical_worktree_root(first_repository.path()).await; + let second_key = canonical_worktree_root(second_repository.path()).await; let first_scan_count = Arc::clone(&scan_count); let second_scan_count = Arc::clone(&scan_count); @@ -186,64 +367,55 @@ async fn status_requests_do_not_share_different_repositories() { share_git_status_run(first_key, move || async move { first_scan_count.fetch_add(1, Ordering::Relaxed); tokio::time::sleep(Duration::from_millis(25)).await; - Some(false) + CLEAN_STATUS }), share_git_status_run(second_key, move || async move { second_scan_count.fetch_add(1, Ordering::Relaxed); tokio::time::sleep(Duration::from_millis(25)).await; - Some(true) + DIRTY_STATUS }), ); - assert_eq!(results, (Some(false), Some(true))); + assert_eq!(results, (CLEAN_STATUS, DIRTY_STATUS)); assert_eq!(scan_count.load(Ordering::Relaxed), 2); } #[tokio::test] async fn completed_status_requests_start_fresh_scans_and_prune_expired_entries() { let first_repository = TempDir::new().expect("create first repository"); - let first_key = GitStatusKey { - git: PathBuf::from("git"), - repo_root: first_repository.path().to_path_buf(), - }; + let first_key = first_repository.path().to_path_buf(); let scan_count = Arc::new(AtomicUsize::new(0)); let first_scan_count = Arc::clone(&scan_count); assert_eq!( share_git_status_run(first_key.clone(), move || async move { first_scan_count.fetch_add(1, Ordering::Relaxed); - Some(true) + DIRTY_STATUS }) .await, - Some(true) + DIRTY_STATUS ); let second_scan_count = Arc::clone(&scan_count); assert_eq!( share_git_status_run(first_key.clone(), move || async move { second_scan_count.fetch_add(1, Ordering::Relaxed); - Some(false) + CLEAN_STATUS }) .await, - Some(false) + CLEAN_STATUS ); assert_eq!(scan_count.load(Ordering::Relaxed), 2); let second_repository = TempDir::new().expect("create second repository"); let third_scan_count = Arc::clone(&scan_count); assert_eq!( - share_git_status_run( - GitStatusKey { - git: PathBuf::from("git"), - repo_root: second_repository.path().to_path_buf(), - }, - move || async move { - third_scan_count.fetch_add(1, Ordering::Relaxed); - Some(true) - }, - ) + share_git_status_run(second_repository.path().to_path_buf(), move || async move { + third_scan_count.fetch_add(1, Ordering::Relaxed); + DIRTY_STATUS + }) .await, - Some(true) + DIRTY_STATUS ); assert_eq!(scan_count.load(Ordering::Relaxed), 3); assert!( @@ -257,10 +429,7 @@ async fn completed_status_requests_start_fresh_scans_and_prune_expired_entries() #[tokio::test] async fn completed_status_requests_are_not_reused_while_a_consumer_retains_them() { let repository = TempDir::new().expect("create repository"); - let key = GitStatusKey { - git: PathBuf::from("git"), - repo_root: repository.path().to_path_buf(), - }; + let key = repository.path().to_path_buf(); let scan_count = Arc::new(AtomicUsize::new(0)); let (release_sender, release_receiver) = tokio::sync::oneshot::channel(); @@ -270,7 +439,7 @@ async fn completed_status_requests_are_not_reused_while_a_consumer_retains_them( share_git_status_run(first_key, move || async move { first_scan_count.fetch_add(1, Ordering::Relaxed); release_receiver.await.ok()?; - Some(true) + DIRTY_STATUS }) .await }); @@ -284,28 +453,25 @@ async fn completed_status_requests_are_not_reused_while_a_consumer_retains_them( .expect("retain the pending shared status scan"); release_sender.send(()).expect("release shared status scan"); - assert_eq!(first.await.expect("status task completed"), Some(true)); - assert_eq!(retained_result.peek(), Some(&Some(true))); + assert_eq!(first.await.expect("status task completed"), DIRTY_STATUS); + assert_eq!(retained_result.peek(), Some(&DIRTY_STATUS)); let second_scan_count = Arc::clone(&scan_count); let result = share_git_status_run(key, move || async move { second_scan_count.fetch_add(1, Ordering::Relaxed); - Some(false) + CLEAN_STATUS }) .await; - assert_eq!(result, Some(false)); + assert_eq!(result, CLEAN_STATUS); assert_eq!(scan_count.load(Ordering::Relaxed), 2); - assert_eq!(retained_result.peek(), Some(&Some(true))); + assert_eq!(retained_result.peek(), Some(&DIRTY_STATUS)); } #[tokio::test] async fn canceling_one_consumer_preserves_the_shared_status_scan() { let repository = TempDir::new().expect("create repository"); - let key = GitStatusKey { - git: PathBuf::from("git"), - repo_root: repository.path().to_path_buf(), - }; + let key = repository.path().to_path_buf(); let scan_count = Arc::new(AtomicUsize::new(0)); let (release_sender, release_receiver) = tokio::sync::oneshot::channel(); @@ -315,7 +481,7 @@ async fn canceling_one_consumer_preserves_the_shared_status_scan() { share_git_status_run(first_key, move || async move { first_scan_count.fetch_add(1, Ordering::Relaxed); release_receiver.await.ok()?; - Some(true) + DIRTY_STATUS }) .await }); @@ -327,7 +493,7 @@ async fn canceling_one_consumer_preserves_the_shared_status_scan() { let second = tokio::spawn(async move { share_git_status_run(second_key, move || async move { second_scan_count.fetch_add(1, Ordering::Relaxed); - Some(false) + CLEAN_STATUS }) .await }); @@ -343,7 +509,7 @@ async fn canceling_one_consumer_preserves_the_shared_status_scan() { assert_eq!(scan_count.load(Ordering::Relaxed), 1); release_sender.send(()).expect("release shared status scan"); - assert_eq!(first.await.expect("status task completed"), Some(true)); + assert_eq!(first.await.expect("status task completed"), DIRTY_STATUS); assert!( git_status_runs() .lock() @@ -357,10 +523,7 @@ async fn canceling_one_consumer_preserves_the_shared_status_scan() { #[tokio::test] async fn canceling_the_last_consumer_allows_a_fresh_status_scan() { let repository = TempDir::new().expect("create repository"); - let key = GitStatusKey { - git: PathBuf::from("git"), - repo_root: repository.path().to_path_buf(), - }; + let key = repository.path().to_path_buf(); let scan_count = Arc::new(AtomicUsize::new(0)); let first_key = key.clone(); let first_scan_count = Arc::clone(&scan_count); @@ -368,7 +531,7 @@ async fn canceling_the_last_consumer_allows_a_fresh_status_scan() { let first = tokio::spawn(async move { share_git_status_run(first_key, move || async move { first_scan_count.fetch_add(1, Ordering::Relaxed); - std::future::pending::>().await + std::future::pending().await }) .await }); @@ -386,11 +549,11 @@ async fn canceling_the_last_consumer_allows_a_fresh_status_scan() { let second_scan_count = Arc::clone(&scan_count); let result = share_git_status_run(key.clone(), move || async move { second_scan_count.fetch_add(1, Ordering::Relaxed); - Some(false) + CLEAN_STATUS }) .await; - assert_eq!(result, Some(false)); + assert_eq!(result, CLEAN_STATUS); assert_eq!(scan_count.load(Ordering::Relaxed), 2); assert!( git_status_runs() diff --git a/codex-rs/memories/write/src/phase1.rs b/codex-rs/memories/write/src/phase1.rs index 19593db95d..f718115d99 100644 --- a/codex-rs/memories/write/src/phase1.rs +++ b/codex-rs/memories/write/src/phase1.rs @@ -262,6 +262,11 @@ mod job { rollout_git_branch: Option<&str>, stage_one_context: &StageOneRequestContext, ) -> anyhow::Result<(StageOneOutput, Option)> { + // Overlap the positive Git observation with reading and preparing the rollout. + // JoinSet aborts the probe if preparation fails or this job is cancelled. + let mut git_probe = tokio::task::JoinSet::new(); + let cwd = config.cwd.clone(); + git_probe.spawn(async move { codex_git_utils::probe_worktree_changes(&cwd).await }); let rollout_items = crate::rollout_input::read_recent_rollout_items(rollout_path).await?; let rollout_contents = match config.memories.version { MemoryVersion::V1 => serialize_filtered_rollout_response_items(&rollout_items)?, @@ -311,7 +316,7 @@ mod job { prompt.output_schema_strict = true; let (result, token_usage) = context - .stream_stage_one_prompt(config, prompt, stage_one_context) + .stream_stage_one_prompt(config, prompt, stage_one_context, git_probe) .await?; let output = StageOneOutput::parse(&result, config.memories.version)?; diff --git a/codex-rs/memories/write/src/runtime.rs b/codex-rs/memories/write/src/runtime.rs index 84fcbd5cdb..1b8c7e313b 100644 --- a/codex-rs/memories/write/src/runtime.rs +++ b/codex-rs/memories/write/src/runtime.rs @@ -320,6 +320,9 @@ impl MemoryStartupContext { config: &Config, prompt: Prompt, context: &StageOneRequestContext, + mut git_probe: tokio::task::JoinSet< + Option<(std::path::PathBuf, codex_git_utils::GitWorktreeStatus)>, + >, ) -> anyhow::Result<(String, Option)> { let installation_id = resolve_installation_id(&config.codex_home).await?; let config_snapshot = self.thread.config_snapshot().await; @@ -349,7 +352,7 @@ impl MemoryStartupContext { let mut client_session = model_client.new_session(); let window_id = format!("{}:0", self.thread_id); - let responses_metadata = detached_memory_responses_metadata( + let mut responses_metadata = detached_memory_responses_metadata( &self.thread_manager, installation_id, session_id_string, @@ -361,6 +364,10 @@ impl MemoryStartupContext { /*sandbox*/ None, ) .await; + if let Some(Ok(Some((root, observed)))) = git_probe.try_join_next() { + responses_metadata.apply_memory_git_change_observation(&root, observed); + } + drop(git_probe); let mut stream = client_session .stream( &Arc::new(prompt), diff --git a/codex-rs/memories/write/src/startup_tests.rs b/codex-rs/memories/write/src/startup_tests.rs index cc659fe197..d6f180f0ef 100644 --- a/codex-rs/memories/write/src/startup_tests.rs +++ b/codex-rs/memories/write/src/startup_tests.rs @@ -727,6 +727,7 @@ async fn memories_startup_phase1_uses_live_thread_service_tier_and_detached_meta &test.config, codex_core::Prompt::default(), &request_context, + tokio::task::JoinSet::new(), ) .await?; let request = wait_for_single_request(&stage_one).await;