Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(door): a staged entry holding the parent version is the door own stale refresh on every write; prove the D4 resolution path

garrytan7d997be0761a
diff --git a/src/core/persistence/git-door.ts b/src/core/persistence/git-door.ts
index c9d3fcbbe0..458f5c433a 100644
--- a/src/core/persistence/git-door.ts
+++ b/src/core/persistence/git-door.ts
@@ -20,9 +20,9 @@
* and after `hash-object -w` stores it (an edit landing during the write refuses `git_target_changed`), and each
* target's index entry must equal HEAD's before the commit is built (a staged change of a target refuses
* `git_index_conflict`, index untouched). After the branch moves, an entry someone staged meanwhile is left as it is
- * and the write reports that its commit landed. A write whose path already holds the planned blob at HEAD only refreshes
- * an index entry still holding the parent commit's version (the door's own interrupted refresh); any other staged
- * entry refuses `git_index_conflict`.
+ * and the write reports that its commit landed. An entry still holding HEAD's first parent's version at that path counts
+ * as clean: it is what an interrupted refresh after the door's own commit leaves, and its bytes are in history, so the
+ * next write (or a replay whose blob HEAD already holds) refreshes it; any other staged entry refuses `git_index_conflict`.
*
* Recovery inverts for those writes: the recovery record names the HEAD it was prepared against (`door`), and a
* publication that stopped after its commit landed keeps the committed file instead of restoring the prior bytes.
@@ -283,15 +283,15 @@ async function doorCommit(session: GitSession, root: string, files: DoorFile[]):
if (fresh.length) await refuseIgnored(session, root, fresh);
const indexed = await readIndex(session, index, [...targets.values()]);
const staged = [...targets.values()].filter(target => indexed.entries.get(target.rel) !== target.head);
- if (!changed.length) {
- if (!staged.length) return head;
+ if (staged.length) {
const parent = await parentEntries(session, head, staged);
const foreign = staged.filter(target => indexed.entries.get(target.rel) !== parent.get(target.rel));
if (foreign.length) throw indexConflict(root, foreign);
- await syncIndex(session, root, prefix, index, [...targets.values()], head, indexed);
+ }
+ if (!changed.length) {
+ if (staged.length) await syncIndex(session, root, prefix, index, [...targets.values()], head, indexed);
return head;
}
- if (staged.length) throw indexConflict(root, staged);
// The trees are rebuilt bottom-up: an emptied directory leaves its parent, the rest are written and linked.
const written: { path: string; oid: string }[] = [];
@@ -358,7 +358,7 @@ function refuseChanged(root: string, targets: Iterable<Target>): void {
'Another process changed the file during the write; recovery leaves the changed file as it is. Retry the write with a new request_id once the file has settled.', { detail: moved.join(',') });
}
-/** Targets whose index entry differs from HEAD's (a staged change, or a merge conflict) refuse as `git_index_conflict`. */
+/** Targets whose index entry differs from HEAD's and from its first parent's (a staged change, or a merge conflict) refuse as `git_index_conflict`. */
function indexConflict(root: string, staged: Target[]): OperationError {
const paths = staged.map(target => target.rel);
return opError('git_index_conflict', `${paths.join(', ')} in ${root} has a staged change that is not committed, so this write committed nothing and the index was left as it was.`,
diff --git a/test/git-door-planned-state.test.ts b/test/git-door-planned-state.test.ts
index 39e4e8db24..66f3a9fe8b 100644
--- a/test/git-door-planned-state.test.ts
+++ b/test/git-door-planned-state.test.ts
@@ -7,10 +7,15 @@
* - D4: a disk edit landing during the publication is never committed: it refuses git_target_changed, HEAD and the
* row keep the prior revision, and recovery holds the edited file as it is (red before: HEAD held the edit while the
* row recorded the publication's content);
+ * - D4's recovery record pauses every later write of the worktree until `gbrain repair stuck-recovery --keep disk`
+ * resolves it: the human's bytes stay on disk, the write ends failed, the queued write publishes, Git and the row
+ * keep the prior revision until the edit is committed, and a sync then imports it;
* - with no interference the planned blob commits and the index and status are clean;
* - plumbing: a file rewritten between plan and commit refuses with nothing committed or staged; a change staged
- * after the pre-commit check and before the index refresh is left staged and the refusal says the commit landed; a
- * commit already at HEAD refreshes an entry still holding the parent's version and refuses any other staged entry.
+ * after the pre-commit check and before the index refresh is left staged and the refusal says the commit landed; an
+ * entry still holding the parent's version (a landed commit whose index refresh failed) counts as clean, so the next
+ * changed write and a replay already at HEAD both refresh it, while any other staged entry refuses (red before: the
+ * next changed write refused git_index_conflict until someone ran git restore --staged).
*/
import { afterEach, describe, expect, test } from 'bun:test';
import { execFileSync } from 'node:child_process';
@@ -26,10 +31,13 @@ import { publishMutation, type PublicationHooks } from '../src/core/persistence/
import { sha256 } from '../src/core/persistence/digest.ts';
import { commitThroughDoor, doorSeams } from '../src/core/persistence/git-door.ts';
import { localHostId } from '../src/core/persistence/identity.ts';
-import { admitWrite, claimNextWrite } from '../src/core/persistence/journal.ts';
+import { admitWrite, claimNextWrite, getWriteRequestById } from '../src/core/persistence/journal.ts';
import { getWorktreeBinding } from '../src/core/persistence/ownership.ts';
import { preparePageMutation } from '../src/core/persistence/page-prepare.ts';
import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
+import { readHeadBlockers } from '../src/core/persistence/head-blocker.ts';
+import { performManagedSync } from '../src/core/persistence/sync-run.ts';
+import { runRepairCommand } from '../src/commands/repair.ts';
import { durableGitRepo } from './helpers/git-publication.ts';
import { managedBrain, settleManagedGitEffects } from './helpers/managed-brain.ts';
import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';
@@ -59,10 +67,24 @@ async function publish(engine: BrainEngine, ctx: OperationContext, content: stri
const intent = { slug: SLUG, content, expected_revision: old.revision };
await admitWrite(engine, { principal: authority.principal, authority, operation: 'put_page', sourceId: 'default', sourceIncarnation: binding.source_incarnation,
slug: SLUG, pageId: old.page.id, requestId: randomUUID(), callerIntent: intent, intent, worktreeId: binding.worktree_id, topologyGeneration: binding.topology_generation });
- const row = (await claimNextWrite(engine, host))!;
+ return publishNext(engine, ctx, hooks);
+}
+
+/** Claims and publishes the next queued write of this host. */
+async function publishNext(engine: BrainEngine, ctx: OperationContext, hooks: PublicationHooks = {}) {
+ const host = localHostId(), row = (await claimNextWrite(engine, host))!;
return publishMutation(engine, row, await preparePageMutation(engine, row, ctx.config), host, hooks);
}
+/** `gbrain repair <args>` in this process, returning what it printed. */
+async function repairCli(engine: BrainEngine, args: string[]): Promise<string> {
+ let printed = '';
+ const original = console.log;
+ console.log = (...parts: unknown[]) => { printed += `${parts.map(String).join(' ')}\n`; };
+ try { await runRepairCommand(engine, args); } finally { console.log = original; }
+ return printed;
+}
+
/** Where the page stands afterwards: the row's title, HEAD's and the disk's title lines, and the index entry's blob. */
async function standing(engine: BrainEngine, root: string) {
const title = (text: string) => /^title: (.*)$/m.exec(text)?.[1];
@@ -120,6 +142,40 @@ for (const backend of testBackends()) describe(`[${backend}] the door commits on
expect(git(root, 'status', '--porcelain')).toBe(`M ${FILE}`);
}, { databaseUrl, setup }), 120_000);
+ test('D4 resolution: the record pauses later writes; repair stuck-recovery --keep disk frees them, keeps the edit, and a commit of it syncs in', () => managedBrain(async ({ engine, ctx, root }) => {
+ await committedPage(engine, ctx);
+ const d4 = await publish(engine, ctx, page('Second'), { boundary: async name => {
+ if (name !== 'after_publication') return;
+ writeFileSync(join(root, FILE), readFileSync(join(root, FILE), 'utf8').replace('title: Second', 'title: USER DISK EDIT'));
+ } });
+ expect(d4).toMatchObject({ state: 'recovering', error_code: 'git_target_changed', blocked_reason: 'unexpected_file_bytes' });
+ const pending = await putPage.handler(ctx, { slug: 'notes/other', content: page('Other'), request_id: randomUUID(), wait_ms: 200 })
+ .then(() => null, (error: { code?: string; writeRequest?: { request_id: string } }) => error);
+ expect(pending).toMatchObject({ code: 'write_pending' });
+ await disposePersistenceConsumer(engine);
+ const [{ id: later }] = await engine.executeRaw<{ id: string }>('SELECT id::text AS id FROM persistence_requests WHERE request_id=$1::uuid', [pending!.writeRequest!.request_id]);
+ expect((await readHeadBlockers(engine, [later!], localHostId())).get(later!)).toMatchObject({ status: 'blocked',
+ blocker: { predicate: 'request_recovery', ids: [d4.request_id], state: 'unexpected_file_bytes', intervention: true, file: { slug: SLUG } } });
+
+ const preview = await repairCli(engine, ['stuck-recovery', '--source', 'default', '--json']);
+ const hash = /"preview_hash": "([0-9a-f]{64})"/.exec(preview)![1]!;
+ const applied = await repairCli(engine, ['stuck-recovery', '--source', 'default', '--keep', 'disk', '--apply', '--expect', hash, '--yes', '--json']);
+ expect(applied).toContain('"resolved": 1');
+ expect(await getWriteRequestById(engine, d4.id)).toMatchObject({ state: 'failed', error_code: 'git_target_changed', recovery: null });
+ expect((await readHeadBlockers(engine, [later!], localHostId())).get(later!)).toEqual({ status: 'claimable' });
+ expect(await publishNext(engine, ctx)).toMatchObject({ id: later, state: 'committed' });
+
+ expect(['first_sync', 'synced', 'up_to_date']).toContain((await performManagedSync(engine, { sourceId: 'default', noPull: true })).status);
+ const head = git(root, 'rev-parse', `HEAD:${FILE}`);
+ expect(await standing(engine, root)).toEqual({ row: 'First', head: 'First', disk: 'USER DISK EDIT', index: head });
+ expect(git(root, 'status', '--porcelain')).toBe(`M ${FILE}`);
+
+ git(root, '-c', 'commit.gpgsign=false', 'commit', '-qm', 'Keep the edit', '--', FILE);
+ expect(['synced', 'up_to_date']).toContain((await performManagedSync(engine, { sourceId: 'default', noPull: true })).status);
+ expect(await standing(engine, root)).toEqual({ row: 'USER DISK EDIT', head: 'USER DISK EDIT', disk: 'USER DISK EDIT', index: git(root, 'rev-parse', `HEAD:${FILE}`) });
+ expect(git(root, 'status', '--porcelain')).toBe('');
+ }, { databaseUrl, setup }), 180_000);
+
test('with no interference the planned blob commits, and the index and status are clean', () => managedBrain(async ({ engine, ctx, root }) => {
await committedPage(engine, ctx);
const result = await publish(engine, ctx, page('Second'));
@@ -198,6 +254,26 @@ describe('door plumbing: the planned state', () => {
expect(git(root, 'rev-parse', ':notes/a.md')).toBe(git(root, 'rev-parse', 'HEAD:notes/a.md'));
});
+ test('after a door commit whose index refresh failed, the next changed write to those paths commits and leaves the index clean', async () => {
+ const root = repo({ 'notes/a.md': 'a\n' });
+ const lock = join(root, '.git', 'index.lock');
+ doorSeams.beforeUpdateRef = () => { writeFileSync(lock, ''); };
+ put(root, 'notes/a.md', 'first\n'); put(root, 'notes/new.md', 'first new\n');
+ const landed = await refusal(commitThroughDoor(root, ['notes/a.md', 'notes/new.md'].map(path => ({ path: join(root, path), planned: sha256(readFileSync(join(root, path))) }))));
+ rmSync(lock);
+ delete doorSeams.beforeUpdateRef;
+ expect(landed).toMatchObject({ code: 'git_unavailable', message: expect.stringContaining('landed, but the index') });
+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('first');
+ expect(git(root, 'rev-parse', ':notes/a.md')).toBe(git(root, 'rev-parse', 'HEAD^:notes/a.md'));
+ expect(git(root, 'ls-files', '--', 'notes/new.md')).toBe('');
+ put(root, 'notes/a.md', 'second\n'); put(root, 'notes/new.md', 'second new\n');
+ const commit = await commitThroughDoor(root, ['notes/a.md', 'notes/new.md'].map(path => ({ path: join(root, path), planned: sha256(readFileSync(join(root, path))) })));
+ expect(commit).toBe(git(root, 'rev-parse', 'HEAD'));
+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('second');
+ expect(git(root, 'show', 'HEAD:notes/new.md')).toBe('second new');
+ expect(git(root, 'status', '--porcelain')).toBe('');
+ });
+
test('a commit already at HEAD refreshes an entry holding the parent\'s version and refuses any other staged entry', async () => {
const root = repo({ 'notes/a.md': 'a\n' });
const commit = await publishAt(root, 'notes/a.md', 'door\n');