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>
This commit is contained in:
Colby Mchenry
2026-07-20 13:46:12 -05:00
committed by GitHub
co-authored by Claude Fable 5
parent f1ca991943
commit 286e9ccc2d
15 changed files with 2890 additions and 6 deletions
+181
View File
@@ -0,0 +1,181 @@
#define TORTURE_FLAG
global using GlobalNs.Thing;
using System;
using System.Collections.Generic;
using static System.Math;
namespace Torture.Alpha
{
using System.Text;
namespace Inner
{
public class Deep { }
}
/// <summary>Doc line.</summary>
/// <remarks>Second doc line over an attribute.</remarks>
[Serializable]
public class TortureClass : Widget
{
#pragma warning disable 168
public const int MaxItems = 10;
public static readonly string DefaultName = "x";
private const int LOCAL_SHADOWED = 5;
private Widget A, B;
private readonly int _count;
private static int counter;
private IRepo _repo;
private ServiceCollection builder;
private int status;
private List<Action<int>> Table = new() { TargetCb };
#nullable enable
public string Name { get; set; }
public Widget Parent { get; init; }
public List<Widget> Items { get; }
public int Computed => MaxItems + 1;
public Widget Made { get; } = new();
public int Accessored { get { return Lookup(1); } set { Store(value); } }
public Action A2 { get; } = () => Register(HandleThing);
public event Action Changed;
public event Action Custom
{
add { Register(HandleThing); }
remove { Unregister(value); }
}
public static TortureClass operator +(TortureClass a, TortureClass b) { return Combine(a, b); }
public static explicit operator int(TortureClass t) { return Score(t); }
public int this[int idx] { get { return Lookup(idx); } }
~TortureClass() { Cleanup(); }
public TortureClass(int seed) : base(Compute(seed)) { Init(seed); }
public TortureClass() => Init(0);
void IDisposable.Dispose() { Cleanup(); }
protected internal void PI() { }
private protected void PP() { }
public async Task Waity() { await Task.Delay(1); }
public int Sum() => Compute(1) + 2;
private void HandleThing(int v) { }
private static void StaticHandler() { }
private void OnClick() { }
private void Handler() { }
private static void TargetCb(int n) { }
private static int Compute(int v) { return v; }
private static Widget Mk() { return null; }
public void Wire(Widget button, int status)
{
Register(HandleThing);
Register(this.HandleThing);
Register(C.StaticHandler);
button.Click += OnClick;
this.status = status;
Del d = Handler;
Action g = () => Register(HandleThing);
}
public void CallsZoo(Widget p, MyDel myDel, HttpRequest request)
{
Helper();
Generic<int>(5);
this.Run(1);
base.Method();
_repo.Save(2);
var svcs = builder
.Services
.AddSingleton<IRepo, Repo>();
"lit".ToUpper();
request?.Method();
p!.Force();
Foo.Create(1).Bar();
GetThing().Bar();
(myDel)(3);
var n = nameof(Widget);
}
public void Reads(User u)
{
DoThing(Constants.MAX);
var x = ReadType.ReadAsDouble;
var y = Outer.Inner.DEEP;
var age = u.Age;
Console.WriteLine(x);
}
public void News()
{
var a = new Widget(1) { Name = Mk() };
var b = new Ns.Foo<int>();
Widget c = new();
var d = new { X = 1 };
var e = new Widget[10];
var f = new[] { Mk() };
}
public IEnumerable<int> Query(List<int> items)
{
var q = from x in items where Check(x) select Map(x);
var s = items.Count switch { 0 => One(), _ => Other() };
var msg = $"Hello {NameOf(this)}";
var raw = """raw text""";
var verb = @"verbatim\path";
int[] coll = [First(), Second()];
return q;
}
public int WithLocal()
{
int Local(int v) { return Compute(v); }
return Local(2);
}
public Task<List<Widget>> Fetch(Widget? maybe, Widget[] arr, (int Code, Widget Payload) pair, List<Widget> list, Sys.ICloneable c, dynamic dyn, String s, int nn) { return null; }
public Task<Widget> Single() { return null; }
public Ns.Foo Qual() { return null; }
public int Reader()
{
var LOCAL_SHADOWED = 1;
return MaxItems + LOCAL_SHADOWED;
}
public string Reader2() => DefaultName.Trim();
}
public class Client : ClientBase<Widget>, Sys.ICloneable, IThing { }
public partial class PartialHost { partial void Hook(); }
public partial class PartialHost { partial void Hook() { } }
public interface IWidgetRepo<T> where T : IEntity
{
Task<T> Get(int id);
string Label { get; }
int Compute(int x) => x + 1;
}
public enum ReadType : byte
{
[Obsolete] ReadAsInt = 1,
ReadAsDouble,
}
public struct Point3 { public int X; }
#region grouped
public class Grouped { }
#endregion
}
namespace Torture.Beta
{
public class Other { }
}
@@ -0,0 +1,24 @@
using Coll = System.Collections.Generic.Dictionary<string, int>;
using Short = SomeType;
using System;
namespace Torture.Scoped;
/// <summary>A positional record with base args.</summary>
public record UserDto(string Name, int Age) : BaseDto(Name), IThing;
public readonly record struct Money(decimal Amount);
public record struct Pointish(int X, int Y);
public record Empty;
public record Bodied(string Label) : BaseDto(Label)
{
public string Loud() { return Shout(Label); }
}
public class Svc(IRepo repo, ICache cache) : Base(repo), IThing
{
public void Go() { repo.Save(1); }
}
@@ -0,0 +1,13 @@
using System;
var builder = CreateBuilder(args);
Run(builder);
var w = new Widget();
DoWork(w);
int Helper(int x) => Compute(x);
Helper(3);
partial class Program
{
static void Main2() { Console.WriteLine("hi"); }
}
+188
View File
@@ -0,0 +1,188 @@
/**
* Kernel↔wasm C# extraction parity (R7b of the kernel migration).
*
* Asserts the native walker (codegraph-kernel/src/csharp.rs) produces the
* SAME ExtractionResult as the wasm TreeSitterExtractor — nodes, edges, and
* unresolved refs compared as canonicalized multisets — over the checked-in
* torture fixtures:
*
* - Torture.cs — block namespace + nested/second-namespace quirks,
* base_list shapes, records, properties (incl. the bare-identifier
* signature loss and never-walked accessor bodies), fields/constants,
* events/operators/indexer/destructor (no nodes, calls → class), ctor
* initializer hole, explicit interface impl, local functions, the call
* zoo (raw member-access texts, chained re-encode, `(myDel)(x)` conv,
* `nameof`), instantiation shapes (incl. invisible `new()`/`new {}`/
* arrays), static value reads, C# type refs, fn-ref candidates
* (`+=` subscription, `this.X` bare-name form, initializer lists),
* value-ref targets + local shadow prune, preprocessor passthrough.
* - TortureFileScoped.cs — file-scoped namespace, alias-import quirks,
* positional records with base args (`BaseDto(Name)` full-text extends),
* C#12 primary-ctor base args (`(repo)` garbage extends preserved).
* - TortureTopLevel.cs — top-level statements (zero-emission locals),
* top-level local function, trailing partial class.
*
* CRLF variants are derived in-memory (#1329 docstring semantics). The
* full-repo sweep lives in scripts/kernel-parity.mjs (serilog /
* Newtonsoft.Json / jellyfin for the §5 gate); this suite keeps the invariant
* alive in `npm test`. Skips when no kernel binary is staged;
* CODEGRAPH_KERNEL_EXPECT=1 turns that into a failure (kernel-scaffold.test.ts).
*/
import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import { extractFromSource } from '../src/extraction';
import { initGrammars, loadGrammarsForLanguages } from '../src/extraction/grammars';
import { tryKernelExtract, resetKernelForTests } from '../src/extraction/kernel';
import type { ExtractionResult } 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);
const FIXTURE_DIR = path.join(__dirname, 'fixtures', 'kernel-parity');
function canon(result: ExtractionResult): { nodes: string[]; edges: string[]; refs: string[] } {
return {
nodes: result.nodes
.map(({ updatedAt: _u, ...n }) => JSON.stringify(n, Object.keys(n).sort()))
.sort(),
edges: result.edges.map((e) => JSON.stringify(e, Object.keys(e).sort())).sort(),
refs: result.unresolvedReferences
.map((r) => JSON.stringify(r, Object.keys(r).sort()))
.sort(),
};
}
const ENV_KEYS = ['CODEGRAPH_KERNEL', 'CODEGRAPH_KERNEL_LANGS'] as const;
let savedEnv: Record<string, string | undefined>;
describe.skipIf(!kernelBuilt)('kernel C# extraction parity', () => {
beforeAll(async () => {
await initGrammars();
await loadGrammarsForLanguages(['csharp']);
});
beforeEach(() => {
savedEnv = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
resetKernelForTests();
});
afterEach(() => {
for (const k of ENV_KEYS) {
if (savedEnv[k] === undefined) delete process.env[k];
else process.env[k] = savedEnv[k];
}
resetKernelForTests();
});
function assertParity(filePath: string, source: string, minNodes = 3): void {
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
delete process.env.CODEGRAPH_KERNEL;
const viaKernel = tryKernelExtract(filePath, source, 'csharp');
expect(viaKernel, `kernel extraction failed for ${filePath}`).not.toBeNull();
process.env.CODEGRAPH_KERNEL = '0';
const viaWasm = extractFromSource(filePath, source, 'csharp');
delete process.env.CODEGRAPH_KERNEL;
const k = canon(viaKernel!);
const w = canon(viaWasm);
expect(k.nodes, `${filePath}: nodes`).toEqual(w.nodes);
expect(k.edges, `${filePath}: edges`).toEqual(w.edges);
expect(k.refs, `${filePath}: refs`).toEqual(w.refs);
expect(viaWasm.nodes.length).toBeGreaterThanOrEqual(minNodes);
}
const FIXTURES: Array<{ file: string; minNodes: number }> = [
{ file: 'Torture.cs', minNodes: 40 },
{ file: 'TortureFileScoped.cs', minNodes: 8 },
{ file: 'TortureTopLevel.cs', minNodes: 2 },
];
for (const { file, minNodes } of FIXTURES) {
it(`${file}: namespaces, records, calls, holes, refs`, () => {
const src = fs.readFileSync(path.join(FIXTURE_DIR, file), 'utf8');
assertParity(`fixtures/${file}`, src, minNodes);
});
// CRLF variant — the shape every Windows autocrlf checkout has. Derived in
// memory so no platform or editor can silently normalize it away; pins the
// JS-multiline-^ docstring semantics for `///` runs (#1329).
it(`${file} CRLF parity`, () => {
const src = fs.readFileSync(path.join(FIXTURE_DIR, file), 'utf8');
const crlf = src.replace(/(?<!\r)\n/g, '\r\n');
assertParity(`fixtures/${file} (crlf)`, crlf, minNodes);
});
}
// Cheap unit pins for the quirks a future grammar bump would silently move
// (checklist §fixtures item 6) — parity is the assertion; the wasm arm is
// the behavior oracle.
const MICROS: Array<{ name: string; source: string; minNodes: number }> = [
{
name: 'alias-import to a qualified target keeps generic args in moduleName',
source: 'using Coll = System.Collections.Generic.Dictionary<string, int>;\n',
minNodes: 2,
},
{
name: 'alias-import to a bare identifier captures the ALIAS name',
source: 'using Short = SomeType;\n',
minNodes: 2,
},
{
name: 'C#12 primary-ctor base args emit the garbage `(repo)` extends ref',
source: 'public class Svc(IRepo repo) : Base(repo), IThing { }\n',
minNodes: 2,
},
{
name: 'enum underlying type emits an extends ref named `byte`',
source: 'public enum E : byte { A = 1, B }\n',
minNodes: 4,
},
{
name: 'nameof(...) emits a calls ref named `nameof`',
source: 'public class C { void M() { var n = nameof(C); } }\n',
minNodes: 3,
},
{
name: 'this./base. callee prefixes are kept raw',
source: 'public class C { void M() { this.Run(1); base.Go(); } }\n',
minNodes: 3,
},
{
name: 'bare-identifier-typed property loses its type in the signature',
source: 'public class C { public Widget Parent { get; set; } }\n',
minNodes: 3,
},
{
name: 'bodiless struct mints no node; bodiless record still does',
source: 'public record Empty;\n',
minNodes: 2,
},
];
for (const m of MICROS) {
it(`micro: ${m.name}`, () => {
assertParity(`micro/${m.name.replace(/[^a-z0-9]+/gi, '-')}.cs`, m.source, m.minNodes);
});
}
it('files with parse errors defer to the wasm extractor (recovery is encoding-dependent)', () => {
const broken = 'class F { void M( { return }} 12 (\n';
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
delete process.env.CODEGRAPH_KERNEL;
expect(tryKernelExtract('src/Broken.cs', broken, 'csharp')).toBeNull();
process.env.CODEGRAPH_KERNEL = '0';
const viaWasm = extractFromSource('src/Broken.cs', broken, 'csharp');
delete process.env.CODEGRAPH_KERNEL;
expect(viaWasm.nodes.some((n) => n.kind === 'file')).toBe(true);
});
});
+1 -1
View File
@@ -36,7 +36,7 @@ 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'];
const GRAMMAR_LANGUAGES: Language[] = ['typescript', 'tsx', 'javascript', 'java', 'python', 'go', 'c', 'cpp', 'rust', 'csharp'];
describe.skipIf(!kernelBuilt)('kernel↔wasm grammar parity', () => {
beforeAll(async () => {