// Meta-GC decision policy — 2026-04-18 (adaptive)
// "A diagram is worth 10,000 words." — russell@unturf.com
digraph meta_gc {
    rankdir=LR
    node [shape=box, style="filled,rounded", fontname="Helvetica", fontsize=10]
    edge [fontname="Helvetica", fontsize=9]
    label="Meta-GC decision at (with-arena thunk) exit — adaptive + greedy layers"
    labelloc=t
    fontsize=13
    fontname="Helvetica Bold"

    enter [label="Arena enter\nsnapshot %r15\nactive ← 1\nfreelist OFF" fillcolor="#fff3cd"]
    thunk [label="Run thunk\n(apply_proc_raw)\nzero volatile regs" fillcolor="#d4edda"]

    gate1 [label="arena_active\nstill 1?" shape=diamond fillcolor="#fab1a0"]
    abort [label="SKIP reset\n(implicit GC ran)\nEMA += escape" fillcolor="#fab1a0"]

    gate_rate [label="adaptive mode\n& rate > 50%?" shape=diamond fillcolor="#ffd6a5"]
    gate_probe [label="probe\ncountdown > 0?" shape=diamond fillcolor="#ffd6a5"]
    skip [label="SKIP verify\nlet heap grow\nnext overflow → full GC\nEMA += escape" fillcolor="#fd9644"]

    verify [label="Verify\nmark phase\n(globals, syms,\nstack, + result)" fillcolor="#81ecec"]
    gate2 [label="Mark in\n[snap, r15)?" shape=diamond fillcolor="#81ecec"]

    reset [label="BULK RESET O(1)\n%r15 ← snap\nresets++\nEMA += 0" fillcolor="#00b894" fontcolor=white]
    escape [label="SWEEP FALLBACK\ngc_sweep full\nescapes++\nEMA += 1" fillcolor="#e17055" fontcolor=white]

    ret [label="Return result\n(arena-stats)" fillcolor="#dfe6e9"]

    enter -> thunk -> gate1
    gate1 -> gate_rate [label="yes"]
    gate1 -> abort [label="no"]
    gate_rate -> verify [label="no (rate low\nor greedy)"]
    gate_rate -> gate_probe [label="yes"]
    gate_probe -> skip [label="yes\n(-- countdown)"]
    gate_probe -> verify [label="no (probe,\nreset countdown)"]
    verify -> gate2
    gate2 -> reset [label="no"]
    gate2 -> escape [label="yes"]
    reset -> ret
    escape -> ret
    skip -> ret
    abort -> ret
}
