Coalesce concurrent Git status scans (#37151)

## What changed

- Share an in-flight `git status --porcelain` invocation among concurrent workspace metadata requests for the same canonical repository root.
- Keep scans for different repositories independent, and start a fresh scan after an in-flight request completes or loses all consumers.
- Pass the known repository root into dirty-worktree detection so requests from sibling directories and symlink aliases use the same key.

## Testing

- Add coverage for request coalescing, repository and symlink keying, completed scans, and consumer cancellation.

GitOrigin-RevId: 4c197c1d5bb94ca83c02979be34b17f24aefde08
This commit is contained in:
Adam Hupp authored and copyberry committed 2026-08-05 20:23:35 +00:00
1 parent a1890b6998
commit b6c3b51533
6 files changed
+434 -34

No files matched your search

+21 -13
View File
@@ -1,5 +1,7 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::OnceLock;
@@ -21,7 +23,7 @@ use crate::responses_metadata::subagent_metadata_kind;
use crate::sandbox_tags::permission_profile_sandbox_tag;
use codex_git_utils::get_git_remote_urls_assume_git_repo;
use codex_git_utils::get_git_repo_root;
use codex_git_utils::get_has_changes;
use codex_git_utils::get_has_changes_in_repo;
use codex_git_utils::get_head_commit_hash;
use codex_protocol::ThreadId;
use codex_protocol::ToolName;
@@ -89,7 +91,7 @@ pub async fn detached_memory_responses_metadata(
#[derive(Debug)]
pub(crate) struct TurnMetadataState {
cwd: AbsolutePathBuf,
repo_root: Option<String>,
repo_root: Option<PathBuf>,
session_id: String,
thread_id: String,
forked_from_thread_id: Option<ThreadId>,
@@ -123,7 +125,7 @@ impl TurnMetadataState {
windows_sandbox_level: WindowsSandboxLevel,
enforce_managed_network: bool,
) -> Self {
let repo_root = get_git_repo_root(&cwd).map(|root| root.to_string_lossy().into_owned());
let repo_root = get_git_repo_root(&cwd);
let sandbox = Some(
permission_profile_sandbox_tag(
permission_profile,
@@ -318,17 +320,20 @@ impl TurnMetadataState {
let state = Arc::clone(self);
*task_guard = Some(tokio::spawn(async move {
let workspace_git_metadata = state.fetch_workspace_git_metadata().await;
let Some(repo_root) = state.repo_root.clone() else {
return;
};
let workspace_git_metadata = state.fetch_workspace_git_metadata(&repo_root).await;
if workspace_git_metadata.is_empty() {
return;
}
let mut workspaces = BTreeMap::new();
workspaces.insert(repo_root, workspace_git_metadata.into());
workspaces.insert(
repo_root.to_string_lossy().into_owned(),
workspace_git_metadata.into(),
);
*state
.enriched_workspaces
.write()
@@ -346,11 +351,11 @@ impl TurnMetadataState {
}
}
async fn fetch_workspace_git_metadata(&self) -> WorkspaceGitMetadata {
async fn fetch_workspace_git_metadata(&self, repo_root: &Path) -> WorkspaceGitMetadata {
let (head_commit_hash, associated_remote_urls, has_changes) = tokio::join!(
get_head_commit_hash(&self.cwd),
get_git_remote_urls_assume_git_repo(&self.cwd),
get_has_changes(&self.cwd),
get_has_changes_in_repo(&self.cwd, repo_root),
);
let latest_git_commit_hash = head_commit_hash.map(|sha| sha.0);
@@ -363,11 +368,13 @@ impl TurnMetadataState {
}
async fn memory_workspaces(cwd: &AbsolutePathBuf) -> BTreeMap<String, TurnMetadataWorkspace> {
let repo_root = get_git_repo_root(cwd).map(|root| root.to_string_lossy().into_owned());
let Some(repo_root) = get_git_repo_root(cwd) 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(cwd),
get_has_changes_in_repo(cwd, &repo_root),
);
let workspace_git_metadata = WorkspaceGitMetadata {
associated_remote_urls,
@@ -375,10 +382,11 @@ async fn memory_workspaces(cwd: &AbsolutePathBuf) -> BTreeMap<String, TurnMetada
has_changes,
};
let mut workspaces = BTreeMap::new();
if let Some(repo_root) = repo_root
&& !workspace_git_metadata.is_empty()
{
workspaces.insert(repo_root, workspace_git_metadata.into());
if !workspace_git_metadata.is_empty() {
workspaces.insert(
repo_root.to_string_lossy().into_owned(),
workspace_git_metadata.into(),
);
}
workspaces
}