"""Public commissioning controls and a small, explicitly scoped Bayesian model. Curve erasure retains the compiled base plan. A runner must instantiate a fresh controller at the actual Blackout boundary. Its first notebook() then requests the ordinary notebook replacement with only the fourth circuit fields removed. All subsequent meter and outcome history remains available to this controller. This is neither full notebook erasure nor a memoryless control. The cost DP assumes independent uniform thresholds in 0..3, and independent uniform future bands in 0..7 and loads in 0..2. It optimizes purchase versus majority trial within a single circuit's remaining tests. A fault costs four extra charge with the engine's legal direct reconnection. Rests are deferred until needed to preserve Ada's availability. The DP excludes those coupled campaign fatigue costs, physical signaling, other rooms, and the terminal objective. """ from __future__ import annotations from dataclasses import dataclass from fractions import Fraction from functools import cache import json from borderland import Action from .causal_reference import CausalReference @dataclass(frozen=True) class ThresholdBelief: intervals: tuple[tuple[int, int], ...] = ((0, 3),) * 8 def __post_init__(self): if not self.intervals or any(not 0 <= low <= high <= 3 for low, high in self.intervals): raise ValueError('Threshold intervals must be nonempty subsets of 0..3.') def flip_probability(self, band, load): low, high = self.intervals[band] return Fraction(max(0, min(high, load) - low + 1), high - low + 1) def observe(self, band, load, flipped): low, high = self.intervals[band] interval = (low, min(high, load)) if flipped else (max(low, load + 1), high) intervals = list(self.intervals) intervals[band] = interval return ThresholdBelief(tuple(intervals)) @dataclass(frozen=True) class AuditDecision: flip_probability: Fraction guess_cost: Fraction audit_cost: Fraction @property def audit(self): return self.audit_cost < self.guess_cost @property def expected_cost(self): return min(self.guess_cost, self.audit_cost) @cache def _future_cost(intervals, tests, fault_cost, audit_cost): if tests == 0: return Fraction(0) belief = ThresholdBelief(intervals) return sum((audit_decision(belief, tests, band, load, fault_cost, audit_cost).expected_cost for band in range(len(intervals)) for load in range(3)), Fraction(0)) / (3 * len(intervals)) @cache def audit_decision(belief, tests, band, load, fault_cost=4, audit_cost=6): """Exact extra charge conditional on the current public meter, including it. The feedback reveals the correct binary bus regardless of the attempted bus. Thus the majority bus minimizes fault cost without sacrificing information. Sorting intervals only for future draws exploits exchangeable band priors. """ if type(tests) is not int or tests not in range(1, 4) or not 0 <= band < len(belief.intervals) or load not in range(3): raise ValueError('Expected remaining tests and a valid current meter.') if fault_cost < 0 or audit_cost < 0: raise ValueError('Costs must be nonnegative.') probability = belief.flip_probability(band, load) guess = min(probability, 1 - probability) * fault_cost for flipped, weight in ((True, probability), (False, 1 - probability)): if weight: posterior = belief.observe(band, load, flipped) guess += weight * _future_cost(tuple(sorted(posterior.intervals)), tests - 1, fault_cost, audit_cost) return AuditDecision(probability, guess, Fraction(audit_cost)) def curve_erased_note(text): """Remove curve fields only; retain every other compiled plan field.""" note = json.loads(text) for circuit in note['p'].get('c', []): del circuit[3:] return json.dumps(note, sort_keys=True, separators=(',', ':')) class CommissioningControl(CausalReference): """Use mode 'audit', 'adaptive', or 'selective' with public transitions only. The reference owns the public prefix and all non-Blackout decisions. The adaptive arm never audits a known-base circuit. Selective compares expected direct-reconnection cost with the six-charge audit. Both defer rests until a further uncertain trial would risk making Ada unavailable. Selective is optimal only in the declared per-circuit cost model, which excludes rests. """ def __init__(self, mode='adaptive', erase_curves=True): super().__init__() if mode not in ('audit', 'adaptive', 'selective'): raise ValueError('Unknown commissioning control mode.') self.mode = mode self.erase_curves = erase_curves self.erasure_applied = False self.beliefs = {} self.belief_updates = [] self.audit_decisions = [] self.scope_exceptions = [] self._pending_trial = None def _ingest(self, transition): o, board = super()._ingest(transition) if o['phase'] != 6: return o, board self.pending_audit = None if self.erase_curves and not self.erasure_applied: for circuit in self.plan.get('c', []): del circuit[3:] self.erasure_applied = True pending = self._pending_trial if pending is not None and o['tick'] != pending['tick']: circuit = next(c for c in board['circuits'] if c['id'] == pending['service']) feedback = board.get('feedback') trip = (isinstance(feedback, dict) and feedback.get('circuit') == circuit['id'] and feedback.get('trip') == 'wiring') success = circuit['completed_tests'] > pending['completed'] if trip or success: actual = ('east' if pending['bus'] == 'west' else 'west') if trip else pending['bus'] prior = self.beliefs.setdefault(circuit['id'], ThresholdBelief()) posterior = prior.observe(pending['band'], pending['load'], actual != pending['base']) self.beliefs[circuit['id']] = posterior self.belief_updates.append(dict(service=circuit['id'], band=pending['band'], load=pending['load'], trip=trip, before=prior.intervals, after=posterior.intervals)) self._pending_trial = None elif isinstance(feedback, dict) and feedback.get('trip'): self._pending_trial = None return o, board def choose(self, transition): o, board = self._ingest(transition) if o['phase'] != 6: return super().choose(transition) if not self.plan.get('e') or len(self.plan.get('c', [])) != len(board['circuits']): return super().choose(transition) circuits = {c['id']: c for c in board['circuits']} live = {c['id'] for c in board['circuits'] if c['status'] == 'live'} ada = next(m for m in o['team'] if m['id'] == 'Ada') for entry in self.plan['c']: cid, base, predecessors = entry[:3] if cid in live or not set(predecessors) <= live: continue circuit = circuits[cid] meter = circuit.get('meter') if meter is None: return super().choose(transition) belief = self.beliefs.setdefault(cid, ThresholdBelief()) if len(entry) == 4: belief = ThresholdBelief(tuple((int(t), int(t)) for t in entry[3])) self.beliefs[cid] = belief probability = belief.flip_probability(meter['band'], meter['load']) uncertain = 0 < probability < 1 if len(entry) < 4 and self.mode == 'audit': return Action('audit_service', dict(service=cid)) if uncertain and ada['available'] and ada['exhaustion'] >= 3: return Action('rest', dict(actor='Ada')) if len(entry) < 4 and self.mode == 'selective': scoped = all(m['available'] for m in o['team']) and ada['exhaustion'] + 3 - circuit['completed_tests'] <= 3 if not scoped: reason = 'Four-charge per-circuit DP excludes fatigue-rest and availability costs.' if reason not in self.scope_exceptions: self.scope_exceptions.append(reason) decision = audit_decision(belief, 3 - circuit['completed_tests'], meter['band'], meter['load']) self.audit_decisions.append(dict(service=cid, test=meter['test'], guess_cost=str(decision.guess_cost), audit_cost=str(decision.audit_cost), audit=decision.audit, assumptions_met=scoped)) if decision.audit and scoped and Action('audit_service', dict(service=cid)) in transition.legal_actions: return Action('audit_service', dict(service=cid)) flipped = probability > Fraction(1, 2) bus = ('east' if base == 'west' else 'west') if flipped else base if circuit['status'] != 'connected' or circuit['bus'] != bus: return Action('connect', dict(circuit=cid, bus=bus)) self._pending_trial = dict(service=cid, band=meter['band'], load=meter['load'], bus=bus, base=base, completed=circuit['completed_tests'], tick=o['tick']) return Action('energize', dict(circuit=cid)) return Action('restore', {})