Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
7.0 KB
Raw Blame
/**
 * v0.32.3 — doctor search_mode + eval_drift check tests.
 * Pins [CDX-20]: status stays 'ok', no health-score docking; hint lives
 * in `message`. Tests the two exported helpers directly to avoid the
 * expensive full runDoctor walk.
 */
import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
import { execSync } from 'child_process';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { checkSearchMode, checkEvalDrift } from '../src/commands/doctor.ts';
import {
  NEW_INSTALL_DEFAULT_RERANKER_MODEL,
} from '../src/core/ai/defaults.ts';

let engine: PGLiteEngine;

beforeAll(async () => {
  engine = new PGLiteEngine();
  await engine.connect({});
  await engine.initSchema();
});

afterAll(async () => {
  await engine.disconnect();
});

beforeEach(async () => {
  await engine.executeRaw(`DELETE FROM config WHERE key LIKE 'search.%'`);
});

describe('checkSearchMode [CDX-20]', () => {
  test('unset mode → ok with pick-a-mode hint (balanced fallback reranks with the live voyage default)', async () => {
    const c = await checkSearchMode(engine);
    expect(c.name).toBe('search_mode');
    // [CDX-20] original never-warn contract holds again: the balanced
    expect(c.status).toBe('ok');
    expect(c.message).toMatch(/unset/i);
    expect(c.message).toContain('gbrain search modes');
  });

  test('mode set, no overrides → "canonical" message, ok (balanced reranks with the live default)', async () => {
    await engine.setConfig('search.mode', 'balanced');
    const c = await checkSearchMode(engine);
    expect(c.status).toBe('ok');
    expect(c.message).toContain('balanced');
    expect(c.message).toContain('canonical');
  });

  test('mode set + overrides → ok with reset hint + override list', async () => {
    await engine.setConfig('search.mode', 'conservative');
    await engine.setConfig('search.cache.enabled', 'false');
    await engine.setConfig('search.tokenBudget', '8000');
    const c = await checkSearchMode(engine);
    expect(c.status).toBe('ok'); // [CDX-20]: conservative reranks nothing → still ok
    expect(c.message).toContain('conservative');
    expect(c.message).toContain('search.cache.enabled');
    expect(c.message).toContain('search.tokenBudget');
    expect(c.message).toContain('gbrain search modes --reset');
  });

  test('upgrade-notice state key is excluded from override count', async () => {
    await engine.setConfig('search.mode', 'balanced');
    await engine.setConfig('search.mode_upgrade_notice_shown', 'true');
    const c = await checkSearchMode(engine);
    expect(c.message).toContain('no per-key overrides');
  });

  test('tokenmax mode is recognized; no override roster in message', async () => {
    await engine.setConfig('search.mode', 'tokenmax');
    const c = await checkSearchMode(engine);
    // tokenmax bundle reranks with the live voyage default → ok.
    expect(c.status).toBe('ok');
    expect(c.message).toContain('tokenmax');
    expect(c.message).toContain('canonical');
  });
});

describe('checkSearchMode reset advice', () => {
  test('explicit voyage overrides equal to the live bundle default → plain --reset advice', async () => {
    // The old #4382 repro: tokenmax + explicit voyage reranker overrides. A
    // reset now restores the SAME live model, so consolidation is safe again.
    await engine.setConfig('search.mode', 'tokenmax');
    await engine.setConfig('search.reranker.enabled', 'true');
    await engine.setConfig('search.reranker.model', NEW_INSTALL_DEFAULT_RERANKER_MODEL);
    const c = await checkSearchMode(engine);
    expect(c.status).toBe('ok');
    expect(c.message).toContain('gbrain search modes --reset');
    expect(c.message).not.toContain('load-bearing');
    // Override roster stays visible.
    expect(c.message).toContain('search.reranker.model');
  });

  test('bundle default, no overrides (balanced) → ok, no reset advice needed', async () => {
    await engine.setConfig('search.mode', 'balanced');
    const c = await checkSearchMode(engine);
    expect(c.status).toBe('ok');
    expect(c.message).not.toContain('--reset');
  });

  test('reranker disabled by override → plain --reset advice (a reset re-arms the LIVE default, not a dying one)', async () => {
    await engine.setConfig('search.mode', 'balanced');
    await engine.setConfig('search.reranker.enabled', 'false');
    const c = await checkSearchMode(engine);
    expect(c.status).toBe('ok');
    expect(c.message).toContain('gbrain search modes --reset');
  });

  test('an explicit search.reranker.model equal to the bundle default is called redundant with a precise unset, never --reset', async () => {
    // Every v0.46.3–v0.47.10 Voyage install carries this row from init.
    await engine.setConfig('search.mode', 'balanced');
    await engine.setConfig('search.reranker.model', NEW_INSTALL_DEFAULT_RERANKER_MODEL);
    const c = await checkSearchMode(engine);
    expect(c.status).toBe('ok');
    expect(c.message).toContain('redundant');
    expect(c.message).toContain('gbrain config unset search.reranker.model');
    expect(c.message).not.toContain('--reset');
  });

  test('conservative mode + overrides keeps the --reset recommendation (conservative bundle reranks nothing)', async () => {
    await engine.setConfig('search.mode', 'conservative');
    await engine.setConfig('search.cache.enabled', 'false');
    const c = await checkSearchMode(engine);
    expect(c.status).toBe('ok');
    expect(c.message).toContain('gbrain search modes --reset');
  });
});

describe('checkEvalDrift [CDX-6]', () => {
  test('returns ok status (never warn — per [CDX-20])', async () => {
    const c = await checkEvalDrift(engine);
    expect(c.name).toBe('eval_drift');
    expect(c.status).toBe('ok');
  });

  test('message is non-empty (either no-drift or drift summary)', async () => {
    const c = await checkEvalDrift(engine);
    expect(c.message).toBeTruthy();
    expect(c.message.length).toBeGreaterThan(0);
  });

  test("probes gbrain's own checkout, not whatever repo the operator stood in (#4606)", async () => {
    // Unrelated scratch repo with a staged file that happens to match the
    // retrieval watch list. Doctor must not report it as gbrain drift.
    const tmp = mkdtempSync(join(tmpdir(), 'gb-drift-cwd-'));
    const git = (args: string) =>
      execSync(`git -c [email protected] -c user.name=t -c commit.gpgsign=false ${args}`, {
        cwd: tmp,
        stdio: 'pipe',
      });
    git('init -q');
    git('commit -q --allow-empty -m init');
    mkdirSync(join(tmp, 'src/core/search'), { recursive: true });
    writeFileSync(join(tmp, 'src/core/search/zz-unrelated-repo.ts'), 'export {};\n');
    git('add -A');
    const prev = process.cwd();
    process.chdir(tmp);
    try {
      const c = await checkEvalDrift(engine);
      expect(c.status).toBe('ok');
      expect(c.message).not.toContain('zz-unrelated-repo.ts');
    } finally {
      process.chdir(prev);
      rmSync(tmp, { recursive: true, force: true });
    }
  });
});