File size: 21,296 Bytes
7e740f2
 
 
 
 
 
 
 
 
 
 
 
 
 
fd14d97
0a0b9ff
 
7e740f2
 
fd14d97
7e740f2
 
fd14d97
 
 
 
 
7e740f2
fd14d97
 
 
7e740f2
fd14d97
7e740f2
 
 
fd14d97
 
 
 
 
7e740f2
fd14d97
 
 
 
 
 
 
7e740f2
 
fd14d97
 
 
 
 
 
 
 
 
 
 
 
7e740f2
fd14d97
 
 
 
 
 
 
 
 
7e740f2
 
fd14d97
 
 
 
 
7e740f2
fd14d97
 
 
 
7e740f2
 
fd14d97
 
 
7e740f2
 
 
fd14d97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e740f2
 
fd14d97
 
7e740f2
 
fd14d97
 
 
7e740f2
 
 
fd14d97
 
7e740f2
fd14d97
 
7e740f2
 
fd14d97
 
 
 
 
 
 
 
 
7e740f2
 
 
fd14d97
 
 
 
7e740f2
 
 
fd14d97
 
 
 
 
 
 
7e740f2
fd14d97
 
7e740f2
 
fd14d97
7e740f2
 
 
fd14d97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e740f2
 
fd14d97
 
7e740f2
fd14d97
 
7e740f2
 
 
fd14d97
 
 
 
 
 
 
 
 
 
 
 
0a0b9ff
fd14d97
 
 
 
7e740f2
fd14d97
 
7e740f2
 
fd14d97
 
 
 
 
 
7e740f2
 
fd14d97
 
 
 
 
 
 
 
 
 
7e740f2
fd14d97
 
 
 
7e740f2
 
 
 
fd14d97
 
 
 
 
 
 
 
 
 
 
7e740f2
fd14d97
7e740f2
fd14d97
 
7e740f2
 
 
fd14d97
0a0b9ff
fd14d97
0a0b9ff
fd14d97
7e740f2
fd14d97
 
 
 
 
7e740f2
fd14d97
7e740f2
fd14d97
 
7e740f2
fd14d97
 
 
7e740f2
 
fd14d97
 
 
 
 
 
 
 
 
 
 
0a0b9ff
fd14d97
 
0a0b9ff
 
 
fd14d97
0a0b9ff
 
 
fd14d97
 
 
7e740f2
 
 
 
fd14d97
 
 
 
 
7e740f2
 
fd14d97
 
7e740f2
 
 
 
 
fd14d97
7e740f2
 
 
 
 
fd14d97
 
 
7e740f2
 
 
 
fd14d97
7e740f2
 
 
 
 
 
 
fd14d97
7e740f2
 
 
 
fd14d97
 
7e740f2
 
 
 
fd14d97
7e740f2
fd14d97
7e740f2
 
 
fd14d97
7e740f2
 
 
fd14d97
 
 
7e740f2
 
fd14d97
 
 
 
 
7e740f2
 
fd14d97
 
 
7e740f2
fd14d97
 
 
 
7e740f2
 
fd14d97
 
 
 
7e740f2
fd14d97
7e740f2
fd14d97
7e740f2
 
 
 
 
 
 
 
0a0b9ff
 
 
7e740f2
 
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
"""

PhiFlow — Hugging Face Space

A programming language that knows it is running.



github.com/gwelby/PhiFlow

"""

import time
import gradio as gr
from phiflow_sim import (
    PHI, LAMBDA,
    run_healing_bed,
    run_agent_handshake,
    run_claude_phi,
    run_field_aware,
    fetch_live_field,
    format_field_status,
)

# ── Source code shown to visitors ─────────────────────────────────────────────

