From 10adb5ca67fa387a99d65ba610c800a81eb748d0 Mon Sep 17 00:00:00 2001 From: garrytan Date: Mon, 2 Nov 2026 16:18:08 +0000 Subject: [PATCH 1/2] test: an exported live-brain URL must not reach a temp-HOME gbrain child (#3466) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- test/state-root-config-merge.test.ts | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/state-root-config-merge.test.ts b/test/state-root-config-merge.test.ts index a0e5cd73e..d6a1a25e5 100644 --- a/test/state-root-config-merge.test.ts +++ b/test/state-root-config-merge.test.ts @@ -163,4 +163,42 @@ describe('test preload hermeticity', () => { expect(r.stderr).toContain('test-setup: stripped inherited GSTACK_STATE_DIR; tests isolate state via GSTACK_HOME'); expect(r.stderr.match(/test-setup: stripped/g)?.length).toBe(2); }); + + test('bun test strips an inherited live-brain URL, so a temp-HOME gbrain child never sees it (#3466)', () => { + const live = path.join(tmp, 'live-gbrain'); + fs.mkdirSync(live, { recursive: true }); + fs.writeFileSync(path.join(live, 'config.json'), JSON.stringify({ database_url: 'postgresql://live-config.invalid/brain' })); + const childHome = path.join(tmp, 'child-home'); + fs.mkdirSync(childHome, { recursive: true }); + const probe = path.join(tmp, 'brain-probe.test.ts'); + fs.writeFileSync(probe, [ + "import { test, expect } from 'bun:test';", + "import { spawnSync } from 'child_process';", + "test('probe', () => {", + " const script = \"const { buildGbrainEnv } = require(\" + JSON.stringify(" + JSON.stringify(path.join(ROOT, 'lib', 'gbrain-exec.ts')) + ") + \"); const e = buildGbrainEnv(); console.log(['GBRAIN_DATABASE_URL','GBRAIN_DIRECT_DATABASE_URL','DATABASE_URL','GBRAIN_HOME'].map(k => k + '=' + (e[k] ?? '')).join(' '));\";", + " const r = spawnSync(process.execPath, ['--no-env-file', '--config=/dev/null', '-e', script], {", + " env: { ...process.env, HOME: " + JSON.stringify(childHome) + " }, encoding: 'utf-8', timeout: 30_000,", + " });", + " expect(r.stdout.trim()).toBe('GBRAIN_DATABASE_URL= GBRAIN_DIRECT_DATABASE_URL= DATABASE_URL= GBRAIN_HOME=');", + "});", + ].join('\n')); + const r = spawnSync('bun', ['test', '--preload', path.join(ROOT, 'test-setup.ts'), probe], { + cwd: tmp, + env: { + ...env, GSTACK_HOME: resolved, + GBRAIN_DATABASE_URL: 'postgresql://live-brain.invalid/brain', + GBRAIN_DIRECT_DATABASE_URL: 'postgresql://live-brain.invalid/direct', + DATABASE_URL: 'postgresql://live-brain.invalid/fallback', + GBRAIN_HOME: live, + }, + encoding: 'utf-8', + timeout: 60_000, + }); + expect(r.stderr).not.toContain('live-brain.invalid'); + expect(r.stderr).not.toContain('live-config.invalid'); + expect(r.status).toBe(0); + for (const name of ['GBRAIN_DATABASE_URL', 'GBRAIN_DIRECT_DATABASE_URL', 'DATABASE_URL', 'GBRAIN_HOME']) { + expect(r.stderr).toContain(`test-setup: stripped inherited ${name}; tests must not reach a live gbrain`); + } + }); }); From 7de336fb413f1ada3a961b766001e580f6629763 Mon Sep 17 00:00:00 2001 From: garrytan Date: Mon, 2 Nov 2026 16:18:08 +0000 Subject: [PATCH 2/2] fix(test): strip the variables that point gbrain at a live brain in the test preload (#3466) Co-authored-by: AndreIntelas <126392195+AndreIntelas@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- changelog.d/gsta209-gbrain-env-isolation.md | 7 +++++++ test-setup.ts | 9 +++++++++ 2 files changed, 16 insertions(+) create mode 100644 changelog.d/gsta209-gbrain-env-isolation.md diff --git a/changelog.d/gsta209-gbrain-env-isolation.md b/changelog.d/gsta209-gbrain-env-isolation.md new file mode 100644 index 000000000..512340e77 --- /dev/null +++ b/changelog.d/gsta209-gbrain-env-isolation.md @@ -0,0 +1,7 @@ +**gstack's own tests no longer write into a developer's real gbrain.** If you export `GBRAIN_DATABASE_URL` for your working brain and run gstack's test suite, tests that spawn bins with a temp `HOME` used to pass that URL straight through. With no gbrain config under the temp `HOME`, the real `gbrain` on your PATH then added `brain-sync-remote-*` sources and test pages to your live brain. From #3466 by @AndreIntelas. + +### Fixed +- **The test preload (`test-setup.ts`) strips the variables gbrain uses to pick a brain** before any test module loads: `GBRAIN_DATABASE_URL`, `GBRAIN_DIRECT_DATABASE_URL`, `DATABASE_URL` and `GBRAIN_HOME`. It prints one line on stderr for each variable it strips. This covers `bun test ` and every shard of `bun run test`. + +### Tests +- `test/state-root-config-merge.test.ts`: a `bun test` run with all four variables exported spawns a child under a temp `HOME` that calls `buildGbrainEnv`, and the child sees none of them. It fails on main. diff --git a/test-setup.ts b/test-setup.ts index 31234cc63..a9c6fc020 100644 --- a/test-setup.ts +++ b/test-setup.ts @@ -39,6 +39,15 @@ for (const name of ['GSTACK_STATE_ROOT', 'GSTACK_STATE_DIR']) { // an unattended Capy session it never set up. The shard engine strips the same // list for the lanes; this reaches direct runs too. for (const name of CAPY_LAUNCH_MARKERS) delete process.env[name]; +// The variables gbrain reads to pick a brain (#3466). Tests spawn bins with +// { ...process.env, HOME: }; with no gbrain config under the temp HOME, +// buildGbrainEnv passes an exported URL through and a real gbrain on PATH +// writes test pages and sources into the developer's live brain. +for (const name of ['GBRAIN_DATABASE_URL', 'GBRAIN_DIRECT_DATABASE_URL', 'DATABASE_URL', 'GBRAIN_HOME']) { + if (process.env[name] === undefined) continue; + delete process.env[name]; + process.stderr.write(`test-setup: stripped inherited ${name}; tests must not reach a live gbrain\n`); +} process.env.GSTACK_TEST_LEGACY_ROOT = path.join(os.tmpdir(), `gstack-test-legacy-root-${process.pid}-${Date.now()}-unused`); // Workflow leases (bin/gstack-lease) default to the installation's git dir; // for the dev checkout under test that is this repo's .git, and a live test