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: release retirement locks after partial initialization (#13328)

[._.]/ Adam Eivy249b0a632f03
diff --git a/server/lib/branchRetirementArchive.js b/server/lib/branchRetirementArchive.js
index d8555a9724..76b8ecd689 100644
--- a/server/lib/branchRetirementArchive.js
+++ b/server/lib/branchRetirementArchive.js
@@ -8,6 +8,7 @@ import { dirname, join } from 'node:path';
import { execGit } from './execGit.js';
import { PATHS } from './paths.js';
import { ensureDir, sha256File, writeFileGuarded } from './fileCore.js';
+import { escapeRegExp } from './textUtils.js';
const MAX_EVIDENCE_BYTES = 25 * 1024 * 1024;
const MAX_RECOVERY_HEADS = 1024;
@@ -34,7 +35,7 @@ const unchanged = (left, right) => JSON.stringify(left.identity) === JSON.string
// Included/global/worktree settings cannot be retired by editing the shared
// config. Refuse them rather than leave a reused name with inherited settings.
async function branchConfiguration(repoPath, branch) {
- const pattern = `^branch\\.${branch.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\.[^.]+$`;
+ const pattern = `^branch\\.${escapeRegExp(branch)}\\.[^.]+$`;
const query = async scope => {
const result = await execGit(['config', '--null', '--show-origin', ...scope, '--get-regexp', pattern], repoPath, { ignoreExitCode: true });
if (![0, 1].includes(result.exitCode)) throw new Error('branch configuration cannot be read');
@@ -47,16 +48,22 @@ async function branchConfiguration(repoPath, branch) {
async function withNativeLock(path, bytes, work) {
const handle = await open(path, 'wx', 0o600);
+ let identity;
try {
- await handle.writeFile(bytes);
- await handle.sync();
- } finally { await handle.close(); }
- const owned = await readEvidence(path);
- try { return await work(owned); }
+ try {
+ identity = await handle.stat();
+ await handle.writeFile(bytes);
+ await handle.sync();
+ } finally { await handle.close(); }
+ return await work(await readEvidence(path));
+ }
finally {
- // Never remove a replaced or modified lock belonging to another writer.
- const current = await readEvidence(path);
- if (current.bytes && unchanged(current, owned)) await unlink(path);
+ // Cover partial writes and sync failures, but never unlink a replacement.
+ const current = await lstat(path).catch(error => {
+ if (error.code === 'ENOENT') return null;
+ throw error;
+ });
+ if (identity && current?.dev === identity.dev && current.ino === identity.ino) await unlink(path);
}
}
diff --git a/server/services/worktreeReap.test.js b/server/services/worktreeReap.test.js
index da4dfc91f2..4766fff3fe 100644
--- a/server/services/worktreeReap.test.js
+++ b/server/services/worktreeReap.test.js
@@ -25,6 +25,21 @@ import {
SKIP_HEAVY_INTEGRATION,
} from '../lib/gitTestRepo.js';
import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';
+const lockWriteFault = vi.hoisted(() => ({ path: null }));
+vi.mock('node:fs/promises', async importOriginal => {
+ const actual = await importOriginal();
+ return { ...actual, open: async (path, ...args) => {
+ const handle = await actual.open(path, ...args);
+ if (path === lockWriteFault.path) {
+ const write = handle.writeFile.bind(handle);
+ handle.writeFile = async bytes => {
+ await write(bytes.subarray(0, 8));
+ throw Object.assign(new Error('fixture disk full'), { code: 'ENOSPC' });
+ };
+ }
+ return handle;
+ } };
+});
vi.mock('../lib/paths.js', async importOriginal => makePathsProxy(await importOriginal(), { dataRoot: () => lazyTempDataRoot('portos-reap-archives-') }));
afterAll(cleanupTempDataRoots);
import { PATHS } from '../lib/paths.js';
@@ -196,6 +211,21 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('local completed ref retirement', () =>
expect((await execGit(['config', 'branch.done.remote'], dir)).stdout.trim()).toBe('keep');
});
+ it('releases its partially written lock after disk failure without changing live configuration', async () => {
+ await execGit(['branch', 'done'], dir);
+ await execGit(['config', 'branch.done.remote', 'keep'], dir);
+ const head = (await execGit(['rev-parse', 'done'], dir)).stdout.trim();
+ const common = (await execGit(['rev-parse', '--path-format=absolute', '--git-common-dir'], dir)).stdout.trim();
+ const original = await readFile(join(common, 'config'));
+ lockWriteFault.path = join(common, 'config.lock');
+ try { expect(await deleteCompletedLocalBranch(dir, 'done', head)).toBe(false); }
+ finally { lockWriteFault.path = null; }
+ expect(existsSync(join(common, 'config.lock'))).toBe(false);
+ expect(await readFile(join(common, 'config'))).toEqual(original);
+ expect((await execGit(['rev-parse', 'done'], dir)).stdout.trim()).toBe(head);
+ await execGit(['config', 'branch.done.remote', 'retry'], dir);
+ });
+
it.each(['config.lock', 'refs/heads/done.lock'])('preserves another writer\'s %s and live settings', async lock => {
await execGit(['branch', 'done'], dir);
await execGit(['config', 'branch.done.remote', 'keep'], dir);