codemap icon indicating copy to clipboard operation
codemap copied to clipboard

Measure and improve import accuracy for every language

Open JordanCoin opened this issue 1 month ago • 7 comments

codemap reports importers and coverage for every supported language, but nothing measures how right those answers are on real repositories. The conformance test (scanner/conformance_test.go) proves one minimal edge per language. It does not measure accuracy. Go is the exception, because TestGoGraphMatchesGoList compares it to go list.

Goal

Replace estimates with measured numbers per language, then fix whatever keeps a language below 90%.

What to measure

On real open-source repositories at pinned commits, compared to the language's own tooling or a sampled manual review:

  • Recall: the share of real intra-project file dependencies that --importers and --deps find.
  • Precision: the share of reported edges that are real.
  • Honesty: whether the coverage status is true for the file asked about. A wrong answer labeled complete is the worst outcome.

Measured results (codemap 4.6.3)

Details, methods and caveats are on each language issue. Most numbers are samples of 30 to 60 files per repo, and some ground truth is scripted, so read these as directional. "False complete" means files with missed edges were labeled complete.

Language Repo Recall Precision False complete
TypeScript ky / excalidraw / trpc 90% / 99% / 78% 100% yes on ky and excalidraw
JavaScript express / eslint / NodeBB 63% / 98% / 100% 63% / 98% / 100% yes on express
Python werkzeug / pytest 94% / 96% 99% / 89% no
Rust ripgrep / tokio 92% / 24% 100% / 48% strict no
C curl / libuv 100% / 100% 100% / 85% false edges only
C++ leveldb / abseil 100% / 98% 100% / 87% yes on abseil
Ruby jekyll / maybe 59% / 0% 91% / undefined yes
PHP monolog / PHPMailer 0% / 0% undefined yes
Lua telescope.nvim / kong 19% / 58% 86% / not measured yes
Bash bats-core / bash-it 0% / 0% none reported yes
Dart shelf / http 100% / 100% 100% no misses
Solidity v4-core / openzeppelin 100% / 95% 100% yes on openzeppelin
CUE hof 73% 100% yes
Swift argument-parser / swift-log 100% / 92% 82% / 80% no
Java gson / commons-lang 0% / 0% not measured no
Kotlin ktor / koin 2% / 32% not measured no
Scala cats / http4s 7% / 24% not measured no
C# Polly / MediatR 0% / 1% not measured no
Elixir phoenix / oban 0% / 0% not measured no

Starting estimates (unmeasured guesses, not data)

Language Estimate
Go 95%+, backed by the go list test, so no issue here
TypeScript 80-90%
JavaScript 80-90%
Python 75-85%
Rust 80-90%
C 70-85%
C++ 70-85%
Ruby 50-70%
PHP 50-70%
Lua 50-70%
Bash 50-70%
Dart 75-90%
Solidity 75-90%
CUE 75-90%
Swift 50-70%
Java near 0%
Kotlin near 0%
Scala near 0%
C# near 0%
Elixir near 0%

Each language issue records the repositories, commits, method, numbers and root causes of misses.

Tasks

  • [ ] TypeScript #238
  • [ ] JavaScript #239
  • [ ] Python #240
  • [ ] Rust #241
  • [ ] C #242
  • [ ] C++ #243
  • [ ] Ruby #244
  • [ ] PHP #245
  • [ ] Lua #246
  • [ ] Bash #247
  • [ ] Dart #248
  • [ ] Solidity #249
  • [ ] CUE #250
  • [ ] Swift #251
  • [ ] Java #252
  • [ ] Kotlin #253
  • [ ] Scala #254
  • [ ] C# #255
  • [ ] Elixir #256

JordanCoin avatar Nov 01 '26 11:11 JordanCoin

Status of the first fix round.

Merged: #257 (import rules: Lua, Ruby autoload, Bash, CUE, Swift).

Open, green against current main:

  • #258 coverage labels. Merged main and reworded the Bash note, since dynamic source now resolves when its literal remainder names one shell file. jekyll lib/jekyll/cache.rb now lists its importer and reads complete. abseil-cpp's rollup reads partial from 14 dynamic source lines in ci/.
  • #259 false edges. Merged main and removed its Lua package.path block, which #257's resolveLuaModule makes unreachable. Lua after the merge, measured against a require scan:
repo edges, main edges, #259 recall precision, main precision, #259
kong 2372 2368 .995 .993 .995
telescope.nvim 217 216 .973 .995 1.000

All 5 removed edges were absent from the scan, and no edges were added.

  • #260 TS/JS re-exports, literal import() and the root /index.js phantom edge:
repo edges recall precision
ky 164 to 182 .901 to 1.000 1.000 to 1.000
excalidraw 3609 to 3768 .954 to .996 1.000 to 1.000
trpc 2026 to 2289 .879 to .991 1.000 to .997
express 154 to 154 .623 to 1.000 .623 to 1.000
eslint 1512 to 1512 .981 to .981 .987 to .987
NodeBB 3556 to 3556 .998 to .998 1.000 to 1.000

Five trpc edges pick a named .js where the compiler picks the sibling .ts. That is existing resolver behaviour and is unchanged.

JordanCoin avatar Nov 01 '26 14:11 JordanCoin

#257 to #261 are all merged. Results below compare v4.6.3 with main at 8c94465 on the 41 measurement repos. Main should not be released yet: adversarial testing found the regressions listed under "Fix before release".

Against truth

repo recall precision
ky .901 to 1.000 1.000 to 1.000
excalidraw .954 to .996 1.000 to 1.000
trpc .879 to .991 1.000 to .997
express .623 to 1.000 .623 to 1.000
eslint .981 to .981 .987 to .987
NodeBB .998 to .998 1.000 to 1.000
pytest .964 to .964 .891 to .999
werkzeug .958 to .958 .953 to .964

Lua on main: kong recall .995, precision .995; telescope.nvim recall .973, precision 1.000. 4.6.3's Lua edge counts were far lower (1151 and 46), but its recall was not rescored here.

None of the removed edges in these ten repos were real. A source check of the removed Swift edges found none whose target type is still used. No added Bash edge lacked a matching source line. C/C++ recall was measured with clang before merge and was not rerun on the combined build. Repos in languages this round did not touch (Rust, Kotlin, Scala, Java, PHP and others) show no edge changes.

Fix before release (regressions from v4.6.3)

  1. Reading a FIFO named *.sh, .php or .rb hangs --importers. The coverage evidence scan reads it as a file. Reproduced.
  2. Lua require("..") or require(".") links to a root init.lua. Reproduced.
  3. C and Lua imports can leave the scan root at graph level, because they skip the resolveRelative guard.
  4. import '../' can link to a root index.js instead of the package.json main.
  5. Bash false edges: a shell function named load, $NVM_DIR/nvm.sh matching an unrelated unique file, and single-quoted '$X'/b.sh.
  6. Raw dynamic words such as $(pwd)/nope.sh appear in --deps --json files[].imports.
  7. The evidence regexes mark files partial on weak evidence: source $x inside a string or heredoc, a PHP use DateTime;, Ruby "Zeitwerk::Loader" in a string.
  8. #include above the root loses its edge but still reads complete. Same label for dropped above-root imports in PHP, Ruby and Bash.

Existing gaps also found

  • complete with real importers missed: builtin source, eval, require $f, Ruby load, importlib and others.
  • One non-UTF-8 byte drops all of a file's imports and still reads complete.
  • source -- ./b.sh and a tab after source find no edge.
  • Windows flake: TestClaimDaemonPIDAdmitsExactlyOneConcurrentClaimant.

Items 3 to 8 come from fixture testing and were not separately reproduced.

JordanCoin avatar Nov 01 '26 21:11 JordanCoin

