Skip to content

Conversation

@tangg555
Copy link
Collaborator

Description

Summary: (summary)
Key Refactoring Changes:

  1. Handlers Decomposition & Decoupling (Handlers 拆分与解耦) :

    • Decomposed the monolithic scheduler logic into specialized handlers (e.g., QueryHandler , MemoryUpdateHandler ) located in src/memos/mem_scheduler/task_schedule_modules/handlers/ .
    • Decoupled task processing logic to improve maintainability and readability.
  2. Unified Monitoring Logic (监控逻辑统一) :

    • Consolidated monitoring responsibilities into TaskScheduleMonitor ( src/memos/mem_scheduler/monitors/task_schedule_monitor.py ).
    • Unified status reporting for both Redis and local queues, providing consistent metrics for system observability.
  3. Unified Retrieval Logic (召回逻辑统一) :

    • Refactored SchedulerRetriever by splitting its responsibilities.
    • Moved post-processing logic (filtering, reranking) to a dedicated MemoryPostProcessor ( src/memos/mem_scheduler/memory_manage_modules/post_processor.py ).
    • Centralized memory enhancement methods within AdvancedSearcher , streamlining the retrieval flow.
  4. Redis Module Decomposition (Redis 模块拆分) :

    • Addressed the issue where orm_modules/api_redis_model.py and webservice_modules/redis_service.py bore excessive responsibilities (queues, state storage, caching, locks).
    • Split Redis-related functionalities into distinct modules (e.g., redis_queue.py for queuing, redis_model.py for ORM-like storage), reducing coupling and file complexity.
  5. Unified Error Handling (错误处理与重试机制统一) :

    • Standardized error handling and retry mechanisms which were previously dispersed across various Consumers and Handlers.
    • Implemented consistent error catching patterns to improve system resilience and debugging.

Fix: #(issue)

Docs Issue/PR: (docs-issue-or-pr-link)

Reviewer: @(reviewer)

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) | 我已在 MemOS-Docs 中创建了相关的文档 issue/PR(如果适用)
  • I have linked the issue to this PR (if applicable) | 我已将 issue 链接到此 PR(如果适用)
  • I have mentioned the person who will review this PR | 我已提及将审查此 PR 的人

tangg555 and others added 19 commits January 20, 2026 15:43
…iever

- Split SchedulerRetriever into SchedulerSearchService and MemoryPostProcessor
- SchedulerSearchService: Unified search interface delegating to Searcher
- MemoryPostProcessor: Handles all post-retrieval processing (enhance, filter, rerank)
- Fix: Use memory_type='All' to prevent 2*top_k bug in search results
- Add robust search_method conversion with logging for unknown methods
- Update all scheduler call sites (base, general, optimized, eval)
- Add comprehensive unit tests for both new services

Breaking changes: None (backward compatible)
Fixes: Search result count bug (was returning up to 2*top_k)
Problem:
- enhance_memories_with_query and recall_for_missing_memories were in MemoryPostProcessor
- This caused SearchHandler to depend on Scheduler (cross-layer coupling)
- These are general search enhancement features, not scheduler-specific

Solution:
- Move both methods from MemoryPostProcessor to AdvancedSearcher
- Update SingleCubeView to use self.searcher instead of self.mem_scheduler.post_processor
- Simplify MemoryPostProcessor to only handle filtering and reranking

Benefits:
- Better separation of concerns (search enhancement belongs to Searcher)
- Removes cross-layer dependency (API layer no longer depends on Scheduler)
- Enables reuse (both Scheduler and SearchHandler share same implementation)
- Improves testability (AdvancedSearcher can be tested independently)

Changes:
- advanced_searcher.py: +291 lines (added enhancement methods)
- post_processor.py: -320 lines (removed enhancement, kept filter/rerank)
- single_cube.py: 6 changes (updated call sites)
- test_post_processor.py: -57 lines (removed obsolete tests)

Type safety: Verified via 'AdvancedSearcher as Searcher' alias in tree.py
Fixes 3 critical issues identified in code review:

1. Configuration behavior change (HIGH RISK)
   Problem: enhance_memories_with_query called without batch_size/retries
   Impact: Behavior inconsistent with historical settings
   Fix: Pass DEFAULT_SCHEDULER_RETRIEVER_BATCH_SIZE and RETRIES explicitly

2. Module dependency inversion (ARCHITECTURE REGRESSION)
   Problem: AdvancedSearcher depended on memos.mem_scheduler.utils
   Impact: Violated decoupling goal (memories should not depend on scheduler)
   Fix: Move extract_json_obj/extract_list_items_in_answer to memos.utils
   Changes:
   - src/memos/utils.py: +163 lines (add utility functions)
   - misc_utils.py: -163 lines, +3 lines (re-export for backward compat)
   - Update imports in 6 files

3. Test coverage migration (NOTED)
   Note: _split_batches tests removed but not migrated
   Action: Defer to future test enhancement

Benefits:
- Maintains historical batch/retry behavior
- Restores proper layering (no memories→scheduler dependency)
- Backward compatible (misc_utils re-exports from memos.utils)

Verification:
✓ No scheduler imports in AdvancedSearcher
✓ Config params passed to enhance_memories_with_query
✓ All existing imports work via re-export
Critical fix for misc_utils.py:
- Removed empty 'def extract_json_obj(text: str):' declaration
- This was leftover from sed deletion and caused IndentationError
- Functions are properly re-exported from memos.utils

Verification: python3 -m py_compile passes
- Fix test_search_service.py: call_count assertion (2 -> 1)
  SearchService now uses memory_type='All' for single search call
- Fix test_post_processor.py: remove incorrect 'with Mock()' usage
- Fix ruff E712: use truthiness check instead of '== True'
- Apply ruff formatting to all modified files

All 23 tests now pass successfully.
- Extract activation memory management logic from `BaseScheduler` to new `ActivationMemoryManager` class
- Move background task schedule monitoring loop from `BaseScheduler` to `TaskScheduleMonitor`
- Update `BaseScheduler` to delegate activation memory operations to `ActivationMemoryManager`
- Update `BaseScheduler` to use `TaskScheduleMonitor` for metrics monitoring
- Update tests to reflect changes in scheduler component naming (retriever -> post_processor/search_service)
@tangg555 tangg555 requested a review from CaralHsi January 26, 2026 13:13
@tangg555 tangg555 requested a review from glin93 January 27, 2026 04:11
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.

2 participants