Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: verify committed refs after ambiguous Git exits (#13292)

[._.]/ Adam Eivy206fc9b0e2de
diff --git a/server/services/git.commit.test.js b/server/services/git.commit.test.js
index f7074ba6a4..86e7519bed 100644
--- a/server/services/git.commit.test.js
+++ b/server/services/git.commit.test.js
@@ -1,36 +1,162 @@
-// These command-contract fixtures use synthetic repositories; real admission is exercised in git.publication.test.js.
-vi.mock('../lib/checkoutRetirementAdmission.js', () => ({ withCheckoutPublication: (_dir, work) => work() }));
-import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { spawn } from 'node:child_process';
+import { readFile, writeFile, unlink } from 'node:fs/promises';
+import { join } from 'node:path';
-vi.mock('../lib/execGit.js', () => {
- const execGit = vi.fn();
- return { execGit, execGitSafe: (...args) => execGit(...args) };
+// Observe the first real Git result, then deterministically interleave another
+// writer before readback. No retry can erase the original failed exit.
+const observation = vi.hoisted(() => ({ afterCommit: null, results: [] }));
+vi.mock('../lib/execGit.js', async importOriginal => {
+ const actual = await importOriginal();
+ return {
+ ...actual,
+ execGit: async (...args) => {
+ const result = await actual.execGit(...args);
+ if (args[0][0] === 'commit' && observation.afterCommit) {
+ observation.results.push(result);
+ await observation.afterCommit(...args);
+ }
+ return result;
+ }
+ };
});
-
import { execGit } from '../lib/execGit.js';
+import { makeGitSandbox, destroyGitSandbox } from '../lib/gitTestRepo.js';
import { commit } from './git.js';
-beforeEach(() => {
- vi.clearAllMocks();
- execGit.mockResolvedValue({ stdout: '[main abc1234] chore: example\n', stderr: '', exitCode: 0 });
+let fixture;
+let repo;
+let owner;
+let ownerClosed;
+const git = (...args) => execGit(args, repo);
+const head = async () => (await git('rev-parse', 'HEAD')).stdout.trim();
+
+// The child exclusively creates, holds and releases its own lock. IPC proves
+// ownership/ready state without sleeps, stale-lock inference or production data.
+async function holdLock(name) {
+ const path = (await git('rev-parse', '--path-format=absolute', '--git-path', name)).stdout.trim();
+ owner = spawn(process.execPath, ['-e', `
+ const fs = require('node:fs');
+ const path = process.argv[1];
+ const fd = fs.openSync(path, 'wx');
+ fs.writeSync(fd, String(process.pid));
+ process.on('message', () => {
+ fs.closeSync(fd);
+ fs.unlinkSync(path);
+ process.disconnect();
+ });
+ process.send({ pid: process.pid });
+ `, path], { stdio: ['ignore', 'ignore', 'pipe', 'ipc'] });
+ ownerClosed = new Promise(resolve => owner.once('close', resolve));
+ const ready = new Promise((resolve, reject) => {
+ owner.once('error', reject);
+ owner.once('message', resolve);
+ owner.once('exit', () => reject(new Error('fixture lock owner exited before ready')));
+ });
+ const { pid } = await ready;
+ expect(pid).toBe(owner.pid);
+ expect(await readFile(path, 'utf8')).toBe(String(pid));
+ return path;
+}
+
+beforeEach(async () => {
+ fixture = await makeGitSandbox({ prefix: 'git-commit-readback-' });
+ repo = join(fixture.scratch, 'linked');
+ await execGit(['worktree', 'add', '-b', 'claim/example', repo], fixture.repo);
+ await git('config', 'rerere.enabled', 'true');
+ await writeFile(join(repo, 'initial.txt'), 'requested change\n');
+ await git('add', 'initial.txt');
+ observation.results = [];
+ observation.afterCommit = async () => {};
});
+afterEach(async () => {
+ observation.afterCommit = null;
+ if (owner?.connected) owner.send('release');
+ if (ownerClosed) await ownerClosed;
+ owner = null;
+ ownerClosed = null;
+ await destroyGitSandbox(fixture?.scratch);
+});
+
+describe('commit outcome readback with real Git', () => {
+ it('recognizes exactly one scoped commit after a rerere collision and preserves the live lock and other staged work', async () => {
+ const before = await head();
+ await writeFile(join(repo, '[id].txt'), 'literal filename\n');
+ await writeFile(join(repo, 'i.txt'), 'unrelated staged work\n');
+ await git('add', '--', ':(literal)[id].txt', 'i.txt');
+ const lock = await holdLock('MERGE_RR.lock');
+ const result = await commit(repo, 'requested commit', { paths: ['initial.txt', '[id].txt'] });
+ const after = await head();
+ expect(result).toEqual({ hash: after, message: 'requested commit', commitOutcome: {
+ command: 'commit', exitCode: 128, beforeHead: before, afterHead: after, status: 'verified-committed'
+ } });
+ expect(observation.results).toHaveLength(1);
+ expect(observation.results[0]).toMatchObject({ exitCode: 128, stderr: expect.stringContaining('MERGE_RR.lock') });
+ expect((await git('rev-list', '--count', `${before}..HEAD`)).stdout.trim()).toBe('1');
+ expect((await git('show', 'HEAD:initial.txt')).stdout).toBe('requested change\n');
+ expect((await git('diff', '--cached', '--name-only')).stdout.trim()).toBe('i.txt');
+ expect((await git('show', 'HEAD:[id].txt')).stdout).toBe('literal filename\n');
+ expect(owner.exitCode).toBeNull();
+ expect(await readFile(lock, 'utf8')).toBe(String(owner.pid));
+ });
+
+ it('preserves the first genuine commit failure without retrying or deleting its live index lock', async () => {
+ const before = await head();
+ const lock = await holdLock('index.lock');
+ await expect(commit(repo, 'not committed')).rejects.toMatchObject({
+ message: expect.stringContaining('index.lock'),
+ commitOutcome: { beforeHead: before, afterHead: before, status: 'unverified', exitCode: 128 }
+ });
+ expect(observation.results).toHaveLength(1);
+ expect(await head()).toBe(before);
+ expect(await readFile(lock, 'utf8')).toBe(String(owner.pid));
+ expect((await git('diff', '--cached', '--name-only')).stdout.trim()).toBe('initial.txt');
+ });
+
+ it('refuses an unrelated branch advance even when the attempted commit was recorded', async () => {
+ await holdLock('MERGE_RR.lock');
+ observation.afterCommit = async () => {
+ const committed = await head();
+ const tree = (await git('rev-parse', 'HEAD^{tree}')).stdout.trim();
+ const other = (await git('commit-tree', tree, '-p', committed, '-m', 'competing writer')).stdout.trim();
+ await git('update-ref', '-m', 'competing writer', 'HEAD', other, committed);
+ };
+ await expect(commit(repo, 'requested commit')).rejects.toMatchObject({
+ message: expect.stringContaining('MERGE_RR.lock'), commitOutcome: { status: 'unverified' }
+ });
+ expect(observation.results).toHaveLength(1);
+ expect((await git('log', '-1', '--format=%s')).stdout.trim()).toBe('competing writer');
+ });
+
+ it('refuses a changed current branch while retaining the commit on the original branch', async () => {
+ await git('branch', 'other');
+ await holdLock('MERGE_RR.lock');
+ observation.afterCommit = () => git('symbolic-ref', 'HEAD', 'refs/heads/other');
+ await expect(commit(repo, 'requested commit')).rejects.toMatchObject({ commitOutcome: { status: 'unverified' } });
+ expect(observation.results).toHaveLength(1);
+ expect((await git('log', '-1', '--format=%s', 'claim/example')).stdout.trim()).toBe('requested commit');
+ expect((await git('symbolic-ref', 'HEAD')).stdout.trim()).toBe('refs/heads/other');
+ });
-// Uniquely pins that an automated commit cannot sweep in files the user staged.
-describe('commit', () => {
- it('scopes the commit to literal pathspecs when paths are given', async () => {
- await expect(commit('/repo/example-app', 'chore: example', { paths: ['.quality.json', 'app/[id].jsx'] }))
- .resolves.toEqual({ hash: 'abc1234', message: 'chore: example' });
- expect(execGit).toHaveBeenCalledWith(
- ['commit', '-m', 'chore: example', '--', ':(literal).quality.json', ':(literal)app/[id].jsx'],
- '/repo/example-app'
- );
+ it('refuses unavailable invocation evidence instead of treating any advanced branch as success', async () => {
+ await holdLock('MERGE_RR.lock');
+ observation.afterCommit = async () => {
+ const log = (await git('rev-parse', '--path-format=absolute', '--git-path', 'logs/refs/heads/claim/example')).stdout.trim();
+ await unlink(log);
+ };
+ await expect(commit(repo, 'requested commit')).rejects.toMatchObject({
+ message: expect.stringContaining('MERGE_RR.lock'), commitOutcome: { status: 'unverified' }
+ });
+ expect(observation.results).toHaveLength(1);
});
- it('commits the index unchanged when no paths are given and rejects unsafe ones', async () => {
- await commit('/repo/example-app', 'chore: example');
- expect(execGit).toHaveBeenCalledWith(['commit', '-m', 'chore: example'], '/repo/example-app');
- await commit('/repo/example-app', 'chore: example', {});
- expect(execGit).toHaveBeenLastCalledWith(['commit', '-m', 'chore: example'], '/repo/example-app');
- await expect(commit('/repo/example-app', 'chore: example', { paths: ['../escape.json'] })).rejects.toThrow('Invalid file path');
+ it('keeps ordinary successful commits on slash-named branches working', async () => {
+ await expect(commit(repo, 'unsafe path', { paths: ['../escape.json'] })).rejects.toThrow('Invalid file path');
+ expect(observation.results).toHaveLength(0);
+ const result = await commit(repo, 'ordinary success');
+ expect(result).toEqual({ hash: expect.stringMatching(/^[a-f0-9]+$/), message: 'ordinary success' });
+ expect((await head()).startsWith(result.hash)).toBe(true);
+ expect(observation.results).toHaveLength(1);
+ expect(observation.results[0].exitCode).toBe(0);
});
});
diff --git a/server/services/git.js b/server/services/git.js
index 9d9d135a95..25eab9e8a7 100644
--- a/server/services/git.js
+++ b/server/services/git.js
@@ -1,4 +1,5 @@
import { existsSync } from 'fs';
+import { randomUUID } from 'node:crypto';
import { mkdtemp } from 'fs/promises';
import { tmpdir } from 'os';
import { delimiter, join, resolve } from 'path';
@@ -174,11 +175,55 @@ export async function commit(dir, message, { paths = [] } = {}) {
return withCheckoutPublication(dir, () => commitAdmitted(dir, message, { paths }));
}
+// Snapshot only read-only identities. Detached/unborn HEAD cannot verify an
+// ambiguous result; ordinary successful commits there still work as before.
+async function commitIdentity(dir) {
+ const branch = await execGit(['symbolic-ref', '--quiet', 'HEAD'], dir, { ignoreExitCode: true });
+ const head = await execGit(['rev-parse', '--verify', 'HEAD'], dir, { ignoreExitCode: true });
+ return {
+ branch: branch.exitCode === 0 ? branch.stdout.trim() : null,
+ head: head.exitCode === 0 && /^[a-f0-9]{40,64}$/.test(head.stdout.trim()) ? head.stdout.trim() : null
+ };
+}
+
+async function verifyCommitReadback(dir, before, action) {
+ const after = await commitIdentity(dir);
+ if (!before.branch?.startsWith('refs/heads/') || !before.head
+ || after.branch !== before.branch || !after.head || after.head === before.head) return { after, verified: false };
+ const log = await execGit(['reflog', '-1', '--format=%H%x00%gs', before.branch], dir);
+ const [hash, subject] = log.stdout.trim().split('\0');
+ const parent = await execGit(['rev-parse', `${after.head}^1`], dir);
+ // A matching tree/message alone could belong to a competing writer. The
+ // per-invocation reflog action correlates this advance with our child process.
+ const current = await commitIdentity(dir);
+ return { after, verified: hash === after.head && subject?.startsWith(`${action}: `)
+ && parent.stdout.trim() === before.head
+ && current.branch === before.branch && current.head === after.head };
+}
+
async function commitAdmitted(dir, message, { paths = [] } = {}) {
const pathspecs = paths.length ? ['--', ...validateFilePaths(paths).map(toLiteralPathspec)] : [];
- // Using spawn with -m argument passes message safely without shell interpretation
- const result = await execGit(['commit', '-m', message, ...pathspecs], dir);
- const hashMatch = result.stdout.match(/\[[\w-]+ ([a-f0-9]+)\]/);
+ const before = await commitIdentity(dir);
+ const action = `portos-commit-${randomUUID()}`;
+ // Rerere can fail AFTER commit updated the ref (#13292). Never repeat the
+ // mutation or touch a lock. Timeouts/signals remain uncertain lifetime errors.
+ const result = await execGit(['commit', '-m', message, ...pathspecs], dir, {
+ ignoreExitCode: true, env: { ...process.env, GIT_REFLOG_ACTION: action }
+ });
+ if (result.exitCode !== 0) {
+ const readback = Number.isInteger(result.exitCode) && !result.terminated
+ ? await verifyCommitReadback(dir, before, action).catch(() => null) : null;
+ const commitOutcome = {
+ command: 'commit', exitCode: result.exitCode, beforeHead: before.head,
+ afterHead: readback?.after?.head ?? null,
+ status: readback?.verified ? 'verified-committed' : 'unverified'
+ };
+ if (readback?.verified) return { hash: readback.after.head, message, commitOutcome };
+ throw Object.assign(new Error(result.stderr || `git exited with code ${result.exitCode}`), {
+ commitOutcome, ...(result.terminated ? { name: 'AbortError', terminated: true, signal: result.signal } : {})
+ });
+ }
+ const hashMatch = result.stdout.match(/^\[[^\r\n]* ([a-f0-9]+)\]/m);
return {
hash: hashMatch ? hashMatch[1] : null,
message