Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
feat: multi-language breadth tier (14 langs) + opt-in LSP edge enrichment
Shrish Dwivedi538ef45fcf1f
diff --git a/package-lock.json b/package-lock.jsonindex 4cada95d..aa85630f 100644--- a/package-lock.json+++ b/package-lock.json@@ -18,7 +18,11 @@"tree-sitter": "^0.21.1","tree-sitter-go": "^0.23.4","tree-sitter-python": "^0.21.0",- "tree-sitter-typescript": "^0.23.2"+ "tree-sitter-typescript": "^0.23.2",+ "tree-sitter-wasms": "^0.1.13",+ "vscode-jsonrpc": "^8.2.1",+ "vscode-languageserver-protocol": "^3.18.2",+ "web-tree-sitter": "^0.25.10"},"bin": {"graft": "dist/cli.js"@@ -934,6 +938,15 @@}}},+ "node_modules/tree-sitter-wasms": {+ "version": "0.1.13",+ "resolved": "https://registry.npmjs.org/tree-sitter-wasms/-/tree-sitter-wasms-0.1.13.tgz",+ "integrity": "sha512-wT+cR6DwaIz80/vho3AvSF0N4txuNx/5bcRKoXouOfClpxh/qqrF4URNLQXbbt8MaAxeksZcZd1j8gcGjc+QxQ==",+ "license": "Unlicense",+ "dependencies": {+ "tree-sitter-wasms": "^0.1.11"+ }+ },"node_modules/ts-algebra": {"version": "2.0.0","resolved": "https://registry.npmjs.org/ts-algebra/-/ts-algebra-2.0.0.tgz",@@ -979,6 +992,54 @@"integrity": "sha512-j375ScV60dom+YkPFIfTLcOiPxkN/buHz5GobjLhixFuANaNs3C9l4GmrWqejgXWJ7BbJcFYpTEUkS1Ge8bpZQ==","dev": true,"license": "MIT"+ },+ "node_modules/vscode-jsonrpc": {+ "version": "8.2.1",+ "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-8.2.1.tgz",+ "integrity": "sha512-kdjOSJ2lLIn7r1rtrMbbNCHjyMPfRnowdKjBQ+mGq6NAW5QY2bEZC/khaC5OR8svbbjvLEaIXkOq45e2X9BIbQ==",+ "license": "MIT",+ "engines": {+ "node": ">=14.0.0"+ }+ },+ "node_modules/vscode-languageserver-protocol": {+ "version": "3.18.2",+ "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.18.2.tgz",+ "integrity": "sha512-XRyDbT0Pp3sSNti3JmxVEUMySWCSi1hhM+/KUlCy1hV1zmrqpM1OwO12EAki8blhmLuIMpaJrYbo0OzGVfK2Qg==",+ "license": "MIT",+ "dependencies": {+ "vscode-jsonrpc": "9.0.1",+ "vscode-languageserver-types": "3.18.0"+ }+ },+ "node_modules/vscode-languageserver-protocol/node_modules/vscode-jsonrpc": {+ "version": "9.0.1",+ "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-9.0.1.tgz",+ "integrity": "sha512-rfuA6T75H6m5EkbhtEPzre9pT0HPcDI2MMy4+nPFIBks5J8JBAUHD4tRYSgaBOijIEC7SRkC1kKyXTLqbmh9jw==",+ "license": "MIT",+ "engines": {+ "node": ">=14.0.0"+ }+ },+ "node_modules/vscode-languageserver-types": {+ "version": "3.18.0",+ "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.18.0.tgz",+ "integrity": "sha512-8TsGPNMIMiiBdkORgRSvLjuiEIiAFtO+KssmYWxQ+uSVvlf7RjK8YKCOjPzZ+YA04jXEV7+7LvkSmHkhpNS99g==",+ "license": "MIT"+ },+ "node_modules/web-tree-sitter": {+ "version": "0.25.10",+ "resolved": "https://registry.npmjs.org/web-tree-sitter/-/web-tree-sitter-0.25.10.tgz",+ "integrity": "sha512-Y09sF44/13XvgVKgO2cNDw5rGk6s26MgoZPXLESvMXeefBf7i6/73eFurre0IsTW6E14Y0ArIzhUMmjoc7xyzA==",+ "license": "MIT",+ "peerDependencies": {+ "@types/emscripten": "^1.40.0"+ },+ "peerDependenciesMeta": {+ "@types/emscripten": {+ "optional": true+ }+ }}}}diff --git a/package.json b/package.jsonindex a5328e98..e569ff79 100644--- a/package.json+++ b/package.json@@ -63,7 +63,11 @@"tree-sitter": "^0.21.1","tree-sitter-go": "^0.23.4","tree-sitter-python": "^0.21.0",- "tree-sitter-typescript": "^0.23.2"+ "tree-sitter-typescript": "^0.23.2",+ "tree-sitter-wasms": "^0.1.13",+ "vscode-jsonrpc": "^8.2.1",+ "vscode-languageserver-protocol": "^3.18.2",+ "web-tree-sitter": "^0.25.10"},"devDependencies": {"@types/d3-force": "^3.0.10",diff --git a/scripts/build-viewer.mjs b/scripts/build-viewer.mjsindex bf4fe0ad..ba1e5950 100644--- a/scripts/build-viewer.mjs+++ b/scripts/build-viewer.mjs@@ -4,7 +4,7 @@* so `graft viz` needs no install or build step at runtime.*/import { build } from "esbuild";-import { mkdirSync, copyFileSync } from "node:fs";+import { mkdirSync, copyFileSync, readdirSync } from "node:fs";import { dirname, join } from "node:path";import { fileURLToPath } from "node:url";@@ -26,3 +26,19 @@ for (const asset of ["index.html", "style.css"]) {}console.log("viewer bundle → dist/viewer/");++// The generic (breadth) tier's tags.scm query files are runtime assets — tsc+// doesn't emit non-TS files, so copy them into dist so the published package+// (which ships `dist` only) can load them. generic.ts resolves dist/graph/queries+// first, then falls back to src/graph/queries for local dev.+const scmSrc = join(root, "src", "graph", "queries");+const scmOut = join(root, "dist", "graph", "queries");+mkdirSync(scmOut, { recursive: true });+let scmCount = 0;+for (const f of readdirSync(scmSrc)) {+ if (f.endsWith(".scm")) {+ copyFileSync(join(scmSrc, f), join(scmOut, f));+ scmCount++;+ }+}+console.log(`grammar queries → dist/graph/queries/ (${scmCount} .scm)`);diff --git a/src/cli.ts b/src/cli.tsindex da5ad539..1c5a9106 100644--- a/src/cli.ts+++ b/src/cli.ts@@ -120,6 +120,7 @@ program.option("-e, --extensions <exts...>", 'code extensions to include (e.g. ".ts" ".py")').option("-j, --concurrency <n>", "files summarized in parallel during --deep (default 5)").option("--no-reuse", "re-parse every file instead of replaying unchanged ones from the extraction cache")+ .option("--lsp", "add compiler-grade call edges via a language server if one is installed (opt-in, slower; e.g. rust-analyzer, clangd)").option("--include-dir <name>","override SKIP_DIRS for this repo's walks — repeatable (e.g. --include-dir build --include-dir tools); " +@@ -127,7 +128,7 @@ program(val: string, prev: string[]) => [...prev, val],[] as string[],)- .action(async (dir: string, opts: { deep?: boolean; extensions?: string[]; concurrency?: string; reuse?: boolean; includeDir?: string[] }) => {+ .action(async (dir: string, opts: { deep?: boolean; extensions?: string[]; concurrency?: string; reuse?: boolean; lsp?: boolean; includeDir?: string[] }) => {const concurrency = opts.concurrency ? Math.max(1, Number(opts.concurrency)) : undefined;if (opts.concurrency && !Number.isFinite(concurrency)) {console.error(`✗ --concurrency must be a number, got "${opts.concurrency}"`);@@ -214,6 +215,7 @@ programllm: deep,concurrency,reuse: opts.reuse,+ lsp: opts.lsp,onProgress: ({ phase, index, total, file }) =>process.stderr.write(`\r${phase === "enrich" ? "summarizing" : "parsing"} ${index + 1}/${total}: ${file.slice(0, 50).padEnd(50)}`,@@ -245,8 +247,9 @@ program.option("--full", "with --source: inline whole definition spans instead of the default ≤8-line crux excerpts").option("--in <path>", "narrow to nodes under this path prefix, filtered before scoring (segment-aware, like scopeOf)").option("--json", "output the result as JSON")+ .option("--no-graph-rank", "rank by lexical relevance only, without the graph-connectivity re-rank (ablation/eval)").option(...NO_REFRESH_FLAG)- .action(async (query: string, dir: string, opts: { limit: string; source?: boolean; full?: boolean; in?: string; json?: boolean; refresh?: boolean }) => {+ .action(async (query: string, dir: string, opts: { limit: string; source?: boolean; full?: boolean; in?: string; json?: boolean; refresh?: boolean; graphRank?: boolean }) => {await refreshBefore(dir, opts);const askGlobalDir = program.opts<GlobalOpts>().dir;if (readWorkspace(resolve(dir), askGlobalDir)) {@@ -258,7 +261,7 @@ programconst engine = engineFrom();let r;try {- r = engine.ask(dir, query, { limit: Number(opts.limit), source: opts.source, full: opts.full, in: opts.in });+ r = engine.ask(dir, query, { limit: Number(opts.limit), source: opts.source, full: opts.full, in: opts.in, graphRank: opts.graphRank });} catch (err) {console.error(`✗ ${err instanceof Error ? err.message : String(err)}`);process.exit(1);@@ -294,15 +297,15 @@ program.argument("[dir]", "repository root", ".").option("-e, --extensions <exts...>", "code extensions to include").option("--json", "output the drift as JSON")- .action((dir: string, opts: { extensions?: string[]; json?: boolean }) => {+ .action(async (dir: string, opts: { extensions?: string[]; json?: boolean }) => {const checkGlobalDir = program.opts<GlobalOpts>().dir;if (readWorkspace(resolve(dir), checkGlobalDir)) {- runWorkspaceCheck(resolve(dir), checkGlobalDir);+ await runWorkspaceCheck(resolve(dir), checkGlobalDir);return;}const engine = engineFrom();const r = engine.check(dir, { extensions: opts.extensions });- const g = engine.checkGraph(dir); // graph.json is only judged when it exists+ const g = await engine.checkGraph(dir); // graph.json is only judged when it exists// A layer that IS present must be in sync; a never-built layer (keyless// build skips the markdown layer) is informational, not a failure.diff --git a/src/engine.ts b/src/engine.tsindex d401e7c4..3fa93d23 100644--- a/src/engine.ts+++ b/src/engine.ts@@ -43,6 +43,8 @@ export interface GraphRunOptions {concurrency?: number;/** Replay unchanged files from the extraction cache (default true). */reuse?: boolean;+ /** Opt-in compiler-grade LSP edge enrichment (`graft build --lsp`). */+ lsp?: boolean;onProgress?: GraphBuildOptions["onProgress"];}@@ -70,8 +72,9 @@ export class Graft {return checkContext(dir, { contextDir: this.cfg.contextDir, extensions: opts.extensions });}- /** Report whether the committed `graph.json` is in sync with the code (Tier-1 diff). */- checkGraph(dir: string): GraphCheckResult {+ /** Report whether the committed `graph.json` is in sync with the code (Tier-1 diff).+ * Async because the breadth tier warms WASM grammars before re-extraction. */+ checkGraph(dir: string): Promise<GraphCheckResult> {return checkGraph(dir, { contextDir: this.cfg.contextDir });}@@ -86,6 +89,7 @@ export class Graft {summarizer: opts.llm ? this.cruxSummarizer() : undefined,concurrency: opts.concurrency,reuse: opts.reuse,+ lsp: opts.lsp,onProgress: opts.onProgress,});}@@ -95,13 +99,14 @@ export class Graft {* channel. Deterministic and $0: routes structural queries to the wiring* edges and everything else to a lexical rank over concepts + symbols.*/- ask(dir: string, query: string, opts: { limit?: number; source?: boolean; full?: boolean; in?: string } = {}): AskResult {+ ask(dir: string, query: string, opts: { limit?: number; source?: boolean; full?: boolean; in?: string; graphRank?: boolean } = {}): AskResult {return ask(dir, query, {contextDir: this.cfg.contextDir,limit: opts.limit,source: opts.source,full: opts.full,in: opts.in,+ graphRank: opts.graphRank,});}diff --git a/src/graph/build.ts b/src/graph/build.tsindex 3d5fbd5a..8124c7cc 100644--- a/src/graph/build.ts+++ b/src/graph/build.ts@@ -18,6 +18,7 @@ import { basename, dirname, resolve } from "node:path";import { walkDir } from "../ingest/fs.js";import { contextDirFor, ensureGitignored, ensureSearchable } from "../context/node-file.js";import { extractFile, languageLabelOf, languageOf, type RawEdge } from "./extract.js";+import { extractGeneric, genericLangOf, warmGenericGrammars } from "./generic.js";import { contentHash } from "../util/id.js";import { relPosix } from "../util/paths.js";import { readSourceFile } from "../util/source.js";@@ -78,6 +79,11 @@ export interface GraphBuildOptions {* the pre-query refresh (`graph/refresh.ts`); an explicit `graft build` never* sets it. See the write block in {@link buildGraph} for why the split exists. */graphOnly?: boolean;+ /** Opt-in compiler-grade edge enrichment via a language server (`graft build+ * --lsp`): adds `lsp_resolved` call edges the AST resolver couldn't (member+ * calls, breadth-tier calls). Off by default — needs a server on PATH and is+ * slower; the graph is fully functional without it. */+ lsp?: boolean;/** Run the Tier-2 LLM meaning pass. Absent → Tier-1 only (cache is still preserved). */summarizer?: CruxSummarizer;/** Max files summarized in parallel during the Tier-2 pass. Default is set in enrich. */@@ -174,11 +180,21 @@ export async function buildGraph(let parsed = 0;let reused = 0;+ // Breadth tier: WASM grammars load asynchronously, so warm the ones this repo+ // needs ONCE here (buildGraph is async) before the synchronous parse loop below+ // can call extractGeneric. Depth-tier (native) grammars need no warmup.+ await warmGenericGrammars(+ new Set(files.map((f) => genericLangOf(f.abs)?.name).filter((n): n is string => !!n)),+ );+files.forEach((f, i) => {const rel = f.rel;opts.onProgress?.({ phase: "parse", index: i, total: files.length, file: rel });- const lang = languageOf(f.abs)!;- const label = languageLabelOf(f.abs)!;+ // Depth tier (hand-written, native grammar) if a language claims the file;+ // otherwise the breadth tier (generic tags.scm over a WASM grammar).+ const lang = languageOf(f.abs);+ const generic = lang ? null : genericLangOf(f.abs);+ const label = languageLabelOf(f.abs) ?? generic?.name ?? "unknown";const cached = priorExtract.files[rel];// Every file is read and hashed, every build — only the *parse* is memoized.@@ -225,7 +241,9 @@ export async function buildGraph(parsed++;try {- const { nodes: fileNodes, rawEdges: fileEdges } = extractFile(rel, source, lang);+ const { nodes: fileNodes, rawEdges: fileEdges } = lang+ ? extractFile(rel, source, lang)+ : extractGeneric(rel, source, generic!.name);nodes.push(...fileNodes);rawEdges.push(...fileEdges);sources.set(rel, source);@@ -279,6 +297,16 @@ export async function buildGraph(edges,};+ // Opt-in compiler-grade enrichment (adds lsp_resolved call edges in place).+ // Runs on the assembled graph so callee positions map back to nodes; never+ // touches the extraction cache (Tier-1 stays pristine, cold==incremental).+ if (opts.lsp) {+ const { enrichWithLsp } = await import("./lsp/enrich.js");+ const r = await enrichWithLsp(graph, root);+ graph.meta.edgeCount = graph.edges.length;+ opts.onProgress?.({ phase: "enrich", index: r.added, total: r.queried, file: `lsp:${r.server ?? "none"}` });+ }+const graphPath = writeGraph(graph, outDir);// `ask`'s token/IDF sidecar — moves per-query corpus tokenization to build// time (~45% of query time on a 32k-node graph, profiled). Lives in thediff --git a/src/graph/check.ts b/src/graph/check.tsindex ecd18134..59a602a8 100644--- a/src/graph/check.ts+++ b/src/graph/check.ts@@ -21,6 +21,7 @@ import { resolve } from "node:path";import { relPosix } from "../util/paths.js";import { contextDirFor } from "../context/node-file.js";import { extractFile, languageOf } from "./extract.js";+import { extractGeneric, genericLangOf, warmGenericGrammars } from "./generic.js";import { listSourceFiles } from "./build.js";import { readGraph, wiringPath } from "./write.js";import { readSourceFile } from "../util/source.js";@@ -41,7 +42,14 @@ export interface GraphCheckOptions {contextDir?: string;}-export function checkGraph(dir: string, opts: GraphCheckOptions = {}): GraphCheckResult {+// async: the breadth tier's WASM grammars load asynchronously and must be warmed+// before the (synchronous) re-extraction below, exactly as buildGraph does — else+// breadth-tier files (.rs, …) would re-extract as empty here and read as `removed`+// against a graph that built them, so `graft check` would never report OK.+export async function checkGraph(+ dir: string,+ opts: GraphCheckOptions = {},+): Promise<GraphCheckResult> {const root = resolve(dir);const outDir = contextDirFor(root, opts.contextDir);@@ -62,9 +70,14 @@ export function checkGraph(dir: string, opts: GraphCheckOptions = {}): GraphChec}// Freshly extract Tier-1 nodes from the code on disk (same file set as build).+ const sourceFiles = listSourceFiles(root, outDir);+ await warmGenericGrammars(+ new Set(sourceFiles.map((f) => genericLangOf(f)?.name).filter((n): n is string => !!n)),+ );const current = new Map<string, string>(); // id → body_hash- for (const file of listSourceFiles(root, outDir)) {- const lang = languageOf(file)!;+ for (const file of sourceFiles) {+ const lang = languageOf(file);+ const generic = lang ? null : genericLangOf(file);let source: string | null;try {source = readSourceFile(file);@@ -73,7 +86,9 @@ export function checkGraph(dir: string, opts: GraphCheckOptions = {}): GraphChec}if (source === null) continue; // unsupported encoding (e.g. UTF-16BE)try {- const { nodes } = extractFile(relPosix(root, file), source, lang);+ const { nodes } = lang+ ? extractFile(relPosix(root, file), source, lang)+ : extractGeneric(relPosix(root, file), source, generic!.name);for (const n of nodes) current.set(n.id, n.body_hash);} catch {// parse failure → skip; the committed nodes for this file become `removed`.diff --git a/src/graph/generic.ts b/src/graph/generic.tsnew file mode 100644index 00000000..6a99c045--- /dev/null+++ b/src/graph/generic.ts@@ -0,0 +1,334 @@+/**+ * Generic "breadth" extractor tier — one language-agnostic extractor over any+ * tree-sitter grammar + its `tags.scm` (the standard tree-sitter tags convention). This is how+ * graft covers the long tail of languages for ~one registry row each, instead of+ * a hand-written extractor per language (the depth tier in extract.ts).+ *+ * Grammars are WASM (`tree-sitter-wasms` bundle) loaded via `web-tree-sitter`, so+ * a new language needs no native node-gyp build. Loading is async (WASM init), so+ * callers MUST `await warmGenericGrammars([...])` once before the synchronous+ * `extractGeneric()` is used in a build/check loop. If a grammar isn't warmed,+ * `extractGeneric` degrades to a file node only (never throws).+ *+ * What it emits (signature-only): a file node + one node per `@definition.<kind>`+ * capture, and bare-name `calls` raw edges from `@reference.call` attributed to+ * the innermost enclosing definition. resolve.ts then resolves those calls by+ * name for free (same-file → extracted, unique-global → inferred). Member-call+ * receiver typing (recvType) is NOT produced here — that needs a per-language+ * binding pass; the opt-in LSP tier fills that gap for popular languages.+ */+import { readFileSync } from "node:fs";+import { fileURLToPath } from "node:url";+import { dirname, join } from "node:path";+import { createRequire } from "node:module";+import { contentHash } from "../util/id.js";+import type { Kind, NodeV1 } from "./types.js";+import type { ExtractResult, RawEdge } from "./extract.js";++const HERE = dirname(fileURLToPath(import.meta.url));+const require = createRequire(import.meta.url);+// queries/ ships beside the compiled JS (copied by the build); fall back to src.+const QUERY_DIRS = [join(HERE, "queries"), join(HERE, "..", "..", "src", "graph", "queries")];++/** A breadth-tier language: graft name, file extensions, and the wasm basename+ * in tree-sitter-wasms/out/tree-sitter-<wasm>.wasm. One row per language. */+export interface GenericLang {+ name: string;+ exts: string[];+ wasm: string;+}++/** The breadth registry. Add a row + a queries/<name>.scm to support a language.+ * Extensions here must NOT collide with the depth tier's EXTENSIONS (extract.ts). */+export const GENERIC_LANGS: readonly GenericLang[] = [+ { name: "rust", exts: [".rs"], wasm: "rust" },+ { name: "java", exts: [".java"], wasm: "java" },+ { name: "c", exts: [".c", ".h"], wasm: "c" },+ { name: "cpp", exts: [".cpp", ".cc", ".cxx", ".hpp", ".hh"], wasm: "cpp" },+ { name: "ruby", exts: [".rb"], wasm: "ruby" },+ { name: "php", exts: [".php"], wasm: "php" },+ { name: "c_sharp", exts: [".cs"], wasm: "c_sharp" },+ // These ship a tags.scm (calls + symbols); ocaml/zig have none and use the+ // node-kind walker fallback (symbols only) — still one row, zero query.+ { name: "kotlin", exts: [".kt", ".kts"], wasm: "kotlin" },+ { name: "scala", exts: [".scala", ".sc"], wasm: "scala" },+ { name: "swift", exts: [".swift"], wasm: "swift" },+ { name: "elixir", exts: [".ex", ".exs"], wasm: "elixir" },+ { name: "solidity", exts: [".sol"], wasm: "solidity" },+ { name: "ocaml", exts: [".ml", ".mli"], wasm: "ocaml" },+ { name: "zig", exts: [".zig"], wasm: "zig" },+];++const byExt = new Map<string, GenericLang>();+for (const l of GENERIC_LANGS) for (const e of l.exts) byExt.set(e, l);++/** The generic language for a path, or null if no breadth grammar claims it. */+export function genericLangOf(path: string): GenericLang | null {+ const lower = path.toLowerCase();+ for (const [ext, l] of byExt) if (lower.endsWith(ext)) return l;+ return null;+}++// tags.scm @definition.<X> → graft Kind (types.ts). Unmapped → "function".+const KIND: Record<string, Kind> = {+ function: "function", method: "method", class: "class", interface: "interface",+ type: "type", struct: "struct", enum: "enum", module: "module",+ constant: "constant", variable: "variable", field: "variable",+ object: "class", property: "variable", // Scala object; Swift/Scala property+};++// Loaded grammars + compiled tags queries, keyed by graft lang name. Populated by+// warmGenericGrammars; read synchronously by extractGeneric.+interface Loaded { language: unknown; query: unknown | null }+const loaded = new Map<string, Loaded>();+let tsMod: typeof import("web-tree-sitter") | null = null;+let initPromise: Promise<void> | null = null;++function requireWasm(wasm: string): Buffer | null {+ // Resolve the grammar wasm from the tree-sitter-wasms bundle.+ try {+ const p = require.resolve(`tree-sitter-wasms/out/tree-sitter-${wasm}.wasm`);+ return readFileSync(p);+ } catch {+ return null;+ }+}++function loadQuery(name: string): string | null {+ for (const dir of QUERY_DIRS) {+ try {+ const raw = readFileSync(join(dir, `${name}.scm`), "utf8");+ // Sanitize editor-specific query predicates the tree-sitter Query compiler rejects.+ return raw.replace(+ /\(#(?:strip!|set!|set-adjacent!|select-adjacent!|make-range!|offset!|gsub!)[^()]*\)/g,+ "",+ );+ } catch {+ /* try next dir */+ }+ }+ return null;+}++/** Warm the WASM grammars for the given graft lang names. Idempotent; must be+ * awaited once before extractGeneric is used in a sync loop. Unknown/unavailable+ * grammars are silently skipped (their files then extract as file-only). */+export async function warmGenericGrammars(langNames: Iterable<string>): Promise<void> {+ const want = new Set(langNames);+ const need = [...want].filter((n) => !loaded.has(n) && GENERIC_LANGS.some((l) => l.name === n));+ if (need.length === 0) return;+ if (!tsMod) {+ tsMod = await import("web-tree-sitter");+ initPromise = initPromise ?? tsMod.Parser.init();+ }+ await initPromise;+ const { Language, Query } = tsMod;+ for (const name of need) {+ const row = GENERIC_LANGS.find((l) => l.name === name)!;+ const bytes = requireWasm(row.wasm);+ if (!bytes) continue;+ try {+ const language = await Language.load(bytes);+ const scm = loadQuery(name);+ let query: unknown | null = null;+ if (scm) {+ try { query = new Query(language, scm); } catch { query = null; }+ }+ loaded.set(name, { language, query });+ } catch {+ /* grammar failed to instantiate — skip; files extract as file-only */+ }+ }+}++/** True if a grammar has been warmed for this lang (else extractGeneric is file-only). */+export function isWarm(langName: string): boolean {+ return loaded.has(langName);+}++const PARSE_CHUNK = 16384; // <32KB slices — same tree-sitter limit workaround as extract.ts++function fileNode(rel: string, source: string): NodeV1 {+ return {+ id: rel, name: rel.split("/").pop() ?? rel, kind: "file", path: rel,+ span: `L1-L${Math.max(1, source.split("\n").length)}`, signature: null,+ exported: true, origin: "generic", body_hash: contentHash(source),+ chars: Buffer.byteLength(source), summary_state: "pending", summary: null, crux: null,+ };+}++interface Def { id: string; startIndex: number; endIndex: number }++/** Extract a single generic-tier file. Synchronous; needs the grammar pre-warmed.+ * Uses the grammar's compiled tags.scm when present (symbols + call edges);+ * otherwise falls back to a node-kind tree walker (symbols only) so ANY warmed+ * grammar yields a graph even with no vendored query. */+export function extractGeneric(rel: string, source: string, langName: string): ExtractResult {+ const nodes: NodeV1[] = [fileNode(rel, source)];+ const rawEdges: RawEdge[] = [];+ const entry = loaded.get(langName);+ if (!entry || !tsMod) return { nodes, rawEdges };++ let tree;+ try {+ const parser = new tsMod.Parser();+ parser.setLanguage(entry.language as never);+ tree = parser.parse((i: number) => source.slice(i, i + PARSE_CHUNK));+ } catch {+ return { nodes, rawEdges };+ }+ if (!tree) return { nodes, rawEdges };++ const minted = new Set<string>([rel]);+ const lines = source.split("\n");+ const defs: Def[] = [];+ // One definition per source span: a grammar's tags.scm can capture the same node+ // under two @definition kinds (Swift `func` is method AND function), and the+ // walker can revisit a nested match — both would emit near-duplicate nodes.+ const spanSeen = new Set<number>();+ // Mint one definition node from a whole-definition tree node. Shared by both+ // the tags.scm path and the walker fallback so id/span/signature/body_text are+ // built identically.+ const mkDef = (name: string, kind: Kind, whole: TsNode): void => {+ if (spanSeen.has(whole.startIndex)) return;+ spanSeen.add(whole.startIndex);+ const idBase = `${rel}#${name}`;+ let id = idBase, n = 2;+ while (minted.has(id)) id = `${idBase}~${n++}`;+ minted.add(id);+ const startRow = whole.startPosition.row, endRow = whole.endPosition.row;+ const sigLine = (lines[startRow] ?? "").trim().replace(/\s*\{?\s*$/, "");+ nodes.push({+ id, name, kind, path: rel,+ span: `L${startRow + 1}-L${endRow + 1}`,+ signature: sigLine || null, exported: true, origin: "generic",+ body_hash: contentHash(source.slice(whole.startIndex, whole.endIndex)),+ body_text: source.slice(whole.startIndex, whole.endIndex).replace(/\s+/g, " ").slice(0, 5000),+ summary_state: "pending", summary: null, crux: null,+ });+ defs.push({ id, startIndex: whole.startIndex, endIndex: whole.endIndex });+ };++ if (entry.query) {+ tagsExtract(entry.query, tree.rootNode as TsNode, rel, mkDef, defs, rawEdges);+ } else {+ walkExtract(tree.rootNode as TsNode, mkDef); // no tags.scm → symbols only+ }+ return { nodes, rawEdges };+}++/** tags.scm path: @definition.<kind> → nodes, @reference.call/@reference.send →+ * bare-name call edges attributed to the innermost enclosing definition. */+function tagsExtract(+ query: unknown,+ root: TsNode,+ rel: string,+ mkDef: (name: string, kind: Kind, whole: TsNode) => void,+ defs: Def[],+ rawEdges: RawEdge[],+): void {+ const q = query as { matches(n: unknown): Array<{ captures: Array<{ name: string; node: TsNode }> }> };+ const matches = q.matches(root);+ // Some grammars' tags.scm capture a definition's own NAME token as a+ // @reference.call too (Ruby tags `def foo`'s `foo` as both @name and a call),+ // producing bogus self-loops (foo→foo). Skip any call at a definition's name token.+ const defNameAt = new Set<number>();+ const calls: Array<{ name: string; at: number }> = [];+ for (const m of matches) {+ const cap: Record<string, TsNode> = {};+ for (const c of m.captures) cap[c.name] = c.node;+ const defKey = Object.keys(cap).find((k) => k.startsWith("definition."));+ if (defKey && cap.name) {+ defNameAt.add(cap.name.startIndex);+ mkDef(cap.name.text, KIND[defKey.slice("definition.".length)] ?? "function", defScope(cap[defKey]));+ }+ if (("reference.call" in cap || "reference.send" in cap) && cap.name)+ calls.push({ name: cap.name.text, at: cap.name.startIndex });+ }+ for (const c of calls) {+ if (defNameAt.has(c.at)) continue;+ const enc = defs+ .filter((d) => d.startIndex <= c.at && c.at < d.endIndex)+ .sort((a, b) => (a.endIndex - a.startIndex) - (b.endIndex - b.startIndex))[0];+ rawEdges.push({ source: enc ? enc.id : rel, relation: "calls", file: rel, name: c.name });+ }+}++// Node-type → Kind for the walker fallback. A node is a definition when its type+// ends with a declaration-ish suffix AND names a construct — matched most-specific+// first (method before function). Covers the common tree-sitter grammar vocab+// without any per-language query.+const DEF_SUFFIX = /(declaration|definition|_item|_specifier|_decl|_def|_binding)$/;+function classifyKind(type: string): Kind | null {+ const t = type.toLowerCase();+ if (!DEF_SUFFIX.test(t) && !/^(class|struct|enum|interface|trait|module|namespace)/.test(t)) return null;+ if (/method|constructor/.test(t)) return "method";+ if (/func|function|def\b|subroutine|procedure/.test(t)) return "function";+ if (/class/.test(t)) return "class";+ if (/struct|record/.test(t)) return "struct";+ if (/interface|trait|protocol/.test(t)) return "interface";+ if (/enum/.test(t)) return "enum";+ if (/module|namespace|package|mod_/.test(t)) return "module";+ if (/const/.test(t)) return "constant";+ if (/typedef|type_alias|type_def|alias/.test(t)) return "type";+ if (/type/.test(t)) return "type";+ if (/(^|_)(val|var|let|field|property)/.test(t)) return "variable";+ return null;+}+/** The declared name of a node: its `name` field, else the first identifier-ish+ * descendant within a couple of levels. */+function nodeName(node: TsNode): string | null {+ const byField = node.childForFieldName?.("name");+ if (byField?.text) return byField.text;+ const stack: Array<{ n: TsNode; d: number }> = [{ n: node, d: 0 }];+ while (stack.length) {+ const { n, d } = stack.shift()!;+ if (d > 0 && /identifier|name/.test(n.type) && n.text && !n.text.includes("\n")) return n.text;+ if (d < 3) for (let i = 0; i < (n.namedChildCount ?? 0); i++) {+ const c = n.namedChild?.(i);+ if (c) stack.push({ n: c, d: d + 1 });+ }+ }+ return null;+}+/** Walker fallback: DFS every named node, emit a def for each classified one+ * (symbols only — no call resolution without a query). */+function walkExtract(root: TsNode, mkDef: (name: string, kind: Kind, whole: TsNode) => void): void {+ const visit = (n: TsNode): void => {+ const kind = classifyKind(n.type);+ if (kind) {+ const name = nodeName(n);+ if (name) mkDef(name, kind, n);+ }+ for (let i = 0; i < (n.namedChildCount ?? 0); i++) {+ const c = n.namedChild?.(i);+ if (c) visit(c);+ }+ };+ visit(root);+}++interface TsNode {+ type: string;+ text: string;+ startIndex: number;+ endIndex: number;+ startPosition: { row: number; column: number };+ endPosition: { row: number; column: number };+ parent: TsNode | null;+ namedChildCount?: number;+ namedChild?(i: number): TsNode | null;+ childForFieldName?(field: string): TsNode | null;+}++// Some grammars' tags.scm put @definition.<X> on a narrow node (C tags the+// `function_declarator`, not the whole `function_definition` with its body), so+// the capture's span stops before the body and calls inside it can't be+// attributed to the function. Expand up to the outermost enclosing+// declaration/definition node so a def's span covers its body.+const DEF_CONTAINER = /(definition|declaration|specifier|_item)$/;+function defScope(node: TsNode): TsNode {+ let n = node;+ while (n.parent && DEF_CONTAINER.test(n.parent.type)) n = n.parent;+ return n;+}diff --git a/src/graph/lsp/client.ts b/src/graph/lsp/client.tsnew file mode 100644index 00000000..351f4a91--- /dev/null+++ b/src/graph/lsp/client.ts@@ -0,0 +1,177 @@+/**+ * A minimal LSP client for the opt-in `--lsp` enrichment pass — just enough to+ * pull compiler-grade call edges out of a language server: spawn it over stdio,+ * initialize, open documents, and run the call-hierarchy requests. Built on+ * `vscode-jsonrpc` (we don't hand-roll the framing). Everything is best-effort:+ * a server that is missing, slow, or errors degrades to "no enrichment", never a+ * crash — the AST graph stands on its own.+ */+import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";+import { pathToFileURL } from "node:url";+import { readFileSync } from "node:fs";+import {+ createMessageConnection,+ StreamMessageReader,+ StreamMessageWriter,+ type MessageConnection,+} from "vscode-jsonrpc/node.js";++export interface LspPosition { line: number; character: number } // 0-indexed+export interface LspRange { start: LspPosition; end: LspPosition }+export interface CallHierarchyItem {+ name: string;+ kind: number;+ uri: string;+ range: LspRange;+ selectionRange: LspRange;+}++const uriOf = (abs: string): string => pathToFileURL(abs).toString();++export class LspClient {+ private proc: ChildProcessWithoutNullStreams;+ private conn: MessageConnection;+ private opened = new Set<string>();+ private ready = false;+ private spawnFailed = false;++ constructor(+ command: string,+ args: string[],+ private readonly root: string,+ private readonly languageId: string,+ private readonly callTimeoutMs = 15000,+ ) {+ this.proc = spawn(command, args, { cwd: root, stdio: ["pipe", "pipe", "pipe"] });+ // ENOENT (bad path) OR an immediate exit (e.g. a rustup shim whose component+ // isn't installed) must fail fast, not hang a request for the full timeout.+ this.proc.on("error", () => { this.spawnFailed = true; });+ // An immediate exit (shim without its component, crash on startup) must fail+ // fast so initialize bails instead of waiting out the timeout; once we've+ // marked !ready, in-flight queries no-op. Firing during dispose is harmless.+ this.proc.on("exit", () => { this.spawnFailed = true; this.ready = false; });+ this.proc.stderr.resume(); // drain; servers are chatty on stderr+ // Swallow write-after-destroy (EPIPE / ERR_STREAM_DESTROYED) when the server+ // closes its stdin during/after shutdown — otherwise it surfaces as an+ // unhandled stream error and taints the process exit code.+ this.proc.stdin.on("error", () => {});+ this.conn = createMessageConnection(+ new StreamMessageReader(this.proc.stdout),+ new StreamMessageWriter(this.proc.stdin),+ );+ // Answer the reverse-RPC requests every server makes during startup so it+ // doesn't block waiting on us; we take defaults for all of them.+ this.conn.onRequest("workspace/configuration", (p: { items: unknown[] }) => (p.items ?? []).map(() => ({})));+ this.conn.onRequest("client/registerCapability", () => null);+ this.conn.onRequest("window/workDoneProgress/create", () => null);+ this.conn.onRequest("workspace/applyEdit", () => ({ applied: false }));+ this.conn.onNotification(() => {}); // swallow all notifications (diagnostics, progress, logs)+ // The connection/streams can error or close asynchronously (server exits,+ // stdin destroyed) — absorb both so a late write never becomes an unhandled+ // exception that taints the build's exit code.+ this.conn.onError(() => { this.ready = false; });+ this.conn.onClose(() => { this.ready = false; });+ this.conn.listen();+ }++ /** Safe to write to the server's stdin right now? */+ private canWrite(): boolean {+ return this.ready && this.proc.stdin.writable && !this.proc.stdin.destroyed;+ }++ /** A request with a timeout so a wedged server can't hang the whole build. */+ private call<T>(method: string, params: unknown): Promise<T> {+ if (!this.canWrite()) return Promise.resolve(null as T);+ return new Promise<T>((resolve) => {+ let done = false;+ const t = setTimeout(() => { if (!done) { done = true; resolve(null as T); } }, this.callTimeoutMs);+ this.conn.sendRequest<T>(method, params).then(+ (r) => { if (!done) { done = true; clearTimeout(t); resolve(r); } },+ () => { if (!done) { done = true; clearTimeout(t); resolve(null as T); } },+ );+ });+ }++ async initialize(): Promise<boolean> {+ // Let a synchronous spawn failure (ENOENT) surface before we wait on a+ // response that will never come.+ await new Promise((r) => setTimeout(r, 50));+ if (this.spawnFailed || this.proc.pid === undefined) return false;+ const rootUri = uriOf(this.root);+ // Cold-workspace load (rust-analyzer/clangd index the project) can take a+ // while — give initialize a generous timeout, separate from per-request.+ const init = await new Promise<unknown>((resolve) => {+ const t = setTimeout(() => resolve(null), 120000);+ this.conn.sendRequest("initialize", {+ processId: process.pid,+ rootUri,+ workspaceFolders: [{ uri: rootUri, name: "root" }],+ capabilities: {+ textDocument: {+ callHierarchy: { dynamicRegistration: false },+ synchronization: { dynamicRegistration: false },+ },+ workspace: { workspaceFolders: true, configuration: true },+ },+ }).then((r) => { clearTimeout(t); resolve(r); }, () => { clearTimeout(t); resolve(null); });+ });+ if (!init) return false;+ this.conn.sendNotification("initialized", {});+ this.ready = true;+ return true;+ }++ /** Open a document (idempotent). Servers resolve cross-file refs from disk, but+ * the LSP spec wants the queried document opened first. */+ didOpen(abs: string): void {+ if (!this.canWrite() || this.opened.has(abs)) return;+ let text = "";+ try { text = readFileSync(abs, "utf8"); } catch { return; }+ this.opened.add(abs);+ try {+ this.conn.sendNotification("textDocument/didOpen", {+ textDocument: { uri: uriOf(abs), languageId: this.languageId, version: 1, text },+ });+ } catch { /* stream closed mid-write */ }+ }++ /** Poll one probe position until call-hierarchy returns something — servers+ * like rust-analyzer/clangd index AFTER `initialized` and answer empty until+ * ready. Returns true once ready (or false at the cap). */+ async waitUntilReady(abs: string, pos: LspPosition, maxMs = 90000): Promise<boolean> {+ this.didOpen(abs);+ const deadline = Date.now() + maxMs;+ while (Date.now() < deadline) {+ const items = await this.prepareCallHierarchy(abs, pos);+ if (items.length) return true;+ await new Promise((r) => setTimeout(r, 2000));+ }+ return false;+ }++ async prepareCallHierarchy(abs: string, pos: LspPosition): Promise<CallHierarchyItem[]> {+ if (!this.ready) return [];+ const r = await this.call<CallHierarchyItem[] | null>("textDocument/prepareCallHierarchy", {+ textDocument: { uri: uriOf(abs) },+ position: pos,+ });+ return r ?? [];+ }++ /** The callees of an item — returns each outgoing call's `to` target. */+ async outgoingCalls(item: CallHierarchyItem): Promise<CallHierarchyItem[]> {+ if (!this.ready) return [];+ const r = await this.call<Array<{ to: CallHierarchyItem }> | null>("callHierarchy/outgoingCalls", { item });+ return (r ?? []).map((c) => c.to).filter(Boolean);+ }++ async dispose(): Promise<void> {+ // Skip the graceful LSP shutdown/exit handshake: writing `exit` to a server+ // that has already closed its stdin throws ERR_STREAM_DESTROYED and taints+ // the exit code. The graph is already built; just tear the process down.+ this.ready = false;+ try { this.conn.dispose(); } catch { /* ignore */ }+ try { this.proc.stdin.destroy(); } catch { /* ignore */ }+ try { this.proc.kill("SIGKILL"); } catch { /* ignore */ }+ }+}diff --git a/src/graph/lsp/enrich.ts b/src/graph/lsp/enrich.tsnew file mode 100644index 00000000..d73e6002--- /dev/null+++ b/src/graph/lsp/enrich.ts@@ -0,0 +1,131 @@+/**+ * Opt-in LSP enrichment (`graft build --lsp`): add compiler-grade call edges the+ * AST resolver couldn't — chiefly member calls (`obj.foo()`) whose receiver type+ * graft can't infer, and every call in the generic breadth tier (which has no+ * receiver typing at all). For each function/method node we ask the language+ * server for its outgoing calls (call hierarchy) and map each callee's definition+ * back to a graft node, adding a `calls` edge stamped `lsp_resolved`. Precision is+ * the server's (compiler-grade), so this closes the edge-recall gap WITHOUT the+ * name-guessing that halved precision (see resolve.ts). Best-effort: no server /+ * a timeout / an error → the graph is returned unchanged.+ */+import { join } from "node:path";+import { readFileSync } from "node:fs";+import { fileURLToPath } from "node:url";+import { relPosix } from "../../util/paths.js";+import { languageLabelOf } from "../extract.js";+import { genericLangOf } from "../generic.js";+import type { GraphV1, NodeV1, EdgeV1 } from "../types.js";+import { LspClient, type CallHierarchyItem } from "./client.js";+import { pickServer } from "./registry.js";++const CALLABLE = new Set<NodeV1["kind"]>(["function", "method"]);+const DEFN = new Set<NodeV1["kind"]>(["function", "method", "class", "struct", "interface", "type", "enum"]);++const langOf = (path: string): string | null => genericLangOf(path)?.name ?? languageLabelOf(path);++interface Span { node: NodeV1; from: number; to: number }+const parseSpan = (s: string): [number, number] | null => {+ const m = /^L(\d+)-L(\d+)$/.exec(s);+ return m ? [Number(m[1]), Number(m[2])] : null;+};++export interface LspEnrichResult { added: number; queried: number; server: string | null }++export async function enrichWithLsp(+ graph: GraphV1,+ root: string,+ opts: { onProgress?: (done: number, total: number) => void; maxNodes?: number } = {},+): Promise<LspEnrichResult> {+ const languagesPresent = new Set<string>();+ for (const n of graph.nodes) { const l = langOf(n.path); if (l) languagesPresent.add(l); }+ const server = pickServer(languagesPresent);+ if (!server) return { added: 0, queried: 0, server: null };++ // Index nodes by file for both source selection and callee→node mapping.+ const byId = new Map(graph.nodes.map((n) => [n.id, n]));+ const spansByFile = new Map<string, Span[]>();+ for (const n of graph.nodes) {+ if (n.kind === "file" || !DEFN.has(n.kind)) continue;+ const sp = parseSpan(n.span); if (!sp) continue;+ const list = spansByFile.get(n.path) ?? [];+ list.push({ node: n, from: sp[0], to: sp[1] });+ spansByFile.set(n.path, list);+ }+ // The definition node covering a 1-indexed line in a file: innermost (smallest span).+ const nodeAt = (rel: string, line: number): NodeV1 | null => {+ const cands = (spansByFile.get(rel) ?? []).filter((s) => s.from <= line && line <= s.to);+ if (!cands.length) return null;+ return cands.sort((a, b) => (a.to - a.from) - (b.to - b.from))[0].node;+ };++ // Source nodes to query: callable nodes in files this server handles.+ const serverLangs = new Set(server.languages);+ let sources = graph.nodes.filter((n) => CALLABLE.has(n.kind) && serverLangs.has(langOf(n.path) ?? ""));+ if (opts.maxNodes && sources.length > opts.maxNodes) sources = sources.slice(0, opts.maxNodes);++ const client = new LspClient(server.command, server.args, root, server.languageId);+ if (!(await client.initialize())) { await client.dispose(); return { added: 0, queried: 0, server: server.command }; }++ const existing = new Set(graph.edges.map((e) => `${e.source}\0${e.relation}\0${e.target}`));+ const fileLines = new Map<string, string[]>();+ const linesOf = (rel: string): string[] => {+ if (!fileLines.has(rel)) {+ try { fileLines.set(rel, readFileSync(join(root, rel), "utf8").split("\n")); }+ catch { fileLines.set(rel, []); }+ }+ return fileLines.get(rel)!;+ };++ // The name-token position of a definition (0-indexed), scanning its first few+ // lines for the name — call hierarchy needs a position ON the symbol.+ const namePos = (src: NodeV1): { line: number; character: number } | null => {+ const sp = parseSpan(src.span); if (!sp) return null;+ const lines = linesOf(src.path);+ for (let ln = sp[0] - 1; ln < Math.min(sp[0] + 2, lines.length); ln++) {+ const c = (lines[ln] ?? "").indexOf(src.name);+ if (c >= 0) return { line: ln, character: c };+ }+ return null;+ };++ // Wait for the server to finish indexing (it answers call-hierarchy empty+ // until ready). Warm up on the first source node that has a findable position.+ const warm = sources.find((s) => namePos(s));+ if (warm) {+ const wp = namePos(warm)!;+ if (!(await client.waitUntilReady(join(root, warm.path), wp))) {+ await client.dispose();+ return { added: 0, queried: 0, server: server.command }; // never became ready+ }+ }++ let added = 0, queried = 0;+ for (const src of sources) {+ const abs = join(root, src.path);+ client.didOpen(abs);+ const pos = namePos(src);+ if (!pos) continue;++ const items = await client.prepareCallHierarchy(abs, pos);+ if (!items.length) continue;+ queried++;+ opts.onProgress?.(queried, sources.length);+ const callees = await client.outgoingCalls(items[0]);+ for (const callee of callees) {+ let calleeAbs: string;+ try { calleeAbs = fileURLToPath(callee.uri); } catch { continue; }+ if (!calleeAbs.startsWith(root)) continue; // external/dependency — skip+ const rel = relPosix(root, calleeAbs);+ const target = nodeAt(rel, (callee.selectionRange?.start.line ?? callee.range.start.line) + 1);+ if (!target || target.id === src.id) continue;+ const key = `${src.id}\0calls\0${target.id}`;+ if (existing.has(key)) continue;+ existing.add(key);+ graph.edges.push({ source: src.id, target: target.id, relation: "calls", confidence: "lsp_resolved" } as EdgeV1);+ added++;+ }+ }+ await client.dispose();+ return { added, queried, server: server.command };+}diff --git a/src/graph/lsp/registry.ts b/src/graph/lsp/registry.tsnew file mode 100644index 00000000..73839288--- /dev/null+++ b/src/graph/lsp/registry.ts@@ -0,0 +1,49 @@+/**+ * LSP server registry — maps graft language names to a language server command.+ * Only servers whose binary is actually on PATH are eligible; a missing binary+ * simply means that language gets no LSP enrichment (the AST graph stands alone).+ */+import { execSync } from "node:child_process";++export interface LspServer {+ /** graft language names (as produced by languageLabelOf/genericLangOf) this serves. */+ languages: string[];+ command: string;+ args: string[];+ /** the LSP `languageId` to tag opened documents with. */+ languageId: string;+}++/** First-match-wins; ordering is priority. Extend by adding a row. */+export const LSP_SERVERS: readonly LspServer[] = [+ { languages: ["rust"], command: "rust-analyzer", args: [], languageId: "rust" },+ { languages: ["cpp", "c"], command: "clangd", args: ["--background-index"], languageId: "cpp" },+ { languages: ["go"], command: "gopls", args: [], languageId: "go" },+ { languages: ["python"], command: "pyright-langserver", args: ["--stdio"], languageId: "python" },+ { languages: ["typescript", "javascript", "tsx"], command: "typescript-language-server", args: ["--stdio"], languageId: "typescript" },+];++const resolved = new Map<string, string | null>();+/** Resolve a command to its ABSOLUTE path via the login shell's PATH. `spawn`+ * resolves against `process.env.PATH`, which often omits `~/.cargo/bin`,+ * `~/go/bin`, etc. where these servers live — so `command -v` can find a server+ * that `spawn(cmd)` then can't. Spawning the absolute path avoids that mismatch. */+function resolveCommand(cmd: string): string | null {+ if (resolved.has(cmd)) return resolved.get(cmd)!;+ let abs: string | null = null;+ try { abs = execSync(`command -v ${cmd}`, { encoding: "utf8" }).trim() || null; } catch { abs = null; }+ resolved.set(cmd, abs);+ return abs;+}++/** Pick the highest-priority installed server that covers at least one of the+ * languages present in the repo, with `command` resolved to an absolute path.+ * Returns null if none is installed. */+export function pickServer(languagesPresent: Set<string>): LspServer | null {+ for (const s of LSP_SERVERS) {+ if (!s.languages.some((l) => languagesPresent.has(l))) continue;+ const abs = resolveCommand(s.command);+ if (abs) return { ...s, command: abs };+ }+ return null;+}diff --git a/src/graph/queries/c.scm b/src/graph/queries/c.scmnew file mode 100644index 00000000..a1a9470b--- /dev/null+++ b/src/graph/queries/c.scm@@ -0,0 +1,13 @@+(struct_specifier name: (type_identifier) @name body:(_)) @definition.class++(declaration type: (union_specifier name: (type_identifier) @name)) @definition.class++(function_declarator declarator: (identifier) @name) @definition.function++(type_definition declarator: (type_identifier) @name) @definition.type++(enum_specifier name: (type_identifier) @name) @definition.type++; graft: call sites (upstream c tags.scm is definition-only)+(call_expression+ function: (identifier) @name) @reference.calldiff --git a/src/graph/queries/c_sharp.scm b/src/graph/queries/c_sharp.scmnew file mode 100644index 00000000..1fbe1393--- /dev/null+++ b/src/graph/queries/c_sharp.scm@@ -0,0 +1,19 @@+; C# tags — minimal, matched to the tree-sitter-wasms c_sharp grammar node types.+; (Upstream tree-sitter-c-sharp tags.scm references node types the bundled wasm+; grammar version lacks — "Bad node name 'type'" — so graft ships this instead.)++(class_declaration name: (identifier) @name) @definition.class+(interface_declaration name: (identifier) @name) @definition.interface+(struct_declaration (identifier) @name) @definition.struct+(enum_declaration name: (identifier) @name) @definition.enum+(record_declaration (identifier) @name) @definition.class+(method_declaration name: (identifier) @name) @definition.method+(constructor_declaration (identifier) @name) @definition.method+(property_declaration name: (identifier) @name) @definition.variable+(namespace_declaration name: (identifier) @name) @definition.module++(invocation_expression+ function: [+ (identifier) @name+ (member_access_expression name: (identifier) @name)+ ]) @reference.calldiff --git a/src/graph/queries/cpp.scm b/src/graph/queries/cpp.scmnew file mode 100644index 00000000..150f0426--- /dev/null+++ b/src/graph/queries/cpp.scm@@ -0,0 +1,23 @@+(struct_specifier name: (type_identifier) @name body:(_)) @definition.class++(declaration type: (union_specifier name: (type_identifier) @name)) @definition.class++(function_declarator declarator: (identifier) @name) @definition.function++(function_declarator declarator: (field_identifier) @name) @definition.function++(function_declarator declarator: (qualified_identifier scope: (namespace_identifier) @local.scope name: (identifier) @name)) @definition.method++(type_definition declarator: (type_identifier) @name) @definition.type++(enum_specifier name: (type_identifier) @name) @definition.type++(class_specifier name: (type_identifier) @name) @definition.class++; graft: call sites (upstream cpp tags.scm is definition-only)+(call_expression+ function: [+ (identifier) @name+ (field_expression field: (field_identifier) @name)+ (qualified_identifier name: (identifier) @name)+ ]) @reference.calldiff --git a/src/graph/queries/elixir.scm b/src/graph/queries/elixir.scmnew file mode 100644index 00000000..582f1160--- /dev/null+++ b/src/graph/queries/elixir.scm@@ -0,0 +1,54 @@+; Definitions++; * modules and protocols+(call+ target: (identifier) @ignore+ (arguments (alias) @name)+ (#any-of? @ignore "defmodule" "defprotocol")) @definition.module++; * functions/macros+(call+ target: (identifier) @ignore+ (arguments+ [+ ; zero-arity functions with no parentheses+ (identifier) @name+ ; regular function clause+ (call target: (identifier) @name)+ ; function clause with a guard clause+ (binary_operator+ left: (call target: (identifier) @name)+ operator: "when")+ ])+ (#any-of? @ignore "def" "defp" "defdelegate" "defguard" "defguardp" "defmacro" "defmacrop" "defn" "defnp")) @definition.function++; References++; ignore calls to kernel/special-forms keywords+(call+ target: (identifier) @ignore+ (#any-of? @ignore "def" "defp" "defdelegate" "defguard" "defguardp" "defmacro" "defmacrop" "defn" "defnp" "defmodule" "defprotocol" "defimpl" "defstruct" "defexception" "defoverridable" "alias" "case" "cond" "else" "for" "if" "import" "quote" "raise" "receive" "require" "reraise" "super" "throw" "try" "unless" "unquote" "unquote_splicing" "use" "with"))++; ignore module attributes+(unary_operator+ operator: "@"+ operand: (call+ target: (identifier) @ignore))++; * function call+(call+ target: [+ ; local+ (identifier) @name+ ; remote+ (dot+ right: (identifier) @name)+ ]) @reference.call++; * pipe into function call+(binary_operator+ operator: "|>"+ right: (identifier) @name) @reference.call++; * modules+(alias) @name @reference.modulediff --git a/src/graph/queries/java.scm b/src/graph/queries/java.scmnew file mode 100644index 00000000..3f3eb40a--- /dev/null+++ b/src/graph/queries/java.scm@@ -0,0 +1,20 @@+(class_declaration+ name: (identifier) @name) @definition.class++(method_declaration+ name: (identifier) @name) @definition.method++(method_invocation+ name: (identifier) @name+ arguments: (argument_list) @reference.call)++(interface_declaration+ name: (identifier) @name) @definition.interface++(type_list+ (type_identifier) @name) @reference.implementation++(object_creation_expression+ type: (type_identifier) @name) @reference.class++(superclass (type_identifier) @name) @reference.classdiff --git a/src/graph/queries/kotlin.scm b/src/graph/queries/kotlin.scmnew file mode 100644index 00000000..fa12bf6d--- /dev/null+++ b/src/graph/queries/kotlin.scm@@ -0,0 +1,43 @@+; Classes+(class_declaration+ (type_identifier) @name) @definition.class++; Objects+(object_declaration+ (type_identifier) @name) @definition.class++; Functions (top-level and member)+(function_declaration+ (simple_identifier) @name) @definition.function++; Properties+(property_declaration+ (variable_declaration+ (simple_identifier) @name)) @definition.constant++; Enum entries+(enum_entry+ (simple_identifier) @name) @definition.constant++; Type aliases+(type_alias+ (type_identifier) @name) @definition.type++; Companion objects (only named ones)+(companion_object+ (type_identifier) @name) @definition.class++; Function calls+(call_expression+ (simple_identifier) @name) @reference.call++; Method calls via navigation+(call_expression+ (navigation_expression+ (navigation_suffix+ (simple_identifier) @name))) @reference.call++; Constructor invocations (class references)+(constructor_invocation+ (user_type+ (type_identifier) @name)) @reference.classdiff --git a/src/graph/queries/php.scm b/src/graph/queries/php.scmnew file mode 100644index 00000000..4384302a--- /dev/null+++ b/src/graph/queries/php.scm@@ -0,0 +1,40 @@+(namespace_definition+ name: (namespace_name) @name) @definition.module++(interface_declaration+ name: (name) @name) @definition.interface++(trait_declaration+ name: (name) @name) @definition.interface++(class_declaration+ name: (name) @name) @definition.class++(class_interface_clause [(name) (qualified_name)] @name) @reference.implementation++(property_declaration+ (property_element (variable_name (name) @name))) @definition.field++(function_definition+ name: (name) @name) @definition.function++(method_declaration+ name: (name) @name) @definition.function++(object_creation_expression+ [+ (qualified_name (name) @name)+ (variable_name (name) @name)+ ]) @reference.class++(function_call_expression+ function: [+ (qualified_name (name) @name)+ (variable_name (name)) @name+ ]) @reference.call++(scoped_call_expression+ name: (name) @name) @reference.call++(member_call_expression+ name: (name) @name) @reference.calldiff --git a/src/graph/queries/ruby.scm b/src/graph/queries/ruby.scmnew file mode 100644index 00000000..47ba1eb5--- /dev/null+++ b/src/graph/queries/ruby.scm@@ -0,0 +1,64 @@+; Method definitions++(+ (comment)* @doc+ .+ [+ (method+ name: (_) @name) @definition.method+ (singleton_method+ name: (_) @name) @definition.method+ ]+ (#strip! @doc "^#\\s*")+ (#select-adjacent! @doc @definition.method)+)++(alias+ name: (_) @name) @definition.method++(setter+ (identifier) @ignore)++; Class definitions++(+ (comment)* @doc+ .+ [+ (class+ name: [+ (constant) @name+ (scope_resolution+ name: (_) @name)+ ]) @definition.class+ (singleton_class+ value: [+ (constant) @name+ (scope_resolution+ name: (_) @name)+ ]) @definition.class+ ]+ (#strip! @doc "^#\\s*")+ (#select-adjacent! @doc @definition.class)+)++; Module definitions++(+ (module+ name: [+ (constant) @name+ (scope_resolution+ name: (_) @name)+ ]) @definition.module+)++; Calls++(call method: (identifier) @name) @reference.call++(+ [(identifier) (constant)] @name @reference.call+ (#is-not? local)+ (#not-match? @name "^(lambda|load|require|require_relative|__FILE__|__LINE__)$")+)diff --git a/src/graph/queries/rust.scm b/src/graph/queries/rust.scmnew file mode 100644index 00000000..1f46733b--- /dev/null+++ b/src/graph/queries/rust.scm@@ -0,0 +1,41 @@+; Rust tags — standard tree-sitter tags convention (@definition.<kind> + @name + @reference.call).+; Vendored for graft's generic breadth tier. Predicates like (#strip! ...) are+; sanitized out at load time by generic.ts.++(function_item (identifier) @name) @definition.function++(function_signature_item (identifier) @name) @definition.function++(struct_item (type_identifier) @name) @definition.struct++(enum_item (type_identifier) @name) @definition.enum++(union_item (type_identifier) @name) @definition.struct++(trait_item (type_identifier) @name) @definition.interface++(type_item (type_identifier) @name) @definition.type++(mod_item (identifier) @name) @definition.module++(macro_definition (identifier) @name) @definition.function++(const_item (identifier) @name) @definition.constant++(static_item (identifier) @name) @definition.constant++; associated functions / methods inside impl blocks are function_item too,+; already captured above; call sites below:++(call_expression+ function: [+ (identifier) @name+ (field_expression field: (field_identifier) @name)+ (scoped_identifier name: (identifier) @name)+ ]) @reference.call++(macro_invocation+ macro: [+ (identifier) @name+ (scoped_identifier name: (identifier) @name)+ ]) @reference.calldiff --git a/src/graph/queries/scala.scm b/src/graph/queries/scala.scmnew file mode 100644index 00000000..9152ae39--- /dev/null+++ b/src/graph/queries/scala.scm@@ -0,0 +1,66 @@+; Definitions++(package_clause+ name: (package_identifier) @name) @definition.module++(trait_definition+ name: (identifier) @name) @definition.interface++(enum_definition+ name: (identifier) @name) @definition.enum++(simple_enum_case+ name: (identifier) @name) @definition.class++(full_enum_case+ name: (identifier) @name) @definition.class++(class_definition+ name: (identifier) @name) @definition.class++(object_definition+ name: (identifier) @name) @definition.object++(function_definition+ name: (identifier) @name) @definition.function++(val_definition+ pattern: (identifier) @name) @definition.variable++(given_definition+ name: (identifier) @name) @definition.variable++(var_definition+ pattern: (identifier) @name) @definition.variable++(val_declaration+ name: (identifier) @name) @definition.variable++(var_declaration+ name: (identifier) @name) @definition.variable++(type_definition+ name: (type_identifier) @name) @definition.type++(class_parameter+ name: (identifier) @name) @definition.property++; References++(call_expression+ (identifier) @name) @reference.call++(instance_expression+ (type_identifier) @name) @reference.interface++(instance_expression+ (generic_type+ (type_identifier) @name)) @reference.interface++(extends_clause+ (type_identifier) @name) @reference.class++(extends_clause+ (generic_type+ (type_identifier) @name)) @reference.class+diff --git a/src/graph/queries/solidity.scm b/src/graph/queries/solidity.scmnew file mode 100644index 00000000..852e285f--- /dev/null+++ b/src/graph/queries/solidity.scm@@ -0,0 +1,43 @@+;; Method and Function declarations+(contract_declaration (_+ (function_definition+ name: (identifier) @name) @definition.method))++(source_file+ (function_definition+ name: (identifier) @name) @definition.function)++;; Contract, struct, enum and interface declarations+(contract_declaration+ name: (identifier) @name) @definition.class++(interface_declaration+ name: (identifier) @name) @definition.interface++(library_declaration+ name: (identifier) @name) @definition.interface++(struct_declaration name: (identifier) @name) @definition.class+(enum_declaration name: (identifier) @name) @definition.class+(event_definition name: (identifier) @name) @definition.class++;; Function calls+(call_expression (expression (identifier)) @name ) @reference.call++(call_expression+ (expression (member_expression+ property: (_) @name ))) @reference.call++;; Log emit+(emit_statement name: (_) @name) @reference.class+++;; Inheritance++(inheritance_specifier+ ancestor: (user_defined_type (_) @name . )) @reference.class+++;; Imports ( note that unknown is not standardised )+(import_directive+ import_name: (_) @name ) @reference.unknowndiff --git a/src/graph/queries/swift.scm b/src/graph/queries/swift.scmnew file mode 100644index 00000000..0038571e--- /dev/null+++ b/src/graph/queries/swift.scm@@ -0,0 +1,51 @@+(class_declaration+ name: (type_identifier) @name) @definition.class++(protocol_declaration+ name: (type_identifier) @name) @definition.interface++(class_declaration+ (class_body+ [+ (function_declaration+ name: (simple_identifier) @name+ )+ (subscript_declaration+ (parameter (simple_identifier) @name)+ )+ (init_declaration "init" @name)+ (deinit_declaration "deinit" @name)+ ]+ )+) @definition.method++(protocol_declaration+ (protocol_body+ [+ (protocol_function_declaration+ name: (simple_identifier) @name+ )+ (subscript_declaration+ (parameter (simple_identifier) @name)+ )+ (init_declaration "init" @name)+ ]+ )+) @definition.method++(class_declaration+ (class_body+ [+ (property_declaration+ (pattern (simple_identifier) @name)+ )+ ]+ )+) @definition.property++(property_declaration+ (pattern (simple_identifier) @name)+) @definition.property++(function_declaration+ name: (simple_identifier) @name) @definition.function\ No newline at end of filediff --git a/src/graph/resolve.ts b/src/graph/resolve.tsindex ea69aacb..ade64b8e 100644--- a/src/graph/resolve.ts+++ b/src/graph/resolve.ts@@ -2,7 +2,7 @@* Resolve {@link RawEdge} intents into concrete {@link EdgeV1} edges by matching* names/specifiers against the whole-repo node index.*- * Confidence mirrors the SCIP/Graphify model:+ * Confidence is a two-tier provenance model:* - `extracted`: the target is certain — a match within the same file, an* import specifier, or a structural containment.* - `inferred`: a bare function target was resolved by a unique name match@@ -117,10 +117,20 @@ export function resolveEdges(if (hit === "ambiguous") continue; // drop — never guess past an ambiguous ownerif (hit) add(e.source, hit.id, "calls", hit.confidence);// No owner-qualified match means the call is unresolved. A unique bare- // method name is not evidence that this receiver has that method.+ // method name is not evidence that this receiver has that method — a+ // name-fallback here was measured to HALVE call-edge precision (73%→37%+ // vs a SCIP oracle) for a 3x count inflation, i.e. mostly noise. See #35.continue;}- const hit = resolveName(e.name!, e.file, ["function"], perFileName, globalName);+ // Depth-tier bare calls are function calls (method calls arrive as viaMember+ // with a receiver type). The generic breadth tier can't type receivers, so its+ // tags.scm @reference.call/@reference.send capture ALL calls as bare names —+ // in method-heavy languages (Java, Ruby, C#) those target methods. Widen the+ // candidate kinds to methods ONLY for generic-origin sources, so depth-tier+ // precision is untouched (an ambiguous function-vs-method name still drops).+ const srcOrigin = byId.get(e.source)?.origin;+ const callKinds: Kind[] = srcOrigin === "generic" ? ["function", "method"] : ["function"];+ const hit = resolveName(e.name!, e.file, callKinds, perFileName, globalName);if (hit) add(e.source, hit.id, "calls", hit.confidence); // drop unresolved calls (too noisy)}}diff --git a/src/graph/source-files.ts b/src/graph/source-files.tsindex 1dcb903b..e855b52b 100644--- a/src/graph/source-files.ts+++ b/src/graph/source-files.ts@@ -12,6 +12,7 @@ import { walkDir } from "../ingest/fs.js";import { relPosix } from "../util/paths.js";import { readIncludeDirs } from "../util/state.js";import { languageOf } from "./extract.js";+import { genericLangOf } from "./generic.js";/*** The source files a graph build parses: supported languages, minus the@@ -26,7 +27,12 @@ export function listSourceFiles(outDir: string,repoFiles: string[] = walkDir(root, readIncludeDirs(resolve(root))),): string[] {- return repoFiles.filter((f) => !f.startsWith(outDir) && languageOf(f) !== null);+ // A file is a source file if a depth-tier grammar (languageOf) OR a breadth-tier+ // grammar (genericLangOf) claims its extension. Both must agree here or `build`+ // and `check` would enumerate different sets.+ return repoFiles.filter(+ (f) => !f.startsWith(outDir) && (languageOf(f) !== null || genericLangOf(f) !== null),+ );}export interface SourceStat {diff --git a/src/graph/types.ts b/src/graph/types.tsindex 2d8c8f17..b1f94f6e 100644--- a/src/graph/types.ts+++ b/src/graph/types.ts@@ -2,8 +2,8 @@* `graph.json` — the code graph schema (v1).** One node per definition (file, class, function, method, interface, type, enum),- * wired by edges (contains, imports, calls, ...). Field names follow the LSP /- * SCIP vocabulary (`name`, `kind`, ...) rather than any one tool's conventions.+ * wired by edges (contains, imports, calls, ...). Field names follow the LSP+ * vocabulary (`name`, `kind`, ...) rather than any one tool's conventions.** Two tiers of data live on a node:* - Tier-1 (deterministic, $0): everything from the AST. Rebuilt on every run.@@ -11,7 +11,7 @@* M1 populates Tier-1 only; Tier-2 fields ship as `pending`/null.*/-/** What a node represents. LSP SymbolKind, narrowed to what TS + Python + Go produce. */+/** What a node represents. LSP SymbolKind, narrowed to what our extractors produce. */export type Kind =| "file"| "class"@@ -20,10 +20,23 @@ export type Kind =| "interface" // TS + Go| "type" // TS + Go (type alias / named type)| "enum" // TS only- | "struct"; // Go only+ | "struct" // Go only+ // The generic (tags.scm) breadth tier also emits these — every tree-sitter+ // grammar's tags.scm uses the tree-sitter tags @definition.<X> vocabulary, and+ // module/constant/variable are common across the long tail (Ruby modules,+ // Rust consts, top-level lets, …). Kept distinct rather than coerced so the+ // breadth tier's kinds read truthfully in cards/skeleton.+ | "module"+ | "constant"+ | "variable";-/** How confident we are an edge is true. */-export type Confidence = "extracted" | "inferred";+/** How confident we are an edge is true, best-first. The hand-written AST+ * resolver assigns `extracted`/`inferred`; the opt-in LSP enrichment pass+ * (`graft build --lsp`) can promote an edge to compiler-grade `lsp_resolved`+ * (an exact server-confirmed target) or `lsp_dispatch` (an interface/virtual+ * candidate). Order matters: consumers that rank by provenance treat earlier+ * values as stronger. */+export type Confidence = "lsp_resolved" | "lsp_dispatch" | "extracted" | "inferred";/** Whether the LLM meaning-layer has been computed for a node. */export type SummaryState = "pending" | "ready" | "stale";@@ -51,7 +64,10 @@ export interface NodeV1 {span: string; // whole definition: "L165-L222"signature: string | null; // "get(k: string): number" — null for kind:"file"exported: boolean;- origin: "ast";+ // How the node was extracted. "ast" = a first-class hand-written extractor+ // (TS/JS/Python/Go, full-fidelity). "generic" = the tags.scm breadth tier+ // (signature-only; symbols + bare edges, no scope-aware binding).+ origin: "ast" | "generic";body_hash: string; // sha256 of the definition text; the Tier-2 re-run triggerchars?: number; // byte length of the WHOLE file (file nodes only); the baseline// `ask` uses to estimate tokens saved vs reading the file wholediff --git a/src/graph/workspace-cli.ts b/src/graph/workspace-cli.tsindex 0c68a9b5..2ce6ece5 100644--- a/src/graph/workspace-cli.ts+++ b/src/graph/workspace-cli.ts@@ -110,8 +110,8 @@ export function runWorkspaceMap(process.stdout.write(federateMap(root, override, { maxDirs: opts.maxDirs }));}-export function runWorkspaceCheck(root: string, override?: string): void {- const { text, ok } = federateCheck(root, override);+export async function runWorkspaceCheck(root: string, override?: string): Promise<void> {+ const { text, ok } = await federateCheck(root, override);process.stdout.write(text);if (!ok) process.exit(1);}diff --git a/src/graph/workspace.ts b/src/graph/workspace.tsindex b272d400..396d0b41 100644--- a/src/graph/workspace.ts+++ b/src/graph/workspace.ts@@ -375,15 +375,15 @@ export function federateMap(/** Per-child drift status. `ok` is false when any BUILT child is stale — an* unbuilt child is surfaced (coverage), never a failure. */-export function federateCheck(+export async function federateCheck(root: string,override?: string,-): { text: string; ok: boolean } {+): Promise<{ text: string; ok: boolean }> {const wg = loadWorkspaceGraphs(root, override);const lines = [`workspace check — ${wg.loaded.length + wg.missing.length} repo(s)`, ""];let ok = true;for (const { child } of wg.loaded) {- const g = checkGraph(join(root, child));+ const g = await checkGraph(join(root, child));if (g.ok) {lines.push(`${child}/: OK`);} else {diff --git a/src/mcp/tools.ts b/src/mcp/tools.tsindex bb98d2a8..8e6e1d89 100644--- a/src/mcp/tools.ts+++ b/src/mcp/tools.ts@@ -148,12 +148,12 @@ function renderMatches(* map/check tools federate across the children — identical to the CLI. Returns* null for tools that don't federate (skeleton is per-file), so the caller* falls through to the normal single-graph path. */-function callWorkspaceTool(+async function callWorkspaceTool(root: string,dirOverride: string | undefined,name: string,args: Record<string, unknown>,-): { text: string; isError: boolean } | null {+): Promise<{ text: string; isError: boolean } | null> {switch (name) {case 'graft_find_code': {const query = String(args.query ?? '');@@ -188,7 +188,7 @@ function callWorkspaceTool(return { text: federateMap(root, dirOverride, { maxDirs }), isError: false };}case 'graft_check_freshness': {- const { text } = federateCheck(root, dirOverride);+ const { text } = await federateCheck(root, dirOverride);return { text, isError: false };}default:@@ -244,8 +244,8 @@ export async function callTool(: await ensureFreshGraph(root, { contextDir: dirOverride });note = refreshNote(r);}- const fed = ws ? callWorkspaceTool(root, dirOverride, name, args) : null;- const res = fed ?? callSingleTool(root, name, args, dirOverride);+ const fed = ws ? await callWorkspaceTool(root, dirOverride, name, args) : null;+ const res = fed ?? (await callSingleTool(root, name, args, dirOverride));return note ? { ...res, text: `${note}\n${res.text}` } : res;} catch (err) {return { text: err instanceof Error ? err.message : String(err), isError: true };@@ -253,12 +253,12 @@ export async function callTool(}/** The single-graph path: every tool, answered from one repo's graph. */-function callSingleTool(+async function callSingleTool(root: string,name: string,args: Record<string, unknown>,dirOverride?: string,-): { text: string; isError: boolean } {+): Promise<{ text: string; isError: boolean }> {switch (name) {case 'graft_find_code': {const query = String(args.query ?? '');@@ -278,7 +278,7 @@ function callSingleTool(case 'graft_check_freshness': {const engine = new Graft({ contextDir: dirOverride });const r = engine.check(root);- const g = engine.checkGraph(root);+ const g = await engine.checkGraph(root);const parts = [formatCheckReport(r)];if (!g.missing) parts.push(formatGraphCheckReport(g));return { text: parts.join('\n\n'), isError: false };diff --git a/test/generic-extract.test.ts b/test/generic-extract.test.tsnew file mode 100644index 00000000..39ccb7ef--- /dev/null+++ b/test/generic-extract.test.ts@@ -0,0 +1,132 @@+/**+ * The generic (breadth) tier: one language-agnostic extractor over a WASM grammar+ * + its tags.scm. These tests prove (a) it emits well-formed graft nodes/edges+ * for a language with NO hand-written extractor, and (b) the EXISTING resolver+ * resolves its bare-name call edges with zero language-specific code.+ */+import { test } from "node:test";+import assert from "node:assert/strict";+import { mkdtempSync, writeFileSync, mkdirSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { warmGenericGrammars, extractGeneric, genericLangOf, isWarm } from "../src/graph/generic.js";+import { resolveEdges } from "../src/graph/resolve.js";+import { buildGraph } from "../src/graph/build.js";+import { readGraph, wiringPath } from "../src/graph/write.js";+import { checkGraph } from "../src/graph/check.js";+import { contextDirFor } from "../src/context/node-file.js";++const RUST = `pub struct Config {+ name: String,+}++pub fn load() -> Config {+ let c = parse();+ Config { name: c }+}++fn parse() -> String {+ helper()+}++fn helper() -> String {+ String::new()+}+`;++test("genericLangOf routes .rs to the breadth tier (and not depth-tier extensions)", () => {+ assert.equal(genericLangOf("src/main.rs")?.name, "rust");+ assert.equal(genericLangOf("src/app.ts"), null); // depth tier owns .ts+ assert.equal(genericLangOf("README.md"), null);+});++test("extractGeneric emits nodes + bare-name call edges for Rust (no rust-specific code)", async () => {+ await warmGenericGrammars(["rust"]);+ assert.ok(isWarm("rust"), "rust grammar should warm");+ const { nodes, rawEdges } = extractGeneric("lib.rs", RUST, "rust");++ const kinds = nodes.filter((n) => n.kind !== "file").map((n) => `${n.kind}:${n.name}`).sort();+ assert.deepEqual(kinds, ["function:helper", "function:load", "function:parse", "struct:Config"]);++ // every non-file node carries the generic provenance + a span + a signature+ for (const n of nodes.filter((n) => n.kind !== "file")) {+ assert.equal(n.origin, "generic");+ assert.match(n.span, /^L\d+-L\d+$/);+ assert.ok(n.signature && n.signature.length > 0, `${n.name} has a signature`);+ }++ // calls are bare-name raw edges attributed to their enclosing function+ const calls = rawEdges.filter((e) => e.relation === "calls");+ const pairs = calls.map((e) => `${e.source.split("#")[1]}→${e.name}`).sort();+ assert.ok(pairs.includes("load→parse"), `load calls parse (got ${pairs.join(", ")})`);+ assert.ok(pairs.includes("parse→helper"), "parse calls helper");+});++test("the EXISTING resolver resolves generic-tier Rust calls by name", async () => {+ await warmGenericGrammars(["rust"]);+ const { nodes, rawEdges } = extractGeneric("lib.rs", RUST, "rust");+ const edges = resolveEdges(nodes, rawEdges);+ const call = edges.find((e) => e.relation === "calls" && e.source.endsWith("#load"));+ assert.ok(call, "load's call edge survived resolution");+ assert.equal(call?.target, "lib.rs#parse", "resolved to the same-file parse definition");+ assert.equal(call?.confidence, "extracted", "same-file → extracted");+});++// One inline snippet per breadth language, each exercising a definition + a call+// that must resolve. Java/Ruby/C# calls target METHODS — proving the generic-origin+// call-kind widening (resolve.ts) works, not just function-target languages.+const SNIPPETS: Array<{ lang: string; file: string; src: string; defs: string[]; call: [string, string] }> = [+ {+ lang: "java", file: "A.java",+ src: `class A {\n int run() { return helper(); }\n int helper() { return 1; }\n}\n`,+ defs: ["class:A", "method:helper", "method:run"], call: ["run", "helper"],+ },+ {+ lang: "ruby", file: "a.rb",+ src: `class Widget\n def render\n draw\n end\n def draw\n 1\n end\nend\n`,+ defs: ["class:Widget", "method:draw", "method:render"], call: ["render", "draw"],+ },+ {+ lang: "c_sharp", file: "A.cs",+ src: `class A {\n int Run() { return Helper(); }\n int Helper() { return 1; }\n}\n`,+ defs: ["class:A", "method:Helper", "method:Run"], call: ["Run", "Helper"],+ },+ {+ lang: "c", file: "a.c",+ src: `int helper() { return 1; }\nint run() { return helper(); }\n`,+ defs: ["function:helper", "function:run"], call: ["run", "helper"],+ },+];++for (const s of SNIPPETS) {+ test(`breadth tier: ${s.lang} — defs + a call that the resolver resolves`, async () => {+ await warmGenericGrammars([s.lang]);+ assert.ok(isWarm(s.lang), `${s.lang} grammar warms`);+ const { nodes, rawEdges } = extractGeneric(s.file, s.src, s.lang);+ const kinds = nodes.filter((n) => n.kind !== "file").map((n) => `${n.kind}:${n.name}`).sort();+ assert.deepEqual(kinds, s.defs, `${s.lang} definitions`);+ const edges = resolveEdges(nodes, rawEdges);+ const call = edges.find((e) => e.relation === "calls" && e.source.endsWith(`#${s.call[0]}`));+ assert.ok(call, `${s.lang}: ${s.call[0]}'s call edge exists and is attributed to the caller`);+ assert.equal(call?.target, `${s.file}#${s.call[1]}`, `${s.lang}: resolved ${s.call[0]}→${s.call[1]}`);+ });+}++test("buildGraph + checkGraph handle a breadth-tier (.rs) repo end-to-end", async () => {+ const dir = mkdtempSync(join(tmpdir(), "graft-rust-"));+ mkdirSync(join(dir, "src"), { recursive: true });+ writeFileSync(join(dir, "src", "lib.rs"), RUST);++ await buildGraph(dir, { reuse: false });+ const g = readGraph(wiringPath(contextDirFor(dir)));+ assert.ok(g, "graph built");+ const rust = g!.nodes.filter((n) => n.path.endsWith(".rs") && n.kind !== "file");+ assert.ok(rust.length >= 4, `indexed the rust defs (got ${rust.length})`);+ assert.ok(rust.every((n) => n.origin === "generic"), "rust nodes are generic-tier");+ assert.ok(g!.edges.some((e) => e.relation === "calls"), "rust call edges resolved");++ // check must agree with build — the async warmup means .rs files are re-extracted+ // identically, so a fresh graph reads as in-sync, not perpetually stale.+ const chk = await checkGraph(dir);+ assert.equal(chk.ok, true, `check OK on a breadth-tier repo (added=${chk.added}, removed=${chk.removed})`);+});diff --git a/test/utf16-source.test.ts b/test/utf16-source.test.tsindex 030751aa..c87534bf 100644--- a/test/utf16-source.test.ts+++ b/test/utf16-source.test.ts@@ -89,7 +89,7 @@ test("A1 hash-what-you-parse consistency: build/check/fingerprint agree on a UTFwriteFileSync(join(dir, "legacy.py"), utf16le("def from_legacy():\n return 42\n"));await buildGraph(dir);- const check = checkGraph(dir);+ const check = await checkGraph(dir);assert.equal(check.ok, true, "checkGraph must decode the same way build.ts did — no added/removed/changed");assert.deepEqual(check.added, []);assert.deepEqual(check.removed, []);diff --git a/test/workspace.test.ts b/test/workspace.test.tsindex a1bad942..24adf340 100644--- a/test/workspace.test.ts+++ b/test/workspace.test.ts@@ -146,14 +146,14 @@ test("check federation: OK when all in sync, STALE + not-ok when a child drifts"const p = workspaceFx(REPOS);await buildWorkspace(p);- const fresh = federateCheck(p);+ const fresh = await federateCheck(p);assert.equal(fresh.ok, true);assert.ok(fresh.text.includes("repoA/: OK"));assert.ok(fresh.text.includes("repoB/: OK"));// Change repoA's code WITHOUT rebuilding → present-and-stale.writeFileSync(join(p, "repoA", "a.ts"), "export function alphaHandler() { return 99; }\nexport function newlyAdded() { return 0; }\n");- const drifted = federateCheck(p);+ const drifted = await federateCheck(p);assert.equal(drifted.ok, false);assert.ok(drifted.text.includes("repoA/: STALE"));rmSync(p, { recursive: true, force: true });