File size: 10,679 Bytes
9abace2 | 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 | """
Paradigm 2: Functional Seed Continuity
Part of SOVEREIGN PYTHON LLM ENGINE
Instead of storing what happened, store HOW TO GET THERE.
Core idea:
- Every agent state transition is a deterministic function of a seed
- History compresses to: (seed: int, step: int)
- To restore state at step N: evaluate f(seed, 0..N)
- Entire ledger = one 64-bit integer + one 64-bit counter
Two components:
SeedChain:
Deterministic PRNG chain. Each step derives next state from
current state via BLAKE2b(seed || step || operation).
Output is a 256-bit hash used as the next state vector.
SeedLedger:
Minimal persistence β stores only (seed, step, checksum) in
a 24-byte binary flat file. No JSONL, no struct chains.
Recovery = re-derive from step 0 to current step.
OperationLog:
Stores the SEQUENCE of operations (not their full state).
Operations are short codes: "THINK", "ACT:tool_id", "OBS", etc.
Combined with seed, fully deterministic replay is possible
for pure-math agents (routing pipeline, constraint eval, etc.)
Caveat (Ahmad's note):
Works perfectly for deterministic subsystems:
- MoE routing pipeline (pure math)
- Constraint evaluation
- Symbolic graph / Jordan analysis
- State machine transitions
For LLM calls (non-deterministic):
Cache model outputs alongside the operation log.
Seed continuity handles everything else;
model outputs are the only non-reproducible artifact.
"""
from __future__ import annotations
import hashlib
import struct
import time
from dataclasses import dataclass, field
from pathlib import Path
from typing import Iterator
# βββββββββββββββββββββββββββββββββββββββββββββ
# Seed derivation
# βββββββββββββββββββββββββββββββββββββββββββββ
def _derive(seed: int, step: int, op: bytes) -> bytes:
"""
Derive next state hash from seed + step + operation.
Pure function β same inputs always produce same output.
"""
h = hashlib.blake2b(
struct.pack('>QQ', seed, step) + op,
digest_size=32
)
return h.digest()
def _derive_seed(seed: int, step: int, op: bytes) -> int:
"""Derive next seed integer."""
raw = _derive(seed, step, op)
return struct.unpack('>Q', raw[:8])[0]
# βββββββββββββββββββββββββββββββββββββββββββββ
# Seed Chain
# βββββββββββββββββββββββββββββββββββββββββββββ
@dataclass
class ChainState:
seed: int
step: int
state_hash: bytes # 32-byte current state vector
op: str # operation that produced this state
class SeedChain:
"""
Deterministic state chain driven by a single 64-bit seed.
Each transition:
new_state = BLAKE2b(current_seed || step || op_bytes)
The chain is:
- Deterministic: same seed + same ops = same history
- Compact: full history = (initial_seed, [ops])
- Verifiable: any step can be recomputed from scratch
- Fork-able: different op sequences from same seed = different timelines
Usage:
chain = SeedChain(seed=0xDEADBEEFCAFEBABE)
chain.advance("THINK")
chain.advance("ACT:filesystem.read")
chain.advance("OBS")
# Serialize to 8 bytes
seed_bytes = chain.export_seed()
# Restore β re-derive to current step
chain2 = SeedChain.from_seed(seed_bytes, ops=chain.ops)
assert chain2.current_hash == chain.current_hash
"""
def __init__(self, seed: int | None = None):
if seed is None:
seed = struct.unpack('>Q', hashlib.blake2b(
struct.pack('>Q', time.time_ns()), digest_size=8
).digest())[0]
self._initial_seed = seed
self._seed = seed
self._step = 0
self._state_hash = _derive(seed, 0, b'INIT')
self._ops: list[str] = []
def advance(self, op: str) -> ChainState:
op_bytes = op.encode('utf-8')
self._seed = _derive_seed(self._seed, self._step, op_bytes)
self._step += 1
self._state_hash = _derive(self._seed, self._step, op_bytes)
self._ops.append(op)
return ChainState(
seed=self._seed,
step=self._step,
state_hash=self._state_hash,
op=op
)
def state_at(self, step: int) -> bytes:
"""Recompute state hash at a specific step (O(step) recomputation)."""
seed = self._initial_seed
state = _derive(seed, 0, b'INIT')
for i, op in enumerate(self._ops[:step]):
op_bytes = op.encode('utf-8')
seed = _derive_seed(seed, i, op_bytes)
state = _derive(seed, i + 1, op_bytes)
return state
def fork(self, from_step: int | None = None) -> 'SeedChain':
"""
Create a new chain branching from a given step.
The fork shares history up to from_step, then diverges.
"""
step = from_step if from_step is not None else self._step
new_chain = SeedChain(seed=self._initial_seed)
for op in self._ops[:step]:
new_chain.advance(op)
return new_chain
def export_seed(self) -> bytes:
"""Export initial seed as 8 bytes."""
return struct.pack('>Q', self._initial_seed)
def export_compact(self) -> bytes:
"""
Export minimal recovery payload: initial_seed(8) + step(8).
Ops must be stored separately to enable full replay.
"""
return struct.pack('>QQ', self._initial_seed, self._step)
@classmethod
def from_seed(cls, seed_bytes: bytes, ops: list[str] | None = None) -> 'SeedChain':
seed = struct.unpack('>Q', seed_bytes[:8])[0]
chain = cls(seed=seed)
for op in (ops or []):
chain.advance(op)
return chain
@property
def current_hash(self) -> bytes:
return self._state_hash
@property
def current_seed(self) -> int:
return self._seed
@property
def step(self) -> int:
return self._step
@property
def ops(self) -> list[str]:
return list(self._ops)
@property
def initial_seed(self) -> int:
return self._initial_seed
# βββββββββββββββββββββββββββββββββββββββββββββ
# Seed Ledger β 24-byte flat file
# βββββββββββββββββββββββββββββββββββββββββββββ
# Layout: initial_seed(8) | current_step(8) | checksum(8)
SEED_LEDGER_SIZE = 24
SEED_LEDGER_FMT = '>QQQ'
class SeedLedger:
"""
Minimal persistence for seed continuity.
Stores only 24 bytes on disk:
- initial_seed (8 bytes)
- current_step (8 bytes)
- checksum (8 bytes, first 8 of BLAKE2b(seed || step))
Recovery: read seed + step, re-derive from ops log.
The ops log is stored separately (OpLog) or reconstructed
from the WORM binary ledger.
"""
def __init__(self, path: Path):
self.path = path
self.path.parent.mkdir(parents=True, exist_ok=True)
def save(self, chain: SeedChain) -> None:
seed = chain.initial_seed
step = chain.step
checksum_raw = hashlib.blake2b(
struct.pack('>QQ', seed, step), digest_size=8
).digest()
checksum = struct.unpack('>Q', checksum_raw)[0]
with open(self.path, 'wb') as f:
f.write(struct.pack(SEED_LEDGER_FMT, seed, step, checksum))
def load(self) -> tuple[int, int] | None:
"""
Returns (initial_seed, step) or None if file missing/corrupt.
"""
if not self.path.exists():
return None
try:
raw = self.path.read_bytes()
if len(raw) < SEED_LEDGER_SIZE:
return None
seed, step, stored_checksum = struct.unpack(SEED_LEDGER_FMT, raw[:24])
# Verify checksum
expected_raw = hashlib.blake2b(
struct.pack('>QQ', seed, step), digest_size=8
).digest()
expected = struct.unpack('>Q', expected_raw)[0]
if stored_checksum != expected:
return None
return seed, step
except Exception:
return None
def file_size(self) -> int:
return SEED_LEDGER_SIZE # always 24 bytes
# βββββββββββββββββββββββββββββββββββββββββββββ
# Operation Log β compact op sequence store
# βββββββββββββββββββββββββββββββββββββββββββββ
class OpLog:
"""
Append-only log of operation codes.
Stored as length-prefixed UTF-8 strings in a binary flat file.
Format per entry:
length(2 bytes uint16 BE) | op_string(utf-8)
Combined with SeedLedger, enables full deterministic replay.
"""
def __init__(self, path: Path):
self.path = path
self.path.parent.mkdir(parents=True, exist_ok=True)
def append(self, op: str) -> None:
op_bytes = op.encode('utf-8')
with open(self.path, 'ab') as f:
f.write(struct.pack('>H', len(op_bytes)))
f.write(op_bytes)
def read_all(self) -> list[str]:
if not self.path.exists():
return []
ops = []
with open(self.path, 'rb') as f:
while True:
length_bytes = f.read(2)
if not length_bytes or len(length_bytes) < 2:
break
length = struct.unpack('>H', length_bytes)[0]
op_bytes = f.read(length)
if len(op_bytes) < length:
break
ops.append(op_bytes.decode('utf-8', errors='replace'))
return ops
def read_up_to(self, step: int) -> list[str]:
return self.read_all()[:step]
def count(self) -> int:
return len(self.read_all())
|