Files
codegraph/__tests__/kernel-grammar-parity.test.ts
T
Colby McHenryandClaude Fable 5 03d54e47a1 feat(kernel): R4 — Java port with Lombok synthesis, gate passed, default-on
Java joins the native kernel (codegraph-kernel/src/java.rs), mirroring
the wasm extractor's Java paths bug-for-bug: package namespaces,
imports, javadoc, annotations→decorates, type_list inheritance,
static-final constants, enum constants, anonymous classes (including
the TS side's 0-based-line quirk on the extends ref), method_invocation
calls with the this.field unwrap and the Foo.getInstance().bar() chain
encoding (#645/#608), static-member value reads, method-reference
fn-refs (#756), value-reference edges, and the full Lombok member
synthesizer (#912: Getter/Setter/Data/Value/Builder/ToString/
EqualsAndHashCode/Slf4j-family with taken-member dedup). The shared
docstring/textutil modules moved to crate level. Grammar:
tree-sitter-java 0.23.5, with the wasm grammar vendored from the same
tag (parser.c sha-matched) replacing tree-sitter-wasms' 2023-era build.

Gate (plan §4c): extraction sweeps 100% — gson 262/262, retrofit
341/341, dubbo 4,048/4,048 — plus a Java torture fixture in npm test;
full-init dump-diffs byte-identical on gson (49,766 rows), retrofit
(62,735), and dubbo (441,266 rows); all R2/R3 repos re-verified; Linux
container runs all 23 kernel tests green under CODEGRAPH_KERNEL_EXPECT=1.

The gate caught a real cross-language bug: fn-ref dedupe and value-ref
self-target checks must compare node ID STRINGS, not node-table rows —
ids collide for same-(kind, name, line) nodes, which minified one-line
bundles hit routinely (retrofit's website JS exposed it; latent in the
TS/JS walker since R2, never released). Fixed in both walkers.

Benchmark honesty: dubbo fresh-init on an 11-core Mac is ~flat
(parse-loop wall 5,020→4,394ms; total ~11.3s both arms) because that
wall is main-thread-bound (reads + store), not worker-CPU-bound — the
§6 expectation assumed otherwise. Where worker CPU binds the kernel
delivers: dubbo on a 2-CPU/6GB container drops 27.8-28.6s → 22.3-22.8s
(~1.25×). The identified lever for the many-core headline is decoding
kernel buffers directly into store rows (skipping per-node JS object
materialization); the buffer contract already carries everything.

DEFAULT_ROUTED now includes java. Full suite: 2,467 tests pass.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-16 23:25:29 -05:00

71 lines
3.0 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'];
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 ?? ''));
});
});