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: dispatch abandoned claim worktrees
[._.]/ Adam Eivy19f90d3024e8
diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.jsindex 05495f2642..1bc6792188 100644--- a/server/services/branchReconcile.js+++ b/server/services/branchReconcile.js@@ -27,7 +27,7 @@ import { isHostShuttingDown } from '../lib/hostShutdown.js';import { getBranches, getDefaultBranch, hasBranchMergeEvidence, deleteBranch } from './git.js';import { execGit } from '../lib/execGit.js';import { listWorktrees, forceRemoveWorktreeDir, classifyWorktreeDirt, reapMergedWorktrees } from './worktreeManager.js';-import { isAgentWorktreeId, worktreeOwnershipReason, worktreeHoldExpiresAt } from '../lib/worktreeOwnership.js';+import { isAgentWorktreeId, isHumanClaimWorktree, worktreeAgentId, worktreeOwnershipReason, worktreeHoldExpiresAt } from '../lib/worktreeOwnership.js';import { execGh, ensureForgeReachable, getIssueDispatchHint } from './github.js';import { resolveForgeExecOptions } from './forgeExecOptions.js';import { isForgeNoAccessError, logForgeNoAccessOnce } from '../lib/forgeAccessErrors.js';@@ -134,7 +134,7 @@ const PR_LIST_LIMIT = 200;/*** Pure classifier: map one branch's git/PR facts to a reconcile state.* First match wins.- * ABANDONED_WIP — dirty worktree of a DEAD CoS agent → agent finishes the work+ * ABANDONED_WIP — dirty worktree of an INACTIVE PortOS run → agent finishes it* MERGED — work is fully in the default branch → deterministic cleanup* CONFLICTED — open PR with merge conflicts → agent resolves* IN_REVIEW — open PR, otherwise → agent drives to merge@@ -142,7 +142,7 @@ const PR_LIST_LIMIT = 200;* (pushed-with-no-PR, or never pushed but holding commits)* WIP — bare pointer, dirty, or LIVE-owned → skip + report (never touch)*- * @param {{ hasUpstream:boolean, ahead?:number|null, hasOrigin?:boolean, isMerged:boolean, worktreeDirty:boolean, abandonedAgentWorktree?:boolean, liveOwnerReason?:string|null, openPr:({mergeable?:string}|null), prStateUnavailable?:boolean }} input+ * @param {{ hasUpstream:boolean, ahead?:number|null, hasOrigin?:boolean, isMerged:boolean, worktreeDirty:boolean, abandonedAgentWorktree?:boolean, abandonedClaimWorktree?:boolean, liveOwnerReason?:string|null, openPr:({mergeable?:string}|null), prStateUnavailable?:boolean }} input* `ahead` is the branch's own commit count over the default branch (null =* unreadable), and `hasOrigin` says the repo has a remote to push that work to.* Together they are what make a never-pushed branch actionable — see the@@ -153,10 +153,10 @@ const PR_LIST_LIMIT = 200;* `liveOwnerReason` is `resolveLiveOwnerReason`'s verdict for the branch — non-null* means an active CoS agent or deliberate lock owns it right now, whether or not* its worktree still exists. A clean `claim-*` directory is only a claim marker;- * dirty claim trees still remain WIP through the ordinary dirty-tree guard.+ * an inactive dirty claim is actionable only when `abandonedClaimWorktree` is true.* @returns {'ABANDONED_WIP'|'MERGED'|'CONFLICTED'|'IN_REVIEW'|'NEEDS_PR'|'WIP'}*/-export function classifyBranch({ hasUpstream, ahead = null, hasOrigin = false, isMerged, worktreeDirty, abandonedAgentWorktree, liveOwnerReason = null, openPr, prStateUnavailable = false }) {+export function classifyBranch({ hasUpstream, ahead = null, hasOrigin = false, isMerged, worktreeDirty, abandonedAgentWorktree, abandonedClaimWorktree, liveOwnerReason = null, openPr, prStateUnavailable = false }) {// A dead agent's worktree that still holds uncommitted work is the ONE dirty// case that must be driven rather than skipped — and it must be caught BEFORE// the `isMerged` test, because an agent that exited without committing leaves@@ -166,7 +166,13 @@ export function classifyBranch({ hasUpstream, ahead = null, hasOrigin = false, i// correctly refused to delete a dirty worktree, so they were only ever// reported as `skipped` and never appeared in-flight. The work sat there// indefinitely while every run logged "nothing in-flight".- if (worktreeDirty && abandonedAgentWorktree) return 'ABANDONED_WIP';+ // Dirty work is normally protected, but a dirty claim tree is the one+ // exception branch-reconcile can safely hand to its coordinator: claim+ // worktrees have no durable agent id, and the caller supplied an authoritative+ // active-agent set before marking this tree abandoned. Keep the live-owner gate+ // in front of the exception so a contradictory caller can never dispatch onto+ // a tree it also says is owned right now.+ if (worktreeDirty && !liveOwnerReason && (abandonedAgentWorktree || abandonedClaimWorktree)) return 'ABANDONED_WIP';if (isMerged) return 'MERGED';// A branch with a LIVE owner belongs to an active CoS agent or an explicitly// locked worktree. It may keep moving (commits, a PR opened, a rebase) for as@@ -390,6 +396,34 @@ export function isAbandonedAgentWorktree({ path, locked, activeAgentIds }) {}) === null;}+/**+ * Is this a dirty human-claim worktree that branch-reconcile may hand to a+ * coordinator sub-agent?+ *+ * Claim worktrees deliberately use a `claim-*` directory rather than the+ * `agent-*` id that lets the ordinary abandonment check prove ownership. The+ * branch-reconcile dispatch path already treats a claim directory as a marker,+ * not as a durable live-process handle; with an authoritative active-agent Set,+ * a dirty claim with no lock/active owner is therefore actionable work rather+ * than a merged branch to park behind the human-claim cleanup grace period.+ *+ * Missing or non-Set liveness is unknown and fails closed. This helper never+ * authorizes cleanup — it only lets classifyBranch expose the dirty tree as+ * ABANDONED_WIP so a sub-agent can inspect and finish it in place.+ *+ * @param {{ path:string, locked?:boolean, worktreeDirty?:boolean, activeAgentIds?:Set<string> }} input+ * @returns {boolean}+ */+export function isAbandonedClaimWorktree({ path, locked, worktreeDirty = false, activeAgentIds }) {+ if (!worktreeDirty || !(activeAgentIds instanceof Set) || !isHumanClaimWorktree(worktreeAgentId(path))) return false;+ return worktreeOwnershipReason({+ path,+ locked,+ activeAgentIds,+ allowLiveClaim: true,+ }) === null;+}+/*** Why this branch must be left ALONE this cycle — the dispatch-side counterpart to* `worktreeProtectionReason`'s teardown gate. Three cases that gate doesn't cover:@@ -412,14 +446,15 @@ export function isAbandonedAgentWorktree({ path, locked, activeAgentIds }) {* claim flow has no durable local agent id for its branch, so treating the* directory's existence as liveness hides clean, open-PR claims after the* claim agent has exited — exactly the branches branch-reconcile exists to- * finish. So this side passes `allowLiveClaim`, while cleanup keeps the hold- * through `worktreeProtectionReason` until the claim is stale or provably- * shipped. A lock still outranks it: the gate tests the lock BEFORE the- * claim, so a locked claim tree reports `worktree-locked` on its own — and- * so does a live agent, on the same ordering. `activeAgentIds` holds- * `agent-<id>` keys (case 2 above reads them as such), so a claim basename- * can only appear there out of contract; if one ever did, holding the branch- * is the fail-safe answer and the intended precedence.+ * finish. So this side passes `allowLiveClaim`; a dirty claim is then+ * surfaced as `ABANDONED_WIP` by `isAbandonedClaimWorktree`, while cleanup+ * keeps its destructive hold through `worktreeProtectionReason` until the+ * claim is stale or provably shipped. A lock still outranks it: the gate+ * tests the lock BEFORE the claim, so a locked claim tree reports+ * `worktree-locked` on its own — and so does a live agent, on the same+ * ordering. The scheduler augments `activeAgentIds` with the basenames of+ * live agents' recorded worktree paths, so a claim basename held by a live+ * agent still wins over the dispatch-side carve-out.** A branch with no worktree at all and no live owner is simply free (null).*@@ -732,7 +767,9 @@ async function worktreeAgeMs(worktreePath) {* @param {string} repoPath* @param {{ defaultBranch:string, activeAgentIds?:Set<string>, remoteHeads?:Map<string,string>|null, hasOrigin?:boolean, origin?:object|null }} ctx* `activeAgentIds` distinguishes a live agent's worktree from an abandoned one (see- * `isAbandonedAgentWorktree`); omitting it leaves every agent worktree protected.+ * `isAbandonedAgentWorktree`); the scheduler also includes live worktree basenames+ * so claim paths owned by an active run remain protected. Omitting it leaves every+ * agent worktree protected.* `remoteHeads` is `listRemoteHeads`' answer when the caller already has it;* omitted, this reads it itself when the repo has an origin, and `null`* (unreadable remote, or no origin to read) is carried through as "we could@@ -745,7 +782,8 @@ async function worktreeAgeMs(worktreePath) {* pin for the standalone case. Both feed `getOpenPrsByHead` — see #7540.* @returns {Promise<object[]>} one entry per candidate branch:* { branch, tip, hasUpstream, hasOrigin, tracking, upstreamGone, isMerged, hasWorktree, worktreePath,- * worktreeDirty, dirtyPaths, behind, ahead, collisionPaths, abandonedAgentWorktree, openPr }+ * worktreeDirty, dirtyPaths, behind, ahead, collisionPaths, abandonedAgentWorktree,+ * abandonedClaimWorktree, openPr }*/export async function gatherBranchState(repoPath, { defaultBranch, activeAgentIds = null, remoteHeads: providedRemoteHeads, hasOrigin: providedHasOrigin, origin: providedOrigin, forgeExec = null, forgeAccount = null } = {}) {const protectedSet = new Set([...PROTECTED_BRANCHES, defaultBranch]);@@ -849,6 +887,7 @@ export async function gatherBranchState(repoPath, { defaultBranch, activeAgentIdahead: divergence.ahead,collisionPaths: divergence.collisionPaths,abandonedAgentWorktree: isAbandonedAgentWorktree({ path: worktreePath, locked: worktreeLocked, activeAgentIds }),+ abandonedClaimWorktree: isAbandonedClaimWorktree({ path: worktreePath, locked: worktreeLocked, worktreeDirty, activeAgentIds }),openPr: prsByHead.get(b.name) || null,prStateUnavailable});@@ -1119,7 +1158,8 @@ async function retireBranch(repoPath, b, { activeAgentIds = new Set(), staleClai* explicitly. `reapRemotes` (default false) additionally deletes merged* branches left on `origin` that nothing local points at; off, they are only* reported — see `reapOrphanedRemotes`. `activeAgentIds` protects in-use CoS- * agent worktrees. `forgeAccount` is the managed app record's explicit gh+ * agent worktrees and any live worktree basename tokens supplied by the caller.+ * `forgeAccount` is the managed app record's explicit gh* 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.diff --git a/server/services/branchReconcile.test.js b/server/services/branchReconcile.test.jsindex 09b8e6d6f0..aff650c0fd 100644--- a/server/services/branchReconcile.test.js+++ b/server/services/branchReconcile.test.js@@ -100,7 +100,7 @@ vi.mock('./supersededBackup.js', () => ({import {classifyBranch, classifyBranches, cleanupMerged, reconcile, gatherBranchState, worktreeProtectionReason,- isAbandonedAgentWorktree, resolveLiveOwnerReason, gatherDivergence,+ isAbandonedAgentWorktree, isAbandonedClaimWorktree, resolveLiveOwnerReason, gatherDivergence,actionOn, filterActionable, desiredEndState, formatInFlightForPrompt, actionableSignature,limitBranchesForAgent,branchPriorityRank, prioritizeBranches, worktreeProtectionExpiresAt, describeIdleReconcilePark,@@ -186,6 +186,18 @@ describe('classifyBranch', () => {liveOwnerReason: null, openPr: null})).toBe('ABANDONED_WIP');});+ it('drives an inactive dirty claim worktree even when its branch tip is merged', () => {+ expect(classifyBranch({+ isMerged: true, hasUpstream: false, worktreeDirty: true, abandonedClaimWorktree: true,+ liveOwnerReason: null, openPr: null+ })).toBe('ABANDONED_WIP');+ });+ it('never lets a live-owner verdict be overridden by an abandoned-worktree hint', () => {+ expect(classifyBranch({+ isMerged: true, worktreeDirty: true, abandonedClaimWorktree: true,+ liveOwnerReason: 'worktree-active-agent', openPr: null+ })).toBe('MERGED');+ });it('local-only bare pointer (no upstream, no commits of its own) → WIP', () => {expect(classifyBranch({ isMerged: false, openPr: null, hasUpstream: false, worktreeDirty: false })).toBe('WIP');expect(classifyBranch({@@ -593,6 +605,24 @@ describe('isAbandonedAgentWorktree', () => {});});+describe('isAbandonedClaimWorktree', () => {+ it('recognizes an unlocked claim tree only with authoritative liveness', () => {+ const dirty = { path: '/wt/claim-issue-42', worktreeDirty: true };+ expect(isAbandonedClaimWorktree({ ...dirty, activeAgentIds: new Set() })).toBe(true);+ expect(isAbandonedClaimWorktree({ ...dirty, activeAgentIds: new Set(['claim-issue-42']) })).toBe(false);+ expect(isAbandonedClaimWorktree({ ...dirty, locked: true, activeAgentIds: new Set() })).toBe(false);+ expect(isAbandonedClaimWorktree({ ...dirty })).toBe(false);+ expect(isAbandonedClaimWorktree({ ...dirty, worktreeDirty: false, activeAgentIds: new Set() })).toBe(false);+ });++ it('never treats an agent or ordinary sibling tree as a claim', () => {+ const activeAgentIds = new Set();+ expect(isAbandonedClaimWorktree({ path: '/wt/agent-deadbeef', worktreeDirty: true, activeAgentIds })).toBe(false);+ expect(isAbandonedClaimWorktree({ path: '/wt/next-issue-42', worktreeDirty: true, activeAgentIds })).toBe(false);+ expect(isAbandonedClaimWorktree({ path: '', worktreeDirty: true, activeAgentIds })).toBe(false);+ });+});+describe('resolveLiveOwnerReason', () => {const live = new Set(['agent-aaaaaaaa']);@@ -608,11 +638,9 @@ describe('resolveLiveOwnerReason', () => {});it('lets a LIVE owner outrank the claim exception, not the other way round', () => {- // activeAgentIds holds `agent-<id>` keys, so a claim basename can only land- // there out of contract — but the gate tests liveness BEFORE the claim, so- // if one ever did, the branch is held rather than dispatched. That is the- // intended direction: never hand a branch to an agent while something says- // a process still owns it.+ // The scheduler adds live claim-worktree basenames to activeAgentIds, and+ // the gate tests liveness BEFORE the claim exception. Never hand a branch+ // to an agent while something says a process still owns it.expect(resolveLiveOwnerReason({ path: '/wt/claim-fix-thing', activeAgentIds: new Set(['claim-fix-thing']) })).toBe('worktree-active-agent');});@@ -1149,6 +1177,30 @@ describe('reconcile', () => {expect(git.deleteBranch).not.toHaveBeenCalled();});+ it('surfaces an inactive dirty claim worktree as in-flight instead of parking it as human-owned', async () => {+ git.getBranches.mockResolvedValue([+ { name: 'claim/issue-42', isDefault: false, current: false, tracking: null, merged: true }+ ]);+ wt.listWorktrees.mockResolvedValue([+ { path: '/repo/data/cos/worktrees/claim-issue-42', branch: 'refs/heads/claim/issue-42' }+ ]);+ git.hasBranchMergeEvidence.mockResolvedValue(true);+ execGit.mockResolvedValue({ stdout: ' M server/services/thing.js\n', exitCode: 0 });++ const res = await reconcile('/repo', { activeAgentIds: new Set() });++ expect(res.inFlight.map((i) => i.state)).toEqual(['ABANDONED_WIP']);+ expect(res.inFlight[0]).toMatchObject({+ branch: 'claim/issue-42',+ worktreePath: '/repo/data/cos/worktrees/claim-issue-42',+ abandonedClaimWorktree: true,+ });+ expect(res.cleaned).toEqual([]);+ expect(res.skipped).toEqual([]);+ expect(wt.forceRemoveWorktreeDir).not.toHaveBeenCalled();+ expect(git.deleteBranch).not.toHaveBeenCalled();+ });+it('still leaves that branch untouched while its agent is running', async () => {git.getBranches.mockResolvedValue([{ name: 'cos/task-x/agent-deadbeef', isDefault: false, current: false, tracking: 'origin/main', merged: true }diff --git a/server/services/cosTaskPreStepBlocks.compose.test.js b/server/services/cosTaskPreStepBlocks.compose.test.jsindex 5250d6c0c5..2704007c38 100644--- a/server/services/cosTaskPreStepBlocks.compose.test.js+++ b/server/services/cosTaskPreStepBlocks.compose.test.js@@ -91,7 +91,12 @@ vi.mock('./agentState.js', async (importActual) => ({getActiveAgentIds: vi.fn(() => []),}));+vi.mock('./cos.js', () => ({+ getAgents: vi.fn(async () => []),+}));+import { generateManagedAppImprovementTaskForType } from './cosTaskGenerator.js';+import { getAgents } from './cos.js';const APP = { id: 'app-1', name: 'Example App', repoPath: '/tmp/example-repo' };const STATE = { config: { confidenceAutoApproval: { enabled: false }, idleReviewPriority: 'MEDIUM' } };@@ -112,6 +117,7 @@ const generate = () =>describe('generateManagedAppImprovementTaskForType composes the extracted pre-step layer', () => {beforeEach(() => {vi.clearAllMocks();+ getAgents.mockResolvedValue([]);getPerpetualDrainState.mockResolvedValue({ signature: null, dispatchCount: 0 });reconcileMock.mockResolvedValue(scan());});@@ -135,4 +141,24 @@ describe('generateManagedAppImprovementTaskForType composes the extracted pre-stexpect(await generate()).toBeNull();expect(parkPerpetual).toHaveBeenCalled();});++ it('protects a live claim worktree by passing its recorded path token to reconcile', async () => {+ getAgents.mockResolvedValue([+ { id: 'agent-live', status: 'running', workspacePath: '/repo/data/cos/worktrees/claim-issue-99' }+ ]);+ reconcileMock.mockResolvedValue(scan({ inFlight: [] }));++ await generate();++ expect(reconcileMock.mock.calls.at(-1)[1].activeAgentIds).toEqual(+ new Set(['agent-live', 'claim-issue-99'])+ );+ });++ it('does not reconcile when the live-agent state cannot be read', async () => {+ getAgents.mockRejectedValue(new Error('state unavailable'));++ expect(await generate()).toBeNull();+ expect(reconcileMock).not.toHaveBeenCalled();+ });});diff --git a/server/services/cosTaskPreStepBlocks.js b/server/services/cosTaskPreStepBlocks.jsindex ed58b9c9e4..07f8eb85f5 100644--- a/server/services/cosTaskPreStepBlocks.js+++ b/server/services/cosTaskPreStepBlocks.js@@ -31,6 +31,7 @@ import { getCodeReviewDefaults } from './codeReview.js';import { NON_ACTIONABLE_ISSUE_LABELS } from './perpetualWork.js';import { DISPATCH_HINT_FANOUT_GUIDANCE } from '../lib/dispatchLabels.js';import { applyAppPlaceholders } from '../lib/appPromptPlaceholders.js';+import { worktreeAgentId } from '../lib/worktreeOwnership.js';import {appendReviewerEffortBlock,buildLocalReviewerInstructions,@@ -386,6 +387,23 @@ export async function resolveBranchReconcileBlock(app, taskType, metadata, taskSconst { reconcile, filterActionable, limitBranchesForAgent, formatInFlightForPrompt, actionableSignature, describeIdleReconcilePark } = await import('./branchReconcile.js');const { formatSupersededForPrompt } = await import('./supersededLedger.js');const { getActiveAgentIds, isTruthyMeta } = await import('./agentState.js');+ const { getAgents } = await import('./cos.js');+ const activeAgentIds = new Set(getActiveAgentIds());+ const liveAgents = await getAgents().catch((err) => {+ emitLog('warn', `branch-reconcile skipped for ${app.name}: active agent state unreadable (${err.message})`, { appId: app.id, analysisType: taskType });+ return null;+ });+ if (!Array.isArray(liveAgents)) return { skip: true };+ // Claim worktrees use a branch-shaped directory name, not the `agent-*` id+ // stored on the run. Add the live record's workspace basename to the same+ // ownership set so a branch-reconcile sub-agent cannot adopt a claim tree+ // while its original agent is still running (or paused for resume).+ for (const agent of liveAgents) {+ if (agent?.status !== 'running' && agent?.status !== 'paused') continue;+ if (agent.id) activeAgentIds.add(agent.id);+ const worktreeId = worktreeAgentId(agent.workspacePath || agent.metadata?.workspacePath);+ if (worktreeId) activeAgentIds.add(worktreeId);+ }// Action toggles were merged (global → per-app override) + value-constrained// by sanitizeTaskMetadata into `metadata`; each is ON unless explicitly false.const actions = {@@ -397,7 +415,7 @@ export async function resolveBranchReconcileBlock(app, taskType, metadata, taskS};const result = await reconcile(app.repoPath, {cleanup: actions.cleanupMerged !== false,- activeAgentIds: new Set(getActiveAgentIds()),+ activeAgentIds,// The app's gh account pin, so a repo owned by another GitHub account is// polled with a credential that can see it (#7540).forgeAccount: app.forgeAccount || null