Skip to content

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

Closed
Memtensor-AI wants to merge 2 commits into
MemTensor:dev-v2.0.33from
Memtensor-AI:bugfix/autodev-2329-20260902085902328
Closed

Fix #2329: add_memory 返回成功但立即查询同一 memory_id 404#2330
Memtensor-AI wants to merge 2 commits into
MemTensor:dev-v2.0.33from
Memtensor-AI:bugfix/autodev-2329-20260902085902328

Conversation

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

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

…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
@Memtensor-AI Memtensor-AI added ai:generated Generated or modified by AI | 由 AI 生成或修改 area:scheduler 调度模块 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 2, 2026
@Memtensor-AI
Memtensor-AI requested a review from wustzdy September 2, 2026 09:36
@Memtensor-AI

Memtensor-AI commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

🤖 Open Code Review

Target: PR #2330
Task: c738ad199edc554f
Base: dev-v2.0.33
Head: bugfix/autodev-2329-20260902085902328
Head SHA: 34298e724444cd685c14abcbf53a98c5b818b49e

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: c738ad199edc554f
  • 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.

- 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.
@CarltonXiang
CarltonXiang deleted the branch MemTensor:dev-v2.0.33 September 3, 2026 11:23
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:scheduler 调度模块 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants