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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(cos): bundle original commits and refuse lossy diffs when archiving held checkouts (#12002)
[._.]/ Adam Eivy5741216c9e7e
diff --git a/docs/features/branch-reconciliation.md b/docs/features/branch-reconciliation.mdindex e0ad5d781e..b4c08dd09b 100644--- a/docs/features/branch-reconciliation.md+++ b/docs/features/branch-reconciliation.md@@ -33,10 +33,10 @@ HEAD or branch is an ownership change. It then requires merge evidence on thedefault branch and no live owner, claim binding or manual completion handoff.Before any mutation, every unique byte is archived under-`data/cos/held-checkout-archives/`: commits not on the default branch, the-staged and unstaged diffs, verbatim copies of untracked and ignored files-(hash-verified), and symlink targets. A nested repository, unmerged or hidden-index entries, an oversized file or a write during archiving fails the archive, which leaves the checkout, its lock and its+`data/cos/held-checkout-archives/`: a verified Git bundle of commits not on the+default branch, the staged and unstaged diffs, verbatim copies of untracked and+ignored files (hash-verified), and symlink targets. A nested repository or+submodule, unmerged or hidden index entries, an oversized file or a write during archiving fails the archive, which leaves the checkout, its lock and itsbranch untouched and removes the partial archive. The lock is released onlyafter a second fresh match, and ownership, HEAD and content are recheckedimmediately before removal; if that recheck refuses, the original lock isdiff --git a/server/services/heldCheckoutRecovery.js b/server/services/heldCheckoutRecovery.jsindex fec5fd055c..426c23e020 100644--- a/server/services/heldCheckoutRecovery.js+++ b/server/services/heldCheckoutRecovery.js@@ -10,9 +10,9 @@* release: a changed lock, HEAD or branch is an ownership change and refuses.** Before any mutation every unique byte is archived under- * `data/cos/held-checkout-archives/`: commits not on the default branch, the- * staged and unstaged diffs, and verbatim copies of untracked AND ignored files- * (symlinks as recorded targets). The archive is verified by hash against the+ * `data/cos/held-checkout-archives/`: a bundle of commits not on the default+ * branch, the staged and unstaged diffs, and verbatim copies of untracked AND+ * ignored files (symlinks as recorded targets). The archive is verified by hash against the* checkout, and the checkout's content hashes must not move between archive and* removal. A live owner, manual completion handoff, unreadable state or archive* failure leaves the checkout, its lock and its branch exactly as found.@@ -53,11 +53,16 @@ const git = (args, cwd) => execGit(args, cwd, { maxBuffer: GIT_OUTPUT_BYTES }).t*/async function snapshotContent(path) {if ((await git(['ls-files', '-u', '-z'], path)).length) throw new Error('unmerged index entries cannot be archived');+ // A submodule's files and commits live in its own repository; its gitlink is not its content.+ if (zLines(await git(['ls-files', '--stage', '-z'], path)).some(line => line.startsWith('160000 '))) {+ throw new Error('submodules cannot be archived');+ }// Assume-unchanged (lowercase tag) and skip-worktree (S) entries hide edits from diff.const hidden = zLines(await git(['ls-files', '-v', '-z'], path)).find(line => /^[a-zS]/.test(line));if (hidden) throw new Error(`${hidden.slice(2)} hides working-tree changes from Git`);- const indexDiff = await git(['diff', '--cached', '--binary'], path);- const worktreeDiff = await git(['diff', '--binary'], path);+ // Raw bytes only: a textconv or external diff driver can drop content.+ const indexDiff = await git(['diff', '--cached', '--binary', '--no-textconv', '--no-ext-diff'], path);+ const worktreeDiff = await git(['diff', '--binary', '--no-textconv', '--no-ext-diff'], path);const others = [];let total = 0;for (const rel of zLines(await git(['ls-files', '--others', '-z'], path))) {@@ -97,8 +102,17 @@ async function writeArchive(dir, { repoPath, tree, branch, defaultBranch, releasconst snapshot = await snapshotContent(tree.path);await ensureDir(dir);- const commits = await git(['format-patch', `${defaultBranch}..${tree.head}`, '--stdout', '--binary'], repoPath);- if (commits) await writeFileGuarded(join(dir, 'commits.patch'), commits);+ // A bundle keeps the original commit objects, merges included, which a+ // squash-merged branch would otherwise lose to garbage collection.+ const unique = Number((await git(['rev-list', '--count', `${defaultBranch}..${tree.head}`], repoPath)).trim());+ if (!Number.isInteger(unique)) throw new Error('could not count commits outside the default branch');+ const bundle = join(dir, 'commits.bundle');+ if (unique > 0) {+ await git(['bundle', 'create', bundle, 'HEAD', `^${defaultBranch}`], tree.path);+ await git(['bundle', 'verify', bundle], repoPath);+ const heads = await git(['bundle', 'list-heads', bundle], repoPath);+ if (!heads.startsWith(`${tree.head} `)) throw new Error('commit bundle does not hold the checkout HEAD');+ }if (snapshot.indexDiff) await writeFileGuarded(join(dir, 'index.diff'), snapshot.indexDiff);if (snapshot.worktreeDiff) await writeFileGuarded(join(dir, 'worktree.diff'), snapshot.worktreeDiff);@@ -115,8 +129,9 @@ async function writeArchive(dir, { repoPath, tree, branch, defaultBranch, releasawait atomicWrite(join(dir, 'manifest.json'), {repoPath, defaultBranch, path: tree.path, head: tree.head, branch,release, archivedAt: new Date().toISOString(),- captured: { commitsPatch: Boolean(commits), indexDiff: Boolean(snapshot.indexDiff), worktreeDiff: Boolean(snapshot.worktreeDiff), files, symlinks },+ captured: { commitBundle: unique > 0, indexDiff: Boolean(snapshot.indexDiff), worktreeDiff: Boolean(snapshot.worktreeDiff), files, symlinks },restore: [+ unique > 0 ? 'git -C <repo> fetch commits.bundle HEAD:refs/heads/<restored-branch>' : null,`git -C <repo> worktree add <checkout> ${tree.head}`,snapshot.indexDiff ? 'git -C <checkout> apply --index index.diff' : null,snapshot.worktreeDiff ? 'git -C <checkout> apply worktree.diff' : null,diff --git a/server/services/heldCheckoutRecovery.test.js b/server/services/heldCheckoutRecovery.test.jsindex a1479e3875..cc67111817 100644--- a/server/services/heldCheckoutRecovery.test.js+++ b/server/services/heldCheckoutRecovery.test.js@@ -55,7 +55,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()});/** A locked checkout outside the managed roots holding unique uncommitted work. */- async function heldCheckout(name, { branch = null, lockReason = 'synthetic external owner' } = {}) {+ async function heldCheckout(name, { branch = null, lockReason = 'synthetic external owner', beforeLock } = {}) {const path = join(external, name);await execGit(['worktree', 'add', ...(branch ? ['-b', branch, path] : ['--detach', path]), 'main'], repo);await writeFile(join(path, '.gitignore'), 'ignored.txt\nlinked\n');@@ -64,6 +64,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()await writeFile(join(path, 'ignored.txt'), 'ignored work\n');// Windows runners may lack symlink privilege; the archive contract is the same.if (SYMLINKS) await symlink('../shared-target', join(path, 'linked'));+ if (beforeLock) await beforeLock(path);await execGit(['worktree', 'lock', '--reason', lockReason, path], repo);const gitPath = (await execGit(['rev-parse', '--show-toplevel'], path)).stdout.trim();const head = (await execGit(['rev-parse', 'HEAD'], path)).stdout.trim();@@ -101,8 +102,14 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()expect(await readFile(join(result.archiveDir, 'index.diff'), 'utf8')).toContain('+staged work');});- it('retires a merged branch checkout and its branch, and drops it from the persisted inventory', async () => {- const { path, release } = await heldCheckout('branch-held', { branch: 'feature/held' });+ it('bundles a squash-merged branch, retires it with its branch, and drops it from the persisted inventory', async () => {+ const { path, release } = await heldCheckout('branch-held', { branch: 'feature/held', beforeLock: async (tree) => {+ await writeFile(join(tree, 'feature.txt'), 'feature\n');+ await execGit(['add', 'feature.txt'], tree);+ await execGit(['commit', '-m', 'feat: synthetic feature'], tree);+ await execGit(['merge', '--squash', 'feature/held'], repo);+ await execGit(['commit', '-m', 'feat: synthetic feature (squash)'], repo);+ } });await recordBranchReconcileOutcome('app-1', {status: 'incomplete', reason: 'holds-remaining', nextAction: 'Recover held checkouts.',remaining: [{ ...release, managed: false, reason: 'worktree-locked' }, { branch: 'other', reason: 'actionable' }],@@ -113,6 +120,9 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()expect(result).toMatchObject({ ok: true, branchDeleted: true });expect(existsSync(path)).toBe(false);expect((await execGit(['branch', '--list', 'feature/held'], repo)).stdout.trim()).toBe('');+ // The original commit stays recoverable after its branch is gone.+ await execGit(['fetch', join(result.archiveDir, 'commits.bundle'), 'HEAD:refs/heads/restored'], repo);+ expect((await execGit(['rev-parse', 'restored'], repo)).stdout.trim()).toBe(release.head);const outcome = await getBranchReconcileOutcome('app-1');expect(outcome.status).toBe('incomplete');expect(outcome.remaining).toEqual([{ branch: 'other', reason: 'actionable' }]);