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): record completed checkouts for claim runs in the source checkout (#13098)
[._.]/125e85060e9e
diff --git a/server/services/agentRepoStateVerification.js b/server/services/agentRepoStateVerification.jsindex 91786d8774..8f1968b734 100644--- a/server/services/agentRepoStateVerification.js+++ b/server/services/agentRepoStateVerification.js@@ -145,13 +145,12 @@ async function probeRepoState({ sourceWorkspace, branchName, worktreePath, branc// tree wedges the next `git worktree add` for that path. The cheap disk check// first, so the common leaked-directory case skips the `git worktree list` spawn.const probeWorktree = async () => {- const present = worktreePath ? await readCheckoutPathPresence(worktreePath) : null;+ // A pathless swarm branch (no completion receipt) is present while any checkout holds it.+ const present = worktreePath ? await readCheckoutPathPresence(worktreePath) : false;if (present !== false) return present;const worktrees = await listWorktrees(sourceWorkspace).catch(() => null);if (!worktrees) return null;- return worktrees.some(wt =>- wt.path === worktreePath- );+ return worktrees.some(wt => (worktreePath ? wt.path === worktreePath : wt.branch?.replace(/^refs\/heads\//, '') === branchName));};const probeLocalBranch = () => readLocalBranchPresence(sourceWorkspace, branchName);@@ -357,13 +356,17 @@ async function verifyRunBranches(input) {// A current branch holder is not evidence of the old claim's checkout.// Only the server's completion capture names its original incarnation.const original = meta.completedCheckouts?.find(row => row.branch === branch);- if (!original?.path) {+ // With no checkout and no local ref left, there is no incarnation to confuse:+ // only the delivery evidence remains to settle.+ const absent = !original?.path && !trees.some(tree => tree.branch?.replace(/^refs\/heads\//, '') === branch)+ && await readLocalBranchPresence(sourceWorkspace, branch).catch(() => null) === false;+ if (!original?.path && !absent) {results.push(await holdBranch(branch, 'claim-checkout-identity-unverified'));continue;}results.push({ branch, ...await verifyAgentBranchState({ ...input, prExpected: true,agentState: { ...input.agentState, metadata: { ...meta, isWorktree: true,- worktreeBranch: branch, sourceWorkspace, workspacePath: original.path } },+ worktreeBranch: branch, sourceWorkspace, workspacePath: original?.path ?? null } },}) });}return { verified: results.every(result => result.verified), branches: results,diff --git a/server/services/agentRepoStateVerification.test.js b/server/services/agentRepoStateVerification.test.jsindex c3310ffb30..66d27945de 100644--- a/server/services/agentRepoStateVerification.test.js+++ b/server/services/agentRepoStateVerification.test.js@@ -636,9 +636,24 @@ describe('verifyAgentRepoState — every hold records the reason it returns', ()expect(recordCleanupOutcome).toHaveBeenLastCalledWith('agent-1', { branch: 'claim/issue-11', status: 'blocked', reason: 'claim-ownership-unreadable', owners: [] });readClaimOwnershipAgents.mockResolvedValue([]);+ localBranch(true);expect((await run({ agentState: state })).branches).toEqual([{ branch: 'claim/issue-11', verified: false, skipReason: 'claim-checkout-identity-unverified', issues: [], recoveryTaskId: null }]);expect(recordCleanupOutcome).toHaveBeenLastCalledWith('agent-1', { branch: 'claim/issue-11', status: 'blocked', reason: 'claim-checkout-identity-unverified' });});++ it('settles a receipt-less swarm branch from delivery evidence once its checkout and ref are gone', async () => {+ const state = agentState({ isWorktree: false, worktreeBranch: null, workspacePath: '/repo', claimPicksOwnBranch: true, claimBranches: ['claim/issue-11'] });+ findPullRequestForBranch.mockResolvedValue({ status: 'found', url: 'https://github.com/example/repo/pull/11', detail: 'MERGED' });+ expect((await run({ agentState: state })).branches).toEqual([expect.objectContaining({ branch: 'claim/issue-11', skipReason: 'already-absent-preservation-unverified' })]);+ expect(recordCleanupOutcome).toHaveBeenLastCalledWith('agent-1', expect.objectContaining({ branch: 'claim/issue-11', path: null, status: 'already-absent' }));+ // A holder or an unreadable ref keeps the identity hold.+ listWorktrees.mockResolvedValue([{ ...RELEASE, branch: 'refs/heads/claim/issue-11' }]);+ expect((await run({ agentState: state })).branches[0].skipReason).toBe('claim-checkout-identity-unverified');+ listWorktrees.mockResolvedValue([]);+ readLocalBranchPresence.mockResolvedValue(null);+ expect((await run({ agentState: state })).branches[0].skipReason).toBe('claim-checkout-identity-unverified');+ expect(retireInventoriedCheckout).not.toHaveBeenCalled();+ });});describe('remediation coverage', () => {diff --git a/server/services/completedCheckoutEvidence.js b/server/services/completedCheckoutEvidence.jsindex 3eb603ce9e..88660a4fed 100644--- a/server/services/completedCheckoutEvidence.js+++ b/server/services/completedCheckoutEvidence.js@@ -42,6 +42,16 @@ export async function readCompletedCheckoutIdentity({ repoPath, branch, path, hereturn { repoPath: repository, branch, path: checkout, head, incarnation: { pointer, gitdir: { path: gitdir, ...administration } } };}+/** The repository a run's checkouts belong to. A claim run that picks its own+ * branch runs in the source checkout itself, so its workspace is the+ * repository and it records no separate sourceWorkspace.+ */+function runRepository(meta) {+ if (typeof meta?.sourceWorkspace === 'string' && meta.sourceWorkspace) return meta.sourceWorkspace;+ return isTruthyMeta(meta?.claimPicksOwnBranch) && meta.isWorktree !== true+ && typeof meta.workspacePath === 'string' && meta.workspacePath ? meta.workspacePath : null;+}+/** Called under the agent-state lock while the successful run still owns its* current claim bindings. Never manufacture evidence for historical releases.* Capture errors withhold cleanup authority without breaking completion.@@ -49,12 +59,14 @@ export async function readCompletedCheckoutIdentity({ repoPath, branch, path, heexport async function captureCompletedCheckouts(agent, { success } = {}) {const meta = agent?.metadata;if (success !== true || !['running', 'paused'].includes(agent?.status) || !agent.id- || !Number.isFinite(Date.parse(agent.startedAt)) || typeof meta?.sourceWorkspace !== 'string') return [];+ || !Number.isFinite(Date.parse(agent.startedAt))) return [];+ const repoPath = runRepository(meta);+ if (!repoPath) return [];const claim = isTruthyMeta(meta.claimPicksOwnBranch);const branches = claim ? meta.claimBranches : meta.isWorktree === true && meta.worktreeBranch ? [meta.worktreeBranch] : null;if (!Array.isArray(branches)) return [];const { listWorktrees } = await import('./worktreeManager.js');- const trees = await listWorktrees(meta.sourceWorkspace).catch(() => null);+ const trees = await listWorktrees(repoPath).catch(() => null);if (!trees) return [];const receipts = [];for (const branch of new Set(branches)) {@@ -65,7 +77,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: meta.sourceWorkspace, branch, path: holders[0].path, head: holders[0].head };+ const target = { repoPath, branch, path: holders[0].path, head: holders[0].head };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 });@@ -85,8 +97,9 @@ export async function hasCompletedCheckoutEvidence(records, target) {const branches = [meta?.worktreeBranch,...(Array.isArray(meta?.claimBranches) ? meta.claimBranches : []),...(Array.isArray(meta?.claimReleasedBranches) ? meta.claimReleasedBranches : [])];- if (!branches.includes(current.branch) || typeof meta?.sourceWorkspace !== 'string') return null;- return await realpath(meta.sourceWorkspace).catch(() => null) === current.repoPath ? agent : null;+ const repoPath = runRepository(meta);+ if (!branches.includes(current.branch) || !repoPath) return null;+ return await realpath(repoPath).catch(() => null) === current.repoPath ? agent : null;}))).filter(Boolean);return scoped.some(agent => agent?.status === 'completed' && agent.result?.success === true// A later run can reuse an existing checkout without changing its inode ordiff --git a/server/services/completedCheckoutEvidence.test.js b/server/services/completedCheckoutEvidence.test.jsindex fb8fdc10f2..fac558fd5c 100644--- a/server/services/completedCheckoutEvidence.test.js+++ b/server/services/completedCheckoutEvidence.test.js@@ -37,6 +37,15 @@ describe('completed claim checkout evidence', () => {expect(await captureCompletedCheckouts({ ...agent, metadata: { ...agent.metadata, workspacePath: repoPath } }, { success: true })).toEqual([]);});+ it('captures a claim run that works from the source checkout without a separate sourceWorkspace', async () => {+ agent.metadata = { claimPicksOwnBranch: true, isWorktree: false, workspacePath: repoPath, claimBranches: [branch] };+ const receipts = await captureCompletedCheckouts(agent, { success: true });+ expect(receipts).toEqual([expect.objectContaining({ branch, head, path: expect.stringMatching(/claim$/) })]);+ expect(await hasCompletedCheckoutEvidence([completed(receipts)], target())).toBe(true);+ // An ordinary worktree run's workspace is its own checkout, never the repository.+ expect(await captureCompletedCheckouts({ ...agent, metadata: { isWorktree: true, workspacePath: repoPath, worktreeBranch: branch } }, { success: true })).toEqual([]);+ });+it('rejects a new checkout reusing the completed branch, path and exact head', async () => {const receipts = await captureCompletedCheckouts(agent, { success: true });expect(receipts).toHaveLength(1);