fix(memory): #2312 — break embedder-rescue recursion that OOM'd CI at 4GB (#2348)

Root cause (found by container bisection, not the suspected SONA/EWC chain):
memory-bridge's rescueAgentdbEmbedder (#2256-followup) monkey-patches
agentdb.embedder.embed to delegate to memory-initializer's generateEmbedding.
But generateEmbedding is bridge-FIRST — it calls bridgeGenerateEmbedding,
which calls registry.getAgentDB().embedder.embed — the patched function:

  generateEmbedding → bridgeGenerateEmbedding → embedder.embed (patched)
    → generateEmbedding → … unbounded async cycle

Microtask-driven, so no stack overflow — just monotonic heap growth to the
V8 limit (4050MB on the CI runner, SIGABRT 134, ~35s). The cycle only arms
when @xenova/transformers fails to init (pipeline=null → rescue applies),
which is why Linux CI died while Windows/macOS dev boxes never reproduced.
TEST 1 passed because the cycle needs the bridge registry initialized first
(post-task's bridgeRecordFeedback does that), which is also why the earlier
CLAUDE_FLOW_SKIP_RUVLLM_TRAJECTORY bypass had no effect — the allocator was
never in the ruvllm/SONA/EWC chain at all (bisection shows them flat at
122MB).

Fix:
- memory-initializer: extract generateLocalEmbedding (transformers/ruvector/
  hash chain, never the bridge). generateEmbedding now delegates to it after
  the bridge attempt — behavior unchanged for normal callers.
- memory-bridge rescue: delegate to generateLocalEmbedding (bridge-free
  leaf). If the import doesn't expose it (stale pairing), decline the rescue
  — fail safe, never recurse.
- Probe hardening: the old probe accepted any non-zero vector, which the
  deterministic hash fallback also satisfies — so it "rescued" agentdb's
  mock with our own mock. Now requires backend === 'onnx'. When declining
  because the local chain is also degraded, tag embedder.backend='mock' so
  bridgeGenerateEmbedding's AUDIT-#3 check stops labeling mock vectors as
  onnx.
- v3-ci.yml: drop continue-on-error from the graph-trajectory smoke — it
  gates again. Verified in a node:22-bookworm container (CI-equivalent
  fresh install + build): 10/10 pass at DEFAULT heap and at 512MB, cold and
  warm model cache. Pre-fix repro in the same container OOM'd in ~36s.
- smoke script: use pathToFileURL for dist imports so it runs on Windows
  (ERR_UNSUPPORTED_ESM_URL_SCHEME) — needed for local debugging of this.

Fixes #2312
This commit is contained in:
rUv authored and GitHub committed 2026-06-12 16:41:33 -04:00
1 parent 53cab89de8
commit 7c6362633e
4 files changed
+68 -23

No files matched your search

+8 -7
View File
@@ -2322,13 +2322,14 @@ jobs:
pnpm install --frozen-lockfile
pnpm -r build
- name: Run trajectory graph edges smoke
# #2312 — TEST 2 (post-task → reinforced-by edge) OOMs at 6 GB+ inside
# the SONA endTrajectory + distillLearning chain (EWCConsolidator init
# is the likely allocator). The graph-edge contract being asserted is
# orthogonal to that path. Gate the step non-blocking until the real
# allocation profile is fixed; TEST 1 (trajectory-step → trajectory-
# caused edge) still runs and gates the same contract.
continue-on-error: true
# #2312 root cause (fixed): memory-bridge's rescueAgentdbEmbedder
# patched agentdb.embedder.embed to delegate to generateEmbedding —
# which is bridge-first, so the patched embed re-entered itself via
# bridgeGenerateEmbedding in an unbounded async cycle (heap OOM at
# the V8 limit, SIGABRT 134; SONA/EWC were innocent). The rescue now
# delegates to generateLocalEmbedding (a bridge-free leaf), so this
# smoke gates again at default heap — verified green in a node:22
# Linux container at 512 MB with a cold model cache.
run: node scripts/smoke-trajectory-graph-edges.mjs
graph-plugin-adapter-smoke:
+10 -6
View File
@@ -9,7 +9,7 @@
* Usage: node scripts/smoke-trajectory-graph-edges.mjs
*/
import { fileURLToPath } from 'url';
import { fileURLToPath, pathToFileURL } from 'url';
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
@@ -17,6 +17,10 @@ import * as os from 'os';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.resolve(__dirname, '..');
const distBase = path.join(projectRoot, 'v3/@claude-flow/cli/dist/src');
// Windows: absolute paths must be file:// URLs for dynamic import() — a bare
// C:\... path fails with ERR_UNSUPPORTED_ESM_URL_SCHEME (hit while debugging
// #2312 locally). No-op on POSIX.
const distImport = (rel) => import(pathToFileURL(path.join(distBase, rel)).href);
// sql.js is available in root node_modules (installed by the CI setup step via
// `npm install --legacy-peer-deps --ignore-scripts` at the repo root).
@@ -72,10 +76,10 @@ console.log('\n[ADR-130 smoke] Phase 3 — SONA trajectory-to-graph hooks\n');
async function testTrajectoryStep() {
console.log('TEST 1: trajectory-step writes trajectory-caused edge');
try {
const { initializeMemoryDatabase } = await import(path.join(distBase, 'memory/memory-initializer.js'));
const { initializeMemoryDatabase } = await distImport('memory/memory-initializer.js');
await initializeMemoryDatabase({ dbPath, force: true });
const mod = await import(path.join(distBase, 'mcp-tools/hooks-tools.js'));
const mod = await distImport('mcp-tools/hooks-tools.js');
const traj = mod.hooksTrajectoryStep ?? mod.allHooksTools?.find(t => t.name === 'hooks_intelligence_trajectory-step');
if (!traj) { fail('1a', 'hooks_intelligence_trajectory-step not found'); return; }
@@ -107,7 +111,7 @@ async function testTrajectoryStep() {
async function testPostTask() {
console.log('\nTEST 2: post-task writes reinforced-by edge');
try {
const mod = await import(path.join(distBase, 'mcp-tools/hooks-tools.js'));
const mod = await distImport('mcp-tools/hooks-tools.js');
const postTask = mod.hooksPostTask ?? mod.allHooksTools?.find(t => t.name === 'hooks_post-task');
if (!postTask) { fail('2a', 'hooks_post-task not found'); return; }
@@ -139,7 +143,7 @@ async function testPostTask() {
async function testPostTaskFailure() {
console.log('\nTEST 3: post-task with success=false does not write reinforced-by edge');
try {
const mod = await import(path.join(distBase, 'mcp-tools/hooks-tools.js'));
const mod = await distImport('mcp-tools/hooks-tools.js');
const postTask = mod.hooksPostTask ?? mod.allHooksTools?.find(t => t.name === 'hooks_post-task');
const beforeCount = await countEdgesByRelation('reinforced-by');
@@ -165,7 +169,7 @@ async function testPostTaskFailure() {
async function testNonBlocking() {
console.log('\nTEST 4: async writes are non-blocking (<200ms tool response)');
try {
const mod = await import(path.join(distBase, 'mcp-tools/hooks-tools.js'));
const mod = await distImport('mcp-tools/hooks-tools.js');
const traj = mod.hooksTrajectoryStep ?? mod.allHooksTools?.find(t => t.name === 'hooks_intelligence_trajectory-step');
const times = [];
+26 -10
View File
@@ -568,25 +568,41 @@ async function rescueAgentdbEmbedder(agentdb: { embedder?: { pipeline?: unknown;
// own embed() does the right thing and we should not interpose.
if (emb.pipeline) return;
type EmbedFn = (text: string) => Promise<{ embedding: number[] | Float32Array; dimensions: number; model: string }>;
let generateEmbedding: EmbedFn | null = null;
// #2312: delegate to generateLOCALEmbedding, NOT generateEmbedding.
// generateEmbedding is bridge-first — routing the rescued embed through it
// closes a cycle (generateEmbedding → bridgeGenerateEmbedding →
// embedder.embed [patched] → generateEmbedding → …) that allocated
// unboundedly via microtasks until V8 hit the heap limit (~4 GB on the CI
// runner, SIGABRT 134). The local chain never consults the bridge, so the
// rescued embedder is a leaf. If the running memory-initializer build
// predates generateLocalEmbedding, decline the rescue entirely — a stale
// pairing must fail safe, not recurse.
type EmbedFn = (text: string) => Promise<{ embedding: number[] | Float32Array; dimensions: number; model: string; backend?: 'onnx' | 'mock' }>;
let localEmbed: EmbedFn | null = null;
try {
const mod = (await import('./memory-initializer.js')) as unknown as { generateEmbedding?: EmbedFn };
generateEmbedding = mod.generateEmbedding ?? null;
const mod = (await import('./memory-initializer.js')) as unknown as { generateLocalEmbedding?: EmbedFn };
localEmbed = mod.generateLocalEmbedding ?? null;
} catch {
return; // can't import the rescuer — leave the mock fallback alone
}
if (!generateEmbedding) return;
const embed: EmbedFn = generateEmbedding;
if (!localEmbed) return;
const embed: EmbedFn = localEmbed;
// Probe once to confirm the rescuer actually returns real (non-zero) vectors.
// If our own fallback chain also dead-ends in mock embeddings, leave
// agentdb as-is rather than swapping one mock for another.
// Probe once to confirm the rescuer actually returns REAL ONNX vectors
// (#2312: the old probe only checked non-zero, which the deterministic
// hash fallback also satisfies — so it "rescued" agentdb's mock with our
// own mock and reported it as real). Require backend === 'onnx'.
try {
const probe = await embed('rescue-probe');
const arr = probe?.embedding ? Array.from(probe.embedding as ArrayLike<number>) : [];
const hasSignal = arr.length > 0 && arr.some((v: number) => Math.abs(v) > 1e-9);
if (!hasSignal) return;
if (!hasSignal || probe.backend !== 'onnx') {
// Local chain is also degraded — leave agentdb's embedder alone, but
// tag it so bridgeGenerateEmbedding's AUDIT-#3 isMock check reports
// backend='mock' truthfully instead of labeling mock vectors 'onnx'.
try { (emb as { backend?: string }).backend = 'mock'; } catch { /* frozen */ }
return;
}
} catch {
return;
}
@@ -1894,6 +1894,30 @@ export async function generateEmbedding(text: string): Promise<{
}
}
return generateLocalEmbedding(text);
}
/**
* Generate an embedding using ONLY the local model chain (transformers.js /
* ruvector ONNX / hash fallback) — never the AgentDB bridge.
*
* #2312: this MUST stay bridge-free. `memory-bridge.ts` rescues a degraded
* agentdb embedder by delegating to this module; if that delegation went
* through `generateEmbedding` (bridge-first), the call would re-enter the
* patched `agentdb.embedder.embed` and recurse unboundedly:
*
* generateEmbedding → bridgeGenerateEmbedding → embedder.embed (patched)
* → generateEmbedding → … (heap OOM at ~4 GB on CI, no stack overflow
* because the cycle is async/microtask-driven)
*
* Keeping the local chain as its own export breaks that cycle structurally.
*/
export async function generateLocalEmbedding(text: string): Promise<{
embedding: number[];
dimensions: number;
model: string;
backend: 'onnx' | 'mock';
}> {
// Ensure model is loaded
if (!embeddingModelState?.loaded) {
await loadEmbeddingModel();