Skip to content

feat(core): fold cross-file references into active lexicon package exports (#1063) - #1105

Merged
lex00 merged 3 commits into
mainfrom
feat/1063-lexicon-export-refs
Jul 27, 2026
Merged

lex00 merged 3 commits into
mainfrom
feat/1063-lexicon-export-refs

Conversation

@lex00

@lex00 lex00 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Closes #1063.

What this is, restated

#1020 gave fold cross-file identifier resolution, but isProjectFileSpecifier limits it to relative/absolute specifiers. A bare package specifier is skipped, so an identifier imported from a lexicon package fails as unresolved identifier and takes the whole file back to the run path — even though what it names is plain static data the folder has always handled. Azure and GCP (pseudo-parameter namespaces), aws's S3Actions, gitlab's CI are all as const object literals.

This extends resolution to bare specifiers that name an active lexicon package of this build, and to nothing else. Specifically:

Design: how "active lexicon package" is decided without cold resolution

The build already knows its lexicons by name, long before discovery runs: resolveProjectLexicons() reads chant.config.ts's lexicons or falls back to detectLexicons()'s text scan, then loadPlugins() imports @intentius/chant-lexicon-<name> for each. That name list is now threaded down as DiscoveryOptions.lexicons into the fold session, which turns it into a Set of package specifiers.

Deciding whether a bare specifier is in scope is then a Set.has on a string. Nothing is probed, read, or resolved to find out what a specifier is — which is the whole point, since resolveModulePath's bare branch can fall through to createRequire(fromFile).resolve(specifier), the call #1020's own history measured at 60s, then 69s, then 361s for successive new packages in one process. This is the same shape #1064 used for @intentius/chant/params: recognize by text, never resolve to check.

Three restrictions fall out of that, each deliberate:

  • Exact package specifier, no subpaths. fastResolveBareSpecifier only recognizes a package root (it looks for <node_modules>/<specifier>/package.json), so a subpath specifier falls straight through to the slow createRequire().resolve(). Lexicons re-export their whole surface from the barrel and every example imports it that way, so subpaths buy nothing and cost exactly the thing this avoids.
  • Lexicons of this build. A @intentius/chant-lexicon-* package that happens to be installed but wasn't loaded is as out of scope as node:fs. A caller that supplies no lexicon list gets no bare-specifier resolution at all, rather than a looser fallback.
  • Data exports only, no callables. A lexicon's resource classes, composite factories and intrinsic functions already have resolution paths that know how to invoke them (resolveResourceEntity, resolveCallExpression, reviveFoldedValue). Binding them as identifier values would widen what folds well past what this needs — none of Azure/GCP/S3Actions/CI is a function.

Once a specifier is in scope, the value comes from the real module, imported through the session-wide importCache (at most one real import() per package per build). importModule is a plain dynamic import() with no cache-busting, so that is the same module object the run path gets.

Identity, and #1097's liveSources

reviveFoldedValue grows one passthrough: a live Intrinsic instance. Without it the generic object walk rebuilds Azure.ResourceGroupLocation as a plain {}, dropping the prototype that carries toJSON — which serializes {} where the run path emits [resourceGroup().location]. isIntrinsic keys off Symbol.for("chant.intrinsic"), a global symbol, so it holds across separately-loaded copies of chant-core.

No liveSources edge is recorded for a lexicon-package value, and I don't think one should be. That mechanism exists so a folded file which captured another discovered file's objects is invalidated when that file is forced back to run — a fold/run disagreement about identity. A lexicon package has no such duality. It is not a discovered source file, so planFoldTaint would drop the edge anyway (if (!fileSet.has(source)) continue). It never falls back to run. And both paths reach it through the identical un-cache-busted import() of the identical resolved path, so the object fold captures is the object the run path holds. There is nothing for the two sides to disagree about. This is also not new behavior: resolveSymbolicValue has resolved AWS.StackName through exactly this mechanism since #1039, with the differential green throughout.

Sandbox

No module-scope filesystem access is added. lexiconPackageName and activeLexiconPackage are pure string arithmetic; the resolve/import happens lazily inside resolveActiveLexiconExport. Same rule PR #1081's 7d63ee54 established for paramsModulePath().

Numbers

just fold-differential

before (2ab05b8c) after
fold 32 53
run-fallback 66 45
drift 0 0
isolated-retry 1 1
tests 101 passed 101 passed

Sorted per-entry diff of the report — 21 lines change, all in the same direction, nothing else moves:

> [identical] fold         lexicons/azure/examples/aks-cluster
> [identical] fold         lexicons/azure/examples/basic-storage
> [identical] fold         lexicons/azure/examples/container-instance
> [identical] fold         lexicons/azure/examples/cosmos-db
> [identical] fold         lexicons/azure/examples/function-app
> [identical] fold         lexicons/azure/examples/key-vault
> [identical] fold         lexicons/azure/examples/multi-resource
> [identical] fold         lexicons/azure/examples/private-endpoint
> [identical] fold         lexicons/azure/examples/redis-cache
> [identical] fold         lexicons/azure/examples/service-bus
> [identical] fold         lexicons/azure/examples/sql-database
> [identical] fold         lexicons/azure/examples/vnet-vms
> [identical] fold         lexicons/azure/examples/web-app
> [identical] fold         lexicons/gcp/examples/basic-bucket
> [identical] fold         lexicons/gcp/examples/cloud-function
> [identical] fold         lexicons/gcp/examples/cloud-run
> [identical] fold         lexicons/gcp/examples/cloud-sql
> [identical] fold         lexicons/gcp/examples/cloud-storage-lifecycle
> [identical] fold         lexicons/gcp/examples/gke-cluster
> [identical] fold         lexicons/gcp/examples/pubsub
> [identical] fold         lexicons/gcp/examples/vpc-network
< [identical] run-fallback  … the same 21 names

EXPECTED_FOLD gains exactly those 21 names. No entry that folded before regressed.

Azure's 13, checked one by one

#1097 reported that all 13 azure corpus entries had this issue as their sole remaining blocker. That held: 13 of 13 flipped. Each had exactly one file, and its reason named Azure:

aks-cluster         main.ts  8:13 - unresolved identifier: Azure   -> folds
basic-storage       main.ts  6:13 - unresolved identifier: Azure   -> folds
container-instance  main.ts  8:13 - unresolved identifier: Azure   -> folds
cosmos-db           main.ts  8:13 - unresolved identifier: Azure   -> folds
function-app        main.ts  6:13 - unresolved identifier: Azure   -> folds
key-vault           main.ts  5:13 - unresolved identifier: Azure   -> folds
multi-resource      main.ts 19:13 - unresolved identifier: Azure   -> folds
private-endpoint    main.ts 11:13 - unresolved identifier: Azure   -> folds
redis-cache         main.ts  5:13 - unresolved identifier: Azure   -> folds
service-bus         main.ts  5:13 - unresolved identifier: Azure   -> folds
sql-database        main.ts 16:13 - unresolved identifier: Azure   -> folds
vnet-vms            main.ts  3:18 - unresolved identifier: Azure   -> folds
web-app             main.ts  7:13 - unresolved identifier: Azure   -> folds

GCP is the same story, 8 of 8, all on GCP in an annotations initializer.

Corpus-wide unresolved identifier tallies

Whole-block parse of every run-fallback reason (both wordings, multi-line reasons kept intact), before and after:

identifier before after
Azure 15 0
GCP 12 0
CI 7 0
S3Actions 2 0
AWS (value position) 2 0
config 36 36
CRDB_CLUSTER 24 24
shared 12 12
GCP_PROJECT_ID 2 2

CI, S3Actions and AWS all cleared, but each lives in a docs-snippets entry carrying several unrelated blockers, so they move no entry on their own — the files behind them now stop on their next blocker instead. E.g. lexicons/aws/examples/lambda-api/src/process-api.ts went from 11:15 - unresolved identifier: S3Actions to 34:20 - unresolved identifier: dataBucket (… nested new ServerSideEncryptionByDefault(...) as a value …).

What deliberately still fails

process and every other environment read. It is unchanged and covered by a test that puts an active lexicon package in scope alongside it, so the pass is not vacuous:

test("process.env still rejects, with the build-parameter guidance, even with an active lexicon package in scope")
  -> reason contains: ambient "process" read is not foldable

It is also visible in the corpus: examples/temporal-crdb-deploy's networking.ts used to stop on GCP; it now resolves past that and stops on process.env further down the chain, which is the correct outcome.

Also still rejected, by the same tests: a lexicon package not loaded by this build, a bare specifier with no active lexicons at all, a non-lexicon package installed right next to one that is active, and a lexicon's callable export used as a value.

Runtime — repeat runs, not a single pass

npx vitest run examples/fold-differential.test.ts, three consecutive runs each side, same machine, same session:

run 1 run 2 run 3
main 2ab05b8c 4.71s 4.78s 4.72s
this branch 4.97s 4.81s 4.80s

Full just test: main 30.17s, this branch 26.51s (and 24.97s on an earlier run). 756 test files, 8703 tests, 1 file / 18 tests skipped, both sides.

Other gates

  • just build — clean.
  • just lint — clean.
  • just test — 756 passed, 1 skipped.
  • just check-lexicons — 15 known tracked failures, no untracked failures. Same as main.
  • Real CLI end-to-end, chant build src --lexicon azure --fold in lexicons/azure/examples/basic-storage: both files report [fold:fold] … no module execution, and location serializes as [resourceGroup().location] — the Intrinsic passthrough doing its job outside the test harness.
  • No lexicon source touched, so no lexicon build to verify. This lands entirely in core resolution.

Not done here

lex00 added 3 commits July 26, 2026 21:23
…ports

#1020 taught fold to resolve an identifier across the module graph, but only
through relative/absolute specifiers — `isProjectFileSpecifier` skipped bare
package specifiers outright. So an identifier imported from a LEXICON package
failed as "unresolved identifier" even when what it named was plain static
data: azure's `Azure` and gcp's `GCP` pseudo-parameter namespaces, aws's
`S3Actions`, gitlab's `CI`. Every one of those is an `as const` object literal
the folder has always handled; the gap was purely the resolver's scope.

`buildExternals` now follows a bare specifier into a package, and only into a
package, that is an ACTIVE LEXICON of this build. The allowlist is the lexicon
names the build already resolved before discovery ran, threaded down as
`DiscoveryOptions.lexicons` and matched by TEXT — nothing is resolved to find
out what it is, which is what keeps `resolveModulePath`'s pathological
cold-resolution path (measured at up to ~361s for a genuinely new bare
specifier, regressed twice during #1020) out of reach. Subpaths are excluded
for the same reason: the fast resolver only recognizes a package root, so a
subpath specifier would fall through to exactly that slow path, and lexicons
re-export their whole surface from the barrel anyway.

What comes back is the real export off the real module, imported through the
session-wide import cache — the same module object the run path gets, since
`importModule` is a plain `import()` with no cache-busting. So identity holds
by construction, and `reviveFoldedValue` grows a passthrough for live
`Intrinsic` instances: without it the generic object walk rebuilds
`Azure.ResourceGroupLocation` as a plain `{}`, dropping the prototype that
carries `toJSON` and serializing `{}` where run emits
`[resourceGroup().location]`.

Callable exports stay out. A lexicon's resource classes, composite factories
and intrinsic functions already have resolution paths that know how to invoke
them; binding them as identifier values would widen what folds well beyond
what this needs.

No `liveSources` edge is recorded for a lexicon-package value. That mechanism
exists for a folded file that captured another discovered FILE's objects, so
it can be invalidated when that file is forced back to run. A lexicon package
has no fold/run duality: it is not a discovered file, `planFoldTaint` filters
to the discovered set anyway, and both paths reach it through the identical
import of the identical path.

`process` and every other ambient environment read still fail, with a test
that pins it alongside an active lexicon package in scope.
The differentials build through core's `build()` rather than the CLI, so they
have to reproduce `cli/commands/build.ts`'s active-lexicon step the same way
they already reproduce its `intrinsics` one — otherwise fold is measured
without the allowlist real `chant build --fold` gives it. Corpus entries now
carry their lexicon names: the full set for the root tutorials (which
deliberately mix lexicons), the one lexicon for a `lexicons/*/examples/*`
fixture, matching how serializers and intrinsics are already selected.

21 entries move run-fallback to fold: all 13 azure examples and all 8 gcp
examples. Each was a single file failing on `Azure.<PseudoParameter>` /
`GCP.<PseudoParameter>`. Nothing else changes — 45 entries still fall back,
drift stays 0, and the suite runs in 4.31s against 4.33s before.

The same change clears every remaining `S3Actions`, `CI` and value-position
`AWS` failure in the corpus, but those sit in `docs-snippets` entries with
several unrelated blockers apiece, so they move no entry on their own.
…blocker snapshot

Adds the new subset bullet, and refreshes the measured blocker breakdown the
page carries: the lexicon-package cause is gone (116 files, 36 entries -> 0),
a nested `new Type(...)` used as a value is now the largest at 63 files and
22 sole-blocker entries, and the per-lexicon lines read azure 13/13, gcp 8/8,
aws 12/18.
@lex00

lex00 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Orchestrator verification, re-run independently: fold-differential 53/98, drift 0 (confirmed across 5 full-suite runs); 5 consecutive clean suite runs, 24–26s, 8703 passed — no hang-class regression; diff read for the two regression classes that have bitten (module-init probing: none added; cold bare-specifier resolution: scope decided by Set.has on the build's own lexicon names, resolution stays behind the existing memoized paths). Azure 0/13→13/13 and GCP 0/8→8/8 as claimed. The two flagged judgment calls (no liveSources for lexicon-package values; isIntrinsic passthrough in revival) both check out — lexicon packages have no fold/run duality so taint doesn't apply, and the passthrough is what keeps toJSON envelopes intact. Merging.

@lex00
lex00 merged commit 7fff510 into main Jul 27, 2026
6 checks passed
lex00 added a commit that referenced this pull request Jul 27, 2026
…ps flagging calls that fold (#1106) (#1110)

* fix(lint): thread the active lexicons' intrinsic registry into runLint

findSubsetViolation (fold/subset.ts) has taken an optional IntrinsicDef
registry since #1044 and answers a registered, opted-in call-form
intrinsic (Ref(...), GetAtt(...)) exactly like fold() does. EVL001 calls
that same predicate but runLint never had a registry to hand it, so
chant lint kept flagging calls that fold cleanly.

Add LintContext.intrinsics, thread it through runLint's signature, and
pass it in from the three places that build a LintContext for a real
project: the lint command's initial pass, its --fix re-lint, and the
LSP's per-file diagnostics. Each gathers its intrinsics from the same
loaded lexicon plugins the build path already reads
plugin.intrinsics?.() off of, mirroring how discover() has threaded
IntrinsicDef[] into the fold path since #1039/#1105.

A LintContext built without that plumbing (a bare unit test, a
consumer that hasn't resolved a project's lexicons) keeps the
pre-#1044 conservative answer: every call is a violation.

* test(lint): flip the EVL/fold divergence test to convergence, add coverage

subset.test.ts pinned "registry-less EVL is STRICTER than fold on an
opted-in call" as a known, tracked property. Now that runLint threads
the intrinsic registry through, a LintContext built with it converges
with fold() instead of diverging from it — flip the test to assert
that, and add one confirming a LintContext built without the registry
still keeps the old conservative answer.

Add unit coverage in evl001-non-literal-expression.test.ts for
context.intrinsics directly (registered+opted-in passes, registered
without the opt-in still flags, unregistered still flags), and an
end-to-end lintCommand test using the real aws lexicon plugin proving
chant lint no longer flags Ref(...) but still flags an unregistered
call in the same project.
@lex00
lex00 deleted the feat/1063-lexicon-export-refs branch August 3, 2026 19:21
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): fold cross-file references into lexicon package exports (Azure/GCP pseudo-params, action constants)

1 participant