Skip to content

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

Open
Memtensor-AI wants to merge 2 commits into
MemTensor:dev-v2.0.36from
Memtensor-AI:bugfix/autodev-2448-20260930155922241
Open

Memtensor-AI wants to merge 2 commits into
MemTensor:dev-v2.0.36from
Memtensor-AI:bugfix/autodev-2448-20260930155922241

Conversation

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

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

…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).
@Memtensor-AI Memtensor-AI added ai:generated Generated or modified by AI | 由 AI 生成或修改 area:core MOS 编排层 / 框架底座 / 跨模块问题 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 30, 2026
@Memtensor-AI

Memtensor-AI commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

🤖 Open Code Review

Target: PR #2451
Task: faf2339ea46ecf16
Base: dev-v2.0.36
Head: bugfix/autodev-2448-20260930155922241
Head SHA: eaa5d00ba8cb601335169b0129814d153dbb12cf

✅ OpenCodeReview: Review complete: 0 finding(s) across 2 selected item(s).

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator Author

🔧 Open Code Review requested Agent fix

Open Code Review found 2 issue(s). I have resumed the development Agent to fix them.

  • Task: faf2339ea46ecf16
  • Fix attempt: 1/2
  • Finding delta: 0 repeated / 2 new / 0 likely resolved

The Agent will push a new commit to this PR branch. OCR will recheck after the commit is pushed.

…#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
@Memtensor-AI Memtensor-AI added status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 and removed status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:generated Generated or modified by AI | 由 AI 生成或修改 area:core MOS 编排层 / 框架底座 / 跨模块问题 status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants