aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
6
akode-rs/memories/read/BUILD.bazel
Normal file
6
akode-rs/memories/read/BUILD.bazel
Normal file
@@ -0,0 +1,6 @@
|
||||
load("//:defs.bzl", "codex_rust_crate")
|
||||
|
||||
codex_rust_crate(
|
||||
name = "read",
|
||||
crate_name = "akode_memories_read",
|
||||
)
|
||||
21
akode-rs/memories/read/Cargo.toml
Normal file
21
akode-rs/memories/read/Cargo.toml
Normal file
@@ -0,0 +1,21 @@
|
||||
[package]
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
name = "akode-memories-read"
|
||||
version.workspace = true
|
||||
|
||||
[lib]
|
||||
name = "akode_memories_read"
|
||||
path = "src/lib.rs"
|
||||
doctest = false
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
akode-protocol = { workspace = true }
|
||||
akode-shell-command = { workspace = true }
|
||||
akode-utils-absolute-path = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = { workspace = true }
|
||||
85
akode-rs/memories/read/src/citations.rs
Normal file
85
akode-rs/memories/read/src/citations.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use akode_protocol::ThreadId;
|
||||
use akode_protocol::memory_citation::MemoryCitation;
|
||||
use akode_protocol::memory_citation::MemoryCitationEntry;
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub fn parse_memory_citation(citations: Vec<String>) -> Option<MemoryCitation> {
|
||||
let mut entries = Vec::new();
|
||||
let mut rollout_ids = Vec::new();
|
||||
let mut seen_rollout_ids = HashSet::new();
|
||||
|
||||
for citation in citations {
|
||||
if let Some(entries_block) =
|
||||
extract_block(&citation, "<citation_entries>", "</citation_entries>")
|
||||
{
|
||||
entries.extend(
|
||||
entries_block
|
||||
.lines()
|
||||
.filter_map(parse_memory_citation_entry),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(ids_block) = extract_ids_block(&citation) {
|
||||
for id in ids_block
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.filter(|line| !line.is_empty())
|
||||
{
|
||||
if seen_rollout_ids.insert(id.to_string()) {
|
||||
rollout_ids.push(id.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if entries.is_empty() && rollout_ids.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(MemoryCitation {
|
||||
entries,
|
||||
rollout_ids,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn thread_ids_from_memory_citation(memory_citation: &MemoryCitation) -> Vec<ThreadId> {
|
||||
memory_citation
|
||||
.rollout_ids
|
||||
.iter()
|
||||
.filter_map(|id| ThreadId::try_from(id.as_str()).ok())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn parse_memory_citation_entry(line: &str) -> Option<MemoryCitationEntry> {
|
||||
let line = line.trim();
|
||||
if line.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (location, note) = line.rsplit_once("|note=[")?;
|
||||
let note = note.strip_suffix(']')?.trim().to_string();
|
||||
let (path, line_range) = location.rsplit_once(':')?;
|
||||
let (line_start, line_end) = line_range.split_once('-')?;
|
||||
|
||||
Some(MemoryCitationEntry {
|
||||
path: path.trim().to_string(),
|
||||
line_start: line_start.trim().parse().ok()?,
|
||||
line_end: line_end.trim().parse().ok()?,
|
||||
note,
|
||||
})
|
||||
}
|
||||
|
||||
fn extract_block<'a>(text: &'a str, open: &str, close: &str) -> Option<&'a str> {
|
||||
let (_, rest) = text.split_once(open)?;
|
||||
let (body, _) = rest.split_once(close)?;
|
||||
Some(body)
|
||||
}
|
||||
|
||||
fn extract_ids_block(text: &str) -> Option<&str> {
|
||||
extract_block(text, "<rollout_ids>", "</rollout_ids>")
|
||||
.or_else(|| extract_block(text, "<thread_ids>", "</thread_ids>"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "citations_tests.rs"]
|
||||
mod tests;
|
||||
71
akode-rs/memories/read/src/citations_tests.rs
Normal file
71
akode-rs/memories/read/src/citations_tests.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use super::parse_memory_citation;
|
||||
use super::thread_ids_from_memory_citation;
|
||||
use akode_protocol::ThreadId;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn parse_memory_citation_supports_legacy_thread_ids() {
|
||||
let first = ThreadId::new();
|
||||
let second = ThreadId::new();
|
||||
|
||||
let citations = vec![format!(
|
||||
"<memory_citation>\n<citation_entries>\nMEMORY.md:1-2|note=[x]\n</citation_entries>\n<thread_ids>\n{first}\nnot-a-uuid\n{second}\n</thread_ids>\n</memory_citation>"
|
||||
)];
|
||||
|
||||
let parsed = parse_memory_citation(citations).expect("memory citation should parse");
|
||||
|
||||
assert_eq!(
|
||||
thread_ids_from_memory_citation(&parsed),
|
||||
vec![first, second]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_memory_citation_supports_rollout_ids() {
|
||||
let thread_id = ThreadId::new();
|
||||
|
||||
let citations = vec![format!(
|
||||
"<memory_citation>\n<rollout_ids>\n{thread_id}\n</rollout_ids>\n</memory_citation>"
|
||||
)];
|
||||
|
||||
let parsed = parse_memory_citation(citations).expect("memory citation should parse");
|
||||
|
||||
assert_eq!(thread_ids_from_memory_citation(&parsed), vec![thread_id]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_memory_citation_extracts_entries_and_rollout_ids() {
|
||||
let first = ThreadId::new();
|
||||
let second = ThreadId::new();
|
||||
let citations = vec![format!(
|
||||
"<citation_entries>\nMEMORY.md:1-2|note=[summary]\nrollout_summaries/foo.md:10-12|note=[details]\n</citation_entries>\n<rollout_ids>\n{first}\n{second}\n{first}\n</rollout_ids>"
|
||||
)];
|
||||
|
||||
let parsed = parse_memory_citation(citations).expect("memory citation should parse");
|
||||
|
||||
assert_eq!(
|
||||
parsed
|
||||
.entries
|
||||
.iter()
|
||||
.map(|entry| (
|
||||
entry.path.clone(),
|
||||
entry.line_start,
|
||||
entry.line_end,
|
||||
entry.note.clone(),
|
||||
))
|
||||
.collect::<Vec<_>>(),
|
||||
vec![
|
||||
("MEMORY.md".to_string(), 1, 2, "summary".to_string()),
|
||||
(
|
||||
"rollout_summaries/foo.md".to_string(),
|
||||
10,
|
||||
12,
|
||||
"details".to_string()
|
||||
),
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
parsed.rollout_ids,
|
||||
vec![first.to_string(), second.to_string()]
|
||||
);
|
||||
}
|
||||
15
akode-rs/memories/read/src/lib.rs
Normal file
15
akode-rs/memories/read/src/lib.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
//! Read-path helpers for Codex memories.
|
||||
//!
|
||||
//! This crate owns memory injection, memory citation parsing, and telemetry
|
||||
//! classification for read access to the memory folder. It intentionally does
|
||||
//! not depend on the memory write pipeline.
|
||||
|
||||
pub mod citations;
|
||||
mod metrics;
|
||||
pub mod usage;
|
||||
|
||||
use akode_utils_absolute_path::AbsolutePathBuf;
|
||||
|
||||
pub fn memory_root(codex_home: &AbsolutePathBuf) -> AbsolutePathBuf {
|
||||
codex_home.join("memories")
|
||||
}
|
||||
1
akode-rs/memories/read/src/metrics.rs
Normal file
1
akode-rs/memories/read/src/metrics.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub const MEMORIES_USAGE_METRIC: &str = "codex.memories.usage";
|
||||
75
akode-rs/memories/read/src/usage.rs
Normal file
75
akode-rs/memories/read/src/usage.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
//! Best-effort classification of shell reads by memory artifact and root version.
|
||||
|
||||
use akode_protocol::MemoryVersion;
|
||||
use akode_protocol::parse_command::ParsedCommand;
|
||||
use akode_shell_command::parse_command::parse_shell_script;
|
||||
|
||||
pub use crate::metrics::MEMORIES_USAGE_METRIC;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum MemoriesUsageKind {
|
||||
MemoryMd,
|
||||
MemorySummary,
|
||||
RawMemories,
|
||||
RolloutSummaries,
|
||||
Skills,
|
||||
}
|
||||
|
||||
impl MemoriesUsageKind {
|
||||
pub fn as_tag(self) -> &'static str {
|
||||
match self {
|
||||
Self::MemoryMd => "memory_md",
|
||||
Self::MemorySummary => "memory_summary",
|
||||
Self::RawMemories => "raw_memories",
|
||||
Self::RolloutSummaries => "rollout_summaries",
|
||||
Self::Skills => "skills",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn memories_usage_from_command(command: &str) -> Vec<(MemoriesUsageKind, MemoryVersion)> {
|
||||
let commands = parse_shell_script(command);
|
||||
if commands
|
||||
.iter()
|
||||
.any(|command| matches!(command, ParsedCommand::Unknown { .. }))
|
||||
{
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
commands
|
||||
.into_iter()
|
||||
.filter_map(|command| match command {
|
||||
ParsedCommand::Read { path, .. } => get_memory_usage(path.display().to_string()),
|
||||
ParsedCommand::Search { path, .. } => path.and_then(get_memory_usage),
|
||||
ParsedCommand::ListFiles { .. } | ParsedCommand::Unknown { .. } => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_memory_usage(path: String) -> Option<(MemoriesUsageKind, MemoryVersion)> {
|
||||
let path = path.replace('\\', "/");
|
||||
let version = if path.contains("memories_v2/") {
|
||||
MemoryVersion::V2
|
||||
} else {
|
||||
MemoryVersion::V1
|
||||
};
|
||||
let path = path.replace("memories_v2/", "memories/");
|
||||
let kind = if path.contains("memories/MEMORY.md") {
|
||||
MemoriesUsageKind::MemoryMd
|
||||
} else if path.contains("memories/memory_summary.md") {
|
||||
MemoriesUsageKind::MemorySummary
|
||||
} else if path.contains("memories/raw_memories.md") {
|
||||
MemoriesUsageKind::RawMemories
|
||||
} else if path.contains("memories/rollout_summaries/") {
|
||||
MemoriesUsageKind::RolloutSummaries
|
||||
} else if path.contains("memories/skills/") {
|
||||
MemoriesUsageKind::Skills
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
Some((kind, version))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "usage_tests.rs"]
|
||||
mod tests;
|
||||
46
akode-rs/memories/read/src/usage_tests.rs
Normal file
46
akode-rs/memories/read/src/usage_tests.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
//! Regression coverage for versioned memory paths in shell usage telemetry.
|
||||
|
||||
use super::MemoriesUsageKind;
|
||||
use super::memories_usage_from_command;
|
||||
use akode_protocol::MemoryVersion;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn classifies_reads_in_both_memory_versions() {
|
||||
for version in [MemoryVersion::V1, MemoryVersion::V2] {
|
||||
let root = version.directory_name();
|
||||
for (reader, root) in [
|
||||
("cat", format!("/tmp/.akode/{root}")),
|
||||
("Get-Content", format!(r"C:\Users\test\.akode\{root}")),
|
||||
] {
|
||||
let command = format!(
|
||||
"{reader} '{root}/memory_summary.md' && {reader} '{root}/rollout_summaries/thread.md'"
|
||||
);
|
||||
assert_eq!(
|
||||
memories_usage_from_command(&command),
|
||||
vec![
|
||||
(MemoriesUsageKind::MemorySummary, version),
|
||||
(MemoriesUsageKind::RolloutSummaries, version),
|
||||
],
|
||||
"memory root: {root}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attributes_mixed_version_reads_to_their_roots() {
|
||||
let command = "cat /tmp/.akode/memories/MEMORY.md \
|
||||
&& sed -n '1,20p' /tmp/.akode/memories_v2/rollout_summaries/thread.md \
|
||||
&& cat /tmp/.akode/memories/raw_memories.md \
|
||||
&& cat /tmp/.akode/memories_v2/skills/testing/SKILL.md";
|
||||
assert_eq!(
|
||||
memories_usage_from_command(command),
|
||||
vec![
|
||||
(MemoriesUsageKind::MemoryMd, MemoryVersion::V1),
|
||||
(MemoriesUsageKind::RolloutSummaries, MemoryVersion::V2),
|
||||
(MemoriesUsageKind::RawMemories, MemoryVersion::V1),
|
||||
(MemoriesUsageKind::Skills, MemoryVersion::V2),
|
||||
],
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user