Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
A gbrain commit no longer reverts someone else's commit of the same file
garrytan1bc0c85fdaf0
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 927b2836bc..620259406d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,16 @@ credits are retained; no result has been reassigned to another provider. Originaidentifiers and attribution are available in the pre-removal Git revision`6040075c6cb95be5881cc2e1b76ef7d71f4e5d29` (retained on 2026-09-23).+## [Unreleased]++### A gbrain commit no longer reverts someone else's commit of the same file++**When a script, an agent or a person commits their edit of a page file while gbrain is committing the same file, their commit now stays.** If that commit landed between gbrain reading HEAD and moving the branch, gbrain's one retry used to rebuild its commit on top of it with the bytes gbrain had staged earlier. HEAD silently went back to gbrain's version, the next sync reported success with nothing imported, the index kept the other version staged, and later edits to the page could be refused as an uncoordinated local edit. The other writer's bytes survived only in their own commit.++- A managed brain's Git effect now checks, before each commit attempt, that no commit since it staged its files changed any of them and that the index still holds what it staged. When one did, it commits nothing over it. A file whose edit the other writer committed completes `superseded` (reason `committed_by_another_writer`), the next sync imports the other version, and gbrain's write stays in the page's history. Every other file in the group is put back and retried on the new HEAD, so a file that still holds gbrain's bytes is committed on top.+- A strict (`canonical: git`) source's write refuses `git_target_changed` in the same case instead of rebuilding over the other commit, so recovery handles it the way it handles an edit made during the write.+- To find edits a brain already lost this way, [docs/guides/shared-checkout-git.md](docs/guides/shared-checkout-git.md#effect-reverted-edits) has a read-only loop over the history.+## [0.60.699.0] - 2026-11-12### The Ubicloud gate VMs run a current gitdiff --git a/docs/guides/shared-checkout-git.md b/docs/guides/shared-checkout-git.mdindex a234ff9973..71eac7a3ee 100644--- a/docs/guides/shared-checkout-git.md+++ b/docs/guides/shared-checkout-git.md@@ -331,6 +331,50 @@ A file flagged assume-unchanged or skip-worktree that differs from HEAD staysin the ledger (git add skips or refuses it), and `--pending` then fails`commit_paths_skipped` naming it.+<a id="effect-reverted-edits"></a>+## Edits a gbrain commit reverted++When another writer committed a page file while one of gbrain's Git effects+was committing the same file, older gbrain versions could rebuild the effect's+commit on top of the other commit with the bytes gbrain had staged before it.+HEAD then went back to gbrain's version without any warning, the next sync+imported nothing for that file, and the other writer's edit survived only in+its own commit. Current versions never lay staged bytes over a commit that+changed the file: when that commit also changed the file on disk, it stays+HEAD, the effect completes `superseded` (reason `committed_by_another_writer`)+and the next sync imports it; when the file still holds gbrain's bytes, the+effect stages them again and commits them on top. A strict source's write+refuses `git_target_changed` instead.++To find edits a brain already lost this way, look for a gbrain commit whose+parent is someone else's commit, made at most two minutes earlier, that changed+the same file. This loop only reads history:++```bash+BRAIN=/path/to/brain-checkout+TOP=$(git -C "$BRAIN" rev-parse --show-toplevel)+g() { git -C "$TOP" -c core.quotePath=false "$@"; }+g log --no-merges --format='%H %P' | while read -r c p extra; do+ [ -n "$p" ] && [ -z "$extra" ] || continue+ case "$(g log -1 --format=%s "$c")" in gbrain:*) ;; *) continue ;; esac+ case "$(g log -1 --format=%s "$p")" in gbrain:*) continue ;; esac+ [ $(( $(g log -1 --format=%ct "$c") - $(g log -1 --format=%ct "$p") )) -le 120 ] || continue+ g diff-tree -r --no-commit-id --name-only "$c" | while IFS= read -r f; do+ g diff-tree -r --no-commit-id --name-only --root -m --first-parent "$p" -- ":(literal)$f" | grep -qxF -- "$f" \+ && echo "$f: edit in $(g log -1 --format='%h (%an, %ci)' "$p") replaced by gbrain commit $(g log -1 --format=%h "$c")"+ done+done+```++Each line is a candidate, not proof: a gbrain write made right after someone+else's commit on purpose looks the same. To confirm one, read both versions+(`git show <their commit>:<file>` and `git show <gbrain commit>:<file>`).+While the commit ledger still holds that time, the reverting effect also has an+earlier `effect` line there whose sha is not in history and whose parent is the+other commit's parent (`git log -1 --format=%P <sha>`). Ask the user which+version to keep, then write the chosen content back as an ordinary page write+(`gbrain put` or `put_page` with the page's current revision).+<a id="scripts-and-the-managed-checkout"></a>## Scripts and the managed checkoutdiff --git a/scripts/postgres-arm-weights.json b/scripts/postgres-arm-weights.jsonindex a6abbfd3f8..9fa9b13ae3 100644--- a/scripts/postgres-arm-weights.json+++ b/scripts/postgres-arm-weights.json@@ -80,6 +80,7 @@"test/doctor-strict-facts-outside-git.test.ts": 7643,"test/doctor-sync-freshness-skill-pack-6076.test.ts": 5394,"test/edge-whitespace-twins.test.ts": 9721,+ "test/effect-commit-moved-head.test.ts": 22000,"test/embed-once-per-content-version.serial.test.ts": 17930,"test/embed-stale-images-managed-6529.test.ts": 11128,"test/embedding-completion-atomic.serial.test.ts": 8616,diff --git a/src/core/persistence/effect-git.ts b/src/core/persistence/effect-git.tsindex d646798f0f..09d26caa29 100644--- a/src/core/persistence/effect-git.ts+++ b/src/core/persistence/effect-git.ts@@ -320,6 +320,8 @@ export async function commitGitTargets(root: string, relativePaths: string[], si// #7091: ls-files and `diff --relative` print paths relative to the root, but status prints them relative to the// repository toplevel; a root below the toplevel (local_path <repo>/brain) strips its prefix so every listing compares.const prefix = paths.length ? (await git(session, ['rev-parse', '--show-prefix'])).stdout.replace(/\n$/, '') : '';+ // GST-2552: the HEAD the group stages against; a commit landing after it that touches a target wins over the staged blob.+ const baseline = paths.length ? await git(session, ['rev-parse', '--verify', '--quiet', 'HEAD']) : null;const indexBefore = await indexSnapshot(session, paths);session.undoStaging = () => restoreIndex(session, prefix, paths, indexBefore);const tracked = new Set((await list(['ls-files', '-z'])).split('\0').filter(Boolean));@@ -407,13 +409,22 @@ export async function commitGitTargets(root: string, relativePaths: string[], si}mkdirSync(persistenceHome(), { recursive: true, mode: 0o700 });const scratch = mkdtempSync(join(persistenceHome(), 'effect-commit-'));- let created: string | null;- try { created = await effectCommit(session, root, prefix, scratch, commitPaths, wanted, commitMessage(commitPaths, noted).map(stripspace).filter(Boolean), noted); }- finally { rmSync(scratch, { recursive: true, force: true }); }- const outcome = created ? { git: 'committed' } : gitFailure('Cannot commit the canonical Git target.',- `git commit failed for ${commitPaths.length} file(s); a missing Git identity (user.name, user.email) in that checkout is one cause to check.`);- for (const c of changed) results.set(c.requested, outcome);- if (!('git' in outcome)) await session.undoStaging();+ let commit: EffectCommit;+ try {+ commit = await effectCommit(session, root, prefix, scratch, commitPaths, wanted, commitMessage(commitPaths, noted).map(stripspace).filter(Boolean), noted,+ baseline?.code === 0 ? baseline.stdout.trim() : null);+ } finally { rmSync(scratch, { recursive: true, force: true }); }+ const { sha, moved, superseded, foreign } = commit;+ // A moved path's index entry another writer replaced stays theirs; one this effect still staged goes back to HEAD's, so+ // no later commit from the index reverts the other commit (a retry stages the file again). Only the rest is restored.+ if (!sha) await restoreIndex(session, prefix, paths.filter(path => !moved.includes(path)), indexBefore);+ const own = moved.filter(path => !foreign.includes(path));+ const reset = !own.length || await git(session, ['reset', '-q', '--', ...literalPathspecs(own)]).then(out => out.code === 0, () => false);+ const outcome = sha ? { git: 'committed' } : moved.length ? gitFailure('Cannot commit the canonical Git target.',+ `Another commit changed ${moved.join(', ')} while this group of ${commitPaths.length} file(s) was being committed, so nothing was committed and the other commit stays; the effect retries against the new HEAD.`)+ : gitFailure('Cannot commit the canonical Git target.',+ `git commit failed for ${commitPaths.length} file(s); a missing Git identity (user.name, user.email) in that checkout is one cause to check.`);+ for (const c of changed) results.set(c.requested, reset && superseded.includes(c.path) ? { git: 'superseded', reason: 'committed_by_another_writer' } : outcome);});return results;}@@ -488,17 +499,46 @@ export function stripspace(paragraph: string): string {* staged blob's id; an outside edit that reached the index first fails that, and the commit is scanned. An index that* no longer holds what was staged (an entry for a file that is gone, none for one on disk), or a tree that equals* HEAD's, fails the commit, so the effect retries rather than reporting a commit of the wrong state.+ *+ * GST-2552: the staged blobs are laid only over a HEAD that holds every path as `baseline` (the HEAD the group staged+ * against) did, with the checkout's index still holding what was staged. A commit that landed since and touched a path,+ * or an index entry someone else replaced, means the staged blob is stale for that path: nothing is committed (Git+ * stays the parent, so the other commit stays) and the path is `moved`. A path the other commit changed whose file no+ * longer holds the staged bytes was superseded by it, and the next sync imports its version; the rest fail and retry.*/+interface EffectCommit { sha: string | null; moved: string[]; superseded: string[]; foreign: string[] }+export const effectSeams: { beforeUpdateRef?: (root: string, attempt: number) => Promise<void> | void } = {};+async function effectCommit(session: GitSession, root: string, prefix: string, scratch: string, paths: string[],- wanted: ReadonlyMap<string, string | null>, paragraphs: string[], noted: ReadonlyMap<string, GitCommitNote>): Promise<string | null> {+ wanted: ReadonlyMap<string, string | null>, paragraphs: string[], noted: ReadonlyMap<string, GitCommitNote>, baseline: string | null): Promise<EffectCommit> {+ const failed: EffectCommit = { sha: null, moved: [], superseded: [], foreign: [] };+ const indexed = async () => {+ const listed = await git(session, ['ls-files', '-s', '-z', '--', ...literalPathspecs(paths)]);+ return listed.code !== 0 ? null : new Map(listed.stdout.split('\0').filter(Boolean).map(entry => {+ const [mode, oid, stage] = entry.slice(0, entry.indexOf('\t')).split(' ');+ return [entry.slice(entry.indexOf('\t') + 1), { mode: mode!, oid: oid!, stage: stage! }] as const;+ }));+ };+ // ls-tree reads the paths from the working directory (root) and prints them from the toplevel (prefix).+ const committed = async (commit: string | null) => {+ if (!commit) return new Map<string, string>();+ const tree = await git(session, ['ls-tree', '-r', '--full-name', '-z', commit, '--', ...literalPathspecs(paths)]);+ return tree.code !== 0 ? null : new Map(tree.stdout.split('\0').filter(Boolean)+ .map(record => [record.slice(record.indexOf('\t') + 1 + prefix.length), record.slice(0, record.indexOf('\t'))] as const));+ };const format = await git(session, ['rev-parse', '--show-object-format']);- const listed = await git(session, ['ls-files', '-s', '-z', '--', ...literalPathspecs(paths)]);- if (format.code !== 0 || listed.code !== 0) return null;- const staged = new Map(listed.stdout.split('\0').filter(Boolean).map(entry => {- const [mode, oid, stage] = entry.slice(0, entry.indexOf('\t')).split(' ');- return [entry.slice(entry.indexOf('\t') + 1), { mode: mode!, oid: oid!, stage: stage! }] as const;- }));- if ([...staged.values()].some(entry => entry.stage !== '0') || paths.some(path => staged.has(path) !== existsSync(join(root, path)))) return null;+ const staged = await indexed();+ if (format.code !== 0 || !staged) return failed;+ if ([...staged.values()].some(entry => entry.stage !== '0') || paths.some(path => staged.has(path) !== existsSync(join(root, path)))) return failed;+ const entry = (record?: { mode: string; oid: string; stage: string }) => record && `${record.mode} ${record.oid} ${record.stage}`;+ // Whether the file still holds what the effect staged: its recorded sha256 when known (clean filters change the blob id).+ const holds = (path: string) => {+ const record = staged.get(path), hash = wanted.get(path);+ if (!record) return !existsSync(join(root, path));+ if (!record.mode.startsWith('100')) return true;+ const digests = fileDigests(join(root, path), format.stdout.trim());+ return !!digests && (hash ? digests.startsWith(`${hash} `) : digests.endsWith(` ${record.oid}`));+ };const verified = paths.every(path => {const hash = wanted.get(path), entry = staged.get(path);if (hash === undefined || hash === '') return false;@@ -508,8 +548,21 @@ async function effectCommit(session: GitSession, root: string, prefix: string, sconst supersedes = supersededBy(trailers), adopted = adoptedBy(trailers);const message = commitMessageArgs(paragraphs, scratch);for (let attempt = 0; ; attempt++) {- const before = await git(session, ['rev-parse', '--verify', '--quiet', 'HEAD']);- const head = before.code === 0 ? before.stdout.trim() : null;+ // The first attempt builds on the HEAD the group staged against; a later one reads the moved HEAD again.+ const before = attempt ? await git(session, ['rev-parse', '--verify', '--quiet', 'HEAD']) : null;+ const head = before ? before.code === 0 ? before.stdout.trim() : null : baseline;+ if (head !== baseline) {+ const base = await committed(baseline), now = await committed(head), index = await indexed();+ if (!base || !now || !index) return failed;+ // A commit that already holds the staged entry (a commit -a that swept it in) changed nothing this effect would lose.+ const committedAway = (path: string) => {+ const record = staged.get(path);+ return now.get(path) !== base.get(path) && now.get(path) !== (record && `${record.mode} blob ${record.oid}`);+ };+ const foreign = paths.filter(path => entry(index.get(path)) !== entry(staged.get(path)));+ const moved = paths.filter(path => foreign.includes(path) || committedAway(path));+ if (moved.length) return { sha: null, moved, superseded: moved.filter(path => committedAway(path) && !holds(path)), foreign };+ }await recordIntentAsync(root, { writer: 'effect', head_before: head, message: paragraphs.join('\n\n') });// update-index reads --cacheinfo paths from the toplevel and --force-remove paths from the working directory (root).const index: GitSession = { ...session, indexFile: join(scratch, `index-${attempt}`) };@@ -517,22 +570,23 @@ async function effectCommit(session: GitSession, root: string, prefix: string, sconst entries = paths.flatMap(path => { const entry = staged.get(path); return entry ? ['--cacheinfo', `${entry.mode},${entry.oid},${prefix}${path}`] : []; });if ((await git(index, head ? ['read-tree', head] : ['read-tree', '--empty'])).code !== 0|| entries.length && (await git(index, ['update-index', '--add', ...entries])).code !== 0- || removals.length && (await git(index, ['update-index', '--force-remove', '--', ...removals])).code !== 0) return null;+ || removals.length && (await git(index, ['update-index', '--force-remove', '--', ...removals])).code !== 0) return failed;const tree = await git(index, ['write-tree']);- const base = head ? await git(session, ['rev-parse', '--verify', '--quiet', `${head}^{tree}`]) : null;- if (tree.code !== 0 || base && (base.code !== 0 || base.stdout.trim() === tree.stdout.trim())) return null;+ const parent = head ? await git(session, ['rev-parse', '--verify', '--quiet', `${head}^{tree}`]) : null;+ if (tree.code !== 0 || parent && (parent.code !== 0 || parent.stdout.trim() === tree.stdout.trim())) return failed;const commit = await git(session, ['commit-tree', tree.stdout.trim(), ...(head ? ['-p', head] : []), ...message]);- if (commit.code !== 0) return null;+ if (commit.code !== 0) return failed;const created = commit.stdout.trim();await recordCommitAsync(root, { sha: created, writer: 'effect', ...(verified ? { verified: true } : {}),...(supersedes.length ? { supersedes } : {}), ...(adopted.length ? { adopted } : {}) });- const moved = await git(session, ['update-ref', '-m', `commit: ${paragraphs[0]!.split('\n')[0]}`, 'HEAD', created, head ?? '']);- if (moved.code === 0) {+ await effectSeams.beforeUpdateRef?.(root, attempt);+ const updated = await git(session, ['update-ref', '-m', `commit: ${paragraphs[0]!.split('\n')[0]}`, 'HEAD', created, head ?? '']);+ if (updated.code === 0) {const detached = await git(session, ['maintenance', 'run', '--auto', '--quiet', '--detach']).catch(() => null);if (detached?.code === 129) await git(session, ['maintenance', 'run', '--auto', '--quiet']).catch(() => undefined);- return created;+ return { sha: created, moved: [], superseded: [], foreign: [] };}- if (attempt) return null;+ if (attempt) return failed;}}diff --git a/src/core/persistence/git-door.ts b/src/core/persistence/git-door.tsindex 8fdd19370f..95fd4b8851 100644--- a/src/core/persistence/git-door.ts+++ b/src/core/persistence/git-door.ts@@ -11,7 +11,9 @@* `hash-object -w -t tree`, which refuses a malformed tree), `commit-tree` writes the commit with the porcelain path's* message (`commitMessage`), and `update-ref <branch> <new> <old>` moves the branch only if it still names the HEAD the* commit was built on. A branch that moved meanwhile (a commit made in the checkout) is read again and the commit- * rebuilt once; a second move refuses, so the publication rolls back. The checkout's index then gets exactly the+ * rebuilt once; a second move refuses, so the publication rolls back. GST-2552: a move that changed a target (a commit+ * of the same file) refuses `git_target_changed` instead, since the planned blob was hashed before that commit and would+ * silently revert it. The checkout's index then gets exactly the* published entries (`update-index --cacheinfo` / `--force-remove`): `git status` shows them clean, a later commit from* that index keeps them, and every other entry, staged or not, is left as it was.*@@ -206,6 +208,12 @@ async function doorCommit(session: GitSession, root: string, files: DoorFile[],target.note ??= file.note;targets.set(rel, target);}+ const start = await git(session, ['rev-parse', '-q', '--verify', `${branch}^{commit}`]);+ const committed = async (commit: string) => {+ const tree = await git(session, ['ls-tree', '-r', '--full-name', '-z', commit, '--', ...literalPathspecs([...targets.keys()])]);+ if (tree.code !== 0) throw gitFailure('Cannot read the canonical Git tree.', `git ls-tree could not read ${commit}.`);+ return new Map(tree.stdout.split('\0').filter(Boolean).map(record => [record.slice(record.indexOf('\t') + 1), record.slice(0, record.indexOf('\t'))] as const));+ };refuseChanged(root, targets.values());const present = [...targets.values()].filter(target => existsSync(join(root, target.rel)));if (present.length) {@@ -222,10 +230,19 @@ async function doorCommit(session: GitSession, root: string, files: DoorFile[],const scratch = mkdtempSync(join(temp, 'commit-'));try {for (let attempt = 0; ; attempt++) {- const resolved = await git(session, ['rev-parse', '-q', '--verify', `${branch}^{commit}`]);+ // The first attempt builds on the HEAD read before the files were hashed; a later one reads the moved branch again.+ const resolved = attempt ? await git(session, ['rev-parse', '-q', '--verify', `${branch}^{commit}`]) : start;if (resolved.code !== 0) throw opError('git_branch_unborn', `Branch ${branch} in ${root} has no commit yet, so the strict source's write has nothing to commit on; nothing was published.`,'Make the first commit on the branch with git on the brain host, then retry the write with a new request_id.');const head = resolved.stdout.trim();+ if (head !== start.stdout.trim()) {+ const [before, now] = [await committed(start.stdout.trim()), await committed(head)];+ const moved = [...targets.values()].filter(target => {+ const key = `${prefix}${target.rel}`, entry = now.get(key);+ return entry !== before.get(key) && (target.oid ? !entry?.endsWith(` ${target.oid}`) : entry !== undefined);+ }).map(target => target.rel);+ if (moved.length) throw targetChanged(root, moved, 'changed in a commit that landed while this write was being committed');+ }// The directories along every target path, read from HEAD level by level; only the named entries are decoded.const want = new Map<string, string[]>([['', []]]);@@ -365,8 +382,11 @@ export function indexRestoredNotice(paths: string[]): Notice {/** The targets whose file no longer holds its planned state (its bytes, or no file for a removal) refuse as `git_target_changed`. */function refuseChanged(root: string, targets: Iterable<Target>): void {const moved = [...targets].filter(target => { try { return hashFile(join(root, target.rel)) !== target.planned; } catch { return true; } }).map(target => target.rel);- if (!moved.length) return;- throw opError('git_target_changed', `${moved.join(', ')} in ${root} no longer holds the bytes this write published, so nothing was committed and the write was not published.`,+ if (moved.length) throw targetChanged(root, moved, 'no longer holds the bytes this write published');+}++function targetChanged(root: string, moved: string[], what: string): OperationError {+ return opError('git_target_changed', `${moved.join(', ')} in ${root} ${what}, so nothing was committed and the write was not published.`,'Another process changed the file during the write. If the file now holds neither this write\'s bytes nor the version before it, recovery leaves it as it is and holds the write, so the source\'s later writes wait: preview with gbrain repair stuck-recovery --json, ask the user which version to keep, then apply that choice with the --keep, --expect and --apply --yes flags the preview lists (--keep disk keeps the edit). If it holds the version before the write, nothing is held and the write failed; read the page before resubmitting it with a new request_id.', { detail: moved.join(',') });}diff --git a/test/effect-commit-moved-head.test.ts b/test/effect-commit-moved-head.test.tsnew file mode 100644index 0000000000..ba2af6792d--- /dev/null+++ b/test/effect-commit-moved-head.test.ts@@ -0,0 +1,219 @@+/**+ * GST-2552: a Git effect's commit whose branch moves under it never rebuilds its staged blobs over the commit that+ * moved it. The crash robot's `sync_and_connector_race_direct_write` interleaving, forced: a put_page's Git effect+ * stages its file and reads HEAD, then someone else writes the same file, stages it and commits it, then the effect's+ * `update-ref` finds HEAD moved. Before the fix the retry laid the old staged blob over the new HEAD, so HEAD silently+ * went back to the effect's bytes, the next sync imported nothing and the index kept the other writer's version staged.+ *+ * Protects: the other commit stays HEAD (Git is the parent), the effect completes `superseded` instead of committing+ * stale content, the next sync imports the other writer's edit, the checkout is clean, a later write of the page+ * commits, and the reference model finds no lost write.+ * Fails when: the effect's retry reuses a blob staged before the other commit, or reports the moved path committed.+ * The commitGitTargets cases pin each kind of move: a porcelain commit of the same file (superseded), a plumbing commit+ * that leaves the effect's bytes on disk (retried, then committed on the new HEAD), one that also changed the file+ * (superseded, index back to HEAD), a group with an untouched member (retried alone) and a removal the other commit+ * undid (superseded).+ * Seams: a `git` shim on PATH that makes the other commit just before the effect's first `update-ref`, and+ * `effectSeams.beforeUpdateRef` for the commitGitTargets cases; real repositories and real engines (PGLite, and+ * Postgres when DATABASE_URL is set).+ */+import { afterEach, describe, expect, test } from 'bun:test';+import { execFileSync } from 'node:child_process';+import { chmodSync, existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, unlinkSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { ReferenceModel } from '../scripts/persistence/model.ts';+import { crossBoundarySequences } from '../scripts/persistence/generator.ts';+import { ROBOT_TOPOLOGY } from '../scripts/persistence/robot-driver.ts';+import { drain, runSteps } from '../scripts/persistence/crash-robot.ts';+import { contextFor, type OpObservation } from '../scripts/persistence/ops.ts';+import { operationsByName } from '../src/core/operations.ts';+import { OperationError } from '../src/core/ops/contract.ts';+import { gitChildEnv } from '../src/core/git-env.ts';+import { commitGitTargets, effectSeams } from '../src/core/persistence/effect-git.ts';+import { prepareTopology } from '../scripts/persistence/history-fixture.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { isolatedSharedSkillsEngine } from './helpers/shared-skills-engine.ts';+import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';+import { withEnv } from './helpers/with-env.ts';++const realGit = Bun.spawnSync(['which', 'git']).stdout.toString().trim();++async function robotBrain<T>(url: string | undefined, path: string, run: (brain: Awaited<ReturnType<typeof prepareTopology>>) => Promise<T>): Promise<T> {+ const home = mkdtempSync(join(tmpdir(), 'gbrain-moved-head-'));+ try {+ return await withEnv({ GBRAIN_HOME: home, DATABASE_URL: undefined, GBRAIN_DATABASE_URL: undefined, PATH: path }, async () => {+ const { engine, close } = await isolatedSharedSkillsEngine(url);+ try { return await run(await prepareTopology(engine, { sources: 2, worktrees: 2, root: join(home, 'checkouts'), prefix: 'robot', connector: true })); }+ finally { await disposePersistenceConsumer(engine); await close(); }+ });+ } finally { rmSync(home, { recursive: true, force: true }); }+}++for (const backend of testBackends()) for (const forced of [false, true]) {+ test(`${backend}: a commit of the same file ${forced ? 'between the effect\'s HEAD read and its update-ref' : 'after the effect'} wins and the next sync imports it`, async () => {+ const url = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;+ const schedule = crossBoundarySequences(ROBOT_TOPOLOGY).find(s => s.label === 'sync_and_connector_race_direct_write')!;+ const [put, sync] = ['sy0', 'sy1'].map(id => schedule.ops.find(d => d.id === id)!);+ const bin = mkdtempSync(join(tmpdir(), 'gbrain-moved-head-bin-'));+ const armed = join(bin, 'armed'), root = join(bin, 'root'), userContent = join(bin, 'user.md');+ writeFileSync(userContent, String(sync.args.content));+ writeFileSync(join(bin, 'git'), `#!/bin/bash+if [ -e "${armed}" ]; then+ for a in "$@"; do if [ "$a" = update-ref ]; then+ r="$(cat "${armed}")"; rm -f "${armed}"+ cp "${userContent}" "$r/notes/alpha.md"+ ${realGit} -C "$r" add -- notes/alpha.md >&2+ ${realGit} -C "$r" -c user.name=U -c [email protected] commit -q -m 'edit notes/alpha' -- notes/alpha.md >&2+ break+ fi; done+fi+exec ${realGit} "$@"+`);+ chmodSync(join(bin, 'git'), 0o755);+ try {+ await robotBrain(url, `${bin}:${process.env.PATH}`, async ({ world }) => {+ const dir = world.roots![sync.source]!;+ const g = (...a: string[]) => Bun.spawnSync([realGit, '-C', dir, ...a]).stdout.toString();+ if (forced) writeFileSync(armed, dir);+ const model = new ReferenceModel(world);+ world.descriptors = new Map(schedule.ops.map(d => [d.id, d]));+ await runSteps(world, model, { ...schedule, ops: [put], groups: [] });+ expect(await drain(world.engine, world)).toBe(true);+ expect(existsSync(armed)).toBe(false);+ if (!forced) {+ writeFileSync(join(dir, 'notes/alpha.md'), String(sync.args.content));+ g('add', '--', 'notes/alpha.md'); g('-c', 'user.name=U', '-c', '[email protected]', 'commit', '-q', '-m', 'edit notes/alpha', '--', 'notes/alpha.md');+ }+ expect(g('log', '-1', '--format=%s').trim()).toBe('edit notes/alpha');+ expect(g('show', 'HEAD:./notes/alpha.md')).toContain('mk-sy1');+ expect(g('status', '--porcelain', '--', 'notes/alpha.md')).toBe('');+ const [effect] = await world.engine.executeRaw<{ outcome: Record<string, unknown> | string }>(+ `SELECT e.outcome FROM persistence_effects e JOIN persistence_requests r ON r.id=e.request_id WHERE r.request_id=$1 AND e.kind='git'`, [put.requestId]);+ const outcome = typeof effect!.outcome === 'string' ? JSON.parse(effect!.outcome) : effect!.outcome;+ expect(outcome.git).toBe(forced ? 'superseded' : 'committed');++ const result = await operationsByName.sync_brain.handler(contextFor(world, 'local', sync.source), { source_id: sync.source, no_pull: true, no_embed: true }) as Record<string, unknown>;+ expect(result.modified).toBe(1);+ const [row] = await world.engine.executeRaw<{ t: string }>(`SELECT compiled_truth AS t FROM pages WHERE source_id=$1 AND slug='notes/alpha'`, [sync.source]);+ expect(row!.t).toContain('mk-sy1');+ const seen: OpObservation = { id: sync.id, kind: sync.kind, actor: sync.actor, source: sync.source, requestId: sync.requestId, status: 'committed', values: {}, raw: result };+ model.beginStep(true); await model.observe(sync, seen); await model.settleGroup([sync], [seen]);+ expect(model.violations).toEqual([]);++ const current = await world.engine.readPageSnapshot('notes/alpha', { sourceId: sync.source });+ await operationsByName.put_page.handler(contextFor(world, 'local', sync.source), { source_id: sync.source, slug: 'notes/alpha',+ content: String(put.args.content).replace('mk-sy0', 'mk-after'), expected_revision: current!.revision, request_id: crypto.randomUUID() });+ expect(await drain(world.engine, world)).toBe(true);+ expect(g('show', 'HEAD:./notes/alpha.md')).toContain('mk-after');+ expect(g('status', '--porcelain', '--', 'notes/alpha.md')).toBe('');+ });+ } finally { rmSync(bin, { recursive: true, force: true }); }+ }, 120_000);+}++const env = gitChildEnv();+const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], env }).trim();+const roots: string[] = [];+function repo(files: Record<string, string>): string {+ const root = realpathSync(mkdtempSync(join(tmpdir(), 'gbrain-moved-head-repo-')));+ roots.push(root);+ git(root, 'init', '-q', '-b', 'main');+ git(root, 'config', 'user.name', 'Example Writer'); git(root, 'config', 'user.email', '[email protected]');+ for (const [path, body] of Object.entries(files)) put(root, path, body);+ git(root, 'add', '-A'); git(root, 'commit', '-qm', 'seed');+ return root;+}+function put(root: string, path: string, body: string): void {+ mkdirSync(join(root, path, '..'), { recursive: true });+ writeFileSync(join(root, path), body);+}+/** A commit of `path` holding `body` made without the checkout's index or files, as another tool's plumbing would. */+function plumbingCommit(root: string, path: string, body: string): void {+ const other = { ...env, GIT_INDEX_FILE: join(root, '.git', 'other-index') };+ const run = (args: string[], input?: string) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', env: other, input }).trim();+ run(['read-tree', 'HEAD']);+ run(['update-index', '--add', '--cacheinfo', `100644,${run(['hash-object', '-w', '--stdin'], body)},${path}`]);+ git(root, 'update-ref', 'refs/heads/main', run(['commit-tree', run(['write-tree']), '-p', 'HEAD', '-m', 'another commit']));+}+/** Runs `move` once, just before the next effect commit's first update-ref. */+function before(move: () => void): void {+ effectSeams.beforeUpdateRef = () => { effectSeams.beforeUpdateRef = undefined; move(); };+}+const committedByHand = (root: string, path: string, body: string) => {+ put(root, path, body);+ git(root, 'add', '--', path); git(root, 'commit', '-qm', 'another commit', '--', path);+};++afterEach(() => {+ effectSeams.beforeUpdateRef = undefined;+ for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });+});++describe('commitGitTargets when another commit lands before update-ref', () => {+ test('a porcelain commit of the same file wins: the target is superseded and the checkout is clean', async () => {+ const root = repo({ 'notes/a.md': 'a\n' });+ before(() => committedByHand(root, 'notes/a.md', 'a, by hand\n'));+ put(root, 'notes/a.md', 'a, by gbrain\n');+ const outcome = (await commitGitTargets(root, ['notes/a.md'])).get('notes/a.md');+ expect(outcome).toEqual({ git: 'superseded', reason: 'committed_by_another_writer' });+ expect(git(root, 'log', '-1', '--format=%s')).toBe('another commit');+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('a, by hand');+ expect(git(root, 'status', '--porcelain')).toBe('');+ });++ test('a plumbing commit that leaves the effect\'s bytes on disk is retried, and the retry commits on the new HEAD', async () => {+ const root = repo({ 'notes/a.md': 'a\n' });+ before(() => plumbingCommit(root, 'notes/a.md', 'a, elsewhere\n'));+ put(root, 'notes/a.md', 'a, by gbrain\n');+ const outcome = (await commitGitTargets(root, ['notes/a.md'])).get('notes/a.md');+ expect(outcome).toBeInstanceOf(OperationError);+ expect((outcome as OperationError).code).toBe('git_unavailable');+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('a, elsewhere');+ expect((await commitGitTargets(root, ['notes/a.md'])).get('notes/a.md')).toEqual({ git: 'committed' });+ expect(git(root, 'log', '-2', '--format=%s').split('\n')[1]).toBe('another commit');+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('a, by gbrain');+ });++ test('a plumbing commit that also changed the file supersedes it, and the index goes back to HEAD', async () => {+ const root = repo({ 'notes/a.md': 'a\n' });+ before(() => { plumbingCommit(root, 'notes/a.md', 'a, elsewhere\n'); put(root, 'notes/a.md', 'a, elsewhere\n'); });+ put(root, 'notes/a.md', 'a, by gbrain\n');+ expect((await commitGitTargets(root, ['notes/a.md'])).get('notes/a.md')).toMatchObject({ git: 'superseded' });+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('a, elsewhere');+ expect(git(root, 'status', '--porcelain')).toBe('');+ });++ test('in a group, only the moved file is superseded; the untouched one is put back and commits on retry', async () => {+ const root = repo({ 'notes/a.md': 'a\n', 'notes/b.md': 'b\n' });+ before(() => committedByHand(root, 'notes/a.md', 'a, by hand\n'));+ put(root, 'notes/a.md', 'a, by gbrain\n'); put(root, 'notes/b.md', 'b, by gbrain\n');+ const outcomes = await commitGitTargets(root, ['notes/a.md', 'notes/b.md']);+ expect(outcomes.get('notes/a.md')).toMatchObject({ git: 'superseded' });+ expect(outcomes.get('notes/b.md')).toBeInstanceOf(OperationError);+ expect(git(root, 'diff', '--cached', '--name-only')).toBe('');+ expect((await commitGitTargets(root, ['notes/b.md'])).get('notes/b.md')).toEqual({ git: 'committed' });+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('a, by hand');+ expect(git(root, 'show', 'HEAD:notes/b.md')).toBe('b, by gbrain');+ expect(git(root, 'status', '--porcelain')).toBe('');+ });++ test('a commit that swept in the effect\'s staged file is not a move: the retry finds it unchanged', async () => {+ const root = repo({ 'notes/a.md': 'a\n', 'notes/b.md': 'b\n' });+ before(() => { put(root, 'notes/b.md', 'b, by hand\n'); git(root, 'commit', '-qam', 'another commit'); });+ put(root, 'notes/a.md', 'a, by gbrain\n');+ expect((await commitGitTargets(root, ['notes/a.md'])).get('notes/a.md')).toBeInstanceOf(OperationError);+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('a, by gbrain');+ expect((await commitGitTargets(root, ['notes/a.md'])).get('notes/a.md')).toEqual({ git: 'unchanged' });+ expect(git(root, 'status', '--porcelain')).toBe('');+ });++ test('a removal whose file another commit put back is superseded; the file stays committed', async () => {+ const root = repo({ 'notes/a.md': 'a\n' });+ before(() => committedByHand(root, 'notes/a.md', 'a, put back by hand\n'));+ unlinkSync(join(root, 'notes/a.md'));+ expect((await commitGitTargets(root, ['notes/a.md'])).get('notes/a.md')).toMatchObject({ git: 'superseded' });+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('a, put back by hand');+ expect(git(root, 'status', '--porcelain')).toBe('');+ });+});diff --git a/test/git-door-plumbing-gbra133.test.ts b/test/git-door-plumbing-gbra133.test.tsindex 0c95e4783d..4291ac8a89 100644--- a/test/git-door-plumbing-gbra133.test.ts+++ b/test/git-door-plumbing-gbra133.test.ts@@ -154,6 +154,45 @@ describe('G2 plumbing door commit', () => {expect(git(root, 'status', '--porcelain')).toBe('');});+ for (const how of ['git commit', 'plumbing']) test(`GST-2552: a commit of the same file (${how}) before update-ref refuses git_target_changed; the other commit stays HEAD`, async () => {+ const root = repo({ 'notes/a.md': 'a\n' });+ let human = '';+ doorSeams.beforeUpdateRef = (_root, attempt) => {+ if (attempt) return;+ if (how === 'git commit') {+ put(root, 'notes/a.md', 'a, committed by hand\n');+ git(root, 'add', '--', 'notes/a.md'); git(root, 'commit', '-qm', 'a commit of the same file');+ } else {+ const other = { ...env, GIT_INDEX_FILE: join(root, '.git', 'other-index') };+ const run = (args: string[], input?: string) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', env: other, input }).trim();+ run(['read-tree', 'HEAD']);+ run(['update-index', '--cacheinfo', `100644,${run(['hash-object', '-w', '--stdin'], 'a, committed by hand\n')},notes/a.md`]);+ git(root, 'update-ref', 'refs/heads/main', run(['commit-tree', run(['write-tree']), '-p', 'HEAD', '-m', 'a commit of the same file']));+ }+ human = git(root, 'rev-parse', 'HEAD');+ };+ put(root, 'notes/a.md', 'a, second revision\n');+ const error = await refusal(door(root, 'notes/a.md'));+ expect(error).toMatchObject({ code: 'git_target_changed' });+ expect(git(root, 'rev-parse', 'HEAD')).toBe(human);+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('a, committed by hand');+ });++ test('GST-2552: a commit that swept in the planned file before update-ref is not a change; the write lands on it', async () => {+ const root = repo({ 'notes/a.md': 'a\n', 'other/c.md': 'c\n' });+ let human = '';+ doorSeams.beforeUpdateRef = (_root, attempt) => {+ if (attempt) return;+ put(root, 'other/c.md', 'c, committed by hand\n');+ git(root, 'add', '-A'); git(root, 'commit', '-qm', 'a commit that took every change');+ human = git(root, 'rev-parse', 'HEAD');+ };+ put(root, 'notes/a.md', 'a, second revision\n');+ expect(await door(root, 'notes/a.md')).toBe(human);+ expect(git(root, 'show', 'HEAD:notes/a.md')).toBe('a, second revision');+ expect(git(root, 'status', '--porcelain')).toBe('');+ });+test('a branch that moves again refuses; Git and the index keep the other commits', async () => {const root = repo({ 'notes/a.md': 'a\n' });const seed = git(root, 'rev-parse', 'HEAD:notes/a.md');diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txtindex 8844966e9f..4b86f8c66f 100644--- a/test/postgres-unit-arms.txt+++ b/test/postgres-unit-arms.txt@@ -92,6 +92,7 @@ test/doctor-routing-skill-pack-source.test.tstest/doctor-strict-facts-outside-git.test.tstest/doctor-sync-freshness-skill-pack-6076.test.tstest/edge-whitespace-twins.test.ts+test/effect-commit-moved-head.test.tstest/embed-once-per-content-version.serial.test.tstest/embed-stale-images-managed-6529.test.tstest/embedding-completion-atomic.serial.test.ts