Files
msd-core/scripts/lint-docs-command-form.cjs
Tom Boucher ffd5370464 fix(#2903): use the command form that actually works in reader-facing docs (#3047)
* fix(#2903): use the command form that actually works in reader-facing docs

Docs told readers to type the colon form, which no runtime registers -- 18 of
19 runtimes use slash-hyphen and the 19th uses shell-var -- so anyone copying an
example got an unrecognized command. Swept 178 occurrences across 53 files,
locale mirrors included so they do not re-diverge from English.

The colon form is a source-authoring token, not a user-facing one: install-time
converters key on it to produce the hyphen form runtimes actually register. So
the sweep is scoped, and three things are deliberately left alone:

- ADRs, which are a historical record; editing their prose falsifies what was
  written at the time.
- The legacy release-notes archive, pending a maintainer decision on whether it
  follows the same historical carve-out. Excluding it keeps a later reversal
  additive rather than a revert.
- Source artifacts under commands, workflows and agents, where the colon form is
  load-bearing. Rewriting those would break the installed-skill guarantee across
  every runtime -- the single largest hazard here.

The plugin namespace form is a real, separate token and survives untouched.

Adds a lint enforcing exactly that boundary, since the correct form genuinely
differs by directory and nothing previously caught the drift.

Also fixes a hardcoded colon form in the capability-matrix generator. The sweep
alone would have left the generated matrix disagreeing with the template that
produces it, so the fix is at the source and the output regenerated.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(#2903): stop the sweep misquoting source frontmatter

Adversarial review caught three lines where the sweep rewrote a citation of the
literal YAML name: key from a source command file. That key genuinely is the
colon form -- this change's own carve-out logic says source-authoring tokens keep
it -- so the docs ended up misquoting the real files. One of the three is an
acceptance-checklist assertion, which the sweep turned into a false statement.

Restored the three citations to match their sources verbatim, surgically: where a
line carried both a name: citation and a real reader-facing slash command, only
the citation reverted and the command stayed corrected.

The guard needed the same distinction, or it would have flagged the restoration
and reddened the build: a gsd:<cmd> token preceded by name: is a citation of a
source token and is now permitted. The exemption is deliberately narrow -- a bare
gsd:<cmd> anywhere else still fails -- with a test pinning that narrowness.

Also makes the detection case-insensitive. Review found /GSD:next slipped through
silently; no such casing exists in the tree today, so this closes a latent gap
rather than fixing a live one.

Swept the whole tree for further corrupted citations: none beyond the three.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(#2903): retire the stale-next invariant and sweep next like every other command

Maintainer decision on a genuine conflict between two contracts.

Invariant #3054 banned the literal /gsd-next from user-facing docs because it
named a retired workflow-advance command. But commands/gsd/next.md is a live
command -- the state-aware smart-entry launcher -- and this issue requires docs
to use the hyphen form every runtime actually registers. Both could not hold for
this one command, so docs had been sidestepping the ban by keeping the colon
form, which is exactly the defect this issue exists to remove.

FEATURES.md already recorded the reassignment: the hyphen form "is not the
retired workflow-advance command; it is reserved for the state-aware smart-entry
launcher. Workflow advancement remains under /gsd-progress --next." With that
reassignment the invariant's premise is obsolete and the guard now contradicts
the documented command form, so it is retired with a comment recording why
rather than deleted silently.

next is now swept like every other command, and the earlier exemption added to
the new guard is removed so nothing is special-cased.

Four citations of the literal name: frontmatter key stay in colon form, because
the source file really does carry name: gsd:next and a doc quoting it must
reproduce it verbatim. Two of those lines were reworded to say which side is the
frontmatter key and which is the slash command, since they previously conflated
the two.

Verified the retired scan would now genuinely fail against this tree -- the
conflict was real and resolved, not dodged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore(#2903): backfill changeset pr number

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-04 13:23:44 -04:00

196 lines
7.4 KiB
JavaScript

#!/usr/bin/env node
/**
* lint-docs-command-form.cjs
*
* Enforces the human-facing command form in docs/ (#2903).
*
* `/gsd:<cmd>` (and bare `gsd:<cmd>`) is a SOURCE-AUTHORING token: install-time
* converters (`transformContentToHyphen`, `convertSlashCommandsTo<Runtime>SkillMentions`)
* rewrite it to `/gsd-<cmd>` per-runtime. It is correct in `commands/gsd/**`,
* `gsd-core/workflows/**`, and `agents/**` — but docs are never passed through a
* converter, so a doc telling a reader to type `/gsd:<cmd>` names a command no
* runtime registers. The real user-facing form is `/gsd-<cmd>`.
*
* This guard fails any `docs/**\/*.md` file that still contains `/gsd:<cmd>` or
* bare `gsd:<cmd>` where `<cmd>` is a real command name (drawn from the
* `commands/gsd/*.md` roster). It explicitly permits `/gsd-core:<cmd>` (the
* Claude Code plugin namespace) and any `gsd:<token>` whose `<token>` is not a
* real command (e.g. the `gsd:section` / `gsd:loop-host` workflow-fragment
* marker syntax documented in docs/reference/workflow-fragments.md, or
* `gsd:command-name` used as a placeholder while explaining the Gemini CLI
* colon-form convention).
*
* Exclusions (never checked):
* - docs/adr/** (historical record)
* - docs/RELEASE-NOTES-LEGACY.md (maintainer question still open)
* - commands/gsd/**, gsd-core/workflows/**, agents/** — never touched by this
* guard at all; the colon form is correct there.
*
* Exemption — `name:` frontmatter key citations: source command files
* (`commands/gsd/*.md`) carry the colon form in their `name:` YAML frontmatter
* key (e.g. `name: gsd:next`). A doc that quotes that key verbatim — e.g.
* `` `name: gsd:next` `` or `name: gsd:next` — is citing the real source file,
* not telling a reader what to type. Rewriting that citation to `gsd-next`
* would make the doc lie about the source it's quoting, so a `gsd:<cmd>` token
* immediately preceded by `name:` (optionally with a backtick/whitespace in
* between) is permitted. This is narrow: it does not exempt `gsd:<cmd>`
* anywhere else on the line or file, including the reader-facing `/gsd-<cmd>`
* form that may appear later in the same sentence.
*
* Detection is case-insensitive (`/GSD:next`, `Gsd:Next`, etc. are all
* flagged) since the install-time converters and runtimes treat command names
* case-insensitively in practice, and a doc typo in casing is still a lie
* about the real command form.
*
* Exit 0 if no violations; exit 1 if any are found (with stderr diagnostics).
*/
'use strict';
const { execFileSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const { ExitError, runMain } = require('./lib/cli-exit.cjs');
const SELF_PATH = path.resolve(__filename);
// GSD_LINT_DOCS_COMMAND_FORM_REPO_ROOT is used by tests to redirect the guard to
// a temporary fixture git repo without touching the real working tree.
const REPO_ROOT = process.env.GSD_LINT_DOCS_COMMAND_FORM_REPO_ROOT
? path.resolve(process.env.GSD_LINT_DOCS_COMMAND_FORM_REPO_ROOT)
: path.resolve(__dirname, '..');
const DOCS_PREFIX = 'docs/';
const ADR_PREFIX = 'docs/adr/';
const RELEASE_NOTES_LEGACY = 'docs/RELEASE-NOTES-LEGACY.md';
const COMMANDS_DIR = path.join(REPO_ROOT, 'commands/gsd');
// Matches `/gsd:<cmd>` and bare `gsd:<cmd>` (not part of `/gsd-core:<cmd>`,
// which does not contain the substring `gsd:` — the hyphen breaks it).
// Case-insensitive so `/GSD:next` / `Gsd:Next` are also caught.
const COMMAND_FORM_RE = /(^|[^A-Za-z0-9_-])(\/)?gsd:([A-Za-z0-9_-]+)/gi;
// A `gsd:<cmd>` token is exempt when it is a citation of a source file's
// YAML `name:` frontmatter key — i.e. the text immediately before the match
// (ending exactly where the match begins) is `name:` followed by optional
// whitespace and/or a backtick. Only applies to the bare (non-`/`) form,
// since the real frontmatter key never carries a leading slash.
const NAME_KEY_CITATION_RE = /name:\s*`?\s*$/i;
function loadRoster() {
let entries;
try {
entries = fs.readdirSync(COMMANDS_DIR);
} catch (err) {
throw new ExitError(1, 'ERROR lint-docs-command-form: could not read commands/gsd: ' + err.message);
}
return new Set(
entries.filter((f) => f.endsWith('.md')).map((f) => f.replace(/\.md$/, '')),
);
}
function isCheckedDocsFile(relPath) {
if (!relPath.startsWith(DOCS_PREFIX)) return false;
if (relPath.startsWith(ADR_PREFIX)) return false;
if (relPath === RELEASE_NOTES_LEGACY) return false;
return relPath.endsWith('.md');
}
/**
* Pure scan — no fs, no git. Returns violations for a single file's content.
*
* @param {string} relPath file path (used only in violation records)
* @param {string} content file contents
* @param {Set<string>} roster valid command names
* @returns {Array<{file:string, line:number, col:number, text:string}>}
*/
function scanContent(relPath, content, roster) {
const violations = [];
const lines = content.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
COMMAND_FORM_RE.lastIndex = 0;
let match;
while ((match = COMMAND_FORM_RE.exec(line)) !== null) {
const [, pre, slash, cmd] = match;
if (!roster.has(cmd.toLowerCase())) continue;
if (!slash) {
const preContext = line.slice(0, match.index + pre.length);
if (NAME_KEY_CITATION_RE.test(preContext)) continue;
}
const matchedToken = (slash || '') + 'gsd:' + cmd;
violations.push({
file: relPath,
line: i + 1,
col: match.index + pre.length + 1,
text: matchedToken,
});
}
}
return violations;
}
function main() {
const roster = loadRoster();
let trackedFiles;
try {
trackedFiles = execFileSync('git', ['ls-files'], { cwd: REPO_ROOT, encoding: 'utf8' })
.split('\n')
.map((f) => f.trim())
.filter(Boolean);
} catch (err) {
throw new ExitError(1, 'ERROR lint-docs-command-form: git ls-files failed: ' + err.message);
}
const docsFiles = trackedFiles.filter(isCheckedDocsFile);
const violations = [];
for (const relPath of docsFiles) {
const fullPath = path.join(REPO_ROOT, relPath);
if (path.resolve(fullPath) === SELF_PATH) continue;
let content;
try {
content = fs.readFileSync(fullPath, 'utf8');
} catch {
// Unreadable/deleted files — skip silently.
continue;
}
violations.push(...scanContent(relPath, content, roster));
}
if (violations.length === 0) {
process.stdout.write(
'ok lint-docs-command-form: ' + docsFiles.length + ' file(s) checked, 0 violations\n',
);
return 0;
}
process.stderr.write('\nERROR lint-docs-command-form: ' + violations.length + ' violation(s) found\n\n');
for (const v of violations) {
process.stderr.write(' ' + v.file + ':' + v.line + ':' + v.col + ' — ' + JSON.stringify(v.text) + '\n');
}
process.stderr.write('\n');
process.stderr.write(
'Fix: docs are never passed through the install-time slash-form converters, so the\n',
);
process.stderr.write(
' colon form names a command no runtime registers. Rewrite to the hyphen form\n',
);
process.stderr.write(
' (`/gsd-<cmd>`), or `/gsd-core:<cmd>` if this is genuinely the Claude Code\n',
);
process.stderr.write(' plugin namespace.\n\n');
return 1;
}
if (require.main === module) runMain(main);
module.exports = {
scanContent,
isCheckedDocsFile,
loadRoster,
COMMAND_FORM_RE,
};