Add session-scoped stores for stable model-facing IDs (#61339)

## What changed

- Add `SessionModelIdStore` with SQLite and in-memory implementations for looking up and assigning aliases by session, namespace, and external key.
- Commit SQLite assignments before returning them, and recheck under a write transaction so concurrent writers reuse the same alias.
- Support snapshotting and initializing alias maps from generator state and ordered keys, and remove mappings when their thread is deleted.
- Expose alias stores through `ThreadStore` and add distinct types for registered environment IDs and model-facing `env` aliases.

## Testing

Add a test using two SQLite runtime instances to verify that concurrent assignments return the same alias, different keys receive different aliases, and lookups and snapshots preserve the assignments.

GitOrigin-RevId: 2668f605251ca76d4891f70f94bfb4151ba8142c
This commit is contained in:
Adam Perry @ OpenAI
2026-11-13 18:43:58 +00:00
committed by copyberry
parent 9d12d007b2
commit a4b69a0ae9
15 changed files with 454 additions and 9 deletions
+7
View File
@@ -525,6 +525,7 @@ pub struct InMemoryThreadStoreCalls {
pub struct InMemoryThreadStore {
state: Arc<tokio::sync::Mutex<InMemoryThreadStoreState>>,
omit_metadata_update_result: Arc<AtomicBool>,
model_ids: Arc<codex_state::MemoryModelIdStore>,
state_db: Option<codex_rollout::StateDbHandle>,
}
@@ -558,6 +559,7 @@ impl InMemoryThreadStore {
Self {
state: Arc::clone(&self.state),
omit_metadata_update_result: Arc::clone(&self.omit_metadata_update_result),
model_ids: Arc::clone(&self.model_ids),
state_db,
}
}
@@ -906,6 +908,7 @@ impl InMemoryThreadStore {
0
};
let mut state = self.state.lock().await;
self.model_ids.remove(params.thread_id.into());
let existed = state.histories.remove(&params.thread_id).is_some();
state.created_threads.remove(&params.thread_id);
state.names.remove(&params.thread_id);
@@ -927,6 +930,10 @@ impl InMemoryThreadStore {
}
impl ThreadStore for InMemoryThreadStore {
fn model_ids(&self) -> Option<Arc<dyn codex_state::SessionModelIdStore>> {
Some(self.model_ids.clone())
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
+6
View File
@@ -516,6 +516,12 @@ impl LocalThreadStore {
}
impl ThreadStore for LocalThreadStore {
fn model_ids(&self) -> Option<Arc<dyn codex_state::SessionModelIdStore>> {
self.state_db
.clone()
.map(|db| db as Arc<dyn codex_state::SessionModelIdStore>)
}
fn default_history_mode(&self) -> ThreadHistoryMode {
ThreadHistoryMode::Paginated
}
+5
View File
@@ -146,6 +146,11 @@ pub trait ThreadStore: Any + Send + Sync {
/// Return this store as [`Any`] for implementation-owned escape hatches.
fn as_any(&self) -> &dyn Any;
/// Returns the authority for session aliases when this backend supports them.
fn model_ids(&self) -> Option<std::sync::Arc<dyn codex_state::SessionModelIdStore>> {
None
}
/// Returns the history mode to use when history does not carry a persisted mode.
///
/// The default is legacy so existing stores stay compatible. Stores whose durable contract is