Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(profile): mark a cut digest line with an ellipsis

Felipe Neves Ricardo196f9815b50d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3ffee8b8..8c738b85 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,12 @@ version in [`docs/changelog/`](docs/changelog/):
### Fixed
+- Fixed the profile digest cutting an entry's line at 240 bytes with no
+ sign of the cut: a `summary`, `abstract` or first prose line over the limit
+ was clipped mid-word, so the agent read part of a rule as the whole rule.
+ The line now ends in `…` inside the same 240 bytes, and
+ `docs/cross-project-profile.md` states the limit next to `summary`.
+ (#1608)
- Fixed a 3.2.0 regression that serialized local document embedding and
dropped the vector stream from queries that raced a document embed: the
local embedder's capacity was hardcoded to two slots (one document, one
diff --git a/crates/ai-memory-core/src/profile.rs b/crates/ai-memory-core/src/profile.rs
index bc812b84..9ee81ff4 100644
--- a/crates/ai-memory-core/src/profile.rs
+++ b/crates/ai-memory-core/src/profile.rs
@@ -13,6 +13,7 @@ use std::collections::BTreeSet;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::UserId;
+use crate::sanitize::truncate_utf8_bytes;
/// Path prefix every profile entry lives under, in whichever scope holds the
/// profile.
@@ -463,8 +464,9 @@ impl ProfileEntry {
///
/// The statement is the frontmatter `summary` or `abstract` when set,
/// otherwise the first line of prose in the body, collapsed to one line
- /// and capped at [`ENTRY_STATEMENT_MAX_BYTES`]. Returns `None` when the
- /// page has nothing to state.
+ /// and capped at [`ENTRY_STATEMENT_MAX_BYTES`]. A longer statement ends in
+ /// `…` inside that cap, so the reader can tell the rule goes on. Returns
+ /// `None` when the page has nothing to state.
#[must_use]
pub fn from_page(
path: &str,
@@ -484,7 +486,7 @@ impl ProfileEntry {
.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 = truncate_bytes(&statement, ENTRY_STATEMENT_MAX_BYTES).to_owned();
+ let statement = truncate_utf8_bytes(&statement, ENTRY_STATEMENT_MAX_BYTES);
let applies_to = match frontmatter.get("applies_to") {
Some(serde_json::Value::String(one)) => vec![one.trim().to_ascii_lowercase()],
Some(serde_json::Value::Array(many)) => many
@@ -624,18 +626,6 @@ fn collapse_whitespace(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
-/// Truncate to at most `max` bytes without splitting a UTF-8 character.
-fn truncate_bytes(text: &str, max: usize) -> &str {
- if text.len() <= max {
- return text;
- }
- let mut end = max;
- while end > 0 && !text.is_char_boundary(end) {
- end -= 1;
- }
- &text[..end]
-}
-
/// Opening fence around stored text in injected context.
pub const UNTRUSTED_HISTORY_START: &str = "<!-- ai-memory:untrusted-history:start -->";
/// Closing fence around stored text in injected context.
@@ -1109,6 +1099,38 @@ mod tests {
assert_eq!(e.category(), "");
}
+ #[test]
+ fn a_statement_over_the_cap_ends_in_an_ellipsis_inside_it() {
+ let entry = |statement: &str| {
+ ProfileEntry::from_page(
+ "profile/habits/a.md",
+ "A",
+ "# A\n",
+ &serde_json::json!({ "summary": statement }),
+ )
+ .unwrap()
+ .statement
+ };
+
+ // A cut mid-sentence used to read as the whole rule.
+ let long = format!("{} never commit the lockfile.", "word ".repeat(60));
+ let cut = entry(&long);
+ assert!(cut.len() <= ENTRY_STATEMENT_MAX_BYTES, "{}", cut.len());
+ assert!(cut.ends_with('…'), "{cut}");
+ assert!(long.starts_with(cut.trim_end_matches('…')));
+
+ // The ellipsis never lands inside a multibyte character.
+ let accented = "é".repeat(ENTRY_STATEMENT_MAX_BYTES);
+ let cut = entry(&accented);
+ assert!(cut.len() <= ENTRY_STATEMENT_MAX_BYTES, "{}", cut.len());
+ assert!(cut.ends_with('…'), "{cut}");
+ assert!(cut.trim_end_matches('…').chars().all(|c| c == 'é'));
+
+ // A statement that fits is left alone.
+ let exact = "x".repeat(ENTRY_STATEMENT_MAX_BYTES);
+ assert_eq!(entry(&exact), exact);
+ }
+
#[test]
fn metadata_fields_are_told_from_sentences() {
for line in [
diff --git a/docs/cross-project-profile.md b/docs/cross-project-profile.md
index 408a1fab..3a7d4654 100644
--- a/docs/cross-project-profile.md
+++ b/docs/cross-project-profile.md
@@ -95,7 +95,7 @@ folder: `stack`, `architecture`, `workflow`, `testing`, `tools`, `style` or
| Key | Meaning |
|---|---|
-| `summary` | The digest line (otherwise the first line of prose in the body). |
+| `summary` | The digest line (otherwise `abstract`, then the first line of prose in the body). The digest keeps at most 240 bytes of it and ends a longer line in `…`, so keep the rule itself inside that. |
| `applies_to` | Stack tags the entry is scoped to, e.g. `[rust]` or `typescript`. |
| `enforced_by` | Something already enforces it (e.g. `pre-push hook`); kept out of the digest. |
diff --git a/docs/design-tiered-session-brief.md b/docs/design-tiered-session-brief.md
index 26687838..73970462 100644
--- a/docs/design-tiered-session-brief.md
+++ b/docs/design-tiered-session-brief.md
@@ -144,7 +144,7 @@ They do not agree on clip size or prose granularity.
| Site | Order | Prose fallback | Clip |
|---|---|---|---|
-| Profile digest `ProfileEntry::from_page` (`profile.rs` ~452-475) | `summary` then `abstract` then first prose **line** (then title) | `first_prose_line`: skips headings, `---`, ADR metadata fields | 240 **bytes**, no ellipsis (`ENTRY_STATEMENT_MAX_BYTES`) |
+| Profile digest `ProfileEntry::from_page` (`profile.rs` ~452-475) | `summary` then `abstract` then first prose **line** (then title) | `first_prose_line`: skips headings, `---`, ADR metadata fields | 240 **bytes**, ellipsis (`ENTRY_STATEMENT_MAX_BYTES`) |
| A2 compaction `summary_line` (`crates/ai-memory-consolidate/src/compaction.rs` ~68-91) | `summary` then `abstract` then first non-empty, non-heading **paragraph** | collapse whitespace | `MAX_SUMMARY_LEN = 500` bytes, ellipsis |
| V61 / wiki `frontmatter_abstract` (`wiki.rs` ~3099, duplicated in `embed.rs`) | `abstract` only | none | store cap `MAX_PAGE_ABSTRACT_LEN = 1024` (`page.rs` ~166) |