mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Defer Git worktree status until the turn starts (#62437)
## Why A turn interrupted during setup can be discarded before it runs. Starting the worktree scan during setup can spend time scanning for a turn that will never send a model request. ## What changed Collect `HEAD` and remote URLs in the background after turn setup, and start the worktree status scan only after the turn is admitted to run. Model requests use the latest available Git metadata while collection continues, and later requests can include the worktree status once it is available. Use shared background Git root discovery for the memory extraction change probe and pass the resolved root into the probe. ## Testing Regression tests cover requests proceeding while status is blocked, later requests carrying the updated `HEAD`, remote URL, and dirty status, and an interrupted wake skipping the worktree scan. GitOrigin-RevId: 00f465a5621d672d8b6d9063c384fb62e493c79f
This commit is contained in:
@@ -14,6 +14,7 @@ use crate::shell_snapshot::ShellSnapshotSandbox;
|
||||
use crate::skills::HostSkillDiscovery;
|
||||
use crate::tools::sandboxing::configured_windows_sandbox_selection;
|
||||
use crate::tools::sandboxing::executor_windows_sandbox_selection;
|
||||
use crate::turn_metadata::TurnGitEnrichment;
|
||||
use arc_swap::ArcSwap;
|
||||
use codex_core_plugins::PluginCommandAttribution;
|
||||
use codex_core_plugins::ResolvedPluginMetricsOperation;
|
||||
@@ -1342,6 +1343,11 @@ impl Session {
|
||||
let cwd = primary_turn_environment
|
||||
.and_then(|environment| environment.cwd().to_abs_path().ok())
|
||||
.unwrap_or_else(|| session_configuration.cwd().clone());
|
||||
let git_enrichment_cwd = if git_enrichment_policy == GitEnrichmentPolicy::Fresh {
|
||||
turn_environments.single_local_environment_cwd()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let network_permission_profile = primary_turn_environment
|
||||
.map(TurnEnvironment::permission_profile)
|
||||
.cloned()
|
||||
@@ -1494,18 +1500,17 @@ impl Session {
|
||||
if let Some(program) = turn_context.cyber_access_program {
|
||||
turn_context.extension_data.insert(program);
|
||||
}
|
||||
let turn_context = Arc::new(turn_context);
|
||||
if git_enrichment_policy == GitEnrichmentPolicy::Fresh
|
||||
&& turn_context
|
||||
.initial_environments
|
||||
.single_local_environment_cwd()
|
||||
.is_some()
|
||||
{
|
||||
// Model, plugin, skill, and helper setup may change repository state.
|
||||
// Read refs after setup; status waits until the turn is admitted.
|
||||
if let Some(cwd) = git_enrichment_cwd {
|
||||
turn_context
|
||||
.turn_metadata_state
|
||||
.spawn_git_enrichment_task(Arc::clone(&self.services.git_root_discovery));
|
||||
.set_git_enrichment(TurnGitEnrichment::start(
|
||||
cwd,
|
||||
Arc::clone(&self.services.git_root_discovery),
|
||||
));
|
||||
}
|
||||
turn_context
|
||||
Arc::new(turn_context)
|
||||
}
|
||||
|
||||
pub(crate) async fn maybe_emit_model_warnings_for_turn(&self, tc: &TurnContext) {
|
||||
|
||||
@@ -474,6 +474,7 @@ impl Session {
|
||||
{
|
||||
_tool_result_capture = session.services.executed_tool_calls.begin_turn(&ctx);
|
||||
}
|
||||
ctx.turn_metadata_state.admit_git_enrichment();
|
||||
task_for_run
|
||||
.run(
|
||||
Arc::clone(&session),
|
||||
|
||||
@@ -1138,6 +1138,11 @@ impl ThreadManager {
|
||||
Arc::clone(&self.state.git_root_discovery)
|
||||
}
|
||||
|
||||
/// Finds the Git root using the shared, bounded background discovery service.
|
||||
pub async fn discover_git_root(&self, cwd: AbsolutePathBuf) -> Option<PathBuf> {
|
||||
self.state.git_root_discovery.discover(cwd).await
|
||||
}
|
||||
|
||||
/// Returns the startup catalog. Use [`CodexThread::model_info`] for a live chat or
|
||||
/// [`Self::models_manager_for_config`] for a new destination.
|
||||
pub fn get_models_manager(&self) -> SharedModelsManager {
|
||||
|
||||
@@ -10,7 +10,6 @@ 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;
|
||||
@@ -137,6 +136,38 @@ pub async fn detached_memory_responses_metadata(
|
||||
metadata
|
||||
}
|
||||
|
||||
/// A single turn owns both its current Git snapshot and the task publishing it.
|
||||
#[derive(Debug, Default)]
|
||||
pub(crate) struct TurnGitEnrichment {
|
||||
workspaces: Option<watch::Receiver<BTreeMap<String, TurnMetadataWorkspace>>>,
|
||||
task: Option<AbortOnDropHandle<()>>,
|
||||
admission: Option<oneshot::Sender<()>>,
|
||||
}
|
||||
|
||||
impl TurnGitEnrichment {
|
||||
pub(crate) fn start(cwd: AbsolutePathBuf, git_root_discovery: Arc<GitRootDiscovery>) -> Self {
|
||||
let (updates, workspaces) = watch::channel(BTreeMap::new());
|
||||
let (admission, request) = oneshot::channel();
|
||||
let task = tokio::spawn(async move {
|
||||
let repo_root = git_root_discovery.discover(cwd.clone());
|
||||
collect_git_workspaces(
|
||||
&cwd,
|
||||
repo_root,
|
||||
GitCollectionAdmission::Pending(request),
|
||||
|observed| {
|
||||
updates.send_replace(observed);
|
||||
},
|
||||
)
|
||||
.await;
|
||||
});
|
||||
Self {
|
||||
workspaces: Some(workspaces),
|
||||
task: Some(AbortOnDropHandle::new(task)),
|
||||
admission: Some(admission),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct TurnMetadataState {
|
||||
cwd: AbsolutePathBuf,
|
||||
@@ -159,13 +190,11 @@ pub(crate) struct TurnMetadataState {
|
||||
auto_review_enabled: bool,
|
||||
node_repl_auto_review_required: bool,
|
||||
node_repl_disabled: bool,
|
||||
enriched_workspaces: RwLock<Option<BTreeMap<String, TurnMetadataWorkspace>>>,
|
||||
git_enrichment: Mutex<TurnGitEnrichment>,
|
||||
turn_started_at_unix_ms: RwLock<Option<i64>>,
|
||||
responses_api_metadata: RwLock<BTreeMap<String, String>>,
|
||||
responsesapi_client_metadata: RwLock<BTreeMap<String, String>>,
|
||||
user_input_requested_during_turn: AtomicBool,
|
||||
enrichment_task: Mutex<Option<AbortOnDropHandle<()>>>,
|
||||
git_enrichment_complete: watch::Sender<bool>,
|
||||
}
|
||||
|
||||
impl codex_analytics::TurnAnalyticsMetadata for TurnMetadataState {
|
||||
@@ -244,13 +273,11 @@ impl TurnMetadataState {
|
||||
auto_review_enabled,
|
||||
node_repl_auto_review_required: model_info.computer_use_review_required(),
|
||||
node_repl_disabled: model_info.node_repl_disabled,
|
||||
enriched_workspaces: RwLock::new(None),
|
||||
git_enrichment: Mutex::default(),
|
||||
turn_started_at_unix_ms: RwLock::new(None),
|
||||
responses_api_metadata: RwLock::new(BTreeMap::new()),
|
||||
responsesapi_client_metadata: RwLock::new(BTreeMap::new()),
|
||||
user_input_requested_during_turn: AtomicBool::new(false),
|
||||
enrichment_task: Mutex::new(None),
|
||||
git_enrichment_complete: watch::channel(/*init*/ true).0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -519,10 +546,12 @@ impl TurnMetadataState {
|
||||
}
|
||||
|
||||
fn current_workspaces(&self) -> BTreeMap<String, TurnMetadataWorkspace> {
|
||||
self.enriched_workspaces
|
||||
.read()
|
||||
self.git_enrichment
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.clone()
|
||||
.workspaces
|
||||
.as_ref()
|
||||
.map(|workspaces| workspaces.borrow().clone())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
@@ -540,71 +569,68 @@ impl TurnMetadataState {
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(turn_started_at_unix_ms);
|
||||
}
|
||||
|
||||
pub(crate) fn spawn_git_enrichment_task(
|
||||
self: &Arc<Self>,
|
||||
git_root_discovery: Arc<GitRootDiscovery>,
|
||||
) {
|
||||
let mut task_guard = self
|
||||
.enrichment_task
|
||||
pub(crate) fn set_git_enrichment(&self, enrichment: TurnGitEnrichment) {
|
||||
*self
|
||||
.git_enrichment
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) = enrichment;
|
||||
}
|
||||
|
||||
pub(crate) fn spawn_git_enrichment_task(&self, git_root_discovery: Arc<GitRootDiscovery>) {
|
||||
let mut enrichment = self
|
||||
.git_enrichment
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if task_guard.is_some() {
|
||||
return;
|
||||
if enrichment.task.is_none() {
|
||||
*enrichment = TurnGitEnrichment::start(self.cwd.clone(), git_root_discovery);
|
||||
}
|
||||
|
||||
self.git_enrichment_complete.send_replace(/*value*/ false);
|
||||
// History-only contexts have no active task to cancel enrichment. Keep the
|
||||
// state weak across awaits so dropping its last owner aborts this waiter.
|
||||
let state = Arc::downgrade(self);
|
||||
let cwd = self.cwd.clone();
|
||||
*task_guard = Some(AbortOnDropHandle::new(tokio::spawn(async move {
|
||||
let workspaces = git_workspaces(&cwd, git_root_discovery.discover(cwd.clone())).await;
|
||||
let Some(state) = state.upgrade() else {
|
||||
return;
|
||||
};
|
||||
if !workspaces.is_empty() {
|
||||
*state
|
||||
.enriched_workspaces
|
||||
.write()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(workspaces);
|
||||
}
|
||||
|
||||
state.git_enrichment_complete.send_replace(/*value*/ true);
|
||||
})));
|
||||
}
|
||||
|
||||
pub(crate) async fn wait_for_git_enrichment(&self) {
|
||||
let mut completion = self.git_enrichment_complete.subscribe();
|
||||
let _ = completion.wait_for(|complete| *complete).await;
|
||||
let workspaces = self
|
||||
.git_enrichment
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.workspaces
|
||||
.clone();
|
||||
if let Some(mut workspaces) = workspaces {
|
||||
// Only the collector owns a sender. Completion and cancellation both close it.
|
||||
while workspaces.changed().await.is_ok() {}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn admit_git_enrichment(&self) {
|
||||
let admission = self
|
||||
.git_enrichment
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.admission
|
||||
.take();
|
||||
if let Some(admission) = admission {
|
||||
let _ = admission.send(());
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn cancel_git_enrichment_task(&self) {
|
||||
let mut task_guard = self
|
||||
.enrichment_task
|
||||
self.git_enrichment
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if let Some(task) = task_guard.take() {
|
||||
task.abort();
|
||||
self.git_enrichment_complete.send_replace(/*value*/ true);
|
||||
}
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.task
|
||||
.take();
|
||||
}
|
||||
}
|
||||
|
||||
async fn git_workspaces(
|
||||
cwd: &AbsolutePathBuf,
|
||||
repo_root: impl Future<Output = Option<PathBuf>>,
|
||||
) -> BTreeMap<String, TurnMetadataWorkspace> {
|
||||
let mut workspaces = BTreeMap::new();
|
||||
collect_git_workspaces(cwd, repo_root, |observed| workspaces = observed).await;
|
||||
workspaces
|
||||
}
|
||||
|
||||
async fn memory_workspaces(
|
||||
cwd: &AbsolutePathBuf,
|
||||
repo_root: impl Future<Output = Option<PathBuf>>,
|
||||
) -> BTreeMap<String, TurnMetadataWorkspace> {
|
||||
let mut workspaces = BTreeMap::new();
|
||||
let collect = collect_git_workspaces(cwd, repo_root, |observed| workspaces = observed);
|
||||
let collect = collect_git_workspaces(
|
||||
cwd,
|
||||
repo_root,
|
||||
GitCollectionAdmission::Immediate,
|
||||
|observed| workspaces = observed,
|
||||
);
|
||||
let _ = tokio::time::timeout(MEMORY_GIT_METADATA_TIMEOUT, collect).await;
|
||||
workspaces
|
||||
}
|
||||
@@ -612,25 +638,43 @@ async fn memory_workspaces(
|
||||
async fn collect_git_workspaces(
|
||||
cwd: &AbsolutePathBuf,
|
||||
repo_root: impl Future<Output = Option<PathBuf>>,
|
||||
admission: GitCollectionAdmission,
|
||||
mut publish: impl FnMut(BTreeMap<String, TurnMetadataWorkspace>),
|
||||
) {
|
||||
let Some(repo_root) = repo_root.await else {
|
||||
return;
|
||||
};
|
||||
let workspace_key = repo_root.to_string_lossy().into_owned();
|
||||
let status =
|
||||
collect_git_status_after_admission(admission, get_worktree_status_in_repo(cwd, &repo_root));
|
||||
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),
|
||||
status,
|
||||
|workspace| {
|
||||
publish(BTreeMap::from([(
|
||||
repo_root.to_string_lossy().into_owned(),
|
||||
workspace,
|
||||
)]));
|
||||
publish(BTreeMap::from([(workspace_key.clone(), workspace)]));
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
enum GitCollectionAdmission {
|
||||
Immediate,
|
||||
Pending(oneshot::Receiver<()>),
|
||||
}
|
||||
|
||||
async fn collect_git_status_after_admission(
|
||||
admission: GitCollectionAdmission,
|
||||
status: impl Future<Output = Option<GitWorktreeStatus>>,
|
||||
) -> Option<GitWorktreeStatus> {
|
||||
if let GitCollectionAdmission::Pending(request) = admission
|
||||
&& request.await.is_err()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
status.await
|
||||
}
|
||||
|
||||
async fn collect_workspace_git_metadata<R: Future<Output = ()>>(
|
||||
head: impl Future<Output = GitHeadState>,
|
||||
collect_remotes: impl FnOnce(watch::Sender<BTreeMap<String, SanitizedGitUrl>>) -> R,
|
||||
|
||||
@@ -141,6 +141,7 @@ async fn create_clean_git_repo(repo_name: &str) -> (TempDir, AbsolutePathBuf) {
|
||||
}
|
||||
|
||||
async fn wait_for_git_enrichment(state: &TurnMetadataState) -> Value {
|
||||
state.admit_git_enrichment();
|
||||
tokio::time::timeout(Duration::from_secs(2), state.wait_for_git_enrichment())
|
||||
.await
|
||||
.expect("git enrichment should complete");
|
||||
@@ -1390,6 +1391,49 @@ fn responses_api_metadata_accepts_previously_valid_reserved_keys() {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn preparing_model_request_metadata_does_not_wait_for_pending_git_enrichment() {
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let (_updates, workspaces) = watch::channel(BTreeMap::new());
|
||||
turn.turn_metadata_state
|
||||
.set_git_enrichment(TurnGitEnrichment {
|
||||
workspaces: Some(workspaces),
|
||||
task: None,
|
||||
admission: None,
|
||||
});
|
||||
let step = StepContext::for_test(Arc::new(turn));
|
||||
|
||||
let metadata = tokio::time::timeout(
|
||||
Duration::from_secs(1),
|
||||
session.responses_metadata(&step, CodexResponsesRequestKind::Turn),
|
||||
)
|
||||
.await
|
||||
.expect("generation must continue while optional Git discovery is pending");
|
||||
assert!(metadata.workspaces.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn turn_git_status_waits_for_admission() {
|
||||
let (admission, request) = oneshot::channel();
|
||||
let started = AtomicBool::new(false);
|
||||
let status =
|
||||
collect_git_status_after_admission(GitCollectionAdmission::Pending(request), async {
|
||||
started.store(true, Ordering::SeqCst);
|
||||
None
|
||||
});
|
||||
tokio::pin!(status);
|
||||
assert!(futures::poll!(status.as_mut()).is_pending());
|
||||
assert!(!started.load(Ordering::SeqCst));
|
||||
admission.send(()).expect("admit");
|
||||
assert!(
|
||||
tokio::time::timeout(Duration::from_secs(5), status)
|
||||
.await
|
||||
.expect("Git status should finish after admission")
|
||||
.is_none()
|
||||
);
|
||||
assert!(started.load(Ordering::SeqCst));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn git_enrichment_publishes_refs_before_status_is_available() {
|
||||
let (_temp_dir, repo) = create_clean_git_repo("repo").await;
|
||||
@@ -1535,9 +1579,12 @@ async fn git_enrichment_keeps_dirty_status_when_an_unborn_repo_contains_a_head_f
|
||||
.success()
|
||||
);
|
||||
std::fs::write(repo.join("HEAD"), "untracked").expect("write HEAD file");
|
||||
let workspaces = git_workspaces(
|
||||
let mut workspaces = BTreeMap::new();
|
||||
collect_git_workspaces(
|
||||
&repo,
|
||||
std::future::ready(Some(repo.as_path().to_path_buf())),
|
||||
GitCollectionAdmission::Immediate,
|
||||
|observed| workspaces = observed,
|
||||
)
|
||||
.await;
|
||||
let workspace = workspaces
|
||||
@@ -1585,8 +1632,10 @@ async fn git_enrichment_rejects_a_committed_status_when_head_is_missing() {
|
||||
assert!(latest.is_none());
|
||||
}
|
||||
|
||||
#[test_case::test_case(false; "new HEAD is clean")]
|
||||
#[test_case::test_case(true; "new HEAD is dirty")]
|
||||
#[tokio::test]
|
||||
async fn git_enrichment_does_not_mark_an_old_head_clean() {
|
||||
async fn git_enrichment_does_not_pair_old_head_with_new_status(expected_has_changes: bool) {
|
||||
let (_temp_dir, repo) = create_clean_git_repo("repo").await;
|
||||
let head = get_head_commit_hash(&repo)
|
||||
.await
|
||||
@@ -1601,10 +1650,15 @@ async fn git_enrichment_does_not_mark_an_old_head_clean() {
|
||||
.expect("commit")
|
||||
.success()
|
||||
);
|
||||
if expected_has_changes {
|
||||
std::fs::write(repo.join("untracked.txt"), "dirty\n").expect("dirty worktree");
|
||||
}
|
||||
let new_head = get_head_commit_hash(&repo).await.expect("read new HEAD");
|
||||
let status = get_worktree_status_in_repo(&repo, &repo)
|
||||
.await
|
||||
.expect("read clean status for the new HEAD");
|
||||
assert!(!status.has_changes);
|
||||
.expect("read successful status for the new HEAD");
|
||||
assert_eq!(status.head_commit_hash, Some(new_head));
|
||||
assert_eq!(status.has_changes, expected_has_changes);
|
||||
Some(status)
|
||||
};
|
||||
assert_eq!(
|
||||
@@ -1721,9 +1775,10 @@ async fn turn_metadata_state_coalesces_concurrent_git_enrichment() {
|
||||
barrier.wait().await;
|
||||
state.spawn_git_enrichment_task(git_root_discovery);
|
||||
state
|
||||
.enrichment_task
|
||||
.git_enrichment
|
||||
.lock()
|
||||
.expect("enrichment task lock")
|
||||
.task
|
||||
.as_ref()
|
||||
.expect("enrichment task")
|
||||
.abort_handle()
|
||||
@@ -1770,9 +1825,10 @@ async fn dropping_turn_metadata_aborts_unused_git_enrichment() {
|
||||
let weak_state = Arc::downgrade(&state);
|
||||
state.spawn_git_enrichment_task(Arc::default());
|
||||
let task = state
|
||||
.enrichment_task
|
||||
.git_enrichment
|
||||
.lock()
|
||||
.expect("enrichment task lock")
|
||||
.task
|
||||
.as_ref()
|
||||
.expect("enrichment task")
|
||||
.abort_handle();
|
||||
@@ -1849,9 +1905,10 @@ async fn turn_metadata_state_git_enrichment_cancellation_is_retryable_and_errors
|
||||
state.cancel_git_enrichment_task();
|
||||
assert!(
|
||||
state
|
||||
.enrichment_task
|
||||
.git_enrichment
|
||||
.lock()
|
||||
.expect("enrichment task lock")
|
||||
.task
|
||||
.is_none()
|
||||
);
|
||||
tokio::time::timeout(Duration::from_secs(2), state.wait_for_git_enrichment())
|
||||
@@ -1889,6 +1946,7 @@ async fn turn_metadata_state_git_enrichment_cancellation_is_retryable_and_errors
|
||||
&model_info_from_slug("gpt-5.4"),
|
||||
));
|
||||
invalid_state.spawn_git_enrichment_task(Arc::default());
|
||||
invalid_state.admit_git_enrichment();
|
||||
tokio::time::timeout(
|
||||
Duration::from_secs(2),
|
||||
invalid_state.wait_for_git_enrichment(),
|
||||
|
||||
@@ -501,6 +501,7 @@ async fn responses_stream_includes_turn_metadata_header_for_git_workspace_e2e()
|
||||
]);
|
||||
|
||||
let test = test_codex()
|
||||
.with_model("test-gpt-5.1-codex")
|
||||
.with_config(|config| config.analytics_enabled = Some(true))
|
||||
.build(&server)
|
||||
.await
|
||||
@@ -617,7 +618,11 @@ async fn responses_stream_includes_turn_metadata_header_for_git_workspace_e2e()
|
||||
let first_response = responses::sse(vec![
|
||||
responses::ev_response_created("resp-2"),
|
||||
responses::ev_reasoning_item("rsn-1", &["thinking"], &[]),
|
||||
responses::ev_exec_command_call("call-1", "echo turn-metadata"),
|
||||
responses::ev_function_call(
|
||||
"wait-for-git",
|
||||
"test_sync_tool",
|
||||
r#"{"wait_for_git_enrichment":true}"#,
|
||||
),
|
||||
responses::ev_completed("resp-2"),
|
||||
]);
|
||||
let follow_up_response = responses::sse(vec![
|
||||
|
||||
@@ -35,7 +35,7 @@ use test_case::test_case;
|
||||
|
||||
const ORIGIN_URL: &str = "https://example.invalid/cxa5426/repo.git";
|
||||
|
||||
fn run_git(cwd: &Path, args: &[&str]) -> Result<String> {
|
||||
pub(super) fn run_git(cwd: &Path, args: &[&str]) -> Result<String> {
|
||||
let output = Command::new("git")
|
||||
.args(args)
|
||||
.current_dir(cwd)
|
||||
@@ -53,21 +53,14 @@ fn run_git(cwd: &Path, args: &[&str]) -> Result<String> {
|
||||
fn create_git_repo() -> Result<(TempDir, String)> {
|
||||
let repo = TempDir::new()?;
|
||||
run_git(repo.path(), &["init", "-q", "-b", "main"])?;
|
||||
std::fs::write(repo.path().join("README.md"), "initial\n")?;
|
||||
run_git(repo.path(), &["add", "README.md"])?;
|
||||
run_git(repo.path(), &["config", "user.name", "CXA-5426"])?;
|
||||
run_git(
|
||||
repo.path(),
|
||||
&[
|
||||
"-c",
|
||||
"user.name=CXA-5426",
|
||||
"-c",
|
||||
"user.email=cxa5426@example.invalid",
|
||||
"commit",
|
||||
"-q",
|
||||
"-m",
|
||||
"initial",
|
||||
],
|
||||
&["config", "user.email", "cxa5426@example.invalid"],
|
||||
)?;
|
||||
std::fs::write(repo.path().join("README.md"), "initial\n")?;
|
||||
run_git(repo.path(), &["add", "README.md"])?;
|
||||
run_git(repo.path(), &["commit", "-qm", "initial"])?;
|
||||
run_git(repo.path(), &["remote", "add", "origin", ORIGIN_URL])?;
|
||||
let head = run_git(repo.path(), &["rev-parse", "HEAD"])?;
|
||||
Ok((repo, head))
|
||||
@@ -92,6 +85,172 @@ fn expected_workspace(repo: &Path, head: &str, has_changes: bool) -> Value {
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn assert_request_workspace(
|
||||
request: &core_test_support::responses::ResponsesRequest,
|
||||
expected: Value,
|
||||
) -> Result<()> {
|
||||
let header: Value = serde_json::from_str(
|
||||
&request
|
||||
.header("x-codex-turn-metadata")
|
||||
.context("turn metadata header")?,
|
||||
)?;
|
||||
assert_eq!(header["workspaces"], expected);
|
||||
assert_eq!(turn_metadata(&request.body_json())?["workspaces"], expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Requests remain prompt while status is blocked, then carry the fresh complete snapshot.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn requests_refresh_git_refs_after_model_lookup_without_waiting_for_status() -> Result<()> {
|
||||
use super::wake_reservation::GatedModelsManager;
|
||||
use core_test_support::responses;
|
||||
use std::io::Write;
|
||||
use std::sync::Arc;
|
||||
use std::time::UNIX_EPOCH;
|
||||
|
||||
struct NotifyRequest(Arc<tokio::sync::Notify>);
|
||||
|
||||
impl wiremock::Match for NotifyRequest {
|
||||
fn matches(&self, _request: &wiremock::Request) -> bool {
|
||||
self.0.notify_one();
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
skip_if_no_network!(Ok(()));
|
||||
let (repo, old_head) = create_git_repo()?;
|
||||
let root = repo.path();
|
||||
let next_origin = "https://example.invalid/cxa5426/next.git";
|
||||
let next = run_git(
|
||||
root,
|
||||
&["commit-tree", "HEAD^{tree}", "-p", "HEAD", "-m", "next"],
|
||||
)?;
|
||||
let server = responses::start_mock_server().await;
|
||||
let first_arrived = Arc::new(tokio::sync::Notify::new());
|
||||
let first_requests = responses::mount_sse_once_match(
|
||||
&server,
|
||||
NotifyRequest(Arc::clone(&first_arrived)),
|
||||
responses::sse(vec![
|
||||
ev_response_created("initial"),
|
||||
ev_function_call(
|
||||
"wait",
|
||||
"test_sync_tool",
|
||||
r#"{"wait_for_git_enrichment":true}"#,
|
||||
),
|
||||
ev_completed("initial"),
|
||||
]),
|
||||
)
|
||||
.await;
|
||||
let mut catalog = codex_models_manager::bundled_models_response()?;
|
||||
catalog
|
||||
.models
|
||||
.iter_mut()
|
||||
.find(|model| model.slug == "gpt-5.5")
|
||||
.context("bundled gpt-5.5 model")?
|
||||
.experimental_supported_tools
|
||||
.push("test_sync_tool".to_string());
|
||||
let models = Arc::new(GatedModelsManager::with_catalog(catalog));
|
||||
let cwd = root.to_path_buf().abs();
|
||||
let test = test_codex()
|
||||
.with_model("gpt-5.5")
|
||||
.with_models_manager(models.clone())
|
||||
.with_config(move |config| config.cwd = cwd)
|
||||
.build(&server)
|
||||
.await?;
|
||||
|
||||
let tracked = root.join("README.md");
|
||||
std::fs::write(&tracked, "changed\n")?;
|
||||
std::fs::File::open(&tracked)?.set_modified(UNIX_EPOCH + Duration::from_secs(1))?;
|
||||
std::fs::create_dir_all(root.join(".git/info"))?;
|
||||
std::fs::write(
|
||||
root.join(".git/info/attributes"),
|
||||
"README.md filter=hold-status\n",
|
||||
)?;
|
||||
let fifo = root.join(".git/release-status");
|
||||
ensure!(
|
||||
Command::new("mkfifo").arg(&fifo).status()?.success(),
|
||||
"create status gate"
|
||||
);
|
||||
let mut gate = std::fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(&fifo)?;
|
||||
run_git(
|
||||
root,
|
||||
&[
|
||||
"config",
|
||||
"filter.hold-status.clean",
|
||||
"read -r line < .git/release-status && cat",
|
||||
],
|
||||
)?;
|
||||
|
||||
let model_lookup = models.gate_next_lookup();
|
||||
let release_model = async {
|
||||
tokio::time::timeout(Duration::from_secs(5), model_lookup.entered.notified()).await?;
|
||||
let switched = run_git(root, &["update-ref", "HEAD", &next])
|
||||
.and_then(|_| run_git(root, &["remote", "set-url", "origin", next_origin]));
|
||||
model_lookup.release.notify_one();
|
||||
switched?;
|
||||
Ok(())
|
||||
};
|
||||
let release_status = async {
|
||||
// The Git command timeout is five seconds. This request must arrive before status
|
||||
// could time out, while the clean filter is still blocked on the FIFO.
|
||||
tokio::time::timeout(Duration::from_secs(4), first_arrived.notified()).await?;
|
||||
let first = first_requests.single_request();
|
||||
let metadata = turn_metadata(&first.body_json())?;
|
||||
if let Some(workspace) = metadata["workspaces"].get(root.to_string_lossy().as_ref()) {
|
||||
assert_ne!(
|
||||
workspace["latest_git_commit_hash"].as_str(),
|
||||
Some(old_head.as_str())
|
||||
);
|
||||
assert_ne!(
|
||||
workspace["associated_remote_urls"]["origin"].as_str(),
|
||||
Some(ORIGIN_URL)
|
||||
);
|
||||
assert!(workspace.get("has_changes").is_none());
|
||||
}
|
||||
let following =
|
||||
responses::mount_sse_once(&server, responses::sse_completed("follow-up")).await;
|
||||
gate.write_all(b"release\n")?;
|
||||
Ok(following)
|
||||
};
|
||||
let ((), (), following) = tokio::time::timeout(Duration::from_secs(12), async {
|
||||
tokio::try_join!(
|
||||
test.submit_text_turn("inspect the workspace"),
|
||||
release_model,
|
||||
release_status,
|
||||
)
|
||||
})
|
||||
.await??;
|
||||
|
||||
let follow_up = following.single_request();
|
||||
assert_eq!(
|
||||
follow_up.function_call_output_text("wait").as_deref(),
|
||||
Some("ok")
|
||||
);
|
||||
assert_request_workspace(
|
||||
&follow_up,
|
||||
json!({
|
||||
root.to_string_lossy().as_ref(): {
|
||||
"associated_remote_urls": {"origin": next_origin},
|
||||
"latest_git_commit_hash": next,
|
||||
"has_changes": true,
|
||||
},
|
||||
}),
|
||||
)?;
|
||||
let input = follow_up.body_json()["input"].to_string();
|
||||
assert!(!input.contains(&old_head));
|
||||
assert!(!input.contains(&next));
|
||||
assert!(!input.contains(ORIGIN_URL));
|
||||
assert!(!input.contains(next_origin));
|
||||
assert!(!input.contains("codex_git_state"));
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
enum GitMetadataAtStartup {
|
||||
Present,
|
||||
RestoredAfterPrewarm,
|
||||
@@ -325,16 +484,22 @@ async fn guardian_prewarm_and_review_skip_redundant_git_enrichment() -> Result<(
|
||||
turn_metadata(&user_turn)?["auto_review_enabled"].as_bool(),
|
||||
Some(true)
|
||||
);
|
||||
let dirty_workspace = expected_workspace(repo.path(), &head, /*has_changes*/ true);
|
||||
if let Some(workspaces) = initial_user_turn_metadata.get("workspaces") {
|
||||
let root = repo.path().to_string_lossy();
|
||||
assert_eq!(
|
||||
workspaces,
|
||||
&expected_workspace(repo.path(), &head, /*has_changes*/ true)
|
||||
workspaces.as_object().context("initial workspaces")?.len(),
|
||||
1
|
||||
);
|
||||
let fields = workspaces[root.as_ref()]
|
||||
.as_object()
|
||||
.context("initial workspace")?;
|
||||
assert!(!fields.is_empty());
|
||||
for (field, value) in fields {
|
||||
assert_eq!(Some(value), dirty_workspace[root.as_ref()].get(field));
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
turn_metadata(&user_turn)?["workspaces"],
|
||||
expected_workspace(repo.path(), &head, /*has_changes*/ true)
|
||||
);
|
||||
assert_eq!(turn_metadata(&user_turn)?["workspaces"], dirty_workspace);
|
||||
assert_eq!(
|
||||
guardian_turn["client_metadata"]["x-openai-subagent"].as_str(),
|
||||
Some("guardian")
|
||||
@@ -462,19 +627,7 @@ async fn concurrent_turns_keep_distinct_worktree_and_repository_metadata() -> Re
|
||||
)?;
|
||||
std::fs::write(worktree.join("branch.txt"), "branch commit\n")?;
|
||||
run_git(&worktree, &["add", "branch.txt"])?;
|
||||
run_git(
|
||||
&worktree,
|
||||
&[
|
||||
"-c",
|
||||
"user.name=CXA-5426",
|
||||
"-c",
|
||||
"user.email=cxa5426@example.invalid",
|
||||
"commit",
|
||||
"-q",
|
||||
"-m",
|
||||
"branch commit",
|
||||
],
|
||||
)?;
|
||||
run_git(&worktree, &["commit", "-qm", "branch commit"])?;
|
||||
let worktree_head = run_git(&worktree, &["rev-parse", "HEAD"])?;
|
||||
let (other_repo, other_head) = create_git_repo()?;
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ use codex_models_manager::manager::StaticModelsManager;
|
||||
use codex_protocol::AgentPath;
|
||||
use codex_protocol::config_types::CollaborationModeMask;
|
||||
use codex_protocol::openai_models::ModelInfo;
|
||||
use codex_protocol::openai_models::ModelsResponse;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::InterAgentCommunication;
|
||||
use codex_protocol::protocol::Op;
|
||||
@@ -36,18 +37,41 @@ use tokio::sync::Notify;
|
||||
use tokio::sync::TryLockError;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Gate {
|
||||
entered: Notify,
|
||||
release: Notify,
|
||||
pub(super) struct Gate {
|
||||
pub(super) entered: Notify,
|
||||
pub(super) release: Notify,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct GatedModelsManager {
|
||||
pub(super) struct GatedModelsManager {
|
||||
inner: StaticModelsManager,
|
||||
gate_next_lookup: AtomicBool,
|
||||
context_build: Gate,
|
||||
}
|
||||
|
||||
impl GatedModelsManager {
|
||||
pub(super) fn new() -> anyhow::Result<Self> {
|
||||
Ok(Self::with_catalog(bundled_models_response()?))
|
||||
}
|
||||
|
||||
pub(super) fn with_catalog(catalog: ModelsResponse) -> Self {
|
||||
Self {
|
||||
inner: StaticModelsManager::new(
|
||||
/*auth_manager*/ None,
|
||||
catalog,
|
||||
codex_models_manager::ModelOrigin::ExplicitConfig,
|
||||
),
|
||||
gate_next_lookup: AtomicBool::new(false),
|
||||
context_build: Gate::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn gate_next_lookup(&self) -> &Gate {
|
||||
self.gate_next_lookup.store(true, Ordering::SeqCst);
|
||||
&self.context_build
|
||||
}
|
||||
}
|
||||
|
||||
impl ModelsManager for GatedModelsManager {
|
||||
fn ensure_initialized(&self, factory: HttpClientFactory) -> ModelsManagerFuture<'_, ()> {
|
||||
self.inner.ensure_initialized(factory)
|
||||
@@ -147,15 +171,7 @@ impl TurnLifecycleContributor for SleepingTurn {
|
||||
async fn interrupt_during_wake_context_construction_does_not_start_turn() -> anyhow::Result<()> {
|
||||
let server = responses::start_mock_server().await;
|
||||
let requests = responses::mount_sse_once(&server, responses::sse_completed("initial")).await;
|
||||
let models = Arc::new(GatedModelsManager {
|
||||
inner: StaticModelsManager::new(
|
||||
/*auth_manager*/ None,
|
||||
bundled_models_response()?,
|
||||
codex_models_manager::ModelOrigin::ExplicitConfig,
|
||||
),
|
||||
gate_next_lookup: AtomicBool::new(/*v*/ false),
|
||||
context_build: Gate::default(),
|
||||
});
|
||||
let models = Arc::new(GatedModelsManager::new()?);
|
||||
let sleeping_turn = Arc::new(SleepingTurn::default());
|
||||
let mut extensions = ExtensionRegistryBuilder::<Config>::new();
|
||||
extensions.turn_lifecycle_contributor(sleeping_turn.clone());
|
||||
@@ -164,6 +180,32 @@ async fn interrupt_during_wake_context_construction_does_not_start_turn() -> any
|
||||
.with_extensions(Arc::new(extensions.build()))
|
||||
.build_with_auto_env(&server)
|
||||
.await?;
|
||||
#[cfg(unix)]
|
||||
let git_root = test
|
||||
.codex
|
||||
.environment_selections()
|
||||
.await
|
||||
.iter()
|
||||
.any(|selection| selection.environment_id == codex_exec_server::LOCAL_ENVIRONMENT_ID)
|
||||
.then(|| test.config.cwd.as_path().to_path_buf());
|
||||
#[cfg(unix)]
|
||||
if let Some(root) = &git_root {
|
||||
super::git_enrichment::run_git(root, &["init", "-q", "-b", "main"])?;
|
||||
std::fs::write(root.join("tracked"), "initial\n")?;
|
||||
super::git_enrichment::run_git(root, &["add", "tracked"])?;
|
||||
super::git_enrichment::run_git(
|
||||
root,
|
||||
&[
|
||||
"-c",
|
||||
"user.name=Codex",
|
||||
"-c",
|
||||
"user.email=codex@example.invalid",
|
||||
"commit",
|
||||
"-qm",
|
||||
"initial",
|
||||
],
|
||||
)?;
|
||||
}
|
||||
let codex = &test.codex;
|
||||
codex
|
||||
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
|
||||
@@ -172,6 +214,26 @@ async fn interrupt_during_wake_context_construction_does_not_start_turn() -> any
|
||||
}]))
|
||||
.await?;
|
||||
sleeping_turn.stop.entered.notified().await;
|
||||
#[cfg(unix)]
|
||||
if let Some(root) = &git_root {
|
||||
codex_git_utils::get_worktree_status_in_repo(root, root)
|
||||
.await
|
||||
.ok_or_else(|| anyhow::anyhow!("initial status should complete"))?;
|
||||
std::fs::create_dir_all(root.join(".git/info"))?;
|
||||
std::fs::write(root.join(".git/info/attributes"), "tracked filter=wake\n")?;
|
||||
super::git_enrichment::run_git(
|
||||
root,
|
||||
&[
|
||||
"config",
|
||||
"filter.wake.clean",
|
||||
"printf ran > .git/wake-status-ran; cat",
|
||||
],
|
||||
)?;
|
||||
let tracked = root.join("tracked");
|
||||
std::fs::write(&tracked, "changed\n")?;
|
||||
std::fs::File::open(tracked)?
|
||||
.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1))?;
|
||||
}
|
||||
|
||||
// Queue mail while the completed turn still owns the reservation, so its
|
||||
// completion starts the wake independently of the submission loop.
|
||||
@@ -192,24 +254,26 @@ async fn interrupt_during_wake_context_construction_does_not_start_turn() -> any
|
||||
matches!(event, EventMsg::RealtimeConversationListVoicesResponse(_))
|
||||
})
|
||||
.await;
|
||||
models
|
||||
.gate_next_lookup
|
||||
.store(/*val*/ true, Ordering::SeqCst);
|
||||
let context_build = models.gate_next_lookup();
|
||||
sleeping_turn.stop.release.notify_one();
|
||||
|
||||
// Context construction happens after the discovery-time reservation check.
|
||||
// Process the interrupt before allowing that context construction to finish.
|
||||
models.context_build.entered.notified().await;
|
||||
context_build.entered.notified().await;
|
||||
codex.submit(Op::Interrupt).await?;
|
||||
codex.submit(Op::RealtimeConversationListVoices).await?;
|
||||
wait_for_event(codex, |event| {
|
||||
matches!(event, EventMsg::RealtimeConversationListVoicesResponse(_))
|
||||
})
|
||||
.await;
|
||||
models.context_build.release.notify_one();
|
||||
context_build.release.notify_one();
|
||||
sleeping_turn.finished.notified().await;
|
||||
|
||||
assert_eq!(sleeping_turn.started.load(Ordering::SeqCst), 1);
|
||||
#[cfg(unix)]
|
||||
if let Some(root) = &git_root {
|
||||
assert!(!root.join(".git/wake-status-ran").exists());
|
||||
}
|
||||
requests.single_request();
|
||||
codex.shutdown_and_wait().await?;
|
||||
Ok(())
|
||||
|
||||
@@ -13,7 +13,6 @@ 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;
|
||||
|
||||
@@ -82,8 +81,11 @@ pub async fn get_worktree_status_in_repo(
|
||||
/// 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)?;
|
||||
pub async fn probe_worktree_changes(
|
||||
cwd: &Path,
|
||||
repo_root: &Path,
|
||||
) -> Option<(PathBuf, GitWorktreeStatus)> {
|
||||
let root = repo_root.to_path_buf();
|
||||
let probe_root = root.clone();
|
||||
let cwd = cwd.to_path_buf();
|
||||
// Relative Git environment paths can resolve differently in sibling cwds.
|
||||
|
||||
@@ -214,15 +214,17 @@ async fn change_probe_only_returns_git_verified_positive_status() {
|
||||
crate::ensure_git_baseline_repository(root)
|
||||
.await
|
||||
.expect("create initial commit");
|
||||
let cwd = root.join("nested");
|
||||
std::fs::create_dir(&cwd).expect("create nested cwd");
|
||||
let head = crate::get_head_commit_hash(root).await.expect("read HEAD");
|
||||
assert_eq!(probe_worktree_changes(root).await, None);
|
||||
assert_eq!(probe_worktree_changes(&cwd, 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,
|
||||
probe_worktree_changes(&cwd, root).await,
|
||||
Some((
|
||||
root.to_path_buf(),
|
||||
GitWorktreeStatus {
|
||||
@@ -240,7 +242,7 @@ async fn change_probe_only_returns_git_verified_positive_status() {
|
||||
.expect("configure hidden untracked files")
|
||||
.success()
|
||||
);
|
||||
assert_eq!(probe_worktree_changes(root).await, None);
|
||||
assert_eq!(probe_worktree_changes(&cwd, root).await, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -266,7 +266,11 @@ mod job {
|
||||
// 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 thread_manager = Arc::clone(&context.thread_manager);
|
||||
git_probe.spawn(async move {
|
||||
let root = thread_manager.discover_git_root(cwd.clone()).await?;
|
||||
codex_git_utils::probe_worktree_changes(&cwd, &root).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)?,
|
||||
|
||||
@@ -81,7 +81,7 @@ pub(crate) struct MemoryStartupContext {
|
||||
version: MemoryVersion,
|
||||
thread_id: ThreadId,
|
||||
thread: Arc<CodexThread>,
|
||||
thread_manager: Arc<ThreadManager>,
|
||||
pub(crate) thread_manager: Arc<ThreadManager>,
|
||||
auth_manager: Arc<AuthManager>,
|
||||
provider: SharedModelProvider,
|
||||
session_telemetry: SessionTelemetry,
|
||||
|
||||
Reference in New Issue
Block a user