codemap icon indicating copy to clipboard operation
codemap copied to clipboard

accuracy(TypeScript): measure import graph accuracy and close gaps below 90%

Open JordanCoin opened this issue 1 month ago • 3 comments

Part of #237.

Starting estimate: 80-90% recall. This is an unmeasured guess.

Measure

Run codemap on real TypeScript repositories at pinned commits and compare it to ground truth.

  • Suggested ground truth: tsc --listFiles or --traceResolution.
  • Report: recall, precision, and whether the coverage status was honest for each file.

Likely gaps to check

tsconfig paths and project references, barrel files, dynamic import(), framework file conventions.

Done when

  • Repositories, commits, method and numbers are recorded here.
  • Each miss has a root cause.
  • Recall is at or above 90%, or the remaining gap is one codemap cannot close and its coverage status says so.

JordanCoin avatar Nov 01 '26 11:11 JordanCoin

TypeScript import graph accuracy (issue #238)

Method: ground truth = TypeScript 5 compiler API (ts.preProcessFile + ts.resolveModuleName, Bundler resolution, allowJs, root tsconfig paths) run from a scratch dir outside the clones, static only, no repo code executed. Intra-project edges only (target must be a git-tracked source file in the repo; workspace-package imports via node_modules symlinks are not in truth). codemap 4.6.3, codemap --json --importers <file> per file (its resolved imports). Auto-written .codemap/config.json was replaced with only=[ts,tsx,js,jsx,mjs,cjs,mts,cts] and no excludes so the filter hid nothing (the auto config for express excluded fixtures, and for ky/others listed only a subset of extensions). Importers calls cost 3s each on large repos, so repos over 200 files use a SAMPLE of 60 source files (python random.Random(238) shuffle of the sorted file list); recall/precision are over edges whose importer is in the sample. Honesty = coverage_status of sampled files that had a missed edge.

repo (shape) SHA src files files measured recall (truth edges found) precision files with a miss / reporting complete
sindresorhus/ky (small lib) 3541888 98 98 (all) 164/182 = 90.1% 164/164 4 / 4 complete
excalidraw/excalidraw (app) 8bcdca4 723 60 (sample) 324/326 = 99.4% 324/324 2 / 1 complete, 1 partial
trpc/trpc (monorepo) a81bba2 1068 60 (sample) 112/143 = 78.3% 112/112 6 / 0 complete (all partial)
pooled 600/651 = 92.2% (sample for 2 of 3) 600/600

Root causes, ranked by edges lost

  1. Re-exports are not edges (47 of 51 lost edges: 16 of ky 18, 31 of trpc 31). export {HTTPError} from './errors/HTTPError.js' (ky source/index.ts:97), export { Unpromise } from "./unpromise" (trpc packages/server/src/vendor/unpromise/index.ts:1), export * from './client.gen' barrels in packages/openapi/test/routers/*-heyapi/client/index.ts. Cause: scanner/sg-rules/typescript.yml:5 (and tsx.yml) match only kind: import_statement and require(...); export_statement with a source is never seen. Excalidraw hides this because its barrel also imports the same module. Barrel files are the hubs that matter most for blast radius, so importers of every re-exported module are undercounted. Fix size: small (add an export_statement with a source field to the rule, plus a rules test).
  2. Dynamic import() not captured (2 edges ky, 1 excalidraw). const {default: ky} = await import('../source/index.js') (ky test/request-stream-support-runtimes.ts), await import("./firebaseSDK") (excalidraw excalidraw-app/data/firebase.ts). Rule has no import(...) call pattern. Fix size: small.
  3. require of a .cjs path (1 edge excalidraw): scripts/buildPackage.js requires ../packages/excalidraw/env.cjs; the .cjs target is probably not a resolvable extension (inferred, not diagnosed). Fix size: small.
  4. Unresolved path aliases and workspace specifiers are reported, not silent: excalidraw note "10 of 2745 project-relative imports did not resolve" (e.g. @excalidraw/excalidraw/global in global.d.ts); trpc "8 of 917". Those are not in my truth set (aliases via node_modules/workspace), so not counted as misses.

Honesty

  • ky: every file is complete (98/98) yet 4 files lose 18 edges. Worst case: a miss under complete. The rule gap is language-wide, so a complete TypeScript claim is overstated whenever a barrel exists.
  • trpc: all 60 sampled files partial (notes list unresolved imports), so the 6 miss files were flagged. Honest, but the notes did not name the actual re-export gap.
  • excalidraw: 58/60 partial; one of its two miss files reported complete. Limits: sample of 60 files per large repo; truth ignores workspace-package edges; n small for per-cause counts.

Measured with codemap 4.6.3. Sampled numbers are samples, not a census.

JordanCoin avatar Nov 01 '26 11:11 JordanCoin

Released in v4.6.4. Recall / precision against the truth set: ky 1.000 / 1.000, excalidraw .996 / 1.000, trpc .991 / .997.

JordanCoin avatar Nov 02 '26 14:11 JordanCoin

Measured in #264 (v4.6.4, TypeScript compiler API per tsconfig as truth):

  • held-out: gstack recall 0.999 / precision 0.999, pi 0.998 / 0.997, claude-mem 0.000 (no graph: #269)
  • tuned: ky 1.000 / 1.000, excalidraw 1.000 / 0.997, trpc 0.875 / 0.940 (nested tsconfig paths: #271)

Caveat: truth unions edges across every tsconfig that lists a file, the most lenient defensible choice. TS truth was not independently rebuilt yet.

JordanCoin avatar Nov 03 '26 18:11 JordanCoin