"""memory - the Workbench's shared specialist memory (Oracle7 4.2): o7spine_v1 + swarm_memory_v2, kept on this machine. your group's memory workbench/local/memory/group.json - every specialist this Workbench built or composed and then ADMITTED on its held-out exam, filed by the request it answered (only verified outcomes are written) shared memories workbench/local/memory/shared/*.json - exports from other groups you imported; each entry's chain hash is checked before it is accepted the commons your group's memory plus every shared one, summed: what a new build recalls from first A recall is a suggestion to REUSE a specialist someone already built and examined ("group X built table-vertical-depth for this gap"); it never answers a question, and the specialist answers only after passing its own exam on this machine.""" from __future__ import annotations import json import os import time from pathlib import Path HERE = Path(__file__).resolve().parent os.environ.setdefault("ORACLE7_ROOT", str(HERE.parent.parent / "root")) import o7spine_v1 as S # noqa: E402 import swarm_memory_v2 as SM # noqa: E402 LOCAL = Path(os.environ.get("ORACLE7_WORKBENCH_LOCAL", str(HERE.parent / "local"))) MEM = LOCAL / "memory" GROUP = MEM / "group.json" SHARED = MEM / "shared" def _load_entries(m, entries): for e in entries: e = dict(e) e.setdefault("recalled", 0) m.index.append(e) m._file(e) if entries: m.head = entries[-1]["h"] def load_group(name=None): m = S.SpineMemory(name or os.environ.get("ORACLE7_GROUP", "this workbench")) if GROUP.exists(): d = json.loads(GROUP.read_text(encoding="utf-8")) bad = S.SpineMemory.verify(d["entries"]) if bad: raise ValueError("your memory file fails its chain at %s: it was changed outside the Workbench" % ", ".join(bad)) m.name = d.get("name", m.name) _load_entries(m, d["entries"]) return m def save_group(m): MEM.mkdir(parents=True, exist_ok=True) GROUP.write_text(json.dumps({"name": m.name, "saved": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "entries": [{k: e[k] for k in ("key", "addr", "word", "fields", "w", "share", "source", "prev", "h")} for e in m.index]}, indent=0, ensure_ascii=False), encoding="utf-8") def commons(): members = [load_group()] if SHARED.is_dir(): for p in sorted(SHARED.glob("*.json")): d = json.loads(p.read_text(encoding="utf-8")) g = S.SpineMemory(d.get("name", p.stem)) _load_entries(g, d["entries"]) members.append(g) return S.SpineMemory.group("commons", members) def remember(questions, gap, specialist, share=True, node=None): """write an ADMITTED specialist to your group's memory (node: where its operation lives; by default read from the questions - Hod when they carry numbers, Binah otherwise; a composed program is always Hod)""" m = load_group() if node: e = m.write(SM.key_of(questions, gap), None, {"spec": specialist, "outcome": "admitted"}, node=node, share=share) else: e = SM.remember(m, questions, gap, specialist, outcome="admitted", share=share) save_group(m) return e def recall(questions, gap="", node=None): """(specialist, confidence, which memory) or None""" c = commons() if not any(c.vals[s] for s in S.SEPH): return None if node: r0 = c.recall(SM.key_of(questions, gap), node=node, threshold=SM.THRESHOLD) r = (r0["fields"]["spec"], r0["confidence"]) if r0 and r0["fields"].get("outcome") == "admitted" else None else: r = SM.recall(c, questions, gap) if not r: return None sid, conf = r owner = "your group" if SHARED.is_dir(): for p in sorted(SHARED.glob("*.json")): d = json.loads(p.read_text(encoding="utf-8")) if any(e["fields"].get("spec") == sid for e in d["entries"]): owner = d.get("name", p.stem) return sid, conf, owner def export(path): m = load_group() d = m.export() Path(path).write_text(json.dumps(d, indent=0, ensure_ascii=False), encoding="utf-8") return len(d["entries"]) def import_shared(path): d = json.loads(Path(path).read_text(encoding="utf-8")) bad = S.SpineMemory.verify(d["entries"]) if bad: raise ValueError("refused: %d entries fail their chain hash (%s)" % (len(bad), ", ".join(bad[:4]))) SHARED.mkdir(parents=True, exist_ok=True) name = "".join(ch if ch.isalnum() or ch in "-_" else "-" for ch in str(d.get("name", "shared")))[:40] or "shared" (SHARED / ("%s.json" % name)).write_text(json.dumps(d, indent=0, ensure_ascii=False), encoding="utf-8") return len(d["entries"]), name def listing(): m = load_group() rows = [{"specialist": e["fields"].get("spec"), "outcome": e["fields"].get("outcome"), "node": e["addr"], "shared": e["share"]} for e in m.index] shared = [p.stem for p in sorted(SHARED.glob("*.json"))] if SHARED.is_dir() else [] return {"group": m.name, "entries": rows, "shared_memories": shared}