Skip to content

3D engine that AI agents drive

Make three gate scripts fail when they should (#2567)

Nguyen Xuan An5c0e5119f0c9
diff --git a/changes/2567.md b/changes/2567.md
new file mode 100644
index 00000000..42f3c7d4
--- /dev/null
+++ b/changes/2567.md
@@ -0,0 +1 @@
+- `check:tool-schemas` now fails on a portable schema finding, `check:change-note` fails on a note with no bullet, and the coverage gate fails when branch or function records are missing (#2567, #2568, #2570)
diff --git a/docs/ci.md b/docs/ci.md
index 0113cf3f..e0deea5b 100644
--- a/docs/ci.md
+++ b/docs/ci.md
@@ -50,7 +50,7 @@ No other test took 60 s or more. 300 s is 2.3 times the slowest, and a hang stil
## Coverage
-Coverage comes from Node's built-in `--experimental-test-coverage`. The `coverage` job fails when line or function coverage falls more than 0.5 points, or branch coverage more than 1.5 points, below `config/coverage-baseline.json` (branch totals move by up to about 0.8 points between runs of the same commit). Raise the baseline by editing that file; lower it only on purpose and say why in the pull request. In CI the gate runs with `--expect-shards 12` and refuses to merge unless the files are exactly `shard-1.info` to `shard-12.info`, each with source files in it. A shard that is missing, left over from another shard count, doubled or empty fails with the problems listed and "Re-run all jobs". This is what a `gh run rerun --failed` after a hung shard used to turn into a wrong total of 91.66 % (#1320); rerun all jobs instead. To reproduce locally: `npm run test:coverage -- --test-reporter=lcov --test-reporter-destination=coverage/lcov.info`, then `npm run coverage:gate -- coverage`.
+Coverage comes from Node's built-in `--experimental-test-coverage`. The `coverage` job fails when line or function coverage falls more than 0.5 points, or branch coverage more than 1.5 points, below `config/coverage-baseline.json` (branch totals move by up to about 0.8 points between runs of the same commit). Raise the baseline by editing that file; lower it only on purpose and say why in the pull request. In CI the gate runs with `--expect-shards 12` and refuses to merge unless the files are exactly `shard-1.info` to `shard-12.info`, each with source files in it. The gate also fails when the files hold no line, branch or function records at all, since that figure would read 100 %. A shard that is missing, left over from another shard count, doubled or empty fails with the problems listed and "Re-run all jobs". This is what a `gh run rerun --failed` after a hung shard used to turn into a wrong total of 91.66 % (#1320); rerun all jobs instead. To reproduce locally: `npm run test:coverage -- --test-reporter=lcov --test-reporter-destination=coverage/lcov.info`, then `npm run coverage:gate -- coverage`.
## Add a job or a smoke
diff --git a/scripts/check-change-note.mjs b/scripts/check-change-note.mjs
index 43c1af47..30764e23 100644
--- a/scripts/check-change-note.mjs
+++ b/scripts/check-change-note.mjs
@@ -6,6 +6,7 @@ import { pathToFileURL } from 'node:url';
import { resolve } from 'node:path';
import { isVersionBump, planChecks } from './preflight.mjs';
import { readReleaseProblem } from './check-release-changelog.mjs';
+import { isFragmentName, parseEntries } from './changelog-fragments.mjs';
import { pullRequestBaseOf } from './size-base.mjs';
const gateNames = new Set(['protected files', 'change note', 'release changelog']);
@@ -17,6 +18,18 @@ export function failingGatesFor(changedPaths, { release = false, releaseProblem
.map(check => ({ name: check.name, failure: check.failure, fix: check.fix }));
}
+// A note the fold writes nothing for: no "- " bullet, or only whitespace. The fold skips it, so the release would lose the change.
+// `readNote` returns the text of an added path. Pure apart from that, so tests can call it without git.
+export function emptyNoteGates(addedPaths, readNote) {
+ const notePaths = addedPaths.filter((path) => {
+ const [folder, name, ...deeper] = path.replaceAll('\\', '/').split('/');
+ return folder === 'changes' && name !== undefined && deeper.length === 0 && isFragmentName(name);
+ });
+ return notePaths
+ .filter(path => parseEntries(readNote(path), path).length === 0)
+ .map(path => ({ name: 'change note', failure: `${path} has no "- " bullet, so changes:fold would drop it.`, fix: 'Write one bullet in it, such as "- `bar` takes a `foo` option (#123)" (changes/README.md).' }));
+}
+
function git(args) {
const result = spawnSync('git', args, { encoding: 'utf8' });
if (result.status !== 0) throw new Error(`git ${args.join(' ')} failed: ${result.stderr.trim()}`);
@@ -37,6 +50,7 @@ async function main() {
if (release) console.log('The package version changed, so this is a release: the protected file and change note gates do not apply, the release changelog gate does.');
const releaseProblem = release ? await readReleaseProblem(process.cwd()) : null;
const failing = failingGatesFor(changedPaths, { release, releaseProblem, addedPaths });
+ if (!release) failing.push(...emptyNoteGates(addedPaths, path => git(['show', `HEAD:${path}`])));
for (const gate of failing) {
console.error(`FAIL ${gate.name}: ${gate.failure}\n Fix: ${gate.fix}`);
}
diff --git a/scripts/check-tool-schemas.mjs b/scripts/check-tool-schemas.mjs
index e998b033..d69afd82 100644
--- a/scripts/check-tool-schemas.mjs
+++ b/scripts/check-tool-schemas.mjs
@@ -217,11 +217,18 @@ export function summarizeFindings(findings) {
return summary;
}
-if (import.meta.url === pathToFileURL(process.argv[1]).href) {
- const tools = await listTools();
+// What the command prints and the exit code it ends with. A portable finding is a bug in our schema, so it fails the gate.
+export function reportToolSchemas(tools) {
const findings = checkToolSchemas(tools);
- console.log(JSON.stringify({ tools: tools.length, summary: summarizeFindings(findings) }, null, 2));
- for (const item of findings.filter(candidate => candidate.profile === 'portable')) {
- console.log(`${item.rule} ${item.path}: ${item.detail}`);
- }
+ const portable = findings.filter(candidate => candidate.profile === 'portable');
+ const lines = [JSON.stringify({ tools: tools.length, summary: summarizeFindings(findings) }, null, 2)];
+ for (const item of portable) lines.push(`${item.rule} ${item.path}: ${item.detail}`);
+ if (portable.length > 0) lines.push(`FAIL ${portable.length} portable finding(s). Fix: change the schema each line names until every client accepts it.`);
+ return { lines, exitCode: portable.length > 0 ? 1 : 0 };
+}
+
+if (import.meta.url === pathToFileURL(process.argv[1]).href) {
+ const { lines, exitCode } = reportToolSchemas(await listTools());
+ console.log(lines.join('\n'));
+ process.exitCode = exitCode;
}
diff --git a/scripts/coverage-gate.mjs b/scripts/coverage-gate.mjs
index ee68d290..c723e7dd 100644
--- a/scripts/coverage-gate.mjs
+++ b/scripts/coverage-gate.mjs
@@ -161,6 +161,11 @@ export function totalsOf(files) {
return { totals, percentages };
}
+// The metrics the lcov files hold no records of. totalsOf reads those as 100%, which would hide a drop to nothing.
+export function unmeasuredMetrics(totals) {
+ return METRICS.filter(metric => totals[metric][1] === 0);
+}
+
export function compareWithBaseline(percentages, baseline) {
return METRICS.map(metric => ({
metric,
@@ -219,6 +224,11 @@ function main() {
}
if (mergedOut) writeFileSync(mergedOut, toLcov(files));
const { totals, percentages } = totalsOf(files);
+ const unmeasured = unmeasuredMetrics(totals);
+ if (unmeasured.length > 0) {
+ console.error(`The lcov files hold no ${unmeasured.join(' or ')} records, so that figure would read 100%. Rerun the tests with the same coverage reporters and pass the lcov they write.`);
+ process.exit(1);
+ }
const baseline = JSON.parse(readFileSync(baselinePath, 'utf8'));
const results = compareWithBaseline(percentages, baseline);
diff --git a/tests/gate-script-false-greens.test.js b/tests/gate-script-false-greens.test.js
new file mode 100644
index 00000000..cf7a70c2
--- /dev/null
+++ b/tests/gate-script-false-greens.test.js
@@ -0,0 +1,75 @@
+// Gate scripts that exited 0 when they should fail: a tool schema finding, an empty change note and an lcov with no branch records.
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { spawnSync } from 'node:child_process';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const root = fileURLToPath(new URL('..', import.meta.url));
+
+function runGit(folder, ...args) {
+ const result = spawnSync('git', ['-c', 'user.name=test', '-c', '[email protected]', '-c', 'commit.gpgsign=false', ...args], { cwd: folder, encoding: 'utf8' });
+ assert.equal(result.status, 0, result.stderr);
+}
+
+test('check-tool-schemas ends with exit code 1 when a tool breaks a portable rule', async () => {
+ const { reportToolSchemas } = await import('../scripts/check-tool-schemas.mjs');
+ const goodTool = { name: 'scene_ok', description: 'Does a thing.', inputSchema: { type: 'object', properties: {}, additionalProperties: false } };
+ const badTool = { ...goodTool, name: 'scene ok with spaces' };
+ assert.equal(reportToolSchemas([goodTool]).exitCode, 0);
+ const report = reportToolSchemas([goodTool, badTool]);
+ assert.equal(report.exitCode, 1);
+ assert.match(report.lines.join('\n'), /tool-name/);
+ assert.match(report.lines.at(-1), /^FAIL 1 portable finding\(s\)\. Fix:/);
+});
+
+test('an added change note with no bullet fails check-change-note, because the fold would drop it', async () => {
+ const folder = mkdtempSync(join(tmpdir(), 'empty-note-gate-'));
+ try {
+ runGit(folder, 'init', '-q');
+ mkdirSync(join(folder, 'changes'));
+ mkdirSync(join(folder, 'src'));
+ writeFileSync(join(folder, 'package.json'), '{"version":"1.0.0"}\n');
+ writeFileSync(join(folder, 'src', 'a.js'), 'export const a = 1;\n');
+ runGit(folder, 'add', '.');
+ runGit(folder, 'commit', '-q', '-m', 'base');
+ writeFileSync(join(folder, 'src', 'a.js'), 'export const a = 2;\n');
+ writeFileSync(join(folder, 'changes', '7.md'), 'Fixed a thing.\n');
+ runGit(folder, 'add', '.');
+ runGit(folder, 'commit', '-q', '-m', 'change');
+ const failing = spawnSync(process.execPath, [join(root, 'scripts', 'check-change-note.mjs'), '--base', 'HEAD~1'], { cwd: folder, encoding: 'utf8' });
+ assert.equal(failing.status, 1, failing.stdout + failing.stderr);
+ assert.match(failing.stderr, /changes\/7\.md has no "- " bullet/);
+ writeFileSync(join(folder, 'changes', '7.md'), '- Fixed a thing (#7)\n');
+ runGit(folder, 'add', '.');
+ runGit(folder, 'commit', '-q', '-m', 'bullet');
+ const passing = spawnSync(process.execPath, [join(root, 'scripts', 'check-change-note.mjs'), '--base', 'HEAD~2'], { cwd: folder, encoding: 'utf8' });
+ assert.equal(passing.status, 0, passing.stdout + passing.stderr);
+ } finally {
+ rmSync(folder, { recursive: true, force: true });
+ }
+});
+
+test('emptyNoteGates reads only top-level change notes and skips size notes', async () => {
+ const { emptyNoteGates } = await import('../scripts/check-change-note.mjs');
+ const texts = { 'changes/1.md': '- ok (#1)', 'changes/2.md': ' \n', 'changes/3.size.md': 'no bullet', 'changes\\4.md': 'prose', 'changes/sub/5.md': 'prose' };
+ const gates = emptyNoteGates(Object.keys(texts), path => texts[path]);
+ assert.deepEqual(gates.map(gate => gate.failure.split(' ')[0]), ['changes/2.md', 'changes\\4.md']);
+});
+
+test('an lcov with lines but no branch or function records fails the coverage gate', () => {
+ const folder = mkdtempSync(join(tmpdir(), 'coverage-no-branches-'));
+ try {
+ const lcovPath = join(folder, 'lines-only.lcov');
+ const baselinePath = join(folder, 'baseline.json');
+ writeFileSync(lcovPath, ['SF:src/a.js', 'DA:1,1', 'DA:2,1', 'end_of_record', ''].join('\n'));
+ writeFileSync(baselinePath, JSON.stringify({ lines: 90, branches: 80, functions: 90 }));
+ const result = spawnSync(process.execPath, [join(root, 'scripts', 'coverage-gate.mjs'), lcovPath, '--baseline', baselinePath], { encoding: 'utf8' });
+ assert.equal(result.status, 1, result.stdout + result.stderr);
+ assert.match(result.stderr, /no branches or functions records/);
+ } finally {
+ rmSync(folder, { recursive: true, force: true });
+ }
+});