diff --git a/.ai/contexts/fast-check.md b/.ai/contexts/fast-check.md index a0533b310..2c231c23d 100644 --- a/.ai/contexts/fast-check.md +++ b/.ai/contexts/fast-check.md @@ -18,11 +18,27 @@ skip it. Reference scanning skips initial YAML front matter, retaining body line numbers. Coverage declarations are validated by `scripts/check-docs.js --validate` in `Taskfile.yaml` (`task check`), by the real-checkout regression in `test/check-docs.test.js`, and by the PR docs gate. The PR gate requires a body -change in a matching owner doc for non-hubs, comparing the PR head with the +change in a matching owner doc for covered non-hubs, comparing the PR head with the merge base, and prints uncovered files. The explicit `HUB_FILES` set in -`scripts/check-docs.js` accepts a non-whitespace body change in any eligible -coverage doc. Metadata-only and whitespace-only edits do not count, and the -uncovered-file fallback excludes ADRs, product pages, `_issues.md` and READMEs. +`scripts/check-docs.js` and the uncovered-file fallback accept a qualifying body +change in any eligible coverage doc; ADRs, product pages, `_issues.md` and +READMEs are ineligible. A doc with a previous version, including a Git-detected +rename or copy, qualifies when its normalized whole body differs from that +version. Initial YAML front matter is stripped, CRLF is normalized to LF, and +`visible` removes JavaScript regex `\s` whitespace plus Unicode `Cc` (control) +and `Cf` (format) characters, wherever they occur. Metadata, whitespace, control +and format characters alone never count as a body change. + +A doc without a previous version qualifies only if at least one nonempty +normalized body line is absent from every deleted eligible doc's body lines. +The deleted-doc set includes non-owners of the changed app file. Lines are split +on LF after stripping front matter and normalizing CRLF, then normalized with +`visible`; empty results are ignored. Splitting or merging deleted docs without +such a line does not qualify. One new visible line does, and with no deleted +docs any visible body line still qualifies. Retained docs do not participate in +this comparison. This is exact normalized-line membership, not semantic prose +comparison: reordering or repeating existing lines adds nothing, but rewrapping +text across lines can produce a qualifying line even without new words. Root patterns must be exact paths; each file still has at most four owners. `public/resume-guard.js` keeps the three contexts describing its decisions and wording plus the session-restore user page as its four owners. A rename in JavaScript that breaks a doc is caught by `task check` and diff --git a/scripts/check-docs.js b/scripts/check-docs.js index 9e068c510..ea069010b 100644 --- a/scripts/check-docs.js +++ b/scripts/check-docs.js @@ -90,9 +90,11 @@ function checkDocs({ files, labels, body = '', documents = {}, previousDocuments const uncovered = [...new Set([...trackedFiles, ...files])].filter(isAppPath).filter(file => covering(file).length === 0); const report = ['Uncovered app files (' + uncovered.length + '), advisory:', ...uncovered.map(file => ' ' + file)]; const visible = text => docBody(text).replace(/[\s\p{Cc}\p{Cf}]/gu, ''); - const movedBodies = new Set(Object.keys(previousDocuments).filter(doc => !Object.hasOwn(documents, doc)).map(doc => visible(previousDocuments[doc]))); + const visibleLines = text => docBody(text).split('\n').map(visible).filter(Boolean); + const deletedLines = new Set(Object.keys(previousDocuments).filter(doc => !Object.hasOwn(documents, doc)) + .flatMap(doc => visibleLines(previousDocuments[doc]))); const changedBody = doc => Object.hasOwn(documents, doc) && visible(documents[doc]) !== visible(previousDocuments[doc]) - && (Object.hasOwn(previousDocuments, doc) || !movedBodies.has(visible(documents[doc]))); + && (Object.hasOwn(previousDocuments, doc) || visibleLines(documents[doc]).some(line => !deletedLines.has(line))); const hasChangedCoverageDoc = files.some(file => isCoverageDoc(file) && trackedFiles.includes(file) && changedBody(file)); const reason = body.split(/\r?\n/).some(line => { const match = line.match(/^[ \t]*(?:[-*+][ \t]+)?no docs:[ \t]*(\S.*)$/i); diff --git a/test/check-docs.test.js b/test/check-docs.test.js index 2e3863382..5d1f87dcd 100644 --- a/test/check-docs.test.js +++ b/test/check-docs.test.js @@ -63,7 +63,7 @@ function runCli(args, { files = ['main.js'], labels = '[]', body = '', documents if (command === failCommand) throw new Error(`${command} unavailable`); assert.ok(['git', 'gh'].includes(command), command); if (command === 'git' && argv[0] === 'merge-base') return 'merge-base-sha'; - if (command === 'git' && argv[0] === 'diff' && argv.includes('--name-status')) return renames.map(([status, from, to]) => status + '\0' + from + '\0' + to + '\0').join(''); + if (command === 'git' && argv[0] === 'diff' && argv.includes('--name-status')) return renames.map(entry => entry.join('\0') + '\0').join(''); if (command === 'git' && argv[0] === 'ls-tree') return (argv.at(-1) === 'head-sha' ? trackedFiles : Object.keys(previousDocuments)).join('\0'); if (command === 'git' && argv[0] === 'show') { const [ref, file] = argv[1].split(':'); @@ -592,3 +592,70 @@ test('GIVEN a stamp-only edit WHEN covered code changes THEN the DoD gate still assert.doesNotMatch(result.output, /front matter must/); assert.equal(runCli([...REFS, '--labels', '[]'], { ...options, documents: { [doc]: documents[doc] + '\nCorrected behavior.' } }).code, 0); }); + +const ownerDoc = body => '---\ncovers: ["public/terminal.js"]\n---\n' + body; +const movementCases = [ + { name: 'reorder and repeat deleted lines', before: { 'docs/old.md': ownerDoc('Section A\nSection B') }, + after: { 'docs/new.md': ownerDoc('Section B\nSection A\nSection A') }, ok: false }, + { name: 'rewrap deleted words onto a new line', before: { 'docs/old.md': ownerDoc('Section A\nSection B') }, + after: { 'docs/new.md': ownerDoc('Section A Section B') }, ok: true }, + { name: 'split A+B into A and B', before: { 'docs/old.md': ownerDoc('Section A\nSection B') }, + after: { 'docs/a.md': ownerDoc('Section A'), 'docs/b.md': ownerDoc('Section B') }, ok: false }, + { name: 'merge A and B into A+B', before: { 'docs/a.md': ownerDoc('Section A'), 'docs/b.md': ownerDoc('Section B') }, + after: { 'docs/new.md': ownerDoc('Section A\nSection B') }, ok: false }, + { name: 'move with one new visible line', before: { 'docs/old.md': ownerDoc('Section A\nSection B') }, + after: { 'docs/new.md': ownerDoc('Section A\nSection B\nNew behavior.') }, ok: true }, + { name: 'brand-new doc without deletions', before: {}, after: { 'docs/new.md': ownerDoc('New behavior.') }, ok: true }, + { name: 'split with blank-only lines', before: { 'docs/old.md': ownerDoc('Section A\nSection B') }, + after: { 'docs/new.md': ownerDoc('Section A\n \t\n') }, ok: false }, + { name: 'split with whitespace-only line differences', before: { 'docs/old.md': ownerDoc('Section A\nSection B') }, + after: { 'docs/new.md': ownerDoc(' S e c t i o n\tA\u00a0\n') }, ok: false }, + { name: 'split with control and format characters', before: { 'docs/old.md': ownerDoc('Section A\nSection B') }, + after: { 'docs/new.md': ownerDoc('Section\0 A\u200b\u0007\n\u200b\0') }, ok: false }, + { name: 'move with a line reused from a deleted non-owner', + before: { 'docs/old.md': ownerDoc('Section A'), 'docs/unrelated.md': '# Unrelated\nBorrowed behavior.' }, + after: { 'docs/new.md': ownerDoc('Section A\nBorrowed behavior.') }, ok: false }, + { name: 'new doc with only invisible body lines', before: {}, after: { 'docs/new.md': ownerDoc('\n \t\u200b\0\n') }, ok: false }, + { name: 'move with metadata-only new lines', before: { 'docs/old.md': ownerDoc('Section A\nSection B') }, + after: { 'docs/new.md': ownerDoc('Section A').replace('covers:', '# New metadata\ncovers:') }, ok: false }, + { name: 'new line matching deleted front matter only', before: { 'docs/old.md': ownerDoc('Section A').replace('covers:', '# Metadata\ncovers:') }, + after: { 'docs/new.md': ownerDoc('Section A\n# Metadata') }, ok: true }, + { name: 'move with a line reused from a retained non-owner', + before: { 'docs/old.md': ownerDoc('Section A'), 'docs/unrelated.md': 'Borrowed behavior.' }, + after: { 'docs/new.md': ownerDoc('Section A\nBorrowed behavior.'), 'docs/unrelated.md': 'Borrowed behavior.' }, ok: true }, +]; + +for (const { name, before, after, ok } of movementCases) { + for (const boundary of ['checkDocs', 'CLI']) { + test('GIVEN unpaired docs WHEN ' + name + ' THEN ' + boundary + ' applies visible-line novelty', () => { + for (const app of ['public/terminal.js', 'main.js', 'new-service.js']) { + const files = [app, ...new Set([...Object.keys(before), ...Object.keys(after)])]; + const trackedFiles = [...new Set([app, 'public/terminal.js', ...Object.keys(after)])]; + if (boundary === 'checkDocs') { + const result = checkDocs({ files, labels: [], trackedFiles, documents: after, previousDocuments: before }); + assert.equal(result.ok, ok, name + ': ' + app); + } else { + const renames = [ + ...Object.keys(before).filter(doc => !Object.hasOwn(after, doc)).map(doc => ['D', doc]), + ...Object.keys(after).map(doc => [Object.hasOwn(before, doc) ? 'M' : 'A', doc]), + ]; + const cli = runCli([...REFS, '--labels', '[]'], { files, trackedFiles, headDocuments: after, previousDocuments: before, renames }); + assert.equal(cli.code, ok ? 0 : 1, name + ': CLI ' + app + '\n' + cli.output); + } + } + }); + } +} + +test('GIVEN an R-status doc WHEN removing or borrowing lines THEN the previous-body comparison is preserved', () => { + const before = ownerDoc('Section A\nSection B'); + const options = { files: ['public/terminal.js', 'docs/old.md', 'docs/new.md', 'docs/unrelated.md'], + trackedFiles: ['public/terminal.js', 'docs/new.md'], + previousDocuments: { 'docs/old.md': before, 'docs/unrelated.md': 'Borrowed behavior.' }, + renames: [['R090', 'docs/old.md', 'docs/new.md']] }; + for (const [after, expected] of [[before, 1], [ownerDoc('Section A'), 0], [before + '\nBorrowed behavior.', 0]]) { + const result = runCli([...REFS, '--labels', '[]'], { ...options, headDocuments: { 'docs/new.md': after } }); + assert.equal(result.code, expected, 'R status must compare with its previous body: ' + after); + assert.ok(result.calls.some(call => call.argv.join(' ') === 'show merge-base-sha:docs/old.md')); + } +});