mirror of
https://github.com/openai/codex.git
synced 2026-12-01 23:43:06 +00:00
Track window and fork positions in turn metadata (#40987)
## What changed - Add the zero-based `window_number` to Responses turn metadata and include it in prewarm requests. - Persist `forked_from_ordinal_exclusive` separately from the physical history base so fork lineage remains accurate after reverts and cold resumes. - Report context-inheriting subagents through `parent_thread_id` without also emitting fork lineage. - Reserve the new metadata keys while accepting and filtering previously valid configured values so Codex-owned values take precedence. ## Testing - Cover window numbering across compaction, resume, and fork operations. - Cover fork cutoff persistence across direct forks, legacy rollouts, reverts into inherited history, and cold resumes. GitOrigin-RevId: 288f6cd632254814c1f166668b33828082d799a1
This commit is contained in:
1 parent
102ae5e2e6
commit
0d654e6536
33 files changed
+562
-44
No files matched your search
@@ -116,6 +116,7 @@ async fn turn_start_forwards_client_metadata_to_responses_request_v2() -> Result
|
||||
metadata["window_id"].as_str(),
|
||||
request.header("x-codex-window-id").as_deref()
|
||||
);
|
||||
assert_eq!(metadata["window_number"].as_u64(), Some(0));
|
||||
assert!(
|
||||
metadata["context_window_id"]
|
||||
.as_str()
|
||||
@@ -193,6 +194,7 @@ async fn turn_start_sends_fork_lineage_in_turn_metadata_for_thread_fork_v2() ->
|
||||
metadata["forked_from_thread_id"].as_str(),
|
||||
Some(source_thread_id.as_str())
|
||||
);
|
||||
assert!(metadata.get("forked_from_ordinal_exclusive").is_none());
|
||||
assert_eq!(metadata["thread_id"].as_str(), Some(thread.id.as_str()));
|
||||
assert_eq!(metadata["turn_id"].as_str(), Some(turn.id.as_str()));
|
||||
|
||||
|
||||
@@ -1442,6 +1442,19 @@ async fn thread_fork_creates_reference_backed_paginated_thread() -> Result<()> {
|
||||
.find(|request| request.url.path().ends_with("/responses"))
|
||||
.expect("forked turn response request");
|
||||
let request_body = response_request.body_json::<Value>()?;
|
||||
let turn_metadata: Value = serde_json::from_str(
|
||||
request_body["client_metadata"]["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.expect("forked turn metadata"),
|
||||
)?;
|
||||
assert_eq!(
|
||||
turn_metadata["forked_from_thread_id"].as_str(),
|
||||
Some(conversation_id.as_str())
|
||||
);
|
||||
assert_eq!(
|
||||
turn_metadata["forked_from_ordinal_exclusive"].as_u64(),
|
||||
Some(history_base.end_ordinal_exclusive)
|
||||
);
|
||||
let model_input = request_body["input"]
|
||||
.as_array()
|
||||
.expect("response input array");
|
||||
@@ -1702,6 +1715,11 @@ async fn assert_thread_fork_freezes_active_paginated_turn_as_interrupted(
|
||||
.await?;
|
||||
let forked_thread_id = forked_thread.id.clone();
|
||||
let forked_path = forked_thread.path.expect("forked rollout path");
|
||||
let history_base = read_session_meta_line(forked_path.as_path())
|
||||
.await?
|
||||
.meta
|
||||
.history_base
|
||||
.expect("fork history base");
|
||||
let child_rollout = std::fs::read_to_string(forked_path.as_path())?
|
||||
.lines()
|
||||
.map(serde_json::from_str::<RolloutLine>)
|
||||
@@ -1933,6 +1951,19 @@ async fn assert_thread_fork_freezes_active_paginated_turn_as_interrupted(
|
||||
.find(|request| request.url.path().ends_with("/responses"))
|
||||
.expect("cold-resumed model request")
|
||||
.body_json::<Value>()?;
|
||||
let turn_metadata: Value = serde_json::from_str(
|
||||
request_body["client_metadata"]["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.expect("cold-resumed turn metadata"),
|
||||
)?;
|
||||
assert_eq!(
|
||||
turn_metadata["forked_from_thread_id"].as_str(),
|
||||
Some(source_thread_id.as_str())
|
||||
);
|
||||
assert_eq!(
|
||||
turn_metadata["forked_from_ordinal_exclusive"].as_u64(),
|
||||
Some(history_base.end_ordinal_exclusive)
|
||||
);
|
||||
let model_input = request_body["input"].as_array().expect("model input");
|
||||
assert!(model_input.iter().any(|item| {
|
||||
item["role"] == expected_marker_role
|
||||
|
||||
@@ -3766,6 +3766,7 @@ async fn thread_resume_prefers_persisted_git_metadata_for_local_threads() -> Res
|
||||
session_id: conversation_id.into(),
|
||||
id: conversation_id,
|
||||
forked_from_id: None,
|
||||
forked_from_ordinal_exclusive: None,
|
||||
parent_thread_id: None,
|
||||
timestamp: "2025-01-05T12:00:00Z".to_string(),
|
||||
cwd: repo_path.clone(),
|
||||
|
||||
@@ -13,6 +13,8 @@ use codex_app_server_protocol::JSONRPCError;
|
||||
use codex_app_server_protocol::JSONRPCMessage;
|
||||
use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::SortDirection;
|
||||
use codex_app_server_protocol::ThreadForkParams;
|
||||
use codex_app_server_protocol::ThreadForkResponse;
|
||||
use codex_app_server_protocol::ThreadHistoryMode;
|
||||
use codex_app_server_protocol::ThreadItemsListParams;
|
||||
use codex_app_server_protocol::ThreadItemsListResponse;
|
||||
@@ -34,12 +36,170 @@ use codex_protocol::config_types::CollaborationMode;
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::config_types::Settings;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_rollout::RolloutItem;
|
||||
use codex_rollout::RolloutLine;
|
||||
use codex_rollout::read_session_meta_line;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
use tempfile::TempDir;
|
||||
use tokio::time::timeout;
|
||||
|
||||
const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
|
||||
|
||||
#[tokio::test]
|
||||
async fn thread_revert_preserves_fork_cutoff_after_cold_resume() -> Result<()> {
|
||||
let server = create_mock_responses_server_repeating_assistant("Done").await;
|
||||
let codex_home = TempDir::new()?;
|
||||
MockResponsesConfig::new(&server.uri()).write(codex_home.path())?;
|
||||
let mut mcp = TestAppServer::builder()
|
||||
.with_codex_home(codex_home.path())
|
||||
.build()
|
||||
.await?;
|
||||
initialize_experimental(&mut mcp).await?;
|
||||
let ThreadStartResponse { thread: parent, .. } = mcp
|
||||
.start_thread(ThreadStartParams {
|
||||
history_mode: Some(ThreadHistoryMode::Paginated),
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
let mut parent_turns = Vec::new();
|
||||
for text in ["parent first", "parent second"] {
|
||||
let completed = mcp
|
||||
.start_turn_and_wait_for_completion(TurnStartParams {
|
||||
thread_id: parent.id.clone(),
|
||||
input: vec![UserInput::Text {
|
||||
text: text.to_string(),
|
||||
text_elements: Vec::new(),
|
||||
}],
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
parent_turns.push(completed.turn.id);
|
||||
}
|
||||
let ThreadForkResponse { thread: child, .. } = mcp
|
||||
.request(|request_id| ClientRequest::ThreadFork {
|
||||
request_id,
|
||||
params: ThreadForkParams {
|
||||
thread_id: parent.id.clone(),
|
||||
..Default::default()
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
let child_meta = read_session_meta_line(child.path.as_ref().expect("child rollout"))
|
||||
.await?
|
||||
.meta;
|
||||
let fork_cutoff = child_meta
|
||||
.history_base
|
||||
.expect("fork history base")
|
||||
.end_ordinal_exclusive;
|
||||
assert_eq!(child_meta.forked_from_ordinal_exclusive, Some(fork_cutoff));
|
||||
let inherited_revert_cutoff =
|
||||
std::fs::read_to_string(parent.path.as_ref().expect("parent rollout"))?
|
||||
.lines()
|
||||
.map(serde_json::from_str::<RolloutLine>)
|
||||
.collect::<Result<Vec<_>, _>>()?
|
||||
.into_iter()
|
||||
.find_map(|line| match line.item {
|
||||
RolloutItem::EventMsg(EventMsg::TurnStarted(turn))
|
||||
if turn.turn_id == parent_turns[1] =>
|
||||
{
|
||||
line.ordinal
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.expect("inherited turn start ordinal");
|
||||
let mut child_turns = Vec::new();
|
||||
for text in ["child first", "child second"] {
|
||||
let completed = mcp
|
||||
.start_turn_and_wait_for_completion(TurnStartParams {
|
||||
thread_id: child.id.clone(),
|
||||
input: vec![UserInput::Text {
|
||||
text: text.to_string(),
|
||||
text_elements: Vec::new(),
|
||||
}],
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
child_turns.push(completed.turn.id);
|
||||
}
|
||||
|
||||
// First revert within the child, then revert into its inherited parent history.
|
||||
for (before_turn_id, expected_cutoff) in [
|
||||
(child_turns[1].clone(), fork_cutoff),
|
||||
(parent_turns[1].clone(), inherited_revert_cutoff),
|
||||
] {
|
||||
let ThreadRevertResponse {
|
||||
thread: reverted, ..
|
||||
} = mcp
|
||||
.request(|request_id| ClientRequest::ThreadRevert {
|
||||
request_id,
|
||||
params: ThreadRevertParams {
|
||||
thread_id: child.id.clone(),
|
||||
before_turn_id,
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
let meta = read_session_meta_line(reverted.path.as_ref().expect("reverted rollout"))
|
||||
.await?
|
||||
.meta;
|
||||
assert_eq!(meta.forked_from_ordinal_exclusive, Some(expected_cutoff));
|
||||
if expected_cutoff == fork_cutoff {
|
||||
assert!(
|
||||
meta.history_base
|
||||
.expect("child revert base")
|
||||
.end_ordinal_exclusive
|
||||
> fork_cutoff
|
||||
);
|
||||
}
|
||||
|
||||
mcp.shutdown_gracefully().await?;
|
||||
mcp = TestAppServer::builder()
|
||||
.with_codex_home(codex_home.path())
|
||||
.build()
|
||||
.await?;
|
||||
initialize_experimental(&mut mcp).await?;
|
||||
let _: ThreadResumeResponse = mcp
|
||||
.request(|request_id| ClientRequest::ThreadResume {
|
||||
request_id,
|
||||
params: ThreadResumeParams {
|
||||
thread_id: child.id.clone(),
|
||||
..Default::default()
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
mcp.start_turn_and_wait_for_completion(TurnStartParams {
|
||||
thread_id: child.id.clone(),
|
||||
input: vec![UserInput::Text {
|
||||
text: "continue after revert and cold resume".to_string(),
|
||||
text_elements: Vec::new(),
|
||||
}],
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
let requests = server.received_requests().await.expect("response requests");
|
||||
let body = requests
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|request| request.url.path().ends_with("/responses"))
|
||||
.expect("resumed model request")
|
||||
.body_json::<Value>()?;
|
||||
let metadata: Value = serde_json::from_str(
|
||||
body["client_metadata"]["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.expect("turn metadata"),
|
||||
)?;
|
||||
assert_eq!(
|
||||
(
|
||||
metadata["forked_from_thread_id"].as_str(),
|
||||
metadata["forked_from_ordinal_exclusive"].as_u64()
|
||||
),
|
||||
(Some(parent.id.as_str()), Some(expected_cutoff))
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn thread_revert_replaces_paginated_history_before_turn() -> Result<()> {
|
||||
let server = create_mock_responses_server_repeating_assistant("Done").await;
|
||||
|
||||
Reference in new issue
Block a user