mirror of
https://github.com/openai/codex.git
synced 2026-06-01 19:02:59 +00:00
[codex] add compaction metadata to turn headers (#24368)
## Summary
- Add `request_kind` values for foreground turn, startup prewarm,
compaction, and detached memory model requests.
- Attach compaction dispatch metadata to local Responses, legacy
`/v1/responses/compact`, and remote v2 compact requests.
- Add the existing logical context-window identifier as `window_id` on
turn-owned model request metadata.
- Keep identity fields optional for detached memory requests, while
still emitting `request_kind="memory"` in non-git/no-sandbox workspaces.
## Root Cause
`x-codex-turn-metadata` has more than one producer. Foreground turns and
compaction requests own a real turn and should carry that turn identity.
Detached memory stage-one requests do not own a foreground turn, so
absent identity fields are valid rather than missing data. Startup
websocket prewarm is also a model request, but it has `generate=false`
and must not be counted as a foreground turn.
`thread_source` or session source identifies where a thread came from
(for example review, guardian, or another subagent). `request_kind`
identifies what the current outbound model request is doing (`turn`,
`prewarm`, `compaction`, or `memory`). A review or guardian thread can
issue either a normal turn request or a compaction request, so source
cannot replace request kind.
## Behavior / Impact
- Ordinary foreground requests send `request_kind="turn"`, their real
identity fields, and `window_id="<thread_id>:<window_generation>"`.
- Startup websocket warmup requests send `request_kind="prewarm"` so
they are not counted as foreground turns.
- Compaction requests send `request_kind="compaction"`, their real
owning turn identity, the existing `window_id`, and
`compaction.{trigger,reason,implementation,phase,strategy}`.
- Detached memory stage-one requests send `request_kind="memory"`
without `session_id`, `thread_id`, `turn_id`, or `window_id`; when no
workspace metadata exists, the kind-only header is still emitted.
- `session_id`, `thread_id`, `turn_id`, and `window_id` remain optional
in the header schema because detached memory requests do not own a
foreground turn or context window.
- `window_id` is not a new ID system: it is copied from the already-sent
`x-codex-window-id` / WS client metadata value at model-request dispatch
time.
- Existing `x-codex-window-id` HTTP/WS emission, value format,
generation advancement, resume behavior, and fork reset behavior are
unchanged.
- `request_kind`, `window_id`, and upstream turn-owned identity fields
remain schema-owned; input `responsesapi_client_metadata` cannot replace
their canonical values.
- No table, DAG, export, app-server API, or MCP `_meta` schema changes
are included.
A compaction attempt stopped by a pre-compact hook issues no model
request and therefore has no request header; its outcome remains in
analytics events. Status, error, duration, and token deltas also remain
analytics fields rather than request-header fields.
Future detached-memory attribution using a real initiating turn ID as
`trigger_turn_id` is intentionally not part of this PR.
## Sync With Main
- Final pushed head `716342e79` is rebased onto `origin/main@0d37db4b2`.
- The metadata conflict came from upstream `#24160`, which added
`forked_from_thread_id` on the same `turn_metadata` surface. Resolution
preserves that field and its protection from client metadata override
alongside this PR's request-kind, compaction, and window-id fields.
- While resolving the overlapping commits, I removed an accidental
recursive model-request overlay and a duplicate detached-memory header
builder before completing the rebase.
## Latency / User Experience Boundary
- Foreground turns perform no new filesystem, git, or network work. New
fields are inserted into metadata already serialized for outgoing
requests.
- Compaction issues the same model/HTTP requests with the same prompt,
model, service tier, and sampling settings; only metadata bytes change.
- Startup prewarm already sent metadata; it is now correctly classified
as `prewarm`.
- Non-git detached memory now sends a small kind-only metadata header
rather than no header.
- This client diff adds no user-visible latency mechanism beyond
negligible serialization and header bytes on already-existing requests.
## Validation
On conflict-resolved head `1d35c2cfb` based on `origin/main@487521733`:
- `just fmt` (passed)
- `just fix -p codex-core` (passed)
- `git diff --check origin/main...HEAD` (passed)
- `just test -p codex-core -E 'test(turn_metadata) |
test(websocket_first_turn_uses_startup_prewarm_and_create) |
test(responses_stream_includes_turn_metadata_header_for_git_workspace_e2e)
|
test(responses_websocket_forwards_turn_metadata_on_initial_and_incremental_create)
| test(remote_compact_v2_retries_failures_with_stream_retry_budget) |
test(window_id_advances_after_compact_persists_on_resume_and_resets_on_fork)'`
(`23 passed`; `bench-smoke` passed)
- `just test -p codex-app-server -E
'test(turn_start_forwards_client_metadata_to_responses_request_v2) |
test(turn_start_forwards_client_metadata_to_responses_websocket_request_body_v2)
| test(auto_compaction_remote_emits_started_and_completed_items)'` (`3
passed`; `bench-smoke` passed)
- `just test -p codex-memories-write` (`29 passed`; `bench-smoke`
passed)
This commit is contained in:
@@ -95,6 +95,16 @@ async fn websocket_first_turn_uses_startup_prewarm_and_create() -> Result<()> {
|
||||
let turn = connection.get(1).expect("missing turn request").body_json();
|
||||
assert_eq!(warmup["type"].as_str(), Some("response.create"));
|
||||
assert_eq!(warmup["generate"].as_bool(), Some(false));
|
||||
let warmup_metadata: Value = serde_json::from_str(
|
||||
warmup["client_metadata"]["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.expect("warmup turn metadata"),
|
||||
)?;
|
||||
assert_eq!(warmup_metadata["request_kind"].as_str(), Some("prewarm"));
|
||||
assert_eq!(
|
||||
warmup_metadata["window_id"].as_str(),
|
||||
warmup["client_metadata"]["x-codex-window-id"].as_str()
|
||||
);
|
||||
assert!(
|
||||
turn["tools"]
|
||||
.as_array()
|
||||
@@ -102,6 +112,12 @@ async fn websocket_first_turn_uses_startup_prewarm_and_create() -> Result<()> {
|
||||
"expected request tools to be populated"
|
||||
);
|
||||
assert_eq!(turn["type"].as_str(), Some("response.create"));
|
||||
let turn_metadata: Value = serde_json::from_str(
|
||||
turn["client_metadata"]["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.expect("turn metadata"),
|
||||
)?;
|
||||
assert_eq!(turn_metadata["request_kind"].as_str(), Some("turn"));
|
||||
|
||||
server.shutdown().await;
|
||||
Ok(())
|
||||
|
||||
@@ -2788,11 +2788,58 @@ async fn manual_compact_twice_preserves_latest_user_messages() {
|
||||
contains_user_text(&requests[1], first_user_message),
|
||||
"first compact request should include history before compaction"
|
||||
);
|
||||
let compact_metadata: Value = serde_json::from_str(
|
||||
&requests[1]
|
||||
.header("x-codex-turn-metadata")
|
||||
.expect("local compact request should include turn metadata"),
|
||||
)
|
||||
.expect("local compact turn metadata should be valid json");
|
||||
assert_eq!(
|
||||
compact_metadata["request_kind"].as_str(),
|
||||
Some("compaction")
|
||||
);
|
||||
assert_eq!(
|
||||
compact_metadata["window_id"].as_str(),
|
||||
requests[1].header("x-codex-window-id").as_deref()
|
||||
);
|
||||
assert_eq!(
|
||||
compact_metadata["compaction"],
|
||||
json!({
|
||||
"trigger": "manual",
|
||||
"reason": "user_requested",
|
||||
"implementation": "responses",
|
||||
"phase": "standalone_turn",
|
||||
"strategy": "memento",
|
||||
})
|
||||
);
|
||||
|
||||
assert!(
|
||||
contains_user_text(&requests[2], second_user_message),
|
||||
"second turn request missing second user message"
|
||||
);
|
||||
let next_turn_metadata: Value = serde_json::from_str(
|
||||
&requests[2]
|
||||
.header("x-codex-turn-metadata")
|
||||
.expect("next regular request should include turn metadata"),
|
||||
)
|
||||
.expect("next regular turn metadata should be valid json");
|
||||
assert_eq!(
|
||||
next_turn_metadata["request_kind"].as_str(),
|
||||
Some("turn"),
|
||||
"regular requests after compaction should remain turn requests"
|
||||
);
|
||||
assert_eq!(
|
||||
next_turn_metadata["window_id"].as_str(),
|
||||
requests[2].header("x-codex-window-id").as_deref()
|
||||
);
|
||||
assert_ne!(
|
||||
compact_metadata["window_id"], next_turn_metadata["window_id"],
|
||||
"the next request should use the new compacted context window"
|
||||
);
|
||||
assert!(
|
||||
next_turn_metadata.get("compaction").is_none(),
|
||||
"regular requests after compaction should not be marked as compact requests"
|
||||
);
|
||||
assert!(
|
||||
contains_user_text(&requests[2], first_user_message),
|
||||
"second turn request should include the compacted user history"
|
||||
|
||||
@@ -368,6 +368,36 @@ async fn remote_compact_replaces_history_for_followups() -> Result<()> {
|
||||
compact_request.header("thread-id").as_deref(),
|
||||
Some(thread_id.as_str())
|
||||
);
|
||||
let compact_metadata: Value = serde_json::from_str(
|
||||
&compact_request
|
||||
.header("x-codex-turn-metadata")
|
||||
.expect("remote compact request should include turn metadata"),
|
||||
)
|
||||
.expect("remote compact turn metadata should be valid json");
|
||||
assert!(
|
||||
compact_metadata["turn_id"]
|
||||
.as_str()
|
||||
.is_some_and(|id| !id.is_empty()),
|
||||
"remote compact turn metadata should include its turn id"
|
||||
);
|
||||
assert_eq!(
|
||||
compact_metadata["request_kind"].as_str(),
|
||||
Some("compaction")
|
||||
);
|
||||
assert_eq!(
|
||||
compact_metadata["window_id"].as_str(),
|
||||
compact_request.header("x-codex-window-id").as_deref()
|
||||
);
|
||||
assert_eq!(
|
||||
compact_metadata["compaction"],
|
||||
json!({
|
||||
"trigger": "manual",
|
||||
"reason": "user_requested",
|
||||
"implementation": "responses_compact",
|
||||
"phase": "standalone_turn",
|
||||
"strategy": "memento",
|
||||
})
|
||||
);
|
||||
let compact_body = compact_request.body_json();
|
||||
assert_eq!(
|
||||
compact_body.get("model").and_then(|v| v.as_str()),
|
||||
@@ -375,6 +405,16 @@ async fn remote_compact_replaces_history_for_followups() -> Result<()> {
|
||||
);
|
||||
let response_requests = responses_mock.requests();
|
||||
let first_response_request = response_requests.first().expect("initial request missing");
|
||||
let first_response_metadata: Value = serde_json::from_str(
|
||||
&first_response_request
|
||||
.header("x-codex-turn-metadata")
|
||||
.expect("initial request should include turn metadata"),
|
||||
)
|
||||
.expect("initial turn metadata should be valid json");
|
||||
assert_ne!(
|
||||
first_response_metadata["turn_id"], compact_metadata["turn_id"],
|
||||
"manual compaction should use its own turn id"
|
||||
);
|
||||
assert_eq!(
|
||||
compact_body["tools"],
|
||||
first_response_request.body_json()["tools"],
|
||||
@@ -407,6 +447,33 @@ async fn remote_compact_replaces_history_for_followups() -> Result<()> {
|
||||
|
||||
let response_requests = responses_mock.requests();
|
||||
let follow_up_request = response_requests.last().expect("follow-up request missing");
|
||||
let follow_up_metadata: Value = serde_json::from_str(
|
||||
&follow_up_request
|
||||
.header("x-codex-turn-metadata")
|
||||
.expect("follow-up request should include turn metadata"),
|
||||
)
|
||||
.expect("follow-up turn metadata should be valid json");
|
||||
assert_eq!(
|
||||
follow_up_metadata["request_kind"].as_str(),
|
||||
Some("turn"),
|
||||
"regular requests after compaction should remain turn requests"
|
||||
);
|
||||
assert!(
|
||||
follow_up_metadata.get("compaction").is_none(),
|
||||
"regular requests after compaction should not be marked as compact requests"
|
||||
);
|
||||
assert_ne!(
|
||||
follow_up_metadata["turn_id"], compact_metadata["turn_id"],
|
||||
"the following user turn should not reuse a manual compact turn id"
|
||||
);
|
||||
assert_eq!(
|
||||
follow_up_metadata["window_id"].as_str(),
|
||||
follow_up_request.header("x-codex-window-id").as_deref()
|
||||
);
|
||||
assert_ne!(
|
||||
follow_up_metadata["window_id"], compact_metadata["window_id"],
|
||||
"the following user turn should use the new compacted context window"
|
||||
);
|
||||
let follow_up_body = follow_up_request.body_json().to_string();
|
||||
assert!(
|
||||
follow_up_body.contains("\"type\":\"compaction\""),
|
||||
@@ -797,6 +864,30 @@ async fn remote_compact_v2_reuses_compaction_trigger_for_followups() -> Result<(
|
||||
"expected compact request to advertise the remote_compaction_v2 beta feature"
|
||||
);
|
||||
assert_eq!(compact_request.path(), "/v1/responses");
|
||||
let compact_metadata: Value = serde_json::from_str(
|
||||
&compact_request
|
||||
.header("x-codex-turn-metadata")
|
||||
.expect("v2 compact request should include turn metadata"),
|
||||
)
|
||||
.expect("v2 compact turn metadata should be valid json");
|
||||
assert_eq!(
|
||||
compact_metadata["request_kind"].as_str(),
|
||||
Some("compaction")
|
||||
);
|
||||
assert_eq!(
|
||||
compact_metadata["window_id"].as_str(),
|
||||
compact_request.header("x-codex-window-id").as_deref()
|
||||
);
|
||||
assert_eq!(
|
||||
compact_metadata["compaction"],
|
||||
json!({
|
||||
"trigger": "manual",
|
||||
"reason": "user_requested",
|
||||
"implementation": "responses_compaction_v2",
|
||||
"phase": "standalone_turn",
|
||||
"strategy": "memento",
|
||||
})
|
||||
);
|
||||
let compact_body = compact_request.body_json().to_string();
|
||||
assert!(
|
||||
compact_body.contains("\"type\":\"compaction_trigger\""),
|
||||
@@ -1139,7 +1230,7 @@ async fn remote_compact_runs_automatically() -> Result<()> {
|
||||
let session_id = harness.test().session_configured.session_id.to_string();
|
||||
let thread_id = harness.test().session_configured.thread_id.to_string();
|
||||
|
||||
mount_sse_once(
|
||||
let initial_request = mount_sse_once(
|
||||
harness.server(),
|
||||
sse(vec![
|
||||
responses::ev_shell_command_call("m1", "echo 'hi'"),
|
||||
@@ -1195,7 +1286,63 @@ async fn remote_compact_runs_automatically() -> Result<()> {
|
||||
compact_mock.single_request().header("thread-id").as_deref(),
|
||||
Some(thread_id.as_str())
|
||||
);
|
||||
let compact_metadata: Value = serde_json::from_str(
|
||||
&compact_mock
|
||||
.single_request()
|
||||
.header("x-codex-turn-metadata")
|
||||
.expect("auto remote compact request should include turn metadata"),
|
||||
)
|
||||
.expect("auto remote compact turn metadata should be valid json");
|
||||
assert_eq!(
|
||||
compact_metadata["request_kind"].as_str(),
|
||||
Some("compaction")
|
||||
);
|
||||
assert_eq!(
|
||||
compact_metadata["compaction"],
|
||||
json!({
|
||||
"trigger": "auto",
|
||||
"reason": "context_limit",
|
||||
"implementation": "responses_compact",
|
||||
"phase": "mid_turn",
|
||||
"strategy": "memento",
|
||||
})
|
||||
);
|
||||
let initial_metadata: Value = serde_json::from_str(
|
||||
&initial_request
|
||||
.single_request()
|
||||
.header("x-codex-turn-metadata")
|
||||
.expect("initial request should include turn metadata"),
|
||||
)
|
||||
.expect("initial turn metadata should be valid json");
|
||||
assert_eq!(
|
||||
initial_metadata["turn_id"], compact_metadata["turn_id"],
|
||||
"automatic mid-turn compaction should keep the current turn id"
|
||||
);
|
||||
assert_eq!(
|
||||
initial_metadata["window_id"], compact_metadata["window_id"],
|
||||
"automatic mid-turn compaction summarizes the current context window"
|
||||
);
|
||||
let follow_up_request = responses_mock.single_request();
|
||||
let follow_up_metadata: Value = serde_json::from_str(
|
||||
&follow_up_request
|
||||
.header("x-codex-turn-metadata")
|
||||
.expect("post-compaction continuation should include turn metadata"),
|
||||
)
|
||||
.expect("post-compaction turn metadata should be valid json");
|
||||
assert_eq!(
|
||||
follow_up_metadata["request_kind"].as_str(),
|
||||
Some("turn"),
|
||||
"post-compaction continuation should be a regular request"
|
||||
);
|
||||
assert!(follow_up_metadata.get("compaction").is_none());
|
||||
assert_eq!(
|
||||
follow_up_metadata["turn_id"], compact_metadata["turn_id"],
|
||||
"automatic mid-turn continuation should keep the current turn id"
|
||||
);
|
||||
assert_ne!(
|
||||
follow_up_metadata["window_id"], compact_metadata["window_id"],
|
||||
"post-compaction continuation should use the next context window"
|
||||
);
|
||||
let follow_up_body = follow_up_request.body_json().to_string();
|
||||
assert!(follow_up_body.contains("REMOTE_COMPACTED_SUMMARY"));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user