Skip to content

Conversation

@elithrar
Copy link
Contributor

@elithrar elithrar commented Jan 3, 2026

Fixes a false-negative workflow failure when the OpenCode agent creates a PR via gh pr create during its run - the post-run infrastructure then attempts to create a duplicate PR for the same branch, causing the workflow to fail with "A pull request already exists".

  • Check for existing open PRs with the same head→base combination before calling pulls.create (unique for any open PR)
  • Return the existing PR number if found, avoiding the duplicate PR error
  • Attempts to create the PR (no worse than existing behavior) if it cannot find a pull
  • a simple withRetry to retry the PR check and PR create API calls (net improvement)

Ref: elithrar/dotfiles#19

@elithrar elithrar force-pushed the fix/duplicate-pr-check branch from b6a93e4 to bbf7d0c Compare January 3, 2026 21:39
@elithrar
Copy link
Contributor Author

elithrar commented Jan 3, 2026

Example of the bug:

  • the agent uses the gh CLI to create a PR (not expected by the harness)
  • the harness sees a dirty branch and attempts to create a PR (it's normal behvavior)
  • PR already exists for that base:head combo and "fails", but "a" PR was created (not great, Bob)
image

@elithrar
Copy link
Contributor Author

elithrar commented Jan 3, 2026

logs - note the agent called gh pr and the harness failing after:

|  Bash     {"command":"gh pr create --title \"Add worktree plugin for concurrent branch development\" --body \"$(cat <<'EOF'\n## Summary\n\n- Adds a worktree plugin (`.opencode/plugin/worktree.ts`) with a `use-worktree` tool for concurrent branch development\n- Implements session-scoped worktrees at `/tmp/opencode-worktree-{sessionID}/` to avoid conflicts between sessions\n- Supports user-defined merge strategies (`ours`, `theirs`, `manual`) with conflict resolution guidance\n\n## Key Features\n\n### Agent Hooks\nThe tool exposes clear actions for the agent to use:\n- `create`: Create a new worktree for a branch (with optional `createBranch` flag)\n- `list`: List all worktrees in the current session\n- `remove`: Remove a specific worktree\n- `merge`: Merge worktree changes back to a target branch\n- `status`: Get the status of a worktree (changes, commits ahead/behind)\n- `cleanup`: Remove all session worktrees\n\n### Merge Strategies\n- **ours**: On conflict, keep changes from the target branch\n- **theirs**: On conflict, keep changes from the worktree branch\n- **manual**: Stop on conflict and return diff for user decision\n\n### Crash-Proof Design\n- All exceptions are caught and emitted as error messages\n- Plugin never throws unhandled exceptions that could crash OpenCode\n- `safeExec` wrapper handles all shell command failures gracefully\n- Event handlers wrapped in try/catch\n\n### Automatic Cleanup\n- Subscribes to `session.deleted` and `session.error` events\n- Automatically removes all session worktrees when session ends\n- Prunes dangling worktree references\n\n## Files Changed\n- `.opencode/plugin/worktree.ts` - The worktree plugin implementation\n- `.opencode/package.json` - Plugin dependencies (`@opencode-ai/plugin`)\n- `.gitignore` - Exclude plugin `node_modules/` and `bun.lock`\n\nCloses #17\nEOF\n)\"","description":"Create PR for worktree plugin"}


Created PR #18: https://github.com/elithrar/dotfiles/pull/18

## Summary

<snip>

Checking if branch is dirty...
368e353b5d39d321bcad48c8ee79662b1cd17dbe
Sending message to opencode...
Add worktree plugin with tool (PR #18)

Pushing to new branch...
branch 'opencode/issue17-20260103124524' set up to track 'origin/opencode/issue17-20260103124524'.
Everything up-to-date
Creating pull request...
POST /repos/elithrar/dotfiles/pulls - 422 with id 2028:22DF8B:BF2795A:358685D8:6959106A in 331ms
118 |       request: requestOptions
119 |     });
120 |   }
121 |   if (status >= 400) {
122 |     octokitResponse.data = await getResponseData(fetchResponse);
123 |     throw new RequestError(toErrorMessage(octokitResponse.data), status, {
                ^
HttpError: Validation Failed: {"resource":"PullRequest","code":"custom","message":"A pull request already exists for elithrar:opencode/issue17-20260103124524."} - https://docs.github.com/rest/pulls/pulls#create-a-pull-request
   status: 422,
  request: {
  method: "POST",
  url: "https://api.github.com/repos/elithrar/dotfiles/pulls",
  headers: [Object ...],
  body: [Object ...],
  request: [Object ...],
},
 response: {
  url: "https://api.github.com/repos/elithrar/dotfiles/pulls",
  status: 422,
  headers: [Object ...],
  data: [Object ...],
},

@elithrar
Copy link
Contributor Author

elithrar commented Jan 3, 2026

test failures are from the worktree changes and are failing on the current dev tip (f6cc84747)

@elithrar elithrar marked this pull request as ready for review January 3, 2026 22:06
@elithrar elithrar changed the title fix(github): handle duplicate PR creation when agent creates PR github: handle duplicate PR creation when agent creates PR Jan 3, 2026
@elithrar elithrar force-pushed the fix/duplicate-pr-check branch from 7564ded to 6137a76 Compare January 4, 2026 01:58
@rekram1-node
Copy link
Collaborator

/review

@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2026

lgtm

@rekram1-node rekram1-node merged commit 4486174 into anomalyco:dev Jan 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants