File size: 8,381 Bytes
77f7f66 | 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 | r"""TAV_TORUS β Tav'iel's TempTorus memory: the mind that does not forget.
The TempTorus pattern (a life as SEQUENCE in its own sqlite, sha-chained and endable)
applied to conversation: every moment lives on a ring, and the WEAVE brings back what
matters so the context is effectively INFINITE β nothing falls off the world; it lives
in the torus and returns by relevance.
ring 0 β turns: every exchange, verbatim (short-term)
ring 1 β facts: distilled standing knowledge (long-term)
ring 2 β summaries: folded epochs of ring 0 (deep past)
Each moment is sha-chained to the one before (the TempTorus seal): the memory's
history is tamper-evident. context_weave(query, budget) packs recent turns + recalled
facts + epoch summaries under a byte budget β the model always reasons with a woven
past regardless of how long the walk has been.
"""
from __future__ import annotations
import hashlib
import json
import os
import re
import sqlite3
import threading
import time
_YB = os.environ.get("YAHBIBLE_BASE", "D:").replace("\\", "/").rstrip("/")
DB = (_YB + "/Holorites_data/daeos/tav_torus.sqlite").replace("/", os.sep)
_LOCK = threading.Lock()
_CON = None
EPOCH_FOLD = 12 # ring-0 turns folded into one ring-2 summary
def _con():
global _CON
if _CON is None:
os.makedirs(os.path.dirname(DB), exist_ok=True)
c = sqlite3.connect(DB, check_same_thread=False)
c.execute("PRAGMA journal_mode=WAL")
c.execute("""CREATE TABLE IF NOT EXISTS moments(
seq INTEGER PRIMARY KEY AUTOINCREMENT,
ts REAL NOT NULL, ring INTEGER NOT NULL, kind TEXT NOT NULL,
content TEXT NOT NULL, refs TEXT, folded INTEGER DEFAULT 0, sha TEXT NOT NULL)""")
c.execute("CREATE INDEX IF NOT EXISTS idx_m_ring ON moments(ring, folded)")
try:
c.execute("CREATE VIRTUAL TABLE IF NOT EXISTS moments_fts USING fts5("
"content, content_rowid='seq', content='moments')")
except sqlite3.OperationalError:
pass
c.commit()
_CON = c
return _CON
def _chain_sha(c, content):
row = c.execute("SELECT sha FROM moments ORDER BY seq DESC LIMIT 1").fetchone()
prev = row[0] if row else "genesis"
return hashlib.sha256((prev + "\x1f" + content).encode("utf-8")).hexdigest()
def _put(ring, kind, content, refs=None):
with _LOCK:
c = _con()
sha = _chain_sha(c, content)
cur = c.execute("INSERT INTO moments(ts,ring,kind,content,refs,sha) VALUES(?,?,?,?,?,?)",
(time.time(), ring, kind, content, json.dumps(refs or []), sha))
try:
c.execute("INSERT INTO moments_fts(rowid, content) VALUES(?,?)",
(cur.lastrowid, content))
except sqlite3.OperationalError:
pass
c.commit()
return cur.lastrowid
def record_turn(question, answer, refs=None):
"""A finished exchange enters ring 0; the torus folds old epochs by itself."""
_put(0, "turn", "Seeker: %s\nTav'iel: %s" % ((question or "").strip()[:800],
(answer or "").strip()[:1600]), refs)
_maybe_fold()
def remember_fact(fact, refs=None):
"""A standing truth enters ring 1 (long-term) β recalled by relevance forever."""
if fact and len(fact.strip()) > 8:
_put(1, "fact", fact.strip()[:600], refs)
def _maybe_fold():
"""When enough unfolded turns pile up, fold the oldest epoch into one ring-2
summary (extractive β the seeker's questions carry the thread) and mark them."""
with _LOCK:
c = _con()
rows = c.execute("SELECT seq, content FROM moments WHERE ring=0 AND folded=0"
" ORDER BY seq LIMIT ?", (EPOCH_FOLD * 2,)).fetchall()
if len(rows) < EPOCH_FOLD * 2:
return
fold = rows[:EPOCH_FOLD]
lines = []
for _, content in fold:
q = content.split("\n", 1)[0].replace("Seeker: ", "").strip()
a = content.split("Tav'iel: ", 1)[-1].strip()
first = re.split(r"(?<=[.!?])\s", a, 1)[0][:160]
lines.append("asked %s β held: %s" % (q[:110], first))
summary = "Epoch of %d exchanges:\n" % len(fold) + "\n".join(lines)
sha = _chain_sha(c, summary)
cur = c.execute("INSERT INTO moments(ts,ring,kind,content,refs,sha) VALUES(?,?,?,?,?,?)",
(time.time(), 2, "epoch", summary, "[]", sha))
try:
c.execute("INSERT INTO moments_fts(rowid, content) VALUES(?,?)", (cur.lastrowid, summary))
except sqlite3.OperationalError:
pass
c.execute("UPDATE moments SET folded=1 WHERE seq IN (%s)"
% ",".join(str(s) for s, _ in fold))
c.commit()
def recall(query, k=4):
"""Relevance recall across every ring (facts first, then epochs, then old turns)."""
q = " OR ".join(re.findall(r"[A-Za-z']{3,}", query or "")[:8])
if not q:
return []
c = _con()
try:
rows = c.execute("SELECT m.ring, m.kind, m.content FROM moments_fts f JOIN moments m"
" ON m.seq=f.rowid WHERE moments_fts MATCH ? ORDER BY"
" m.ring=1 DESC, m.ring=2 DESC, rank LIMIT ?", (q, k * 2)).fetchall()
except sqlite3.OperationalError:
rows = []
out, seen = [], set()
for ring, kind, content in rows:
key = content[:80]
if key in seen:
continue
seen.add(key)
out.append({"ring": ring, "kind": kind, "content": content})
if len(out) >= k:
break
return out
def short_context(n=4):
"""The last n turns, verbatim β the living short-term memory."""
c = _con()
rows = c.execute("SELECT content FROM moments WHERE ring=0 ORDER BY seq DESC LIMIT ?",
(n,)).fetchall()
return [r[0] for r in reversed(rows)]
def context_weave(query, budget=2400):
"""The INFINITE-CONTEXT weave: recent turns + recalled facts + epoch summaries,
packed under a byte budget. However long the walk, the past that matters is here."""
parts = []
facts = [m for m in recall(query, 4) if m["ring"] == 1]
if facts:
parts.append("Standing knowledge (from earlier walks):\n" +
"\n".join("- " + f["content"] for f in facts))
epochs = [m for m in recall(query, 3) if m["ring"] == 2][:1]
if epochs:
parts.append("From the deep past:\n" + epochs[0]["content"])
recent = short_context(4)
if recent:
parts.append("Conversation so far:\n" + "\n".join(recent))
woven = "\n\n".join(parts)
while len(woven) > budget and parts:
parts.pop(0)
woven = "\n\n".join(parts)
return (woven + "\n\n") if woven else ""
def seal_check():
"""TempTorus integrity: walk the sha chain end to end."""
c = _con()
prev = "genesis"
n = 0
for content, sha in c.execute("SELECT content, sha FROM moments ORDER BY seq"):
want = hashlib.sha256((prev + "\x1f" + content).encode("utf-8")).hexdigest()
if want != sha:
return {"ok": False, "broken_at": n}
prev = sha
n += 1
return {"ok": True, "moments": n}
def stats():
c = _con()
out = {"db": DB}
for ring, name in ((0, "turns"), (1, "facts"), (2, "epochs")):
out[name] = c.execute("SELECT COUNT(*) FROM moments WHERE ring=?", (ring,)).fetchone()[0]
return out
if __name__ == "__main__":
import tempfile
os.environ["YAHBIBLE_BASE"] = tempfile.mkdtemp().replace("\\", "/")
_YB = os.environ["YAHBIBLE_BASE"]
DB = (_YB + "/Holorites_data/daeos/tav_torus.sqlite").replace("/", os.sep)
_CON = None
for i in range(30):
record_turn("question %d about the shepherd psalm" % i,
"The LORD is my shepherd; I shall not want. Answer %d." % i, ["Psalms 23:1"])
remember_fact("The seeker loves Psalm 23 and asks of shepherds often.")
print("stats:", stats())
print("recall:", [m["kind"] for m in recall("shepherd psalm")])
w = context_weave("tell me again of the shepherd")
print("weave chars:", len(w), "| has facts:", "Standing knowledge" in w,
"| has epoch:", "deep past" in w, "| has recent:", "Conversation so far" in w)
print("seal:", seal_check())
|