Skip to content

feat(python,cli)!: v1.0 builders emit catalog IDs; stateless MacroExpander - #4567

Open
jacobsimionato wants to merge 1 commit into
macros/5-omit-builder-defaults-v2from
macros/5b-stateless-v10-macros-v2
Open

jacobsimionato wants to merge 1 commit into
macros/5-omit-builder-defaults-v2from
macros/5b-stateless-v10-macros-v2

Conversation

@jacobsimionato

@jacobsimionato jacobsimionato commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

In A2UI v1.0, one surface can mix components from several catalogs, so each component carries @catalogId and each catalog function call carries catalogId. A component can skip it only if the surface has a default catalog, which is the catalogId set on createSurface. The generated Python v1.0 builders (typed component classes produced by dart/a2ui_cli) never wrote these IDs.

To fill the gap, MacroExpander added them after the fact. Macros are Python functions that expand into A2UI components on the server, and MacroExpander runs that expansion. It kept state across calls: registered catalogs, a 256-entry cache of each surface's default catalog, and a pass that wrote catalog IDs onto the expanded components. That state made one expander unsafe to share between concurrent requests. Callers such as ToolPointerBinder (the experimental helper that binds tool outputs onto templates and macros) also had to track surface catalogs themselves.

This PR makes the builders write their own catalog IDs and removes the expander's state.

Replaces #4550 (GitHub marked it merged during a stack reorder; nothing landed on main).

Stack: 4/11 (overview) · previous: #4566 · next: #4528

Reviewer guide: A2UI Python macros: reviewer guide explains how the whole stack fits together.

Summary

  1. Generated v1.0 builders write their catalog URI by default, for components and non-system functions (see the example below). @-prefixed system functions get no catalog ID, and v0.9 output is unchanged.
  2. The typed function-call bases in a2ui.builder.v1_0 accept the catalog_id= class keyword, as ComponentBuilderNode already did.
  3. MacroExpander keeps no state between calls. It only knows surface defaults from createSurface messages in the same transform_to_transport() call.
  4. In v1.0, a createSurface whose catalogId is the macro catalog loses its catalogId, because the client has no renderer for macros. Macro matching reads the value before it is removed. The surface then has no default, so every expanded component names its own catalog. Breaking changes 4 and 5 follow from this.
  5. ToolPointerBinder drops its own surface tracking and calls transform_to_transport(messages) directly.

Breaking changes and migration

  1. Removed MacroExpander APIs: the base_catalogs constructor argument, register_catalog(), transaction(), the surface_catalogs property, and the surface_catalogs= argument of transform_to_transport(). Delete these calls and declare catalog_id= on custom builders instead (item 3). RendererState.surface_catalogs is unchanged.
  2. v1.0 JSON now includes catalog IDs. Update tests that compare exact v1.0 JSON, or pass catalog_id=None on an instance to leave the ID out.
  3. Custom v1.0 builders must name their catalog. The expander no longer fills it in, so add catalog_id= to your builder classes.
  4. The macro catalog ID must be unique. In v1.0, transform_catalogs() raises A2uiCatalogError if a transport catalog uses macro_catalog_id (default https://a2ui.org/catalogs/macros). Pass a different catalog_id= to MacroExpander.
  5. Later turns need an explicit macro catalog. If you pass catalog_id= to MacroExpander, a top-level macro without @catalogId expands only when its surface's createSurface is in the same transform_to_transport() call. In later turns, set "@catalogId" to macro_catalog_id on macro components, which always expand in v1.0, or resend the createSurface in the same batch.

New public API

from a2ui.builder.v1_0 import FormatString, StringFunctionCall, Text

class FlightCode(StringFunctionCall, catalog_id="https://example.com/travel.json"):
    """Serializes with this catalogId unless an instance passes catalog_id=None."""

# Text(id="t", text="Hi") now flattens to
# {"id": "t", "component": "Text", "text": "Hi",
#  "@catalogId": "https://a2ui.org/specification/v1_0/catalogs/basic/catalog.json"}
plain = Text(id="t", text="Hi", catalog_id=None)  # no @catalogId
fmt = FormatString(value="Hello", catalog_id=None)  # no catalogId

Where to review

Category Files Diff (+/-)
Production 5 +462 / -684
Generated (basic.py, 33 goldens) 34 +166 / -37
Tests (incl. macros.yaml) 9 +557 / -497
Docs and changelogs 7 +69 / -48
Total 55 +1254 / -1266

Start here:

  1. Do macros still match correctly with and without a createSurface in the batch? See _resolve_macro_invocation and _prepare_message in python/a2ui_agent/src/a2ui/transformers/macros/expander.py.
  2. Does catalog_id=None opt out without failing pydantic's check that rejects None fields? See _TypedFunctionCall in python/a2ui_agent/src/a2ui/builder/v1_0/models.py.
  3. Are catalog IDs emitted only for v1.0, and never on @ system functions? See dart/a2ui_cli/lib/src/emitters/python/python_emitter.dart.
  4. Did anything rely on the removed surface tracking? See python/a2ui_agent_experimental/src/a2ui/experimental/decision/binder/binder.py.
Implementation details
  • python_emitter.dart
    • Component and function class headers get catalog_id=CATALOG_ID only when the catalog is v1.0 and has a non-empty catalog ID.
    • New: function class names strip leading @ (a Python class name cannot start with @), while the wire name in call= keeps it.
  • models.py
    • _TypedFunctionCall stores the class keyword in _default_catalog_id from both __init_subclass__ and __pydantic_init_subclass__, then rewrites the inherited catalog_id field default and calls model_rebuild(force=True).
    • A before validator drops explicit catalog_id=None / catalogId=None when a default exists, so the opt-out does not trip _reject_explicit_nulls. Other explicit nulls are still rejected.
    • An after validator adds catalog_id to model_fields_set when the default applies, so exclude_unset=True dumps still include it.
    • The TYPE_CHECKING branch gets a matching _TypedFunctionCall stub so type checkers accept the class keyword.
  • expander.py
    • Removes _surface_catalogs, _MAX_TRACKED_SURFACES, _catalog_base_components, _stamp_lowered_primitives, and _resolve_surface_catalog_pair.
    • Per-call data moves into frozen dataclasses: _ExpansionContext (surface ID, in-batch default, protocol version) and _ResolvedInvocation. _PreparedMessageBody holds one message body during expansion.
    • _assemble_message re-validates a message only when one of its bodies changed.
  • Conformance: golden 35_v1_0_auto_stamping_from_base_catalogs_on_defaultless_surface.json is renamed to 35_v1_0_self_stamping_builders_on_defaultless_surface.json, and the other v1.0 builder and macro goldens are regenerated with stamped IDs.
Verification

Measured at the stack tip (all 11 commits applied); not measured on this commit alone:

  • Full Python suite: 6027 passed.
  • mypy: clean across 283 source files.
  • Conformance goldens: all 56 valid.
  • Dart a2ui_cli tests: 116 passed.
  • Prettier and pyink clean on every commit; every commit has a DCO sign-off.

This PR adds stamping and opt-out tests to dart/a2ui_cli/test/codegen_test.dart and python/a2ui_agent/tests/test_pydantic_builders_v1_0.py. It rewrites the stateless matching cases in python/a2ui_agent/tests/test_macros.py and removes the surface-tracking and transaction tests from python/a2ui_agent/tests/test_macro_lowering.py.

@jacobsimionato jacobsimionato changed the title feat(python,cli)!: self-stamp v1.0 builder catalogIds and make MacroExpander stateless feat(python,cli)!: self-stamp v1.0 builders & stateless MacroExpander Dec 3, 2026

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

Copy link
Copy Markdown
Contributor

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 a breaking change to make MacroExpander pure and stateless by enabling generated v1.0 catalog builders to self-stamp their catalog URIs by default. This eliminates the need for mutable per-session surface tracking, base_catalogs, and surface_catalogs parameters. The Dart CLI PythonEmitter has been updated to output components and functions that self-stamp catalog_id=CATALOG_ID. Feedback on the changes highlights a potential issue where CATALOG_ID might not be defined or imported in the generated Python files, which could lead to runtime NameErrors.

Comment on lines +429 to +431
final classHeader = (isV1 && catalog.catalogId.isNotEmpty)
? 'class ${comp.name}(ComponentBuilderNode, catalog_id=CATALOG_ID):'
: 'class ${comp.name}(ComponentBuilderNode):';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

The generated Python code references CATALOG_ID in the class definition, but it is not clear where this constant is defined or imported. If it is not defined in the generated code, this will result in a NameError at runtime. Please ensure CATALOG_ID is defined or imported in the generated Python files.

@jacobsimionato jacobsimionato changed the title feat(python,cli)!: self-stamp v1.0 builders & stateless MacroExpander feat(python,cli)!: v1.0 builders emit catalog IDs; stateless MacroExpander Dec 3, 2026
…xpander stateless

Signed-off-by: Jacob Simionato <jsimionato@google.com>
@jacobsimionato
jacobsimionato force-pushed the macros/5b-stateless-v10-macros-v2 branch from a76c7ba to e985d4b Compare December 3, 2026 12:24
github-actions Bot added a commit that referenced this pull request Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

📖 Docs Site Preview Deployed

Preview URL: https://a2ui.org/pr-preview/pr-4567/

Modified pages in this PR:

Built from commit e985d4b · Workflow run · GitHub Pages may take ~30–60s to propagate updates. This preview is removed automatically when the PR closes.

This branch has not been deployed

No deployments
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.

1 participant