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: stop blaming a worktree agent for a commit it only rebased onto

[._.]/ Adam Eivy7c4453fa9450
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 305016b0c2..16b2c639c9 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -202,3 +202,5 @@
- Internal: [issue-3713] `brctl evict <path>` turns out to work, correcting a note carried by [issue-3706] and [issue-3716] that recorded programmatic eviction as unavailable to an unentitled CLI (leaving a manual Finder step as the only route). Like `brctl download` it is absent from `brctl --help` but fully functional, refusing only when run as root. That makes the whole evict/measure/heal loop scriptable, so the next iCloud syscall question can be answered without a human at the keyboard; the recipe, and the tooling that genuinely does not work (`fileproviderctl`, `chflags dataless`, `FileManager.evictUbiquitousItem`), is now a section in `server/lib/icloudFile.js`.
- **[issue-3716] Dropped the unreachable legacy `.icloud` placeholder probe from the MortalLoom store.** macOS has two possible representations for an iCloud file evicted by "Optimize Mac Storage": the modern APFS *dataless* vnode (path stays put, `stat` reports a real size with zero blocks) and the pre-APFS `.<name>.icloud` sibling stub that *replaces* the file. `mortalLoomStore` guarded both — the stub probe added speculatively alongside a real dataless incident, never observed firing — and the Obsidian vault adapter guarded only the first, so `createNote` would have written a fresh note beside a stub-shadowed one. Before copying the probe into a second adapter, the representation was measured: across 223 iCloud ubiquity containers holding 373 evicted files (macOS 26, APFS), every evicted file was dataless and zero `.icloud` placeholders existed anywhere under the iCloud root or the home directory. The probe is dead code, so it was deleted rather than duplicated — an absent path is now unambiguously absent on both read and write, and `updateStore`'s overwrite guard makes one `existsSync` call instead of three. The finding, and where the probe would have to go if a placeholder is ever actually observed, is recorded in `server/lib/icloudFile.js`. No user-visible behavior change: refusing to overwrite an evicted (dataless) store, and the on-demand `brctl download` pre-warm, are untouched.
+
+- **[issue-3725] A worktree agent is no longer failed for a commit it merely rebased onto.** The primary-checkout branch-jack guard attributes stranded commits to the agent's own branch before blaming a run, but one of its two attribution paths asked only "is this commit reachable from the agent's branch tip?" — which is equally true of a commit the agent *authored and applied to `main`* and one another actor put on `main` mid-run that the agent then *rebased onto*. Since `/do:pr` rebases on `main` before pushing, every worktree run that outlived a concurrent commit to `main` inherited that commit into its branch's base and was failed with `primary-checkout-mutated`, escalating to a human and denting the task type's success rate — in the observed case the run had actually finished its work and pushed its branch. Attribution now recognizes that a branch built strictly *on top of* the drift descends from it rather than causing it, and skips the blame. A real branch-jack leaves the primary *at* a commit the agent authored, never strictly behind its own branch, so the original incident shape is still caught.
diff --git a/server/lib/primaryCheckoutGuard.js b/server/lib/primaryCheckoutGuard.js
index c184f03489..76eb70eb93 100644
--- a/server/lib/primaryCheckoutGuard.js
+++ b/server/lib/primaryCheckoutGuard.js
@@ -34,8 +34,10 @@
* strands commits, they are attributed to the agent's own worktree branch by
* PATCH-ID (`git cherry`, not raw SHA, so a cherry-picked or rebased copy still
* matches). Stranded commits the agent demonstrably did not author — a read-only
- * reasoner that never branched, or commits with no patch-equivalent on the
- * agent's branch — are carried as `{ drifted: false, unattributed: true }`:
+ * reasoner that never branched, commits with no patch-equivalent on the agent's
+ * branch, or commits the agent merely REBASED ONTO (#3725: `/do:pr` rebases on
+ * `main` before pushing, so any run that outlives a concurrent commit to `main`
+ * inherits it into its branch's base) — are carried as `{ drifted: false, unattributed: true }`:
* 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
@@ -182,6 +184,21 @@ async function resolveAgentBranchSha(checkoutPath, agentBranch) {
return null;
}
+/**
+ * Is `descendant` STRICTLY ahead of `ancestor` — reachable from it, and not the same
+ * commit? Non-throwing; an unresolvable comparison returns false, which the caller
+ * treats as "no reason to skip the attribution checks".
+ */
+async function isStrictlyAhead(checkoutPath, ancestor, descendant) {
+ if (!ancestor || !descendant || ancestor === descendant) return false;
+ const result = await execGit(
+ ['merge-base', '--is-ancestor', ancestor, descendant],
+ checkoutPath,
+ { ignoreExitCode: true, timeout: GIT_TIMEOUT_MS },
+ ).catch(() => null);
+ return Boolean(result && result.exitCode === 0);
+}
+
/**
* Was the drift plausibly THIS agent's doing (#3703)? Looks only at the commits
* that could actually be this run's branch-jack — reachable from the drifted head,
@@ -208,6 +225,20 @@ async function resolveAgentBranchSha(checkoutPath, agentBranch) {
async function isDriftAttributableToAgent(checkoutPath, { runBase, upstream, driftedHead, agentBranch }) {
const agentSha = await resolveAgentBranchSha(checkoutPath, agentBranch);
if (!agentSha) return false;
+ // The agent's branch is BUILT ON TOP OF the drifted head, so the stranded commits are
+ // part of the branch's BASE — put there by whoever moved the primary first, then
+ // inherited when the agent branched from (or rebased onto) the moved `main`. `/do:pr`
+ // rebases onto `main` before pushing, so EVERY agent that outlives a concurrent commit
+ // to `main` ends in this shape; the reachability check below only asks "is this commit
+ // on the agent's branch?", and would read that inherited base as proof of authorship.
+ // The causal arrow runs primary → agent here, which is a rebase, not a branch-jack.
+ //
+ // A real jack leaves the primary AT a commit the agent authored — its branch tip, or a
+ // patch-equivalent copy on a divergent history — never strictly BEHIND its own branch,
+ // so the #3680 incident shape still fails this test and is still caught. The residual
+ // miss (an agent that jacks and then keeps committing to its branch) is the documented
+ // fail-open trade in the module header, and its commits are on a pushed branch anyway.
+ if (await isStrictlyAhead(checkoutPath, driftedHead, agentSha)) return false;
// Candidate branch-jack commits (set S): new this run (`^runBase`) AND unpushed
// (`^upstream`, when there is an upstream to compare against), non-merge only — a
// merge commit reaches the primary via a human's `git merge` / non-ff pull, never a
diff --git a/server/lib/primaryCheckoutGuard.test.js b/server/lib/primaryCheckoutGuard.test.js
index b266b08ef6..ffabfacf25 100644
--- a/server/lib/primaryCheckoutGuard.test.js
+++ b/server/lib/primaryCheckoutGuard.test.js
@@ -213,6 +213,51 @@ describe('detectPrimaryCheckoutDrift', () => {
expect(verdict.unattributed).toBe(true);
});
+ it('does not blame an agent that REBASED onto a commit another actor put on main (#3725)', async () => {
+ // Observed on agent-c925dbfb: a foreign actor committed F to the primary's main
+ // mid-run; the agent then rebased its worktree branch onto the moved main (what
+ // `/do:pr` does before pushing) and added its own commit on top. F is now reachable
+ // from the agent's branch tip, so excluding that tip drops the stranded count to
+ // zero and the reachability check reads the INHERITED BASE as proof of authorship.
+ // The agent built on F; it did not put F there.
+ await addOrigin();
+ const agentBranch = 'cos/task-x/agent-y';
+ const baseline = await capturePrimaryCheckoutState(repo);
+ // A foreign actor strands F on the primary's main during the run...
+ await commit('a foreign actor\'s commit F');
+ const drifted = (await capturePrimaryCheckoutState(repo)).head;
+ // ...and the agent's branch is cut from the moved main (equivalently: rebased onto
+ // it) and carries its own work on top.
+ await execGit(['checkout', '-b', agentBranch, drifted], repo);
+ await commit('the agent\'s actual work');
+ await execGit(['checkout', 'main'], repo);
+
+ const verdict = await detectPrimaryCheckoutDrift(baseline, { agentBranch });
+ expect(verdict.drifted).toBe(false);
+ expect(verdict.unattributed).toBe(true);
+ expect(verdict.unpushedCount).toBe(1);
+ expect(verdict.suggestedFix).toBeUndefined();
+ });
+
+ it('still catches a jack whose commits sit at the agent branch tip (#3725 must not over-clear)', async () => {
+ // The #3680 shape: the agent's own commits were applied to main, leaving the primary
+ // AT the agent's branch tip — not strictly behind it. The rebase escape hatch above
+ // keys on STRICTLY ahead, so this must remain a failure.
+ await addOrigin();
+ const agentBranch = 'cos/task-x/agent-y';
+ const baseline = await capturePrimaryCheckoutState(repo);
+ await execGit(['checkout', '-b', agentBranch], repo);
+ await commit('the agent\'s own work');
+ const agentTip = (await capturePrimaryCheckoutState(repo)).head;
+ // A stray `/do:pr` fast-forwards the PRIMARY's main onto the agent's commit.
+ await execGit(['checkout', 'main'], repo);
+ await execGit(['merge', '--ff-only', agentTip], repo);
+
+ const verdict = await detectPrimaryCheckoutDrift(baseline, { agentBranch });
+ expect(verdict.drifted).toBe(true);
+ expect(verdict.reason).toBe(PRIMARY_CHECKOUT_MUTATED_REASON);
+ });
+
it('never attributes a branch-jack to a read-only reasoner that never branched (Case A)', async () => {
const baseline = await capturePrimaryCheckoutState(repo);
// A 24-file commit lands on main mid-run, authored by another actor.