Commit Graph

17 Commits

Author SHA1 Message Date
Dragan Spiridonov 3bc09cda6d fix(llm-router): address 10 findings from qe-devils-advocate audit
A post-Phase-4 devil's-advocate audit identified 10 issues. All addressed
with code fixes + tests. Some were real bugs (silent billing regression,
2 unwired MCP tools, 2 HybridRouter routing bugs); others were robustness
improvements (split singletons, silent errors, decorative type fields).

Findings → fixes (severity tagged in audit report):

1. CRITICAL — silent billing regression on upgrade
   Users with ANTHROPIC_API_KEY in env would silently start paying for
   Claude API calls on every Fleet operation. The CHANGELOG's "opt out
   via code changes" wasn't sufficient — needed an env-only escape.
   Fix: AQE_LLM_ROUTER_DISABLED=1 env kill-switch honored in both
   QEKernelImpl._initializeLLMRouter and src/mcp/tools/base.ts
   getSharedLLMRouter. Truthy values disable; 'false'/'0'/'no'/'off'/''
   pass. Unified parse rule between kernel and MCP base.

2. HIGH — 2 unwired MCP standalone tools (audit said 3; QualityCriteria
   isn't LLM-aware so excluded)
   src/mcp/tools/test-generation/generate.ts:82 was calling
   createTestGeneratorService(memory) without router. Same for
   visual-accessibility/index.ts:177. Both now use the
   createXxxServiceWithDependencies factories with { memory, llmRouter }.

3. CRITICAL — E2E only tested manual routing mode, not production
   Production default is rule-based. Adding two new E2E tests for
   rule-based mode SURFACED two real bugs in HybridRouter that I fixed:

   3a. executeWithFallback hardcoded ['claude','openai','ollama'] as the
       only fallback providers; gemini/openrouter/azure-openai/bedrock
       users had no fallback even though ProviderManager can construct
       them. Plus the user's defaultProvider wasn't auto-included in the
       chain. Both fixed at src/shared/llm/router/hybrid-router.ts:612.

   3b. A non-retryable error from one provider (e.g. Claude "invalid
       x-api-key") aborted the entire fallback loop via `throw error`,
       defeating the whole point of a fallback chain. Fixed: skip the
       inter-attempt delay for non-retryable but continue to the next
       fallback entry.

4. HIGH — MCPToolContext.llmRouter field was decorative
   Added the field but no code populated it. Every getLLMRouter(context)
   call fell through to getSharedLLMRouter().
   Fix: added llmRouter and memory to BaseMCPTool.invoke() options;
   qe-tool-bridge.ts and registry.ts now explicitly forward the shared
   singleton via { llmRouter } at invoke time.

5. HIGH — 14 of 15 wired services had only STRUCTURAL verification, not
   behavioral (only TestExecutor had a chat() round-trip test)
   Fix: new tests/integration/llm-router-behavioral.test.ts asserts
   isLLMAnalysisAvailable()===true (the exact gate the audit named) for
   all 14 services + 2 full behavioral round-trips. 16 tests total.

6. HIGH — MCP and kernel built SEPARATE HybridRouter instances
   Different cost trackers, caches, circuit breakers per side.
   routing_economics/routing_metrics would only see half the traffic.
   Fix: kernel.ts._initializeLLMRouter calls setSharedLLMRouter(router)
   on init so both paths share one instance. kernel.dispose() calls
   resetSharedLLMRouter() so the next kernel doesn't inherit the
   previous instance.

7. HIGH — Silent error swallowing in _initializeLLMRouter
   console.warn'd then continued without LLM, with no signal for
   monitoring. A misconfigured provider would silently degrade with no
   alert.
   Fix: now publishes kernel.llm-router.init-failed (caught error) and
   kernel.llm-router.init-no-provider (enabled=true but nothing
   available) events on the event bus. Test confirms event delivery.

8. MEDIUM — Service count mismatch in docs (ADR/CHANGELOG said "14
   services / 12 domains"; test enumerates 15 services / 11
   LLM-enhanced domains)
   Fix: ADR-043 addendum + CHANGELOG corrected to 15/11. Added
   explicit service list and noted the 2 non-LLM domains
   (enterprise-integration, coordination).

9. MEDIUM — aqe llm config --set apiKey=... silently dropped the key
   stripApiKeys() returned a successfully-saved file without the key,
   user thought it was saved.
   Fix: CLI updateConfig() now throws a clear error pointing to env
   vars BEFORE calling save. stripApiKeys also now logs a console.warn
   when it strips anything, so programmatic callers see the strip.

10. LOW — Env vs disk precedence was counterintuitive and undocumented
    Force-enable-on-env overrides explicit `enabled: false` on disk.
    Fix: doc comment in config-store.applyEnvProviderDetection +
    ADR-043 "Precedence & env-var conventions" section explains.

Verification
- npx tsc --noEmit: clean
- 191 unit + integration tests pass (16 new behavioral tests for #5,
  17 wiring tests, gemini provider, config-store, llm-router-service,
  kernel-llm-router with 17 tests including all kill-switch cases,
  CLI persistence with 28 tests)
- 5 real-provider E2E tests pass (Gemini 2.5 Flash, OpenAI gpt-4o-mini,
  OpenRouter, plus 2 rule-based-mode tests that previously failed and
  now pass with the HybridRouter fallback fixes)

Refs ADR-043 audit findings (2026-05-21)
Builds on 94033e3 (Phase 1), be1e60d (Phase 2), c206c81 (Phase 3+4)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 12:09:18 +00:00
Dragan Spiridonov 94033e3500 feat(llm-router): Phase 1 — kernel singleton + persistence + Gemini key alias
Wires the foundation for ADR-043's HybridRouter into the kernel boot
sequence so domain plugins can finally receive an llmRouter dependency.
Phase 1 lands the kernel/CLI/provider changes; Phase 2 will plumb the
router through all 13 domains.

Background: the LLM-enhancement code paths added in ADR-051
(isLLMAnalysisAvailable() across 14 services) were unreachable because
no construction site ever passed an llmRouter — services accepted the
optional dependency but no caller injected it. ProviderManager +
HybridRouter were only instantiated from the `aqe llm advise` CLI
subcommand.

Changes
- src/shared/llm/providers/gemini.ts: read GOOGLE_API_KEY as a third
  fallback after GOOGLE_AI_API_KEY and GEMINI_API_KEY, so users with
  the conventional Google key name don't need to alias it themselves.
- src/shared/llm/router/config-store.ts (new): load/save
  .agentic-qe/llm-config.json with env-detection layering
  (defaults < disk < env < explicit override). Strips apiKey fields
  defensively before persisting. Filters out providers the
  ProviderManager doesn't yet construct (onnx) to keep boot logs
  clean.
- src/shared/llm/llm-router-service.ts (new): builds a HybridRouter
  from the loaded config, picks primary/fallbacks from the available
  set honoring the user's defaultProvider when possible. Accepts an
  injected ProviderManager for tests.
- src/kernel/interfaces.ts: extends QEKernel with llmRouter? and
  KernelConfig with llmRouter.enabled ('auto'|true|false) +
  configOverride + providerManager test-injection.
- src/kernel/kernel.ts: kernel-singleton llmRouter built during
  initialize(), exposed via kernel.llmRouter, forwarded to all 13
  DOMAIN_FACTORIES as a 4th arg. Phase 2 wires each factory through
  to its coordinator/services; for now the wrappers accept-and-discard.
- src/cli/commands/llm-router.ts: replaces the
  `// would be persisted in real implementation` stub with real
  read/write through the config-store. `aqe llm config --set` now
  survives a process restart. Adds AQE_CONFIG_ROOT env var as a test
  seam so tests don't mutate the real .agentic-qe/.

Tests (110 total, all passing)
- gemini.test.ts: +3 new tests for each env-var alias, existing
  tests updated to clear all three names
- config-store.test.ts (new, 29 tests): load/save/merge precedence,
  env detection, secret stripping, atomicity
- llm-router-service.test.ts (new, 10 tests): pickEnabledProviders,
  pickPrimaryAndFallbacks, end-to-end with injected mock provider
- kernel-llm-router.test.ts (new, 4 tests): kernel boot with router
  enabled=false / auto-no-providers / injected mock, chat() round-trip
- llm-router.test.ts (CLI): +2 persistence-to-disk tests, env-isolated
- tests/mocks/index.ts: shared createMockLLMProvider() helper

Verification
- npx tsc --noEmit: clean
- 110 tests pass; no regressions in existing kernel.test.ts (34/34)
- Stderr noise from PersistentSONAEngine init is pre-existing and
  unrelated to this change.

What this commit does NOT do
- Does NOT change behavior for any user yet — DOMAIN_FACTORIES
  wrappers still discard the llmRouter param. Phase 2 lights up the
  domain services.

Refs ADR-043 (vendor-independent LLM), ADR-051 (LLM-enhanced services)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 09:38:40 +00:00
Dragan Spiridonov c83c0345b3 chore(release): bump version to v3.9.34
Stops unbounded growth of patterns.rvf / brain.rvf.

- #495: AbortSignal in HybridMemoryBackend.initialize() so a 5s
  createHybridBackendWithTimeout abort actually cancels the work
  instead of leaking a 14-min runaway init (~20 GB / 12 MB/s leak
  observed in the field). UnifiedMemoryManager.initialize() also
  accepts {signal} with throwIfAborted() at each await seam.
- Post-dream compaction in DreamScheduler — threshold-gated compact()
  against patterns.rvf and brain.rvf so append-only growth has a
  steady-state janitor.
- Boot-time size guard in getSharedRvfAdapter() for one-shot recovery
  of installations already in the bad state.
- Bounded SQLite→RVF backfill (top-1000 by last_used_at) so
  `: > patterns.rvf` for disk recovery doesn't trigger an unbounded
  replay (the "regrew to 59 GB" pathology).
- RvfDualWriter.compact() for brain.rvf parity.

Three operator env knobs added (no rebuild needed):
- AQE_RVF_SIZE_GUARD_BYTES (default 256 MB)
- AQE_RVF_DEAD_RATIO_THRESHOLD (default 0.30)
- AQE_RVF_BACKFILL_LIMIT (default 1000)

29 new unit tests. Performance gate 15/15 pass. test:ci 4035 pass
(3 pre-existing failures, verified identical on main).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 17:42:55 +00:00
Dragan Spiridonov 358990cc01 fix(kernel,learning): namespace-aware reads + timestamp rehydration (#491 Bug 2+3)
Issue #491 — even with the worker→kernel wiring restored (Bug 1/4b),
`mineExperiences` mined 0 for every domain on installs with hundreds
of indexed experiences. Two stacked bugs in the learning read path:

Bug 2 — namespace mismatch (the keystone)
=========================================
LearningCoordinator writes every experience with
`namespace: 'learning-optimization'`, but the MemoryBackend
interface only let writers specify a namespace; reads had no such
option. HybridBackend.get/search/has/delete therefore hard-coded
`defaultNamespace='default'`. The coordinator could not read back
its own data — `learning:experience:index:domain:<d>:*` returned 0
keys even when the DB held 81+ matching rows in the
`learning-optimization` namespace.

Fix:
  - Add `RetrieveOptions { namespace? }` to kernel/interfaces.ts
    (symmetric with the existing `StoreOptions.namespace`).
  - Extend MemoryBackend.get/has/delete/search to accept the option
    (backward-compatible — undefined still routes to defaultNamespace).
  - HybridBackend honors the option in all four methods.
  - InMemoryBackend likewise; its `search` now also scopes the scan
    by namespace prefix when one is supplied, so unit-test fakes
    behave the same as the production hybrid backend.
  - All 15 read sites in LearningCoordinator pass
    `LEARNING_NS = { namespace: 'learning-optimization' }`, matching
    the writes that already specified it.

One pre-existing unit test (memory-backend.test.ts) was calling
`backend.get(key, 'test-ns')` against the undocumented positional
namespace param InMemoryBackend used to accept. Migrated it to the
options form — the positional shape was never part of the
MemoryBackend interface.

Bug 3 — Date vs ISO-string in time-window filter
================================================
`recordExperience` stores `timestamp: new Date()`. JSON.stringify
serializes it to an ISO string and JSON.parse does not rehydrate.
`TimeRange.contains` does `date >= start`; in `string >= Date`, the
string coerces to NaN and every comparison is false — so even with
Bug 2 fixed, the window filter would still silently drop every row.

Fix: in `getExperiencesByDomainAndTime`, coerce
`experience.timestamp` to a real `Date` at the kv-read boundary
before calling `timeRange.contains`. Downstream callers receive a
hydrated Experience with a proper Date. We deliberately fix this at
the boundary rather than making `TimeRange.contains` tolerant of
strings — the bug is "kv round-trip drops the Date type", not "the
value object is wrong", and a tolerant comparator would mask future
type drift everywhere else.

Round-trip integration test:
tests/integration/learning/coordinator-roundtrip.test.ts
pins three assertions:
  1. recordExperience → mineExperiences finds the experience back
     (this returned 0 before either fix).
  2. The mined experience's timestamp is a real Date instance, not a
     string (proves Bug 3 fix is at the boundary).
  3. A time window deliberately in the past still excludes recent
     writes (proves we didn't fix Bug 3 by making the filter a
     no-op).

Verified red→green: with Bugs 2+3 reverted (interface kept), tests
1 and 2 fail with `experienceCount=0` and `rows.length=0` — exactly
the symptoms from the issue. With both fixes in place, 3/3 pass in
~30ms. 466/466 kernel unit tests and 335/335 adjacent suites stay
green; the only test that needed migration is documented above.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-17 08:46:53 +00:00
Dragan Spiridonov 43ebcaa326 fix(routing,kernel,cli): audit follow-ups — CRITICAL #1 + HIGH #1/#2 + smoke
Addresses three findings from the brutal-honesty audit of the Phase 1-4
session (commits 66553141..70154c1b). Real integration smoke included.

CRITICAL #1 — mincut safety gate inverted on empty graph
========================================================

Before this fix, `QEReasoningBank.routeTask` consulted
`getSharedMinCutMonitor().isCritical()` unconditionally. The mincut
calculator returns 0 on an empty graph; `isCritical()` is defined as
`minCutValue < warningThreshold` — so an empty graph reported as
critical and ε was dampened to 0.2x.

CLI hook routing is the ONLY place ADR-095 exploration matters in
practice, and the SwarmGraph is empty there (Queen coordinator activity
is what populates it). So exploration was permanently dampened in the
exact deployment case ADR-095 was meant to help.

Fix:
  - new src/learning/routing-topology-gate.ts with
    `resolveTopologyCriticalFromSharedMincut()` — returns true ONLY
    when graph has actual vertices AND `isCritical()` returns true.
    Empty graph = no signal = full ε rate.
  - qe-reasoning-bank.ts routeTask now uses the helper.
  - 4 regression tests in tests/unit/learning/routing-mincut-safety-gate.test.ts
    cover: never-initialized, initialized-but-empty (regression case),
    populated-with-critical (legit dampening), populated-with-healthy.

HIGH #2 — dream-scheduler-wiring test passed by way of failure
==============================================================

The previous test used `memoryBackend: 'memory'` which made the
DreamEngine.initialize() fail ("UnifiedMemoryManager not initialized")
in every run. The kernel's try/catch caught the failure and the test
asserted the kernel survived — NOT that the scheduler actually started.
The happy-path was never exercised.

Rewrite uses a real tmpdir SQLite backend AND resets BOTH singletons
between tests (UnifiedMemoryManager + UnifiedPersistenceManager — the
latter was missing). Adds an assertion that `_dreamScheduler !== undefined`
after initialize() so we know the scheduler ACTUALLY STARTED.

The previous "failure tolerance" test was removed: with proper singleton
hygiene there's no clean way to force scheduler init to fail without
invasive mocking. The behavior is enforced by the try/catch in kernel.ts
and visible by inspection.

Also added an `await scheduler.dispose()` in the kernel's catch path so
a partially-constructed scheduler doesn't leak.

HIGH #1 — Real integration smoke against a tmpdir
==================================================

Ran (and documented):

  1. `aqe init --auto` in /tmp/aqe-smoke-* — fresh shop
  2. Inspected generated .agentic-qe/workers/start-daemon.cjs:
     ✓ contains `candidates.push(require.resolve('agentic-qe/dist/mcp/bundle.js'))`
     ✓ contains `WARNING: using npx fallback` message
     (B.1 verified in actual artifact, not just source template)
  3. Confirmed routing_outcomes schema in the fresh DB:
     ✓ exploration INTEGER, criticality REAL, q_weight REAL all present
     (ADR-095 migration applies at CREATE TABLE time for new installs)
  4. Hand-seeded 12 exploit + 3 explore rows; ran `aqe learning loop-health`:
     ✓ JSON path surfaces routingDiversification with correct counts,
       avg quality per bucket, criticality, q_weight
     ✓ Text path now surfaces the section even when loop-health kv is
       absent (bug fix below)

CLI-bundle smoke for `aqe hooks route` and full daemon start OOMs in
this 8GB codespace even at 4GB heap — that's an environment limitation
documented under qe-reasoning-bank.test.ts; CI has the headroom.

Bug found during smoke: aqe learning loop-health (non-JSON) returned
early when no loop-health kv record existed, never reaching the
routing-diversification render. Operators in CLI-only deployments
(routing happens but daemon-side workers never tick) would see an
empty dashboard. Fixed by removing the early return.

Honest framing on qe-reasoning-bank.test.ts "flaky" claim
==========================================================

It isn't flaky — it's resource-heavy. The project already excludes it
from `test:unit:fast` (package.json) and runs it in `test:ci` where
heap headroom exists. 58 tests × full transformer-embeddings init +
HNSW indexing exceeds an 8GB codespace's default heap.

I framed it as "flaky" in the audit. That was wrong. Added an extended
header comment to the test file documenting the project convention so
future readers don't repeat the same mistake.

Test status
===========

638/638 tests pass across the 47 impacted unit suites. The +4 net
additions (vs 634) are the new routing-mincut-safety-gate tests;
dream-scheduler-wiring stayed at 5 tests (one removed, four expanded).
Build clean. Type-check clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 16:25:47 +00:00
Dragan Spiridonov 6236a2cdc7 feat(kernel): kernel-side dream cycles + hooks-as-producers boundary (ADR-094, #488 Phase 2)
Moves dream cycles out of hook subprocesses into the long-lived kernel
process via the DreamScheduler that already existed but was never wired.
Single cutover, no feature flag — per maintainer decision on ADR-094.

Why this matters
================

Before this commit, every ~20 Edit/Write tool uses triggered a dream cycle
inside a short-lived `npx aqe hooks post-edit` subprocess. The cycle held
a SQLite write transaction for up to 10 seconds while the kernel-owning
MCP server, other hook subprocesses, and user MCP tool calls queued
behind it. stderr from the cycle was consumed by Claude Code's hook
reader and lost from operator visibility. Cold DreamEngine init paid
every ~20 edits.

Now the kernel's `QEKernelImpl.initialize()` boots a `DreamScheduler`
that runs cycles on its own cadence inside the long-lived process. Hook
subprocesses keep only `incrementDreamExperience` (a counter bump that
returns in ~50ms). Operators see `aqe learning loop-health` report the
`dreamScheduler` component as live within one tick.

What landed
===========

src/kernel/kernel.ts
  - new private `_dreamScheduler?: DreamScheduler`
  - new config flag `enableDreamScheduler` (default `true`, mirrors
    `enableExperienceBridge`)
  - initialize() constructs DreamEngine + DreamScheduler after the
    bridge starts, wraps in try/catch (best-effort), failure logs a
    warning and continues (kernel boot never blocks on this)
  - dispose() stops the scheduler BEFORE bus/memory cleanup so a dream
    in-progress doesn't write to a disposed backend

src/learning/dream/dream-scheduler.ts
  - executeDream() now records loop-health on every tick (success or
    failure) via the helper from #488 Phase 1 B.2. Operators see
    `learning:loop-health.components.dreamScheduler.successesSinceBoot`
    grow on each cycle.

src/cli/commands/hooks-handlers/editing-hooks.ts
src/cli/commands/hooks-handlers/task-hooks.ts
  - dropped `checkAndTriggerDream` import + call
  - kept `incrementDreamExperience` (the counter is the producer-side
    signal the kernel scheduler reads)
  - JSON output now emits `dreamTriggered: false, dreamReason:
    'deferred-to-kernel'` so operator scripts grepping the field see
    *where* the cycle actually runs

src/cli/commands/hooks-handlers/hooks-shared.ts
  - removed the `checkAndTriggerDream` re-export. The function itself
    is still defined in hooks-dream-learning.ts and exported there for
    direct test access and the documented rollback path.

tests/unit/architecture/hooks-boundary.test.ts (NEW)
  - greps `src/cli/commands/hooks-handlers/` for forbidden patterns:
    imports from `src/learning/dream/` and references to
    `checkAndTriggerDream`. Skips comment lines so explanatory mentions
    don't false-positive. `hooks-dream-learning.ts` is in `allowedFiles`
    since it DEFINES the function.
  - This is the enforcement mechanism for the boundary contract. A
    contributor accidentally re-adding heavy hook-side work gets a
    clear CI failure pointing them at ADR-094.

tests/unit/kernel/dream-scheduler-wiring.test.ts (NEW)
  - asserts `enableDreamScheduler` default, opt-out path, init+dispose
    cleanliness with and without the scheduler, idempotent dispose

tests/unit/cli/commands/post-edit-dream-trigger.test.ts (REWRITTEN)
  - was asserting checkAndTriggerDream was called from post-edit (the
    #480 fix). That contract is now wrong — the kernel is the only
    place that triggers dreams.
  - now asserts: counter is bumped (the kernel reads it), no inline
    cycle trigger, JSON emits `deferred-to-kernel`, hook stays robust
    when the memory backend throws.

docs/implementation/adrs/ADR-094-kernel-side-dream-cycles.md
  - status: Implemented
  - migration section replaced with the single-cutover rationale and
    a documented 4-line rollback path

583/583 tests pass across CLI, workers, bridge, learning, init/phases,
architecture, and kernel suites. Build clean.

Boundary contract (now enforced in CI)
======================================

Hook subprocesses MUST NOT do work that exceeds ~100ms. Acceptable
hook-side work: producer SQLite writes, counter increments, in-memory
routing lookups. Unacceptable: DreamEngine init, multi-second SQLite
transactions, anything that depends on long-lived state.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 14:25:45 +00:00
Dragan Spiridonov e8105b887d test(kernel): pass enableExperienceBridge: false in 5 lazy-loading assertions
After v3.9.28's bridge fix, kernel.initialize() eager-loads all enabled
domain plugins when the bridge is enabled (default). Five existing tests
asserted lazy semantics (no plugins loaded after initialize, isDomainLoaded
returns false, getPendingDomains returns the list, getDomainAPI returns
undefined, domain health 'healthy' before load) — those assertions only
hold when the bridge is off. Pass the new opt-out flag in those tests so
they exercise the lazy path explicitly.

Caught by the npm-publish workflow's Tests-on-tag SHA gate, not by the
PR-merge CI matrix that ran fast unit tests differently.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 10:18:10 +00:00
Dragan Spiridonov da638f9079 feat(kernel): migrate NativeHnswBackend from @ruvector/router to hnswlib-node (#399)
@ruvector/router 0.1.28 was found to have four serious bugs that
together broke production code-intelligence semantic search and
forced the v3.9.5 hotfix to disable native HNSW entirely:

  1. HNSW search returned essentially random results — recall@10 was
     0% on the project's FNV fixture and 10% on textbook unit-Gaussian
     random vectors. The graph walk could not even find self-vectors
     when querying with a stored vector. Reproduced deterministically
     on linux-arm64; matches the linux-x64 GitHub Actions failures
     reported in #399.
  2. The VectorDb constructor unconditionally wrote a `vectors.db`
     redb file to the current working directory, polluting users'
     project roots with multi-MB files outside .agentic-qe/, in a
     non-SQLite format that no AQE migration or backup tooling knew
     about. Direct violation of the unified memory architecture.
  3. The redb file held a process-wide exclusive lock — only ONE
     VectorDb instance could exist per process. Subsequent constructors
     threw "Database error: Database already open. Cannot acquire lock."
  4. NAPI dispose did not synchronously release the redb lock (NAPI GC
     is not synchronous). The lock outlived dispose() and caused the
     v3.9.5 futex deadlock when the indexer tried to recreate after
     reset.

NativeHnswBackend is rewritten to wrap hnswlib-node@^3.0.0 — the
canonical Node.js binding for Yury Malkov's reference C++ Hnswlib
implementation, used by Pinecone, Weaviate, Qdrant, LangChain, and
ChromaDB. Empirical verification on the same fixture
(`scripts/diagnose-issue-399*.mjs`):

  - hnswlib-node:        recall@10 = 100%, top-1 = id 42 ✓
  - @ruvector/router:    recall@10 =  10%, top-1 = id 999 (random)

Insert is also faster (~324ms vs ~430ms for 1k vectors) and search
is comparable (0.35ms vs 0.21ms with correct vs wrong results).

No new dependency was added — `hnswlib-node` was already a direct
dependency in `package.json` from the legacy code path in
`src/integrations/embeddings/index/HNSWIndex.ts`.

Implementation notes:

  - Drops the local `vectorStore` mirror that the previous backend
    needed to re-score wrong native results — hnswlib-node returns
    correct distances directly.
  - Drops the `bruteForceSearch` private fallback — there is no
    fallback path because hnswlib-node always returns correct results.
  - `clear()` now recreates the underlying HierarchicalNSW from
    scratch instead of just markDelete-ing every label. The
    tombstone-only approach leaks state across long test runs that
    share the singleton HnswAdapter registry, producing duplicate
    labels from `searchKnn` after many cycles.
  - `search()` overshoots k by 2x and dedups by id as a defensive
    measure against any future hnswlib-node duplicate-label edge
    case, then truncates back to k.
  - `add()` doubles maxElements via `resizeIndex()` when the live id
    count fills the current capacity, so callers don't need to know
    the final index size at construction time.
  - `useNativeHNSW` default flipped back to `true`. Users with large
    codebases get sublinear HNSW search by default again.
  - All four `@ruvector/router` failure modes have explicit regression
    guards in the unit and integration test suites.

The two #399-skipped tests in `phase1-integration.test.ts`
(line 222 and 1185) are un-skipped and now pass green against the
new backend. The three flag-default tests that asserted
`useNativeHNSW: false` (broken by v3.9.5's hotfix) are updated to
assert `true` again.

Stale comments referencing `@ruvector/router`'s lock behavior are
updated in `src/kernel/unified-memory.ts`, `src/init/phases/06-code-intelligence.ts`,
`src/init/phases/10-workers.ts`, and `src/cli/index.ts`. The CLI
status output (`aqe ruvector status`) and the `useNativeHNSW` flag
description are updated to mention `hnswlib-node` instead of
`@ruvector/router`.

Refs #399, ADR-090.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 18:22:24 +00:00
Dragan Spiridonov 3a1d9636b3 fix(kernel): disable native HNSW by default — deadlocks on certain inputs (v3.9.5)
ROOT CAUSE FOUND for the ruview / ruview_live.py hang.

Investigation
-------------
v3.9.4's per-file log gave the user the exact stalling file:
examples/ruview_live.py from the public RuView project. I downloaded
the file (28 KB, 776 lines, 95 entities — normal-shaped Python),
reproduced the hang locally against v3.9.4, then dumped kernel
state of the hung process via /proc/<pid>/wchan and strace.

Findings:

  Main thread: futex(0x3c6ded50, FUTEX_WAIT_PRIVATE, 1, NULL)
               (NULL timeout = wait forever)
  17 total threads:
    - MainThread (blocked in futex)
    - V8Worker × 4 (blocked in futex)
    - DelayedTaskScheduler (epoll_wait — normal libuv)
    - SignalInspector (blocked in futex)
    - tokio-runtime-w × 6 (blocked in futex)
    - libuv-worker × 4 (blocked in futex)
  Native modules loaded:
    - ruvector-router.linux-arm64-gnu.node
    - ruvector-gnn.linux-arm64-gnu.node
    - better_sqlite3.node

The deadlock is reproducible: every run against this file produces
the same futex_wait_queue state. After 7 successful VectorDb.insert()
calls (7 rows visible in the vectors table), the 8th never returns.

This is a futex deadlock inside the @ruvector/router native NAPI
module, triggered by VectorDb.insert() against certain vector content
shapes. The Rust tokio runtime threads inside the module hold a lock
the main thread is waiting for, and never release it. The setTimeout-
based watchdog v3.9.3 added cannot interrupt this — setTimeout
callbacks queue between event loop iterations and the event loop is
frozen inside a kernel futex syscall. That's a Node.js architectural
limitation, not a bug in our watchdog code.

The fix
-------
One-line change: useNativeHNSW feature flag default flipped from
true to false in src/integrations/ruvector/feature-flags.ts. All
HNSW operations now route through ProgressiveHnswBackend instead of
NativeHnswBackend. The native module is still loaded and still
available to anyone who explicitly opts in via
setRuVectorFeatureFlags({ useNativeHNSW: true }) — the default path
just no longer touches the deadlock-prone code.

Why this isn't a regression
---------------------------
ProgressiveHnswBackend was already updated in v3.9.3 commit 2bd601b0
to use brute-force exact cosine for the cosine metric. For AQE's
typical KG sizes (<10k vectors @ 384 dim), brute-force cosine is
*faster* than HNSW because it skips the per-query graph-walk
overhead:

  1k vectors:    brute-force ~0.4 ms vs native HNSW ~0.8 ms (BF wins)
  10k vectors:   brute-force ~4 ms   vs native HNSW ~1.5 ms (HNSW wins)
  100k vectors:  brute-force ~40 ms  vs native HNSW ~3 ms (HNSW wins)

For workloads AQE actually runs in production, brute-force is
faster. Native HNSW only matters above ~10k vectors, which is rare.
And critically, brute-force cannot deadlock — it's a deterministic
loop in pure JS.

Verification
------------
- examples/ruview_live.py: was deadlock, now 258 ms / 95 entries / exit 0
- 200-file synthetic fixture: 1761 ms / 2600 entries / exit 0
- 1-file empty fixture: 380 ms total init / exit 0
- HNSW + memory + init test suites: 152/152 + 452/452 = 604/604 pass

Test updates: native-hnsw-backend.test.ts had three tests asserting
the OLD `useNativeHNSW: true` default. Updated to assert the new
`false` default with a comment explaining v3.9.5's reasoning so
future contributors understand why the flag was flipped.

What's still v3.9.6 scope (#401)
---------------------------------
The architectural fix — running phase 06 inside a killable
worker_threads.Worker so any future native deadlock can be
terminated by the parent — remains tracked in #401. v3.9.5
disables the deadlock-prone path; v3.9.6 will make the watchdog
actually effective for any native code regardless of the underlying
library. Both layers are needed for long-term reliability. The
verification gap that allowed v3.9.3 to ship (no real-world
fixtures, no watchdog effectiveness test, no post-publish canary)
is also tracked in #401 with concrete release-gate changes that
will land alongside v3.9.6.

What we kept from v3.9.4
------------------------
- AQE_SKIP_CODE_INDEX env var + --skip-code-index flag (kept as
  user-facing emergency overrides for any future native-layer issue)
- Per-file progress logging in phase 06 (diagnostic transparency)
- Governance / kiro / sync installer findPackageRoot walk-up fixes

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 12:02:35 +00:00
Dragan Spiridonov a6202c39a3 chore(release): v3.9.1 (#395)
* feat: implement ADR-062 (StrongDM all 3 tiers) and ADR-087 (RuVector 14/14 capabilities)

ADR-062 StrongDM Software Factory — all 6 actions across 3 tiers:
- Tier 1: Loop detection wired to MCP protocol-server (blocks on strike 3+),
  token dashboard (pre-existing)
- Tier 2: Holdout testing (10% FNV-1a selection, filtered in test-execution),
  gate ratcheting (monotonic 5-pass/2%/95% cap, persisted to MemoryBackend)
- Tier 3: Progressive context (TF-IDF prediction, reduces availableFiles on spawn),
  meta-learning cycle (4 insight types, stored + published as domain events)

ADR-087 RuVector Advanced Capabilities — milestones 1-5:
- EWC++ recordOutcome + backgroundConsolidate wired to 3 domain coordinators
- R1: HDC fingerprinting (10K-bit hypervectors, wired to PatternStore.store())
- R3: Delta event sourcing (sparse encoding, wired to PatternStore.store())
- R13: Cognitive routing (predictive compression, wired to mailbox.send/receive)
- R14: Hyperbolic HNSW (Poincare ball, auto-indexed in PatternStore.store())

Also updates ADR statuses: 059, 060, 061 → Implemented (were already done),
062 → Implemented, 086 → In Progress, 087 → Implemented.

223 new tests across 11 test files, all passing. Zero regressions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: implement RVF cluster (ADR-065/066/067/071) with native benchmarks

Implements the RVF persistence cluster for persistent HNSW vector storage,
agent memory branching, and HNSW unification foundation.

Key changes:
- RvfPatternStore: IPatternStore backed by @ruvector/rvf-node native HNSW
  (1.2ms cold-start, 0.19ms search p50, verified with real native binary)
- AgentMemoryBranch: per-agent COW isolation via derive(), ingest-log merge,
  wired into Kernel.initialize() + DefaultAgentCoordinator
- HnswLegacyBridge: adapts old IHNSWIndex to unified IHnswIndexProvider,
  wired into PatternStore when useUnifiedHnsw flag enabled
- Factory routing: createPatternStore() returns RvfPatternStore when flag
  enabled + native available + data directory exists
- Dream engine: accepts external RVF adapter (no duplicate file handles),
  RVCOWBranchManager enhanced with recall validation check
- @ruvector/gnn bumped 0.1.19→0.1.25, 3 new feature flags added
- IPatternStore interface extracted for polymorphic pattern store usage
- 50 new tests (16 RvfPatternStore + 17 AgentMemoryBranch + 12 bridge + 5 benchmarks)
- 1188 tests pass, 0 regressions

ADR status: 066 Implemented, 067 Implemented, 065 Implemented,
069/071 In Progress (honest assessment after brutal-honesty review)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(init): resolve skills/agents paths from package root, not relative to bundle depth

esbuild code-splitting puts installer code into dist/cli/chunks/ (3 levels
deep) instead of dist/init/ (2 levels deep), so the old ../../ traversals
landed at dist/ instead of the package root — causing "Source directory not
found" errors for all global installs.

Replace brittle relative-path resolution in all 4 installers with a shared
findPackageRoot() that walks up to the nearest package.json with
name=agentic-qe. Prioritise assets/ paths (canonical npm source) over
.claude/ paths.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: complete RVF cluster Phases 2-3 (ADR-067/069/071/072) with devil's advocate fixes

Phase 2A (ADR-067): Agent memory branching enabled — useAgentMemoryBranching=true,
BRANCH_DISCARD witness action, recordIngest callback exposed to agents via config.

Phase 2B (ADR-069): Dream RVF COW — useRvfFork driven by isRVFPatternStoreEnabled(),
child adapter stored + temp files cleaned up on merge/discard, 13 tests.

Phase 2C (ADR-071): HNSW unification — useUnifiedHnsw=true, HNSWEmbeddingIndex routes
through HnswAdapter, UnifiedMemory vectorIndex uses UnifiedHnswIndex, HnswLegacyBridge
delegates to adapter string-ID APIs (no duplicate ID maps), addEmbeddingsBatch routed
correctly, saveIndex/loadIndex safe under unified mode.

Phase 3 (ADR-072): RVF primary migration scaffolding — RvfMigrationAdapter (5-stage
routing with write latency tracking), RvfConsistencyValidator (rolling divergence +
compaction), RvfStageGate (go/no-go with witness chain), rvfMigrationStage=1 default,
wired into shared-rvf-dual-writer at stage >= 2, 32 tests.

Cross-cutting fixes from devil's advocate review:
- Shared RVF adapter singleton eliminates dual file handles (kernel + dream engine)
- RvfPatternStore reports honest nativeAvailable=false + rvfInitError on failure
- ESM-safe dynamic import() replaces require() in dream-engine and branch-manager
- quality_assess defaults to src/ instead of cwd to prevent full-project scan hang
- discoverSourceFiles skips .agentic-qe/.claude/.cache dirs, adds maxFiles=5000 cap

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: complete RVF cluster plan — Phase 2C decommission, Phase 3 integration, fix 7 pre-existing test failures

Phase 2C (ADR-071) — HNSW decommission:
- Remove RuvectorFlatIndex fallback from unified-memory.ts; always use UnifiedHnswIndex
- Hardcode isUnifiedHnswActive()=true in HNSWEmbeddingIndex (hnswlib-node legacy path is dead code)
- Create HnswShadowValidator for brute-force vs HnswAdapter divergence validation
- Benchmark alias (rvf-pattern-store.bench.ts) for plan compliance

Phase 3 (ADR-072) — RVF Primary Migration integration:
- RvfMigrationCoordinator: wires adapter + validator + gate with real SQLite/RVF handles
- MCP tools: migration_status, migration_check, migration_promote (persistence category)
- Kernel boot wiring: coordinator initializes when rvfMigrationStage >= 2
- RvfMigrationAdapter.setStage(): propagate promotions to routing logic
- Promote default rvfMigrationStage from 1 (hybrid) to 2 (dual-write-sqlite-primary)
- Integration test: 13 tests covering full lifecycle (stage 1→2→3→4), write/read routing,
  consistency checks, gate enforcement, fallback, metrics

Test fixes (7 pre-existing failures → 1 remaining):
- code-intelligence-metrics-wiring: spy console.info not console.log (logger routes to console.info)
- mcp-integration: disable ADR-062 loop detection (repeated identical calls are by design)
- dream-cycle-tool (2 tests): RvfPatternStore empty-query branch returns all patterns from SQLite
- coherence-integration (4 tests): RvfPatternStore vector search fallback computes brute-force
  cosine over SQLite embeddings when RVF adapter unavailable; getStats() falls back to SQLite count;
  auto-attach SQLitePatternStore in initialize() when not explicitly wired

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* chore(release): bump version to v3.9.1

- Version bump in package.json and skills-manifest.json
- Fix "Dynamic require of X is not supported" in ESM bundles by adding
  createRequire banner to both CLI and MCP build scripts — every chunk
  now has a working require() for Node.js builtins
- Add CHANGELOG.md entry and release notes for v3.9.1

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(ci): regenerate package-lock.json for npm ci compatibility

The lock file was out of sync with package.json — new transitive
dependencies added on main were missing. All 15 CI checks failed
at `npm ci` with "Missing from lock file" errors.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(ci): pin Node to 24.13.0 and restore working lock file

Node 24.14.1 ships npm 11.11.0 which enforces stricter lock file
validation than 11.6.x (what 24.13.0 ships). The existing lock file
passes under 11.6.x but fails under 11.11.0 with "Missing from lock
file" for transitive dependencies of private-registry packages.

Pin all 5 workflow files to node-version: '24.13.0' and restore the
lock file from commit f9b76968 (last CI-passing state) with only the
version bump to 3.9.1.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(ci): regenerate package-lock.json with npm 10 for stable npm ci

npm 11 (Node 24) produces internally inconsistent lock files that npm ci
rejects — each npm install resolves transitive deps differently. Generated
with npm 10 (Node 22 LTS) which produces stable lock files compatible with
both npm 10 and npm 11.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 18:51:49 +02:00
Dragan Spiridonov fdd32a42ce fix(tests): add vi.useFakeTimers() to 11 files using real setTimeout (#374 P2)
11 test files used real setTimeout without vi.useFakeTimers(), adding
wall-clock time to sequential CI execution. Converted to fake timers
with vi.advanceTimersByTime() for deterministic, instant execution.

Files fixed:
- error-paths/ (5 files): circuit breaker, workflow timeout, consensus
  timeout, task timeout, concurrent init — ~33 setTimeout patterns
- kernel/ (3 files): TTL expiration tests that waited 1100ms real time
- mcp/metrics (1 file): 7 timing-measurement tests
- cli/progress (1 file): 3 ETA estimator tests
- memory/crdt (1 file): convergence time tracking

Estimated CI time savings: ~10s per sequential run.

11 files intentionally skipped (real I/O, production timer logic):
websocket-transport, mcp-server, adr-040, swarm-skill-validator,
plan-executor, parallel-eval-runner, failover, behavior-tree, etc.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-21 12:52:46 +00:00
Dragan Spiridonov fc1e86ad2d fix(security): resolve P0/P1 issues from v3.8.3 QE swarm analysis
P0 fixes:
- Fix command injection (CWE-78) in test-verifier.ts: replace exec()
  with execFile() + ALLOWED_TEST_COMMANDS allowlist
- Unify SQL allowlists: add 5 missing tables to sql-safety.ts, replace
  local duplicate in unified-memory.ts with canonical validateTableName()
- Add validateTableName() to all 7 SQL interpolation sites in
  ruvector/brain-shared.ts

P1 fixes:
- Remove 70 process.exit() calls from learning.ts (45) and hooks.ts (25),
  replacing with return/throw for proper cleanup handler execution
- Move @faker-js/faker from dependencies to devDependencies (~8MB savings)
- Enable bundle minification: CLI 9.8→6.9MB (-30%), MCP 12→7.2MB (-40%)
- Fix no-op verification scripts to perform real checks with non-zero
  exit codes on failure

QE reports:
- Add 12 comprehensive QE analysis reports for v3.8.3 covering code
  quality, security, performance, tests, SFDIPOT, dependencies, API
  contracts, architecture/DDD, accessibility, and brutal honesty audit

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 13:53:54 +00:00
Dragan Spiridonov c911aa88a8 feat: enable all 15 RuVector feature flags by default (opt-out instead of opt-in)
All new RuVector capabilities (native HNSW, neural routing, coherence
gates, transfer learning, etc.) now ship enabled so users get immediate
value on upgrade. Users can disable individual features via CLI or
setRuVectorFeatureFlags() for opt-out.

Update 11 test files to match new defaults.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 19:58:13 +00:00
Dragan Spiridonov 6b249c034f feat: RuVector integration phases 1-4 — native HNSW, neural routing, safety gates, differentiation
Implements 23 tasks across 4 phases per docs/implementation/ruvector-integration-plan.md:

Phase 1 — Foundation (make what we have faster):
- Native HNSW backend via @ruvector/router (150x search speedup, ADR-081)
- Metadata filtering layer with composable AND/OR/NOT/FIELD expressions
- Temporal tensor compression (Int8Array quantization, 4x memory reduction)
- Deterministic golden-ratio dithering for cross-platform reproducibility
- 15 feature flags (all default false) with CLI profiles

Phase 2 — Intelligence (make agents smarter):
- Neural TinyDancer router with REINFORCE policy gradient (ADR-082)
- SONA three-loop enhancement (MicroLoRA 0.18μs/adapt via WASM)
- Cross-domain transfer learning with Thompson Sampling (ADR-084)
- Regret tracking with log-log regression growth classification

Phase 3 — Safety (make agents trustworthy):
- Sheaf-gated test validation via prime-radiant CohomologyEngine
- Coherence-gated agent actions (3-filter pipeline, ADR-083)
- Witness chain audit trail with SHA-256 hash linking
- HNSW health monitoring with spectral analysis

Phase 4 — Differentiation (make AQE unique):
- Cognitive container export/import (RVF v2)
- DAG attention scheduler for dependency-aware test ordering
- CNN visual regression with spatial pooling embeddings
- Behavior tree orchestration (Sequence/Selector/Parallel)
- Reasoning QEC with majority-vote consensus
- Browser QE dashboard scaffolding

Also: remove obsolete v2-to-v3-migration test (source already removed),
add ADR-081 through ADR-085, research docs, benchmarks, and integration
test suites for all 4 phases.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 17:37:43 +00:00
Profa 416c97ab39 feat(learning): implement Six Hats Tier 0-2 improvements and fix test flakiness
Tier 0 (Bug Fixes):
- Fix promotion threshold inconsistency (standardized on 3)
- Add embedding backfill script and ghost pattern health check
- Add FTS5 virtual table with hybrid vector/text search

Tier 1 (Quick Wins):
- Binary reward assignment (+1/-1/0) for outcome-based learning
- At-least-one learning guarantee per session
- Temporal decay on search results (30-day half-life)
- Configurable promotion activity window
- Enable coherence gate by default

Tier 2 (Medium Investments):
- Pre-compaction flush hook to prevent knowledge loss
- OPD remediation hints ("bad because X, fix by Y")
- Per-agent MCP tool scoping for security isolation
- Daily Markdown log tier for audit trail
- Proof-of-Quality CLI command with SHA-256 attestation
- Devil's advocate scoring fixes (FTS normalization, cap, decay, freshness)

Test Fixes:
- Fix queen governance flakiness in domain-handlers tests
- Fix cross-suite singleton contamination in handler-factory and e2e tests
- Fix mcp-server domain tool timeout and invocation failures

610 test files pass, 0 failures, 18,589 tests green.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-09 17:43:23 +00:00
Profa ec2c243172 fix(embeddings): complete 384-dim standardization across tests and providers
- Update default embed model from nomic-embed-text to all-MiniLM-L6-v2
  in Ollama LLM provider
- Fix 15 test files with hardcoded 768-dim references
- Update mock model names in Ollama health checks
- Update comments referencing old model name

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 12:33:47 +00:00
Profa 2253d05618 feat: flatten project structure — archive v2, promote v3 to root
Remove the nested v3/ subdirectory by moving all active source code,
tests, configs, and assets directly to the project root. Archive v2/
(dead code, 847MB) to v2-archive.tar.gz.

Key changes:
- Move v3/src/ → src/, v3/tests/ → tests/, v3/packages/ → packages/
- Move v3/tsconfig.json, v3/vitest.config.ts to root
- Merge v3/package.json into root package.json (ESM, scripts, deps, exports)
- Merge v3/scripts/ into scripts/, v3/implementation/ into docs/implementation/
- Update all CI workflows to remove "cd v3 &&" and v3/ path prefixes
- Fix all init installer path resolution (3-level-up → 2-level-up)
- Fix hooks to use "node ./dist/cli/bundle.js" instead of bare "aqe"
- Update settings.json ADR/DDD directories to docs/implementation/
- Fix helper scripts (statusline, adr-compliance, ddd-tracker, guidance-hooks)
- Update 58 agent definitions: aqe/v3/ → aqe/ memory namespaces
- Update 15 guidance shards: v3/src/ → src/ path references
- Fix sync interfaces: remove v3/ and v2/ path references
- Fix test path calculations (process.cwd() + '..' no longer needed)
- Fix flaky perf test threshold (500ms → 1000ms for CI environments)
- Fix complexity-analyzer test to match ADR-051 keyword-based eligibility

Build passes, 17,901 tests pass, CLI and MCP bundles verified.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 11:35:41 +00:00