Files
codegraph/scripts/add-lang
Colby MchenryandClaude Opus 5.5 9ed79c6cf1 fix(add-lang): check-grammar fails a grammar that crashes or hangs its process (#2808)
Root cause: scripts/add-lang/check-grammar.mjs ran the parse loop in its own
process and printed "RESULT: PASS" as soon as the loop finished. A grammar
can finish every parse and still take the process down after that. With the
issue's vim grammar (tree-sitter-wasm 2.0.4) on Node 24, V8's optimizing tier
aborts the process from a background compile job ("Fatal process out of
memory: Zone") after PASS is printed, so the verdict and the exit code
disagree. The check also ran without the V8 flag indexing always uses
(--liftoff-only), so it judged grammars in a setup codegraph never runs them
in, and an iteration count of 0 or a word passed without a single parse.

Fix: the script runs the grammar in a child process, the way indexing does,
and prints the verdict only after that child has exited.
- The child gets indexing's V8 flags (WASM_RUNTIME_FLAGS) and loads every
  vendored grammar beside the one under test, like a polyglot index.
- It parses the sample with one reused parser, walks every node of each tree
  and frees it, as extraction does. Every parse of the same sample must give
  the same tree.
- It then parses the sample repeated to 1 MiB, the most indexing reads from
  one file, within the 10 s indexing gives one file. Only a crash or a hang
  counts there.
- The parent fails the grammar when the child dies (any exit code or
  signal), runs past a timeout (CODEGRAPH_CHECK_GRAMMAR_TIMEOUT_MS, 120 s by
  default), or reports ERROR trees or differing trees. An iteration count that
  is not a whole number above 0 is refused (exit 2).
The add-lang skill now says what counts as a FAIL.

Validation:
- New __tests__/add-lang-check-grammar.test.ts. A preload marks the process
  that runs WebAssembly, records its flags, and on request kills it or makes
  it hang at exit, after the parses. With main's script 4 of 5 tests fail:
  PASS printed for a process that then died, a hang until the test's 60 s
  limit, no --liftoff-only in the parsing process, and "0" passing. With the
  fix all 5 pass. The fifth (the tree-sitter-wasms Lua ABI-13 grammar fails
  with ERROR trees) passes on both.
- The issue's vim grammar (sha256 df3906a0, the issue's file) on Node 24.21.0:
  main's script printed PASS and then died with the Zone OOM, exit 3, in 3 of
  3 runs. The new check runs it with --liftoff-only, as indexing does, where
  it parses cleanly, 1 MiB included, and passes in 3 of 3. A copy of the new
  script with the flag withheld fails it in 3 of 3: "the parses finished, but
  the process running them then died (exit code 2147483651, 0x80000003)".
- Old vs new on the 19 vendored grammars with real samples, plus the
  tree-sitter-wasms Lua and the vim grammar, on Node 22.22.0 and 24.21.0
  (84 runs): the same verdict every time, except vim on Node 24, where main's
  PASS came with exit 3. No grammar's parses diverged.

Fixes #2464

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 17:04:12 +00:00
..