Spaces:
Sleeping

api_relay / README.md
api-relay's picture
Update README.md
df02478 verified
|
Raw History Blame Contribute Delete
5.73 kB
---
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://<random>.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 `<runner-tunnel-url>/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.)