Repository navigation
feat(plugins): add context-aware hooks for memory pipelines - #2623
consultantQ wants to merge 11 commits into
Conversation
Port the reviewed Hook-only tests from smartcomment-integration. Keep upstream Search tests intact apart from required hook_context assertions, and isolate new behavior tests with an opt-in fixture. Cover Reader grouping/order and all five MemRead operation sites. Validation on feat/add-more-plugin-hooks: 49 targeted tests passed; related regression: 266 passed, 1 skipped. Individual test files, Ruff, staged pre-commit, and diff checks passed. Full make test stopped after 180 seconds at the existing Ollama network request.
- Normalize scheduler context to dictionaries and remove target parameter - Include reader and deletion arguments in operation inputs - Remove archive observations and redundant search context initialization - Update hook documentation and regression tests
There was a problem hiding this comment.
🟡 Changes recommended
Both sync and async wrappers mishandle caller-supplied hook_context, causing TypeError for methods that reject the keyword or receive it positionally, and passing the wrong context to hooks in the positional case.
2 open issues
What changed in this PR
This PR extends MemOS plugin hooks across memory pipelines, providing correlation metadata for future tracing integrations without adding a SmartComment dependency.
Changes:
- Add
HookContextand context-aware callbacks while retaining legacy callback support. - Add search-stage hooks and success/failure hooks for extraction, textual writes, and scheduler operations.
- Preserve fine-transfer input/output alignment and document grouping contracts with regression tests.
| File | Description |
|---|---|
tests/plugins/test_hook_context.py |
Tests context metadata and callback compatibility. |
tests/multi_mem_cube/test_single_cube_hooks.py |
Tests textual-write hooks and result replacement. |
tests/mem_scheduler/test_operation_observer.py |
Tests scheduler operation hooks and correlation. |
tests/mem_scheduler/test_mem_read_grouping.py |
Tests reader grouping metadata. |
tests/mem_reader/test_hook_pipeline.py |
Tests extraction success and failure hooks. |
tests/mem_reader/test_fine_transfer_order.py |
Tests ordered results and empty output slots. |
tests/conftest.py |
Adds hook-registration isolation fixture. |
tests/api/test_search_pipeline_hooks.py |
Updates expected search hook parameters. |
tests/api/test_search_hook_context.py |
Tests search context, replacement, and failures. |
src/memos/plugins/README.md |
Introduces context-aware hook usage. |
src/memos/plugins/hooks.py |
Adds context handling and legacy callback adaptation. |
src/memos/plugins/HOOKS.md |
Documents pipeline hook contracts. |
src/memos/plugins/hook_defs.py |
Declares new hooks and context parameters. |
src/memos/plugins/hook_context.py |
Defines correlation metadata and its builder. |
src/memos/plugins/base.py |
Corrects registration-method wording in the docstring. |
src/memos/plugins/__init__.py |
Exports context APIs. |
src/memos/multi_mem_cube/single_cube.py |
Adds hooks around textual-memory writes. |
src/memos/mem_scheduler/task_schedule_modules/operation_observer.py |
Adds scheduler operation observation. |
src/memos/mem_scheduler/task_schedule_modules/handlers/mem_read_handler.py |
Instruments memory operations and reports grouping. |
src/memos/mem_scheduler/task_schedule_modules/base_handler.py |
Adds a shared operation-observer factory. |
src/memos/mem_reader/simple_struct.py |
Adds extraction hooks and preserves transfer alignment. |
src/memos/api/handlers/search_handler.py |
Adds contextual search-stage and failure hooks. |
Review effort: Balanced
| if _accepts_hook_context(func): | ||
| call_kwargs["hook_context"] = hook_context |
| if _accepts_hook_context(func): | ||
| call_kwargs["hook_context"] = hook_context |
🤖 Open Code ReviewTarget: PR #2623 🔍 OpenCodeReview found 2 issue(s) in this PR.
1.
|

Description
This PR is a prerequisite for the upcoming SmartComment tracing integration.
SmartComment uses these hooks and their correlation metadata to build execution graphs across memory extraction, storage, search, and scheduler operations.
The integration is developed on the
feat/smartcomment-integrationbranch and will be submitted as a separate PR after this change is merged.The hooks introduced here are general-purpose extension points and add no dependency on SmartComment.
This PR extends the plugin hook system across search, memory extraction, textual memory writes, and scheduler operations.
Changes:
HookContextwith request, user, task, cube, and operation metadata so plugins can correlate events across processing stages.hook_context.Noneresults omitted. Results now preserve input order and retain empty groups for missing outputs, keeping each result aligned with its source memory.per_input,batch, orunknowngrouping metadata so plugins can interpret reader outputs correctly.Dependencies: No new third-party dependencies.
Related Issue (Required): None (new feature, no prior issue)
Type of change
How Has This Been Tested?
Validation results on commit
5cbd7d4b:initialization: 26 paths and 59 schemas, with no differences.
The commands below describe the focused test suite:
Checklist
Reviewer Checklist