Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(eval_drift): refresh a racy index after a timed-out diff so later probes answer; doctor goldens settle the checkout index before the first capture
garrytanc3e9ebf37e06
diff --git a/src/core/eval/drift-watch.ts b/src/core/eval/drift-watch.tsindex a3c661b91c..203b6b5d16 100644--- a/src/core/eval/drift-watch.ts+++ b/src/core/eval/drift-watch.ts@@ -95,33 +95,37 @@ export const RACY_DIFF_MS = 75;* the range). When omitted, compares HEAD against* working tree (uncommitted changes only).*/-export function filesDriftedSince(repoRoot: string, commitSha?: string, racyDiffMs = RACY_DIFF_MS): string[] | null {+export function filesDriftedSince(repoRoot: string, commitSha?: string, racyDiffMs = RACY_DIFF_MS, diffTimeoutMs = 5000): string[] | null {if (!existsSync(repoRoot)) return null;if (commitSha !== undefined && (commitSha === '' || commitSha.startsWith('-'))) return null;+ const refreshIndex = () => {+ try {+ execFileSync('git', ['update-index', '-q', '--refresh'], { env: gitChildEnv(), cwd: repoRoot, stdio: ['pipe', 'pipe', 'pipe'], timeout: 30_000 });+ } catch {+ /* a held index.lock or read-only checkout keeps the index as it was */+ }+ };+ const startedAt = Date.now();try {const args = commitSha? ['diff', '--name-only', '--end-of-options', `${commitSha}..HEAD`]: ['diff', '--name-only', 'HEAD'];- const startedAt = Date.now();const out = execFileSync('git', args, {env: gitChildEnv(),cwd: repoRoot,encoding: 'utf-8',stdio: ['pipe', 'pipe', 'pipe'],- timeout: 5000,+ timeout: diffTimeoutMs,});- if (!commitSha && Date.now() - startedAt >= racyDiffMs) {- try {- execFileSync('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 */- }- }+ if (!commitSha && Date.now() - startedAt >= racyDiffMs) refreshIndex();return out.split('\n').map(s => s.trim()).filter((s): s is string => s.length > 0);} catch {+ // A diff never rewrites the index, so one that ran out of time on a racy index would time out the same way+ // on every later probe; refresh it so the next probe answers.+ if (!commitSha && Date.now() - startedAt >= diffTimeoutMs) refreshIndex();return null;}}diff --git a/test/drift-watch.test.ts b/test/drift-watch.test.tsindex 6ebda98247..24cd332f7f 100644--- a/test/drift-watch.test.ts+++ b/test/drift-watch.test.ts@@ -151,6 +151,14 @@ describe('filesDriftedSince refreshes a racy index after a slow diff', () => {expect(indexMtime(repo)).toBe(before);});+ test('a diff that runs out of time still rewrites the racy index, so the next probe answers', () => {+ const repo = committedRepoWithRacyIndex();+ const before = indexMtime(repo);+ expect(filesDriftedSince(repo, undefined, 60_000, 1)).toBeNull();+ expect(indexMtime(repo)).toBeGreaterThan(before);+ expect(filesDriftedSince(repo)).toEqual([]);+ });+test('a held index.lock leaves the answer unchanged', () => {const repo = committedRepoWithRacyIndex();writeFileSync(join(repo, '.git', 'index.lock'), '');diff --git a/test/helpers/doctor-json-golden.ts b/test/helpers/doctor-json-golden.tsindex 2dfe3b5713..096b1c5a4c 100644--- a/test/helpers/doctor-json-golden.ts+++ b/test/helpers/doctor-json-golden.ts@@ -87,6 +87,18 @@ export interface GbrainRun {}/** Spawn `bun src/cli.ts <args>` in the hermetic doctor env. */+/**+ * eval_drift diffs this checkout against HEAD with a 5 s limit. A freshly copied checkout (a CI VM) has a racy+ * index, so the first diff can run out of time and report "could not probe" while the next one answers; refresh+ * the index once before the first capture so every capture sees the same settled tree.+ */+let indexSettled = false;+function settleRepoIndex(): void {+ if (indexSettled) return;+ indexSettled = true;+ Bun.spawnSync(['git', 'update-index', '-q', '--refresh'], { cwd: REPO_ROOT, stdout: 'ignore', stderr: 'ignore' });+}+export async function runGbrain(h: DoctorHome, args: string[], env: Record<string, string | undefined> = {}, timeoutMs = 120_000): Promise<GbrainRun> {const childEnv: Record<string, string> = {};for (const [k, v] of Object.entries(process.env)) if (v !== undefined) childEnv[k] = v;@@ -110,6 +122,7 @@ export async function runGbrain(h: DoctorHome, args: string[], env: Record<strinif (v === undefined) delete childEnv[k];else childEnv[k] = v;}+ settleRepoIndex();const proc = Bun.spawn([process.execPath, '--no-env-file', '--preload', PRELOAD, '--preload', CLOCK_PRELOAD, CLI, ...args], {cwd: h.work,env: childEnv,