Skip to content

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

Rust

fix(consolidate): bound raw session context

AkitaOnRails17d7c46f92b9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index deba33f5..5a4f6dba 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,6 +42,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
(default 32 events).
### Fixed
+- Bounded heuristic session-page raw observation dumps and single-page
+ consolidation prompts so very large sessions cannot re-include unbounded
+ `## Raw observations` history (issue #102).
- Hook spool no longer counts a server `429` (saturation / `hook queue full`)
against a spooled event's `MAX_ATTEMPTS` retry budget: transient backpressure
keeps the event queued without burning an attempt (`MAX_AGE_MS` still bounds
diff --git a/crates/ai-memory-consolidate/src/consolidator.rs b/crates/ai-memory-consolidate/src/consolidator.rs
index 5fa113e8..fa630c8d 100644
--- a/crates/ai-memory-consolidate/src/consolidator.rs
+++ b/crates/ai-memory-consolidate/src/consolidator.rs
@@ -617,8 +617,9 @@ fn build_request(
}
}
if !current_body.trim().is_empty() {
+ let current_body = prepare_current_body_for_prompt(current_body);
buf.push_str("\nCurrent (heuristic) page body:\n\n```\n");
- buf.push_str(current_body);
+ buf.push_str(&current_body);
buf.push_str("\n```\n");
}
@@ -650,6 +651,7 @@ fn build_request(
/// differently; under-shooting the budget loses some context but never
/// causes a 400 from the provider.
const OBSERVATION_BUDGET_CHARS: usize = 400_000;
+const CURRENT_BODY_BUDGET_CHARS: usize = 20_000;
/// Per-observation char-cost estimate used by [`window_observations_to_budget`]
/// to predict how much each entry will add to the prompt buffer. Matches
@@ -707,6 +709,46 @@ fn window_observations_to_budget(
(&observations[keep_from..], skipped)
}
+fn prepare_current_body_for_prompt(current_body: &str) -> String {
+ let without_raw = elide_raw_observations_section(current_body);
+ clip_current_body_for_prompt(&without_raw, CURRENT_BODY_BUDGET_CHARS)
+}
+
+fn elide_raw_observations_section(current_body: &str) -> String {
+ let Some(raw_start) = current_body.find("## Raw observations") else {
+ return current_body.to_string();
+ };
+
+ let after_raw = raw_start + "## Raw observations".len();
+ let raw_end = current_body[after_raw..]
+ .find("\n## ")
+ .map(|offset| after_raw + offset + 1)
+ .unwrap_or(current_body.len());
+
+ let mut out = String::with_capacity(current_body.len().saturating_sub(raw_end - raw_start));
+ out.push_str(current_body[..raw_start].trim_end());
+ if !out.is_empty() {
+ out.push_str("\n\n");
+ }
+ out.push_str(
+ "[Raw observations section omitted; SQLite observations are supplied separately.]",
+ );
+ if raw_end < current_body.len() {
+ out.push_str("\n\n");
+ out.push_str(current_body[raw_end..].trim_start());
+ }
+ out
+}
+
+fn clip_current_body_for_prompt(s: &str, max_chars: usize) -> String {
+ let mut chars = s.chars();
+ let mut out: String = chars.by_ref().take(max_chars).collect();
+ if chars.next().is_some() {
+ out.push_str("\n[current heuristic page body truncated]");
+ }
+ out
+}
+
fn observation_label(obs: &Observation) -> String {
match (&obs.extension, &obs.source_event) {
(Some(extension), Some(source_event)) => {
@@ -906,6 +948,40 @@ mod tests {
assert_eq!(kept[0].id, obs[1].id);
}
+ #[test]
+ fn build_request_elides_raw_observations_from_current_body() {
+ let raw_dump = (0..2_000)
+ .map(|i| format!("- `other` @ 1970-01-01T00:00:00Z — raw-entry-{i}"))
+ .collect::<Vec<_>>()
+ .join("\n");
+ let current_body = format!(
+ "# session\n\nKeep this summary.\n\n## Raw observations\n\n{raw_dump}\n\n_Synthesised by ai-memory._\n"
+ );
+
+ let request = build_request(SessionId::new(), &[], &current_body);
+ let prompt = &request.messages[0].content;
+
+ assert!(prompt.contains("Keep this summary."));
+ assert!(prompt.contains("Raw observations section omitted"));
+ assert!(!prompt.contains("raw-entry-0"));
+ assert!(!prompt.contains("raw-entry-1999"));
+ }
+
+ #[test]
+ fn build_request_clips_large_current_body_with_marker() {
+ let current_body = format!(
+ "# huge\n\n{}\n\n## Raw observations\n\n- should-not-appear\n",
+ "x".repeat(CURRENT_BODY_BUDGET_CHARS + 10_000),
+ );
+
+ let request = build_request(SessionId::new(), &[], &current_body);
+ let prompt = &request.messages[0].content;
+
+ assert!(prompt.contains("[current heuristic page body truncated]"));
+ assert!(!prompt.contains("should-not-appear"));
+ assert!(prompt.len() < current_body.len());
+ }
+
/// Slugifier produces a clean ASCII path for typical English titles.
#[test]
fn slugify_handles_typical_rule_title() {
diff --git a/crates/ai-memory-hooks/src/synth.rs b/crates/ai-memory-hooks/src/synth.rs
index 4266848d..cd759735 100644
--- a/crates/ai-memory-hooks/src/synth.rs
+++ b/crates/ai-memory-hooks/src/synth.rs
@@ -13,6 +13,10 @@ use ai_memory_core::{
};
use jiff::tz::TimeZone;
+const RAW_OBSERVATION_MAX_LINES: usize = 500;
+const RAW_OBSERVATION_HEAD_LINES: usize = 250;
+const RAW_OBSERVATION_TAIL_LINES: usize = RAW_OBSERVATION_MAX_LINES - RAW_OBSERVATION_HEAD_LINES;
+
/// Build a [`NewPage`] from the observations collected during a session.
///
/// The returned page is *always* under `sessions/<session-id>.md` so each
@@ -114,20 +118,42 @@ fn render_body(session_id: SessionId, observations: &[Observation], title: &str)
}
buf.push_str("## Raw observations\n\n");
- for obs in observations {
- let kind = observation_kind_label(obs);
- buf.push_str(&format!(
- "- `{}` @ {} — {}\n",
- kind,
- human_ts(&obs.created_at),
- obs.title.chars().take(80).collect::<String>(),
- ));
- }
+ render_raw_observations(&mut buf, observations);
buf.push_str("\n_Synthesised by ai-memory (M3, no-LLM heuristic)._\n");
buf
}
+fn render_raw_observations(buf: &mut String, observations: &[Observation]) {
+ if observations.len() <= RAW_OBSERVATION_MAX_LINES {
+ for obs in observations {
+ render_raw_observation(buf, obs);
+ }
+ return;
+ }
+
+ for obs in &observations[..RAW_OBSERVATION_HEAD_LINES] {
+ render_raw_observation(buf, obs);
+ }
+ let omitted = observations.len() - RAW_OBSERVATION_HEAD_LINES - RAW_OBSERVATION_TAIL_LINES;
+ buf.push_str(&format!(
+ "\n_... {omitted} raw observations omitted from the middle (showing first {RAW_OBSERVATION_HEAD_LINES} and last {RAW_OBSERVATION_TAIL_LINES})._\n\n",
+ ));
+ for obs in &observations[observations.len() - RAW_OBSERVATION_TAIL_LINES..] {
+ render_raw_observation(buf, obs);
+ }
+}
+
+fn render_raw_observation(buf: &mut String, obs: &Observation) {
+ let kind = observation_kind_label(obs);
+ buf.push_str(&format!(
+ "- `{}` @ {} — {}\n",
+ kind,
+ human_ts(&obs.created_at),
+ obs.title.chars().take(80).collect::<String>(),
+ ));
+}
+
fn observation_kind_label(obs: &Observation) -> String {
match (&obs.extension, &obs.source_event) {
(Some(extension), Some(source_event)) => {
@@ -242,4 +268,48 @@ mod tests {
assert!(page.body.contains("`other [fstech:lead.contact]`"));
}
+
+ #[test]
+ fn raw_observations_small_session_includes_all_entries() {
+ let observations: Vec<Observation> = (0..5)
+ .map(|i| obs(ObservationKind::Other, &format!("entry-{i}")))
+ .collect();
+
+ let page = synthesize_session_page(
+ WorkspaceId::new(),
+ ProjectId::new(),
+ SessionId::new(),
+ &observations,
+ );
+
+ for i in 0..5 {
+ assert!(page.body.contains(&format!("entry-{i}")));
+ }
+ assert!(!page.body.contains("raw observations omitted"));
+ }
+
+ #[test]
+ fn raw_observations_large_session_omits_middle_with_count() {
+ let observations: Vec<Observation> = (0..600)
+ .map(|i| obs(ObservationKind::Other, &format!("entry-{i}")))
+ .collect();
+
+ let page = synthesize_session_page(
+ WorkspaceId::new(),
+ ProjectId::new(),
+ SessionId::new(),
+ &observations,
+ );
+
+ assert!(page.body.contains("entry-0"));
+ assert!(page.body.contains("entry-249"));
+ assert!(!page.body.contains("entry-250"));
+ assert!(!page.body.contains("entry-349"));
+ assert!(page.body.contains("entry-350"));
+ assert!(page.body.contains("entry-599"));
+ assert!(
+ page.body
+ .contains("100 raw observations omitted from the middle")
+ );
+ }
}