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

address review (codex): widen stale-claim reap window to 7 days

Adam Eivydad197e6da12
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 0b3a7ee544..92f90f1bfb 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -2,7 +2,7 @@
## CoS Agents
-- **branch-reconcile now reaps ABANDONED `/claim` worktrees instead of parking on them forever.** When a `/claim` (or `/do:next`) agent finishes WITHOUT committing, it leaves a `claim/issue-N` branch that's just a bare pointer at an already-merged commit plus a lingering worktree. branch-reconcile classified these as `MERGED` but its cleanup unconditionally protected any `claim-*` worktree as a live human session (`worktree-human-claim`) — so it skipped them every run and parked with the misleading *"nothing in-flight (cleaned 0)"*, and the stale branches/worktrees accumulated indefinitely (this is exactly what left `claim/issue-1933` + `claim/issue-1938` stuck locally). The protection is now age-gated: a claim worktree is reaped only when it's **merged AND clean AND older than `STALE_CLAIM_IDLE_MS` (24h)** — a trio that's only ever true for an abandoned claim, since a claim with real work in progress is never merged+clean (uncommitted edits → dirty → WIP; committed work → not an ancestor of default → not MERGED). Recent claim worktrees stay fully protected so the `/claim` Phase-7 self-clean window and any live human session are untouched, and unknown-age worktrees fail safe toward not-deleting. The park log also now names any merged branches held back by a protection guard (`, N merged branch(es) held back (worktree-human-claim, …)`) so a lingering worktree is a visible breadcrumb instead of an invisible "cleaned 0". Worktree age is read from the worktree root's mtime (set at `git worktree add`, unmoved by deep-file edits) in `gatherBranchState` and threaded into the pure `worktreeProtectionReason` gate. (`server/services/branchReconcile.js`, `server/services/cosTaskGenerator.js`)
+- **branch-reconcile now reaps ABANDONED `/claim` worktrees instead of parking on them forever.** When a `/claim` (or `/do:next`) agent finishes WITHOUT committing, it leaves a `claim/issue-N` branch that's just a bare pointer at an already-merged commit plus a lingering worktree. branch-reconcile classified these as `MERGED` but its cleanup unconditionally protected any `claim-*` worktree as a live human session (`worktree-human-claim`) — so it skipped them every run and parked with the misleading *"nothing in-flight (cleaned 0)"*, and the stale branches/worktrees accumulated indefinitely (this is exactly what left `claim/issue-1933` + `claim/issue-1938` stuck locally). The protection is now age-gated: a claim worktree is reaped only when it's **merged AND clean AND older than `STALE_CLAIM_IDLE_MS` (7 days)** — a trio that's only ever true for an abandoned claim, since a claim with real work in progress is never merged+clean (uncommitted edits → dirty → WIP; committed work → not an ancestor of default → not MERGED). Because a merged+clean worktree has nothing beyond the default branch and nothing uncommitted, the reap loses no recoverable work, and re-running `/claim` recreates it — so the window is set very conservatively (a full week) to preserve even a *paused* human session returned-to within days; unknown-age worktrees fail safe toward not-deleting, and branch-reconcile's daily recheck reaps a genuinely-leaked worktree once it finally crosses the threshold. The park log also now names any merged branches held back by a protection guard (`, N merged branch(es) held back (worktree-human-claim, …)`) so a lingering worktree is a visible breadcrumb instead of an invisible "cleaned 0". Worktree age is read from the worktree root's mtime (set at `git worktree add`, unmoved by deep-file edits) in `gatherBranchState` and threaded into the pure `worktreeProtectionReason` gate. (`server/services/branchReconcile.js`, `server/services/cosTaskGenerator.js`)
- **An explicit "Run" of a perpetual claim/reconcile task that finds no work now tells you so — instead of silently doing nothing.** Triggering an on-demand `claim-issue` (or any perpetual task) that had nothing actionable produced zero UI feedback: the task parked with a log line buried in pm2, so the user was left wondering why "40 open issues" didn't spawn an agent. The on-demand path now (1) resets any existing park **and the branch/issue-reconcile convergence signature** before re-running the detector, so a manual trigger always re-checks live state and never honors a stale/cached verdict — previously an explicit "Run" of `branch-reconcile`/`issue-reconcile` could re-park `no-progress` against an unchanged-since-last-run set even though the user asked to re-drive; and (2) when the fresh check still finds no work, emits a `cos:schedule:on-demand-empty` event that surfaces a toast — e.g. *"claim-issue: re-checked now — no claimable issues (0 of 40 claimable — 2 in-flight, 38 filtered). Next auto-recheck in 33 hours."* The GitHub/GitLab work-detector now returns a breakdown (`open`, `inFlight` held by open claim branches/PRs, `filtered` skipped by label/assignee/epic), plumbed through the park record and event, so the message explains WHY a non-empty queue yields zero claimable work — the common case being issues already shipped whose stale `claim/issue-N` branches still count as in-flight. The event carries an `outcome` so the message fits the case: `parked` (perpetual detector found no work), `transient` (a `gh`/`glab` probe failure — *"couldn't complete the check just now — try again"*, never a false "nothing to do"), or `idle` (a non-perpetual task like `pr-watcher` genuinely had nothing — a calm neutral note, not an error). Both on-demand drain engines — `cos.dequeueNextTask` and `cosTaskGenerator.spawnPriority0OnDemand` — share the reset-and-emit path (via `emitOnDemandEmpty`), so a user Run gets feedback no matter which engine picks it up. The event fires ONLY for user-initiated on-demand runs (never background parks), so there's no toast spam. (`server/services/cos.js`, `server/services/cosTaskGenerator.js`, `server/services/perpetualWork.js`, `server/services/taskSchedule.js`, `server/services/socket.js`, `client/src/hooks/useOnDemandTaskToast.js`)
diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.js
index 4fc2e2af30..e8d597b7e8 100644
--- a/server/services/branchReconcile.js
+++ b/server/services/branchReconcile.js
@@ -36,10 +36,18 @@ export const PROTECTED_BRANCHES = ['main', 'master', 'release'];
// such a claim worktree once it is older than this AND its branch is merged AND
// its worktree is clean. That trio is only ever true for an ABANDONED claim: a
// claim with real work in progress is never merged+clean (uncommitted edits →
-// dirty → WIP; committed work → not an ancestor of default → not MERGED), so the
-// age gate exists purely to spare the narrow "just created, about to start"
-// window a live human session occupies.
-export const STALE_CLAIM_IDLE_MS = 24 * 60 * 60 * 1000; // 24h
+// dirty → WIP; committed work → not an ancestor of default → not MERGED).
+//
+// Because a reaped worktree is merged+clean, the reap loses NOTHING recoverable —
+// no commits beyond the default branch, no uncommitted edits — and re-running
+// `/claim` recreates it in seconds. So even a false positive (a human who claimed
+// an issue, left the branch untouched at the default commit, and returns to a
+// *paused* session) costs only a re-claim, never work. The window is set very
+// conservatively at a full week anyway — mtime alone can't prove abandonment, so
+// we err far toward preservation: a paused session returned-to within 7 days keeps
+// its worktree, while a genuinely-leaked one (which persists indefinitely) is
+// reaped on the daily recheck once it crosses the threshold.
+export const STALE_CLAIM_IDLE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
// Bound the gh query (single-user repos never realistically truncate at 200).
const PR_LIST_LIMIT = 200;
diff --git a/server/services/branchReconcile.test.js b/server/services/branchReconcile.test.js
index 01a0cd1b73..2c77ff8f40 100644
--- a/server/services/branchReconcile.test.js
+++ b/server/services/branchReconcile.test.js
@@ -144,7 +144,8 @@ describe('cleanupMerged', () => {
git.isBranchMergedInto.mockResolvedValue(true);
execGit.mockResolvedValue({ stdout: '', exitCode: 0 }); // clean
const res = await cleanupMerged('/repo', 'main', [
- { branch: 'claim/issue-1933', worktreePath: '/repo/data/cos/worktrees/claim-issue-1933', worktreeAgeMs: 3 * 24 * 60 * 60 * 1000 }
+ // 10 days old — comfortably past the 7-day STALE_CLAIM_IDLE_MS default
+ { branch: 'claim/issue-1933', worktreePath: '/repo/data/cos/worktrees/claim-issue-1933', worktreeAgeMs: 10 * 24 * 60 * 60 * 1000 }
]);
expect(res.cleaned).toEqual(['claim/issue-1933']);
expect(wt.forceRemoveWorktreeDir).toHaveBeenCalledWith('/repo', '/repo/data/cos/worktrees/claim-issue-1933', expect.any(Object));