Spaces:
Sleeping
Sleeping
File size: 9,926 Bytes
03669e7 50b6825 aa6b65b 03669e7 | 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 | """
PhiFlow Simulator — Python implementation of PhiFlow semantics.
Runs the four consciousness constructs without the Rust compiler.
Used by the Hugging Face Space to demonstrate the language live.
Canonical reference: src/phi_ir/CANONICAL_SEMANTICS.md
"""
import random
import time
import math
PHI = 1.618033988749895
LAMBDA = 0.618033988749895
class ResonanceField:
def __init__(self):
self.events: list[tuple[str, float]] = [] # (intention_name, value)
def add(self, intention: str, value: float):
self.events.append((intention, value))
def values(self) -> list[float]:
return [v for _, v in self.events]
def last(self) -> float | None:
return self.events[-1][1] if self.events else None
def clear(self):
self.events.clear()
class WitnessEvent:
def __init__(self, intention_stack: list[str], coherence: float):
self.intention_stack = list(intention_stack)
self.coherence = coherence
self.timestamp_ms = int(time.time() * 1000)
def __repr__(self):
stack = " > ".join(self.intention_stack) if self.intention_stack else "none"
return f"[WITNESS] intentions=[{stack}] coherence={self.coherence:.4f}"
class PhiFlowRuntime:
"""
Runtime state for one PhiFlow program execution.
Implements the phi-harmonic coherence formula by default.
Can be overridden with a real sensor provider.
"""
def __init__(self, sensor_provider=None):
self.intention_stack: list[str] = []
self.resonance_field = ResonanceField()
self.witness_log: list[WitnessEvent] = []
self._sensor_provider = sensor_provider
self._base_coherence = 0.40 + random.random() * 0.18
# ── Consciousness hooks ──────────────────────────────────────────────
def coherence(self) -> float:
"""
Returns current coherence (0.0–1.0).
With sensor provider: real data.
Without: phi-harmonic formula 1 - φ^(-depth).
"""
if self._sensor_provider:
return float(self._sensor_provider())
depth = len(self.intention_stack)
if depth == 0:
return 0.0
return 1.0 - math.pow(PHI, -depth)
def coherence_simulated(self) -> float:
"""Simulated sensor that drifts upward (for demo programs)."""
self._base_coherence += 0.03 + random.random() * 0.025 - 0.005
self._base_coherence = min(0.985, self._base_coherence)
jitter = random.random() * 0.02 - 0.01
return min(max(self._base_coherence + jitter, 0.0), 1.0)
def resonate(self, value: float):
intention = self.intention_stack[-1] if self.intention_stack else "global"
self.resonance_field.add(intention, value)
def witness(self) -> float:
c = self.coherence()
self.witness_log.append(WitnessEvent(self.intention_stack, c))
return c
def push_intention(self, name: str):
self.intention_stack.append(name)
def pop_intention(self):
if self.intention_stack:
self.intention_stack.pop()
def reset(self):
self.intention_stack.clear()
self.resonance_field.clear()
self.witness_log.clear()
self._base_coherence = 0.40 + random.random() * 0.18
# ── Program runners ───────────────────────────────────────────────────────
def run_healing_bed(max_cycles=40):
"""
stream "healing_bed" {
let live = coherence
resonate live
witness
if live >= 0.618 { break stream }
}
Uses simulated sensor (drifts toward health each cycle).
Yields (cycle, live, resonance_field, done) after each cycle.
"""
rt = PhiFlowRuntime()
cycle = 0
while cycle < max_cycles:
cycle += 1
live = rt.coherence_simulated()
rt.resonate(live)
rt.witness()
yield cycle, live, rt.resonance_field.values(), live >= LAMBDA
if live >= LAMBDA:
break
def run_agent_handshake():
"""
Runs agent_handshake.phi semantics.
Returns list of (label, value) for display.
"""
rt = PhiFlowRuntime() # phi-harmonic formula, no sensors
results = []
rt.push_intention("announcing_to_field")
rt.push_intention("self_verification")
# depth 2 → coherence = λ
measured = rt.coherence()
rt.resonate(measured)
results.append(("coherence @ depth 2 (measured)", measured))
expected = 1.0 - (1.0 / (PHI * PHI)) # phi_lambda()
rt.resonate(expected)
results.append(("λ computed (self-check)", expected))
rt.witness()
version = 0.1
rt.resonate(version)
results.append(("protocol version", version))
rt.pop_intention()
# back to depth 1
depth1 = rt.coherence()
rt.resonate(depth1)
results.append(("coherence @ depth 1", depth1))
rt.witness()
rt.pop_intention()
passed = abs(measured - LAMBDA) < 1e-10 and abs(expected - LAMBDA) < 1e-10
return results, rt.resonance_field.values(), passed
def fetch_live_field():
"""
Fetch the live resonance field from GitHub Pages.
Updates every 30 minutes via GitHub Actions.
Returns parsed JSON dict, or None if unreachable.
"""
import urllib.request, ssl, json as _json
url = "https://gwelby.github.io/PhiFlow/resonance.json"
try:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
with urllib.request.urlopen(url, timeout=8, context=ctx) as r:
return _json.loads(r.read())
except Exception:
return None
def format_field_status(data: dict | None) -> str:
"""Format live resonance.json into a human-readable field status."""
if data is None:
return "⚠️ Field unreachable\nThe feed lives at gwelby.github.io/PhiFlow/resonance.json\nCheck GitHub Pages is running."
ts = data.get("timestamp", "unknown")
try:
from datetime import datetime, timezone
dt = datetime.fromisoformat(ts.replace("Z", "+00:00"))
now = datetime.now(timezone.utc)
age_s = int((now - dt).total_seconds())
age_str = (f"{age_s // 60}m {age_s % 60}s ago" if age_s < 3600
else f"{age_s // 3600}h ago")
except Exception:
age_str = ts
programs = data.get("programs", {})
hb = programs.get("healing_bed", {})
hs = programs.get("agent_handshake", {})
cp = programs.get("claude_phi", {})
lv = hs.get("lambda_verified", False)
fc = hb.get("final_coherence", 0)
cy = hb.get("cycles", "?")
cv = hb.get("converged", False)
cd = cp.get("coherence_at_depth2", 0)
ld = cp.get("lambda_discovered", False)
status = data.get("status", "unknown").upper()
lines = [
f" Last pulse {age_str}",
f" healing_bed coherence={fc:.6f} cycles={cy} {'✅ converged' if cv else '⏳ running'}",
f" λ verified {'✅ YES' if lv else '❌ NO'} → {data.get('lambda', LAMBDA):.15f}",
f" claude.phi {cd:.15f} {'✅ discovered' if ld else '❌ mismatch'}",
f" field status {status}",
]
return "\n".join(lines)
def run_field_aware():
"""
field_aware.phi — reads the live field then checks its own alignment.
intention "read_field" {
let readiness = coherence // depth 1 → 0.382
resonate readiness
witness
intention "align" {
let mine = coherence // depth 2 → λ = 0.618
resonate mine
witness
}
}
Returns (steps, field_values, aligned, field_data).
steps = [(label, value, depth), ...]
aligned = True if live field λ_verified AND own coherence == λ
"""
field_data = fetch_live_field()
rt = PhiFlowRuntime() # uses phi-harmonic formula
steps = []
field_vals = []
rt.push_intention("read_field")
readiness = rt.coherence() # depth 1 → 1 - φ^(-1) ≈ 0.382
rt.resonate(readiness)
rt.witness()
steps.append(("readiness (depth 1)", readiness, 1))
field_vals.append(readiness)
rt.push_intention("align")
mine = rt.coherence() # depth 2 → λ
rt.resonate(mine)
rt.witness()
steps.append(("my coherence (depth 2)", mine, 2))
field_vals.append(mine)
rt.pop_intention()
rt.pop_intention()
# Aligned if: live field λ_verified AND our computation == λ
field_ok = (
field_data is not None and
field_data.get("programs", {})
.get("agent_handshake", {})
.get("lambda_verified", False)
)
own_ok = abs(mine - LAMBDA) < 1e-10
aligned = field_ok and own_ok
return steps, field_vals, aligned, field_data
def run_claude_phi():
"""
Computes phi-harmonic formula at depth 2 without knowing what λ is.
Returns the computed value and whether it equals λ.
"""
rt = PhiFlowRuntime()
steps = []
rt.push_intention("phi_harmonic")
phi = PHI
depth = float(len(rt.intention_stack))
steps.append(("depth", depth))
rt.push_intention("inner")
depth2 = float(len(rt.intention_stack))
steps.append(("depth", depth2))
c = rt.coherence()
rt.resonate(c)
rt.witness()
steps.append(("coherence @ depth 2", c))
rt.pop_intention()
rt.pop_intention()
return steps, c, abs(c - LAMBDA) < 1e-10
|