Skip to content

feat(craft): add the engine core with the text format - #23

Merged
innkuika merged 2 commits into
mainfrom
craft/engine-text-format
Dec 1, 2026
Merged

innkuika merged 2 commits into
mainfrom
craft/engine-text-format

Conversation

@innkuika

@innkuika innkuika commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

Starts craft/packages/a2ui_craft, the framework-independent core of the Craft engine, with its first feature: the text format. Stacked on #22.

Every engine PR in this series has the same three parts: a feature blueprint (in craft/blueprints/, A2UI's blueprint format), conformance cases for that blueprint (in craft/conformance/, language-agnostic, so any engine can run them), and the code.

What's in it

  • Blueprints: craft/blueprints/modules/craft_engine.blueprint.md (the engine's principles and layers) and craft/blueprints/features/craft_text_format.blueprint.md. craft/blueprints/validate.py runs A2UI's blueprint validator on them.

  • Code: lib/src/rfw/model.dart and text.dart are Yegor's fork of RFW's library model and text parser, from yjbanov/a2ui_craft at aece53f. Compared with RFW 2.0.0 (feat(craft): add Remote Flutter Widgets 2.0.0, unmodified #22) they add three kinds of reference:

    This PR also rejects keywords such as default as symbol names. To see exactly what changed relative to RFW, ignoring formatting, run craft/tool/diff_rfw.sh (about 130 changed lines). The files keep RFW's BSD license (see the package's LICENSE); scripts/fix_licenses.py skips them.

  • lib/src/canonical_json.dart: the canonical JSON form of a library, which the conformance cases use.

  • Tests: RFW 2.0.0's own text and model tests (38) pass, adapted only to run outside Flutter (imports; testWidgets → test). Also 28 conformance cases in craft/conformance/craft/craft_text_format.yaml, one or more per requirement.

  • CI: a new Presubmit Craft workflow validates the blueprints and runs dart analyze --fatal-infos on Craft packages. The tests run in the existing presubmit.

Aligned with the A2UI main repo (Craft will graduate into it): the blueprints use A2UI's format and validator, and the conformance suite follows A2UI's case format. Every case has an action and features tags; feature IDs (F-CRAFT-…) are registered in craft/conformance/features.yaml; suites are validated against craft/conformance/conformance_schema.json; and A2UI's own check_feature_coverage.py and check_executed_cases.py run in CI. craft/README.md maps every file to its place in the main repo. This PR adds that shared setup; each later PR adds only its own actions, feature IDs and cases.

An independent review checked the blueprint against the tests and the code. It mutated expectations and the parser to confirm the tests catch regressions, re-derived expected outputs by hand, and checked licenses and claims.

Next: names and libraries (qualified names, imports, export).

Pre-launch Checklist

One time:

For this PR:

  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • License checks pass (python3 ./scripts/fix_licenses.py --check).
  • Format checks pass (./scripts/fix_format.sh --check).

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the core Dart implementation of the Craft template engine (a2ui_craft), which is a framework-independent fork of Remote Flutter Widgets (RFW) 2.0.0. It adds specification blueprints, a canonical JSON serializer for conformance testing, and shared test cases. The feedback highlights a potential runtime type error in canonical_json.dart when pattern matching maps, and notes that the newly introduced reference types (ThemeReference, MediaReference, and SymbolReference) lack unit tests in model_test.dart to verify their behavior.

Comment on lines +53 to +56
Map<String, Object?>() => <String, Object?>{
for (final MapEntry<String, Object?> entry in node.entries)
entry.key: canonicalValue(entry.value),
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Matching Map<String, Object?>() using pattern matching will fail at runtime if the map's key type is not exactly String (for example, Map<dynamic, dynamic> or Map<Object, Object?>), even if all keys in the map are actually strings. This is because Map key types are not covariant with String in Dart.\n\nUsing Map() instead of Map<String, Object?>() is more robust and prevents unexpected runtime crashes when processing maps with different runtime type arguments.

    Map() => <String, Object?>{\n      for (final MapEntry<Object?, Object?> entry in node.entries)\n        entry.key as String: canonicalValue(entry.value),\n    },

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed: Map<String, dynamic> matches Map<String, Object?> at runtime, and the parser and decoder only produce Map<String, Object?>. A map with non-string keys isn't a library value and is meant to hit the ArgumentError at the end; an as String cast would hide that.

Comment on lines +189 to +190
});
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to the repository style guide (Best practices #4: 'If there are code changes, code should have tests'), the newly introduced reference types (ThemeReference, MediaReference, and SymbolReference) should have unit tests in model_test.dart to verify their behavior (such as toString(), equality, and constructReference).

  });\n\n  test('ThemeReference, MediaReference, SymbolReference', () async {\n    const themeRef = ThemeReference(<Object>['color', 'primary']);\n    expect('$themeRef', 'theme.color.primary');\n    expect(themeRef.constructReference(<Object>['accent']).parts, const <Object>['color', 'primary', 'accent']);\n\n    const mediaRef = MediaReference(<Object>['width']);\n    expect('$mediaRef', 'media.width');\n    expect(mediaRef.constructReference(<Object>['height']).parts, const <Object>['width', 'height']);\n\n    const symbolRef = SymbolReference('Location', <Object>['args', 'latLong']);\n    expect('$symbolRef', 'Location.args.latLong');\n    expect(symbolRef.constructReference(<Object>['alt']).parts, const <Object>['args', 'latLong', 'alt']);\n    expect(symbolRef == const SymbolReference('Location', <Object>['args', 'latLong']), isTrue);\n  });\n}
