mirror of
https://github.com/openai/codex.git
synced 2026-11-16 06:54:36 +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
@@ -52,6 +52,7 @@ use tokio::sync::Mutex;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::agents_md_manager::SessionInstructions;
|
||||
use crate::codex_delegate::run_codex_thread_interactive;
|
||||
use crate::config::Config;
|
||||
use crate::config::Constrained;
|
||||
@@ -234,7 +235,7 @@ pub struct GuardianReviewSessionReuseKey {
|
||||
impl GuardianReviewSessionReuseKey {
|
||||
fn from_spawn_config(
|
||||
spawn_config: &Config,
|
||||
user_instructions: Option<Instructions>,
|
||||
instructions: SessionInstructions,
|
||||
parent_history_version: u64,
|
||||
context_mode: GuardianContextMode,
|
||||
) -> Self {
|
||||
@@ -262,7 +263,7 @@ impl GuardianReviewSessionReuseKey {
|
||||
permissions: spawn_config.permissions.clone(),
|
||||
developer_instructions: spawn_config.developer_instructions.clone(),
|
||||
base_instructions: spawn_config.base_instructions.clone(),
|
||||
user_instructions,
|
||||
user_instructions: instructions.user,
|
||||
compact_prompt: spawn_config.compact_prompt.clone(),
|
||||
cwd: PathUri::from_abs_path(&spawn_config.cwd),
|
||||
mcp_servers: spawn_config.mcp_servers.clone(),
|
||||
|
||||
@@ -32,7 +32,7 @@ impl PreparedSession {
|
||||
let parent_compaction = context_policy.parent_compaction(history, compaction_model_hash)?;
|
||||
let mut key = GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
&config,
|
||||
parent.user_instructions().await,
|
||||
parent.inherited_instructions().await,
|
||||
history.history_version(),
|
||||
parent.guardian_context_mode,
|
||||
)
|
||||
@@ -134,6 +134,11 @@ impl ReviewerSessionFactory for PreparedSession {
|
||||
.await?
|
||||
}
|
||||
};
|
||||
let inherited = session.inherited_instructions().await;
|
||||
let context = GuardianReviewSessionReuseKey {
|
||||
user_instructions: inherited.user,
|
||||
..context
|
||||
};
|
||||
Ok(GuardianReviewSession {
|
||||
session,
|
||||
io,
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use super::super::prompt::BUNDLED_GUARDIAN_POLICY_TEMPLATE;
|
||||
use super::super::prompt::guardian_policy_prompt_with_config_and_template;
|
||||
use super::*;
|
||||
use crate::agents_md_manager::AgentsMdManager;
|
||||
use crate::context_manager::ContextManager;
|
||||
use codex_guardian_reviewer::ReviewerRequest;
|
||||
use codex_guardian_reviewer::ReviewerSession;
|
||||
use codex_guardian_reviewer::ReviewerSessionFactory;
|
||||
use codex_history::CodexHarnessMetadata;
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
use codex_protocol::openai_models::AutoReviewMessages;
|
||||
@@ -56,7 +60,7 @@ async fn run_review_preserves_evidence_during_parent_compaction() {
|
||||
let (mut reviewer, tx_event, rx_sub) = test_review_session().await;
|
||||
reviewer.reuse_key = GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
¶ms.spawn_config,
|
||||
parent.user_instructions().await,
|
||||
parent.inherited_instructions().await,
|
||||
params.parent_history.history_version(),
|
||||
parent.guardian_context_mode,
|
||||
)
|
||||
@@ -138,7 +142,7 @@ async fn test_review_session() -> (
|
||||
let (_agent_status_tx, agent_status) = tokio::sync::watch::channel(AgentStatus::PendingInit);
|
||||
let reuse_key = GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
session.get_config().await.as_ref(),
|
||||
session.user_instructions().await,
|
||||
session.inherited_instructions().await,
|
||||
session.clone_history().await.history_version(),
|
||||
GuardianContextMode::Legacy,
|
||||
);
|
||||
@@ -275,6 +279,57 @@ async fn spawned_guardian_session_preserves_windows_sandbox_proxy_settings() {
|
||||
manager.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn spawned_guardian_reuse_key_matches_inherited_instructions() {
|
||||
let mut params = test_review_params().await;
|
||||
let latest_global = Some(Instructions {
|
||||
text: "latest global instructions".to_string(),
|
||||
source: params.spawn_config.codex_home.join("AGENTS.md"),
|
||||
});
|
||||
let parent = Arc::get_mut(&mut params.parent_session).expect("unshared parent session");
|
||||
parent.services.agents_md_manager = Arc::new(AgentsMdManager::new(SessionInstructions {
|
||||
user: latest_global.clone(),
|
||||
..Default::default()
|
||||
}));
|
||||
// Reproduce an update between reuse-key capture and reviewer creation.
|
||||
let stale_key = GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
¶ms.spawn_config,
|
||||
SessionInstructions {
|
||||
user: Some(Instructions {
|
||||
text: "previous global instructions".to_string(),
|
||||
source: params.spawn_config.codex_home.join("AGENTS.md"),
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
/*parent_history_version*/ 0,
|
||||
parent.guardian_context_mode,
|
||||
);
|
||||
let expected_key = GuardianReviewSessionReuseKey {
|
||||
user_instructions: latest_global.clone(),
|
||||
..stale_key.clone()
|
||||
};
|
||||
let prepared = factory::prepare_review(params)
|
||||
.await
|
||||
.expect("prepare review");
|
||||
let review = prepared
|
||||
.factory()
|
||||
.spawn(
|
||||
stale_key,
|
||||
GuardianReviewSessionKind::TrunkNew,
|
||||
/*snapshot*/ None,
|
||||
CancellationToken::new(),
|
||||
)
|
||||
.await
|
||||
.expect("spawn reviewer after instruction update");
|
||||
|
||||
assert_eq!(review.reuse_key, expected_key);
|
||||
assert_eq!(
|
||||
review.session.inherited_instructions().await.user,
|
||||
latest_global
|
||||
);
|
||||
review.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_review_session_config_change_invalidates_cached_session() {
|
||||
let parent_config = crate::config::test_config().await;
|
||||
@@ -290,7 +345,7 @@ async fn guardian_review_session_config_change_invalidates_cached_session() {
|
||||
.expect("cached guardian config");
|
||||
let cached_reuse_key = GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
&cached_spawn_config,
|
||||
/*user_instructions*/ None,
|
||||
SessionInstructions::default(),
|
||||
/*parent_history_version*/ 0,
|
||||
GuardianContextMode::Legacy,
|
||||
);
|
||||
@@ -310,7 +365,7 @@ async fn guardian_review_session_config_change_invalidates_cached_session() {
|
||||
.expect("next guardian config");
|
||||
let next_reuse_key = GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
&next_spawn_config,
|
||||
/*user_instructions*/ None,
|
||||
SessionInstructions::default(),
|
||||
/*parent_history_version*/ 0,
|
||||
GuardianContextMode::Legacy,
|
||||
);
|
||||
@@ -324,7 +379,7 @@ async fn guardian_review_session_config_change_invalidates_cached_session() {
|
||||
cached_reuse_key,
|
||||
GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
&cached_spawn_config,
|
||||
/*user_instructions*/ None,
|
||||
SessionInstructions::default(),
|
||||
/*parent_history_version*/ 0,
|
||||
GuardianContextMode::Legacy,
|
||||
)
|
||||
@@ -334,7 +389,7 @@ async fn guardian_review_session_config_change_invalidates_cached_session() {
|
||||
cached_reuse_key,
|
||||
GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
&cached_spawn_config,
|
||||
/*user_instructions*/ None,
|
||||
SessionInstructions::default(),
|
||||
/*parent_history_version*/ 1,
|
||||
GuardianContextMode::Legacy,
|
||||
)
|
||||
@@ -370,13 +425,13 @@ async fn guardian_review_session_config_change_invalidates_cached_session() {
|
||||
assert_ne!(
|
||||
GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
&compaction_enabled_config,
|
||||
/*user_instructions*/ None,
|
||||
SessionInstructions::default(),
|
||||
/*parent_history_version*/ 0,
|
||||
GuardianContextMode::Legacy,
|
||||
),
|
||||
GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
&compaction_enabled_config,
|
||||
/*user_instructions*/ None,
|
||||
SessionInstructions::default(),
|
||||
/*parent_history_version*/ 1,
|
||||
GuardianContextMode::Legacy,
|
||||
)
|
||||
@@ -489,7 +544,7 @@ async fn guardian_review_session_compact_scope_change_invalidates_cached_session
|
||||
.expect("cached guardian config");
|
||||
let cached_reuse_key = GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
&cached_spawn_config,
|
||||
/*user_instructions*/ None,
|
||||
SessionInstructions::default(),
|
||||
/*parent_history_version*/ 0,
|
||||
GuardianContextMode::Legacy,
|
||||
);
|
||||
@@ -509,7 +564,7 @@ async fn guardian_review_session_compact_scope_change_invalidates_cached_session
|
||||
.expect("next guardian config");
|
||||
let next_reuse_key = GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
&next_spawn_config,
|
||||
/*user_instructions*/ None,
|
||||
SessionInstructions::default(),
|
||||
/*parent_history_version*/ 0,
|
||||
GuardianContextMode::Legacy,
|
||||
);
|
||||
@@ -909,10 +964,18 @@ async fn run_review_on_reused_session_waits_for_submitted_turn() {
|
||||
#[tokio::test]
|
||||
async fn run_review_removes_trunk_when_event_stream_is_broken() {
|
||||
let (mut review_session, tx_event, rx_sub) = test_review_session().await;
|
||||
let params = test_review_params().await;
|
||||
let mut params = test_review_params().await;
|
||||
let parent = Arc::get_mut(&mut params.parent_session).expect("unshared parent session");
|
||||
parent.services.agents_md_manager = Arc::new(AgentsMdManager::new(SessionInstructions {
|
||||
user: Some(Instructions {
|
||||
text: "parent global instructions".to_string(),
|
||||
source: params.spawn_config.codex_home.join("AGENTS.md"),
|
||||
}),
|
||||
..Default::default()
|
||||
}));
|
||||
review_session.reuse_key = GuardianReviewSessionReuseKey::from_spawn_config(
|
||||
¶ms.spawn_config,
|
||||
params.parent_session.user_instructions().await,
|
||||
params.parent_session.inherited_instructions().await,
|
||||
params
|
||||
.parent_session
|
||||
.clone_history()
|
||||
|
||||
@@ -76,7 +76,7 @@ impl ManagedReviewerThreads {
|
||||
environments: Some(context.environments().to_selections()),
|
||||
inherited_environments: Some(context.environments().clone()),
|
||||
user_instructions: Some(LoadedUserInstructions {
|
||||
instructions: parent.user_instructions().await,
|
||||
instructions: parent.inherited_instructions().await.user,
|
||||
warnings: Vec::new(),
|
||||
}),
|
||||
client_mcp_extensions: parent.services.client_mcp_extensions.clone(),
|
||||
|
||||
Reference in new issue
Block a user