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

perf(cos): halve held-checkout retirement time by running independent Git reads together (#12342)

Adam Eivyb3467e990a11
diff --git a/server/services/heldCheckoutRecovery.js b/server/services/heldCheckoutRecovery.js
index 1a63501ee2..e4396ce0da 100644
--- a/server/services/heldCheckoutRecovery.js
+++ b/server/services/heldCheckoutRecovery.js
@@ -125,8 +125,20 @@ async function rawBlobId(source, size, format) {
* The fingerprint is compared again immediately before removal.
*/
async function snapshotContent(path) {
- if ((await git(['ls-files', '-u', '-z'], path)).length) throw new Error('unmerged index entries cannot be archived');
- const stage = zLines(await git(['ls-files', '--stage', '-z'], path));
+ // These reads are independent and each Git spawn is mostly launch latency, so
+ // they run together; every destructive boundary re-reads this snapshot anyway.
+ const [unmerged, stageOut, tagged, indexDiff, worktreeDiff, objectFormat, others] = await Promise.all([
+ git(['ls-files', '-u', '-z'], path),
+ git(['ls-files', '--stage', '-z'], path),
+ git(['ls-files', '-v', '-z'], path),
+ // A textconv or external diff driver can drop bytes from a patch.
+ git(['diff', '--cached', '--binary', '--no-textconv', '--no-ext-diff'], path),
+ git(['diff', '--binary', '--no-textconv', '--no-ext-diff'], path),
+ git(['rev-parse', '--show-object-format'], path),
+ git(['ls-files', '--others', '-z'], path),
+ ]);
+ if (unmerged.length) throw new Error('unmerged index entries cannot be archived');
+ const stage = zLines(stageOut);
const gitlinks = [];
for (const line of stage.filter(row => row.startsWith('160000 '))) {
const tab = line.indexOf('\t');
@@ -137,11 +149,8 @@ async function snapshotContent(path) {
gitlinks.push({ path: rel, commit: header.split(' ')[1], state: info ? 'empty' : 'missing' });
}
// 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));
+ const hidden = zLines(tagged).find(line => /^[a-zS]/.test(line));
if (hidden) throw new Error(`${hidden.slice(2)} hides working-tree changes from Git`);
- // A textconv or external diff driver can drop bytes from a patch.
- 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 files = [];
let total = 0;
@@ -150,7 +159,7 @@ async function snapshotContent(path) {
if (info.size > MAX_FILE_BYTES || total > MAX_TOTAL_BYTES) throw new Error(`${rel} exceeds the archive size limit`);
files.push({ path: rel, bytes: info.size, sha256: await sha256File(join(path, rel)), ...extra });
};
- const format = (await git(['rev-parse', '--show-object-format'], path)).trim() || 'sha1';
+ const format = objectFormat.trim() || 'sha1';
for (const line of stage) {
const tab = line.indexOf('\t');
const [mode, blob] = line.slice(0, tab).split(' ');
@@ -162,7 +171,7 @@ async function snapshotContent(path) {
if (await rawBlobId(join(path, rel), info.size, format) !== blob) await addFile(rel, info, { tracked: true });
}
const symlinks = [];
- for (const rel of zLines(await git(['ls-files', '--others', '-z'], path))) {
+ for (const rel of zLines(others)) {
// `ls-files --others` names a nested repository as `dir/` without its files.
if (rel.endsWith('/')) throw new Error(`nested repository at ${rel} cannot be archived`);
const info = await lstat(join(path, rel));
@@ -190,17 +199,16 @@ async function archiveCheckout(input) {
}
async function writeArchive(dir, { repoPath, tree, branch, defaultBranch, release, review }) {
- const snapshot = await snapshotContent(tree.path);
+ const [snapshot, administration] = await Promise.all([snapshotContent(tree.path), snapshotAdministration(tree.path)]);
await ensureDir(dir);
await chmod(dir, 0o700);
- const administration = await snapshotAdministration(tree.path);
// A bundle keeps the original commit objects, merges included, which a
// squash-merged branch would otherwise lose to garbage collection.
const bundle = join(dir, 'commits.bundle');
await git(['bundle', 'create', bundle, 'HEAD'], tree.path);
- await git(['bundle', 'verify', bundle], repoPath);
- if (!(await git(['bundle', 'list-heads', bundle], repoPath)).startsWith(`${tree.head} `)) throw new Error('bundle head mismatch');
+ const [, heads] = await Promise.all([git(['bundle', 'verify', bundle], repoPath), git(['bundle', 'list-heads', bundle], repoPath)]);
+ if (!heads.startsWith(`${tree.head} `)) throw new Error('bundle head mismatch');
await chmod(bundle, 0o600);
for (const file of administration.files) {
const destination = join(dir, 'administration', file.path);
@@ -209,7 +217,6 @@ async function writeArchive(dir, { repoPath, tree, branch, defaultBranch, releas
await chmod(destination, 0o600);
if (await sha256File(destination) !== file.sha256) throw new Error('administration archive mismatch');
}
- if ((await snapshotAdministration(tree.path)).fingerprint !== administration.fingerprint) throw new Error('administration changed while archiving');
if (snapshot.indexDiff) await writeFileGuarded(join(dir, 'index.diff'), snapshot.indexDiff);
if (snapshot.worktreeDiff) await writeFileGuarded(join(dir, 'worktree.diff'), snapshot.worktreeDiff);
@@ -220,7 +227,10 @@ async function writeArchive(dir, { repoPath, tree, branch, defaultBranch, releas
await copyFileGuarded(join(tree.path, file.path), destination);
if (await sha256File(destination) !== file.sha256) throw new Error(`archived copy of ${file.path} does not match`);
}
- if ((await snapshotContent(tree.path)).fingerprint !== snapshot.fingerprint) throw new Error('checkout content changed while archiving');
+ // Re-read after every copy so the check covers the whole archive window.
+ const [after, administrationAfter] = await Promise.all([snapshotContent(tree.path), snapshotAdministration(tree.path)]);
+ if (administrationAfter.fingerprint !== administration.fingerprint) throw new Error('administration changed while archiving');
+ if (after.fingerprint !== snapshot.fingerprint) throw new Error('checkout content changed while archiving');
await atomicWrite(join(dir, 'manifest.json'), {
repoPath, defaultBranch, path: tree.path, head: tree.head, branch,
@@ -324,14 +334,17 @@ async function retireHeldCheckout({ repoPath, release, review }, opts = {}) {
const verifyRemoval = async (holder) => {
if (!holder || holder.head !== tree.head || shortBranch(holder.branch) !== branch) return 'worktree-holder-changed';
if (opts.inventoryCurrent && !await opts.inventoryCurrent()) return 'release-not-in-inventory';
- const reason = await ownerReason({ repoPath, tree, branch, readAgents, reviewed: Boolean(review) });
- if (review) {
- const fresh = await reviewCurrentCheckout({ repoPath, release, prUrl: review.prUrl, unlocked: release.locked }, opts);
- if (!fresh.ok || fresh.review.digest !== review.digest) return fresh.reason || 'retirement-review-changed';
- }
- if ((await snapshotAdministration(tree.path)).fingerprint !== archive.administrationFingerprint) return 'checkout-administration-changed';
+ // Independent final reads, all taken immediately before removal; the
+ // verdicts below keep their original precedence.
+ const [reason, fresh, administration, current] = await Promise.all([
+ ownerReason({ repoPath, tree, branch, readAgents, reviewed: Boolean(review) }),
+ review ? reviewCurrentCheckout({ repoPath, release, prUrl: review.prUrl, unlocked: release.locked }, opts) : null,
+ snapshotAdministration(tree.path),
+ snapshotContent(tree.path).catch(() => null),
+ ]);
+ if (fresh && (!fresh.ok || fresh.review.digest !== review.digest)) return fresh.reason || 'retirement-review-changed';
+ if (administration.fingerprint !== archive.administrationFingerprint) return 'checkout-administration-changed';
if (reason) return reason;
- const current = await snapshotContent(tree.path).catch(() => null);
return current?.fingerprint === archive.fingerprint ? null : 'checkout-content-changed';
};
const removal = await forceRemoveWorktreeDir(repoPath, tree.path, {
diff --git a/server/services/heldCheckoutRecovery.test.js b/server/services/heldCheckoutRecovery.test.js
index 2b2679c2cd..da8f2098b5 100644
--- a/server/services/heldCheckoutRecovery.test.js
+++ b/server/services/heldCheckoutRecovery.test.js
@@ -78,8 +78,9 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()
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();
+ const [gitPath, head] = (await Promise.all([
+ execGit(['rev-parse', '--show-toplevel'], path), execGit(['rev-parse', 'HEAD'], path),
+ ])).map(result => result.stdout.trim());
return { path, release: { path: gitPath, head, branch, locked: true, lockReason } };
}
@@ -266,8 +267,9 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()
await execGit(['commit', '-am', 'strengthen implementation'], repo);
}
} });
- const main = (await execGit(['rev-parse', 'main'], repo)).stdout.trim();
- const admin = (await execGit(['rev-parse', '--absolute-git-dir'], path)).stdout.trim();
+ const [main, admin] = (await Promise.all([
+ execGit(['rev-parse', 'main'], repo), execGit(['rev-parse', '--absolute-git-dir'], path),
+ ])).map(result => result.stdout.trim());
await writeFile(join(admin, 'portos-review-status'), 'private synthetic review marker');
injected.policies = [{ id: 'agent-legacy', status: 'completed', result: { success: true },
metadata: { workspacePath: repo, claimBranches: [release.branch], prCompletionPolicy: 'review-then-merge' } }];
diff --git a/server/services/heldCheckoutReview.js b/server/services/heldCheckoutReview.js
index 66583dea32..3ad9cf2025 100644
--- a/server/services/heldCheckoutReview.js
+++ b/server/services/heldCheckoutReview.js
@@ -75,20 +75,28 @@ export async function buildCheckoutRetirementReview({ repoPath, release, prUrl,
if (release.branch === forge.defaultBranch) throw new Error('protected-branch');
if (!oid(forge.changeBase) || !oid(forge.firstCommit) || !Array.isArray(forge.changedPaths)
|| !forge.changedPaths.length || !forge.changedPaths.every(safePath)) throw new Error('retirement-original-scope-unavailable');
- for (const head of [forge.prHead, forge.mergeCommit, forge.defaultHead, forge.changeBase, forge.firstCommit]) {
- await git(['cat-file', '-e', `${head}^{commit}`], repoPath).catch(() => { throw new Error('retirement-local-objects-missing'); });
- }
- await git(['merge-base', '--is-ancestor', forge.mergeCommit, forge.defaultHead], repoPath)
- .catch(() => { throw new Error('retirement-merge-not-in-default'); });
- const parent = (await git(['rev-list', '--parents', '-n', '1', forge.firstCommit], repoPath)).trim().split(/\s+/);
+ // Independent reads run together (each Git spawn is mostly launch latency);
+ // the verdicts keep their original precedence.
+ await Promise.all([...new Set([forge.prHead, forge.mergeCommit, forge.defaultHead, forge.changeBase, forge.firstCommit])]
+ .map(head => git(['cat-file', '-e', `${head}^{commit}`], repoPath).catch(() => { throw new Error('retirement-local-objects-missing'); })));
+ const [inDefault, parents, inPr] = await Promise.allSettled([
+ git(['merge-base', '--is-ancestor', forge.mergeCommit, forge.defaultHead], repoPath),
+ git(['rev-list', '--parents', '-n', '1', forge.firstCommit], repoPath),
+ git(['merge-base', '--is-ancestor', forge.firstCommit, forge.prHead], repoPath),
+ ]);
+ if (inDefault.status === 'rejected') throw new Error('retirement-merge-not-in-default');
+ if (parents.status === 'rejected') throw parents.reason;
+ const parent = parents.value.trim().split(/\s+/);
if (parent.length !== 2 || parent[1] !== forge.changeBase) throw new Error('retirement-original-scope-unavailable');
- await git(['merge-base', '--is-ancestor', forge.firstCommit, forge.prHead], repoPath)
- .catch(() => { throw new Error('retirement-original-scope-unavailable'); });
+ if (inPr.status === 'rejected') throw new Error('retirement-original-scope-unavailable');
const base = (await git(['merge-base', release.head, forge.changeBase], repoPath)).trim();
if (base === release.head) throw new Error('retirement-original-scope-unavailable');
- const localPaths = (await git(['diff', '--name-only', '-z', base, release.head], repoPath)).split('\0').filter(Boolean);
+ const [localPathsOut, localPatch] = await Promise.all([
+ git(['diff', '--name-only', '-z', base, release.head], repoPath),
+ git(['diff', '--binary', '--no-ext-diff', '--no-textconv', base, release.head], repoPath),
+ ]);
+ const localPaths = localPathsOut.split('\0').filter(Boolean);
const paths = [...new Set([...forge.changedPaths, ...localPaths])].sort();
- const localPatch = await git(['diff', '--binary', '--no-ext-diff', '--no-textconv', base, release.head], repoPath);
const difference = paths.length ? await git(['--literal-pathspecs', 'diff', '--binary', '--no-ext-diff', '--no-textconv', release.head, forge.defaultHead, '--', ...paths], repoPath) : '';
// Historical ancestry/cherry equivalence does not establish that current
// code still implements the original work (it may have been strengthened or