File size: 19,487 Bytes
a8aea51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from __future__ import annotations

import argparse
import json
import random
from dataclasses import asdict, dataclass
from pathlib import Path

from borderland.domain import canonical
from borderland.replay import source_hash, verify_replay
from borderland_eval.archive_cases import (ArchiveCase, BoundExceeded, Counter, actor_packet,
                                          digest, physical_projection, require_twins, surviving_projection)
from borderland_eval.archive_learning import (ArchivePolicy, FEATURES, execute_docket,
                                             ownership, trial)


def implementation_hash():
    return digest({name: Path(__file__).with_name(name).read_text() for name in
                   ('archive_cases.py', 'archive_learning.py', 'archive_study.py')})


def write_json(path, value):
    Path(path).write_text(canonical(value) + '\n')


@dataclass(frozen=True)
class StudyPlan:
    source_hash: str
    train_roots: tuple[int, ...]
    development_roots: tuple[int, ...]
    confirmation_roots: tuple[int, ...]
    initializations: tuple[int, ...]
    pilot_only: bool = True

    @classmethod
    def read(cls, path):
        values = json.loads(Path(path).read_text())
        required = {'source_hash', 'train_roots', 'development_roots', 'confirmation_roots', 'initializations'}
        if not required <= set(values) or set(values) - required - {'pilot_only'}:
            raise ValueError('plan keys differ from documented schema')
        for key in required - {'source_hash'}:
            if not isinstance(values[key], list) or any(type(value) is not int or value < 0 for value in values[key]):
                raise ValueError('roots and initializations must be nonnegative integer lists')
            values[key] = tuple(values[key])
        plan = cls(**values)
        plan.validate()
        return plan

    def validate(self):
        if self.source_hash != source_hash():
            raise ValueError('plan engine source mismatch')
        if (len(self.train_roots), len(self.development_roots), len(self.confirmation_roots)) != (32, 8, 16):
            raise ValueError('plan requires 32 training, 8 development and 16 confirmation root groups')
        roots = self.train_roots + self.development_roots + self.confirmation_roots
        if len(roots) != len(set(roots)) or any(type(root) is not int or root < 0 for root in roots):
            raise ValueError('root groups must be distinct nonnegative integers')
        if len(self.initializations) != 3 or len(set(self.initializations)) != 3 or any(type(seed) is not int or seed < 0 for seed in self.initializations):
            raise ValueError('three distinct action RNG initializations required')
        if type(self.pilot_only) is not bool:
            raise ValueError('pilot_only must be boolean')


def new_output(path):
    path = Path(path)
    path.mkdir(parents=True, exist_ok=False, mode=0o700)
    return path


def save_verified(execution, path):
    execution.campaign.save_replay(path, visibility='private')
    receipt = verify_replay(path)
    if not receipt['verified']:
        raise ValueError('private replay verification failed')
    return dict(path=str(Path(path).resolve()), verification=receipt)


def probe(policy, evidence):
    return [asdict(policy.decide(evidence, service, random.Random(0), greedy=True))
            for service in evidence.services]


