Skip to content

Fix #2329: add_memory 返回成功但立即查询同一 memory_id 404 - #2573

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

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

Conversation

@syzsunshine219

@syzsunshine219 syzsunshine219 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

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

Source head: 34298e724444cd685c14abcbf53a98c5b818b49e

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 #2330.


Original change description

Description

Fixed the add_memory → get_memory 404 race condition in async mode.

Root cause: In async mode, add_memory writes fast-mode LongTermMemory nodes and immediately returns their IDs (IDs_A) to the caller. The scheduler's MemReadMessageHandler._process_memories_with_reader would then call fine_transfer_simple_mem which produces brand-new UUID nodes (IDs_B), write those, and hard-delete IDs_A. Any get_memory(IDs_A) call issued after the scheduler ran would return 404 because the original nodes no longer existed.

Fix applied in src/memos/mem_scheduler/task_schedule_modules/handlers/mem_read_handler.py: when fine_transfer_simple_mem produces exactly one enhanced memory per input item (the common 1:1 case), the enhanced item's .id is overwritten with the original node's ID before writing. Neo4j uses MERGE semantics, so the graph node is updated in-place and the caller's handle remains permanently valid. Reused IDs are then excluded from the subsequent delete list so the freshly-updated node is never destroyed. The existing delete-and-replace behavior is fully preserved for 1:N expansions and zero-output cases.

Tests: 4 new unit tests added in tests/mem_scheduler/test_mem_read_handler_id_stability.py covering the 1:1 reuse path (ID assigned, ID not deleted), the 1:N expansion fallback (old ID still deleted), and the zero-output fallback. All 50 scheduler tests pass. Ruff clean.

Related Issue (Required): Fixes #2329

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

@MatthewZhuang, @CarltonXiang, @syzsunshine219, @World-controller please review this PR.

Reviewer Checklist

AutoDev Bot added 2 commits September 2, 2026 17:34
…event 404

In async mode, add_memory writes fast-mode nodes and returns their IDs
to the caller. The scheduler's MemReadMessageHandler would then create
refined nodes with brand-new UUIDs and hard-delete the original IDs,
causing any get_memory(original_id) call to return 404.

Fix: when fine_transfer_simple_mem returns exactly one enhanced memory
per input item (1:1 mapping), reuse the original node ID on the enhanced
item. Neo4j uses MERGE semantics so the node is updated in-place and the
caller's handle stays valid. The reused IDs are excluded from the delete
list so the updated node is never immediately destroyed.

The existing delete-and-replace behaviour is preserved for 1:N expansions
and zero-output cases.

Fixes MemTensor#2329
- Assert text_mem.add was called before checking delete args so the
  happy path is verified (avoids a silent no-op when the handler
  exits early with empty memory_items).
- Simplify redundant `if processed_memories and len(...) > 0:` to
  `if processed_memories:` — the length check was dead code.
@syzsunshine219
syzsunshine219 marked this pull request as ready for review November 9, 2026 15:47
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.

add_memory 返回成功但立即查询同一 memory_id 404

1 participant