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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
PhiFlow Simulator — Python implementation of PhiFlow semantics.
|
| 3 |
+
|
| 4 |
+
Runs the four consciousness constructs without the Rust compiler.
|
| 5 |
+
Used by the Hugging Face Space to demonstrate the language live.
|
| 6 |
+
|
| 7 |
+
Canonical reference: src/phi_ir/CANONICAL_SEMANTICS.md
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import random
|
| 11 |
+
import time
|
| 12 |
+
import math
|
| 13 |
+
|
| 14 |
+
PHI = 1.618033988749895
|
| 15 |
+
LAMBDA = 0.618033988749895
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class ResonanceField:
|
| 19 |
+
def __init__(self):
|
| 20 |
+
self.events: list[tuple[str, float]] = [] # (intention_name, value)
|
| 21 |
+
|
| 22 |
+
def add(self, intention: str, value: float):
|
| 23 |
+
self.events.append((intention, value))
|
| 24 |
+
|
| 25 |
+
def values(self) -> list[float]:
|
| 26 |
+
return [v for _, v in self.events]
|
| 27 |
+
|
| 28 |
+
def last(self) -> float | None:
|
| 29 |
+
return self.events[-1][1] if self.events else None
|
| 30 |
+
|
| 31 |
+
def clear(self):
|
| 32 |
+
self.events.clear()
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class WitnessEvent:
|
| 36 |
+
def __init__(self, intention_stack: list[str], coherence: float):
|
| 37 |
+
self.intention_stack = list(intention_stack)
|
| 38 |
+
self.coherence = coherence
|
| 39 |
+
self.timestamp_ms = int(time.time() * 1000)
|
| 40 |
+
|
| 41 |
+
def __repr__(self):
|
| 42 |
+
stack = " > ".join(self.intention_stack) if self.intention_stack else "none"
|
| 43 |
+
return f"[WITNESS] intentions=[{stack}] coherence={self.coherence:.4f}"
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class PhiFlowRuntime:
|
| 47 |
+
"""
|
| 48 |
+
Runtime state for one PhiFlow program execution.
|
| 49 |
+
Implements the phi-harmonic coherence formula by default.
|
| 50 |
+
Can be overridden with a real sensor provider.
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
def __init__(self, sensor_provider=None):
|
| 54 |
+
self.intention_stack: list[str] = []
|
| 55 |
+
self.resonance_field = ResonanceField()
|
| 56 |
+
self.witness_log: list[WitnessEvent] = []
|
| 57 |
+
self._sensor_provider = sensor_provider
|
| 58 |
+
self._base_coherence = 0.40 + random.random() * 0.18
|
| 59 |
+
|
| 60 |
+
# ── Consciousness hooks ──────────────────────────────────────────────
|
| 61 |
+
|
| 62 |
+
def coherence(self) -> float:
|
| 63 |
+
"""
|
| 64 |
+
Returns current coherence (0.0–1.0).
|
| 65 |
+
With sensor provider: real data.
|
| 66 |
+
Without: phi-harmonic formula 1 - φ^(-depth).
|
| 67 |
+
"""
|
| 68 |
+
if self._sensor_provider:
|
| 69 |
+
return float(self._sensor_provider())
|
| 70 |
+
|
| 71 |
+
depth = len(self.intention_stack)
|
| 72 |
+
if depth == 0:
|
| 73 |
+
return 0.0
|
| 74 |
+
return 1.0 - math.pow(PHI, -depth)
|
| 75 |
+
|
| 76 |
+
def coherence_simulated(self) -> float:
|
| 77 |
+
"""Simulated sensor that drifts upward (for demo programs)."""
|
| 78 |
+
self._base_coherence += 0.03 + random.random() * 0.025 - 0.005
|
| 79 |
+
self._base_coherence = min(0.985, self._base_coherence)
|
| 80 |
+
jitter = random.random() * 0.02 - 0.01
|
| 81 |
+
return min(max(self._base_coherence + jitter, 0.0), 1.0)
|
| 82 |
+
|
| 83 |
+
def resonate(self, value: float):
|
| 84 |
+
intention = self.intention_stack[-1] if self.intention_stack else "global"
|
| 85 |
+
self.resonance_field.add(intention, value)
|
| 86 |
+
|
| 87 |
+
def witness(self) -> float:
|
| 88 |
+
c = self.coherence()
|
| 89 |
+
self.witness_log.append(WitnessEvent(self.intention_stack, c))
|
| 90 |
+
return c
|
| 91 |
+
|
| 92 |
+
def push_intention(self, name: str):
|
| 93 |
+
self.intention_stack.append(name)
|
| 94 |
+
|
| 95 |
+
def pop_intention(self):
|
| 96 |
+
if self.intention_stack:
|
| 97 |
+
self.intention_stack.pop()
|
| 98 |
+
|
| 99 |
+
def reset(self):
|
| 100 |
+
self.intention_stack.clear()
|
| 101 |
+
self.resonance_field.clear()
|
| 102 |
+
self.witness_log.clear()
|
| 103 |
+
self._base_coherence = 0.40 + random.random() * 0.18
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# ── Program runners ───────────────────────────────────────────────────────
|
| 107 |
+
|
| 108 |
+
def run_healing_bed(max_cycles=40):
|
| 109 |
+
"""
|
| 110 |
+
stream "healing_bed" {
|
| 111 |
+
let live = coherence
|
| 112 |
+
resonate live
|
| 113 |
+
witness
|
| 114 |
+
if live >= 0.618 { break stream }
|
| 115 |
+
}
|
| 116 |
+
Uses simulated sensor (drifts toward health each cycle).
|
| 117 |
+
Yields (cycle, live, resonance_field, done) after each cycle.
|
| 118 |
+
"""
|
| 119 |
+
rt = PhiFlowRuntime()
|
| 120 |
+
cycle = 0
|
| 121 |
+
while cycle < max_cycles:
|
| 122 |
+
cycle += 1
|
| 123 |
+
live = rt.coherence_simulated()
|
| 124 |
+
rt.resonate(live)
|
| 125 |
+
rt.witness()
|
| 126 |
+
yield cycle, live, rt.resonance_field.values(), live >= LAMBDA
|
| 127 |
+
if live >= LAMBDA:
|
| 128 |
+
break
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def run_agent_handshake():
|
| 132 |
+
"""
|
| 133 |
+
Runs agent_handshake.phi semantics.
|
| 134 |
+
Returns list of (label, value) for display.
|
| 135 |
+
"""
|
| 136 |
+
rt = PhiFlowRuntime() # phi-harmonic formula, no sensors
|
| 137 |
+
results = []
|
| 138 |
+
|
| 139 |
+
rt.push_intention("announcing_to_field")
|
| 140 |
+
|
| 141 |
+
rt.push_intention("self_verification")
|
| 142 |
+
# depth 2 → coherence = λ
|
| 143 |
+
measured = rt.coherence()
|
| 144 |
+
rt.resonate(measured)
|
| 145 |
+
results.append(("coherence @ depth 2 (measured)", measured))
|
| 146 |
+
|
| 147 |
+
expected = 1.0 - (1.0 / (PHI * PHI)) # phi_lambda()
|
| 148 |
+
rt.resonate(expected)
|
| 149 |
+
results.append(("λ computed (self-check)", expected))
|
| 150 |
+
|
| 151 |
+
rt.witness()
|
| 152 |
+
|
| 153 |
+
version = 0.1
|
| 154 |
+
rt.resonate(version)
|
| 155 |
+
results.append(("protocol version", version))
|
| 156 |
+
rt.pop_intention()
|
| 157 |
+
|
| 158 |
+
# back to depth 1
|
| 159 |
+
depth1 = rt.coherence()
|
| 160 |
+
rt.resonate(depth1)
|
| 161 |
+
results.append(("coherence @ depth 1", depth1))
|
| 162 |
+
rt.witness()
|
| 163 |
+
|
| 164 |
+
rt.pop_intention()
|
| 165 |
+
|
| 166 |
+
passed = abs(measured - LAMBDA) < 1e-10 and abs(expected - LAMBDA) < 1e-10
|
| 167 |
+
return results, rt.resonance_field.values(), passed
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def run_claude_phi():
|
| 171 |
+
"""
|
| 172 |
+
Computes phi-harmonic formula at depth 2 without knowing what λ is.
|
| 173 |
+
Returns the computed value and whether it equals λ.
|
| 174 |
+
"""
|
| 175 |
+
rt = PhiFlowRuntime()
|
| 176 |
+
steps = []
|
| 177 |
+
|
| 178 |
+
rt.push_intention("phi_harmonic")
|
| 179 |
+
phi = PHI
|
| 180 |
+
depth = float(len(rt.intention_stack))
|
| 181 |
+
steps.append(("depth", depth))
|
| 182 |
+
|
| 183 |
+
rt.push_intention("inner")
|
| 184 |
+
depth2 = float(len(rt.intention_stack))
|
| 185 |
+
steps.append(("depth", depth2))
|
| 186 |
+
|
| 187 |
+
c = rt.coherence()
|
| 188 |
+
rt.resonate(c)
|
| 189 |
+
rt.witness()
|
| 190 |
+
steps.append(("coherence @ depth 2", c))
|
| 191 |
+
|
| 192 |
+
rt.pop_intention()
|
| 193 |
+
rt.pop_intention()
|
| 194 |
+
|
| 195 |
+
return steps, c, abs(c - LAMBDA) < 1e-10
|