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: retire owner-free claims during branch reconciliation

[._.]/ Adam Eivy208964992dfd
diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.js
index b8a9b74c93..408cd56028 100644
--- a/server/services/branchReconcile.js
+++ b/server/services/branchReconcile.js
@@ -117,8 +117,8 @@ export const STALE_CLAIM_IDLE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
// or whose PR was merged from a peer machine, which PortOS users routinely run —
// sits in exactly that state for the seconds-to-minutes it spends on teardown,
// and reaping there deletes a directory a live process is still standing in.
-// mtime is the only liveness proxy available (a claim has no durable local agent
-// id), so keep a short idle floor over it. An hour is far longer than any
+// Without a fresh claim registry read, mtime remains the liveness proxy,
+// so keep a short idle floor over it. An hour is far longer than any
// teardown and still turns a week-long park into a single recheck.
export const SHIPPED_CLAIM_IDLE_MS = 60 * 60 * 1000; // 1 hour
@@ -1060,7 +1060,12 @@ async function releaseRetiredClaim(repoPath, branch, { origin, forgeExec, forgeA
* A failed gate skips the branch (with a reason) — never a force-delete of
* unmerged or dirty work.
*
- * SHIPPED_CLAIM: a `claim-*` worktree holds for STALE_CLAIM_IDLE_MS on the chance
+ * With a fresh owner-registry read under the claim binding lock, an owner-free
+ * claim can retire immediately. The age windows below are only the fallback
+ * for callers without that authoritative recheck; neither path bypasses dirty
+ * work, managed-root, lock, or active-owner guards.
+ *
+ * SHIPPED_CLAIM fallback: a `claim-*` worktree holds for STALE_CLAIM_IDLE_MS on the chance
* a claim session is still using it. Waiting out a full week is pointless once
* the claim has demonstrably SHIPPED, and doing it anyway is what left four
* finished claims parked while every run reported "cleaned 0" and parked on
@@ -1078,8 +1083,7 @@ async function releaseRetiredClaim(repoPath, branch, { origin, forgeExec, forgeA
*
* What it does NOT prove is that the claim PROCESS has exited — so this
* SHORTENS the idle window to SHIPPED_CLAIM_IDLE_MS rather than removing it,
- * leaving one mechanism (the age window) with two durations instead of a second
- * way past the gate. Everything the window already guarantees still holds: an
+ * for callers without a registry recheck. The fallback remains conservative: an
* unreadable mtime fails safe to protected, and an explicit lock still wins.
*
* @param {string} repoPath
@@ -1246,11 +1250,19 @@ async function retireBranch(repoPath, b, opts) {
const readAgents = opts.claimOwners?.readAgents;
if (!readAgents) return retireBranchNow(repoPath, b, opts);
return withClaimOwnershipLock(async () => {
+ const agents = await readAgents().catch(() => null);
const reason = claimCheckoutOwnerReason({
- branchName: b.branch, holderPath: b.worktreePath, sourceWorkspace: repoPath,
- agents: await readAgents().catch(() => null),
+ branchName: b.branch, holderPath: b.worktreePath, sourceWorkspace: repoPath, agents,
+ });
+ if (reason) return { ok: false, reason };
+ // A fresh registry read under the binding lock supersedes the directory-age
+ // proxy. A claim name alone is not a live owner. Keep all other retirement
+ // gates, and refresh workspace owners too (not only explicit claim bindings).
+ return retireBranchNow(repoPath, b, {
+ ...opts,
+ activeAgentIds: buildActiveOwnerIds(opts.activeAgentIds, agents),
+ allowLiveClaim: true,
});
- return reason ? { ok: false, reason } : retireBranchNow(repoPath, b, opts);
});
}
diff --git a/server/services/branchReconcile.ownership.test.js b/server/services/branchReconcile.ownership.test.js
index fe67f73236..5076413e39 100644
--- a/server/services/branchReconcile.ownership.test.js
+++ b/server/services/branchReconcile.ownership.test.js
@@ -93,6 +93,35 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('branch-reconcile worktree ownership (re
expect(left).not.toContain('claim/issue-5');
});
+ it('retires fresh owner-free claims while preserving locked and unfinished work', async () => {
+ const root = join(repo, '.claude', 'worktrees');
+ await mkdir(root, { recursive: true });
+ const clean = await addTree(join(root, 'claim-issue-11'), 'claim/issue-11');
+ const locked = await addTree(join(root, 'claim-issue-12'), 'claim/issue-12');
+ await execGit(['worktree', 'lock', locked], repo);
+ const dirty = await addTree(join(root, 'claim-issue-13'), 'claim/issue-13');
+ await writeFile(join(dirty, 'unfinished.txt'), 'preserve this work\n');
+ const unmerged = await addTree(join(root, 'claim-issue-14'), 'claim/issue-14');
+ await writeFile(join(unmerged, 'committed.txt'), 'finish this work\n');
+ await execGit(['add', 'committed.txt'], unmerged);
+ await execGit(['commit', '-m', 'feat: unfinished work'], unmerged);
+
+ const result = await reconcile(repo, {
+ activeAgentIds: new Set(), claimOwners: { agents: [], readAgents: async () => [] },
+ });
+ 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(result.inFlight).toEqual(expect.arrayContaining([
+ expect.objectContaining({ branch: 'claim/issue-13', state: 'ABANDONED_WIP' }),
+ ]));
+ // This hermetic repo has no forge, so committed work is held until PR state
+ // can be established rather than being retired as merged.
+ expect(result.wip).toContainEqual(expect.objectContaining({ branch: 'claim/issue-14' }));
+ for (const path of [locked, dirty, unmerged]) expect(existsSync(path)).toBe(true);
+ expect(await branches()).toEqual(expect.arrayContaining(['claim/issue-12', 'claim/issue-13', 'claim/issue-14']));
+ });
+
it('leaves the operator-initiated merged-branch cleanup able to remove an unmanaged tree', async () => {
const loose = await addTree(join(external, 'loose'), 'loose-br', { ageMs: 30 * DAY });
diff --git a/server/services/branchReconcile.test.js b/server/services/branchReconcile.test.js
index 1db708ef51..93d67c1707 100644
--- a/server/services/branchReconcile.test.js
+++ b/server/services/branchReconcile.test.js
@@ -1394,6 +1394,33 @@ describe('reconcile', () => {
expect(git.deleteBranch).not.toHaveBeenCalled();
});
+ it('preserves work written after the scan even when the claim has no owner', async () => {
+ git.hasBranchMergeEvidence.mockResolvedValue(true);
+ execGit.mockResolvedValue({ stdout: '?? unfinished.js\n', exitCode: 0 });
+ const result = await cleanupMerged('/repo', 'main', [{
+ branch: 'claim/issue-101', worktreePath: '/repo/data/cos/worktrees/claim-issue-101', worktreeAgeMs: 0,
+ }], { activeAgentIds: new Set(), claimOwners: { agents: [], readAgents: async () => [] } });
+ expect(result.skipped).toEqual([{ branch: 'claim/issue-101', reason: 'worktree-dirty' }]);
+ expect(wt.forceRemoveWorktreeDir).not.toHaveBeenCalled();
+ expect(git.deleteBranch).not.toHaveBeenCalled();
+ });
+
+ it('preserves a recent claim when a workspace owner appears after the scan', async () => {
+ dirtyClaim();
+ worktreeMtimeMs = Date.now();
+ execGit.mockResolvedValue({ stdout: '', exitCode: 0 });
+ const result = await reconcile('/repo', {
+ activeAgentIds: new Set(),
+ claimOwners: { agents: [], readAgents: async () => [{
+ id: 'agent-late', status: 'running', workspacePath: '/repo/data/cos/worktrees/claim-issue-101',
+ }] },
+ });
+ expect(result.cleaned).toEqual([]);
+ expect(result.skipped).toHaveLength(1);
+ expect(wt.forceRemoveWorktreeDir).not.toHaveBeenCalled();
+ expect(git.deleteBranch).not.toHaveBeenCalled();
+ });
+
it('never retires a merged, clean claim checkout its live owner is still handing off', async () => {
git.getBranches.mockResolvedValue([
{ name: 'claim/issue-101', isDefault: false, current: false, tracking: 'origin/claim/issue-101', merged: true }