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: recover integrated claims through exact retirement review
[._.]/ Adam Eivy4d37772cd7b2
diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.jsindex 6f4575f212..8cb1c27023 100644--- a/server/services/branchReconcile.js+++ b/server/services/branchReconcile.js@@ -1793,7 +1793,7 @@ async function reapDetachedWorktrees(repoPath, defaultBranch, detached, { active* account pin, so an app whose repo belongs to another GitHub account is* polled with a credential that can actually see it (#7540); unset, the* owner-match in `forgeAuth.resolveForgeForRepo` still applies.- * @returns {Promise<{ defaultBranch:string, cleaned:string[], inFlight:object[], wip:object[], skipped:{branch:string,reason:string}[], orphanRemotes:{reaped:string[],reported:object[]}, forgeUnavailable?:boolean, prStateUnavailable?:boolean }>}+ * @returns {Promise<{ defaultBranch:string, cleaned:string[], inFlight:object[], wip:object[], merged:object[], skipped:{branch:string,reason:string}[], orphanRemotes:{reaped:string[],reported:object[]}, forgeUnavailable?:boolean, prStateUnavailable?:boolean }>}* A `wip` entry with a `liveOwnerReason` is held because a live agent/claim/lock* owns it — the "leave it alone, this reconcile IS done" case. `superseded` holds* only the verified-superseded branches the reap could NOT take this cycle; the@@ -1938,6 +1938,9 @@ export async function reconcile(repoPath = PATHS.root, { cleanup = true, reapSup// the coordinator prompt.superseded: supersededReap.held,wip,+ // Retained MERGED claims may need their completed owner's cleanup policy+ // reviewed. They are not WIP, so do not hide them from that second stage.+ merged: merged.filter(branch => skipped.some(row => row.branch === branch.branch)),skipped: [...skipped, ...supersededReap.skipped, ...detachedReap.skipped],// Detached trees at merged commits that were NOT removed, managed or not.detachedWorktrees: detachedReap.held,diff --git a/server/services/branchReconcile.ownership.test.js b/server/services/branchReconcile.ownership.test.jsindex bda9585a86..e2f2788a77 100644--- a/server/services/branchReconcile.ownership.test.js+++ b/server/services/branchReconcile.ownership.test.js@@ -185,6 +185,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('branch-reconcile worktree ownership (re// Age and registry absence do not prove a manual claim finished.expect(result.cleaned, context).not.toContain('claim/issue-5');expect(reasonFor('claim/issue-5'), context).toBe('claim-completion-unverified');+ expect(result.merged).toContainEqual(expect.objectContaining({ branch: 'claim/issue-5', state: 'MERGED' }));expect(existsSync(staleClaim)).toBe(true);expect(reasonFor('claim/issue-6'), context).toBe('worktree-locked');expect(existsSync(lockedClaim)).toBe(true);diff --git a/server/services/branchReconcileRetirement.js b/server/services/branchReconcileRetirement.jsindex f26c0c1ebb..21a79aac91 100644--- a/server/services/branchReconcileRetirement.js+++ b/server/services/branchReconcileRetirement.js@@ -8,6 +8,13 @@ async function mergedPrUrl(app, branch, tip) {return (await findMergedPullRequest(app.repoPath, branch, tip, { forgeAccount: app.forgeAccount ?? null })).url;}+/** The manual-claim grace gate is not a completed run's cleanup handoff. Only+ * that one refusal is eligible for a fresh, exact-head policy review below. */+export function retainedMergedClaims(result) {+ return (result.merged || []).filter(branch => result.skipped?.some(row =>+ row.branch === branch.branch && row.reason === 'claim-completion-unverified'));+}+/** Inventory is persisted first: the same preview used by the live endpoint* rejects changed owners, policies, checkout identity and missing proof objects.*/@@ -17,8 +24,10 @@ export async function prepareReconcileRetirements(app, result, deps = {}) {const retired = [];const readPr = deps.readPr || mergedPrUrl;const prepare = deps.prepare || (await import('./heldCheckoutRecovery.js')).prepareInventoriedCheckoutRetirement;- for (const branch of result.wip || []) {- if (branch.rule !== 'completion-policy' || branch.completionPolicyReason !== 'pr-completion-review-required'+ const mergedClaims = retainedMergedClaims(result);+ for (const branch of [...(result.wip || []), ...mergedClaims]) {+ const mergedClaim = mergedClaims.includes(branch);+ if ((!mergedClaim && (branch.rule !== 'completion-policy' || branch.completionPolicyReason !== 'pr-completion-review-required'))|| branch.liveOwnerReason || branch.worktreeDirty || branch.worktreeStatusUnavailable) continue;if (branch.openPr && !staleBehindOpenPr(branch)) continue;const tree = result.worktreeInventory?.find(row => row.branch === branch.branch && row.path === branch.worktreePath);@@ -29,7 +38,7 @@ export async function prepareReconcileRetirements(app, result, deps = {}) {// Every commit is already on the default branch, so no PR identity or owner// history is needed to prove nothing is lost: archive and retire it. The// service re-proves merge evidence and ownership under its fence.- if (branch.isMerged === true) {+ if (branch.isMerged === true && !mergedClaim) {const retire = deps.retire || (await import('./heldCheckoutRecovery.js')).retireInventoriedCheckout;const outcome = await retire({ appId: app.id, release },{ beforeMutation: assertAutomaticRetirementAllowed, settleMergedReviewObligation: true })@@ -47,6 +56,14 @@ export async function prepareReconcileRetirements(app, result, deps = {}) {// The preview's policy gate requires completed successful legacy review// owners. An empty policy set after a concurrent change is not that proof.if (!prepared.review?.policy?.policies?.length) { held.push({ ...release, reason: 'retirement-policy-changed' }); continue; }+ if (mergedClaim) {+ // The existing review policy requires a terminal exact-bound owner;+ // an empty registry, merged PR alone or the old grace clock grants+ // nothing. Keep this digest-bound policy proof through final removal.+ if (prepared.review.forge?.prHead !== release.head || !prepared.review.policy.integratedCleanupHead) {+ held.push({ ...release, reason: 'claim-completion-unverified' }); continue;+ }+ }if (!['integrated', 'review-supersession'].includes(prepared.review.comparison?.disposition)|| typeof prepared.review.comparison.localPatch !== 'string' || typeof prepared.review.comparison.difference !== 'string') {held.push({ ...release, reason: 'retirement-comparison-unavailable' }); continue;diff --git a/server/services/branchReconcileRetirement.test.js b/server/services/branchReconcileRetirement.test.jsindex 8103f2db1c..2f00560ba1 100644--- a/server/services/branchReconcileRetirement.test.js+++ b/server/services/branchReconcileRetirement.test.js@@ -12,6 +12,8 @@ const branch = { branch: 'claim/issue-42', tip: head, worktreePath: '/example/chrule: 'completion-policy', completionPolicyReason: 'pr-completion-review-required' };const tree = { branch: branch.branch, path: branch.worktreePath, head, managed: true, locked: false, lockReason: null };const scan = (b = {}, t = {}) => ({ wip: [{ ...branch, ...b }], worktreeInventory: [{ ...tree, ...t }] });+const retainedMerged = () => ({ wip: [], merged: [{ ...branch, state: 'MERGED', rule: 'merged', isMerged: true }],+ skipped: [{ branch: branch.branch, reason: 'claim-completion-unverified' }], worktreeInventory: [tree] });const setup = () => ({readPr: vi.fn(async () => 'https://github.com/example/repo/pull/42'),prepare: vi.fn(async () => ({ ok: true, review: { digest: 'd'.repeat(64), policy: { policies: [{ id: 'old-owner', status: 'completed', success: true }] }, comparison: { disposition: 'review-supersession', localPatch: '+old', difference: '+new' }, forge: { prHead: head } } })),@@ -19,6 +21,41 @@ const setup = () => ({describe('historical completed-checkout review obligations', () => {beforeEach(() => forge.read.mockReset());+ it('routes a retained merged claim through its known completed-owner policy before full archival retirement', async () => {+ const deps = setup();+ deps.prepare.mockResolvedValue({ ok: true, review: { digest: 'd'.repeat(64), forge: { prHead: head },+ policy: { integratedCleanupHead: 'c'.repeat(40), policies: [{ id: 'known-owner', status: 'completed', success: false }] },+ comparison: { disposition: 'integrated', localPatch: '+original', difference: '' } } });+ deps.retire = vi.fn(async () => ({ ok: true, branchDeleted: true, archiveDir: '/private/verified-archive' }));+ expect(await prepareReconcileRetirements(app, retainedMerged(), deps)).toMatchObject({ candidates: [], held: [],+ retired: [expect.objectContaining({ branch: branch.branch, head })] });+ expect(deps.prepare).toHaveBeenCalledOnce();+ expect(deps.retire).toHaveBeenCalledWith({ appId: app.id, release: expect.objectContaining({ head }),+ review: { prUrl: 'https://github.com/example/repo/pull/42', digest: 'd'.repeat(64), acceptSuperseded: false } },+ { beforeMutation: expect.any(Function) });+ deps.retire.mockClear();+ deps.prepare.mockResolvedValue({ ok: true, review: { digest: 'e'.repeat(64), forge: { prHead: head },+ policy: { integratedCleanupHead: 'c'.repeat(40), policies: [{ id: 'known-owner', status: 'completed', success: false }] },+ comparison: { disposition: 'review-supersession', localPatch: '+original', difference: '+replacement' } } });+ expect(await prepareReconcileRetirements(app, retainedMerged(), deps)).toMatchObject({ candidates: [{ state: 'REVIEW_RETIREMENT' }], retired: [] });+ expect(deps.retire).not.toHaveBeenCalled();+ });++ it('does not turn registry silence or another merged-cleanup refusal into a completed-owner release', async () => {+ const deps = setup();+ deps.retire = vi.fn();+ deps.prepare.mockResolvedValue({ ok: true, review: { forge: { prHead: head }, policy: { policies: [] } } });+ expect(await prepareReconcileRetirements(app, retainedMerged(), deps)).toMatchObject({+ retired: [], held: [{ reason: 'retirement-policy-changed' }],+ });+ deps.prepare.mockClear();+ for (const reason of ['claim-owner-active', 'pr-completion-human-handoff', 'archive-failed', 'worktree-unmanaged-location']) {+ const result = retainedMerged(); result.skipped[0].reason = reason;+ expect(await prepareReconcileRetirements(app, result, deps)).toEqual({ candidates: [], held: [], retired: [] });+ }+ expect(deps.prepare).not.toHaveBeenCalled();+ expect(deps.retire).not.toHaveBeenCalled();+ });it('archives and retires a checkout whose commits are all on the default branch without a PR read or dispatch', async () => {const deps = setup();deps.retire = vi.fn(async () => ({ ok: true, branchDeleted: true, archiveDir: '/private/verified-archive' }));diff --git a/server/services/cosTaskPreStepBlocks.compose.test.js b/server/services/cosTaskPreStepBlocks.compose.test.jsindex ae1d1313f5..ef5f4c5bd1 100644--- a/server/services/cosTaskPreStepBlocks.compose.test.js+++ b/server/services/cosTaskPreStepBlocks.compose.test.js@@ -260,18 +260,22 @@ describe('prepareManagedAppImprovementTask composes the extracted pre-step layer]) }));});- it('a normal user reconcile drains machine-proven retirement without generating an agent', async () => {+ it.each(['completion-policy', 'merged-claim'])('a normal user reconcile drains %s retirement without generating an agent', async kind => {const release = { branch: 'claim/issue-1', path: '/example/checkouts/1', head: 'a'.repeat(40), managed: true, locked: false };- reconcileMock.mockResolvedValue(scan({ inFlight: [], wip: [{ branch: release.branch, worktreePath: release.path, tip: release.head,- rule: 'completion-policy', completionPolicyReason: 'pr-completion-review-required' }], worktreeInventory: [release] }));+ const branch = { branch: release.branch, worktreePath: release.path, tip: release.head,+ rule: 'completion-policy', completionPolicyReason: 'pr-completion-review-required' };+ reconcileMock.mockResolvedValue(scan({ inFlight: [], wip: kind === 'completion-policy' ? [branch] : [],+ merged: kind === 'merged-claim' ? [{ ...branch, state: 'MERGED', isMerged: true }] : [],+ skipped: kind === 'merged-claim' ? [{ ...release, reason: 'claim-completion-unverified' }] : [], worktreeInventory: [release] }));mergedPrRead.mockResolvedValue('https://github.com/example/repo/pull/1');- prepareRetirement.mockResolvedValue({ ok: true, review: { digest: 'd'.repeat(64), policy: { policies: [{ id: 'completed-owner' }] },+ prepareRetirement.mockResolvedValue({ ok: true, review: { digest: 'd'.repeat(64), policy: { integratedCleanupHead: 'c'.repeat(40), policies: [{ id: 'completed-owner' }] },forge: { prHead: release.head }, comparison: { disposition: 'integrated', localPatch: '', difference: '' } } });expect(await generate({ onDemand: true })).toBeNull();expect(retireCheckout).toHaveBeenCalledOnce();expect(retireCheckout.mock.calls[0][0].review.acceptSuperseded).toBe(false);expect(reserveReview).not.toHaveBeenCalled();expect(recordPerpetualDispatch).not.toHaveBeenCalled();+ expect(recordBranchReconcileOutcome.mock.calls.at(-1)[1].remaining).toEqual([]);});it('keeps an unresolved retirement visible after the checkout disappears on a later refill', async () => {diff --git a/server/services/cosTaskPreStepBlocks.js b/server/services/cosTaskPreStepBlocks.jsindex 250c694a4d..de6c13293e 100644--- a/server/services/cosTaskPreStepBlocks.js+++ b/server/services/cosTaskPreStepBlocks.js@@ -534,7 +534,9 @@ export async function resolveBranchReconcileBlock(app, taskType, metadata, taskSconst unresolved = unresolvedRetirements(previousOutcome);const absentObligations = unresolved.filter(release => !result.worktreeInventory?.some(tree => sameRetirementCheckout(tree, release)));let retirementReviews = { candidates: [], held: [] };- if (actions.cleanupMerged !== false && result.wip?.some(branch => branch.rule === 'completion-policy' && branch.completionPolicyReason === 'pr-completion-review-required')) {+ const { prepareReconcileRetirements, retainedMergedClaims } = await import('./branchReconcileRetirement.js');+ if (actions.cleanupMerged !== false && (result.wip?.some(branch => branch.rule === 'completion-policy' && branch.completionPolicyReason === 'pr-completion-review-required')+ || retainedMergedClaims(result).length)) {const inventory = describeIdleReconcilePark(result.skipped || [], heldLive, {wip: result.wip, worktreeInventory: result.worktreeInventory,detachedWorktrees: result.detachedWorktrees, superseded: result.superseded,@@ -543,12 +545,13 @@ export async function resolveBranchReconcileBlock(app, taskType, metadata, taskSstatus: 'running', reason: 'preparing-retirement-reviews', remaining: [...inventory.remaining, ...absentObligations],nextAction: 'Review completed local work before verified archival retirement.',});- const { prepareReconcileRetirements } = await import('./branchReconcileRetirement.js');retirementReviews = await prepareReconcileRetirements(app, result);}const retiredNames = new Set((retirementReviews.retired || []).map(row => row.branch));if (retiredNames.size) {result.wip = result.wip.filter(row => !retiredNames.has(row.branch));+ result.merged = result.merged?.filter(row => !retiredNames.has(row.branch));+ result.skipped = result.skipped.filter(row => !retiredNames.has(row.branch));result.worktreeInventory = result.worktreeInventory.filter(row => !retiredNames.has(row.branch));result.cleaned.push(...retiredNames);}diff --git a/server/services/heldCheckoutRecovery.test.js b/server/services/heldCheckoutRecovery.test.jsindex 6a65c8b95f..43b09faccd 100644--- a/server/services/heldCheckoutRecovery.test.js+++ b/server/services/heldCheckoutRecovery.test.js@@ -1649,6 +1649,60 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('held checkout recovery (real git)', ()expect(injected.fetches).toHaveLength(missingProof ? 1 : 0);});+ it.each(['complete', 'owner-reacquired', 'identity-changed'])('recovers retained merged claims through persisted retirement: %s', async scenario => {+ const c = await reviewedCheckout('retained-merged-claim', { normalMerge: true });+ await execGit(['worktree', 'unlock', c.path], repo);+ await execGit(['restore', 'initial.txt'], c.path);+ await mkdir(join(c.admin, 'info'), { recursive: true });+ await writeFile(join(c.admin, 'info', 'exclude'), 'private fixture exclusion note\n');+ await writeFile(join(c.path, '.gitignore'), '/.gitignore\n/untracked.txt\n/ignored.txt\n/linked\n');+ expect((await execGit(['status', '--porcelain'], c.path)).stdout).toBe('');+ const release = { ...c.release, locked: false, lockReason: null,+ checkoutIdentity: await readRetirementCheckoutIdentity(c.path) };+ injected.policies[0].result = { success: false, executionSuccess: true,+ taskOutcome: { status: 'incomplete', recovery: { mode: 'cleanup' } } };+ const remaining = [{ ...release, reason: 'claim-completion-unverified' }];+ await recordBranchReconcileOutcome('app-1', { status: 'incomplete', remaining });+ const { prepareReconcileRetirements } = await import('./branchReconcileRetirement.js');+ const input = { wip: [], merged: [{ branch: release.branch, tip: release.head, worktreePath: release.path,+ state: 'MERGED', isMerged: true }], skipped: remaining,+ worktreeInventory: [{ ...release, managed: true }] };+ const result = await prepareReconcileRetirements(injected.app, input, {+ readPr: async () => c.forge.prUrl,+ prepare: async request => {+ const reviewed = await prepareInventoriedCheckoutRetirement(request, c.opts);+ if (scenario === 'owner-reacquired') c.opts.readAgents = async () => [{ id: 'new-owner', status: 'running',+ metadata: { sourceWorkspace: repo, workspacePath: c.path, worktreeBranch: release.branch } }];+ if (scenario === 'identity-changed') {+ const pointer = await readFile(join(c.path, '.git'));+ await rename(join(c.path, '.git'), join(c.path, '.git.previous')); await writeFile(join(c.path, '.git'), pointer);+ }+ return reviewed;+ },+ // Model the daemon's admitted mutation callback; exercise the actual+ // inventory, policy, owner, archive, removal and persisted-receipt gates.+ retire: (request, options) => retireInventoriedCheckout(request, { ...c.opts, ...options, beforeMutation: async () => {} }),+ });+ const outcome = await getBranchReconcileOutcome('app-1');+ if (scenario !== 'complete') {+ expect(result).toMatchObject({ retired: [], held: [{ reason: scenario === 'owner-reacquired' ? 'claim-owner-active' : 'checkout-identity-changed' }] });+ expect(await registered(c.path)).toBe(true);+ expect(outcome.retired || []).toEqual([]);+ return;+ }+ expect(result).toMatchObject({ candidates: [], held: [], retired: [expect.objectContaining({ checkoutIdentity: release.checkoutIdentity })] });+ expect(await registered(c.path)).toBe(false);+ const receipt = outcome.retired.find(row => row.branch === release.branch);+ expect(receipt).toMatchObject({ head: release.head, branchDeleted: true, checkoutIdentity: release.checkoutIdentity });+ expect(await readFile(join(receipt.archiveDir, 'files/ignored.txt'), 'utf8')).toBe('ignored work\n');+ expect(await readFile(join(receipt.archiveDir, 'files/untracked.txt'), 'utf8')).toBe('untracked work\n');+ expect(await readFile(join(receipt.archiveDir, 'administration/gitdir/portos-review-status'), 'utf8')).toBe('private synthetic review marker');+ const restored = join(scratch, 'standalone.git');+ await execGit(['init', '--bare', restored], scratch);+ await execGit(['bundle', 'unbundle', join(receipt.archiveDir, 'commits.bundle')], restored);+ await execGit(['fsck', '--full', '--strict', '--no-reflogs', release.head], restored);+ });+it('settles a review obligation for unattended retirement only once every commit is on the default branch', async () => {const c = await reviewedCheckout('settled', { normalMerge: true, superseded: true });await execGit(['worktree', 'unlock', c.path], repo);