mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
## Why Allow v2 memories to build in the background while the selected memory version continues supplying context, and let clients check whether v2 has enough consolidated data and a valid summary to use. ## What changed - Add `memories.dual_write`, disabled by default, to run v1 and v2 extraction and consolidation concurrently with separate stores and directories. - Add the experimental `memory/status` endpoint, returning `v2ConsolidatedThreads` and `v2Ready`. Readiness requires a valid v2 summary and a consolidation thread count meeting `minConsolidatedThreads` (default 20, supported range 1–4096). - Persist the largest thread count from a successful consolidation across pruning and clear it on memory reset. - Share v2 summary validation between consolidation and status reporting, and tag memory metrics with `memory_version`. ## Testing Add tests for independent dual-write outputs, preserving existing notes without importing them into v2, and readiness behavior across consolidation ownership checks, missing summaries, configurable thresholds, thread deletion, and memory reset. GitOrigin-RevId: 54417cc60bf419062401355c0da8c79119995f61
95 lines
3.2 KiB
Rust
95 lines
3.2 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_login::AuthManager;
|
|
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>,
|
|
auth_manager: Arc<AuthManager>,
|
|
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;
|
|
}
|
|
|
|
let versions = if config.memories.dual_write {
|
|
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_manager = Arc::clone(&auth_manager);
|
|
let parent_permission_profile = parent_permission_profile.clone();
|
|
let context = Arc::new(MemoryStartupContext::new(
|
|
Arc::clone(&thread_manager),
|
|
Arc::clone(&auth_manager),
|
|
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_manager, &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;
|
|
});
|
|
}
|
|
}
|