Skip to content

feat(core): registered foldable authoring helpers — phase/output fold, foldResource non-literal first arg (#1082) - #1091

Merged
lex00 merged 3 commits into
mainfrom
feat/1082-foldable-authoring-helpers
Jul 27, 2026
Merged

lex00 merged 3 commits into
mainfrom
feat/1082-foldable-authoring-helpers

Conversation

@lex00

@lex00 lex00 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Closes #1082.

Acceptance criteria, restated

  1. A component authored the way chant documents it — phase("Apply", [...]) inside a Component object literal — must fold statically instead of falling the whole file back to running.
  2. new Parameter("String", {...}) must fold. foldResource required constructor argument 0 to be the props object literal, so the AWS deploy-time Parameter (signature (type, props)) could never fold whatever surrounded it. The fix is to drop the positional assumption and reconstruct the entity with exactly the arguments the source wrote.
  3. The new capability must be an explicit, hand-written, closed list of chant's own helper names. Calls in general keep failing: an unlisted name, a user's own function, an arrow function, a locally-declared function.
  4. The rest of chant's exported authoring surface must be audited for the same shape, with what was admitted and what was left out stated in writing.
  5. just fold-differential must stay at ≥ 21 of 98 folding, with zero drift on any entry that folds.
  6. loomster's fold count must measurably improve, measured against a build from this branch.
  7. Implied by the shared-subset contract: findSubsetViolation (which EVL001/EVL003 use) must never be stricter than fold(). Since fold() now accepts allowlisted helper calls, the shared classifier must accept them too, or lint would flag source that folds.

One clarification on the issue's own numbers: the acceptance line says "13/57" while the table shows loomster main at 13 fold / 42 run-fallback (55 files) and the migrated tree at 13/44 (57). 13/57 is the migrated tree. loomster#156 is not merged, so the tree I could measure is main — see the measurements section, which reproduces the issue's main row exactly.

Design: how the allowlist is declared, and why it stays closed

The registry is packages/core/src/fold/foldable-helpers.ts — a hand-written array of names, each carrying the module that defines it and why it passes the admission criteria. It is data, not a rule about what calls look like.

A name being in it is not permission to invoke whatever it is bound to. Folding a helper call is a two-key operation:

  1. Shape and name, in fold() and findSubsetViolation: the callee is a bare identifier naming a registered helper that this file has not shadowed with its own const. fold() reduces the call to a symbolic { __helper, args } envelope and executes nothing — the same treatment a registered intrinsic tagged template gets from { __intrinsic }.
  2. Provenance and invocation, in fold-import.ts: the envelope revives by resolving that name through the folding file's own import bindings and checking the import actually comes from chant — a published @intentius/chant* specifier, or a specifier resolving inside chant-core's own tree (which is how in-repo and test callers import it). Only then is the real function invoked with the folded arguments.

So the function that runs is the one the source's own import names. Fold never substitutes a reimplementation of a helper, which is why this registry cannot drift from the helpers' real behavior — a whole category of bug that a table of hand-written reducers would have introduced. It also means a project's own phase imported from ./helpers, or declared in the file, resolves to nothing chant owns and the file falls back to run, unchanged.

Registration is by name, and two of the names are defined twice in chant (phase/gate by both the component contract and the Op builders; stackOutput by both the component contract and the cross-stack primitive). That needs no disambiguation, precisely because step 2 invokes whichever one the file imported — and both definitions of each name independently satisfy the admission criteria, which is what makes sharing the name safe.

One extra guard for output(): it is pure but identity-sensitive, reading through its ref's WeakRef to the parent entity to learn the lexicon. An argument that revived to a symbolic { __attrRef } envelope is rejected rather than handed over, because constructing a LexiconOutput from a look-alike plain object produces output that is wrong rather than absent. A ref that resolved cross-file to a genuine live AttrRef passes through untouched. This reuses the existing rejection that already guarded intrinsic interiors, generalised from insideIntrinsic to requireLiveRefs.

Admitted

phase, gate, activity, stackOutput, output.

Deliberately not admitted

  • env() (env.ts) reads process.env. Its result depends on the environment rather than on its arguments, so folding it would freeze one run's environment into a statically-derived value. It is exactly the call fold must keep rejecting. Build-time parameters (feat(core): build-time parameter injection — DECIDED: option 1 (unblocks loomster, folds 13/55 today) #1064) are the supported answer.
  • Op() (op/builders.ts) constructs an OpResource, a Declarable. fold() already rejects a nested new Type(...) used as a value for a real, differential-caught reason; a factory returning one is the same hazard wearing a call.
  • propagate(), withDefaults(), resource(), mergeDefaults() (composite.ts) are composite definition helpers, not value-position helpers. propagate() mutates its argument in place, and fold-import's composite spine already resolves it live.
  • createResource() / createProperty() (runtime.ts) build classes at a lexicon module's top level; they never appear as a value in a project file.
  • Call-form lexicon intrinsics (Ref(...), Join(...)) are lexicon surface, not chant's own, and belong to feat(core): fold registered plain-call intrinsics (per-intrinsic opt-in) — DECIDED: yes #1044's IntrinsicDef registry. Only their tagged-template form folds today; this PR does not change that.

Scope item 2: constructor argument positions

foldResource now folds every argument in source order into FoldedResource.args, and resolveResourceEntity spreads that list into the constructor. The classic (props) / (props, attributes) shape is detected and reported exactly as before, with no args list, so no existing folded spec changes shape. findSubsetViolation was relaxed in step, classifying every argument on its own terms rather than rejecting one for its position — a widening, keeping it permissive relative to fold(). resourceCtorArgMessage had no remaining caller and was removed.

Measurements

All numbers below are from runs on this branch, rebased onto 405bb9b5 (chant-v0.20.0, i.e. after #1081 merged), in a clean worktree with its own npm install.

Corpus — just fold-differential

fold run-fallback drift
before (405bb9b5) 21 77 0
after 24 74 0

Newly folding: lexicons/aws/examples/fargate-alb, lexicons/aws/examples/multi-service-alb, lexicons/aws/examples/vpc — all three are the corpus's new Parameter("String", {...}) users. No entry regressed; all three are added to EXPECTED_FOLD so a future regression on them fails by name. The published count in typescript-as-data.mdx was regenerated with npm run generate:fold-coverage.

loomster — chant build src --lexicon aws --fold, run from loomster root with this branch's chant bin

loomster is at df687ad (main); loomster#156 is unmerged, so this is the issue's "loomster main" row, which the before-run reproduces exactly (13 fold / 42 run-fallback / 15 process blockers under 00bfbd75).

files fold run-fallback
before 55 13 42
after 55 13 42

The file count did not move, and I am not going to dress that up. Every blocker this issue targets is gone — all 10 of them — but each of those files has a user-defined function call immediately behind the chant one, which is the category the issue puts out of scope:

blocker before after
phase(...) 7 0
output(...) 2 0
resource ctor first arg not an object literal 1 0
sn(...) (loomster's own, reads process.env at module scope) 0 7
literalOutputValue(...) (loomster's own composite helper) 0 2
"would fold in isolation, but an importer falls back" 2 3

The 7 component files each stop at stack: sn("loom-db"); the 2 output files at literalOutputValue(...). downstream-stub/params.ts — the new Parameter(...) file — now folds in isolation and is held back only by the existing fold-taint rule, which is why the last row moved 2 → 3.

So acceptance criterion 6 is not met as stated on the tree I can measure. What is demonstrated is that chant's own API surface is no longer the ceiling: the next blocker in every case is application code, and the issue's own out-of-scope note ("worth confirming separately for loomster's helpers") is now the confirmed next step. sn() in particular is a build-time-parameter migration away from disappearing — it exists only to read LOOM_PROJECT/LOOM_ENV/LOOM_INSTANCE — so the migrated tree plus a sn rewrite is where the count actually moves. I would rather report the measured 13 than pick a scope that flatters it.

Test evidence

  • just build (tsc core) — clean.
  • just lint — clean.
  • just test — full suite; see the check run.
  • just fold-differential — 24/98, drift 0 (raw before/after logs kept).

New tests:

  • fold.test.ts — helper call folds to the envelope with arguments folded in order; helpers nest; an unfoldable argument still rejects the call; an unregistered name is still rejected naming the callee; a registered name shadowed by a local const is rejected; a registered name reached as a method (ns.phase(...)) is rejected; a spread argument is rejected. Constructor positions: props-second folds with an args list; a bare (type) call folds without inventing a props object; both classic shapes report unchanged; a non-foldable argument in any position rejects.
  • subset.test.ts — helper calls added to the shared supported/unsupported equivalence corpus (so EVL and fold must agree on them, including position of the rejection); a new documented divergence for helper shadowing.
  • subset-doc-parity.test.ts — the new doc section's own snippet is pulled out of the .mdx and classified for real, so the prose cannot drift from the classifier.
  • fold-import.test.ts — end-to-end against chant's real helpers: a component with phase/gate/stackOutput folds to the exact plain data the helpers return, with a throw sentinel proving the module never executed; nested fan-out phases revive bottom-up; a registered name imported from the project's own code does not fold; one declared in the file does not fold; an unregistered chant import (inferArchetype) still falls back, proving registration is per-helper and not per-module; output() folds against a real cross-file AttrRef into a genuine LexiconOutput (sourceLexicon proves the WeakRef deref happened) and falls back over a same-file reference. Constructor positions are covered end-to-end against a (type, props) class structurally identical to the real AWS Parameter.

lex00 added 3 commits July 26, 2026 19:58
#1082)

Call-shaped things may fold only via a declared, per-helper opt-in — the
decision #1044 settled for lexicon intrinsics. This is the same registry one
level up, for chant's own authoring API, written by hand rather than read
from a lexicon manifest.

The module carries the admission criteria and, next to each entry, why it
passes them — plus the audit of what was deliberately left out (env(), Op(),
the composite definition helpers, createResource/createProperty, and
call-form lexicon intrinsics, which remain #1044's registry to admit).

No behavior yet: nothing imports this.
…ositionally (#1082)

Two blockers that no amount of user-side rewriting could lift.

phase()/output() and peers are chant's own documented API, so every file
using components or outputs fell back to run on the first call. fold() now
reduces a call to a REGISTERED helper to a symbolic {__helper, args}
envelope — still executing nothing — exactly as a registered intrinsic
tagged template reduces to {__intrinsic}. fold-import revives it by
resolving the name through the folding file's own imports and checking the
import really is chant's (a published chant specifier, or a path inside
chant-core's own tree), then invoking the real function. So the function
that runs is the one the source named, and fold cannot diverge from run by
construction. A same-named helper of the project's own, or one declared in
the file, resolves to nothing chant owns and the file falls back.

foldResource assumed constructor argument 0 was the props object, so the AWS
deploy-time Parameter — (type, props) — could never fold anywhere. Arguments
are now folded positionally and the entity is constructed with exactly what
the source wrote; the classic (props)/(props, attributes) shape is reported
unchanged.

findSubsetViolation accepts both, keeping it at least as permissive as
fold() — otherwise EVL001 would flag source that folds cleanly. It checks
the helper NAME only; provenance needs the module graph, which is
fold-import's, and that asymmetry is documented alongside the three that
already existed.

An argument that is a same-file {__attrRef} envelope is rejected rather than
passed to a helper: output() derefs its ref's parent WeakRef to learn the
lexicon, and a look-alike plain object would produce wrong output rather
than none.
…ove (#1082)

Corpus fold coverage 21 -> 24 of 98, drift 0. The three new entries
(aws fargate-alb, multi-service-alb, vpc) are the corpus's
new Parameter("String", {...}) users, unblocked by positional constructor
arguments. Recorded name-by-name in EXPECTED_FOLD so a future regression on
them fails loudly, and the published count regenerated.

Documents the allowlist as a supported pattern, with the two conditions a
call must meet, and why env() is excluded.
@lex00

lex00 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Orchestrator verification, all measurements re-run independently:

  • fold-differential: 24/98, drift 0 (confirmed in 5 separate full-suite runs)
  • full suite: 5 consecutive clean runs, 34–35s each, 8656 passed — no hang class regression
  • loomster, measured on the migrated tree (loomster#156 merged) with this PR's build via npm-pack tarball: 13 fold / 44 run-fallback / 57 files — unchanged, confirming the agent's finding on the pre-migration tree. The chant-helper blockers this PR targeted are gone; the binding constraints are now Ref(...) in call form (7 files → feat(core): fold registered plain-call intrinsics (per-intrinsic opt-in) — DECIDED: yes #1044) and loomster's own sn()/buildData()/literalOutputValue() helpers (loomster-side), with 9 more files blocked only by taint behind those — so the two remaining levers cascade.

Criterion 5 (corpus) exceeded, criterion 6 (loomster) correctly not claimed — it needs #1044 plus a loomster-side sn() migration, both now filed. The pre-existing resolveCallExpression trust gap flagged in review is #1093.

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): chant's own authoring helpers (phase, output, Parameter) are unfoldable — caps real-app coverage

1 participant