diff --git a/.github/scripts/README.md b/.github/scripts/README.md index 143edca..bce2a80 100644 --- a/.github/scripts/README.md +++ b/.github/scripts/README.md @@ -141,13 +141,14 @@ `../workflows/README.md`'s "Issue moderation" section and `https://github.com/craftainer/template-base/blob/main/docs/adrs/0001-harden-the-issue-moderation-workflows.md`. - `moderate_lib.sh` — sourced by the moderation scripts below: `is_sha`, - `is_ancestor` (compare API), `check_result_tree` (the commits a patch + `is_ancestor` (compare API; a failed call is retried up to three times, + a definite "not ahead" answer is not), `check_result_tree` (the commits a patch produced against the path allow-list and no symlink or submodule), `agent_file` (a result file must be a regular file really under `.moderation-out/`) and the size caps. - `moderate_check_paths.sh` — the path allow-list: reads NUL-separated paths on stdin and fails on any under `.github/`, `.devcontainer/`, - `.claude/`, a `Makefile`/`*.mk`, `Dockerfile*`, `compose*.y*ml`/`docker-compose*.y*ml`, + `.devcontainer.json`, `.claude/`, a `Makefile`/`*.mk`, `Dockerfile*`, `compose*.y*ml`/`docker-compose*.y*ml`, `.pre-commit-config.yaml`, `scripts/release*` and the other files that decide what runs with credentials. - `moderate_prepare.sh` / `moderate_verify_branch.sh` — run on the runner @@ -157,7 +158,9 @@ - `moderate_package_result.sh` — the Claude job's last step: reads the agent's commits through a fresh bare repository and its note or triage file only if a regular file under `.moderation-out/`, and writes - `meta.json` and `patch.diff` for the `apply` job. + `meta.json` and `patch.diff` for the `apply` job. A triage patch over the + size cap becomes a `failed` result carrying that reason, which + `moderate_apply_triage.sh` includes in its "no usable result" note. - `moderate_apply_result.sh` / `moderate_apply_triage.sh` — the `apply` jobs' scripts (fix/build, and triage): validate the artifact, then push, open the PR or comment, and label. The only scripts that write; they diff --git a/.github/scripts/moderate_apply_triage.sh b/.github/scripts/moderate_apply_triage.sh index 0af529b..66c8dd6 100755 --- a/.github/scripts/moderate_apply_triage.sh +++ b/.github/scripts/moderate_apply_triage.sh @@ -82,8 +82,14 @@ comment="$(jq -r --argjson n "$MAX_TEXT_CHARS" '(.comment // "") | .[0:$n] | gsu if ! [[ "$action" =~ ^[a-z-]{1,30}$ ]] || [ -z "${label_for[$action]+set}" ] || [ -z "$comment" ]; then # Includes "failed" (no usable result), an action not in the table and an # empty comment: nothing from the result is acted on. + # A "failed" result may carry the packaging step's own reason (a patch over + # the size cap); any other unusable result is not echoed. + note="" + if [ "$action" = failed ] && [ -n "$comment" ]; then + note=" $comment" + fi gh issue edit "$issue_number" --add-label "needs-human" || true - gh issue comment "$issue_number" --body "Automated triage ended without a usable result. A maintainer needs to look at this issue." + gh issue comment "$issue_number" --body "Automated triage ended without a usable result.$note A maintainer needs to look at this issue." exit 0 fi diff --git a/.github/scripts/moderate_check_paths.sh b/.github/scripts/moderate_check_paths.sh index 4da2335..4b416ea 100755 --- a/.github/scripts/moderate_check_paths.sh +++ b/.github/scripts/moderate_check_paths.sh @@ -13,7 +13,7 @@ set -euo pipefail denied=0 while IFS= read -r -d '' path; do case "$path" in - .github/* | .devcontainer/* | .claude/* | .vscode/* | .mcp.json | \ + .github/* | .devcontainer/* | .devcontainer.json | .claude/* | .vscode/* | .mcp.json | \ .pre-commit-config.yaml | .pre-commit-config.yml | .gitattributes | .gitmodules | \ Makefile | */Makefile | GNUmakefile | */GNUmakefile | makefile | */makefile | *.mk | \ scripts/release* | scripts/develop.sh | scripts/post-create.sh | \ diff --git a/.github/scripts/moderate_lib.sh b/.github/scripts/moderate_lib.sh index 34f7583..f40ede9 100755 --- a/.github/scripts/moderate_lib.sh +++ b/.github/scripts/moderate_lib.sh @@ -20,11 +20,22 @@ is_sha() { # is_ancestor : is the same commit as, or an ancestor # of, the second argument. Asks the compare API instead of local history, so a -# shallow checkout and a long branch both work. +# shallow checkout and a long branch both work. A failed API call (a 5xx, a +# rate limit, a dropped connection) is retried up to three times, so a +# transient error does not abandon an otherwise good result; an answer other +# than ahead/identical (diverged, behind) is final and not retried, and a call +# that keeps failing still refuses (fails closed). MODERATE_RETRY_DELAY is the +# pause in seconds between attempts (default 2). is_ancestor() { - local status - status="$(gh api "repos/$GITHUB_REPOSITORY/compare/$1...$2" --jq .status)" || return 1 - [ "$status" = ahead ] || [ "$status" = identical ] + local status attempt + for attempt in 1 2 3; do + if status="$(gh api "repos/$GITHUB_REPOSITORY/compare/$1...$2" --jq .status)"; then + [ "$status" = ahead ] || [ "$status" = identical ] + return + fi + [ "$attempt" = 3 ] || sleep "${MODERATE_RETRY_DELAY:-2}" + done + return 1 } # check_result_tree : every path that differs between and HEAD in diff --git a/.github/scripts/moderate_package_result.sh b/.github/scripts/moderate_package_result.sh index 7b78dfa..6f46d1a 100755 --- a/.github/scripts/moderate_package_result.sh +++ b/.github/scripts/moderate_package_result.sh @@ -93,6 +93,12 @@ if [ "$mode" = build ]; then else action=failed comment="" + # A patch over the cap is the one reason worth naming: apply_triage posts a + # failed result's comment, so the maintainer learns why instead of seeing the + # generic "no usable result" note. + if [ "$too_large" = 1 ]; then + comment="The automated change is larger than the ${MAX_PATCH_BYTES}-byte limit for an unattended run." + fi result="$workspace/.moderation-out/triage.json" if [ "$too_large" = 0 ] && agent_file "$result" "$workspace" \ && jq -e '(.action | type == "string") and (.comment | type == "string")' "$result" > /dev/null 2>&1; then diff --git a/.github/scripts/test_moderate_apply_result.py b/.github/scripts/test_moderate_apply_result.py index 5b34b86..c0abdb6 100644 --- a/.github/scripts/test_moderate_apply_result.py +++ b/.github/scripts/test_moderate_apply_result.py @@ -118,6 +118,7 @@ def run_script( "FAKE_LOG": str(self.tmp / "log.txt"), "FAKE_LABELS": labels, "FAKE_FAIL": fail, + "MODERATE_RETRY_DELAY": "0", "FAKE_REMOTE_SHA": remote_sha, "FAKE_PR_URL": pr_url, "GITHUB_SERVER_URL": "https://github.com", diff --git a/.github/scripts/test_moderate_apply_triage.py b/.github/scripts/test_moderate_apply_triage.py index 11083df..210859a 100644 --- a/.github/scripts/test_moderate_apply_triage.py +++ b/.github/scripts/test_moderate_apply_triage.py @@ -78,6 +78,7 @@ def run_script( "GITHUB_RUN_ID": "99", "DEFAULT_BRANCH": "main", "ISSUE_NUMBER": "7", + "MODERATE_RETRY_DELAY": "0", **(run_env or {}), } return subprocess.run( # noqa: S603 -- fixed argv, trusted script @@ -148,6 +149,20 @@ def test_unknown_action_acts_on_nothing_from_the_result(self) -> None: log[1].startswith("gh issue comment 7 --body Automated triage ended") ) + def test_failed_result_reason_is_posted(self) -> None: + """A `failed` result's own reason (the size cap) reaches the note; other unusable ones stay generic.""" + reason = "The automated change is larger than the limit." + proc = self.run_script("failed", comment=reason) + self.assertEqual(proc.returncode, 0, proc.stderr) + self.assertEqual( + self.log()[1], + f"gh issue comment 7 --body Automated triage ended without a usable result. {reason} " + "A maintainer needs to look at this issue.", + ) + (self.tmp / "log.txt").unlink() + self.run_script("bogus", comment=reason) + self.assertNotIn(reason, "\n".join(self.log())) + def test_result_labels_are_ignored(self) -> None: """Only the fixed table decides the label; a label field in the result is never read.""" self.run_script("needs-info", meta_extra={"label": "claude:ok", "labels": "x"}) diff --git a/.github/scripts/test_moderate_tools.py b/.github/scripts/test_moderate_tools.py index 742c612..f9166ed 100644 --- a/.github/scripts/test_moderate_tools.py +++ b/.github/scripts/test_moderate_tools.py @@ -95,6 +95,7 @@ def test_denied(self) -> None: ".github/scripts/run_claude.sh", ".github/renovate.json", ".devcontainer/Dockerfile", + ".devcontainer.json", ".devcontainer/stack/x/compose.yml", ".claude/settings.json", ".mcp.json", @@ -128,6 +129,8 @@ def test_allowed(self) -> None: "docs/Makefile.md", "scripts/other.sh", ".githubx/y", + "sub/.devcontainer.json", + ".devcontainerx/y", "github/workflows/x.yml", "README.md", ] @@ -143,6 +146,53 @@ def test_newline_in_a_path(self) -> None: self.assertEqual(self.verdict(".github/x\nb"), 1) +class IsAncestorTest(unittest.TestCase): + """``is_ancestor`` retries a failing compare call but not a definite answer.""" + + def answer(self, outcomes: str) -> tuple[int, int]: + """Return (exit status, number of gh calls) when gh yields these outcomes in turn. + + Each character is one call: ``a`` prints ahead, ``d`` prints diverged, ``x`` fails. + """ + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + root = Path(tmp.name) + gh = root / "bin" / "gh" + gh.parent.mkdir() + gh.write_text( + "#!/usr/bin/env bash\n" + f'n=$(cat "{root}/n" 2>/dev/null || echo 0); echo $((n+1)) > "{root}/n"\n' + f'c=$(printf %s "{outcomes}" | cut -c$((n+1)))\n' + 'case "$c" in a) echo ahead;; d) echo diverged;; *) exit 1;; esac\n' + ) + gh.chmod(0o755) + proc = run( + ["bash", "-c", f'. "{SCRIPTS}/moderate_lib.sh"; is_ancestor a b'], + env={ + "PATH": f"{gh.parent}:{os.environ['PATH']}", + "GITHUB_REPOSITORY": "o/r", + "MODERATE_RETRY_DELAY": "0", + }, + ) + return proc.returncode, int((root / "n").read_text()) + + def test_success_is_not_retried(self) -> None: + """An ahead answer needs one call.""" + self.assertEqual(self.answer("a"), (0, 1)) + + def test_transient_errors_are_retried(self) -> None: + """Two failed calls then success still succeeds.""" + self.assertEqual(self.answer("xxa"), (0, 3)) + + def test_persistent_errors_fail_closed_after_three_calls(self) -> None: + """A call that never works refuses, after exactly three attempts.""" + self.assertEqual(self.answer("xxxa"), (1, 3)) + + def test_a_definite_no_is_not_retried(self) -> None: + """A diverged answer is final.""" + self.assertEqual(self.answer("da"), (1, 1)) + + @unittest.skipUnless(shutil.which("jq") and shutil.which("git"), "jq and git are required") class PackageResultTest(unittest.TestCase): """``moderate_package_result.sh`` reads the agent's repository without trusting it.""" @@ -254,6 +304,20 @@ def test_triage_result(self) -> None: self.assertEqual((meta["action"], meta["comment"]), ("repro-ready", "it fails")) self.assertTrue((self.out / "patch.diff").exists()) + def test_triage_patch_over_the_cap_keeps_its_reason(self) -> None: + """An oversized patch is dropped, but the triage result says why it failed.""" + (self.ws / "big.txt").write_text("x" * 2_200_000 + "\n") + self.git("add", "big.txt") + self.git("commit", "-q", "-m", "feat: big") + (self.ws / ".moderation-out" / "triage.json").write_text( + json.dumps({"action": "repro-ready", "comment": "it fails"}) + ) + self.package("triage") + meta = json.loads((self.out / "meta.json").read_text()) + self.assertEqual(meta["action"], "failed") + self.assertIn("larger than", meta["comment"]) + self.assertFalse((self.out / "patch.diff").exists()) + def test_triage_result_bad_shapes(self) -> None: """Invalid JSON, wrong types and a symlinked file all become `failed`.""" target = self.ws / ".moderation-out" / "triage.json" diff --git a/.github/template-sync-state.json b/.github/template-sync-state.json index 685a507..9b9fe66 100644 --- a/.github/template-sync-state.json +++ b/.github/template-sync-state.json @@ -1,4 +1,4 @@ { "template_repo": "craftainer/template-base", - "synced_tag": "v1.15.4" + "synced_tag": "v1.15.5" } diff --git a/.github/workflows/README.md b/.github/workflows/README.md index dd7ff84..a8c20b1 100644 --- a/.github/workflows/README.md +++ b/.github/workflows/README.md @@ -9,7 +9,9 @@ A step before the hooks runs `../scripts/semver_release_type.py` when an instance or downstream template ships it (template-rust-crate does; base does not, so the step is skipped here) and the hooks step forwards its - `SEMVER_RELEASE_TYPE` into the container, so a `!`-marked PR title (or, on + `SEMVER_RELEASE_TYPE` into the container (the step appends the script's + output to `$GITHUB_ENV` only if it is empty or exactly + `SEMVER_RELEASE_TYPE=`, failing otherwise), so a `!`-marked PR title (or, on `main`, head commit subject) runs `cargo semver-checks` as `major`. - `smoke.yml` — builds the `runner` stage of the root `Dockerfile` via the root `compose.yml`, starts it against the real Postgres, RustFS, diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index b7ed72f..63eb05e 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -72,7 +72,17 @@ jobs: REF: ${{ github.ref }} PR_TITLE: ${{ github.event.pull_request.title }} COMMIT_MESSAGE: ${{ github.event.head_commit.message }} - run: python3 -I .github/scripts/semver_release_type.py >> "$GITHUB_ENV" + run: | + # The script's stdout is appended to $GITHUB_ENV, so a stray or + # injected line (a newline in a value, another variable name) would + # set arbitrary environment for later steps. Accept nothing but + # empty output or the single expected assignment. + out="$(python3 -I .github/scripts/semver_release_type.py)" + if [ -n "$out" ] && ! [[ "$out" =~ ^SEMVER_RELEASE_TYPE=[a-z]+$ ]]; then + echo "::error::semver_release_type.py printed something other than SEMVER_RELEASE_TYPE=" >&2 + exit 1 + fi + [ -z "$out" ] || printf '%s\n' "$out" >> "$GITHUB_ENV" # --hook-stage manual runs every hook (both the fast pre-commit-stage # ones and the extensive pre-push-stage ones) in one pass, inside the