Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
feat(consolidate): score non-empty Stop for the session reviewer (#196)
Samir Hanna Verzaf06dda434e09
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7005bd8a..a5835b0e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]### Added+- The session-review sampler now scores a non-empty `Stop` observation just+ below `PreCompact` (88 vs 90) instead of the low `55` prior, so the opt-in+ assistant/Stop excerpt (a late summary or correction) competes for a sampling+ slot without evicting user prompts. An empty `Stop` keeps its low prior. The+ reviewer still sees only the first 1500 characters of any observation body, so+ a correction inside a long excerpt must fall in that window ([#196]).- Opt-in assistant/Stop capture for Claude Code (#196). When BOTH the server(`capture_assistant = true` / `AI_MEMORY_CAPTURE_ASSISTANT=true`) and theclient (`install-hooks --agent claude-code --capture-assistant`) opt in, adiff --git a/crates/ai-memory-consolidate/src/auto_improve.rs b/crates/ai-memory-consolidate/src/auto_improve.rsindex b7e69ef6..8675427b 100644--- a/crates/ai-memory-consolidate/src/auto_improve.rs+++ b/crates/ai-memory-consolidate/src/auto_improve.rs@@ -50,6 +50,11 @@ const DEFAULT_REVIEW_MAX_TOKENS: u32 = 16_000;const MAX_SESSION_PAGE_CHARS: usize = 32_000;const SAMPLE_LIMIT_WITH_SESSION_PAGE: usize = 48;const SAMPLE_LIMIT_WITHOUT_SESSION_PAGE: usize = 72;+// Per-observation body cap in the reviewer projection. Note this is a SECOND,+// tighter truncation than storage: the opt-in assistant/Stop excerpt (#196) is+// persisted at up to 2 KB, but the reviewer only ever sees its first 1500 chars+// here (head-biased). A late correction inside a long Stop body must fall within+// this window to influence the reviewer.const MAX_OBSERVATION_BODY_CHARS: usize = 1_500;const PROMPT_SCAFFOLD_RESERVE_CHARS: usize = 4_000;const MAX_REJECTION_CONTEXT_CHARS: usize = 12_000;diff --git a/crates/ai-memory-consolidate/src/projection.rs b/crates/ai-memory-consolidate/src/projection.rsindex a3cfcda9..c175e720 100644--- a/crates/ai-memory-consolidate/src/projection.rs+++ b/crates/ai-memory-consolidate/src/projection.rs@@ -353,6 +353,12 @@ fn observation_score(obs: &Observation, idx: usize, total: usize) -> i32 {ObservationKind::SessionEnd => 95,ObservationKind::PreCompact => 90,ObservationKind::PostCompaction => 85,+ // A non-empty Stop carries the opt-in assistant excerpt (#196) — a+ // high-signal end-of-turn summary or late correction. Score it just+ // below PreCompact (90), above PostCompaction (85), so it competes for a+ // sampling slot without TYING any kind (ties resolve by position). An+ // empty Stop (capture off, or gated out) keeps its low prior.+ ObservationKind::Stop if !obs.body.trim().is_empty() => 88,ObservationKind::Stop => 55,ObservationKind::PostToolUse => 30,ObservationKind::Notification => 25,@@ -643,4 +649,102 @@ mod tests {assert!(projected.text.chars().count() <= cfg.max_total_chars);assert!(projected.text.contains("observations omitted"));}++ /// Lock the exact reviewer ordering for the opt-in Stop excerpt (#196, D4):+ /// a non-empty Stop scores below PreCompact (no tie — ties resolve by+ /// position), above PostCompaction, well above an empty Stop, and never+ /// outranks a UserPrompt (so a busy multi-turn session cannot let Stops+ /// crowd prompts out). Same idx/total/importance so only the kind term (and+ /// body-emptiness) differ — neutral text avoids the high-signal/anchor bonuses.+ #[test]+ fn non_empty_stop_score_sits_below_precompact_above_postcompaction() {+ let idx = 50;+ let total = 100;+ let imp = 5;+ let mk = |kind, body| observation_score(&obs(idx, kind, "note", body, imp), idx, total);++ let stop_full = mk(ObservationKind::Stop, "the assistant said hello");+ let stop_empty = mk(ObservationKind::Stop, "");+ let precompact = mk(ObservationKind::PreCompact, "the assistant said hello");+ let postcompaction = mk(ObservationKind::PostCompaction, "the assistant said hello");+ let user_prompt = mk(ObservationKind::UserPrompt, "the assistant said hello");++ // Kind terms: Stop-nonempty 88, PreCompact 90, PostCompaction 85,+ // Stop-empty 55, UserPrompt 100. Everything else cancels.+ assert_eq!(stop_full, precompact - 2, "must sit just below PreCompact");+ assert_eq!(+ stop_full,+ postcompaction + 3,+ "must sit above PostCompaction"+ );+ assert_eq!(stop_full, stop_empty + 33, "non-empty must beat empty Stop");+ assert!(+ user_prompt > stop_full,+ "a Stop must never outrank a prompt"+ );+ }++ /// A late non-empty Stop (the opt-in assistant excerpt with a correction) is+ /// selected out of a long routine session AND its text reaches the projection.+ #[test]+ fn non_empty_stop_late_correction_is_selected_and_rendered() {+ let mut observations: Vec<_> = (0..60)+ .map(|idx| obs(idx, ObservationKind::PostToolUse, "routine", "x", 1))+ .collect();+ // The assistant's final turn retracts an earlier claim — the exact signal+ // PR3 wants the reviewer to see. Kept within the excerpt window.+ observations[40] = obs(+ 40,+ ObservationKind::Stop,+ "stop",+ "CORRECTION_SENTINEL: the earlier fix was wrong; use config.rs instead",+ 6,+ );+ let cfg = ObservationProjectionConfig::new(20_000, 12, 2_000);+ let projected = project_observations(&observations, &cfg);++ assert!(+ projected.selected_indices.contains(&40),+ "non-empty Stop must win a sampling slot: {:?}",+ projected.selected_indices+ );+ assert!(+ projected.text.contains("CORRECTION_SENTINEL"),+ "the correction must reach the reviewer projection"+ );+ }++ /// The Stop=88 bump must not create a herd that evicts UserPrompts (100): in a+ /// busy multi-turn session, every prompt AND every non-empty Stop is selected.+ #[test]+ fn non_empty_stops_do_not_evict_user_prompts() {+ let mut observations: Vec<_> = (0..60)+ .map(|idx| obs(idx, ObservationKind::PostToolUse, "routine", "x", 1))+ .collect();+ let prompts = [10, 25, 45];+ let stops = [15, 30, 50];+ for &i in &prompts {+ observations[i] = obs(i, ObservationKind::UserPrompt, "prompt", "a request", 5);+ }+ for &i in &stops {+ observations[i] = obs(i, ObservationKind::Stop, "stop", "assistant summary", 5);+ }+ let cfg = ObservationProjectionConfig::new(20_000, 12, 2_000);+ let projected = project_observations(&observations, &cfg);++ for i in prompts {+ assert!(+ projected.selected_indices.contains(&i),+ "UserPrompt at {i} was evicted: {:?}",+ projected.selected_indices+ );+ }+ for i in stops {+ assert!(+ projected.selected_indices.contains(&i),+ "non-empty Stop at {i} was not selected: {:?}",+ projected.selected_indices+ );+ }+ }}