Add thread and child retention policies for injected items (#62654)

## Why

The `always` policy ties retention across context windows to inheritance by
spawned agents. Callers need to control those behaviors independently.

## What changed

- `alwaysInThread` carries items across context windows in the current thread
  and excludes them from spawned agents, including full-history forks.
- `alwaysInChildren` passes items still in the parent's context to spawned
  agents and leaves each thread's context-window retention to the usual rules.
- `always` continues to apply in both scopes, including for items saved by
  older versions. Retained tool calls and outputs must be adjacent, with the
  call first and the same retention policy on both items.

## Testing

Extend integration coverage for retention across remote compaction, inheritance
by spawned agents, and injection through the app server.

GitOrigin-RevId: aafede69cfbe149aa52292e6029d248d4bdf6401
This commit is contained in:
Vivian Fang
2026-11-20 02:59:58 +00:00
committed by copyberry
parent e787cf7024
commit a0a30c40e8
12 changed files with 120 additions and 37 deletions
@@ -225,8 +225,11 @@ async fn interrupt_during_remote_compaction_preserves_summary_on_resume() -> Res
Ok(())
}
#[test_case::test_case("always"; "shared")]
#[test_case::test_case("alwaysInThread"; "thread only")]
#[test_case::test_case("alwaysInChildren"; "children only")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn remote_slash_compact_preserves_context_across_windows() -> Result<()> {
async fn remote_slash_compact_preserves_context_across_windows(retention: &str) -> Result<()> {
skip_if_no_network!(Ok(()));
let server = start_mock_server().await;
@@ -275,11 +278,11 @@ async fn remote_slash_compact_preserves_context_across_windows() -> Result<()> {
});
let call: InjectedResponseItem = serde_json::from_value(json!({
"item": {"type": "function_call", "call_id": "release-check", "name": "check_release", "arguments": "{}"},
"retention": "always"
"retention": retention
}))?;
let result: InjectedResponseItem = serde_json::from_value(json!({
"item": {"type": "function_call_output", "call_id": "release-check", "output": "The release candidate passed."},
"retention": "always"
"retention": retention
}))?;
test.codex
.inject_response_items_with_retention(vec![
@@ -325,6 +328,17 @@ async fn remote_slash_compact_preserves_context_across_windows() -> Result<()> {
json!([mail]),
);
assert!(
requests[1]
.function_call_output_text("release-check")
.is_some()
);
if retention == "alwaysInChildren" {
for request in &requests[2..] {
assert!(request.function_call_output_text("release-check").is_none());
}
return Ok(());
}
insta::assert_snapshot!(
"remote_slash_compact",
context_snapshot::format_context_snapshot(
@@ -5,6 +5,7 @@ use codex_exec_server::CreateDirectoryOptions;
use codex_protocol::protocol::EnvironmentConfigState;
use codex_protocol::protocol::TurnEnvironmentRequests;
use codex_protocol::protocol::TurnEnvironmentSelection;
use codex_protocol::turn_input::InjectedResponseItem;
use core_test_support::submit_thread_settings;
use core_test_support::test_codex::environment_config_for_selection;
use pretty_assertions::assert_eq;
@@ -196,6 +197,18 @@ async fn spawn_inherits_captured_settings_after_a_turn_update(
.await?;
configured[0].config = EnvironmentConfigState::Ready(config);
}
let instruction = |text: &str, retention: &str| {
serde_json::from_value::<InjectedResponseItem>(json!({
"item": {"type": "message", "role": "developer", "content": [{"type": "input_text", "text": text}]},
"retention": retention,
}))
};
test.codex
.inject_response_items_with_retention(vec![
instruction("Send progress updates.", "alwaysInThread")?,
instruction("Read the worker log.", "alwaysInChildren")?,
])
.await?;
answer_paused_turn(&test.codex, &request.turn_id).await?;
let child_thread_id = tokio::time::timeout(
@@ -215,6 +228,13 @@ async fn spawn_inherits_captured_settings_after_a_turn_update(
.iter()
.find(|request| request.header("thread-id") == Some(child_thread_id.to_string()))
.expect("child should make a model request");
assert!(
requests
.iter()
.any(|request| request.body_contains_text("Send progress updates."))
);
assert!(!child_request.body_contains_text("Send progress updates."));
assert!(child_request.body_contains_text("Read the worker log."));
assert_eq!(
request_settings(child_request),
json!({