Skip to content

Fix #2448: prepare_reference_data crashes on dict entries with missing or non-string id - #2584

Open
syzsunshine219 wants to merge 2 commits into
MemTensor:mainfrom
Memtensor-AI:bugfix/autodev-2448-20260930155922241
Open

syzsunshine219 wants to merge 2 commits into
MemTensor:mainfrom
Memtensor-AI:bugfix/autodev-2448-20260930155922241

Conversation

@syzsunshine219

@syzsunshine219 syzsunshine219 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Restores the change proposed in #2451 for review against main after its former base branch, dev-v2.0.36, was deleted. The source commits and authors are preserved.

Source head: eaa5d00ba8cb601335169b0129814d153dbb12cf

This PR is open for review. Review and CI against the current base are pending; validation claims in the original description below refer to the earlier snapshot. The original discussion and evidence remain in #2451.


Original change description

Description

Fix #2448: prepare_reference_data no longer crashes on dict memory entries with a missing or non-string id.

Root cause: the dict branch of prepare_reference_data in src/memos/mem_os/utils/reference_utils.py assumed every entry carried an id key and that the id was a string, so cached search results / MCP payloads that had already been serialized would raise KeyError('id') or AttributeError: 'int' object has no attribute 'split', and the exception escaped the streaming pipeline. The TextualMemoryItem branch was unaffected because pydantic guarantees the invariant there.

Fix: normalize dict entries defensively — auto-create metadata if missing, skip ref_id derivation when id is absent (keeping the id slot explicit as None), and coerce non-string ids (int, uuid.UUID, ...) with str() before the prefix split. The original id value is preserved in metadata["id"] unchanged so downstream consumers can still round-trip it. The TextualMemoryItem branch is untouched.

Tests: added 9 regression cases in tests/mem_os/utils/test_reference_utils.py covering both crash repros from the issue, string / UUID / int id shapes, missing metadata, missing memory, and the TextualMemoryItem baseline. All 9 pass; the wider tests/mem_os/ suite (45 tests) also passes with no regressions. ruff format and ruff check are clean on the touched files. Local commit created and branch pushed to origin as bugfix/autodev-2448-20260930155922241 (commit 22d8e980).

Related Issue (Required): Fixes #2448

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Automated tests are pending.

  • Unit Test
  • Test Script Or Test Steps (please provide)
  • Pipeline Automated API Test (please provide)

Checklist

  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have created related documentation issue/PR in MemOS-Docs (if applicable)
  • I have linked the issue to this PR (if applicable)
  • I have mentioned the person who will review this PR

@WeiminLee please review this PR.

Reviewer Checklist

MemOS AutoDev and others added 2 commits October 1, 2026 00:14
…non-string id (MemTensor#2448)

The dict branch of prepare_reference_data used to assume every entry carried
a string "id" key, so cached search results / MCP payloads that had already
been serialized would raise KeyError('id') or AttributeError on .split('-').
Both errors escaped the streaming pipeline.

Normalize dict entries instead: create the metadata dict if missing, skip
ref_id derivation when id is absent (keep id slot as None), and coerce
non-string ids (int, uuid.UUID, ...) with str() before the prefix split.
The original id value is preserved in metadata["id"] unchanged so downstream
consumers can still round-trip it.

Regression tests in tests/mem_os/utils/test_reference_utils.py cover both
crash repros from the issue plus the TextualMemoryItem branch (baseline).
…#2448)

OCR findings resolved:

1. src/memos/mem_os/utils/reference_utils.py: the dict-entry branch
   aliased ``memories_json = memories`` and then wrote ``ref_id``,
   ``embedding``, ``sources``, ``memory`` and ``id`` into the caller's
   ``metadata`` dict in place. Cached payloads (search cache, MCP
   messages reused across calls) would be silently corrupted. Now we
   shallow-copy the outer entry and the metadata dict before mutating.

2. tests/mem_os/utils/test_reference_utils.py: the class docstring
   documented ``test_pre_fix_would_have_raised`` as ``xfail(strict=False)``
   but the decorator was missing, so the test ran as a plain passing test
   and the "pre-fix demonstration" gating story was silently lost. The
   decorator is now applied and the docstring rewritten to reflect the
   actual XPASS/XFAIL flip semantics.

Also adds ``test_caller_dict_is_not_mutated`` as a regression guard for
finding 1 (the pre-existing tests could not catch the aliasing bug).

Verification:
- pytest tests/mem_os/utils/test_reference_utils.py -v
  8 passed, 2 xpassed (documented XPASS from the pre-fix demo cases)
- ruff check src/memos/mem_os/utils/reference_utils.py tests/mem_os/utils/test_reference_utils.py
  All checks passed
@syzsunshine219
syzsunshine219 marked this pull request as ready for review November 9, 2026 15:48
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.

prepare_reference_data crashes on dict entries with missing or non-string id

1 participant