The gbrain source wireup and skill start follow GBRAIN_HOME, so the wireup writes to the right brain (GST-2621) (#4343)

* Find gbrain's config under GBRAIN_HOME in the source wireup and skill start (#4325)

Both helpers read only ~/.gbrain/config.json. With GBRAIN_HOME naming one
brain and ~/.gbrain holding another, the wireup locked the ~/.gbrain brain's
database_url and sent sources add and sync there, and skill start skipped
its /sync-gbrain hint. Both now resolve the path through one shared helper,
bin/gstack-gbrain-home.sh, using gbrain's configDir() rule. The wireup gains
a read-only --check-target mode that tells an operator whether an earlier
run could have written to the wrong brain, without printing any URL.

Co-authored-by: exGeni <135786749+exGeni@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Use localhost test database URLs the credential scan accepts (#4325)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Test the shared gbrain config path helper directly (#4325)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Report the caller's database URL settings in the wireup target check (#4325)

--check-target printed the URL env after the startup lock had exported GBRAIN_DATABASE_URL, so it said a URL was set when the caller had none. The helper comment no longer calls gbrainConfigDir() an exact twin.

Review-Finding: p1/specialist-batched#1 reader=red-team
Review-Finding: p1/specialist-batched#2 reader=testing
Review-Finding: p1/specialist-batched#3 reader=testing
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Document the GBRAIN_HOME config path and the wireup target check (#4325)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: exGeni <135786749+exGeni@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-13 17:29:07 +00:00
1 parent 1d3c63c53d
commit 43c815be88
7 files changed
+336 -20

No files matched your search

+183
View File
@@ -606,3 +606,186 @@ describe('gstack-gbrain-source-wireup without jq (B8)', () => {
});
});
// #4325: the lock and every database call must follow GBRAIN_HOME the way
// gbrain's configDir() does. Two homes hold different database_urls; the
// GBRAIN_HOME one is the brain gbrain itself would use.
describe('gstack-gbrain-source-wireup — GBRAIN_HOME (#4325)', () => {
const DEFAULT_URL = 'postgresql://postgres:postgres@localhost:5432/default_brain';
const NAMED_URL = 'postgresql://postgres:postgres@localhost:5433/named_brain';
let gbrainHome: string;
function writeConfig(dir: string, config: Record<string, unknown>) {
fs.mkdirSync(path.join(dir, '.gbrain'), { recursive: true });
fs.writeFileSync(path.join(dir, '.gbrain', 'config.json'), JSON.stringify(config));
}
function databaseCalls(): string[] {
return gbrainCalls().filter((c) => c.includes('sources add') || /^gbrain sync --source/.test(c));
}
beforeEach(() => {
gbrainHome = path.join(tmpHome, 'named-home');
writeConfig(tmpHome, { engine: 'postgres', database_url: DEFAULT_URL });
writeConfig(gbrainHome, { engine: 'postgres', database_url: NAMED_URL });
setupGstackRepo('git@github.com:user/gstack-brain-user.git');
makeFakeGbrain({});
});
test('GBRAIN_HOME set: sources add and sync carry the GBRAIN_HOME brain, never the default one', () => {
const r = run([], { env: { GBRAIN_DATABASE_URL: '', DATABASE_URL: '', GBRAIN_HOME: gbrainHome } });
expect(r.status).toBe(0);
const calls = databaseCalls();
expect(calls.length).toBe(2);
for (const c of calls) {
expect(c).toContain(`[GBRAIN_DATABASE_URL=${NAMED_URL}]`);
expect(c).not.toContain(DEFAULT_URL);
}
});
test('negative control: GBRAIN_HOME unset still locks the default brain', () => {
const r = run([], { env: { GBRAIN_DATABASE_URL: '', DATABASE_URL: '' } });
expect(r.status).toBe(0);
const calls = databaseCalls();
expect(calls.length).toBe(2);
for (const c of calls) expect(c).toContain(`[GBRAIN_DATABASE_URL=${DEFAULT_URL}]`);
});
test('GBRAIN_HOME with surrounding whitespace resolves as gbrain trims it', () => {
const r = run([], {
env: { GSTACK_BRAIN_NO_SYNC: '1', GBRAIN_DATABASE_URL: '', DATABASE_URL: '', GBRAIN_HOME: ` ${gbrainHome} ` },
});
expect(r.status).toBe(0);
expect(databaseCalls()[0]).toContain(`[GBRAIN_DATABASE_URL=${NAMED_URL}]`);
});
test('GBRAIN_HOME config without a database_url exports nothing, so gbrain picks its own brain', () => {
writeConfig(gbrainHome, { engine: 'pglite' });
const r = run([], {
env: { GSTACK_BRAIN_NO_SYNC: '1', GBRAIN_DATABASE_URL: '', DATABASE_URL: '', GBRAIN_HOME: gbrainHome },
});
expect(r.status).toBe(0);
const calls = databaseCalls();
expect(calls.length).toBe(1);
expect(calls[0]).not.toContain('GBRAIN_DATABASE_URL=');
});
test('a relative GBRAIN_HOME (gbrain refuses it) never falls back to the default brain', () => {
const r = run([], {
env: { GSTACK_BRAIN_NO_SYNC: '1', GBRAIN_DATABASE_URL: '', DATABASE_URL: '', GBRAIN_HOME: 'relative/home' },
});
expect(r.stderr).toContain('GBRAIN_HOME');
for (const c of gbrainCalls()) expect(c).not.toContain(DEFAULT_URL);
});
test('the ZeroEntropy advisory reads the GBRAIN_HOME config', () => {
writeConfig(gbrainHome, { engine: 'postgres', database_url: NAMED_URL, embedding: { recipe: 'zeroentropyai' } });
const r = run([], {
env: { GSTACK_BRAIN_NO_SYNC: '1', GBRAIN_DATABASE_URL: '', DATABASE_URL: '', GBRAIN_HOME: gbrainHome },
});
expect(r.status).toBe(0);
expect(r.stderr).toContain('ZeroEntropy');
});
});
// #4325 audit: --check-target tells an operator whether an earlier wireup run
// with GBRAIN_HOME set could have written to the default brain. Read-only, and
// it never prints a database URL or any part of one.
describe('gstack-gbrain-source-wireup --check-target (#4325)', () => {
const DEFAULT_URL = 'postgresql://postgres:postgres@localhost:5432/default_brain';
const NAMED_URL = 'postgresql://postgres:postgres@localhost:5433/named_brain';
let gbrainHome: string;
function writeConfig(dir: string, config: Record<string, unknown>) {
fs.mkdirSync(path.join(dir, '.gbrain'), { recursive: true });
fs.writeFileSync(path.join(dir, '.gbrain', 'config.json'), JSON.stringify(config));
}
function check(env: Record<string, string>) {
const r = run(['--check-target'], { env: { GBRAIN_DATABASE_URL: '', DATABASE_URL: '', ...env } });
const out = r.stdout + r.stderr;
for (const secret of [DEFAULT_URL, NAMED_URL, 'postgres:postgres', 'localhost', ':5432', ':5433', 'default_brain', 'named_brain', 'postgresql://']) {
expect(out).not.toContain(secret);
}
expect(gbrainCalls()).toEqual([]);
return r;
}
beforeEach(() => {
gbrainHome = path.join(tmpHome, 'named-home');
writeConfig(tmpHome, { engine: 'postgres', database_url: DEFAULT_URL });
writeConfig(gbrainHome, { engine: 'postgres', database_url: NAMED_URL });
makeFakeGbrain({});
});
test('two brains with different URLs under GBRAIN_HOME → MISMATCH', () => {
const r = check({ GBRAIN_HOME: gbrainHome });
expect(r.status).toBe(0);
expect(r.stdout).toMatch(/^TARGET: MISMATCH /m);
expect(r.stdout).toMatch(/^URL_ENV: GBRAIN_DATABASE_URL unset, DATABASE_URL unset /m);
});
test('GBRAIN_HOME unset → MATCH', () => {
const r = check({});
expect(r.status).toBe(0);
expect(r.stdout).toMatch(/^TARGET: MATCH /m);
});
test('same URL in both configs → MATCH', () => {
writeConfig(gbrainHome, { engine: 'postgres', database_url: DEFAULT_URL });
expect(check({ GBRAIN_HOME: gbrainHome }).stdout).toMatch(/^TARGET: MATCH /m);
});
test('default config without a database_url → MATCH (the old rule locked nothing)', () => {
writeConfig(tmpHome, { engine: 'pglite' });
expect(check({ GBRAIN_HOME: gbrainHome }).stdout).toMatch(/^TARGET: MATCH /m);
});
test('GBRAIN_HOME has no config but the default brain has a URL → MISMATCH', () => {
fs.rmSync(path.join(gbrainHome, '.gbrain'), { recursive: true, force: true });
expect(check({ GBRAIN_HOME: gbrainHome }).stdout).toMatch(/^TARGET: MISMATCH /m);
});
test('writes nothing: configs unchanged, no gstack state, no worktree', () => {
const configs = [tmpHome, gbrainHome].map((d) => path.join(d, '.gbrain', 'config.json'));
const before = configs.map((p) => fs.readFileSync(p, 'utf-8'));
check({ GBRAIN_HOME: gbrainHome });
expect(configs.map((p) => fs.readFileSync(p, 'utf-8'))).toEqual(before);
for (const dir of [tmpHome, gbrainHome]) expect(fs.readdirSync(path.join(dir, '.gbrain'))).toEqual(['config.json']);
expect(fs.existsSync(gstackHome)).toBe(false);
expect(fs.existsSync(worktreeDir)).toBe(false);
});
});
// The shared helper itself (#4325): every gstack shell read of gbrain's config
// resolves through bin/gstack-gbrain-home.sh, which follows gbrain's configDir().
describe('bin/gstack-gbrain-home.sh (#4325)', () => {
const HELPER = path.join(ROOT, 'bin/gstack-gbrain-home.sh');
function configPath(gbrainHome?: string) {
const env: Record<string, string> = { PATH: HERMETIC_PATH, HOME: '/home/fixture' };
if (gbrainHome !== undefined) env.GBRAIN_HOME = gbrainHome;
const r = spawnSync('bash', ['-c', '. "$1" && gstack_gbrain_config_path', 'helper', HELPER], {
env,
encoding: 'utf8',
timeout: 10_000,
});
return { status: r.status, out: r.stdout };
}
test('unset or blank GBRAIN_HOME means ~/.gbrain', () => {
for (const home of [undefined, '', ' ']) {
expect(configPath(home)).toEqual({ status: 0, out: '/home/fixture/.gbrain/config.json\n' });
}
});
test('GBRAIN_HOME is trimmed and is the parent of .gbrain', () => {
expect(configPath(' /srv/brain/ ')).toEqual({ status: 0, out: '/srv/brain/.gbrain/config.json\n' });
});
test('a GBRAIN_HOME gbrain refuses prints nothing and fails, with no ~/.gbrain fallback', () => {
for (const home of ['relative/home', '/srv/../brain', '/srv/brain/..']) {
expect(configPath(home)).toEqual({ status: 1, out: '' });
}
});
});
+24
View File
@@ -637,6 +637,30 @@ describe('gstack-skill-start behavior', () => {
expect(out).toMatch(/^ARTIFACTS_SYNC: off$/m);
});
test('the /sync-gbrain hint follows GBRAIN_HOME the way gbrain resolves its config (#4325)', () => {
const bin = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-ss-gbrain-bin-'));
const named = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-ss-gbrain-home-'));
const empty = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-ss-gbrain-empty-'));
try {
fs.writeFileSync(path.join(bin, 'gbrain'), '#!/bin/sh\necho "gbrain 0.0.0"\n', { mode: 0o755 });
fs.mkdirSync(path.join(named, '.gbrain'));
fs.writeFileSync(path.join(named, '.gbrain', 'config.json'), '{}\n');
const env = { PATH: `${bin}${path.delimiter}${process.env.PATH}` };
expect(runStart([], env)).not.toContain('GBrain configured');
expect(runStart([], { ...env, GBRAIN_HOME: named })).toContain('GBrain configured');
fs.mkdirSync(path.join(tmpHome, '.gbrain'));
fs.writeFileSync(path.join(tmpHome, '.gbrain', 'config.json'), '{}\n');
try {
expect(runStart([], env)).toContain('GBrain configured');
expect(runStart([], { ...env, GBRAIN_HOME: empty })).not.toContain('GBrain configured');
} finally {
fs.rmSync(path.join(tmpHome, '.gbrain'), { recursive: true, force: true });
}
} finally {
for (const dir of [bin, named, empty]) fs.rmSync(dir, { recursive: true, force: true });
}
});
test('artifacts-sync consent is asked before any artifacts egress, and only in interactive sessions', () => {
const gh = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-ss-privacy-'));
const bin = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-ss-privacy-bin-'));