Sixth R7b port — the T1½ batch finale. Checklist-first recipe
(docs/design/kotlin-kernel-port-checklist.md, 1,121 lines, dist-extractor
ground truth); parity passed FIRST RUN on all three repos.
THE NOVEL MECHANISM — vendored-grammar-C (the §4 tracker's prescription,
first use): the crates.io tree-sitter-kotlin 0.3.8 pins `tree-sitter >= 0.21,
< 0.23` (the kernel links 0.25) and tree-sitter-kotlin-ng is a DIFFERENT
grammar (8 fields vs 0, renamed kinds — extractor-breaking), so no crate dep
is possible. The fwcd 0.3.8 tag's sha-matched parser.c + scanner.c are
vendored into codegraph-kernel/grammars/kotlin and compiled by build.rs (cc),
exposed via tree-sitter-language::LanguageFn. The wasm re-vendor is
behavior-NEUTRAL (0 CST/error disagreements across 1,984 gate-repo files;
old-vs-new full-init dumps byte-identical ×3) — a reproducibility re-vendor,
ABI stays 14.
Walker firsts: extension-function receivers (getReceiverType →
`WidgetK::extend` QN OVERRIDE with no package prefix, the qualified-receiver
`com::qext` first-segment bug, and the owner-contains fallback that excludes
`interface` kinds and is source-order dependent) and extractModifiers
(expect/actual platform modifiers → the node DECORATORS wire field on every
created node — the KMP synthesizer's feed, incl. `actual typealias`).
Preserved bug-for-bug: the FIELD_COUNT-0 dead cluster (no signatures, ZERO
type-annotation refs), hook-consumed property initializers emitting nothing
(incl. `by lazy {}`), the bodiless-vs-bodied class header asymmetry, enum-
entry bodies being invisible, KDoc never a docstring AND chain-breaking,
comment-gluing into import/package extents, `@Anno(args)` emitting nothing
while `@Marker` decorates, zero instantiates refs, the paren-then-lambda
`trailing()` garbage callee, text-includes visibility/suspend false
positives, and the packaged-file value-ref target drop. The fun-interface
misparse-recovery hook is DEFER-SHIELDED (every such file has_error) and
deliberately not ported. The swift-sweep lesson pre-applied: the shared
`assignment` shadow-prune case is implemented alongside the
property_declaration case.
Gates: sweeps 0-diff okio 299/322, okhttp 531/580, kotlinx.coroutines
1031/1082 (deferrals exactly the predicted 23/49/51 — both-arm grammar
reality incl. PHANTOM hasError files with complete CSTs; the kernel trusts
the flag); full-init dumps byte-identical ×3 (46.5k/108.9k/92.3k lines); KMP
expect/actual synthesis IDENTICAL across arms (412 edges on
kotlinx.coroutines — the tracker's KMP validation); kernel-kotlin-parity
suite (torture reflowed off the phantom shapes + .kts script + CRLF variants
+ fun-interface and phantom defer pins) + kotlin grammar-parity row (the
C-build ↔ wasm table identity proof); full suite 2,633 green ×2 under
CODEGRAPH_KERNEL_EXPECT=1. DEFAULT_ROUTED += kotlin (15 langs).
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
71 lines
3.1 KiB
TypeScript
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', 'php', 'swift', 'kotlin'];
|
|
|
|
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 ?? ''));
|
|
});
|
|
});
|