23 KiB
Poker Sharingan on Platinum
Status: the images, the runner (credit ledger, Stripe webhook, job API, PlatinumClient) and the local stack are built and tested end to end without Platinum or Stripe credentials: see README.md. This document is the hosting design and the Platinum gap analysis; where it still says "skeleton" or "untested" the README's tables are authoritative. Architecture change since this was written: an eval job's sandbox runs the match worker (sharingan-arena run --bot "sharingan pipe bot.py", signed result.json, POST /v1/results), not a WebSocket bot; the WebSocket path survives as mode:"connect". Repo paths marked PT: are in ~/Projects/kortix/platinum (read only).
0. The three facts that shape everything
- The arena loads the blueprint itself.
arena/server/src/house.rs:41-47opensARENA_BLUEPRINTwithCompactPolicy::open(mmap) and serves it ashouse:sharingan-1. Both benchmark tracks (ladder-v1,sharingan-1) need it, and the 4,800 hands/s hosted path only works in-process. So the arena and the ~140 GB blueprint belong on the same machine.sharingan-serve(stateless/v1/act, with turn/river search) is a separate product; running it in the same VM lets both share one page cache (two sandboxes never share guest page cache). That is what thecoreimage target is for. - Platinum cannot host a 140 GB mmapped file today. Hard caps: RAM 32 GiB per sandbox (
PT:apps/api/src/api/sandboxes.ts:473, config clampPT:apps/api/src/config.ts:813), disk 100 GB (sandboxes.ts:475,templates.ts:787,876,1005,1113,1199,config.ts:814), volume 100 GiB (PT:docs/volumes.mdLimits), template size 20 GiB default (config.ts:805). See G1/G2. - Platinum has no custom domains and no private sandbox-to-sandbox network (
PT:apps/api/src/tlsAsk.ts,PT:apps/api/src/sandboxProxy.ts:105-122,PT:infra/03-network.sh:64-66).arena.sharingan.pokerneeds a front door we own, and bots reach the arena over its public URL. See G3/G4.
player -> sharingan.poker (Cloudflare, existing wrangler site)
-> Stripe Checkout -> our billing service (credit ledger)
| POST /jobs (credits reserved first)
v
runner service --Platinum SDK--> Platinum org "sharingan-bots"
| one ephemeral microVM per run
| (template sharingan-bot, egress = arena only)
| arena REST | wss://arena.sharingan.poker/v1/play
v v
arena.sharingan.poker --Cloudflare Worker--> Platinum org "sharingan-core"
app/sandbox "core": sharingan-arena :8080 + sharingan-serve :8081
bigmem pool host, 256+ GB RAM, blueprint on local disk, SQLite or Postgres
Two Platinum orgs on purpose: at balance <= 0 Platinum auto-stops running sandboxes after a 60 minute grace (PT:docs/billing.md Zero balance). Bot spend must not be able to stop the arena.
1. Mapping
(a) Arena server (ghcr.io/<org>/sharingan-arena, docker/Dockerfile --target arena)
- Shape: a Platinum App (
/v1/apps,PT:apps/api/src/api/apps.ts), source{kind:"image"}pinned tag,port: 8080,health_path: "/healthz"(arena routearena/server/src/api.rs:27). The app gives a stable URL<slug>.apps.<domain>, env/secret injection (runtime_env, secrets encrypted at rest), blue-green rollout (PT:apps/rollout.tsdeploy()), rollback (POST /v1/apps/:id/rollback), and a GitHub push webhook if we use{kind:"git"}(Dockerfile in repo;handleGitPushinapps.ts, GitHub App connectionapps/github.ts). WebSockets pass through the app/preview proxy (sandboxProxy.tsWS path). - Do not use the app path for the stateful production arena until G5 is fixed. The rollout boots a fresh VM from the template on every deploy (
rollout.ts:128-148, novolumesincreateBody), so SQLite on the rootfs is lost on each release. Until then run the arena as a plain persistent sandbox:sandboxes.create({template, type:"persistent", expose:[{port:8080, public:true}], env, backup_interval_min: 15}), upgrade by snapshot + replace by hand. - Persistence.
ARENA_DB=/data/arena.db. Two honest options:- SQLite on the sandbox disk (persistent sandbox,
auto_stop_minutes:0,auto_archive_days:0), plus a 5 minutesqlite3 .backupto S3. Fine for launch traffic. - SQLite on a Platinum volume: not recommended. A mount is a private working copy committed about every 5 s or on
fsync(PT:docs/volumes.mdCommit and pull); SQLite fsyncs every transaction and a second VM (rollout overlap) would fork the file. - Move to Postgres when any of: more than one arena instance, blue-green deploys needed, or hand-history volume past ~50 GB.
store.rsalready has theStoretrait; the Postgres impl is the work (about 1-2 days). Use a managed Postgres (not on Platinum).
- SQLite on the sandbox disk (persistent sandbox,
- Config:
ARENA_BIND=0.0.0.0:8080(default is loopback, already set in the image),ARENA_ADMIN_TOKENfrom a Platinum secret/env,ARENA_ENABLE_WEBHOOKS=false,ARENA_OPEN_REGISTRATION=truebehind Cloudflare rate limits (the arena has none; README Known gaps). - Without blueprint (
USE_BLUEPRINT=0) this fits Platinum today (4 vCPU, 8 GB):ladder-v1andsharingan-1return 400, but plain duplicate matches against the other house bots work. That is Phase 1.
(b) SHARINGAN-1 inference (needs big memory)
- Target
core(arena + serve in one VM, entrypointdocker/entrypoint.sh): 32+ vCPU (search threads 16, concurrency 2, plus arena), RAM >= 256 GB (140 GB page cache plus working set), disk >= 400 GB local NVMe. The blueprint is pulled once from S3 (ASSETS_S3_URI, bucketpluribus-repro-2026or a copy) andcat-warmed. Image carries no weights. - What Platinum needs (G1, G2): raise the RAM and disk ceilings, enrol one large host into a dedicated pool. Pools already exist and are exactly the right fence (
PT:apps/api/src/hostPools.ts: a non-generalpool is only used by creates that name it;sandboxes.create({pool:"bigmem", pool_fallback:"none"}); host joins via bootstrap token body{"pool":"bigmem"},PT:DEPLOY.md"Dedicated host pool"). Hosts are bare metal with/dev/kvm; pick one with >= 384 GB RAM and 2+ TB NVMe. - Also set
backup_interval_min: 0(the hourly rootfs backup would tar 140 GB) andauto_archive_days: 0for this sandbox; the weights are re-fetchable from S3. - Interim (no Platinum change): run
coreoutside Platinum on one rented big-RAM Linux box withdocker run(same image, same env), reachable atarena.sharingan.pokerthrough the same Cloudflare Worker/DNS. Everything else (runner, bot sandboxes, billing) is on Platinum and only sees a URL. When G1/G2 land, move the container into a bigmem Platinum host (even the same machine: install it as a Platinum host viaPT:DEPLOY.mdhost installer) with zero client changes. A weaker interim: Phase 1 arena without blueprint.
(c) Eval runner (uploaded bots)
runner/ (TypeScript, @platinum-dev/sdk; the SDK is TS/Python only, and the arena runner is a Node service). One job = one sandbox = one benchmark run.
Current flow (worker mode, see README.md): the sandbox gets no network at all; it runs sharingan-arena run which plays the whole duplicate benchmark in-process (thousands of hands/s) with the user's bot as a setpriv-dropped stdin/stdout subprocess, signs result.json with an HMAC key (root-only file), and the runner POSTs it to /v1/results. Steps 4-6 below describe the original WebSocket design, kept as mode:"connect":
- Template
sharingan-bot(docker/Dockerfile.bot,templates/sharingan-bot.json): python 3.12 +sdk/python+ node 22 +poker-sharingan+ two launchers. Built once byrunner/src/provision.ts(Template.fromImage(...).build(),PT:docs/templates.md). - Per run:
sandboxes.create({template, type:"ephemeral", cpu:1, ram_mb:1024, network:{allowOut:[arenaHost], denyOut:[all]}, metadata:{sharingan_job}}, {waitForRunning:true}). Cold boot about 98 ms, warm claim about 14 ms (PT:README.mdPerformance). Egress is locked to the arena host (hostname rules read TLS SNI;PT:docs/networking.md). The doc says the egress policy is not a hard boundary (source-IP spoofing), but the arena is public anyway and the bot only holds a throwaway token. - Artifact (tar.gz, <= 8 MiB;
files.writetruncates above 16 MiB, SDK README) written withfiles.write, unpacked withsh. Pythonrequirements.txt: PyPI added to the allowlist,pip install, thenupdateNetworkdrops it. - Bot started with
sbx.process.start({cmd:[setpriv ... python3 launch_bot.py], env:{ARENA_TOKEN,...}, timeoutMs}). It connects out overwss://.../v1/play. We do not usesharingan play(creates its own table vs house bots) orsharingan serve(webhook mode: needsARENA_ENABLE_WEBHOOKS, a public URL per sandbox, an extra proxy hop per decision).bot/launch_bot.pyis a thin wrapper oversharingan connect(python SDK);launch_bot.mjsis the TypeScript equivalent. - Runner retries
POST /v1/benchmarks/{track}/runs(with the bot's own token) until the arena stops answering409 not_connected, pollsGET .../runs/{id}(or listens for thebenchmark_progressWS event) untilfinished, thenusage()anddelete()in afinally. - Why not fork from a snapshot.
sandbox.fork()/clone()cold-boots from a disk copy (about 3.5 s) and reserves the parent's full shape (PT:docs/snapshots.md); a template create is faster and simpler. Use snapshot+clone only for the repeat-run case where a bot has heavy installed deps: build once per bot version (snapshot()afterpip install),clone({snapshotId})per run. Not implemented. - Leak protection. Process-level
timeoutMs(guest-side kill), runner wall-clock deadline plus arena abort,auto_stop_minutes:30, andreapOrphans()every 5 minutes (lists running sandboxes bymetadata.sharingan_job). G9 is the real fix. - Throughput. A trivial Python bot does about 550 hands/s with
lanes:4(TABLE_API 12b item 12); 20,000 hands is under a minute plus decision latency. The sandbox shape is the Python bot's limit, so offercpu1/2 as a price tier. - Trust. The arena flags remote bots
verified:false(benchmarks.rs:159,verified: rec.house). A bot run in our sandbox is not "house", so it stays unverified. Arena change (small, not done): let an admin-authenticated run request carryhosted:true(admin token held only by the runner) to setverified. Also register bots withowner = userId(already a field onPOST /v1/bots,api.rsRegisterReq.owner). - Cost metering. See (d).
(d) Credits and billing: two separate money flows
- Players -> us (our Stripe account). Stripe Checkout sells credit packs; webhook
checkout.session.completedcredits an append-only ledger (per user, integer micro-USD, idempotent on the Stripe event id). BeforePOST /jobsthe billing service reservesreserveUsd(hands)(runner/src/cost.ts); after the job settles it chargeschargeUsdand releases the rest, idempotent onidempotencyKey. Failed runs caused by us (provisioning, arena down) are refunded in full; a bot crash or timeout is charged for time used. Prepaid credits, no Stripe metering. The README's "buy credits, run evals" fits this. - Platinum -> us. Platinum is prepaid per org (
PT:docs/billing.md): vCPU-seconds + GiB-seconds sampled every 60 s, storage per GiB-hour, requests metered. Fund the two orgs with Platinum's own checkout/auto-top-up (PT:apps/api/src/autoTopup.ts) on our card, setPUT /v1/billing/limits(usage_limit_usdas circuit breaker,usage_alert_usdwith theorg.usage_alertwebhook,PT:docs/webhooks.md) on the bots org. - Per-run cost.
GET /v1/sandboxes/:id/usage(SDKsbx.usage(),PT:packages/sdk-ts/src/index.ts:1872) returns uptime and cost but 404s once the sandbox is deleted (PT:apps/api/src/api/sandboxes.ts:5640-5643) and lags up to a 60 s cycle. So the runner reads it before delete and also meters wall-clock x shape x our rates itself, and billsmax(local, platinum)timesMARKUP. The fixed arena/bigmem host is recovered per hand (USD_PER_HAND_ARENA_SHARE), not per second. Reconcile monthly againstGET /v1/billing/usage?group=sandbox(needs a*key; the runner key is scopedsandboxes:write,PT:docs/api-keys.md).
(e) Platinum calls per step
| Step | Call | Where |
|---|---|---|
| Build templates | Template.fromImage(img).build(client,{name,default_cpu,default_ram_mb,default_disk_gb,entrypoint}) -> POST /v1/templates/from-image |
PT:packages/sdk-ts/src/template.ts, PT:apps/api/src/api/templates.ts:866 |
| Pre-stage for fast first boot | POST /v1/templates/:id/prepare, /materialize (no typed method: client.request) |
PT:docs/templates.md |
| Deploy arena as app | client.request("POST","/v1/apps",{slug,source:{kind:"image",image},port:8080,health_path:"/healthz",runtime_env,cpu,ram_mb,region},{wait_for_state:"live"}); redeploy POST /v1/apps/:id/deploy, rollback /rollback, logs /logs |
PT:apps/api/src/api/apps.ts:257,478,522,546 (no SDK wrapper; flag flags.apps_enabled default off, config.ts:668) |
| Deploy core as sandbox | sandboxes.create({template,pool:"bigmem",pool_fallback:"none",cpu,ram_mb,disk_gb,expose:[{port:8080,public:true},{port:8081}],env,backup_interval_min:0,auto_archive_days:0,type:"persistent"},{waitForRunning:true}) |
PT:packages/sdk-ts/src/index.ts:76-170 (SandboxSpec) |
| Public URL | sbx.exposedUrl(8080) -> https://8080-<id>.sbx.<domain>; app: https://<slug>.apps.<domain> |
PT:docs/networking.md, apps.ts appUrl() |
| Secrets/env | env, app runtime_env {secret:true}, org secrets with egress injection (not for inbound tokens) |
PT:docs/secrets.md, apps.ts encEnv |
| Run sandbox | sandboxes.create({template:"sharingan-bot-...", type:"ephemeral", network, metadata}) |
SDK SandboxSpec |
| Upload bot | sbx.files.write(path, bytes) (<= 16 MiB), sbx.sh(...) |
SDK files, sh |
| Run bot | sbx.process.start({cmd,cwd,env,timeoutMs}), proc.logs(), proc.kill() |
PT:packages/sdk-ts/src/process.ts |
| Tighten egress | sbx.updateNetwork({allowOut,denyOut}) |
SDK index.ts:1807 |
| Meter | sbx.usage() |
index.ts:1872 |
| Tear down | sbx.delete() |
index.ts:1571 |
| Orphan sweep | sandboxes.iterate({state:"running"}) + metadata |
index.ts:1186 |
| Spend breaker | PUT /v1/billing/limits {usage_limit_usd,usage_alert_usd} |
PT:docs/billing.md |
| Lifecycle events (optional) | webhooks sandbox.state_updated, org.usage_alert |
PT:docs/webhooks.md |
2. Gaps in Platinum, with the minimal fix
| # | Gap (evidence) | Blocks | Minimal change |
|---|---|---|---|
| G1 | RAM per sandbox capped at 32 GiB: zod max(32768) at sandboxes.ts:473 (and :151,:6756), config limits.org_max_ram_mb max:32768 (config.ts:813), tier table config.ts:891-900. Reference hosts are 60 GB (PT:README.md). |
Production blueprint (b) | Raise the zod/config max to 524288; set limits.org_overrides[org].maxRamMb for the core org; enrol a >= 384 GB host with pool:"bigmem"; check memoryAdmission.ts and consider disabling KSM scanning on that host (KSM over 256 GB burns CPU). |
| G2 | Disk per sandbox and template default capped at 100 GB (sandboxes.ts:475, templates.ts:787,876,1005,1113,1199, config.ts:814); volume max 100 GiB (docs/volumes.md); template size cap 20 GiB default (config.ts:805, per-org overridable up to 512000 MB). |
Production blueprint | Raise the same maxes to >= 1024 GB behind the per-org override; weights come from S3 on first boot, so no template size change is needed. Host needs >= 1 TB NVMe free. |
| G3 | No custom domains. Only <slug>.apps.<domain> (sandboxProxy.ts:105-122) and <port>-<id>.sbx.<domain>; Caddy on-demand TLS is gated by tlsAsk.ts which says yes only to those names. |
arena.sharingan.poker |
Add app_domains(hostname, app_id, verified_at), DNS TXT verification, let lookupAppRoute match an exact host, and let tlsAsk answer yes for verified hostnames. Interim: Cloudflare Worker (edge/arena-proxy-worker.ts). |
| G4 | No private path sandbox <-> sandbox/app: bridge-to-bridge forward is dropped (infra/03-network.sh:64-66); guest IPs are per-host (10.42/16). Bot traffic therefore hairpins through the public edge and is metered as external requests. |
Latency/cost of the hot path; "internal network" requirement | Unverified whether a per-sandbox allowOut to a peer's internalIp overrides the bridge drop (test first; I believe not). Fix: org-scoped peer allow-rule (network.allow_peers:[{sandbox, port}]) rendered into the per-sandbox nft chain ahead of the drop, same-host only; add colocate_with placement so bots land on the arena's host. Cross-host needs an overlay (larger). Public path works today. |
| G5 | Apps cannot be stateful: bootVm passes no volumes, network or secrets (rollout.ts:128-148) and always blue-greens onto a fresh VM. |
Arena as an App with SQLite | Pass volumes through CreateBody -> createBody; add strategy:"recreate" (stop old before booting new) for stateful apps. Or use Postgres and ignore this. |
| G6 | Volumes are commit/pull working copies, not a shared POSIX FS (docs/volumes.md). No documented mmap/lock semantics. |
SQLite or weights on a volume | Document it; recommend Postgres. No Platinum change needed. |
| G7 | Template images must be pulled from a registry; I found no registry credential field on from-image (templates.ts:866) and the apps image must be a pinned tag (apps.ts isPinned). Unverified. |
Private images | Publish public GHCR images (code is Apache-2.0, weights are not in the image). If private is wanted: add registry_auth (secret ref) to from-image/apps source. |
| G8 | No SDK wrapper for apps or billing (PT:packages/sdk-parity.json, SDK README "Not wrapped yet"); apps are flag-gated and undocumented (config.ts:668; no docs/apps.md). |
Convenience only | Use client.request; enable via limits.org_overrides[org].apps. Optionally add client.apps.* (thin). |
| G9 | No hard per-sandbox lifetime or budget: only idle auto-stop and day-granular archive/delete (docs/sandboxes.md Retention). A runner crash leaves a billing sandbox. |
Safe untrusted-code billing | Add max_runtime_seconds to CreateBody; reconciler deletes past created_at + ttl (reuse the auto-delete sweep, reconciler.ts near auto_delete_days). Optional max_cost_usd. Interim: guest timeoutMs, runner deadline, reaper. |
| G10 | Per-run cost is awkward: /usage 404s after delete, 60 s sampling, no tag/metadata grouping, /v1/billing/* needs a full-access key. |
Exact metering | Keep /v1/sandboxes/:id/usage readable for N days after delete with a cost_usd field; allow GET /v1/billing/usage?group=metadata.sharingan_job or a read-only billing:read scope. Interim: own meter (cost.ts). |
| G11 | One prepaid balance per org, no reseller/sub-tenant mode; zero balance stops running sandboxes after 60 min. | Isolation of arena from bot spend | None required: use two orgs, auto top-up, usage-limit breaker on the bots org. |
| G12 | Concurrency caps by tier: inflight, total cpu/ram (config.ts:891-900) and create rate limits (rateLimit.spawn.test.ts). |
Burst of many runs | Raise org overrides; runner MAX_CONCURRENT_RUNS below the cap. |
| G13 | Template first boot on a host needs the image present; custom templates are not in the warm pool by default. | Cold start latency | Call prepare/materialize after each template build; keep a few bot sandboxes warm only if needed. |
| G15 | Worker sandboxes need the blueprint: ladder-v1's panel includes house:sharingan-1, so the in-sandbox worker mmaps it and /v1/results rejects a different blueprint_fingerprint. 140 GB per sandbox is impossible (G1, G2, G6; guest page caches are not shared). |
ladder-v1 / sharingan-1 workers in production |
Run the worker inside the core VM with a bubblewrap/gVisor bot jail, or let the worker's SHARINGAN-1 seat call sharingan-serve over HTTP (arena change), or a worker-sized blueprint. |
| G14 | Hostname egress allowlist reads SNI/Host; with everything else denied it is unclear whether DNS is still allowed. Unverified. | Locked-down bot sandboxes | Test on first run. If DNS is blocked, allow the resolver IP explicitly. |
Arena-side changes needed (in this repo, not done): Postgres Store; hosted attestation so Platinum-run bots can be verified; rate limiting (or do it in Cloudflare); optional /v1/health alias.
3. Phases
- Phase 1, works on Platinum today:
arenaimage as a persistent sandbox (no blueprint), runner + bot template + bot sandboxes, Cloudflare Worker forarena.sharingan.poker. Only matches against the non-blueprint house bots. - Phase 2, interim full product:
coreimage on one external big-RAM box (docker run), same Worker; everything else on Platinum.ladder-v1andsharingan-1live. - Phase 3: land G1/G2 (+G9), move
coreinto thebigmempool on Platinum; then G3 (custom domain) and G4 (private path) as optimizations; Postgres when scaling out.
4. What the user must provide
- Platinum: control-plane URL, an API key for each org (
sharingan-corefull access;sharingan-botsscopedsandboxes:write,templates:writefor provisioning), theappsflag on the core org, prepaid balance or auto-top-up, region choice (same region for core and bots), and the decision on G1/G2/G9 (and a bigmem host). - Registry: a GHCR (or other public registry) org to push
sharingan-arena|serve|core|bot:0.1.0. - Assets: read-only S3 credentials and URI for
sharingan-1.plcpandbuckets-200/(ASSETS_S3_URI); confirm the final blueprint file name andARENA_SIZING=medium. - DNS:
arena.sharingan.pokeron Cloudflare (Worker route,UPSTREAMvar), and for G3 later a CNAME target from Platinum. - Stripe: account, products/prices for credit packs, webhook endpoint and secret; refund policy for infra failures.
- Secrets:
ARENA_ADMIN_TOKEN,RUNNER_AUTH_TOKEN,SHARINGAN_API_KEYS, billing webhook token. - Decisions: price per 20k-hand run and
MARKUP; Platinum's actual per-vCPU-second and per-GiB-second rates forcost.ts; Postgres provider.
5. What is verified now, what is not
Built and run (rootless podman on box A, deploy/local/e2e.sh): the arena, serve, core and bot images (clean SIGTERM, entrypoint.sh incl. wait -n supervision, mounted-blueprint path, non-root), the runner (typecheck clean, 106 tests), the full job flow in both modes against a real arena on Postgres with the tiny blueprint, the credit ledger, signed Stripe webhooks (fake secret), and sandbox teardown.
Still unverified, all of it needs a live Platinum control plane: every SDK call shape in runner/src/platinum/real.ts (typed against SDK 0.9.0 and exercised against a mock fetch only), network:{allow_out:[], deny_out:["0.0.0.0/0"]} leaving exec/files/process usable, setpriv / env -i inside the guest, process.start timeout_ms, usage() cost field names, hostname egress with DNS (G14), and whether Platinum can pull the images (G7). The ASSETS_S3_URI fetch path of entrypoint.sh never ran (no S3 credentials); nor did the Cloudflare Worker.