Repository navigation
Description
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
packages/core/src/fold/fold.ts+subset.ts— the reducer and the shared subset predicate.packages/core/src/discovery/fold-import.ts— per-file fold with run-fallback, composite factory interpretation (feat(core): fold — composite factory interpretation (removes the last run) #1023), intrinsic threading (fix(fold): thread lexicon intrinsics through --fold and revive folded tags #1042).- Symbolic cross-resource refs already work within a file:
bucket.namefolds to anAttrRef. This issue is about making that work across files.
Scope
Extend identifier and property-access resolution to follow import statements through the module graph:
- Resolve an imported binding to its
export constinitializer 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
FoldErrornaming 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
constfolds identically to an inline one. - An imported resource referenced via
.attrfolds 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
FoldErrornaming 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_FOLDinexamples/fold-differential.test.tsis 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
- Measure with
just fold-differentialin a checkout with its ownnode_modulesand generated lexicon artifacts. Never symlinknode_modulesfrom another checkout — it resolves lexicon plugins to the wrong tree and produces confidently wrong numbers (this cost a full review cycle on fix(fold): thread lexicon intrinsics through --fold and revive folded tags #1042). - Diff the sorted per-entry
[identical] <mode> <name>lines againstmain, not a summary count. No entry that folds today may regress. - Fold reasons currently embed the full multi-line source node and use two inconsistent wordings (bug(core): fold fallback reasons embed the whole source node, truncating the real error #1054), so grepping them is unreliable — parse whole blocks, not lines.
Activity
[-]feat(core): fold — AST value reducer for the supported subset (leaf, single-file)[/-][+]feat(core): fold — resolve identifiers across the module graph[/+]epic: fold TypeScript-as-Data instead of running it — #1026,#1021,#1022,
docs: adversarial audit of fold / TypeScript-as-Data docs (#1038) (#1043
lex00 commentedon Jul 26, 2026
Refreshed measurement against
main(d5b94031)Re-ran
chant build --foldacross 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):
export default, unresolvable ctor import)Per-entry, of the 71 entries with at least one fallback:
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.
fix(fold): stop cross-file resolution from hanging sandbox-differential
feat(fold): resolve identifiers across the module graph (#1055)
docs(fold): refresh the foldable/not-foldable inventory after #1020 (#…
feat(core): fold registered call-form intrinsics — per-intrinsic opt-…
feat(core): fold cross-file references into active lexicon package ex…
fix(core): fold fallback reasons lead with the cause, one line, unifi…
docs(fold): correct stale cross-file, template, and corpus-count clai…