#!/usr/bin/env python3 """ ingest_eigensolid_data.py - Design and implement database shims for eigensolid snapshots and weights. Runs a simulated 8-strand braid eigensolid convergence loop, computes Q16_16 fixed-point metrics (entropy, convergence_metric), and outputs SQL insert statements or populates database records in the ENE substrate schema. NO float coordinates or float arithmetic are used in the core calculations. """ from __future__ import annotations import sys import uuid import json from typing import List, Tuple, Dict, Any, Optional # Q16_16 Scaling constants Q16_SCALE = 65536 RAW_MIN = -(2 ** 31) RAW_MAX = (2 ** 31) - 1 def q16_clamp(val: int) -> int: return max(RAW_MIN, min(RAW_MAX, val)) def q16_add(a: int, b: int) -> int: return q16_clamp(a + b) def q16_sub(a: int, b: int) -> int: return q16_clamp(a - b) def q16_mul(a: int, b: int) -> int: return q16_clamp((a * b) // Q16_SCALE) def q16_div(a: int, b: int) -> int: if b == 0: return 0 return q16_clamp((a * Q16_SCALE) // b) def q16_log2(x: int) -> int: """Computes log2(x) in Q16_16 format where x is a raw Q16_16 integer (> 0).""" if x <= 0: return 0 shift = 0 temp = x if temp >= 65536: while temp >= 131072: temp >>= 1 shift += 1 else: while temp < 65536: temp <<= 1 shift -= 1 diff = temp - 65536 # Taylor approximation for log2(1 + y) on [0, 1) in Q16_16 fraction = diff - (diff * diff) // 131072 return (shift * 65536) + fraction class Strand: def __init__(self, strand_index: int, phase_x: int, phase_y: int): self.strand_index = strand_index self.phase_x = phase_x self.phase_y = phase_y def copy(self) -> Strand: return Strand(self.strand_index, self.phase_x, self.phase_y) class BraidState: def __init__(self, strands: List[Strand]): self.strands = sorted(strands, key=lambda s: s.strand_index) def copy(self) -> BraidState: return BraidState([s.copy() for s in self.strands]) def simulate_crossing_step(state: BraidState, weights: List[List[int]], contractive_factor: int) -> BraidState: """ Applies crossing step dynamics to the 8-strand braid state in pure Q16_16. Strand crossings update their x and y phases using weights and contractive factor. """ next_strands = [s.copy() for s in state.strands] # We update adjacent pairs (0,1), (2,3), (4,5), (6,7) for pair_idx in range(4): idx1 = pair_idx * 2 idx2 = idx1 + 1 s1 = state.strands[idx1] s2 = state.strands[idx2] # Get crossing weight for this pair w = weights[idx1][idx2] # x_new = c * x_old + w * y_partner # y_new = c * y_old + w * x_partner next_strands[idx1].phase_x = q16_add(q16_mul(contractive_factor, s1.phase_x), q16_mul(w, s2.phase_y)) next_strands[idx1].phase_y = q16_add(q16_mul(contractive_factor, s1.phase_y), q16_mul(w, s2.phase_x)) next_strands[idx2].phase_x = q16_add(q16_mul(contractive_factor, s2.phase_x), q16_mul(w, s1.phase_y)) next_strands[idx2].phase_y = q16_add(q16_mul(contractive_factor, s2.phase_y), q16_mul(w, s1.phase_x)) return BraidState(next_strands) def compute_entropy(state: BraidState) -> int: """Computes fixed-point Q16_16 entropy based on phase amplitudes.""" total_amplitude = 0 amplitudes = [] for s in state.strands: # L1 norm of phase coordinates as amplitude proxy amp = q16_add(abs(s.phase_x), abs(s.phase_y)) amplitudes.append(amp) total_amplitude = q16_add(total_amplitude, amp) if total_amplitude == 0: return 0 entropy = 0 for amp in amplitudes: if amp == 0: continue p = q16_div(amp, total_amplitude) log2_p = q16_log2(p) entropy = q16_sub(entropy, q16_mul(p, log2_p)) return max(0, entropy) def run_convergence_loop( initial_state: BraidState, weights: List[List[int]], contractive_factor: int, max_steps: int = 50, tolerance: int = 655 # ~0.01 in Q16_16 ) -> Tuple[BraidState, int, int, bool]: """ Simulates convergence of the braid crossing dynamics. Returns: (final_state, step_count, convergence_metric, is_stable) """ curr = initial_state.copy() step_count = 0 is_stable = False for step in range(max_steps): nxt = simulate_crossing_step(curr, weights, contractive_factor) step_count += 1 # Check difference max_diff = 0 for i in range(8): dx = abs(q16_sub(nxt.strands[i].phase_x, curr.strands[i].phase_x)) dy = abs(q16_sub(nxt.strands[i].phase_y, curr.strands[i].phase_y)) max_diff = max(max_diff, dx, dy) curr = nxt if max_diff < tolerance: is_stable = True break # Convergence metric is 1.0 - clamp(max_diff / tolerance) scaled to Q16_16 # If stable, metric is close to 65536. Let's compute it. if step_count == 0: metric = 65536 else: # metric = clamp(1 - max_diff / 65536) metric = max(0, min(65536, 65536 - max_diff)) return curr, step_count, metric, is_stable # ── SQL Insert Generators ─────────────────────────────────────────────────── def generate_crossing_weights_insert(recipe_id: str, row_index: int, col_index: int, weight_raw: int, contractive_factor: int) -> str: cw_id = str(uuid.uuid4()) return ( f"INSERT INTO ene.crossing_weights (id, recipe_id, row_index, col_index, weight_raw, contractive_factor) " f"VALUES ('{cw_id}', '{recipe_id}', {row_index}, {col_index}, {weight_raw}, {contractive_factor}) " f"ON CONFLICT (recipe_id, row_index, col_index) DO UPDATE " f"SET weight_raw = EXCLUDED.weight_raw, contractive_factor = EXCLUDED.contractive_factor;" ) def generate_eigensolid_snapshot_insert(snapshot_id: str, package_id: str, receipt_id: Optional[str], step_count: int, convergence_metric: int, entropy: int, is_stable: bool) -> str: receipt_val = f"'{receipt_id}'" if receipt_id else "NULL" stable_val = "true" if is_stable else "false" return ( f"INSERT INTO ene.eigensolid_snapshots (id, package_id, receipt_id, step_count, convergence_metric, entropy, is_stable) " f"VALUES ('{snapshot_id}', '{package_id}', {receipt_val}, {step_count}, {convergence_metric}, {entropy}, {stable_val});" ) def generate_braid_strand_insert(snapshot_id: str, strand_index: int, phase_x: int, phase_y: int) -> str: bs_id = str(uuid.uuid4()) return ( f"INSERT INTO ene.braid_strands (id, snapshot_id, strand_index, phase_x, phase_y) " f"VALUES ('{bs_id}', '{snapshot_id}', {strand_index}, {phase_x}, {phase_y}) " f"ON CONFLICT (snapshot_id, strand_index) DO UPDATE " f"SET phase_x = EXCLUDED.phase_x, phase_y = EXCLUDED.phase_y;" ) # ── DB Population Functions ────────────────────────────────────────────────── def insert_eigensolid_data( conn, package_id: str, recipe_id: str, weights: List[List[int]], contractive_factor: int, initial_state: BraidState ) -> Tuple[str, List[str]]: """ Runs the simulation, inserts the records via pg connection, and returns (snapshot_id, inserted_queries). """ final_state, step_count, convergence_metric, is_stable = run_convergence_loop( initial_state, weights, contractive_factor ) entropy = compute_entropy(final_state) snapshot_id = str(uuid.uuid4()) queries = [] cur = conn.cursor() # 1. Insert snapshot snap_sql = generate_eigensolid_snapshot_insert( snapshot_id=snapshot_id, package_id=package_id, receipt_id=recipe_id, step_count=step_count, convergence_metric=convergence_metric, entropy=entropy, is_stable=is_stable ) cur.execute( """INSERT INTO ene.eigensolid_snapshots (id, package_id, receipt_id, step_count, convergence_metric, entropy, is_stable) VALUES (%s, %s, %s, %s, %s, %s, %s)""", (snapshot_id, package_id, recipe_id, step_count, convergence_metric, entropy, is_stable) ) queries.append(snap_sql) # 2. Insert braid strands for s in final_state.strands: bs_sql = generate_braid_strand_insert(snapshot_id, s.strand_index, s.phase_x, s.phase_y) cur.execute( """INSERT INTO ene.braid_strands (id, snapshot_id, strand_index, phase_x, phase_y) VALUES (%s, %s, %s, %s, %s) ON CONFLICT (snapshot_id, strand_index) DO UPDATE SET phase_x = EXCLUDED.phase_x, phase_y = EXCLUDED.phase_y""", (str(uuid.uuid4()), snapshot_id, s.strand_index, s.phase_x, s.phase_y) ) queries.append(bs_sql) # 3. Insert crossing weights for r in range(8): for c in range(8): w = weights[r][c] cw_sql = generate_crossing_weights_insert(recipe_id, r, c, w, contractive_factor) cur.execute( """INSERT INTO ene.crossing_weights (id, recipe_id, row_index, col_index, weight_raw, contractive_factor) VALUES (%s, %s, %s, %s, %s, %s) ON CONFLICT (recipe_id, row_index, col_index) DO UPDATE SET weight_raw = EXCLUDED.weight_raw, contractive_factor = EXCLUDED.contractive_factor""", (str(uuid.uuid4()), recipe_id, r, c, w, contractive_factor) ) queries.append(cw_sql) conn.commit() return snapshot_id, queries # ── Main / Simulation execution ────────────────────────────────────────────── def main() -> int: print("--- Eigensolid DB Integration Shim ---") # Mocking input variables package_id = "pkg_eigensolid_mock_01" recipe_id = "rec_eigensolid_mock_01" # Define an 8x8 weight matrix in Q16_16 # e.g., diagonal/adjacent elements have weights, others are 0 weights = [[0] * 8 for _ in range(8)] for i in range(7): weights[i][i+1] = 16384 # 0.25 in Q16_16 weights[i+1][i] = 16384 contractive_factor = 32768 # 0.5 in Q16_16 # Initialize strands with mock Q16_16 coordinates initial_strands = [] for i in range(8): initial_strands.append(Strand( strand_index=i, phase_x=(i + 1) * 8192, # starts around 0.125 to 1.0 phase_y=(8 - i) * 8192 )) initial_state = BraidState(initial_strands) # Run the convergence simulation final_state, step_count, convergence_metric, is_stable = run_convergence_loop( initial_state, weights, contractive_factor ) entropy = compute_entropy(final_state) print("\nSimulation Results:") print(f" Step Count to Stable/End: {step_count}") print(f" Is Stable: {is_stable}") print(f" Convergence Metric: {convergence_metric} (Q16_16: {convergence_metric / Q16_SCALE:.4f})") print(f" Entropy: {entropy} (Q16_16: {entropy / Q16_SCALE:.4f})") # Output generated SQL insert statements print("\nGenerated SQL Statements:") snapshot_id = str(uuid.uuid4()) print(generate_eigensolid_snapshot_insert(snapshot_id, package_id, recipe_id, step_count, convergence_metric, entropy, is_stable)) for s in final_state.strands: print(generate_braid_strand_insert(snapshot_id, s.strand_index, s.phase_x, s.phase_y)) for r in range(8): for c in range(8): w = weights[r][c] if w != 0: print(generate_crossing_weights_insert(recipe_id, r, c, w, contractive_factor)) return 0 if __name__ == "__main__": sys.exit(main())