diff --git a/.github/workflows/claude-review.yml b/.github/workflows/claude-review.yml index 9ebdfe20..b4b242cd 100644 --- a/.github/workflows/claude-review.yml +++ b/.github/workflows/claude-review.yml @@ -65,7 +65,9 @@ on: description: >- Claude model that reviews. "auto" (the default) means Opus, except that the "Pick the model" step reviews the small sites (the repositories it lists) with Sonnet, so one - place sets the model for 19 callers (owner, 2026-10-30, token efficiency). Any other + place sets the model for 19 callers (owner, 2026-10-30, token efficiency). A diff of + only lockfiles, generated files or package.json patch bumps (outside ship-bot's + control-plane paths) gets Sonnet in any repository. Any other value, claude-opus-5-5 included, is used exactly as passed. required: false type: string @@ -250,14 +252,117 @@ jobs: # One place for the model. The small sites get Sonnet; 4thcltr.com, heuristics, papers, # gorfed.net, bindercurve.com, assess and ops keep Opus under "auto". Matching is by # repository name, case-insensitive (ACID2REAPER's remote is spelled in capitals). + # A diff whose every changed path is a mechanical R0 class (lockfiles, generated files, a + # patch bump in package.json) gets Sonnet in an Opus repository too. Docs are not a class: + # a deny-list over *.md leaked rule files four times, so a docs diff keeps the repository's + # normal reviewer. Fails closed: a missing or empty file list, a rename, a quoted path or any + # path outside those classes keeps Opus. - name: Pick the model if: steps.dup.outputs.skip != 'true' id: model env: REPO: ${{ github.repository }} REQUESTED: ${{ inputs.model }} + REVIEW_INPUT: review-input + BASE: ${{ steps.pr.outputs.base }} + HEAD: ${{ steps.pr.outputs.head }} run: | set -euo pipefail + # The lists of ship-bot/lib/drafts.mjs: CONTROL_PLANE_PATHS, CONTROL_PLANE_BASENAMES and + # REPO_EXTRA. test/claudeReview.test.mjs fails when they drift apart. + cp_paths=(.github/ .claude/ ship-bot/ scripts/gates/ scripts/lib/ scripts/review-status.mjs janitor/ capacity/ verification-kit/) + cp_basenames=(review.md claude.md agents.md skill.md canaries.json) + case "$(printf '%s' "$REPO" | tr '[:upper:]' '[:lower:]')" in + gorfednet/.github) cp_paths+=(scripts/ fleet.json templates/ docs/claude-home/) ;; + gorfednet/ops) cp_paths+=(control/claude-home/ control/ship-bot-patches/ hosts/ infra/ scripts/ inventory-patches/ credentials.json rotations.json crawler-policy.json cta-sites.json) ;; + esac + # 0 when the lowercased path is control plane. + control_plane() { + local p="${1#./}" x + for x in "${cp_paths[@]}"; do + if [[ "$x" == */ ]]; then + [[ "$p" == "$x"* || "$p" == "${x%/}" ]] && return 0 + else + [[ "$p" == "$x" ]] && return 0 + fi + done + for x in "${cp_basenames[@]}"; do [[ "${p##*/}" == "$x" ]] && return 0; done + return 1 + } + # 0 when the path is a lockfile, a generated file or a patch-bumped package.json. + r0_class() { + case "$1" in + *.md|*.mdx|*.markdown) return 1 ;; + package-lock.json|*/package-lock.json|pnpm-lock.yaml|*/pnpm-lock.yaml|yarn.lock|*/yarn.lock|bun.lock|*/bun.lock|bun.lockb|*/bun.lockb|cargo.lock|*/cargo.lock|poetry.lock|*/poetry.lock|gemfile.lock|*/gemfile.lock) return 0 ;; + */generated/*) return 0 ;; + package.json|*/package.json) patch_bump_only "$2"; return ;; + esac + # *.generated.* on the file name only: a pattern over the whole path would cross slashes. + case "${1##*/}" in + *.generated.*) return 0 ;; + esac + return 1 + } + # 0 only when every changed path is R0. Matching is on the lowercased path, as ship-bot's + # touchesControlPlane does (a case-insensitive disk lands Docs/x on docs/x). + r0_only() { + local list="${REVIEW_INPUT:-review-input}/files.txt" added deleted path lower found=0 + [[ -s "$list" ]] || return 1 + while IFS=$'\t' read -r added deleted path || [[ -n "$path" ]]; do + [[ -n "$path" ]] || return 1 + found=1 + lower="$(printf '%s' "$path" | tr '[:upper:]' '[:lower:]')" + case "$lower" in + *'=>'*|\"*) return 1 ;; + # Control plane (ship-bot's touchesControlPlane, ship-bot/lib/drafts.mjs): never R0. + *) control_plane "$lower" && return 1 + r0_class "$lower" "$path" || return 1 ;; + esac + done < "$list" + [[ "$found" == 1 ]] + } + # 0 only when this package.json changed nothing but dependency versions that stay within + # the same range prefix, major and minor, inside the same section (dependencies, + # devDependencies, peerDependencies, optionalDependencies, overrides, resolutions). Read + # from git, so a line's section is known. Fails closed on any error. + patch_bump_only() { + [[ -n "${BASE:-}" && -n "${HEAD:-}" ]] || return 1 + local mb + mb="$(git merge-base "$BASE" "$HEAD")" || return 1 + node --input-type=module - "$mb" "$HEAD" "$1" <<'JS' || return 1 + import { execFileSync } from 'node:child_process' + import { isDeepStrictEqual } from 'node:util' + const [, , base, head, path] = process.argv + const read = (rev) => JSON.parse(execFileSync('git', ['show', `${rev}:${path}`], { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 })) + const before = read(base) + const after = read(head) + const SECTIONS = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies', 'overrides', 'resolutions'] + const VERSION = /^([\^~]?)(\d+)\.(\d+)\.(\d+)$/ + const sameBump = (x, y) => { + if (x === y) return true + if (typeof x !== 'string' || typeof y !== 'string') return false + const a = VERSION.exec(x) + const b = VERSION.exec(y) + // Same prefix, major and minor; the patch must rise; 0.0.x moves every patch, so it is no patch bump. + return Boolean(a && b && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && !(a[2] === '0' && a[3] === '0') && Number(b[4]) > Number(a[4])) + } + const ok = (() => { + const keys = new Set([...Object.keys(before), ...Object.keys(after)]) + for (const key of keys) { + if (!SECTIONS.includes(key)) { + if (!isDeepStrictEqual(before[key], after[key])) return false + continue + } + const x = before[key] ?? {} + const y = after[key] ?? {} + if (!isDeepStrictEqual(Object.keys(x).sort(), Object.keys(y).sort())) return false + for (const name of Object.keys(x)) if (!sameBump(x[name], y[name])) return false + } + return true + })() + process.exit(ok ? 0 : 1) + JS + } model="${REQUESTED:-auto}" if [[ "$model" == auto ]]; then model=claude-opus-5-5 @@ -265,6 +370,10 @@ jobs: case "$repo" in promptboi.com|denseware.com|ssatcy.com|gorfmusic.com|subrythm.com|blackpixelrecords.com|anal0g.org|rowanmcarthur.com|acid2reaper) model=claude-sonnet-5-5 ;; esac + if [[ "$model" == claude-opus-5-5 ]] && r0_only; then + model=claude-sonnet-5-5 + echo "every changed path is R0: Sonnet reads it" + fi fi echo "model=${model}" >> "$GITHUB_OUTPUT" echo "reviewing ${REPO} with ${model}" diff --git a/test/claudeReview.test.mjs b/test/claudeReview.test.mjs index dc6ed254..91425b4b 100644 --- a/test/claudeReview.test.mjs +++ b/test/claudeReview.test.mjs @@ -17,6 +17,7 @@ import { tmpdir } from 'node:os' import { dirname, join } from 'node:path' import { after, describe, it } from 'node:test' import { buildReviewInput } from '../.github/actions/review-input/review-input.mjs' +import { CONTROL_PLANE_BASENAMES, CONTROL_PLANE_PATHS, touchesControlPlane } from '../ship-bot/lib/drafts.mjs' import { claudeReviewVerdict, REVIEW_BOT_LOGIN } from '../scripts/lib/reviewVerdict.mjs' const workflow = readFileSync('.github/workflows/claude-review.yml', 'utf8') @@ -663,11 +664,33 @@ describe('claude-review model choice', () => { const script = /run: \|\n([\s\S]*)$/.exec(pick)[1].replace(/^ {10}/gm, '') /** The model the step selects for a repository and requested model. */ - function choose(repo, requested) { + function choose(repo, requested, input) { const dir = mkdtempSync(join(tmpdir(), 'pick-model-')) const out = join(dir, 'out') writeFileSync(out, '') - execFileSync('bash', ['-c', script], { env: { PATH: process.env.PATH, REPO: repo, REQUESTED: requested, GITHUB_OUTPUT: out } }) + let cwd = dir + const env = { PATH: process.env.PATH, REPO: repo, REQUESTED: requested, GITHUB_OUTPUT: out } + if (input) { + mkdirSync(join(dir, 'in')) + if (input.files !== undefined) writeFileSync(join(dir, 'in', 'files.txt'), input.files.map((p) => `1\t0\t${p}`).join('\n') + (input.files.length && !input.noFinalNewline ? '\n' : '')) + if (input.diff !== undefined) writeFileSync(join(dir, 'in', 'full.diff'), input.diff) + env.REVIEW_INPUT = join(dir, 'in') + } + if (input?.pkg) { + const repoDir = join(dir, 'repo') + mkdirSync(repoDir) + const git = (...args) => execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', '-c', 'commit.gpgsign=false', ...args], { cwd: repoDir, encoding: 'utf8' }).trim() + git('init', '-q', '-b', 'main') + writeFileSync(join(repoDir, 'package.json'), JSON.stringify(input.pkg.before, null, 2) + '\n') + git('add', '-A') + git('commit', '-q', '-m', 'base') + env.BASE = git('rev-parse', 'HEAD') + writeFileSync(join(repoDir, 'package.json'), JSON.stringify(input.pkg.after, null, 2) + '\n') + git('commit', '-q', '-am', 'head') + env.HEAD = git('rev-parse', 'HEAD') + cwd = repoDir + } + execFileSync('bash', ['-c', script], { env, cwd }) const model = /^model=(.*)$/m.exec(readFileSync(out, 'utf8'))[1] rmSync(dir, { recursive: true, force: true }) return model @@ -702,4 +725,123 @@ describe('claude-review model choice', () => { assert.equal(choose('gorfednet/promptboi.com', ''), 'claude-sonnet-5-5') assert.equal(choose('gorfednet/assess', ''), 'claude-opus-5-5') }) + + describe('R0-only diffs', () => { + const opus = 'claude-opus-5-5' + const sonnet = 'claude-sonnet-5-5' + const pick = (files, pkg) => choose('gorfednet/ops', 'auto', { files, pkg }) + const pkgOf = (deps, extra = {}) => ({ name: 'x', version: '1.0.0', ...extra, ...deps }) + const dep = (v, section = 'dependencies') => ({ [section]: { 'left-pad': v, other: '^2.0.0' } }) + + it('picks Sonnet for lockfiles and generated files in an Opus repository', () => { + assert.equal(pick(['package-lock.json', 'pnpm-lock.yaml', 'src/x.generated.ts', 'a/generated/y.ts']), sonnet) + }) + it('keeps Opus for a lockfile or generated file under a control-plane path', () => { + for (const p of ['scripts/x.generated.mjs', 'ship-bot/package-lock.json', 'control/claude-home/generated/a.json', '.github/x.generated.yml']) assert.equal(pick([p]), opus, p) + }) + it('picks Sonnet for a patch bump in package.json and its lockfile', () => { + assert.equal(pick(['package.json', 'package-lock.json'], { before: pkgOf(dep('^1.2.3')), after: pkgOf(dep('^1.2.9')) }), sonnet) + }) + it('keeps Opus when the patch does not rise, and for 0.0.x', () => { + assert.equal(pick(['package.json'], { before: pkgOf(dep('^1.2.9')), after: pkgOf(dep('^1.2.3')) }), opus) + assert.equal(pick(['package.json'], { before: pkgOf(dep('0.0.1')), after: pkgOf(dep('0.0.2')) }), opus) + assert.equal(pick(['package.json'], { before: pkgOf(dep('^0.1.2')), after: pkgOf(dep('^0.1.3')) }), sonnet) + }) + it('keeps Opus for a minor or major bump', () => { + assert.equal(pick(['package.json'], { before: pkgOf(dep('^1.2.3')), after: pkgOf(dep('^1.3.0')) }), opus) + assert.equal(pick(['package.json'], { before: pkgOf(dep('^1.2.3')), after: pkgOf(dep('^2.0.0')) }), opus) + }) + it('keeps Opus when only the range prefix changes (not a patch bump)', () => { + assert.equal(pick(['package.json'], { before: pkgOf(dep('~1.2.3')), after: pkgOf(dep('^1.2.3')) }), opus) + assert.equal(pick(['package.json'], { before: pkgOf(dep('^1.2.3')), after: pkgOf(dep('^1.2.4')) }), sonnet) + }) + it('keeps Opus when a dependency moves between sections', () => { + const before = pkgOf({ dependencies: { esbuild: '^0.25.1' }, devDependencies: {} }) + const after = pkgOf({ dependencies: {}, devDependencies: { esbuild: '^0.25.2' } }) + assert.equal(pick(['package.json'], { before, after }), opus) + for (const [a, b] of [['dependencies', 'peerDependencies'], ['devDependencies', 'optionalDependencies'], ['dependencies', 'overrides'], ['dependencies', 'resolutions']]) { + assert.equal(pick(['package.json'], { before: pkgOf({ [a]: { x: '1.0.0' }, [b]: {} }), after: pkgOf({ [a]: {}, [b]: { x: '1.0.1' } }) }), opus, `${a} to ${b}`) + } + }) + it('keeps Opus for a package.json edit that is not a version bump', () => { + assert.equal(pick(['package.json'], { before: pkgOf(dep('^1.2.3'), { scripts: { a: 'x' } }), after: pkgOf(dep('^1.2.4'), { scripts: { a: 'y' } }) }), opus) + assert.equal(pick(['package.json'], { before: pkgOf(dep('^1.2.3')), after: pkgOf(dep('^1.2.4'), { version: '1.0.1' }) }), opus) + }) + const REPO_EXTRA = (() => { + const source = readFileSync('ship-bot/lib/drafts.mjs', 'utf8') + const block = /const REPO_EXTRA = \{([\s\S]*?)\n\}/.exec(source)[1] + const out = {} + for (const m of block.matchAll(/'(gorfednet\/[^']+)': \[([^\]]*)\]/g)) out[m[1]] = [...m[2].matchAll(/'([^']+)'/g)].map((x) => x[1]) + return out + })() + /** A lockfile or generated file inside each control-plane entry (the entry itself when it is a file). */ + const probes = (entry) => (entry.endsWith('/') ? [`${entry}package-lock.json`, `${entry}generated/x.json`] : [entry]) + + it('keeps Opus for every control-plane path ship-bot knows, in the repository it applies to', () => { + assert.deepEqual(Object.keys(REPO_EXTRA).sort(), ['gorfednet/.github', 'gorfednet/ops']) + for (const repo of ['gorfednet/assess', ...Object.keys(REPO_EXTRA)]) { + for (const entry of [...CONTROL_PLANE_PATHS, ...(REPO_EXTRA[repo] ?? [])]) { + for (const path of probes(entry)) { + assert.equal(touchesControlPlane(path, repo), true, `${repo} ${path}`) + assert.equal(choose(repo, 'auto', { files: [path] }), opus, `${repo} ${path}`) + } + } + } + }) + it('names each ops and .github extra by path, with a test per path', () => { + for (const [path, repo] of [['infra/generated/nginx.conf', 'ops'], ['hosts/web/package-lock.json', 'ops'], ['inventory-patches/generated/x.json', 'ops'], ['control/ship-bot-patches/generated/a.json', 'ops'], ['scripts/package-lock.json', 'ops'], ['templates/yarn.lock', '.github'], ['docs/claude-home/generated/a.json', '.github'], ['scripts/x.generated.mjs', '.github']]) { + assert.equal(choose(`gorfednet/${repo}`, 'auto', { files: [path] }), opus, `${repo} ${path}`) + } + }) + it('lets a lockfile through where ship-bot does not call the path control plane', () => { + assert.equal(choose('gorfednet/assess', 'auto', { files: ['scripts/package-lock.json', 'hosts/x/package-lock.json'] }), sonnet) + assert.equal(choose('gorfednet/ops', 'auto', { files: ['apps/web/package-lock.json'] }), sonnet) + }) + it("carries ship-bot's lists exactly: drift in drafts.mjs fails here", () => { + const lists = /cp_paths=\(([^)]*)\)\n\s+cp_basenames=\(([^)]*)\)/.exec(workflow) + const words = (text) => text.trim().split(/\s+/).sort() + assert.deepEqual(words(lists[1]), [...CONTROL_PLANE_PATHS].sort()) + assert.deepEqual(words(lists[2]), CONTROL_PLANE_BASENAMES.map((b) => b.toLowerCase()).sort()) + const extras = /case "\$\(printf[^\n]*\n([\s\S]*?)\n\s+esac/.exec(workflow)[1] + for (const [repo, entries] of Object.entries(REPO_EXTRA)) { + const line = extras.split('\n').find((l) => l.includes(repo + ')')) + assert.deepEqual(words(/\+=\(([^)]*)\)/.exec(line)[1]), [...entries].sort(), repo) + } + }) + it('does not let *.generated.* cross a slash in a directory name', () => { + assert.equal(pick(['a.generated.d/app.ts']), opus) + assert.equal(pick(['a.generated.d/sub/app.ts']), opus) + assert.equal(pick(['a/app.generated.ts']), sonnet) + }) + it('keeps the last path of a list with no final newline', () => { + assert.equal(choose('gorfednet/ops', 'auto', { files: ['package-lock.json', 'src/app.ts'], noFinalNewline: true }), opus) + assert.equal(choose('gorfednet/ops', 'auto', { files: ['package-lock.json', 'x/app.generated.ts'], noFinalNewline: true }), sonnet) + }) + it('reads the heredoc script as a module', () => { + assert.match(workflow, /node --input-type=module - "\$mb"/) + }) + it('keeps Opus for a rule basename at any depth, even inside a generated directory', () => { + for (const p of ['docs/generated/CLAUDE.md', 'x/generated/canaries.json', 'a/generated/REVIEW.md', 'a/generated/SKILL.md', 'a/generated/AGENTS.md']) assert.equal(pick([p]), opus, p) + assert.equal(pick(['x/generated/notes.json']), sonnet) + }) + it('keeps Opus for a mixed list (code beside a lockfile)', () => { + assert.equal(pick(['package-lock.json', 'src/app.ts']), opus) + assert.equal(pick(['package-lock.json', '.github/workflows/ci.yml']), opus) + }) + it('keeps Opus for every markdown file: docs are not an R0 class', () => { + for (const p of ['REVIEW.md', 'CLAUDE.md', 'control/claude-home/rules/x.md', '.claude/agents/a.md', 'README.md', 'ship-bot/README.md', 'docs/verification-rules.md', 'docs/retired-checks.md', 'docs/content-guidelines.md', 'templates/REVIEW.site-footer.md', 'templates/CLAUDE.snippet.md', 'scripts/local-llm/out.md', 'janitor/PROMPT.md', 'janitor/SKILL.md', 'skills/x/SKILL.md', 'docs/skills/a.md', 'a/agents/b.md', 'x/rules/y.md', 'Docs/Verification-Rules.md', 'MEMORY.md', 'ship-bot/lib/notes.md', 'docs/programme-template.md', 'docs/a.md', 'docs/notes/README.md', 'hosts/x/README.md', 'infra/README.md', 'docs/generated/notes.md', 'docs/x.generated.md', 'a/generated/page.mdx', 'a/x.generated.markdown']) assert.equal(pick([p]), opus, p) + }) + it('fails closed: an empty, missing or unreadable list keeps Opus', () => { + assert.equal(pick([]), opus) + assert.equal(choose('gorfednet/ops', 'auto', {}), opus) + assert.equal(choose('gorfednet/ops', 'auto'), opus) + // Only the guard stops these: each matches */generated/* and no control-plane prefix sees through the brace or the quote. + assert.equal(pick(['{.github/workflows => x}/generated/ci.yml']), opus) + assert.equal(pick(['"x/generated/odd\\303\\251.json"']), opus) + assert.equal(pick(['x/generated/ok.json']), sonnet) + }) + it('never overrides an explicit model', () => { + assert.equal(choose('gorfednet/ops', 'claude-opus-5-5', { files: ['package-lock.json'] }), opus) + }) + }) })