Files
codegraph/__tests__/kernel-grammar-parity.test.ts
T
1909931238 feat(kernel): R7b Ruby walker — ruby module, tree-sitter-ruby 0.23.1 bump, ref-flag wire slot, ruby default-routed (#1379)
Third R7b port, checklist-first recipe (docs/design/ruby-kernel-port-checklist.md).

Grammar bump first, validated standalone (the rust pattern): tree-sitter-ruby
^0.20.1 (tree-sitter-wasms, 2024-02) → v0.23.1 — crate pinned =0.23.1, wasm
built from tag 71bd32f's checked-in parser.c/scanner.c (both sha-matched
against the crates.io tarball; content bump, ABI stays 14). Old-vs-new
full-init dumps: sinatra/jekyll byte-identical; rails = exactly the one
classified hunk (the `recv&.!=` safe-nav operator misparse fix,
`table_name.!` → `table_name.!=`, precision-positive).

Walker (python.rs chassis + the six ruby divergences) preserves bug-for-bug:
the importTypes:['call'] funnel (class-body DSL — attr_accessor, has_many,
define_method incl. its block, sinatra route blocks — emits NOTHING at
non-body scope), hook-handled module multiply-capture (nested modules re-scan
their subtree per level after popping — `this.hooked` fn-refs from class AND
module AND file), the sibling-scan visibility trio (bare `private` invisible;
`private :sym`/`private def` poison all later defs; the inner def stays
public), bare-call statements (do…end body_statement emits, brace-block
block_body doesn't), `.new` instantiates with last-`::`-segment names,
constant-receiver references refs, require/require_relative path refs
(posix-normalized, `.rb`-suffixed, `Kernel.require` and interpolated-path
quirks included), `=begin` docstring marker survival, and the reverse-order
value-ref DFS.

Wire v2: the hook's mixin `implements` refs carry `filePath: ctx.filePath` —
the ONE extraction-ref denormalized field (php's trait-use refs share the
shape). RefRow's first pad byte becomes a flags slot (REF_FLAG_FILE_PATH);
decode re-attaches its own filePath parameter; KERNEL_ABI_VERSION 1→2 on both
sides (mismatched dist/.node pairs degrade to wasm, as designed).

Gates: sweeps 0-diff sinatra 147/147, jekyll 164/164, rails 3452/3452 (3,763
files, 0 deferrals — ruby error incidence 0.00%, any deferral = walker bug);
full-init dumps byte-identical ×3 (7.2k/9.4k/375.6k lines); kernel-ruby-parity
suite (torture + CRLF + wire-flag pin + defer) + ruby grammar-parity row;
full suite 2,613 green ×2 under CODEGRAPH_KERNEL_EXPECT=1 (one unrelated
mcp-initialize timing flake under parallel load, passes solo 3/3 ×3).
DEFAULT_ROUTED += ruby (12 langs).

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-20 15:44:13 -05:00

71 lines
3.1 KiB
TypeScript

/**
* Grammar-source parity gate (R1, migration plan §3.5).
*
* The native kernel compiles grammars from crates.io / vendored sources; the
* wasm fallback loads grammars from tree-sitter-wasms / src/extraction/wasm.
* If the two are built from different grammar revisions, a language's graph
* would depend on WHICH path extracted it — per-language routing (and the
* kernel-absent fallback) must be graph-neutral.
*
* Rather than trusting version metadata, this asserts the grammars are
* behaviorally identical where extraction can observe them: ABI version and
* the full node-kind and field tables, compared id by id.
*
* Runs wherever a kernel binary is staged (scripts/build-kernel.sh); skips
* otherwise. CI that builds the kernel sets CODEGRAPH_KERNEL_EXPECT=1 so the
* skip can't mask a missing build (asserted in kernel-scaffold.test.ts).
*/
import { describe, it, expect, beforeAll } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import type { Language as WasmLanguage } from 'web-tree-sitter';
import { getKernel, resetKernelForTests } from '../src/extraction/kernel';
import { initGrammars, loadGrammarsForLanguages, getParser } from '../src/extraction/grammars';
import type { Language } from '../src/types';
const KERNEL_PATH = path.join(
__dirname,
'..',
'codegraph-kernel',
'prebuilds',
`${process.platform}-${process.arch}`,
'codegraph-kernel.node'
);
const kernelBuilt = fs.existsSync(KERNEL_PATH);
// Every kernel-capable language. `jsx` shares the javascript grammar on BOTH
// paths (langs.rs mirrors WASM_GRAMMAR_FILES), so the distinct grammars are:
const GRAMMAR_LANGUAGES: Language[] = ['typescript', 'tsx', 'javascript', 'java', 'python', 'go', 'c', 'cpp', 'rust', 'csharp', 'ruby'];
describe.skipIf(!kernelBuilt)('kernel↔wasm grammar parity', () => {
beforeAll(async () => {
resetKernelForTests();
await initGrammars();
await loadGrammarsForLanguages(GRAMMAR_LANGUAGES);
});
it.each(GRAMMAR_LANGUAGES)('%s: node-kind and field tables are identical', (language) => {
const kernel = getKernel();
expect(kernel).not.toBeNull();
const native = kernel!.grammarInfo(language);
expect(native, `kernel has no grammar for ${language}`).not.toBeNull();
const wasmLang = getParser(language)?.language as WasmLanguage | null | undefined;
expect(wasmLang, `wasm grammar for ${language} not loaded`).toBeTruthy();
expect(native!.abiVersion, 'grammar ABI version').toBe(wasmLang!.abiVersion);
expect(native!.nodeKindCount, 'node-kind count').toBe(wasmLang!.nodeTypeCount);
expect(native!.fieldCount, 'field count').toBe(wasmLang!.fieldCount);
const wasmKinds: (string | null)[] = [];
for (let i = 0; i < wasmLang!.nodeTypeCount; i++) wasmKinds.push(wasmLang!.nodeTypeForId(i));
expect(native!.nodeKinds).toEqual(wasmKinds.map((k) => k ?? ''));
// Field ids are 1-based on both sides.
const wasmFields: (string | null)[] = [];
for (let i = 1; i <= wasmLang!.fieldCount; i++) wasmFields.push(wasmLang!.fieldNameForId(i));
expect(native!.fieldNames).toEqual(wasmFields.map((f) => f ?? ''));
});
});