Source code for embodichain.gen_sim.task_engine.semantic_plan_validation

# ----------------------------------------------------------------------------
# Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
#
# 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
#
#     http://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.
# ----------------------------------------------------------------------------

"""Semantic-only plan checking, separate from grounded bundle validation."""

from __future__ import annotations
from typing import Any
import json
from .contracts import canonical_hash, validate_task_candidate
from .semantic_graph import validate_semantic_task_graph
from embodichain.lab.task_program.language.semantic_contracts import (
    semantic_planning_catalog,
    validate_semantic_call_payload,
)

__all__ = ["validate_semantic_plan", "semantic_plan_rules"]

from .ontology import TASK_CONTRACTS
from .interpretation import _TERMINAL_PARK_CALL, _TERMINAL_PARK_KINDS

# Task Engine side enums are distinct from logical resource IDs. This mapping
# is deliberately closed: custom resource/control-part names are not rewritten.
_INTENT_RESOURCE_IDS = {"left_arm": "left", "right_arm": "right"}


class _PlanDiagnostics(ValueError):
    def __init__(self, errors: list[dict[str, Any]]) -> None:
        self.diagnostics = {
            "schema_version": "semantic_plan_diagnostics/v1",
            "code": "symbolic_state_invalid",
            "errors": errors,
            "note": "States after an invalid node are projected only to discover independent errors; any diagnostic rejects the complete plan.",
        }
        super().__init__(json.dumps(self.diagnostics, ensure_ascii=False))


