From 8825077c9733f21d2c36459ce714f8218555ea29 Mon Sep 17 00:00:00 2001 From: James Emberton <60827102+j-emberton@users.noreply.github.com> Date: Wed, 2 Dec 2026 08:58:06 +0000 Subject: [PATCH] Consolidate settlement results and clarify child boundary lookup --- .../_settle_assignments.py | 59 +++++++++---------- .../structural/scc/test_settle_assignments.py | 4 +- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/bilt/core/_structural_analysis/_causalization_block_solver/_settle_assignments.py b/src/bilt/core/_structural_analysis/_causalization_block_solver/_settle_assignments.py index 9c6f3fa0..c50d6205 100644 --- a/src/bilt/core/_structural_analysis/_causalization_block_solver/_settle_assignments.py +++ b/src/bilt/core/_structural_analysis/_causalization_block_solver/_settle_assignments.py @@ -69,7 +69,7 @@ def settle_assignments( available, backend, preferred_boundaries=preferred_boundaries, - ).settle(target) + ).settle((target,)) def settle_assignments_many( @@ -84,25 +84,17 @@ def settle_assignments_many( Keep established intermediate values and child routes within one settlement rather than starting a fresh dependency search for each target. """ - settlement = _TargetSettlement(assignments, available, backend) - for target in targets: - settlement.settle(target) - return ( - tuple(settlement.steps[name] for name in sorted(settlement.steps)), - frozenset((settlement.requested & assignments.keys()) - settlement.steps.keys()), - ) + return _TargetSettlement(assignments, available, backend).settle(targets) -def _assignment_step( - operation: BiltModelConstituent, +def _lookup_child_step( + operation: SubSystemConstituent, output: str, - available: collections.abc.Set[str], + boundary: collections.abc.Set[str], backend: Backend, ) -> PlanStep | None: - if not isinstance(operation, SubSystemConstituent): - return PlanStep(operation=operation, output=output) - - candidate_ports = (set(operation.__bilt_parameters__) & available) - {output} + """Find a child step within the offered boundary, excluding the target.""" + candidate_ports = (set(operation.__bilt_parameters__) & boundary) - {output} child_inputs = operation.required_inputs_for(output, backend, within=candidate_ports) if child_inputs is None: return None @@ -133,7 +125,16 @@ def __init__( self.stalled_at: dict[str, int] = {} self.cycle_visits = 0 - def settle(self, output: str) -> tuple[tuple[PlanStep, ...], frozenset[str]]: + def settle(self, targets: tuple[str, ...]) -> tuple[tuple[PlanStep, ...], frozenset[str]]: + """Settle targets in order and report the accumulated steps and rejected outputs.""" + for target in targets: + self._settle_target(target) + return ( + tuple(self.steps[name] for name in sorted(self.steps)), + frozenset((self.requested & self.assignments.keys()) - self.steps.keys()), + ) + + def _settle_target(self, output: str) -> None: """Revisit stalled dependencies whenever another value was established.""" while output not in self.established: before = len(self.established) @@ -147,10 +148,6 @@ def settle(self, output: str) -> tuple[tuple[PlanStep, ...], frozenset[str]]: pending.append(self._resolve(dependency)) if len(self.established) == before: break - return ( - tuple(self.steps[name] for name in sorted(self.steps)), - frozenset((self.requested & self.assignments.keys()) - self.steps.keys()), - ) def _resolve(self, attribute: str) -> collections.abc.Iterator[str]: if attribute in self.active: @@ -189,35 +186,37 @@ def _resolve(self, attribute: str) -> collections.abc.Iterator[str]: def _child_step( self, operation: SubSystemConstituent, output: str ) -> collections.abc.Generator[str, None, PlanStep | None]: - # These directions come from the expanded model. The relevant values - # can still be pending on a parent-local coupled block. + """Try preferred, established and finally speculative child boundaries.""" + # Preferred directions can depend on a pending parent-local coupled block. preferred = None if self.preferred_boundaries is None else self.preferred_boundaries.get(output) if preferred is not None and preferred <= self.candidates: yield from sorted(preferred) - step = _assignment_step(operation, output, preferred | self.fixed, self.backend) + step = _lookup_child_step(operation, output, preferred | self.fixed, self.backend) if step is not None: return step ports = sorted((set(operation.__bilt_parameters__) & self.candidates) - {output}) # Establish parent-produced boundary inputs together before asking the # child to search with an unnecessarily incomplete set of supplied ports. yield from self._settle_parent_inputs(ports) - step = self._child_route(operation, output) + step = self._established_child_step(operation, output) if step is not None: return step + # Establish additional child-produced ports, retrying only on progress. for port in ports: if not isinstance(self.assignments.get(port), SubSystemConstituent): continue before = len(self.established) yield port if len(self.established) != before: - step = _assignment_step(operation, output, self.established, self.backend) + step = _lookup_child_step(operation, output, self.established, self.backend) if step is not None: return step + # The initial probe must defer speculative boundaries to the full search. if self.defer_child_boundary and not self.established.issuperset(ports): raise DeferredChildBoundaryError - # Preserve cyclic/unresolved candidates for explicit checks, without + # Retain cyclic/unresolved candidates for explicit checks without # claiming their dependencies have been established. - return _assignment_step(operation, output, self.candidates, self.backend) + return _lookup_child_step(operation, output, self.candidates, self.backend) def _settle_parent_inputs(self, ports: list[str]) -> collections.abc.Iterator[str]: for port in ports: @@ -227,14 +226,14 @@ def _settle_parent_inputs(self, ports: list[str]) -> collections.abc.Iterator[st if self.defer_child_boundary and port not in self.established: raise DeferredChildBoundaryError - def _child_route(self, operation: SubSystemConstituent, output: str) -> PlanStep | None: + def _established_child_step(self, operation: SubSystemConstituent, output: str) -> PlanStep | None: """Avoid constraining a child with its own already-computed sibling outputs. Prefer the ordinary route. Only if the child's requested route is structurally overconstrained do we retry without outputs produced by this same child in the parent. Explicitly supplied ports stay fixed. """ - step = _assignment_step(operation, output, self.established, self.backend) + step = _lookup_child_step(operation, output, self.established, self.backend) if step is None or step.child_inputs is None: return step @@ -254,7 +253,7 @@ def _child_route(self, operation: SubSystemConstituent, output: str) -> PlanStep operation.child_port(output), ) except BiltOverconstrainedError: - reduced = _assignment_step(operation, output, self.established - sibling_outputs, self.backend) + reduced = _lookup_child_step(operation, output, self.established - sibling_outputs, self.backend) if reduced is not None and reduced.child_inputs is not None: try: validate_request_structure( diff --git a/tests/core/structural/scc/test_settle_assignments.py b/tests/core/structural/scc/test_settle_assignments.py index 6465163f..e256139d 100644 --- a/tests/core/structural/scc/test_settle_assignments.py +++ b/tests/core/structural/scc/test_settle_assignments.py @@ -467,12 +467,12 @@ def expose(answer: float, child: Child) -> bool: PYTHON_BACKEND, preferred_boundaries={"child.c": frozenset({"child.a"})}, ) - steps, rejected = trial.settle("answer") + steps, rejected = trial.settle(("answer",)) assert not rejected assert {step.output for step in steps} == {"answer", "child.a", "child.c"} assert not trial.stalled_at cycle_visits = trial.cycle_visits - trial.settle("child.c") + trial.settle(("child.c",)) assert trial.cycle_visits > cycle_visits