Skip to content

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

Rust

fix(config): score the consolidation floor against the effective budget

yubr-zzh14fcf4341a43
diff --git a/crates/ai-memory-cli/src/config.rs b/crates/ai-memory-cli/src/config.rs
index 557e71cf..27e3632f 100644
--- a/crates/ai-memory-cli/src/config.rs
+++ b/crates/ai-memory-cli/src/config.rs
@@ -2052,16 +2052,57 @@ impl Config {
anyhow::bail!("auto_improve.scheduler.experience_{message}");
}
+ // The safety margin scales the input budget, so a non-positive value
+ // would starve every prompt and one above 1.0 would loosen the budget
+ // past the nominal token limit it is meant to tighten (#884). NaN also
+ // fails every comparison below, so it is rejected here too.
+ //
+ // Validated before the floor check below, which needs a trustworthy
+ // margin to compute the effective budget.
+ let safety_margin = config.consolidation.input_token_safety_margin;
+ if !(safety_margin > 0.0 && safety_margin <= 1.0) {
+ anyhow::bail!(
+ "consolidation.input_token_safety_margin must be in (0.0, 1.0] \
+ (got {safety_margin}); it scales the approximate input-token budget"
+ );
+ }
+
// Fail at startup rather than shipping a prompt that is all scaffolding
// and no observations: below this floor the fixed system prompt and page
// conventions consume the entire budget, so every consolidation would
// either be evidence-free or rejected by the provider.
+ //
+ // The floor is a *character* budget -- what the builders actually need
+ // to hold the fixed prompts plus MIN_OBSERVATION_RESERVE_CHARS of
+ // evidence -- expressed in tokens at the default margin. Comparing the
+ // configured budget against it therefore has to put both sides on the
+ // same footing: the configured budget at ITS margin, the requirement at
+ // the DEFAULT margin.
+ //
+ // Scoring the configured side at the default margin instead would make
+ // the check a no-op, because the margin cancels out of both sides and
+ // it collapses back to `max_input_tokens < MIN_CONSOLIDATION_MAX_INPUT_TOKENS`
+ // -- the raw-count comparison this replaced. Doing it this way is what
+ // makes a lowered margin demand more tokens, which is the point: the
+ // margin exists to buy headroom on dense corpora (#884), and buying it
+ // by shrinking the budget below what the prompt needs is exactly the
+ // case that used to pass and then omit every observation (#1234).
let min_input_tokens = ai_memory_consolidate::MIN_CONSOLIDATION_MAX_INPUT_TOKENS;
- if config.consolidation.max_input_tokens < min_input_tokens {
+ let required_input_chars = ai_memory_consolidate::effective_input_chars(
+ min_input_tokens,
+ ai_memory_consolidate::DEFAULT_CONSOLIDATION_INPUT_TOKEN_SAFETY_MARGIN,
+ );
+ let effective_input_chars = ai_memory_consolidate::effective_input_chars(
+ config.consolidation.max_input_tokens,
+ safety_margin,
+ );
+ if effective_input_chars < required_input_chars {
anyhow::bail!(
- "consolidation.max_input_tokens must be at least {min_input_tokens} \
- (got {}); below that the system prompt and page conventions leave \
- no room for observations",
+ "consolidation.max_input_tokens must leave at least {required_input_chars} \
+ characters for the prompt (got {}, which is {effective_input_chars} at \
+ input_token_safety_margin = {safety_margin}); below that the system \
+ prompt and page conventions leave no room for observations. Raise \
+ max_input_tokens, or raise input_token_safety_margin towards 1.0",
config.consolidation.max_input_tokens
);
}
@@ -2073,21 +2114,6 @@ impl Config {
config.consolidation.max_output_tokens
);
}
- // The safety margin scales the input budget, so a non-positive value
- // would starve every prompt and one above 1.0 would loosen the budget
- // past the nominal token limit it is meant to tighten (#884). NaN also
- // fails every comparison below, so it is rejected here too.
- let safety_margin = config.consolidation.input_token_safety_margin;
- if !(safety_margin > 0.0 && safety_margin <= 1.0) {
- anyhow::bail!(
- "consolidation.input_token_safety_margin must be in (0.0, 1.0] \
- (got {safety_margin}); it scales the approximate input-token budget"
- );
- }
- // The safety margin scales the input budget, so a non-positive value
- // would starve every prompt and one above 1.0 would loosen the budget
- // past the nominal token limit it is meant to tighten (#884). NaN also
- // fails every comparison below, so it is rejected here too.
// Zero (or a sub-second remainder rounded down) would cut every
// provider request off before it is sent.
if config.llm_timeout_secs == 0 {
@@ -3826,6 +3852,62 @@ mod tests {
}
}
+ /// #1234: the floor's promise is a *character* budget -- enough room for
+ /// the fixed prompts plus `MIN_OBSERVATION_RESERVE_CHARS` of evidence -- so
+ /// the check has to score the configured budget at its own margin against
+ /// that requirement at the default margin. 8,000 tokens is the case that
+ /// used to load and then send every observation to the omission marker:
+ /// it clears the requirement at margin 1.0 but not at the default 0.8.
+ #[test]
+ fn load_rejects_a_budget_whose_effective_size_is_below_the_floor() {
+ // Clears the character requirement at margin 1.0, misses it at 0.8.
+ for (tokens, margin) in [(8_000, 0.8), (8_000, 0.6), (8_000, 0.5), (9_000, 0.7)] {
+ let tmp = TempDir::new().unwrap();
+ let config_path = tmp.path().join("config.toml");
+ std::fs::write(
+ &config_path,
+ format!(
+ "[consolidation]
+max_input_tokens = {tokens}
+ input_token_safety_margin = {margin}
+"
+ ),
+ )
+ .unwrap();
+ let error = Config::load(Some(&config_path), Some(tmp.path().to_path_buf()))
+ .expect_err("a budget too small at its own margin must fail closed");
+ assert!(
+ error.to_string().contains("consolidation.max_input_tokens"),
+ "unexpected error for {tokens} tokens at margin {margin}: {error:#}"
+ );
+ }
+ }
+
+ /// Controls for the test above, so a blanket rejection cannot pass for a
+ /// working guard: the same token counts are accepted once the margin is
+ /// high enough to clear the requirement, and 8,000 at margin 1.0 -- which
+ /// the reject test deliberately excludes -- must still load.
+ #[test]
+ fn load_accepts_a_budget_whose_effective_size_clears_the_floor() {
+ for (tokens, margin) in [(8_000, 1.0), (10_000, 0.8), (10_000, 0.7)] {
+ let tmp = TempDir::new().unwrap();
+ let config_path = tmp.path().join("config.toml");
+ std::fs::write(
+ &config_path,
+ format!(
+ "[consolidation]
+max_input_tokens = {tokens}
+ input_token_safety_margin = {margin}
+"
+ ),
+ )
+ .unwrap();
+ let cfg = Config::load(Some(&config_path), Some(tmp.path().to_path_buf()))
+ .expect("a budget that clears the requirement must load");
+ assert_eq!(cfg.consolidation.max_input_tokens, tokens);
+ }
+ }
+
#[test]
fn load_rejects_a_consolidation_output_limit_too_small_for_json() {
let min = ai_memory_consolidate::MIN_CONSOLIDATION_MAX_OUTPUT_TOKENS;
@@ -3892,19 +3974,24 @@ mod tests {
}
/// A small-context provider needs both sides of the context allocation to
- /// survive the config round-trip.
+ /// survive the config round-trip. The budget is derived from the floor
+ /// rather than written out, so the test keeps meaning "the smallest
+ /// accepted budget round-trips" when the floor moves (#1234) instead of
+ /// pinning a number the floor happens to allow today.
#[test]
fn load_accepts_a_small_context_consolidation_budget() {
let tmp = TempDir::new().unwrap();
let config_path = tmp.path().join("config.toml");
+ let small = ai_memory_consolidate::MIN_CONSOLIDATION_MAX_INPUT_TOKENS;
+ let output = ai_memory_consolidate::MIN_CONSOLIDATION_MAX_OUTPUT_TOKENS;
std::fs::write(
&config_path,
- "[consolidation]\nmax_input_tokens = 7000\nmax_output_tokens = 1000\n",
+ format!("[consolidation]\nmax_input_tokens = {small}\nmax_output_tokens = {output}\n"),
)
.unwrap();
let cfg = Config::load(Some(&config_path), Some(tmp.path().to_path_buf())).unwrap();
- assert_eq!(cfg.consolidation.max_input_tokens, 7_000);
- assert_eq!(cfg.consolidation.max_output_tokens, 1_000);
+ assert_eq!(cfg.consolidation.max_input_tokens, small);
+ assert_eq!(cfg.consolidation.max_output_tokens, output);
}
/// `AI_MEMORY_LLM_TIMEOUT_SECS` (figment maps it to this field) exists so