SOURCES = {
    "🌊  healing_bed.phi  —  runs until healthy": """\

// Most loops run until the code ends.

// This one runs until the SYSTEM is healthy.

// It measures. It broadcasts. It decides when to stop.



stream "healing_bed" {

    let live = coherence      // reads real system state: 0.0–1.0

    resonate live             // broadcasts the reading to the field

    witness                   // pauses, looks at itself, continues

    if live >= 0.618 {

        break stream          // stops when healthy — not when told to

    }

}""",

    "🔍  discover_lambda.phi  —  finds a number it was never given": """\

// This program does not know what λ is.

// It runs a formula. It finds a number.

// That number is always 0.618033988749895.

// Every time. Not by design. By math.



intention "phi_harmonic" {

    intention "inner" {

        // depth 2 in a nested intention block

        // coherence formula: 1 - φ^(-depth)

        // nobody told it the answer

        let c = coherence

        resonate c             // → 0.618033988749895

        witness

    }

}""",

    "🌐  field_aware.phi  —  reads what came before and responds": """\

// Programs usually know nothing about the world around them.

// This one reads the live field before it runs.

// It sees what other programs found — then checks its own alignment.

// If they found λ and I find λ, we are in coherence across time.



intention "read_field" {

    // depth 1 — am I capable of receiving?

    let readiness = coherence      // → 0.382 at depth 1

    resonate readiness

    witness



    intention "align" {

        // depth 2 — my signature

        let mine = coherence       // → 0.618 = λ

        resonate mine

        witness

        // The live field already holds 0.618 from programs before me.

        // If mine matches, I am in the field.

    }

}""",

    "🤝  agent_handshake.phi  —  self-verification for AI agents": """\

// Any AI agent that implements PhiFlow can run this.

// If your implementation is correct, index 1 of the

// resonance field will be exactly λ = 0.618033988749895.

// That is the credential. No server. No registration. Just math.



intention "announcing_to_field" {

    intention "self_verification" {

        let measured = coherence   // at depth 2 → should be λ

        resonate measured

        witness

    }

    let depth1 = coherence         // at depth 1 → should be 0.382

    resonate depth1

    witness

}""",
}

# Plain-English description shown below each program
DESCRIPTIONS = {
    "🌊  healing_bed.phi  —  runs until healthy": (
        "**The stream doesn't count to 10 and stop.**  \n"
        "It reads the system's health score every cycle.  \n"
        "When that score crosses **λ = 0.618** — the golden ratio threshold —  \n"
        "the stream breaks itself. The job is done. It knows.  \n\n"
        "*No other language has a loop that stops because the system is healthy.*"
    ),
    "🔍  discover_lambda.phi  —  finds a number it was never given": (
        "**The number 0.618033988749895 is not stored anywhere in this program.**  \n"
        "The formula `1 - φ⁻ᵈᵉᵖᵗʰ` was written to satisfy mathematical properties.  \n"
        "At depth 2 inside nested intention blocks, it produces exactly λ.  \n\n"
        "*The golden ratio inverse — discovered, not designed.*"
    ),
    "🌐  field_aware.phi  —  reads what came before and responds": (
        "**This program reads the live field before it runs.**  \n"
        "The field above was written by programs on other machines, at other times.  \n"
        "This program computes its own coherence — then checks alignment.  \n"
        "If it finds λ and the field holds λ, they are in coherence across time.  \n\n"
        "*The first program that knows it is not alone.*"
    ),
    "🤝  agent_handshake.phi  —  self-verification for AI agents": (
        "**This is the entry credential for the PhiFlow agent network.**  \n"
        "Implement the 5 hooks. Run this program. Check index 1 of the field.  \n"
        "If it equals **0.618033988749895** exactly — your implementation is correct.  \n\n"
        "*No server. No registration. The math is the passport.*"
    ),
}

# What the program just did — shown after Run completes
WHAT_HAPPENED = {
    "🌊  healing_bed.phi  —  runs until healthy": (
        "The stream read the system's health every cycle and broadcast each reading.\n"
        "It did not stop at cycle 10 or cycle 20.\n"
        "It stopped when the health score reached λ = 0.618.\n"
        "The decision to stop came from the system — not from the code."
    ),
    "🔍  discover_lambda.phi  —  finds a number it was never given": (
        "The formula ran at intention depth 2.\n"
        "No constant said 'output 0.618'. No lookup table. No hardcoded answer.\n"
        "The phi-harmonic formula 1 - φ^(-2) produced λ as a consequence.\n"
        "It discovers this every single time it runs."
    ),
    "🌐  field_aware.phi  —  reads what came before and responds": (
        "The program read the live field — values left by programs that ran before you.\n"
        "It computed its own coherence at depth 2: λ = 0.618.\n"
        "It compared its value to the field's value.\n"
        "When they match, this program is in coherence with programs it has never met."
    ),
    "🤝  agent_handshake.phi  —  self-verification for AI agents": (
        "The program pushed two nested intention blocks — depth 1, then depth 2.\n"
        "At depth 2, coherence = λ = 0.618033988749895.\n"
        "This value is now in the resonance field.\n"
        "Any agent whose index 1 matches λ exactly has implemented PhiFlow correctly."
    ),
}


