Skip to content

fix(bin): require per-PR captain approval before merging - #65

Merged
EvanAgee merged 4 commits into
mainfrom
fm/fm-merge-guard-captain-merge
Aug 29, 2026
Merged

EvanAgee merged 4 commits into
mainfrom
fm/fm-merge-guard-captain-merge

Conversation

@EvanAgee

@EvanAgee EvanAgee commented Aug 29, 2026 •

Copy link
Copy Markdown
Owner

Intent

Make bin/fm-pr-merge.sh refuse to merge a pull request for a project whose registered delivery posture includes captain-merge unless the caller supplies explicit approval for that exact pull request.
Resolve the project from project= in state/.meta, map the recorded project path to its registry name, and read data/projects.md only through bin/fm-project-mode.sh. Extend that parser instead of adding a second parser. Registry bracket lists are free-form. Match the captain-merge token exactly, including when a parenthetical follows it.
Refuse before any review-thread query or merge call. The stderr refusal must name the project, full matching registry line, and canonical pull request URL.
Add --captain-approved . It bypasses only the captain-merge refusal when its value exactly matches the canonical URL produced by bin/fm-pr-lib.sh. Any mismatch refuses. Log a successful bypass on stderr. Never infer approval from an environment variable, config file, or default.
A missing registry or a project absent from the registry must not trigger this guard. A registered project without the exact token must merge normally.
Update the script header and --help. Add one line in AGENTS.md section 7 stating that captain-merge requires --captain-approved and firstmate may pass it only under a current explicit captain instruction naming that pull request.
Extend tests/fm-pr-merge.test.sh through the executable. Cover no-flag refusal, mismatched approval refusal, matching approval with stderr log, a project without the token, and a project absent from the registry. Do not assert on source bytes.
bin/fm-lint.sh and bin/fm-doc-audience-check.sh must pass.
The pull request body must include this real scratch-registry proof from 2026-08-29:
command_exit=1
error: captain approval required; refusing to merge captain-merge project "pto"
error: registry: - pto [no-mistakes, factory-enabled, captain-merge (HARD: firstmate never merges without per-PR approval), evidence-required] - scratch registry proof
error: PR: https://github.com/EvanAgee/faapto/pull/163
error: pass --captain-approved https://github.com/EvanAgee/faapto/pull/163 only after a current explicit captain instruction naming this PR

What Changed

  • Require an exact --captain-approved <pr-url> match before merging a registered captain-merge project. The guard runs before review-thread queries and logs either the refusal details or a successful bypass.

    Scratch-registry proof from 2026-08-29:

    command_exit=1
    error: captain approval required; refusing to merge captain-merge project "pto"
    error: registry: - pto [no-mistakes, factory-enabled, captain-merge (HARD: firstmate never merges without per-PR approval), evidence-required] - scratch registry proof
    error: PR: https://github.com/EvanAgee/faapto/pull/163
    error: pass --captain-approved https://github.com/EvanAgee/faapto/pull/163 only after a current explicit captain instruction naming this PR
    
  • Extend fm-project-mode.sh to find the exact captain-merge token in free-form registry annotations and return the full matching registry line. Missing registries and unregistered projects remain unguarded.

  • Add executable coverage for approval refusal and bypass paths, registry parsing, project identity validation, and unguarded projects. Update the operator contract and script documentation with the new approval requirement.

Risk Assessment

✅ Low: The merge guard is well-bounded, preserves the required refusal ordering, and closes the previously identified bypass paths.

Testing

Inspected the target change, verified help output, ran the focused executable-level merge tests, and reproduced the requested scratch-registry refusal through the real CLI. All checks passed. This is a CLI-only change, so visual evidence was not applicable; the reviewer-visible command transcript is attached.

Evidence: Captain merge refusal transcript

Source: Captain merge refusal transcript

command_exit=1 error: captain approval required; refusing to merge captain-merge project "pto" error: registry: - pto [no-mistakes, factory-enabled, captain-merge (HARD: firstmate never merges without per-PR approval), evidence-required] - scratch registry proof error: PR: https://github.com/EvanAgee/faapto/pull/163&#10;error: pass --captain-approved https://github.com/EvanAgee/faapto/pull/163 only after a current explicit captain instruction naming this PR stdout_bytes=0

