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

fix: release stranded issue claims after branch cleanup (#8774)

[._.]/ Adam Eivy19daa4815b6e
diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.js
index 1bc6792188..f5e4b47223 100644
--- a/server/services/branchReconcile.js
+++ b/server/services/branchReconcile.js
@@ -929,6 +929,27 @@ export function describeIdleReconcilePark(skipped = [], heldLive = []) {
};
}
+/**
+ * Release only exact issue claims whose local retirement already succeeded.
+ * Keep forge/account resolution aligned with the reconcile cycle, including
+ * enterprise hosts. A forge outage must never undo or block local cleanup.
+ */
+async function releaseRetiredClaim(repoPath, branch, { origin, forgeExec, forgeAccount = null } = {}) {
+ const issue = /^claim\/issue-([1-9]\d*)$/.exec(branch)?.[1];
+ if (!issue) return;
+ await (async () => {
+ const repoSpec = githubRepoSpec(origin === undefined ? await getOriginInfo(repoPath) : origin);
+ if (!repoSpec) return;
+ const { cwd, env } = forgeExec || await resolveForgeExecOptions(repoPath, { forgeAccount });
+ await execGh([
+ 'issue', 'edit', issue, '--repo', repoSpec,
+ '--remove-assignee', '@me', '--remove-label', 'in-progress'
+ ], undefined, { cwd, env });
+ })().catch(() => {
+ console.log(`⚠️ branch-reconcile: could not release claim markers for #${issue} after local cleanup`);
+ });
+}
+
/**
* Deterministically clean up fully-merged branches: remove the lingering
* worktree, then delete the local branch. Safety gates (ALL must hold):
@@ -967,7 +988,7 @@ export function describeIdleReconcilePark(skipped = [], heldLive = []) {
* @returns {Promise<{cleaned:string[], skipped:{branch:string,reason:string,retryAt?:string}[]}>}
* A skip carries `retryAt` when its hold is known to lift at a specific time.
*/
-export async function cleanupMerged(repoPath, defaultBranch, merged, { activeAgentIds = new Set() } = {}) {
+export async function cleanupMerged(repoPath, defaultBranch, merged, { activeAgentIds = new Set(), origin, forgeExec, forgeAccount = null } = {}) {
const cleaned = [];
const skipped = [];
for (const b of merged) {
@@ -992,6 +1013,7 @@ export async function cleanupMerged(repoPath, defaultBranch, merged, { activeAge
skipped.push({ branch: b.branch, ...failure });
continue;
}
+ await releaseRetiredClaim(repoPath, b.branch, { origin, forgeExec, forgeAccount });
cleaned.push(b.branch);
}
return { cleaned, skipped };
@@ -1204,6 +1226,12 @@ export async function reconcile(repoPath = PATHS.root, { cleanup = true, reapSup
const forgeExec = githubRepoSpec(origin)
? await resolveForgeExecOptions(repoPath, { forgeAccount })
: null;
+ // The early reaper may have deleted the branch before gather can see it.
+ // Release these claims even when the subsequent forge probe is unavailable.
+ for (const entry of worktreeCleanup.reaped || []) {
+ if (!entry?.branchDeleted) continue;
+ await releaseRetiredClaim(repoPath, entry.branch, { origin, forgeExec, forgeAccount });
+ }
if (forgeExec) {
const forge = await ensureForgeReachable('branch-reconcile', {
hostname: githubApiHost(origin.host),
@@ -1261,7 +1289,7 @@ export async function reconcile(repoPath = PATHS.root, { cleanup = true, reapSup
const wip = classified.filter((c) => c.state === 'WIP');
const { cleaned: cleanedBranches, skipped } = cleanup
- ? await cleanupMerged(repoPath, defaultBranch, merged, { activeAgentIds })
+ ? await cleanupMerged(repoPath, defaultBranch, merged, { activeAgentIds, origin, forgeExec, forgeAccount })
: { cleaned: [], skipped: merged.map((m) => ({ branch: m.branch, reason: 'cleanup-disabled' })) };
const cleaned = [...new Set([...cleanedWorktrees, ...cleanedBranches])];
diff --git a/server/services/branchReconcile.test.js b/server/services/branchReconcile.test.js
index aff650c0fd..bb3a411a5d 100644
--- a/server/services/branchReconcile.test.js
+++ b/server/services/branchReconcile.test.js
@@ -287,6 +287,48 @@ describe('classifyBranches', () => {
});
});
+describe('retired claim markers in reconcile', () => {
+ it('releases early-reaped claims on the enterprise host with the pinned account', async () => {
+ getOriginInfo.mockResolvedValue({ hasOrigin: true, host: 'github.example.com', fullName: 'example/app' });
+ wt.reapMergedWorktrees.mockResolvedValue({ reaped: [{ branch: 'claim/issue-42', branchDeleted: true }], skipped: [] });
+ git.getBranches.mockResolvedValue([]);
+ await reconcile('/repo', { forgeAccount: 'app-account' });
+ expect(execGh).toHaveBeenCalledWith([
+ 'issue', 'edit', '42', '--repo', 'github.example.com/example/app',
+ '--remove-assignee', '@me', '--remove-label', 'in-progress'
+ ], undefined, { cwd: '/repo', env: PINNED_ENV });
+ expect(resolveForgeExecOptionsMock).toHaveBeenCalledWith('/repo', { forgeAccount: 'app-account' });
+ expect(resolveForgeExecOptionsMock).toHaveBeenCalledOnce();
+ });
+
+ it('keeps the reap successful and logs once when marker release fails during a forge outage', async () => {
+ wt.reapMergedWorktrees.mockResolvedValue({ reaped: [{ branch: 'claim/issue-42', branchDeleted: true }], skipped: [] });
+ execGh.mockRejectedValueOnce(new Error('forge unavailable'));
+ ensureForgeReachableMock.mockResolvedValueOnce({ ok: false, status: 'unavailable' });
+ const log = vi.spyOn(console, 'log').mockImplementation(() => {});
+ const result = await reconcile('/repo');
+ expect(result.cleaned).toEqual(['claim/issue-42']);
+ expect(result.forgeUnavailable).toBe(true);
+ expect(log.mock.calls.filter(([line]) => line.includes('could not release claim markers for #42'))).toHaveLength(1);
+ log.mockRestore();
+ });
+
+ it('does not release protected claims, unrelated branches, or non-GitHub issues', async () => {
+ wt.reapMergedWorktrees.mockResolvedValue({
+ reaped: [{ branch: 'feature/issue-42', branchDeleted: true }, { branch: 'claim/issue-44', branchDeleted: false }],
+ skipped: [{ branch: 'claim/issue-43', reason: 'worktree-active-agent' }]
+ });
+ git.getBranches.mockResolvedValue([]);
+ await reconcile('/repo');
+ expect(execGh.mock.calls.filter(([args]) => args[0] === 'issue')).toEqual([]);
+ execGh.mockClear();
+ getOriginInfo.mockResolvedValue({ hasOrigin: true, host: 'gitlab.com', fullName: 'example/app' });
+ wt.reapMergedWorktrees.mockResolvedValue({ reaped: [{ branch: 'claim/issue-42', branchDeleted: true }] });
+ await reconcile('/repo');
+ expect(execGh).not.toHaveBeenCalled();
+ });
+});
+
describe('cleanupMerged', () => {
it('removes worktree + deletes branch when merged and clean', async () => {
git.hasBranchMergeEvidence.mockResolvedValue(true);
@@ -352,6 +394,28 @@ describe('cleanupMerged', () => {
expect(git.deleteBranch).toHaveBeenCalledWith('/repo', 'claim/issue-1933', { local: true });
});
+ it('releases a retired issue claim with the repository credential after deletion', async () => {
+ git.hasBranchMergeEvidence.mockResolvedValue(true);
+ await cleanupMerged('/repo', 'main', [{ branch: 'claim/issue-42', worktreePath: '/wt/retired' }]);
+ expect(execGh).toHaveBeenCalledWith([
+ 'issue', 'edit', '42', '--repo', 'github.com/atomantic/PortOS',
+ '--remove-assignee', '@me', '--remove-label', 'in-progress'
+ ], undefined, { cwd: '/repo', env: PINNED_ENV });
+ expect(execGh.mock.invocationCallOrder[0]).toBeGreaterThan(git.deleteBranch.mock.invocationCallOrder[0]);
+ });
+
+ it('leaves markers intact for a live owner or a failed branch deletion', async () => {
+ git.hasBranchMergeEvidence.mockResolvedValue(true);
+ const branch = { branch: 'claim/issue-42', worktreePath: '/wt/claim-issue-42', worktreeAgeMs: 10 * 24 * 60 * 60 * 1000 };
+ const held = await cleanupMerged('/repo', 'main', [branch], { activeAgentIds: new Set(['claim-issue-42']) });
+ expect(held.skipped[0].reason).toBe('worktree-active-agent');
+ expect(execGh).not.toHaveBeenCalled();
+ git.deleteBranch.mockResolvedValue({ error: 'delete refused' });
+ const failed = await cleanupMerged('/repo', 'main', [branch]);
+ expect(failed.cleaned).toEqual([]);
+ expect(execGh).not.toHaveBeenCalled();
+ });
+
it('reaps the invalid empty-issue claim immediately when merged and clean', async () => {
expect(isMalformedClaimBranch('claim/issue-')).toBe(true);
expect(isMalformedClaimBranch('claim/issue-42')).toBe(false);