Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(cycle): #8073 a refused batch commit must not revert bytes another writer committed; an unknown checkout is not committed into (red)

garrytan9bc173a5d859
diff --git a/src/core/cycle/derived-write-through.ts b/src/core/cycle/derived-write-through.ts
index de1e42bafb..29e9af9394 100644
--- a/src/core/cycle/derived-write-through.ts
+++ b/src/core/cycle/derived-write-through.ts
@@ -37,7 +37,7 @@ import { isDbOnly, loadStorageConfig } from '../storage-config.ts';
import { deletePageThrough, isWriteThroughDisabled, resolvePageWriteTarget, writePageThrough, type PageWriteTarget } from '../write-through.ts';
import { urlCloneReadOnly } from '../url-clone.ts';
import { gitChildEnv, literalPathspecs } from '../git-env.ts';
-import { classifyGitCheckout } from '../git-checkout.ts';
+import { classifyGitCheckout, type GitCheckoutClass } from '../git-checkout.ts';
import { checkoutRoot, commitAndPush, commitPathsLocked, notePending, scopePaths } from '../git/commit-push.ts';
import { managedFilesystemRootFor, refreshManagedFilesystemRoots } from '../persistence/filesystem-guard.ts';
import { withSourceFilesystemLock } from '../minions/source-filesystem.ts';
@@ -91,8 +91,16 @@ interface DerivedFileChange { slug: string; sourceId: string; filePath: string;
/** A batch commit the checkout refused: the door's code and message, the batch's files (root-relative) and those left as they were. */
export interface DerivedCommitFailure { code: string; message: string; paths: string[]; kept: string[] }
-/** Test seam: runs after a batch's files are written and before its commit; a throw stands in for a crash (no rollback). */
-export const derivedWriteSeams: { beforeCommit?: (root: string) => void } = {};
+/**
+ * Test seams. `beforeCommit` runs after a batch's files are written and before its commit; a throw stands in for a crash
+ * (no rollback). `checkoutClass` replaces `classifyGitCheckout` for a write root (an `unknown` answer needs a failed lookup).
+ */
+export const derivedWriteSeams: { beforeCommit?: (root: string) => void; checkoutClass?: (root: string) => GitCheckoutClass } = {};
+
+/** Whether `root` sits in a Git checkout this module commits into (`classifyGitCheckout`, or the test seam). */
+function checkoutClassOf(root: string): GitCheckoutClass {
+ return (derivedWriteSeams.checkoutClass ?? classifyGitCheckout)(root);
+}
/** How long a batch waits for the checkout's commit lock before it rolls back. */
const DERIVED_COMMIT_LOCK_WAIT_MS = 30_000;
@@ -125,7 +133,7 @@ async function writeDerivedFile(engine: BrainEngine, slug: string, sourceId: str
*/
function noteWritten(root: string, filePath: string): void {
try {
- if (classifyGitCheckout(root) !== 'git') return;
+ if (checkoutClassOf(root) !== 'git') return;
const top = checkoutRoot(root);
notePending(top, scopePaths(top, [filePath], top));
} catch { /* best-effort: the batch commit right after is the primary path */ }
@@ -178,7 +186,7 @@ async function commitDerivedChanges(engine: BrainEngine, label: string, changes:
const failures: DerivedCommitFailure[] = [];
for (const root of new Set(changes.map(c => c.writeRoot))) {
const batch = changes.filter(c => c.writeRoot === root);
- if (classifyGitCheckout(root) === 'not_git') continue;
+ if (checkoutClassOf(root) === 'not_git') continue;
derivedWriteSeams.beforeCommit?.(root);
try {
await refreshManagedFilesystemRoots(engine);
diff --git a/test/cycle/derived-write-through-commit.test.ts b/test/cycle/derived-write-through-commit.test.ts
index 36ffe2093a..929a14f4a9 100644
--- a/test/cycle/derived-write-through-commit.test.ts
+++ b/test/cycle/derived-write-through-commit.test.ts
@@ -30,6 +30,7 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import type { ChatOpts, ChatResult } from '../../src/core/ai/gateway.ts';
import { derivedWriteSeams } from '../../src/core/cycle/derived-write-through.ts';
+import { commitPathsLocked } from '../../src/core/git/commit-push.ts';
import { runPhaseExtractAtoms } from '../../src/core/cycle/extract-atoms.ts';
import { runPhaseSynthesizeConcepts } from '../../src/core/cycle/synthesize-concepts.ts';
import type { BrainEngine } from '../../src/core/engine.ts';
@@ -199,6 +200,43 @@ for (const { backend, databaseUrl } of pathologicalArms()) {
});
}, 180_000);
+ test('a refused commit never reverts a file another writer already committed with the batch\'s bytes', async () => {
+ await withBrain(databaseUrl, 'git', async b => {
+ const mergeHead = join(b.root, '.git', 'MERGE_HEAD');
+ derivedWriteSeams.beforeCommit = root => {
+ writeFileSync(join(root, 'notes.md'), 'writer B\n');
+ commitPathsLocked(root, ['notes.md'], 'writer B', { lockWaitMs: 5_000 });
+ writeFileSync(mergeHead, `${b.git('rev-parse', 'HEAD')}\n`);
+ };
+ let phase: Awaited<ReturnType<Brain['extract']>>;
+ try {
+ phase = await b.extract(['Renders vs proof'], 'hash-one');
+ } finally {
+ delete derivedWriteSeams.beforeCommit;
+ rmSync(mergeHead, { force: true });
+ }
+ const [atom] = await b.atoms();
+ expect({ swept: lastCommitPaths(b), status: phase.status, failed: phase.failed, file: existsSync(b.path(atom!.slug)),
+ bound: atom!.source_path !== null, dirty: dirty(b) })
+ .toEqual({ swept: ['notes.md', relOf(atom!.slug)].sort(), status: 'ok', failed: [], file: true, bound: true, dirty: '' });
+ });
+ }, 180_000);
+
+ test('a write root whose checkout lookup fails (unknown) is not committed into: its files stay and nothing rolls back', async () => {
+ await withBrain(databaseUrl, 'plain', async b => {
+ derivedWriteSeams.checkoutClass = () => 'unknown';
+ let phase: Awaited<ReturnType<Brain['extract']>>;
+ try {
+ phase = await b.extract(['Renders vs proof'], 'hash-one');
+ } finally {
+ delete derivedWriteSeams.checkoutClass;
+ }
+ const [atom] = await b.atoms();
+ expect({ status: phase.status, failed: phase.failed, file: existsSync(b.path(atom!.slug)), bound: atom!.source_path !== null })
+ .toEqual({ status: 'ok', failed: [], file: true, bound: true });
+ });
+ }, 180_000);
+
test('storage.db_only: atoms stay database-only and nothing is committed', async () => {
await withBrain(databaseUrl, 'git', async b => {
writeFileSync(join(b.root, 'gbrain.yml'), 'storage:\n db_only:\n - atoms/\n');