mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Refresh global instructions at model-request boundaries (#44675)
## Why Running root threads retained their startup global instructions, so edits to global `AGENTS.md` files did not take effect during an active session. ## What changed - Reload global instructions when capturing model-request context, including after tools within the same turn. Apply changes without repeating unchanged instructions or rediscovering repository instructions when the environment and trust level are unchanged. - Preserve the last successful global instructions on read failures, suppress recurring warnings until recovery, and clear instructions when their source is removed or blank. - Serialize refreshes and allow cancellation without blocking subsequent requests. - Give new subagents the parent's applied instruction snapshot and update Guardian reviewer reuse to account for refreshed instructions. ## Testing Add regression coverage for live edits and removal, read failures and recovery, warning suppression, cancellation, subagent inheritance, and Guardian reviewer reuse. Update resume, fork, and compaction tests to verify refreshed instructions. GitOrigin-RevId: f7e9b399740fa4f45e482c070f6e901ee7cf85cd
This commit is contained in:
1 parent
28f43b0417
commit
935ac7710d
23 files changed
+1143
-249
No files matched your search
@@ -1,4 +1,7 @@
|
||||
use std::collections::HashSet;
|
||||
use std::io;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use codex_extension_api::Instructions;
|
||||
use codex_extension_api::LoadUserInstructionsFuture;
|
||||
@@ -13,12 +16,25 @@ const LOCAL_AGENTS_MD_FILENAME: &str = "AGENTS.override.md";
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CodexHomeUserInstructionsProvider {
|
||||
codex_home: AbsolutePathBuf,
|
||||
// Cached instructions and warning history are shared across provider clones.
|
||||
state: Arc<Mutex<InstructionsState>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct InstructionsState {
|
||||
// Failed refreshes retain the last good value; confirmed absence still clears it.
|
||||
last_successful_instructions: Option<Instructions>,
|
||||
// Report ongoing failures once, then again if they return after recovery.
|
||||
active_warnings: HashSet<String>,
|
||||
}
|
||||
|
||||
impl CodexHomeUserInstructionsProvider {
|
||||
/// Creates a provider rooted at the supplied absolute Codex home directory.
|
||||
pub fn new(codex_home: AbsolutePathBuf) -> Self {
|
||||
Self { codex_home }
|
||||
Self {
|
||||
codex_home,
|
||||
state: Arc::new(Mutex::new(InstructionsState::default())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn load_from_codex_home(&self) -> LoadedUserInstructions {
|
||||
@@ -69,7 +85,26 @@ impl CodexHomeUserInstructionsProvider {
|
||||
|
||||
impl UserInstructionsProvider for CodexHomeUserInstructionsProvider {
|
||||
fn load_user_instructions(&self) -> LoadUserInstructionsFuture<'_> {
|
||||
Box::pin(self.load_from_codex_home())
|
||||
Box::pin(async move {
|
||||
let mut loaded = self.load_from_codex_home().await;
|
||||
let mut state = self
|
||||
.state
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if loaded.instructions.is_some() || loaded.warnings.is_empty() {
|
||||
state
|
||||
.last_successful_instructions
|
||||
.clone_from(&loaded.instructions);
|
||||
} else {
|
||||
loaded.instructions = state.last_successful_instructions.clone();
|
||||
}
|
||||
let previous_warnings = std::mem::take(&mut state.active_warnings);
|
||||
loaded.warnings.retain(|warning| {
|
||||
state.active_warnings.insert(warning.clone())
|
||||
&& !previous_warnings.contains(warning)
|
||||
});
|
||||
loaded
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -110,6 +110,7 @@ async fn directory_override_falls_back_to_default() {
|
||||
#[tokio::test]
|
||||
async fn recoverable_override_read_error_warns_and_falls_back_to_default() {
|
||||
let home = TempDir::new().expect("temp dir");
|
||||
let provider = provider(&home);
|
||||
let override_path = home.path().join(LOCAL_AGENTS_MD_FILENAME);
|
||||
create_symlink_loop(&override_path);
|
||||
fs::write(home.path().join(DEFAULT_AGENTS_MD_FILENAME), "default").expect("write default");
|
||||
@@ -120,11 +121,70 @@ async fn recoverable_override_read_error_warns_and_falls_back_to_default() {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
provider(&home).load_user_instructions().await,
|
||||
provider.load_user_instructions().await,
|
||||
expected(
|
||||
&home,
|
||||
DEFAULT_AGENTS_MD_FILENAME,
|
||||
"default",
|
||||
vec![warning.clone()]
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
provider.clone().load_user_instructions().await,
|
||||
expected(&home, DEFAULT_AGENTS_MD_FILENAME, "default", Vec::new())
|
||||
);
|
||||
|
||||
fs::remove_file(&override_path).expect("remove broken symlink");
|
||||
assert_eq!(
|
||||
provider.load_user_instructions().await,
|
||||
expected(&home, DEFAULT_AGENTS_MD_FILENAME, "default", Vec::new())
|
||||
);
|
||||
create_symlink_loop(&override_path);
|
||||
assert_eq!(
|
||||
provider.load_user_instructions().await,
|
||||
expected(&home, DEFAULT_AGENTS_MD_FILENAME, "default", vec![warning])
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn failed_refresh_retains_last_success_until_recovery_or_removal() {
|
||||
let home = TempDir::new().expect("temp dir");
|
||||
let provider = provider(&home);
|
||||
let path = home.path().join(DEFAULT_AGENTS_MD_FILENAME);
|
||||
fs::write(&path, "initial").expect("write initial instructions");
|
||||
let initial = provider.load_user_instructions().await;
|
||||
|
||||
fs::remove_file(&path).expect("remove initial file");
|
||||
create_symlink_loop(&path);
|
||||
let failed = provider.load_user_instructions().await;
|
||||
assert_eq!(failed.instructions, initial.instructions);
|
||||
assert_eq!(failed.warnings.len(), 1);
|
||||
assert_eq!(provider.load_user_instructions().await, initial);
|
||||
|
||||
fs::remove_file(&path).expect("remove broken symlink");
|
||||
fs::write(&path, "recovered").expect("restore instructions");
|
||||
assert_eq!(
|
||||
provider.load_user_instructions().await,
|
||||
expected(&home, DEFAULT_AGENTS_MD_FILENAME, "recovered", Vec::new()),
|
||||
);
|
||||
|
||||
fs::write(&path, " \n").expect("clear instructions");
|
||||
assert_eq!(
|
||||
provider.load_user_instructions().await,
|
||||
LoadedUserInstructions::default(),
|
||||
);
|
||||
fs::remove_file(&path).expect("remove cleared instructions");
|
||||
create_symlink_loop(&path);
|
||||
let failed_after_clear = provider.load_user_instructions().await;
|
||||
assert_eq!(failed_after_clear.instructions, None);
|
||||
assert_eq!(failed_after_clear.warnings.len(), 1);
|
||||
fs::remove_file(&path).expect("remove broken symlink");
|
||||
assert_eq!(
|
||||
provider.load_user_instructions().await,
|
||||
LoadedUserInstructions::default(),
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_utf8_is_lossy() {
|
||||
let home = TempDir::new().expect("temp dir");
|
||||
|
||||
Reference in new issue
Block a user