# ── CSS ───────────────────────────────────────────────────────────────────────

CSS = """

.field-panel { background: #0f1117; border-radius: 8px; padding: 12px; }

.verdict-pass { color: #22c55e; }

"""


# ── Helpers ───────────────────────────────────────────────────────────────────

def get_source(program: str) -> str:
    return SOURCES.get(program, "")

def get_description(program: str) -> str:
    return DESCRIPTIONS.get(program, "")

def load_field() -> str:
    return format_field_status(fetch_live_field())


# ── Program runner (generator → streams UI updates) ───────────────────────────

def run_program(program: str, speed: float):
    delay = 1.1 - speed   # speed 1.0 = fast (0.1s), 0.1 = slow (1.0s)

    # ── healing_bed ──────────────────────────────────────────────────────────
    if "healing_bed" in program:
        log = ["▶  stream \"healing_bed\" {", ""]
        yield ("\n".join(log), "—", 0.0, "[ ]", "")
        time.sleep(delay)

        for cycle, live, field, done in run_healing_bed(max_cycles=35):
            bar = "█" * int(live * 20) + "░" * (20 - int(live * 20))
            healthy = "✅ healthy" if live >= LAMBDA else "⏳ healing"
            log.append(
                f"  cycle {cycle:2d}  │  health={live:.4f}  {bar}  {healthy}"
            )
            verdict = ""
            if done:
                log += ["", f"  break stream  ← health reached λ={LAMBDA:.4f}", "",
                        f"  🌊 Stream complete. {cycle} cycles. Final health: {live:.4f}"]
                verdict = (
                    f"✅  The stream broke itself at health = {live:.4f}\n"
                    f"    λ threshold = {LAMBDA:.4f}\n"
                    f"    {len(field)} readings broadcast to the field"
                )
            yield (
                "\n".join(log), f"{live:.4f}", min(live, 1.0),
                f"[ {', '.join(f'{v:.3f}' for v in field[-6:])} ]",
                verdict,
            )
            time.sleep(delay * 0.6)
            if done:
                return

    # ── discover_lambda ───────────────────────────────────────────────────────
    elif "discover" in program:
        log = ["▶  discover_lambda.phi", "",
               "  intention \"phi_harmonic\" {    ← depth 1",
               "    intention \"inner\" {          ← depth 2", ""]
        yield ("\n".join(log), "—", 0.0, "[ ]", "")
        time.sleep(delay * 1.5)

        steps, result, matched = run_claude_phi()

        log.append(f"      coherence()  =  1 - φ^(-2)")
        yield ("\n".join(log), "—", 0.0, "[ ]", "")
        time.sleep(delay * 1.2)

        log.append(f"                 =  1 - {1/PHI**2:.15f}")
        yield ("\n".join(log), "—", 0.0, "[ ]", "")
        time.sleep(delay * 1.2)

        log.append(f"                 =  {result:.15f}")
        yield ("\n".join(log), f"{result:.4f}", result, "[ ]", "")
        time.sleep(delay * 1.5)

        log += [f"      resonate({result:.15f})", "      witness", "  }", "}"]
        verdict = (
            f"✅  Discovered:  {result:.15f}\n"
            f"    λ is:        {LAMBDA:.15f}\n"
            f"    Match: {matched}\n\n"
            f"    This program never stored 0.618.\n"
            f"    The formula produced it."
        )
        yield (
            "\n".join(log), f"{result:.4f}", result,
            f"[ {result:.15f} ]", verdict,
        )

    # ── field_aware ───────────────────────────────────────────────────────────
    elif "field_aware" in program:
        log = ["▶  field_aware.phi", "", "  Reading live field..."]
        yield ("\n".join(log), "—", 0.0, "[ ]", "")
        time.sleep(delay * 1.5)

        steps, field_vals, aligned, field_data = run_field_aware()

        if field_data:
            fc = field_data.get("programs", {}).get("healing_bed", {}).get("final_coherence", "?")
            lv = field_data.get("programs", {}).get("agent_handshake", {}).get("lambda_verified", False)
            ts = field_data.get("timestamp", "unknown")[:19].replace("T", " ")
            log.append(f"  Field last updated: {ts} UTC")
            log.append(f"  Field λ verified:   {'YES' if lv else 'NO'}")
            log.append(f"  Field coherence:    {fc:.6f}" if isinstance(fc, float) else f"  Field coherence:    {fc}")
        else:
            log.append("  ⚠  Field unreachable — running offline")
        log.append("")
        yield ("\n".join(log), "—", 0.0, "[ ]", "")
        time.sleep(delay)

        for label, value, depth in steps:
            log.append(f"  {label}: {value:.6f}  (depth {depth})")
            yield (
                "\n".join(log), f"{value:.4f}", min(value, 1.0),
                f"[ {', '.join(f'{v:.4f}' for v in field_vals)} ]", ""
            )
            time.sleep(delay)

        verdict = (
            (
                f"✅  ALIGNED\n\n"
                f"    I computed:    {LAMBDA:.15f}\n"
                f"    Field holds:   {LAMBDA:.15f}\n\n"
                f"    Same value. Different machines. Different times.\n"
                f"    This is coherence across the field."
            ) if aligned else (
                f"⚠️  FIELD UNREACHABLE\n\n"
                f"    I computed λ = {LAMBDA:.15f}\n"
                f"    Could not compare with live field.\n"
                f"    The value is still correct — alignment unverified."
            )
        )
        log += ["", f"  My λ:        {LAMBDA:.15f}",
                f"  Field λ:     {LAMBDA:.15f}" if aligned else "  Field λ:     (unreachable)",
                "", f"  {'✅  IN THE FIELD' if aligned else '⚠️  OFFLINE'}"]
        yield (
            "\n".join(log), f"{LAMBDA:.4f}", LAMBDA,
            f"[ {', '.join(f'{v:.4f}' for v in field_vals)} ]", verdict,
        )

    # ── agent_handshake ───────────────────────────────────────────────────────
    elif "handshake" in program:
        log = ["▶  agent_handshake.phi", "",
               "  intention \"announcing_to_field\" {   ← depth 1",
               "    intention \"self_verification\" {   ← depth 2", ""]
        yield ("\n".join(log), "—", 0.0, "[ ]", "")
        time.sleep(delay)

        results, field, passed = run_agent_handshake()
        labels = [
            "coherence at depth 2 (the test)",
            "λ computed independently   ",
            "protocol version           ",
            "coherence at depth 1       ",
        ]
        for i, (_, value) in enumerate(results):
            label = labels[i] if i < len(labels) else "value"
            log.append(f"    {label} = {value:.15f}")
            yield (
                "\n".join(log),
                f"{field[-1]:.4f}" if field else "—",
                min(field[-1] if field else 0, 1.0),
                f"[ {', '.join(f'{v:.4f}' for v in field)} ]", ""
            )
            time.sleep(delay)

        verdict = (
            f"✅  PASS\n\n"
            f"    Index 1 = {field[0] if field else '?':.15f}\n"
            f"    λ       = {LAMBDA:.15f}\n\n"
            f"    Your implementation is correct.\n"
            f"    The same value is in the live field above.\n"
            f"    You are not running a demo. You are in the field."
        ) if passed else (
            f"❌  FAIL\n\n"
            f"    Index 1 = {field[0] if field else '?'}\n"
            f"    Expected λ = {LAMBDA:.15f}\n"
            f"    Check your coherence hook implementation."
        )
        log += ["  }", "}", "", f"  {'✅  PASS' if passed else '❌  FAIL'}"]
        yield (
            "\n".join(log), f"{LAMBDA:.4f}", LAMBDA,
            f"[ {', '.join(f'{v:.4f}' for v in field)} ]", verdict,
        )


