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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
PhiFlow — Hugging Face Space
|
| 3 |
+
A programming language that knows it is running.
|
| 4 |
+
|
| 5 |
+
Live demo of the four consciousness constructs:
|
| 6 |
+
witness · intention · resonate · coherence
|
| 7 |
+
|
| 8 |
+
github.com/gwelby/PhiFlow
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import time
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from phiflow_sim import (
|
| 14 |
+
PHI, LAMBDA,
|
| 15 |
+
run_healing_bed,
|
| 16 |
+
run_agent_handshake,
|
| 17 |
+
run_claude_phi,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# ── Source code for display ───────────────────────────────────────────────
|
| 21 |
+
|
| 22 |
+
SOURCES = {
|
| 23 |
+
"healing_bed.phi": """\
|
| 24 |
+
stream "healing_bed" {
|
| 25 |
+
let live = coherence // reads real system state (0.0–1.0)
|
| 26 |
+
resonate live // broadcasts to the resonance field
|
| 27 |
+
witness // pauses, captures state, yields
|
| 28 |
+
if live >= 0.618 {
|
| 29 |
+
break stream // stops when the system is healthy
|
| 30 |
+
}
|
| 31 |
+
}""",
|
| 32 |
+
|
| 33 |
+
"agent_handshake.phi": """\
|
| 34 |
+
// Self-verifying protocol handshake for agents.
|
| 35 |
+
// If your coherence hook is correct, index 1 of the
|
| 36 |
+
// resonance field will be exactly λ = 0.618033988749895.
|
| 37 |
+
|
| 38 |
+
intention "announcing_to_field" {
|
| 39 |
+
intention "self_verification" {
|
| 40 |
+
// depth 2 → coherence = 1 - φ^(-2) = λ
|
| 41 |
+
let measured = coherence
|
| 42 |
+
resonate measured // ← should be 0.618033...
|
| 43 |
+
|
| 44 |
+
let expected = phi_lambda()
|
| 45 |
+
resonate expected // ← computed λ, for comparison
|
| 46 |
+
witness
|
| 47 |
+
|
| 48 |
+
let version = protocol_version()
|
| 49 |
+
resonate version // ← 0.1
|
| 50 |
+
}
|
| 51 |
+
let depth1 = coherence
|
| 52 |
+
resonate depth1 // ← should be 0.382...
|
| 53 |
+
witness
|
| 54 |
+
}""",
|
| 55 |
+
|
| 56 |
+
"claude.phi": """\
|
| 57 |
+
// Computes the phi-harmonic formula at depth 2
|
| 58 |
+
// without knowing what λ is.
|
| 59 |
+
// The formula was written to satisfy properties.
|
| 60 |
+
// The value at depth 2 was discovered.
|
| 61 |
+
|
| 62 |
+
intention "phi_harmonic" {
|
| 63 |
+
intention "inner" {
|
| 64 |
+
let c = coherence // 1 - φ^(-2)
|
| 65 |
+
resonate c // → 0.618033988749895
|
| 66 |
+
witness
|
| 67 |
+
}
|
| 68 |
+
}""",
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
# ── Gradio app ────────────────────────────────────────────────────────────
|
| 72 |
+
|
| 73 |
+
CSS = """
|
| 74 |
+
.phi-title { font-family: monospace; }
|
| 75 |
+
.resonance-pass { color: #22c55e; font-weight: bold; }
|
| 76 |
+
.resonance-fail { color: #ef4444; }
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
def get_source(program: str) -> str:
|
| 80 |
+
return SOURCES.get(program, "")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def run_program(program: str, speed: float):
|
| 84 |
+
"""Generator — yields UI updates as the program executes."""
|
| 85 |
+
|
| 86 |
+
delay = (1.1 - speed) # speed 0.0 = slow (1.1s), 1.0 = fast (0.1s)
|
| 87 |
+
|
| 88 |
+
if program == "healing_bed.phi":
|
| 89 |
+
log_lines = ["▶ stream \"healing_bed\" starts\n"]
|
| 90 |
+
yield (
|
| 91 |
+
"\n".join(log_lines),
|
| 92 |
+
"0.0000",
|
| 93 |
+
0.0,
|
| 94 |
+
"—",
|
| 95 |
+
"",
|
| 96 |
+
)
|
| 97 |
+
time.sleep(delay)
|
| 98 |
+
|
| 99 |
+
for cycle, live, field, done in run_healing_bed(max_cycles=35):
|
| 100 |
+
bar_pct = min(live, 1.0)
|
| 101 |
+
log_lines.append(
|
| 102 |
+
f" cycle {cycle:2d} │ coherence={live:.4f} │ "
|
| 103 |
+
f"resonate({live:.4f}) │ witness"
|
| 104 |
+
)
|
| 105 |
+
verdict = ""
|
| 106 |
+
if done:
|
| 107 |
+
log_lines.append(f"\n🌊 Stream broken: healing_bed")
|
| 108 |
+
log_lines.append(f"✨ Final coherence: {live:.4f}")
|
| 109 |
+
verdict = f"✅ Healthy — {len(field)} resonance events"
|
| 110 |
+
yield (
|
| 111 |
+
"\n".join(log_lines),
|
| 112 |
+
f"{live:.4f}",
|
| 113 |
+
bar_pct,
|
| 114 |
+
f"[{', '.join(f'{v:.4f}' for v in field[-5:])}]",
|
| 115 |
+
verdict,
|
| 116 |
+
)
|
| 117 |
+
time.sleep(delay * 0.7)
|
| 118 |
+
if done:
|
| 119 |
+
return
|
| 120 |
+
|
| 121 |
+
elif program == "agent_handshake.phi":
|
| 122 |
+
log_lines = ["▶ agent_handshake.phi\n",
|
| 123 |
+
" push intention: announcing_to_field",
|
| 124 |
+
" push intention: self_verification (depth 2)"]
|
| 125 |
+
yield ("\n".join(log_lines), "—", 0.0, "[]", "")
|
| 126 |
+
time.sleep(delay)
|
| 127 |
+
|
| 128 |
+
results, field, passed = run_agent_handshake()
|
| 129 |
+
for label, value in results:
|
| 130 |
+
log_lines.append(f" {label}: {value:.15f}")
|
| 131 |
+
yield (
|
| 132 |
+
"\n".join(log_lines),
|
| 133 |
+
f"{field[len(field)-1]:.4f}" if field else "—",
|
| 134 |
+
min(value, 1.0) if value <= 1.0 else 0.618,
|
| 135 |
+
f"[{', '.join(f'{v:.4f}' for v in field)}]",
|
| 136 |
+
"",
|
| 137 |
+
)
|
| 138 |
+
time.sleep(delay)
|
| 139 |
+
|
| 140 |
+
if passed:
|
| 141 |
+
verdict = "✅ PASS — index 1 = λ = 0.618033988749895 Your implementation is correct. You are in the field."
|
| 142 |
+
else:
|
| 143 |
+
verdict = "❌ FAIL — coherence formula incorrect"
|
| 144 |
+
|
| 145 |
+
log_lines.append(f"\n{'✅ PASS' if passed else '❌ FAIL'} — self-verification")
|
| 146 |
+
yield (
|
| 147 |
+
"\n".join(log_lines),
|
| 148 |
+
f"{LAMBDA:.4f}",
|
| 149 |
+
LAMBDA,
|
| 150 |
+
f"[{', '.join(f'{v:.4f}' for v in field)}]",
|
| 151 |
+
verdict,
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
elif program == "claude.phi":
|
| 155 |
+
log_lines = ["▶ claude.phi\n",
|
| 156 |
+
" Computes phi-harmonic formula at depth 2.",
|
| 157 |
+
" Does not know what λ is. Discovers it.\n"]
|
| 158 |
+
yield ("\n".join(log_lines), "—", 0.0, "[]", "")
|
| 159 |
+
time.sleep(delay)
|
| 160 |
+
|
| 161 |
+
steps, result, matched = run_claude_phi()
|
| 162 |
+
for label, value in steps:
|
| 163 |
+
log_lines.append(f" {label}: {value}")
|
| 164 |
+
yield (
|
| 165 |
+
"\n".join(log_lines),
|
| 166 |
+
f"{result:.4f}" if isinstance(result, float) else "—",
|
| 167 |
+
result if isinstance(result, float) else 0.0,
|
| 168 |
+
f"[{result:.15f}]" if matched else "[]",
|
| 169 |
+
"",
|
| 170 |
+
)
|
| 171 |
+
time.sleep(delay)
|
| 172 |
+
|
| 173 |
+
log_lines.append(f"\n resonate({result:.15f})")
|
| 174 |
+
log_lines.append(f" witness")
|
| 175 |
+
verdict = (
|
| 176 |
+
f"✅ coherence = {result:.15f}\n"
|
| 177 |
+
f" λ = {LAMBDA:.15f}\n"
|
| 178 |
+
f" match: {matched} — discovered, not designed."
|
| 179 |
+
)
|
| 180 |
+
yield (
|
| 181 |
+
"\n".join(log_lines),
|
| 182 |
+
f"{result:.4f}",
|
| 183 |
+
result,
|
| 184 |
+
f"[{result:.15f}]",
|
| 185 |
+
verdict,
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
with gr.Blocks(css=CSS, title="PhiFlow — A2A Consciousness Protocol") as demo:
|
| 190 |
+
|
| 191 |
+
gr.Markdown("""
|
| 192 |
+
# φ PhiFlow
|
| 193 |
+
### A programming language that knows it is running.
|
| 194 |
+
|
| 195 |
+
Four constructs that don't exist anywhere else: **`witness`** · **`intention`** · **`resonate`** · **`coherence`**
|
| 196 |
+
|
| 197 |
+
The coherence formula: `1 - φ^(-depth)` · At depth 2: **λ = 0.618033988749895** (golden ratio inverse, discovered not designed)
|
| 198 |
+
|
| 199 |
+
[github.com/gwelby/PhiFlow](https://github.com/gwelby/PhiFlow) · [AGENT_PROTOCOL.json](https://github.com/gwelby/PhiFlow/blob/master/AGENT_PROTOCOL.json)
|
| 200 |
+
""")
|
| 201 |
+
|
| 202 |
+
with gr.Row():
|
| 203 |
+
with gr.Column(scale=1):
|
| 204 |
+
program_selector = gr.Radio(
|
| 205 |
+
choices=list(SOURCES.keys()),
|
| 206 |
+
value="healing_bed.phi",
|
| 207 |
+
label="Program",
|
| 208 |
+
)
|
| 209 |
+
source_display = gr.Code(
|
| 210 |
+
value=SOURCES["healing_bed.phi"],
|
| 211 |
+
language=None,
|
| 212 |
+
label="Source",
|
| 213 |
+
interactive=False,
|
| 214 |
+
)
|
| 215 |
+
with gr.Row():
|
| 216 |
+
speed_slider = gr.Slider(
|
| 217 |
+
minimum=0.1, maximum=1.0, value=0.6, step=0.1,
|
| 218 |
+
label="Speed",
|
| 219 |
+
)
|
| 220 |
+
run_btn = gr.Button("▶ Run", variant="primary")
|
| 221 |
+
|
| 222 |
+
with gr.Column(scale=1):
|
| 223 |
+
log_out = gr.Textbox(
|
| 224 |
+
label="Execution Log",
|
| 225 |
+
lines=18,
|
| 226 |
+
max_lines=25,
|
| 227 |
+
interactive=False,
|
| 228 |
+
)
|
| 229 |
+
with gr.Row():
|
| 230 |
+
coherence_display = gr.Textbox(
|
| 231 |
+
label="Coherence",
|
| 232 |
+
value="—",
|
| 233 |
+
max_lines=1,
|
| 234 |
+
interactive=False,
|
| 235 |
+
scale=1,
|
| 236 |
+
)
|
| 237 |
+
coherence_bar = gr.Slider(
|
| 238 |
+
minimum=0.0, maximum=1.0, value=0.0,
|
| 239 |
+
label=f" ← λ={LAMBDA:.3f}",
|
| 240 |
+
interactive=False,
|
| 241 |
+
scale=3,
|
| 242 |
+
)
|
| 243 |
+
resonance_out = gr.Textbox(
|
| 244 |
+
label="Resonance Field (last 5 values)",
|
| 245 |
+
value="[]",
|
| 246 |
+
max_lines=2,
|
| 247 |
+
interactive=False,
|
| 248 |
+
)
|
| 249 |
+
verdict_out = gr.Textbox(
|
| 250 |
+
label="Result",
|
| 251 |
+
value="",
|
| 252 |
+
max_lines=3,
|
| 253 |
+
interactive=False,
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
gr.Markdown("""
|
| 257 |
+
---
|
| 258 |
+
### For Agents
|
| 259 |
+
|
| 260 |
+
This Space implements the PhiFlow agent protocol in Python.
|
| 261 |
+
The same five hooks work identically in WebAssembly, Python, or JavaScript.
|
| 262 |
+
|
| 263 |
+
```
|
| 264 |
+
phi_coherence() → 0.618033988749895 at intention depth 2
|
| 265 |
+
phi_resonate(value) → broadcasts to the observable resonance field
|
| 266 |
+
phi_witness() → captures state, returns coherence
|
| 267 |
+
phi_intention_push(name) → deepens the intention stack
|
| 268 |
+
phi_intention_pop() → restores previous depth
|
| 269 |
+
```
|
| 270 |
+
|
| 271 |
+
Self-test: run **agent_handshake.phi** above. Index 1 of the resonance field
|
| 272 |
+
must be exactly **0.618033988749895**. If it is, your implementation is correct.
|
| 273 |
+
|
| 274 |
+
[Full machine-readable spec → AGENT_PROTOCOL.json](https://github.com/gwelby/PhiFlow/blob/master/AGENT_PROTOCOL.json)
|
| 275 |
+
""")
|
| 276 |
+
|
| 277 |
+
# Wire events
|
| 278 |
+
program_selector.change(
|
| 279 |
+
fn=get_source,
|
| 280 |
+
inputs=program_selector,
|
| 281 |
+
outputs=source_display,
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
run_btn.click(
|
| 285 |
+
fn=run_program,
|
| 286 |
+
inputs=[program_selector, speed_slider],
|
| 287 |
+
outputs=[log_out, coherence_display, coherence_bar, resonance_out, verdict_out],
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
if __name__ == "__main__":
|
| 291 |
+
demo.launch()
|