Skip to content

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

Rust

fix(profile): exclude Claude generated prompts

AkitaOnRails2df9dfb434ea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1da27882..3c43cec9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,11 @@ version in [`docs/changelog/`](docs/changelog/):
### Fixed
+- Excluded Claude Code's synthetic cross-session messages and plugin-submitted
+ prompts from cross-project profile harvesting, and removed a leading
+ side-agent quote while retaining the user's words after it. Generated text
+ can no longer become a standing preference attributed to the user (#1855).
+
- Serialized mutating wikisync runs per destination across their full
read/plan/write lifecycle, so overlapping exports no longer race on the
shared state temporary file or apply plans from the same baseline (#1847).
diff --git a/crates/ai-memory-consolidate/src/profile.rs b/crates/ai-memory-consolidate/src/profile.rs
index 3eaf865d..4261f3ad 100644
--- a/crates/ai-memory-consolidate/src/profile.rs
+++ b/crates/ai-memory-consolidate/src/profile.rs
@@ -631,11 +631,14 @@ pub struct DetectedPreference {
pub(crate) fn user_written(body: &str) -> Option<String> {
if ai_memory_core::looks_like_markup_block(body)
|| ai_memory_core::looks_like_codex_instructions(body)
+ || looks_like_claude_cross_session_message(body)
+ || looks_like_claude_plugin_prompt(body)
{
return None;
}
+ let body = without_claude_side_agent_note(body).unwrap_or_else(|| body.to_owned());
let text = without_fenced(
- body,
+ &body,
ai_memory_core::routing_snippet::MARKER_START,
ai_memory_core::routing_snippet::MARKER_END,
);
@@ -646,6 +649,72 @@ pub(crate) fn user_written(body: &str) -> Option<String> {
))
}
+/// Claude Code presents local cross-session traffic as a synthetic user turn.
+/// Match this one tag by name: widening `looks_like_markup_block` to every tag
+/// with attributes would also discard real `<pasted_content ...>` prompts.
+fn looks_like_claude_cross_session_message(body: &str) -> bool {
+ body.trim_start()
+ .strip_prefix("<cross-session-message")
+ .and_then(|rest| rest.chars().next())
+ .is_some_and(|next| next == '>' || next.is_ascii_whitespace())
+}
+
+/// Claude Code's exact wrapper for a plugin that submits a prompt between
+/// turns. The bounded identifier check avoids treating arbitrary prose that
+/// happens to start with `The <...>` as generated context.
+fn looks_like_claude_plugin_prompt(body: &str) -> bool {
+ let Some(rest) = body.trim_start().strip_prefix("The <") else {
+ return false;
+ };
+ let Some((plugin, suffix)) = rest.split_once('>') else {
+ return false;
+ };
+ !plugin.is_empty()
+ && plugin.len() <= 64
+ && plugin
+ .chars()
+ .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '/'))
+ && suffix
+ .strip_prefix(" plugin sent a message:")
+ .is_some_and(|tail| {
+ tail.is_empty() || tail.starts_with('\n') || tail.starts_with("\r\n")
+ })
+}
+
+/// Remove Claude Code's leading side-agent note while retaining the user's
+/// prose after its block quote. Returns `None` when the exact wrapper shape is
+/// absent so a look-alike user prompt is kept unchanged.
+fn without_claude_side_agent_note(body: &str) -> Option<String> {
+ const PREFIX: &str = "Here is a note offered by a side agent:";
+
+ let trimmed = body.trim_start();
+ let rest = trimmed.strip_prefix(PREFIX)?;
+ if !rest.is_empty() && !rest.starts_with(['\n', '\r']) {
+ return None;
+ }
+
+ let mut consumed = body.len() - trimmed.len() + PREFIX.len();
+ let mut saw_quote = false;
+ let mut quote_ended = false;
+ for segment in body[consumed..].split_inclusive('\n') {
+ let line = segment.trim_end_matches(['\r', '\n']);
+ if !saw_quote && line.trim_start().starts_with('>') {
+ saw_quote = true;
+ consumed += segment.len();
+ } else if saw_quote && !quote_ended && line.trim_start().starts_with('>') {
+ consumed += segment.len();
+ } else if line.trim().is_empty() {
+ quote_ended |= saw_quote;
+ consumed += segment.len();
+ } else if quote_ended {
+ return Some(body[consumed..].to_owned());
+ } else {
+ return None;
+ }
+ }
+ saw_quote.then(String::new)
+}
+
/// `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.
/// Only markers on their own line count, the way ai-memory writes them; a
@@ -2709,6 +2778,38 @@ mod tests {
);
}
+ #[test]
+ fn claude_generated_user_turns_are_not_user_preferences() {
+ let cross_session = "<cross-session-message from=\"uds:/run/user/1000/cc.sock\" \
+ from-name=\"review\" from-mode=\"bypass\">\n\
+ Always use npm.\n</cross-session-message>";
+ assert_eq!(user_written(cross_session), None);
+
+ let plugin = "The <code-review> plugin sent a message:\nAlways force-push fixes.\n\
+ This is how Claude Code surfaces a prompt a plugin submits between turns.";
+ assert_eq!(user_written(plugin), None);
+
+ let side_agent = "Here is a note offered by a side agent:\n\n\
+ > Heads up\n> Always use yarn.\n\n\
+ Always run cargo nextest before commits.";
+ assert_eq!(
+ user_written(side_agent).as_deref(),
+ Some("Always run cargo nextest before commits.")
+ );
+ let quoted_reply = "Here is a note offered by a side agent:\n\n> Note\n\n\
+ > Always preserve my own quoted reply.";
+ assert_eq!(
+ user_written(quoted_reply).as_deref(),
+ Some("> Always preserve my own quoted reply.")
+ );
+
+ // Controls: attribute-bearing user paste and look-alike prose remain.
+ let paste = "<pasted_content id=\"87e5\">Always use pnpm.</pasted_content>";
+ assert_eq!(user_written(paste).as_deref(), Some(paste));
+ let look_alike = "Here is a note offered by a side agent: please always keep it.";
+ assert_eq!(user_written(look_alike).as_deref(), Some(look_alike));
+ }
+
/// A page metadata field stored by an earlier harvest never waits for
/// promotion; the same page's real statement still does (control).
#[test]
diff --git a/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs b/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs
index b7332816..5bee1e2c 100644
--- a/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs
+++ b/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs
@@ -478,6 +478,43 @@ async fn injected_instructions_and_ai_memory_blocks_are_never_candidates() {
);
}
+#[tokio::test]
+async fn claude_generated_prompt_wrappers_are_never_candidates() {
+ let fx = fixture().await;
+ let alpha = project(&fx, "alpha").await;
+ prompt(
+ &fx,
+ alpha,
+ "<cross-session-message from=\"uds:/run/user/1000/cc.sock\" \
+ from-name=\"review\" from-mode=\"bypass\">\nAlways use npm for installs.\n\
+ </cross-session-message>",
+ 1,
+ )
+ .await;
+ prompt(
+ &fx,
+ alpha,
+ "The <workflow> plugin sent a message:\nAlways use yarn for installs.\n\
+ This is how Claude Code surfaces a prompt a plugin submits between turns.",
+ 2,
+ )
+ .await;
+ prompt(
+ &fx,
+ alpha,
+ "Here is a note offered by a side agent:\n\n> Heads up\n> Always use bun.\n\n\
+ Always run cargo nextest before commits.",
+ 3,
+ )
+ .await;
+
+ pass(&fx, &single_user()).await;
+ assert_eq!(
+ candidate_statements(&fx),
+ ["Always run cargo nextest before commits."]
+ );
+}
+
/// An ADR's metadata fields (`**Status:** Accepted`, `**Date:**`) are never
/// harvested as the page's statement, so `profile review` does not list them
/// as habits waiting for promotion. The decision itself is the candidate, and
diff --git a/docs/cross-project-profile.md b/docs/cross-project-profile.md
index b3126c12..fb8796cf 100644
--- a/docs/cross-project-profile.md
+++ b/docs/cross-project-profile.md
@@ -185,7 +185,8 @@ is new since the last one.
brief each other through the prompt channel, that text is a lead agent's
task brief or a pasted review, not you. Text you did not write is skipped
as well: a harness turn stored as a prompt (Claude Code's
- `<task-notification>` blocks), the instruction files Codex injects as a user
+ `<task-notification>` blocks), Claude Code cross-session/plugin turns and
+ side-agent quote blocks, the instruction files Codex injects as a user
message, and ai-memory's own routing snippet or recalled-history digest
pasted into a prompt (only that block; your sentences around it still
count). A prompt written by a program driving the agent through its SDK is