Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Git effects: one file git add refuses no longer parks its whole group, and a failed step leaves nothing staged
garrytan36da58e8d0a8
diff --git a/src/core/cli-flag-registry.generated.ts b/src/core/cli-flag-registry.generated.tsindex 25377b4e16..c44a942a23 100644--- a/src/core/cli-flag-registry.generated.ts+++ b/src/core/cli-flag-registry.generated.ts@@ -73,7 +73,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {'founder': ['--brain', '--help', '--json', '--mcp-only', '--request-id', '--since', '--source', '--until'],'friction': ['--agent', '--base', '--brain', '--compare', '--help', '--hint', '--json', '--kind', '--message', '--no-redact', '--phase', '--run-id', '--severity', '--source', '--transcript-path', '--transcripts'],'frontmatter': ['--allow-catch-all', '--batch', '--brain', '--cached', '--diff-filter', '--dry-run', '--exclude-standard', '--fix', '--force', '--full-name', '--get', '--git-common-dir', '--help', '--importable', '--include-ambiguous', '--include-catch-all', '--json', '--literal-pathspecs', '--local', '--name-only', '--no-verify', '--others', '--path', '--show-prefix', '--show-toplevel', '--source', '--stage', '--staged', '--stdin', '--type', '--uninstall'],- 'git': ['--abbrev-ref', '--absolute-git-dir', '--add', '--aggressive', '--all', '--allow-outside-source-root', '--autostash', '--base', '--batch-check', '--brain', '--branch', '--cached', '--cacheinfo', '--cherry-mark', '--count', '--dry-run', '--enable', '--error-unmatch', '--exit-code', '--expect-remote', '--ff-only', '--file', '--force', '--force-remove', '--force-with-lease', '--format', '--get', '--get-all', '--get-regexp', '--get-url', '--git-common-dir', '--git-dir', '--git-path', '--hard', '--help', '--index-info', '--install', '--is-ancestor', '--is-inside-work-tree', '--json', '--keep-checkout', '--local', '--local-env-vars', '--max-count', '--merge-base', '--message', '--mode', '--name-only', '--name-status', '--no-assume-unchanged', '--no-autostash', '--no-commit-id', '--no-cron', '--no-gpg-sign', '--no-merges', '--no-optional-locks', '--no-recurse-submodules', '--no-renames', '--no-skip-worktree', '--no-tags', '--no-verify', '--no-write-fetch-head', '--not', '--only', '--parents', '--pat-file', '--path', '--path-format', '--pending', '--porcelain', '--pull', '--push', '--push-only', '--quiet', '--rebase', '--repo', '--require-clean', '--resolve', '--reverse', '--right-only', '--root', '--scan-secrets', '--short', '--show-toplevel', '--source', '--source-id', '--staged', '--stat', '--stdin', '--topo-order', '--type', '--uninstall', '--unset', '--unset-all', '--untracked-files', '--verify', '--worktree', '--write-tree'],+ 'git': ['--abbrev-ref', '--absolute-git-dir', '--add', '--aggressive', '--all', '--allow-outside-source-root', '--autostash', '--base', '--batch-check', '--brain', '--branch', '--cached', '--cacheinfo', '--cherry-mark', '--count', '--dry-run', '--enable', '--error-unmatch', '--exit-code', '--expect-remote', '--ff-only', '--file', '--force', '--force-remove', '--force-with-lease', '--format', '--get', '--get-all', '--get-regexp', '--get-url', '--git-common-dir', '--git-dir', '--git-path', '--hard', '--help', '--index-info', '--install', '--is-ancestor', '--is-inside-work-tree', '--json', '--keep-checkout', '--local', '--local-env-vars', '--max-count', '--merge-base', '--message', '--mode', '--name-only', '--name-status', '--no-assume-unchanged', '--no-autostash', '--no-commit-id', '--no-cron', '--no-gpg-sign', '--no-merges', '--no-optional-locks', '--no-recurse-submodules', '--no-renames', '--no-skip-worktree', '--no-tags', '--no-verify', '--no-write-fetch-head', '--not', '--only', '--parents', '--pat-file', '--path', '--path-format', '--pending', '--porcelain', '--pull', '--push', '--push-only', '--quiet', '--rebase', '--repo', '--request-id', '--require-clean', '--resolve', '--reverse', '--right-only', '--root', '--scan-secrets', '--short', '--show-toplevel', '--source', '--source-id', '--staged', '--stat', '--stdin', '--topo-order', '--type', '--uninstall', '--unset', '--unset-all', '--untracked-files', '--verify', '--worktree', '--write-tree'],'google': ['--access', '--account', '--allow-create-db', '--allow-docker', '--brain', '--calendar-id', '--chat-model', '--client-id', '--client-json', '--client-secret', '--code', '--consent-state', '--content-root', '--db-only', '--embedding-dimensions', '--embedding-model', '--expansion-model', '--force', '--git', '--grant-types', '--help', '--history-days', '--http', '--issuer-url', '--json', '--key', '--kind', '--limit', '--local-postgres', '--mcp-only', '--mcp-url', '--migrate-only', '--model', '--no-browser', '--no-embedding', '--no-git', '--no-probe', '--non-interactive', '--oauth-client-id', '--oauth-client-secret', '--paste', '--path', '--pglite', '--plan', '--port', '--prefer-postgres', '--probe', '--provenance', '--purge-client', '--query', '--reauth', '--retry-failed', '--schema-pack', '--scopes', '--search-mode', '--services', '--skip-embed-check', '--source', '--status', '--supabase', '--surface', '--sync-budget-ms', '--timeout-ms', '--to', '--token-command', '--url', '--version', '--via', '--yes'],'graph-query': ['--brain', '--depth', '--direction', '--help', '--hop', '--include-foreign', '--json', '--mcp-only', '--request-id', '--source', '--type'],'hook': ['--allow-unverified-remote', '--auto', '--batch-check', '--brain', '--branch', '--cached', '--catalog', '--count', '--diff-filter', '--dir', '--enable', '--end-of-options', '--env', '--gbrain-bin', '--get', '--harness', '--help', '--instructions', '--instructions-only', '--jq', '--json', '--limit', '--name-only', '--no-instructions', '--only', '--others', '--pat-file', '--path', '--porcelain', '--print', '--pure', '--quiet', '--remove', '--scope', '--seat', '--session', '--show-current', '--show-toplevel', '--since', '--source', '--source-ids', '--verify', '--workspace', '--yes'],diff --git a/src/core/git/commit-push.ts b/src/core/git/commit-push.tsindex 6147b50f93..b173f2f65f 100644--- a/src/core/git/commit-push.ts+++ b/src/core/git/commit-push.ts@@ -470,7 +470,7 @@ function flaggedChanges(root: string, listed: string[]): SkippedPath[] {* is left out (its commit lives in the nested repository, which hash-object cannot read). Ignored files under a named* directory are left out as git leaves them; a named ignored file is refused by `git add` itself.*/-function unstagedPaths(root: string, rel: string[], listed: string[]): SkippedPath[] {+function unstagedPaths(root: string, rel: string[], listed: string[], dirtyBefore: Map<string, string>, sha: string | null): SkippedPath[] {const skipped = new Map(flaggedChanges(root, listed).map(s => [s.path, s.reason]));for (const path of unindexedFiles(root, rel, listed)) if (!skipped.has(path)) skipped.set(path, 'not_staged');// Only a named directory can hide an unstaged change from the two checks above (a nested repository inside it).@@ -480,14 +480,31 @@ function unstagedPaths(root: string, rel: string[], listed: string[]): SkippedPaconst parents = path.split('/').slice(0, -1).map((_, i, parts) => parts.slice(0, i + 1).join('/'));if (!skipped.has(path) && parents.some(parent => existsSync(join(root, parent, '.git')))) skipped.set(path, 'not_staged');}- const status = dirs.length ? gitOk(root, ['--no-optional-locks', 'status', '--porcelain=v1', '-z', '--untracked-files=all', '--no-renames', '--', ...literalPathspecs(dirs)], 'status') : '';- for (const entry of status.split('\0').filter(Boolean)) {- const path = entry.slice(3);- if ((entry[1] !== ' ' || entry.startsWith('??')) && !skipped.has(path)) skipped.set(path, 'not_staged');+ // GST-2128: an entry that was clean before the add, or a tracked one this commit carried, is another writer's later change, not a skip.+ const status = directoryStatus(root, dirs);+ const carried = sha && status.size ? committedModes(root, sha) : new Map<string, string>();+ for (const [path, xy] of status) {+ if (xy[1] === ' ' || skipped.has(path) || !dirtyBefore.has(path)) continue;+ if (xy !== '??' && carried.has(path) && carried.get(path) !== '160000') continue;+ skipped.set(path, 'not_staged');}return [...skipped].map(([path, reason]) => ({ path, reason })).sort((a, b) => a.path.localeCompare(b.path));}+/** The `git status` entries under the named paths that are directories, as path (a nested repository keeps its trailing /) to XY code. */+function directoryStatus(root: string, rel: string[]): Map<string, string> {+ const dirs = rel.filter(path => { try { return lstatSync(join(root, path)).isDirectory(); } catch { return false; } });+ const status = dirs.length ? gitOk(root, ['--no-optional-locks', 'status', '--porcelain=v1', '-z', '--untracked-files=all', '--no-renames', '--', ...literalPathspecs(dirs)], 'status') : '';+ return new Map(status.split('\0').filter(Boolean).map(entry => [entry.slice(3), entry.slice(0, 2)]));+}++/** The paths commit `sha` changed, each with its new mode (160000 for a gitlink, 000000 for a removal). */+function committedModes(root: string, sha: string): Map<string, string> {+ const fields = gitOk(root, ['diff-tree', '--root', '-r', '--no-renames', '--no-commit-id', '-z', sha], 'diff-tree').split('\0');+ const modes = new Map<string, string>();+ for (let i = 0; i + 1 < fields.length; i += 2) if (fields[i]!.startsWith(':')) modes.set(fields[i + 1]!, fields[i]!.split(' ')[1]!);+ return modes;+}/** Named paths that are files on disk yet absent from the index (`listed`: `ls-files -v` after `git add -A`), as a file inside a nested repository is. */function unindexedFiles(root: string, rel: string[], listed: string[]): string[] {@@ -588,13 +605,14 @@ function commitPaths(root: string, rel: string[], message: string, scope: Commitif (swept.length) {const list = `${swept.slice(0, 10).join(', ')}${swept.length > 10 ? ', ...' : ''}`;throw opError('commit_prestaged_paths', `${swept.length} file(s) under the named path(s) were already staged by another writer: ${list}.`,- 'Nothing was staged or committed. If they are yours, name each file explicitly; otherwise leave them to the writer that staged them, or unstage them with the user\'s agreement (git restore --staged -- <file>; the working tree keeps its bytes).',+ 'Nothing was staged or committed. If they are yours, name each file explicitly; otherwise leave them to the writer that staged them, or unstage them with the user\'s agreement (git restore --staged -- <file>; the working tree keeps its bytes). Files a parked gbrain Git effect left staged are gbrain\'s own: gbrain sources writer status lists the parked effects with their source and request id, and gbrain sources writer retry-effects with that source and --request-id commits them.',{ detail: JSON.stringify({ paths: swept }), fix: { ask_user: true, consent: [], actor: 'user', requires_exclusive: false,argv: ['git', '-C', root, 'restore', '--staged', '--', ...swept],why: 'Another writer staged these files; committing them would publish whatever that writer had in progress, and unstaging them drops its staging.',user_message: `Files already staged by another writer sit under the path you asked to commit (${list}). Commit only the files you name, or unstage those (their bytes stay on disk)?`,docs: 'docs/guides/shared-checkout-git.md#outcomes' } });}+ const dirtyBefore = directoryStatus(root, rel);const before = indexEntries(root, rel);// GST-2118: git add refuses a named skip-worktree file that is on disk; it is left to the skipped-path check, so the other paths still commit.const sparse = new Set(gitOk(root, ['ls-files', '-v', '-z', '--', ...literalPathspecs(rel)], 'ls-files -v').split('\0')@@ -633,7 +651,7 @@ function commitPaths(root: string, rel: string[], message: string, scope: Commit// GST-2116: after the try, so a failure from here on never restores the index under a commit that landed.const sha = landed.committed ? gitOk(root, ['rev-parse', 'HEAD'], 'rev-parse HEAD') : null;if (sha) recordCommit(root, { sha, writer: 'commit_push' });- return { sha, skipped: unstagedPaths(root, landed.paths, landed.listed) };+ return { sha, skipped: unstagedPaths(root, landed.paths, landed.listed, dirtyBefore, sha) };}// ── Pending write-through ───────────────────────────────────────────────────diff --git a/src/core/persistence/effect-git.ts b/src/core/persistence/effect-git.tsindex 1d7fa8146d..9b77c74812 100644--- a/src/core/persistence/effect-git.ts+++ b/src/core/persistence/effect-git.ts@@ -62,7 +62,9 @@ export interface GitEffectOwner {onReclaim?: (receipt: IndexLockReclaim) => void;}-export interface GitSession { root: string; hooks: string; signal?: AbortSignal; timeoutMs: number; owner?: GitEffectOwner; indexFile?: string }+export interface GitSession { root: string; hooks: string; signal?: AbortSignal; timeoutMs: number; owner?: GitEffectOwner; indexFile?: string;+ /** GST-2128: puts back the index entries a step staged; withSession runs it when the work throws. */+ undoStaging?: () => Promise<void> }/** #7759: no global --literal-pathspecs (Git would export it to every hook and child); pathspecs pass through literalPathspecs. */const gitArgs = (session: GitSession, args: string[]) =>@@ -218,9 +220,14 @@ async function stageTarget(session: GitSession, relativePath: string): Promise<{async function refuseIgnoredTarget(session: GitSession, relativePath: string): Promise<void> {const status = await git(session, ['status', '--porcelain', '--ignored=matching', '--untracked-files=all', '--', ...literalPathspecs([relativePath])]);if (status.code !== 0 || !status.stdout.startsWith('!! ')) return;+ throw ignoredTargetError(relativePath, await ignoreRule(session, relativePath));+}++/** The ignore rule (`source:line: pattern`) that matches `relativePath`, tracked or not; `.gitignore` when git cannot name it. */+async function ignoreRule(session: GitSession, relativePath: string): Promise<string> {// check-ignore refuses pathspec magic and reads a path literally except a leading ':', which ./ keeps literal; it only names the rule.- const matched = await run(session, ['check-ignore', '-v', '--', relativePath.startsWith(':') ? `./${relativePath}` : relativePath]);- throw ignoredTargetError(relativePath, matched.error ? '.gitignore' : matched.stdout.split('\t')[0]!.replace(/^(.*?):(\d+):/, '$1:$2: '));+ const matched = await run(session, ['check-ignore', '--no-index', '-v', '--', relativePath.startsWith(':') ? `./${relativePath}` : relativePath]);+ return matched.error ? '.gitignore' : matched.stdout.split('\t')[0]!.replace(/^(.*?):(\d+):/, '$1:$2: ');}/** The refusal of a target a .gitignore `rule` (`source:line: pattern`) excludes; `detail` names the rule. */@@ -240,7 +247,8 @@ export async function withSession<T>(root: string, signal: AbortSignal | undefintry {const session: GitSession = { root, hooks, signal, owner, timeoutMs: GIT_STEP_TIMEOUT_MS };if (scaled) session.timeoutMs = await stepTimeoutMs(session);- return await work(session);+ try { return await work(session); }+ catch (error) { await session.undoStaging?.(); throw error; }} finally { rmSync(hooks, { recursive: true, force: true }); }}@@ -310,6 +318,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$/, '') : '';+ 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));const dirty = new Set((await list(['status', '--porcelain', '-z', '--no-renames', '--untracked-files=all'])).split('\0').filter(Boolean).map(entry => entry.slice(3)).map(path => path.startsWith(prefix) ? path.slice(prefix.length) : path));@@ -331,13 +341,37 @@ export async function commitGitTargets(root: string, relativePaths: string[], sielse results.set(requested, { git: 'unchanged' });}if (staged.length) {- const add = await git(session, ['add', '-A', '--', ...literalPathspecs([...new Set(staged)])]);- if (add.code !== 0) throw gitFailure('Cannot stage the canonical Git target.', `git add failed for this group of ${new Set(staged).size} file(s).`);- const diff = await git(session, ['diff', '--cached', '--name-only', '--relative', '-z', '--no-renames', '--', ...literalPathspecs([...new Set(staged)])]);- if (diff.code !== 0) throw gitFailure('Cannot compare the canonical Git target.', `git diff --cached failed for this group of ${new Set(staged).size} file(s).`);+ // GST-2128: tracked paths stage with add -u, which takes a tracked file under a folder an ignore rule covers (plain+ // add stages it yet exits 1) and can never add an untracked file; untracked paths with add -A, which refuses an+ // ignored one. A file either add refuses never parks the group: each file is then staged alone, and a refused one+ // fails by itself (git_target_ignored naming the rule when that is why) with what was staged of it put back.+ const stage = async (paths: string[]) => {+ const kept = paths.filter(path => tracked.has(path)), fresh = paths.filter(path => !tracked.has(path));+ const runs = [...(kept.length ? [['add', '-u', '--', ...literalPathspecs(kept)]] : []), ...(fresh.length ? [['add', '-A', '--', ...literalPathspecs(fresh)]] : [])];+ for (const args of runs) { const out = await git(session, args); if (out.code !== 0) return out; }+ return null;+ };+ let addable = [...new Set(staged)];+ if (await stage(addable)) {+ const refused = new Map<string, OperationError>();+ for (const path of addable) {+ const out = await stage([path]);+ if (out) refused.set(path, /ignored by one of your \.gitignore files/.test(out.stderr) ? ignoredTargetError(path, await ignoreRule(session, path))+ : gitFailure('Cannot stage the canonical Git target.', `git add failed for ${path}.`));+ }+ // Every file refused for a reason other than an ignore rule is git itself failing, not one file: the group fails as before.+ if (refused.size === addable.length && [...refused.values()].every(error => error.code !== 'git_target_ignored')) {+ throw gitFailure('Cannot stage the canonical Git target.', `git add failed for this group of ${addable.length} file(s).`);+ }+ for (const [requested, path] of normalized) if (refused.has(path)) results.set(requested, refused.get(path)!);+ if (refused.size) await restoreIndex(session, prefix, [...refused.keys()], indexBefore);+ addable = addable.filter(path => !refused.has(path));+ }+ const diff = addable.length ? await git(session, ['diff', '--cached', '--name-only', '--relative', '-z', '--no-renames', '--', ...literalPathspecs(addable)]) : { code: 0, stdout: '' };+ if (diff.code !== 0) throw gitFailure('Cannot compare the canonical Git target.', `git diff --cached failed for this group of ${addable.length} file(s).`);const indexed = new Set(diff.stdout.split('\0').filter(Boolean));for (const [requested, path] of normalized) {- if (!staged.includes(path) || results.has(requested)) continue;+ if (!addable.includes(path) || results.has(requested)) continue;if (indexed.has(path)) changed.push({ requested, path });else results.set(requested, { git: 'unchanged' });}@@ -385,10 +419,59 @@ export async function commitGitTargets(root: string, relativePaths: string[], siconst outcome = result.code === 0 ? { 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();});return results;}+interface IndexSnapshot { entries: Map<string, string>; flagged: Map<string, string> }++/** The index records (`ls-files -s`) and assume-unchanged or skip-worktree tags (`ls-files -v`) under `paths`, keyed by repository path. */+async function indexSnapshot(session: GitSession, paths: string[]): Promise<IndexSnapshot> {+ if (!paths.length) return { entries: new Map(), flagged: new Map() };+ const listed = await git(session, ['ls-files', '-s', '--full-name', '-z', '--', ...literalPathspecs(paths)]);+ const tagged = await git(session, ['ls-files', '-v', '--full-name', '-z', '--', ...literalPathspecs(paths)]);+ if (listed.code !== 0 || tagged.code !== 0) throw gitFailure('Cannot inspect the canonical Git index.', `git ls-files failed for this group of ${paths.length} file(s).`);+ const records = (out: string) => out.split('\0').filter(Boolean);+ return {+ entries: new Map(records(listed.stdout).map(record => [record.slice(record.indexOf('\t') + 1), record.slice(0, record.indexOf('\t'))])),+ flagged: new Map(records(tagged.stdout).filter(record => /^[a-zS] /.test(record)).map(record => [record.slice(2), record[0]!])),+ };+}++/**+ * GST-2128: puts the index entries under `paths` back to `before` after an effect group's add or commit failed. `git add`+ * already staged them in the checkout's shared index, and a parked effect that leaves them there makes every later+ * commit-push naming that folder refuse `commit_prestaged_paths`. Only entries that changed and still differ from HEAD+ * are rewritten, so a commit that landed before its step failed is never reversed in the index; a rewritten entry gets+ * its assume-unchanged or skip-worktree flag back. It runs without the session's abort signal, since an aborted step is+ * one it has to clean up after. Best effort: a failure here never hides the step's own.+ */+async function restoreIndex(session: GitSession, prefix: string, paths: string[], before: IndexSnapshot): Promise<void> {+ try {+ session = { ...session, signal: undefined };+ const now = await indexSnapshot(session, paths);+ const local = (path: string) => path.startsWith(prefix) ? path.slice(prefix.length) : path;+ // ls-tree reads its paths literally and relative to the root, like update-index.+ const tree = await git(session, ['ls-tree', '-r', '--full-name', '-z', 'HEAD', '--', ...paths]);+ const head = new Map(tree.code !== 0 ? [] : tree.stdout.split('\0').filter(Boolean).map(record => {+ const [mode, , oid] = record.slice(0, record.indexOf('\t')).split(' ');+ return [record.slice(record.indexOf('\t') + 1), `${mode} ${oid} 0`] as const;+ }));+ const staged = (path: string) => now.entries.get(path) !== head.get(path);+ const wanted = new Set(paths);+ const added = [...now.entries.keys()].filter(path => !before.entries.has(path) && staged(path));+ const changed = [...before.entries].filter(([path, entry]) => wanted.has(local(path)) && now.entries.get(path) !== entry && staged(path));+ if (added.length) await git(session, ['update-index', '--force-remove', '--', ...added.map(local)]);+ const cacheinfo = changed.filter(([, entry]) => entry.endsWith(' 0')).flatMap(([path, entry]) => ['--cacheinfo', `${entry.split(' ')[0]},${entry.split(' ')[1]},${path}`]);+ if (cacheinfo.length) await git(session, ['update-index', '--add', ...cacheinfo]);+ for (const [flag, tags] of [['--skip-worktree', /[sS]/], ['--assume-unchanged', /[a-z]/]] as const) {+ const flagged = changed.map(([path]) => path).filter(path => tags.test(before.flagged.get(path) ?? ''));+ if (flagged.length) await git(session, ['update-index', flag, '--', ...flagged.map(local)]);+ }+ } catch { /* the step's own failure is what the effect reports and retries */ }+}+/*** #7089: whether `root`'s branch has commits its upstream (the local remote-tracking ref, never fetched) lacks; false* without an upstream. A Git effect whose target is absent asks this before completing: its removal may have beendiff --git a/test/effect-git-failed-commit-unstages-gst2128.test.ts b/test/effect-git-failed-commit-unstages-gst2128.test.tsnew file mode 100644index 0000000000..b20288ed7a--- /dev/null+++ b/test/effect-git-failed-commit-unstages-gst2128.test.ts@@ -0,0 +1,173 @@+/**+ * GST-2128: a Git effect whose commit failed left its targets staged in the checkout's shared index, and nothing ever+ * unstaged them. Each parked effect added more, and every `gbrain git commit-push` naming a directory that held one was+ * refused `commit_prestaged_paths`, because the files looked like another writer's work in progress. A failed effect+ * commit now puts the index entries it changed back as they were, so the folder stays committable.+ * Seams: none; the real `commitGitTargets` and `commitAndPush` over a real repository. A lock file on the branch ref+ * makes the effect's commit fail the way a busy or broken ref does, and leaves the index lock free.+ */+import { afterEach, expect, test } from 'bun:test';+import { execFileSync } from 'node:child_process';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { commitAndPush } from '../src/core/git/commit-push.ts';+import { gitChildEnv } from '../src/core/git-env.ts';+import { commitGitTargets } from '../src/core/persistence/effect-git.ts';+import { withEnv } from './helpers/with-env.ts';++const roots: string[] = [];+afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); });+const env = gitChildEnv();+const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], env }).trim();++/** A checkout whose source folder is `brain/` below the repository root, with two committed pages and a note. */+function checkout(): { top: string; root: string } {+ const top = mkdtempSync(join(tmpdir(), 'gbrain-effect-unstage-'));+ roots.push(top);+ git(top, 'init', '-q', '-b', 'main');+ git(top, 'config', 'user.name', 'Example'); git(top, 'config', 'user.email', '[email protected]'); git(top, 'config', 'commit.gpgsign', 'false');+ const root = join(top, 'brain');+ mkdirSync(join(root, 'people'), { recursive: true });+ writeFileSync(join(root, 'people', 'alice-example.md'), 'Alice, first version.\n');+ writeFileSync(join(root, 'people', 'bob-example.md'), 'Bob, first version.\n');+ writeFileSync(join(root, 'notes.md'), 'A note.\n');+ git(top, 'add', '-A'); git(top, 'commit', '-qm', 'seed');+ return { top, root };+}+const index = (top: string) => git(top, 'ls-files', '-s', '-v');+const lockBranch = (top: string) => { const lock = join(top, '.git', 'refs', 'heads', 'main.lock'); writeFileSync(lock, ''); return () => rmSync(lock); };++test("an effect group's failed commit leaves no staged paths: no edit, new file or removal of any target stays staged", async () => {+ const { top, root } = checkout();+ writeFileSync(join(root, 'people', 'alice-example.md'), 'Alice, second version.\n');+ writeFileSync(join(root, 'people', 'carol-example.md'), 'Carol, new.\n');+ rmSync(join(root, 'notes.md'));+ const before = index(top);+ const unlock = lockBranch(top);+ const results = await commitGitTargets(root, ['people/alice-example.md', 'people/carol-example.md', 'notes.md']);+ unlock();+ for (const path of ['people/alice-example.md', 'people/carol-example.md', 'notes.md']) expect(results.get(path)).toMatchObject({ code: 'git_unavailable' });+ expect(git(top, 'diff', '--cached', '--name-only')).toBe('');+ expect(index(top)).toBe(before);+ expect(git(top, 'diff', '--name-only')).toBe('brain/notes.md\nbrain/people/alice-example.md');+ expect(git(top, 'ls-files', '--others', '--exclude-standard')).toBe('brain/people/carol-example.md');+});++test('another writer\'s staging and a flagged entry beside the effect\'s targets are kept', async () => {+ const { top, root } = checkout();+ writeFileSync(join(root, 'people', 'bob-example.md'), 'Bob, staged by another writer.\n');+ git(top, 'add', '--', 'brain/people/bob-example.md');+ git(top, 'update-index', '--skip-worktree', '--', 'brain/notes.md');+ writeFileSync(join(root, 'people', 'alice-example.md'), 'Alice, second version.\n');+ const before = index(top);+ const unlock = lockBranch(top);+ await commitGitTargets(root, ['people/alice-example.md', 'notes.md']);+ unlock();+ expect(index(top)).toBe(before);+ expect(git(top, 'diff', '--cached', '--name-only')).toBe('brain/people/bob-example.md');+});++test('after a parked effect, commit-push naming its folder commits instead of refusing commit_prestaged_paths', async () => {+ const { top, root } = checkout();+ writeFileSync(join(root, 'people', 'alice-example.md'), 'Alice, written by a Git effect.\n');+ const unlock = lockBranch(top);+ expect((await commitGitTargets(root, ['people/alice-example.md'])).get('people/alice-example.md')).toMatchObject({ code: 'git_unavailable' });+ unlock();+ writeFileSync(join(root, 'people', 'dave-example.md'), 'Dave, from an agent job.\n');+ const result = commitAndPush({ repo: root, cwd: root, paths: ['people'], message: 'agent job pages' });+ expect(result.committed).toBeTruthy();+ expect(git(top, 'show', 'HEAD:brain/people/dave-example.md')).toBe('Dave, from an agent job.');+ expect(git(top, 'status', '--porcelain')).toBe('');+});++test('one target tracked under a folder a later ignore rule covers commits with its group instead of parking it', async () => {+ const { top, root } = checkout();+ mkdirSync(join(root, 'archive'));+ writeFileSync(join(root, 'archive', 'carol-example.md'), 'Carol, first version.\n');+ git(top, 'add', '-A'); git(top, 'commit', '-qm', 'archive');+ writeFileSync(join(root, '.gitignore'), 'archive/\n');+ git(top, 'add', '-A'); git(top, 'commit', '-qm', 'ignore the archive folder');+ for (const name of ['people/alice-example.md', 'people/bob-example.md', 'archive/carol-example.md']) writeFileSync(join(root, name), `${name}, second version.\n`);+ const results = await commitGitTargets(root, ['people/alice-example.md', 'archive/carol-example.md', 'people/bob-example.md']);+ for (const name of ['people/alice-example.md', 'people/bob-example.md', 'archive/carol-example.md']) {+ expect(results.get(name)).toEqual({ git: 'committed' });+ expect(git(top, 'show', `HEAD:brain/${name}`)).toBe(`${name}, second version.`);+ }+ expect(git(top, 'status', '--porcelain')).toBe('');+});++/** A git that runs `command` for real and then exits 1 when `when` (a shell test over "$@") holds; every other run is the real git. */+function failingGit(when: string): string {+ const bin = mkdtempSync(join(tmpdir(), 'gbrain-failing-git-')); roots.push(bin);+ const realGit = execFileSync('sh', ['-c', 'command -v git'], { encoding: 'utf8' }).trim();+ writeFileSync(join(bin, 'git'), `#!/bin/sh\nif ${when}; then ${realGit} "$@"; exit 1; fi\nexec ${realGit} "$@"\n`, { mode: 0o755 });+ return `${bin}:${process.env.PATH}`;+}+const has = (word: string) => `case " $* " in *"${word}"*) true ;; *) false ;; esac`;++test('a file git add refuses fails alone: the rest of its group commits, and nothing of it stays staged', async () => {+ const { top, root } = checkout();+ writeFileSync(join(root, 'people', 'alice-example.md'), 'Alice, second version.\n');+ writeFileSync(join(root, 'people', 'bob-example.md'), 'Bob, second version.\n');+ // Stages bob and still exits 1, the way add treats a path it also reports as one it would not add.+ const path = failingGit(`${has(' add ')} && ${has('bob-example.md')}`);+ const results = await withEnv({ PATH: path }, () => commitGitTargets(root, ['people/alice-example.md', 'people/bob-example.md']));+ expect(results.get('people/alice-example.md')).toEqual({ git: 'committed' });+ expect(results.get('people/bob-example.md')).toMatchObject({ code: 'git_unavailable' });+ expect(git(top, 'show', 'HEAD:brain/people/alice-example.md')).toBe('Alice, second version.');+ expect(git(top, 'diff', '--cached', '--name-only')).toBe('');+ expect(git(top, 'diff', '--name-only')).toBe('brain/people/bob-example.md');+});++test('a group step that throws after the add leaves no staged paths', async () => {+ const { top, root } = checkout();+ writeFileSync(join(root, 'people', 'alice-example.md'), 'Alice, second version.\n');+ writeFileSync(join(root, 'people', 'bob-example.md'), 'Bob, second version.\n');+ const path = failingGit(`${has(' diff ')} && ${has(' --cached ')}`);+ await expect(withEnv({ PATH: path }, () => commitGitTargets(root, ['people/alice-example.md', 'people/bob-example.md'])))+ .rejects.toMatchObject({ code: 'git_unavailable' });+ expect(git(top, 'diff', '--cached', '--name-only')).toBe('');+ expect(git(top, 'diff', '--name-only')).toBe('brain/people/alice-example.md\nbrain/people/bob-example.md');+});++test('a commit that lands but reports a failure is not reversed in the index', async () => {+ const { top, root } = checkout();+ const path = failingGit(has(' commit '));+ writeFileSync(join(root, 'people', 'alice-example.md'), 'Alice, second version.\n');+ const results = await withEnv({ PATH: path }, () => commitGitTargets(root, ['people/alice-example.md']));+ expect(results.get('people/alice-example.md')).toMatchObject({ code: 'git_unavailable' });+ expect(git(top, 'show', 'HEAD:brain/people/alice-example.md')).toBe('Alice, second version.');+ expect(git(top, 'status', '--porcelain')).toBe('');+});++test('an untracked target an ignore rule covers is never added: it parks git_target_ignored and its group commits', async () => {+ const { top, root } = checkout();+ writeFileSync(join(root, '.gitignore'), 'private/\n');+ git(top, 'add', '-A'); git(top, 'commit', '-qm', 'ignore the private folder');+ mkdirSync(join(root, 'private'));+ writeFileSync(join(root, 'private', 'dave-example.md'), 'Kept out of Git.\n');+ writeFileSync(join(root, 'people', 'alice-example.md'), 'Alice, second version.\n');+ const results = await commitGitTargets(root, ['people/alice-example.md', 'private/dave-example.md']);+ expect(results.get('people/alice-example.md')).toEqual({ git: 'committed' });+ expect(results.get('private/dave-example.md')).toMatchObject({ code: 'git_target_ignored', detail: expect.stringContaining('private/') });+ expect(git(top, 'ls-files', '--', 'brain/private')).toBe('');+ expect(git(top, 'ls-tree', '-r', '--name-only', 'HEAD', '--', 'brain/private')).toBe('');+ expect(git(top, 'diff', '--cached', '--name-only')).toBe('');+});++test('an untracked target that an ignore rule covers by the time the group adds it is refused alone and never added', async () => {+ const { top, root } = checkout();+ mkdirSync(join(root, 'private'));+ writeFileSync(join(root, 'private', 'dave-example.md'), 'Kept out of Git.\n');+ writeFileSync(join(root, 'people', 'alice-example.md'), 'Alice, second version.\n');+ // The rule lands between the group's status (which listed the file as untracked) and its add.+ const path = failingGit('false');+ writeFileSync(join(path.split(':')[0]!, 'git'), `#!/bin/sh\ncase " $* " in *" add "*) printf 'private/\\n' >> '${join(top, '.git', 'info', 'exclude')}' ;; esac\nexec ${execFileSync('sh', ['-c', 'command -v git'], { encoding: 'utf8' }).trim()} "$@"\n`, { mode: 0o755 });+ const results = await withEnv({ PATH: path }, () => commitGitTargets(root, ['people/alice-example.md', 'private/dave-example.md']));+ expect(results.get('people/alice-example.md')).toEqual({ git: 'committed' });+ expect(results.get('private/dave-example.md')).toMatchObject({ code: 'git_target_ignored', detail: expect.stringContaining('private/') });+ expect(git(top, 'ls-files', '--', 'brain/private')).toBe('');+ expect(git(top, 'ls-tree', '-r', '--name-only', 'HEAD', '--', 'brain/private')).toBe('');+ expect(git(top, 'diff', '--cached', '--name-only')).toBe('');+});diff --git a/test/effect-retry-already-committed-gst2128.test.ts b/test/effect-retry-already-committed-gst2128.test.tsnew file mode 100644index 0000000000..1e6b91a622--- /dev/null+++ b/test/effect-retry-already-committed-gst2128.test.ts@@ -0,0 +1,120 @@+/**+ * GST-2128 / GBRA-398: the recovery for a group of Git effects parked on `git_unavailable` is to commit their files by name and+ * then retry them. A retried effect whose target bytes are already in HEAD must complete as `unchanged` and join the push,+ * not park again; one that still has changes in the same group commits as usual.+ * Seams: real PGLite journal and effect runner, a claimed worktree on a real repository; the operator's commit is plain git.+ */+import { afterAll, beforeAll, expect, test } from 'bun:test';+import { randomUUID } from 'node:crypto';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { dirname, join } from 'node:path';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import type { OperationContext } from '../src/core/ops/contract.ts';+import { claimWorktree, getWorktreeBinding } from '../src/core/persistence/ownership.ts';+import { localHostId, registerLocalWriter } from '../src/core/persistence/identity.ts';+import { submissionAuthority } from '../src/core/persistence/authority.ts';+import { admitWrite, claimNextWrite } from '../src/core/persistence/journal.ts';+import { publishMutation } from '../src/core/persistence/coordinator.ts';+import { runPersistenceEffects } from '../src/core/persistence/effects.ts';+import { retryParkedEffects } from '../src/core/persistence/effect-retry.ts';+import { serializePageToMarkdown } from '../src/core/markdown.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { gitAsync } from './helpers/git-publication.ts';+import { withEnv } from './helpers/with-env.ts';++let engine: PGLiteEngine;+let home: string;+const config = { engine: 'pglite' as const, embedding_disabled: true };+const page = (body: string) => ({ type: 'note', title: 'Example', compiled_truth: body, timeline: '', frontmatter: {} });+beforeAll(async () => {+ engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema();+ home = mkdtempSync(join(tmpdir(), 'gbrain-retry-committed-'));+}, 120_000);+afterAll(async () => { await disposePersistenceConsumer(engine); await engine.disconnect(); rmSync(home, { recursive: true, force: true }); });++async function write(root: string, sourceId: string, slug: string): Promise<{ id: string; requestId: string }> {+ const binding = (await getWorktreeBinding(engine, sourceId, localHostId()))!;+ await engine.putPage(slug, page('Before'), { sourceId });+ const snapshot = (await engine.readPageSnapshot(slug, { sourceId }))!;+ const file = join(root, `${slug}.md`); mkdirSync(dirname(file), { recursive: true });+ writeFileSync(file, serializePageToMarkdown(snapshot.page, snapshot.tags));+ const ctx: OperationContext = { engine, config, remote: false, dryRun: false, sourceId, logger: { info() {}, warn() {}, error() {} } };+ const authority = await submissionAuthority(ctx, 'put_page', sourceId, binding.source_incarnation, slug);+ const requestId = randomUUID();+ const admitted = await admitWrite(engine, { principal: authority.principal, authority, operation: 'put_page', sourceId,+ sourceIncarnation: binding.source_incarnation, slug, pageId: snapshot.page.id, requestId,+ callerIntent: { content: 'After' }, intent: { content: 'After' }, worktreeId: binding.worktree_id, topologyGeneration: binding.topology_generation });+ const row = (await claimNextWrite(engine, localHostId()))!;+ expect(row.id).toBe(admitted.id);+ const result = await publishMutation(engine, row, { observedRevision: snapshot.revision, file: { path: file, root, content: 'After' },+ apply: async tx => { await tx.putPage(slug, page('After'), { sourceId }); return {}; } }, localHostId());+ expect(result.state).toBe('committed');+ await engine.executeRaw(`UPDATE persistence_effects SET state='failed',error_code='targets_parked',+ data=data || $2::text::jsonb WHERE request_id=$1::uuid AND kind='git'`,+ [row.id, JSON.stringify({ parked: [{ slug, error_code: 'git_unavailable' }], target_failures: 5 })]);+ return { id: row.id, requestId };+}+const effect = async (id: string) => (await engine.executeRaw<{ state: string; outcome: Record<string, unknown> | null; error_code: string | null }>(+ "SELECT state,outcome,error_code FROM persistence_effects WHERE request_id=$1::uuid AND kind='git'", [id]))[0];++/** A claimed, durable worktree on a repository that tracks a bare remote; `seed` runs before the first commit. */+async function claimedRepo(name: string, seed: (root: string) => void = () => {}) {+ await registerLocalWriter(engine, 'cli');+ const root = join(home, name); mkdirSync(root);+ await gitAsync(root, 'init', '-q', '-b', 'main');+ await gitAsync(root, 'config', 'user.name', 'Example Writer'); await gitAsync(root, 'config', 'user.email', '[email protected]');+ writeFileSync(join(root, 'initial.md'), 'Initial\n');+ seed(root);+ await gitAsync(root, 'add', '-A'); await gitAsync(root, 'commit', '-q', '-m', 'Initial');+ const remote = join(home, `${name}.git`);+ await gitAsync(home, 'init', '-q', '--bare', '-b', 'main', remote);+ await gitAsync(root, 'remote', 'add', 'origin', remote); await gitAsync(root, 'push', '-q', '-u', 'origin', 'main');+ const sourceId = `retry-${randomUUID().slice(0, 8)}`;+ await engine.executeRaw('INSERT INTO sources(id,name,local_path) VALUES($1,$1,$2)', [sourceId, root]);+ const binding = await claimWorktree(engine, sourceId, root, localHostId());+ await engine.executeRaw("UPDATE persistence_host_bindings SET git_durability='enabled' WHERE worktree_id=$1::uuid", [binding.worktree_id]);+ return { root, remote, sourceId };+}+const isolated = (fn: () => Promise<void>) => () => withEnv({ GBRAIN_HOME: home, GBRAIN_BRAIN_ID: 'host', GBRAIN_SOURCE: undefined, DATABASE_URL: undefined,+ GBRAIN_DATABASE_URL: undefined, GBRAIN_GIT_ALLOW_FILE_TRANSPORT: '1' }, fn);+const runEffects = async () => {+ await engine.executeRaw("UPDATE persistence_effects SET next_attempt_at=now() WHERE kind='git'");+ await runPersistenceEffects(engine, config, { hostId: localHostId(), limit: 10 });+};++test('a retried git effect whose target bytes are already committed completes as unchanged and joins the push, without parking', isolated(async () => {+ const { root, remote, sourceId } = await claimedRepo('brain');+ const committed = await write(root, sourceId, 'people/alice-example');+ const pending = await write(root, sourceId, 'people/bob-example');+ await gitAsync(root, 'add', '--', 'people/alice-example.md');+ await gitAsync(root, 'commit', '-q', '-m', 'Commit the parked page by name', '--', 'people/alice-example.md');+ const head = (await gitAsync(root, 'rev-parse', 'HEAD')).trim();+ for (const w of [committed, pending]) expect(await retryParkedEffects(engine, sourceId, w.requestId, false)).toMatchObject({});+ await runEffects();+ expect(await effect(committed.id)).toMatchObject({ state: 'committed', outcome: { git: 'unchanged', push: 'committed' }, error_code: null });+ expect(await effect(pending.id)).toMatchObject({ state: 'committed', outcome: { git: 'committed', push: 'committed' }, error_code: null });+ expect((await gitAsync(root, 'rev-list', '--count', `${head}..HEAD`)).trim()).toBe('1');+ expect((await gitAsync(root, 'status', '--porcelain')).trim()).toBe('');+ expect((await gitAsync(remote, 'rev-parse', 'main')).trim()).toBe((await gitAsync(root, 'rev-parse', 'HEAD')).trim());+}), 120_000);++test('one retried target tracked under a folder an ignore rule now covers does not park its group, whose other targets are already committed', isolated(async () => {+ // The relabel shape: a page tracked before a later .gitignore rule covered its folder.+ const { root, remote, sourceId } = await claimedRepo('ignored', root => {+ mkdirSync(join(root, 'archive')); writeFileSync(join(root, 'archive', 'carol-example.md'), 'Tracked before the rule.\n');+ });+ writeFileSync(join(root, '.gitignore'), 'archive/\n');+ await gitAsync(root, 'add', '.gitignore'); await gitAsync(root, 'commit', '-q', '-m', 'Ignore the archive folder');+ const writes = [await write(root, sourceId, 'people/alice-example'), await write(root, sourceId, 'people/bob-example'), await write(root, sourceId, 'archive/carol-example')];+ await gitAsync(root, 'add', '--', 'people/'); await gitAsync(root, 'commit', '-q', '-m', 'Commit the parked pages by name', '--', 'people/');+ for (const w of writes) await retryParkedEffects(engine, sourceId, w.requestId, false);+ await runEffects();+ const [alice, bob, carol] = await Promise.all(writes.map(w => effect(w.id)));+ expect(alice).toMatchObject({ state: 'committed', outcome: { git: 'unchanged' }, error_code: null });+ expect(bob).toMatchObject({ state: 'committed', outcome: { git: 'unchanged' }, error_code: null });+ expect(carol).toMatchObject({ state: 'committed', outcome: { git: 'committed' }, error_code: null });+ expect((await gitAsync(root, 'show', 'HEAD:archive/carol-example.md'))).toContain('After');+ expect((await gitAsync(root, 'status', '--porcelain')).trim()).toBe('');+ expect((await gitAsync(remote, 'rev-parse', 'main')).trim()).toBe((await gitAsync(root, 'rev-parse', 'HEAD')).trim());+}), 120_000);diff --git a/test/git-commit-push-skipped-paths-gst2116.test.ts b/test/git-commit-push-skipped-paths-gst2116.test.tsindex 37ee3adf31..b2acc97646 100644--- a/test/git-commit-push-skipped-paths-gst2116.test.ts+++ b/test/git-commit-push-skipped-paths-gst2116.test.ts@@ -356,3 +356,18 @@ test('managed: the put_page fix names the managed source in both the CLI argv anexpect(thrown?.fix?.mcp?.tool).toBe('put_page');expect(thrown?.fix?.mcp?.arguments.source_id).toBe('source-a');}, 60_000);++test('classic: a file another writer adds or edits under a named directory just after the commit is not reported as skipped', async () => {+ const f = fixture('classic-race', 'classic');+ writeFileSync(join(f.brain, 'people', 'alice-example.md'), page('Alice', 'Second version.'));+ // Stands in for a writer that lands between the commit and the skipped-path check: it adds a page and edits the one just committed.+ const hook = join(f.brain, '.git', 'hooks', 'post-commit');+ writeFileSync(hook, `#!/bin/sh\nprintf '%s\\n' '---' 'title: Dave' '---' 'Written after the commit.' > people/dave-example.md\nprintf 'A later edit.\\n' >> people/alice-example.md\n`);+ chmodSync(hook, 0o755);+ const r = await commitPush(f, 'people/');+ expect({ code: r.exitCode, stdout: r.stdout }).toMatchObject({ code: 0 });+ expect(JSON.parse(r.stdout)).toMatchObject({ outcome: 'pushed' });+ expect(headBlob(f, 'people/alice-example.md')).toBe(page('Alice', 'Second version.').trim());+ expect(git(f.brain, 'ls-files', '--others', '--', 'people/')).toBe('people/dave-example.md');+ expect(remoteTip(f)).toBe(head(f));+}, 60_000);