fix(embedding): add foreground-priority gate to prevent turn.start starvation - #2187
fix(embedding): add foreground-priority gate to prevent turn.start starvation#2187pittosporum-seu wants to merge 1 commit into
Conversation
…arvation (MemTensor#2186) The local ONNX embedding provider runs CPU-bound inference sequentially on the main thread. When the background capture pipeline (reflection, reward, skill crystallization) is embedding trace rows, a foreground retrieval request (turn.start) must wait for the entire batch to finish, often exceeding the host's 8s prefetch timeout. Add a lightweight cooperative yield mechanism: - priority-gate.ts: enterForeground()/yieldIfForegroundPending() signals - local.ts: yield between individual ONNX inference calls when foreground pending - retrieve.ts: mark foreground retrieval embed calls via enterForeground() This gives the single-threaded ONNX inference a cooperative scheduling point without changing the Embedder interface or adding worker threads.
🤖 Open Code ReviewTarget: PR #2187 🔍 OpenCodeReview found 5 issue(s) in this PR. 1.
|
✅ Automated Test Results: PASSEDAll tests passed (6/6 executed). memos_local_plugin/unit: 6/6. Duration: 2s Branch: |
Description
Add a lightweight foreground-priority gate to the embedding layer to prevent
turn.startRPC starvation when the background capture pipeline (reflection, reward, skill crystallization) is running CPU-bound ONNX inference.Problem: The local ONNX embedding provider (
Xenova/all-MiniLM-L6-v2) runs inference sequentially on the main thread. When background tasks embed trace rows in a batch loop, foreground retrieval requests must wait for the entire batch to finish — often exceeding the host's 8-second prefetch timeout. This causes memory injection to be silently skipped on every affected turn.Solution: A cooperative yield mechanism (
priority-gate.ts) that:enterForeground()before embeddingisForegroundPending()between individual inference calls and yields the event loop viasetImmediateEmbedderinterface or adding worker threadsDesign rationale: Minimal, non-breaking change. No interface changes to
Embedder, no new dependencies, no worker threads. The yield is a singlesetImmediatebetween ONNX calls — negligible overhead when no foreground is pending (just a boolean check).Related Issue (Required): Fixes #2186
Type of change
How Has This Been Tested?
Unit test:
tests/unit/embedding/priority-gate.test.ts— 6 tests covering:Additionally verified:
npx tsc --noEmitpasses with zero errorsChecklist
Changes Made
core/embedding/priority-gate.tscore/embedding/providers/local.tscore/embedding/index.tscore/retrieval/retrieve.tstests/unit/embedding/priority-gate.test.ts