Files
codex/codex-rs/memories/write/src/start.rs
T
cooper-oai 1c414e0d65 Use thread runtime auth for Guardian Decisions and memory quota checks (#60064)
## Why

Guardian Decisions requests used bearer keys directly, bypassing runtime authentication. Memory startup quota checks used the app-level auth manager instead of the thread's provider auth context.

## What changed

- Bind Decisions credentials to runtime authentication and use route-aware HTTP clients with sensitive header protection. Preserve workload identity credentials, reuse the saved-login runtime binding, and isolate explicit API key bindings.
- Select the staging Decisions endpoint when the ChatGPT environment is staging.
- Use the thread's `ProviderAuthContext` for memory startup and quota checks, preserving account-bound network policy and applying runtime authentication.
- Keep custom destinations independent of runtime assertions.

## Testing

Add regression tests for dual-header and assertion-only authentication, fresh Decisions assertions, saved versus explicit credential isolation, workload identity, staging routing, redirect header protection, and memory quota authentication with custom destinations.

GitOrigin-RevId: 27bc7821a1cad1f5d1d2a71680527ea91ace95db
2026-11-07 05:50:00 +00:00

103 lines
3.6 KiB
Rust

use crate::ensure_layout;
use crate::extensions::seed_extension_instructions;
use crate::guard;
use crate::metrics::MEMORY_STARTUP;
use crate::phase1;
use crate::phase2;
use crate::runtime::MemoryStartupContext;
use codex_core::CodexThread;
use codex_core::ThreadManager;
use codex_core::config::Config;
use codex_features::Feature;
use codex_model_provider::ProviderAuthContext;
use codex_protocol::MemoryVersion;
use codex_protocol::ThreadId;
use codex_protocol::models::PermissionProfile;
use codex_protocol::protocol::SessionSource;
use std::sync::Arc;
use tracing::warn;
/// Starts the asynchronous startup memory pipeline for an eligible root session.
///
/// The pipeline is skipped for ephemeral sessions, disabled feature flags, and
/// subagent sessions.
pub fn start_memories_startup_task(
thread_manager: Arc<ThreadManager>,
thread_id: ThreadId,
thread: Arc<CodexThread>,
config: Arc<Config>,
parent_permission_profile: PermissionProfile,
source: &SessionSource,
) {
if config.ephemeral
|| !config.features.enabled(Feature::MemoryTool)
|| source.is_non_root_agent()
{
return;
}
#[expect(
clippy::expect_used,
reason = "Core installs provider auth before starting memories"
)]
let auth = thread
.thread_extension_data()
.get::<ProviderAuthContext>()
.expect("session startup installs the provider auth context");
// Dual-write warms v2 while v1 is selected; it is no longer needed after cutover.
let versions = if config.memories.dual_write && config.memories.version == MemoryVersion::V1 {
vec![MemoryVersion::V1, MemoryVersion::V2]
} else {
vec![config.memories.version]
};
for version in versions {
let mut pipeline_config = config.as_ref().clone();
pipeline_config.memories.version = version;
let config = Arc::new(pipeline_config);
let auth = Arc::clone(&auth);
let parent_permission_profile = parent_permission_profile.clone();
let context = Arc::new(MemoryStartupContext::new(
Arc::clone(&thread_manager),
Arc::clone(&auth),
thread_id,
Arc::clone(&thread),
config.as_ref(),
source.clone(),
));
tokio::spawn(async move {
if context.memory_store().await.is_none() {
warn!("state db unavailable for memories startup pipeline; skipping");
return;
}
let root = config
.codex_home
.join(config.memories.version.directory_name());
if let Err(err) = ensure_layout(&root).await {
warn!("failed preparing memories root: {err}");
return;
}
if let Err(err) = seed_extension_instructions(&root).await {
warn!("failed seeding memory extension instructions: {err}");
}
// Clean memories to make preserve DB size. This does not consume tokens so can be
// done before the quota check.
phase1::prune(context.as_ref(), &config).await;
if !guard::rate_limits_ok(&auth, &config).await {
context.counter(
MEMORY_STARTUP,
/*inc*/ 1,
&[("status", "skipped_rate_limit")],
);
return;
}
// Run phase 1.
phase1::run(Arc::clone(&context), Arc::clone(&config)).await;
// Run phase 2.
phase2::run(context, config, parent_permission_profile).await;
});
}
}