Skip to content

Create PR: auto-prepare behind-only branches and return remote errors inline - #8534

Merged
AmethystLiang merged 4 commits into
mainfrom
create-pr-should-handle-sync
Jul 18, 2026
Merged

AmethystLiang merged 4 commits into
mainfrom
create-pr-should-handle-sync

Conversation

@AmethystLiang

@AmethystLiang AmethystLiang commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Create PR intent can prepare behind-only branches in one click instead of blocking with “Sync this branch first,” and remote-action failures are returned inline so the flow can classify them without a stale error mirror.

What changed

  • Behind-only auto-prepare: when hosted review is blocked on needs_sync and the branch is purely behind (ahead = 0), Create PR runs the remote prepare step instead of dead-ending.
  • Diverged branches stay blocked: ahead + behind still requires explicit Sync so Create PR never merges without consent; patch-equivalent divergence still uses force-with-lease.
  • Inline remote errors: runRemoteAction returns the failure object to callers (no lastRemoteActionErrorByWorktreeRef race).
  • Conflict vs generic copy: distinguish genuine merge-conflict sync failures from push-stage / network / auth failures in the intent notice.
  • Also included: keybinding write-path consolidation + legacy seed hardening; flaky process-replacement test pin for Math.random.

Test plan

  • Unit tests for remote-step resolution, eligibility (needs_sync / behind-only), and conflict classifier
  • Keybinding seed / write tests
  • Manually: clean branch behind upstream → Create PR fast-prepares and opens composer/review
  • Manually: diverged branch still shows “Sync this branch before creating a review”
  • Manually: dirty behind-only path (note: follow-up hardens this further)

Follow-up

A safety fix landed locally after this squash: route behind-only through git pull --ff-only (not merge-capable sync), fast-forward before commit for dirty trees, and discriminate runRemoteAction outcomes (ok / failed / superseded / skipped). That will ship in a separate PR.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The Create PR intent flow now returns a dedicated sync step for ordinary fast-forward synchronization, preserves the latest remote action error per worktree, and distinguishes sync merge conflicts from other failures. The UI uses conflict-specific or generic notices accordingly. Tests cover sync-step resolution and error classification. Locales remove the previous needs-sync copy and add sync-state messages, alongside unrelated changed-files and usage-display notice strings.

🚥 Pre-merge checks | ✅ 2 | ❌ 3

❌ Failed checks (2 warnings, 1 inconclusive)

