Repository navigation
fix(memory): bind background sync to plugin lifecycle owners - #167679
Conversation
Replace the module-level async-context snapshot with the approved instance lifecycle background runner. Each callback uses its exact instance and current adopted registry while preserving host resources and independent completion through disposal cleanup. Carry the runner through runtime, tools, CLI, watchers, manager reuse, and maintenance acquisition. Cover retained and replaced Memory Core reloads through startup, session updates, watch notifications, and intervals. Preserve sync observation throughout the fixture, retain cleanup failures, and share the owner-aware test runner across direct tools and scheduling harnesses.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. What this changesMemory Core, the plugin that indexes notes and conversations for recall, binds background indexing to its owning plugin instance so indexing continues after plugin reloads. Example: The operator changes plugins.load while using openai embeddings.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR remains necessary and has no actionable findings. The previous integration requirement is resolved, and the reviewed head has successful CI. Its maintainer-authored lifecycle repair merits landing. Priority: P1 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherMemory Core turns workspace notes and saved conversations into a searchable SQLite index using embedding providers. The plugin lifecycle controls which provider registry background work can use and when that work must finish during reload or shutdown. flowchart TD
A[Notes and conversation updates] --> B[Memory background triggers]
B --> C[Owning plugin lifecycle]
C --> D{Admission still open?}
D -->|Yes| E[Current adopted provider registry]
E --> F[Embedding and indexing]
F --> G[Searchable SQLite memory index]
D -->|No| H[Reject new work and drain cleanup]
Technical reviewBest possible solution: Keep background execution in the existing plugin lifecycle owner and land the coherent Memory Core cutover. Do we have a high-confidence way to reproduce the issue? Yes: current main still restores the import-time registry snapshot, and the PR supplies a real Gateway reload reproduction plus failing original-production controls. This read-only review verified the mechanism without running target code. Is this the best way to solve the issue? Yes: the existing plugin instance already owns registry adoption, admission, and cleanup, so placing detachment there removes the stale snapshot without creating a competing lifecycle owner. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 3663c3544571. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
PR surfaceSource +103, Tests +918, Docs +19, Other +2. Total +1042 across 56 files. Added test files: 3. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReviewed October 9, 2026, 4:07 AM ET / 08:07 UTC (Revision 3). |
The PR wrapper extracts only its inventory; the new plugin-instance background runner and its two imports were missing, breaking wrapper provisioning and the eager-import closure check. Also prune the now-obsolete Memory Core mock baseline entry.
|
@clawsweeper re-review The revision-2 requirement is addressed at head |
|
🦞👀 Re-review progress:
|
|
Merged via squash.
|
Backport #167724 together with its required #167679 lifecycle background-context owner. Preserve the release branch database admission implementation and its existing detached context owner. (cherry picked from commit ee5dea9) (cherry picked from commit 226b033) (cherry picked from commit 474bbab335a6cf7b1157097612b81d2d6098e260)
…w#167679) * fix(memory): bind background sync to lifecycle owners Replace the module-level async-context snapshot with the approved instance lifecycle background runner. Each callback uses its exact instance and current adopted registry while preserving host resources and independent completion through disposal cleanup. Carry the runner through runtime, tools, CLI, watchers, manager reuse, and maintenance acquisition. Cover retained and replaced Memory Core reloads through startup, session updates, watch notifications, and intervals. Preserve sync observation throughout the fixture, retain cleanup failures, and share the owner-aware test runner across direct tools and scheduling harnesses. * fix(memory): list lifecycle runner modules in the PR wrapper inventory The PR wrapper extracts only its inventory; the new plugin-instance background runner and its two imports were missing, breaking wrapper provisioning and the eager-import closure check. Also prune the now-obsolete Memory Core mock baseline entry.
What Problem This Solves
After any hot plugin reload, Memory Core's background sync fails on every trigger until the Gateway restarts. Reloads are common: editing
plugins.load/installs,transcripts,agents.defaults.decisionModel, or a plugin entry. The failures are log-only, so memory indexing silently stops:Reproduced on main (dev Gateway, real OpenAI provider,
openaiembeddings): after aplugins.loadhot reload, in-flight turns finish fine, but every later memory sync fails. 7 of 7 failures in the log came after the reload. The nightly dreaming sweep failed the same way ("Pluginmemory-coreruntime is no longer active").User Impact
Why This Change Was Made
extensions/memory-core/src/memory/background-context.tstook one module-levelAsyncLocalStorage.snapshot()at import and ran all background work inside it. That snapshot permanently captured the first plugin registry. Reload correctly retired the memory managers, but their replacements reused the stale snapshot and resolved the retiredopenaiplugin instance.A plugin-local per-registration snapshot was evaluated and ruled out. Provider-only reloads keep Memory Core's registration (the loader skips re-registering retained instances), and registration is not guaranteed to run outside a turn.
The maintainer approved one additive SDK capability on the existing instance-bound lifecycle facade:
It is owned by the plugin-instance lifecycle (
src/plugins/plugin-instance-background.ts). It detaches the caller's turn and request context, selects that exact instance's currently adopted registry, preserves host resource bindings, and honors admission, drain, and cleanup authority. Retired managers keep their old owner and never adopt a replacement. No SDK subpaths or package exports were added. Memory Core's session-sync, watcher, interval, startup catch-up, and cleanup paths now use it, and the module-level snapshot and its import side effect are deleted. The remote memory-files worker keeps its own worker-lifetime context.Evidence
Live (dev Gateway from this change, real OpenAI provider,
openaiembeddings): four turns on both runtimes were in flight whenplugins.loadwas edited. All four finished correctly. There were 0[memory] sync failedlines before or after the reload; on main every later trigger fails. The memory index kept growing after the reload:memory_index_chunksrows were written 15–37 s afterwards, all with realtext-embedding-3-smallembeddings.Tests:
Production +190/-87, tests/support +1030/-112, docs +19/-0.
Main integration verification
ClawSweeper revision 2 requested integration with main's publication-generation lifetime change in
manager.ts. Commit0c1d63993774b835ba843d1cac8cfc642b3d0cf8merges main3b3cd5939993f16017b6747cbcf80a83b49006efwithout conflicts and preserves that lifetime change. The PR remains +1242/-200 across 56 files relative to the integrated main. Independent review of the combined candidate found no actionable findings through P2.Focused proof on the combined candidate: 32 tests passed across five files in 162.63 s wall. This includes retained/replaced Memory Core reloads, detached context, exact-owner retirement, cleanup draining, and publication settlement:
pnpm test src/plugins/memory-background-reload.test.ts src/plugins/api-facades.background.test.ts src/plugins/memory-runtime.owners.test.ts extensions/memory-core/src/memory/manager-close-observation-lifecycle.test.ts extensions/memory-core/src/memory/manager-publication-settlement.test.ts --maxWorkers=1Prior-head CI run 37899879030 passed at
fa781eb3ac9d4cba619621abb90845a137bd82ed(70 successful jobs, 23 skipped). The new head remains subject to its own review and required CI gates.