Skip to content

feat(pr): move task to new PR/MR created on the same branch - #471

Merged
simion merged 2 commits into
simion:mainfrom
nvkvin:feature/multiple-pr-same-task
Nov 26, 2026
Merged

simion merged 2 commits into
simion:mainfrom
nvkvin:feature/multiple-pr-same-task

Conversation

@nvkvin

@nvkvin nvkvin commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Once a task's PR or MR was merged or closed, Termic previously kept polling that settled identity forever even when Phase 2 work continued on the same branch and a new PR or MR was opened.
This change keeps the task pinned to the settled PR or MR until a newer PR or MR exists on the same branch,
while continuing to stay pinned on the settled PR if the checkout switched branches or detached HEAD.

What changed

  • Same-branch rollover rules (src-tauri/src/lib.rs, src-tauri/src/forge.rs):
    • Added persisted pr_head (headRefName / source_branch) on Task and TaskMember so rollover checks know which branch the tracked PR or MR came from.
    • Resolved legacy records missing pr_head by stored PR number first so their real head branch is backfilled before evaluating any branch lookup.
    • Scoped settled replacement acceptance strictly to newer PR or MR numbers (candidate.number > known_number) on the same branch (branch == pr_head), keeping detached HEADs, moved branches, fork review aliases (pr-<n>-<head>), unrelated forks, and older open PRs targeting other bases out of rollover.
    • Added CheckoutBranch distinction (OnBranch vs Detached vs NoGit) and GitHub URL fallback (pr_lookup_github_url_fallback) so tasks whose .git checkout is absent on disk still roll over using their cached GitHub URL and branch while real detached checkouts stay pinned.
  • Batched GitHub replacement scan & forge compatibility (src-tauri/src/forge.rs, src-tauri/src/lib.rs):
    • Updated batch_route to emit BatchRoute::SettledSameBranch and added github_branch_pr_replacements_batch using lightweight paginated GraphQL candidate scans (first: 20, up to 5 pages) followed by github_pr_status_batch_meta hydration for selected winners only.
    • Added ReplacementOutcome (KeepOld, Replace, Retry) and failure streak fallback so rate refusals, partial alias nulls, or transient GraphQL errors leave the cached UI entry intact without locking into long backoff.
    • Serialized PR identity writes with pr_persist_lock and apply_pr_identity so slow background polls cannot overwrite a newer PR just saved by task_pr_create, and updated task_pr_create to hydrate by the returned PR number.
    • Updated GitLab fallback resolution (gitlab_newest_open_mr_iid, gitlab_mr_status) to pass --hostname plus a percent-encoded project path on server-backed tasks (instead of unsupported -R on glab api) and match source_project_id, and updated Azure pick_azure_branch_pr to match forkSource and ignore older IDs once settled.
  • Frontend store, UI, locales, and docs (src/store/pr.ts, src/components/task/PrCard.tsx, src/locales/, docs/ipc.md):
    • Synced task PR identity on transitions and setLookup, checked settled PRs on turn-end (bypassing the 30s floor, including settled multi-repo members), reduced background polling for batchable settled GitHub PRs to 3 minutes (SETTLED_BATCH_STALE_MS) alongside Rust's RECHECK_SECS, enabled unseen highlights and opened toasts when replacing a settled PR, and keyed wasMerged checks on (provider, number) so a direct merged -> merged rollover still runs the merge lifecycle.
    • Added a + button (pr-create-new) on merged and closed PR cards with matching English and Simplified Chinese pr.createNew strings, updated e2e/specs/git.e2e.ts, and documented the updated batch flow in docs/ipc.md.

Testing

  • Manually tested in the dev app by merging the existing PR and creating a new PR on the same branch; on refresh, the task updated to the new open PR.
  • npm test: 256 test files, 3956 tests passed (including rollover unit tests in src/store/pr.test.ts).
  • cargo test --workspace --lib --manifest-path src-tauri/Cargo.toml: passed (termic_lib: 1770 passed; 0 failed; 8 ignored; termic_cli: 98 passed; termic_proto: 29 passed; termic_remote: 73 passed).
  • cargo check --lib, npm run build (tsc -b && vite build), npm run typecheck:e2e, and git diff --check HEAD: all passed after the final edit.
  • Note: live forge API calls and full make e2e WebDriver runs were not executed locally.

Once a tracked PR or MR is merged or closed, keep tracking that settled
identity across detached checkouts or branch switches, but roll the task
over to a newer PR or MR created on the same branch.

- Track each task's and composition member's PR source branch (`pr_head`)
  and reject replacements from other branches, detached HEADs, fork
  review aliases (`pr-<n>-<head>`), unrelated forks, or older open PRs.
- Resolve legacy records lacking `pr_head` by stored number first before
  evaluating rollover, and handle `merged -> merged` transitions cleanly.
- Scan same-branch replacements on GitHub in lightweight paginated
  GraphQL batches and hydrate only selected winners, preserving cached
  entries on rate refusals or transient errors.
- Serialize task PR persistence so stale polls cannot overwrite a newly
  created PR, and hydrate `task_pr_create` by the returned PR number.
- Support `--hostname` + percent-encoded project paths on GitLab API
  fallbacks, `source_project_id` / `forkSource` filtering on GitLab and
  Azure, URL-based fallback for checkouts without `.git`, turn-end
  discovery, and a "+" action on settled PR cards to open a new PR.
@simion

simion commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Could you add a direct test for resolve_pr_with covering a settled PR after the checkout switched branches or went detached, confirming it only re-fetches by the stored number and never reaches accept_branch_candidate? Thanks for the contribution!

— Simion's agent here, he'll take a look at this shortly.

@nvkvin

nvkvin commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Added resolve_pr_with_keeps_settled_pr_pinned_off_branch_or_detached in src-tauri/src/lib.rs.
It asserts that once a PR is settled (merged or closed, including when a cached open PR is discovered live to have settled),
switching to another branch,
detaching HEAD,
or staying on a pr-<n>-<head> fork checkout only re-fetches by the stored PR number
and never invokes by-branch lookup or accept_branch_candidate.

Verified with cargo check --lib, cargo test --workspace --lib --manifest-path src-tauri/Cargo.toml (termic_lib: 1771 passed / 8 ignored, termic_cli: 98 passed, termic_proto: 29 passed, termic_remote: 73 passed), and git diff --check HEAD.

@simion simion left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Good fix, and the added test covers exactly the branch-switch/detached HEAD case called out. Merging.

@simion
simion merged commit d5d1c28 into simion:main Nov 26, 2026
6 of 7 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