Repository navigation
feat(python,cli)!: v1.0 builders emit catalog IDs; stateless MacroExpander - #4567
jacobsimionato wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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.
| final classHeader = (isV1 && catalog.catalogId.isNotEmpty) | ||
| ? 'class ${comp.name}(ComponentBuilderNode, catalog_id=CATALOG_ID):' | ||
| : 'class ${comp.name}(ComponentBuilderNode):'; |
There was a problem hiding this comment.
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.
…xpander stateless Signed-off-by: Jacob Simionato <jsimionato@google.com>
a76c7ba to
e985d4b
Compare
📖 Docs Site Preview DeployedPreview URL: https://a2ui.org/pr-preview/pr-4567/ Modified pages in this PR: Built from commit |
Problem
In A2UI v1.0, one surface can mix components from several catalogs, so each component carries
@catalogIdand each catalog function call carriescatalogId. A component can skip it only if the surface has a default catalog, which is thecatalogIdset oncreateSurface. The generated Python v1.0 builders (typed component classes produced bydart/a2ui_cli) never wrote these IDs.To fill the gap,
MacroExpanderadded them after the fact. Macros are Python functions that expand into A2UI components on the server, andMacroExpanderruns 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 asToolPointerBinder(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
@-prefixed system functions get no catalog ID, and v0.9 output is unchanged.a2ui.builder.v1_0accept thecatalog_id=class keyword, asComponentBuilderNodealready did.MacroExpanderkeeps no state between calls. It only knows surface defaults fromcreateSurfacemessages in the sametransform_to_transport()call.createSurfacewhosecatalogIdis the macro catalog loses itscatalogId, 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.ToolPointerBinderdrops its own surface tracking and callstransform_to_transport(messages)directly.Breaking changes and migration
MacroExpanderAPIs: thebase_catalogsconstructor argument,register_catalog(),transaction(), thesurface_catalogsproperty, and thesurface_catalogs=argument oftransform_to_transport(). Delete these calls and declarecatalog_id=on custom builders instead (item 3).RendererState.surface_catalogsis unchanged.catalog_id=Noneon an instance to leave the ID out.catalog_id=to your builder classes.transform_catalogs()raisesA2uiCatalogErrorif a transport catalog usesmacro_catalog_id(defaulthttps://a2ui.org/catalogs/macros). Pass a differentcatalog_id=toMacroExpander.catalog_id=toMacroExpander, a top-level macro without@catalogIdexpands only when its surface'screateSurfaceis in the sametransform_to_transport()call. In later turns, set"@catalogId"tomacro_catalog_idon macro components, which always expand in v1.0, or resend thecreateSurfacein the same batch.New public API
Where to review
basic.py, 33 goldens)macros.yaml)Start here:
createSurfacein the batch? See_resolve_macro_invocationand_prepare_messageinpython/a2ui_agent/src/a2ui/transformers/macros/expander.py.catalog_id=Noneopt out without failing pydantic's check that rejectsNonefields? See_TypedFunctionCallinpython/a2ui_agent/src/a2ui/builder/v1_0/models.py.@system functions? Seedart/a2ui_cli/lib/src/emitters/python/python_emitter.dart.python/a2ui_agent_experimental/src/a2ui/experimental/decision/binder/binder.py.Implementation details
python_emitter.dartcatalog_id=CATALOG_IDonly when the catalog is v1.0 and has a non-empty catalog ID.@(a Python class name cannot start with@), while the wire name incall=keeps it.models.py_TypedFunctionCallstores the class keyword in_default_catalog_idfrom both__init_subclass__and__pydantic_init_subclass__, then rewrites the inheritedcatalog_idfield default and callsmodel_rebuild(force=True).beforevalidator drops explicitcatalog_id=None/catalogId=Nonewhen a default exists, so the opt-out does not trip_reject_explicit_nulls. Other explicit nulls are still rejected.aftervalidator addscatalog_idtomodel_fields_setwhen the default applies, soexclude_unset=Truedumps still include it.TYPE_CHECKINGbranch gets a matching_TypedFunctionCallstub so type checkers accept the class keyword.expander.py_surface_catalogs,_MAX_TRACKED_SURFACES,_catalog_base_components,_stamp_lowered_primitives, and_resolve_surface_catalog_pair._ExpansionContext(surface ID, in-batch default, protocol version) and_ResolvedInvocation._PreparedMessageBodyholds one message body during expansion._assemble_messagere-validates a message only when one of its bodies changed.35_v1_0_auto_stamping_from_base_catalogs_on_defaultless_surface.jsonis renamed to35_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:
a2ui_clitests: 116 passed.This PR adds stamping and opt-out tests to
dart/a2ui_cli/test/codegen_test.dartandpython/a2ui_agent/tests/test_pydantic_builders_v1_0.py. It rewrites the stateless matching cases inpython/a2ui_agent/tests/test_macros.pyand removes the surface-tracking and transaction tests frompython/a2ui_agent/tests/test_macro_lowering.py.