Files
codex/codex-rs/history/src/initial_context_ids.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

54 lines
2.1 KiB
Rust

//! Initializes the thread allocator and active window from rollout history.
use codex_protocol::ContextWindowId;
use codex_thread_context_ids::ThreadContextIds;
use crate::InitialHistory;
use crate::RolloutItem;
/// Ready-to-use allocator and the window it starts or resumes.
pub struct InitialContextIds {
pub context_ids: ThreadContextIds,
pub initial_window_id: ContextWindowId,
}
impl InitialHistory {
/// Initializes the allocator and active window from the history the thread will replay.
/// On resume, use the snapshot validated by the rollout writer. For fixtures use
/// `codex_thread_context_ids_test_support::thread_context_ids()`.
pub fn initialize_context_ids(&self) -> InitialContextIds {
// A resume already owns its window.
let resumed_window = match self {
InitialHistory::Resumed(resumed) => {
resumed
.history
.iter()
.rev()
.find_map(|record| match record {
RolloutItem::Compacted(checkpoint) => {
checkpoint.window_id.as_ref()?.validated()
}
RolloutItem::SessionMeta(meta)
if meta.meta.id == resumed.conversation_id =>
{
meta.meta.context_window.as_ref()?.window_id.validated()
}
_ => None,
})
}
InitialHistory::New | InitialHistory::Cleared | InitialHistory::Forked(_) => None,
};
let context_ids = context_ids_from_rollout(self.get_rollout_items());
let initial_window_id = resumed_window.unwrap_or_else(|| context_ids.next_window());
InitialContextIds {
context_ids,
initial_window_id,
}
}
}
/// Creates an allocator without allocating a window, for isolated rollout reconstruction.
pub fn context_ids_from_rollout(_records: &[RolloutItem]) -> ThreadContextIds {
ThreadContextIds::for_thread()
}