split_continuous_references leaves references merged when comma styles are mixed
Bug Description
split_continuous_references in src/memos/mem_os/utils/reference_utils.py splits a combined reference tag [1:aaa, 4:bbb] into [1:aaa][4:bbb] via two sequential str.replace passes: one for ", " and one for ",". The first pass that matches rewrites the content substring, so the second pass can no longer find the original content. When an LLM emits mixed separator styles in one tag, only the last boundary is split:
split_continuous_references("[1:92ff35fb,4:bfe6f044, 7:abcd1234]")
# actual: '[1:92ff35fb,4:bfe6f044][7:abcd1234]'
# expected: '[1:92ff35fb][4:bfe6f044][7:abcd1234]'
The function is on the streaming chat path (api/handlers/chat_handler.py → process_streaming_references_complete), so the merged references are rendered as literal text instead of individual reference tags.
Expected Behavior
Every comma between the brackets splits a reference boundary regardless of the whitespace that follows it.
Environment
MemOS main (a7367d0), python 3.14.
Additional Context
Fix + red/green-verified unit tests in tests/mem_os/test_reference_utils.py ready; PR to follow referencing this issue.
🤖 AutoDev has picked up this issue and started working on it.
Task ID: 75c34c73ae1d5ded
Working branch: bugfix/autodev-2417-20260928032002157
Target branch: latest dev* branch
Workflow: opsp (analysis → coding → testing → PR)
I will post the PR link here once done. If I need more information, I will ask in the comments.
✅ AutoDev task 75c34c73ae1d5ded completed.
Summary: Fixes #2417: split_continuous_references in src/memos/mem_os/utils/reference_utils.py no longer strands bare commas when a reference tag mixes , and , separator styles. The two sequential str.replace passes (one for ", ", one for ",") mutated the text on the first pass, so the second pass could not find the original content substring and any surviving bare commas were left un-split — producing merged references like [1:92ff35fb,4:bfe6f044][7:abcd1234] that render as literal text on the streaming chat path.
The fix replaces the two passes with a single re.sub(r",\s*", "][", content_between_brackets), so every comma between the brackets becomes a split boundary regardless of trailing whitespace. All guard clauses (single [, single ], comma present between brackets) are preserved bit-for-bit; multi-tag and non-reference inputs are unaffected.
Tests: added tests/mem_os/test_reference_utils.py with 17 cases covering the exact reproduction from the issue plus other separator variants (bare, comma-space, multi-space, tab) and the existing guard clauses. All 17 pass; the full tests/mem_os/ regression suite (53 tests) also passes; ruff check and ruff format are clean on the touched files. Before the fix, 5 of the new cases (including the issue's exact reproduction) failed red as expected.
Base branch: main
Branch: bugfix/autodev-2417-20260928032002157
Commit: fa495aa
PR: https://github.com/MemTensor/MemOS/pull/2419
Assigned to: @syzsunshine219
Reviewers: @WeiminLee
This issue has been automatically marked as stale due to inactivity.
This issue has been automatically closed due to inactivity.