# Copyright 2024 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Unit tests for `a2ui.experimental.decision.gated`.""" from __future__ import annotations from typing import Any import warnings import pytest from a2ui.core import A2uiCatalogError from a2ui.core.basic_catalog import BasicCatalog from a2ui.core.schema import ProtocolVersion from a2ui.experimental.decision import ( DecisionAnswer, DecisionBatch, DecisionDistribution, DecisionModelError, MockDecisionModel, QuestionKind, ) from a2ui.experimental.decision.gated import ( GATE_EXTENSION_KEY, VARIANT_EXTENSION_KEY, GatedTemplateEvaluator, GatedTemplateSpec, extract_gates_and_variants, prune_components, ) from a2ui.inference_formats import to_message_models from a2ui.utils import validate_payload def _basic() -> BasicCatalog: return BasicCatalog(ProtocolVersion.V1_0) class _ScriptedBackend(MockDecisionModel): """Test model returning explicit scripted answers and recording batches.""" def __init__(self, answers: dict[str, tuple[str, float] | float]) -> None: super().__init__() self._answers = answers self.batches: list[DecisionBatch] = [] async def __call__( self, batch: DecisionBatch, ) -> list[DecisionAnswer]: self.batches.append(batch) out: list[DecisionAnswer] = [] for q in batch.questions: spec = self._answers.get(q.id) if q.kind is QuestionKind.BOOLEAN: yes_p = float(spec) if isinstance(spec, (int, float)) else 0.5 dist = DecisionDistribution( probabilities={"yes": yes_p, "no": 1.0 - yes_p} ) else: if isinstance(spec, tuple): choice, prob = spec else: choice, prob = (q.options[0] if q.options else "none"), 0.9 probs = {opt: 0.0 for opt in q.options} if q.allow_none: probs["none"] = 0.0 probs[choice] = prob dist = DecisionDistribution(probabilities=probs) out.append(DecisionAnswer(question_id=q.id, distribution=dist)) return out def _incident_components() -> list[dict[str, Any]]: return [ {"id": "root", "component": "Card", "child": "main_col"}, { "id": "main_col", "component": "Column", "children": [ "header_text", "rollback_card", "metrics_compact", "metrics_detailed", ], }, { "id": "header_text", "component": "Text", "text": {"@path": "/service"}, "@metadata": { "extensions": { "vendor_tag": "keep-me", } }, }, { "id": "rollback_card", "component": "Card", "child": "rollback_text", "@metadata": { "extensions": { GATE_EXTENSION_KEY: { "condition": "Show rollback alert when service is degraded.", "threshold": 0.5, }, "vendor_keep": True, } }, }, { "id": "rollback_text", "component": "Text", "text": "Initiate rollback immediately.", }, { "id": "metrics_compact", "component": "Row", "children": ["cpu_compact"], "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "metrics_layout", "name": "compact", "description": "Compact single-row summary.", "prompt": "Choose metrics layout density.", } } }, }, {"id": "cpu_compact", "component": "Text", "text": {"@path": "/cpu"}}, { "id": "metrics_detailed", "component": "Column", "children": ["cpu_detail", "err_detail"], "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "metrics_layout", "name": "detailed", "description": "Detailed multi-row breakdown.", } } }, }, {"id": "cpu_detail", "component": "Text", "text": {"@path": "/cpu"}}, {"id": "err_detail", "component": "Text", "text": {"@path": "/errorRate"}}, ] def test_unpruned_gated_template_is_valid_v1_0_wire_json() -> None: basic = _basic() wire = [ { "version": "1.0", "createSurface": {"surfaceId": "s1", "catalogId": basic.catalog_id}, }, { "version": "1.0", "updateComponents": { "surfaceId": "s1", "components": _incident_components(), }, }, ] models = to_message_models(wire) validate_payload([basic], models) assert len(models) == 2 def test_from_messages_collects_create_surface_and_update_components() -> None: basic = _basic() wire = to_message_models([ { "version": "1.0", "createSurface": { "surfaceId": "s1", "catalogId": basic.catalog_id, }, }, { "version": "1.0", "updateComponents": { "surfaceId": "s1", "components": _incident_components(), }, }, ]) spec = GatedTemplateSpec.from_messages( "incident", "Service incident dashboard", wire, required_paths=("/service",), ) assert spec.catalog_id == basic.catalog_id assert len(spec.components) == len(_incident_components()) assert spec.required_paths == ("/service",) inline_spec = GatedTemplateSpec.from_messages( "inline_incident", "Inline components", [{ "version": "1.0", "createSurface": { "surfaceId": "s2", "catalogId": basic.catalog_id, "components": [{"id": "root", "component": "Text", "text": "Hello"}], }, }], ) assert len(inline_spec.components) == 1 assert inline_spec.catalog_id == basic.catalog_id @pytest.mark.asyncio async def test_evaluate_prunes_gate_and_non_winning_variant_and_strips_metadata() -> ( None ): basic = _basic() spec = GatedTemplateSpec( id="incident", description="Incident dashboard", components=tuple(_incident_components()), catalog_id=basic.catalog_id, ) backend = _ScriptedBackend({ "gate:rollback_card": 0.15, "variant:metrics_layout": ("compact", 0.91), }) evaluator = GatedTemplateEvaluator( backend, transport_catalogs=[basic], templates=[spec] ) result = await evaluator.evaluate( "incident", "Show healthy service status", { "service": "checkout-api", "cpu": "24%", "errorRate": "0.01%", "apiKey": "sk_live_" + ("9" * 64), }, surface_id="s_healthy", ) assert not result.abstained assert result.decision_calls == 1 assert "rollback_card" in result.pruned_component_ids assert "rollback_text" in result.pruned_component_ids assert "metrics_detailed" in result.pruned_component_ids assert "cpu_detail" in result.pruned_component_ids assert "err_detail" in result.pruned_component_ids assert "metrics_compact" in result.kept_component_ids assert "cpu_compact" in result.kept_component_ids dumped = [m.model_dump(by_alias=True, exclude_none=True) for m in result.messages] comps = {c["id"]: c for c in dumped[1]["updateComponents"]["components"]} assert comps["main_col"]["children"] == ["header_text", "metrics_compact"] assert comps["header_text"]["@metadata"]["extensions"] == {"vendor_tag": "keep-me"} assert "@metadata" not in comps["metrics_compact"] assert dumped[2]["updateDataModel"]["value"] == { "service": "checkout-api", "cpu": "24%", "errorRate": "0.01%", } @pytest.mark.asyncio async def test_evaluate_keeps_gate_and_preserves_sibling_vendor_extension() -> None: basic = _basic() spec = GatedTemplateSpec( id="incident", description="Incident dashboard", components=tuple(_incident_components()), catalog_id=basic.catalog_id, ) backend = _ScriptedBackend({ "gate:rollback_card": 0.92, "variant:metrics_layout": ("detailed", 0.88), }) evaluator = GatedTemplateEvaluator( backend, transport_catalogs=[basic], templates=[spec] ) result = await evaluator.evaluate( "incident", "Show degraded service incident", {"service": "checkout-api", "cpu": "94%", "errorRate": "8.2%"}, ) assert not result.abstained dumped = [m.model_dump(by_alias=True, exclude_none=True) for m in result.messages] comps = {c["id"]: c for c in dumped[1]["updateComponents"]["components"]} assert "rollback_card" in comps assert comps["rollback_card"]["@metadata"]["extensions"] == {"vendor_keep": True} assert comps["main_col"]["children"] == [ "header_text", "rollback_card", "metrics_detailed", ] def test_prune_components_cascades_upward_across_card_button_modal_tabs_and_childlist_template() -> ( None ): components = [ { "id": "root", "component": "Column", "children": [ "keep_text", "card_wrap", "btn_wrap", "modal_wrap", "tabs_partial", "tabs_empty", "list_kept", "list_pruned", ], }, {"id": "keep_text", "component": "Text", "text": "Header"}, {"id": "card_wrap", "component": "Card", "child": "card_inner"}, {"id": "card_inner", "component": "Text", "text": "Drop me"}, { "id": "btn_wrap", "component": "Button", "child": "btn_label", "action": {"event": {"name": "go"}}, }, {"id": "btn_label", "component": "Text", "text": "Drop btn"}, { "id": "modal_wrap", "component": "Modal", "trigger": "modal_trigger", "content": "modal_body", }, {"id": "modal_trigger", "component": "Text", "text": "Open"}, {"id": "modal_body", "component": "Text", "text": "Modal Body"}, { "id": "tabs_partial", "component": "Tabs", "tabs": [ {"title": "A", "child": "tab_a"}, {"title": "B", "child": "tab_b"}, ], }, {"id": "tab_a", "component": "Text", "text": "Tab A"}, {"id": "tab_b", "component": "Text", "text": "Tab B"}, { "id": "tabs_empty", "component": "Tabs", "tabs": [{"title": "Only", "child": "tab_only"}], }, {"id": "tab_only", "component": "Text", "text": "Gone"}, { "id": "list_kept", "component": "List", "children": {"componentId": "item_kept", "path": "/items"}, }, {"id": "item_kept", "component": "Text", "text": {"@path": "name"}}, { "id": "list_pruned", "component": "List", "children": {"componentId": "item_pruned", "path": "/other"}, }, {"id": "item_pruned", "component": "Text", "text": {"@path": "name"}}, ] surviving, pruned = prune_components( components, { "card_inner", "btn_label", "modal_trigger", "tab_b", "tab_only", "item_pruned", }, ) by_id = {c["id"]: c for c in surviving} assert set(by_id.keys()) == { "root", "keep_text", "tabs_partial", "tab_a", "list_kept", "item_kept", } assert by_id["root"]["children"] == ["keep_text", "tabs_partial", "list_kept"] assert by_id["tabs_partial"]["tabs"] == [{"title": "A", "child": "tab_a"}] assert "modal_wrap" in pruned assert "modal_body" in pruned assert "tabs_empty" in pruned assert "list_pruned" in pruned @pytest.mark.asyncio async def test_root_losing_required_child_abstains() -> None: basic = _basic() spec = GatedTemplateSpec( id="fragile", description="Fragile card", components=( {"id": "root", "component": "Card", "child": "only_child"}, { "id": "only_child", "component": "Text", "text": "Hello", "@metadata": { "extensions": { GATE_EXTENSION_KEY: {"condition": "Keep only child?"} } }, }, ), catalog_id=basic.catalog_id, ) backend = _ScriptedBackend({"gate:only_child": 0.1}) evaluator = GatedTemplateEvaluator( backend, transport_catalogs=[basic], templates=[spec] ) res = await evaluator.evaluate("fragile", "Test", {"a": 1}) assert res.abstained assert "root" in res.reason @pytest.mark.asyncio async def test_variant_min_confidence_and_allow_none_behavior() -> None: basic = _basic() def _make_spec(allow_none: bool) -> GatedTemplateSpec: return GatedTemplateSpec( id="vtest", description="Variant test", components=( {"id": "root", "component": "Column", "children": ["opt_a", "opt_b"]}, { "id": "opt_a", "component": "Text", "text": "A", "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "mode", "name": "a", "description": "Option A", "allowNone": allow_none, "minConfidence": 0.75, } } }, }, { "id": "opt_b", "component": "Text", "text": "B", "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "mode", "name": "b", "description": "Option B", "allowNone": allow_none, "minConfidence": 0.75, } } }, }, ), catalog_id=basic.catalog_id, ) low_conf_backend = _ScriptedBackend({"variant:mode": ("a", 0.55)}) ev_strict = GatedTemplateEvaluator( low_conf_backend, transport_catalogs=[basic], templates=[_make_spec(False)] ) strict_res = await ev_strict.evaluate("vtest", "Pick", {"x": 1}) assert strict_res.abstained assert "min_confidence" in strict_res.reason ev_opt = GatedTemplateEvaluator( low_conf_backend, transport_catalogs=[basic], templates=[_make_spec(True)] ) opt_res = await ev_opt.evaluate("vtest", "Pick", {"x": 1}) assert not opt_res.abstained assert "opt_a" in opt_res.pruned_component_ids assert "opt_b" in opt_res.pruned_component_ids @pytest.mark.asyncio async def test_select_and_evaluate_single_call_and_two_call_split() -> None: basic = _basic() spec = GatedTemplateSpec( id="incident", description="Incident dashboard", components=tuple(_incident_components()), catalog_id=basic.catalog_id, required_paths=("/service",), ) answers = { "select:template": ("incident", 0.94), "gate:incident:rollback_card": 0.85, "variant:incident:metrics_layout": ("compact", 0.90), } backend_one = _ScriptedBackend(answers) ev_one = GatedTemplateEvaluator( backend_one, transport_catalogs=[basic], templates=[spec], max_pairs=100 ) res_one = await ev_one.select_and_evaluate( "Show incident", {"service": "api", "cpu": "80%", "errorRate": "2%"} ) assert not res_one.abstained assert res_one.decision_calls == 1 assert res_one.template_id == "incident" backend_two = _ScriptedBackend(answers) ev_two = GatedTemplateEvaluator( backend_two, transport_catalogs=[basic], templates=[spec], max_pairs=2 ) res_two = await ev_two.select_and_evaluate( "Show incident", {"service": "api", "cpu": "80%", "errorRate": "2%"} ) assert not res_two.abstained assert res_two.decision_calls == 2 def test_extract_gates_and_variants_rejects_invalid_declarations() -> None: with pytest.raises(A2uiCatalogError, match="root"): extract_gates_and_variants([{"id": "a", "component": "Text", "text": "x"}]) with pytest.raises(A2uiCatalogError, match="Root component"): extract_gates_and_variants([{ "id": "root", "component": "Text", "text": "x", "@metadata": {"extensions": {GATE_EXTENSION_KEY: {"condition": "Yes?"}}}, }]) with pytest.raises(A2uiCatalogError, match="cannot declare both"): extract_gates_and_variants([ {"id": "root", "component": "Column", "children": ["a"]}, { "id": "a", "component": "Text", "text": "x", "@metadata": { "extensions": { GATE_EXTENSION_KEY: {"condition": "Show?"}, VARIANT_EXTENSION_KEY: { "group": "g", "name": "n", "description": "d", }, } }, }, ]) with pytest.raises(A2uiCatalogError, match="Duplicate variant name"): extract_gates_and_variants([ {"id": "root", "component": "Column", "children": ["a", "b"]}, { "id": "a", "component": "Text", "text": "x", "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "g", "name": "same", "description": "d1", } } }, }, { "id": "b", "component": "Text", "text": "y", "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "g", "name": "same", "description": "d2", } } }, }, ]) def test_gated_template_evaluator_emits_experimental_warning( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.delenv("A2UI_SUPPRESS_EXPERIMENTAL_FEATURE_WARNINGS", raising=False) basic = _basic() with warnings.catch_warnings(record=True) as caught: warnings.simplefilter("always") GatedTemplateEvaluator(MockDecisionModel(), transport_catalogs=[basic]) assert any( "[EXPERIMENTAL]" in str(w.message) and "GatedTemplateEvaluator" in str(w.message) for w in caught ) @pytest.mark.asyncio async def test_excluded_paths_are_redacted_from_decision_batch_state() -> None: basic = _basic() spec = GatedTemplateSpec( id="incident", description="Incident dashboard", components=tuple(_incident_components()), catalog_id=basic.catalog_id, ) backend = _ScriptedBackend({ "gate:rollback_card": 0.9, "variant:metrics_layout": ("compact", 0.9), }) evaluator = GatedTemplateEvaluator( backend, transport_catalogs=[basic], templates=[spec] ) await evaluator.evaluate( "incident", "Show status", { "service": "checkout-api", "cpu": "40%", "errorRate": "0.1%", "internal_secret_note": "TOP_SECRET_MARKER", }, exclude=("/internal_secret_note",), ) assert backend.batches assert "TOP_SECRET_MARKER" not in (backend.batches[0].state or "") def test_conflicting_variant_prompts_raise_catalog_error() -> None: with pytest.raises(A2uiCatalogError, match="conflicting 'prompt'"): extract_gates_and_variants([ {"id": "root", "component": "Column", "children": ["a", "b"]}, { "id": "a", "component": "Text", "text": "x", "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "g", "name": "a", "description": "Option A", "prompt": "Prompt one", } } }, }, { "id": "b", "component": "Text", "text": "y", "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "g", "name": "b", "description": "Option B", "prompt": "Prompt two", } } }, }, ]) @pytest.mark.asyncio async def test_redacts_nested_mutable_secret_array_column_tuple_and_proto() -> None: basic = _basic() spec = GatedTemplateSpec( id="incident", description="Incident dashboard", components=tuple(_incident_components()), catalog_id=basic.catalog_id, ) payload: dict[str, Any] = { "service": "checkout-api", "cpu": "40%", "errorRate": "0.1%", "user": {"name": "Alice", "apiKey": "NESTED_SECRET_VALUE"}, "items": [ {"label": "first", "secret": "COL_SECRET_1"}, {"label": "second", "secret": "COL_SECRET_2"}, ], "tags": ("prod", "us-east"), "__proto__": {"polluted": True}, } # Making /user mutable must NOT expose nested /user/apiKey. backend = _ScriptedBackend({ "gate:rollback_card": 0.9, "variant:metrics_layout": ("compact", 0.9), }) evaluator = GatedTemplateEvaluator( backend, transport_catalogs=[basic], templates=[spec] ) res = await evaluator.evaluate( "incident", "Show status", payload, exclude=("/items/secret",), mutable_paths=("/user",), ) assert not res.abstained state_text = backend.batches[0].state or "" assert "NESTED_SECRET_VALUE" not in state_text assert "COL_SECRET_1" not in state_text assert "COL_SECRET_2" not in state_text assert "__proto__" not in state_text data_model = res.messages[-1].model_dump(by_alias=True, exclude_none=True)[ "updateDataModel" ]["value"] assert data_model == { "service": "checkout-api", "cpu": "40%", "errorRate": "0.1%", "user": {"name": "Alice"}, "items": [{"label": "first"}, {"label": "second"}], "tags": ["prod", "us-east"], } # Explicit nested mutable path keeps /user/apiKey. res_explicit = await evaluator.evaluate( "incident", "Show status", payload, exclude=("/items/secret",), mutable_paths=("/user/apiKey",), ) explicit_data = res_explicit.messages[-1].model_dump( by_alias=True, exclude_none=True )["updateDataModel"]["value"] assert explicit_data["user"] == { "name": "Alice", "apiKey": "NESTED_SECRET_VALUE", } @pytest.mark.asyncio async def test_max_values_chars_truncates_state_and_validates_bounds() -> None: basic = _basic() spec = GatedTemplateSpec( id="incident", description="Incident dashboard", components=tuple(_incident_components()), catalog_id=basic.catalog_id, ) for bad_limit in (0, -1, True): with pytest.raises(ValueError, match="max_values_chars"): GatedTemplateEvaluator( MockDecisionModel(), transport_catalogs=[basic], templates=[spec], max_values_chars=bad_limit, # type: ignore[arg-type] ) backend = _ScriptedBackend({ "gate:rollback_card": 0.9, "variant:metrics_layout": ("compact", 0.9), }) evaluator = GatedTemplateEvaluator( backend, transport_catalogs=[basic], templates=[spec], max_values_chars=60, ) await evaluator.evaluate( "incident", "Show status", { "service": "checkout-api-with-very-long-service-name", "cpu": "40%", "errorRate": "0.1%", "details": "x" * 200, }, ) state_text = backend.batches[0].state or "" values_lines = [ line for line in state_text.splitlines() if line.startswith("Values:") ] assert len(values_lines) == 1 assert values_lines[0].endswith("... [truncated]") assert len(values_lines[0]) <= 60 @pytest.mark.asyncio async def test_nan_probabilities_raise_contract_error_in_gated_evaluator() -> None: basic = _basic() strict_spec = GatedTemplateSpec( id="strict", description="Strict variant template", components=( { "id": "root", "component": "Column", "children": ["gate_t", "opt_a", "opt_b"], }, { "id": "gate_t", "component": "Text", "text": "Alert", "@metadata": { "extensions": { GATE_EXTENSION_KEY: {"condition": "Show?", "threshold": 0.5} } }, }, { "id": "opt_a", "component": "Text", "text": "A", "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "mode", "name": "a", "description": "Option A", "allowNone": False, "minConfidence": 0.75, } } }, }, { "id": "opt_b", "component": "Text", "text": "B", "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "mode", "name": "b", "description": "Option B", "allowNone": False, "minConfidence": 0.75, } } }, }, ), catalog_id=basic.catalog_id, ) async def _nan_variant_model(_batch: DecisionBatch) -> list[DecisionAnswer]: return [ DecisionAnswer( "gate:gate_t", DecisionDistribution({"yes": 0.9, "no": 0.1}), ), DecisionAnswer( "variant:mode", DecisionDistribution({"a": float("nan"), "b": 0.1}), ), ] nan_variant_ev = GatedTemplateEvaluator( _nan_variant_model, transport_catalogs=[basic], templates=[strict_spec], ) with pytest.raises(DecisionModelError) as var_exc: await nan_variant_ev.evaluate("strict", "Show", {"x": 1}) assert var_exc.value.code == "DECISION_CONTRACT_ERROR" async def _nan_select_model(_batch: DecisionBatch) -> list[DecisionAnswer]: return [ DecisionAnswer( "select:template", DecisionDistribution({"strict": float("nan"), "none": 0.1}), ), ] nan_select_ev = GatedTemplateEvaluator( _nan_select_model, transport_catalogs=[basic], templates=[strict_spec], ) with pytest.raises(DecisionModelError) as sel_exc: await nan_select_ev.select_and_evaluate("Show", {"x": 1}) assert sel_exc.value.code == "DECISION_CONTRACT_ERROR" def test_unknown_template_catalog_id_fails_fast_at_construction() -> None: basic = _basic() bad_spec = GatedTemplateSpec( id="bad", description="Unknown catalog", components=tuple(_incident_components()), catalog_id="urn:test:missing-catalog", ) with pytest.raises(A2uiCatalogError, match="unknown catalog_id"): GatedTemplateEvaluator( MockDecisionModel(), transport_catalogs=[basic], templates=[bad_spec], ) @pytest.mark.asyncio async def test_unknown_variant_option_raises_and_custom_catalog_child_slot_prunes() -> ( None ): from a2ui.core import Catalog from a2ui.core.schema import ProtocolVersion from a2ui.experimental.decision import CatalogDecisionGraph, DecisionModelError basic = _basic() strict_spec = GatedTemplateSpec( id="strict_v", description="Strict variant", components=( {"id": "root", "component": "Column", "children": ["opt_a", "opt_b"]}, { "id": "opt_a", "component": "Text", "text": "A", "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "mode", "name": "a", "description": "Option A", "allowNone": False, } } }, }, { "id": "opt_b", "component": "Text", "text": "B", "@metadata": { "extensions": { VARIANT_EXTENSION_KEY: { "group": "mode", "name": "b", "description": "Option B", "allowNone": False, } } }, }, ), catalog_id=basic.catalog_id, ) async def _hallucinated_variant(_batch: Any) -> list[DecisionAnswer]: return [ DecisionAnswer("variant:mode", DecisionDistribution({"hallucinated": 0.99})) ] ev = GatedTemplateEvaluator( _hallucinated_variant, transport_catalogs=[basic], templates=[strict_spec], ) with pytest.raises(DecisionModelError, match="unknown candidate") as exc_info: await ev.evaluate("strict_v", "Show", {"x": 1}) assert exc_info.value.code == "DECISION_INVALID_OPTION" # H7: schema-driven custom child slot traversal and optional child pruning. custom_cat = Catalog.from_json( { "catalogId": "urn:test:custom-panel", "components": { "Panel": { "type": "object", "required": ["id", "component", "body"], "properties": { "id": {"type": "string"}, "component": {"const": "Panel"}, "body": {"$ref": "#/$defs/ComponentId"}, "footer": {"$ref": "#/$defs/ComponentId"}, }, }, "Leaf": { "type": "object", "required": ["id", "component"], "properties": { "id": {"type": "string"}, "component": {"const": "Leaf"}, }, }, }, }, protocol_version=ProtocolVersion.V1_0, ) graph = CatalogDecisionGraph([custom_cat]) comps = [ {"id": "root", "component": "Panel", "body": "b1", "footer": "f1"}, {"id": "b1", "component": "Leaf"}, {"id": "f1", "component": "Leaf"}, ] surviving, pruned = prune_components( comps, {"f1"}, graph=graph, surface_catalog_id=custom_cat.catalog_id ) assert [c["id"] for c in surviving] == ["root", "b1"] assert "footer" not in surviving[0] assert pruned == ("f1",)