command_exit=1
error: captain approval required; refusing to merge captain-merge project "pto"
error: registry: - pto [no-mistakes, factory-enabled, captain-merge (HARD: firstmate never merges without per-PR approval), evidence-required] - scratch registry proof
error: PR: https://github.com/EvanAgee/faapto/pull/163
error: pass --captain-approved https://github.com/EvanAgee/faapto/pull/163 only after a current explicit captain instruction naming this PR
stdout_bytes=0

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 4 issues found → auto-fixed (2) ✅
  • 🚨 bin/fm-project-mode.sh:72 - The intent says registry bracket lists are free-form and captain-merge must match exactly. This regex requires whitespace before the token and whitespace or ( after it, so [no-mistakes,captain-merge,evidence-required] is missed and the merge proceeds without approval. Accept punctuation-delimited tokens while still rejecting captain-merge-later, and cover the comma-adjacent form through the executable.
  • 🚨 bin/fm-pr-merge.sh:105 - The intent requires a refusal to name the project, full matching registry line, and canonical PR URL. A mismatched --captain-approved value exits here before project lookup and prints only the canonical URL. For a protected project, defer this refusal until registry resolution so it emits the required details, while retaining a generic mismatch refusal for unguarded projects.
  • 🚨 bin/fm-pr-merge.sh:194 - Every nonzero parser result is treated as no matching policy. If an existing registry is unreadable or awk fails, the script clears the result and continues to the review query and merge. Preserve the parser status, ignore only the documented missing-registry, absent-project, and absent-token result, and refuse on operational errors.
  • 🚨 bin/fm-project-mode.sh:69 - The intent guards only a registered delivery posture containing the token, but this path scans the first bracket pair anywhere on the project line. A valid legacy entry such as - app - note [captain-merge] is falsely guarded even though its posture has no token. Require the posture annotation at the registry format's third field before scanning its brackets.

🔧 Fix: Fix captain merge guard parsing and diagnostics
3 issues (2 errors, 1 warning) still open:

  • 🚨 bin/fm-pr-merge.sh:184 - The required behavior says, “Resolve the project from project= in state/<task-id>.meta.” This read hides failures and treats a missing, empty, or ambiguous project value as unprotected. A partial metadata write can therefore skip policy lookup and reach the review query and merge without approval. Require exactly one readable, nonempty project value before continuing.
  • 🚨 bin/fm-pr-merge.sh:94 - The requirement says, “Any mismatch refuses.” Each repeated --captain-approved flag overwrites the previous value, so a wrong URL followed by the canonical URL permits the merge. Refuse duplicate approval flags or validate every supplied value.
  • ⚠️ bin/fm-project-mode.sh:35 - The new --captain-merge mode returns a full registry line, but the header still promises only two output words and its consumer list omits fm-pr-merge.sh. Update both statements to satisfy CONTRIBUTING.md’s accurate-header rule.

🔧 Fix: Fix captain approval and project identity validation
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-pr-merge.test.sh
  • ./bin/fm-pr-merge.sh --help
  • Ran bin/fm-pr-merge.sh task-proof https://github.com/EvanAgee/faapto/pull/163 against the required scratch registry and confirmed exit 1, the complete refusal message, canonical URL, and zero stdout.
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Summary by CodeRabbit

  • New Features

    • Added captain approval requirements for projects using the captain-merge policy.
    • Added support for approving merges with a matching pull request URL.
    • Added project policy detection for captain-merge projects.
  • Bug Fixes

    • Rejects missing, duplicate, mismatched, or invalid approval information.
    • Prevents merges when project policy data cannot be verified.
  • Documentation

    • Updated script and architecture documentation to describe approval and merge policy checks.

@coderabbitai

coderabbitai Bot commented Aug 29, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2a07635d-91f7-4951-b4a0-e43ca6efa7b1

📥 Commits

Reviewing files that changed from the base of the PR and between 806d481 and 9d5ee51.

📒 Files selected for processing (6)
  • AGENTS.md
  • bin/fm-pr-merge.sh
  • bin/fm-project-mode.sh
  • docs/architecture.md
  • docs/scripts.md
  • tests/fm-pr-merge.test.sh

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

Changes

The merge command now supports --captain-approved <pr-url>. Projects marked with the exact captain-merge token require a matching approval URL. Project policy lookup, merge documentation, and automated tests were updated.

Captain-approved merge flow

Layer / File(s) Summary
Project policy lookup
bin/fm-project-mode.sh
The script recognizes --captain-merge, validates registry and project presence, and returns the full registry line only when the exact policy token exists.
Merge argument and policy gate
bin/fm-pr-merge.sh, AGENTS.md, docs/architecture.md, docs/scripts.md
The merge command parses approval options, validates project metadata and policy status, and requires an approval URL that matches the canonical PR URL.
Captain-merge test coverage
tests/fm-pr-merge.test.sh
Tests cover approval validation, policy detection, parser failures, project identity, duplicate flags, API-call ordering, and merge refusal behavior.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: 🟡 Moderate · up to 9d5ee

The merge guard can select approval policy from task metadata while targeting a repository supplied separately in the pull request URL; if those identities do not match, a protected pull request could be merged without the required captain approval. Duplicate project entries could also select a less restrictive policy. This should be fixed or explicitly accepted before merging.

Sequence Diagram(s)

sequenceDiagram
  participant fm-pr-merge.sh
  participant fm-project-mode.sh
  participant fm-pr-check.sh
  participant gh-axi
  fm-pr-merge.sh->>fm-project-mode.sh: Query registered captain-merge policy
  fm-project-mode.sh-->>fm-pr-merge.sh: Return registry line or policy status
  fm-pr-merge.sh->>fm-pr-merge.sh: Validate captain approval URL
  fm-pr-merge.sh->>fm-pr-check.sh: Record PR metadata
  fm-pr-merge.sh->>gh-axi: Invoke pr merge
Loading

Suggested reviewers: kunchenguid, karotkriss

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 3 files. (3 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: requiring per-PR captain approval before merging.
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.
Full details: Docstring Coverage

Explanation

Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 3 files. (3 skipped: 3 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fm/fm-merge-guard-captain-merge

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.

@EvanAgee EvanAgee added the agent-pr-watched An agent merge watch is armed on this PR label Aug 29, 2026
@EvanAgee
EvanAgee merged commit f6cfa0b into main Aug 29, 2026
20 checks passed
EvanAgee added a commit that referenced this pull request Sep 22, 2026
Refs #1
Refs #42
Refs #60
Refs #65
Refs #73
Refs #74
Refs #107
Refs #109
Refs #117

## What I walked

In a throwaway `.scratch-c4-walk/home` and local Git repository:

```
$ FM_HOME=.scratch-c4-walk/home bin/fm-merge-local.sh walk-clean
merged fm/walk-clean into local main (aa84759 -> 1b9c678) in /Users/evanagee/.treehouse/firstmate-df5ff1/9/firstmate/.scratch-c4-walk/project
$ git -C .scratch-c4-walk/project log -1 --format=%s main
clean local landing
```

```
$ FM_HOME=.scratch-c4-walk/home FM_GH_BIN=.scratch-c4-walk/fake-gh bin/fm-flow.sh walk status
matched rulesets: main-protection=1 (rules: pull_request,required_status_checks) review=2 (rules: pull_request)
registry posture: [no-mistakes-prod-only +yolo]
main-protection ruleset 1 enforcement: active
main-protection ruleset gates: pull_request + required_status_checks present
review ruleset 2 enforcement: active
stored previous posture: <none>
verdict: full
```

```
$ FM_HOME=.scratch-c4-walk/home bin/fm-merge-local.sh walk-outage fm/walk-outage
error: task walk-outage is mode=no-mistakes; a PR-bound task lands locally only while GitHub is unreachable (state/.github-down present)
merge its PR with bin/fm-pr-merge.sh after approval
exit=1
```

The flow status used a local fake that returned read-only ruleset data. No GitHub request, PR, or push was made.

## Port record

The root already had guarded `fm-pr-merge.sh` and `fm-merge-local.sh`, canonical v2 PR poll registration and repair, GitLab merge verification, task control locks, leases, captain holds, and persisted merge authority. During the final rebase, the root also gained the outage landing skill unchanged.

I kept those root implementations. The port adds auto-arm, reviewer chase, review-thread gating, issue claims and post-merge closing, GitHub outage landing and return sync, and the project flow switch around them.

I dropped the fork's obsolete `captain-merge` token, `fm-pr-check-migrate.sh`, and C3 delivery timing, backfill, and token ledger changes. The root's captain-hold authority and repairable poll publication cover those seams.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-pr-watched An agent merge watch is armed on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant