Include context window IDs in response metadata (#39847)

## What changed

- Add `context_window_id` to turn metadata for sampling and compaction requests, using the UUID for the model-visible context window.
- Keep the ID stable for requests in the same context window and advance it after compaction, independently of the existing thread-generation `window_id`.
- Reserve `context_window_id` so client metadata cannot override it.

## Testing

- Cover HTTP and WebSocket metadata, remote compaction, token-budget context alignment, resume behavior, and post-compaction window changes.

GitOrigin-RevId: 55cceea377ad509c8dd96ad668f2b5703ea46a63
This commit is contained in:
Brandon Zhang authored and copyberry committed 2026-08-21 03:31:56 +00:00
1 parent bd19459358
commit 27c05a52e0
14 files changed
+151 -32

No files matched your search

+14 -6
View File
@@ -1,6 +1,7 @@
use super::*;
use crate::responses_metadata::AUTO_REVIEW_ENABLED_KEY;
use crate::responses_metadata::CONTEXT_WINDOW_ID_KEY;
use crate::responses_metadata::CodexResponsesRequestKind;
use crate::responses_metadata::CompactionTurnMetadata;
use crate::responses_metadata::INSTALLATION_ID_KEY;
@@ -738,6 +739,10 @@ fn turn_metadata_state_merges_client_metadata_without_replacing_reserved_fields(
),
("turn_id".to_string(), "client-supplied".to_string()),
(WINDOW_ID_KEY.to_string(), "client-supplied".to_string()),
(
CONTEXT_WINDOW_ID_KEY.to_string(),
"client-supplied".to_string(),
),
("thread_source".to_string(), "client-supplied".to_string()),
("request_kind".to_string(), "client-supplied".to_string()),
(
@@ -819,6 +824,7 @@ fn turn_metadata_state_merges_client_metadata_without_replacing_reserved_fields(
assert_eq!(json["turn_id"].as_str(), Some("turn-a"));
assert!(json.get("request_kind").is_none());
assert!(json.get(WINDOW_ID_KEY).is_none());
assert!(json.get(CONTEXT_WINDOW_ID_KEY).is_none());
assert_eq!(
json["turn_started_at_unix_ms"].as_i64(),
Some(1_700_000_000_123)
@@ -954,12 +960,14 @@ fn turn_metadata_state_overlays_compaction_only_on_compaction_requests() {
#[test]
fn responses_api_metadata_rejects_reserved_keys() {
assert_eq!(
validate_extra_metadata(
BTreeMap::from([("thread_source".to_string(), "sdk".to_string())]).iter()
),
Err("responses_api_metadata contains a reserved key")
);
for reserved_key in ["thread_source", WINDOW_ID_KEY, CONTEXT_WINDOW_ID_KEY] {
assert_eq!(
validate_extra_metadata(
BTreeMap::from([(reserved_key.to_string(), "sdk".to_string())]).iter()
),
Err("responses_api_metadata contains a reserved key")
);
}
}
#[tokio::test]