Skip to content

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

Rust

fix(profile): harvest only the text the user wrote

Felipe Neves Ricardo080655bd2b40
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 70d75cad..888e3198 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
so `backfill` stored the global file as a user prompt in every project, and
the profile harvest admitted its "always"/"never" lines, which repeat across
projects by construction. The import now recognizes both headings. (#1173)
+- Fixed the profile harvest reading text the user did not write as the
+ user's words: a user-prompt observation that is a harness turn, such as
+ Claude Code's `<task-notification>` blocks or Codex's injected
+ instructions, is now skipped, and ai-memory's own routing and
+ recalled-history blocks inside a prompt are left out, including a block the
+ 16 KiB prompt cap cut before its end marker. Codex instructions an earlier
+ version stored as prompts are skipped too when `profile rebuild` reads them
+ again. An entry an earlier version already admitted stays until
+ `ai-memory profile forget` removes it. (#1173)
- Fixed Cursor tool calls being stored with no title or content: Cursor's own
hooks, and the Claude Code hooks Cursor also runs, send Claude's
`tool_name`/`tool_input` fields, but Cursor was missing from the tool-capture
diff --git a/crates/ai-memory-consolidate/src/profile.rs b/crates/ai-memory-consolidate/src/profile.rs
index 0836b4ce..21d73414 100644
--- a/crates/ai-memory-consolidate/src/profile.rs
+++ b/crates/ai-memory-consolidate/src/profile.rs
@@ -536,6 +536,50 @@ pub struct DetectedPreference {
pub confidence: Option<f64>,
}
+/// The part of a stored prompt the user wrote, or `None` when the whole turn
+/// is harness context. A user-prompt observation can carry text no one typed:
+/// a harness block delivered as a user turn (`<task-notification>`), the
+/// instruction files Codex injects, or ai-memory's own routing snippet and
+/// recalled history pasted along. Harvesting those turns the repository's
+/// rules, or ai-memory's security notice, into "the user's" preferences.
+/// Checked here as well as at import: hooks store harness turns as prompts,
+/// and `profile rebuild` re-reads observations stored before an import filter
+/// existed.
+fn user_written(body: &str) -> Option<String> {
+ if ai_memory_core::looks_like_markup_block(body)
+ || ai_memory_core::looks_like_codex_instructions(body)
+ {
+ return None;
+ }
+ let text = without_fenced(
+ body,
+ ai_memory_core::routing_snippet::MARKER_START,
+ ai_memory_core::routing_snippet::MARKER_END,
+ );
+ Some(without_fenced(
+ &text,
+ ai_memory_core::profile::UNTRUSTED_HISTORY_START,
+ ai_memory_core::profile::UNTRUSTED_HISTORY_END,
+ ))
+}
+
+/// `text` without the regions between `start` and `end`. An unclosed `start`
+/// drops the rest: the 16 KiB prompt cap can cut a block before its end.
+fn without_fenced(text: &str, start: &str, end: &str) -> String {
+ let mut out = String::new();
+ let mut rest = text;
+ while let Some(open) = rest.find(start) {
+ out.push_str(&rest[..open]);
+ let inner = &rest[open + start.len()..];
+ match inner.find(end) {
+ Some(close) => rest = &inner[close + end.len()..],
+ None => return out,
+ }
+ }
+ out.push_str(rest);
+ out
+}
+
/// Sentences of `text` outside fenced code blocks and indented code, each
/// trimmed and collapsed to one line.
fn sentences(text: &str) -> Vec<String> {
@@ -1851,21 +1895,21 @@ async fn harvest_project(
let mut pending: Vec<PendingSentence> = Vec::new();
for prompt in &prompts {
mark.observations_until = mark.observations_until.max(prompt.created_at);
+ let Some(text) = user_written(&prompt.body) else {
+ continue;
+ };
let session_ref = format!("session:{}", prompt.session_id);
if llm.is_some() {
- pending.extend(
- sentences(&prompt.body)
- .into_iter()
- .filter(|s| has_marker(s))
- .map(|sentence| PendingSentence {
- sentence,
- observed_at: prompt.created_at,
- session_ref: session_ref.clone(),
- contributor: prompt.contributor.clone(),
- }),
- );
+ pending.extend(sentences(&text).into_iter().filter(|s| has_marker(s)).map(
+ |sentence| PendingSentence {
+ sentence,
+ observed_at: prompt.created_at,
+ session_ref: session_ref.clone(),
+ contributor: prompt.contributor.clone(),
+ },
+ ));
} else {
- for found in detect_preferences(&prompt.body) {
+ for found in detect_preferences(&text) {
candidates.push(candidate_from(
ProfileCandidateSource::Prompt,
session_ref.clone(),
diff --git a/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs b/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs
index a7565ac3..25453090 100644
--- a/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs
+++ b/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs
@@ -379,6 +379,53 @@ async fn tool_output_is_never_a_candidate() {
assert!(statements.iter().any(|s| s.contains("pnpm")));
}
+/// Text a harness or ai-memory put into a user-prompt observation is never
+/// harvested as the user's words: the instruction files Codex injects as a
+/// user turn (stored before the import filter knew its bare heading, and read
+/// again by `profile rebuild`), a harness block delivered as a user turn, and
+/// ai-memory's own routing block pasted into a prompt, even when the 16 KiB
+/// cap cut it before its end marker. The user's own sentence next to the
+/// pasted block is harvested (control).
+#[tokio::test]
+async fn injected_instructions_and_ai_memory_blocks_are_never_candidates() {
+ use ai_memory_core::routing_snippet::{MARKER_END, MARKER_START, SNIPPET_BODY};
+
+ let fx = fixture().await;
+ let alpha = project(&fx, "alpha").await;
+ let codex_turn = format!(
+ "# AGENTS.md instructions\n\n<INSTRUCTIONS>\n<!-- global-rules:start -->\n\
+ Always use yarn classic for installs.\n<!-- global-rules:end -->\n\n\
+ {MARKER_START}{SNIPPET_BODY}"
+ );
+ prompt(&fx, alpha, &codex_turn, 1).await;
+ prompt(
+ &fx,
+ alpha,
+ "<task-notification>\nAlways use yarn berry for installs.\n</task-notification>",
+ 2,
+ )
+ .await;
+ let pasted = format!(
+ "Always use pnpm for installs.\n\n{MARKER_START}\n\
+ Never write routine notes to memory by hand.\n{MARKER_END}\n"
+ );
+ prompt(&fx, alpha, &pasted, 3).await;
+ let truncated = format!("Here is my file:\n{MARKER_START}{SNIPPET_BODY}");
+ prompt(&fx, alpha, &truncated, 4).await;
+
+ pass(&fx, &single_user()).await;
+ let statements = candidate_statements(&fx);
+ assert!(
+ statements.iter().any(|s| s.contains("pnpm")),
+ "the user's own sentence was lost: {statements:?}"
+ );
+ assert_eq!(
+ statements.len(),
+ 1,
+ "injected text became a candidate: {statements:?}"
+ );
+}
+
/// `[profile] contribute = false` keeps a project out entirely; the same
/// words in a contributing project are harvested (control).
#[tokio::test]