Check name Status Explanation Resolution
Description check ⚠️ Warning No pull request description was provided, so none of the required template sections are filled. Add the required sections: Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The branch-style title is related to sync handling but not a clear, concise summary of the change. Rename it to a concise sentence like "Handle sync conflicts in Create PR flow".
✅ Passed checks (2 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/renderer/src/components/right-sidebar/SourceControl.tsx (1)

2005-2056: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

lastRemoteActionErrorByWorktreeRef is never pruned when a worktree is removed.

This effect exists specifically to drop stale per-worktree ref/state entries once a worktree ID is no longer in worktreeMap (see the comment above it and the sibling remoteActionErrorSequenceByWorktreeRef cleanup at Line 2035). The new lastRemoteActionErrorByWorktreeRef added in this PR tracks the same kind of per-worktree data but isn't included in this pruning pass, so entries for deleted worktrees accumulate for the life of the session.

🧹 Proposed fix to prune the new ref alongside its sibling
     for (const key of Object.keys(remoteActionErrorSequenceByWorktreeRef.current)) {
       if (!worktreeMap.has(key)) {
         delete remoteActionErrorSequenceByWorktreeRef.current[key]
       }
     }
+    for (const key of Object.keys(lastRemoteActionErrorByWorktreeRef.current)) {
+      if (!worktreeMap.has(key)) {
+        delete lastRemoteActionErrorByWorktreeRef.current[key]
+      }
+    }
🧹 Nitpick comments (1)
src/renderer/src/lib/source-control-remote-error.ts (1)

113-123: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consolidate duplicate merge-conflict regex logic.

isMergeConflictErrorMessage (lines 113-122) duplicates the exact two regexes already inline at lines 132 and 141 in resolveRemoteOperationErrorMessage. Two sources of truth for "is this a conflict message" can drift if one is updated without the other, silently breaking the new Create-PR sync-conflict classification.

♻️ Proposed consolidation
-  if (/unmerged files|needs merge|you have not concluded your merge/i.test(error.message)) {
+  if (isMergeConflictErrorMessage(error.message)) {
     if (options?.isRebase) {
       return 'Rebase blocked — resolve existing conflicts first.'
     }
     return options?.isSync
       ? 'Sync blocked — resolve existing merge conflicts first.'
       : 'Pull blocked — resolve existing merge conflicts first.'
   }

Note: this merges the two original conditions (unfinished-merge vs. fresh-conflict) into one check, so the follow-on branches that produced different copy for each would need to be re-derived or kept as a single combined message if that distinction is no longer needed downstream.

Also applies to: 132-132, 141-141


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bab77d84-7479-435d-993c-c8da1e9f442b

📥 Commits

Reviewing files that changed from the base of the PR and between 0b65d72 and 175d690.

📒 Files selected for processing (9)
  • src/renderer/src/components/right-sidebar/SourceControl.tsx
  • src/renderer/src/components/right-sidebar/source-control-create-pr-intent-flow.test.ts
  • src/renderer/src/components/right-sidebar/source-control-create-pr-intent-flow.ts
  • src/renderer/src/i18n/locales/en.json
  • src/renderer/src/i18n/locales/es.json
  • src/renderer/src/i18n/locales/ja.json
  • src/renderer/src/i18n/locales/ko.json
  • src/renderer/src/i18n/locales/zh.json
  • src/renderer/src/lib/source-control-remote-error.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/renderer/src/components/terminal-pane/pty-connection.test.ts (1)

16469-16514: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Restore the global Math.random spy in a finally block.

If any assertion or setup in this test throws, line 16514 is skipped and the mock leaks into subsequent tests. Wrap the test body in try/finally so restoration is guaranteed.

Proposed fix
 const randomSpy = vi.spyOn(Math, 'random').mockReturnValue(0.5)
 
-// existing test body
-
-randomSpy.mockRestore()
+try {
+  // existing test body
+} finally {
+  randomSpy.mockRestore()
+}

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 600091ae-3c6d-430e-b731-f39eacc6dfcd

📥 Commits

Reviewing files that changed from the base of the PR and between 4880414 and b62d2d8.

📒 Files selected for processing (1)
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts

- `needs_sync` now routes ordinary behind branches through the existing
  sync step (fetch/pull/push) instead of blocking Create PR entirely;
  patch-equivalent divergence still force-pushes with lease.
- Distinguish genuine merge-conflict sync failures from push-stage or
  fetch/auth failures so the notice copy tells users to resolve
  conflicts only when that's actually the problem.
- Fix stale-closure bug where the intent runner read outdated remote
  action errors by mirroring them into a ref.
- Extract resolveCreatePrIntentProgressStep and resolveCreatePrIntentRemoteFailureNoticeKind so SourceControl.tsx no longer inlines remote-step/error branching, keeping the classification logic testable and consistent with isCreatePrIntentSyncConflictError
- Dedupe merge-conflict regexes in source-control-remote-error.ts into shared patterns so conflict detection and toast copy can't drift apart
- Clean up stale lastRemoteActionErrorByWorktreeRef entries when a worktree is removed, matching existing cleanup for other per-worktree refs
Process cadence polling uses random jitter to prevent thundering herd.
Pin Math.random to nominal value so test timing is deterministic
and doesn't race condition checks on the replacement-hook owner.
@AmethystLiang
AmethystLiang force-pushed the create-pr-should-handle-sync branch from b62d2d8 to d99e0d1 Compare July 18, 2026 00:07
Replace the stale-prone `lastRemoteActionErrorByWorktreeRef` mirror with
inline error returns from `runRemoteAction`, so callers can classify sync
failures without a synchronization race. Auto-sync only behind-only branches
where the pull is a pure fast-forward; genuinely diverged branches keep the
explicit sync-first stop to prevent accidental merges. Consolidate keybinding
file writes into a shared helper and fix the legacy-binding seed to cache its
snapshot instead of re-reading on first access.
@AmethystLiang

Copy link
Copy Markdown
Contributor Author

@coderabbitai[bot] fixed

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

@AmethystLiang Thanks for fixing this! 🐇


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

@AmethystLiang
AmethystLiang merged commit 45c4a1f into main Jul 18, 2026
2 checks passed
@AmethystLiang
AmethystLiang deleted the create-pr-should-handle-sync branch July 18, 2026 07:21
@AmethystLiang AmethystLiang changed the title refs/heads/create-pr-should-handle-sync Create PR: auto-prepare behind-only branches and return remote errors inline Jul 18, 2026
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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.

1 participant