"""``CLEAN_DELETE_V1`` โ€” remove a node and its incident relations (ยง10.3). The node and every relation/constraint incident to it are deleted from the world graph, the remaining constraints are re-validated for feasibility, and the whole world is re-rendered fresh later (P6). The original raster is never inpainted โ€” deletion is structural, not pixel-level. After deletion the executor may report ``MISSING_INFORMATION`` / ``INVALID_REFERENT`` (a ``U`` state) or a changed answer; the operator does not decide which (P7 does, by execution). An infeasible result (a dangling reference or a now-broken constraint graph) is rejected via :class:`InterventionError` โ€” never emitted as a broken world. """ from __future__ import annotations from typing import Any from ..dsl.ast import Dsl from ..sources.base import World from ..sources.world_ops import ( clone_world, find_point, find_row, find_series, is_geometry, is_plot, is_table, node_kind, world_sha256, ) from .base import ( InterventionError, OperatorName, TransformedWorld, transformed_hash, ) from .feasibility import FeasibilityError, is_feasible _NAME: OperatorName = "CLEAN_DELETE_V1" class CleanDeleteOperator: """Delete ``target_node`` (and incident relations) from the world graph.""" name = _NAME def apply( self, world: World, target_node: str, *, seed: int, dsl: Dsl, **_kwargs: Any, ) -> TransformedWorld: if node_kind(world, target_node) is None: raise InterventionError(f"CLEAN_DELETE_V1: target {target_node!r} is not a world node") new = clone_world(world) _delete(new, target_node) try: is_feasible(new, dsl=dsl) except FeasibilityError as exc: raise InterventionError( f"CLEAN_DELETE_V1: infeasible after deleting {target_node!r}: {exc}" ) from exc return TransformedWorld( world=new, operator=_NAME, target_node=target_node, seed=seed, transformed_world_sha256=transformed_hash( operator=_NAME, target_node=target_node, seed=seed, source_world_sha256=world_sha256(world), transformed_world_sha256=world_sha256(new), hidden_nodes=frozenset(), substitute_value=None, ), hidden_nodes=frozenset(), substitute_value=None, description=f"clean-delete {target_node}", ) def _delete(world: World, node_id: str) -> None: """Remove ``node_id`` and its incident relations, in place.""" if is_plot(world): point = find_point(world, node_id) if point is not None: sidx, _pidx, _p = point pts = world["series"][sidx].get("points", []) or [] world["series"][sidx]["points"] = [q for q in pts if q.get("id") != node_id] return series = find_series(world, node_id) if series is not None: world["series"] = [s for s in world.get("series", []) if s.get("id") != node_id] return if is_table(world): if find_row(world, node_id) is not None: world["rows"] = [r for r in world.get("rows", []) if r.get("id") != node_id] return if is_geometry(world): # Delete the entity plus every constraint that mentions it (incident # relations). A constraint whose only stated entity is the deleted one # is gone entirely; the feasibility check then guards dangling refs. world["entities"] = [e for e in world.get("entities", []) if e.get("id") != node_id] world["constraints"] = [ c for c in world.get("constraints", []) if node_id not in {str(a) for a in (c.get("args") or []) if isinstance(a, str)} ] return __all__ = ["CleanDeleteOperator"]