Commit graph

51 commits

Author SHA1 Message Date
3e0ca555b5
examples/cuda-fanout: guard delete-file in 3 binary handlers
handle-binary-shake, handle-binary-cgbn, handle-binary-secp each
called (delete-file in-path) (delete-file out-path) unconditionally
in both the daemon-ok and daemon-error branches — 6 sites total.

When the daemon failed without producing out-path (e.g. crash, OOM,
bad payload), delete-file raised file-not-found and the entire
listener exited. Crashed 3090-ai once during ai.foxhop.net deployment
smoke.

Wrap every delete-file with (if (file-exists? PATH) (delete-file PATH))
so a missing portal cannot kill the worker. (ok pong) sanity check
passes on Python tier.

Sites (12 guards = 6 path pairs × 2 paths):
  handle-binary-shake : ok branch + error branch
  handle-binary-cgbn  : ok branch + error branch
  handle-binary-secp  : ok branch + error branch
2026-06-05 18:58:45 -04:00
08446ef7b6
examples/cuda-fanout: bend multi-worker fan-out
Adds *bend-workers* list with round-robin dispatch, BEND_WORKERS env
loader, and helpers (bend-set-workers!, bend-pick-worker,
bend-parse-workers-env, bend-load-workers-from-env!). Single-host
legacy callers unaffected — when *bend-workers* is empty the
dispatcher falls back to *bend-worker-host* / *bend-worker-port*.

Validated cross-tier (Python + C lumbda) against a live two-host
cluster (3090-ai.foxhop.net:9091, ai.foxhop.net:9092) — round-robin
distributes evenly; cgbn mod-mul results byte-identical to gmpy2
reference on both hosts.
2026-06-05 18:18:29 -04:00
424051b7a4
catalog: cuda-secp256k1 v3 windowed-G — 13.60 Mkeys/s @ n=1M (1.73x v1)
Form A Day-3 shipped at lumbda ecfe27a. Windowed-G ladder (w=4,
16-entry G-table built once via batch-inverse on the 15 Z-coords)
landed clean.

Bench on 3090, n=1M (best-of-3, --no-batch-inv):
  v1 (Day-1)      127.04 ms  →   7.87 Mkeys/s
  v3 (Day-3 w=4)   73.54 ms  →  13.60 Mkeys/s  (1.73x v1, ~309x coincurve)

Byte-identity PASS at n in {32, 1000, 10000, 100000, 1000000} plus
known-small edge cases (k in {1, 2, 3, 7, 0xdeadbeef, n-1, n, 2^128-1}).

Daemon default still serves v1; --window-w 4 flag selects v3
explicitly. Fox's call on flipping the daemon default.

Day-4 plan: stack v3 + Day-2 batch-inv. v3 cut scalar_mul; the
residual inversion cost now actually matters, which is what Day-2
needed to win.

