diff --git a/.crumb b/.crumb index d854ce8..fafbbfb 100644 --- a/.crumb +++ b/.crumb @@ -21,8 +21,8 @@ "generated": { "children": [ { - "digest": "40d778cb00ec60253084751365e4660cd147804daf36fda8ac23d4363f5fa05e", - "last_commit": "0d4c608", + "digest": "bccfc765e0a32a2e4a31cc84d1b5fad1047ac129def178f543c208afb9e7b3a9", + "last_commit": "cfe1c22", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -33,8 +33,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "c34f88bb1ba323a3712efd9fd12cbefa5d0b8d92b3ea66375c5b0b916cfdebd7", - "last_commit": "4478c1a", + "digest": "8bb604d86df1aa3f433600354b036dab1734d921530cf48147886a6ebeeabca9", + "last_commit": "78a7014", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." }, @@ -45,15 +45,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:d5b710c190d0e1e5376811ce19509ec003fffb3a1855ae2d68c0b6b784df8b13", - "compiled_at": "2026-10-10T14:28:20Z", + "children_root": "sha256:770180f54121cb55bfbc154efd4eb2cc2fca53dd7dd8fc43911a64c930f384b3", + "compiled_at": "2026-10-10T15:59:58Z", "compiler": "crumb-compile/1", - "digest": "sha256:ef7a691b54e9143c64bc8b762a5edd42fe7d806290fb828dbc9f015b7342ad8f", + "digest": "sha256:1c66ac3d3cce9f995c4eb047efa11f56f126d9eb4fb48b0735b1ff5b29e1ec6f", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 255, - "generated_at_commit": "0d4c60879e1694595020d13678f804e30cd316d8", - "source_tree": "sha256:3cd181f0e99b5b2a267e2ecdaa6ec356358938d2470c116fe1e4c74a45bba43f" + "files": 260, + "generated_at_commit": "78a7014095431bb50d5e718ab0035a2891e89513", + "source_tree": "sha256:31728b54e286db11569f4cc953d87ede8b3fef5e70827dac790150ff08c99177" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 241cf05..0a230cd 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -38,7 +38,7 @@ "children": [ { "digest": "0f5f051b567320ca492dc91af59fd67460e76e96d0a56efbdf33c093d84e96d6", - "last_commit": "0d4c608", + "last_commit": "b2400fe", "name": "adr", "purpose": "Directory docs/adr. Purpose not yet described by a human." }, @@ -56,7 +56,7 @@ }, { "digest": "476ca8e3cf8257376bcf010981a621b3c129f0724014ce4a443ed0d27efc8f27", - "last_commit": "0d4c608", + "last_commit": "b2400fe", "name": "plans", "purpose": "Directory docs/plans. Purpose not yet described by a human." }, @@ -74,14 +74,14 @@ } ], "children_root": "sha256:7c342e6d7fe441f2ac93916c1a65c61a275d8ebc576d614364cc6c4fa05b03a7", - "compiled_at": "2026-10-10T14:28:20Z", + "compiled_at": "2026-10-10T15:59:58Z", "compiler": "crumb-compile/1", - "digest": "sha256:40d778cb00ec60253084751365e4660cd147804daf36fda8ac23d4363f5fa05e", + "digest": "sha256:bccfc765e0a32a2e4a31cc84d1b5fad1047ac129def178f543c208afb9e7b3a9", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 147, - "generated_at_commit": "0d4c60879e1694595020d13678f804e30cd316d8", - "source_tree": "sha256:c34cc507b049d0284a619d9472e5445f5d99c9d290c71e23813f32bc3bde7dc6" + "files": 148, + "generated_at_commit": "78a7014095431bb50d5e718ab0035a2891e89513", + "source_tree": "sha256:68b8a49fc6c1846bdc726e3b8d56c4c4ff75d4abb5153ed3f351ca3fccad9c0d" }, "owner_lane": "", "provenance": { diff --git a/docs/REPOSITORY_EXECUTION_RULES.md b/docs/REPOSITORY_EXECUTION_RULES.md new file mode 100644 index 0000000..a9736fe --- /dev/null +++ b/docs/REPOSITORY_EXECUTION_RULES.md @@ -0,0 +1,197 @@ +# AIEN Repository Execution Rules + +## Adoption + +Adopted 2026-10-10 from Drake's revised rules for the six-issue campaign (interplane#97 and #98, aien-sovereign-core#397 and #398, aegis-runtime#38 and #39). Tooling: `scripts/pr-gate.sh` and `scripts/rebase-crumb.sh`. + +## Recorded facts (2026-10-10) + +- (a) Branch protection read from GitHub: interplane requires the checks schemas-and-fixtures, rust, python, cross-language, adapter-aien and adapter-odysseus, in strict mode, with no review count. aien-sovereign-core requires the check "Workspace Verification & Invariant Audit", 0 reviews, enforce_admins on. aegis-runtime requires the check "Test & Invariant Verification", 0 reviews, enforce_admins on. Therefore `--admin` is never needed for a green PR. +- (b) interplane has no .crumb files, so the crumb step is skipped there with a notice. aegis-runtime and aien-sovereign-core use crumbs. +- (c) INTERPLANE carries a Python SDK by Drake's 2026-10-04 direction (approved exception). The Motus adapter for interplane#97 lives only under interplane adapters/motus. No Python enters any AIEN-owned repository. + + +These rules apply to Claude Code, all delegated workers, and all repositories involved in this implementation campaign. + +The objective is to ship verified code without corrupting shared branches, bypassing security requirements, or creating unnecessary integration conflicts. + +## 1. Source of Truth + +GitHub main is authoritative. Inspect current code, tests, active PRs, and repository instructions before editing. + +Never implement an issue solely from its description. Check whether the functionality already exists or has been superseded. + +Use separate branches and worktrees for concurrent development. + +One worker owns each writable worktree. Never allow two workers to modify the same worktree concurrently. + +## 2. Crumb Compilation + +Every applicable PR must have `.crumb` compilation and verification completed after its final source change. + +Required commands: `crumb compile .` then `crumb verify .` + +Commit generated `.crumb` changes separately as the final commit. + +Do not modify source files after that commit without rerunning compilation and verification. + +If a repository does not support these commands, report the discrepancy and follow its explicit repository policy. Never silently skip a required check. + +## 3. Rebase and Crumb Conflicts + +Every sovereign-core merge to main can invalidate `.crumb` files in other open PRs. + +After each sovereign-core merge, evaluate remaining PRs for drift. + +Approved recovery script: `scripts/rebase-crumb.sh ` (this repository). + +Before running it: + +- Confirm the worktree has no uncommitted changes. +- Confirm the intended branch and remote. +- Inspect the script before first use. +- Preserve the expected remote head for force-with-lease protection. +- Ensure no other worker is writing to that branch. + +The script takes main's `.crumb`, recompiles crumbs as the last commit, and pushes with lease. + +If it reports CODE CONFLICT, stop automated rebasing for that branch. Resolve the source conflict before recompiling. + +Never discard source changes to manufacture a clean rebase. + +Do not rewrite another worker's branch without coordination. + +## 4. Definition of CLEAN + +A PR may merge only when all of the following hold: + +- Working tree is clean. +- Changes are based on the intended current main. +- No unresolved merge conflicts. +- Required compilation and crumb verification pass. +- Required tests and CI checks pass. +- Required approvals are present and valid. +- GitHub reports a mergeable state. +- No outstanding blocking security findings. +- The final diff contains no unintended changes. +- The implementation satisfies the linked issue's acceptance criteria. + +Do not equate GitHub's CLEAN merge-state label with complete verification. + +## 5. Merge Procedure + +Serialize merges into each repository's main branch. + +Default command: `gh pr merge N -R aien-dev/ --squash --delete-branch` + +Only use `--admin` when explicitly authorized for that specific PR and when the required project governance permits it. + +Never use administrative privileges to hide failing tests, missing approvals, security findings, or unresolved conflicts. + +After every merge, refresh main and revalidate dependent or competing PRs. + +## 6. GitHub PR Editing + +The normal `gh pr edit` workflow is currently unreliable in this environment. + +Use the API fallback: `gh api -X PATCH repos/aien-dev//pulls/N -F body=@file` + +Verify the returned response and fetch the PR afterward to confirm the update. + +Do not assume an API operation succeeded merely because the command exited. + +## 7. Language and Runtime Constraints + +No Python or systemd may be introduced as part of this campaign. + +Use Rust for AIEN-owned runtime, orchestration, adapters, and execution control. + +Motus may run externally using its own dependencies, but the AIEN integration must communicate over an explicitly verified protocol boundary. + +If a task requires Python inside an AIEN-owned repository, treat it as an architectural conflict. Redesign it or report the blocker before proceeding. + +Existing Python files do not authorize adding more Python. Flag any conflict with the strict repository policy rather than silently ignoring it. + +## 8. Command Hooks + +A local Bash hook rejects certain command text containing accelerator or kernel-module related terms. + +Do not embed source-file contents containing these terms into Bash commands or heredocs. Use the approved Write tool for file creation and editing. + +Do not attempt to bypass restrictions on executing prohibited operations. + +If an approved development command is incorrectly blocked, report the exact failure and use an authorized alternative. + +## 9. Commits and Attribution + +Use small, logically isolated commits. + +Required commit trailers, when attribution accurately reflects the contributors: + +`Co-authored-by: Drake Stapleton ` +`Co-Authored-By: Claude Fable 5.1 ` + +Do not invent contributors or misrepresent authorship. + +Required PR footer: 🤖 Generated with [Claude Code](https://claude.com/claude-code) + +Every PR must reference its issue, describe its implementation, identify verification performed, and disclose limitations. + +## 10. Worker Models and Verification + +Use Sonnet for delegated implementation workers unless instructed otherwise. + +Workers may inspect, implement, test, and propose solutions within their assigned ownership boundaries. + +A worker's claim of completion is not verification. + +The coordinating agent must independently inspect every worker's changes and verify relevant tests, security invariants, and actual GitHub state. + +Do not accept unsupported claims about test passes, hardware performance, successful merges, or completed integrations. + +Use an independent reviewer for security-sensitive changes. + +## 11. Failure Handling + +Do not repeatedly retry the same failing operation without identifying the cause. + +On failure: + +1. Capture the exact failure and affected commit. +2. Determine whether it is source-related, environmental, or caused by concurrent changes. +3. Apply the smallest justified correction. +4. Rerun the affected verification. +5. Escalate or isolate persistent blockers without destroying valid work. + +Never force-push another worker's changes away. + +Never turn off tests, security checks, or authorization rules merely to make a PR merge. + +## 12. Communication + +Use plain English when reporting to Drake. Do not use em dashes or en dashes. + +Provide concise updates that identify completed work, active work, blockers, and next actions. + +Do not confuse attempted operations with completed operations. + +## 13. Final Delivery + +Before declaring the campaign complete: + +- Verify every merged PR against its issue. +- Check relevant cross-repository interfaces. +- Run integration tests. +- Confirm no unintended dependency or architecture changes. +- Report remaining failed, skipped, or unavailable verification. +- Confirm final GitHub issue and PR status. + +A task is complete only when the implementation, verification, and required integration are finished. + +## 14. Worker Instructions + +Worker briefs live with each campaign; this document wins when they conflict. + +Stable rules and validated scripts live in aien-architecture (`docs/REPOSITORY_EXECUTION_RULES.md`, `scripts/pr-gate.sh`, `scripts/rebase-crumb.sh`). + +**Operating principle: Ship correct, verified code. Preserve other workers' changes. Protect main. Never trade correctness for apparent progress.** diff --git a/scripts/.crumb b/scripts/.crumb index 2ee4a9c..082d16d 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-06T20:50:34Z", + "compiled_at": "2026-10-10T15:59:58Z", "compiler": "crumb-compile/1", - "digest": "sha256:c34f88bb1ba323a3712efd9fd12cbefa5d0b8d92b3ea66375c5b0b916cfdebd7", + "digest": "sha256:8bb604d86df1aa3f433600354b036dab1734d921530cf48147886a6ebeeabca9", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 7, - "generated_at_commit": "abda85392f829daeefd57b126eac299df8397325", - "source_tree": "sha256:ad70f421e2dca07f97cdf023e1b747fd0c3871f861f7ed206556222e4c1183b5" + "files": 11, + "generated_at_commit": "78a7014095431bb50d5e718ab0035a2891e89513", + "source_tree": "sha256:fc28f00940d1199ab2b6a930b0a59905bf4e9729b636d1ec11c1e333ceb858a2" }, "owner_lane": "", "provenance": { diff --git a/scripts/pr-gate.sh b/scripts/pr-gate.sh new file mode 100755 index 0000000..db43afe --- /dev/null +++ b/scripts/pr-gate.sh @@ -0,0 +1,144 @@ +#!/usr/bin/env bash +# PR merge gate for aien-dev repositories. Reads GitHub state at the moment of the call, checks +# docs/REPOSITORY_EXECUTION_RULES.md rules 2, 4 and 5, then (unless --dry-run) squash-merges. +# It does not replace a human read of the diff, and it never rebases anything by itself. +# +# Usage: pr-gate.sh [--worktree DIR] [--dry-run] [--admin-authorized-by ""] +# Env: GH (default gh), GIT (default git), CRUMB (default crumb), SLEEP_SECS (default 10, UNKNOWN retry). +# Output: one line per step, "GATE : PASS|FAIL|SKIP ". Exit 1 at the first FAIL. +set -u +GH=${GH:-gh} +GIT=${GIT:-git} +CRUMB=${CRUMB:-crumb} +SLEEP_SECS=${SLEEP_SECS:-10} + +usage() { echo 'usage: pr-gate.sh [--worktree DIR] [--dry-run] [--admin-authorized-by ""]' >&2; exit 2; } +[ $# -ge 2 ] || usage +REPO=$1; N=$2; shift 2 +WT=""; DRY=0; ADMIN="" +while [ $# -gt 0 ]; do + case "$1" in + --worktree) [ $# -ge 2 ] || usage; WT=$2; shift 2 ;; + --dry-run) DRY=1; shift ;; + --admin-authorized-by) [ $# -ge 2 ] || usage; ADMIN=$2; shift 2 ;; + *) usage ;; + esac +done +case "$N" in ''|*[!0-9]*) usage ;; esac +FULL="aien-dev/$REPO" + +pass() { echo "GATE $1: PASS $2"; } +skip() { echo "GATE $1: SKIP $2"; } +fail() { echo "GATE $1: FAIL $2"; exit 1; } + +FIELDS=number,state,isDraft,baseRefName,headRefName,headRefOid,mergeable,mergeStateStatus,reviewDecision,statusCheckRollup +view() { $GH pr view "$N" -R "$FULL" --json "$FIELDS" 2>&1; } + +# 1. pr +PR=$(view) || fail pr "could not read PR: $(echo "$PR" | head -1)" +echo "$PR" | jq -e . >/dev/null 2>&1 || fail pr "gh did not return JSON" +f() { echo "$PR" | jq -r "$1"; } +STATE=$(f .state); DRAFT=$(f .isDraft); BASE=$(f .baseRefName); HEADREF=$(f .headRefName); HEADOID=$(f .headRefOid) +[ "$STATE" = OPEN ] || fail pr "state is $STATE, not OPEN" +[ "$DRAFT" = false ] || fail pr "PR is a draft" +[ "$BASE" = main ] || fail pr "base is $BASE, not main" +pass pr "#$N $HEADREF at ${HEADOID:0:8}, open, not draft, base main" + +# 2. local +if [ -z "$WT" ]; then + skip local "no worktree given" +else + G="$GIT -C $WT" + $G rev-parse --is-inside-work-tree >/dev/null 2>&1 || fail local "$WT is not a git checkout" + $G fetch -q origin 2>/dev/null || fail local "fetch failed in $WT" + [ -z "$($G status --porcelain)" ] || fail local "worktree is dirty" + CUR=$($G rev-parse --abbrev-ref HEAD) + [ "$CUR" = "$HEADREF" ] || fail local "worktree is on $CUR, PR branch is $HEADREF" + [ "$($G rev-parse HEAD)" = "$HEADOID" ] || fail local "local HEAD $($G rev-parse --short HEAD) differs from GitHub head ${HEADOID:0:7}" + [ "$($G merge-base origin/main HEAD)" = "$($G rev-parse origin/main)" ] || fail local "stale base, branch is not on current origin/main; run scripts/rebase-crumb.sh" + pass local "clean, on $CUR, HEAD matches GitHub, based on current origin/main" +fi + +# 3. crumb +if [ -z "$WT" ]; then + skip crumb "no worktree given" +elif ! $G ls-files '*.crumb' '**/.crumb' | grep -q .; then + skip crumb "no crumbs in repo (rule 2 notice)" +else + VOUT=$(cd "$WT" && $CRUMB verify . 2>&1); VRC=$? + VLAST=$(echo "$VOUT" | tail -1) + { [ "$VRC" -eq 0 ] && echo "$VLAST" | grep -q 'OK' && ! echo "$VLAST" | grep -q 'FAIL'; } || fail crumb "crumb verify did not print OK: $VLAST" + if $G diff --name-only origin/main...HEAD | grep -q '\.crumb$'; then + NONCRUMB=$($G diff-tree --no-commit-id --name-only -r HEAD | grep -v '\.crumb$' || true) + [ -z "$NONCRUMB" ] || fail crumb "crumbs changed but the HEAD commit also changes source ($(echo "$NONCRUMB" | head -3 | tr '\n' ' ')); the crumb commit must be separate and last (rule 2)" + fi + pass crumb "verify OK, crumb commit is separate and last" +fi + +# 4. base +TRIES=0 +while :; do + MSS=$(f .mergeStateStatus); MRG=$(f .mergeable) + [ "$MSS" = UNKNOWN ] || break + TRIES=$((TRIES+1)) + [ "$TRIES" -le 5 ] || fail base "GitHub has not computed mergeability" + sleep "$SLEEP_SECS" + PR=$(view) || fail base "could not re-read PR" +done +[ "$MRG" != CONFLICTING ] || fail base "mergeable is CONFLICTING (merge conflicts)" +case "$MSS" in + CLEAN) pass base "mergeStateStatus CLEAN" ;; + BEHIND) fail base "stale, rebase first" ;; + DIRTY) fail base "merge conflicts" ;; + BLOCKED) fail base "required checks or reviews missing" ;; + UNSTABLE) fail base "a check is failing" ;; + HAS_HOOKS) + if [ "$DRY" = 1 ]; then pass base "HAS_HOOKS (accepted in dry run only)" + else fail base "merge queue or hooks present, check by hand"; fi ;; + *) fail base "unexpected mergeStateStatus $MSS" ;; +esac + +# 5. checks +RD=$(f '.reviewDecision // ""') +case "$RD" in + CHANGES_REQUESTED) fail checks "reviewDecision is CHANGES_REQUESTED" ;; + REVIEW_REQUIRED) fail checks "reviewDecision is REVIEW_REQUIRED" ;; +esac +PROT=$($GH api "repos/$FULL/branches/main/protection/required_status_checks" --jq .contexts 2>&1); PRC=$? +if [ "$PRC" -ne 0 ]; then + if echo "$PROT" | grep -qE '404|Not Found'; then + echo "WARNING: $FULL main has no branch protection; no required contexts to verify" >&2 + skip checks "no branch protection" + else + fail checks "could not read required checks: $(echo "$PROT" | head -1)" + fi +else + echo "$PROT" | jq -e 'type=="array"' >/dev/null 2>&1 || fail checks "required checks response is not a list" + MISSING=$(echo "$PR" | jq -r --argjson req "$PROT" ' + (.statusCheckRollup // []) as $r + | $req[] as $c + | select(([$r[] | select((.name == $c or .context == $c) and (.conclusion == "SUCCESS" or .state == "SUCCESS"))] | length) == 0) + | $c') + [ -z "$MISSING" ] || fail checks "required check not green: $(echo "$MISSING" | tr '\n' ',' | sed 's/,$//')" + pass checks "$(echo "$PROT" | jq 'length') required contexts green, reviewDecision ${RD:-none}" +fi + +# 6. merge +if [ "$DRY" = 1 ]; then + skip merge "dry run" +else + ARGS=(pr merge "$N" -R "$FULL" --squash --delete-branch) + NOTE="squash" + if [ -n "$ADMIN" ]; then ARGS+=(--admin); NOTE="squash with --admin, authorized by: $ADMIN"; fi + MOUT=$($GH "${ARGS[@]}" 2>&1) || fail merge "gh pr merge failed: $(echo "$MOUT" | head -1)" + POST=$($GH pr view "$N" -R "$FULL" --json state,mergedAt,mergeCommit 2>&1) || fail merge "merge ran but state could not be re-read" + [ "$(echo "$POST" | jq -r .state)" = MERGED ] || fail merge "after merge, state is $(echo "$POST" | jq -r .state), not MERGED" + pass merge "$NOTE; merge commit $(echo "$POST" | jq -r '.mergeCommit.oid // "unknown"')" +fi + +# 7. queue +LIST=$($GH pr list -R "$FULL" --json number,headRefName,mergeStateStatus 2>&1) || fail queue "could not list open PRs: $(echo "$LIST" | head -1)" +echo "$LIST" | jq -r --argjson me "$N" '.[] | select(.number != $me) + | "QUEUE #\(.number) \(.headRefName) \(.mergeStateStatus)" + (if (.mergeStateStatus == "BEHIND" or .mergeStateStatus == "DIRTY") then " needs scripts/rebase-crumb.sh" else "" end)' +pass queue "$(echo "$LIST" | jq --argjson me "$N" '[.[] | select(.number != $me)] | length') other open PRs listed, none rebased" +exit 0 diff --git a/scripts/rebase-crumb.sh b/scripts/rebase-crumb.sh new file mode 100755 index 0000000..4918785 --- /dev/null +++ b/scripts/rebase-crumb.sh @@ -0,0 +1,93 @@ +#!/usr/bin/env bash +# Rebase a PR branch onto origin/main, resolve conflicts that are only in .crumb files by taking +# main's copy, recompile crumbs as a fresh last commit, and push with a lease on the old head. +# +# Usage: rebase-crumb.sh +# Env: GIT (default git), CRUMB (default crumb), so tests can stub the tools. +# Exit: 0 done, 2 precondition refused, 3 CODE CONFLICT (rebase aborted), 4 crumb or rebase failure +# (nothing pushed). +set -u +GIT=${GIT:-git} +CRUMB=${CRUMB:-crumb} +TRAILERS='Co-authored-by: Drake Stapleton +Co-Authored-By: Claude Fable 5.1 ' + +refuse() { echo "REFUSED: $1" >&2; exit 2; } + +[ $# -eq 2 ] || { echo "usage: rebase-crumb.sh " >&2; exit 2; } +W=$1; B=$2 +cd "$W" 2>/dev/null || refuse "worktree $W does not exist" +$GIT rev-parse --is-inside-work-tree >/dev/null 2>&1 || refuse "$W is not a git checkout" +[ -z "$($GIT status --porcelain)" ] || refuse "working tree is not clean" +[ "$B" != "main" ] || refuse "branch is main" +URL=$($GIT config --get remote.origin.url 2>/dev/null) || refuse "no origin remote" +case "$URL" in + *github.com/aien-dev/*|*git@github.com:aien-dev/*) ;; + *) refuse "origin URL is not under aien-dev: $URL" ;; +esac +$GIT fetch -q origin || refuse "fetch failed" +$GIT rev-parse -q --verify "refs/remotes/origin/$B" >/dev/null || refuse "origin/$B does not exist" + +GD=$($GIT rev-parse --git-dir) +LOCK="$GD/rebase-crumb.lock" +if [ -e "$LOCK" ] && [ -z "$(find "$LOCK" -mmin +120 2>/dev/null)" ]; then + refuse "lock $LOCK is fresh (another run, or younger than 2 hours)" +fi +echo $$ > "$LOCK" || refuse "cannot create lock" +trap 'rm -f "$LOCK"' EXIT + +LEASE=$($GIT rev-parse "origin/$B") +$GIT checkout -q -B "$B" "origin/$B" || { echo "checkout failed" >&2; exit 4; } + +rebasing() { [ -d "$($GIT rev-parse --git-path rebase-merge)" ] || [ -d "$($GIT rev-parse --git-path rebase-apply)" ]; } + +$GIT rebase -q origin/main >/dev/null 2>&1 +while rebasing; do + C=$($GIT diff --name-only --diff-filter=U) + if [ -z "$C" ]; then + GIT_EDITOR=true $GIT rebase --continue >/dev/null 2>&1 && continue + # A resolved commit that became empty cannot be continued; skip it. + if [ -z "$($GIT diff --cached --name-only)" ]; then + $GIT rebase --skip >/dev/null 2>&1 && continue + fi + echo "rebase continue failed" >&2; $GIT status --short | head; $GIT rebase --abort; exit 4 + fi + NONCRUMB=$(echo "$C" | grep -v '\.crumb$' || true) + if [ -n "$NONCRUMB" ]; then + echo "CODE CONFLICT: $(echo "$NONCRUMB" | tr '\n' ' ')" + $GIT rebase --abort + exit 3 + fi + # During a rebase "ours" is the branch being rebased onto, which is main. + echo "$C" | while IFS= read -r f; do + if $GIT cat-file -e "origin/main:$f" 2>/dev/null; then + $GIT checkout -q --ours -- "$f" && $GIT add -- "$f" + else + $GIT rm -q -f -- "$f" + fi + done + GIT_EDITOR=true $GIT rebase --continue >/dev/null 2>&1 || { + if [ -z "$($GIT diff --cached --name-only)" ] && $GIT rebase --skip >/dev/null 2>&1; then continue; fi + echo "rebase continue failed" >&2; $GIT status --short | head; $GIT rebase --abort; exit 4 + } +done + +if $GIT ls-files '*.crumb' '**/.crumb' | grep -q .; then + $CRUMB compile . >/dev/null 2>&1 + VOUT=$($CRUMB verify . 2>&1); VRC=$? + if [ "$VRC" -ne 0 ] || ! echo "$VOUT" | tail -1 | grep -q 'OK' || echo "$VOUT" | tail -1 | grep -q 'FAIL'; then + echo "crumb verify did not print OK; nothing pushed: $(echo "$VOUT" | tail -1)" >&2 + exit 4 + fi + echo "crumb: $(echo "$VOUT" | tail -1)" + if [ -n "$($GIT status --porcelain)" ]; then + $GIT add -A && $GIT commit -q -m "crumb compile (after rebase onto main) + +$TRAILERS" || { echo "crumb commit failed" >&2; exit 4; } + fi +else + echo "crumb: no crumbs in repo, step skipped (rule 2 notice)" +fi + +$GIT push -q --force-with-lease="refs/heads/$B:$LEASE" origin "$B" || { echo "push failed (lease or network)" >&2; exit 4; } +echo "rebased $B onto $($GIT rev-parse --short origin/main): $($GIT log --oneline -1 | cut -c1-70)" diff --git a/scripts/test_pr_gate.sh b/scripts/test_pr_gate.sh new file mode 100755 index 0000000..9c7bf44 --- /dev/null +++ b/scripts/test_pr_gate.sh @@ -0,0 +1,168 @@ +#!/usr/bin/env bash +# Offline test for pr-gate.sh. A fake `gh` on PATH answers from fixture JSON chosen by $CASE and +# records any merge command line. Local and crumb steps use real temporary git repos (bare origin +# plus worktree) and a fake `crumb`. No network, no auth. +set -u +here="$(cd "$(dirname "$0")" && pwd)" +gate="$here/pr-gate.sh" +tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT +export GIT_AUTHOR_NAME=T GIT_AUTHOR_EMAIL=t@example.invalid GIT_COMMITTER_NAME=T GIT_COMMITTER_EMAIL=t@example.invalid +export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null +export SLEEP_SECS=0 +FX="$tmp/fx"; mkdir -p "$FX" "$tmp/bin"; export FX + +cat > "$tmp/bin/gh" <<'S' +#!/usr/bin/env bash +d="$FX/$CASE" +case "$1 $2" in + "pr view") + if echo "$*" | grep -q mergeCommit && [ -e "$d/merged-flag" ]; then cat "$d/merged.json" + else + # UNKNOWN case: the first N answers can be counted + cat "$d/pr.json" + fi ;; + "pr list") cat "$d/list.json" ;; + "pr merge") echo "$*" > "$d/merge.cmd"; touch "$d/merged-flag" ;; + api\ repos/*) + if [ -e "$d/prot404" ]; then echo "gh: Not Found (HTTP 404)" >&2; exit 1; fi + cat "$d/prot.json" ;; + *) echo "fake gh: unhandled: $*" >&2; exit 2 ;; +esac +S +cat > "$tmp/bin/crumb" <<'S' +#!/usr/bin/env bash +[ "$1" = verify ] || exit 0 +if [ "${CRUMB_FAIL:-0}" = 1 ]; then echo "crumb verify: FAIL: stale"; exit 1; fi +echo "crumb verify: OK" +S +chmod +x "$tmp/bin/gh" "$tmp/bin/crumb" +export PATH="$tmp/bin:$PATH" + +pass=0; bad=0 +ok() { pass=$((pass+1)); } +no() { bad=$((bad+1)); echo "TEST FAIL: $1"; } +run() { OUT=$("$gate" "$@" 2>&1); rc=$?; } +expect() { # expect