File size: 7,169 Bytes
ad19be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""cli10 - the commands for every capability from 5.5 to 10.0, in the same oracle7.py as everything before them.

  organs                         which capabilities this install carries, version by version
  lesson "question"              7.5  a physics answer opened into a lesson: each step with its Wikidata formula, checked;
                                      a worked example re-solved from the equations; three practice questions
  compute [length]               5.5  the units of the composition space, counted exactly (none stored), sampled and decoded
  grow [rounds] [questions]      6.0  growth: questions it cannot answer become parts it builds itself (your workspace copy)
  packet publish <part id>       6.5  a part as a packet another place can check;  packet verify <file>  check one here
  place                          7.0  the Slow Fire rules on a simulated oak (greeting, consent, sensors examined, tampering)
  discover                       8.5  hypotheses written from the formula library alone, before any data is fetched
  ledger ask "question" | ledger replay   9.5  answers recorded against a content-addressed state; replayed as of then
  air "question"                 10.0 the air's carbon from NOAA's records, cited, computed two ways
  audit <ledger>                 10.0 re-verify a conversation ledger without trusting it (--reread asks again)"""
import json
import random
import sys
import time
from pathlib import Path

import organs10 as O

NAMES = {"organs", "lesson", "compute", "grow", "packet", "place", "discover", "ledger", "air", "audit"}


def _out(obj, as_json, text):
    print(json.dumps(obj, indent=1, default=str) if as_json else text)


def _workspace():
    """growth changes a workspace, so it grows the user's own copy; the shipped parts stay as they were sealed"""
    import shutil
    w = O.MADE / "workspace"
    if not (w / "swarm" / "parts.json").exists():
        shutil.copytree(O.PHYSICS, w, dirs_exist_ok=True)
    return w


def main(cmd, rest, as_json=False):
    if cmd == "organs":
        s = O.status()
        return _out(s, as_json, "\n".join("%-28s %s" % (k, "present" if v else "MISSING") for k, v in s.items())) or 0
    if cmd == "lesson":
        if not rest:
            print('usage: lesson "What is the X when the A is 3 m and the B is 2 s?"')
            return 2
        import gen60
        import grow60 as G
        import teach75 as T
        lib = gen60.Library(json.load(open(O.LIBRARY / "derived.json", encoding="utf-8")))
        planner = G.Planner(G.load_parts(str(O.PHYSICS)))
        out = O.MADE / "lessons" / time.strftime("%Y%m%dT%H%M%S")
        out.mkdir(parents=True, exist_ok=True)
        r = T.lesson(str(O.PHYSICS), lib, planner, " ".join(rest), random.Random(0), str(out), 0)
        if r is None:
            print("I can't open a lesson on that: no checked plan answers it.")
            return 1
        if as_json:
            return _out(r, True, "") or 0
        print("Answer: %.6g" % r["answer"])
        for i, s in enumerate(r["steps"], 1):
            print("%d. %s" % (i, json.dumps(s, default=str)[:300]))
        print("checks:", json.dumps(r.get("checks"), default=str)[:400])
        print("files:", out)
        return 0
    if cmd == "compute":
        import substrate_v55 as S
        L = rest[0] if rest else "4"
        sys.argv = ["substrate_v55.py", str(O.PHYSICS), L, "200", "0"]
        S.main()
        return 0
    if cmd == "grow":
        import gen60
        import grow60 as G
        rounds = rest[0] if rest else "1"
        n = int(rest[1]) if len(rest) > 1 else 40
        w = _workspace()
        lib = gen60.Library(json.load(open(O.LIBRARY / "derived.json", encoding="utf-8")))
        stream = O.MADE / "grow" / ("stream_%d.json" % int(time.time()))
        stream.parent.mkdir(parents=True, exist_ok=True)
        json.dump(gen60.make(lib, int(time.time()) % 100000, n, 0), open(stream, "w"), indent=0)
        sys.argv = ["grow60.py", str(w), str(O.LIBRARY / "derived.json"), str(stream), rounds, "40", str(stream.parent / "run")]
        G.main()
        return 0
    if cmd == "packet":
        import node65 as N
        if rest[:1] == ["publish"] and len(rest) > 1:
            p = N.make_packet(str(O.PHYSICS), rest[1], "this machine")
            f = O.MADE / "packets" / (rest[1].replace("#", "_") + ".json")
            f.parent.mkdir(parents=True, exist_ok=True)
            f.write_text(json.dumps(p, indent=1), encoding="utf-8")
            return _out(p, as_json, "packet written: %s (hash %s)" % (f, p.get("hash", "")[:16])) or 0
        if rest[:1] == ["verify"] and len(rest) > 1:
            lib = N.library_index(json.load(open(O.LIBRARY / "derived.json", encoding="utf-8")))
            v = N.verify(json.load(open(rest[1], encoding="utf-8")), lib, 0)
            return _out(v, as_json, ("ADMIT" if v["admit"] else "REFUSE: %s" % v["why"])) or (0 if v["admit"] else 1)
        print("usage: packet publish <part id> | packet verify <file>")
        return 2
    if cmd == "place":
        import slowfire71 as F
        out = O.PLACES / ("sim_%d.json" % int(time.time()))
        out.parent.mkdir(parents=True, exist_ok=True)
        r = F.simulate(int(rest[0]) if rest else 0, out=str(out))
        s = F.score(r)
        return _out(s, as_json, "\n".join("%-28s %s" % (k, v) for k, v in s.items()) + "\nrecord: %s" % out) or 0
    if cmd == "discover":
        import discover85 as D
        derived = json.load(open(O.LIBRARY / "derived.json", encoding="utf-8"))
        raw = json.load(open(O.LIBRARY / "library_raw.json", encoding="utf-8"))
        hs, _parts = D.hypotheses(derived, raw)
        txt = "%d hypotheses written from the library alone (no data fetched yet). Testing them fetches Wikidata values: " \
              "python -c \"import discover85; discover85.test(<hypotheses file>, <out dir>)\"" % len(hs)
        return _out({"hypotheses": len(hs), "first": hs[:5]}, as_json, txt) or 0
    if cmd == "ledger":
        import ledger95 as L
        store, led = O.LEDGER / "store", O.LEDGER / "ledger.jsonl"
        store.mkdir(parents=True, exist_ok=True)
        if rest[:1] == ["ask"] and len(rest) > 1:
            sh = L.snapshot(str(O.PHYSICS), str(store))
            e = L.ask(str(store), sh, str(led), " ".join(rest[1:]))
            return _out(e, as_json, "%s  %s  (state %s, entry %s)" % (e["status"], e.get("value") or e.get("why"), sh[:12], e["hash"][:12])) or 0
        if rest[:1] == ["replay"]:
            r = L.replay(str(store), str(led))
            return _out(r, as_json, json.dumps({k: v for k, v in r.items() if k != "mismatches"})) or 0
        print('usage: ledger ask "question" | ledger replay')
        return 2
    if cmd == "air":
        a = O.V.Air().ask(" ".join(rest))
        return _out(a, as_json, a.get("answer") or "Declined: %s" % a.get("why")) or 0
    if cmd == "audit":
        import audit10 as A
        if not rest:
            print("usage: audit <ledger.jsonl> [--reread]")
            return 2
        r = A.audit(rest[0], "--reread" in rest)
        return 0 if r["verdict"] == "PASS" else 1
    return 2