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.7 KB
Raw Blame
/**
 * ADR-323 — typed memory provenance. Regression guard for the
 * `--provenance` / `--provenance-filter` wiring added to `memory store` /
 * `memory search`, plus the crash discovered while implementing it: an
 * earlier version of the provenance-filtered search path skipped the
 * RaBitQ/HNSW acceleration branches entirely, which crashed the CLI process
 * on exit (a libuv assertion on Windows) even though it printed correct
 * results first. The fix keeps those branches unconditional and applies the
 * provenance filter as a lookup within/after them instead of skipping them.
 *
 * Real end-to-end guard: drives the built CLI in a temp cwd exactly like a
 * user would, because the crash only reproduced through the actual CLI
 * process (not under the vitest/module-import transform).
 */

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { execFileSync } from 'node:child_process';
import { mkdtempSync, rmSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

const HERE = dirname(fileURLToPath(import.meta.url));
const CLI = join(HERE, '..', 'bin', 'cli.js');
const CLI_BUILT = existsSync(CLI);

function run(args: string[], cwd: string): { stdout: string; exit: number | null } {
  try {
    const stdout = execFileSync('node', [CLI, ...args], {
      cwd,
      encoding: 'utf-8',
      stdio: ['ignore', 'pipe', 'pipe'],
      timeout: 60_000,
    });
    return { stdout, exit: 0 };
  } catch (err) {
    const e = err as { status?: number | null; stdout?: Buffer; stderr?: Buffer };
    return {
      stdout: (e.stdout?.toString() ?? '') + (e.stderr?.toString() ?? ''),
      exit: e.status ?? null,
    };
  }
}

function searchJson(query: string, cwd: string, extraArgs: string[] = []) {
  const { stdout } = run(['memory', 'search', '-q', query, '-n', 'adr323', '--threshold', '0.05', '--format', 'json', ...extraArgs], cwd);
  const start = stdout.indexOf('{');
  expect(start).toBeGreaterThanOrEqual(0);
  return JSON.parse(stdout.slice(start)) as { results: Array<{ key: string; provenanceType?: string }> };
}

describe.skipIf(!CLI_BUILT)('ADR-323 typed memory provenance', () => {
  let workdir: string;

  beforeAll(() => {
    workdir = mkdtempSync(join(tmpdir(), 'ruflo-adr323-'));
    run(['memory', 'init'], workdir);
    run(['memory', 'store', '-k', 'user/goal', '-n', 'adr323', '--value', 'ship the deploy by Friday', '--provenance', 'user_claim'], workdir);
    run(['memory', 'store', '-k', 'agent/plan', '-n', 'adr323', '--value', 'deploy plan generated by planner agent', '--provenance', 'agent_output'], workdir);
    run(['memory', 'store', '-k', 'tool/deploy', '-n', 'adr323', '--value', 'deploy status reported by tool', '--provenance', 'tool_result'], workdir);
    run(['memory', 'store', '-k', 'misc/note', '-n', 'adr323', '--value', 'deploy notes without explicit provenance'], workdir);
  }, 60_000);

  afterAll(() => {
    try { rmSync(workdir, { recursive: true, force: true }); } catch { /* ignore */ }
  });

  it('store rejects an invalid --provenance value with a non-zero exit', () => {
    const { stdout, exit } = run(
      ['memory', 'store', '-k', 'bad/entry', '-n', 'adr323', '--value', 'x', '--provenance', 'not_a_real_type'],
      workdir,
    );
    expect(exit).not.toBe(0);
    expect(stdout.toLowerCase()).toMatch(/provenance/);
  });

  it('an entry stored without --provenance defaults to "unknown"', () => {
    const r = searchJson('notes', workdir);
    const entry = r.results.find(x => x.key === 'misc/note');
    expect(entry).toBeDefined();
    expect(entry!.provenanceType).toBe('unknown');
  });

  it('search with no --provenance-filter returns entries of every provenance type', () => {
    const r = searchJson('deploy', workdir);
    const keys = r.results.map(x => x.key);
    expect(keys).toContain('user/goal');
    expect(keys).toContain('agent/plan');
    expect(keys).toContain('misc/note');
  });

  it('persists explicit provenance through the native bridge and HNSW refresh', () => {
    const r = searchJson('deploy', workdir);
    expect(r.results.find(x => x.key === 'user/goal')?.provenanceType).toBe('user_claim');
    expect(r.results.find(x => x.key === 'agent/plan')?.provenanceType).toBe('agent_output');
    expect(r.results.find(x => x.key === 'tool/deploy')?.provenanceType).toBe('tool_result');
  });

  it('--provenance-filter user_claim returns only the user-claim entry', () => {
    const r = searchJson('deploy', workdir, ['--provenance-filter', 'user_claim']);
    const keys = r.results.map(x => x.key);
    expect(keys).toContain('user/goal');
    expect(keys).not.toContain('agent/plan');
    expect(keys).not.toContain('misc/note');
  });

  it('--provenance-filter accepts a comma-separated list (OR semantics)', () => {
    const r = searchJson('deploy', workdir, ['--provenance-filter', 'agent_output,tool_result']);
    const keys = r.results.map(x => x.key);
    expect(keys).toContain('agent/plan');
    expect(keys).not.toContain('user/goal');
    expect(keys).not.toContain('misc/note');
  });

  it('enforces provenance filtering in keyword and hybrid modes', () => {
    for (const searchType of ['keyword', 'hybrid']) {
      const r = searchJson('deploy', workdir, ['--type', searchType, '--provenance-filter', 'tool_result']);
      const keys = r.results.map(x => x.key);
      expect(keys).toContain('tool/deploy');
      expect(keys).not.toContain('user/goal');
      expect(keys).not.toContain('agent/plan');
      expect(keys).not.toContain('misc/note');
    }
  });

  it('enforces provenance filtering inside SmartRetrieval query expansion', () => {
    const r = searchJson('deploy', workdir, ['--smart', '--provenance-filter', 'user_claim']);
    const keys = r.results.map(x => x.key);
    expect(keys).toContain('user/goal');
    expect(keys).not.toContain('agent/plan');
    expect(keys).not.toContain('tool/deploy');
    expect(keys).not.toContain('misc/note');
  });

  it('SmartRetrieval returns every matching entry instead of fusing them into one (#2271)', () => {
    // The fixture entries were stored seconds apart, so their 12-char search ids collide.
    const r = searchJson('deploy', workdir, ['--smart']);
    const keys = r.results.map(x => x.key);
    expect(keys).toEqual(expect.arrayContaining(['user/goal', 'agent/plan', 'tool/deploy', 'misc/note']));
  });

  it('preserves an existing provenance type when an upsert omits the flag', () => {
    const updated = run(
      ['memory', 'store', '-k', 'agent/plan', '-n', 'adr323', '--value', 'updated deploy plan'],
      workdir,
    );
    expect(updated.exit).toBe(0);
    const r = searchJson('updated deploy', workdir, ['--provenance-filter', 'agent_output']);
    const entry = r.results.find(x => x.key === 'agent/plan');
    expect(entry).toBeDefined();
    expect(entry?.provenanceType).toBe('agent_output');
  });

  it('search rejects an invalid --provenance-filter value with a non-zero exit', () => {
    const { stdout, exit } = run(
      ['memory', 'search', '-q', 'deploy', '-n', 'adr323', '--provenance-filter', 'not_a_type', '--format', 'json'],
      workdir,
    );
    expect(exit).not.toBe(0);
    expect(stdout.toLowerCase()).toMatch(/provenance/);
  });

  // Regression for the crash found during implementation: a
  // provenance-filtered search must exit cleanly (code 0), not just print
  // correct JSON and then crash on process teardown.
  it('a provenance-filtered search exits cleanly, run repeatedly', () => {
    for (let i = 0; i < 3; i++) {
      const { exit } = run(
        ['memory', 'search', '-q', 'deploy', '-n', 'adr323', '--threshold', '0.05', '--provenance-filter', 'user_claim', '--format', 'json'],
        workdir,
      );
      expect(exit).toBe(0);
    }
  });
});