Files
codegraph/__tests__/kernel-grammar-parity.test.ts
T
286e9ccc2d feat(kernel): R7b C# walker — csharp module, tree-sitter-c-sharp 0.23.5 pin, csharp default-routed (#1378)
Second R7b port, checklist-first recipe (docs/design/csharp-kernel-port-checklist.md;
parity passed FIRST RUN again). No grammar bump — the #717 vendored wasm verified
table-identical to crate 0.23.5 (ABI 15, STATE_COUNT 8053, node-kind + field tables);
first port with no grammar-prep step. The #237 #if-blanking preParse stays TS-side
via the existing route-point hoist.

Walker preserves bug-for-bug: the single-namespace-node quirks (second namespace
nests under the first, nested namespaces leave no trace, import refs hang off the
namespace node), raw member-access callee texts (this./base./literal receivers,
multi-line fluent chains) with unconditional chain re-encode, deliberate emission
holes (property accessor/expression bodies, ctor initializers, delegates/events/
operators/indexers/local functions, top-level locals), garbage extends refs
((repo) primary-ctor args, BaseDto(Name) record bases, enum : byte), the alias-
import moduleName quirks, nameof-as-call, CSHARP fn-ref spec (+= subscription,
this.X bare-name form, argument layer, initializer lists), C# type-ref engine
(nested-generic returnType failure included), and value-ref shadow pruning.

Gates: sweeps 0-diff serilog 211/216 / Newtonsoft.Json 914/945 / jellyfin
2104/2105 (deferrals match the survey's per-repo predictions — both-arm #if
damage; default --max-deferral 0.1 holds, no c/cpp exemption); full-init dumps
byte-identical ×3 (14.0k/109.1k/210.8k lines); kernel-csharp-parity suite
(torture ×3 + CRLF variants + 8 micro-pins + defer) + csharp grammar-parity row;
full suite 2,608 ×2 under CODEGRAPH_KERNEL_EXPECT=1. DEFAULT_ROUTED += csharp
(11 langs).

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-20 13:46:12 -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'];
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 ?? ''));
});
});