mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-30 19:49:14 +00:00
Master landed significant work since this branch was cut (v0.15.x → v0.16.x →
v0.17.0 gbrain dream + runCycle → v0.18.0 multi-source brains → v0.18.1 RLS
hardening). Bumped this branch's version from the claimed 0.18.0 to 0.19.0
because master already owns 0.18.x.
Conflicts resolved:
- VERSION: 0.19.0 (was 0.18.0 on HEAD vs 0.18.1 on master)
- package.json: 0.19.0, kept all 11 eval-facing exports, merged master's
typescript devDep + postinstall script + test script (typecheck added)
- src/core/types.ts: union of both PageType additions. Master had added
`meeting | note`; this branch added `email | slack | calendar-event`
for inbox/chat/calendar ingest. Final enum carries all five.
- CHANGELOG.md: renumbered the BrainBench-extraction entry to 0.19.0 and
placed it above master's 0.18.1 RLS entry. Tweaked copy ("In v0.17 it
lived inside this repo" → "Previously it lived inside this repo") to
stop implying a specific version that never shipped.
- CLAUDE.md: adjusted "BrainBench in a sibling repo" heading from
(v0.18+) → (v0.19+).
- docs/benchmarks/2026-04-18-minions-vs-openclaw-production.md:
resolved modify-vs-delete conflict in favor of delete (the extraction).
- scripts/llms-config.ts: dropped the docs/benchmarks/ entry (directory
no longer exists here; lives in gbrain-evals).
- llms.txt / llms-full.txt: regenerated after the config change.
- bun.lock: accepted master's (master already dropped pdf-parse as a
drive-by; aligned with our removal).
Tests: 2094 pass, 236 skip, 18 fail. Spot-checked failures — build-llms,
dream, orphans tests all pass in isolation. Failures reproduce only under
full-suite parallel load and are pre-existing master flakiness (matches the
graph-quality flake noted in the earlier summary). Not merge-introduced.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
191 lines
7.3 KiB
TypeScript
191 lines
7.3 KiB
TypeScript
/**
|
|
* v0.18.0 Step 6 — source resolution priority tests.
|
|
*
|
|
* Priority order (highest first):
|
|
* 1. Explicit --source flag
|
|
* 2. GBRAIN_SOURCE env var
|
|
* 3. .gbrain-source dotfile walk-up
|
|
* 4. Registered source whose local_path contains CWD (longest prefix wins)
|
|
* 5. Brain-level `sources.default` config key
|
|
* 6. Fallback: literal 'default'
|
|
*/
|
|
|
|
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
|
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs';
|
|
import { join } from 'path';
|
|
import { tmpdir } from 'os';
|
|
import { resolveSourceId, __testing } from '../src/core/source-resolver.ts';
|
|
import type { BrainEngine } from '../src/core/engine.ts';
|
|
|
|
// ── Stub engine ────────────────────────────────────────────
|
|
|
|
function makeStub(registeredSources: string[], paths: Array<{ id: string; local_path: string }>, defaultKey: string | null): BrainEngine {
|
|
return {
|
|
kind: 'pglite',
|
|
executeRaw: async <T>(sql: string, params?: unknown[]): Promise<T[]> => {
|
|
if (sql.includes('SELECT id FROM sources WHERE id = $1')) {
|
|
const target = params?.[0];
|
|
return (registeredSources.includes(target as string)
|
|
? [{ id: target } as unknown as T]
|
|
: []);
|
|
}
|
|
if (sql.includes('SELECT id, local_path FROM sources')) {
|
|
return paths as unknown as T[];
|
|
}
|
|
return [];
|
|
},
|
|
getConfig: async (key: string) => (key === 'sources.default' ? defaultKey : null),
|
|
} as unknown as BrainEngine;
|
|
}
|
|
|
|
// ── Priority 1: explicit flag ──────────────────────────────
|
|
|
|
describe('resolveSourceId priority 1 — explicit flag', () => {
|
|
test('wins over every other signal', async () => {
|
|
const engine = makeStub(['default', 'gstack', 'wiki'], [{ id: 'wiki', local_path: '/tmp' }], 'gstack');
|
|
process.env.GBRAIN_SOURCE = 'wiki';
|
|
try {
|
|
const id = await resolveSourceId(engine, 'gstack', '/tmp/whatever');
|
|
expect(id).toBe('gstack');
|
|
} finally {
|
|
delete process.env.GBRAIN_SOURCE;
|
|
}
|
|
});
|
|
|
|
test('rejects unregistered explicit source with actionable error', async () => {
|
|
const engine = makeStub(['default'], [], null);
|
|
await expect(resolveSourceId(engine, 'ghost')).rejects.toThrow(/not found/);
|
|
});
|
|
|
|
test('rejects invalid format', async () => {
|
|
const engine = makeStub(['default'], [], null);
|
|
await expect(resolveSourceId(engine, 'WRONG-case!')).rejects.toThrow(/Invalid --source/);
|
|
});
|
|
});
|
|
|
|
// ── Priority 2: env var ────────────────────────────────────
|
|
|
|
describe('resolveSourceId priority 2 — GBRAIN_SOURCE env', () => {
|
|
test('wins over dotfile / registered-path / default', async () => {
|
|
const engine = makeStub(['default', 'env-wins'], [{ id: 'other', local_path: '/tmp' }], 'default');
|
|
process.env.GBRAIN_SOURCE = 'env-wins';
|
|
try {
|
|
const id = await resolveSourceId(engine, null, '/tmp/x');
|
|
expect(id).toBe('env-wins');
|
|
} finally {
|
|
delete process.env.GBRAIN_SOURCE;
|
|
}
|
|
});
|
|
});
|
|
|
|
// ── Priority 3: dotfile walk-up ────────────────────────────
|
|
|
|
describe('resolveSourceId priority 3 — .gbrain-source dotfile walk-up', () => {
|
|
let tmpdirPath: string;
|
|
|
|
beforeEach(() => {
|
|
tmpdirPath = mkdtempSync(join(tmpdir(), 'gbrain-resolver-test-'));
|
|
});
|
|
afterEach(() => {
|
|
rmSync(tmpdirPath, { recursive: true, force: true });
|
|
});
|
|
|
|
test('finds dotfile in CWD', async () => {
|
|
writeFileSync(join(tmpdirPath, '.gbrain-source'), 'gstack\n');
|
|
const engine = makeStub(['default', 'gstack'], [], null);
|
|
const id = await resolveSourceId(engine, null, tmpdirPath);
|
|
expect(id).toBe('gstack');
|
|
});
|
|
|
|
test('walks up ancestors to find dotfile', async () => {
|
|
writeFileSync(join(tmpdirPath, '.gbrain-source'), 'wiki\n');
|
|
const deep = join(tmpdirPath, 'a', 'b', 'c');
|
|
mkdirSync(deep, { recursive: true });
|
|
const engine = makeStub(['default', 'wiki'], [], null);
|
|
const id = await resolveSourceId(engine, null, deep);
|
|
expect(id).toBe('wiki');
|
|
});
|
|
|
|
test('ignores dotfile with invalid content', async () => {
|
|
writeFileSync(join(tmpdirPath, '.gbrain-source'), 'INVALID!\n');
|
|
const engine = makeStub(['default'], [], null);
|
|
const id = await resolveSourceId(engine, null, tmpdirPath);
|
|
expect(id).toBe('default');
|
|
});
|
|
});
|
|
|
|
// ── Priority 4: registered local_path match (longest prefix) ──
|
|
|
|
describe('resolveSourceId priority 4 — registered local_path longest-prefix match', () => {
|
|
test('picks registered source whose local_path contains CWD', async () => {
|
|
const engine = makeStub(
|
|
['default', 'gstack'],
|
|
[{ id: 'gstack', local_path: '/tmp/gstack' }],
|
|
null,
|
|
);
|
|
const id = await resolveSourceId(engine, null, '/tmp/gstack/plans/foo');
|
|
expect(id).toBe('gstack');
|
|
});
|
|
|
|
test('longest prefix wins when paths are nested (per Codex second pass)', async () => {
|
|
// Codex flagged: overlapping paths need longest-prefix resolution.
|
|
// If gstack at /tmp/gstack and plans at /tmp/gstack/plans both
|
|
// exist, CWD inside plans/ must pick plans.
|
|
const engine = makeStub(
|
|
['default', 'gstack', 'plans'],
|
|
[
|
|
{ id: 'gstack', local_path: '/tmp/gstack' },
|
|
{ id: 'plans', local_path: '/tmp/gstack/plans' },
|
|
],
|
|
null,
|
|
);
|
|
const id = await resolveSourceId(engine, null, '/tmp/gstack/plans/deeper');
|
|
expect(id).toBe('plans');
|
|
});
|
|
|
|
test("CWD outside any registered path falls through to default", async () => {
|
|
const engine = makeStub(
|
|
['default', 'gstack'],
|
|
[{ id: 'gstack', local_path: '/tmp/gstack' }],
|
|
null,
|
|
);
|
|
const id = await resolveSourceId(engine, null, '/some/other/dir');
|
|
expect(id).toBe('default');
|
|
});
|
|
});
|
|
|
|
// ── Priority 5: brain-level default ────────────────────────
|
|
|
|
describe('resolveSourceId priority 5 — sources.default config key', () => {
|
|
test("returns configured default when no higher signal present", async () => {
|
|
const engine = makeStub(['default', 'custom'], [], 'custom');
|
|
const id = await resolveSourceId(engine, null, '/some/random/dir');
|
|
expect(id).toBe('custom');
|
|
});
|
|
});
|
|
|
|
// ── Priority 6: fallback ────────────────────────────────────
|
|
|
|
describe('resolveSourceId priority 6 — fallback', () => {
|
|
test("returns 'default' when no signal at all", async () => {
|
|
const engine = makeStub(['default'], [], null);
|
|
const id = await resolveSourceId(engine, null, '/random/dir');
|
|
expect(id).toBe('default');
|
|
});
|
|
});
|
|
|
|
// ── Regex validation ───────────────────────────────────────
|
|
|
|
describe('SOURCE_ID_RE', () => {
|
|
test('accepts valid ids', () => {
|
|
for (const id of ['default', 'wiki', 'gstack', 'yc-media', 'garrys-list', 'a', '123']) {
|
|
expect(__testing.SOURCE_ID_RE.test(id)).toBe(true);
|
|
}
|
|
});
|
|
test('rejects invalid ids', () => {
|
|
for (const id of ['', 'a'.repeat(33), 'Upper', 'has_underscore', 'trailing-', '-leading', 'with spaces', 'with.dots']) {
|
|
expect(__testing.SOURCE_ID_RE.test(id)).toBe(false);
|
|
}
|
|
});
|
|
});
|