Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(test): runs ignore an inherited git repository and an AI agent's output mode

garrytanbe14ff248c81
diff --git a/bunfig.toml b/bunfig.toml
index 27933f8177..7452ee83f4 100644
--- a/bunfig.toml
+++ b/bunfig.toml
@@ -49,7 +49,9 @@ timeout = 60_000
# git-hermetic-preload: inject commit.gpgsign=false/tag.gpgsign=false via
# GIT_CONFIG_COUNT env config so a dev box's global signing config can't
# route test-repo commits through gpg-agent (which intermittently fails
-# under full-suite load). See test/helpers/git-hermetic-preload.ts.
+# under full-suite load). It first drops the repository-locating variables
+# gitChildEnv drops (#7451), so a run started from a git hook never writes
+# into the caller's repository. See test/helpers/git-hermetic-preload.ts.
#
# provider-keys-preload: strip ambient ANTHROPIC_API_KEY/OPENAI_API_KEY so
# key-aware model routing (resolveTierDefault) resolves identically to
diff --git a/scripts/lib/test-env.sh b/scripts/lib/test-env.sh
index 7ef8b81e0d..82b33c1dba 100644
--- a/scripts/lib/test-env.sh
+++ b/scripts/lib/test-env.sh
@@ -9,6 +9,12 @@
# (test/scripts/run-unit-parallel.test.ts symlinks a minimal PATH with no
# sysctl/nproc/vm_stat/timeout and no package.json).
+# #7448: Bun switches `bun test` to failure-only output when it detects an AI agent
+# (CLAUDECODE or REPL_ID set, or AGENT=1). A passing file then prints no header and
+# the runners' file census marks it incomplete. Bun reads AGENT first, and AGENT=0
+# keeps the full output whatever the caller's environment.
+export AGENT=0
+
# ──────────────────────────────────────────────────────────────────────────
# CPU detection: Apple Silicon perf cores → Mac total physical → nproc → 4.
# Returns a single positive integer.
diff --git a/test/git-context-preload-7451.test.ts b/test/git-context-preload-7451.test.ts
new file mode 100644
index 0000000000..0c8c49e019
--- /dev/null
+++ b/test/git-context-preload-7451.test.ts
@@ -0,0 +1,83 @@
+/**
+ * #7451: a test run started from a git hook inherits the hook's repository (GIT_DIR, GIT_WORK_TREE, ...). Test helpers
+ * spawn git with `{ ...process.env }`, so with GIT_DIR set a `git -C <temp repo> config` wrote into the caller's
+ * repository instead (seen: `core.bare=true`, `user.name t`). git-hermetic-preload drops every variable
+ * `gitChildEnv` drops, so a run never touches the repository of the process that launched it.
+ *
+ * Each case spawns a real `bun test` child (cwd = repo root, so this repo's bunfig preloads apply) on a generated
+ * probe that initializes a throwaway repo and writes its config the way the doctor tests do.
+ */
+import { afterAll, describe, expect, test } from 'bun:test';
+import { mkdtempSync, rmSync, writeFileSync } from 'fs';
+import { tmpdir } from 'os';
+import { join, resolve } from 'path';
+import { GIT_LOCAL_ENV_VARS } from '../src/core/git-env.ts';
+
+const REPO_ROOT = resolve(import.meta.dir, '..');
+const dirs: string[] = [];
+afterAll(() => { for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); });
+
+function git(args: string[], cwd: string): string {
+ const env: Record<string, string> = {};
+ for (const [key, value] of Object.entries(process.env)) if (value !== undefined && !(GIT_LOCAL_ENV_VARS as readonly string[]).includes(key)) env[key] = value;
+ const proc = Bun.spawnSync(['git', ...args], { cwd, env, stdout: 'pipe', stderr: 'pipe' });
+ return proc.stdout.toString().trim();
+}
+
+function outerRepo(): { root: string; outer: string; probe: string; inner: string } {
+ const root = mkdtempSync(join(tmpdir(), 'gbrain-git-context-'));
+ dirs.push(root);
+ const outer = join(root, 'outer');
+ const inner = join(root, 'inner');
+ git(['init', '-q', outer], root);
+ const probe = join(root, 'git-context-probe.test.ts');
+ writeFileSync(probe, `import { expect, test } from 'bun:test';
+test('a throwaway repo gets its own config', () => {
+ const run = (args: string[], cwd: string) => Bun.spawnSync(['git', ...args], { cwd, env: { ...process.env }, stdout: 'pipe', stderr: 'pipe' });
+ expect(run(['init', '-q', ${JSON.stringify(inner)}], ${JSON.stringify(root)}).exitCode).toBe(0);
+ expect(run(['-C', ${JSON.stringify(inner)}, 'config', 'user.name', 't'], ${JSON.stringify(root)}).exitCode).toBe(0);
+ expect(run(['-C', ${JSON.stringify(inner)}, 'config', 'user.email', 't@t'], ${JSON.stringify(root)}).exitCode).toBe(0);
+});
+`);
+ return { root, outer, probe, inner };
+}
+
+function runProbe(probe: string, extra: Record<string, string>): { exitCode: number; output: string } {
+ const proc = Bun.spawnSync(['bun', 'test', '--timeout=15000', probe], {
+ cwd: REPO_ROOT, env: { ...process.env, ...extra }, stdout: 'pipe', stderr: 'pipe', timeout: 30_000, killSignal: 'SIGKILL',
+ });
+ return { exitCode: proc.exitCode ?? -1, output: proc.stdout.toString() + proc.stderr.toString() };
+}
+
+describe('git-hermetic-preload: inherited git context (#7451)', () => {
+ test('a run with GIT_DIR and GIT_WORK_TREE set leaves the outer repository config untouched', () => {
+ const { outer, probe, inner } = outerRepo();
+ const before = git(['config', '--file', join(outer, '.git', 'config'), '--list'], outer);
+ const result = runProbe(probe, { GIT_DIR: join(outer, '.git'), GIT_WORK_TREE: outer, GIT_INDEX_FILE: join(outer, '.git', 'index') });
+ expect(result.output).toContain('1 pass');
+ expect(result.exitCode).toBe(0);
+ expect(git(['config', '--file', join(outer, '.git', 'config'), '--list'], outer)).toBe(before);
+ expect(git(['config', '--file', join(outer, '.git', 'config'), '--get', 'core.bare'], outer)).toBe('false');
+ expect(git(['config', '--file', join(inner, '.git', 'config'), '--get', 'user.name'], inner)).toBe('t');
+ }, 60_000);
+
+ test('the preload drops exactly what gitChildEnv drops and keeps the gpg signing override', () => {
+ const { probe } = outerRepo();
+ const check = probe.replace(/git-context-probe\.test\.ts$/, 'env-probe.test.ts');
+ writeFileSync(check, `import { expect, test } from 'bun:test';
+test('env', () => {
+ for (const key of ${JSON.stringify(GIT_LOCAL_ENV_VARS.filter(key => key !== 'GIT_CONFIG_COUNT'))}) expect(process.env[key]).toBeUndefined();
+ expect(process.env.GIT_CONFIG_VALUE_0).toBe('false');
+ expect(process.env.GIT_CONFIG_KEY_0).toBe('commit.gpgsign');
+ expect(process.env.GIT_CONFIG_KEY_1).toBe('tag.gpgsign');
+ expect(process.env.GIT_CONFIG_COUNT).toBe('2');
+ expect(process.env.GIT_SSH_COMMAND).toBe('ssh -o BatchMode=yes');
+});
+`);
+ const extra: Record<string, string> = { GIT_CONFIG_PARAMETERS: "'core.bare=true'", GIT_CONFIG_COUNT: '1', GIT_CONFIG_KEY_0: 'core.bare', GIT_CONFIG_VALUE_0: 'true', GIT_SSH_COMMAND: 'ssh -o BatchMode=yes' };
+ for (const key of GIT_LOCAL_ENV_VARS) if (!(key in extra)) extra[key] = '/nonexistent-git-context';
+ const result = runProbe(check, extra);
+ expect(result.output).toContain('1 pass');
+ expect(result.exitCode).toBe(0);
+ }, 60_000);
+});
diff --git a/test/helpers/git-hermetic-preload.ts b/test/helpers/git-hermetic-preload.ts
index 006f293b56..bbd6d76733 100644
--- a/test/helpers/git-hermetic-preload.ts
+++ b/test/helpers/git-hermetic-preload.ts
@@ -25,9 +25,19 @@
* entries, and skips entirely if signing keys are already injected — an
* operator or a future signing-behavior test can override deliberately.
*
+ * #7451: it first drops every variable production's `gitChildEnv` drops (GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE,
+ * GIT_CONFIG_* and the rest of `git rev-parse --local-env-vars`). A run started from a git hook inherits the hook's
+ * repository through them, and a helper's `git -C <temp repo> config ...` then wrote into the caller's repository
+ * (`core.bare=true`, `user.name t`). Transport and credential variables pass through, as in production.
+ *
* Imported by `bunfig.toml` via
* `preload = [..., "./test/helpers/git-hermetic-preload.ts"]`.
*/
+import { gitChildEnv } from '../../src/core/git-env.ts';
+
+const kept = gitChildEnv({}, process.env);
+for (const key of Object.keys(process.env)) if (!(key in kept)) delete process.env[key];
+
const base = Number(process.env.GIT_CONFIG_COUNT ?? '0') || 0;
const existingKeys: string[] = [];
for (let i = 0; i < base; i++) {
diff --git a/test/scripts/run-unit-parallel.test.ts b/test/scripts/run-unit-parallel.test.ts
index fc615d0f9e..296a4af52a 100644
--- a/test/scripts/run-unit-parallel.test.ts
+++ b/test/scripts/run-unit-parallel.test.ts
@@ -74,14 +74,14 @@ afterAll(() => {
if (TMPROOT) rmSync(TMPROOT, { recursive: true, force: true });
});
-function runWrapper(extraArgs: string[] = []): { code: number; stdout: string; stderr: string } {
+function runWrapper(extraArgs: string[] = [], env: Record<string, string> = {}): { code: number; stdout: string; stderr: string } {
const result = spawnSync(
'bash',
[join(TMPROOT, 'scripts', 'run-unit-parallel.sh'), '--shards', '2', ...extraArgs],
// Shard-mechanics tests pin explicit --shards behavior with tiny
// synthetic files; disable mem-adaptation so a RAM-limited runner (CI's
// ~7GB) can't collapse 2 shards -> 1 and break the shard 1/2 expectations.
- { cwd: TMPROOT, encoding: 'utf-8', env: { ...process.env, GBRAIN_TEST_NO_MEM_ADAPT: '1' } },
+ { cwd: TMPROOT, encoding: 'utf-8', env: { ...process.env, GBRAIN_TEST_NO_MEM_ADAPT: '1', ...env } },
);
return {
code: result.status ?? -1,
@@ -101,6 +101,12 @@ describe('run-unit-parallel.sh exit-code propagation (a)', () => {
try {
const r = runWrapper();
expect(r.code).toBe(0);
+ // #7448: an AI agent's environment switches Bun to failure-only output; the census still counts every passing file.
+ for (const agent of [{ CLAUDECODE: '1' }, { REPL_ID: 'repl-example' }, { AGENT: '1' }] as Record<string, string>[]) {
+ const quiet = runWrapper([], agent);
+ expect(quiet.stdout + quiet.stderr).not.toContain('incomplete');
+ expect(quiet.code).toBe(0);
+ }
} finally {
// Restore the failing fixture for any downstream tests in the same
// describe block (afterAll cleans the whole tempdir; this is belt-