Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): reason-trailer scan stays off the resident's path and its purge-epoch read runs once per sweep slice

garrytan9d26fdc6322f
diff --git a/src/commands/sync/full.ts b/src/commands/sync/full.ts
index 6159834ccd..34e98487ae 100644
--- a/src/commands/sync/full.ts
+++ b/src/commands/sync/full.ts
@@ -117,7 +117,7 @@ export async function performFullSync(
// GBRA-217: a re-import of a source that already has pages reads its reasons from the whole history (a first sync creates
// every page, so it archives no snapshot to annotate and runs no scan).
const [known] = await engine.executeRaw<{ any: boolean }>('SELECT EXISTS (SELECT 1 FROM pages WHERE source_id = $1) AS any', [opts.sourceId ?? 'default']);
- const reasons = await scanClassicReasons(engine, { sourceId: opts.sourceId, company, anchorPath, from: null, to: headCommit,
+ const reasons = await scanClassicReasons(engine, { sourceId: opts.sourceId, company, anchorPath, gitRoot: gitContextRoot, from: null, to: headCommit,
imports: known?.any === true ? 1 : 0 });
let result: import('../import.ts').RunImportResult;
try {
diff --git a/src/commands/sync/incremental.ts b/src/commands/sync/incremental.ts
index e057c49c9a..caaeaba0ce 100644
--- a/src/commands/sync/incremental.ts
+++ b/src/commands/sync/incremental.ts
@@ -38,7 +38,7 @@ async function runSyncPhases(engine: BrainEngine, opts: SyncOpts, holds: LegacyH
/** GBRA-217: the pass's one reason-trailer scan over `last_commit..pin`, when it imports or renames files. */
async function withPassReasons(engine: BrainEngine, plan: SyncPlan): Promise<SyncPlan> {
const { filtered } = plan;
- const reasons = await scanClassicReasons(engine, { sourceId: plan.opts.sourceId, company: plan.company, anchorPath: plan.anchorPath,
+ const reasons = await scanClassicReasons(engine, { sourceId: plan.opts.sourceId, company: plan.company, anchorPath: plan.anchorPath, gitRoot: plan.gitContextRoot,
from: plan.lastCommit, to: plan.pin, imports: filtered.added.length + filtered.modified.length + filtered.renamed.length });
return { ...plan, reasons };
}
diff --git a/src/commands/sync/reasons.ts b/src/commands/sync/reasons.ts
index 59f437bc3d..ab59cc373d 100644
--- a/src/commands/sync/reasons.ts
+++ b/src/commands/sync/reasons.ts
@@ -10,10 +10,10 @@ import type { SyncResult } from '../sync.ts';
export { importHistoryFor as fileHistory } from '../../core/reason-trailers.ts';
/** The pass's scan, or null when it imports nothing or the source is a company brain (its pinned revision is not annotated). */
-export async function scanClassicReasons(engine: BrainEngine, input: { sourceId: string | undefined; company: unknown; anchorPath: string;
+export async function scanClassicReasons(engine: BrainEngine, input: { sourceId: string | undefined; company: unknown; anchorPath: string; gitRoot: string;
from: string | null; to: string; imports: number }): Promise<SyncReasonScan | null> {
if (input.company || input.imports === 0) return null;
- return scanSyncReasons(engine, { sourceId: input.sourceId ?? 'default', sourceRoot: input.anchorPath, from: input.from, to: input.to });
+ return scanSyncReasons(engine, { sourceId: input.sourceId ?? 'default', sourceRoot: input.anchorPath, gitRoot: input.gitRoot, from: input.from, to: input.to });
}
/** The result with the pass's reason notices (a failed or capped scan, secrets in pushed trailers), when there are any. */
diff --git a/src/core/hardened-git.ts b/src/core/hardened-git.ts
index 9ae202ed8c..38881721d4 100644
--- a/src/core/hardened-git.ts
+++ b/src/core/hardened-git.ts
@@ -15,7 +15,8 @@
* (`assertHardenedGitArgs`, wave 12 W4.3), and `hardenedPathDirty` answers
* "is this file changed from HEAD" without `git status`.
*/
-import { spawn, spawnSync } from './spawn.ts';
+import { execFile, spawn, spawnSync } from './spawn.ts';
+import { promisify } from 'node:util';
import { lstatSync } from 'node:fs';
import { once } from 'node:events';
import type { Writable } from 'node:stream';
@@ -35,11 +36,30 @@ let operatorConfig: { key: string; at: number; values: string[] } | null = null;
* repository's config takes part. Re-read every 30 s, so a running server picks up an edit without a restart.
*/
function operatorConfigSafeDirectories(): string[] {
- const key = ['HOME', 'XDG_CONFIG_HOME', 'GIT_CONFIG_GLOBAL', 'GIT_CONFIG_SYSTEM', 'GIT_CONFIG_NOSYSTEM', 'PATH'].map(name => process.env[name] ?? '').join('\0');
- if (operatorConfig?.key === key && Date.now() - operatorConfig.at < OPERATOR_CONFIG_TTL_MS) return operatorConfig.values;
- const run = spawnSync('git', ['config', '--includes', '--show-scope', '-z', '--get-all', 'safe.directory'],
- { cwd: '/', env: { ...gitChildEnv(), LC_ALL: 'C' }, stdio: ['ignore', 'pipe', 'ignore'], timeout: 10_000, maxBuffer: 1024 * 1024, killSignal: 'SIGKILL' });
- const fields = run.status === 0 ? run.stdout.toString('utf8').split('\0') : [];
+ const key = operatorConfigKey();
+ const known = freshOperatorConfig(key);
+ if (known) return known;
+ const run = spawnSync('git', OPERATOR_CONFIG_ARGS, { ...operatorConfigOptions(), stdio: ['ignore', 'pipe', 'ignore'] });
+ return rememberOperatorConfig(key, run.status === 0 ? run.stdout : null);
+}
+
+/** GBRA-133: the same read without blocking, for `hardenedGitStream`, so an asynchronous Git read spawns no synchronous Git. */
+async function operatorConfigSafeDirectoriesAsync(): Promise<string[]> {
+ const key = operatorConfigKey();
+ const known = freshOperatorConfig(key);
+ if (known) return known;
+ const stdout = await promisify(execFile)('git', OPERATOR_CONFIG_ARGS, { ...operatorConfigOptions(), encoding: 'buffer' }).then(run => run.stdout, () => null);
+ return rememberOperatorConfig(key, stdout);
+}
+
+const OPERATOR_CONFIG_ARGS = ['config', '--includes', '--show-scope', '-z', '--get-all', 'safe.directory'];
+const operatorConfigOptions = () => ({ cwd: '/', env: { ...gitChildEnv(), LC_ALL: 'C' }, timeout: 10_000, maxBuffer: 1024 * 1024, killSignal: 'SIGKILL' as const });
+const operatorConfigKey = () => ['HOME', 'XDG_CONFIG_HOME', 'GIT_CONFIG_GLOBAL', 'GIT_CONFIG_SYSTEM', 'GIT_CONFIG_NOSYSTEM', 'PATH'].map(name => process.env[name] ?? '').join('\0');
+const freshOperatorConfig = (key: string) => operatorConfig?.key === key && Date.now() - operatorConfig.at < OPERATOR_CONFIG_TTL_MS ? operatorConfig.values : null;
+
+/** Git's `-z --show-scope` output (null when the read failed) as the system and global entries, cached under `key`. */
+function rememberOperatorConfig(key: string, stdout: Buffer | null): string[] {
+ const fields = stdout ? stdout.toString('utf8').split('\0') : [];
const values: string[] = [];
for (let i = 0; i + 1 < fields.length; i += 2) if (fields[i] === 'system' || fields[i] === 'global') values.push(fields[i + 1]!);
operatorConfig = { key, at: Date.now(), values };
@@ -54,10 +74,14 @@ export function __resetOperatorGitConfigForTests(): void { operatorConfig = null
* ownership check for a repository owned by another user, which the operator allowed for their own `git` too.
*/
export function hardenedGitEnvironment(): NodeJS.ProcessEnv {
+ return hardenedEnvironmentWith(operatorConfigSafeDirectories());
+}
+
+function hardenedEnvironmentWith(operatorSafeDirectories: string[]): NodeJS.ProcessEnv {
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_')));
return { ...env, GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: '/dev/null', GIT_OPTIONAL_LOCKS: '0',
GIT_NO_LAZY_FETCH: '1', GIT_NO_REPLACE_OBJECTS: '1', GIT_TERMINAL_PROMPT: '0', GIT_LFS_SKIP_SMUDGE: '1', LC_ALL: 'C',
- ...safeDirectoryEnv([...operatorConfigSafeDirectories(), ...envSafeDirectories()]) };
+ ...safeDirectoryEnv([...operatorSafeDirectories, ...envSafeDirectories()]) };
}
export type HardenedGitResult = { ok: true; stdout: Buffer } | { ok: false; reason: 'exit' | 'timeout' | 'too_large' | 'unavailable'; status?: number; stderr?: string };
@@ -147,7 +171,8 @@ export type HardenedGitInput = string | Buffer | Iterable<Buffer | string> | Asy
* `onChunk` or from a `stdin` iterable kills the process and rejects, so a
* caller never mistakes a run cut short for a finished one. An `onChunk` that
* returns a promise holds stdout until it settles (a large read yields to the
- * event loop between chunks); a rejection fails the run like a throw.
+ * event loop between chunks); a rejection fails the run like a throw. The operator's `safe.directory` read it needs runs
+ * asynchronously too (GBRA-133), so the whole read spawns no synchronous Git.
*/
export function hardenedGitStream(root: string, args: string[],
opts: { stdin?: HardenedGitInput; timeoutMs: number; maxBytes?: number; onChunk?: (chunk: Buffer) => void | Promise<void> }): Promise<HardenedGitStreamResult> {
@@ -158,9 +183,9 @@ export function hardenedGitStream(root: string, args: string[],
// reachable for seconds after the read had ended.
const { stdin, timeoutMs, maxBytes } = opts;
let onChunk = opts.onChunk;
- return new Promise<HardenedGitStreamResult>((accept, reject) => {
+ return operatorConfigSafeDirectoriesAsync().then(operator => new Promise<HardenedGitStreamResult>((accept, reject) => {
const child = spawn('git', [...HARDENED_GIT_ARGS, '-C', root, ...args],
- { env: hardenedGitEnvironment(), stdio: [stdin === undefined ? 'ignore' : 'pipe', 'pipe', 'ignore'] });
+ { env: hardenedEnvironmentWith(operator), stdio: [stdin === undefined ? 'ignore' : 'pipe', 'pipe', 'ignore'] });
let bytes = 0;
let last = -1;
let stopped: 'timeout' | 'too_large' | undefined;
@@ -196,7 +221,7 @@ export function hardenedGitStream(root: string, args: string[],
else accept({ ok: true, exitCode: 0, signal: null, bytes, endsWithNul: last === 0 });
}));
if (stdin !== undefined && child.stdin) void feedStdin(child.stdin, stdin, fail);
- });
+ }));
}
async function feedStdin(stdin: Writable, input: HardenedGitInput, fail: (error: unknown) => void): Promise<void> {
diff --git a/src/core/persistence/sync-convergence.ts b/src/core/persistence/sync-convergence.ts
index de30932e30..383098a502 100644
--- a/src/core/persistence/sync-convergence.ts
+++ b/src/core/persistence/sync-convergence.ts
@@ -33,6 +33,7 @@ import { importGroupReads, sliceReads } from './import-reads.ts';
import { PAGE_PURGE_TOMBSTONES_SQL } from './page-purge.ts';
import { WORKTREE_BINDING_SQL } from './ownership.ts';
import { SOURCE_TRUST_CONFIG_SQL } from '../trust/channel.ts';
+import { SOURCE_PURGE_EPOCH_SQL } from '../reason-trailers.ts';
/** keep_db DB-wins writes one pass admits (design v3.2 §4, [estimated]); the rest are `keep_db_pending`. */
export const KEEP_DB_PASS_CAP = 200;
@@ -185,8 +186,11 @@ export async function buildConvergenceReceipt(engine: BrainEngine, level: Shadow
};
}
-/** Plain reads every screened preparation repeats beside group-publish.ts's `preparationReads` set: the binding, the owner trust config and the purge tombstones. */
-const SCREEN_STABLE_READS = [WORKTREE_BINDING_SQL, SOURCE_TRUST_CONFIG_SQL, PAGE_PURGE_TOMBSTONES_SQL];
+/**
+ * Plain reads every screened preparation repeats beside group-publish.ts's `preparationReads` set: the binding, the owner
+ * trust config, the purge tombstones and the source's purge epoch (the key of the pass's reason-trailer scan, GBRA-217).
+ */
+const SCREEN_STABLE_READS = [WORKTREE_BINDING_SQL, SOURCE_TRUST_CONFIG_SQL, PAGE_PURGE_TOMBSTONES_SQL, SOURCE_PURGE_EPOCH_SQL];
/**
* The dry run's read-only `sameContentAtOrigin` (sync-run.ts): prepares each drift path's import as a screening request
diff --git a/src/core/reason-trailers.ts b/src/core/reason-trailers.ts
index 99239c1252..06bb6d3f82 100644
--- a/src/core/reason-trailers.ts
+++ b/src/core/reason-trailers.ts
@@ -131,8 +131,8 @@ export interface SyncReasonScan {
* canonicalised and secret-scanned (a secret, an over-long value or a tombstoned (commit, path) gives NULL); a value
* naming no path its commit touched is malformed and counted. A failed or capped scan maps nothing.
*/
-export async function scanSyncReasons(engine: BrainEngine, input: { sourceId: string; sourceRoot: string; from: string | null; to: string }): Promise<SyncReasonScan | null> {
- const repo = gitTopLevel(input.sourceRoot);
+export async function scanSyncReasons(engine: BrainEngine, input: { sourceId: string; sourceRoot: string; gitRoot?: string; from: string | null; to: string }): Promise<SyncReasonScan | null> {
+ const repo = input.gitRoot ? knownGitTopLevel(input.gitRoot, input.sourceRoot) : gitTopLevel(input.sourceRoot);
if (!repo) return null;
const range = input.from ? `${input.from}..${input.to}` : input.to;
const byPath = new Map<string, ReasonDecision>();
@@ -187,6 +187,14 @@ export function gitTopLevel(sourceRoot: string): { gitRoot: string; scopePrefix:
return top.status === 'found' ? { gitRoot: top.gitRoot, scopePrefix: top.scopePrefix } : null;
}
+/**
+ * GBRA-133: a classic sync pass already discovered its checkout's top level (real path) without blocking, so its scan places
+ * `sourceRoot` under it and runs no synchronous `git rev-parse` on the resident's path; null when the source path is gone.
+ */
+function knownGitTopLevel(gitRoot: string, sourceRoot: string): { gitRoot: string; scopePrefix: string } | null {
+ try { return { gitRoot, scopePrefix: relative(gitRoot, realpathSync(sourceRoot)).split(sep).join('/') }; } catch { return null; }
+}
+
/** A file's path relative to the scan's Git top level, resolving its directory's real path (the file itself may be gone). */
function repoPathOf(scan: SyncReasonScan, filePath: string): string {
let dir = dirname(filePath);
@@ -228,6 +236,7 @@ export function reasonScanNotices(scan: SyncReasonScan | null | undefined, sourc
return notices;
}
+export const SOURCE_PURGE_EPOCH_SQL = 'SELECT purge_epoch FROM sources WHERE id = $1';
const cache = new Map<string, { at: number; scan: Promise<SyncReasonScan | null> }>();
const CACHE_TTL_MS = 10 * 60_000;
@@ -237,7 +246,7 @@ const CACHE_TTL_MS = 10 * 60_000;
* that writes its tombstones, so a purge from any process makes the next request scan again and read them.
*/
export async function cachedSyncReasonScan(engine: BrainEngine, input: Parameters<typeof scanSyncReasons>[1]): Promise<SyncReasonScan | null> {
- const [source] = await engine.executeRaw<{ purge_epoch: string | number }>('SELECT purge_epoch FROM sources WHERE id = $1', [input.sourceId]);
+ const [source] = await engine.executeRaw<{ purge_epoch: string | number }>(SOURCE_PURGE_EPOCH_SQL, [input.sourceId]);
const key = [input.sourceId, String(source?.purge_epoch ?? 0), input.sourceRoot, input.from ?? '', input.to].join('\0');
const hit = cache.get(key);
if (hit && Date.now() - hit.at < CACHE_TTL_MS) return hit.scan;
diff --git a/test/hardened-git-safe-directory-7584.test.ts b/test/hardened-git-safe-directory-7584.test.ts
index 68c8efb14c..d33416b2c6 100644
--- a/test/hardened-git-safe-directory-7584.test.ts
+++ b/test/hardened-git-safe-directory-7584.test.ts
@@ -7,12 +7,13 @@
* Ownership by another user is forced with Git's own `GIT_TEST_ASSUME_DIFFERENT_OWNER` through a `git` wrapper first on
* PATH, because the hardened environment drops every `GIT_*` variable a test could set.
*/
-import { describe, expect, test } from 'bun:test';
+import { describe, expect, spyOn, test } from 'bun:test';
import { chmodSync, mkdtempSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { execFileSync } from 'node:child_process';
-import { hardenedGitStream, hardenedGitSync } from '../src/core/hardened-git.ts';
+import { __resetOperatorGitConfigForTests, hardenedGitStream, hardenedGitSync } from '../src/core/hardened-git.ts';
+import * as spawn from '../src/core/spawn.ts';
import { gitChildEnv } from '../src/core/git-env.ts';
import { withEnv } from './helpers/with-env.ts';
import { makeGitFixture } from './helpers/git-fixture.ts';
@@ -81,6 +82,18 @@ describe.skipIf(process.platform === 'win32')('#7584 hardened reads of a reposit
});
});
+ test('GBRA-133: a streamed read honours the allowance and spawns no synchronous Git, even with nothing cached', async () => {
+ const { root, config, base } = await setup();
+ await withEnv({ ...base, GIT_CONFIG_GLOBAL: config('global', root) }, async () => {
+ __resetOperatorGitConfigForTests();
+ const sync = spyOn(spawn, 'spawnSync');
+ try {
+ expect((await hardenedGitStream(root, ['rev-parse', '--verify', '--quiet', 'HEAD^{commit}'], { ...LIMITS, onChunk: () => {} })).ok).toBe(true);
+ expect(sync).not.toHaveBeenCalled();
+ } finally { sync.mockRestore(); }
+ });
+ });
+
test('an allowance for another directory does not open this one', async () => {
const { dir, root, config, base } = await setup();
await withEnv({ ...base, GIT_CONFIG_GLOBAL: config('global', join(dir, 'elsewhere')) }, () => {