use crate::memory::{MemoryBackend, MemoryError, MemoryService, worker_gate};
use crate::memory_records::{MAX_SITUATION_BYTES, RecordIndex, Typed, content_cap};
use kernel::Signal;
use kernel::store::FileDocumentStore;
use serde_json::{Map, Value, json};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use yaml_rust2::{Yaml, YamlLoader};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Harness {
ClaudeCode,
Codex,
}
impl Harness {
pub fn parse(name: &str) -> Option<Harness> {
match name {
"claude-code" | "claude" => Some(Harness::ClaudeCode),
"codex" => Some(Harness::Codex),
_ => None,
}
}
pub fn label(self) -> &'static str {
match self {
Harness::ClaudeCode => "Claude Code",
Harness::Codex => "Codex",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Note {
pub key: String,
pub kind: &'static str,
pub situation: String,
pub content: String,
pub file: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Source {
pub harness: Harness,
pub directory: PathBuf,
pub project: Option<PathBuf>,
}
pub fn notes_root(harness: Harness, home: &Path, var: &dyn Fn(&str) -> Option<String>) -> PathBuf {
let configured = |name: &str| var(name).filter(|value| !value.trim().is_empty()).map(PathBuf::from);
match harness {
Harness::ClaudeCode => configured("CLAUDE_CONFIG_DIR")
.unwrap_or_else(|| home.join(".claude"))
.join("projects"),
Harness::Codex => configured("CODEX_HOME")
.unwrap_or_else(|| home.join(".codex"))
.join("memories"),
}
}
pub fn discover(harness: Harness, root: &Path) -> Vec<Source> {
match harness {
Harness::ClaudeCode => {
let mut projects: Vec<PathBuf> = std::fs::read_dir(root)
.into_iter()
.flatten()
.flatten()
.map(|entry| entry.path())
.filter(|project| project.join("memory").is_dir())
.collect();
projects.sort();
projects
.into_iter()
.map(|project| Source {
harness,
directory: project.join("memory"),
project: project_path(&project),
})
.collect()
}
Harness::Codex => {
if root.join("memory_summary.md").is_file() || root.join("MEMORY.md").is_file() {
vec![Source {
harness,
directory: root.to_path_buf(),
project: None,
}]
} else {
Vec::new()
}
}
}
}
pub fn project_path(project_dir: &Path) -> Option<PathBuf> {
let name = project_dir.file_name()?.to_string_lossy().into_owned();
let segments: Vec<&str> = name.trim_start_matches('-').split('-').collect();
let mut matches = Vec::new();
resolve_segments(Path::new("/"), &segments, &mut matches);
(matches.len() == 1).then(|| matches.remove(0))
}
fn resolve_segments(base: &Path, segments: &[&str], matches: &mut Vec<PathBuf>) {
if matches.len() > 1 {
return;
}
if segments.is_empty() {
matches.push(base.to_path_buf());
return;
}
for take in 1..=segments.len() {
let candidate = base.join(segments[..take].join("-"));
if candidate.is_dir() {
resolve_segments(&candidate, &segments[take..], matches);
}
}
}
impl Source {
pub fn read(&self) -> Result<Vec<Note>, String> {
match self.harness {
Harness::ClaudeCode => claude_code_notes(&self.directory),
Harness::Codex => codex_notes(&self.directory),
}
}
}
fn collect_markdown(directory: &Path, out: &mut Vec<PathBuf>) -> Result<(), String> {
let entries = std::fs::read_dir(directory).map_err(|e| format!("{}: {e}", directory.display()))?;
for entry in entries {
let entry = entry.map_err(|e| e.to_string())?;
let path = entry.path();
let name = entry.file_name().to_string_lossy().into_owned();
let file_type = entry.file_type().map_err(|e| e.to_string())?;
if name.starts_with('.') || file_type.is_symlink() {
continue;
}
if file_type.is_dir() {
if name != "old" {
collect_markdown(&path, out)?;
}
} else if file_type.is_file() && name.ends_with(".md") && name != "MEMORY.md" {
out.push(path);
}
}
Ok(())
}
fn split_frontmatter(text: &str) -> (Option<&str>, &str) {
let Some(rest) = text.strip_prefix("---\n").or_else(|| text.strip_prefix("---\r\n")) else {
return (None, text);
};
let mut cursor = 0;
for line in rest.split_inclusive('\n') {
if line.trim_end() == "---" {
return (Some(&rest[..cursor]), &rest[cursor + line.len()..]);
}
cursor += line.len();
}
(None, text)
}
fn yaml_text<'a>(yaml: &'a Yaml, key: &str) -> Option<&'a str> {
yaml[key].as_str().map(str::trim).filter(|text| !text.is_empty())
}
pub fn claude_code_notes(directory: &Path) -> Result<Vec<Note>, String> {
let mut files = Vec::new();
collect_markdown(directory, &mut files)?;
files.sort();
let mut notes = Vec::new();
for path in files {
let text = std::fs::read_to_string(&path).map_err(|e| format!("{}: {e}", path.display()))?;
let (front, body) = split_frontmatter(&text);
let front = front
.and_then(|yaml| YamlLoader::load_from_str(yaml).ok())
.and_then(|mut docs| (!docs.is_empty()).then(|| docs.remove(0)))
.unwrap_or(Yaml::Null);
let body = body.trim();
if body.is_empty() {
continue;
}
let stem = path
.file_stem()
.map(|stem| stem.to_string_lossy().into_owned())
.unwrap_or_default();
let name = yaml_text(&front, "name").map(str::to_string).unwrap_or(stem);
let kind = match yaml_text(&front["metadata"], "type").or_else(|| yaml_text(&front, "type")) {
Some("feedback") => "preference",
Some("user" | "project") => "fact",
_ => "reference",
};
notes.push(Note {
key: slug_key(&name),
kind,
situation: yaml_text(&front, "description").unwrap_or_default().to_string(),
content: body.to_string(),
file: path,
});
}
Ok(notes)
}
struct Section {
heading: String,
lines: Vec<String>,
}
impl Section {
fn bullets(&self) -> Vec<String> {
let mut bullets: Vec<String> = Vec::new();
for line in &self.lines {
let trimmed = line.trim_start_matches([' ', '\t']);
if let Some(item) = trimmed.strip_prefix("- ") {
bullets.push(item.trim().to_string());
} else if (line.starts_with(' ') || line.starts_with('\t'))
&& let Some(last) = bullets.last_mut()
{
last.push(' ');
last.push_str(line.trim());
}
}
bullets
}
fn paragraphs(&self) -> Vec<String> {
self.lines
.iter()
.map(|line| line.trim())
.filter(|line| !line.is_empty() && !line.starts_with("- "))
.map(str::to_string)
.collect()
}
}
fn sections(text: &str) -> Vec<Section> {
let mut sections: Vec<Section> = Vec::new();
for line in text.lines() {
if let Some(heading) = line.strip_prefix("## ") {
sections.push(Section {
heading: heading.trim().to_string(),
lines: Vec::new(),
});
} else if line.starts_with('#') {
sections.push(Section {
heading: String::new(),
lines: Vec::new(),
});
} else if let Some(section) = sections.last_mut() {
section.lines.push(line.to_string());
}
}
sections.retain(|section| !section.heading.is_empty());
sections
}
fn note(key: &str, kind: &'static str, situation: &str, content: &str, file: &Path) -> Note {
Note {
key: slug_key(key),
kind,
situation: situation.to_string(),
content: content.to_string(),
file: file.to_path_buf(),
}
}
fn summary_notes(text: &str, file: &Path) -> Vec<Note> {
let mut notes = Vec::new();
for section in sections(text) {
match section.heading.as_str() {
"User Profile" => {
let profile = section.paragraphs().join(" ");
if !profile.is_empty() {
notes.push(note(
"codex-user-profile",
"fact",
"who the user is, as Codex summarized it",
&profile,
file,
));
}
}
"User preferences" => {
for bullet in section.bullets() {
notes.push(note(
&format!("codex-pref-{}", slug_key(&bullet)),
"preference",
"how the user wants work done, as Codex summarized it",
&bullet,
file,
));
}
}
"General Tips" => {
for bullet in section.bullets() {
notes.push(note(
&format!("codex-tip-{}", slug_key(&bullet)),
"procedure",
"working practice Codex learned across tasks",
&bullet,
file,
));
}
}
_ => {}
}
}
notes
}
fn task_heading(heading: &str) -> Option<(String, String)> {
let rest = heading.strip_prefix("Task ")?;
let (number, title) = rest.split_once(": ")?;
(!number.is_empty() && number.bytes().all(|b| b.is_ascii_digit()) && !title.trim().is_empty())
.then(|| (number.to_string(), title.trim().to_string()))
}
fn trailing_task_tag(content: &str) -> Option<(String, usize)> {
let trimmed = content.trim_end();
let inner = trimmed.strip_suffix(']')?;
let open = inner.rfind("[Task ")?;
let number = &inner[open + "[Task ".len()..];
if number.is_empty() || !number.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
let start = content[..open].trim_end().len();
Some((number.to_string(), start))
}
fn memory_notes(text: &str, file: &Path) -> Vec<Note> {
let mut tasks: BTreeMap<String, String> = BTreeMap::new();
let mut notes = Vec::new();
for section in sections(text) {
if let Some((number, title)) = task_heading(§ion.heading) {
tasks.insert(number, title);
continue;
}
let (kind, prefix) = match section.heading.as_str() {
"User preferences" => ("preference", "codex-pref"),
"Reusable knowledge" => ("fact", "codex-fact"),
"Failures and how to do differently" => ("pitfall", "codex-pitfall"),
_ => continue,
};
for bullet in section.bullets() {
let mut situation = String::from("from the user's Codex task memory");
let mut content = bullet.clone();
while let Some((number, start)) = trailing_task_tag(&content) {
if let Some(title) = tasks.get(&number) {
situation = format!("Codex task: {title}");
}
content.truncate(start);
}
if content.trim().is_empty() {
continue;
}
notes.push(note(
&format!("{prefix}-{}", slug_key(&content)),
kind,
&situation,
content.trim(),
file,
));
}
}
notes
}
pub fn codex_notes(directory: &Path) -> Result<Vec<Note>, String> {
let mut notes = Vec::new();
let mut read_any = false;
for (name, parse) in [
("memory_summary.md", summary_notes as fn(&str, &Path) -> Vec<Note>),
("MEMORY.md", memory_notes),
] {
let path = directory.join(name);
match std::fs::read_to_string(&path) {
Ok(text) => {
read_any = true;
notes.extend(parse(&text, &path));
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(format!("{}: {error}", path.display())),
}
}
if !read_any {
return Err(format!("no Codex memory files found in {}", directory.display()));
}
Ok(notes)
}
pub fn slug_key(text: &str) -> String {
let mut key = String::new();
let mut last_dash = true;
for character in text.chars().flat_map(char::to_lowercase) {
if character.is_ascii_alphanumeric() {
key.push(character);
last_dash = false;
} else if !last_dash {
key.push('-');
last_dash = true;
}
if key.len() >= 60 {
break;
}
}
let key = key.trim_matches('-').to_string();
if key.is_empty() { "note".to_string() } else { key }
}
fn clean(text: &str) -> String {
text.replace("\r\n", "\n")
.chars()
.filter(|ch| !ch.is_control() || *ch == '\n' || *ch == '\t')
.collect::<String>()
.trim()
.to_string()
}
fn truncate_bytes(text: &str, max: usize) -> &str {
let mut end = 0;
for (offset, ch) in text.char_indices() {
if offset + ch.len_utf8() > max {
break;
}
end = offset + ch.len_utf8();
}
&text[..end]
}
pub fn home_relative(path: &Path, home: &Path) -> String {
match path.strip_prefix(home) {
Ok(rest) if rest.as_os_str().is_empty() => "~".into(),
Ok(rest) => format!("~/{}", rest.display()),
Err(_) => path.display().to_string(),
}
}
fn note_key(harness: Harness, note: &Note, occurrence: usize) -> String {
let file = note.file.display().to_string();
let mut identity = match harness {
Harness::ClaudeCode => file,
Harness::Codex => format!("{file}\0{}", clean(¬e.content)),
};
if occurrence > 1 {
identity.push_str(&format!("\0{occurrence}"));
}
let suffix = &jsrt::hash::sha256_hex(identity.as_bytes())[..12];
let base = note.key.trim_end_matches('-');
let end = base.len().min(64 - 1 - suffix.len());
format!("{}-{suffix}", base[..end].trim_end_matches('-'))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Action {
Add,
Replace(String),
Unchanged(String),
Refuse(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Planned {
pub typed: Typed,
pub content: String,
pub scope: &'static str,
pub project: Option<PathBuf>,
pub file_sha: String,
pub action: Action,
}
pub type ScopeTags<'a> = &'a dyn Fn(Option<&Path>) -> Map<String, Value>;
pub fn plan(sources: &[Source], home: &Path, index: &RecordIndex, tags: ScopeTags<'_>) -> Result<Vec<Planned>, String> {
let mut planned = Vec::new();
for source in sources {
let notes = source.read()?;
let scope = if source.project.is_some() { "project" } else { "global" };
let filters = tags(source.project.as_deref());
let mut seen: BTreeMap<String, usize> = BTreeMap::new();
for note in notes {
let count = seen.entry(note_key(source.harness, ¬e, 1)).or_default();
*count += 1;
let key = note_key(source.harness, ¬e, *count);
let situation = clean(¬e.situation).replace(['\n', '\t'], " ");
let situation = match truncate_bytes(&situation, MAX_SITUATION_BYTES).trim() {
"" => "from the user's imported notes".to_string(),
text => text.to_string(),
};
let content = clean(¬e.content);
let typed = Typed {
kind: note.kind.to_string(),
key: Some(key.clone()),
situation,
path_globs: Vec::new(),
source: Some(format!(
"{} {}",
source.harness.label(),
home_relative(¬e.file, home)
)),
};
let file_sha = std::fs::read(¬e.file)
.map(|bytes| jsrt::hash::sha256_hex(&bytes))
.map_err(|e| format!("{}: {e}", note.file.display()))?;
let action = if content.len() > content_cap(note.kind) {
Action::Refuse(format!(
"over the {}-byte cap of a {} record",
content_cap(note.kind),
note.kind
))
} else if host::secrets::contains_sensitive_memory(&content)
|| host::secrets::contains_sensitive_memory(&typed.screened())
{
Action::Refuse("holds a secret or personal identifier".into())
} else {
match index.by_key(&key, scope, &filters)? {
Some(entry)
if entry.text_hash == jsrt::hash::sha256_hex(content.as_bytes()) && entry.typed == typed =>
{
Action::Unchanged(entry.id)
}
Some(entry) => Action::Replace(entry.id),
None => Action::Add,
}
};
planned.push(Planned {
typed,
content,
scope,
project: source.project.clone(),
file_sha,
action,
});
}
}
Ok(planned)
}
pub fn propose_payload(planned: &Planned) -> Map<String, Value> {
let typed = &planned.typed;
let source = typed.source.clone().unwrap_or_default();
let mut payload = json!({
"text": planned.content,
"evidence": [{"ref": source, "sha256": planned.file_sha}],
"scope": planned.scope,
"evidenceClass": "user_statement",
"type": typed.kind,
"situation": typed.situation,
"source": source,
});
if let Some(key) = &typed.key {
payload["key"] = key.clone().into();
}
payload.as_object().cloned().unwrap_or_default()
}
pub fn import_one(service: &MemoryService, planned: &Planned) -> Result<Value, MemoryError> {
service.propose(&propose_payload(planned), &Signal::new())
}
pub type BackendFor<'a> = &'a dyn Fn(Option<&Path>) -> Result<Arc<dyn MemoryBackend>, String>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Imported {
Stored(String),
Replaced(String),
Unchanged,
Refused(String),
NotStored(String),
Failed(String),
}
pub fn import_all(
planned: &[Planned],
journal: &Path,
index: Arc<RecordIndex>,
backend_for: BackendFor<'_>,
each: &mut dyn FnMut(&Planned, Imported) -> Result<(), String>,
) -> Result<(), String> {
let mut service: Option<(Option<PathBuf>, MemoryService)> = None;
for item in planned {
let outcome = match &item.action {
Action::Unchanged(_) => Imported::Unchanged,
Action::Refuse(reason) => Imported::Refused(reason.clone()),
Action::Add | Action::Replace(_) => {
if service.as_ref().is_none_or(|(project, _)| *project != item.project) {
let memory = MemoryService::new(
Arc::new(FileDocumentStore::new(journal)),
backend_for(item.project.as_deref())?,
worker_gate(),
)
.map_err(|e| e.to_string())?
.with_records(index.clone());
service = Some((item.project.clone(), memory));
}
let Some((_, memory)) = &service else {
continue;
};
match import_one(memory, item) {
Ok(op) if op["state"] == "sensitive" || op["state"] == "skipped" => {
Imported::Refused("holds a secret or personal identifier".into())
}
Ok(op) if op["phase"] == "backend" => {
let id = op["memoryId"].as_str().unwrap_or_default().to_string();
match item.action {
Action::Replace(_) => Imported::Replaced(id),
_ => Imported::Stored(id),
}
}
Ok(op) => Imported::NotStored(op["state"].as_str().unwrap_or("?").to_string()),
Err(error) => Imported::Failed(error.to_string()),
}
}
};
each(item, outcome)?;
}
Ok(())
}