Skip to content

fix(network): resolve imports by each language's own rule (#3544 #3545 #3552 #3553, #3554 in part) - #3562

Merged
squid-protocol merged 4 commits into
mainfrom
claude/festive-fermat-kojuy7
Sep 25, 2026
Merged

squid-protocol merged 4 commits into
mainfrom
claude/festive-fermat-kojuy7

Conversation

@squid-protocol

@squid-protocol squid-protocol commented Sep 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes #3544, #3545, #3552 and #3553; fixes #3554 except its Rust mod x; part (below).

NetworkRiskSensor._resolve_target resolved every import token by searching the whole repo for its path, file name or stem. It never used the importing file's location or the language's module rule. The new graph-accuracy gate (#3547) measured the cost on 16 pinned real repos. This PR adds the language rules, each declared per language with a new top-level DEFINITION flag, like the existing case_insensitive_imports / imports_are_source_members.

Change Issue Languages (flag)
Stage 0: location first. A .//../ token (any language), and every import of a language that searches the importer's directory first, resolves at dirname(importer)/token before the name search. A token without an extension also takes the same extension-less path or its index file. ./x.js also takes x.ts. A token with any other extension names exactly that file, so <poll.h> is never poll.c. #3553, #3552 c, cpp, objective-c, ruby, zig (imports_resolve_from_importer_dir)
Python's module rule. a.b is a/b.py or a/b/__init__.py under a source root, a directory that isn't itself a package. So from fastapi import X binds fastapi/__init__.py, which a stem search could never find, and import types isn't fastapi/types.py. Leading dots are relative to the importing package. Several matches still draw no edge (#261 unchanged). Path-shaped tokens keep the name search. #3545, #3544 python, embedded_python (package_init_file)
The package path is checked even for a lone candidate. A dotted token matched through its last segment must end in its package path, so starlette.requests is not the only local requests.py. A token that matched as a whole file name (dofile("x.lua")) is exempt. #3544 lua, perl, java (import_path_mirrors_module_path)
Token shapes. Perl :: is the path separator. Java retries the enclosing class for import static a.b.C.member and nested a.b.Outer.Inner. #3554 perl (import_path_separator), java (imports_may_name_member)

C#, Kotlin and Scala are deliberately left out of the mirror check, because their namespaces don't mirror files. JS/TS bare specifiers are unchanged.

Measured: tests/tools/import_graph_accuracy.py (baseline ratcheted in this PR)

language precision before → after recall before → after
python (fastapi, flask, httpx) 89.5 → 98.9 58.1 → 97.9
typescript (zod) 1.9 → 91.4 0.2 → 100.0
javascript (express) 82.9 → 90.5 47.6 → 100.0
java (gson) 98.0 → 100.0 98.2 → 100.0
c (libuv, hiredis) 99.9 → 99.9 91.1 → 100.0
zig (zls) 100.0 → 100.0 79.0 → 94.1
ruby (rack) 99.6 → 99.6 96.2 → 99.6
perl (HTTP-Message) 100.0 → 97.9 4.1 → 100.0
lua, php, cpp, go, rust unchanged unchanged
  • Perl precision drops because it now draws 47 edges instead of 2. The one false positive is use HTTP::Request; inside POD documentation in HTTP/Config.pm: the import capture reads the raw file.
  • Java: part of the move is the audit's own fix (commit c4b25c0). A nested-class import (...TestTypes.BagOfPrimitives) lives in the outer class's file; the audit used to score those as unresolvable.

Where the remaining gaps are:

Crucible edge diff (main → this branch, one full-corpus scan each)

+229 edges:

  • 220 Python module and __init__ edges;
  • 5 Zig and 3 TypeScript relative imports whose name repeats elsewhere;
  • 1 Markdown relative link.

−128 edges:

  • 69 cross-language basename matches, all wrong. Examples: Python import os → assembly/bootos/os.asm, import io → scheme/racket/io.ss, import datetime → livecode/.../datetime.livecodescript, and Java import java.util.Collection → php/laravel_core/Collection.php.
  • 44 Python Import resolver links a dotted import to any lone same-named file, ignoring its package path (false dependency edges) #3544 wrong targets, e.g. starlette.types → fastapi/types.py.
  • 13 micropython and 2 Java edges pointed at paths the crucible flattened. import lib.logging targets lib/logging.py, which the corpus stores as logging.py. Springboot's Binder.java lost its package directories. No edge is right there.

Golden masters

Both fixtures were re-blessed with crucible_check.py --update --yes and have identical scope: 2,443 differences.

Type of change

  • Bug fix
  • Parsing or engine logic (network_risk_sensor.py, per-language DEFINITION flags)

CI checklist

  • python tests/tools/audit_check.py: ruff OK, dead-key OK, ast-accuracy OK. mypy reports only the 3 local "stubs not installed for yaml" findings that main has too (none in the touched files).
  • python -m pytest tests/: 11,086 passed. The only error is test_security_auditor.py, because numpy isn't installed locally.
  • crucible_check.py --update --yes: both fixtures, scoped as above.
  • Graph gates: import_graph_accuracy.py (regenerated, the gains above), call_graph_accuracy.py --ci OK, call_graph_resolution.py python --ci OK.
  • Tree-sitter / tri-comparison: function detection is untouched; CI runs them.

Verification

$ python -m pytest tests/security_auditing/test_import_resolution_by_language_rule.py      -- 13 passed (12 new tests, one parametrized over 2 cases): C include vs <poll.h>,
                                                                                               ./db + require_relative + zig subdir, ./lib -> index, ./x.js -> x.ts, js over ts,
                                                                                               fastapi __init__, stdlib vs package module, relative dots, src layout,
                                                                                               lua lone candidate + dofile, perl ::, java static + nested
$ python -m pytest tests/security_auditing/test_network_risk_sensor.py tests/tools_recorders/test_edge_data.py   -- existing resolver/edge tests pass unchanged (#261 kept)
$ python tests/tools/import_graph_accuracy.py --regenerate                                    -- table above

The COBOL COPY resolution (Stage 3) is untouched; mainframe-ground-truth.yml checks it on this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YAtpBfGGyBGGovitSVaM5Q

…#3552 #3553 #3554)

_resolve_target only ever searched by path, file name or stem:
- #3553/#3552: a relative token (./x, ../x) and every C/C++/Obj-C/Ruby/Zig
  import now resolve against the importing file's directory first, so a name
  that repeats elsewhere (libuv's internal.h, express's ./db) still links;
  an extensionless token takes the same extension-less path or its index file,
  and an ESM ./x.js takes x.ts. A token with any other extension names exactly
  that file (<poll.h> is never poll.c).
- #3545/#3544 (python): package_init_file languages resolve by Python's rule:
  a.b is a/b.py or a/b/__init__.py under a source root (a non-package dir),
  so from fastapi import X binds fastapi/__init__.py and import types is not
  fastapi/types.py; leading dots are relative; several matches draw no edge
  (#261 unchanged).
- #3544 (lua/perl/java): a dotted token must end in its package path even
  when it is the only candidate (starlette.requests is not the local
  requests.py).
- #3554: perl :: is the path separator; java retries the enclosing class
  for static-member and nested-class imports.
Per-language behaviour is declared by new top-level DEFINITION flags.

import_graph_accuracy.py: a nested Java class resolves to its outer class
file (the audit scored those imports as unresolvable).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YAtpBfGGyBGGovitSVaM5Q
Callers that already hold a file (tests' `src/lib.py`, `f1.py`) pass
path-shaped tokens; only a dotted module name (`a.b`, `.a`) takes
Python's module rule. Fixes test_edge_data and the graph-metric tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YAtpBfGGyBGGovitSVaM5Q
…atch

A token that matched as a whole file name (lua `dofile("nextvar.lua")`)
already names its file; only a match made through a dotted token's last
segment has to end in the token's package path. Restores cosmopolitan's
20 dofile edges.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YAtpBfGGyBGGovitSVaM5Q
…t import-graph baseline

Golden masters (both fixtures identical in scope): 2443 differences, 2311 of
them the corpus-wide Composition Fit z-score (network metrics feed it); 132
direct: Dependency Blast Radius / Popularity Rank and the network summary.
Crucible edge diff main -> branch: +229 (220 python __init__ / module edges,
5 zig and 3 typescript relative imports, 1 markdown link), -128 (69
cross-language basename matches such as python `import os` ->
bootos/os.asm, 44 python #3544 wrong targets, 13 micropython and 2 java
edges to paths the corpus flattened).

import_graph_accuracy baseline: python 89.5/58.1 -> 98.9/97.9, typescript
1.9/0.2 -> 91.4/100, javascript 82.9/47.6 -> 90.5/100, java 98.0/98.2 ->
100/100, c recall 91.1 -> 100, zig recall 79.0 -> 94.1, ruby recall 96.2 ->
99.6, perl recall 4.1 -> 100 (precision 100 -> 97.9: the one edge comes from
a `use` inside POD).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YAtpBfGGyBGGovitSVaM5Q
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants