Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
/**
* Production-wrapper regression coverage with semantic response caching disabled.
* Fresh reads must retain filtering, metadata and telemetry guarantees; legacy
* cache implementation behavior is covered by the direct cache-class suites.
* Serial because embedding and gateway configuration are process-global.
*/
import { afterAll, beforeAll, beforeEach, describe, expect, mock, setDefaultTimeout, test } from 'bun:test';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import * as realEmbedding from '../src/core/embedding.ts';
import type { HybridSearchMeta } from '../src/core/types.ts';
/** Deterministic 1536d unit vector so consults match writes at cosine 1.0. */
function fixedEmbedding(): Float32Array {
const arr = new Float32Array(1536);
for (let i = 0; i < 1536; i++) arr[i] = Math.sin(1 + i * 0.001);
let norm = 0;
for (let i = 0; i < 1536; i++) norm += arr[i] * arr[i];
norm = Math.sqrt(norm);
if (norm > 0) for (let i = 0; i < 1536; i++) arr[i] /= norm;
return arr;
}
mock.module('../src/core/embedding.ts', () => ({
...realEmbedding,
embed: async () => fixedEmbedding(),
embedQuery: async () => fixedEmbedding(),
}));
const { hybridSearchCached, awaitPendingSearchCacheWrites } =
await import('../src/core/search/hybrid.ts');
const { configureGateway, resetGateway } = await import('../src/core/ai/gateway.ts');
const { PGLiteEngine } = await import('../src/core/pglite-engine.ts');
const { importFromContent } = await import('../src/core/import-file.ts');
const { serializeMarkdown } = await import('../src/core/markdown.ts');
setDefaultTimeout(30_000);
let engine: InstanceType<typeof PGLiteEngine>;
let tmpHome: string;
const savedGbrainHome = process.env.GBRAIN_HOME;
beforeAll(async () => {
tmpHome = mkdtempSync(join(tmpdir(), 'gbrain-private-cache-fold-'));
process.env.GBRAIN_HOME = tmpHome;
resetGateway();
configureGateway({
embedding_model: 'openai:text-embedding-3-large',
embedding_dimensions: 1536,
env: { OPENAI_API_KEY: 'sk-fake' },
});
engine = new PGLiteEngine();
await engine.connect({});
await engine.initSchema();
const fixtures: Array<[string, string, Record<string, unknown>]> = [
['notes/world-widget', 'Widget World', { visibility: 'world' }],
['notes/private-widget', 'Widget Private', { visibility: 'private' }],
];
for (const [slug, title, frontmatter] of fixtures) {
const truth = `${title} is a builder.`;
const result = await importFromContent(engine, slug,
serializeMarkdown(frontmatter, truth, '', { type: 'concept', title, tags: [] }),
{ noEmbed: true, forceRechunk: true });
expect(result.status).toBe('imported');
}
});
afterAll(async () => {
if (savedGbrainHome === undefined) delete process.env.GBRAIN_HOME;
else process.env.GBRAIN_HOME = savedGbrainHome;
try { await engine.disconnect(); } catch { /* ignore */ }
resetGateway();
try { rmSync(tmpHome, { recursive: true, force: true }); } catch { /* ignore */ }
});
beforeEach(async () => {
await engine.executeRaw('DELETE FROM query_cache');
});
describe('private visibility applies on every fresh wrapper read', () => {
test('repeated remote-default reads stay uncached and never expose private pages', async () => {
let firstMeta: HybridSearchMeta | undefined;
const first = await hybridSearchCached(engine, 'builder', {
limit: 10,
excludePrivate: true,
onMeta: (m) => { firstMeta = m; },
});
expect(firstMeta?.cache?.status).toBe('disabled');
expect(first.map((r) => r.slug)).toEqual(['notes/world-widget']);
await awaitPendingSearchCacheWrites();
expect((await engine.executeRaw('SELECT id FROM query_cache')).length).toBe(0);
let repeatMeta: HybridSearchMeta | undefined;
const repeat = await hybridSearchCached(engine, 'builder', {
limit: 10,
excludePrivate: true,
onMeta: (m) => { repeatMeta = m; },
});
expect(repeatMeta?.cache?.status).toBe('disabled');
expect(repeat.map((r) => r.slug)).toEqual(['notes/world-widget']);
});
test('a private-excluding read stays filtered after a trusted read', async () => {
let trustedMeta: HybridSearchMeta | undefined;
const trusted = await hybridSearchCached(engine, 'builder', {
limit: 10,
onMeta: (m) => { trustedMeta = m; },
});
expect(trustedMeta?.cache?.status).toBe('disabled');
expect(trusted.map((r) => r.slug).sort()).toEqual(['notes/private-widget', 'notes/world-widget']);
await awaitPendingSearchCacheWrites();
expect((await engine.executeRaw('SELECT id FROM query_cache')).length).toBe(0);
let remoteMeta: HybridSearchMeta | undefined;
const remote = await hybridSearchCached(engine, 'builder', {
limit: 10,
excludePrivate: true,
onMeta: (m) => { remoteMeta = m; },
});
expect(remoteMeta?.cache?.status).toBe('disabled');
expect(remote.map((r) => r.slug)).toEqual(['notes/world-widget']);
await awaitPendingSearchCacheWrites();
expect((await engine.executeRaw('SELECT id FROM query_cache')).length).toBe(0);
});
test('a trusted read still includes private pages after a filtered read', async () => {
let remoteMeta: HybridSearchMeta | undefined;
await hybridSearchCached(engine, 'builder', {
limit: 10,
excludePrivate: true,
onMeta: (m) => { remoteMeta = m; },
});
expect(remoteMeta?.cache?.status).toBe('disabled');
await awaitPendingSearchCacheWrites();
expect((await engine.executeRaw('SELECT id FROM query_cache')).length).toBe(0);
let trustedMeta: HybridSearchMeta | undefined;
const trusted = await hybridSearchCached(engine, 'builder', {
limit: 10,
onMeta: (m) => { trustedMeta = m; },
});
expect(trustedMeta?.cache?.status).toBe('disabled');
expect(trusted.map((r) => r.slug).sort()).toEqual(['notes/private-widget', 'notes/world-widget']);
});
});