mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
## What changed - Consolidate v2 rollout summaries into `memory_summary.md` without generating `raw_memories.md` or requiring `MEMORY.md`. Validate the summary's required sections and size below 10,000 UTF-8 bytes. - Add v2 read instructions for selective history retrieval, evidence-grounded preferences, citations, and explicit memory edits. Split injected instructions into fragments to preserve the complete summary within fragment byte limits. - Record memory citation usage in the store selected by `memories.version`. ## Testing Add coverage for v2 consolidation without a handbook, summary validation, version-isolated memory reads, and resetting both memory versions while preserving threads. GitOrigin-RevId: 1d895fb4a23a973f1a45ba03be07a1f480c10227
69 lines
2.2 KiB
Rust
69 lines
2.2 KiB
Rust
use crate::MEMORY_TOOL_DEVELOPER_INSTRUCTIONS_SUMMARY_TOKEN_LIMIT;
|
|
use codex_protocol::MemoryVersion;
|
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
|
use codex_utils_output_truncation::TruncationPolicy;
|
|
use codex_utils_output_truncation::truncate_text;
|
|
use codex_utils_template::Template;
|
|
use std::sync::LazyLock;
|
|
use tokio::fs;
|
|
|
|
static MEMORY_TOOL_DEVELOPER_INSTRUCTIONS_TEMPLATE: LazyLock<Template> = LazyLock::new(|| {
|
|
parse_embedded_template(
|
|
include_str!("../templates/memories/read_path.md"),
|
|
"memories/read_path.md",
|
|
)
|
|
});
|
|
|
|
static MEMORY_V2_TEMPLATE: LazyLock<Template> = LazyLock::new(|| {
|
|
parse_embedded_template(
|
|
include_str!("../templates/memories/read_path_v2.md"),
|
|
"memories/read_path_v2.md",
|
|
)
|
|
});
|
|
|
|
fn parse_embedded_template(source: &'static str, template_name: &str) -> Template {
|
|
match Template::parse(source) {
|
|
Ok(template) => template,
|
|
Err(err) => panic!("embedded template {template_name} is invalid: {err}"),
|
|
}
|
|
}
|
|
|
|
/// Build the memory read-path prompt that is added to developer instructions.
|
|
///
|
|
/// Large `memory_summary.md` files are truncated at
|
|
/// [MEMORY_TOOL_DEVELOPER_INSTRUCTIONS_SUMMARY_TOKEN_LIMIT].
|
|
pub(crate) async fn build_memory_tool_developer_instructions(
|
|
codex_home: &AbsolutePathBuf,
|
|
version: MemoryVersion,
|
|
) -> Option<String> {
|
|
let base_path = codex_home.join(version.directory_name());
|
|
let memory_summary_path = base_path.join("memory_summary.md");
|
|
let memory_summary = fs::read_to_string(&memory_summary_path)
|
|
.await
|
|
.ok()?
|
|
.trim()
|
|
.to_string();
|
|
let memory_summary = truncate_text(
|
|
&memory_summary,
|
|
TruncationPolicy::Tokens(MEMORY_TOOL_DEVELOPER_INSTRUCTIONS_SUMMARY_TOKEN_LIMIT),
|
|
);
|
|
if memory_summary.is_empty() {
|
|
return None;
|
|
}
|
|
let base_path = base_path.display().to_string();
|
|
let template = match version {
|
|
MemoryVersion::V1 => &MEMORY_TOOL_DEVELOPER_INSTRUCTIONS_TEMPLATE,
|
|
MemoryVersion::V2 => &MEMORY_V2_TEMPLATE,
|
|
};
|
|
template
|
|
.render([
|
|
("base_path", base_path.as_str()),
|
|
("memory_summary", memory_summary.as_str()),
|
|
])
|
|
.ok()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "prompts_tests.rs"]
|
|
mod tests;
|