Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
feat(profile): warn on digest truncation
AkitaOnRails3eb624536c13
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex daa70815..2bb09d2d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,11 @@ version in [`docs/changelog/`](docs/changelog/):### Added+- Profile writes now return `digest_warning` when the selected summary,+ abstract, or prose statement exceeds the 240-byte session-start digest+ limit. The write still succeeds, but its author can shorten the summary+ instead of discovering the truncation only in a later session (#1609).+- Added project routing for the named profiles of a multiplexing Hermesgateway. The gateway exports only the default profile's `terminal.cwd`, soevery other profile's events used to fall back to the gateway's own workingdiff --git a/crates/ai-memory-core/src/profile.rs b/crates/ai-memory-core/src/profile.rsindex bc812b84..4c2b29aa 100644--- a/crates/ai-memory-core/src/profile.rs+++ b/crates/ai-memory-core/src/profile.rs@@ -472,18 +472,7 @@ impl ProfileEntry {body: &str,frontmatter: &serde_json::Value,) -> Option<Self> {- let from_frontmatter = ["summary", "abstract"].iter().find_map(|key| {- frontmatter- .get(key)- .and_then(serde_json::Value::as_str)- .map(str::trim)- .filter(|value| !value.is_empty())- .map(str::to_owned)- });- let statement = from_frontmatter- .or_else(|| first_prose_line(body))- .or_else(|| Some(title.trim().to_owned()).filter(|t| !t.is_empty()))?;- let statement = collapse_whitespace(&statement);+ let statement = profile_statement(title, body, frontmatter)?;let statement = truncate_bytes(&statement, ENTRY_STATEMENT_MAX_BYTES).to_owned();let applies_to = match frontmatter.get("applies_to") {Some(serde_json::Value::String(one)) => vec![one.trim().to_ascii_lowercase()],@@ -547,6 +536,29 @@ impl ProfileEntry {}}+/// Select and normalize the uncapped statement a profile digest would use.+/// Writers use this before persistence so any truncation warning and the+/// eventual session-start digest cannot disagree about the source text.+#[must_use]+pub fn profile_statement(+ title: &str,+ body: &str,+ frontmatter: &serde_json::Value,+) -> Option<String> {+ let from_frontmatter = ["summary", "abstract"].iter().find_map(|key| {+ frontmatter+ .get(key)+ .and_then(serde_json::Value::as_str)+ .map(str::trim)+ .filter(|value| !value.is_empty())+ .map(str::to_owned)+ });+ from_frontmatter+ .or_else(|| first_prose_line(body))+ .or_else(|| Some(title.trim().to_owned()).filter(|title| !title.is_empty()))+ .map(|statement| collapse_whitespace(&statement))+}+fn first_prose_line(body: &str) -> Option<String> {body.lines().map(str::trim)diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rsindex 79466db5..ed516652 100644--- a/crates/ai-memory-mcp/src/server.rs+++ b/crates/ai-memory-mcp/src/server.rs@@ -4509,7 +4509,9 @@ impl AiMemoryServer {tags such as `rust`, limiting it to projects on that stack) and \`enforced_by` (what already enforces it, which keeps it out of the \digest); other pages refuse both. `scope: \"global\"` writes the \- reserved `_global` scope directly. \+ reserved `_global` scope directly. A profile write whose selected \+ statement exceeds the session-start digest limit succeeds and returns \+ `digest_warning`, so shorten its summary when the whole rule matters. \\Optional `kind`, `entities`, `abstract`, and `relations` carry bounded \metadata. This replaces the whole page; omitted metadata is cleared. \@@ -4771,6 +4773,25 @@ impl AiMemoryServer {} else {serde_json::Value::Object(fm)};+ let digest_warning = profile_scope+ .then(|| {+ ai_memory_core::profile::profile_statement(+ title.as_deref().unwrap_or_default(),+ &body,+ &frontmatter,+ )+ })+ .flatten()+ .filter(|statement| {+ statement.len() > ai_memory_core::profile::ENTRY_STATEMENT_MAX_BYTES+ })+ .map(|statement| {+ format!(+ "The session-start profile digest shows only the first {} bytes of this entry's {}-byte statement. Set a shorter summary to keep the whole rule visible.",+ ai_memory_core::profile::ENTRY_STATEMENT_MAX_BYTES,+ statement.len()+ )+ });// Loop prevention: a webhook that writes back into the engine sets// `X-Memory-Skip-Admission-Chain` so the chain doesn't re-invoke it@@ -4842,6 +4863,9 @@ impl AiMemoryServer {if let Some(warning) = manifest_warning {result["manifest_warning"] = serde_json::Value::String(warning.to_string());}+ if let Some(warning) = digest_warning {+ result["digest_warning"] = serde_json::Value::String(warning);+ }ok_json(&result)}@@ -17792,6 +17816,53 @@ mod tests {.expect_err("scope: profile is refused while the profile is off");}+ #[tokio::test]+ async fn profile_write_warns_when_its_digest_statement_will_be_cut() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let app = store+ .writer+ .get_or_create_project(ws, "app", None)+ .await+ .unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ let server = AiMemoryServer::new(store.reader.clone(), store.writer.clone(), ws, app)+ .with_wiki(wiki);++ let mut long = profile_entry("habits/long.md", Some("profile"));+ long.metadata.abstract_text = Some("x".repeat(300));+ let result = server+ .memory_write_page(Parameters(long), OptionalParts(test_parts_default()))+ .await+ .unwrap();+ let json = call_tool_json(result);+ assert!(json["digest_warning"].as_str().is_some_and(|warning| {+ warning.contains("240 bytes") && warning.contains("300-byte")+ }));++ let short = server+ .memory_write_page(+ Parameters(profile_entry("habits/short.md", Some("profile"))),+ OptionalParts(test_parts_default()),+ )+ .await+ .unwrap();+ assert!(call_tool_json(short).get("digest_warning").is_none());++ let mut project = profile_entry("notes/long.md", None);+ project.body = format!("# Long\n\n{}", "x".repeat(300));+ let project = server+ .memory_write_page(Parameters(project), OptionalParts(test_parts_default()))+ .await+ .unwrap();+ assert!(call_tool_json(project).get("digest_warning").is_none());+ }+/// `applies_to` and `enforced_by` reach a profile entry's frontmatter/// through the tool, in the shape the digest reads; a rewrite keeps them/// only when it passes them again (writes replace all metadata); an