Skip to content

Improve DeerMem fact-cap eviction policy #4641

Description

@qingbo1011

Current behavior

As of main at commit 8234370a6ad2abd83bdba35271b902aa0bd19d47:

The capacity path and the two protection settings it bypasses can be summarized as follows:

flowchart TD
    Update["Memory update is applied"] --> Append["Append eligible new facts"]
    Append --> Over{"len(facts) > max_facts?"}

    Over -- "No" --> Persist["Persist current facts"]
    Over -- "Yes" --> Trim["_trim_facts_to_max"]

    Trim --> Rank["Sort by confidence descending"]
    Rank --> Keep["Keep first max_facts entries"]
    Rank --> Drop["Omit lower-confidence entries"]

    Keep --> Persist
    Drop --> DeleteSet["Convert missing fact IDs to deletes"]
    DeleteSet --> Unlink["Physically unlink fact files"]

    Injection["Injection: correction has a guaranteed 500-token budget"]
    Staleness["Staleness review: correction is protected by default"]

    Injection -. "not consulted" .-> Trim
    Staleness -. "not consulted" .-> Trim
Loading

Why this is a problem

Consider a store that is already at its fact limit. A fact with confidence 0.9 that has not been confirmed for 180 days will outrank and survive over a fact with confidence 0.7 that the user confirmed seven days ago. The recently confirmed fact is removed from persisted memory because confidence is the only eviction key, even though its recency may make it more useful.

Category importance can be lost in the same way. A correction fact can be pushed out by enough higher-confidence preference facts. Once the correction has been removed, its guaranteed injection budget cannot help because it is no longer present to inject.

Physical removal also makes an incorrect eviction difficult to inspect or reverse. The capacity-trim path does not write a durable eviction record showing which fact was evicted, why it lost the ranking, or how to restore it.

Proposed directions

I would appreciate maintainer guidance on one or a combination of these directions:

  1. Use a composite eviction score that combines confidence with recency decay, based on updatedAt or a dedicated last-confirmed timestamp.
  2. Add protected categories or per-category quotas so facts such as correction are not the first capacity-eviction candidates. This could reuse or align with the existing guaranteed/staleness category settings.
  3. Replace capacity eviction with soft deletion, or at minimum write an audit log containing the evicted fact, timestamp, reason, and ranking inputs so the decision is recoverable and auditable.

I am willing to submit a PR once maintainers agree on the preferred direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageAwaiting maintainer triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions