feat(cli): add gram check --fix and the fix-idempotent fuzz invariant #300

Merged
abiwab merged 6 commits from feat/check-fix into dev/1.6 2026-11-27 14:58:48 +01:00
Owner

PR 6b of .notes/backlog/plan-dx-et-aides-ecriture.md (phase 1 § 1C, "CLI gram check"): --fix and the fuzz idempotence invariant. Starts from dev/1.6 with #299 merged.

What changes

  • @gram-lang/modules/lint exports applyPreferredFixes(text, uri, diagnostics) (@experimental), a pure pass shared by the CLI and the fuzz. It returns { text, applied }, applied holding { diagnostic, fix } pairs.
    • A whole fix is left out when it holds a createFile, a patch on another uri, no patch, or a patch outside the text.
    • Overlaps are left out greedily, in text order. Two insertions at one point, or an insertion at a replacement's edge, count as an overlap.
    • Patches are applied from the end back.
  • UNKNOWN_INGREDIENT no longer marks its spelling fix as preferred, since renaming an ingredient changes the recipe. On main, VS Code only applies that preferred fix on demand ("Auto Fix…"), so no fix/… on main. The unreleased modules-lint.md changeset is corrected to match.
  • New gram check --fix option:
    • It loops to a fixed point, five passes at most. The fixed text goes back to runLintPipeline in memory (new entryText pipeline option), and each file is written once at the end, only if it changed.
    • lint.rules and // gram-ignore are respected for free: a code turned off has no diagnostic, so no fix. A @use dependency is never touched.
    • Files are processed one at a time with --fix, so none is read as another's dependency while it is being rewritten.
    • Terminal: N fixes applied, M remaining after the report. JSON: fixed on each entry of files[], with the diagnostics left (still version: 1), and nothing on stderr.
  • MERGED_STEP_LINE (unreleased, dev/1.6 only) leaves alone a line that could not open a block on its own, a bare [Stir] or a ## Title ~{60min}. with text after its anchor. Its preferred fix turned these into parse errors. Both were found by the new invariant.
  • New fuzz invariant fix-idempotent, built on Prepared.fixed:
    • If the source parses, the fixed text parses. If it compiled, it still compiles.
    • No diagnostic of the same code overlaps the fixed one's span, remapped through the applied patches. The overlap is strict: a removed servings: line followed by yields: at the same offset is not a comeback.
    • Three minimized inputs are added to fuzz/regressions/.
  • Docs in en + fr:
    • cli.mdx: --fix and its guarantee, and files[].fixed in the schema table.
    • ai-generation-notes.mdx: --fix in the loop.
    • warnings.mdx: UNKNOWN_INGREDIENT and MERGED_STEP_LINE.
    • modules.mdx: applyPreferredFixes.
  • Changeset check-fix.md: minor, cli and modules.

Checks

  • bun test: 4351 pass, 1 skip, 0 fail (was 4322 pass, 1 skip). Each commit is green on its own.
  • Conformance: 148/148 and 24/24, no golden changed.
  • Fuzz: 50,000 inputs on seed 2230234224, 0 signatures. Seed 1981115172, which found the ## … case, is clean too.
  • Audit: 128, unchanged.
  • Playground chunk: 1060.9 KB, unchanged.
  • Typecheck, biome and deprecations:check pass.
  • Each new test fails when the guard it covers is mutated away.
  • One test renamed on purpose: "UNKNOWN_INGREDIENT prefers a spelling fix only when it is the one closest name" is now "UNKNOWN_INGREDIENT never prefers a spelling fix, even with one closest name".
  • Not covered by a test:
    • The one-at-a-time processing under --fix, since a race between files does not reproduce reliably.
    • The invariant's "breaks the compilation" branch, since compile-contract already guarantees compile never throws.
PR 6b of .notes/backlog/plan-dx-et-aides-ecriture.md (phase 1 § 1C, "CLI `gram check`"): `--fix` and the fuzz idempotence invariant. Starts from `dev/1.6` with #299 merged. ## What changes - `@gram-lang/modules/lint` exports `applyPreferredFixes(text, uri, diagnostics)` (@experimental), a pure pass shared by the CLI and the fuzz. It returns `{ text, applied }`, `applied` holding `{ diagnostic, fix }` pairs. - A whole fix is left out when it holds a `createFile`, a patch on another uri, no patch, or a patch outside the text. - Overlaps are left out greedily, in text order. Two insertions at one point, or an insertion at a replacement's edge, count as an overlap. - Patches are applied from the end back. - `UNKNOWN_INGREDIENT` no longer marks its spelling fix as preferred, since renaming an ingredient changes the recipe. On `main`, VS Code only applies that preferred fix on demand ("Auto Fix…"), so no `fix/…` on `main`. The unreleased `modules-lint.md` changeset is corrected to match. - New `gram check --fix` option: - It loops to a fixed point, five passes at most. The fixed text goes back to `runLintPipeline` in memory (new `entryText` pipeline option), and each file is written once at the end, only if it changed. - `lint.rules` and `// gram-ignore` are respected for free: a code turned off has no diagnostic, so no fix. A `@use` dependency is never touched. - Files are processed one at a time with `--fix`, so none is read as another's dependency while it is being rewritten. - Terminal: `N fixes applied, M remaining` after the report. JSON: `fixed` on each entry of `files[]`, with the diagnostics left (still `version: 1`), and nothing on stderr. - `MERGED_STEP_LINE` (unreleased, `dev/1.6` only) leaves alone a line that could not open a block on its own, a bare `[Stir]` or a `## Title ~{60min}.` with text after its anchor. Its preferred fix turned these into parse errors. Both were found by the new invariant. - New fuzz invariant `fix-idempotent`, built on `Prepared.fixed`: - If the source parses, the fixed text parses. If it compiled, it still compiles. - No diagnostic of the same code overlaps the fixed one's span, remapped through the applied patches. The overlap is strict: a removed `servings:` line followed by `yields:` at the same offset is not a comeback. - Three minimized inputs are added to `fuzz/regressions/`. - Docs in en + fr: - `cli.mdx`: `--fix` and its guarantee, and `files[].fixed` in the schema table. - `ai-generation-notes.mdx`: `--fix` in the loop. - `warnings.mdx`: `UNKNOWN_INGREDIENT` and `MERGED_STEP_LINE`. - `modules.mdx`: `applyPreferredFixes`. - Changeset `check-fix.md`: minor, cli and modules. ## Checks - `bun test`: 4351 pass, 1 skip, 0 fail (was 4322 pass, 1 skip). Each commit is green on its own. - Conformance: 148/148 and 24/24, no golden changed. - Fuzz: 50,000 inputs on seed 2230234224, 0 signatures. Seed 1981115172, which found the `## …` case, is clean too. - Audit: 128, unchanged. - Playground chunk: 1060.9 KB, unchanged. - Typecheck, biome and `deprecations:check` pass. - Each new test fails when the guard it covers is mutated away. - One test renamed on purpose: "UNKNOWN_INGREDIENT prefers a spelling fix only when it is the one closest name" is now "UNKNOWN_INGREDIENT never prefers a spelling fix, even with one closest name". - Not covered by a test: - The one-at-a-time processing under `--fix`, since a race between files does not reproduce reliably. - The invariant's "breaks the compilation" branch, since `compile-contract` already guarantees `compile` never throws.
test(fuzz): name the fix-idempotent regressions after #300
Some checks failed
CI / Build & Test (pull_request) Failing after 5m49s
efdeda046f
test(cli): name the files of the --fix JSON test instead of globbing them
All checks were successful
CI / Build & Test (pull_request) Successful in 6m3s
d311881532
abiwab merged commit 295f35f133 into dev/1.6 2026-11-27 14:58:48 +01:00
abiwab deleted branch feat/check-fix 2026-11-27 14:58:49 +01:00
Sign in to join this conversation.
No description provided.