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: ignore unrelated primary branch switches
[._.]/ Adam Eivycf7f356a1c69
diff --git a/.changelog/next/fixed-agent-95abb20e.md b/.changelog/next/fixed-agent-95abb20e.mdnew file mode 100644index 0000000000..1c0f9f9d35--- /dev/null+++ b/.changelog/next/fixed-agent-95abb20e.md@@ -0,0 +1 @@+- Worktree agents no longer fail when another actor switches the shared primary checkout to a different branch without stranding commits.diff --git a/server/lib/primaryCheckoutGuard.js b/server/lib/primaryCheckoutGuard.jsindex 5c47ca2cfb..272501c567 100644--- a/server/lib/primaryCheckoutGuard.js+++ b/server/lib/primaryCheckoutGuard.js@@ -66,9 +66,20 @@* warn-logged (unreviewed commits on `main` are worth surfacing) but not failed.* The asymmetry is deliberate — a missed branch-jack still leaves a warn log and* recoverable commits, while a false failure escalates to a human and dents the- * task type's success rate. (A checkout left on the WRONG BRANCH with nothing- * local-only strands no commit to attribute, and still reports its benign- * `git checkout <branch>` recovery as before — there is nothing to discard.)+ * task type's success rate.+ *+ * A checkout left on the WRONG BRANCH with nothing local-only strands no commit to+ * attribute, and for a long time that exempted it from the gate entirely: it failed+ * the run outright, because there was supposedly "nothing to blame on anyone"+ * (#4231). That inverted the gate — the shape carrying the LEAST evidence of a+ * branch-jack was the only one never asked to prove authorship. The primary is+ * shared, and a human running `git checkout -b my-feature` in it is ordinary work,+ * so it failed every agent alive at that moment, with prose that contradicted+ * itself: "No commits were stranded" as the recovery for "A worktree agent+ * committed to the primary". With nothing stranded the only signal left is+ * WHICH branch it landed on, so the agent's OWN worktree branch still fails (a+ * shape only the agent produces) and every other branch is `unattributed` —+ * worth surfacing, since the next spawn baselines against it, but not a failure.** Two halves, deliberately split so they can sit on opposite ends of a run:* `capturePrimaryCheckoutState` stamps a baseline at spawn time (onto the agent@@ -521,9 +532,7 @@ export async function detectPrimaryCheckoutDrift(baseline, { agentBranch = null// have produced them. Attribution runs whenever there is (or should be) a// stranded commit — a resolvable positive count, OR an UNRESOLVABLE count// (`null`: a pruned baseline or a wedged git), which must fail OPEN rather than- // manufacture a failure out of a check that could not run. Only a pure branch- // switch that stranded exactly zero commits skips it (nothing to blame on- // anyone) and keeps its benign `git checkout` report.+ // manufacture a failure out of a check that could not run.// Prefer the patch-level verdict when there is one; a resolved empty set means// nothing is stranded even where the sha count disagrees. Null falls back to the// sha count, and a null THERE falls back to the movement count, which is@@ -531,6 +540,27 @@ export async function detectPrimaryCheckoutDrift(baseline, { agentBranch = nullconst strandedCount = strandedByPatch? strandedByPatch.length: (unpushedCount === null ? commitCount : unpushedCount);++ // A BARE BRANCH SWITCH — the checkout is parked on a different branch and+ // stranded exactly nothing (#4231). This used to skip attribution entirely and+ // fail the run on the theory that there was "nothing to blame on anyone", which+ // inverted the gate: the shape with the LEAST evidence of a branch-jack was the+ // only one that never had to prove authorship. The primary is a shared checkout,+ // and a human running `git checkout -b my-feature` in it is ordinary work — so+ // every agent alive at that moment was failed, with a recovery note that+ // contradicted its own escalation ("No commits were stranded" under "A worktree+ // agent committed to the primary"). agent-8249579a was recorded+ // `success: false` this way while its PR was merging.+ //+ // With nothing stranded there is no commit to attribute by patch-id, so the one+ // signal left is WHICH branch the checkout landed on. The agent's OWN worktree+ // branch is a shape only the agent produces, and it stays a failure. Any other+ // branch is somebody else's business: warn-logged as `unattributed` (the primary+ // being off `main` still matters — the next spawn baselines against it) but never+ // a failure, per the fail-open asymmetry in the module header.+ if (strandedCount === 0 && current.branch !== agentBranch) {+ return { drifted: false, unattributed: true, baseline, current, commitCount, unpushedCount, message };+ }if (strandedCount === null || strandedCount > 0) {const attributed = await isDriftAttributableToAgent(baseline.path, {runBase: baseline.head,@@ -600,7 +630,7 @@ export function formatDriftMessage({ baseline, current, commitCount, baselineRewexport function formatDriftRecovery({ current, baseline = null, commitCount, unpushedCount = null, strandedCount = null, agentBranch, upstreamRef = null }) {if (unpushedCount === 0 && baseline?.branch && baseline.branch !== current.branch) {return [- `A worktree-isolated agent left the PRIMARY checkout on \`${current.branch}\` instead of \`${baseline.branch}\`.`,+ `A worktree-isolated agent left the PRIMARY checkout on its own worktree branch \`${current.branch}\` instead of \`${baseline.branch}\`.`,`No commits were stranded — \`${current.branch}\` carries nothing its upstream lacks — so restore it with \`git -C ${current.path} checkout ${baseline.branch}\`.`,].join(' ');}diff --git a/server/lib/primaryCheckoutGuard.test.js b/server/lib/primaryCheckoutGuard.test.jsindex acb3920289..347d7f2e54 100644--- a/server/lib/primaryCheckoutGuard.test.js+++ b/server/lib/primaryCheckoutGuard.test.js@@ -489,13 +489,71 @@ describe('detectPrimaryCheckoutDrift', () => {expect(verdict.unattributed).toBe(true);});- it('detects a branch switch even with no new commits', async () => {+ it('detects a branch switch onto the agent\'s OWN worktree branch, advising checkout not reset', async () => {+ await addOrigin();+ const agentBranch = 'cos/task-x/agent-y';+ await execGit(['push', 'origin', `main:${agentBranch}`], repo);const baseline = await capturePrimaryCheckoutState(repo);- await execGit(['checkout', '-b', 'someone-elses-branch'], repo);+ // Only the agent parks the shared primary on the agent's own branch, so this+ // stays a failure even though nothing was stranded to recover — and the+ // recovery is the benign `git checkout`, with nothing to discard.+ await execGit(['checkout', '-b', agentBranch, '--track', `origin/${agentBranch}`], repo);- const verdict = await detectPrimaryCheckoutDrift(baseline);+ const verdict = await detectPrimaryCheckoutDrift(baseline, { agentBranch });expect(verdict.drifted).toBe(true);expect(verdict.commitCount).toBe(0);+ expect(verdict.unpushedCount).toBe(0);+ expect(verdict.message).toContain(`main → ${agentBranch}`);+ expect(verdict.suggestedFix).toContain(`git -C ${repo} checkout main`);+ expect(verdict.suggestedFix).not.toContain('reset --hard');+ });++ it('does not fail a run for a branch switch it did not make (#4231)', async () => {+ await addOrigin();+ const agentBranch = 'cos/task-x/agent-y';+ await execGit(['branch', agentBranch, 'origin/main'], repo);+ const baseline = await capturePrimaryCheckoutState(repo);+ // The HUMAN starts a feature branch in the shared primary mid-run and pushes+ // it, so nothing at all is stranded. This used to skip attribution ENTIRELY+ // and record the run `success: false` with an escalation to a human, under a+ // recovery note that contradicted it ("No commits were stranded").+ await execGit(['checkout', '-b', 'my-feature'], repo);+ await commit('the human\'s own work');+ await execGit(['push', '-u', 'origin', 'my-feature'], repo);++ const verdict = await detectPrimaryCheckoutDrift(baseline, { agentBranch });+ expect(verdict.unpushedCount).toBe(0);+ expect(verdict.drifted).toBe(false);+ expect(verdict.unattributed).toBe(true);+ // Still surfaced: the next spawn baselines against wherever the primary is.+ expect(verdict.message).toContain('main → my-feature');+ expect(verdict.suggestedFix).toBeUndefined();+ });++ it('does not fail a run for an UNPUSHED branch the human started mid-run (#4231)', async () => {+ await addOrigin();+ const agentBranch = 'cos/task-x/agent-y';+ await execGit(['branch', agentBranch, 'origin/main'], repo);+ const baseline = await capturePrimaryCheckoutState(repo);+ // Same shape, no upstream on the new branch — the human's commit IS stranded,+ // but it is not patch-equivalent to anything on the agent's branch.+ await execGit(['checkout', '-b', 'my-feature'], repo);+ await commit('the human\'s own work');++ const verdict = await detectPrimaryCheckoutDrift(baseline, { agentBranch });+ expect(verdict.drifted).toBe(false);+ expect(verdict.unattributed).toBe(true);+ });++ it('does not fail a run for a branch switch when it carried no branch at all (#4231)', async () => {+ const baseline = await capturePrimaryCheckoutState(repo);+ await execGit(['checkout', '-b', 'someone-elses-branch'], repo);++ // A read-only reasoner never branched, so it cannot have moved the checkout.+ const verdict = await detectPrimaryCheckoutDrift(baseline, { agentBranch: null });+ expect(verdict.drifted).toBe(false);+ expect(verdict.unattributed).toBe(true);+ expect(verdict.commitCount).toBe(0);expect(verdict.message).toContain('main → someone-elses-branch');});@@ -538,19 +596,6 @@ describe('detectPrimaryCheckoutDrift', () => {expect(verdict.suggestedFix).toContain(`git -C ${repo} reset --hard origin/main`);});- it('advises checkout, not reset, for a branch switch that stranded nothing', async () => {- await addOrigin();- await execGit(['push', 'origin', 'main:someone-elses-branch'], repo);- const baseline = await capturePrimaryCheckoutState(repo);- await execGit(['checkout', '-b', 'someone-elses-branch', '--track', 'origin/someone-elses-branch'], repo);-- const verdict = await detectPrimaryCheckoutDrift(baseline);- expect(verdict.drifted).toBe(true);- expect(verdict.unpushedCount).toBe(0);- expect(verdict.suggestedFix).toContain(`git -C ${repo} checkout main`);- expect(verdict.suggestedFix).not.toContain('reset --hard');- });-it('reports an attributed commit on a branch with no upstream to clear it against', async () => {// No `origin` at all: an unpushed commit is unreviewed by definition, so the// guard must not go quiet just because it cannot compare against an upstream —