MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

SentenceChunker.chunk discards Chunk objects (token_count/sentences) by rebinding to str

Open simpleqt opened this issue 2 months ago • 4 comments

Bug Description

SentenceChunker.chunk in src/memos/chunkers/sentence_chunker.py builds a proper Chunk object and then immediately overwrites it with the restored-URL string:

chunk = Chunk(text=c.text, token_count=c.token_count, sentences=c.sentences)
chunk = self.restore_urls(chunk.text, url_map)   # rebinds `chunk` to a bare str
chunks.append(chunk)

So the method always returns list[str] despite declaring list[str] | list[Chunk]: token_count and sentences are silently discarded, and callers that use the documented Chunk API (.text / .token_count / .sentences) get an AttributeError. The existing test masks this by accepting both return types.

Expected Behavior

The method should return actual Chunk objects with the URL-restored text plus the preserved token_count and sentences.

Environment

MemOS main, python 3.14.

Additional Context

Fix + red/green-verified test ready; PR to follow referencing this issue.

simpleqt avatar Sep 28 '26 16:09 simpleqt

🤖 AutoDev has picked up this issue and started working on it.

Task ID: 8692b21ce1690c04 Working branch: bugfix/autodev-2433-20260928165809486 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.

Memtensor-AI avatar Sep 28 '26 16:09 Memtensor-AI

✅ AutoDev task 8692b21ce1690c04 completed.

Summary: Fixes #2433. SentenceChunker.chunk in src/memos/chunkers/sentence_chunker.py was building a Chunk(text, token_count, sentences) and then rebinding the local chunk variable to the plain string returned by restore_urls, so the method returned list[str] and silently discarded token_count / sentences. Callers using the documented Chunk API (.text, .token_count, .sentences) raised AttributeError.

The fix restores URLs on the raw text first, then constructs the Chunk with the restored text and the preserved chonkie metadata, and narrows the return annotation to list[Chunk] to match BaseChunker.chunk. Callers were audited: prefer_text_memory/spliter.py and mem_reader/simple_struct.py already read chunk.text (they had been silently broken); mem_reader/multi_modal_struct.py already tolerates both shapes; mem_reader/read_multi_modal/base.py uses SimpleTextSplitter, not the sentence chunker, so it is unaffected.

tests/chunkers/test_sentence_chunker.py was rewritten to drop the if isinstance(chunks[0], str) mask that had been hiding the bug. It now unconditionally asserts Chunk type plus preserved token_count / sentences, and adds test_sentence_chunker_restores_urls_on_chunk_text to verify that URLs are restored inside chunk.text while metadata still survives. Both new tests fail on the unpatched code (verified TDD red) and pass after the fix; full pytest tests/chunkers/ -q reports 25 passed; ruff check and ruff format --check on the two touched files are clean.

Base branch: main Branch: bugfix/autodev-2433-20260928165809486 Commit: 6ae2753 PR: https://github.com/MemTensor/MemOS/pull/2435 Assigned to: @syzsunshine219 Reviewers: @WeiminLee

Memtensor-AI avatar Sep 28 '26 17:09 Memtensor-AI

This issue has been automatically marked as stale due to inactivity.

github-actions[bot] avatar Oct 29 '26 02:10 github-actions[bot]

This issue has been automatically closed due to inactivity.

github-actions[bot] avatar Nov 06 '26 02:11 github-actions[bot]