Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

7.4 KB
Raw Blame
/**
 * Dream Cycle 2026-08-27 (intelligence surface).
 *
 * `LocalSonaCoordinator.distillLearning()` (memory/intelligence.ts) gates every
 * pattern-confidence update behind an EWC "forgetting" penalty. Before this
 * fix, it called `EWCConsolidator.getPenalty(oldWeights, newWeights)` with
 * 1-element weight arrays (`[oldConfidence]`, `[proposedConfidence]`) and no
 * explicit `fisher` argument. `getPenalty()`'s length guard
 * (`Math.min(oldWeights.length, newWeights.length, fisherDiag.length)`)
 * collapses that call to reading only `globalFisher[0]` β€” one arbitrary
 * dimension out of 384 β€” regardless of the pattern's actual embedding. Two
 * purpose-built methods for exactly this scalar-confidence case already
 * existed in the same file (`computeConfidencePenalty`,
 * `updateFisherFromConfidences` β€” their own docstrings say "used by SONA
 * after distillLearning") but were never called from production. This test
 * regression-guards the wiring: distillLearning() must use the
 * full-diagonal-aware methods, not the 1-element-collapsing ones.
 *
 * CORRECTION (post-review, ruvnet, 2026-08-27): the original hypothesis in
 * the gist/issue overclaimed that this fix makes the penalty "a function of
 * each pattern's own embedding-derived importance." It does not.
 * `computeConfidencePenalty(oldConfidence, newConfidence)` takes no
 * pattern-specific embedding β€” it only reads `avg(globalFisher)`, a value
 * shared by every pattern at a given point in time. Two different patterns
 * evaluated under the *same* consolidator state with the *same* confidence
 * delta get an *identical* penalty, both before and after this fix. What
 * actually changed is which Fisher signal that shared penalty is built
 * from: `globalFisher[0]` alone (old, arbitrary single dimension) vs.
 * `avg(globalFisher)` (new, the full 384-dim signal `updateFisherFromConfidences`
 * already accumulates). See the second test below, which is the acceptance
 * test ruvnet's review specified: fixed state, equal deltas, different
 * embeddings β€” proving the penalty does NOT discriminate per pattern.
 * True per-pattern discrimination would require a different, larger design
 * change (e.g. passing each pattern's own embedding into a Fisher-weighted
 * per-dimension penalty) β€” out of scope for tonight's candidate.
 *
 * Runs in a temp cwd so ReasoningBank/EWC disk persistence never touches the
 * real repo tree.
 */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { mkdtempSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';

describe('#dream-2026-08-27 distillLearning EWC confidence gate', () => {
  let tmpRoot: string;
  let originalCwd: string;

  beforeEach(() => {
    originalCwd = process.cwd();
    tmpRoot = mkdtempSync(join(tmpdir(), 'ruflo-ewc-distill-'));
    process.chdir(tmpRoot);
  });

  afterEach(async () => {
    process.chdir(originalCwd);
    rmSync(tmpRoot, { recursive: true, force: true });
    vi.restoreAllMocks();
    vi.resetModules();
  });

  it('uses computeConfidencePenalty/updateFisherFromConfidences, never getPenalty([x],[y])/recordGradient', async () => {
    const intelligence = await import('../src/memory/intelligence.js');
    const ewcModule = await import('../src/memory/ewc-consolidation.js');

    ewcModule.resetEWCConsolidator();
    intelligence.clearIntelligence();

    const computeConfidencePenaltySpy = vi.spyOn(
      ewcModule.EWCConsolidator.prototype,
      'computeConfidencePenalty',
    );
    const updateFisherFromConfidencesSpy = vi.spyOn(
      ewcModule.EWCConsolidator.prototype,
      'updateFisherFromConfidences',
    );
    const getPenaltySpy = vi.spyOn(ewcModule.EWCConsolidator.prototype, 'getPenalty');
    const recordGradientSpy = vi.spyOn(ewcModule.EWCConsolidator.prototype, 'recordGradient');

    await intelligence.initializeIntelligence();

    const embedding = new Array(384).fill(0).map((_, i) => (i % 7 === 0 ? 0.9 : 0.05));

    // First trajectory: bank is empty, so distillLearning finds nothing similar
    // yet; this call only seeds a pattern (confidence 0.8) for the next call.
    await intelligence.recordTrajectory(
      [{ type: 'action', content: 'first run of a repeatable action', embedding }],
      'success',
    );

    // Second trajectory: an (almost) identical embedding is now similar to the
    // pattern stored by the first call, so distillLearning's confidence-update
    // gate actually executes this time.
    await intelligence.recordTrajectory(
      [{ type: 'action', content: 'second run of the same repeatable action', embedding }],
      'success',
    );

    expect(computeConfidencePenaltySpy).toHaveBeenCalled();
    expect(updateFisherFromConfidencesSpy).toHaveBeenCalled();
    // The old, defect-shaped call pattern must not reappear.
    expect(getPenaltySpy).not.toHaveBeenCalled();
    expect(recordGradientSpy).not.toHaveBeenCalled();
  });

  it('acceptance test (ruvnet review): under one fixed consolidator state, two patterns with the same confidence delta but different embeddings get the SAME penalty β€” this fix does not add per-pattern discrimination', async () => {
    const { EWCConsolidator } = await import('../src/memory/ewc-consolidation.js');
    const consolidator = new EWCConsolidator({
      lambda: 0.4,
      dimensions: 384,
      storagePath: join(tmpRoot, 'ewc-fisher.json'),
    });

    // Populate one shared, non-uniform Fisher state β€” a stand-in for "some
    // amount of prior distillation has already happened."
    const priorEmbedding = new Array(384).fill(0).map((_, i) => (i % 5 === 0 ? 0.8 : 0.02));
    consolidator.updateFisherFromConfidences([
      { id: 'prior', embedding: priorEmbedding, oldConf: 0.5, newConf: 0.6 },
    ]);

    // Pattern A would carry a high-magnitude embedding, Pattern B a
    // near-zero one β€” but computeConfidencePenalty(oldConf, newConf) has no
    // parameter to accept either one. That absence is exactly the finding:
    // both calls below are identical in every way computeConfidencePenalty
    // can observe, given the same confidence delta (0.5 -> 0.6) under the
    // same consolidator state.
    const penaltyA = consolidator.computeConfidencePenalty(0.5, 0.6);
    const penaltyB = consolidator.computeConfidencePenalty(0.5, 0.6);

    // Both calls hit the exact same code path with the exact same inputs β€”
    // computeConfidencePenalty has no embedding/pattern-id parameter to
    // differentiate them, so they MUST be identical. This is the reviewer's
    // required acceptance test, made explicit and permanent: it fails (goes
    // from a passing equality to a meaningless tautology) the moment anyone
    // adds real per-pattern discrimination here, which is the intended
    // trigger to update this test and the corrected claim above together.
    expect(penaltyA).toBe(penaltyB);
    expect(penaltyA).toBeGreaterThan(0);

    // The part of the original claim that IS true: the shared penalty uses
    // the full averaged Fisher diagonal, not an arbitrary single dimension.
    // Prove it by comparing against the old call shape on the SAME state.
    const oldShapePenalty = consolidator.getPenalty([0.5], [0.6]);
    // globalFisher[0] happens to be a "hot" dimension in priorEmbedding
    // (index 0 % 5 === 0), so the old call shape is not literally zero here,
    // but it is a different, non-representative slice of the same state.
    expect(oldShapePenalty).not.toBe(penaltyA);
  });
});