graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Ruby: stable subset of files yields zero nodes in full-repo runs (0.9.6) — each extracts fine in isolation

Open krishnateja7 opened this issue 4 months ago • 6 comments

First: thanks for the fast turnaround on #1634 and #1640 — both verified working on our repo after upgrading to 0.9.6. Plain modules and Struct.new/Data.define constants now get real nodes (63 new Result-struct nodes here), constant-receiver edges land, and our Rails-truth sidecar's file-node fallback counter went 99 → 0 exactly as predicted. Roughly 1,000 edge-pairs our sidecar used to supply are now emitted natively. Great releases.

While re-measuring on 0.9.6 we found two new issues (filing separately; they may share a root cause in parallel extraction). This one is the more impactful of the two.

Symptom

In full-repo runs of graphify update ., a stable subset of Ruby files yields ZERO nodes — not even file nodes. In our repo it is exactly 5 of ~700 app/**/*.rb files, and the set is identical across consecutive runs.

Detection method:

covered = graph["nodes"].map { |n| n["source_file"] }.to_set
Dir["app/**/*.rb"].reject { |f| covered.include?(f) }
# => same 5 files, every run

Each file extracts perfectly in isolation

Copy any of the 5 files alone (or into a small fresh probe corpus) and run graphify update . there: full extraction — class/module node, method nodes, file node all present, stable across reruns.

Failure is batch-context-dependent, not content-dependent

  • A 3-file probe corpus containing exactly three of the failing files: 1 extracted fine, 2 produced zero nodes.
  • The same 2 files later extracted fine in a different 4-file corpus (original content, unmodified).
  • Minimal probes of every construct the files use (extend self, begin/rescue/retry, namespaced superclass class X < Devise::PasswordsController, def-less AR model body, endless methods) all extract fine.
  • Stripping comments from two of the files made them extract in a batch where the originals didn't — but the originals also extract fine in other batches, so comment content is not the trigger either. The only consistent predictor is which other files are being processed alongside.

File shapes for reference (no shared decisive construct): one Devise-inheriting namespaced controller (~30 lines), one ActiveRecord model with a kwargs def self. upsert + begin/rescue/retry (~70 lines, comment-heavy), three extend self modules with long leading comment blocks.

Hypothesis

Parallel/batched extraction drops files at a batch boundary, or a worker exception mid-batch silently swallows the remainder of its batch. Possibly the same root as the community-assignment nondeterminism we're filing alongside this.

Impact

affected/explain are silently blind to and through the dropped files — in our case the dropped set includes hub modules that many services route through, so reverse-dependency queries under-report with no signal that anything is missing.

A cheap mitigation independent of the fix: warn (or count in output) when a scanned source file produces zero nodes.

Environment

  • graphifyy 0.9.6 (uv tool install), macOS (Darwin 25.5)
  • Rails repo: ~11,800 nodes / ~19,800 links total, ~5,600 Ruby nodes
  • Reproduced on every full-repo run since upgrading; drop set byte-stable across runs

krishnateja7 avatar Jul 04 '26 20:07 krishnateja7

Thanks for the detailed report, and glad #1634/#1640 landed well.

Status: I shipped a mitigation on v8 (1288a55), but I want to be straight that it is not yet a confirmed root-cause fix.

What I changed:

  • Empties are no longer cached. Every extractable file produces at least a file node, so a zero-node result is anomalous. Both the parallel worker and the sequential fallback now skip the cache write when a non-error result has no nodes, so a re-run re-extracts and self-heals instead of loading a stale empty. This should end the byte-stable persistence you saw across runs.
  • extract now warns and lists any source file that landed in the graph with zero nodes, so the blindness in affected/explain is no longer silent.

What I could not do yet: reproduce the underlying trigger. I built synthetic Ruby corpora matching your described shapes (extend self modules with long comment blocks, a Devise-inheriting namespaced controller, an AR model with kwargs def self. + begin/rescue/retry + endless method) and ran them in-process (sequential, shared state) and could not get any file to extract to zero nodes. The comment-stripping-changes-behavior clue points at the content-hash cache, which the no-cache-on-empty change addresses, but the reason a valid file occasionally extracts empty when co-processed with certain others is still open.

If it recurs on 0.9.7+, the warning will now name the files. A repro that would let me pin it: the specific file(s) plus GRAPHIFY_DEBUG=1 graphify update . stderr (it prints per-file extractor exceptions), or the smallest batch that reproduces. Leaving this open until the root is found.

safishamsi avatar Jul 05 '26 09:07 safishamsi

Update: I instrumented the installed 0.9.6 package (uv tool, macOS) and root-caused this on our side. It is not the parallel/batch effect I hypothesized. It is two deterministic file-DISCOVERY filters in detect.py, and our "batch-context dependent" observation was a misread: it is path and name dependent, and our isolation probes moved the files out of the triggering paths.

Cause A (3 of 5 files): app/services/snapshots/ is pruned as a snapshot dir. _is_noise_dir("snapshots") returns True because bare "snapshots" is in the default noise set (alongside "snapshots", commented as Jest/Vitest snapshot dirs, detect.py ~line 671). We have a legitimate Rails "snapshots" SERVICE namespace, so round_reader.rb, entry_enrichment.rb and backfill_marker.rb are pruned during os.walk before enumeration.

Cause B (2 of 5 files): _is_sensitive drops passwords_controller.rb and device_token.rb via _generic_keyword_hit, even though classify_file returns code. device_token matches because "token" ends the stem; passwords_controller matches the "<=2 word stem containing a keyword" rule. device_tokens_controller.rb and device_token_policy.rb correctly survive (3-word stems), which is why the drop looked selective.

