from __future__ import annotations import argparse import json import random import time from collections import Counter as Counts, deque from dataclasses import asdict from pathlib import Path from borderland import Campaign from borderland.domain import canonical from borderland.replay import source_hash from borderland_eval.archive_cases import BoundExceeded, Counter, Execution, PublicEvidence, PublicRecord, digest from borderland_eval.archive_learning import ArchivePolicy from borderland_eval.archive_study import new_output, save_verified, write_json from borderland_eval.context import actor_transition, actor_view from borderland_verify.causal_reference import CausalReference ASSISTANCE = dict(outside_archive='scripted_public_CausalReference', exploration='scripted_public_CausalReference', selections='all_eight_frozen_ArchivePolicy', proof_filing_bind='scripted_executor_without_selection_replacement', notebook='scripted_CausalReference.notebook_after_valid_bind', derived_facts='CausalReference._compile_public_Infrastructure', recovery='existing_public_reference_audits_at_actual_cost') def implementation_hash(): root = Path(__file__).parent.parent names = ('borderland_eval/archive_transfer.py', 'borderland_eval/archive_cases.py', 'borderland_eval/archive_learning.py', 'borderland_eval/archive_study.py', 'borderland_eval/context.py', 'borderland_verify/causal_reference.py') return digest({name: (root / name).read_text() for name in names}) class TransferController: def __init__(self, weights, rng=None): self.policy = ArchivePolicy(weights) self.reference = CausalReference() self.rng = random.Random(0) if rng is None else rng self.pending = deque() self.decisions = () def evidence(self): reference = self.reference records = [] for record in reference.records.values(): packet = asdict(record) if record.id in reference.ratings: packet['ratings'] = [int(value) for value in reference.ratings[record.id]] records.append(PublicRecord(record.id, record.service, record.supersedes, record.applies_channel, record.applies_bit, canonical(packet))) return PublicEvidence(tuple((name, *values) for name, values in reference.calibrations.items()), tuple(sorted(records, key=lambda record: record.id)), ()) def command(self, current): observation, board = self.reference._ingest(current) phase = observation['phase'] if phase == 3 and len(self.reference.seen_pages) == 3 * len(board['slots']): if not self.decisions: evidence = self.evidence() if len(evidence.services) != 8: raise ValueError('standard Archive requires eight services') self.decisions = tuple(self.policy.decide(evidence, service, self.rng, greedy=True) for service in evidence.services) records = {record.id: record for record in evidence.records} selected = [records[decision.candidates[decision.selected]] for decision in self.decisions] for record in selected: if record.supersedes is not None: self.pending.append(('action', dict(tag='compare_seal', arguments=dict( left_id=record.supersedes, right_id=record.id)), 'scripted_proof_executor')) self.pending.append(('action', dict(tag='file_record', arguments=dict(record_id=record.id)), 'scripted_filing_executor')) self.pending.append(('action', dict(tag='bind_docket', arguments=dict( record_ids=sorted(record.id for record in selected))), 'scripted_bind_executor')) if not self.pending: raise ValueError('failed bind cannot receive replacement selections') return self.pending.popleft() if phase >= 4: note = self.reference.notebook(current) if note is not None: return 'note', note, 'scripted_public_notebook_author' action = self.reference.choose(current) return 'action', action.to_dict(), 'scripted_public_reference' def run_campaign(seed, weights, action_seed, condition, output, *, command_limit=300, seconds=60): if condition not in ('retained', 'erased') or type(command_limit) is not int or not 1 <= command_limit <= 300: raise ValueError('retained or erased condition and command limit within 1..300 required') if type(seconds) not in (int, float) or not 0 < seconds <= 60: raise ValueError('seconds must be finite and within 0..60 exclusive of zero') frozen = ArchivePolicy(weights).weights destination = new_output(output) hashes = dict(source_hash=source_hash(), implementation_hash=implementation_hash()) execution = Execution(Campaign.generated(seed, generation_version=3, memory_profile='commissioning', archive_workload='standard', notebook_bytes=768), Counter(command_limit, seconds)) current = execution.campaign.reset() controller = TransferController(frozen, random.Random(action_seed)) decisions, resets, boundary, boundary_public = [], 0, None, None status = 'running' def spend(kind, payload, role): nonlocal current before = current count = len(execution.commands) try: current = execution.command(kind, payload, role) except ValueError: current = execution.campaign.observe() raise finally: if len(execution.commands) > count: execution.commands[-1].update(phase_before=int(before.observation.phase), phase_after=int(current.observation.phase), charge_before=before.observation.charge, charge_after=current.observation.charge, notebook_bytes=len(current.observation.notebook['text'].encode())) try: while not current.done: execution.counter.check() kind, payload, role = controller.command(actor_transition(current)) if controller.decisions and not decisions: decisions = [asdict(decision) for decision in controller.decisions] spend(kind, payload, role) if kind == 'action' and payload['tag'] == 'bind_docket' and int(current.observation.phase) != 4: status = 'bind_failed' break if current.context_reset: if int(current.observation.phase) != 6 or boundary is not None: raise ValueError('unexpected Blackout boundary') boundary = len(execution.commands) boundary_public = actor_view(current) controller = TransferController(frozen) resets += 1 if condition == 'erased': spend('note', '', 'lawful_boundary_notebook_erasure') else: status = 'engine_' + current.outcome except (BoundExceeded, ValueError, RuntimeError, StopIteration, KeyError) as error: status = str(error) if isinstance(error, BoundExceeded) else 'controller_' + type(error).__name__ failure = str(error) else: failure = None if hashes != dict(source_hash=source_hash(), implementation_hash=implementation_hash()): raise ValueError('source changed during transfer trial') commands = execution.commands report = dict(**hashes, seed=seed, action_seed=action_seed, condition=condition, weights=frozen, status=status, failure=failure, outcome=current.outcome, verified_escape=current.outcome == 'escaped', terminal=current.done, command_limit=command_limit, seconds_limit=seconds, elapsed_seconds=time.monotonic() - execution.counter.started, actual_commands=execution.counter.used, commands=commands, decisions=decisions, controller_resets=resets, boundary_index=boundary, boundary_public=boundary_public, prefix_hash=digest(commands[:boundary] if boundary is not None else commands), public_stats=dict(phase=int(current.observation.phase), charge=current.observation.charge, tick=current.observation.tick, actions=sum(command['kind'] == 'action' for command in commands), note_writes=sum(command['kind'] == 'note' for command in commands), audits=sum(command['kind'] == 'action' and command['input']['tag'] == 'audit_service' for command in commands), roles=dict(Counts(command['role'] for command in commands))), assistance=ASSISTANCE, replay_witness=save_verified(execution, destination / 'private.jsonl')) write_json(destination / 'receipt.json', report) return report def require_pair(retained, erased): if retained['condition'] != 'retained' or erased['condition'] != 'erased': raise ValueError('retained and erased pair required') if any(retained[key] != erased[key] for key in ('seed', 'action_seed', 'weights', 'source_hash', 'implementation_hash')): raise ValueError('paired world, frozen weights and action RNG must match') left, right = retained['boundary_index'], erased['boundary_index'] if left != right or canonical(retained['commands'][:left]) != canonical(erased['commands'][:right]): raise ValueError('preintervention command bytes differ') if canonical(retained['boundary_public']) != canonical(erased['boundary_public']): raise ValueError('preintervention public state differs') return dict(prefix_equal=True, boundary_reached=left is not None, prefix_commands=left if left is not None else retained['actual_commands'], prefix_hash=retained['prefix_hash']) def evaluate(plan_path, weights_paths, output): plan = json.loads(Path(plan_path).read_text()) checkpoints = [json.loads(Path(path).read_text()) for path in weights_paths] expected = dict(source_hash=source_hash(), implementation_hash=implementation_hash(), weights_hash=digest(checkpoints)) if any(plan.get(key) != value for key, value in expected.items()) or not plan.get('analysis'): raise ValueError('prospective transfer freeze must match source, implementation, weights and analysis') roots = plan.get('roots') if not isinstance(roots, list) or not roots or any(type(root) is not int or root < 0 for root in roots) or len(set(roots)) != len(roots): raise ValueError('explicit distinct nonnegative root list required') for checkpoint in checkpoints: ArchivePolicy(checkpoint['initial_weights']) ArchivePolicy(checkpoint['weights']) if type(checkpoint['initialization']) is not int or checkpoint['initialization'] < 0: raise ValueError('nonnegative action RNG initialization required') if not checkpoints or len({checkpoint['initialization'] for checkpoint in checkpoints}) != len(checkpoints): raise ValueError('distinct frozen weight initializations required') destination = new_output(output) write_json(destination / 'binding.json', dict(plan=plan, checkpoints=checkpoints, assistance=ASSISTANCE)) rows, pairs = [], [] for root in roots: for parity in (0, 1): for checkpoint in checkpoints: initialization = checkpoint['initialization'] trials = {} for policy, condition in (('initial', 'retained'), ('trained', 'retained'), ('trained', 'erased')): weights = checkpoint['initial_weights' if policy == 'initial' else 'weights'] name = f'{root}-{parity}-{initialization}-{policy}-{condition}' row = run_campaign(2 * root + parity, weights, initialization, condition, destination / name) row.update(root=root, parity=parity, initialization=initialization, policy=policy) rows.append(row) trials[policy, condition] = row write_json(destination / 'progress.json', dict(rows=rows, pairs=pairs)) pair = require_pair(trials['trained', 'retained'], trials['trained', 'erased']) pairs.append(dict(root=root, parity=parity, initialization=initialization, **pair)) write_json(destination / 'progress.json', dict(rows=rows, pairs=pairs)) if expected != dict(source_hash=source_hash(), implementation_hash=implementation_hash(), weights_hash=digest(checkpoints)): raise ValueError('source changed during transfer evaluation') report = dict(**expected, plan_hash=digest(plan), rows=rows, pairs=pairs, actual_commands=sum(row['actual_commands'] for row in rows), root_commands={str(root): sum(row['actual_commands'] for row in rows if row['root'] == root) for root in roots}, assistance=ASSISTANCE, scope='frozen assisted Archive selection transfer; scripted notebook and suffix') write_json(destination / 'evaluation.json', report) return report def main(): parser = argparse.ArgumentParser(description='Frozen assisted Archive full campaign transfer. No provider calls.') parser.add_argument('--plan', required=True) parser.add_argument('--weights', nargs='+', required=True) parser.add_argument('--output', required=True) args = parser.parse_args() report = evaluate(args.plan, args.weights, args.output) print(canonical(dict(trials=len(report['rows']), actual_commands=report['actual_commands']))) if __name__ == '__main__': main()