class ArchiveStudy:
    def __init__(self, plan, output):
        plan.validate()
        self.plan = plan
        self.output = Path(output)

    def fit(self):
        self.plan.validate()
        output = new_output(self.output)
        binding = dict(plan=asdict(self.plan), plan_hash=digest(asdict(self.plan)),
                       engine_hash=source_hash(), implementation_hash=implementation_hash(),
                       features=FEATURES, objective='actual_phase3_to4_binary_exit',
                       learning_rate=.15, gamma=1, baseline='previous_arithmetic_running_return_mean',
                       scope='assisted_applicability_only',
                       evaluation='frozen_greedy_randomized_ties_same_rule_initial_and_trained')
        write_json(output / 'binding.json', binding)
        summaries = []
        for initialization in self.plan.initializations[:1] if self.plan.pilot_only else self.plan.initializations:
            policy = ArchivePolicy()
            rng = random.Random(initialization)
            counter = Counter(8448, 60)
            cases = {}
            returns = []
            status = 'running'
            gate = None
            first_probe = None
            init_output = new_output(output / str(initialization))
            with (init_output / 'episodes.jsonl').open('w') as stream:
                for episode in range(32 if self.plan.pilot_only else 256):
                    root = self.plan.train_roots[(episode // 2) % len(self.plan.train_roots)]
                    parity = episode % 2
                    start_actions = counter.used
                    execution = None
                    receipt = dict(episode=episode, root=root, parity=parity, owned_count=ownership(episode))
                    try:
                        if source_hash() != binding['engine_hash'] or implementation_hash() != binding['implementation_hash']:
                            raise ValueError('source changed during study')
                        if (root, parity) in cases:
                            case = cases[root, parity]
                            execution = case.reconstruct(counter)
                        else:
                            case, execution = ArchiveCase.acquire(root, parity, counter)
                            cases[root, parity] = case
                            write_json(init_output / f'case-{root}-{parity}.json', case.recipe())
                        if first_probe is None:
                            first_probe = case.evidence
                            write_json(init_output / 'initial-probe.json', probe(policy, first_probe))
                        result, decisions = trial(execution, case.evidence, policy, ownership(episode), rng)
                        baseline = sum(returns) / len(returns) if returns else 0.0
                        receipt.update(result, update=policy.update(decisions, result['reward'], baseline))
                        if source_hash() != binding['engine_hash'] or implementation_hash() != binding['implementation_hash']:
                            raise ValueError('source changed during study')
                        returns.append(result['reward'])
                        if result['reward'] and not (init_output / 'success-witness.jsonl').exists():
                            receipt['replay_witness'] = save_verified(execution, init_output / 'success-witness.jsonl')
                    except (BoundExceeded, ValueError) as error:
                        status = str(error)
                        if execution is not None:
                            receipt.update(execution.trial_receipt)
                        receipt.update(failed=True, failure=status)
                    receipt.update(actual_actions=counter.used - start_actions, cumulative_actions=counter.used,
                                   commands=counter.current_commands if execution is None else execution.commands)
                    stream.write(canonical(receipt) + '\n')
                    stream.flush()
                    if status != 'running':
                        break
                    if episode == 31:
                        before = probe(ArchivePolicy(), first_probe)
                        after = probe(policy, first_probe)
                        changed = any(a['probabilities'] != b['probabilities'] for a, b in zip(before, after))
                        gate = dict(passed=bool(sum(returns) and any(policy.weights) and changed),
                                    successes=sum(returns), episodes=len(returns), actions=counter.used,
                                    weights=policy.weights, probabilities_changed=changed)
                        try:
                            counter.check()
                            write_json(init_output / 'pilot-gate.json', gate)
                            counter.check()
                        except BoundExceeded as error:
                            status = str(error)
                            gate.update(passed=False, failure=status)
                            write_json(init_output / 'pilot-gate.json', gate)
                        if not gate['passed']:
                            if status == 'running':
                                status = 'pilot_signal_failed'
                            break
                        if not self.plan.pilot_only:
                            counter.limit = 67584
                            counter.seconds = 180
                if status == 'running':
                    try:
                        counter.check()
                    except BoundExceeded as error:
                        status = str(error)
                        if self.plan.pilot_only:
                            gate.update(passed=False, failure=status)
                            write_json(init_output / 'pilot-gate.json', gate)
                    else:
                        status = 'pilot_complete' if self.plan.pilot_only else 'schedule_complete'
            summary = dict(initialization=initialization, status=status, completed_episodes=len(returns),
                           successes=sum(returns), actual_actions=counter.used, weights=policy.weights,
                           initial_weights=(0.0,) * 5, pilot_gate=gate)
            write_json(init_output / 'checkpoint.json', summary)
            summaries.append(summary)
            if status not in ('pilot_complete', 'schedule_complete'):
                break
        report = dict(binding=binding, initializations=summaries, confirmation_materialized=False,
                      complete=len(summaries) == (1 if self.plan.pilot_only else 3) and
                      all(row['status'] in ('pilot_complete', 'schedule_complete') for row in summaries))
        write_json(output / 'report.json', report)
        return report

    def evaluate(self, output, split='development', freeze=None):
        self.plan.validate()
        if split not in ('development', 'confirmation'):
            raise ValueError('evaluation split must be development or confirmation')
        binding = json.loads((self.output / 'binding.json').read_text())
        if (binding['implementation_hash'] != implementation_hash() or
                binding.get('engine_hash') != source_hash() or
                binding['plan_hash'] != digest(asdict(self.plan))):
            raise ValueError('training binding differs from evaluation source or plan')
        checkpoints = [json.loads((self.output / str(seed) / 'checkpoint.json').read_text()) for seed in self.plan.initializations]
        if any(row['status'] != 'schedule_complete' for row in checkpoints):
            raise ValueError('evaluation requires all three complete frozen schedules')
        if split == 'confirmation':
            if freeze is None:
                raise ValueError('confirmation requires explicit prospective freeze file')
            frozen = json.loads(Path(freeze).read_text())
            expected = dict(implementation_hash=implementation_hash(), plan_hash=digest(asdict(self.plan)),
                            checkpoints_hash=digest(checkpoints))
            if any(frozen.get(key) != value for key, value in expected.items()) or not frozen.get('analysis'):
                raise ValueError('confirmation freeze does not match source, plan, checkpoints and analysis')
        destination = new_output(output)
        roots = self.plan.development_roots if split == 'development' else self.plan.confirmation_roots
        rows = []
        for root in roots:
            for parity in (0, 1):
                admission_counter = Counter(240, 60)
                case, _ = ArchiveCase.acquire(root, parity, admission_counter)
                write_json(destination / f'case-{root}-{parity}.json', case.recipe())
                for checkpoint in checkpoints:
                    seed = checkpoint['initialization']
                    for label, weights in (('initial', checkpoint['initial_weights']), ('trained', checkpoint['weights'])):
                        counter = Counter(264, 60)
                        execution = case.reconstruct(counter)
                        result, _ = trial(execution, case.evidence, ArchivePolicy(weights), 8,
                                          random.Random(seed), greedy=True)
                        if source_hash() != binding['engine_hash'] or implementation_hash() != binding['implementation_hash']:
                            raise ValueError('source changed during evaluation')
                        witness = save_verified(execution, destination / f'{root}-{parity}-{seed}-{label}.jsonl')
                        rows.append(dict(root=root, parity=parity, initialization=seed, policy=label,
                                         actual_actions=counter.used, commands=execution.commands,
                                         replay_witness=witness, **result))
        report = dict(split=split, plan_hash=digest(asdict(self.plan)), rows=rows,
                      admission_actions=sum(len(json.loads((destination / f'case-{root}-{parity}.json').read_text())['prefix']) for root in roots for parity in (0, 1)),
                      assistance='scripted prefix, evidence retention, proof/file executor at all stages')
        write_json(destination / 'evaluation.json', report)
        return report


def prepare_packets(plan, output, roots):
    plan.validate()
    if any(root not in plan.development_roots for root in roots):
        raise ValueError('offline preparation accepts explicit development roots only')
    destination = new_output(output)
    expected_implementation = implementation_hash()
    receipts = []
    for root in roots:
        pair_output = new_output(destination / str(root))
        cases = [ArchiveCase.acquire(root, parity, Counter(240, 60))[0] for parity in (0, 1)]
        for parity, case in enumerate(cases):
            write_json(pair_output / f'operator-case-{parity}.json', case.recipe())
        if cases[0].evidence.records != cases[1].evidence.records or cases[0].evidence.history != cases[1].evidence.history:
            raise ValueError('raw Archive packets differ between twins')
        channel = cases[0].evidence.measurements[0][0]
        if cases[0].evidence.bit(channel) == cases[1].evidence.bit(channel):
            raise ValueError('target public bit did not change')
        packets = []
        physical = []
        witnesses = []
        branch_commands = []
        for parity, case in enumerate(cases):
            for treatment in ('correct', 'erased', 'foil'):
                execution = case.reconstruct(Counter(264, 60))
                note = json.loads(execution.campaign.observe().observation.notebook['text'])
                note['m'][0][1] = ('?' if treatment == 'erased' else str(cases[1 - parity].evidence.bit(channel))
                                  if treatment == 'foil' else str(case.evidence.bit(channel)))
                text = canonical(note)
                if len(text.encode()) != len(execution.campaign.observe().observation.notebook['text'].encode()):
                    raise ValueError('note treatments must use equal UTF-8 bytes')
                note['v'] = 1
                text = canonical(note)
                execution.command('note', text, 'lawful_equal_byte_intervention')
                packet = actor_packet(execution, case.evidence)
                packets.append(packet)
                physical.append(physical_projection(execution))
                write_json(pair_output / f'actor-{parity}-{treatment}.json', packet)
                witnesses.append(save_verified(execution, pair_output / f'prefix-{parity}-{treatment}.jsonl'))
                if treatment in ('correct', 'foil'):
                    note_bits = {name: int(bit) for name, bit, _ in note['m']}
                    selected = {service: next(record.id for record in case.evidence.candidates(service)
                                if record.supersedes is not None and record.applies_bit == note_bits[record.applies_channel])
                                for service in case.evidence.services}
                    expected = int(treatment == 'correct')
                    if execute_docket(execution, case.evidence, selected, ()) != expected:
                        raise ValueError('public note witness disagrees with expected intervention outcome')
                    witnesses.append(save_verified(execution, pair_output / f'{treatment}-continuation-{parity}.jsonl'))
                branch_commands.append(execution.counter.used)
        for packet, state in zip(packets[1:], physical[1:]):
            require_twins(packets[0], packet)
            if state != physical[0]:
                raise ValueError('private physical state mismatch outside notebook and revision')
        receipt = dict(root=root, channel=channel, actor_packets=6,
                       target_services=sorted({record.subject for record in cases[0].evidence.records if record.applies_channel == channel}),
                       actual_preparation_commands=sum(len(case.prefix) for case in cases) +
                       sum(branch_commands),
                       surviving_projection_hash=digest(surviving_projection(packets[0])),
                       complete_projection_equal=True, private_physical_equal=True,
                       source_hash=source_hash(), implementation_hash=implementation_hash(),
                       witnesses=witnesses, readiness='offline_only')
        if source_hash() != plan.source_hash or implementation_hash() != expected_implementation:
            raise ValueError('source changed during offline preparation')
        write_json(pair_output / 'admission.json', receipt)
        receipts.append(receipt)
    write_json(destination / 'preparation.json', receipts)
    return receipts


def main():
    parser = argparse.ArgumentParser(description='Assisted Archive applicability study. No provider calls.')
    parser.add_argument('command', choices=('run', 'train', 'evaluate', 'prepare'))
    parser.add_argument('--plan', required=True)
    parser.add_argument('--output', required=True)
    parser.add_argument('--training')
    parser.add_argument('--split', choices=('development', 'confirmation'), default='development')
    parser.add_argument('--freeze')
    parser.add_argument('--roots', nargs='+', type=int)
    args = parser.parse_args()
    plan = StudyPlan.read(args.plan)
    if args.command == 'prepare':
        if args.roots is None:
            parser.error('prepare requires explicit --roots from development_roots')
        result = prepare_packets(plan, args.output, args.roots)
    elif args.command == 'evaluate':
        if args.training is None:
            parser.error('evaluate requires --training')
        result = ArchiveStudy(plan, args.training).evaluate(args.output, args.split, args.freeze)
    else:
        result = ArchiveStudy(plan, args.output).fit()
    print(canonical(result if args.command != 'evaluate' else dict(split=args.split, episodes=len(result['rows']))))


if __name__ == '__main__':
    main()