mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +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
@@ -53,6 +53,103 @@ use wiremock::MockServer;
|
||||
|
||||
const TEST_INSTALLATION_ID: &str = "11111111-1111-4111-8111-111111111111";
|
||||
|
||||
struct ParentInstructionsProvider(codex_extension_api::Instructions);
|
||||
|
||||
impl codex_extension_api::UserInstructionsProvider for ParentInstructionsProvider {
|
||||
fn load_user_instructions(&self) -> codex_extension_api::LoadInstructionsFuture<'_> {
|
||||
Box::pin(async move {
|
||||
codex_extension_api::LoadedUserInstructions {
|
||||
instructions: Some(self.0.clone()),
|
||||
warnings: Vec::new(),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl codex_extension_api::ThreadInstructionsProvider for ParentInstructionsProvider {
|
||||
fn load_thread_instructions(&self) -> codex_extension_api::LoadInstructionsFuture<'_> {
|
||||
codex_extension_api::UserInstructionsProvider::load_user_instructions(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn live_fork_keeps_instructions_when_source_is_unloaded_during_setup() {
|
||||
let temp_dir = tempdir().expect("tempdir");
|
||||
let mut config = test_config().await;
|
||||
config.codex_home = temp_dir.path().join("codex-home").abs();
|
||||
config.cwd = config.codex_home.abs();
|
||||
std::fs::create_dir_all(&config.codex_home).expect("create codex home");
|
||||
let global = codex_extension_api::Instructions {
|
||||
text: "global instructions".to_string(),
|
||||
source: None,
|
||||
};
|
||||
let thread = codex_extension_api::Instructions {
|
||||
text: "source thread instructions".to_string(),
|
||||
source: None,
|
||||
};
|
||||
let mut manager = ThreadManager::with_models_provider_and_home_for_tests(
|
||||
CodexAuth::from_api_key("dummy"),
|
||||
config.model_provider.clone(),
|
||||
config.codex_home.to_path_buf(),
|
||||
Arc::new(codex_exec_server::EnvironmentManager::default_for_tests()),
|
||||
);
|
||||
Arc::get_mut(&mut manager.state)
|
||||
.expect("unshared manager")
|
||||
.user_instructions_provider = Arc::new(ParentInstructionsProvider(global.clone()));
|
||||
let source = manager
|
||||
.start_thread(StartThreadOptions {
|
||||
environments: Some(Vec::new()),
|
||||
thread_instructions_provider: Some(Arc::new(ParentInstructionsProvider(
|
||||
thread.clone(),
|
||||
))),
|
||||
..StartThreadOptions::new(config.clone())
|
||||
})
|
||||
.await
|
||||
.expect("start source");
|
||||
let history = InitialHistory::Resumed(ResumedHistory {
|
||||
conversation_id: source.thread_id,
|
||||
history: Arc::new(Vec::new()),
|
||||
rollout_path: None,
|
||||
});
|
||||
|
||||
// Queue removal between the first source lookup and subsequent startup
|
||||
// lookups. Tokio's fair RwLock makes this ordering deterministic.
|
||||
let fork = manager.fork_thread_from_history(
|
||||
ForkSnapshot::Interrupted,
|
||||
StartThreadOptions {
|
||||
environments: Some(Vec::new()),
|
||||
..StartThreadOptions::new(config)
|
||||
},
|
||||
history,
|
||||
);
|
||||
tokio::pin!(fork);
|
||||
{
|
||||
let _guard = manager.state.threads.write().await;
|
||||
let mut context = std::task::Context::from_waker(std::task::Waker::noop());
|
||||
assert!(std::future::Future::poll(fork.as_mut(), &mut context).is_pending());
|
||||
}
|
||||
let removal = manager.remove_thread_if_matches(&source.thread_id, &source.thread);
|
||||
tokio::pin!(removal);
|
||||
assert!(futures::poll!(&mut removal).is_pending());
|
||||
assert!(futures::poll!(&mut fork).is_pending());
|
||||
assert!(removal.await.is_some());
|
||||
let fork = fork.await.expect("fork survives source removal");
|
||||
let instructions = fork.thread.session.inherited_instructions().await;
|
||||
assert_eq!(
|
||||
(instructions.user, instructions.thread),
|
||||
(Some(global), Some(thread))
|
||||
);
|
||||
source
|
||||
.thread
|
||||
.shutdown_and_wait()
|
||||
.await
|
||||
.expect("shutdown source");
|
||||
fork.thread
|
||||
.shutdown_and_wait()
|
||||
.await
|
||||
.expect("shutdown fork");
|
||||
}
|
||||
|
||||
/// A thread opt-out wins over a shared client without disabling its siblings.
|
||||
#[tokio::test]
|
||||
async fn thread_analytics_opt_out_overrides_shared_client() {
|
||||
@@ -1149,19 +1246,6 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history
|
||||
}
|
||||
}
|
||||
|
||||
struct ParentInstructionsProvider(codex_extension_api::Instructions);
|
||||
|
||||
impl codex_extension_api::UserInstructionsProvider for ParentInstructionsProvider {
|
||||
fn load_user_instructions(&self) -> codex_extension_api::LoadUserInstructionsFuture<'_> {
|
||||
Box::pin(async move {
|
||||
codex_extension_api::LoadedUserInstructions {
|
||||
instructions: Some(self.0.clone()),
|
||||
warnings: Vec::new(),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let temp_dir = tempdir().expect("tempdir");
|
||||
let mut config = test_config().await;
|
||||
config.codex_home = temp_dir.path().join("codex-home").abs();
|
||||
@@ -1197,7 +1281,7 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history
|
||||
|
||||
let parent_instructions = codex_extension_api::Instructions {
|
||||
text: "parent user instructions must not be inherited".to_string(),
|
||||
source: config.codex_home.join("AGENTS.md"),
|
||||
source: Some(config.codex_home.join("AGENTS.md")),
|
||||
};
|
||||
let mut manager = ThreadManager::with_models_provider_and_home_for_tests(
|
||||
CodexAuth::from_api_key("dummy"),
|
||||
@@ -1219,11 +1303,12 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history
|
||||
Arc::clone(&manager_state.extensions),
|
||||
manager_state.mcp_manager.codex_apps_tools_cache(),
|
||||
));
|
||||
manager_state.user_instructions_provider =
|
||||
Arc::new(ParentInstructionsProvider(parent_instructions.clone()));
|
||||
let parent_provider = Arc::new(ParentInstructionsProvider(parent_instructions.clone()));
|
||||
manager_state.user_instructions_provider = parent_provider.clone();
|
||||
let parent = manager
|
||||
.start_thread(StartThreadOptions {
|
||||
metrics_service_name: Some("codex_work_desktop".to_string()),
|
||||
thread_instructions_provider: Some(parent_provider),
|
||||
..StartThreadOptions::new(config.clone())
|
||||
})
|
||||
.await
|
||||
@@ -1232,9 +1317,10 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history
|
||||
.thread
|
||||
.session
|
||||
.set_multi_agent_version_if_unset(MultiAgentVersion::V2);
|
||||
let parent_snapshot = parent.thread.session.inherited_instructions().await;
|
||||
assert_eq!(
|
||||
parent.thread.session.inherited_instructions().await.user,
|
||||
Some(parent_instructions)
|
||||
(parent_snapshot.user, parent_snapshot.thread),
|
||||
(Some(parent_instructions.clone()), Some(parent_instructions))
|
||||
);
|
||||
assert_eq!(
|
||||
parent
|
||||
@@ -1317,14 +1403,10 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history
|
||||
Some(parent.thread_id)
|
||||
);
|
||||
assert_eq!(reviewer.session_configured.forked_from_id, None);
|
||||
assert!(
|
||||
reviewer
|
||||
.thread
|
||||
.session
|
||||
.inherited_instructions()
|
||||
.await
|
||||
.user
|
||||
.is_none()
|
||||
let reviewer_snapshot = reviewer.thread.session.inherited_instructions().await;
|
||||
assert_eq!(
|
||||
(reviewer_snapshot.user, reviewer_snapshot.thread),
|
||||
(None, None)
|
||||
);
|
||||
assert!(
|
||||
reviewer
|
||||
|
||||
Reference in new issue
Block a user