Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Merge commit '672f27c326ccf93df32f20f1bb4636f61f7c93a6' into capy/effect-commit-unstage
garrytand84d0e7baeb8
diff --cc src/core/persistence/effect-git.tsindex 9b77c74812,a0f0b6e38f..a6b879ddc4--- a/src/core/persistence/effect-git.ts+++ b/src/core/persistence/effect-git.ts@@@ -398,80 -366,104 +400,153 @@@ export async function commitGitTargets(const commitPaths = [...new Set(changed.map(c => c.path))];const noted = new Map<string, GitCommitNote>();for (const c of changed) { const note = notes?.get(c.requested); if (note && !noted.has(c.path)) noted.set(c.path, note); }- let messageDir: string | undefined;- let result;- try {- const paragraphs = commitMessage(commitPaths, noted);- const message = commitMessageArgs(paragraphs, () => {- mkdirSync(persistenceHome(), { recursive: true, mode: 0o700 });- return messageDir = mkdtempSync(join(persistenceHome(), 'commit-message-'));- });- const before = await git(session, ['rev-parse', '--verify', '--quiet', 'HEAD']);- await recordIntentAsync(root, { writer: 'effect', head_before: before.code === 0 ? before.stdout.trim() : null, message: paragraphs.join('\n\n') });- result = await git(session, [...(commitPaths.some(path => removed.has(path)) ? NO_FSCACHE : []), 'commit', '--only', ...message, '--', ...literalPathspecs(commitPaths)]);- } finally { if (messageDir) rmSync(messageDir, { recursive: true, force: true }); }- if (result.code === 0) {- const head = await git(session, ['rev-parse', 'HEAD']);- const trailers = [...noted.values()].flatMap(note => note.trailers ?? []);- const supersedes = supersededBy(trailers), adopted = adoptedBy(trailers);- if (head.code === 0) await recordCommitAsync(root, { sha: head.stdout.trim(), writer: 'effect', ...(supersedes.length ? { supersedes } : {}), ...(adopted.length ? { adopted } : {}) });+ const wanted = new Map<string, string | null>();+ for (const c of changed) {+ const hash = expected?.get(c.requested);+ wanted.set(c.path, hash !== undefined && (!wanted.has(c.path) || wanted.get(c.path) === hash) ? hash : '');}- const outcome = result.code === 0 ? { git: 'committed' } : gitFailure('Cannot commit the canonical Git target.',+ 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();});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 */ }+}++ /** Git's whitespace cleanup of a `-m` paragraph, as `git commit -m` applies it: trailing blanks, repeated and edge blank lines go. */+ export function stripspace(paragraph: string): string {+ const lines: string[] = [];+ for (const line of paragraph.split('\n').map(text => text.replace(/[ \t\r]+$/, ''))) if (line || lines.length && lines[lines.length - 1]) lines.push(line);+ while (lines.length && !lines[lines.length - 1]) lines.pop();+ return lines.join('\n');+ }++ /**+ * #8896: a Git effect's commit, made by plumbing so its sha is gbrain's own before any ref names it. The index entries+ * `git add` just staged for `paths` are laid over HEAD's tree in a scratch index, `commit-tree` writes the commit, the+ * ledger records its sha, and `update-ref HEAD <new> <old>` moves the branch only if HEAD is still the commit it was+ * built on; a moved HEAD is read again and the commit rebuilt once. Null when git fails or HEAD moved twice. The+ * secret scan skips an effect commit only by this recorded sha, so an amend, reset or reword is a different commit and+ * is scanned. The record is `verified` only when every path's staged blob is the bytes the effect wrote (`wanted`: the+ * sha256 the effect recorded, null for a removal, '' for none known): the file, read once, hashes to it and has the+ * 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.+ */+ 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> {+ 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 verified = paths.every(path => {+ const hash = wanted.get(path), entry = staged.get(path);+ if (hash === undefined || hash === '') return false;+ return hash === null ? !entry : !!entry && entry.mode.startsWith('100') && fileDigests(join(root, path), format.stdout.trim()) === `${hash} ${entry.oid}`;+ });+ const trailers = [...noted.values()].flatMap(note => note.trailers ?? []);+ const 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;+ 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}`) };+ const removals = paths.filter(path => !staged.has(path));+ const 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;+ 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 commit = await git(session, ['commit-tree', tree.stdout.trim(), ...(head ? ['-p', head] : []), ...message]);+ if (commit.code !== 0) return null;+ 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) {+ 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;+ }+ if (attempt) return null;+ }+ }++ /** `<sha256> <git blob id>` of the file's bytes, read once; null when it cannot be read or changes size while read. */+ function fileDigests(path: string, objectFormat: string): string | null {+ let fd: number;+ try { fd = openSync(path, 'r'); } catch { return null; }+ try {+ const size = fstatSync(fd).size;+ const content = createHash('sha256'), blob = createHash(objectFormat === 'sha256' ? 'sha256' : 'sha1').update(`blob ${size}\0`);+ const buffer = Buffer.alloc(1024 * 1024);+ let read = 0;+ for (let n; (n = readSync(fd, buffer, 0, buffer.length, null)); read += n) { content.update(buffer.subarray(0, n)); blob.update(buffer.subarray(0, n)); }+ return read === size ? `${content.digest('hex')} ${blob.digest('hex')}` : null;+ } finally { closeSync(fd); }+ }+/*** #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 been