Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
perf(doctor): eval_drift refreshes a racy index after a slow working-tree diff
time-attack18dd27d6c123
diff --git a/src/core/eval/drift-watch.ts b/src/core/eval/drift-watch.tsindex ea79be6de8..334af81e01 100644--- a/src/core/eval/drift-watch.ts+++ b/src/core/eval/drift-watch.ts@@ -76,6 +76,13 @@ export function resolveGbrainSourceRoot(moduleUrl: string = import.meta.url): streturn null;}+/**+ * A working-tree diff at least this slow refreshes the index afterwards. Slowness there means racily-clean entries+ * (a fresh clone or a just-pulled checkout), which git re-hashes on every diff until the index is rewritten; a+ * settled checkout diffs fast and skips the refresh.+ */+export const RACY_DIFF_MS = 75;+/*** Return repo-relative paths that have changed in the working tree since* the given commit (or HEAD if no commit). Returns `null` when the probe@@ -85,19 +92,27 @@ export function resolveGbrainSourceRoot(moduleUrl: string = import.meta.url): st* `commitSha` is a full or short SHA. When omitted, compares HEAD against* working tree (uncommitted changes only).*/-export function filesDriftedSince(repoRoot: string, commitSha?: string): string[] | null {+export function filesDriftedSince(repoRoot: string, commitSha?: string, racyDiffMs = RACY_DIFF_MS): string[] | null {if (!existsSync(repoRoot)) return null;try {const range = commitSha ? `${commitSha}..HEAD` : 'HEAD';const args = commitSha? ['diff', '--name-only', range]: ['diff', '--name-only', 'HEAD'];+ const startedAt = Date.now();const out = execSync(`git ${args.join(' ')}`, {cwd: repoRoot,encoding: 'utf-8',stdio: ['pipe', 'pipe', 'pipe'],timeout: 5000,});+ if (!commitSha && Date.now() - startedAt >= racyDiffMs) {+ try {+ execSync('git update-index -q --refresh', { cwd: repoRoot, stdio: ['pipe', 'pipe', 'pipe'], timeout: 5000 });+ } catch {+ /* a held index.lock or read-only checkout keeps the index as it was */+ }+ }return out.split('\n').map(s => s.trim())diff --git a/test/drift-watch.test.ts b/test/drift-watch.test.tsindex 84f4be9245..6ebda98247 100644--- a/test/drift-watch.test.ts+++ b/test/drift-watch.test.ts@@ -4,7 +4,7 @@*/import { afterAll, describe, expect, test } from 'bun:test';import { execSync } from 'child_process';-import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs';+import { existsSync, mkdirSync, mkdtempSync, rmSync, statSync, utimesSync, writeFileSync } from 'fs';import { tmpdir } from 'os';import { join } from 'path';import {@@ -120,6 +120,46 @@ describe('filesDriftedSince + watchedFilesDrifted probe failure is distinguishab});});+describe('filesDriftedSince refreshes a racy index after a slow diff', () => {+ function committedRepoWithRacyIndex(): string {+ const repo = scratchRepoWithStagedSearchFile();+ execSync('git -c [email protected] -c user.name=t -c commit.gpgsign=false commit -q -m add', { cwd: repo, stdio: 'pipe' });+ const past = new Date(Date.now() - 3_600_000);+ utimesSync(join(repo, '.git', 'index'), past, past);+ return repo;+ }+ const indexMtime = (repo: string) => statSync(join(repo, '.git', 'index')).mtimeMs;++ test('a slow diff rewrites the index and keeps the answer', () => {+ const repo = committedRepoWithRacyIndex();+ const before = indexMtime(repo);+ expect(filesDriftedSince(repo, undefined, 0)).toEqual([]);+ expect(indexMtime(repo)).toBeGreaterThan(before);+ });++ test('a fast diff leaves the index alone', () => {+ const repo = committedRepoWithRacyIndex();+ const before = indexMtime(repo);+ expect(filesDriftedSince(repo, undefined, 60_000)).toEqual([]);+ expect(indexMtime(repo)).toBe(before);+ });++ test('a commit range never refreshes', () => {+ const repo = committedRepoWithRacyIndex();+ const before = indexMtime(repo);+ expect(filesDriftedSince(repo, 'HEAD~1', 0)).toEqual(['src/core/search/hybrid.ts']);+ expect(indexMtime(repo)).toBe(before);+ });++ test('a held index.lock leaves the answer unchanged', () => {+ const repo = committedRepoWithRacyIndex();+ writeFileSync(join(repo, '.git', 'index.lock'), '');+ expect(filesDriftedSince(repo, undefined, 0)).toEqual([]);+ writeFileSync(join(repo, 'src/core/search/hybrid.ts'), 'export const x = 1;\n');+ expect(filesDriftedSince(repo, undefined, 0)).toEqual(['src/core/search/hybrid.ts']);+ });+});+describe('resolveGbrainSourceRoot (#4606)', () => {test('resolves this source checkout from the module location', () => {const root = resolveGbrainSourceRoot();