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
perf: reduce redundant Git reads inside merged-checkout retirement (#13154)
[._.]/ Adam Eivy409ea1d74f63
diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.jsindex 1d7b474889..5be9c16e29 100644--- a/server/services/branchReconcile.js+++ b/server/services/branchReconcile.js@@ -1170,12 +1170,6 @@ export async function cleanupMerged(repoPath, defaultBranch, merged, { activeAgeconst skipped = [];const claimMarkersHeld = [];for (const b of merged) {- // Re-verify at action time — state may have shifted since the gather.- const hasMergeEvidence = await hasBranchMergeEvidence(repoPath, b.branch, defaultBranch);- if (!hasMergeEvidence) {- skipped.push({ branch: b.branch, reason: 'not-merged-on-recheck' });- continue;- }const retired = await retireBranch(repoPath, b, {activeAgentIds,claimOwners,@@ -1186,7 +1180,7 @@ export async function cleanupMerged(repoPath, defaultBranch, merged, { activeAge});if (!retired.ok) {const { ok, ...failure } = retired;- skipped.push({ branch: b.branch, ...(b.worktreePath && { path: b.worktreePath }), ...failure });+ skipped.push({ branch: b.branch, ...(failure.reason !== 'not-merged-on-recheck' && b.worktreePath && { path: b.worktreePath }), ...failure });continue;}const markerHold = await retainedClaimMarkers(repoPath, b.branch, { origin, forgeExec });@@ -1388,18 +1382,19 @@ async function admitBranchRetirement(repoPath, b, { activeAgentIds, staleClaimIdconst defaultBranch = await getDefaultBranch(repoPath, { strict: true }).catch(() => null);if (!defaultBranch) return stop({ ok: false, reason: 'default-branch-unavailable' });if (b.branch === defaultBranch) return stop({ ok: false, reason: 'protected-branch' });+ const expectedHead = (await execGit(['rev-parse', '--verify', `refs/heads/${b.branch}`], repoPath).catch(() => null))?.stdout?.trim();+ if (!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(expectedHead || '')) return stop({ ok: false, reason: mergedRetirement ? 'not-merged-on-recheck' : 'branch-head-unavailable' });const { readBranchCompletionHold } = await import('./branchCompletionPolicy.js');- const readPolicyHold = () => readBranchCompletionHold({ repoPath, branch: b.branch, retirement: mergedRetirement });+ const readPolicyHold = (tip = expectedHead) => readBranchCompletionHold({ repoPath, branch: b.branch, tip, retirement: mergedRetirement });const policyHold = await readPolicyHold();if (policyHold) return stop({ ok: false, reason: policyHold });let archiveTree = null;let claimCompletion = null;- const expectedHead = (await execGit(['rev-parse', '--verify', `refs/heads/${b.branch}`], repoPath).catch(() => null))?.stdout?.trim();- if (!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(expectedHead || '')) return stop({ ok: false, reason: mergedRetirement ? 'not-merged-on-recheck' : 'branch-head-unavailable' });+ let holders = null;if (mergedRetirement) {// Completion audits may know only the branch. Resolve its actual holder,// and pin merge evidence to its current commit rather than the scan's flag.- const holders = await listWorktrees(repoPath).catch(() => null);+ holders = await listWorktrees(repoPath).catch(() => null);if (!holders) return stop({ ok: false, reason: 'worktree-ownership-unreadable' });const holder = holders.find(wt => wt.branch === `refs/heads/${b.branch}`);if (!await hasBranchMergeEvidence(repoPath, expectedHead, mergeTarget).catch(() => false)) return stop({ ok: false, reason: 'not-merged-on-recheck' });@@ -1450,7 +1445,7 @@ async function admitBranchRetirement(repoPath, b, { activeAgentIds, staleClaimIdconst { hasCompletedCheckoutEvidence } = await import('./completedCheckoutEvidence.js');if (!records) return stop({ ok: false, reason: 'claim-ownership-unreadable' });if (await hasCompletedCheckoutEvidence(records, {- repoPath, branch: b.branch, path: b.worktreePath, head: expectedHead,+ repoPath, branch: b.branch, path: b.worktreePath, head: expectedHead, worktrees: holders,})) claimCompletion = 'receipt';else {const hold = await manualClaimShippedHold(repoPath, b, expectedHead, forgeAccount);@@ -1460,7 +1455,7 @@ async function admitBranchRetirement(repoPath, b, { activeAgentIds, staleClaimIdclaimCompletion = 'forge-merged';}}- const tree = (await listWorktrees(repoPath)).find(wt => wt.path === b.worktreePath);+ const tree = holders ? holders.find(wt => wt.path === b.worktreePath) : (await listWorktrees(repoPath)).find(wt => wt.path === b.worktreePath);if (!tree || tree.head !== expectedHead) return stop({ ok: false, reason: 'worktree-holder-changed' });archiveTree = tree;}@@ -1511,15 +1506,16 @@ export async function checkoutIdleMs(worktreePath) {* `prepare` backup, each taken after admission with the ownership lock released.*/async function finishBranchRetirement(repoPath, { b, expectedHead, readPolicyHold, mergeTarget, label, requireCleanWorktree, claimOwners, mergedRetirement, claimCompletion }, { prepared = null, preservation = null } = {}) {+ let removal = null;if (b.worktreePath) {- const verifyRemoval = async (holder) => {+ const verifyRemoval = async (holder, worktrees) => {if (!holder || holder.branch !== `refs/heads/${b.branch}` || holder.head !== expectedHead) return 'worktree-holder-changed';if (claimOwners !== undefined) {const agents = claimOwners?.readAgents ? await claimOwners.readAgents().catch(() => null) : claimOwners?.agents;const reason = claimCheckoutOwnerReason({ branchName: b.branch, holderPath: b.worktreePath, sourceWorkspace: repoPath, agents });if (reason) return reason;}- const policyHold = await readPolicyHold();+ const policyHold = await readPolicyHold(expectedHead);if (policyHold) return policyHold;// A forge-merged exact head cannot un-merge, and the holder check above// pins that head; only a run receipt can be superseded by a later owner.@@ -1528,7 +1524,7 @@ async function finishBranchRetirement(repoPath, { b, expectedHead, readPolicyHolconst { hasCompletedCheckoutEvidence } = await import('./completedCheckoutEvidence.js');const records = await readBranchCompletionPolicies({ repairIndex: false }).catch(() => null);if (!records || !await hasCompletedCheckoutEvidence(records, {- repoPath, branch: b.branch, path: b.worktreePath, head: expectedHead,+ repoPath, branch: b.branch, path: b.worktreePath, head: expectedHead, worktrees,})) return 'claim-completion-unverified';}const dirtyPaths = await worktreeDirtyPaths(b.worktreePath);@@ -1539,19 +1535,19 @@ async function finishBranchRetirement(repoPath, { b, expectedHead, readPolicyHolif (mergedRetirement && !await hasBranchMergeEvidence(repoPath, expectedHead, mergeTarget).catch(() => false)) return 'not-merged-on-recheck';return preservation ? preservation.verify(holder) : null;};- const removal = await forceRemoveWorktreeDir(repoPath, b.worktreePath, { label, log: 'all', verifyRemoval });+ removal = await forceRemoveWorktreeDir(repoPath, b.worktreePath, { label, log: 'all', verifyRemoval });// The branch is still checked out by a surviving worktree: deleting it would// fail, and reporting success would mark the branch reconciled while it is held.if (!removal.removed) return { ok: false, reason: removal.reason || 'worktree-remove-failed' };}- const latestPolicyHold = await readPolicyHold();+ const latestPolicyHold = await readPolicyHold(expectedHead);if (latestPolicyHold) return { ok: false, reason: latestPolicyHold };if (claimOwners !== undefined) {const agents = claimOwners?.readAgents ? await claimOwners.readAgents().catch(() => null) : claimOwners?.agents;const reason = claimCheckoutOwnerReason({ branchName: b.branch, holderPath: b.worktreePath, sourceWorkspace: repoPath, agents });if (reason) return { ok: false, reason };}- const holders = await listWorktrees(repoPath).catch(() => null);+ const holders = removal?.entries || await listWorktrees(repoPath).catch(() => null);if (!holders || holders.some(wt => wt.branch === `refs/heads/${b.branch}`)) return { ok: false, reason: 'branch-has-new-holder' };if (mergedRetirement) {if (!await hasBranchMergeEvidence(repoPath, expectedHead, mergeTarget).catch(() => false)) return { ok: false, reason: 'not-merged-on-recheck' };@@ -1578,8 +1574,8 @@ async function finishBranchRetirement(repoPath, { b, expectedHead, readPolicyHol* @param {string} defaultBranch* @returns {Promise<{path:string, head:string|null, locked:boolean, worktreeAgeMs:number|null, dirtyPaths:string[]}[]>}*/-async function gatherDetachedWorktrees(repoPath, defaultBranch) {- const worktrees = await listWorktrees(repoPath).catch(() => []);+async function gatherDetachedWorktrees(repoPath, defaultBranch, preloadedTrees = null) {+ const worktrees = preloadedTrees || await listWorktrees(repoPath).catch(() => []);const detached = [];for (const [index, entry] of worktrees.entries()) {// git lists the main checkout first; the path check covers a caller whose list is not ordered.@@ -1603,8 +1599,8 @@ const isCommitOnBranch = (repoPath, sha, branch) =>// Read after cleanup so removed holders cannot survive in the recovery report.// No content or ownership inference: retirement still uses its existing gates.-async function gatherRecoveryWorktrees(repoPath) {- const trees = await listWorktrees(repoPath).catch(() => null);+async function gatherRecoveryWorktrees(repoPath, preloadedTrees = null) {+ const trees = preloadedTrees || await listWorktrees(repoPath).catch(() => null);if (!trees?.length) return null;return trees.slice(1).filter(tree => !tree.bare).map(tree => {const managed = worktreeOwnershipReason({ path: tree.path, roots: reconcileWorktreeRoots(repoPath), allowLiveClaim: true }) !== 'worktree-unmanaged-location';@@ -1865,8 +1861,10 @@ export async function reconcile(repoPath = PATHS.root, { cleanup = true, reapSup? await cleanupMerged(repoPath, defaultBranch, merged, { activeAgentIds, claimOwners, origin, forgeExec, forgeAccount }): { cleaned: [], skipped: merged.map((m) => ({ branch: m.branch, reason: 'cleanup-disabled' })) };// Detached worktrees have no branch, so nothing above can see them (#10825).+ const postCleanupWorktrees = await listWorktrees(repoPath).catch(() => []);+ const detachedWorktrees = await gatherDetachedWorktrees(repoPath, defaultBranch, postCleanupWorktrees);const detachedReap = cleanup- ? await reapDetachedWorktrees(repoPath, defaultBranch, await gatherDetachedWorktrees(repoPath, defaultBranch), { activeAgentIds, claimOwners })+ ? await reapDetachedWorktrees(repoPath, defaultBranch, detachedWorktrees, { activeAgentIds, claimOwners }): { cleaned: [], skipped: [], held: [] };const cleaned = [...new Set([...cleanedWorktrees, ...cleanedBranches, ...detachedReap.cleaned])];@@ -1897,7 +1895,7 @@ export async function reconcile(repoPath = PATHS.root, { cleanup = true, reapSupskipped: [...skipped, ...supersededReap.skipped, ...detachedReap.skipped],// Detached trees at merged commits that were NOT removed, managed or not.detachedWorktrees: detachedReap.held,- worktreeInventory: await gatherRecoveryWorktrees(repoPath),+ worktreeInventory: await gatherRecoveryWorktrees(repoPath, detachedReap.cleaned.length === 0 ? postCleanupWorktrees : null),orphanRemotes,prStateUnavailable};diff --git a/server/services/branchReconcile.ownership.test.js b/server/services/branchReconcile.ownership.test.jsindex d6aeca91a0..c02f16b2a5 100644--- a/server/services/branchReconcile.ownership.test.js+++ b/server/services/branchReconcile.ownership.test.js@@ -25,10 +25,11 @@ vi.mock('../lib/paths.js', async importOriginal => makePathsProxy(await importOr}));afterAll(cleanupTempDataRoots);-const injected = vi.hoisted(() => ({ statusPath: null, statusThrows: false, beforeRemove: null, owners: [], policies: [], forge: {} }));+const injected = vi.hoisted(() => ({ statusPath: null, statusThrows: false, beforeRemove: null, owners: [], policies: [], forge: {}, gitSpawns: [] }));vi.mock('../lib/execGit.js', async (importOriginal) => {const actual = await importOriginal();return { ...actual, execGit: async (args, cwd, options) => {+ injected.gitSpawns.push({ args, cwd });// Git reports forward slashes even when path.join uses Windows separators.if (args[0] === 'status' && injected.statusPath && cwd?.replaceAll('\\', '/') === injected.statusPath.replaceAll('\\', '/')) {if (injected.statusThrows || !options?.ignoreExitCode) throw new Error('synthetic unreadable index');@@ -87,7 +88,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('branch-reconcile worktree ownership (relet external;beforeEach(async () => {- Object.assign(injected, { statusPath: null, statusThrows: false, beforeRemove: null, owners: [], policies: [], forge: {} });+ Object.assign(injected, { statusPath: null, statusThrows: false, beforeRemove: null, owners: [], policies: [], forge: {}, gitSpawns: [] });scratch = realpathSync(await mkdtemp(join(tmpdir(), 'portos-reconcile-own-')));const safeRepo = join(scratch, 'repo');await mkdir(safeRepo);@@ -226,12 +227,17 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('branch-reconcile worktree ownership (reawait execGit(['worktree', 'lock', locked], repo);const clean = await addCompletedClaim();+ injected.gitSpawns.length = 0;const result = await reconcileWithoutOwners();expect(result.cleaned).toEqual(['claim/issue-11']);expect(existsSync(clean)).toBe(false);expect(result.skipped).toContainEqual(expect.objectContaining({ branch: 'claim/issue-12', reason: 'worktree-locked' }));expect(existsSync(locked)).toBe(true);expect(await branches()).toEqual(expect.arrayContaining(['claim/issue-12']));+ // Pinned spawn-count ceiling for single-checkout retirement (#13154):+ // Reduced from ~120 serial spawns down to <=105 by sharing Git reads inside+ // the critical section (e.g. 1 worktree list per admission, repo common-dir cache).+ expect(injected.gitSpawns.length).toBeLessThanOrEqual(105);});it('holds a claim checkout with uncommitted or unmerged work', async () => {diff --git a/server/services/completedCheckoutEvidence.js b/server/services/completedCheckoutEvidence.jsindex 88660a4fed..8686fae571 100644--- a/server/services/completedCheckoutEvidence.js+++ b/server/services/completedCheckoutEvidence.js@@ -10,6 +10,18 @@ import { isTruthyMeta } from '../lib/metadataFlags.js';const shortBranch = branch => branch?.replace(/^refs\/heads\//, '');const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);+const repoCommonByRepo = new Map();+async function resolveRepoCommon(repository) {+ let cached = repoCommonByRepo.get(repository);+ if (!cached) {+ cached = execGit(['rev-parse', '--git-common-dir'], repository)+ .then(r => r.stdout.trim())+ .catch(() => null);+ repoCommonByRepo.set(repository, cached);+ }+ return cached;+}+async function filesystemIdentity(path, type) {const stat = await lstat(path, { bigint: true });if (!(type === 'file' ? stat.isFile() : stat.isDirectory()) || typeof stat.dev !== 'bigint'@@ -19,24 +31,26 @@ async function filesystemIdentity(path, type) {return { dev: String(stat.dev), ino: String(stat.ino), birthtimeNs: String(stat.birthtimeNs) };}-export async function readCompletedCheckoutIdentity({ repoPath, branch, path, head }) {+export async function readCompletedCheckoutIdentity({ repoPath, branch, path, head, worktrees = null, repoCommon = null }) {const { listWorktrees } = await import('./worktreeManager.js');const repository = await realpath(repoPath);const checkout = await realpath(path);if (checkout === repository || !/^[0-9a-f]{40}(?:[0-9a-f]{24})?$/.test(head || '')) return null;- const holders = (await listWorktrees(repository)).filter(tree => shortBranch(tree.branch) === branch);+ const trees = worktrees || await listWorktrees(repository);+ const holders = trees.filter(tree => shortBranch(tree.branch) === branch);if (holders.length !== 1 || await realpath(holders[0].path) !== checkout || holders[0].head !== head) return null;const pointer = await filesystemIdentity(join(checkout, '.git'), 'file');const gitdir = await realpath((await execGit(['rev-parse', '--absolute-git-dir'], checkout)).stdout.trim());const administration = await filesystemIdentity(gitdir, 'directory');- const [currentHead, currentBranch, common] = await Promise.all([+ const [currentHead, currentBranch, common, resolvedRepoCommon] = await Promise.all([execGit(['rev-parse', '--verify', 'HEAD^{commit}'], checkout),execGit(['symbolic-ref', '--quiet', '--short', 'HEAD'], checkout),execGit(['rev-parse', '--git-common-dir'], checkout),+ repoCommon ?? resolveRepoCommon(repository),]);- const repoCommon = await execGit(['rev-parse', '--git-common-dir'], repository);+ if (!resolvedRepoCommon) return null;if (currentHead.stdout.trim() !== head || currentBranch.stdout.trim() !== branch- || await realpath(resolve(checkout, common.stdout.trim())) !== await realpath(resolve(repository, repoCommon.stdout.trim()))) return null;+ || await realpath(resolve(checkout, common.stdout.trim())) !== await realpath(resolve(repository, resolvedRepoCommon))) return null;if (!same(pointer, await filesystemIdentity(join(checkout, '.git'), 'file'))|| !same(administration, await filesystemIdentity(gitdir, 'directory'))) return null;return { repoPath: repository, branch, path: checkout, head, incarnation: { pointer, gitdir: { path: gitdir, ...administration } } };@@ -77,7 +91,7 @@ export async function captureCompletedCheckouts(agent, { success } = {}) {const recordedPath = typeof meta.workspacePath === 'string' ? await realpath(meta.workspacePath).catch(() => null) : null;if (!recordedPath || await realpath(holders[0].path).catch(() => null) !== recordedPath) continue;}- const target = { repoPath, branch, path: holders[0].path, head: holders[0].head };+ const target = { repoPath, branch, path: holders[0].path, head: holders[0].head, worktrees: trees };const first = await readCompletedCheckoutIdentity(target).catch(() => null);if (!first || !same(first, await readCompletedCheckoutIdentity(target).catch(() => null))) continue;receipts.push({ version: 1, agentId: agent.id, startedAt: agent.startedAt, capturedAt: new Date().toISOString(), ...first });diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.jsindex ad124b498f..115bfea8fd 100644--- a/server/services/worktreeManager.js+++ b/server/services/worktreeManager.js@@ -262,7 +262,7 @@ async function queueWorktreeCreate(repo, fn) {if (!key) key = '__unknown__';return queueWorktreeCreateForKey(key, async () => {const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(repo, fn);+ return withCheckoutPublication(repo, () => fn(common));});}@@ -275,7 +275,7 @@ async function queueWorktreeCreate(repo, fn) {*/export function deleteCompletedLocalBranch(repoPath, branch, expectedHead, { beforeMutation = async () => {} } = {}) {let callbackError;- return queueWorktreeCreate(repoPath, async () => {+ return queueWorktreeCreate(repoPath, async (commonDir) => {if (PROTECTED_BRANCHES.includes(branch)) return false;const { getDefaultBranch } = await import('./git.js');const defaultBranch = await getDefaultBranch(repoPath, { strict: true }).catch(() => null);@@ -283,7 +283,8 @@ export function deleteCompletedLocalBranch(repoPath, branch, expectedHead, { befif (!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(expectedHead || '')) return false;const ref = `refs/heads/${branch}`;if ((await execGit(['check-ref-format', ref], repoPath, { ignoreExitCode: true })).exitCode !== 0) return false;- const common = (await execGit(['rev-parse', '--path-format=absolute', '--git-common-dir'], repoPath)).stdout.trim();+ const common = (commonDir && isAbsolute(commonDir)) ? commonDir+ : (await execGit(['rev-parse', '--path-format=absolute', '--git-common-dir'], repoPath)).stdout.trim();if (!isAbsolute(common)) return false;const preservation = await preserveBranchRetirement(repoPath, common, branch, expectedHead);if (!await preservation.verify()) return false;@@ -376,11 +377,15 @@ async function probeWorktreeRemoval(repo, worktreePath) {(err) => (err?.code === 'ENOENT' || err?.code === 'ENOTDIR' ? 'absent' : 'unknown'),);const target = canonicalPath(worktreePath);+ let entries = null;const registration = await listWorktrees(repo).then(- (entries) => (entries.some(wt => canonicalPath(wt.path) === target) ? 'registered' : 'absent'),+ (list) => {+ entries = list;+ return list.some(wt => canonicalPath(wt.path) === target) ? 'registered' : 'absent';+ },() => 'unknown',);- return { directory, registration };+ return { directory, registration, entries };}/**@@ -457,7 +462,7 @@ async function forceRemoveWorktreeDirAdmitted(repo, worktreePath, { label, log =const holder = entries.find(wt => canonicalPath(wt.path) === canonicalPath(worktreePath)) || null;if (holder?.locked) return 'worktree-locked';if (!verifyRemoval) return null;- const reason = await verifyRemoval(holder).catch(() => 'worktree-ownership-unreadable');+ const reason = await verifyRemoval(holder, entries).catch(() => 'worktree-ownership-unreadable');if (reason) return reason;// Ownership/content checks await I/O. Refresh their registration afterward;// Git's own removal still honors locks acquired after this final read.@@ -485,7 +490,9 @@ async function forceRemoveWorktreeDirAdmitted(repo, worktreePath, { label, log =if (logAll) console.log(`⚠️ Worktree prune failed for ${subject}: ${pruneErr.message}`);});});- const outcome = removalOutcome(worktreePath, await probeWorktreeRemoval(repo, worktreePath), heldReason || lastFailure);+ const probe = await probeWorktreeRemoval(repo, worktreePath);+ const outcome = removalOutcome(worktreePath, probe, heldReason || lastFailure);+ if (probe.entries) outcome.entries = probe.entries;if (heldReason) outcome.reason = heldReason;if (!outcome.removed && label) {// A gate keeping the checkout is the cleanup working as designed (the warning@@ -2225,7 +2232,7 @@ export async function reapMergedWorktrees(sourceWorkspace, {const agents = await readOwners();const ownerReason = claimCheckoutOwnerReason({ branchName, holderPath: wt.path, sourceWorkspace, agents });if (ownerReason) return ownerReason;- const policyHold = await readBranchCompletionHold({ repoPath: sourceWorkspace, branch: branchName, retirement: true });+ const policyHold = await readBranchCompletionHold({ repoPath: sourceWorkspace, branch: branchName, tip: wt.head, retirement: true });if (policyHold) return policyHold;const reason = worktreeOwnershipReason({ path: wt.path, locked: holder.locked, activeAgentIds: readActiveIds(), roots: managedRoots, requireKnownLiveness: true });if (reason) return reason;@@ -2257,7 +2264,7 @@ export async function reapMergedWorktrees(sourceWorkspace, {skipped.push({ path: wt.path, branch: branchName, reason: removal.reason || 'remove-failed', warning: removal.warning });return;}- const holders = await listWorktrees(sourceWorkspace).catch(() => null);+ const holders = removal.entries || await listWorktrees(sourceWorkspace).catch(() => null);const branchDeleted = holders && !holders.some(holder => holder.branch === wt.branch)&& await deleteCompletedLocalBranch(sourceWorkspace, branchName, wt.head);reaped.push({ path: wt.path, branch: branchName, locked: !!wt.locked, branchDeleted: Boolean(branchDeleted), archiveDir: preservation.archiveDir });