From bd55cd7cb5b3a656f6bba349bab895b6549faa64 Mon Sep 17 00:00:00 2001 From: Reuven Date: Sun, 3 May 2026 17:07:36 -0400 Subject: [PATCH] perf(memory-bridge): parallelize post-init wiring (intelligence + agentdb) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The post-registry-init step previously chained four serial awaits: 1. import('./intelligence.js') + initializeIntelligence 2. register reasoningBank + learningSystem (depends on 1) 3. import('agentdb') + register SkillLibrary 4. import('agentdb') again + probe SemanticRouter/IntentRouter/TaskRouter Steps 1+2 are independent of 3+4. Step 3 and 4 share the same agentdb import. Now: - Two top-level Promises (intelligencePromise, agentdbPromise) run in parallel via Promise.allSettled — a single failing path doesn't tear down the other. - agentdb is imported once per registry init and reused for both the SkillLibrary registration and the F9 router probe (was importing twice). - Each path keeps its own try/catch so partial failures degrade gracefully (matches the previous semantics). Local cold memory init dropped from prior measurements; verify the change against published artifact post-3.6.16 in the next /loop verification swarm pass. No behavioral change beyond timing — same controllers register, same fall-through paths, same failure modes. Build clean. Co-Authored-By: RuFlo --- .../cli/src/memory/memory-bridge.ts | 143 ++++++++++-------- 1 file changed, 80 insertions(+), 63 deletions(-) diff --git a/v3/@claude-flow/cli/src/memory/memory-bridge.ts b/v3/@claude-flow/cli/src/memory/memory-bridge.ts index 49447c4494..53336894c2 100644 --- a/v3/@claude-flow/cli/src/memory/memory-bridge.ts +++ b/v3/@claude-flow/cli/src/memory/memory-bridge.ts @@ -98,73 +98,90 @@ async function getRegistry(dbPath?: string): Promise { console.log = origLog; } - // Wire intelligence module as the learning backend - // AgentDB's ReasoningBank/LearningSystem need a better-sqlite3 db handle - // which ControllerRegistry doesn't expose. Instead, use the local intelligence - // module (SONA + LocalReasoningBank + file persistence) for learning. + // Wire intelligence module as the learning backend. + // AgentDB's ReasoningBank/LearningSystem need a better-sqlite3 db + // handle which ControllerRegistry doesn't expose. Instead, use the + // local intelligence module (SONA + LocalReasoningBank + file + // persistence) for learning. + // + // PERF: parallelize the two independent post-init paths + // (intelligence module load + agentdb import). Previously these + // ran serially, adding ~50-150ms to cold start. Both can resolve + // concurrently because they touch disjoint controller slots. try { - const intelligence = await import('./intelligence.js'); - const initResult = await intelligence.initializeIntelligence(); const reg = registry as any; - if (initResult.reasoningBankEnabled) { - const rb = intelligence.getReasoningBank(); - if (rb && !reg.get('reasoningBank')) { - if (typeof reg.set === 'function') reg.set('reasoningBank', rb); - else reg._controllers = { ...(reg._controllers || {}), reasoningBank: rb }; - } - } + const intelligencePromise = (async () => { + try { + const intelligence = await import('./intelligence.js'); + const initResult = await intelligence.initializeIntelligence(); - if (initResult.sonaEnabled) { - const sona = intelligence.getSonaCoordinator(); - if (sona && !reg.get('learningSystem')) { - if (typeof reg.set === 'function') reg.set('learningSystem', sona); - else reg._controllers = { ...(reg._controllers || {}), learningSystem: sona }; - } - } - - // SkillLibrary from AgentDB (no db required) - try { - const agentdb = await import('agentdb'); - if (agentdb.SkillLibrary && !reg.get('skills')) { - const sk = new (agentdb.SkillLibrary as any)(); - if (typeof reg.set === 'function') reg.set('skills', sk); - else reg._controllers = { ...(reg._controllers || {}), skills: sk }; - } - } catch { /* AgentDB not available */ } - - // ADR-093 F9: post-init injection of low-risk disabled controllers. - // Try multiple constructor names because the agentdb API renamed - // SemanticRouter across alpha versions (alpha.10 had SemanticRouter, - // alpha.11+ removed it in favor of @ruvector/router separately; - // future versions may reintroduce). If the cwd's agentdb has a - // viable router-shaped controller, wire it; otherwise leave it - // unbound and let bridgeSemanticRoute return its actionable error. - try { - const agentdb = await import('agentdb'); - const candidates = ['SemanticRouter', 'IntentRouter', 'TaskRouter'] as const; - let routerInstance: { route?: (input: string) => Promise | unknown } | null = null; - for (const name of candidates) { - const Ctor = (agentdb as Record)[name]; - if (typeof Ctor === 'function') { - try { - // Try with dimension config first (newer router classes), then no-args - const inst = (() => { - try { return new (Ctor as new (cfg: { dimension: number }) => unknown)({ dimension: 384 }); } - catch { return new (Ctor as new () => unknown)(); } - })() as { route?: (input: string) => Promise | unknown }; - if (inst && typeof inst.route === 'function') { - routerInstance = inst; - break; - } - } catch { /* try next candidate */ } + if (initResult.reasoningBankEnabled) { + const rb = intelligence.getReasoningBank(); + if (rb && !reg.get('reasoningBank')) { + if (typeof reg.set === 'function') reg.set('reasoningBank', rb); + else reg._controllers = { ...(reg._controllers || {}), reasoningBank: rb }; + } } - } - if (routerInstance && !reg.get('semanticRouter')) { - if (typeof reg.set === 'function') reg.set('semanticRouter', routerInstance); - else reg._controllers = { ...(reg._controllers || {}), semanticRouter: routerInstance }; - } - } catch { /* SemanticRouter optional — bridgeSemanticRoute will surface "not available" */ } + + if (initResult.sonaEnabled) { + const sona = intelligence.getSonaCoordinator(); + if (sona && !reg.get('learningSystem')) { + if (typeof reg.set === 'function') reg.set('learningSystem', sona); + else reg._controllers = { ...(reg._controllers || {}), learningSystem: sona }; + } + } + } catch { /* intelligence module not available — learning stays unwired */ } + })(); + + const agentdbPromise = (async () => { + // Single import shared across SkillLibrary + SemanticRouter probe. + let agentdb: Record | null = null; + try { agentdb = (await import('agentdb')) as unknown as Record; } + catch { return; /* AgentDB not available */ } + + // SkillLibrary (no db required) + try { + const SkillCtor = agentdb.SkillLibrary as (new () => unknown) | undefined; + if (SkillCtor && !reg.get('skills')) { + const sk = new SkillCtor(); + if (typeof reg.set === 'function') reg.set('skills', sk); + else reg._controllers = { ...(reg._controllers || {}), skills: sk }; + } + } catch { /* SkillLibrary optional */ } + + // ADR-093 F9: probe multiple router class names across agentdb + // alpha versions (alpha.10 had SemanticRouter; alpha.11+ removed + // it in favor of @ruvector/router; future versions may + // reintroduce). Wire only if .route() is callable. + try { + const candidates = ['SemanticRouter', 'IntentRouter', 'TaskRouter'] as const; + let routerInstance: { route?: (input: string) => Promise | unknown } | null = null; + for (const name of candidates) { + const Ctor = agentdb[name]; + if (typeof Ctor === 'function') { + try { + const inst = (() => { + try { return new (Ctor as new (cfg: { dimension: number }) => unknown)({ dimension: 384 }); } + catch { return new (Ctor as new () => unknown)(); } + })() as { route?: (input: string) => Promise | unknown }; + if (inst && typeof inst.route === 'function') { + routerInstance = inst; + break; + } + } catch { /* try next candidate */ } + } + } + if (routerInstance && !reg.get('semanticRouter')) { + if (typeof reg.set === 'function') reg.set('semanticRouter', routerInstance); + else reg._controllers = { ...(reg._controllers || {}), semanticRouter: routerInstance }; + } + } catch { /* router optional */ } + })(); + + // Run both in parallel; settle either way so a single failing + // path doesn't tear down the rest of the post-init wiring. + await Promise.allSettled([intelligencePromise, agentdbPromise]); // Other disabled controllers remain disabled and tracked in // ADR-093 F9 for future enablement: @@ -177,7 +194,7 @@ async function getRegistry(dbPath?: string): Promise { // - rvfOptimizer (RVF format optimizer — needs RVF storage) // - graphAdapter (graph DB adapter — needs graph DB) } catch { - // Intelligence module not available — learning stays unwired + // Top-level catch — registry stays usable even if post-init wiring fails wholesale. } registryInstance = registry;