Skip to content

feat(core): rank fold blockers by dominator retained-count over the import graph - #1968

Merged
lex00 merged 1 commit into
mainfrom
feat/1083-fold-blocker-ranking
Aug 26, 2026
Merged

lex00 merged 1 commit into
mainfrom
feat/1083-fold-blocker-ranking

Conversation

@lex00

@lex00 lex00 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Model

chant build --fold already reports a flat fallback list. This ranks it: immediate dominators (Cooper–Harvey–Kennedy) over the forward import-failure graph among a build's "run"-mode files. File d dominates file n when every causal path to n's failure passes through d; d's retained count is its dominator-subtree size — the number of files that would fold if d's own problem were fixed. Every file lands in exactly one dominator subtree, so retained counts are conserved (top-level blockers' retained sum to the total run-labeled file count), a diamond is never double-counted, and a file with two independent blockers inflates neither — it becomes its own entry instead.

Per the issue's re-scope (2026-08-26 comment), this is forward contagion only: file n fails because an import, d, also fails. Fold's other rule (#1044) runs the opposite direction — a file that would fold fine in isolation is forced to run because a run-mode importer needs it to keep object identity. A dominator tree over import edges can't express that edge, so those files are excluded from the tree entirely and reported in a separate reverseTainted bucket instead of being folded into anyone's retained count.

The import-edge graph itself isn't rebuilt: fold-import.ts's planFoldTaint already parses every project file's imports/re-exports to compute its own (differently-directed) taint propagation. That loop is extracted into an exported buildProjectImportEdges, which planFoldTaint now calls too — discovery/fold-rank.ts reuses it for the dominator graph instead of re-parsing.

FoldDecision gains one new field, reverseTainted?: boolean, set exactly where discovery/index.ts already builds the reverse-rule reason string — so the ranking doesn't need to pattern-match reason text to tell the two contagion directions apart.

CLI surface

chant build --fold --fold-rank [<path>], mirroring --report's existing context-sensitive bare-boolean-vs-path convention (main.ts's parseArgs):

  • --fold-rank alone prints the ranked text report to stderr, independent of --verbose.
  • --fold-rank <path> also writes the dominator tree as a Brendan Gregg collapsed stack export (weighted by retained count) to <path> — one line per file, each a chain from a synthetic fold root frame through its dominators down to itself, weight 1; folding identical prefixes (what every collapsed-format consumer does) reproduces each node's retained count exactly. Opens in any flame/icicle viewer, no chant-specific tooling.

No new build-time cost unless --fold-rank is passed.

Example output

Against a synthetic project (process-cfg.ts reads process.env directly; params.ts imports it; three files import params.ts/each other; one unrelated leaf; one file that would fold in isolation but is dragged to "run" by a run-mode importer):

fold-rank: 6 blocker(s), 6 file(s) blocked
  retained 5: process-cfg.ts — ambient "process" read is not foldable — ...
  retained 4: params.ts (behind process-cfg.ts) — ...
  retained 2: dependent-a.ts (behind params.ts) — ...
  retained 1: dependent-b.ts (behind params.ts) — ...
  retained 1: dependent-c.ts (behind dependent-a.ts) — ...
  retained 1: unrelated-leaf.ts — ...
fold-rank: 1 file(s) held back only by the reverse rule (#1044):
  would-fold.ts

Collapsed export (--fold-rank <path>):

fold;process-cfg.ts 1
fold;process-cfg.ts;params.ts 1
fold;process-cfg.ts;params.ts;dependent-a.ts 1
fold;process-cfg.ts;params.ts;dependent-a.ts;dependent-c.ts 1
fold;process-cfg.ts;params.ts;dependent-b.ts 1
fold;unrelated-leaf.ts 1

process-cfg.ts retains 5 (itself + params.ts + a/b/c), never 8 — dependent-c.ts is reached once, through dependent-a.ts, not double-counted; unrelated-leaf.ts is its own independent blocker; would-fold.ts never enters the tree.

Tests

packages/core/src/discovery/fold-rank.test.ts (real temp-file fixtures, same convention as fold-import.test.ts):

  • chain — one root-cause blocker retains every file behind it
  • diamond — a single root cause reached two ways is not double-counted
  • two independent blockers — a shared dependent inflates neither
  • a reverse-tainted file is reported separately and never enters the tree
  • fold-mode files are excluded from the tree entirely
  • retained counts are conserved across a mixed graph
  • collapsed-format export folds to retained counts under a shared root frame

Plus CLI-surface tests: main.test.ts (--fold-rank/--fold-rank <path>/joined-form parsing) and build.test.ts (--fold-rank end-to-end through buildCommand, asserting both the printed report and the written collapsed file).

Test plan

  • npm install
  • npx vitest run packages/core — 328 files / 5027 tests passed
  • CHANT_SKIP_ARTIFACT_CHECK=1 npx vitest run packages/core/src/discovery/fold-rank.test.ts packages/core/src/discovery/fold-import.test.ts packages/core/src/discovery/index.test.ts packages/core/src/cli/main.test.ts packages/core/src/cli/commands/build.test.ts — all green

Fixes #1083

Note for reviewers

Per the issue: another PR (#1174) is concurrently touching call-form fold handling in fold-import.ts. The only change here to that file is extracting planFoldTaint's existing edge-building loop into an exported buildProjectImportEdges (identical logic, no behavior change) — nothing in the call-form resolution path was touched.

🤖 Generated with Claude Code

Computes immediate dominators (Cooper-Harvey-Kennedy) over the forward
import-failure graph among a --fold build's run-mode files, so fixing
one root cause is ranked by how many files it actually unblocks —
conserved, with diamonds and independent-blocker files counted exactly
once. Reverse-taint files (chant #1044 — would fold in isolation, held
back by a run-mode importer) are excluded from the tree and reported in
a separate bucket, since that contagion direction can't be expressed as
a dominator edge.

Surfaced via `chant build --fold --fold-rank [<path>]`, mirroring
--report's context-sensitive bare-boolean-vs-path convention: bare
prints the ranked text report, a path additionally exports the
dominator tree in Brendan Gregg collapsed stack format (weighted by
retained count) for any flame/icicle viewer.

fold-import.ts's planFoldTaint already built the project import-edge
graph; extracted it into exported buildProjectImportEdges so the new
ranking (discovery/fold-rank.ts) reuses it instead of re-parsing every
file's imports.

Fixes #1083.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@lex00
lex00 merged commit 612792b into main Aug 26, 2026
5 checks passed
@lex00
lex00 deleted the feat/1083-fold-blocker-ranking branch August 26, 2026 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(core): rank fold blockers by dominator retained-count

1 participant