Skip to content

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

Rust

fix(consolidate): keep terminal state, corrections, metrics and operator instructions (#1221)

AkitaOnRailsf4f5a58b2d31
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 092b52ce..60eed928 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
+- Fixed consolidation dropping the tail of a session from the compiled
+ page. The single and batch prompts now carry explicit rules: say so when
+ the observation log ends on an unanswered prompt or without a normal
+ stop, and record in-flight work (background jobs, watchers, timers) as
+ pending state; when a later observation corrects an earlier "done"
+ milestone, state only the final state with the superseded claim as
+ history at most; preserve the last reported metrics of in-flight work
+ (sizes, rates, counts, ETAs) verbatim; carry operator "run X when Y"
+ instructions verbatim, with their preconditions, into a
+ Next steps / Pending section; and, for sessions that operated on
+ external systems, emit an "Access and verification" section — hosts,
+ the per-host auth mechanism (never secrets or tokens themselves),
+ job-state marker locations, staged-script paths — grounded only in what
+ the observations show. (#1221)
- Softened the consolidation prompts' wikilink quota that pushed models to
invent links. The single and batch prompts told the model "2-5 well-chosen
links per page beat exhaustive linking; zero links should be rare", which
diff --git a/crates/ai-memory-consolidate/prompts/batch_consolidate_system.md b/crates/ai-memory-consolidate/prompts/batch_consolidate_system.md
index b2999fd8..e7ba6bd4 100644
--- a/crates/ai-memory-consolidate/prompts/batch_consolidate_system.md
+++ b/crates/ai-memory-consolidate/prompts/batch_consolidate_system.md
@@ -63,6 +63,27 @@ Do:
- If a session yields no durable insight, return only the
episodic session page. Resist the urge to manufacture content.
+## TERMINAL STATE AND CARRIED DETAIL
+
+1. Terminal state: when the observation log ends on an unanswered prompt
+ or ends without a normal stop, say so explicitly. Record work still
+ in flight (background jobs, watchers, timers) as pending state, never
+ as done.
+2. Late corrections: when a later observation corrects an earlier "done"
+ milestone, state only the final state; mention the superseded claim
+ as history at most, never as current fact.
+3. Final metrics: preserve the last reported quantitative state of
+ in-flight work verbatim — sizes, rates, counts, ETAs. Do not round,
+ paraphrase, or drop them.
+4. Operator instructions: when the user said "run X when Y", carry the
+ instruction verbatim, with its preconditions, into a
+ Next steps / Pending section.
+5. Access and verification: for sessions that operated on external
+ systems, add an "Access and verification" section — hosts, the auth
+ mechanism per host (NEVER secrets or tokens themselves),
+ job-state marker locations, staged-script paths — listing only what
+ the observations actually show. This is extraction, not scaffolding.
+
## WIKILINKS — connect pages into the graph
The wiki is a graph: pages reference each other with Obsidian-style
diff --git a/crates/ai-memory-consolidate/prompts/single_consolidate_system.md b/crates/ai-memory-consolidate/prompts/single_consolidate_system.md
index 075e005d..d59d1d88 100644
--- a/crates/ai-memory-consolidate/prompts/single_consolidate_system.md
+++ b/crates/ai-memory-consolidate/prompts/single_consolidate_system.md
@@ -41,6 +41,27 @@ observations, treat the most recent/final state as authoritative.
Superseded drafts, plans, errors, or assumptions may be mentioned as
history only when useful, but must not be presented as current fact.
+## TERMINAL STATE AND CARRIED DETAIL
+
+1. Terminal state: when the observation log ends on an unanswered prompt
+ or ends without a normal stop, say so explicitly. Record work still
+ in flight (background jobs, watchers, timers) as pending state, never
+ as done.
+2. Late corrections: when a later observation corrects an earlier "done"
+ milestone, state only the final state; mention the superseded claim
+ as history at most, never as current fact.
+3. Final metrics: preserve the last reported quantitative state of
+ in-flight work verbatim — sizes, rates, counts, ETAs. Do not round,
+ paraphrase, or drop them.
+4. Operator instructions: when the user said "run X when Y", carry the
+ instruction verbatim, with its preconditions, into a
+ Next steps / Pending section.
+5. Access and verification: for sessions that operated on external
+ systems, add an "Access and verification" section — hosts, the auth
+ mechanism per host (NEVER secrets or tokens themselves),
+ job-state marker locations, staged-script paths — listing only what
+ the observations actually show. This is extraction, not scaffolding.
+
## Style rules
1. Title: short, descriptive (≤ 80 chars). No filler. Name THIS session
diff --git a/crates/ai-memory-consolidate/src/consolidator.rs b/crates/ai-memory-consolidate/src/consolidator.rs
index 503f57d6..3547b850 100644
--- a/crates/ai-memory-consolidate/src/consolidator.rs
+++ b/crates/ai-memory-consolidate/src/consolidator.rs
@@ -3010,6 +3010,63 @@ mod tests {
}
}
+ /// #1221: consolidation was losing the tail of a session — an
+ /// unanswered final prompt, a late correction to a "done" milestone,
+ /// the last reported metrics of in-flight work, operator "run X when Y"
+ /// instructions, and how to reach and verify external systems the
+ /// session touched. Each rule must be pinned in both prompts.
+ #[test]
+ fn consolidation_system_prompts_preserve_terminal_state_and_carried_detail() {
+ for (name, prompt) in [("single", SYSTEM_PROMPT), ("batch", BATCH_SYSTEM_PROMPT)] {
+ // 1. Terminal state: unanswered prompt / abnormal stop is said
+ // explicitly; in-flight work is pending state, not done.
+ assert!(prompt.contains("Terminal state"), "{name} prompt");
+ assert!(
+ prompt.contains("ends on an unanswered prompt"),
+ "{name} prompt"
+ );
+ assert!(prompt.contains("without a normal stop"), "{name} prompt");
+ assert!(prompt.contains("pending state"), "{name} prompt");
+ // 2. Late corrections: final state only, superseded claim is
+ // history at most.
+ assert!(prompt.contains("Late corrections"), "{name} prompt");
+ assert!(
+ prompt.contains("corrects an earlier \"done\""),
+ "{name} prompt"
+ );
+ assert!(
+ prompt.contains("state only the final state"),
+ "{name} prompt"
+ );
+ assert!(prompt.contains("as history at most"), "{name} prompt");
+ // 3. Final metrics of in-flight work, verbatim.
+ assert!(prompt.contains("Final metrics"), "{name} prompt");
+ assert!(
+ prompt.contains("sizes, rates, counts, ETAs"),
+ "{name} prompt"
+ );
+ // 4. Operator "run X when Y" instructions carried verbatim with
+ // their preconditions into Next steps / Pending.
+ assert!(prompt.contains("Operator instructions"), "{name} prompt");
+ assert!(prompt.contains("run X when Y"), "{name} prompt");
+ assert!(prompt.contains("preconditions"), "{name} prompt");
+ assert!(prompt.contains("Next steps / Pending"), "{name} prompt");
+ // 5. Access and verification for external systems: extraction
+ // grounded in the observations, never secrets themselves.
+ assert!(prompt.contains("Access and verification"), "{name} prompt");
+ assert!(
+ prompt.contains("NEVER secrets or tokens themselves"),
+ "{name} prompt"
+ );
+ assert!(prompt.contains("job-state marker"), "{name} prompt");
+ assert!(prompt.contains("staged-script paths"), "{name} prompt");
+ assert!(
+ prompt.contains("extraction, not scaffolding"),
+ "{name} prompt"
+ );
+ }
+ }
+
#[test]
fn build_request_elides_raw_observations_from_current_body() {
let raw_dump = (0..2_000)
@@ -3232,9 +3289,14 @@ mod tests {
/// The batch path must include schema and dynamic slot snapshots in its
/// envelope instead of assuming a fixed number of slots.
+ ///
+ /// The budget sits above the validated floor so the optional-context
+ /// block reaches its clipping regime (a visible `[truncated]` marker)
+ /// rather than being dropped whole: at the floor the growing system
+ /// prompt leaves no optional-context room at all.
#[test]
fn batch_budget_accounts_for_many_slot_snapshots() {
- let budgets = PromptBudgets::from_limits(6_500, 1_000);
+ let budgets = PromptBudgets::from_limits(8_000, 1_000);
let observations = (0..64).map(|_| obs_of_size(4_000)).collect::<Vec<_>>();
let slots = (0..100)
.map(|index| SlotSnapshot {