mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Add session-scoped aliases for registered environment IDs (#65140)
## What changed Pair each registered executor ID with a model-facing alias assigned on first use by the session's model ID store. Turn environment snapshots can resolve these pairs in selection order, including for executors that are still starting or have failed. Environment context snapshots and rendering continue to use registered IDs. ## Testing Cover alias assignment in selection order, stability across readiness updates, and reuse of the same ID pair after reopening the SQLite store. GitOrigin-RevId: e5fa6e5549ebb12fcf651e5bf7413402fb4db1b3
This commit is contained in:
committed by
copyberry
parent
55ea9a4620
commit
e1e967cd46
@@ -108,6 +108,7 @@ pub use runtime::collect_runtime_db_backups;
|
||||
pub use runtime::is_sqlite_corruption_error;
|
||||
pub use runtime::model_ids::MemoryModelIdStore;
|
||||
pub use runtime::model_ids::ModelIdStoreFuture;
|
||||
pub use runtime::model_ids::SessionModelIdMapping;
|
||||
pub use runtime::model_ids::SessionModelIdStore;
|
||||
pub use runtime::open_thread_history_db;
|
||||
pub use runtime::recovery::RuntimeDbBackup;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
use std::collections::HashMap;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use anyhow::Result;
|
||||
@@ -11,6 +12,7 @@ use codex_utils_model_facing_id::ModelFacingId;
|
||||
use codex_utils_model_facing_id::ModelFacingIdGenerator;
|
||||
use codex_utils_model_facing_id::ModelFacingIdGeneratorState;
|
||||
use codex_utils_model_facing_id::ModelIdKey;
|
||||
use codex_utils_model_facing_id::ModelIdKind;
|
||||
use codex_utils_model_facing_id::ModelIdMap;
|
||||
use sqlx::SqliteConnection;
|
||||
use sqlx::types::Json;
|
||||
@@ -37,6 +39,34 @@ pub trait SessionModelIdStore: Send + Sync {
|
||||
fn initialize(&self, session: SessionId, maps: Vec<ModelIdMap>) -> ModelIdStoreFuture<'_, ()>;
|
||||
}
|
||||
|
||||
/// Stable model-facing names for external objects referenced by a session and its workers.
|
||||
pub struct SessionModelIdMapping {
|
||||
session_id: SessionId,
|
||||
store: Result<Arc<dyn SessionModelIdStore>>,
|
||||
}
|
||||
|
||||
impl SessionModelIdMapping {
|
||||
/// Defer store errors until a caller actually needs a mapped ID.
|
||||
pub fn new(session_id: SessionId, store: Result<Arc<dyn SessionModelIdStore>>) -> Self {
|
||||
Self { session_id, store }
|
||||
}
|
||||
|
||||
pub fn session_id(&self) -> SessionId {
|
||||
self.session_id
|
||||
}
|
||||
|
||||
/// Assign an alias on first use and pair it with the original external key.
|
||||
pub async fn get_or_assign<I: ModelIdKind>(&self, key: I::Key) -> Result<I> {
|
||||
let stored_key = ModelIdKey::new::<I>(&key)?;
|
||||
let store = self
|
||||
.store
|
||||
.as_ref()
|
||||
.map_err(|err| anyhow::anyhow!("session model ID store: {err:#}"))?;
|
||||
let alias = store.get_or_assign(self.session_id, stored_key).await?;
|
||||
Ok(I::from_parts(key, alias.into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl SessionModelIdStore for StateRuntime {
|
||||
fn lookup(
|
||||
&self,
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
use super::*;
|
||||
use crate::SqliteConfig;
|
||||
use codex_utils_absolute_path::test_support::PathExt;
|
||||
use codex_utils_model_facing_id::RegisteredEnvironmentId;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
/// Concurrent writers reserve a key once, and an independent connection observes the winner.
|
||||
@@ -14,13 +13,19 @@ async fn concurrent_assignments_share_a_durable_alias() -> Result<()> {
|
||||
let a = StateRuntime::init(config.clone(), "test".into()).await?;
|
||||
let b = StateRuntime::init(config, "test".into()).await?;
|
||||
let session = SessionId::new();
|
||||
let key = ModelIdKey::new(&RegisteredEnvironmentId::new("first"))?;
|
||||
let key = ModelIdKey {
|
||||
kind: "env",
|
||||
key: serde_json::to_string("first")?,
|
||||
};
|
||||
let (left, right) = tokio::try_join!(
|
||||
a.get_or_assign(session, key.clone()),
|
||||
b.get_or_assign(session, key.clone()),
|
||||
)?;
|
||||
assert_eq!(left, right);
|
||||
let second = ModelIdKey::new(&RegisteredEnvironmentId::new("second"))?;
|
||||
let second = ModelIdKey {
|
||||
kind: "env",
|
||||
key: serde_json::to_string("second")?,
|
||||
};
|
||||
let next = b.get_or_assign(session, second.clone()).await?;
|
||||
assert_ne!(left, next);
|
||||
assert_eq!(a.lookup(session, key.clone()).await?, Some(left));
|
||||
|
||||
Reference in New Issue
Block a user