Three agents still in flight: walker promotion (#39), lever
generator (#40), ai.foxhop.net second worker (#30).
2026-06-05 18:16:08 -04:00
ecfe27a46f
bend form A — Day-3 v3 windowed-base ladder, 1.73x at n=1M on 3090
Per Day-2 progress doc the kernel bottleneck was scalar_mul (256 doubles +
~128 adds per scalar), not _ModInv. v3 swaps the binary double-and-add for
a windowed-base ladder: precompute table[0..15] = i*P affine on-device,
walk scalar 4 bits at a time MSB->LSB, cutting per-scalar adds from ~128
to ~63. Table loaded into __shared__ (1024 B) per block.

Benched on 3090-ai (best-of-3, --no-batch-inv):

  n=10k:   v1 4.22 Mkeys/s  v3 2.18 Mkeys/s  0.52x  (init overhead dominates)
  n=100k:  v1 7.48 Mkeys/s  v3 9.29 Mkeys/s  1.24x
  n=1M:    v1 7.87 Mkeys/s  v3 13.60 Mkeys/s 1.73x

Byte-identical against coincurve at n in {32, 1000, 10000, 100000, 1000000}.

CLI: --window-w 4 selects v3 (Day-3 canonical). w=8 reserved but stub-
rejected since device-side table init is register-stack-bounded at W <= 16.
Default no-flag behaviour stays v1 (Day-1) so the gpu-worker.lsp daemon
inherits the safe baseline until fox routes traffic to v3.

Companion progress doc at plans/form-A-day3-progress.md.
2026-06-05 18:14:56 -04:00
b2209ac337
cuda-fanout plans: Day-2/3 progress + Form D pivot RESULTS docs
Captures empirical record from three agents that finished today:

  form-A-day2-progress.md (STOPPED, no merge)
    v2 Montgomery batch inversion regressed v1 by 0.63x-0.94x
    across n in {10k, 100k, 1M}. Byte-identity PASS at every N;
    math correct. Root cause: at this N, scalar_mul (256 Jacobian
    doubles x 5-6 ModMult each) dominates, NOT _ModInv. v2's Phase
    B/D used 1 thread/block leaving ~97% of SMs idle. v1 baseline
    re-measured at 7.88 Mkeys/s at n=1M (catalog upward correction
    from initial 6.51). Day-3 path: warp-level prefix scan OR
    windowed-G ladder.

  form-D-axis-flip-RESULTS.md (Form D opt 1 shipped foxhop 1f7ac9d)
    Per-candidate axis kernel measured 217 Mops/s at K=32 M=4 on
    3090; 23.7x over per-shot N=4 at same M. Both axes saturate
    at the same ~220-250 Mops/s, refuting the bandwidth-bound
    diagnosis. Axis flip's win is occupancy-amortization at small
    M, not bandwidth redistribution.

  form-D-build-progress.md (Form D AG kernel parked)
    Aaronson-Gottesman tableau dead-end: our point-add circuit
    contains no H or S, so state never leaves the computational
    basis and AG buys nothing. Three pivot options proposed; fox
    picked options 1 + 3 in parallel.

Day-1 binary at lumbda 7661788 stays canonical for
cuda-secp256k1-batched-mul; v2 working-tree code lives uncommitted
on the build host as Day-3 scratch.
2026-06-05 15:32:32 -04:00
95b3b1af21
catalog: Form D opt 3 landed; both pivots converge on compute-saturation
Form D Option 3 (QECCOPS2 packed op format) shipped at foxhop
commit 90484ca. Numbers:

  n_batches  unpacked  packed  speedup
       16    5842 ms   5428    1.076x
       64    6226 ms   5830    1.068x
      128    6604 ms   6201    1.065x

On-disk shrink: 716 MB → 307 MB (2.33x). Storage win, not compute win.

Critical diagnostic correction from the opt 3 agent: the 3.5x
bandwidth projection was WRONG because ops_loader.c already
narrowed u64 → u32 on load, so in-VRAM Op was already 28 B, not
56 B. Max realistic VRAM-reduction was 28 → 24 B = 1.17x best
case. The measured 1.07x matches: per-shot state traffic (qubits
+ bits per thread) is ~85x larger than the op stream, which
warp-broadcasts through L1/L2. Op-stream bandwidth was not the
bottleneck.

Both Form D pivots (opt 1 axis-flip, opt 3 packing) converged on
the same finding: the 3090 is compute-saturated at ~250 Mops/s on
the kickmix circuit, not bandwidth-saturated. Algorithm + layout
tweaks already extracted; the real next macro-lever is multi-GPU
fan-out across the fleet (3090-ai + ai.foxhop.net 4090 + future
nodes, each saturating its own ceiling in parallel).

cuda-sim-axis-flip remains LIVE as a tool with specific use:
many-candidates × few-shots search-loop early-screen. ops.bin
packing remains useful for fleet rsync (3.5x smaller payloads
across the LAN matters when shipping candidate variants).
2026-06-05 14:16:20 -04:00
f8705f2e0d
cuda-fanout plans: Form D Option 3 (ops.bin packing) results
QECCOPS2 packed-op format landed in foxhop ecdsa/cuda/ at commit
90484ca. 28 → 24 B per op in VRAM, 56 → 24 B on disk (2.33× shrink).

n_batches 16/64/128 on RTX 3090: ~7% kernel speedup, byte-identical
CPU vs unpacked-GPU vs packed-GPU. Bandwidth-bound diagnosis from
form-D-build-progress.md §4 stands — per-shot state traffic (~85×
larger than op stream) owns the 1.07× ceiling. Per-candidate axis-
flip (sim_gpu_axis.cu, foxhop 1f7ac9d) remains the open lever; this
~7% stacks on top.
2026-06-05 14:14:56 -04:00
262607f438
catalog: cuda-sim-axis-flip live, bandwidth diagnosis flipped
Form D opt 1 (axis-flip refactor) shipped at foxhop 1f7ac9d.
Per-candidate parallelism over per-shot delivers 217 Mops/s at
K=32 candidates × M=4 shots on a 3090, byte-identical with the
CPU reference at every (K, M) pair we measured.

Key finding inside the numbers: the bandwidth-bound diagnosis
flagged in the Form D structural-finding doc was WRONG. Both
axes (per-shot N=128 and per-candidate K=32 M=4) saturate at the
SAME ~220-250 Mops/s on the 3090. Per-shot wins by 14% at full
saturation; axis flip wins by 23.7x at small M because it fills
SMs in one launch instead of leaving them idle.

So the axis kernel is the right tool for lumbda's many-candidates
× few-shots search-loop early-screen pattern — not a replacement
for the per-shot kernel.

Form D opt 3 (ops.bin packing) still in flight. Since compute
saturates before bandwidth on this device, packing may not
deliver expected gain. Letting that agent finish so we have
empirical numbers either way.

Live forms now: 5
  cuda-shake-fanout           ~12x host hashlib @ 1M
  cuda-sim-ops-bin            1.07x @ 128 batches (kickmix per-shot)
  cuda-bignum-cgbn            1.28 Gops/s mod-mul @ 1M (256-bit)
  cuda-secp256k1-batched-mul  6.51 Mkeys/s @ 100k (148x coincurve)
  cuda-sim-axis-flip          217 Mops/s @ K=32 M=4 (kickmix per-candidate)
2026-06-05 14:12:56 -04:00
7695163e1a
catalog: promote CGBN + secp256k1 to live, record measured throughput
Form A (cuda-secp256k1-batched-mul) shipped earlier this session at
commit 7661788. Form B (cuda-bignum-cgbn) extended to all 9 ops at
21bd26a. Both now serving on 3090-ai gpu-worker:9091 alongside the
shake form.

Catalog Live forms table:
  cuda-bignum-cgbn          | 1.28 Gops/s kernel mod-mul @ n=1M, 256-bit
                              (~256x GMP CPU single-thread); 9 ops total
  cuda-secp256k1-batched-mul| 6.51 Mkeys/s @ n=100k (~148x coincurve CPU);
                              Day-2 Montgomery batch inversion projected
                              toward FixedPaul's 6.5 Gkeys/s on 4090

Wire magics now in use:
  BSHK/BSHR — cuda-shake-fanout
  BCGB/BCGR — cuda-bignum-cgbn
  BSCP/BSCR — cuda-secp256k1-batched-mul
  (BSTB/BSTR reserved for cuda-clifford-stabilizer; parked per
   structural finding — our circuit has no H/S gates)

Form D pivots (axis-flip sim_gpu.cu + ops.bin packing) still in
flight; CATALOG.md + bend.html will gain rows once they ship.

Notable defect surfaced during Form A build: widely-cited secp256k1
generator y-coordinate
  0x483ADA7726A47B0DAFFA10ED2E11458A823D0E1D89DCAB14C7C39D9F8B97C20A
does NOT satisfy y^2 = x^3 + 7 mod p. Real Gy =
  0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
Cross-tutorial propagation. UNDF candidate logged.
2026-06-05 14:10:37 -04:00
766178818c bend form A — cuda-secp256k1-batched-mul live on 3090
Per examples/cuda-fanout/plans/form-A-secp256k1-batched-mul.md.
Batched secp256k1 scalar*G via per-thread Jacobian double-and-add
plus per-thread Z-inversion to affine. Field arithmetic uses
FixedPaul/VanitySearch-Bitcrack GPUMath.h verbatim
(commit 66e6f9d, AGPL-3.0, vendored under
vendor/vanity-search-bitcrack/).

Wire: BSCP request / BSCR response distinct from BSHK/BCGB.
   "BSCP" u32 op_id u32 n base_xy(64B BE) scalars(n*32B BE)
   "BSCR" u32 status u32 n points(n*64B BE x||y)

Validation against coincurve on 3090-ai.foxhop.net byte-identical
across known-small (k in {1,2,3,7,0xdeadbeef,n-1,n,2^128-1})
& random sweeps at n in {32, 1k, 10k, 100k}.

Measured throughput on 3090, kernel-only:
   n=10k     2.32 ms    4.31 Mkeys/s
   n=100k   15.37 ms    6.51 Mkeys/s

End-to-end over warm TCP daemon from another host:
   n=100k  400 ms  250 kkeys/s wall  (PCIe + wire serialization
   bound; kernel still <16 ms)

Speedup vs coincurve CPU single-thread (~44 kkeys/s host)
~148x at n=100k kernel-only. Day-2 work to add _ModInvGrouped
batched inversion should push toward FixedPaul's 6.9 Gkeys/s
published on 4090.

gpu-worker.lsp: maybe-register-daemon! for cuda-secp256k1-batched-mul,
handle-binary-secp branch in handle-one dispatching on BSCP magic.
Makefile: secp256k1-batch-mul / secp256k1-test / secp256k1-bench
targets. Test harness ships with coincurve preferred, falls back to
python-ecdsa or pure-Python double-and-add for the host oracle.
2026-06-05 14:08:50 -04:00
277250ecfa
bend Form D structural finding — pivot to axis-flip + ops.bin packing
Form D build agent discovered: our point-add circuit contains no
Hadamard or S gates (only X/CX/CCX/CZ/CCZ/SWAP/R/HMR/Z/NEG).
State never leaves the computational basis. Aaronson-Gottesman
tableau compression buys nothing when superposition does not
exist; reduces to exactly what sim_gpu.cu already does at one
bit per qubit per shot.

Toffoli fraction measured 13.87% (well under the 40% threshold
the planner flagged). The 1.07x cuda-sim-ops-bin ceiling traces
to memory-bandwidth on per-shot striped state — not algorithm.

STABSim-class wins remain valid for QEC / surface-code work where
H + S exist; that's a future workload.

Two replacement directions queued:
  1. axis-flip sim_gpu.cu — per-candidate parallelism over per-shot.
     ~3 days. Reuses BSHK new op_id; no AG tableau.
  2. ops.bin packing — 56→16 B per op halves global-memory traffic.
     Addresses the actual bottleneck.

Catalog + public bend.html both updated. Form D progress doc at
examples/cuda-fanout/plans/form-D-build-progress.md documents the
structural reasoning in full.
2026-06-05 14:03:03 -04:00
21bd26a4d3
bend form B — cgbn-batch-worker covers all 9 dispatcher ops on 3090
Lands the remaining 8 ops from plans/form-B-bignum-cgbn.md §2:
  0x01 mod-add        cgbn_add + carry-or-ge-modulus subtract
  0x02 mod-sub        cgbn_sub + borrow conditional add
  0x04 mod-sqr        cgbn_sqr_wide + cgbn_rem_wide
  0x05 mod-inv        cgbn_modular_inverse (binary GCD)
  0x06 mod-exp        cgbn_modular_power (binary ladder)
  0x07 mod-reduce     cgbn_rem standalone
  0x08 add-no-mod     cgbn_add, truncated 256-bit
  0x09 mul-no-mod     cgbn_mul_wide, full 512-bit output (low|high)

process_one_bin now classifies op_id into three families (binary-mod /
unary-mod / no-mod), validates wire size per family, and carves modulus
/ a / b pointers accordingly. Output buffer width is 2x for 0x09 only.

test_cgbn_known_answers.py extended: one driver per op, gmpy2 reference
(with pure-Python fallback for invert/powmod), validated byte-identical
across n in {32, 1k, 10k, 100k}. ALL PASS on 3090-ai.foxhop.net.

Measured kernel throughput at n=100k (single 3090, median of 3):
  mod-add     0.19 ms   526 Mops/s
  mod-sub     0.19 ms   526 Mops/s
  mod-mul     0.24 ms   417 Mops/s
  mod-sqr     0.24 ms   417 Mops/s
  mod-inv     2.39 ms    42 Mops/s
  mod-exp     1.02 ms    98 Mops/s   (16-bit exponents)
  mod-reduce  0.20 ms   500 Mops/s
  add-no-mod  0.19 ms   526 Mops/s
  mul-no-mod  0.19 ms   526 Mops/s

mod-inv at 42 Mops/s tracks plan §8 projection (50-100 Mops/s on 3090
via CGBN's binary GCD) on the low end — Bernstein-Yang batched inverse
(form E) remains the upgrade path. mod-exp 98 Mops/s is for short
exponents only; full 256-bit ladder will drop ~16x per plan §8.
2026-06-05 14:00:47 -04:00
3ab4044805
bend form B — cgbn-batch-worker live on 3090, 1.28 Gops/s kernel
Day-1 baseline per examples/cuda-fanout/plans/form-B-bignum-cgbn.md
lands at 1.28 Gops/s 256-bit mod-mul kernel throughput on a 3090
@ n=1M instances. ~256x over single-thread GMP CPU (5 Mops/s).

Validated byte-identical with gmpy2 reference at n=32, 1k, 10k,
100k across three modulus families (secp256k1 prime, Mersenne-ish,
arbitrary odd) — all PASS.

Files:
  cgbn-batch-worker.cu    Day-1 binary: --daemon + --binary modes,
                          op_id 0x03 mod-mul at 256-bit width,
                          BCGB/BCGR wire (distinct magic from SHAKE's
                          BSHK/BSHR so gpu-worker.lsp can route).
                          Includes gmp.h before cgbn.h so CGBN's
                          dispatch picks cgbn_mpz.h (host path) instead
                          of the unimplemented cgbn_cpu.h stub.
                          Drops const from kernel args (CGBN API
                          non-const).
  Makefile                cgbn-batch-worker target, CGBN_INC env var.
  gpu-worker.lsp          handle-binary-cgbn routes BCGB-prefixed
                          BSHK payloads through the CGBN daemon;
                          maybe-register-daemon! lets a worker host
                          skip forms whose binaries aren't installed.
  test_cgbn_known_answers.py
                          gmpy2 cross-validation harness; falls back to
                          pure-Python pow(a*b,1,m) if gmpy2 missing.

Per-call wall-time stays ~160ms because of cold cudaMalloc + context
init each --binary spawn. The plan-projected 15k crossover applies to
daemon mode (warm context). Daemon wiring lands in the next commit.

Remaining ops (0x01 mod-add, 0x02 mod-sub, 0x04 mod-sqr, 0x05 mod-inv,
0x06 mod-exp, 0x07 mod-reduce, 0x08 add-no-mod, 0x09 mul-no-mod) land
per-op as we measure each.
2026-06-05 13:32:43 -04:00
4c5e04b07f
bend: planning docs for forms A, B, D (CATALOG.md build runway)
Three implementation plans land per CATALOG.md "Build order —
recommended 3-form runway":

  form-B-bignum-cgbn.md (3-5 dev-days, fewest unknowns):
    direct NVlabs CGBN consumption. 9-op dispatcher
    (mod-add/sub/mul/sqr/inv/exp/reduce + add-no-mod +
    mul-no-mod-2x-width). BSHK/BSHR mirrors shake256-fanout.
    15k crossover (wire overhead vs raw kernel). Risks:
    CGBN maintenance freshness + sm_86/sm_89 verification.

  form-A-secp256k1-batched-mul.md (6 dev-days):
    FixedPaul/VanitySearch-Bitcrack picked (AGPL-3, active
    2025-05-16) over JeanLucPons (GPL-3, stale 2022-04-04).
    Vendor 3 headers ~2400 LoC + write secp_mul_batch_kernel.
    BSPK/BSPR wire mirrors BSHK. Kernel extraction is the
    dominant risk.

  form-D-clifford-stabilizer.md (10 dev-days):
    STABSim & Qimax not open-source; clean-room
    Aaronson-Gottesman tableau recommended. Reuse QECCOPS1
    ops.bin over BSHK op_id=0x02 — no .stim translation.
    Toffoli option (b): score symbolically, simulate
    Clifford-only — gated on first measuring Toffoli fraction
    (point-add suspected ~50%, Amdahl eats gains if worse).

Plans live as actionable instructions in second-person voice;
each cites URLs for every claim. CATALOG.md & bend.html
untouched here — plans inform future surveys without inventing
numbers.
2026-06-05 13:25:37 -04:00
a33ae4331d
bend catalog Wave 3 — 15 surveyed forms (2026-06-05)
Adds 15 new GPU primitive forms across domains not covered by
Waves 1-2: ZK/SNARK provers, pairing crypto, tensor network
contraction, sparse LA, CV primitives, numerical solvers,
generic belief propagation, MD/CFD, convex opt, DSP beyond cuFFT,
DB aggregations, graph theory beyond triangle/PageRank.

Top hits by relevance to our missions:

  * cuda-mfcc-spectral (97x) — punters-cc, BT-DISC forensics, real-time CC pipeline. unsandbox HIGH.
  * cuda-batched-lp-simplex (95x over CPLEX) — Prime Mission workstation-to-queue balancing. unsandbox HIGH.
  * cuda-betweenness-centrality-weighted (30-150x) — workaholic-node detection on dependency DAG, MOAD-0001 model. undefect HIGH.
  * cuda-hungarian-lap (400M-var LAP ~13s) — workstation balancing. unsandbox HIGH.
  * cuda-msm-bls12-381 (27.86x) — Pippenger shares branchless modmul shape with our reversible secp256k1 inner loop. ECDSA HIGH.
  * cuda-tensornet-contract (tensor QR 100x vs Xeon 8480+) — alt to stabilizer/kickmix sim path; MPS/PEPS evaluates reversible secp256k1 circuits beyond Clifford. ECDSA HIGH.
  * cuda-loopy-bp-mrf (45x) — LBP substrate for FuzzingBrain probabilistic analysis. undefect HIGH.
  * cuda-ega-gpu-aggregation (6-29x) — defect-corpus aggregation at planetary scale; telemetry queue aggregation. undefect HIGH + unsandbox HIGH.

Plus highlight numbers: FluidX3D LBM 100-200x vs ANSYS Fluent,
CUDASW++4.0 5.71 TCUPS on H100 for sequence alignment, ICICLE-Snark
the fastest Groth16 today.

Wave 3 filter-outs noted: AMGX, GROMACS, NVOFA, junction-tree BP,
batched L-BFGS — all below 10x or insufficiently general.

CATALOG.md mirrors the table for in-repo reading; bend.html
renders the public version with reference URLs.
2026-06-05 13:18:06 -04:00
a17c114263
bend.html: dedicated page + 27-form catalog with published speedups
Splits a dense bend section out of index.html (now 6 lines: tagline,
example, three highlights, CTA) into a new public page at
lumbda.com/bend.html carrying:

  * full wire protocol (S-exp + binary BSHK modes)
  * tier-choice table (Py / C / asm × S-exp / binary)
  * real workload table from foxhop ecdsafail measurements
  * 2 live forms + 7 surveyed forms (Wave 1: secp256k1 batch-mul,
    CGBN bignum, Pollard rho, Stim-on-GPU stabilizer, Bernstein-Yang
    inverse, NTT, CUB sort/scan)
  * 20 surveyed forms (Wave 2) sorted by speedup descending —
    minhash 600-1000x, cuckoo filter 378x, ChaCha20 400 GB/s,
    SAT 93x, Dilithium PQ 57.7x, BLAKE3 tree, cuFFT batched,
    Bloom filter modern, GEMM FP8, hash-join 1.8T tuples/s on
    1024xA100, cuGraph 38B TEPS, TRUST triangle 1T TEPS, nvCOMP 2.2x
  * skipped section listing forms that don't pass the threshold,
    so the catalog stays honest about what GPU dominates vs not
  * cited canonical references for every entry

CATALOG.md mirrors the same data — single source of truth in repo;
bend.html renders the same metadata for public reading.

Build order remains: A cuda-secp256k1-batched-mul, D Clifford
stabilizer, B CGBN bignum.
2026-06-05 13:02:31 -04:00
78ff89fb8b
bend: cuda-sim-ops-bin handler + per-call CPU/GPU telemetry
gpu-worker.lsp gains a cuda-sim-ops-bin op handler that spawns
demo_ops from www.foxhop.net/ecdsa/cuda via spawn-process-stdio,
drains stdout, & parses our (cuda-sim-result ...) portal back.

Each call now emits two log lines:
;;; bend RECV cuda-sim-ops-bin ops=PATH n-batches=N t-ms=...
;;; bend DONE cuda-sim-ops-bin n-batches=N wall-ms=W cpu-ms=C gpu-ms=G mismatches=0 gpu/cpu=R

so we can tell how fast bend jobs run on CPU vs GPU per call.

CLAUDE.md & www/index.html mention this integration is now live
end-to-end across our fleet.
2026-06-05 10:18:11 -04:00
01ea93f68f
binary wire mode: 12x faster than host hashlib at 1M inputs
The S-expression wire format was the bottleneck at huge payload sizes
-- 23.8 s end-to-end for 1M x 16 B inputs on the Python tier, while
the actual CUDA kernel finishes the same workload in ~47 ms. The
hex-S-exp parser ate everything between.

New binary wire mode (magic 'BSHK' prefix; payload is the daemon's
binary portal format verbatim) bypasses S-expression parsing entirely.
Worker writes the blob to disk, calls daemon process-bin, reads result,
prepends 'BSHR' magic, replies.

Measured 3090-ai, daemon warm, localhost:

  workload      Py S-exp    Py binary   C S-exp    C binary
  100 x 16 B     3.43 ms     0.74 ms    0.40 ms    0.15 ms
  1k x 16 B     23.24 ms     0.76 ms    2.77 ms    0.22 ms
  10k x 16 B   218.82 ms     1.27 ms    CLIFF      0.88 ms
  100k x 16 B  2,219 ms     10.18 ms    CLIFF     10.35 ms
  1M x 16 B   23,811 ms    159    ms    CLIFF    157    ms

150x speedup at 1M inputs on Python tier. C tier S-exp CLIFFs
between 1k and 10k inputs (reader payload limit); binary mode
bypasses the CLIFF entirely. At 100k+ inputs both tiers converge
since file I/O + CUDA kernel dominates over wire framing.

Host comparison: hashlib.shake_256 over 1M tiny inputs takes ~2 s
on a single Python core. Bend via binary worker = 157 ms = 12x
faster than host. Bend now wins at huge workloads, not just heavy
ones.

Implementation:

  lumbda.py
    * tcp-send/tcp-recv switched to latin-1 (1:1 byte mapping)
      so binary payloads pass through cleanly. UTF-8 was mangling
      bytes with replacement chars.
    * write-binary-file / read-binary-file primitives.

  c/builtins.c
    * write-binary-file / read-binary-file matching Python tier.

  examples/cuda-fanout/wire.lsp
    * wire-send-raw / wire-recv-raw helpers that frame a raw
      payload string without S-expression serialization.

  examples/cuda-fanout/gpu-worker.lsp
    * handle-binary-shake: write portal blob, daemon process-bin,
      read result, wire-send 'BSHR' + bytes.
    * handle-one dispatches on first 4 bytes of payload: 'BSHK'
      goes to binary path, anything else stays S-exp.

  examples/cuda-fanout/bench_tiers.py
    * make_payload_binary builds the BSHK protocol payload.
    * --binary flag in CLI.

  www/index.html
    * full S-exp + binary comparison table.
    * 'bend now beats host hashlib at huge workloads' headline finding.
2026-06-05 09:40:45 -04:00
d69e8ed859
asm tier: delete-file + worker now fully functional; 3-tier bench
Three defects fixed today on the asm tier worker path:

1. Multi-line "..." docstrings crashed asm tier's scheme_read.
   wire.lsp, bend.lsp, gpu-worker.lsp had docstrings spanning
   several lines; replaced with ;; comments before each define.
   asm tier loads these cleanly now.

2. asm tier lacked delete-file. handle-cuda-shake-fanout called
   it to clean up temp portal files. Added bi_delete_file via
   SYS_UNLINK = 87 syscall (~20 LoC asm). BI_DELETEFILE constant
   slotted after sibling-agent's BI_STRTOSYM.

3. All Scheme files in examples/cuda-fanout/ now ASCII-only.
   Earlier em-dash / × / → / μ tripped asm tier's reader in
   subtle ways during file load. iconv pass + sed fixes.

Result: all three tiers complete the bench through their own
cliff. New 3-tier table:

  workload              Python      C tier      asm tier
  small (3 × 16 B)       1.27 ms    0.16 ms     0.21 ms
  small (100 × 16 B)     3.43 ms    0.40 ms     1.99 ms
  medium (1000)         23.24 ms    2.77 ms     CLIFF
  med (10k)            218.82 ms    CLIFF       CLIFF
  huge (50k)         1,099 ms       CLIFF       CLIFF
  huge (100k)        2,219 ms       CLIFF       CLIFF
  huge (1M)         23,811 ms       CLIFF       CLIFF

asm tier at 0.21 ms beats Python by 6× at smallest workload,
matches C at the bottom (~30% slower). asm cliffs at 1000;
C tier cliffs at 10k. Both cliffs are reader/buffer limits
inside the tier, not network or kernel. CUDA kernel itself
finishes 1M × 16B in ~47 ms — three orders of magnitude under
any tier's wire cost at huge scale.

bench_tiers.py made cliff-resilient: respawns worker on per-
workload failure & continues, so the full row prints for every
tier instead of bailing on first cliff.

www/index.html: full 3-column table + honest framing of when
each tier earns its slot.
2026-06-05 09:24:26 -04:00
e1fe20c5b9
wire.lsp: recv-exact O(n²) → O(n); bench reports linear Python scaling
wire.lsp's recv-exact previously accumulated received chunks via
`(string-append acc chunk)` in a loop — quadratic on payload size.
Replaced with a chunk-list accumulator + single `(apply string-append
…)` at the end. Lumbda's string-append knows total length up front
& allocates once.

Python tier now scales linearly across input counts (~22 µs per input):

  workload             Python      C tier
  small  (3 × 16 B)    1.25 ms    0.16 ms    8× C win
  small  (100 × 16 B)  3.39 ms    0.40 ms    8× C win
  medium (1000)        23.52 ms   2.60 ms    9× C win
  med    (10k)         220.15 ms  (cliff)
  huge   (50k)         1,100 ms   (cliff)
  huge   (100k)        2,225 ms   (cliff)
  huge   (1M)          23,811 ms  (cliff)

C tier cliffs somewhere between 1k & 10k inputs per call — its reader
hits a payload limit we still need to track down. CUDA kernel for
1M × 16B finishes in ~47 ms on this 3090, so at huge sizes the wire
cost dominates regardless of tier.

Web page updated with the linear-scaling table & honest framing: at
small inputs C wins by 9×; at huge inputs the right next move is a
binary wire mode parallel to the daemon's already-binary portal
format. Stalls are gone.
2026-06-05 08:55:43 -04:00
373c8ea0d3
bench + www: honest huge-workload numbers + wire bottleneck found
Added a write-to-string-shim.lsp for asm tier (which lacks the
native builtin); asm launch script pre-defines *argv* + loads the
shim so wire.lsp's wire-send finds a write-to-string definition.
Python/C tiers keep the native builtin — the shim is opt-in.

Bench extended with two huge workloads (100k × 16 B, 1M × 16 B).

Real numbers, 3090-ai, daemon warm, both ends localhost:

  workload              Python    C tier   C win
  small (3 × 16 B)       1.16 ms   0.14 ms   8.3×
  small (100 × 16 B)     3.39 ms   0.42 ms   8.1×
  medium (1000 × 16 B)  23.26 ms   2.67 ms   8.7×
  huge (100k × 16 B)    2,220 ms   STALL     n/a
  huge (1M × 16 B)     24,338 ms   STALL     n/a

THE FINDING: at huge sizes, the bottleneck is the S-expression
text wire format, not the CUDA kernel. shake256-fanout finishes
1M × 16B in ~47 ms; the Python worker takes 24 SECONDS end-to-end
because wire.lsp's recv-exact accumulates chunks via string-append
in a loop — O(n²) at multi-MB payload sizes. C tier fails outright.

The right fix is binary wire framing between client + worker,
parallel to the binary portal format the daemon + leaf already use.
That's a separate piece of work; today's Web page edit calls it out
honestly so visitors know when bend is the right tool.

asm tier worker hosting still has process-management quirks
(doesn't survive nohup detachment in this environment); bench
ships with --skip-asm by default in this run.
2026-06-05 08:50:36 -04:00
f24afcc5d9
bench + www: real numbers — C tier ~9× faster than Python as worker host
Wrote examples/cuda-fanout/bench_tiers.py — spawns a worker per
tier, fires N TCP round-trips at three workload sizes through the
warm daemon, reports median + p99.

Measured on 3090-ai, daemon warm:

  workload             Python   C tier   C win
  small  (3 × 16 B)    1.27 ms  0.14 ms  9.1×
  small  (100 × 16 B)  3.46 ms  0.41 ms  8.4×
  medium (1000 × 16 B) 23.51 ms 2.67 ms  8.8×

Ratio stays at ~9× across the grid — the per-byte cost of
Python's S-expression reader/printer compared to the C tier's
reader. Justifies the LUMBDA=c default landed in the previous
commit.

asm tier worker starts up & listens (after the launch script
predefines *argv* '()), but bench script saw malformed responses on
this run — likely a write-to-string format difference between asm
& Python/C reader. Leaving for follow-up; published numbers cover
the tiers that completed end-to-end.

www/index.html bend section gains the measured table under a new
'Tier choice for the worker host' subsection. Replaces the earlier
hand-wavy ~10× claim with the actual measured numbers.
2026-06-05 08:39:31 -04:00
ceb497ac96
examples/cuda-fanout: README — asm tier primitives landed
Reflects commit 4f03c48: spawn-process-stdio + flush-port now work
on asm tier. Updates the per-tier matrix accordingly.
2026-06-04 20:41:34 -04:00
aa428cdeb2
examples/cuda-fanout: DESIGN-ecdsa-integration.md
Sketches how bend would wire into ecdsafail-challenge candidate
search loop on the foxhop.net side:

  ecdsa/lumbda/search.lsp
    → (bend!-call '(cuda-sim-ops-bin ops-path 141))
    → gpu-worker.lsp routes to demo_ops --portal
    → S-exp result back to lumbda, scoring proceeds

Identifies the two pieces missing before this lands:
  1. `system`-equivalent primitive in lumbda (or spawn+wait via
     existing spawn-process-stdio)
  2. Phase B step 7 (Solinas mod-mul) so lumbda emits real-scale
     ops.bin variants worth bending

Once both close, this is a half-day wire-up.

Cross-references:
  ~/git/www.foxhop.net/ecdsa/cuda/                    — the CUDA prototype
  ~/git/www.foxhop.net/ecdsa/lumbda/search.lsp       — current search loop
  examples/cuda-fanout/DESIGN-go-gpu.md              — the broader bend RPC design
2026-06-04 20:08:40 -04:00
07a8a4aaf7
examples/cuda-fanout: asm tier client-only support
asm tier lacks define-syntax + (error …) + spawn-process-stdio, so
the macro form (bend …) and the host-side gpu-worker.lsp aren't
asm-portable. But the wire protocol & TCP primitives are — asm
tier works as a bend CLIENT.

Three changes:

1. bend.lsp split into core (function-form) + bend-macros.lsp
   (define-syntax wrappers). Asm tier loads core; Python/C load both.

2. Function form: (bend-call '(op . args)) / (bend!-call …) does
   the same dispatch the macro does, on every tier.

3. (error …) calls replaced with portable bend-error that displays
   and returns 'bend-failure. Lets asm tier handle the no-worker
   case without crashing.

New file:
  smoke-bend-asm.lsp — minimal asm-tier smoke test

Verified on 3090-ai:
  asm tier tcp-connect to a known Python listener on :19200: PASS
  asm tier loads wire.lsp + bend.lsp cleanly: PASS
  bend-error portable across all three tiers: PASS

README updated with the asm tier client-only story + what would need
to land for full asm parity (the missing primitives + Scheme macros).

Per-tier status:
  Python tier ✓ host + client (macro & function forms)
  C tier      ✓ host + client (macro & function forms)
  asm tier    ✓ client (function form); host pending the missing
               primitives
2026-06-04 20:07:22 -04:00
494ae3193c
Python tier: spawn-process-stdio + flush-port; gpu-worker.lsp end-to-end
Two new primitives in the Python tier dispatch table make
gpu-worker.lsp (pure Scheme) replace mock-worker.py:

  (spawn-process-stdio path args) → (stdin-port . stdout-port)
    spawns a long-running subprocess via subprocess.Popen with
    stdin/stdout piped + line-buffered. Returns a Pair the
    caller holds across many request cycles.

  (flush-port port)
    flushes a write port. No-op when port has no flush method.

read-line also extended to accept file-like ports (subprocess pipes)
not just StringInputPort / sys.stdin.

gpu-worker.lsp fixes:
  - run-loop split out as its own tail-recursive function (named-let
    inside cond was harder to debug than non-named explicit recursion)
  - handle-cuda-shake-fanout unwraps (quote ...) wrapping that
    bend.lsp adds when it serializes `'expr` through write-to-string

End-to-end on 3090-ai (lumbda Python tier as both client + worker):

  shell A:  python3 -u lumbda.py /tmp/launch-worker.lsp
            → gpu-worker: ready cuda-shake-fanout ← ./shake256-fanout
            → gpu-worker listening on port 9091

  shell B:  python3 lumbda.py smoke-bend.lsp     # run 3×
            === smoke-bend ===
            1. cost estimator picks local for 3 inputs (cost too small): OK
            2. worker available? #t
            3. bend! (cuda-shake-fanout '("00" "01" "deadbeef") 32):
               ("b8d01df855…" "94da6280b2…" "fa094fa86e…")

  All three runs identical bytes. All three hashes byte-identical to
  hashlib.shake_256 — verified across the full chain:

    lumbda Python → bend macro → wire-send (length-prefixed S-exp)
      → gpu-worker.lsp (pure Scheme) → spawn-process-stdio
      → shake256-fanout --daemon (warm CUDA context on 3090)
      → kernel → output portal → wire-send response → bend returns

  No Python mock anywhere — except the leaf CUDA binary, which is the
  point of the contribution.

Documented in README.md including the python3 -u footnote for
buffering. Once a Scheme-level (flush-port (current-output-port))
is wired into the worker loop, even -u becomes optional.

Per-tier status after this commit:
  Python tier ✓ end-to-end working
  C tier      → still needs spawn-process-stdio + flush-port in
                its primitive dispatch (Scheme files unchanged)
  asm tier    → same, plus raw fork+pipe+execve syscalls for the
                spawn primitive
2026-06-04 19:36:15 -04:00
aaa6e9075c
examples/cuda-fanout: bend end-to-end on lumbda Python tier
Wired bend.lsp to lumbda's existing TCP primitives via wire.lsp
(length-prefixed S-exp framing, lifted from
ecdsa/lumbda/fleet/wire.lsp). The (bend …) macro now actually
dispatches: lumbda → tcp-connect → wire-send → wire-recv → result.

End-to-end on the 3090 (mock-worker as gpu-worker stand-in until
spawn-process-stdio lands in lumbda's core):

  λ> (load "smoke-bend.lsp")
  === smoke-bend ===
  1. cost estimator picks local for 3 inputs (cost too small): OK
  2. worker available? #t
  3. bend! (cuda-shake-fanout '("00" "01" "deadbeef") 32):
     (#xb8d01df855... #x94da6280b2... #xfa094fa86e...)

All three hashes byte-identical to hashlib.shake_256.

Files added:
  wire.lsp         — 8-digit-LE length-prefixed S-exp framing
  smoke-bend.lsp   — minimal lumbda-side test
  mock-worker.py   — Python stand-in for gpu-worker.lsp until
                     spawn-process-stdio + flush-port primitives
                     land in lumbda's core

bug fix:
  wire-recv had one missing close-paren; lumbda surfaced it as
  'unclosed (' on load. Fixed in the same commit.

mock-worker.py accepts two request shapes since bend.lsp serializes
(quote (...)) for list literals while the portal format uses
(inputs ...). Tolerating both keeps the wire protocol bend-friendly.

Per-tier integration status:
  Python tier — bend, wire, smoke-test all work ✓
  C tier      — needs: same Scheme files port directly; tcp-* exist;
                spawn-process-stdio still missing for gpu-worker.lsp
  asm tier    — needs: tcp-* exist; spawn-process-stdio requires raw
                fork + pipe + execve in asm; biggest delta vs Python

Open primitive gaps for full cross-tier bend:
  spawn-process-stdio   — for gpu-worker.lsp's daemon pool
  flush-port            — to push daemon stdin
  (current-time-ms      — exists in Python tier; needed in C/asm too)

Once those land, gpu-worker.lsp replaces mock-worker.py and bend
runs cross-tier-identical. The protocol & cost-estimator code in
bend.lsp + wire.lsp need no changes — they speak only the existing
tcp-* + read-from-string + write-to-string primitives every tier
already has.
2026-06-04 19:16:01 -04:00
731a9e5319
examples/cuda-fanout: bend primitive + binary format + GPU now wins
Three changes that together make the GPU primitive viable for the
go-gpu/bend pattern:

1. Binary portal format (length-prefixed raw bytes) — eliminates the
   hex-string parse that ate 99% of wall time. Old text portal at
   262 MB workload spent 421 sec parsing; binary format = native
   speed. New flag + daemon command:

     shake256-fanout --binary <in.bin> <out.bin>
     daemon: process-bin <in.bin> <out.bin>

   Wire (in):  u32 out_bytes | u32 n | (u32 len | len bytes) × n
   Wire (out): u32 n | u32 out_bytes | n × out_bytes

2. bend primitive (Lisp-smart GPU dispatch). Picked 'bend' over
   {go, spark, cast, fan} per fox — HVM2 lineage, fits the
   'reshape compute for GPU' mental model.

     (bend (cuda-shake-fanout inputs 32))
       → runtime inspects expr; routes to GPU worker if cost-est
         exceeds threshold AND worker reachable; else evaluates
         locally in original lexical scope
     (bend! expr)
       → force GPU, error if no worker available

   Implementation files:
     bend.lsp        — macro + cost-estimator-based router
     gpu-worker.lsp  — TCP listener, dispatches over warm daemons
     DESIGN-go-gpu.md — full architecture (already shipped)

   Tier-specific helpers (tcp-*, spawn-process-stdio, sexp->string)
   are noted as TODO per tier — Python uses subprocess + socket,
   C uses fork + portal, asm uses syscall fork + sock_stream.

3. bench_binary.py — combined daemon + binary format benchmark.
   GPU wins every cell of the grid by 1.5–10×:

     in_sz   N           total    host    dev   speedup
     32      1,000,000   32 MB    470 ms   47 ms  10.11x
     32      100,000     3.2 MB    47 ms    5 ms   9.95x
     1024    100,000     102 MB   177 ms   79 ms   2.24x
     16384   10,000      164 MB   231 ms  124 ms   1.86x
     262144  1,000       262 MB   363 ms  231 ms   1.57x

   Same workloads that lost 0.00× at hex+per-spawn now win 10× at
   binary+daemon. 4000× relative perf swing from fixing wire format
   and warming the context.

The peak 10× at small-input × high-N is the natural shape of crypto
protocols (commitments, Fiat-Shamir, PoW search). That's the win
zone for cuda-shake-fanout. README updated with the full table.
2026-06-04 18:58:59 -04:00
28beee9944
examples/cuda-fanout: daemon mode (574x faster per-call) + honest bench
Bench findings drove three changes to the reference primitive:

1. Per-spawn mode loses to host hashlib at every size we tested.
   The 200 ms cuda-ctx-init per process spawn eats any win the
   kernel could give us on SHAKE256-class compute. Honest table:

     in_sz  N         total    host ms  device ms  kernel ms  speedup
     32     3 M       96 MB    1818     4246       3.04       0.43x
     1024   100 k     102 MB   260      4753       1.72       0.05x
     16384  10 k      164 MB   343      18638      2.66       0.02x
     262144 1 k       262 MB   508      454171     39.07      0.00x

   The 454 SECONDS at 262 MB is portal hex-parsing, NOT the kernel
   (which is 39 ms). At the current S-exp hex wire format, even
   our biggest kernels are dwarfed by hex-string parsing.

2. Daemon mode lands in shake256-fanout.cu. Touch CUDA context
   once at startup, then accept commands on stdin:

     process <in.portal> <out.portal>     → fan-out + write result
     quit                                  → clean shutdown

   bench_daemon.py measures 574x speedup per call:

     workload: 10 calls × 100 inputs × 32 bytes each
       host hashlib loop  :    0.6 ms total ( 0.06 ms/call)
       per-spawn fanout   : 1825.9 ms total (182.59 ms/call)
       daemon-mode init   :  109.2 ms  (one-time)
       daemon-mode calls  :    3.2 ms total ( 0.32 ms/call)

   Daemon is the production architecture for any workload doing
   repeated fan-outs. The (go-gpu …) primitive lumbda will expose
   wraps the daemon's stdin protocol — per-tier dispatcher spawns
   one daemon per GPU host at boot, every (go-gpu …) form routes
   through the existing daemon. CUDA init never re-runs while
   lumbda is up.

3. DESIGN-go-gpu.md captures the architecture sketch fox proposed:
   Go-keyword-style coroutines that ship S-expressions to a remote
   GPU box, like vLLM inference but for arbitrary lumbda forms backed
   by a registered CUDA primitive. Wire protocol, scheduling,
   failure semantics, per-tier integration cost, and the four open
   questions for fox to lock the keyword + scope.

README.md gains the full perf table, the daemon protocol, & honest
documentation of when GPU is the wrong tool (SHAKE256 is too light;
real wins are in our ecdsa/cuda/sim_gpu.cu kernel that does 30 G
ops per launch and spends 99% of wall time in the kernel itself).
2026-06-04 18:43:06 -04:00
f11e310b24
examples: cuda-fanout reference primitive for cross-tier GPU work
Establishes the integration pattern for lumbda's future cuda primitive
across Python / C / asm tiers without dragging the CUDA toolchain into
lumbda's core build.

Shape: leaf binary that every tier spawns via its existing process-
spawn primitive & talks to through S-expression input + output
portals. Asm tier inherits via fork + execve syscalls; no libcudart
linkage; no DKMS dependency at lumbda build time.

Files:
  shake256-fanout.cu   self-contained CUDA SHAKE256 fan-out, Keccak
                       permutation derived from FIPS 202 reference
                       (tiny-sha3 lineage, CC0 → re-licensed AGPLv3)
  Makefile             nvcc build + make test + make bench
  test_roundtrip.py    validates output byte-identical to
                       hashlib.shake_256
  bench.py             device vs host throughput at N = 1k / 10k / 100k
  lumbda-call.lsp      reference Scheme wrapper showing the
                       (cuda-shake-fanout inputs out-bytes) API shape
                       lumbda's core would dispatch to per-tier
  README.md            full integration story, wire contract, the
                       three changes each tier needs (~20 LoC each),
                       generalization path for other CUDA primitives

Tested on 3090-ai (RTX 3090):
  make test  → PASS — 4 / 4 hashes byte-identical to hashlib.shake_256

Honest bench (32-byte inputs):
  N         host (Python hashlib)  device (kernel launch dominated)
  1,000              0.6 ms                  188.1 ms
  10,000             5.9 ms                  195.7 ms
  100,000           58.7 ms                  313.8 ms

Useful primitive when inputs are larger (KB+) or N reaches millions;
honest about the launch-overhead break-even point. This is the
reference, not the win — the win is locking the API shape so each
tier registers under one stable name.

Provenance: extracted as the generic pattern from
~/git/www.foxhop.net/ecdsa/cuda/sim_gpu.cu where on-device SHAKE
delivered 2.6× memory compression for batched reversible-circuit
simulation. Re-shipping the primitive back to the lumbda repo so the
ecosystem inherits the work.
2026-06-04 17:50:48 -04:00
2061cb169a zoe-favorites-test: cover all four tiers (Python + C + asm + asm-full)
Two changes, one wiring.

1. rhoff gets a Birthday-bound iteration cap. Pollard rho expects
   ~√n iterations before a collision; capping at 4·√n + 32 lets
   honest runs finish while rejecting pathological c values quickly.
   rho's outer retry draws a new c and keeps the total work bounded.
   Without this cap, a bad c on the non-GC asm tier could allocate
   let* bindings every iteration until virtual memory ran out.
   (factor 91) and (factor 1001) now complete across many random
   seeds on default asm; Zoë's Scheme port passes end-to-end.

2. tests/ursa-scheme.lsp — Scheme-port-only half of the acceptance
   suite. Zero macros, so it runs under every tier including the
   minimal asm (which has no cl-compat). Also drops the vector
   literal `#(...)` (asm reader does not accept) in favor of
   (vector->list (digits …)) and drops the `(exit 1)` trailer
   (asm has no `exit` builtin). The new file is 15 assertions
   covering expt-mod, Miller-Rabin, factor, Mersenne / Lucas-Lehmer,
   repunit-value, digit round-trips, and of-n-bits.

3. tests/cl-compat.lsp — the multiple-value-bind test is commented
   out. It uses `values` / `call-with-values` which exist in Python
   and C as builtins but not on asm-full. The cl-compat macro itself
   is still exercised by Python and C; asm-full skips this specific
   check rather than fail. The full 44 remaining assertions all pass
   on every tier now.

4. tests/zoe-favorites-test.sh — extended coverage matrix:

      Python   cl-compat + ursa (Scheme + CL)
      C        cl-compat + ursa
      asm-full cl-compat + ursa
      asm      ursa-scheme (port only — no macros on minimal)

   The old script ran two tiers (Python + C). Now it runs seven
   test/tier pairs. The run_one helper grew a post-hoc output check:
   any line starting with FAIL: or a missing "N passed" signature
   marks the run as failed; non-zero exit from asm (which always
   exits 1 on EOF) is not itself a failure.

   Final line updated to "All Zoë-favorites tests passed (Python +
   C + asm + asm-full)".

make test-all stays green.
2026-04-24 12:27:51 -04:00
192118388f cl-compat: run Zoë Trout's favorites unchanged (ticket 0004)
Zoë Trout's favorites at wedgewack.org/ursa.lisp.txt are Common Lisp:
iterative LOOP macros, setf cascades, defun with &optional, image-
based stone-lisp culture. Her first contribution to lumbda was a
question — "do we care for our programs, and how long are they alive
for?" — and the answer now extends beyond the RNG portal (§7.5) to
iteration style itself.

Four-phase delivery, all under ticket 0004:

  Phase A — idiomatic Scheme ports at examples/ursa-scheme.lsp.
    Every Zoë defun rewritten as named-let + tail recursion + list-
    backed work queue + type-predicate dispatch.

  Phase B — CL compat shim at cl-compat.lsp.
    defun (with &optional), setf (simple vars, multi-pair), flet,
    multiple-value-bind, t / nil (nil=#f so cond/if compose),
    evenp/oddp/plusp/minusp/zerop, mod/ash/logbitp/nreverse,
    cl-when/cl-unless (plain when is a void-returning lumbda special
    form), declare (no-op), cddddr (missing accessor).

  Phase C — cl-loop macro covering 14 patterns.
    while/until/repeat, for VAR from A to/below/downto B, for VAR =
    INIT [then STEP], for VAR across VEC, of-type T, do, when/unless
    return, finally (return VAL). Sequential do*-style stepping via
    gensym + cl-subst. Look-ahead termination so `repeat 4 for s = 4
    then (- (* s s) 2) finally (return s)` returns 37634 (pre-step)
    rather than 1416317954 (post-step). Every expansion ends in a
    named-let tail call — TCO holds for loops of any length.

  Phase D — load examples/ursa.lisp.txt with minimal annotation.
    Preserves Zoë's CL. Minimal edits documented in file header:
    load cl-compat.lsp, loop→cl-loop, when→cl-when, random→random-int,
    &key→&optional. rho/digits omitted (need make-array/CLOS — see
    ticket 0004 for scope boundary).

Defect uncovered along the way (c/types.c env_lookup): a "global
shortcut" checked global env immediately after missing the local
frame, SKIPPING intermediate parent scopes. Broke lexical scoping
whenever a parent scope shadowed a global. Reproduced with
  (define s 4)
  (let ((s 100)) (let ((m 0)) s))  ; returned 4, should return 100
Any nested let whose body referenced a shadowed name silently read
the global. Fix: remove the shortcut, walk the parent chain end-to-
end. 1255 assertions across five suites pass unchanged after fix —
surfaced only because cl-loop iterator names routinely collide with
globals accumulated in a stone-lisp image.

Whitepaper §9.2 documents the CL-in-Scheme design and the guarantees
that survive (TCO, portal determinism, cross-impl reproducibility).
Zoë added to authors + acknowledgments; reacknowledgment reframes
her first contribution as the deeper program-lifetime question, with
RNG portal as a derivative (§7.5) and cl-loop as the follow-up.

Tests: tests/cl-compat.lsp (44 assertions) and tests/ursa.lsp (28
assertions) exercise both paths under Python + C via tests/zoe-
favorites-test.sh, wired into make test-all.

MOAD notes: unmoad flags memq/assq in cl-compat.lsp over cl-loop-
keywords (~30 elements, constant) and var->new (≤4 state vars per
loop). Both are macro-expansion-time, bounded-small-N — not runtime
hot paths. Pre-existing c/types.c findings (strcmp-in-loop for
record-type lookup) are not from this change.
2026-04-24 07:02:21 -04:00
94b29421ed lumbda-www: sendfile(2) primitive + adaptive preload — matches caddy throughput at 9x less RSS
asm-gc gains (tcp-sendfile socket path) → builtin (90 lines) that issues
SYS_SENDFILE(40) in a loop, streaming a file from fd → socket with no bounce
through the Lumbda heap. Zero-copy kernel path for large responses.

examples/http-static-server-sendfile.lsp (hybrid): small assets
(≤ 16 KB) stay inline-cached as full HTTP responses; large assets cache
only headers and stream the body via tcp-sendfile. 4-way race on
i5-8350U, 100 PDF requests (2.56 MiB), concurrency 8:

  uncached   159 req/s    406 MiB/s   15.5 MB RSS
  cached     238 req/s    603 MiB/s    7.2 MB RSS
  sendfile   480 req/s   1226 MiB/s    4.2 MB RSS
  caddy      485 req/s   1238 MiB/s   37.1 MB RSS

sendfile lands within 2% of caddy on throughput with 9x less peak RSS in
a 27 KB binary vs caddy's 38 MB (1400x smaller).

examples/http-static-server-adaptive.lsp (learning preload): per-URL hit
counter persisted to www.hits every N requests. At boot, ranks and
preloads top *cache-max* URLs from the prior run's data (cold-start
falls back to a seed list). Cold requests beyond the seed promote on
first hit. Drops heap-restore arena pattern since the server mutates
persistent state every request; relies on GC build's mark-sweep.

tests/bench-www-race.sh: adds sendfile variant on port 8083, auto-sizes
PDF byte count from the on-disk whitepaper so a whitepaper rebuild
doesn't desync the MiB/s calc.

Whitepaper §11.7 "Static File Serving: Cache, Sendfile, and Adaptive
Preload" documents the four variants, benchmark table, and the
arena-vs-mutation tradeoff. §13 Future Work adds DAG-of-hot-paths
predictive preload as the direction for > 1000-resource deployments
where frequency-only ranking is too narrow.
2026-04-19 12:41:40 -04:00
ed90adc451 lumbda-www cached: in-memory hash-table cache, 1.64x PDF throughput
examples/http-static-server-cached.lsp — same HTTP server but every
preloaded URL's full HTTP/1.0 response (headers + body) is composed
once at startup and stored in a hash-table, so the per-request
handler is a single hash-table-ref/default. No file->string, no
string-append, no MIME lookup in the hot path.

Config knobs:
  *docroot*        filesystem root (default "www")
  *cache-max*      soft cap on cached entries (default 100)
  *preload-paths*  list of URL paths to pre-fetch at startup

Preload list for lumbda.com: "/", "/style.css", "/whitepaper.pdf",
"/robots.txt", "/404.html" — anything not in the list returns the
cached 404 response (no disk hit). Cache lives pre-snapshot so
heap-restore never reclaims it; RSS stays at the cache size
forever.

Race vs caddy on this laptop (2000 small / 200 large, concurrency 8):

                         small req/s  PDF req/s  PDF MiB/s  peak RSS
  lumbda-www uncached     690          195         495      15.5 MB
  lumbda-www cached       686          319         811       7.2 MB
  caddy file-server       688          478       1,217      38.9 MB

Cache wins on the PDF: 1.64x faster than uncached, RSS DROPS from
15.5 MB to 7.2 MB because the cached path allocates nothing per
request (all allocation happened pre-snapshot). On small files
already-hot paths mean the cache is a wash — 690 vs 686 is noise.

Caddy still wins 1.5x on the PDF via sendfile(2) zero-copy; we
allocate the 2.67 MB response once at startup and tcp-send it.
Closing the gap further would take a sendfile asm primitive —
separate project. For a minimal static site serving its own
whitepaper, the cached 27 KB asm binary is viable: 319 req/s
and 811 MiB/s with 5x less memory than caddy.

tests/bench-www-race.sh updated to run all three side-by-side
(uncached + cached + caddy) at three ports. Cached server's port
is patched via sed at the entry point so the two lumbda variants
don't collide. PDF byte-integrity checked on all three paths.
2026-04-19 12:18:56 -04:00
dd961d2133 lumbda-www: asm-gc static file server for lumbda.com + caddy race
Ships examples/http-static-server.lsp — ~65 lines of portable Scheme
that reads files from a docroot (default ./www) and serves them over
HTTP/1.0 with MIME dispatch, path-traversal rejection, heap-snapshot
per request. Runs in any tier; target deployment is asm-gc for the
27 KB stripped binary + bounded memory backstop.

Required one asm fix first: heap_grow was mmap'ing fixed HEAP_SIZE
chunks, so any single allocation larger than a chunk (notably the
2.67 MB whitepaper PDF read via file->string) loop-looped through
.ha_overflow forever. Now heap_grow rounds required bytes up to
HEAP_SIZE multiples on oversize alloc, so a big request carves its
own big chunk in one go. Small allocs still land in standard-sized
chunks.

Two new benches:

tests/bench-lumbda-www.sh — drive N small + M large requests against
asm-gc, verify PDF round-trip, sample peak RSS. At 1000/100: 331 req/s
small, 120 req/s large (304 MiB/s), peak 15.5 MB.

tests/bench-www-race.sh — adjacent A/B vs caddy v2.5.1 on the same
docroot. Numbers on this laptop, concurrency 8, 2000 small + 200 large:

                         small req/s  PDF req/s  PDF MiB/s  peak RSS    binary
  lumbda-www (asm-gc)     375          137         349       7–16 MB    27 KB
  caddy file-server       358          231         588       38 MB      38 MB

Reading: lumbda edges caddy on small files (less per-request overhead),
caddy wins 1.7x on large files (sendfile zero-copy; we allocate the
whole file into a string and write it with one syscall). Both byte-
identical on the PDF. Memory: lumbda 2.5-5x less at steady state.
Binary size: 1400x smaller (27 KB vs 38 MB).

Feature gap: caddy has HTTPS, HTTP/2, range, middleware, etc. lumbda
has none of that yet — but for the specific job of serving lumbda.com's
six-file docroot it is viable right now.

Makefile adds `bench-lumbda-www` and `bench-www-race` targets.
137 asm no-GC + 137 asm GC tests still pass.
2026-04-19 12:12:59 -04:00
f7352b51b0 rename: uncommonlisp -> lumbda throughout the repo
Historical internal name "uncommonlisp" retired in favor of the
public name "lumbda" ahead of lumbda.com going live. Scope of
this commit:

Source files renamed:
  uncommonlisp.py                     -> lumbda.py
  asm/uncommonlisp.s                  -> asm/lumbda.s
  c/uncommonlisp.h                    -> c/lumbda.h
  whitepaper/uncommonlisp-whitepaper  -> whitepaper/lumbda-whitepaper (.rst + .pdf)

Binaries renamed (tracked ones; c/ was always gitignored):
  asm/uncommonlisp, asm/uncommonlisp-gc, asm/uncommonlisp.o,
  asm/uncommonlisp-gc.o                -> asm/lumbda(-gc)(.o)
  c/.gitignore                          -> ignores lumbda

Internal string updates (sed pass ordered longest-first):
  asm/uncommonlisp -> asm/lumbda
  c/uncommonlisp   -> c/lumbda
  uncommonlisp.py  -> lumbda.py
  UNCOMMONLISP_BIN -> LUMBDA_BIN (asm/test.sh env var)
  "uncommonlisp> " -> "lumbda> " (asm REPL prompt baked into binary)
  UNCOMMONLISP     -> LUMBDA (macros, comments)
  uncommonlisp     -> lumbda (prose)

Binary portal magic updated:
  "ULPORTAL" -> "LUMBDAB1"   # "Lumbda Binary v1"
Old portal files are not backward-compatible — this is a deliberate
break since it's the rename moment. S-expression portals already
carry their own ";; lumbda-portal v1" header and remain cleanly
versioned.

WHITEPAPER.pdf / WHITEPAPER.rst symlinks repointed to the renamed
files. Makefile's whitepaper target targets lumbda-whitepaper.pdf.

Not changed (intentional, separate phases):
  - Filesystem directory /home/fox/git/uncommonlisp itself
    (fox renames locally and the gitlab repo URL in a follow-up)
  - tests.py hardcoded cwd=/home/fox/git/uncommonlisp
    (matches the current on-disk location; will flip when the
    directory rename ships)
  - Git history (immutable; old commits still say uncommonlisp,
    which is correct — that's what they were)

Verified:
  137 asm no-GC + 137 asm GC + 571 Python + 83 C + 189 shared
  functional tests all pass under the new names.
  bench-gc-http (2000 req): all 4 cells behave as expected
  (cells 1/2 flat, 3 leaks, 4 bounded at 1 chunk).
  Python REPL, C REPL, asm REPL all start cleanly.
2026-04-19 10:20:11 -04:00
3348e9b4bd bench-gc-http + asm-gc rows in existing benches; §6.6.4 HTTP validation
New infra:
  - examples/http-server-noarena.lsp: same HTTP server minus the
    heap-snapshot/heap-restore arena loop. Isolates whether the GC
    build actually holds memory under real traffic, independent
    of the portable snapshot pattern.
  - tests/bench-gc-http.sh: drives 5,000 concurrent requests per
    cell across the full 2x2 matrix {no-GC, GC} x {snapshot, no}.
  - Makefile: new `bench-gc-http` target.

Extended benches to exercise both asm binaries:
  - tests/bench-hashset.sh now runs against both asm/uncommonlisp
    and asm/uncommonlisp-gc, with set +e so a GC-build crash on
    one workload doesn't abort the other.
  - tests/web-benchmark.sh adds a dedicated asm-gc row (and prints
    its stripped binary size) so the HTTP throughput comparison
    reports both.

Whitepaper updates:
  - §6.6.4 "Validation: HTTP Server Under Sustained Load" — the
    4-cell memory matrix. 3/4 cells green; cell 4 (GC + no
    snapshot) crashes at first GC trigger — another instance of
    the conservative-scan type-confusion class we already fixed
    once at the env/string boundary. Logged as a known issue
    rather than shipping a partial fix under time pressure.
    heap-snapshot + heap-restore remains the recommended pattern
    for production asm code; the naive GC is diagnostic + control
    group, not a replacement for the arena discipline.
  - §6.5 hash-set speedup table slightly softened to ~15-20x (was
    15-21x) since run-to-run noise on a shared laptop shifts the
    per-phase ratio by a few percent. Ratio is stable to first
    order.
  - §8.6 narrative references the ~1280x symbolic-vs-brute-force
    figure instead of the stale 40x.
  - §6 reproducibility list now lists `make bench-gc-http`.

All 137 asm no-GC + 137 asm GC + 189 shared functional tests
still pass.
2026-04-18 16:13:59 -04:00
3d55092037 asm-gc: adaptive EMA-driven meta-GC policy + bench + two correctness fixes
Moves the meta-GC from greedy (always verify) to adaptive: track a
scaled EMA of recent escape rate; when rate exceeds 50% (128/256),
SKIP the verifier and let the heap grow until natural GC; every 16
skipped arenas, force a verify as a probe to re-sample the rate.

Two correctness fixes uncovered while testing adaptive:

1. gc_mark_env was picking up 24-byte strings and closures as if
   they were env nodes (size check alone is ambiguous). Now also
   requires offset 0 to be tagged TAG_SYM, which env nodes always
   are and strings/closures never are.

2. gc_mark_drain's vector/hash-table dispatch walked `length`
   elements without sanity-checking that `8 + length*8` fits in
   the block. A 25-char string (40-byte payload) misinterpreted as
   a 25-element vector walked 200 bytes off the end, reading
   adjacent blocks' bytes as tagged roots and setting mark bits on
   wrong things. Both paths now validate the header's payload-size
   against the claimed length / nbuckets before walking.

New builtin:
  (arena-set-mode 0|1) — 0 = greedy baseline, 1 = adaptive (default)

arena-stats extended to six fields:
  (calls resets escapes skipped bytes-reclaimed ema-rate)

Bench (tests/bench-gc-adaptive.sh, one process per phase to isolate
a separate latent cross-phase bug we haven't cracked, N=1000 per
phase, i5-8350U):

  workload   mode     time_ms   resets   escapes   skipped
  friendly   greedy    732      1000        0         0
  friendly   adapt     691      1000        0         0
  hostile    greedy    568         0     1000         0
  hostile    adapt     607         0     1000        11
  mixed      greedy   1981        17     1983         0
  mixed      adapt    1694        14     1986         2

Adaptive wins on friendly (-6%) and mixed (-17%). On fully hostile
workloads both modes are dominated by implicit full-GC firings
(982/1000 arenas trigger heap overflow that clears arena_active
before reaching the policy), so adaptive barely activates and
greedy happens to edge out by ~7%. The mixed result is the clear
adaptive win — and the one that matches the pattern the policy was
designed for: probe-and-adapt as the workload shifts.

137 asm (no-GC) + 137 asm (GC) + 189 shared functional tests all
still pass.
2026-04-18 11:33:39 -04:00
a8eddd492e asm-gc: meta-GC layer — arena fast path with mark-phase verifier
Adds (with-arena thunk) as the O(1) bulk-reclaim fast path on top
of the existing naive mark-sweep. The meta-GC:

  1. Snapshots %r15 at arena entry.
  2. Sets arena_active=1 so heap_alloc bypasses the free list
     during the arena body (keeps the chain pristine for restore).
  3. Invokes the thunk via apply_proc_raw.
  4. Zeros volatile registers after apply_proc_raw returns, so the
     conservative stack scan in verify doesn't see stale tagged
     pointers that apply_proc_raw left behind (they'd otherwise
     look like live roots pointing into the arena — false escape).
  5. If an implicit GC fired during the thunk (heap overflow
     cleared arena_active), skips the reset — snapshot is stale.
  6. Otherwise runs the existing mark phase plus the thunk's
     return value as an extra root, then walks [snap_r15, %r15)
     by block headers checking for any marked block. None marked
     -> bulk-reset %r15 to snapshot (O(1) reclaim of the whole
     arena range). Any marked -> escape, fall through to naive
     sweep on the full range.

Two new GC-build builtins:
  (with-arena thunk) -> thunk's return value
  (arena-stats)      -> (calls resets escapes bytes-reclaimed)

Meta-GC benchmark (tests/bench-gc-arena.sh, i5-8350U, 2000 iters
of build-sum-discard over 200-element lists):

  Phase A (naive sweep only):
    time=932ms  gc-collections=200  arena=unused
  Phase B (arena-wrapped, same workload):
    time=945ms  gc-collections=1    arena=(2000 2000 0 205_392_000)

Arena reset rate on this truly-transient workload: 2000/2000 =
100%. Bytes reclaimed via O(1) bulk: 205 MB across the run with
only 1 full mark-sweep firing (for the initial global env). Time
is within ~1% of naive-only — the arena verify's mark cost is
comparable to the sweeps it replaces on this workload, but with
bounded per-iteration latency (no jitter from pressure-driven
sweeps) and the stats machinery to prove it.

Escape detection tested: when the thunk returns a pair that the
caller captures (set! escaped (with-arena ...)), every arena
correctly reports escape and keeps the data live via the
fall-through sweep. 137 asm (no-GC) + 137 asm (GC) + 189 shared
functional tests still pass.
2026-04-18 10:01:28 -04:00
489776baa4 asm: naive stop-the-world mark-sweep GC as a control group
Adds a second asm build (asm/uncommonlisp-gc) behind the GC_NAIVE
assembler flag, providing the benchmark baseline we previously had
no data for. Same binary, same surface, different allocator:

  - 8-byte header per heap block (size << 1 | mark), placed at -8
    from the tagged pointer so existing untag + offset accesses
    stay unchanged.
  - Chunk list tracked in a side array, letting sweep walk every
    mmap'd region by header-chained blocks instead of guessing.
  - Free list rebuilt each sweep, first-fit alloc with split on
    large-leftover (>= 24 bytes).
  - Mark phase enumerates five root classes: %r14 (global env,
    untagged chain), sym_else_val, sym_table entries, every
    sym_hash_bucket chain, and a conservative scan from current
    %rsp to the initial stack_top captured at _start. The stack
    scan runs twice per word — once as a tagged value, once as a
    potential untagged env-node pointer (size-guarded to 24 bytes
    so it can't walk off a wrong-size block).
  - Transitive marking via an explicit 16K-entry mark stack;
    gc_mark_env walks untagged env chains from %r14 and from every
    closure's env field.
  - heap_alloc preserves the non-GC ABI (only %rax clobbered) so
    existing callers like bi_append, which holds state in %rcx
    across make_pair, keep working.
  - Overflow path uses check-then-write bumps and pads the old
    chunk's tail with a single dead block before growing, so sweep
    never walks into uninitialized mmap'd memory.
  - HEAP_SIZE shrinks to 1 MB under GC_NAIVE so the collector
    actually runs on ordinary workloads.
  - Two diagnostic builtins in the GC build: (gc-collect) to force
    a collection, (gc-stats) -> (collections . live-bytes).

Control-group bench (examples/bench-gc-memory.lsp, 2000 iterations
of build-sum-discard over 200-element lists, i5-8350U):

  tier           time_ms   peak_rss   final_rss
  asm no-GC       1097     133.9 MB   133.9 MB   (grows, never shrinks)
  asm naive GC    1431       1.1 MB     1.1 MB   (steady state)

124x less memory at a ~30% throughput cost. That is the number we
were guessing at before. Reproduce: make bench-gc.

Tests: 137 asm (no-GC) + 137 asm (GC) + 189 shared functional pass.
The two asm builds are tested independently via UNCOMMONLISP_BIN in
asm/test.sh; asm/Makefile now builds both and exposes a test-gc
target.
2026-04-18 09:34:51 -04:00
afb5616843 asm: native hash-set + benchmark — 15-21x over portable
Adds 6 hash-set builtins (make-hash-set, hash-set?, hash-set-add!,
hash-set-contains?, hash-set-size, hash-set->list). Same sentinel
scheme as hash-table but tag word = -2 (hash-table is -1, vector
is >= 0). One cons cell per entry (vs two for hash-table) since
a set stores keys only — that's where the speedup over the Scheme-
level vector-based ht-* lib comes from.

Benchmark (tests/bench-hashset.sh, via make bench-hashset),
N=5000, i5-8350U asm tier:

                portable   native   speedup
  insert        ~130 ms    ~7 ms    ~20x
  hit-lookup    ~125 ms    ~8 ms    ~15x
  miss-lookup   ~240 ms    ~12 ms   ~20x

Portable is the ht-* lib from proof-netspace-server-lib.lsp
(vectors + cons chains + modulo, pure Scheme). Native replaces
the Scheme-level bucket walk with an asm loop that dereferences
pairs directly — no env lookups, no frame building per iteration.

All 137 asm + 189 functional (Python + C) tests still green.
2026-04-18 06:15:21 -04:00
5ea687a888 proof netspace: 2-node spiral demo — independent caches converge
Extracts the 300-line server body into proof-netspace-server-lib.lsp
so multi-node demos can share it without duplication. The existing
proof-netspace-server.lsp entry point stays stable — now a 25-line
config wrapper that sets defaults and loads the lib.

New 2-node scaffolding:

  proof-netspace-node-a.lsp  — port 9086, cache /tmp/lumbda-A-*
  proof-netspace-node-b.lsp  — port 9087, cache /tmp/lumbda-B-*
  spiral-client.lsp          — drives both nodes, seeds them with
                               partially-overlapping theorem sets,
                               runs one A→B and one B→A envelope
                               round-trip, reports sizes
  spiral-demo.sh             — orchestrator: starts both nodes,
                               runs client, tears down cleanly.
                               Accepts python|c|asm — all three
                               converge identically (A=3 B=3 → A=5 B=5).

Proves the envelope primitive at use-case scale: N independent caches
mesh-converge in O(N) spiral passes. Foundation for the "looping and
spiraling across time and space of manifolds" runtime topology.
2026-04-18 05:29:23 -04:00
d88149a502 proof netspace: envelope teleport + portable hash-table on vectors
Extends proof-netspace RPC with two verbs that let peers exchange the
full solution space in one round-trip:

  (envelope)           → reply (envelope (h1 h2 ...))
  (merge (h1 h2 ...))  → fold hashes into local DB, reply (merged N)

Any node can now bootstrap from a peer's cache instead of re-verifying
every theorem locally. Two nodes that swap envelopes both become
supersets of what either knew — the primitive for mesh-wide spiral.

*proof-db* swapped from linear alist to a hash-set. O(N·M) merge drops
to O(M). The hash-table is a ~20-line pure-Lumbda library over
make-vector / vector-ref / vector-set! — runs unmodified in all three
tiers. No asm hash-table primitive needed.

Also fixes a pre-existing asm defect: bi_makevec clobbered %rax via
the GETARG macro's internal scratch use, causing SIGSEGV on every
(make-vector N fill) call. The bug shipped because asm/test.sh only
covered the variadic (vector ...) constructor; tests/functional.lsp
had one make-vector assert but was never wired into asm's harness.
Added five make-vector assertions to asm/test.sh (132 → 137).

Portal snapshot rewritten to emit (set! *proof-db* ...) so the
top-level binding is actually mutated on restart — previous
(define ...) form bound locally on some code paths, leaving the
in-memory DB empty after load.

Verified: make test-all green (137 asm + 189 functional + Python/C
tests), 3-tier matrix cold+warm+restart all clean.
2026-04-17 21:36:26 -04:00
06b93c588a portal over HTTP: 9/9 cross-runtime, plus eval-to-global-env fix
Closes the last loop promised in the whitepaper's Future Work: a
node serves its state as an S-expression portal over HTTP, another
node pulls it down with tcp-connect + tcp-recv and materializes the
bindings locally via (eval (read-from-string line)).

examples/portal-http-server.lsp (90 lines):
- Holds some state (counter, my-int, my-list, my-fib, my-str)
- GET /portal → S-expression body: a sequence of (define ...) forms
- GET / → HTML index
- Uses heap-snapshot / heap-restore for O(1) memory on asm

examples/portal-http-client.lsp (90 lines):
- tcp-connect, send HTTP/1.0 GET, receive full response
- Strip headers (walk to first \r\n\r\n)
- Split body by \n, eval each non-empty, non-comment line
- The remote bindings are now live locally

3×3 server/client matrix: all 9 combinations green. Every runtime
hosts, every runtime consumes. The wire format is Scheme source;
no schema, no JSON, no Protobuf.

Prerequisite fix: `eval` semantics aligned across all three impls.

Python and C's `eval` special form previously evaluated its result
in the CALLER's env, so a nested (eval (read-from-string
"(define x 42)")) would install x in the local function scope —
invisible to later top-level code. asm's bi_eval always used the
global env (r14). With this commit, all three impls evaluate the
eval'd result in the global env, matching asm's existing behavior.

Python: uncommonlisp.py leval eval-handler now does `env = env.g`
before continuing the trampoline.
C: c/eval.c SYM_EVAL branch now does `env = env->global`.
asm: no change (already correct).

One pre-existing Python defect surfaced by the client:
`count` is a SRFI-1-style builtin (`d(S('count'), ...)`), so a
local let-loop variable named `count` collides with it in the
inline-cache lookup path and OP_LOOK_ADD1 fires on the builtin
instead of the local. Worked around by renaming the loop
accumulator to `cnt`. Underlying Env.lookup shortcut-to-global
issue is out of scope for this commit.

Regression: 975 tests still green.
2026-04-17 13:41:48 -04:00
ccf86e3c3f rpc-chain-bench: Python → C relay → asm, timing end-to-end
Adds a transparent S-expression relay (examples/rpc-relay.lsp) plus a
sequential load generator (examples/rpc-chain-bench.lsp) and a bench
script (tests/rpc-chain-bench.sh) that wires them into multi-hop
chains across runtimes.

The relay is pure byte-forwarding: tcp-accept, tcp-recv, tcp-connect
to backend, tcp-send, tcp-recv reply, tcp-send back. Never parses.
Which is the point — S-expressions are the envelope.

Same rpc-relay.lsp runs as relay in any impl; chains are arbitrary
combinations of {Py, C, asm} nodes.

Measured (200 requests, ping, same laptop):

  (A) Py client → asm backend            direct, 1 hop   2061 rps
  (B) Py client → C relay → asm          2 hops          1234 rps
  (C) Py client → Py → C → asm           3 hops           766 rps
  (D) asm client → Py → C → asm          3 hops           796 rps

Per-hop cost ≈ 600-700 µs/request (TCP round-trip + context switch).

Safety: every server spawn used the six-layer pattern from CLAUDE.md
(ulimit -v 512MB + timeout 30 + trap + explicit kill + pgrep verify).
Four benchmark cells × up to 3 servers each = 10+ server spawns.
Zero strays, zero safety-net activations.
2026-04-17 09:18:42 -04:00
574ddc50d7 S-expressions over sockets — RPC + remote REPL in portable Scheme
Fuses portal (Scheme-source-as-interchange) with sockets (bytes over
the network). Wire protocol: one S-expression per connection. Same
server + client .lsp runs byte-identically in Python, C, and asm.

New primitives in all three impls:
- read-from-string — parse one sexp from a string

Asm gets two more:
- symbol->string — standard R7RS, was missing
- eval — evaluate a Scheme value in the global env (Python + C had
  it as a special form; asm exposes it as a builtin)

examples/rpc-server.lsp (port 9080):
- Whitelisted dispatch: ping / add / mul / fib / echo
- Never calls eval on client input; safe by construction
- Uses heap-snapshot/restore for O(1) memory on asm
- ~90 lines, portable

examples/rpc-client.lsp:
- Sends one request, reads one response, displays both
- 45 lines, portable

examples/repl-server.lsp (port 9081):
- DANGER: full remote eval. Any Scheme form accepted and evaluated
  in the server's global env. Persistent across connections.
- Deliberately does NOT use heap-snapshot — remote (define x ...)
  lives in the global env above any snapshot point; rewinding would
  invalidate the new binding. The ulimit -v 512 MB safety net
  (documented in CLAUDE.md) ensures an escaped process can't crash
  the machine.
- ~70 lines, portable. Demonstrates what "the language IS the
  interchange format" gets you at the limit: a single socket and
  a single primitive (eval) carry a full-powered REPL.

Verified 3×3 server×client matrix: all 9 combinations green.
All 132 asm + 571 py + 189 shared + 83 c tests still pass.

One quirk discovered and worked around: in asm, a closure captures
its env chain by pointer at define time. Forward-referenced names
in mutually-recursive toplevel defines can fail under specific
heap-restore patterns — see the leaf-first ordering note in
rpc-server.lsp.
2026-04-17 09:00:04 -04:00
b8d6afdeb3 heap-snapshot + native HTTP client + MOAD-0001 cleanup
Three wins in one commit.

1) heap-snapshot / heap-restore (asm arena primitive)
   asm has no GC. Long-running servers leaked ~64 MB per heap growth.
   Two new builtins let a programmer capture r15 and later rewind to
   it, recycling intermediate allocations in O(1) memory.
   Python + C get no-op versions so portable .lsp code can call them
   unconditionally.

   examples/http-server.lsp now takes a snapshot at top level and
   rewinds after every request. Measured asm RSS: 88 KB initial,
   100 KB after 100 requests, 100 KB after 1100 requests — flat.
   Prior behavior was +64 MB per few thousand requests.

2) examples/http-client-bench.lsp — native HTTP load generator
   Uses only the six tcp-* primitives + current-time-ms. Runs
   identically in all three impls. Eliminates curl's ~2 ms/req
   fork+exec overhead, so real server throughput shows up:

     Python server ← Python client   2403 rps
     C      server ← C      client   2439 rps
     asm    server ← asm    client   2994 rps
     asm    server ← C      client   2500 rps

   The earlier curl-based bench was clamped near 400 rps by the
   client; the actual servers handle 6–7× that.

3) MOAD-0001 cleanup
   - c/builtins.c bi_string_replace: strncmp-at-every-position
     (hand-rolled, sedimentary) → strstr (libc-tuned, typically
     Boyer-Moore-Horspool). O(N*k) → O(N + matches*k).
   - uncommonlisp.py _tokenize_lines: per-token src.count('\n', 0, pos)
     → precompute line_starts once, bisect_right per token.
     O(N*M) → O(M + N log M).

Also adds current-time-ms to all three impls so benchmarks can
time themselves without relying on the Python/C float `current-time`
(asm has no floats). Seconds-since-epoch tagged as a 61-bit int.

Test counts unchanged: 571 py + 132 asm + 189 shared + 83 c = 975.
All green via make test-all.
2026-04-16 20:49:32 -04:00
477bd5f7cd guardrails: bound http server, trap+cleanup bench, asm-no-GC in CLAUDE.md
Prevent recurrence of 2026-04-16 incident where a leaked asm HTTP
server grew to 19.3 GB RSS and crashed the machine.

examples/http-server.lsp:
- Adds *max-requests* = 50000 hard ceiling. Server self-terminates
  before unbounded heap growth reaches dangerous levels.
- Loop tracks request count, exits cleanly + closes server socket.

tests/web-benchmark.sh:
- SPAWNED_PIDS array tracks every background process.
- EXIT/INT/TERM trap kills them all (SIGTERM then SIGKILL).
- stop_server does SIGTERM with 500ms grace period then SIGKILL.
- Final straggler check via pgrep narrows to actual HTTP server
  processes (not shell/tmux with "uncommonlisp" in the name).
- pkill -9 fallback as belt-and-suspenders.

CLAUDE.md:
- New "Asm memory discipline" section documents the bump allocator
  leak behavior and the required operational discipline.
- Test counts updated (571 py + 83 c + 132 asm + 189 shared = 975).
2026-04-16 19:38:34 -04:00
bfd4ec7ec8 sockets + portable HTTP server — 6 primitives, same server runs in all 3
Added tcp-listen/accept/connect/recv/send/close to Python, C, and asm.
One examples/http-server.lsp runs identically in all three impls and
serves HTTP/1.0 with routing, content-type, and content-length headers.

asm additions:
- SYS_SOCKET/BIND/LISTEN/ACCEPT/CONNECT/SETSOCKOPT syscalls
- 6 tcp-* builtins using the existing port encoding (SPECIAL ≥ 1000)
- bi_tcp_connect: dotted-quad IPv4 parser, no DNS dependency

Defects fixed along the way (surfaced by the HTTP server):
- string-append: was 2-arg only; now variadic (walks arg list twice)
- number->string: was stubbed to VAL_VOID; now correctly writes digits
  into a heap-allocated string (incl. negative handling)
- String-literal reader: \r and \0 escape sequences now handled (was
  silently dropping backslash, treating them as literal 'r' / '0')
- tcp_accept: sockaddr buffer was 8 bytes, now 16 (was corrupting
  caller's stack when accept wrote full struct sockaddr_in)

Pinocchio benchmark (tests/web-benchmark.sh):
At concurrency=20, 1000 requests, serving a 1KB body:

  uncommonlisp Python   373 req/s
  uncommonlisp C        370 req/s
  uncommonlisp asm      370 req/s
  python3 http.server   381 req/s  (stdlib reference)
  busybox httpd         382 req/s  (production reference)

All five converge within 3% — the client (curl fork/exec) is the
bottleneck, not the server. Our single-threaded blocking servers
are indistinguishable from battle-tested ones at this load.

Binary sizes:
  uncommonlisp asm    45 KB   (HTTP + everything else)
  busybox httpd       2.1 MB  (multi-call binary)
  python3             8 MB    (interpreter)

The asm HTTP server is 46× smaller than busybox and 176× smaller
than Python, serves from 7 Linux syscalls, and the entire protocol
handler is 70 lines of portable Scheme.

Test counts: 132 asm (up 1), rest unchanged. All green.
2026-04-16 18:58:27 -04:00
6b832d5154 Add portal: serialize and resume VM state across machines
Portal saves the full machine state — env chain, compiled procedures,
continuations, frame stack — to a JSON file. Another interpreter
instance loads it and resumes execution from the exact instruction.

Demo: start a primality test on machine A, checkpoint mid-computation,
resume on machine B. 1000000007 prime check: machine B picks up from
i=30000 and finishes in 6% of the original time.

Implementation:
- PortalSerializer: graph-aware with identity tracking for shared env
  references. Handles cycles (closures referencing their own env).
- portal-checkpoint!: triggers mid-execution save from within VM loop.
  Hooks into TAIL_CALL (loop back-edge) for compiled code.
- --portal-resume CLI flag: load .portal file and resume continuation.
- portal-save / portal-resume Scheme builtins.

571 tests green (7 new portal tests: unit + integration + functional).
2026-04-13 19:00:11 -04:00