// Bend cross-tier GPU dispatch — 2026-06-07
// "A diagram is worth 10,000 words." — russell@unturf.com
//
// One (bend …) form. The dispatcher picks local vs worker per call.
// When it goes remote, the wire frame is either S-expression text
// (small payloads, parser cost dominates) or a 4-byte magic + raw
// bytes (huge payloads). Workers spawn a leaf CUDA binary via each
// tier's native process-spawn primitive. CUDA kernel returns bytes
// back through the same wire frame.
digraph bend_dispatch {
    rankdir=LR
    splines=true
    nodesep=0.45
    ranksep=0.7
    node [shape=box, style="filled,rounded", fontname="Helvetica", fontsize=11, margin="0.18,0.10"]
    edge [fontname="Helvetica", fontsize=9, arrowsize=0.85]
    label="Bend — one (bend …) form across host, wire, worker, and GPU"
    labelloc=t
    fontsize=14
    fontname="Helvetica Bold"

    // ─── Caller side ────────────────────────────────────────────────
    subgraph cluster_caller {
        label="Caller tier  (any of Python / C / asm)"
        labelloc=t
        style="rounded,filled"
        fillcolor="#e8f4f8"
        color="#0984e3"
        fontname="Helvetica Bold"
        fontsize=11
        fontcolor="#0984e3"
        call    [label="(bend! …) form\nin user code" fillcolor="#b3d9e8"]
        cost    [label="cost estimator\n(local vs wire)" fillcolor="#d6eaf8"]
        local   [label="evaluate locally\n(small payloads)" fillcolor="#aed6f1"]
        call -> cost
        cost -> local [label="cheap" fontcolor="#1f618d"]
    }

    // ─── Wire ──────────────────────────────────────────────────────
    subgraph cluster_wire {
        label="Wire — port 8320 (BEND)"
        labelloc=t
        style="rounded,filled"
        fillcolor="#fff8e1"
        color="#f57f17"
        fontname="Helvetica Bold"
        fontsize=11
        fontcolor="#f57f17"
        sexp    [label="S-expression\n(text)\n— small payloads" fillcolor="#ffe082"]
        bin     [label="binary frame\nmagic + raw bytes\n— huge payloads\n(BSHK, BCGB, BSCP,\n BSRT, BSB3, …)" fillcolor="#ffd54f"]
    }

    cost -> sexp [label="≤ ~1k inputs" fontcolor="#f57f17"]
    cost -> bin  [label="≥ ~1k inputs\n(150× over S-exp\n at 1 M)" fontcolor="#f57f17"]

    // ─── Worker side ───────────────────────────────────────────────
    subgraph cluster_worker {
        label="Worker tier  (any of Python / C / asm — listens on :8320)"
        labelloc=t
        style="rounded,filled"
        fillcolor="#e8f5e9"
        color="#2e7d32"
        fontname="Helvetica Bold"
        fontsize=11
        fontcolor="#2e7d32"
        recv    [label="gpu-worker\nrecv + dispatch\n+ health" fillcolor="#a5d6a7"]
        spawn   [label="spawn-process-stdio\n— Py: subprocess.Popen\n— C:  fork + execve\n— asm: pipe2+fork+execve\n      (raw syscall, no libc)" fillcolor="#c8e6c9"]
        recv -> spawn
    }

    sexp -> recv
    bin  -> recv

    // ─── Leaf CUDA binary + GPU ────────────────────────────────────
    subgraph cluster_gpu {
        label="Leaf CUDA binary + RTX 3090"
        labelloc=t
        style="rounded,filled"
        fillcolor="#f3e5f5"
        color="#7b1fa2"
        fontname="Helvetica Bold"
        fontsize=11
        fontcolor="#7b1fa2"
        leaf    [label="shake256-fanout /\nsecp256k1-batch-mul /\ncgbn-batch-worker /\nblake3-fanout /\nradix-sort /\ndemo_ops …" fillcolor="#ce93d8"]
        kernel  [label="CUDA kernel\n(byte-identical\n vs host reference)" fillcolor="#ba68c8"]
        leaf -> kernel
    }

    spawn -> leaf [label="stdin / stdout" fontcolor="#7b1fa2"]

    // ─── Return path ───────────────────────────────────────────────
    kernel -> leaf [label="bytes back" fontcolor="#7b1fa2" style=dashed]
    leaf -> spawn [style=invis]
    spawn -> recv [label="result" style=dashed fontcolor="#2e7d32"]
    recv -> cost  [label="wire frame back" style=dashed fontcolor="#f57f17" constraint=false]
    local -> call [label="value" style=dashed fontcolor="#1f618d" constraint=false]
}