References
  1. Best practices Bump joi from 17.13.7 to 17.13.9 in /typescript-starter #4: If there are code changes, code should have tests. (link)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point for toString and SymbolReference equality; added a test in the latest push. Parsing and the canonical form of these references are covered by the conformance cases. constructReference on them isn't used by the engine, so I didn't add tests for it.

Base automatically changed from craft/rfw to main November 23, 2026 19:11
@innkuika
innkuika force-pushed the craft/engine-text-format branch 2 times, most recently from 2152ff8 to 5ac782f Compare November 23, 2026 22:05
Starts craft/packages/a2ui_craft, the framework-independent core of the
Craft engine, with its first feature: the text format.

- lib/src/rfw/model.dart and text.dart are Yegor's fork of RFW's library
  model and text parser (yjbanov/a2ui_craft at aece53f), formatted with
  the repo's formatter. Compared with RFW 2.0.0 they add three references:
  theme.<parts>, media.<parts> and symbol references (Name.<parts>); this
  PR also rejects keywords such as `default` as symbol names.
  craft/tool/diff_rfw.sh shows the exact diff, ignoring formatting. They
  keep RFW's BSD license (the package's LICENSE file says which files);
  the license script skips them.
- RFW 2.0.0's own text and model tests pass, adapted only to run outside
  Flutter (imports, and testWidgets replaced by test).
