Skip to content

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

Rust

fix(hooks): keep harness markup blocks out of automatic handoffs

Cayanf7d710917829
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d3639798..d35c70a1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+- Fixed automatic handoffs carrying harness scaffolding as the user's
+ request: a user turn that opens with a markup block whose opening tag has
+ no attributes (for example the `<task-notification>` block Claude Code
+ delivers when a background task, agent or workflow completes) no longer
+ becomes the handoff's `Started:` / `Last:` summary, its `Continue from:`
+ line, or its `Unresolved question:` line. The handoff now uses the last
+ prompt the user actually wrote, and falls back to the no-prompt summary
+ when every prompt in the session is such a block. Prompts that only
+ contain markup later in the text, pastes with attributes such as
+ `<pasted_content id="…">`, and stored observations are unchanged.
+
## [2.6.1] - 2026-10-08
### Fixed
diff --git a/crates/ai-memory-core/src/lib.rs b/crates/ai-memory-core/src/lib.rs
index ab200355..11229a6a 100644
--- a/crates/ai-memory-core/src/lib.rs
+++ b/crates/ai-memory-core/src/lib.rs
@@ -23,7 +23,7 @@ pub mod repository_identity;
pub use repository_identity::{MARKER_FILENAME, MARKER_FILENAMES};
pub mod routing_skills;
pub mod scaffolding;
-pub use scaffolding::looks_like_scaffolding;
+pub use scaffolding::{looks_like_markup_block, looks_like_scaffolding};
pub mod routing_snippet;
pub mod sanitize;
pub mod slots;
diff --git a/crates/ai-memory-core/src/scaffolding.rs b/crates/ai-memory-core/src/scaffolding.rs
index 82403881..d720a61e 100644
--- a/crates/ai-memory-core/src/scaffolding.rs
+++ b/crates/ai-memory-core/src/scaffolding.rs
@@ -52,9 +52,26 @@ pub fn looks_like_scaffolding(candidate: &str) -> bool {
return true;
}
- // A markup/context block: `<ide_opened_file>…`, `<system-reminder>…`,
- // `<user_info>…`. Requires a closing bracket so a bare comparison like
- // "< 5ms is fine" stays prose.
+ if looks_like_markup_block(trimmed) {
+ return true;
+ }
+
+ // A decorated shell prompt echoed into the paste.
+ if trimmed.starts_with(PROMPT_DECORATION) {
+ return true;
+ }
+
+ false
+}
+
+/// `true` when `candidate` opens with a markup/context block:
+/// `<ide_opened_file>…`, `<system-reminder>…`, `<user_info>…`,
+/// `<task-notification>…`. Requires a closing bracket so a bare comparison
+/// like "< 5ms is fine" stays prose, and a bare tag name so an opening tag
+/// with attributes (`<pasted_content id="87e5">`, a user paste) stays prose.
+#[must_use]
+pub fn looks_like_markup_block(candidate: &str) -> bool {
+ let trimmed = candidate.trim();
if let Some(rest) = trimmed.strip_prefix('<')
&& let Some(tag) = rest.split('>').next()
&& !tag.is_empty()
@@ -66,12 +83,6 @@ pub fn looks_like_scaffolding(candidate: &str) -> bool {
{
return true;
}
-
- // A decorated shell prompt echoed into the paste.
- if trimmed.starts_with(PROMPT_DECORATION) {
- return true;
- }
-
false
}
diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rs
index 5d5076f9..1b511435 100644
--- a/crates/ai-memory-hooks/src/router.rs
+++ b/crates/ai-memory-hooks/src/router.rs
@@ -4155,7 +4155,9 @@ fn build_auto_handoff(
match obs.kind {
ObservationKind::UserPrompt => {
let text = pick_text(obs);
- if !text.is_empty() {
+ // A harness block delivered as a user turn (`<task-notification>`)
+ // is not a request to continue from.
+ if !text.is_empty() && !ai_memory_core::looks_like_markup_block(text) {
prompts.push(text.to_string());
}
}
@@ -4259,7 +4261,11 @@ fn derive_open_questions(
observations.iter().rev().find(|o| o.kind == kind)
};
- let last_prompt_obs = last_of_kind(ObservationKind::UserPrompt);
+ // Harness blocks delivered as a user turn are not the user's question.
+ let last_prompt_obs = observations.iter().rev().find(|o| {
+ let text = if o.body.is_empty() { &o.title } else { &o.body };
+ o.kind == ObservationKind::UserPrompt && !ai_memory_core::looks_like_markup_block(text)
+ });
let last_tool = last_of_kind(ObservationKind::PostToolUse);
let last_stop = last_of_kind(ObservationKind::Stop);
let has_session_end = observations
@@ -18179,6 +18185,177 @@ mod tests {
);
}
+ const TASK_NOTIFICATION: &str = "<task-notification>\n<task-id>abc</task-id>\n\
+ <status>completed</status>\n<summary>Agent \"review\" completed</summary>\n\
+ </task-notification>";
+
+ fn handoff_from(
+ observations: &[ai_memory_core::Observation],
+ turn_checkpoint: bool,
+ ) -> NewHandoff {
+ use ai_memory_core::{ProjectId, WorkspaceId};
+ build_auto_handoff(
+ WorkspaceId::new(),
+ ProjectId::new(),
+ AgentKind::ClaudeCode,
+ SessionId::new(),
+ None,
+ observations,
+ None,
+ turn_checkpoint,
+ )
+ }
+
+ #[test]
+ fn auto_handoff_skips_a_trailing_task_notification_prompt() {
+ let observations = vec![
+ mk_obs(ObservationKind::UserPrompt, "start", "map the sweep code"),
+ mk_obs(
+ ObservationKind::UserPrompt,
+ "later",
+ "fix the pinned-page sweep",
+ ),
+ mk_obs(
+ ObservationKind::UserPrompt,
+ "<task-notification>",
+ TASK_NOTIFICATION,
+ ),
+ ];
+ let handoff = handoff_from(&observations, false);
+ assert_eq!(
+ handoff.summary,
+ "Started: map the sweep code\n\nLast: fix the pinned-page sweep"
+ );
+ assert_eq!(
+ handoff.open_questions,
+ vec!["Continue from: fix the pinned-page sweep".to_string()]
+ );
+ assert!(!handoff.summary.contains("task-notification"));
+ assert!(
+ handoff
+ .open_questions
+ .iter()
+ .all(|q| !q.contains("task-notification"))
+ );
+ }
+
+ #[test]
+ fn auto_handoff_with_only_task_notification_prompts_uses_the_no_prompt_fallback() {
+ let observations = vec![
+ mk_obs(
+ ObservationKind::UserPrompt,
+ "<task-notification>",
+ TASK_NOTIFICATION,
+ ),
+ mk_obs(
+ ObservationKind::UserPrompt,
+ "<task-notification>",
+ TASK_NOTIFICATION,
+ ),
+ ];
+ let handoff = handoff_from(&observations, false);
+ assert_eq!(handoff.summary, "Session ended; 2 observations recorded.");
+ assert!(
+ handoff.open_questions.is_empty(),
+ "got: {:?}",
+ handoff.open_questions
+ );
+ }
+
+ #[test]
+ fn auto_handoff_does_not_treat_a_task_notification_question_as_unresolved() {
+ let notification = "<task-notification>\n<task-id>abc</task-id>\n\
+ <status>completed</status>\n<summary>Should the docs change too?";
+ let observations = vec![
+ mk_obs(
+ ObservationKind::UserPrompt,
+ "fix",
+ "fix the pinned-page sweep",
+ ),
+ mk_obs(
+ ObservationKind::UserPrompt,
+ "<task-notification>",
+ notification,
+ ),
+ ];
+ let handoff = handoff_from(&observations, false);
+ assert!(
+ handoff
+ .open_questions
+ .iter()
+ .all(|q| !q.starts_with("Unresolved question:")),
+ "got: {:?}",
+ handoff.open_questions
+ );
+ }
+
+ #[test]
+ fn turn_checkpoint_continues_from_the_real_prompt_before_a_task_notification() {
+ let observations = vec![
+ mk_obs(
+ ObservationKind::UserPrompt,
+ "fix",
+ "fix the pinned-page sweep",
+ ),
+ mk_obs(
+ ObservationKind::UserPrompt,
+ "<task-notification>",
+ TASK_NOTIFICATION,
+ ),
+ ];
+ let handoff = handoff_from(&observations, true);
+ assert_eq!(
+ handoff.open_questions,
+ vec!["Continue from last request: fix the pinned-page sweep".to_string()]
+ );
+ }
+
+ #[test]
+ fn auto_handoff_keeps_a_prompt_with_markup_after_its_first_word() {
+ let observations = vec![mk_obs(
+ ObservationKind::UserPrompt,
+ "fix",
+ "fix <div> alignment",
+ )];
+ let handoff = handoff_from(&observations, false);
+ assert_eq!(handoff.summary, "Session focused on: fix <div> alignment");
+ assert_eq!(
+ handoff.open_questions,
+ vec!["Continue from: fix <div> alignment".to_string()]
+ );
+ }
+
+ #[test]
+ fn auto_handoff_keeps_a_pasted_content_prompt() {
+ let pasted = "<pasted_content id=\"5b6f\">\nEu quero inicializar um harness";
+ let observations = vec![
+ mk_obs(ObservationKind::UserPrompt, "start", "map the sweep code"),
+ mk_obs(ObservationKind::UserPrompt, "paste", pasted),
+ ];
+ let handoff = handoff_from(&observations, false);
+ assert!(
+ handoff.summary.ends_with(&format!("Last: {pasted}")),
+ "got: {}",
+ handoff.summary
+ );
+ assert_eq!(
+ handoff.open_questions,
+ vec![format!("Continue from: {pasted}")]
+ );
+ }
+
+ #[test]
+ fn auto_handoff_keeps_a_prompt_decorated_prompt() {
+ let prompt = "\u{276f} rode o workflow de arquitetura";
+ let observations = vec![mk_obs(ObservationKind::UserPrompt, "run", prompt)];
+ let handoff = handoff_from(&observations, false);
+ assert_eq!(handoff.summary, format!("Session focused on: {prompt}"));
+ assert_eq!(
+ handoff.open_questions,
+ vec![format!("Continue from: {prompt}")]
+ );
+ }
+
/// A search/list tool is file-adjacent but not file activity; it must
/// fall through to the prompt-based heuristics.
#[test]