mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
## 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
60 lines
1.6 KiB
Rust
60 lines
1.6 KiB
Rust
//! Stable names for external objects in low-cardinality ID spaces.
|
|
|
|
use std::hash::Hash;
|
|
|
|
use serde::Serialize;
|
|
use serde::de::DeserializeOwned;
|
|
|
|
use crate::ModelFacingId;
|
|
use crate::ModelFacingIdGenerator;
|
|
use crate::ModelFacingIdGeneratorState;
|
|
use crate::RestoreError;
|
|
|
|
/// The key type selects the alias type and a unique, stable prefix. Its JSON encoding must
|
|
/// distinguish unequal keys and remain stable across releases.
|
|
pub trait ModelIdKind: Eq + Hash + Serialize + DeserializeOwned {
|
|
const PREFIX: &'static str;
|
|
type Alias: From<ModelFacingId>;
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ModelIdKey {
|
|
pub kind: &'static str,
|
|
pub key: String,
|
|
}
|
|
|
|
impl ModelIdKey {
|
|
pub fn new<K: ModelIdKind>(key: &K) -> serde_json::Result<Self> {
|
|
let mut key = serde_json::to_value(key)?;
|
|
key.sort_all_objects();
|
|
Ok(Self {
|
|
kind: K::PREFIX,
|
|
key: key.to_string(),
|
|
})
|
|
}
|
|
}
|
|
|
|
/// A namespace needs only its initial generator state and the external keys in allocation order.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct ModelIdMap {
|
|
pub initial: ModelFacingIdGeneratorState,
|
|
pub keys: Vec<String>,
|
|
}
|
|
|
|
impl ModelIdMap {
|
|
pub fn new(seed: u64, kind: &'static str) -> Self {
|
|
Self {
|
|
initial: ModelFacingIdGenerator::new(seed, kind).snapshot(),
|
|
keys: Vec::new(),
|
|
}
|
|
}
|
|
|
|
pub fn lookup(&self, key: &str) -> Result<Option<ModelFacingId>, RestoreError> {
|
|
self.keys
|
|
.iter()
|
|
.position(|bound| bound == key)
|
|
.map(|ordinal| self.initial.id_at(ordinal as u64))
|
|
.transpose()
|
|
}
|
|
}
|