Skip to content

fix(memory): refresh cached facts after external changes - #1026

Draft
yunyv wants to merge 3 commits into
modelscope:mainfrom
yunyv:fix/facts-cache-hot-reload
Draft

yunyv wants to merge 3 commits into
modelscope:mainfrom
yunyv:fix/facts-cache-hot-reload

Conversation

@yunyv

@yunyv yunyv commented Oct 26, 2026

Copy link
Copy Markdown

Change Summary

The file backend compares both MEMORY.md and facts text when rebuilding its prompt snapshot, but FactsStorage previously returned its first cached value indefinitely. External edits, clearing or deleting facts.json could therefore leave stale facts in later prompts. A later sequential write through an older FactsStorage instance could also overwrite facts added by another instance. FileBasedBackend.invalidate() cleared only the MEMORY.md cache.

  • Validate the facts cache using (mtime_ns, size), following FileMemoryStorage's existing approach, and refresh after changes, deletion or recreation.
  • Clear both underlying caches on explicit backend invalidation.
  • Add 13 regressions covering FTS/hybrid prompt refresh, removal of stale prompt content, sequential updates, unchanged-cache reuse, missing/recreated files, and explicit invalidation when metadata is unchanged.

This is metadata-based freshness, not concurrent-write safety or strong consistency. Edits preserving both size and mtime require explicit invalidation. Existing malformed-JSON fallback behavior is retained; this does not add JSON recovery or schema validation. Three existing formatting hunks in file_based.py are included to satisfy the configured changed-file formatter.

Validation

  • New regressions on the untouched baseline: 12 failed, 1 passed; fixed: 13 passed, using normal package imports.
  • Related memory suite: 190 passed, 71 skipped (optional backend dependencies):
    python -m pytest tests/memory --ignore=tests/memory/test_default_memory.py --ignore=tests/memory/test_default_memory_zh.py --disable-socket --allow-unix-socket -q
    The two excluded files are legacy provider-integration tests. No live model/provider calls were made; outbound sockets were disabled. The full project test suite was not run.
  • Configured changed-file pre-commit hooks, pip check, and git diff --check passed.
  • pre-commit install and pre-commit run --all-files were run. Full-repository checks fail on existing flake8/isort/YAPF/quote-formatting issues on both baseline and fixed code; normalized flake8 output is identical. Unrelated automatic edits were reverted, then final tests and changed-file hooks reran successfully.

Related issue number

None. Identified from the file backend's existing two-source snapshot and invalidation contract. All-state facts issue/PR searches were rechecked on 2026-10-26; no competing fix was found. #1002 changes tool-failure notices, and #994 concerns DefaultMemory rollback, not this facts cache.

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Run pre-commit install and pre-commit run --all-files before git commit, and passed lint check. Both ran; the existing full-repository failures above remain. Changed-file checks passed.
  • Documentation reflects the changes where applicable (internal cache behavior; no new command or configuration)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant