mirror of
https://github.com/openai/codex.git
synced 2026-11-16 06:54:36 +00:00
Add a provider for thread-scoped instructions (#44701)
## What changed - Expose `ThreadInstructionsProvider` through `StartThreadOptions`. Load its snapshot at startup and model-request boundaries, composing it after global instructions and before repository instructions. Empty or blank output clears only the thread contribution. - Reject thread instructions exceeding 10,000 estimated tokens independently of the repository instruction budget. Allow host-provided instructions without a filesystem source and rename the shared future type to `LoadInstructionsFuture`. - Retain the provider across warm resumes; require hosts to supply it again for cold resumes and offline forks. Live forks and subagents inherit applied snapshots without inheriting the source thread's provider, including when the parent is unloaded during setup or reload. - Include thread instructions in guardian reviewer inheritance and reuse decisions. ## Testing Add coverage for composition and clearing, refresh within an active turn, size rejection before sampling, cancellation-safe refresh, fork and resume behavior, parent eviction, and reviewer reuse invalidation. GitOrigin-RevId: 7be9a523cbbfd67704067dfd526188dad89a3c88
This commit is contained in:
1 parent
dc55274818
commit
fc948f8c47
21 files changed
+1303
-153
No files matched your search
@@ -222,6 +222,7 @@ pub struct GuardianReviewSessionReuseKey {
|
||||
developer_instructions: Option<String>,
|
||||
base_instructions: Option<String>,
|
||||
user_instructions: Option<Instructions>,
|
||||
thread_instructions: Option<Instructions>,
|
||||
compact_prompt: Option<String>,
|
||||
cwd: PathUri,
|
||||
mcp_servers: Constrained<HashMap<String, McpServerConfig>>,
|
||||
@@ -264,6 +265,7 @@ impl GuardianReviewSessionReuseKey {
|
||||
developer_instructions: spawn_config.developer_instructions.clone(),
|
||||
base_instructions: spawn_config.base_instructions.clone(),
|
||||
user_instructions: instructions.user,
|
||||
thread_instructions: instructions.thread,
|
||||
compact_prompt: spawn_config.compact_prompt.clone(),
|
||||
cwd: PathUri::from_abs_path(&spawn_config.cwd),
|
||||
mcp_servers: spawn_config.mcp_servers.clone(),
|
||||
|
||||
@@ -137,6 +137,7 @@ impl ReviewerSessionFactory for PreparedSession {
|
||||
let inherited = session.inherited_instructions().await;
|
||||
let context = GuardianReviewSessionReuseKey {
|
||||
user_instructions: inherited.user,
|
||||
thread_instructions: inherited.thread,
|
||||
..context
|
||||
};
|
||||
Ok(GuardianReviewSession {
|
||||
|
||||
@@ -282,22 +282,27 @@ async fn spawned_guardian_session_preserves_windows_sandbox_proxy_settings() {
|
||||
#[tokio::test]
|
||||
async fn spawned_guardian_reuse_key_matches_inherited_instructions() {
|
||||
let mut params = test_review_params().await;
|
||||
let latest = Some(Instructions {
|
||||
text: "latest thread instructions".to_string(),
|
||||
source: None,
|
||||
});
|
||||
let latest_global = Some(Instructions {
|
||||
text: "latest global instructions".to_string(),
|
||||
source: params.spawn_config.codex_home.join("AGENTS.md"),
|
||||
source: None,
|
||||
});
|
||||
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(),
|
||||
thread: latest.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"),
|
||||
thread: Some(Instructions {
|
||||
text: "previous thread instructions".to_string(),
|
||||
source: None,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
@@ -305,7 +310,8 @@ async fn spawned_guardian_reuse_key_matches_inherited_instructions() {
|
||||
parent.guardian_context_mode,
|
||||
);
|
||||
let expected_key = GuardianReviewSessionReuseKey {
|
||||
user_instructions: latest_global.clone(),
|
||||
user_instructions: latest_global,
|
||||
thread_instructions: latest.clone(),
|
||||
..stale_key.clone()
|
||||
};
|
||||
let prepared = factory::prepare_review(params)
|
||||
@@ -323,10 +329,7 @@ async fn spawned_guardian_reuse_key_matches_inherited_instructions() {
|
||||
.expect("spawn reviewer after instruction update");
|
||||
|
||||
assert_eq!(review.reuse_key, expected_key);
|
||||
assert_eq!(
|
||||
review.session.inherited_instructions().await.user,
|
||||
latest_global
|
||||
);
|
||||
assert_eq!(review.session.inherited_instructions().await.thread, latest);
|
||||
review.shutdown().await;
|
||||
}
|
||||
|
||||
@@ -394,6 +397,17 @@ async fn guardian_review_session_config_change_invalidates_cached_session() {
|
||||
GuardianContextMode::Legacy,
|
||||
)
|
||||
);
|
||||
assert_ne!(
|
||||
cached_reuse_key.clone(),
|
||||
GuardianReviewSessionReuseKey {
|
||||
thread_instructions: Some(Instructions {
|
||||
text: "updated thread instructions".to_string(),
|
||||
source: None,
|
||||
}),
|
||||
..cached_reuse_key.clone()
|
||||
},
|
||||
"changing thread instructions must invalidate reviewer history"
|
||||
);
|
||||
assert_ne!(
|
||||
cached_reuse_key
|
||||
.clone()
|
||||
@@ -969,7 +983,11 @@ async fn run_review_removes_trunk_when_event_stream_is_broken() {
|
||||
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"),
|
||||
source: None,
|
||||
}),
|
||||
thread: Some(Instructions {
|
||||
text: "parent thread instructions".to_string(),
|
||||
source: None,
|
||||
}),
|
||||
..Default::default()
|
||||
}));
|
||||
|
||||
@@ -5,7 +5,6 @@ use std::sync::Arc;
|
||||
use std::sync::Weak;
|
||||
|
||||
use codex_async_utils::OrCancelExt;
|
||||
use codex_extension_api::LoadedUserInstructions;
|
||||
use codex_history::InitialHistory;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::InternalSessionSource;
|
||||
@@ -75,28 +74,28 @@ impl ManagedReviewerThreads {
|
||||
thread_source: Some(ThreadSource::GuardianReview),
|
||||
environments: Some(context.environments().to_selections()),
|
||||
inherited_environments: Some(context.environments().clone()),
|
||||
user_instructions: Some(LoadedUserInstructions {
|
||||
instructions: parent.inherited_instructions().await.user,
|
||||
warnings: Vec::new(),
|
||||
}),
|
||||
client_mcp_extensions: parent.services.client_mcp_extensions.clone(),
|
||||
..StartThreadOptions::new(config)
|
||||
};
|
||||
let spawned = match history.unwrap_or(InitialHistory::New) {
|
||||
InitialHistory::Forked(history) => manager
|
||||
.fork_internal_session(parent.thread_id(), options, history)
|
||||
.or_cancel(&cancel)
|
||||
.await
|
||||
.map_err(codex_protocol::error::CodexErr::from)??,
|
||||
InitialHistory::New | InitialHistory::Cleared => manager
|
||||
.spawn_internal_session(parent.thread_id(), options)
|
||||
.or_cancel(&cancel)
|
||||
.await
|
||||
.map_err(codex_protocol::error::CodexErr::from)??,
|
||||
let history = match history.unwrap_or(InitialHistory::New) {
|
||||
InitialHistory::Forked(history) => InitialHistory::Forked(history),
|
||||
InitialHistory::New | InitialHistory::Cleared => InitialHistory::New,
|
||||
InitialHistory::Resumed(_) => {
|
||||
anyhow::bail!("guardian review forks cannot resume an existing thread")
|
||||
}
|
||||
};
|
||||
// Generic internal sessions start isolated. Reviewers explicitly inherit
|
||||
// the parent's applied instructions, just like standalone delegates.
|
||||
let spawned = manager
|
||||
.spawn_internal_session_with_history(
|
||||
parent.thread_id(),
|
||||
options,
|
||||
history,
|
||||
Some(parent.inherited_instructions().await),
|
||||
)
|
||||
.or_cancel(&cancel)
|
||||
.await
|
||||
.map_err(codex_protocol::error::CodexErr::from)??;
|
||||
let thread = spawned.thread;
|
||||
let session = Arc::clone(&thread.session);
|
||||
let io = forward_session_io(
|
||||
|
||||
Reference in new issue
Block a user