Skip to content

feat(core): fold — resolve identifiers across the module graph #1020

Closed

Description

@lex00

Part of the fold epic #1019 (epic closed; this child never shipped). Cold handoff — body rewritten 2026-07-26 against main d5b94031. The original text referenced the epic's draft placeholder numbers (#1, #3, #5); the real issues are #1026 (reducer), #1021 (symbolic refs), #1023 (composites), all shipped.

Why this matters — measured, not assumed

Across all 98 corpus entries: 125 of 304 per-file fallbacks are unresolved identifier, the single largest cause. Per entry, cross-file resolution is involved in 44 of the 71 entries that have any fallback, and 18 entries are blocked by nothing else — they fold the moment this lands.

Together with #1044 (call-as-a-value, ~20 entries standalone) these two account for ~50 of the 71 blocked entries, which would take fold coverage from today's 15/98 to roughly 65/98.

What already shipped that this builds on

Scope

Extend identifier and property-access resolution to follow import statements through the module graph:

  • Resolve an imported binding to its export const initializer in the defining module, and fold it in that module's scope (its own consts, its own imports).
  • Handle re-export chains (export { x } from "./y").
  • Detect import cycles and fail with a located FoldError naming the cycle path.
  • require() stays rejected. Imports only.

The hard part — read this before designing

planFoldTaint (fold-import.ts, long doc comment — read it in full) exists because fold and run must never disagree about which object identity a given file's exports have. Today a file that folds but is imported by a run-fallback file is forced back to run, precisely so both sides share one module instance.

Cross-file folding changes that calculus. If alb.ts folds network.vpc.VpcId by reaching into network.ts, the resulting AttrRef's parent must be the same object that folding network.ts produced — not a second one built from the same source. AttrRef.parent is a WeakRef and logical names are assigned by identity match in discovery/resolve.ts:50; a duplicate object silently fails to get a logical name and serialization dies.

So this needs a per-build memo keyed on the defining file + export name, so each cross-file export folds once and every referrer shares that instance. Design that in from the start; it is not a later optimization.

Expect the taint rules to relax as a result — that is the point — but do not remove planFoldTaint. It still governs fold-vs-run boundaries for anything that still falls back.

Acceptance criteria

  • An imported const folds identically to an inline one.
  • An imported resource referenced via .attr folds to a symbolic ref pointing at the same entity instance the defining file's fold produced (assert identity, not just equality).
  • A reference cycle produces a located FoldError naming the cycle path.
  • Re-export chains resolve.
  • Resolution reuses discovery's existing module-graph traversal — no second file loader.
  • Tests cover: cross-file const, cross-file resource attr, re-export chain, cycle, and the shared-instance identity case.
  • Coverage moves and is gated. EXPECTED_FOLD in examples/fold-differential.test.ts is a committed list asserted by name; update it and report the before/after count. Drift must stay 0.

Out of scope

Composite factories imported from other files, and call-as-a-value (#1044). Do not widen the expression subset here — this is resolution only.

Verification standard

Activity

  1. added
    enhancement
    on Jul 24, 2026
  2. changed the title [-]feat(core): fold — AST value reducer for the supported subset (leaf, single-file)[/-] [+]feat(core): fold — resolve identifiers across the module graph[/+] on Jul 24, 2026
  3. lex00 commented on Jul 26, 2026

    @lex00
    ContributorAuthor

    Refreshed measurement against main (d5b94031)

    Re-ran chant build --fold across all 98 corpus entries and classified every fallback by its actual cause. Note the caveat now filed as #1054: reasons embed the full multi-line source node and use two different wordings for call-as-a-value, which skewed my first three passes. These numbers are from a parser that handles both.

    Per-file fallback blocks (304 total):

    cause blocks
    cross-file identifier (#1020) 125
    call-as-a-value (#1044) 113
    shape (arrow fns, non-resource exports, export default, unresolvable ctor import) 55
    taint (derivative — clears when its cause clears) 11

    Per-entry, of the 71 entries with at least one fallback:

    blocked by entries
    ONLY call-as-a-value (#1044) 20
    ONLY cross-file (#1020) 18
    needs both #1020 and #1044 12
    needs #1020 + #1044 + shape 8
    needs #1020 + shape 6
    ONLY shape 6
    needs #1044 + shape 1

    Leverage: #1044 alone unblocks ~20 entries, #1020 alone ~18, and together ~50 — taking fold coverage from today's 15/98 to roughly 65/98.

    Two corrections to what I previously said in this thread: the two issues are near-equal in leverage rather than #1020 dominating, and shape blockers gate only 6 entries on their own, not the large untracked category I suggested earlier.

    For this issue specifically: cross-file resolution is involved in 44 of the 71 blocked entries and is the single largest per-file cause at 125 blocks. It has no design blocker — the spec is settled — so it is implementable immediately, unlike #1044 which needs an architectural decision first. That is why I am working this one first despite #1044 having marginally higher standalone leverage.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions