#!/usr/bin/env python3 """path_hrr_v1 - the network's PATH MEMORY in the substrate's own algebra (phase 4, HRR form). Codebook (same construction as the recall index and phase0_baseline_v1): osub_seed = sha256-seeded unit phasors, D=1024. op key K[c] = osub_seed("o7op:%d" % c) - one key per op code c (0..255; 0xFE = core math) role R[p] = osub_seed("o7pos:%d" % p) - one role per path position PATH = sum_p bind(K[c_p], R[p]) - a whole path is ONE vector (validated: 4-hop recovery 100% at D=1024) SHAPE = superpose of osub_seed(token) over the question's shape tokens (goal signature + given signatures) store(shape, path): keeps (SHAPE, PATH, length). recall(shape): the stored entry whose SHAPE resonates most with the query's (partial cue - a new question whose shape overlaps a stored one retrieves that path), then unbind each position and clean up against the op codebook. Retrieval only ORDERS the plans the network tries; every answer still has to pass every check, so memory can never make an answer wrong, only find a proven path first. """ from __future__ import annotations import hashlib import numpy as np D = 1024 def osub_seed(text: str, dim: int = D) -> np.ndarray: h = int(hashlib.sha256(text.lower().strip().encode()).hexdigest(), 16) rng = np.random.RandomState(h % (2 ** 31)) return np.exp(1j * rng.uniform(0, 2 * np.pi, dim)).astype(np.complex64) def bind(a, b): return np.fft.ifft(np.fft.fft(a) * np.fft.fft(b)).astype(np.complex64) def unbind(c, b): return np.fft.ifft(np.fft.fft(c) * np.conj(np.fft.fft(b))).astype(np.complex64) class PathHRR: def __init__(self, n_codes: int = 256, max_len: int = 8): self.K = np.stack([osub_seed("o7op:%d" % c) for c in range(n_codes)]) self.R = [osub_seed("o7pos:%d" % p) for p in range(max_len)] self.entries: list[tuple[np.ndarray, np.ndarray, int]] = [] def shape_vec(self, tokens): vs = [osub_seed("o7shape:" + t) for t in tokens] if not vs: return None v = np.mean(np.stack(vs), axis=0) return (v / np.maximum(np.abs(v), 1e-8)).astype(np.complex64) def path_vec(self, codes): return np.sum([bind(self.K[c], self.R[p]) for p, c in enumerate(codes)], axis=0).astype(np.complex64) def decode(self, vec, length): out = [] for p in range(length): est = unbind(vec, self.R[p]) sims = np.real(self.K.conj() @ est) / D out.append(int(np.argmax(sims))) return out def store(self, tokens, codes): sv = self.shape_vec(tokens) if sv is None or not codes: return self.entries.append((sv, self.path_vec(codes), len(codes))) def recall(self, tokens, min_resonance: float = 0.35): """-> (codes, resonance) of the most resonant stored shape, or (None, best) below the threshold""" sv = self.shape_vec(tokens) if sv is None or not self.entries: return None, 0.0 best, arg = -1.0, None for i, (s, _, _) in enumerate(self.entries): r = float(np.real(np.vdot(s, sv)) / D) if r > best: best, arg = r, i if best < min_resonance: return None, best _, pv, n = self.entries[arg] return self.decode(pv, n), best