Files
codex/codex-rs/protocol/src/context_window_id.rs
T
Ruslan Nigmatullin 340a18ed7c Preserve model-facing ID allocations in saved history (#60771)
## Why

Restoring model-facing ID sequences requires allocation ordinals alongside the rendered IDs. Responses API payloads still require string IDs.

## What changed

- Add allocation metadata to `ResponseItemId` while keeping equality, hashing, and ordering based on the rendered ID.
- Preserve optional `id_allocation` metadata in rollout items, inter-agent messages, compaction replacement history, and Guardian checkpoints. Continue accepting legacy string IDs.
- Support structured context-window allocations alongside UUIDv7 strings, validate the `window` salt on restoration, and keep request metadata IDs as strings.

## Testing

Add tests for allocation round trips, legacy ID compatibility, string-only raw response item IDs, and context-window allocation validation.

GitOrigin-RevId: ce1fd964bde9a4e7c527432be99aea87ada30169
2026-11-11 09:58:13 +00:00

58 lines
1.6 KiB
Rust

//! Rollouts store a UUIDv7 string or a window allocation. Request metadata uses its display form.
use codex_utils_model_facing_id::ModelFacingId;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use std::fmt;
use uuid::Uuid;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum ContextWindowId {
Text(String),
Generated(Box<ModelFacingId>),
}
impl ContextWindowId {
pub fn new_uuid_v7() -> Self {
Self::Text(Uuid::now_v7().to_string())
}
pub fn allocation(&self) -> Option<&ModelFacingId> {
match self {
Self::Text(_) => None,
Self::Generated(id) => (id.salt() == "window").then_some(id),
}
}
/// Parses a UUIDv7 window ID from a rollout string.
pub fn parse(value: &str) -> Option<Self> {
Uuid::parse_str(value)
.ok()
.filter(|uuid| uuid.get_version_num() == 7)
.map(|uuid| Self::Text(uuid.to_string()))
}
/// Restore the window without discarding its allocation ordinal.
pub fn validated(&self) -> Option<Self> {
match self {
Self::Text(text) => Self::parse(text),
Self::Generated(_) => self.allocation().map(|_| self.clone()),
}
}
}
impl fmt::Display for ContextWindowId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Text(text) => formatter.write_str(text),
Self::Generated(id) => id.fmt(formatter),
}
}
}
#[cfg(test)]
#[path = "context_window_id_tests.rs"]
mod tests;