diff --git a/.github/actions/collect-base-activity/action.yml b/.github/actions/collect-base-activity/action.yml index 563f8458451..4d91b9c63d0 100644 --- a/.github/actions/collect-base-activity/action.yml +++ b/.github/actions/collect-base-activity/action.yml @@ -200,7 +200,7 @@ runs: # the copy at , where the runner leaves it. Sets check_wrappers # (their paths at ) and check_prompt. stage_checks() { - local dir=$1 at=$2 check script flags wrapper config + local dir=$1 at=$2 check script flags wrapper config part check_wrappers=() [[ $at =~ ^/[A-Za-z0-9_./-]+$ && $WORKSPACE =~ ^/[A-Za-z0-9_./-]+$ ]] || fail "the checks directory or workspace has a name a rule cannot carry" @@ -220,6 +220,14 @@ runs: mkdir -p "$dir/plugins/skill-quality" cp -R "$BASE_PATH/plugins/skill-quality/scripts" "$dir/plugins/skill-quality/" fi + # check-affected.sh runs the comment-lint hook from this copy, which + # sources and reads files across these three directories. + if [ -d "$BASE_PATH/plugins/code-tidying/git-hooks" ]; then + mkdir -p "$dir/plugins/code-tidying" + for part in git-hooks lib scripts; do + cp -R "$BASE_PATH/plugins/code-tidying/$part" "$dir/plugins/code-tidying/" + done + fi for check in "${checks[@]}"; do script=${check%% *} flags=${check#"$script"} diff --git a/.github/actions/collect-base-activity/collect-base-activity.test.mjs b/.github/actions/collect-base-activity/collect-base-activity.test.mjs index b5b4c44be2a..c9be12247be 100644 --- a/.github/actions/collect-base-activity/collect-base-activity.test.mjs +++ b/.github/actions/collect-base-activity/collect-base-activity.test.mjs @@ -3,6 +3,7 @@ // come from the contract (pr-run-activity.md, "Skill activities" and // "prepare: merge-base"): a writing skill gets the signed-commit tool and the // instruction to commit; a merge-base skill runs on run-claude-cli alone. +// test-scope: .github/actions/resolve-config/package.json .github/scripts/apply-patch/* plugins/skill-quality/* import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; @@ -371,6 +372,28 @@ test("a listed check's copy carries the base's skill-quality scripts", () => { } }); +test("a listed check's copy carries the base's code-tidying comment-lint files", () => { + const tidying = path.join(base, "plugins", "code-tidying"); + const files = { + "git-hooks/comment-lint.sh": "echo base hook\n", + "lib/git-hooks/check-lib.sh": "echo base lib\n", + "scripts/comment-settings.sh": "echo base settings\n", + }; + for (const [rel, body] of Object.entries(files)) { + mkdirSync(path.dirname(path.join(tidying, rel)), { recursive: true }); + writeFileSync(path.join(tidying, rel), body); + } + try { + const { status, checksDir } = withChecks(["scripts/check-x.sh --check"]); + assert.equal(status, 0); + for (const [rel, body] of Object.entries(files)) { + assert.equal(readFileSync(path.join(checksDir, "plugins", "code-tidying", rel), "utf8"), body, rel); + } + } finally { + rmSync(tidying, { recursive: true }); + } +}); + // A skill that commits runs its checks, fixes and re-runs them before it // commits, since the checks read the working tree (#8319 review). test("a committing skill with checks is told to commit after the checks and their fixes", () => { diff --git a/scripts/check-affected-comment-lint.test.sh b/scripts/check-affected-comment-lint.test.sh new file mode 100755 index 00000000000..51a1f2fa8f9 --- /dev/null +++ b/scripts/check-affected-comment-lint.test.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env bash +# test-scope: scripts/check-affected.sh scripts/check-affected-gates.txt plugins/code-tidying/git-hooks/* plugins/code-tidying/lib/* plugins/code-tidying/scripts/* +# shellcheck disable=SC2016 +set -uo pipefail + +SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || exit 2 +# shellcheck source=lib/test-harness.sh +. "$SELF_DIR/lib/test-harness.sh" || exit 2 +# shellcheck source=test-git-helpers.sh +. "$SELF_DIR/test-git-helpers.sh" || exit 2 + +if ! python3 -c 'import pygments' 2>/dev/null; then + echo "FAIL - python3 cannot import pygments, which the comment lint needs" >&2 + exit 1 +fi + +tmp="$(mktemp -d)" || exit 2 +trap 'rm -rf "$tmp"' EXIT +repo="$tmp/repo" +planted="$SELF_DIR/../plugins/code-tidying/git-hooks/fixtures/shape2.sh.txt" +mkdir -p "$repo/scripts" +git_init_test_repo "$repo" || exit 2 +cp "$SELF_DIR/check-affected.sh" "$SELF_DIR/check-affected-gates.txt" "$repo/scripts/" +printf '#!/bin/sh\necho "$1"\n' >"$repo/tool.sh" +git_test_config "$repo" add -A +git_test_config "$repo" commit -q -m base +git_test_config "$repo" branch -q bare +printf 'x\n' >"$repo/notes.txt" +git_test_config "$repo" add notes.txt +git_test_config "$repo" commit -q -m notes + +run() { + local -a envs=() + while [[ "$1" != -- ]]; do + envs+=("$1") + shift + done + shift + RC=0 + OUT="$(cd "$repo" && env ${envs[@]+"${envs[@]}"} bash scripts/check-affected.sh --no-fetch "$@" 2>"$tmp/err")" || RC=$? + ERR="$(cat "$tmp/err")" +} +check() { + if eval "$2"; then ok "$1"; else bad "$1" "rc=$RC out=$OUT err=$ERR"; fi +} + +run -- --base bare +check "a tree without the comment-lint hook skips comment-lint" \ + '[[ $ERR == *"SKIP comment-lint (no plugins/code-tidying/git-hooks/comment-lint.sh beside this script)"* ]]' + +mkdir -p "$repo/plugins/code-tidying" "$repo/docs/conventions" +for part in git-hooks lib scripts; do + cp -R "$SELF_DIR/../plugins/code-tidying/$part" "$repo/plugins/code-tidying/" +done +printf 'comment_posture: necessary\ncomment_lint: block\n' >"$repo/docs/conventions/code-tidying.yaml" +git_test_config "$repo" add -A +git_test_config "$repo" commit -q -m lint-base +git_test_config "$repo" branch -q lint-base +printf '#!/bin/sh\necho "$1" "$2"\n' >"$repo/tool.sh" +git_test_config "$repo" commit -q -am clean-tool + +mkdir -p "$tmp/nopy" +printf '#!/bin/sh\nexit 1\n' >"$tmp/nopy/python3" +chmod +x "$tmp/nopy/python3" +run PATH="$tmp/nopy:$PATH" -- --base lint-base +check "a python3 that cannot import pygments skips comment-lint, not a failure" \ + '[[ $ERR == *"SKIP comment-lint (python3 with pygments not installed)"* && $ERR != *"FAIL comment-lint"* ]]' + +run -- --base lint-base +check "a change with no comment passes comment-lint" \ + '[[ $ERR != *"FAIL comment-lint"* && $ERR != *"SKIP comment-lint"* && $ERR != *"CI only: comment-lint "* ]]' + +cp "$planted" "$repo/tool.sh" +git_test_config "$repo" commit -q -am planted-tool +run -- --base lint-base +check "a committed comment the posture disallows fails comment-lint at its line" \ + '[[ $RC == 1 && $ERR == *"FAIL comment-lint"* && $ERR == *"tool.sh:8:"* ]]' + +printf 'comment_posture: necessary\ncomment_lint: off\n' >"$repo/docs/conventions/code-tidying.yaml" +git_test_config "$repo" commit -q -am lint-off +run -- --base lint-base +check "the merge-base's comment_lint level decides, not one the branch sets" \ + '[[ $RC == 1 && $ERR == *"FAIL comment-lint"* && $ERR == *"tool.sh:8:"* ]]' + +git_test_config "$repo" reset -q --hard lint-base +cp "$planted" "$repo/tool.sh" +run -- --uncommitted +check "an uncommitted comment the posture disallows fails comment-lint under --uncommitted" \ + '[[ $RC == 1 && $ERR == *"FAIL comment-lint"* && $ERR == *"tool.sh:8:"* ]]' + +git_test_config "$repo" reset -q --hard lint-base +mkdir -p "$repo/team-conventions" +git_test_config "$repo" mv docs/conventions/code-tidying.yaml team-conventions/code-tidying.yaml +printf '\nTeam conventions live in `team-conventions`.\n\n' >"$repo/AGENTS.md" +git_test_config "$repo" add AGENTS.md +git_test_config "$repo" commit -q -m home-base +git_test_config "$repo" branch -q home-base +cp "$planted" "$repo/tool.sh" +git_test_config "$repo" commit -q -am planted-tool-home +run -- --base home-base +check "a merge-base whose AGENTS.md points at a non-default home reads its comment_lint level there" \ + '[[ $RC == 1 && $ERR == *"FAIL comment-lint"* && $ERR == *"tool.sh:8:"* ]]' + +test_harness::report diff --git a/scripts/check-affected-gates.txt b/scripts/check-affected-gates.txt index aace9d39b6b..98118a4a211 100644 --- a/scripts/check-affected-gates.txt +++ b/scripts/check-affected-gates.txt @@ -28,6 +28,7 @@ as-of-recheck run as-of-recheck cost-figures run cost-figures changed-skills run changed-skills comment-hygiene run comment-hygiene +comment-lint run comment-lint size-check run size-check # Not run by scripts/check-affected.sh. The reason names where the gate comes from @@ -51,7 +52,6 @@ changelog-fragments-release ci-only release pull request only, scripts/check-cha vendor-version-bump ci-only scripts/check-vendor-version-bump.sh --check-bump portability-lint ci-only scripts/check-skill-portability.sh cant-fail-scan ci-only scripts/check-cant-fail-scan.sh -comment-lint ci-only pull request only; the code-tidying comment-lint git hook runs it at commit, scripts/check-comment-lint.sh with HEAD_SHA and BASE_REF by hand evals-quality ci-only bash plugins/skill-quality/scripts/check-evals-quality.sh eval-cases ci-only python3 plugins/evals/skills/validate/scripts/validate-cases.py listing-budget-configured ci-only whole-repo scanner, bash plugins/skill-quality/scripts/check-listing-budget.sh plugins/*/skills diff --git a/scripts/check-affected.sh b/scripts/check-affected.sh index 555cc1997c4..649d4274e72 100755 --- a/scripts/check-affected.sh +++ b/scripts/check-affected.sh @@ -278,7 +278,7 @@ fi gates_file="$tools/scripts/check-affected-gates.txt" declare -a passed=() skipped=() failed=() out="$(mktemp)" || exit 2 -trap 'rm -rf "$out" "$out.failures" "$out.patterns" "$out.not-run" "$out.left" "$out.node" "$out.md"' EXIT +trap 'rm -rf "$out" "$out.failures" "$out.patterns" "$out.not-run" "$out.left" "$out.node" "$out.md" "$out.cl"' EXIT skip() { skipped+=("$1 ($2)"); } @@ -525,6 +525,39 @@ else bash "$tools/scripts/check-comment-hygiene.sh" "$merge_base" "${changed[@]}" fi +comment_lint=plugins/code-tidying/git-hooks/comment-lint.sh +comment_lint_merge_base_diff() { + local d="$out.cl" f home head + head="$(git rev-parse HEAD)" && + git -c init.defaultBranch=lint init --quiet --template= "$d" && + printf '%s\n' "$(cd "$(git rev-parse --git-common-dir)/objects" && pwd -P)" >"$d/.git/objects/info/alternates" && + git -C "$d" update-ref --no-deref HEAD "$merge_base" && + git -C "$d" read-tree "$head" || return 2 + copy_from_merge_base() { + if git cat-file -e "$merge_base:$1" 2>/dev/null; then + mkdir -p "$d/$(dirname "$1")" && git show "$merge_base:$1" >"$d/$1" + fi + } + copy_from_merge_base AGENTS.md && copy_from_merge_base CLAUDE.md || return 2 + home="$(bash "$tools/plugins/code-tidying/lib/resolve-convention-home.sh" --root "$d" --ref "$merge_base" 2>/dev/null)" || home="" + [[ -n "$home" && "$home" != /* && "$home" != *..* ]] || home=docs/conventions + for f in code-tidying.yaml code-tidying.local.yaml; do + copy_from_merge_base "${home%/}/$f" || return 2 + done + (cd "$d" && AI_AGENT=ci sh "$tools/$comment_lint") +} +if ((${#changed[@]} == 0)); then + skip comment-lint "no changed files" +elif [[ ! -f "$tools/$comment_lint" ]]; then + skip comment-lint "no $comment_lint beside this script" +elif ! python3 -c 'import pygments' >/dev/null 2>&1; then + skip comment-lint "python3 with pygments not installed" +else + run_check comment-lint \ + "AI_AGENT=ci sh $comment_lint --worktree for uncommitted edits, else scripts/check-comment-lint.sh with HEAD_SHA, BASE_REF and GITHUB_EVENT_NAME=pull_request" \ + comment_lint_merge_base_diff +fi + # machine-specific-paths: the scan the workflow's check-machine-paths composite # (melodic-software/ci-workflows) runs, over the changed files of the types it # scans. The regex bodies come from the merge-base's guardrails plugin copy of