#262 fixes the regressions listed above. It is open and green at 19985a3, and not merged yet.

Compared with main on the 41 measurement repos, no edges were lost. The only change is 2 added edges in eslint, both real: require("..") and require("../..") now follow package.json main to lib/api.js. On bats-core through the default CLI path, main and #262 give identical results.

Three adversarial rounds ran during this work. The last full round was on the commit before the final one. Its one blocker and three smaller findings were fixed and checked by repro, but the final commit did not get a fourth full round.

Known limits are in the #262 PR body. Additional behaviour to know: a relative $(cd .. && pwd) prefix no longer links and reads partial, and a /* inside a multi-line PHP string can still hide a namespace. The security review's context cancellation and total-bytes cap were not added, so total evidence reads still grow with file count, with an 8 MiB limit per file.

JordanCoin avatar Nov 02 '26 13:11 JordanCoin

#262 is merged to main as df69afb, and CI on that commit is green on all jobs, including Windows. Not released yet: v4.6.3 is still the latest release.

Measured on the PR head that was squash-merged: 0 edges lost against the previous main across 41 repos, with 2 real edges added in eslint. The known limits from the earlier comment still apply.

JordanCoin avatar Nov 02 '26 14:11 JordanCoin

v4.6.4 is released from df69afb and carries #257, #259, #260, #258 and #262. Measured numbers are on #238, #239, #240 and #246, and the Bash behavior change is on #247. The other language issues have no new numbers in this comment: across the 41-repo comparison the final fixes lost no edges and only added two real eslint edges. Per-language gaps not addressed in this round stay open.

JordanCoin avatar Nov 02 '26 14:11 JordanCoin

Correction on the numbers in the comments on #238, #239, #240 and #246: an audit found they are weaker than they read.

  • All of the scored repos are ones the fixes were tuned on. No repo was held out.
  • The Python and Lua truth sets came from hand-written scripts that share codemap's assumptions, and choosing different root folders moves the result.
  • Several JS figures come from 60-file samples, and the CommonJS repos were scored with ESM resolution rules.
  • No commits were pinned, and no single command reproduces the numbers.

The TypeScript compiler-based numbers for ky and excalidraw are the most solid of them.

A reproducible benchmark is in progress to replace them. Truth comes from each language's own toolchain, the repos are held out and chosen by a rule committed before codemap runs on them, every file is counted, results carry confidence intervals, and one command reruns it all. Treat the earlier figures as provisional until that lands.

JordanCoin avatar Nov 03 '26 12:11 JordanCoin

First measured numbers are in #264: a reproducible benchmark (make bench) of codemap v4.6.4 (same resolver as v4.7.0) against each language's own toolchain on 12 pre-registered held-out repos plus 8 tuned repos, reported separately. An independent rebuild of frp, axios and stable-diffusion-webui matched the committed numbers exactly.

Held-out, per language (full census, 95% Wilson intervals, edges are not independent so intervals do not generalise to other repos):

lang repos recall precision notes
Go ollama, frp, caveman 11,681 / 11,681 11,681 / 11,681 non-test files, linux/amd64, package-level edges
JavaScript three.js, axios, ECC 0.976 to 1.000 per repo 1.000 .cjs / .mjs misses: #267, #268
TypeScript gstack, pi, claude-mem 0.998, 0.999, 0.000 0.997 to 0.999 claude-mem built no graph (30 s scan budget): #269
Python yt-dlp, sd-webui, spec-kit 0.893 to 1.000 0.994, 0.043, 0.966 namespace fan-out: #265; package members: #266

Pooled across all 12: recall 0.923, precision 0.680. Not good enough to claim; the bugs above account for nearly all of the gap. Plan: fix #265, #266, #269, rerun on the same pinned SHAs, and add a fresh held-out set for any public claim.

Corrects my earlier numbers in this thread, which were not measured this way.

JordanCoin avatar Nov 03 '26 18:11 JordanCoin