Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

docs(workflow): require PRs to contain latest origin/main

vastsafff4fd06d34c
diff --git a/.github/workflows/pr-base.yml b/.github/workflows/pr-base.yml
new file mode 100644
index 000000000..469d737db
--- /dev/null
+++ b/.github/workflows/pr-base.yml
@@ -0,0 +1,33 @@
+name: PR base
+
+# Every pull request must contain the current target branch tip (`main`).
+# ci.yml ignores docs, so this gate lives on its own and runs for every PR.
+
+on:
+ pull_request:
+
+permissions:
+ contents: read
+
+concurrency:
+ group: pr-base-${{ github.ref }}
+ cancel-in-progress: true
+
+jobs:
+ pr-base:
+ name: Head contains latest base
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ steps:
+ - uses: actions/checkout@v7
+ with:
+ fetch-depth: 0
+
+ - name: Check PR base ancestry
+ env:
+ PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
+ PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
+ run: node scripts/check-pr-base-main.mjs --base "$PR_BASE_SHA" --head "$PR_HEAD_SHA"
+
+ - name: Unit-test the PR base check
+ run: node --test scripts/check-pr-base-main.test.mjs
diff --git a/AGENTS.md b/AGENTS.md
index 7eb504f19..3dcdac89e 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -1,6 +1,6 @@
# AGENTS.md
-Policy-Sync: 2026-09-20.1
+Policy-Sync: 2026-09-21.1
Mandatory rules for AI coding agents working in PI-Desktop.
@@ -253,6 +253,22 @@ integration strategy or rely on the PR integration candidate per § 16.
Resolve conflicts inside your own worktree. Never resolve task conflicts
by modifying the primary checkout.
+### Before opening or updating a PR
+
+The request head must contain the latest `origin/main`. `origin/main`
+must be an ancestor of that head:
+
+```bash
+git fetch origin main
+git merge-base --is-ancestor origin/main HEAD
+pnpm check:pr-base
+```
+
+If that fails, refresh in the task worktree (`git rebase origin/main` on
+a private branch; a non-destructive merge when rewriting history would
+be unsafe), then re-run the check. Do not open or update a PR that is
+behind `origin/main`.
+
### Fixed delivery order
```text
@@ -261,7 +277,7 @@ by modifying the primary checkout.
3. run targeted static/unit/integration checks
4. review the task diff
5. commit the task
-6. refresh the task branch against latest origin/main
+6. refresh the task branch against latest origin/main (`pnpm check:pr-base`)
7. resolve conflicts inside the task worktree
8. run required task-candidate E2E in the task worktree
9. push the request branch
@@ -274,7 +290,8 @@ by modifying the primary checkout.
Do not insert `merge task → local main` between steps 6 and 8. The task
branch itself becomes the local integration candidate by incorporating
-the latest `origin/main`.
+the latest `origin/main`. Do not open or update a PR that is behind
+`origin/main`.
A task-candidate E2E result is valid only when its tested commit and
base revision are known. The PR integration gate then protects against
@@ -757,6 +774,7 @@ Landing blockers:
* relevant test failure
* required E2E failure
* merge conflict
+* PR head does not contain the latest `origin/main`
* data corruption risk
* security violation
* secret leakage
diff --git a/CLAUDE.md b/CLAUDE.md
index fa5945bae..b9d670562 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -1,6 +1,6 @@
# CLAUDE.md
-Policy-Sync: 2026-09-20.1
+Policy-Sync: 2026-09-21.1
Instructions for Claude Code CLI and Claude Cowork on PI-Desktop.
@@ -78,7 +78,7 @@ Branch names: `feat/...`, `fix/...`, `docs/...`, `refactor/...`, `chore/...`.
14. remove your worktree and merged local branch
```
-Do **not** insert `merge task → local main` between refresh and task-candidate E2E. The task branch itself is the local integration candidate after incorporating latest `origin/main`.
+Do **not** insert `merge task → local main` between refresh and task-candidate E2E. The task branch itself is the local integration candidate after incorporating latest `origin/main`. Do not open or update a PR that is behind `origin/main`. Run `pnpm check:pr-base` before opening or updating a PR.
Record E2E evidence:
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index a1de95ea8..48a366eb9 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -35,6 +35,9 @@ For a suspected security vulnerability, do not open a public issue. Follow
- Create the branch from an up-to-date `origin/main` and use a name such as
`feat/provider-import`, `fix/session-refresh`, or
`docs/contributing-guide`.
+- Before opening or updating a pull request, `origin/main` must be an ancestor
+ of the request head. Run `pnpm check:pr-base`. Do not open or update a PR
+ that is behind `origin/main`.
- Preserve uncommitted work in the primary checkout. Do not reset, stash, move,
or overwrite another contributor's work.
- Do not reuse another request's branch or worktree. Remove only your own
@@ -122,11 +125,11 @@ Run only the relevant subset when the change is low risk. Documentation-only
changes normally need no runtime tests; at minimum, review the rendered
Markdown and run `git diff --check`.
-Code-bearing changes require the relevant E2E suite on the integrated local
-`main` before the request branch is pushed and the pull request is opened. A
-run on the request branch itself is useful for debugging but does not replace
-that gate. Record any unavailable required suite as `NOT RUN` with its reason,
-alternative validation, and remaining risk.
+Code-bearing changes require the relevant E2E suite on a candidate that
+contains latest `origin/main` before the request branch is pushed and the pull
+request is opened. A run on a stale request branch is useful for debugging but
+does not replace that gate. Record any unavailable required suite as `NOT RUN`
+with its reason, alternative validation, and remaining risk.
## Commit and Pull Request
@@ -144,8 +147,9 @@ Before committing, review the complete diff. Never commit:
- Local databases, logs, configuration, or machine-specific paths.
- `node_modules/`, build artifacts, release packages, or unrelated changes.
-Merge the request branch into local `main` and run the required E2E suite from
-that integrated checkout first; then open a pull request against `main` with:
+Refresh against latest `origin/main` (`pnpm check:pr-base`) and run the
+required E2E suite from the request worktree first; then open a pull request
+against `main` with:
- a concise summary and rationale;
- affected specs, ADRs, and E2E scenarios;
@@ -169,14 +173,14 @@ git branch -d <type>/<short-description>
git worktree prune
```
-Local `main` already carries its own integration merge of the request branch,
-so the remote merge is synchronized with `git merge origin/main` rather than a
-fast-forward pull. Reset local `main` to `origin/main` once the request commits
-are verified present in remote `main` and that local merge is no longer needed.
+After a remote merge, local `main` is synchronized with `git merge --ff-only
+origin/main` (or `git merge origin/main` if it has diverged). Do not merge the
+request branch into local `main` merely to open a PR.
-For code-bearing changes, record the required E2E result from the integrated
-local `main` before the pull request is opened, and rerun the affected suites
-when the remote merge lands executable content that differs from that commit.
+For code-bearing changes, record the required E2E result from a candidate that
+contains latest `origin/main` before the pull request is opened, and rerun the
+affected suites when the remote merge lands executable content that differs
+from that commit.
## Issue and Security Reports
diff --git a/docs/spec/06-delivery/03-ai-development-workflow.md b/docs/spec/06-delivery/03-ai-development-workflow.md
index e59a12a68..56ac50fcb 100644
--- a/docs/spec/06-delivery/03-ai-development-workflow.md
+++ b/docs/spec/06-delivery/03-ai-development-workflow.md
@@ -37,9 +37,9 @@ The rules below govern every change to the PI-Desktop codebase and documentation
### R4 — Request branch + worktree + merge gate
-> **Every development request starts from `main` in a dedicated branch and
-> worktree. A user request to commit or push includes completing the task's
-> integration into `main` through the authorized delivery route.**
+> **Every development request starts from current `origin/main` in a
+> dedicated branch and worktree. Opening or updating a pull request
+> requires that head to contain the latest `origin/main`.**
- Before editing, preserve any existing uncommitted work, fetch `origin/main`,
fast-forward local `main` when its worktree is clean, and create a new request
@@ -59,34 +59,33 @@ The rules below govern every change to the PI-Desktop codebase and documentation
environment state into tracked files. Install or generate worktree-local
state only when isolation or version compatibility requires it.
- Development commits and direct pushes on `main` are forbidden.
-- A user request to commit, push, or both authorizes and requires integrating
- this task into local `main`. Do not stop after a task-branch commit or push,
- or ask for a second merge confirmation. Explicit branch-only or draft-only
- instructions override this completion target.
-- A commit or local-merge request does not itself authorize remote publishing.
- If remote delivery has not been authorized, complete the required validation
- and merge into local `main` without pushing or opening a remote PR/MR.
+- A commit request does not itself authorize remote publishing. If remote
+ delivery has not been authorized, stop after the request-branch commit in the
+ worktree. Do not merge the task into local `main` to create an E2E candidate.
+- Before opening or updating a PR/MR, fetch `origin/main` and make it an
+ ancestor of the request head (`git merge-base --is-ancestor origin/main HEAD`,
+ or `pnpm check:pr-base`). Refresh in the task worktree (`git rebase
+ origin/main` on a private branch; a non-destructive merge when rewriting
+ history would be unsafe). Do not open or update a PR that is behind
+ `origin/main`.
- When a push or other remote delivery is authorized, the fixed delivery order
- applies: merge the request branch into local `main`, run the required E2E
- gate against that integrated commit (R7), and only then push the request
- branch, open a PR/MR targeting `main`, pass the required remote checks and
- reviews, and merge using a permitted strategy. Fetch and safely synchronize
- local `main` with the landed change. Do not infer permission to push directly
- to `main`, force-push, or discard unrelated local work.
-- A code-bearing change is pushed for review, opened as a PR/MR, and merged
- remotely only after the request branch is merged into local `main` and the
- R7 gate has run against that integrated commit.
+ in `AGENTS.md` applies: refresh against latest `origin/main`, run
+ task-candidate E2E in the worktree, push the request branch, open a PR/MR
+ targeting `main`, pass the required remote checks (including the PR-base
+ gate) and reviews, and merge using a permitted strategy. Then fetch and
+ safely synchronize local `main` with the landed change. Do not infer
+ permission to push directly to `main`, force-push, or discard unrelated
+ local work.
- Both routes retain the required validation, security, and conflict gates.
- Relevant E2E runs on the integrated local `main` before the request branch is
- pushed for review, under R7. If a gate, authentication, permissions, or
- required review prevents integration, report the actual blocker and remaining
- work; the requested integration is not Done.
+ Relevant E2E runs on a candidate that already contains latest `origin/main`
+ (R7). If a gate, authentication, permissions, or required review prevents
+ integration, report the actual blocker and remaining work; the requested
+ integration is not Done.
- Worktree cleanup is mandatory and immediate. As soon as the request branch is
- integrated into `main` — including a local `main` merge when the request is
- delivered without a remote PR/MR — remove the worktree and delete the merged
- branch. A merged request must not leave a worktree on disk. Remove only your
- own worktree and branch, and only after verifying the merge commits are
- present in `main`.
+ integrated into remote `main` (or the user-authorized local-only delivery is
+ complete), remove the worktree and delete the merged branch. A merged request
+ must not leave a worktree on disk. Remove only your own worktree and branch,
+ and only after verifying the merge commits are present in `main`.
- If the user requests a launch after delivery, build and start the app from
the integrated `main` checkout and its development environment.
@@ -182,9 +181,9 @@ an unambiguous pull request number for this repository.
### R7 — Code-bearing changes require relevant E2E after main integration
-> **Every code-bearing change must pass relevant E2E on the integrated local
-> `main` before its request branch is pushed, a PR/MR is opened, or a
-> commit-only delivery is declared complete.**
+> **Every code-bearing change must pass relevant E2E on a candidate that
+> already contains the latest `origin/main`, before its request branch is
+> pushed or a PR/MR is opened.**
This rule applies to changes that modify executable or runtime-affecting
content, including `apps/`, `packages/`, `crates/`, runtime scripts, build or
@@ -192,12 +191,10 @@ CI configuration, packaging behavior, protocol behavior, and persisted data.
Documentation-only changes are exempt when they do not alter executable
behavior.
-E2E execution is mandatory for code-bearing changes, and the gate runs on the
-integrated local `main` commit that carries the change, before the branch push
-and the PR/MR; a route that stops at local `main` runs it before the change is
-declared delivered. Run the selected suites from the latest integrated local
-`main` checkout and commit. An E2E run on the request branch itself is
-exploratory and does not satisfy R7. Select suites using the
+E2E execution is mandatory for code-bearing changes. Task-candidate E2E runs
+in the request worktree after incorporating `origin/main`. Do not merge the
+task into local `main` to satisfy this gate. An E2E run on a branch that is
+behind `origin/main` does not satisfy R7. Select suites using the
regression-surface guidance in `04-e2e-test-plan.md`; build, typecheck,
lint, unit tests, integration tests, manual review, and source inspection do
not replace relevant E2E.
@@ -209,8 +206,8 @@ If a required suite cannot run in the current environment, record the suite,
reason, alternative validation, and remaining risk as `NOT RUN`. The branch
push and PR/MR may still proceed with that record so the change can be
validated in a capable environment, but the gate is not satisfied and delivery
-remains incomplete until the suite passes against the integrated `main` that
-carries the change. A failed required suite blocks the push, the PR/MR, and
+remains incomplete until the suite passes against a candidate that contains
+latest `origin/main`. A failed required suite blocks the push, the PR/MR, and
declaring the change delivered until the failure is classified and fixed.
After the PR/MR merges into remote `main`, rerun the affected suites when the
@@ -252,8 +249,8 @@ Every change follows this sequence. Steps may be iterated if the implementation
7. Run targeted local checks necessary for the change's risk
8. Commit with conventional message
9. Update BOARD if milestone-related
-10. Complete the requested local `main` integration and run the relevant E2E
- gate from that integrated commit (R7); when remote delivery is authorized,
+10. Refresh against latest `origin/main` (`pnpm check:pr-base`) and run
+ task-candidate E2E in the worktree (R7); when remote delivery is authorized,
push branch + open PR/MR to main only after that gate
11. Merge the PR/MR into remote `main` through the remote gates when remote
delivery is authorized, synchronize local `main`, verify, and clean up
@@ -272,11 +269,11 @@ Every change follows this sequence. Steps may be iterated if the implementation
| **4. Implement** | Write code, config, or assets. | Changed files. |
| **5. Spec-sync** | Update specs per the impact list. Add ADR if architectural. Update `decisions-log.md` if an implementation default changes. | Updated docs/spec/\* and/or docs/adr/\*. |
| **6. E2E doc** | When R3 applies, add or update scenario entries in `04-e2e-test-plan.md` and link to acceptance criteria IDs (A–H). Otherwise, confirm no scenario update is needed. | Updated e2e test plan, or confirmed not applicable. |
-| **7. Validate** | Use change risk and regression scope to select the smallest useful local checks. The relevant E2E gate for a code-bearing change runs on the integrated local `main` after step 10's local integration and before any branch push or PR/MR; a suite that cannot run is recorded as `NOT RUN` and keeps delivery incomplete. | Targeted check and E2E results, or an explicit environment limitation. |
+| **7. Validate** | Use change risk and regression scope to select the smallest useful local checks. The relevant E2E gate for a code-bearing change runs in the request worktree after `origin/main` is incorporated (`pnpm check:pr-base`) and before any branch push or PR/MR; a suite that cannot run is recorded as `NOT RUN` and keeps delivery incomplete. | Targeted check and E2E results, or an explicit environment limitation. |
| **8. Commit** | Git commit with conventional message (see §4). | One or more commits. |
| **9. BOARD** | If the change completes a milestone deliverable, update `docs/project/BOARD.md`. | Updated board. |
-| **10. Local integrate + gate** | Complete the requested local `main` integration, then run the relevant E2E gate from that integrated commit. When remote publishing is authorized, push the request branch and open a PR/MR targeting `main` only after that gate. | Verified local `main` integration with the E2E gate result, or a recorded `NOT RUN` limitation; reviewable remote change or a local-only delivery route. |
-| **11. Remote merge + cleanup** | For authorized remote delivery, merge the PR/MR into remote `main` through the required gates and synchronize local `main`; rerun the affected suites when the landed executable content differs from the commit the gate ran on. Verify the expected commits and remove the merged worktree and branch. | Requested integration complete, or an explicit blocker / narrower user-requested handoff. |
+| **10. Refresh + gate** | Refresh against latest `origin/main`, confirm it is an ancestor of HEAD (`pnpm check:pr-base`), then run task-candidate E2E in the worktree. When remote publishing is authorized, push the request branch and open a PR/MR targeting `main` only after that gate. | Verified candidate with the E2E gate result, or a recorded `NOT RUN` limitation; reviewable remote change or a local-only delivery route. |
+| **11. Remote merge + cleanup** | For authorized remote delivery, merge the PR/MR into remote `main` through the required gates (including the PR-base check) and synchronize local `main`; rerun the affected suites when the landed executable content differs from the commit the gate ran on. Verify the expected commits and remove the merged worktree and branch. | Requested integration complete, or an explicit blocker / narrower user-requested handoff. |
| **12. Launch** | When requested, build and start from the integrated `main` checkout and its development environment. | Running app includes the delivered change. |
### Local Validation and E2E Execution Policy
@@ -285,29 +282,29 @@ Every change follows this sequence. Steps may be iterated if the implementation
delivery. Documentation-only changes and low-risk mechanical edits normally
require no local tests or checks and proceed to the authorized delivery route
under R4. No separate approval or waiver is needed to skip unnecessary checks;
- the required E2E gate on the integrated local `main` still applies to
- code-bearing changes.
+ the required E2E gate on a candidate that already contains latest
+ `origin/main` still applies to code-bearing changes.
- Changes with material regression risk, including security boundaries,
protocol contracts, data migrations, build configuration, or widely shared
behavior, normally require the smallest targeted non-E2E validation that can
address that risk. A full local suite is not the default.
- E2E scenario documentation and E2E execution are separate concerns. R3 still
requires scenario updates for user-visible or protocol-visible behavior.
-- Every code-bearing change must run at least one relevant E2E suite on the
- integrated local `main` before its branch is pushed or a PR/MR is opened,
- and must run the union of suites required by the affected regression
- surfaces. The available commands are defined by the root `package.json` and
- the selection matrix in `04-e2e-test-plan.md`.
-- Development-time iteration remains risk-based: an E2E run on the request
- branch may be used for debugging, but only a run against the integrated local
- `main` commit satisfies this policy.
+- Every code-bearing change must run at least one relevant E2E suite on a
+ candidate that contains latest `origin/main` before its branch is pushed or
+ a PR/MR is opened, and must run the union of suites required by the affected
+ regression surfaces. The available commands are defined by the root
+ `package.json` and the selection matrix in `04-e2e-test-plan.md`.
+- Development-time iteration remains risk-based: an E2E run on a stale request
+ branch may be used for debugging, but only a run after incorporating
+ `origin/main` satisfies this policy.
- If the environment cannot run a required suite, record the suite, reason,
alternative validation, and remaining risk as `NOT RUN`. The branch push and
PR/MR may proceed with that record, but the gate is not satisfied and
- delivery remains incomplete until the suite passes against the integrated
- `main` that carries the change.
+ delivery remains incomplete until the suite passes against a candidate that
+ contains latest `origin/main`.
- Required E2E jobs that the hosting platform starts before a remote merge do
- not replace the local `main` gate. Observe and report their result; after
+ not replace the task-candidate gate. Observe and report their result; after
the remote merge, rerun the affected suites when the landed executable
content differs from the commit the gate ran on. Otherwise the recorded
result stands.
@@ -429,12 +426,13 @@ The repository uses a mandatory request-branch and worktree workflow:
when sharing would be unsafe or incompatible, but that state stays ignored
and must not leak into commits.
- **Delivery follows R4's authorization boundary and its fixed order.**
- Commit-only delivery completes a validated local `main` merge. Authorized
- push/remote delivery first merges the request branch into local `main` and
- runs the required E2E gate against that integrated commit (R7), then pushes
- the request branch and opens the PR/MR, merges it into remote `main` through
- the PR/MR gates, and synchronizes local `main`. Explicit branch-only or
- draft-only requests retain their narrower scope.
+ Commit-only delivery stops at the request-branch commit in the worktree.
+ Authorized push/remote delivery first refreshes against latest `origin/main`
+ (`pnpm check:pr-base`), runs task-candidate E2E in the worktree (R7), then
+ pushes the request branch and opens the PR/MR, merges it into remote `main`
+ through the PR/MR gates (including the PR-base check), and synchronizes
+ local `main`. Explicit branch-only or draft-only requests retain their
+ narrower scope. Do not open or update a PR that is behind `origin/main`.
Typical request start (run from the primary checkout; choose a path outside it):
@@ -446,10 +444,9 @@ git worktree add -b <type>/<short-description> <worktree-path> origin/main
If `main` is checked out in a clean primary worktree, `git switch main` plus
`git fetch origin main` and `git pull --ff-only origin main` should be run
-before `git worktree add`. That fast-forward fails once local `main` carries
-its own integration merge of a delivered request; synchronize with
-`git merge origin/main` in that case, and resolve the divergence before
-starting new work without discarding commits. If the primary worktree is not
+before `git worktree add`. If local `main` has diverged, synchronize with
+`git merge origin/main` and resolve the divergence before starting new work
+without discarding commits. If the primary worktree is not
clean or is on another branch, leave it untouched and create the request
worktree directly from the fetched `origin/main`. Never discard, stash, move,
or overwrite unrelated work merely to satisfy this sequence.
@@ -464,62 +461,35 @@ Typical authorized GitHub delivery (use the hosting platform's equivalent when
needed; synchronize and clean up from the clean primary checkout after merge):
```bash
-# from the primary checkout: local `main` integration and the R7 E2E gate come first
-cd <primary-checkout>
+# from the request worktree
git fetch origin main
-git switch main
-git merge <type>/<short-description>
-# run the required E2E suites for this change from the integrated local `main` (R7)
+git rebase origin/main # private branch; do not force-push a shared branch
+pnpm check:pr-base
+# run the required task-candidate E2E suites for this change (R7)
git push -u origin <type>/<short-description>
gh pr create --base main --head <type>/<short-description>
gh pr checks --watch
gh pr merge --merge
git fetch origin main
-git merge origin/main
-git merge-base --is-ancestor <type>/<short-description> origin/main
-git worktree remove <worktree-path>
-git branch -d <type>/<short-description>
-git worktree prune
-git push origin --delete <type>/<short-description>
-```
-
-After the remote merge, synchronize local `main` with `git merge origin/main`:
-local `main` normally carries its own integration merge of the request branch
-and is no longer strictly behind `origin/main`. Reset local `main` to
-`origin/main` once the request commits are verified present in remote `main`
-and that local merge is no longer needed; the reset also restores plain
-fast-forward synchronization for later requests.
-
-If the primary checkout cannot take the local integration merge, use a
-short-lived worktree for `main` instead of disturbing unrelated work. If the
-PR/MR is abandoned, the local integration merge remains in local `main` until
-it is reset.
-
-Request cleanup when the request is integrated by merging into local `main`
-instead of a remote PR/MR (run from the primary checkout):
-
-```bash
+# from a clean primary checkout
git switch main
-git merge <type>/<short-description>
-git merge-base --is-ancestor <type>/<short-description> main
+git merge --ff-only origin/main
git worktree remove <worktree-path>
git branch -d <type>/<short-description>
git worktree prune
+git push origin --delete <type>/<short-description>
```
-For local-only delivery, `git branch -d` may refuse because the request branch
-still tracks `origin/main`, which does not contain the locally delivered
-commits. Only if the ancestry check above exited successfully and this is your
-own request branch with that inherited upstream, unset its upstream and retry
-normal deletion:
+After the remote merge, synchronize local `main` with a fast-forward when
+possible. Reset local `main` to `origin/main` only after the request commits
+are verified present in remote `main` and no local-only commits remain.
-```bash
-git branch --unset-upstream <type>/<short-description>
-git branch -d <type>/<short-description>
-```
+If the primary checkout cannot take that synchronization, use a short-lived
+worktree for `main` instead of disturbing unrelated work.
-Local `main` must contain every task commit. This fallback does not apply to
-a published request branch whose remote `main` integration is still pending.
+Do not merge the request branch into local `main` to create an E2E candidate
+or to open a PR. Local-only delivery (no remote PR/MR) stays on the request
+branch until the user explicitly asks to integrate it.
The worktree must be clean before removal; commit or discard the request's own
leftover changes first. Use `git branch -d` rather than `-D` so an unmerged
@@ -540,19 +510,19 @@ explicit branch-only or draft-only delivery scope:
3. All impacted specs are updated.
4. E2E scenarios are documented (or confirmed not needed per §3).
5. Necessary targeted local validation passes; for a code-bearing change the
- relevant E2E gate has run against the integrated local `main` commit before
- the branch push, the PR/MR, or a commit-only completion, or the environment
- limitation is recorded as `NOT RUN` and delivery remains incomplete until
- the suite passes against the integrated `main` that carries the change;
- automatically triggered remote gates also pass.
+ relevant E2E gate has run on a candidate that contains latest `origin/main`
+ before the branch push, the PR/MR, or a commit-only completion, or the
+ environment limitation is recorded as `NOT RUN` and delivery remains
+ incomplete until that suite passes; automatically triggered remote gates
+ also pass, including the PR-base ancestry check.
6. Change is committed with a conventional message.
7. BOARD is updated if a milestone deliverable completed.
8. No secrets or local data are present in the commit.
-9. Requested commit/push delivery is integrated into local `main` before the
- request branch is pushed or the PR/MR is opened. When remote delivery was
- authorized, the PR/MR was reviewed and merged into remote `main`, local
- `main` includes the landed change, and the affected suites were rerun when
- the landed executable content differs from the commit the gate ran on.
+9. Before a PR/MR is opened or updated, `origin/main` is an ancestor of the
+ request head (`pnpm check:pr-base`). When remote delivery was authorized,
+ the PR/MR was reviewed and merged into remote `main`, local `main` includes
+ the landed change, and the affected suites were rerun when the landed
+ executable content differs from the commit the gate ran on.
10. After integration, the expected commits were verified in `main`, the request
worktree was removed, and the merged request branch was deleted.
11. If a GitHub issue was linked: the claim was verified before implementation;
@@ -589,12 +559,13 @@ D164, and D260. GitHub release notes are not a substitute.
| Large uncommitted diffs | Violates R2; loss of granularity |
| Changing behavior without spec update | Violates R1; specs become unreliable |
| Skipping e2e doc for user-visible changes | Violates R3; traceability gap |
-| Declaring a code-bearing change complete without successful relevant E2E after integration into local or remote `main` | Violates R7 and leaves cross-process behavior unverified |
+| Declaring a code-bearing change complete without successful relevant E2E after incorporating latest `origin/main` | Violates R7 and leaves cross-process behavior unverified |
| Treating the full local test/check suite as an automatic pre-push requirement | Ignores risk-based validation and delays delivery without evidence of need |
| Developing or creating development commits on `main`, or pushing it directly | Violates R4; bypasses isolation and review gates |
| Developing a new request in the primary checkout or another request's worktree | Violates R4; mixes task files and local state |
| Reusing a request branch for unrelated work | Mixes request scope and weakens traceability |
-| Stopping at a task-branch commit or push when the user requested commit/push delivery | Violates R4 unless the user explicitly limited delivery to a branch or draft |
+| Opening or updating a PR/MR whose head is behind `origin/main` | Violates R4; review starts on a stale base |
+| Stopping at a task-branch commit or push when the user requested remote delivery | Violates R4 unless the user explicitly limited delivery to a branch or draft |
| Leaving a merged request worktree on disk | Violates R4; stale worktrees accumulate and invite cross-request contamination |
| Modifying baseline frozen decisions without ADR + version bump | Baseline is frozen; changes need formal process |
| Committing generated artifacts that CI should rebuild | Repo bloat, merge conflicts |
@@ -608,7 +579,7 @@ D164, and D260. GitHub release notes are not a substitute.
| Force-pushing a contributor's branch to land a linked pull request | Violates R6; landing fixes go on top of the author's commits |
| Tagging a stable app release without updating `packages/shared/src/changelog.ts` for every shipped locale | Violates D164 / D345 / release runbook; in-app What's new is empty for that locale and version |
| Tagging a stable app release while `README.md` / `README.zh-CN.md` still state an older release line, or bypassing `scripts/check-release-docs.mjs` with `--skip-docs-check` | Violates D260 / release runbook; published documentation advertises a version the release no longer matches |
-| Pushing a request branch or opening a PR/MR before the required E2E has run against the integrated local `main`, or declaring a code-bearing change delivered without that result (except a recorded `NOT RUN` limitation) | Violates R7's fixed order; review would start on an unvalidated commit |
+| Pushing a request branch or opening a PR/MR before the required E2E has run against a candidate that contains latest `origin/main`, or declaring a code-bearing change delivered without that result (except a recorded `NOT RUN` limitation) | Violates R7's fixed order; review would start on an unvalidated commit |
---
@@ -624,17 +595,20 @@ This workflow spec itself is accepted when:
worktree created from current `main`.
- [ ] Request worktrees reuse the primary checkout's environment where safe
without committing local environment state.
-- [ ] Commit/push requests complete local `main` integration without another
- merge confirmation; explicit branch-only or draft-only limits prevail.
+- [ ] Opening or updating a PR/MR requires `origin/main` to be an ancestor of
+ the request head (`pnpm check:pr-base`); a PR behind `origin/main` is
+ rejected.
- [ ] Remote publishing requires authorization and uses PR/MR gates before
remote `main` integration and local synchronization.
-- [ ] Local `main` integration and the relevant E2E gate precede the request
- branch push and the pull request.
+- [ ] Refreshing against latest `origin/main` and the relevant E2E gate precede
+ the request branch push and the pull request. Do not merge the task into
+ local `main` to create that candidate.
- [ ] Worktree removal and branch deletion are required immediately after the
- request branch is merged into `main`, including local-merge delivery.
-- [ ] Relevant E2E execution is mandatory for code-bearing changes on the
- integrated local `main` before the PR/MR is opened, without a separate
- test request; E2E documentation remains mandatory under R3.
+ request branch is merged into `main`.
+- [ ] Relevant E2E execution is mandatory for code-bearing changes on a
+ candidate that contains latest `origin/main` before the PR/MR is opened,
+ without a separate test request; E2E documentation remains mandatory
+ under R3.
- [ ] Local validation is risk-based; unnecessary checks may be skipped without
blocking commit, push, or PR/MR creation.
- [ ] Definition of Done is complete and actionable.
diff --git a/docs/spec/06-delivery/05-change-checklist.md b/docs/spec/06-delivery/05-change-checklist.md
index 87b9735c7..9365af2d7 100644
--- a/docs/spec/06-delivery/05-change-checklist.md
+++ b/docs/spec/06-delivery/05-change-checklist.md
@@ -73,9 +73,9 @@ Before editing any file for a new request:
- [ ] Mutable, incompatible, or concurrency-sensitive environment state stays
worktree-local and ignored.
- [ ] The current branch is not `main` before implementation begins.
-- [ ] Delivery scope is recorded: a commit request includes local `main`
- integration; a push request includes PR-based remote `main` integration and
- local synchronization. Explicit branch-only or draft-only limits are honored.
+- [ ] Delivery scope is recorded: a commit request stays on the request
+ branch; a push request includes PR-based remote `main` integration after
+ `pnpm check:pr-base`. Explicit branch-only or draft-only limits are honored.
---
@@ -121,11 +121,11 @@ After implementation (or alongside it):
cross-component regression risk makes them necessary.
- [ ] The smallest necessary targeted local checks passed, or local validation
was assessed as unnecessary with no separate approval or waiver required.
-- [ ] Relevant E2E suites were selected and passed on the integrated local
- `main` before the request branch was pushed and a PR/MR was opened, or before
- a commit-only delivery was declared complete; required validation needs no
- separate user request. Documentation-only changes retain their existing
- exemption.
+- [ ] Relevant E2E suites were selected and passed on a candidate that contains
+ latest `origin/main` before the request branch was pushed and a PR/MR was
+ opened, or before a commit-only delivery was declared complete; required
+ validation needs no separate user request. Documentation-only changes retain
+ their existing exemption.
- [ ] Results apply to the commit the gate ran on, and the affected suites were
rerun when the landed executable content changed. Any required suite not run
is recorded with its reason, alternative validation, and remaining risk;
@@ -154,23 +154,22 @@ After implementation (or alongside it):
Before marking requested integration complete, apply the R4 delivery scope;
explicit branch-only or draft-only requests retain their narrower scope:
-- [ ] Requested commit/push delivery is integrated into local `main`; a
- task-branch commit or push alone was not reported as completion, and no second
- merge confirmation was requested.
-- [ ] For a code-bearing change: the relevant E2E gate ran against the
- integrated local `main` before the branch push and PR/MR creation, or before
- a commit-only delivery was declared complete, or its `NOT RUN` limitation is
- recorded with reason, alternative validation, and remaining risk.
-- [ ] Commit-only or local-merge delivery did not publish remotely without
- separate authorization.
+- [ ] Before a PR/MR is opened or updated, `origin/main` is an ancestor of the
+ request head (`pnpm check:pr-base`). A PR behind `origin/main` is not opened.
+- [ ] For a code-bearing change: the relevant E2E gate ran against a candidate
+ that contains latest `origin/main` before the branch push and PR/MR creation,
+ or before a commit-only delivery was declared complete, or its `NOT RUN`
+ limitation is recorded with reason, alternative validation, and remaining risk.
+- [ ] Commit-only delivery did not publish remotely without separate
+ authorization.
- [ ] For authorized remote delivery: the request branch was pushed, its PR/MR
targeted `main` with only this task's changes, and the description documented
impacted specs, E2E scenarios, and validation.
-- [ ] For authorized remote delivery: PR self-review, required checks, and
- reviews passed; the PR/MR merged into remote `main` using a permitted
- strategy; local `main` was synchronized with the landed change; when the
- landed executable content differs from the commit the gate ran on, the
- affected suites were rerun and recorded.
+- [ ] For authorized remote delivery: PR self-review, required checks (including
+ the PR-base ancestry gate), and reviews passed; the PR/MR merged into remote
+ `main` using a permitted strategy; local `main` was synchronized with the
+ landed change; when the landed executable content differs from the commit the
+ gate ran on, the affected suites were rerun and recorded.
- [ ] No direct push to `main`, force-push, discarded unrelated work, or bypassed
gate was inferred from the delivery request. Genuine blockers were reported.
- [ ] Request worktree is removed after merge.
@@ -234,11 +233,11 @@ user's delivery scope:
| # | Gate | Source |
|---|---|---|
-| 1 | Request branch and worktree created from an up-to-date `main`; primary environment reused where safe | [R4 — Request branch + worktree + merge gate](03-ai-development-workflow.md#r4--request-branch--worktree--merge-gate) |
+| 1 | Request branch and worktree created from an up-to-date `origin/main`; primary environment reused where safe | [R4 — Request branch + worktree + merge gate](03-ai-development-workflow.md#r4--request-branch--worktree--merge-gate) |
| 2 | Code/doc implements the planned change | Step 4 of [development loop](03-ai-development-workflow.md#2-development-loop) |
| 3 | All impacted specs updated | [R1 — Spec-sync](03-ai-development-workflow.md#r1--spec-first--spec-sync) |
| 4 | E2E scenarios documented (or confirmed not needed) | [R3 — E2E coverage doc](03-ai-development-workflow.md#r3--e2e-coverage-doc) |
-| 5 | Targeted local checks follow the existing risk standard; for a code-bearing change the relevant E2E gate passed on the integrated local `main` before the branch push, the PR/MR, or a commit-only completion (or its `NOT RUN` limitation is recorded); after the remote merge the affected suites were rerun when the landed executable content changed; required tests need no separate user request | Steps 7, 10, and 11 of development loop |
+| 5 | Targeted local checks follow the existing risk standard; for a code-bearing change the relevant E2E gate passed on a candidate that contains latest `origin/main` (`pnpm check:pr-base`) before the branch push, the PR/MR, or a commit-only completion (or its `NOT RUN` limitation is recorded); after the remote merge the affected suites were rerun when the landed executable content changed; required tests need no separate user request | Steps 7, 10, and 11 of development loop |
| 6 | Change committed with conventional message | [R2 — Commit-per-change](03-ai-development-workflow.md#r2--commit-per-change) |
| 7 | BOARD updated if milestone deliverable completed | Step 9 of development loop |
| 8 | No secrets or local data in commit | [§4.4 Never commit](03-ai-development-workflow.md#44-never-commit) |
diff --git a/docs/zh-CN/spec/06-delivery/03-ai-development-workflow.md b/docs/zh-CN/spec/06-delivery/03-ai-development-workflow.md
index 66e45ee01..63e111c26 100644
--- a/docs/zh-CN/spec/06-delivery/03-ai-development-workflow.md
+++ b/docs/zh-CN/spec/06-delivery/03-ai-development-workflow.md
@@ -40,43 +40,18 @@
### R4 — 请求分支+工作树+合并门
-> **每个开发请求都必须从 `main` 的专用分支和工作树开始。用户请求提交或推送时,还必须通过获准的交付路径将该任务集成到 `main`。**
-
-- 在编辑之前,保留任何现有的未提交工作,获取 `origin/main`,
- 当工作树干净时快进本地 `main`,并创建一个新请求
- 来自最新提交的分支和工作树。小学现有工作
- 决不能仅仅为了开始新的操作而移动、隐藏或覆盖结账
- 请求。
-- 每个请求使用一个短期分支。命名它
- `<type>/<short-description>`,其中 `type` 匹配常规更改
- 实用时键入,例如 `feat/provider-import` 或
- `docs/request-branch-workflow`。
-- 每个请求使用一个专用工作树。不要在
- 主要结账或重用另一个请求的工作树。
-- 在安全的情况下重用主要结帐的开发环境:已安装
- 工具链、包管理器存储、构建缓存和忽略本地
- 环境配置仍然是规范环境。参考或
- 需要时将这些资源链接到请求工作树中;请勿复制
- 环境状态写入跟踪文件。安装或生成worktree-local
- 仅当隔离或版本兼容性需要时才声明。
-- 禁止对 `main` 进行开发提交和直接推送。
-- 用户请求提交、推送或两者时,即授权并要求将本次任务集成到本地
- `main`。如果用户没有明确要求仅保留分支或草稿,不得停在任务分支提交或推送,
- 也不得再次请求合并确认。
-- 仅请求提交或本地合并,并不自动授权远程发布。未获远程交付授权时,完成必要的
- 验证并合入本地 `main`,不要推送或创建远程 PR/MR。
-- 远程推送获得授权后,推送请求分支,创建面向 `main` 的 PR/MR,通过所需的远程
- 检查和审查,并使用仓库允许的策略合并。随后安全地获取并同步本地 `main`。
- 不得据此推断可以直接推送 `main`、强制推送或丢弃无关本地工作。
-- 两条交付路径都必须保留验证、E2E、安全和冲突门禁。如果门禁、认证、权限或所需
- 审查阻止集成,必须报告实际阻塞原因和剩余工作;该请求尚未完成。
-- 工作树清理是强制性的并且是立即的。一旦请求分支
- 集成到 `main` — 包括请求时的本地 `main` 合并
- 在没有远程 PR/MR 的情况下交付 — 删除工作树并删除合并的
- 分支。合并的请求不得在磁盘上留下工作树。仅删除您的
- 自己的工作树和分支,并且只有在验证合并提交之后
- 存在于 `main` 中。
-- 如果用户在交付后要求启动应用,必须从已集成的 `main` 工作树和开发环境构建并启动。
+> **每个开发请求都必须从当前 `origin/main` 的专用分支和工作树开始。打开或更新 PR 时,该 head 必须包含最新的 `origin/main`。**
+
+- 编辑前保留未提交工作,获取 `origin/main`,工作树干净时快进本地 `main`,并从该提交创建请求分支和工作树。不得为了开新请求而移动、藏匿或覆盖主工作区里的现有工作。
+- 每个请求使用一个短期分支,命名为 `<type>/<short-description>`。
+- 每个请求使用一个专用工作树。不要在主工作区或别人的工作树里实现新请求。
+- 在安全的情况下复用主工作区的工具链、包存储和缓存。
+- 禁止在 `main` 上开发或直接推送。
+- 仅请求提交并不授权远程发布。未获远程授权时,停在任务分支提交;不要为了跑 E2E 把任务合进本地 `main`。
+- 打开或更新 PR/MR 之前,获取 `origin/main` 并确认它是请求 head 的祖先(`git merge-base --is-ancestor origin/main HEAD` 或 `pnpm check:pr-base`)。私有分支用 rebase;已共享且不宜改写历史时用非破坏性合并。不得打开或更新落后于 `origin/main` 的 PR。
+- 获准远程交付后,按 `AGENTS.md` 的固定顺序:相对最新 `origin/main` 刷新,在工作树跑 task-candidate E2E,推送请求分支,打开面向 `main` 的 PR/MR,通过远程检查(含 PR-base 门)和审查后合并,再同步本地 `main`。
+- 合入 `main` 后立即删除自己的工作树和已合并分支。
+- 若用户要求启动应用,从已集成的 `main` 构建并启动。
### R5 — 先核实链接的 GitHub issue,再回复并关闭
diff --git a/docs/zh-CN/spec/06-delivery/05-change-checklist.md b/docs/zh-CN/spec/06-delivery/05-change-checklist.md
index 1f0033641..0e6aa8ed3 100644
--- a/docs/zh-CN/spec/06-delivery/05-change-checklist.md
+++ b/docs/zh-CN/spec/06-delivery/05-change-checklist.md
@@ -125,11 +125,11 @@
在将请求标记为完成之前:
- [ ] 请求分支被推送到远程。
+- [ ] 打开或更新 PR 前,`origin/main` 已是请求 head 的祖先(`pnpm check:pr-base`)。不得打开落后于 `origin/main` 的 PR。
- [ ] PR/MR 以 `main` 为目标,并且仅包含请求的逻辑更改。
- [ ] PR 描述列出了受影响的规格和 e2e 场景。
- [ ] 公关自我审查清单已完成。
-- [ ] 要求远程检查和审查通过。
-- [ ] 使用允许的合并策略将 PR/MR 合并到 `main` 中。
+- [ ] 要求远程检查(含 PR-base 门)和审查通过。
- [ ] 请求工作树在合并后被删除。
- [ ] 合并请求分支在本地删除 (`git branch -d`)。
- [ ] 合并后删除远程请求分支。
diff --git a/package.json b/package.json
index f46ab099b..cea4a8a9e 100644
--- a/package.json
+++ b/package.json
@@ -43,6 +43,7 @@
"check:marketplace": "node scripts/check-marketplace-catalog.mjs",
"check:release-docs": "node scripts/check-release-docs.mjs",
"check:agent-policy": "node scripts/check-agent-policy-sync.mjs",
+ "check:pr-base": "node scripts/check-pr-base-main.mjs",
"docs:dev": "pnpm --filter @pi-desktop/docs dev",
"docs:build": "pnpm --filter @pi-desktop/docs build",
"docs:preview": "pnpm --filter @pi-desktop/docs preview",
diff --git a/scripts/check-agent-policy-sync.mjs b/scripts/check-agent-policy-sync.mjs
index ba2adffa3..66b103d7a 100644
--- a/scripts/check-agent-policy-sync.mjs
+++ b/scripts/check-agent-policy-sync.mjs
@@ -91,6 +91,10 @@ const REQUIRED_ANCHORS = [
id: "no-local-main-before-e2e",
anyOf: ["merge task → local main"],
},
+ {
+ id: "pr-contains-latest-main",
+ anyOf: ["do not open or update a PR that is behind `origin/main`"],
+ },
{
id: "task-candidate-e2e",
anyOf: ["task-candidate e2e"],
diff --git a/scripts/check-pr-base-main.mjs b/scripts/check-pr-base-main.mjs
new file mode 100644
index 000000000..4d98fbabe
--- /dev/null
+++ b/scripts/check-pr-base-main.mjs
@@ -0,0 +1,95 @@
+#!/usr/bin/env node
+/**
+ * Require the current head to contain the latest integration base.
+ *
+ * Locally that base is `origin/main`. In GitHub Actions pass the pull
+ * request base and head SHAs. `origin/main` must be an ancestor of the
+ * PR head — do not open or update a PR that is behind `origin/main`.
+ *
+ * Usage:
+ * node scripts/check-pr-base-main.mjs
+ * node scripts/check-pr-base-main.mjs --base origin/main --head HEAD
+ * pnpm check:pr-base
+ */
+import { execFileSync } from "node:child_process";
+import { fileURLToPath } from "node:url";
+
+const root = fileURLToPath(new URL("..", import.meta.url));
+
+function parseArgs(argv) {
+ const out = {
+ base: process.env.PR_BASE_SHA || process.env.GITHUB_BASE_SHA || "origin/main",
+ head: process.env.PR_HEAD_SHA || "HEAD",
+ cwd: root,
+ };
+ for (let i = 0; i < argv.length; i += 1) {
+ const flag = argv[i];
+ const value = argv[i + 1];
+ if (flag === "--base" && value) {
+ out.base = value;
+ i += 1;
+ } else if (flag === "--head" && value) {
+ out.head = value;
+ i += 1;
+ } else if (flag === "--cwd" && value) {
+ out.cwd = value;
+ i += 1;
+ }
+ }
+ return out;
+}
+
+function git(cwd, args) {
+ return execFileSync("git", args, {
+ cwd,
+ encoding: "utf8",
+ stdio: ["ignore", "pipe", "pipe"],
+ }).trim();
+}
+
+function fail(message) {
+ console.error(message);
+ process.exit(1);
+}
+
+const args = parseArgs(process.argv.slice(2));
+
+let baseSha;
+let headSha;
+try {
+ baseSha = git(args.cwd, ["rev-parse", "--verify", `${args.base}^{commit}`]);
+ headSha = git(args.cwd, ["rev-parse", "--verify", `${args.head}^{commit}`]);
+} catch (error) {
+ const detail = error.stderr?.toString().trim() || error.message;
+ fail(
+ `PR base check: cannot resolve --base ${args.base} or --head ${args.head}. ` +
+ `Fetch origin/main first.\n${detail}`,
+ );
+}
+
+if (baseSha === headSha) {
+ console.log(`PR base check passed: head ${headSha.slice(0, 12)} is the base.`);
+ process.exit(0);
+}
+
+try {
+ git(args.cwd, ["merge-base", "--is-ancestor", baseSha, headSha]);
+} catch {
+ let behind = "unknown";
+ try {
+ behind = git(args.cwd, ["rev-list", "--count", `${headSha}..${baseSha}`]);
+ } catch {
+ // Keep the generic count when history is incomplete.
+ }
+ fail(
+ `PR base check failed: ${args.base} (${baseSha.slice(0, 12)}) is not an ancestor of ` +
+ `${args.head} (${headSha.slice(0, 12)}). The branch is ${behind} commit(s) behind the base. ` +
+ `Fetch origin/main and rebase (private branch) or merge it, then re-run pnpm check:pr-base. ` +
+ `Do not open or update a PR that is behind origin/main.`,
+ );
+}
+
+console.log(
+ `PR base check passed: ${args.base} (${baseSha.slice(0, 12)}) is an ancestor of ` +
+ `${args.head} (${headSha.slice(0, 12)}).`,
+);
diff --git a/scripts/check-pr-base-main.test.mjs b/scripts/check-pr-base-main.test.mjs
new file mode 100644
index 000000000..340990cb8
--- /dev/null
+++ b/scripts/check-pr-base-main.test.mjs
@@ -0,0 +1,101 @@
+import assert from "node:assert/strict";
+import { execFileSync } from "node:child_process";
+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import test from "node:test";
+import { fileURLToPath } from "node:url";
+
+const script = fileURLToPath(new URL("./check-pr-base-main.mjs", import.meta.url));
+
+function git(cwd, args) {
+ return execFileSync("git", args, {
+ cwd,
+ encoding: "utf8",
+ stdio: ["ignore", "pipe", "pipe"],
+ }).trim();
+}
+
+function runCheck(cwd, extra = []) {
+ try {
+ const stdout = execFileSync("node", [script, "--cwd", cwd, ...extra], {
+ encoding: "utf8",
+ stdio: ["ignore", "pipe", "pipe"],
+ });
+ return { code: 0, stdout, stderr: "" };
+ } catch (error) {
+ return {
+ code: error.status ?? 1,
+ stdout: error.stdout?.toString() ?? "",
+ stderr: error.stderr?.toString() ?? "",
+ };
+ }
+}
+
+function initRepo() {
+ const dir = mkdtempSync(join(tmpdir(), "pr-base-"));
+ git(dir, ["init", "-b", "main"]);
+ git(dir, ["config", "user.email", "[email protected]"]);
+ git(dir, ["config", "user.name", "Dev"]);
+ writeFileSync(join(dir, "a.txt"), "a\n");
+ git(dir, ["add", "a.txt"]);
+ git(dir, ["commit", "-m", "base"]);
+ return dir;
+}
+
+test("check-pr-base-main accepts a head that already contains main", () => {
+ const dir = initRepo();
+ try {
+ git(dir, ["checkout", "-b", "feat/ok"]);
+ writeFileSync(join(dir, "b.txt"), "b\n");
+ git(dir, ["add", "b.txt"]);
+ git(dir, ["commit", "-m", "feat"]);
+ const result = runCheck(dir, ["--base", "main", "--head", "HEAD"]);
+ assert.equal(result.code, 0);
+ assert.match(result.stdout, /PR base check passed/);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+});
+
+test("check-pr-base-main rejects a head that is behind main", () => {
+ const dir = initRepo();
+ try {
+ git(dir, ["checkout", "-b", "feat/stale"]);
+ writeFileSync(join(dir, "b.txt"), "b\n");
+ git(dir, ["add", "b.txt"]);
+ git(dir, ["commit", "-m", "feat"]);
+ git(dir, ["checkout", "main"]);
+ writeFileSync(join(dir, "c.txt"), "c\n");
+ git(dir, ["add", "c.txt"]);
+ git(dir, ["commit", "-m", "main moved"]);
+ git(dir, ["checkout", "feat/stale"]);
+ const result = runCheck(dir, ["--base", "main", "--head", "HEAD"]);
+ assert.equal(result.code, 1);
+ assert.match(result.stderr, /not an ancestor/);
+ assert.match(result.stderr, /behind origin\/main/);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+});
+
+test("check-pr-base-main accepts a head rebased onto latest main", () => {
+ const dir = initRepo();
+ try {
+ git(dir, ["checkout", "-b", "feat/rebased"]);
+ writeFileSync(join(dir, "b.txt"), "b\n");
+ git(dir, ["add", "b.txt"]);
+ git(dir, ["commit", "-m", "feat"]);
+ git(dir, ["checkout", "main"]);
+ writeFileSync(join(dir, "c.txt"), "c\n");
+ git(dir, ["add", "c.txt"]);
+ git(dir, ["commit", "-m", "main moved"]);
+ git(dir, ["checkout", "feat/rebased"]);
+ git(dir, ["rebase", "main"]);
+ const result = runCheck(dir, ["--base", "main", "--head", "HEAD"]);
+ assert.equal(result.code, 0);
+ assert.match(result.stdout, /is an ancestor/);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+});