File size: 6,918 Bytes
767a051
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Command-line interface for TOM Learner 0.1 / WQK 0.5."""
from __future__ import annotations

import argparse
import json
from pathlib import Path
import shutil
from typing import Any, Mapping

from tom_world03.canonical import canonical_bytes

from .baseline import trusted_affine_learning_baseline
from .handoff import verify_corrective_handoff, verify_literal_handoff
from .io import load_observation_set
from .learner import learn_observation_set
from .store import LearnerStore


def _emit(value: Mapping[str, Any] | list[Any], output: str | None = None) -> None:
    data = canonical_bytes(value) + b"\n"
    if output:
        path = Path(output)
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_bytes(data)
    print(json.dumps(value, indent=2, sort_keys=True))


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(
        prog="tom-learner05",
        description=(
            "Reference affine oracle/evidence-store tools and corrective seeded-authority verification"
        ),
    )
    sub = parser.add_subparsers(dest="command", required=True)

    p_handoff = sub.add_parser("verify-handoff", help="verify every 0.4.2 authoritative literal file")
    p_handoff.add_argument("root", nargs="?", default=".")
    p_handoff.add_argument("--handoff")
    p_handoff.add_argument("--output")

    p_corrective_handoff = sub.add_parser(
        "verify-corrective-handoff",
        help="verify the 0.4.2 base plus the explicit 0.5.1 corrective overlay",
    )
    p_corrective_handoff.add_argument("root", nargs="?", default=".")
    p_corrective_handoff.add_argument("--corrective")
    p_corrective_handoff.add_argument("--output")

    p_validate = sub.add_parser("validate-dataset", help="verify and typecheck one observation set")
    p_validate.add_argument("dataset")
    p_validate.add_argument("--output")

    p_split = sub.add_parser("split", help="produce the deterministic ID-only train/validation/holdout split")
    p_split.add_argument("dataset")
    p_split.add_argument("--output")

    p_learn = sub.add_parser(
        "learn", help="run the non-authoritative host reference for one exact affine hypothesis"
    )
    p_learn.add_argument("dataset")
    p_learn.add_argument("--output")
    p_learn.add_argument("--records-dir")

    p_baseline = sub.add_parser("baseline", help="run the independent fractions.Fraction baseline")
    p_baseline.add_argument("dataset")
    p_baseline.add_argument("--output")

    p_init = sub.add_parser("init-store", help="initialize an empty parent-bound learner overlay store")
    p_init.add_argument("store")
    p_init.add_argument("--seed", default="TOM_seed_genome_2026-09-01.txt")
    p_init.add_argument("--replace", action="store_true")
    p_init.add_argument("--output")

    p_promote = sub.add_parser("promote", help="learn and commit one accepted or rejected session")
    p_promote.add_argument("store")
    p_promote.add_argument("dataset")
    p_promote.add_argument("--expected-parent", required=True)
    p_promote.add_argument("--output")

    p_audit = sub.add_parser("audit", help="audit all immutable learner evidence and commit ancestry")
    p_audit.add_argument("store")
    p_audit.add_argument("--allow-orphans", action="store_true")
    p_audit.add_argument("--output")

    p_reconstruct = sub.add_parser("reconstruct", help="reconstruct the authoritative learner overlay")
    p_reconstruct.add_argument("store")
    p_reconstruct.add_argument("--output")

    args = parser.parse_args(argv)

    if args.command == "verify-handoff":
        record = verify_literal_handoff(args.root, args.handoff)
        _emit(record, args.output)
        return 0 if record["valid"] else 1

    if args.command == "verify-corrective-handoff":
        record = verify_corrective_handoff(args.root, args.corrective)
        _emit(record, args.output)
        return 0 if record["valid"] else 1

    if args.command == "validate-dataset":
        raw, dataset = load_observation_set(args.dataset)
        record = {
            "schema": "TOM-LEARNER-DATASET-VALIDATION-0.1",
            "status": "valid",
            "id": dataset.id,
            "content_hash": dataset.content_hash,
            "observation_count": len(dataset.observations),
            "literal_bytes": len(canonical_bytes(raw)) + 1,
            "base_world_hash": dataset.base_world_hash,
            "base_handoff_hash": dataset.base_handoff_hash,
        }
        _emit(record, args.output)
        return 0

    if args.command in {"split", "learn", "baseline", "promote"}:
        raw, dataset = load_observation_set(args.dataset)

    if args.command == "split":
        from .split import deterministic_split
        _emit(deterministic_split(dataset), args.output)
        return 0

    if args.command == "learn":
        run = learn_observation_set(dataset)
        if args.records_dir:
            directory = Path(args.records_dir)
            directory.mkdir(parents=True, exist_ok=True)
            for index, record in enumerate(run.all_records()):
                schema = str(record.get("schema", "record")).lower().replace("tom-", "").replace("-", "_")
                path = directory / f"{index:04d}_{schema}_{record['content_hash'][7:19]}.json"
                path.write_bytes(canonical_bytes(record) + b"\n")
        _emit(run.summary(), args.output)
        return 0 if run.accepted else 3

    if args.command == "baseline":
        _emit(trusted_affine_learning_baseline(raw), args.output)
        return 0

    if args.command == "init-store":
        target = Path(args.store)
        if target.exists() and args.replace:
            shutil.rmtree(target)
        store = LearnerStore.initialize(target, Path(args.seed).read_bytes())
        record = {
            "schema": "TOM-LEARNER-STORE-INITIALIZATION-0.1",
            "store": str(target),
            "head": store.head(),
            "descriptor": store.descriptor(),
        }
        _emit(record, args.output)
        return 0

    if args.command == "promote":
        store = LearnerStore(args.store)
        run = learn_observation_set(dataset)
        commit = store.commit_learning(run, expected_parent=args.expected_parent)
        record = {
            "schema": "TOM-LEARNER-PROMOTION-RESULT-0.1",
            "accepted": run.accepted,
            "summary": run.summary(),
            "commit": commit,
            "new_head": store.head(),
        }
        _emit(record, args.output)
        return 0

    if args.command == "audit":
        record = LearnerStore(args.store).audit(require_no_orphans=not args.allow_orphans)
        _emit(record, args.output)
        return 0 if record["valid"] else 1

    if args.command == "reconstruct":
        _emit(LearnerStore(args.store).reconstruct(), args.output)
        return 0

    return 2


if __name__ == "__main__":
    raise SystemExit(main())