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: verify a pinned PR head against a fresh fetch and page review threads (#12486)

[._.]/ Adam Eivy1674d15b83e6
diff --git a/server/services/prRemediationFollowUp.js b/server/services/prRemediationFollowUp.js
index 01f3e9c55f..9204fbb040 100644
--- a/server/services/prRemediationFollowUp.js
+++ b/server/services/prRemediationFollowUp.js
@@ -93,7 +93,7 @@ function renderContext({ repoFullName, number, url, headRefName, headSha, author
' ```',
' Never use `gh pr view` to read the conversation, and never read an unfiltered review, comment, or thread. If both reads return nothing, hand the PR back (below) rather than guessing. Every finding is in scope: fix blocking findings, non-blocking findings, and nits alike. After its feedback is addressed, resolve each unresolved thread PortOS opened — this lists their ids and nothing else:',
' ```bash',
- ` gh api graphql -f owner=${owner} -f repo=${repo} -F number=${number} -f query='query($owner:String!,$repo:String!,$number:Int!){repository(owner:$owner,name:$repo){pullRequest(number:$number){reviewThreads(first:100){nodes{id isResolved comments(first:1){nodes{author{login}}}}}}}}' --jq ".data.repository.pullRequest.reviewThreads.nodes[] | select(.isResolved == false and .comments.nodes[0].author.login == \\"$ME\\") | .id"`,
+ ` gh api graphql --paginate -f owner=${owner} -f repo=${repo} -F number=${number} -f query='query($owner:String!,$repo:String!,$number:Int!,$endCursor:String){repository(owner:$owner,name:$repo){pullRequest(number:$number){reviewThreads(first:100,after:$endCursor){pageInfo{hasNextPage endCursor} nodes{id isResolved comments(first:1){nodes{author{login}}}}}}}}' --jq ".data.repository.pullRequest.reviewThreads.nodes[] | select(.isResolved == false and .comments.nodes[0].author.login == \\"$ME\\") | .id"`,
' gh api graphql -f id=<thread-id> -f query=\'mutation($id:ID!){resolveReviewThread(input:{threadId:$id}){thread{isResolved}}}\'',
' ```',
`2. You are ALREADY in an isolated worktree with \`${headRefName}\` checked out at \`${headSha}\`, the commit PortOS reviewed — PortOS attached it for you. Do not check the PR out again, do not create another worktree, do not switch branches, and never pull new commits from the PR's head branch into it. ${accessNote}`,
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 4ea04ce06e..a178ed892e 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -636,11 +636,16 @@ async function resolveCommit(sourceWorkspace, ref) {
}
/**
- * Refuse a pinned attach unless the remote tip IS the reviewed commit, and
+ * Refuse a pinned attach unless the freshly fetched remote tip IS the reviewed commit, and
* unless resetting an existing local branch to it loses nothing (equal or a
* fast-forward). The pin exists because the attached code is then executed.
*/
-async function assertPinnedAttachable(sourceWorkspace, branchName, startPoint, pinnedHead, localExists) {
+async function assertPinnedAttachable(sourceWorkspace, branchName, remote, pinnedHead, localExists) {
+ // A cached remote-tracking ref can still name the reviewed commit after the
+ // contributor moved or deleted the branch, so the tip must come from a fetch
+ // that succeeded now; a failed fetch refuses the attach.
+ await execGit(['fetch', remote, branchName], sourceWorkspace);
+ const startPoint = `${remote}/${branchName}`;
const remoteTip = await resolveCommit(sourceWorkspace, startPoint);
if (remoteTip !== pinnedHead) {
const err = new Error(`Cannot attach worktree to ${branchName}: ${startPoint} is at ${remoteTip || 'an unreadable commit'}, not the reviewed head ${pinnedHead}`);
@@ -773,8 +778,9 @@ async function createWorktreeUnlocked(agentId, sourceWorkspace, taskId, options
// later `git push` to the CONTRIBUTOR's fork. Tracking origin here would
// push their commits into the upstream repository instead — the #4172
// failure mode, one remote over.
- const startPoint = forkRemote ? `${forkRemote}/${branchName}` : `origin/${branchName}`;
- if (pinnedHead) await assertPinnedAttachable(sourceWorkspace, branchName, startPoint, pinnedHead, !!localExists);
+ const remote = forkRemote || 'origin';
+ const startPoint = `${remote}/${branchName}`;
+ if (pinnedHead) await assertPinnedAttachable(sourceWorkspace, branchName, remote, pinnedHead, !!localExists);
// Use -B (force-create) so we don't fail if a stale local ref exists.
// Attach path re-uses an existing branch, so no orphan-branch cleanup on failure.
// A pinned attach checks out the verified SHA itself, not the ref, so a
diff --git a/server/services/worktreeManager.pinnedHead.test.js b/server/services/worktreeManager.pinnedHead.test.js
index 2dbba19555..d3ee3be457 100644
--- a/server/services/worktreeManager.pinnedHead.test.js
+++ b/server/services/worktreeManager.pinnedHead.test.js
@@ -64,6 +64,18 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('createWorktree pinned to a reviewed hea
expect(existsSync(join(PATHS.worktrees, 'agent-moved'))).toBe(false);
});
+ it('refuses when only a stale remote-tracking ref still names the reviewed commit', async () => {
+ const reviewed = await publish(sandbox.repo, 'origin', BRANCH, 'reviewed.txt');
+ // The branch is deleted upstream (not via this checkout, which would drop
+ // its own tracking ref); without pruning, origin/<branch> survives.
+ await execGit(['config', 'fetch.prune', 'false'], sandbox.repo);
+ await execGit(['branch', '-D', BRANCH], sandbox.origin);
+
+ await expect(createWorktree('agent-stale', sandbox.repo, 'task-stale', { existingBranch: BRANCH, expectedHeadSha: reviewed }))
+ .rejects.toThrow();
+ expect(existsSync(join(PATHS.worktrees, 'agent-stale'))).toBe(false);
+ });
+
it('resolves a pinned fork head through the fork even when origin has a same-named branch', async () => {
// A fork PR opened from a branch whose name origin also uses: the unpinned
// resolve order would pick origin's, a different branch entirely.