Evidence (stock 0.9.6, graspologic-free):

  • _get_extractor returns extract_ruby and a direct _safe_extract yields real nodes for all five (6, 4, 5, 8, 7), so extraction itself is fine.
  • None of the five have an AST cache entry, while control files do, so they are never dispatched to extraction.
  • Live detect(root) returns all five OUT and the controls IN; _is_noise_dir("snapshots") is True; _is_sensitive is True for the two named files.

So the never-enumerated files never reach extraction or the cache, which is why the no-cache-on-empty change (1288a55) and the zero-node extract warning will not surface them.

The fix belongs in discovery: (a) do not prune a bare "snapshots" dir as noise outside a JS project (keep "snapshots", or gate on JS detection, or make it opt-out), and (b) do not let _is_sensitive drop a file that classify_file resolves to a real code language (a .rb named device_token is a module, not a secret store).

No artifacts needed; this reproduces on any repo with a snapshots/ code dir or a password/token source filename. Happy to share the short instrumentation snippet (it only calls into your own package).

krishnateja7 avatar Jul 05 '26 20:07 krishnateja7

Repo-wide blast radius on our side, in case it helps weigh the fix: applying the same two filters to every tracked file (2653 total), 12 files are dropped, not just the 5 I first reported. My earlier count only looked at app/**/*.rb.

Noise-dir prune of snapshots/ (6):

  • app/services/snapshots/{round_reader,entry_enrichment,backfill_marker}.rb
  • their 3 specs under spec/services/snapshots/

_is_sensitive name false-positive (6):

  • app/controllers/api/v1/passwords_controller.rb
  • app/models/device_token.rb
  • db/migrate/..._create_device_tokens.rb (a production schema migration)
  • spec/factories/device_tokens.rb
  • frontend/src/test/tokens.test.ts
  • frontend/src/app/components/ui/tests/menu-overlay-tokens.test.tsx

So on a normal app the surface is a bit wider than it looks: a whole service namespace plus its tests, a schema migration, and unrelated frontend test files that just happen to have "tokens" in the name. None of these are secrets or Jest snapshots. Flagging in case the real-world impact helps prioritize the two discovery fixes.

krishnateja7 avatar Jul 05 '26 20:07 krishnateja7

Verified against the 0.9.7 release, two observations.

  1. The drop persists on 0.9.7, as expected: I re-ran the same filter replay over our tracked files and the identical 12 files are still excluded (6 under the snapshots/ service namespace via the noise-dir prune, 6 via the _is_sensitive name keywords). detect.py's noise set and keyword logic are unchanged between 0.9.6 and 0.9.7, so this is just confirming the state, not reporting a regression.

  2. One thing worth knowing about the 1288a55 mitigation: it cannot surface the files in this issue, structurally. The new warning lists files that reached extraction and produced zero nodes. It does fire for us on 0.9.7 (it flagged 12 JSON data files, which is a nice orthogonal win), but the files from this issue are pruned during discovery (os.walk pruning and _is_sensitive), so they never reach extraction and never appear in the warning. If someone uses the warning's silence as evidence their corpus is fully covered, discovery-dropped files are exactly the blind spot it misses.

So the two fixes from the root-cause comment above are still what closes this: (a) scope the bare snapshots entry so it only prunes actual JS/TS snapshot dirs (for example only when the directory contains .snap files or sits under a __tests__/test root), and (b) require a stronger signal than a bare name keyword before dropping a file that classify_file already identified as code (or demote keyword hits on code files to a warning instead of a silent drop). Happy to re-run the census here the day a release touches either filter.

krishnateja7 avatar Jul 06 '26 03:07 krishnateja7

Thank you for the exceptional root-cause work, that instrumentation made this unambiguous. You were right that the 0.9.7 no-cache-on-empty change couldn't touch these, because the files are dropped during discovery and never reach extraction. Both filters are now fixed on v8 (5d01373):

  • Cause A (snapshots/ prune): a bare snapshots/ dir is pruned only when it actually contains .snap files or sits directly under a JS test root (__tests__/__test__). __snapshots__ stays unconditionally pruned. So a Rails app/services/snapshots/ code namespace (and its specs under spec/services/snapshots/) survives.
  • Cause B (_is_sensitive name keyword): a file that classify_file resolves to programming-language source is now exempt from the weak name-keyword drop, so device_token.rb and passwords_controller.rb are kept. Real secret stores in data/config formats (credentials.json, secrets.yaml, .env, .pem, ...) are still caught — those route through the CODE path for manifest parsing but are deliberately not exempted, so no secret-leak regression.

Added regressions for both (the snapshots code-namespace case, the two Ruby modules, and the data-format guard). This will be in the next release; a re-run of your census then would be very welcome to confirm all 12 files return.

safishamsi avatar Jul 06 '26 15:07 safishamsi

Census re-run on 0.9.12 (upgraded from 0.9.7), confirming the fix end-to-end on our corpus:

  • All 12 previously-dropped files are back in the graph, with file, class, and method nodes (e.g. app/models/device_token.rb → file node + DeviceToken class + its methods). Both halves behave as described: the app/services/snapshots/ code namespace and its specs survive the noise-dir prune, and the token/password-named source files survive _is_sensitive, while .env-style data/config secret stores remain excluded.
  • Broader sweep: 0 of 1,818 tracked code files (rb/ts/tsx across app, lib, spec, db/migrate, frontend) are absent from graph-node source_files. No residual discovery holes on this repo.

Thanks for the quick turnaround. Confirmed closed from our side.

krishnateja7 avatar Jul 10 '26 21:07 krishnateja7