Files
codex/codex-rs/protocol/src/response_item_id_tests.rs
T
Ruslan Nigmatullin 583ec348cf Centralize thread context ID allocation (#62633)
## Why

Response item IDs and context window IDs use separate generator callbacks.
A shared allocator gives history and inference a single API for both.

## What changed

Use `ThreadContextIds` to allocate response item IDs, retained-context
revisions, and compaction window IDs. Move initial window selection into
`InitialHistory` so resumed threads reuse a valid window ID from their
rollout. Give rollout reconstruction its own allocator.

Assign hook prompt IDs through the session's normal response item
recording path.

## Testing

Move the prefixed UUIDv7 item ID test to the allocator crate and add
coverage for JSON escaping in Guardian's item ID token budget.

GitOrigin-RevId: 31232f50648382033506d1f86815ce7c1de78378
2026-11-20 00:44:31 +00:00

70 lines
2.3 KiB
Rust

use pretty_assertions::assert_eq;
use serde_json::json;
use super::ResponseItemId;
use crate::models::ResponseItem;
#[test]
fn creates_prefixed_ids_with_explicit_suffix() {
let id = ResponseItemId::with_suffix("msg", "test");
assert_eq!(id.as_str(), "msg_test");
assert_eq!(id.to_string(), "msg_test");
assert_eq!(String::from(id), "msg_test");
}
#[test]
fn accepts_server_ids_verbatim() {
let id = ResponseItemId::from_server("legacy-id".to_string());
assert_eq!(id.as_str(), "legacy-id");
}
#[test]
fn deserializes_arbitrary_ids_as_strings() {
let id: ResponseItemId = serde_json::from_str("\"legacy-id\"").expect("id");
assert_eq!(id.as_str(), "legacy-id");
assert_eq!(
serde_json::to_string(&id).expect("serialized id"),
"\"legacy-id\""
);
}
#[test]
fn structured_ids_round_trip_but_raw_items_only_accept_strings() {
let model_id = serde_json::from_value(json!({"salt":"item","ordinal":7,"value":42})).unwrap();
let id = ResponseItemId::generated("msg", model_id);
let mut stored = serde_json::to_value(id).unwrap();
assert_eq!(
stored,
json!({"id":"msg_item042","id_allocation":{
"prefix":"msg","salt":"item","ordinal":7,"value":42
}})
);
stored["id"] = json!("msg_item042_2");
stored["id_allocation"]["suffix"] = json!("_2");
let id: ResponseItemId = serde_json::from_value(stored.clone()).unwrap();
assert_eq!(id.as_str(), "msg_item042_2");
assert_eq!(serde_json::to_value(&id).unwrap(), stored);
let mut raw = json!({"type": "message", "role": "assistant", "content": [], "id": stored});
assert!(serde_json::from_value::<ResponseItem>(raw.clone()).is_err());
raw["id"] = json!(id.as_str());
let item: ResponseItem = serde_json::from_value(raw.clone()).unwrap();
assert_eq!(item.id().unwrap().allocation(), None);
assert_eq!(serde_json::to_value(item).unwrap(), raw);
}
#[test]
fn recognizes_non_empty_prefix_and_suffix() {
for (value, expected) in [
("msg_test", true),
("legacy-id", false),
("", false),
("_test", false),
("msg_", false),
] {
let id: ResponseItemId =
serde_json::from_value(value.into()).expect("ID should deserialize");
assert_eq!(id.is_prefixed(), expected, "{value}");
}
}