--- title: Api relay sdk: docker emoji: 📚 colorFrom: red colorTo: yellow --- # Archx Relay Space (v4) — deploy & operate Pure-push, batch-first relay for the Archx dataset workforce. One contract, no artificial caps, no polling of any kind (the relay pushes jobs to the runner's Cloudflare quick-tunnel URL). > v4 = Cloudflare quick tunnels: runners dial out, get an ephemeral > `https://.trycloudflare.com` URL, register it, and receive jobs. > No new secrets, no Cloudflare account, tunnel dies with the runner. > See `../docs/relay-execution-architecture.md` for why (GitHub-hosted runners > have no inbound address) and `../docs/deploy.md` for the from-scratch runbook. ## Contract (prefix `/dataset_verification`) ### Agent endpoints (`AGENT_API_KEY`) | Endpoint | Purpose | |---|---| | `POST /desktop/batch` | Submit a batch. Body: `{batch_id?, jobs:[…]}`. **One batch_id for the whole batch.** | | `GET /desktop/batch/{batch_id}` | Poll: `{status, job_count, completed_job_count, remaining, results:[…]}` with `?consume=true` to fetch-and-delete | | `DELETE /desktop/batch/{batch_id}` | Cancel pending batch | | `GET /desktop/artifact/{artifact_id}` | Stream artifact bytes (images/audio/any file) | | `GET /desktop/status` | Schema, counters, per-worker liveness (**URLs redacted**) | ### Runner endpoints (`RUNNER_API_KEY`) | Endpoint | Purpose | |---|---| | `POST /worker/register` | Mint/refresh identity; same `worker_id` + new url **supersedes** the old generation (old URL gets a `/shutdown` push) | | `POST /worker/result` | Report one **cell** result | | `POST /worker/artifact` | Upload artifact bytes (streamed to disk; no size constants) | | `POST /worker/done` | Draining; releases claims | Delivery (relay → runner): the relay atomically claims a matchable queued cell and `POST`s it to `/job` (`Authorization: Bearer RUNNER_API_KEY`). `202` → executed; non-2xx/timeout → claim rolls back with backoff; 3 consecutive failures → runner `unresponsive` until re-registered. There is **no** poll/stream endpoint — runners never call in to ask for work. ### Job model ```jsonc { "batch_id": "batch_abc", // optional, returned as the batch id "jobs": [{ "job_id": "job_1", // optional "platform": "linux", // base (matrix overrides) "runtime": "python-3.12", // base (matrix overrides) "files": {"main.py": "…"}, // any number of files — multi-file example "files_variants": {"windows": {"main.py": "…"}}, // per-OS overlay, applied on top of files "entry_point": "python main.py", // single step, OR use steps: "steps": [ // shared environment, sequential steps {"name": "write", "entry_point": "python write_file.py"}, {"name": "read", "entry_point": "python read_file.py", "timeout": 30} ], "dependencies": ["numpy"], // pip, installed once per cell venv "network_required": false, "stdin": "1 2\n", // or supply matrix.stdin_cases "matrix": { // one example → many cells, ONE batch_id "platforms": ["linux", "windows", "macos"], "runtimes": ["python-3.11", "python-3.12", "python-3.13"], "stdin_cases": ["1 2\n", "3 4 5\n"] }, "timeout": 60, // per step default "stop_on_failure": true }] } ``` Cells are cartesian products: 3 OS × 3 runtimes × 2 stdin cases = 18 cells, one batch. Cell ids look like `job_1#windows/python-3.12/case-2`. Linux/Windows path differences are solved by `files_variants` (+ caller-side `pathlib` idioms in generated jobs). ### Artifacts (images, audio, anything) Jobs write deliverables under `outputs/`. The runner uploads them to the relay; the agent downloads via `GET /desktop/artifact/{artifact_id}` with byte-exact integrity (`X-Artifact-SHA256` header). Typical uses: generated plots for vision analysis, generated audio for ASR/tts evals, logs. Artifacts share the 3-hour retention TTL. ### No caps Request-size middleware is absent; artifact uploads stream to disk in chunks; batches are unlimited in count. The only limits are physics: Space RAM (index is tiny), Space disk (payloads+artifacts), claim deadlines, and the 3-hour retention. If the Space runs out of disk, jobs fail loudly (`infra_failure`) — never silently. ## Deploy See **`../docs/deploy.md`** — the exact from-scratch runbook (secrets reset → Persistent Storage → secrets → deploy → verify → pilot). Short version: `.github/workflows/deploy_space.yml` pushes this directory to the HF Space on change; the Space needs exactly two secrets (`AGENT_API_KEY`, `RUNNER_API_KEY`) and Persistent Storage mounted at `/data`. ## Test ```bash cd space python tests/e2e_test.py # matrix expansion, push delivery (real worker subprocess), # crash -> requeue -> recovery, supersede /shutdown, # artifact round-trip, batch consume — all local, no network ``` Passes locally in ~30 s (Python 3.11+, no network). ## Operations - Crash safety: jobs claimed at crash are requeued on boot; orphan payloads are reconciled; results are atomic (tmp+rename). A Space rebuild loses nothing queued. - Supersede: cron mints new runner generations every 5h; the relay forgets the old tunnel URL and pushes `/shutdown` to it; in-flight cells finish or their claim deadline requeues them. - Keep-alive: `.github/workflows/keepalive.yml` pings `/health` once per hour at a `secrets.randbelow` random offset — not a bot pattern. (A sleeping Space cannot push jobs; keep the ping or use a non-sleeping Space tier.)