Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(git): sync-git, git-visible-files, census and drift-watch Git children use gitChildEnv; guard covers execSync (GBRA-75 approved)

time-attack79015541d553
diff --git a/src/core/eval/drift-watch.ts b/src/core/eval/drift-watch.ts
index 334af81e01..25d7d3ab4d 100644
--- a/src/core/eval/drift-watch.ts
+++ b/src/core/eval/drift-watch.ts
@@ -15,6 +15,7 @@ import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
+import { gitChildEnv } from '../git-env.ts';
/**
* Glob-ish patterns watched for retrieval drift. Each pattern is matched
@@ -101,6 +102,7 @@ export function filesDriftedSince(repoRoot: string, commitSha?: string, racyDiff
: ['diff', '--name-only', 'HEAD'];
const startedAt = Date.now();
const out = execSync(`git ${args.join(' ')}`, {
+ env: gitChildEnv(),
cwd: repoRoot,
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
@@ -108,7 +110,7 @@ export function filesDriftedSince(repoRoot: string, commitSha?: string, racyDiff
});
if (!commitSha && Date.now() - startedAt >= racyDiffMs) {
try {
- execSync('git update-index -q --refresh', { cwd: repoRoot, stdio: ['pipe', 'pipe', 'pipe'], timeout: 5000 });
+ execSync('git update-index -q --refresh', { env: gitChildEnv(), cwd: repoRoot, stdio: ['pipe', 'pipe', 'pipe'], timeout: 5000 });
} catch {
/* a held index.lock or read-only checkout keeps the index as it was */
}
diff --git a/src/core/fence-repair/census.ts b/src/core/fence-repair/census.ts
index 76d3ffb166..fa591c40c0 100644
--- a/src/core/fence-repair/census.ts
+++ b/src/core/fence-repair/census.ts
@@ -42,6 +42,7 @@ import {
type CandidateFinding, type CandidateLocation, type CandidateRecord, type FileFinding, type PageFinding, type ScanRecord,
} from './census-store.ts';
import type { FenceCtx, FencePage, FenceReason, FenceTier } from './types.ts';
+import { gitChildEnv } from '../git-env.ts';
type Exec = Pick<BrainEngine, 'executeRaw'>;
@@ -174,7 +175,7 @@ async function scanPages(engine: Exec, source: CensusSource, progress: ScanProgr
function git(root: string, args: string[]): string | null {
if (knownOutsideGitRepo(root)) return null;
try {
- return execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', maxBuffer: 512 * 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'] });
+ return execFileSync('git', ['-C', root, ...args], { env: gitChildEnv(), encoding: 'utf8', maxBuffer: 512 * 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'] });
} catch (error) {
noteGitFailure(root, error);
return null;
diff --git a/src/core/git-visible-files.ts b/src/core/git-visible-files.ts
index dfd2504246..f1270bce88 100644
--- a/src/core/git-visible-files.ts
+++ b/src/core/git-visible-files.ts
@@ -2,6 +2,7 @@ import { AsyncLocalStorage } from 'node:async_hooks';
import { execFileSync } from 'child_process';
import { lstatSync } from 'fs';
import { join, resolve } from 'path';
+import { gitChildEnv } from './git-env.ts';
interface GitScope {
listings: Map<string, string | null>;
@@ -54,6 +55,7 @@ export function gitLsFiles(dir: string, args: string[]): string | null {
if (!knownOutsideGitRepo(dir)) {
try {
stdout = execFileSync('git', ['-C', dir, 'ls-files', ...args], {
+ env: gitChildEnv(),
encoding: 'utf8', maxBuffer: 512 * 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'],
});
} catch (error) {
diff --git a/src/core/sync-git.ts b/src/core/sync-git.ts
index 68a8022131..dd996df656 100644
--- a/src/core/sync-git.ts
+++ b/src/core/sync-git.ts
@@ -11,6 +11,7 @@ import { resolveSlugForPath, DEFAULT_SOURCE_ID } from './sync.ts';
import { DELETE_BATCH_SIZE } from './engine-constants.ts';
import { invalidateGitListingCache, knownOutsideGitRepo, noteGitFailure } from './git-visible-files.ts';
import { hasUnresolvedDbOnlyDeclaration, loadStorageConfig } from './storage-config.ts';
+import { gitChildEnv } from './git-env.ts';
/**
* v0.32.7 CJK wave (codex post-merge F4): resolve a slug by `pages.source_path`
@@ -318,6 +319,7 @@ export function git(
): string {
noteGitCommand(args);
return execFileSync('git', buildGitInvocation(repoPath, args, configs), {
+ env: gitChildEnv(),
encoding: 'utf-8',
timeout: timeoutMs,
maxBuffer: 100 * 1024 * 1024,
@@ -335,6 +337,7 @@ export function git(
export function gitRawOutput(repoPath: string, args: string[]): string {
noteGitCommand(args);
return execFileSync('git', buildGitInvocation(repoPath, args, []), {
+ env: gitChildEnv(),
encoding: 'utf-8',
timeout: 30000,
maxBuffer: 100 * 1024 * 1024,
@@ -627,6 +630,7 @@ export function isPathSafe(filePath: string, gitRoot: string): boolean {
export function hasOriginRemote(repoPath: string): boolean {
try {
execFileSync('git', buildGitInvocation(repoPath, ['remote', 'get-url', 'origin']), {
+ env: gitChildEnv(),
encoding: 'utf-8',
timeout: 30000,
stdio: ['ignore', 'ignore', 'ignore'],
diff --git a/test/git-env-guard.test.ts b/test/git-env-guard.test.ts
index 222f7aee17..c7b01df1a9 100644
--- a/test/git-env-guard.test.ts
+++ b/test/git-env-guard.test.ts
@@ -3,8 +3,9 @@
* through a helper that drops Git's repository-locating variables (`gitChildEnv`,
* or the stricter `backupGitEnv` / `hardenedGitEnvironment`). A new spawn that
* inherits `process.env` (implicitly or with `...process.env`) fails here.
- * PENDING lists files owned by another lane; each is a named hunk request and
- * leaves the list once that lane adopts the helper.
+ * PENDING lists files owned by another lane that still need the helper (empty:
+ * GBRA-75 approved its hunk for sync-git.ts, git-visible-files.ts, fence-repair/census.ts
+ * and eval/drift-watch.ts in wave 13).
*/
import { expect, test } from 'bun:test';
import { readdirSync, readFileSync, statSync } from 'node:fs';
@@ -12,9 +13,9 @@ import { join, relative } from 'node:path';
const ROOT = join(import.meta.dir, '..');
const HELPERS = /\benv\s*:\s*(?:\{\s*\.\.\.)?(gitChildEnv|backupGitEnv|hardenedGitEnvironment)\(/;
-const SPAWN = /\b(spawnSync|spawn|execFileSync|execFileBounded|execFile)\(\s*['"]git['"]\s*,|\bBun\.spawn(Sync)?\(\s*\[\s*['"]git['"]/g;
-/** GBRA-75 (wave 10) owns these; the hunk request adopts gitChildEnv after wave 10 merges. */
-const PENDING = new Set(['src/core/sync-git.ts', 'src/core/git-visible-files.ts', 'src/core/fence-repair/census.ts']);
+const SPAWN = /\b(spawnSync|spawn|execFileSync|execFileBounded|execFile)\(\s*['"]git['"]\s*,|\bBun\.spawn(Sync)?\(\s*\[\s*['"]git['"]|\b(execSync|exec)\(\s*[`'"]git[\s`'"]/g;
+/** Files owned by another lane that still need the helper; each entry is a named hunk request. */
+const PENDING = new Set<string>([]);
function files(dir: string, out: string[] = []): string[] {
for (const name of readdirSync(dir)) {
@@ -41,6 +42,8 @@ function callText(source: string, open: number): string {
export function unscrubbedGitSpawns(path: string, source: string): string[] {
const found: string[] = [];
for (const match of source.matchAll(SPAWN)) {
+ const lineStart = source.lastIndexOf('\n', match.index!) + 1;
+ if (/^\s*(\*|\/\/)/.test(source.slice(lineStart, match.index))) continue;
const call = callText(source, source.indexOf('(', match.index!));
const named = /,\s*([A-Za-z_$][\w$]*)\s*\)$/.exec(call)?.[1];
const scrubbed = HELPERS.test(call) || (named !== undefined && new RegExp(`\\b${named}\\s*=\\s*\\{[^;]*${HELPERS.source}`).test(source));
@@ -65,6 +68,10 @@ test('the guard catches implicit and explicit process.env inheritance and accept
expect(unscrubbedGitSpawns('x.ts', "execFileSync('git', ['status'], { env: gitChildEnv({ A: ')' }) });")).toEqual([]);
expect(unscrubbedGitSpawns('x.ts', "const opts = { stdio: 'ignore', env: gitChildEnv() };\nexecFileSync('git', ['add'], opts);")).toEqual([]);
expect(unscrubbedGitSpawns('x.ts', "spawnSync('git', HARDENED, { env: hardenedGitEnvironment() });")).toEqual([]);
+ expect(unscrubbedGitSpawns('x.ts', "execSync('git update-index -q --refresh', { cwd: root });")).toEqual(['x.ts:1']);
+ expect(unscrubbedGitSpawns('x.ts', " * `execSync(`git -C ${path} ...`)` in a doc comment")).toEqual([]);
+ expect(unscrubbedGitSpawns('x.ts', "execSync(`git ${args.join(' ')}`, { cwd: root });")).toEqual(['x.ts:1']);
+ expect(unscrubbedGitSpawns('x.ts', "execSync(`git ${args.join(' ')}`, { env: gitChildEnv(), cwd: root });")).toEqual([]);
});
test('PENDING names only files that still need the hunk', () => {