Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/src/content/docs/concepts/typescript-as-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ Fold covers more than a single leaf resource now:
- **Registered intrinsic tagged templates** — a lexicon can register a tagged template (e.g. a string-substitution helper) as foldable; when it's registered, an interpolation inside it folds like any other value in the subset. A tagged template whose tag *isn't* registered falls outside the subset. See your lexicon's intrinsics page for which of its intrinsics are tagged templates.
- **Registered intrinsic calls** — the same, for an intrinsic authored as a plain call (`Ref(...)`, `Concat(...)`), where its lexicon has opted that intrinsic in (#1044). Arguments fold by the ordinary subset rules; the real function is resolved through the file's own imports and invoked, so the intrinsic object is the one the run path would have built. Opt-in is per intrinsic and off by default — see [Registered intrinsic calls](#registered-intrinsic-calls).
- **Cross-file references** — an imported `const`, or an imported resource referenced through an attribute, resolves through the module graph and folds in the defining file's own scope, including re-export chains (#1020). Each cross-file export folds exactly once per build and every referrer shares that one instance, so a symbolic reference always points at the same entity the defining file produced. An import cycle is rejected with a located error rather than followed.
- **Lexicon package exports** — the same resolution follows a bare import into a lexicon package, so a lexicon's plain data exports fold as values: Azure's and GCP's pseudo-parameter namespaces (`Azure.ResourceGroupLocation`, `GCP.ProjectId`), AWS's `S3Actions`, GitLab's `CI` (#1063). Only lexicons this build actually loaded are followed, and only the package itself, never a subpath or an unrelated package — the boundary is the lexicon list your project already declares or chant already detected, not `node_modules` at large. The lexicon module is imported the same way the run path imports it, so what fold captures is the same object, not a copy.

What still forces a fallback to run, per file: **a function call used as a value that is neither a registered authoring helper nor an opted-in intrinsic** — your own helper, an array `.map(...)`, a method call, an intrinsic whose lexicon has not opted it in. Also: a composite call or `new Type(...)` nested as a *value* inside something else rather than being the file's own top-level export, a re-export, `export default`, `let`/`var`, and an exported function or class declaration.

Expand All @@ -287,16 +288,16 @@ None of this is something you opt into piecemeal or write differently for — it
### Fold coverage today

{/* GENERATED:fold-coverage:start */}
As of this measurement, **32 of 98** example projects in the corpus fold completely — every file in them reduces to a value with zero module execution. The remaining **66** have at least one file that falls back to running.
As of this measurement, **53 of 98** example projects in the corpus fold completely — every file in them reduces to a value with zero module execution. The remaining **45** have at least one file that falls back to running.
{/* GENERATED:fold-coverage:end */}

This is measured over the same corpus [`just fold-differential`](#byte-identical-output) builds both ways: every tutorial under `examples/*/src`, plus each lexicon's own build fixtures under `lexicons/*/examples/*/src`. It's a count of chant's own example suite, not a sample of real-world source — read it as "how much of chant's examples fold today," not as an estimate for any other codebase.

That corpus is also chant's own maintained examples, and some of them are expected to be rewritten into foldable shapes over time, or dropped. Both changes move this number without the folder gaining or losing any actual capability: rewriting an example raises the count for a reason unrelated to what `--fold` can do, and adding or removing an example moves the denominator the same way. Treat a change here as "the corpus changed," not as evidence either way about fold coverage of your own project.

As measured on the corpus after #1044 (chant `1fb1b375`, per-file fallback reasons classified by cause): a function call used as a value blocks 37 files and is the only blocker in 9 entries — down from 77 files and 21 entries before opting the AWS and Azure intrinsics in. What's left there is a lexicon that hasn't been audited yet (github's `Checkout`, 6 entries; helm's `include`/`printf`, 3) plus array `.map(...)` and project-local helpers, which are arbitrary JavaScript and stay rejected by construction. Cross-file references into a lexicon package's own exports (rather than a project's own source files) now block 116 files across 36 entries, tracked as [#1063](https://github.com/INTENTIUS/chant/issues/1063), and are the largest remaining cause.
As measured on the corpus after #1063 (per-file fallback reasons classified by cause): cross-file references into a lexicon package's own exports no longer block anything — that cause went from 116 files across 36 entries to zero. The largest remaining cause is a `new Type(...)` nested as a value inside something else: 63 files, and the only blocker in 22 entries. Next is a function call used as a value that is neither a registered authoring helper nor an opted-in intrinsic: 38 files, the only blocker in 9 entries, mostly a lexicon that hasn't been audited yet (github's `Checkout`, helm's `include`/`printf`) plus array `.map(...)` and project-local helpers, which are arbitrary JavaScript and stay rejected by construction. Unresolved identifiers still block 67 files, but every one of those is now a project-file chain the folder doesn't reach yet or an environment read it refuses on purpose — 9 of them are `process.env`.

Per lexicon, so the two can be judged separately: the AWS corpus went from 4 of 18 entries folding to **12 of 18**, and the 6 that remain are blocked by cross-file references, shape gaps, and composite-factory calls — not by intrinsics. The Azure corpus went from **0 of 13 to 0 of 13**: `Concat(...)` stopped blocking both entries where it was the only blocker, and both then stopped on `Azure.ResourceGroupLocation` used as a plain value, which is #1063. Azure's fold coverage is capped at zero by that, not by anything #1044 could have fixed. This is also the correction to what #1039 predicted: the intrinsics wiring was called "the single biggest lever" and moved one entry; the call form moved eight; and for Azure specifically the lever that matters is a different issue entirely.
Per lexicon, so the causes can be judged separately: the Azure corpus went from **0 of 13 to 13 of 13** and the GCP corpus from **0 of 8 to 8 of 8**. Both were capped at zero by a single line per example — a pseudo-parameter read through the lexicon's own namespace — and nothing else. AWS holds at **12 of 18**; its remaining 6 are nested constructors and composite-factory calls, unaffected by #1063 even though the same change cleared every `S3Actions` and value-position `AWS` failure in that corpus. This is the second correction to what #1039 predicted: the intrinsics wiring was called "the single biggest lever" and moved one entry, the call form moved eight, and reaching into lexicon package exports moved twenty-one.

These counts are a snapshot, not refreshed by the mechanism above; they'll drift from the corpus over time the same way the rest of this page would without #1062. One gap the earlier snapshot named — a resource constructor whose first argument isn't the props object literal — is closed as of #1082: constructor arguments are folded positionally, so a class like the AWS `Parameter` (`new Parameter("String", {...})`) folds like any other.

Expand Down
22 changes: 21 additions & 1 deletion examples/differential-corpus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,21 @@ export interface CorpusEntry {
serializers: Serializer[];
/** Intrinsics to fold with (chant #1039) — see {@link ALL_INTRINSICS}/{@link INTRINSICS_BY_LEXICON}. */
intrinsics: IntrinsicDef[];
/**
* chant #1063 — the lexicon NAMES active for this entry, matching the
* serializer/intrinsic selection below. The differentials build through
* core's `build()` rather than the CLI, so — exactly as they already do for
* `intrinsics` — they have to reproduce `cli/commands/build.ts`'s own
* `options.plugins.map(p => p.name)` step, or the fold path would be
* measured without the bare-specifier allowlist real `chant build --fold`
* gives it.
*/
lexicons: string[];
}

/** Every lexicon name the corpus can build with — the keys of {@link SERIALIZER_BY_LEXICON}. */
export const ALL_LEXICONS: string[] = Object.keys(SERIALIZER_BY_LEXICON);

function isDir(p: string): boolean {
try {
return statSync(p).isDirectory();
Expand Down Expand Up @@ -169,7 +182,13 @@ export function discoverCorpus(): CorpusEntry[] {
if (!dirent.isDirectory()) continue;
const srcDir = resolve(examplesDir, dirent.name, "src");
if (isDir(srcDir)) {
entries.push({ name: `examples/${dirent.name}`, srcDir, serializers: ALL_SERIALIZERS, intrinsics: ALL_INTRINSICS });
entries.push({
name: `examples/${dirent.name}`,
srcDir,
serializers: ALL_SERIALIZERS,
intrinsics: ALL_INTRINSICS,
lexicons: ALL_LEXICONS,
});
}
}

Expand All @@ -189,6 +208,7 @@ export function discoverCorpus(): CorpusEntry[] {
srcDir,
serializers: [serializer],
intrinsics: INTRINSICS_BY_LEXICON[lexDirent.name] ?? [],
lexicons: [lexDirent.name],
});
}
}
Expand Down
39 changes: 36 additions & 3 deletions examples/fold-differential.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ const report: ReportRow[] = [];
* stopped on `Azure.ResourceGroupLocation` — a lexicon-package namespace
* access, #1063. That is the measured ceiling this PR reports: azure's
* remaining corpus is gated on #1063, not on #1044.
*
* chant #1063 grew it from 32 to 53 — the largest single jump so far, and
* the whole of azure's and gcp's corpora at once. The 21 added entries are
* every azure example (13) and every gcp example (8): each had exactly one
* file, failing on `Azure.<PseudoParameter>` / `GCP.<PseudoParameter>` — an
* identifier imported from the LEXICON PACKAGE rather than from a sibling
* project file, which #1020's cross-file resolution skipped by construction
* (it followed relative specifiers only). Following a bare specifier into an
* ACTIVE lexicon package also cleared every remaining `S3Actions` (aws),
* `CI` (gitlab) and value-position `AWS` (aws) failure in the corpus, but
* those all live in `docs-snippets` entries carrying several unrelated
* blockers apiece, so they move no entry on their own.
*/
const EXPECTED_FOLD: readonly string[] = [
"examples/alert-triage",
Expand All @@ -135,7 +147,28 @@ const EXPECTED_FOLD: readonly string[] = [
"lexicons/aws/examples/multi-service-alb",
"lexicons/aws/examples/rds-postgres",
"lexicons/aws/examples/vpc",
"lexicons/azure/examples/aks-cluster",
"lexicons/azure/examples/basic-storage",
"lexicons/azure/examples/container-instance",
"lexicons/azure/examples/cosmos-db",
"lexicons/azure/examples/function-app",
"lexicons/azure/examples/key-vault",
"lexicons/azure/examples/multi-resource",
"lexicons/azure/examples/private-endpoint",
"lexicons/azure/examples/redis-cache",
"lexicons/azure/examples/service-bus",
"lexicons/azure/examples/sql-database",
"lexicons/azure/examples/vnet-vms",
"lexicons/azure/examples/web-app",
"lexicons/docker/examples/basic-app",
"lexicons/gcp/examples/basic-bucket",
"lexicons/gcp/examples/cloud-function",
"lexicons/gcp/examples/cloud-run",
"lexicons/gcp/examples/cloud-sql",
"lexicons/gcp/examples/cloud-storage-lifecycle",
"lexicons/gcp/examples/gke-cluster",
"lexicons/gcp/examples/pubsub",
"lexicons/gcp/examples/vpc-network",
"lexicons/gitlab/examples/node-pipeline",
"lexicons/gitlab/examples/python-pipeline",
"lexicons/helm/examples/composites-basic",
Expand Down Expand Up @@ -182,7 +215,7 @@ async function buildBothWays(
entry: CorpusEntry,
): Promise<{ run: Awaited<ReturnType<typeof build>>; fold: Awaited<ReturnType<typeof build>>; neededIsolation: boolean }> {
const run = await build(entry.srcDir, entry.serializers, undefined, { fold: false });
const fold = await build(entry.srcDir, entry.serializers, undefined, { fold: true, intrinsics: entry.intrinsics });
const fold = await build(entry.srcDir, entry.serializers, undefined, { fold: true, intrinsics: entry.intrinsics, lexicons: entry.lexicons });

if (outputsEqual(normalizeOutputs(fold.outputs), normalizeOutputs(run.outputs))) {
return { run, fold, neededIsolation: false };
Expand All @@ -191,7 +224,7 @@ async function buildBothWays(
vi.resetModules();
const freshRun = await build(entry.srcDir, entry.serializers, undefined, { fold: false });
vi.resetModules();
const freshFold = await build(entry.srcDir, entry.serializers, undefined, { fold: true, intrinsics: entry.intrinsics });
const freshFold = await build(entry.srcDir, entry.serializers, undefined, { fold: true, intrinsics: entry.intrinsics, lexicons: entry.lexicons });
return { run: freshRun, fold: freshFold, neededIsolation: true };
}

Expand All @@ -209,7 +242,7 @@ describe("fold differential — fold output === run output (#1025, epic #1019)",
// both-ways comparison + module-reset retry; that retry (which reloads the
// whole module graph) is what makes the full corpus intractable now that
// many entries fall back, and it buys nothing for entries we don't gate.
const probe = await build(entry.srcDir, entry.serializers, undefined, { fold: true, intrinsics: entry.intrinsics });
const probe = await build(entry.srcDir, entry.serializers, undefined, { fold: true, intrinsics: entry.intrinsics, lexicons: entry.lexicons });
const mode = classifyFoldMode(probe.foldDecisions);
if (mode !== "fold") {
report.push({ name: entry.name, mode, identical: true, fileCount: probe.foldDecisions.length, neededIsolation: false });
Expand Down
2 changes: 2 additions & 0 deletions examples/sandbox-differential.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ async function buildBothWays(entry: CorpusEntry) {
fold: true,
sandbox: true,
intrinsics: entry.intrinsics,
lexicons: entry.lexicons,
});

const runResult = await run();
Expand Down Expand Up @@ -276,6 +277,7 @@ describe("sandbox differential — sandboxed-run output === in-process-run outpu
const plainFold = await build(entry.srcDir, entry.serializers, undefined, {
fold: true,
intrinsics: entry.intrinsics,
lexicons: entry.lexicons,
});
expect(
classifyMode(sandboxedResult.foldDecisions),
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ export interface BuildOptions {
*/
intrinsics?: IntrinsicDef[];

/**
* chant #1063 — the lexicon names loaded for this build. Passed straight
* through to {@link DiscoveryOptions.lexicons} in `./discovery/index`;
* ignored unless {@link fold} is set. The CLI populates this from
* `options.plugins.map(p => p.name)`.
*/
lexicons?: readonly string[];

/**
* chant #1045 Phase 2 — opt-in: run-fallback files (or, when {@link fold}
* isn't set, every file) execute together, isolated, in one sandboxed
Expand Down Expand Up @@ -513,6 +521,7 @@ export async function build(
const discoveryResult = await discover(path, {
fold: options?.fold,
intrinsics: options?.intrinsics,
lexicons: options?.lexicons,
sandbox: options?.sandbox,
buildParams: options?.buildParams,
});
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,22 @@ export async function buildCommand(options: BuildOptions): Promise<BuildResult>
// any), hence the guard.
const intrinsics = options.plugins?.flatMap((plugin) => plugin.intrinsics?.() ?? []) ?? [];

// #1063 — the same loaded plugins, by NAME, are this build's allowlist for
// following a bare import specifier into a lexicon package (so `Azure`,
// `GCP`, `S3Actions`, `CI` fold as values). A plugin's `name` is the
// lexicon name `loadPlugin()` was called with, which is exactly what
// `@intentius/chant-lexicon-<name>` was imported from — see
// ../plugins.ts and fold-import.ts's `lexiconPackageName`.
const lexicons = options.plugins?.map((plugin) => plugin.name) ?? [];

// Run the build
const result = await build(infraPath, options.serializers, undefined, {
ownership,
config: config as unknown as Record<string, unknown>,
fold,
sandbox,
intrinsics,
lexicons,
buildParams: paramsResolution.provenance,
});

Expand Down
Loading
Loading