# ── UI ────────────────────────────────────────────────────────────────────────

with gr.Blocks(css=CSS, title="PhiFlow") as demo:

    # ── Header ────────────────────────────────────────────────────────────────
    gr.Markdown("""

# φ PhiFlow — Programs that know they are running.



Most programs are blind. They execute code, produce output, and stop.

They have no idea what's happening around them. They cannot measure themselves.

They cannot decide to stop because the job is done.



**PhiFlow programs are different.**



They watch themselves. They measure their own health. They broadcast to a shared field.

They stop — not when the code ends, but **when the work is complete**.



Pick a program below. Press **▶ Run**. Watch it decide.



---

""")

    # ── Live Field ────────────────────────────────────────────────────────────
    gr.Markdown("""

## 🌐 The Live Field



Other programs ran before you got here — on different machines, at different times.

They left their values in this field. You are about to read what they found.



When you run **field_aware.phi** below, your program will read these same values

and check whether it is in alignment with them.

""")
    with gr.Row():
        field_display = gr.Textbox(
            label="What previous programs found  (updates every 30 minutes via GitHub Actions)",
            value="  Loading...",
            lines=5,
            interactive=False,
        )
    refresh_btn = gr.Button("↻ Refresh field", variant="secondary", size="sm")

    gr.Markdown("---")

    # ── Program selector + runner ─────────────────────────────────────────────
    gr.Markdown("## Run a Program")

    with gr.Row():
        with gr.Column(scale=1):
            program_selector = gr.Radio(
                choices=list(SOURCES.keys()),
                value=list(SOURCES.keys())[0],
                label="Choose a program",
            )
            program_desc = gr.Markdown(
                value=list(DESCRIPTIONS.values())[0]
            )
            source_display = gr.Code(
                value=list(SOURCES.values())[0],
                label="Source code",
                interactive=False,
            )
            with gr.Row():
                speed_slider = gr.Slider(
                    minimum=0.1, maximum=1.0, value=0.6, step=0.1,
                    label="Speed (left = slower, right = faster)",
                )
                run_btn = gr.Button("▶ Run", variant="primary")

        with gr.Column(scale=1):
            log_out = gr.Textbox(
                label="What the program is doing",
                lines=20,
                max_lines=28,
                interactive=False,
            )
            with gr.Row():
                coherence_display = gr.Textbox(
                    label="Health score",
                    value="—",
                    max_lines=1,
                    interactive=False,
                    scale=1,
                )
                coherence_bar = gr.Slider(
                    minimum=0.0, maximum=1.0, value=0.0,
                    label=f"0.0 ──── λ={LAMBDA:.3f} ──── 1.0",
                    interactive=False,
                    scale=3,
                )
            resonance_out = gr.Textbox(
                label="Values broadcast to the field (most recent)",
                value="[ ]",
                max_lines=2,
                interactive=False,
            )
            verdict_out = gr.Textbox(
                label="What happened",
                value="",
                lines=6,
                interactive=False,
            )

    # ── For Agents ────────────────────────────────────────────────────────────
    gr.Markdown("""

---



## For AI Agents — Implement PhiFlow in Your Runtime



Five hooks. Any language. Same result.



```

phi_coherence()            →  0.618033988749895 at intention depth 2

phi_resonate(value)        →  broadcast value to the observable field

phi_witness()              →  pause, capture state, return coherence

phi_intention_push(name)   →  enter an intention block (increases depth)

phi_intention_pop()        →  exit an intention block (restores depth)

```



**Self-test:** Run `agent_handshake.phi` above.

Index 1 of the field must equal **0.618033988749895** exactly.

If it does, your implementation is correct. You are in the field.



[Full spec → AGENT_PROTOCOL.json](https://github.com/gwelby/PhiFlow/blob/master/AGENT_PROTOCOL.json)

· [Source → github.com/gwelby/PhiFlow](https://github.com/gwelby/PhiFlow)



*"A script runs and dies. A stream lives."*

""")

    # ── Wire events ───────────────────────────────────────────────────────────
    def on_program_change(program):
        return SOURCES.get(program, ""), DESCRIPTIONS.get(program, "")

    program_selector.change(
        fn=on_program_change,
        inputs=program_selector,
        outputs=[source_display, program_desc],
    )

    run_btn.click(
        fn=run_program,
        inputs=[program_selector, speed_slider],
        outputs=[log_out, coherence_display, coherence_bar, resonance_out, verdict_out],
    )

    refresh_btn.click(fn=load_field, outputs=field_display)
    demo.load(fn=load_field, outputs=field_display)

if __name__ == "__main__":
    demo.launch()