From 46493c31f8d604f422ef01f1715d1304dc59b680 Mon Sep 17 00:00:00 2001 From: Wistom Zhang <77424093+Yakyou7@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:30:41 +0800 Subject: [PATCH] fix(memory): rescue at-risk reserved facts below eviction cap (#8331) * fix(memory): rescue at-risk reserved facts below eviction cap * fix(memory): draw hybrid reserve from ranking tail to keep repeated writes bounded --------- Co-authored-by: Totoro --- .../backends/deermem/deermem/core/eviction.py | 22 +- backend/tests/test_memory_eviction.py | 273 ++++++++++++++++++ config.example.yaml | 2 +- docs/guide/memory.md | 4 +- 4 files changed, 292 insertions(+), 9 deletions(-) diff --git a/backend/packages/harness/deerflow/agents/memory/backends/deermem/deermem/core/eviction.py b/backend/packages/harness/deerflow/agents/memory/backends/deermem/deermem/core/eviction.py index 7f95407963..099a0355eb 100644 --- a/backend/packages/harness/deerflow/agents/memory/backends/deermem/deermem/core/eviction.py +++ b/backend/packages/harness/deerflow/agents/memory/backends/deermem/deermem/core/eviction.py @@ -166,6 +166,10 @@ def _score_fact( ) +def _fact_category(fact: dict[str, Any]) -> str: + return str(fact.get("category") or "").strip().lower() + + def select_facts_for_capacity( facts: list[dict[str, Any]], *, @@ -186,9 +190,11 @@ def select_facts_for_capacity( """Select facts under the configured cap without mutating the snapshot. ``confidence`` exactly preserves the historical ranking. ``hybrid-v1`` - combines three bounded signals and reserves only the minimum number of - capacity slots for configured reserved categories (defaulting to correction); - corrections take priority when included. Unused slots return to ordinary competition. + combines three bounded signals and reserves up to ``reserved_slots`` seats + from the ranking tail (``max_facts - reserved_slots`` and below) for + ``reserved_categories`` (defaulting to ``correction``), so high-scoring + winners already well inside the cap do not starve at-risk facts below it + while total protection stays bounded across repeated writes. """ if isinstance(reserved_categories, str): raise TypeError("reserved_categories must be an iterable of strings, not a bare str") @@ -240,12 +246,16 @@ def select_facts_for_capacity( correction_reserved_max, math.ceil(max_facts * correction_reserved_fraction), ) - reserved_candidates = [item for item in ranked if str(item[1].get("category") or "").strip().lower() in target_categories] - reserved_candidates.sort(key=lambda item: str(item[1].get("category") or "").strip().lower() != "correction") + # Draw the reserve from the tail that actually contends for the bottom + # ``reserved_slots`` seats, so high-scoring winners inside ``[:tail_start]`` + # never consume slots needed by at-risk facts while total reserve stays bounded. + tail_start = max(0, max_facts - reserved_slots) + reserved_candidates = [item for item in ranked[tail_start:] if _fact_category(item[1]) in target_categories] + reserved_candidates.sort(key=lambda item: _fact_category(item[1]) != "correction") reserved_count = min(reserved_slots, len(reserved_candidates), max_facts) reserved_candidates = reserved_candidates[:reserved_count] selected_indexes.update(index for index, _fact in reserved_candidates) - reserved_correction_count = sum(str(fact.get("category") or "").strip().lower() == "correction" for _index, fact in reserved_candidates) + reserved_correction_count = sum(_fact_category(fact) == "correction" for _index, fact in reserved_candidates) for index, _fact in ranked: if len(selected_indexes) >= max(0, max_facts): diff --git a/backend/tests/test_memory_eviction.py b/backend/tests/test_memory_eviction.py index 197b29590b..505daca35d 100644 --- a/backend/tests/test_memory_eviction.py +++ b/backend/tests/test_memory_eviction.py @@ -4,6 +4,7 @@ from __future__ import annotations import json from datetime import UTC, datetime, timedelta +from typing import Any import pytest @@ -17,7 +18,9 @@ from deerflow.agents.memory.backends.deermem.deermem.core.paths import ( agent_eviction_audit_path, agent_usage_path, ) +from deerflow.agents.memory.backends.deermem.deermem.core.prompt import format_memory_for_injection from deerflow.agents.memory.backends.deermem.deermem.core.storage import FileMemoryStorage +from deerflow.agents.memory.backends.deermem.deermem.core.updater import MemoryUpdater, _select_stale_candidates NOW = datetime(2026, 8, 13, tzinfo=UTC) @@ -275,6 +278,276 @@ def test_hybrid_reserve_honors_explicit_correction_exclusion(reserved_categories assert decision.reserved_correction_slots == 0 +def test_hybrid_reserve_rescues_at_risk_fact_when_top_winners_exceed_slots() -> None: + # Mirrors #8194 review example: 3 preferences in top 10, intervening contexts + # at ranks #4..#14, and 1 vital preference at rank #15 (plus a lower junk one). + facts = [ + *[_fact(f"top_pref_{i}", confidence=0.98 - i * 0.01, category="preference") for i in range(3)], + *[_fact(f"context_{i}", confidence=0.90 - i * 0.01, category="context") for i in range(11)], + _fact("endangered_vital_pref", confidence=0.70, category="preference"), + _fact("junk_low_pref", confidence=0.10, category="preference"), + ] + + decision = select_facts_for_capacity( + facts, + max_facts=10, + policy=EVICTION_POLICY_HYBRID_V1, + reserved_categories=["preference"], + now=NOW, + ) + + kept_ids = {fact["id"] for fact in decision.kept} + evicted_ids = {item.fact_id for item in decision.evicted} + assert kept_ids == {"top_pref_0", "top_pref_1", "top_pref_2", *(f"context_{i}" for i in range(6)), "endangered_vital_pref"} + assert evicted_ids == {"context_6", "context_7", "context_8", "context_9", "context_10", "junk_low_pref"} + assert decision.reserved_correction_slots == 0 + + +@pytest.mark.parametrize( + ("align_all_knobs", "expected_stale", "expected_injected"), + [ + (False, True, False), + (True, False, True), + ], +) +def test_end_to_end_protection_requires_eviction_staleness_and_injection_knobs( + align_all_knobs: bool, + expected_stale: bool, + expected_injected: bool, +) -> None: + now = datetime.now(UTC) + aged_ts = (now - timedelta(days=120)).isoformat() + recent_ts = now.isoformat() + cfg = DeerMemConfig( + max_facts=10, + fact_eviction_policy=EVICTION_POLICY_HYBRID_V1, + eviction_reserved_categories=["correction", "preference"], + staleness_protected_categories=["correction", "preference"] if align_all_knobs else ["correction"], + guaranteed_categories=["correction", "preference"] if align_all_knobs else ["correction"], + ) + vital = _fact( + "vital_pref", + confidence=0.70, + category="preference", + created_at=aged_ts, + ) + vital["content"] = "User has a severe peanut allergy and must avoid all peanut ingredients" + clutter = [] + for i in range(10): + item = _fact( + f"context_{i}", + confidence=0.90, + category="context", + created_at=recent_ts, + ) + item["content"] = f"Routine recent project note #{i} covering sprint tasks and build status" + clutter.append(item) + + eviction = select_facts_for_capacity( + [vital, *clutter], + max_facts=cfg.max_facts, + policy=cfg.fact_eviction_policy, + reserved_categories=cfg.eviction_reserved_categories, + now=now, + ) + assert any(fact["id"] == "vital_pref" for fact in eviction.kept) + + stale_ids = {fact["id"] for fact in _select_stale_candidates({"facts": eviction.kept}, cfg)} + assert ("vital_pref" in stale_ids) is expected_stale + + injected = format_memory_for_injection( + {"facts": eviction.kept}, + max_tokens=60, + use_tiktoken=False, + guaranteed_categories=cfg.guaranteed_categories, + guaranteed_token_budget=cfg.guaranteed_token_budget, + ) + assert ("severe peanut allergy" in injected) is expected_injected + + +def test_hybrid_reserve_stays_bounded_across_repeated_writes(tmp_path) -> None: + # Regression for #8331 review: writing 10 lower-scoring corrections one by + # one into a full 10-context store must keep the 1-slot reserve bounded at + # 9 contexts + 1 correction so a subsequent high-scoring context still lands. + config = DeerMemConfig( + storage_path=str(tmp_path), + retrieval_adapter="", + max_facts=10, + fact_eviction_policy=EVICTION_POLICY_HYBRID_V1, + ) + storage = FileMemoryStorage(config) + updater = MemoryUpdater(config, storage=storage) + + for i in range(10): + _, fact_id = updater.create_memory_fact( + f"High-scoring context fact {i}", + category="context", + confidence=0.95, + agent_name="default", + user_id="u", + ) + assert fact_id is not None + + for i in range(10): + updater.create_memory_fact( + f"Lower-scoring correction fact {i}", + category="correction", + confidence=0.20, + agent_name="default", + user_id="u", + ) + + mid_facts = storage.load("default", user_id="u")["facts"] + assert len(mid_facts) == 10 + assert sum(fact["category"] == "context" for fact in mid_facts) == 9 + assert sum(fact["category"] == "correction" for fact in mid_facts) == 1 + + _, new_ctx_id = updater.create_memory_fact( + "Subsequent higher-scoring context fact", + category="context", + confidence=0.98, + agent_name="default", + user_id="u", + ) + assert new_ctx_id is not None + final_facts = storage.load("default", user_id="u")["facts"] + assert any(fact["id"] == new_ctx_id for fact in final_facts) + assert sum(fact["category"] == "context" for fact in final_facts) == 9 + assert sum(fact["category"] == "correction" for fact in final_facts) == 1 + + +def test_hybrid_tail_reserve_stays_bounded_with_high_scoring_winners_across_writes(tmp_path) -> None: + # Even when 3 high-scoring preferences already win top-10 slots, repeated + # low-scoring preference writes can only occupy the 1 tail-reserve slot. + config = DeerMemConfig( + storage_path=str(tmp_path), + retrieval_adapter="", + max_facts=10, + fact_eviction_policy=EVICTION_POLICY_HYBRID_V1, + eviction_reserved_categories=["preference"], + ) + storage = FileMemoryStorage(config) + updater = MemoryUpdater(config, storage=storage) + + for i in range(3): + updater.create_memory_fact(f"Top preference {i}", category="preference", confidence=0.98 - i * 0.01, agent_name="default", user_id="u") + for i in range(7): + updater.create_memory_fact(f"High context {i}", category="context", confidence=0.90, agent_name="default", user_id="u") + for i in range(5): + updater.create_memory_fact(f"Low preference {i}", category="preference", confidence=0.50 - i * 0.05, agent_name="default", user_id="u") + + facts = storage.load("default", user_id="u")["facts"] + assert len(facts) == 10 + assert sum(fact["category"] == "context" for fact in facts) == 6 + assert sum(fact["category"] == "preference" for fact in facts) == 4 + assert any(fact["content"] == "Low preference 0" for fact in facts) + + +@pytest.mark.parametrize( + ("facts", "max_facts", "reserved_categories", "fraction", "reserved_max", "expected_kept", "expected_correction_slots"), + [ + # Same-tier protected pool: never evict a higher-scored preference to rescue a lower-scored preference. + ( + [*[_fact(f"pref_{i}", confidence=0.9 - i * 0.05, category="preference") for i in range(4)]], + 3, + ["preference"], + 0.5, + 5, + {"pref_0", "pref_1", "pref_2"}, + 0, + ), + # Correction outranks other reserved categories when no unprotected victim exists. + ( + [*[_fact(f"pref_{i}", confidence=0.9 - i * 0.05, category="preference") for i in range(3)], _fact("corr_low", confidence=0.1, category="correction")], + 3, + ["correction", "preference"], + 0.5, + 5, + {"pref_0", "pref_1", "corr_low"}, + 1, + ), + # Within a 1-slot tail reserve, correction priority takes the single slot while top context stays. + ( + [_fact("ctx_high", confidence=0.95, category="context"), _fact("pref_low", confidence=0.80, category="preference"), _fact("corr_below", confidence=0.10, category="correction")], + 2, + ["correction", "preference"], + 0.5, + 5, + {"ctx_high", "corr_below"}, + 1, + ), + # With 2 tail-reserve slots, both correction and preference outlast an unprotected higher-scored context in the tail. + ( + [_fact("ctx_0", confidence=0.98, category="context"), _fact("ctx_1", confidence=0.95, category="context"), _fact("pref_low", confidence=0.80, category="preference"), _fact("corr_below", confidence=0.10, category="correction")], + 3, + ["correction", "preference"], + 0.5, + 5, + {"ctx_0", "pref_low", "corr_below"}, + 1, + ), + # Natural winner correction does not consume reserve budget or inflate reserved_correction_slots. + ( + [_fact("corr_top", confidence=0.99, category="correction"), _fact("ctx_1", confidence=0.90, category="context"), _fact("pref_below", confidence=0.50, category="preference")], + 2, + ["correction", "preference"], + 0.5, + 5, + {"corr_top", "pref_below"}, + 0, + ), + # Zero reserve budget (fraction=0 or max=0 or empty list) rescues nothing. + ( + [_fact("ctx_0", confidence=0.95, category="context"), _fact("ctx_1", confidence=0.90, category="context"), _fact("corr_below", confidence=0.10, category="correction")], + 2, + ["correction"], + 0.0, + 5, + {"ctx_0", "ctx_1"}, + 0, + ), + ( + [_fact("ctx_0", confidence=0.95, category="context"), _fact("ctx_1", confidence=0.90, category="context"), _fact("corr_below", confidence=0.10, category="correction")], + 2, + ["correction"], + 0.5, + 0, + {"ctx_0", "ctx_1"}, + 0, + ), + ( + [_fact("ctx_0", confidence=0.95, category="context"), _fact("ctx_1", confidence=0.90, category="context"), _fact("corr_below", confidence=0.10, category="correction")], + 2, + [" "], + 0.5, + 5, + {"ctx_0", "ctx_1"}, + 0, + ), + ], +) +def test_hybrid_reserve_priority_and_budget_matrix( + facts: list[dict[str, Any]], + max_facts: int, + reserved_categories: list[str], + fraction: float, + reserved_max: int, + expected_kept: set[str], + expected_correction_slots: int, +) -> None: + decision = select_facts_for_capacity( + facts, + max_facts=max_facts, + policy=EVICTION_POLICY_HYBRID_V1, + correction_reserved_fraction=fraction, + correction_reserved_max=reserved_max, + reserved_categories=reserved_categories, + now=NOW, + ) + assert {fact["id"] for fact in decision.kept} == expected_kept + assert decision.reserved_correction_slots == expected_correction_slots + + @pytest.mark.parametrize("argument", ["reserved_categories", "decay_exempt_categories"]) def test_eviction_rejects_bare_string_category_arguments(argument: str) -> None: with pytest.raises(TypeError, match=argument): diff --git a/config.example.yaml b/config.example.yaml index db746b40af..f6db0c9335 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -2757,7 +2757,7 @@ memory: eviction_access_half_life_days: 30 eviction_correction_reserved_fraction: 0.10 eviction_correction_reserved_max: 10 - eviction_reserved_categories: [correction] # hybrid-v1 only: categories eligible for the reserved slots above (default: ["correction"]) + eviction_reserved_categories: [correction] # hybrid-v1 only: tail-reserve categories; align with staleness_protected_categories & guaranteed_categories for end-to-end protection (default: ["correction"]) eviction_decay_exempt_categories: [] # hybrid-v1 only: categories exempt from confirmation freshness time-decay (default: []) eviction_audit_max_entries: 200 # metadata-only events per user/agent scope; 0 disables max_injection_tokens: 2000 # Maximum tokens for memory injection diff --git a/docs/guide/memory.md b/docs/guide/memory.md index 8b486a1e79..74007dc7ea 100644 --- a/docs/guide/memory.md +++ b/docs/guide/memory.md @@ -127,9 +127,9 @@ Legacy memory files are normalized when read or imported, including recoverable In the default DeerMem `middleware` mode, automatic extraction now classifies every proposed fact by scope, durability, and authority before a deterministic write gate accepts it. The user memory pool stores only durable, descriptive user-level facts. Conversations assigned to a project can also retain durable, descriptive project facts in a separate [project pool](projects.md#project-memory). Temporary task state and one-time action permissions stay in conversation state. User-global summaries require both user scope and descriptive authority, contradiction removals are scope-gated, and a replacement-dependent removal is applied only when its replacement actually survives validation and storage. These classification labels are extraction-only metadata, add no extra LLM call, and are not written into the fact files. The explicit CRUD tools in `memory.mode: tool` remain a separate, model-directed path. Deployments that override the bundled DeerMem prompts via `memory.backend_config.prompts_dir` must add the new classification fields to their custom templates (the `memory_update` fact/summary/removal formats and the `consolidation` consolidated-fact schema): the write gate fails closed, so an un-migrated template stops every extraction-driven fact, summary, and removal write, surfacing only through the `rejected_by_scope_gate` metrics and the high-rejection-rate warning. -When a fact scope reaches `max_facts`, DeerMem still uses the historical confidence-only eviction order by default. Operators can opt in to `memory.backend_config.fact_eviction_policy: hybrid-v1`, which combines bounded confidence (65%), explicit-confirmation freshness (25%), and query-driven access heat (10%). Hybrid signal metadata is collected only while hybrid-v1 or shadow mode is enabled. Automatic confirmation from lexical reinforcement signals and model-selected fact IDs is disabled: a phrase elsewhere in a batch cannot establish that the user confirmed a particular fact. Existing confirmation metadata remains readable; extraction and injection never increment it. Custom prompts may retain factsToReinforce for compatibility, but that proposal alone cannot update confirmation counters or reset staleness. Access heat is stored in a separate decaying sidecar and increases only when `memory_search` actually returns the fact, so reads do not rewrite canonical Markdown or its `updatedAt`. Hybrid mode also reserves a bounded minimum of correction slots (10% of the cap, at most 10; unused slots return to normal competition). Capacity deletion remains physical, but a bounded metadata-only audit records fact IDs, categories, policy scores, and reasons without copying fact content. `fact_eviction_shadow_enabled: true` evaluates hybrid-v1 alongside the default policy without changing actual retention. This feature adds no LLM invocation and can be rolled back by selecting `confidence`. +When a fact scope reaches `max_facts`, DeerMem still uses the historical confidence-only eviction order by default. Operators can opt in to `memory.backend_config.fact_eviction_policy: hybrid-v1`, which combines bounded confidence (65%), explicit-confirmation freshness (25%), and query-driven access heat (10%). Hybrid signal metadata is collected only while hybrid-v1 or shadow mode is enabled. Automatic confirmation from lexical reinforcement signals and model-selected fact IDs is disabled: a phrase elsewhere in a batch cannot establish that the user confirmed a particular fact. Existing confirmation metadata remains readable; extraction and injection never increment it. Custom prompts may retain factsToReinforce for compatibility, but that proposal alone cannot update confirmation counters or reset staleness. Access heat is stored in a separate decaying sidecar and increases only when `memory_search` actually returns the fact, so reads do not rewrite canonical Markdown or its `updatedAt`. Hybrid mode also reserves up to a bounded number of slots (10% of the cap, at most 10; unused slots return to normal competition) from the ranking tail (`max_facts - reserved_slots` and below) for `eviction_reserved_categories` (default `["correction"]`), so high-scoring winners already well inside the cap do not consume slots needed to rescue at-risk facts below it while protection stays bounded across repeated writes. Capacity deletion remains physical, but a bounded metadata-only audit records fact IDs, categories, policy scores, and reasons without copying fact content. `fact_eviction_shadow_enabled: true` evaluates hybrid-v1 alongside the default policy without changing actual retention. This feature adds no LLM invocation and can be rolled back by selecting `confidence`. -When `eviction_reserved_categories` includes `correction`, corrections take priority within the shared reserve. Other configured categories use the remaining reserved slots in score order. The audit field `reservedCorrectionSlots` counts the correction facts selected by that reservation. +When `eviction_reserved_categories` includes `correction`, corrections take priority within that tail reserve. Other configured categories use the remaining reserved slots in score order. The audit field `reservedCorrectionSlots` counts the correction facts selected by that reservation. Note that capacity reservation only protects facts during `max_facts` eviction: staleness review (`staleness_protected_categories`, default `["correction"]`) and prompt injection (`guaranteed_categories`, default `["correction"]`) remain independent knobs, so operators expecting end-to-end protection for additional categories (such as `preference`) should configure the same categories in all three settings. When `lastConfirmedAt` is absent, hybrid confirmation freshness and staleness review use `createdAt`. Repeated mentions in conversation do not refresh either timestamp.