diff --git a/.agents/rules/rule_authoring_standards.md b/.agents/rules/rule_authoring_standards.md index 5be68a81b..c30486c04 100644 --- a/.agents/rules/rule_authoring_standards.md +++ b/.agents/rules/rule_authoring_standards.md @@ -119,6 +119,7 @@ Destinations by language, all reachable through `.githooks/pre-commit` and its ` | Python | [`pyproject.toml`](../../pyproject.toml) ruff `select` and banned-api config, mypy | | Go | [`.golangci.yml`](../../.golangci.yml), `nogo_config.json`, custom checks in `tools/lint/` | | TypeScript / UI | `cmd/ui/eslint.config.js`, prettier, `tools/lint/check_file_naming.py` (UI template/test and `.github/scripts/` test naming) | +| Non-UI JavaScript | [`tools/lint/eslint.nonui.config.mjs`](../../tools/lint/eslint.nonui.config.mjs) (`.github/scripts/`, `tools/nightly_tests/`, `cmd/experiment_state_server/static/`) | | Bazel | buildifier | | Terraform | [`.tflint.hcl`](../../.tflint.hcl) | | Agent docs (`.agents/**/*.md`, `AGENTS.md`) | `tools/lint/check_agent_doc_paths.py` (path references must exist) | diff --git a/.github/scripts/README.md b/.github/scripts/README.md index 6d42daf2a..d8711afad 100644 --- a/.github/scripts/README.md +++ b/.github/scripts/README.md @@ -124,7 +124,7 @@ Any modification touching protected files immediately aborts auto-reapproval: - `api/buf.yaml` and `api/BUILD.bazel` (the breaking-change rules and test definition, which could otherwise weaken or redirect the check) - Target base branch configuration files -Scoping uses 3-dot diff against the PR's target base branch (`git diff --name-only origin/${baseRef}...${currentSha}`) to prevent false positives when intermediate commits are merged into `origin/main`. If Git commands fail, the check defaults to `hasProtectedFileChanges = true` (fail-safe). +Scoping uses 3-dot diff against the PR's target base branch (`git diff --name-only --no-renames origin/${baseRef}...${currentSha}`) to prevent false positives when intermediate commits are merged into `origin/main` and to detect renames out of protected paths. If Git commands fail, the check defaults to `hasProtectedFileChanges = true` (fail-safe). **Clean rebase exception**: A PR touching protected files is still auto-reapproved, for any author, when the push is a pure clean rebase: every approved commit is unchanged in `git range-diff`, with zero changed patch lines and no removed or added commits. Any other change (including grace delta or test-only edits) keeps the lockout. Restoring a dismissed approval on the same commit also stays locked out. diff --git a/.github/scripts/approval-policy.test.js b/.github/scripts/approval-policy.test.js index 383914309..3c7720189 100644 --- a/.github/scripts/approval-policy.test.js +++ b/.github/scripts/approval-policy.test.js @@ -76,7 +76,7 @@ test('checkProtectedFileChanges detects sensitive modifications and handles git // Sensitive files modified childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames') { return '.github/scripts/incremental-approval.js\ncmd/server/main.go\n'; } throw new Error(`Unexpected command: ${cmd}`); @@ -91,7 +91,7 @@ test('checkProtectedFileChanges detects sensitive modifications and handles git // Non-sensitive files modified childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames') { return 'pkg/main.go\nREADME.md\n'; } throw new Error(`Unexpected command: ${cmd}`); @@ -143,11 +143,11 @@ test('checkProtectedFileChanges uses 3-dot diff against baseRef and avoids false const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff') { - if (args[2] === 'origin/feat/tools-dev-env-helm-deployer...c40cf0f') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames') { + if (args[3] === 'origin/feat/tools-dev-env-helm-deployer...c40cf0f') { return 'tools/dev_env/local_runner.py\ntools/dev_env/local_runner_test.py\n'; } - if (args[2] === '8fa2009' && args[3] === 'c40cf0f') { + if (args[3] === '8fa2009' && args[4] === 'c40cf0f') { return '.github/scripts/incremental-approval.js\ntools/dev_env/local_runner.py\n'; } } @@ -171,7 +171,7 @@ test('checkProtectedFileChanges detects sensitive files in 3-dot diff', () => { const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...c40cf0f') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...c40cf0f') { return '.github/workflows/incremental-pr-approval.yml\n'; } throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); @@ -189,7 +189,7 @@ test('checkProtectedFileChanges respects 3-argument signature even when execOpti const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...c40cf0f') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...c40cf0f') { return 'pkg/foo.go\n'; } throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); @@ -203,6 +203,23 @@ test('checkProtectedFileChanges respects 3-argument signature even when execOpti } }); +test('checkProtectedFileChanges detects moved protected file when --no-renames reports both paths', () => { + const originalExec = childProcess.execFileSync; + + childProcess.execFileSync = (cmd, args) => { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...c40cf0f') { + return '.github/workflows/lint.yml\ndocs/lint.yml.bak\n'; + } + throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); + }; + + try { + assert.strictEqual(checkProtectedFileChanges('main', 'c40cf0f', 'c40cf0f'), true); + } finally { + childProcess.execFileSync = originalExec; + } +}); + test('formatReapprovalReviewBody formats markdown body accurately', () => { const body = formatReapprovalReviewBody('Clean rebase detected.', 'alice', 'abcdef1234567890'); assert.match(body, /🤖 \*\*Incremental PR Approval\*\*/); diff --git a/.github/scripts/git-range-diff.js b/.github/scripts/git-range-diff.js index d97769395..09446b13d 100644 --- a/.github/scripts/git-range-diff.js +++ b/.github/scripts/git-range-diff.js @@ -263,7 +263,9 @@ function computeBulkCommitPatchIds(shas, opts) { patchIdMap.set(sha, patchId); } } - } catch (_) {} + } catch (_) { + /* Best-effort cache population; caller tolerates partial map. */ + } return patchIdMap; } @@ -310,7 +312,9 @@ function resolveParentBaseFromMatch(matchedSha, commitIndex, opts) { for (const target of [`${matchedSha}~${baseOffset}`, `${matchedSha}~1`]) { try { return execGit(['rev-parse', '--verify', target], { ...opts, ignoreExitCode: false }).trim(); - } catch (_) {} + } catch (_) { + /* Probe next parent candidate when this offset does not exist. */ + } } return null; } @@ -369,7 +373,9 @@ function resolveStackedApprovedRange(currentRange, approvedRange, approvedSha, p const correlatedBase = correlateStackedParentBase(currCommits, approvedMaps, opts); if (correlatedBase) return `${correlatedBase}..${approvedSha}`; } - } catch (_) {} + } catch (_) { + /* Fall back to commit-count range resolution below. */ + } try { execGit(['rev-parse', '--verify', `${approvedSha}~${prCommitCount}`], { ...opts, ignoreExitCode: false }); @@ -538,24 +544,23 @@ function getPrChangedFiles(baseRefOrApprovedSha, approvedShaOrCurrentSha, curren } if (baseRef) { - const prDiffOutput = execGit(['diff', '--name-only', `origin/${baseRef}...${headSha}`], opts); + const diffOpts = { ...opts, ignoreExitCode: false }; + const prDiffOutput = execGit(['diff', '--name-only', '--no-renames', `origin/${baseRef}...${headSha}`], diffOpts); const files = new Set(prDiffOutput.split(/\r?\n/).map(f => f.trim()).filter(Boolean)); if (approvedSha && approvedSha !== headSha) { - try { - const isAncestor = execGit(['merge-base', '--is-ancestor', approvedSha, headSha], { ...opts, ignoreExitCode: true }); - if (isAncestor.status === 0) { - const followUpDiff = execGit(['diff', '--name-only', approvedSha, headSha], opts); - for (const file of followUpDiff.split(/\r?\n/).map(f => f.trim()).filter(Boolean)) { - files.add(file); - } + const isAncestor = execGit(['merge-base', '--is-ancestor', approvedSha, headSha], { ...opts, ignoreExitCode: true }); + if (isAncestor.status === 0) { + const followUpDiff = execGit(['diff', '--name-only', '--no-renames', approvedSha, headSha], diffOpts); + for (const file of followUpDiff.split(/\r?\n/).map(f => f.trim()).filter(Boolean)) { + files.add(file); } - } catch (_) {} + } } return Array.from(files); } - const diffOutput = execGit(['diff', '--name-only', approvedSha, headSha], opts); + const diffOutput = execGit(['diff', '--name-only', '--no-renames', approvedSha, headSha], { ...opts, ignoreExitCode: false }); return diffOutput.split(/\r?\n/).map(f => f.trim()).filter(Boolean); } diff --git a/.github/scripts/git-range-diff.test.js b/.github/scripts/git-range-diff.test.js index bccd73cf1..8252fa226 100644 --- a/.github/scripts/git-range-diff.test.js +++ b/.github/scripts/git-range-diff.test.js @@ -18,14 +18,6 @@ const childProcess = require('child_process'); const { validateGitRefs, buildGitEnv, - execGit, - ensureGitRefs, - isFastForwardApproval, - getCommitCountInRange, - getCommitPatchId, - indexApprovedCandidateCommits, - correlateStackedParentBase, - resolveStackedApprovedRange, resolveCommitRanges, runGitRangeDiff, parseNumstatDelta, @@ -563,13 +555,13 @@ test('buildGitEnv constructs non-interactive environment and basic auth header', test('getPrChangedFiles returns files from 3-dot diff and includes follow-up commits if ancestor', () => { const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...headSha') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...headSha') { return 'file1.go\nfile2.go\n'; } if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { return ''; } - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'approvedSha' && args[3] === 'headSha') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'approvedSha' && args[4] === 'headSha') { return 'file2.go\nfile3.go\n'; } throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); @@ -583,6 +575,112 @@ test('getPrChangedFiles returns files from 3-dot diff and includes follow-up com } }); +test('getPrChangedFiles skips follow-up diff when approvedSha is unreachable or non-ancestor', () => { + const originalExec = childProcess.execFileSync; + let followUpCalled = false; + childProcess.execFileSync = (cmd, args) => { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...headSha') { + return 'file1.go\nfile2.go\n'; + } + if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { + const err = new Error('fatal: Not a valid object name approvedSha'); + err.status = 128; + throw err; + } + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'approvedSha' && args[4] === 'headSha') { + followUpCalled = true; + return 'file3.go\n'; + } + throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); + }; + + try { + const files = getPrChangedFiles('main', 'approvedSha', 'headSha'); + assert.strictEqual(followUpCalled, false); + assert.deepStrictEqual(files.sort(), ['file1.go', 'file2.go']); + } finally { + childProcess.execFileSync = originalExec; + } +}); + +test('getPrChangedFiles propagates follow-up git diff failures instead of failing open', () => { + const originalExec = childProcess.execFileSync; + childProcess.execFileSync = (cmd, args) => { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...headSha') { + return 'file1.go\n'; + } + if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { + return ''; + } + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'approvedSha' && args[4] === 'headSha') { + const err = new Error('git diff failed: ETIMEDOUT'); + err.status = 1; + throw err; + } + throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); + }; + + try { + assert.throws(() => getPrChangedFiles('main', 'approvedSha', 'headSha'), /ETIMEDOUT/); + } finally { + childProcess.execFileSync = originalExec; + } +}); + +test('getPrChangedFiles normalizes ignoreExitCode to false for git diff calls', () => { + const originalExec = childProcess.execFileSync; + childProcess.execFileSync = (cmd, args) => { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...headSha') { + return 'file1.go\n'; + } + if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { + return ''; + } + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'approvedSha' && args[4] === 'headSha') { + return 'file2.go\n'; + } + throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); + }; + + try { + const threeDotFiles = getPrChangedFiles('main', 'approvedSha', 'headSha', { ignoreExitCode: true }); + assert.deepStrictEqual(threeDotFiles.sort(), ['file1.go', 'file2.go']); + const twoDotFiles = getPrChangedFiles('approvedSha', 'headSha', { ignoreExitCode: true }); + assert.deepStrictEqual(twoDotFiles, ['file2.go']); + } finally { + childProcess.execFileSync = originalExec; + } +}); + +test('getPrChangedFiles passes --no-renames across 3-dot, follow-up, and 2-dot git diff calls', () => { + const originalExec = childProcess.execFileSync; + const diffArgs = []; + childProcess.execFileSync = (cmd, args) => { + if (cmd === 'git' && args[0] === 'diff') { + diffArgs.push(args); + return '.github/workflows/lint.yml\ndocs/lint.yml.bak\n'; + } + if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { + return ''; + } + throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); + }; + + try { + const threeDot = getPrChangedFiles('main', 'approvedSha', 'headSha'); + const twoDot = getPrChangedFiles('approvedSha', 'headSha'); + assert.deepStrictEqual(threeDot, ['.github/workflows/lint.yml', 'docs/lint.yml.bak']); + assert.deepStrictEqual(twoDot, ['.github/workflows/lint.yml', 'docs/lint.yml.bak']); + assert.deepStrictEqual(diffArgs, [ + ['diff', '--name-only', '--no-renames', 'origin/main...headSha'], + ['diff', '--name-only', '--no-renames', 'approvedSha', 'headSha'], + ['diff', '--name-only', '--no-renames', 'approvedSha', 'headSha'] + ]); + } finally { + childProcess.execFileSync = originalExec; + } +}); + test('getCommitModifiedFiles returns file list for commit from diff-tree', () => { const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { diff --git a/.github/scripts/range-diff-parser.js b/.github/scripts/range-diff-parser.js index 9e05d1fac..e864e6169 100644 --- a/.github/scripts/range-diff-parser.js +++ b/.github/scripts/range-diff-parser.js @@ -126,8 +126,9 @@ function parseRangeDiff(output) { const addedCommitShas = []; for (const line of lines) { + // eslint-disable-next-line no-control-regex -- intentionally strips ANSI escape codes const cleanLine = line.replace(/\x1b\[[0-9;]*m/g, ''); - const commitMatch = cleanLine.match(/^\s*(?:\d+|-):\s+([a-f0-9-]+)\s+([=!<>])\s+(?:\d+|-):\s+([a-f0-9-]+)/); + const commitMatch = cleanLine.match(/^ {0,3}(?:\d+|-):\s+([a-f0-9-]+)\s+([=!<>])\s+(?:\d+|-):\s+([a-f0-9-]+)/); if (commitMatch) { const status = commitMatch[2]; if (status !== '=') { diff --git a/.github/scripts/range-diff-parser.test.js b/.github/scripts/range-diff-parser.test.js index c6f92c448..ddc769de2 100644 --- a/.github/scripts/range-diff-parser.test.js +++ b/.github/scripts/range-diff-parser.test.js @@ -280,6 +280,39 @@ test('parseRangeDiff identifies added commits and captures addedCommitShas', () assert.deepStrictEqual(resultRemoved.addedCommitShas, []); }); +test('parseRangeDiff ignores commit-pair patterns in code context and commit message lines', () => { + const sampleOutput = [ + '1: a1b2c3d ! 1: e5f6g7h Commit title', + ' ## Commit message ##', + ' 1: a1b2c3d < -: -------', + ' -: ------- > 2: 89abcdef', + ' @@ pkg/a.go:10,5 @@', + ' 1: 0000000 = 1: 0000000', + ' 1: a1b2c3d < -: -------', + ' -: ------- > 2: 89abcdef', + ' ++realDeltaLine()' + ].join('\n'); + + const result = parseRangeDiff(sampleOutput); + assert.strictEqual(result.hasRemovedCommits, false); + assert.deepStrictEqual(result.addedCommitShas, []); + assert.strictEqual(result.additions, 1); + assert.strictEqual(result.totalDelta, 1); + assert.strictEqual(result.isCleanRebase, false); +}); + +test('parseRangeDiff matches genuine commit headers padded with up to 3 leading spaces', () => { + const sampleOutput = [ + ' 10: a1b2c3d = 10: e5f6g7h Tenth commit', + ' -: ------- > 1000: 89abcdef Added thousandth commit' + ].join('\n'); + + const result = parseRangeDiff(sampleOutput); + assert.strictEqual(result.hasRemovedCommits, false); + assert.deepStrictEqual(result.addedCommitShas, ['89abcdef']); + assert.strictEqual(result.isCleanRebase, false); +}); + test('isIgnoredRangeDiffHeader detects metadata and header lines', () => { assert.strictEqual(isIgnoredRangeDiffHeader('@@ Metadata'), true); assert.strictEqual(isIgnoredRangeDiffHeader('Author: Alice '), true); diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9d1612106..a1c9981a7 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -72,6 +72,13 @@ jobs: - 'tools/common/tool_utils.py' - '.github/workflows/nightly-examples-e2e.yml' - '.github/workflows/lint.yml' + nonui_js: + - '.github/scripts/**/*.js' + - 'tools/nightly_tests/**/*.js' + - 'cmd/experiment_state_server/static/**/*.js' + - 'tools/lint/eslint.nonui.config.mjs' + - 'cmd/ui/package-lock.json' + - '.github/workflows/lint.yml' # 1. Python Linting (Ruff & Mypy) - name: 'Set up Python 3.13' @@ -242,7 +249,7 @@ jobs: # 3. UI Linting & Formatting (Prettier & ESLint) - name: 'Set up Node.js' - if: steps.changes.outputs.ui == 'true' + if: github.event_name == 'workflow_dispatch' || steps.changes.outputs.ui == 'true' || steps.changes.outputs.nonui_js == 'true' uses: 'actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a' # v4.2.0 with: node-version: '22' @@ -250,7 +257,7 @@ jobs: cache-dependency-path: 'cmd/ui/package-lock.json' - name: 'Install UI Dependencies' - if: steps.changes.outputs.ui == 'true' + if: github.event_name == 'workflow_dispatch' || steps.changes.outputs.ui == 'true' || steps.changes.outputs.nonui_js == 'true' run: npm ci --prefer-offline --no-audit working-directory: 'cmd/ui' @@ -264,6 +271,18 @@ jobs: run: npm run lint working-directory: 'cmd/ui' + - name: 'Run ESLint on Non-UI JS Scripts' + if: github.event_name == 'workflow_dispatch' || steps.changes.outputs.nonui_js == 'true' + run: | + mapfile -d '' -t NONUI_JS_FILES < <(git -c core.quotePath=false ls-files -z -- \ + ':(glob).github/scripts/**/*.js' \ + ':(glob)tools/nightly_tests/**/*.js' \ + ':(glob)cmd/experiment_state_server/static/**/*.js') + wait "$!" + if [ "${#NONUI_JS_FILES[@]}" -gt 0 ]; then + ./cmd/ui/node_modules/.bin/eslint --no-config-lookup -c tools/lint/eslint.nonui.config.mjs -- "${NONUI_JS_FILES[@]}" + fi + # 3a. GitHub Actions & Nightly JS Script Tests (node:test, no dependencies) - name: 'Set up Node.js for JS Script Tests' if: github.event_name == 'workflow_dispatch' || steps.changes.outputs.github_scripts == 'true' || steps.changes.outputs.examples_coverage == 'true' diff --git a/docs/developer.md b/docs/developer.md index 50047a48b..90ae899c3 100644 --- a/docs/developer.md +++ b/docs/developer.md @@ -255,7 +255,7 @@ Go source files (`.go`) are formatted using standard **`gofmt`**: ### 3. UI Code (`prettier` & `eslint`) Angular and TypeScript frontend code under `cmd/ui` is formatted and linted using **Prettier** and **ESLint**: * **`prettier`**: Enforces consistent styling on `.ts`, `.html`, and `.scss` files (`npm run format:check`). -* **`eslint`**: Enforces Angular best practices, import sorting, and TypeScript strictness (`npm run lint`). +* **`eslint`**: Enforces Angular best practices, import sorting, and TypeScript strictness under `cmd/ui` (`npm run lint`). Non-UI `.js` scripts use the separate config documented in section 8. ### 4. Bazel Build Files (`buildifier`) Bazel build graphs (`BUILD`, `BUILD.bazel`, `WORKSPACE`, `MODULE.bazel`, and `.bzl` files) are formatted and analyzed using **`buildifier`**: @@ -277,10 +277,11 @@ Tracked UI templates, UI test specs, and GitHub Actions script tests are checked * Enforces `*.ng.html` for HTML files under `cmd/ui/` (requiring templates under `cmd/ui/app/` and allowing root `cmd/ui/index.html`), `*_test.ts` Karma unit test specs under `cmd/ui/app/` (rejecting `*.spec.ts`, `*_spec.ts`, and `*.test.ts` repo-wide outside `cmd/ui/screenshot_tests/`), `*.screenshot.spec.ts` Playwright specs under `cmd/ui/screenshot_tests/`, and top-level `*.test.js` Node workflow script tests under `.github/scripts/`. * Prevents misnamed test specs (such as `foo.spec.ts` or `.github/scripts/foo_test.js`) from silently compiling without executing in Karma or CI; runs in both the pre-commit hook and CI. -### 8. GitHub Actions Workflows & Shell Scripts (`actionlint` & `shellcheck`) -Workflows and tracked shell scripts are validated in CI ([`.github/workflows/lint.yml`](../.github/workflows/lint.yml)) and locally in `.githooks/pre-commit`: +### 8. GitHub Actions Workflows, Shell Scripts & Non-UI JS (`actionlint`, `shellcheck`, `eslint`) +Workflows, tracked shell scripts, and non-UI JavaScript scripts are validated in CI ([`.github/workflows/lint.yml`](../.github/workflows/lint.yml)) and locally in `.githooks/pre-commit`: * **`actionlint`**: Validates `.github/workflows/*.{yml,yaml}` and integrates `shellcheck` across workflow `run:` blocks. * **`shellcheck`**: Validates tracked `*.sh` and `*.bash` scripts with `shellcheck -S style -e SC1091`. +* **Non-UI JS ESLint**: Runs `./cmd/ui/node_modules/.bin/eslint --no-config-lookup -c tools/lint/eslint.nonui.config.mjs` recursively across `.github/scripts/**/*.js`, `tools/nightly_tests/**/*.js`, and `cmd/experiment_state_server/static/**/*.js` via [`tools/lint/eslint.nonui.config.mjs`](../tools/lint/eslint.nonui.config.mjs) (reusing the ESLint installation in `cmd/ui`). * **Node JS Script Tests**: Runs `node --test .github/scripts/*.test.js` and `node --test tools/nightly_tests/*_test.js` in CI when workflow scripts or nightly test utilities change. --- @@ -295,7 +296,7 @@ Developers can run the pre-commit hook locally in two modes: ``` Automatically formats files and adds missing headers. Formatter fixes to files that were fully staged before the hook ran are restaged automatically ([`tools/lint/precommit_autostage.py`](../tools/lint/precommit_autostage.py)); fixes to partially staged, unstaged, or unrelated files fail the hook so you can review and stage them. Set `XMC_PRECOMMIT_NO_AUTOSTAGE=1` to disable auto-restaging. Affected Bazel unit tests are skipped when every staged path is Markdown or under `docs/`. Which checks run depends on what is staged: * **Per-file linters and formatters** (Ruff, Prettier, ESLint, `terraform fmt`, `addlicense`) only process staged files. - * **Cross-file checks** (mypy, TFLint, `helm lint`, Trivy) also run when a staged delete or rename removes a file they cover. mypy also runs when `pyproject.toml` or a `.gitignore` changes, and TFLint when `.tflint.hcl` changes. The agent doc path check runs on any staged delete or rename. + * **Cross-file checks** (mypy, TFLint, `helm lint`, Trivy) also run when a staged delete or rename removes a file they cover. mypy also runs when `pyproject.toml` or a `.gitignore` changes, and TFLint when `.tflint.hcl` changes. The agent doc path check runs on any staged delete or rename. Staging `tools/lint/eslint.nonui.config.mjs` triggers a full non-UI JS ESLint run across tracked `.github/scripts/**/*.js`, `tools/nightly_tests/**/*.js`, and `cmd/experiment_state_server/static/**/*.js` files (in `fix` mode, a missing `cmd/ui/node_modules` warns and skips, while `--check` fails closed). * **mypy** runs bare `mypy`, so `[tool.mypy]` in `pyproject.toml` is the single source of truth for the checked files in CI and in both hook modes. It checks every Python file in the repository, so new directories are covered automatically and edits that break unchanged importers fail locally. Two lists deny files: `.gitignore` (`exclude_gitignore`, mypy 1.16 or later) skips local-only directories such as virtualenvs, Bazel output and the `cluster-toolkit` clone, and `exclude` skips tracked files. Untracked `.py` files that `.gitignore` does not cover are checked too, so remove or fix stray scratch files before committing. * **Check-only mode (mirrors CI validation)**: diff --git a/tools/README.md b/tools/README.md index 6c4435ed8..fb9230a49 100644 --- a/tools/README.md +++ b/tools/README.md @@ -91,6 +91,7 @@ Offline syntax walkers and static validation tooling: - [`precommit_gates.py`](lint/precommit_gates.py): Declarative runner (`--mode fix|check`) and shared git/execution primitives for pre-commit Python lint gates (`check_legacy_imports`, `check_examples_coverage`, `check_matrix_contract`, `check_agent_doc_paths`, `check_file_naming`, `check_ui_selector_contract`). - [`precommit_deploy_checks.py`](lint/precommit_deploy_checks.py): Pre-commit infrastructure, Helm, and Trivy security gate runner (`--mode fix|check`) covering `deploy/` and `test/helm/`. - [`precommit_runner.py`](lint/precommit_runner.py): Top-level pure-stdlib Python orchestrator invoked by `.githooks/pre-commit` for language, UI, deploy, and affected-test gates. +- [`eslint.nonui.config.mjs`](lint/eslint.nonui.config.mjs): ESLint 10 flat configuration enforcing baseline JS correctness (`no-empty`, `no-unused-vars`, `eqeqeq`, `complexity: 15`, `max-lines-per-function: 80`) recursively across `.github/scripts/**/*.js`, `tools/nightly_tests/**/*.js`, and `cmd/experiment_state_server/static/**/*.js`; invoked by `precommit_runner.py` (`check_nonui_js`) and `.github/workflows/lint.yml`. ### 8. [`tools/migrations/components/`](migrations/components/README.md) (One-Time Component Cutover) Temporary migration side script (`migrate.py`) for transitioning existing umbrella-managed metrics deployments to the `stable-2026a` Component profile (`prepare`, `handoff [--undo]`, `finalize`). See [`docs/migrations/components.md`](../docs/migrations/components.md). diff --git a/tools/lint/eslint.nonui.config.mjs b/tools/lint/eslint.nonui.config.mjs new file mode 100644 index 000000000..fc47b3928 --- /dev/null +++ b/tools/lint/eslint.nonui.config.mjs @@ -0,0 +1,136 @@ +/** + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const BASE_RULES = { + 'constructor-super': 'error', + 'for-direction': 'error', + 'getter-return': 'error', + 'no-async-promise-executor': 'error', + 'no-case-declarations': 'error', + 'no-class-assign': 'error', + 'no-compare-neg-zero': 'error', + 'no-cond-assign': 'error', + 'no-const-assign': 'error', + 'no-constant-binary-expression': 'error', + 'no-constant-condition': 'error', + 'no-control-regex': 'error', + 'no-debugger': 'error', + 'no-delete-var': 'error', + 'no-dupe-args': 'error', + 'no-dupe-class-members': 'error', + 'no-dupe-else-if': 'error', + 'no-dupe-keys': 'error', + 'no-duplicate-case': 'error', + 'no-empty': ['error', { allowEmptyCatch: false }], + 'no-empty-pattern': 'error', + 'no-ex-assign': 'error', + 'no-extra-boolean-cast': 'error', + 'no-fallthrough': 'error', + 'no-func-assign': 'error', + 'no-global-assign': 'error', + 'no-import-assign': 'error', + 'no-invalid-regexp': 'error', + 'no-irregular-whitespace': 'error', + 'no-loss-of-precision': 'error', + 'no-new-native-nonconstructor': 'error', + 'no-obj-calls': 'error', + 'no-prototype-builtins': 'error', + 'no-redeclare': 'error', + 'no-self-assign': 'error', + 'no-setter-return': 'error', + 'no-shadow-restricted-names': 'error', + 'no-sparse-arrays': 'error', + 'no-this-before-super': 'error', + 'no-undef': 'error', + 'no-unexpected-multiline': 'error', + 'no-unreachable': 'error', + 'no-unsafe-finally': 'error', + 'no-unsafe-negation': 'error', + 'no-unused-labels': 'error', + 'no-unused-vars': [ + 'error', + { + argsIgnorePattern: '^_', + varsIgnorePattern: '^_', + caughtErrorsIgnorePattern: '^_', + }, + ], + 'no-useless-catch': 'error', + 'no-useless-escape': 'error', + 'no-var': 'error', + 'prefer-const': 'error', + 'use-isnan': 'error', + 'valid-typeof': 'error', + eqeqeq: ['error', 'always'], + complexity: ['error', 15], + 'max-lines-per-function': [ + 'error', + { max: 80, skipBlankLines: true, skipComments: true }, + ], +}; + +export default [ + { + files: ['.github/scripts/**/*.js', 'tools/nightly_tests/**/*.js'], + languageOptions: { + ecmaVersion: 2022, + sourceType: 'commonjs', + globals: { + Buffer: 'readonly', + console: 'readonly', + module: 'readonly', + process: 'readonly', + require: 'readonly', + URL: 'readonly', + URLSearchParams: 'readonly', + fetch: 'readonly', + setTimeout: 'readonly', + clearTimeout: 'readonly', + __dirname: 'readonly', + __filename: 'readonly', + }, + }, + rules: { + ...BASE_RULES, + }, + }, + { + files: ['cmd/experiment_state_server/static/**/*.js'], + languageOptions: { + ecmaVersion: 2022, + sourceType: 'script', + globals: { + window: 'readonly', + document: 'readonly', + fetch: 'readonly', + console: 'readonly', + location: 'readonly', + URL: 'readonly', + setTimeout: 'readonly', + }, + }, + rules: { + ...BASE_RULES, + }, + }, + { + files: ['**/*.test.js', '**/*_test.js'], + rules: { + 'max-lines-per-function': 'off', + complexity: 'off', + }, + }, +]; diff --git a/tools/lint/precommit_runner.py b/tools/lint/precommit_runner.py index 4ffda8b7c..0125c2b7d 100755 --- a/tools/lint/precommit_runner.py +++ b/tools/lint/precommit_runner.py @@ -19,6 +19,7 @@ import argparse import os +import re import shutil import sys import tempfile @@ -286,6 +287,41 @@ def check_shell(ctx: gates.RunContext) -> None: ctx.fail("shellcheck failed.") +_NONUI_JS_GLOBS = ( + ":(glob).github/scripts/**/*.js", + ":(glob)tools/nightly_tests/**/*.js", + ":(glob)cmd/experiment_state_server/static/**/*.js", +) +_NONUI_JS_PATTERN = ( + r"^(\.github/scripts|tools/nightly_tests|cmd/experiment_state_server/static)/.*\.js$" +) + + +def check_nonui_js(ctx: gates.RunContext) -> None: + """Runs ESLint on non-UI JavaScript files using `tools/lint/eslint.nonui.config.mjs`.""" + if ctx.mode == "check" or gates.staged_paths_match( + "ACMR", r"^tools/lint/eslint\.nonui\.config\.mjs$" + ): + files = gates.git_ls_files(_NONUI_JS_GLOBS) + else: + regex = re.compile(_NONUI_JS_PATTERN) + files = [p for p in gates.git_staged_paths("ACMR") if regex.search(p)] + if not files: + return + eslint = gates.resolve_ui_binary(ctx, "eslint") + if not eslint: + return + print("--> Running Non-UI JS Linter (ESLint)...") + gates.run_mode_command( + ctx, + [eslint, "--no-config-lookup", "-c", "tools/lint/eslint.nonui.config.mjs"], + check_args=[], + fix_args=["--fix"], + failure_msg="Non-UI JS ESLint check failed.", + files=files, + ) + + def _autostage(subcommand: str, state_path: str) -> bool: cmd = [sys.executable, "tools/lint/precommit_autostage.py", subcommand, "--state", state_path] return gates.exec_cmd(cmd, quiet_out=True).returncode == 0 @@ -311,6 +347,7 @@ def _run_linter_phase(mode: str) -> bool: deploy_checks.check_deploy, check_workflows, check_shell, + check_nonui_js, ): fn(ctx) if mode == "fix": diff --git a/tools/lint/precommit_runner_test.py b/tools/lint/precommit_runner_test.py index 29eec1ad2..814d5c9a5 100644 --- a/tools/lint/precommit_runner_test.py +++ b/tools/lint/precommit_runner_test.py @@ -397,6 +397,129 @@ def test_check_shell_chunking_failures_and_missing_binary(self): self.assertTrue(ctx_sh_missing.failed) self.assertIn(runner._SHELLCHECK_HINT, buf.getvalue()) + def test_check_nonui_js(self): + ctx_cfg = gates.RunContext(mode="fix") + with ( + mock.patch.object( + gates, "git_staged_paths", return_value=["tools/lint/eslint.nonui.config.mjs"] + ), + mock.patch.object( + gates, "git_ls_files", return_value=[".github/scripts/a.js"] + ) as cfg_ls, + mock.patch.object(gates, "resolve_ui_binary", return_value="eslint"), + mock.patch.object( + gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 0, "", "") + ) as exec_mock, + mock.patch("sys.stdout", new_callable=io.StringIO), + ): + runner.check_nonui_js(ctx_cfg) + self.assertFalse(ctx_cfg.failed) + cfg_ls.assert_called_once_with(runner._NONUI_JS_GLOBS) + self.assertEqual(exec_mock.call_args.kwargs["cwd"], gates.REPO_ROOT) + self.assertEqual( + exec_mock.call_args.args[0], + [ + "eslint", + "--no-config-lookup", + "-c", + "tools/lint/eslint.nonui.config.mjs", + "--fix", + ".github/scripts/a.js", + ], + ) + + ctx_chk = gates.RunContext(mode="check") + with ( + mock.patch.object( + gates, "git_ls_files", return_value=[".github/scripts/a.js"] + ) as chk_ls, + mock.patch.object(gates, "resolve_ui_binary", return_value="eslint"), + mock.patch.object( + gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 0, "", "") + ) as chk_exec, + mock.patch("sys.stdout", new_callable=io.StringIO), + ): + runner.check_nonui_js(ctx_chk) + self.assertFalse(ctx_chk.failed) + chk_ls.assert_called_once_with(runner._NONUI_JS_GLOBS) + self.assertEqual(chk_exec.call_args.kwargs["cwd"], gates.REPO_ROOT) + self.assertEqual( + chk_exec.call_args.args[0], + [ + "eslint", + "--no-config-lookup", + "-c", + "tools/lint/eslint.nonui.config.mjs", + ".github/scripts/a.js", + ], + ) + + ctx_fail = gates.RunContext(mode="fix") + staged_paths = [ + ".github/scripts/a.js", + "tools/nightly_tests/sub/b.js", + "cmd/experiment_state_server/static/c.js", + "cmd/ui/b.js", + ] + with ( + mock.patch.object(gates, "git_staged_paths", return_value=staged_paths), + mock.patch.object(gates, "resolve_ui_binary", return_value="eslint"), + mock.patch.object( + gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 1, "", "") + ) as fail_exec, + mock.patch("sys.stdout", new_callable=io.StringIO), + ): + runner.check_nonui_js(ctx_fail) + self.assertTrue(ctx_fail.failed) + self.assertEqual(fail_exec.call_args.kwargs["cwd"], gates.REPO_ROOT) + self.assertEqual( + fail_exec.call_args.args[0], + [ + "eslint", + "--no-config-lookup", + "-c", + "tools/lint/eslint.nonui.config.mjs", + "--fix", + ".github/scripts/a.js", + "tools/nightly_tests/sub/b.js", + "cmd/experiment_state_server/static/c.js", + ], + ) + + def test_check_nonui_js_skips_and_missing_binary(self): + ctx_skip = gates.RunContext(mode="fix") + with ( + mock.patch.object(gates, "git_staged_paths", return_value=["cmd/ui/b.js", "README.md"]), + mock.patch.object(gates, "resolve_ui_binary") as skip_resolve, + mock.patch.object(gates, "exec_cmd") as skip_exec, + ): + runner.check_nonui_js(ctx_skip) + self.assertFalse(ctx_skip.failed) + skip_resolve.assert_not_called() + skip_exec.assert_not_called() + + ctx_missing_chk = gates.RunContext(mode="check") + with ( + mock.patch.object(gates, "git_ls_files", return_value=[".github/scripts/a.js"]), + mock.patch.object(gates.shutil, "which", return_value=None), + mock.patch.object(gates, "exec_cmd") as missing_chk_exec, + mock.patch("sys.stdout", new_callable=io.StringIO), + ): + runner.check_nonui_js(ctx_missing_chk) + self.assertTrue(ctx_missing_chk.failed) + missing_chk_exec.assert_not_called() + + ctx_missing_fix = gates.RunContext(mode="fix") + with ( + mock.patch.object(gates, "git_staged_paths", return_value=[".github/scripts/a.js"]), + mock.patch.object(gates.shutil, "which", return_value=None), + mock.patch.object(gates, "exec_cmd") as missing_fix_exec, + mock.patch("sys.stdout", new_callable=io.StringIO), + ): + runner.check_nonui_js(ctx_missing_fix) + self.assertFalse(ctx_missing_fix.failed) + missing_fix_exec.assert_not_called() + class OrchestrationAndMainTest(unittest.TestCase): def test_linter_phase_blocks_on_snapshot_fail_no_autostage_and_restage_fail(self): @@ -420,6 +543,7 @@ def test_linter_phase_blocks_on_snapshot_fail_no_autostage_and_restage_fail(self mock.patch.object(deploy_checks, "check_deploy"), mock.patch.object(runner, "check_workflows"), mock.patch.object(runner, "check_shell"), + mock.patch.object(runner, "check_nonui_js"), mock.patch.object( gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 1, "", "") ), @@ -440,6 +564,7 @@ def test_linter_phase_blocks_on_snapshot_fail_no_autostage_and_restage_fail(self mock.patch.object(deploy_checks, "check_deploy"), mock.patch.object(runner, "check_workflows"), mock.patch.object(runner, "check_shell"), + mock.patch.object(runner, "check_nonui_js"), mock.patch.object(runner, "_autostage", side_effect=[True, False]) as auto_mock, mock.patch("sys.stdout", new_callable=io.StringIO) as buf, ):