- craft/blueprints/ holds the engine's module blueprint and the
  craft_text_format feature blueprint, in A2UI's format, checked by
  craft/blueprints/validate.py (A2UI's validator).
- craft/conformance/craft/craft_text_format.yaml holds 28 shared cases,
  stated in the canonical JSON form of a library, which
  test/conformance/ runs.
- The shared cases follow A2UI's conformance conventions, in the layout
  they will have in the A2UI repository (craft/conformance/README.md):
  every case names an `action` (parse_library and parse_data so far) and
  the `features` it exercises. craft/conformance/conformance_schema.json
  defines the fields of each action, and
  craft/conformance/tests/test_conformance_yaml.py validates every suite
  against it. craft/conformance/features.yaml lists the feature IDs, one
  per blueprint requirement (F-CRAFT-TEXT-01 to 05), each with the anchor
  of its requirement, validated by features.schema.json (A2UI's, with
  Craft's ID pattern and harnesses).
- The Dart harness (test/conformance/conformance_harness.dart) runs each
  case with the handler of its action, fails a case with an unknown action
  or key, and writes A2UI's executed-case manifest; suite_registry.dart
  names the test file of each suite.
- Feature names carry a craft_ prefix and feature IDs an F-CRAFT-
  prefix, and paths in blueprints and suites are relative to craft/, so
  that the move to the A2UI repository is mechanical; craft/README.md
  maps each path and says how the codebase blueprints' commits get
  pinned.
- A Presubmit Craft workflow validates the blueprints and the suites,
  analyzes the Craft packages, checks the feature registry with A2UI's
  check_feature_coverage.py, and checks with A2UI's check_executed_cases.py
  that the Dart harness ran every case.

Co-authored-by: Yegor <yegor.jbanov@gmail.com>
@innkuika
innkuika force-pushed the craft/engine-text-format branch from 5ac782f to 5dc010b Compare November 23, 2026 23:19
@innkuika
innkuika marked this pull request as ready for review November 24, 2026 23:10
@innkuika
innkuika requested a review from ditman November 24, 2026 23:10

@ditman ditman left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Really solid foundation — the pure-Dart split, minimal delta against unmodified RFW 2.0.0 (diff_rfw.sh), web/Wasm compilation check in CI, and direct reuse of A2UI's blueprint/conformance validators make this very easy to review and graduate into a2ui-project/a2ui later.

Approving this so we can keep the stack moving — please address the concerns below (and the inline comments) in a follow-up PR down the line:

  1. Remove wildcard exports from the main barrel (lib/a2ui_craft.dart): Please replace the bare export 'src/...'; directives with explicit show lists so we can track public API changes clearly across PRs, and keep conformance-only helpers (canonicalLibrary/canonicalValue) and internal RFW runtime types (Bound*Reference, Any*, etc.) off the main package barrel.
  2. SymbolReference equality vs. ThemeReference/MediaReference + unused constructReference (model.dart): SymbolReference overrides ==/hashCode while the other reference nodes don't, which leaks into switch duplicate-case parsing (rejecting duplicate SymbolReference keys while accepting duplicate ThemeReference/MediaReference keys) without being specified in the blueprint. Included a reproducer test inline. Also constructReference on all three new reference classes is currently unused and untested.
  3. null.x consistency in map vs. list values (text.dart): Box(a: [null.x]) reports null cannot be used as a symbol name, whereas Box(a: null.x) takes the nullOk branch first and fails later expecting ) (and shifts to the next line if .x wraps). Included a reproducer test inline.
  4. Minor harness/canonical-JSON/doc nits: copying parts in _reference, adding direct unit tests for canonicalValue(missing) / ArgumentError (and noting the dart2js 1.0 is int caveat), tightening parse_cases.dart error position assertions, plus a couple of small schema/README nits.

(Also noted that CR/LF handling is already queued up in #38 on top of the stack, so I left line endings out of this review.)

Comment thread craft/packages/a2ui_craft/README.md Resolved
- Theme, media and symbol references all compare by identity, like RFW's
  references: SymbolReference no longer overrides ==/hashCode, so a
  switch keyed twice by the same reference parses for every kind. The
  blueprint states the rule; the unused constructReference methods of
  the three classes are gone.
- `null.x` as an argument or map value is rejected with "null cannot be
  used as a symbol name", as in a list; data files parse as in RFW.
- canonicalValue copies reference parts and notes that under dart2js a
  whole-number double gets the integer form; canonical_json_test covers
  null, missing and the ArgumentError fallback.
- The parse harness checks that every rejection reports a line >= 1 and
  a column >= 0 and that `expect` has exactly one key.
- New conformance cases: duplicate reference switch keys, references
  rejected in data files, media(...) in a value position, theme and
  media without a part, and null.x as an argument (also wrapped).
- README: diff_rfw.sh diffs lib/src/rfw/ only.
@innkuika
innkuika added this pull request to stack #51 December 1, 2026 19:57
@innkuika
innkuika merged commit f2e52d2 into main Dec 1, 2026
22 checks passed
@innkuika
innkuika deleted the craft/engine-text-format branch December 1, 2026 20:35
@innkuika

innkuika commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the gh stack tip, David! The rest of the series is now a stack: https://github.com/a2ui-project/labs/stack/51 (#24–#33, then the dogfooding fixes #38 and #39). After this merged, GitHub rebased the stack onto main automatically.

@ditman

ditman commented Dec 1, 2026

Copy link
Copy Markdown

Thanks for creating the stack! I'll ask the agent to publish a bunch of review comments at once (looking at them myself to make sure they make sense :P)

innkuika added a commit that referenced this pull request Dec 3, 2026
# Description

Adds the **libraries and names** feature to the Craft engine core: how a
name in a template denotes exactly one widget or function. Stacked on
#23. Same three parts as every engine PR: a blueprint, conformance cases
for it, and the code.

- **Blueprint**:
`craft/blueprints/features/craft_libraries_and_names.blueprint.md`.
- **Text format** (`lib/src/rfw/text.dart`, `model.dart`):
- qualified call names `lib::Name` and `a.b::Name`; the qualified string
is the call's name, so the binary format doesn't change;
- `export widget X = ...;`, which marks a declaration as exported. The
canonical JSON form carries `"exported": true`.

No source that RFW accepts changes meaning; RFW's own tests still pass.
- **Name resolution** (`lib/src/name_resolution.dart`):
- a library's own declarations come first, but a declaration never
resolves to itself (`widget Button = Button(...)` wraps core's
`Button`);
  - then `core`, when imported;
- names from any other import must be qualified, so adding a name to a
library can never change or break an existing template;
- imports are not transitive, and other libraries' private (not
exported) declarations are invisible;
- only exported declarations of a catalog library are agent-facing
components;
  - function names follow the same rules.
- **Tests**: 45 conformance cases in
`craft/conformance/craft/craft_libraries_and_names.yaml` (parse cases
and resolution queries; failures are pinned by reason, not message).
Every rejection case has a control case that differs only in the name.
Unit tests are in `test/name_resolution_test.dart`.

An independent review checked the blueprint against the tests and the
code, including code mutations.

Next: the binary format (versioning, extension tags, the exported flag).

## Pre-launch Checklist

One time:

- [x] I signed the [CLA].
- [x] I read the [Contributors Guide].

For this PR:

- [x] I updated/added relevant documentation.
- [x] My code changes (if any) have tests.
- [x] License checks pass (`python3 ./scripts/fix_licenses.py --check`).
- [x] Format checks pass (`./scripts/fix_format.sh --check`).

<!-- Links -->

[CLA]: https://cla.developers.google.com/
[Contributors Guide]:
https://github.com/a2ui-project/a2ui-labs/blob/main/CONTRIBUTING.md
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.

2 participants