[docs] def semantic_plan_rules() -> dict[str, Any]: """Derive producer obligations from task affordances and call declarations. Returns: Required catalog capabilities and candidate calls for each task contract. """ catalog = semantic_planning_catalog()["calls"] held_transport = sorted( cid for cid, call in catalog.items() if "transportable" in call["capabilities"] ) return { key: { "semantics": contract.semantics, "requires_capabilities": sorted( contract.required_affordances - {"graspable"} ), "requires_one_of": sorted( cid for cid, call in catalog.items() if set(call["capabilities"]) & (contract.required_affordances - {"graspable"}) ), # A held single-arm result ends in held transport, not release. **( {"terminal_hold_requires_one_of": held_transport} if contract.resource_mode == "single_arm" and contract.moves_primary_object and "terminal_behavior" in contract.applicable_intent_fields else {} ), } for key, contract in TASK_CONTRACTS.items() }
def _validate_clause_coverage( document: dict[str, Any], requirements: list[dict[str, Any]], graph: dict[str, Any], annotations: dict[str, Any], bindings: list[dict[str, Any]], target_groups: dict[str, list[str]], grounding: dict[str, Any], ) -> list[dict[str, Any]]: """Pair frozen qualifiers with their target and an actual implementing call.""" roles = {r["id"]: r for r in document["configuration"]["roles"]} entities = { role: { e["id"] for ref in document["interpretation"]["references"] if ref["role_id"] == role for e in ref["entities"] } for role in roles } predicates = {p["id"]: [p] for p in document["objectives"]["goal"]} predicates.update( {m["id"]: m["achieve"] for m in document["objectives"]["milestones"]} ) entity_by_id = {item["id"]: item for item in grounding["objects"]} declared_park = any( c["origin"] == "explicit" and (c.get("constraint") or {}).get("kind") in _TERMINAL_PARK_KINDS for c in document["interpretation"]["clauses"] ) park_ids = { n["id"] for n in graph["nodes"] if n["call"].get("call_id") == _TERMINAL_PARK_CALL } park_ancestors = {} task_nodes = set() if declared_park: nodes = validate_semantic_task_graph(graph)["nodes"] park_ancestors = _ancestor_sets(nodes) task_nodes = {n["id"] for n in nodes} - park_ids witnessed_park = set() def matches_role(uid: str | None, subjects: set[str]) -> bool: seen = set() while uid is not None and uid not in seen: if uid in subjects: return True seen.add(uid) uid = entity_by_id.get(uid, {}).get("parent") return False report = [] for clause in document["interpretation"]["clauses"]: if clause["origin"] != "explicit": continue quoted = [r for r in requirements if clause["quote"] in r["quote"]] if not quoted: raise ValueError(f"Graph omits instruction qualifier {clause['id']!r}") target = clause["target"] constraint = clause.get("constraint", {}) kind = constraint.get("kind") node_ids = {node_id for r in quoted for node_id in r["node_ids"]} if kind in _TERMINAL_PARK_KINDS: arm = _TERMINAL_PARK_KINDS[kind] matches = [ n["id"] for n in graph["nodes"] if n["id"] in node_ids & park_ids and n["task_instance_id"] in target_groups.get(target, ()) and n["call"].get("resources", {}).get("primary") == arm and annotations[n["id"]] in ( dict(object=None, reference=None, relation=None, intent={}), dict(object=kind, reference=None, relation=None, intent={}), ) and task_nodes <= park_ancestors[n["id"]] ] if len(matches) != 1 or matches[0] in witnessed_park: raise ValueError( f"Terminal park for {arm!r} must quote {clause['quote']!r} " "and have one matching park node after all task actions" ) witnessed_park.update(matches) report.append( dict( clause_id=clause["id"], target=target, status="covered", node_ids=matches, reason="terminal_park_posture_pending_binding", ) ) continue target_roles = ( {target} if target in roles else { p.get("subject", p.get("arguments", {}).get("subject")) for p in predicates.get(target, []) } & roles.keys() ) subjects = {entity for role in target_roles for entity in entities[role]} candidates = [ n for n in graph["nodes"] if n["id"] in node_ids and ( target not in target_groups or n["task_instance_id"] in target_groups[target] ) and ( matches_role(annotations[n["id"]]["object"], subjects) or matches_role(annotations[n["id"]]["reference"], subjects) ) ] if not candidates: raise ValueError( f"Instruction qualifier {clause['id']!r} references the wrong target" ) covered = False if kind == "role_property": covered = ( target in roles and constraint["key"] in roles[target]["properties"] and roles[target]["properties"][constraint["key"]] == constraint["value"] ) if not covered: raise ValueError( f"Instruction qualifier {clause['id']!r} has no frozen role property" ) elif kind in {"return_to_origin", "restore_upright"}: witnessed = set() for node in candidates: ann = annotations[node["id"]] call = node["call"] if kind == "return_to_origin": implements = ( validate_semantic_call_payload(call)["effect"] == "release" and ann["intent"].get("return_to_origin") is True and ann["reference"] in {None, ann["object"]} and any( b["node_id"] == node["id"] and b["kind"] == "source_pose" and b["subject"] == ann["object"] for b in bindings ) ) if implements: later = graph["nodes"][graph["nodes"].index(node) + 1 :] implements = not any( ( target not in target_groups or following["task_instance_id"] in target_groups[target] ) and matches_role( annotations[following["id"]]["object"], {ann["object"]} ) and validate_semantic_call_payload(following["call"])[ "effect" ] != "preserve" for following in later ) else: # The catalog pour primitive restores its source orientation. implements = call.get("call_id") == "simulation.pour" if implements: witnessed.add(ann["object"]) covered = bool(subjects) and subjects <= witnessed if not covered: raise ValueError( f"Instruction qualifier {clause['id']!r} is not implemented: {kind}" ) report.append( { "clause_id": clause["id"], "target": target, "status": "covered" if covered else "unverified", "node_ids": [n["id"] for n in candidates], "reason": ( "structured_declaration" if covered else "unsupported_or_unstructured_qualifier" ), } ) # Fresh parks are resource-only; pre-audit archives used grounded object annotations. if witnessed_park != park_ids and ( declared_park or any(annotations[nid]["object"] is None for nid in park_ids) ): raise ValueError( "Terminal park nodes must match the audited arm requirements exactly" ) return report def _producer_ids(step: dict[str, Any]) -> set[str]: """Return the calls that can produce one source step's semantic goal.""" rules = semantic_plan_rules()[step["task_type"]] if step.get("terminal_behavior") == "hold" and ( "terminal_hold_requires_one_of" in rules ): return set(rules["terminal_hold_requires_one_of"]) return set(rules["requires_one_of"])
[docs] def validate_semantic_plan( graph: dict[str, Any], candidate: dict[str, Any], grounding: dict[str, Any], annotations: dict[str, Any], requirements: list[dict[str, Any]], bindings: list[dict[str, Any]], ) -> dict[str, Any]: """Check calls, symbolic state and source coverage without physical evidence. Args: graph: Canonical graph carrying unbound logical identities. candidate: Source task candidate with validated task intent. grounding: Visible, instruction-declared and unresolved entity evidence. annotations: Per-node semantic subject, reference, relation and intent. requirements: Quoted source clauses with implementing node IDs. bindings: Explicit runtime binding requests; not execution evidence. Returns: Recomputable phase-specific coverage and pending-binding summary. Raises: ValueError: Invalid call, topology, state, identity or missing semantics. State diagnostics may propose resource-slot edits for a repair request; these suggestions never change the input or establish validity without a complete subsequent validation. """ candidate = validate_task_candidate(candidate) return _validate_interpreted_plan( graph, candidate["draft"], candidate["scene_request"]["references"], grounding, annotations, requirements, bindings, )
def _validate_interpreted_plan( graph, draft, references, grounding, annotations, requirements, bindings ): """Check an executable witness against resolved source intent.""" graph = validate_semantic_task_graph(graph) if ( graph["task_id"] != draft["task_id"] or graph["instruction"] != draft["instruction"] ): raise ValueError("Plan source identity mismatch") for entity in grounding["objects"]: if entity.get("provenance") not in { "observed", "instruction_declared", "runtime_required", }: raise ValueError("Invalid evidence provenance") box = entity.get("bbox") if entity["provenance"] == "observed": import math if ( type(box) is not list or len(box) != 4 or any( type(v) not in {int, float} or not math.isfinite(v) or not 0 <= v <= 1 for v in box ) or box[0] >= box[2] or box[1] >= box[3] ): raise ValueError("Invalid normalized image evidence") elif box is not None: raise ValueError("Unobserved entities cannot have boxes") steps = {s["id"]: s for s in draft["steps"]} nodes = {n["id"]: n for n in graph["nodes"]} entities = {o["id"] for o in grounding["objects"]} entity_by_id = {o["id"]: o for o in grounding["objects"]} def subject_matches(actual: str, expected: str, step: dict[str, Any]) -> bool: seen = set() while actual is not None: if actual == expected: return True if actual in seen: raise ValueError("Cyclic part hierarchy") seen.add(actual) if TASK_CONTRACTS[step["task_type"]].source_structure != "articulation": return False actual = entity_by_id[actual].get("parent") if actual is not None and actual not in entities: raise ValueError("Unknown part parent") return False if set(annotations) != set(nodes): raise ValueError("Every node requires exactly one semantic annotation") for annotation in annotations.values(): if ( type(annotation) is not dict or set(annotation) != {"object", "reference", "relation", "intent"} or type(annotation["intent"]) is not dict ): raise ValueError("Invalid semantic annotation: intent must be a dictionary") if set(g["id"] for g in graph["task_groups"]) != set(steps): raise ValueError("Graph must retain every source step") _validate_reference_identity(grounding["bindings"], references) expected_refs = { f"{step['id']}.{role}" for step in steps.values() for role in ("object", "target") if step[role]["kind"] != "none" } if set(grounding["bindings"]) != expected_refs: raise ValueError("Logical bindings must cover every source selector exactly") for step in steps.values(): for role in ("object", "target"): selector = step[role] if selector["kind"] == "step_result" and grounding["bindings"][ f"{step['id']}.{role}" ] != grounding["bindings"].get(f"{selector['step_id']}.object"): raise ValueError("Cross-step result identity was lost") for ref, ids in grounding["bindings"].items(): if not ids or set(ids) - entities: raise ValueError(f"Unresolved logical identity for {ref}") bound_kinds = {} for binding in bindings: if ( set(binding) != {"id", "node_id", "kind", "parameter", "subject", "value", "source"} or binding["node_id"] not in nodes ): raise ValueError("Malformed runtime binding requirement") node = nodes[binding["node_id"]] ann = annotations[binding["node_id"]] park_arm = ( node["call"].get("resources", {}).get("primary") if node["call"].get("call_id") == _TERMINAL_PARK_CALL and ann["object"] is None else None ) if park_arm is None and binding["subject"] not in entities: # Name the identity error so a repair can correct the subject. raise ValueError( f"Node {binding['node_id']} semantic.object " f"{binding['subject']!r} is not a grounded entity ID; use one " f"of {sorted(entities)}. Task role IDs are not entity IDs." ) allowed = set(validate_semantic_call_payload(node["call"])["runtime_bindings"]) if ann["intent"].get("return_to_origin") is True: allowed.add("source_pose") expected_parameter = ( "runtime.initial_pose" if binding["kind"] == "source_pose" else "runtime." + binding["kind"] ) if ( binding["subject"] != (park_arm if park_arm is not None else ann["object"]) or binding["kind"] not in allowed or binding["value"] != ann or binding["source"] != node["task_instance_id"] or binding["parameter"] != expected_parameter ): raise ValueError( "Runtime binding does not match its source node and intent" ) key = (binding["node_id"], binding["kind"]) if key in bound_kinds: raise ValueError("Duplicate binding requirement") bound_kinds[key] = binding held_after: dict[str, dict[str, str]] = {} held = {} acquisition_nodes: dict[str, str] = {} last_object_node = {} effects = {} state_errors: list[dict[str, Any]] = [] dependency_ancestors = _ancestor_sets(graph["nodes"]) def ancestors(nid: str) -> set[str]: return dependency_ancestors[nid] # A one-shot repair can only fix what one diagnostic names, so report every # placement relation mismatch together instead of the first one alone. relation_mismatches = [] for node in graph["nodes"]: call = node["call"] if call["kind"] != "place": continue destination = "inside" if "inside" in call else "on" if "on" in call else None relation = annotations.get(node["id"], {}).get("relation") if destination is not None and relation != destination: relation_mismatches.append((node["id"], destination, relation)) if len(relation_mismatches) > 1: raise ValueError( "Placement nodes use place.on/inside but declare a different " "semantic.relation: " + "; ".join( f"{node_id} uses place.{destination} with relation={relation!r}" for node_id, destination, relation in relation_mismatches ) + ". Set EVERY listed placement node relation to its place " "destination kind; a relative relation such as right_of requires " "simulation.place_relative instead of builtin place." ) for node in graph["nodes"]: cid = node["call"].get("call_id", node["call"]["kind"]) contract = validate_semantic_call_payload(node["call"]) ann = annotations[node["id"]] resource_only_park = cid == _TERMINAL_PARK_CALL and ann == dict( object=None, reference=None, relation=None, intent={} ) if set(ann) != {"object", "reference", "relation", "intent"} or ( ann["object"] not in entities and not resource_only_park ): raise ValueError(f"Invalid annotation for {node['id']}") if ann["reference"] is not None and ann["reference"] not in entities: raise ValueError( f"Unknown reference entity {ann['reference']!r} on {node['id']}. An original pose/support alias is NOT an observed entity: for return_to_origin set semantic.reference to the source object ID {ann['object']!r} (or null); keep the pose/affordance alias only in call arguments and the source_pose binding." ) if any( key in ann["intent"] for key in { "position", "pose", "qpos", "quaternion", "angle", "twist_angle", "joint_position", "waypoint", "trajectory", "grasp_pose", "target_pose", } ): raise ValueError( "Metric execution data is forbidden in semantic intent; declare a runtime binding" ) if type(ann["intent"]) is not dict: raise ValueError("Node intent must be declarative") if ( ann["reference"] == ann["object"] and ann["relation"] in {"inside", "on"} and not ( ann["relation"] == "on" and ann["intent"].get("return_to_origin") is True ) ): raise ValueError( "An object cannot be its own placement destination; original-pose binding must be explicit" ) args = node["call"].get("arguments", node["call"]) obj = args.get("object", args.get("handle", ann["object"])) part_argument = next( (name for name, kind in contract["required"].items() if kind == "part"), None, ) if obj != ann["object"] and not ( part_argument is not None and subject_matches(obj, ann["object"], steps[node["task_instance_id"]]) ): raise ValueError( f"Node {node['id']} call subject {obj!r} differs from annotation {ann['object']!r}; control parts require a declared parent relationship" ) if args.get("reference") is not None and args["reference"] != ann["reference"]: raise ValueError("Call reference differs from annotation") if cid == "place": destination = ( "inside" if "inside" in args else "on" if "on" in args else None ) target_id = args.get(destination) if destination else None if ( isinstance(target_id, str) and target_id in entities and ann["reference"] is not None and ann["intent"].get("return_to_origin") is not True and target_id != ann["reference"] and entity_by_id[target_id].get("parent") != ann["reference"] ): raise ValueError( f"Node {node['id']} placement destination {target_id!r} differs from its declared reference {ann['reference']!r}" ) if destination is not None and ann["relation"] != destination: raise ValueError( f"Node {node['id']} uses place.{destination} but semantic.relation={ann['relation']!r}. Set THIS placement node relation to {destination!r}. Pour relation above belongs only on pouring/transport nodes; restoration place.on requires relation on." ) if "state" in args and args["state"] != ann["intent"].get("target_state"): raise ValueError("Control state differs from requested task state") if args.get("relation") is not None and args["relation"] != ann["relation"]: raise ValueError("Call relation differs from annotation") if obj is not None: if obj in last_object_node and last_object_node[obj] not in ancestors( node["id"] ): raise ValueError( "Operations on the same object must have an explicit dependency" ) last_object_node[obj] = node["id"] resources = node["call"].get("resources", {}) actor = resources.get("primary", "auto") effect = contract["effect"] def state_error( code: str, hint: str, repair_operations: list[dict[str, Any]] | None = None, ) -> None: diagnostic = { "code": code, "node_id": node["id"], "call_id": cid, "subject": obj, "primary_resource": actor, "held_before": dict(held), "hint": hint, } if repair_operations: diagnostic["repair_operations"] = repair_operations state_errors.append(diagnostic) if effect in {"acquire", "acquire_align"}: if obj in held or actor in held.values(): state_error( "occupied_acquisition", f"Remove a redundant acquisition of {obj!r} or release the occupied resource first. Pour restores orientation; never add axis_align to a held pouring source.", ) held[obj] = actor acquisition_nodes[obj] = node["id"] elif effect in {"release", "held"}: if obj not in held: state_error( "missing_held_object", f"Insert a canonical pick of object {obj!r} BEFORE THIS node {node['id']!r}, or remove this redundant transport. A place releases the object and a later relocation requires reacquisition. Do not acquire the reference object.", ) elif held[obj] != actor: state_error( "holder_mismatch", f"Use the same acquisition resource {held[obj]!r} for THIS node {node['id']!r}; omit resources consistently when it is auto. Do not change the object's identity.", [ { "operation": "remove" if held[obj] == "auto" else "set", "node_id": node["id"], "path": "/call/resources/primary", **({"value": held[obj]} if held[obj] != "auto" else {}), } ], ) if effect == "release": held.pop(obj, None) acquisition_nodes.pop(obj, None) elif effect == "transfer": source, dest = resources["source"], resources["destination"] if held.get(obj) != source or dest in held.values(): state_error( "handover_state", f"Handover requires {obj!r} held by {source!r} and receiver {dest!r} free. Acquire using the source arm before THIS node.", ( [ { "operation": "set", "node_id": acquisition_nodes[obj], "path": "/call/resources/primary", "value": source, } ] if held.get(obj) == "auto" and obj in acquisition_nodes else None ), ) held[obj] = dest acquisition_nodes.pop(obj, None) elif effect == "coordinated_release": if held: state_error( "coordinated_arms_busy", "Release existing held objects before this two-arm operation.", ) elif effect == "control" and actor in held.values(): state_error( "control_arm_busy", "Release the selected arm's held object before this control operation.", ) for kind in contract["runtime_bindings"]: if (node["id"], kind) not in bound_kinds: raise ValueError(f"Missing runtime binding: {node['id']}.{kind}") effects[node["id"]] = cid held_after[node["id"]] = dict(held) if state_errors: raise _PlanDiagnostics(state_errors) referenced_nodes = set() for requirement in requirements: if ( set(requirement) != {"id", "quote", "node_ids"} or not isinstance(requirement["quote"], str) or requirement["quote"] not in graph["instruction"] or not requirement["node_ids"] or set(requirement["node_ids"]) - set(nodes) ): raise ValueError( "Instruction requirements must quote the source and reference real nodes" ) referenced_nodes.update(requirement["node_ids"]) if referenced_nodes != set(nodes): raise ValueError("Every call must be justified by the original instruction") for step_id, step in steps.items(): group = next(g for g in graph["task_groups"] if g["id"] == step_id) calls = [nodes[n] for n in group["node_ids"]] if not set(effects[n["id"]] for n in calls) & _producer_ids(step): raise ValueError( f"Missing semantic producer for {step_id}:{step['task_type']}" ) kinds = {effects[n["id"]] for n in calls} if step.get("terminal_behavior") == "place" and not kinds & { "place", "simulation.place_relative", "simulation.coordinated_transport", }: raise ValueError(f"Missing terminal placement/release for {step_id}") if ( "target_setting" in TASK_CONTRACTS[step["task_type"]].applicable_intent_fields and step.get("twist_goal_mode") != "coarse" and not any( annotations[n["id"]]["intent"].get("target_setting") == step["target_setting"] for n in calls ) ): raise ValueError("Requested printed knob setting was lost") producer_ids = _producer_ids(step) held_transport = step.get("terminal_behavior") == "hold" and ( "terminal_hold_requires_one_of" in semantic_plan_rules()[step["task_type"]] ) # Binding derives each held target and its validators from that # subject's producing transport alone. held_goal = step.get("orientation_goal", "none") if held_transport else "none" for subject in grounding["bindings"].get(f"{step_id}.object", []): producers = [ n for n in calls if effects[n["id"]] in producer_ids and subject_matches(annotations[n["id"]]["object"], subject, step) ] if len(producers) != 1: raise ValueError( f"{step_id} requires exactly one semantic producer per declared subject, got {len(producers)} for {subject}" ) producer = producers[0] if "lift_height_m" in step: lift_annotation = annotations[producer["id"]] height = lift_annotation["intent"].get("lift_height_m") if ( lift_annotation["reference"] is not None or lift_annotation["relation"] not in (None, "none") or lift_annotation["intent"].get("direction") != "up" or type(height) not in {int, float} or height != step["lift_height_m"] ): raise ValueError( f"{step_id} requires a targetless upward held transport " f"with semantic.intent.lift_height_m={step['lift_height_m']}." ) if ( held_goal != "none" and annotations[producer["id"]]["intent"].get("orientation_goal") != held_goal ): raise ValueError( f"{step_id} ends holding {subject} with orientation_goal=" f"{held_goal}: declare it in semantic.intent of its held " f"transport {producer['id']}." ) expected = grounding["bindings"].get(f"{step_id}.object") if expected and not all( any( subject_matches(annotations[n["id"]]["object"], obj, step) for n in calls ) for obj in expected ): actual = [annotations[n["id"]]["object"] for n in calls] raise ValueError( f"Step {step_id} expects objects {expected}, got {actual}. A control part must declare parent: instruction_object_id in grounding, or use that pending logical object identity." ) target = grounding["bindings"].get(f"{step_id}.target") if step.get("terminal_behavior") == "hold": if held_transport and any( effects[n["id"]] == "simulation.axis_align" for n in calls ): raise ValueError( f"{step_id} ends holding its object: acquire it with pick; " "the held transport realizes any upright orientation, so " "remove simulation.axis_align." ) if TASK_CONTRACTS[step["task_type"]].resource_mode == "coordinated": raise ValueError( "Coordinated hold requires a matching hold call; the current transport call releases" ) if any(obj not in held_after[calls[-1]["id"]] for obj in expected or []): raise ValueError("Requested terminal hold is not retained") if target: placement_goal = step.get("terminal_behavior") != "hold" and ( "placeable" in TASK_CONTRACTS[step["task_type"]].required_affordances or step.get("terminal_behavior") == "place" ) for obj in expected or []: applicable = [ n for n in calls if subject_matches(annotations[n["id"]]["object"], obj, step) and ( validate_semantic_call_payload(n["call"])["effect"] in {"release", "coordinated_release"} if placement_goal else effects[n["id"]] in producer_ids ) ] if not any( annotations[n["id"]]["reference"] in target and ( step["relation"] == "none" or annotations[n["id"]]["relation"] == step["relation"] ) for n in applicable ): raise ValueError( f"Requested target/relation missing on the semantic producer for {step_id}.{obj}" ) for key in ( "relation", "orientation_goal", "target_state", "layout", "axis", "direction", "terminal_behavior", "pour_goal_mode", "twist_goal_mode", ): val = step.get(key, "none") if val not in ("none", None, 0) and not any( ( annotations[n["id"]].get("relation") if key == "relation" else annotations[n["id"]]["intent"].get(key) ) == val for n in calls ): raise ValueError(f"Missing explicit intent {step_id}.{key}={val}") for field, slot in ( ("required_arm", "primary"), ("transfer_arm", "source"), ("receive_arm", "destination"), ): arm = step.get(field, "none") expected_resource = _INTENT_RESOURCE_IDS.get(arm, arm) if arm not in {"none", "auto"} and not any( effects[n["id"]] != _TERMINAL_PARK_CALL and n["call"].get("resources", {}).get(slot) == expected_resource for n in calls ): raise ValueError( f"Missing required arm {step_id}.{field}: intent {arm!r} " f"requires logical resource {expected_resource!r} in " f"call.resources.{slot}. Keep this logical resource consistent " "across the acquisition, transfer and subsequent held calls. " "Intent side labels are not robot control-part names." ) for parent in step["depends_on"]: parent_group = next(g for g in graph["task_groups"] if g["id"] == parent) if not all( any(p in ancestors(n["id"]) for p in parent_group["node_ids"]) for n in calls ): raise ValueError("Source step ordering was lost") allowed_held = set() for step in steps.values(): if step.get("terminal_behavior") == "hold": allowed_held.update(grounding["bindings"].get(f"{step['id']}.object", [])) if set(held) - allowed_held: raise ValueError("Task ends holding objects that must be released") referenced = {uid for values in grounding["bindings"].values() for uid in values} referenced.update( a[key] for a in annotations.values() for key in ("object", "reference") if a[key] is not None ) for node in graph["nodes"]: declaration = validate_semantic_call_payload(node["call"]) args = node["call"].get("arguments", node["call"]) for key, rule in {**declaration["required"], **declaration["optional"]}.items(): value = args.get(key) if ( isinstance(rule, str) and rule in {"entity", "part", "affordance", "target"} and isinstance(value, str) and value in entities ): referenced.add(value) observed = sum( o["provenance"] == "observed" for o in grounding["objects"] if o["id"] in referenced ) total = len(referenced) # Every runtime binding is pending, even when the relevant object is visible. return { "status": "covered", "semantic_status": "valid", "visual_status": ("complete" if observed == total else "pending"), "pending_bindings": len(bindings), "visual_observed_entities": observed, "visual_total_entities": total, "execution_ready": False, "runtime_evidence_status": "not_run", "goals": [], "issues": [], "lineage": {}, } def _ancestor_sets(nodes: list[dict[str, Any]]) -> dict[str, set[str]]: """Compute closure once in validated topological order, with local ownership.""" result: dict[str, set[str]] = {} for node in nodes: ancestors = set(node["depends_on"]) for parent in node["depends_on"]: ancestors.update(result[parent]) result[node["id"]] = ancestors return result def _validate_reference_identity( bindings: dict[str, list[str]], references: list[dict[str, Any]] ) -> None: """A repeated exact scene selector denotes the same logical entity set.""" selected: dict[tuple[str, str, int], set[str]] = {} for reference in references: key = ( reference["reference"].strip().casefold(), reference["quantifier"], reference["count"], ) ids = set(bindings[reference["reference_id"]]) if key in selected and selected[key] != ids: raise ValueError( f"Repeated scene reference {reference['reference']!r} must retain identities {sorted(selected[key])}; do not split the same object when its role changes" ) selected[key] = ids