RFC: agent model-gateway (design) + pull-based run-queue (prototype)
Draft / RFC. The design note is explicitly exploration, not yet approved; the queue is a Phase-0 prototype. Opened for review, not merge.
Follows from a design discussion on whether to run the claude CLI per box to "avoid API calling." Conclusion: the only thing that avoids metered billing is subscription auth, which is against per-seat terms for a container fleet and is being closed off — so the ToS-clean wins (cost, key custody, metering) come from centralizing, not subscriptions. This PR captures that as a design + prototypes the orthogonal "pull instead of push" idea, including worker-credential minting so poll mode runs end-to-end.
1. Model-gateway design note — docs/AGENT-MODEL-GATEWAY-DESIGN.md
A daemon-side gateway that holds the one provider API key and brokers every box's model calls: key never in a box, per-tenant token metering (the writer the metering plane lacks), central model-tiering, shared prompt cache, rate-limit. Boxes point the existing Agent SDK at it via ANTHROPIC_BASE_URL + a short-lived scoped token — an env change, not a code change. Phased 0–4. Explicitly not a subscription-auth bridge.
2. Pull-based run-queue prototype (push → pull, outbound-only)
A producer enqueues; long-lived worker boxes lease tasks, run locally, complete.
- proto:
EnqueueAgentTask/LeaseAgentTask/CompleteAgentTaskonAgentSkillService. SQS-style visibility-timeout lease → at-most-one active worker per task, redelivery on expiry, stale-token completion rejected. - queue core (
internal/server/agent_task_queue.go): in-memory, lock-guarded FIFO, injectable clock. Unit-tested, race-clean. - producer:
containarium agent enqueue+ clients. - worker runtime:
agent-runtimepollmode (poll.ts,CONTAINARIUM_AGENT_MODE=poll) — lease → run → complete, reusing the A2ArunTask.
3. Worker-credential minting — poll mode end-to-end
- proto:
StartAgentWorkerRPC. - server: provisions/reuses the skill box, then mints a queue credential — a JWT scoped to
agents:runonly, separate from the skill's in-box token (leasing is a runtime action the skill's scopes don't grant) — and launches poll mode with the credential + daemon URL seeded as env.buildWorkerPollCommandresolves the daemon host from the box's default route at launch, so the daemon needn't know the bridge address. - CLI:
containarium agent worker <skill>+ clients. - end-to-end test: the full producer→worker loop (enqueue → lease → run → complete) driven through the real RPC handlers with a real
agents:runcredential context — proves credential-scoped auth + queue threading, the cross-RPC stale-lease guard, and theagents:rungate on every op.
containarium agent worker hello-agent --server <host> # start a poll-mode worker
containarium agent enqueue hello-agent --input '{"q":…}' --server <host> # produce
Deferred (documented, intentional)
Credential lifetime (30m TTL → long-lived worker needs refresh, same question the gateway token raises); worker-box egress allowance to the daemon host:port when eBPF enforce is on; queue durability across restart; per-tenant fairness + dead-letter. One remaining acceptance step: a live box→daemon transport check (gateway resolution + reachability) — not run here.
Testing
go build ./... clean; queue + RPC tests pass under -race; agent worker / agent enqueue CLI render; proto/swagger regenerated; poll.ts typechecks (SDK-free subgraph).
🤖 Generated with Claude Code
Live transport check — passed (validated on a two-GPU lab backend).
Validated the one piece the Go tests can't cover: that a worker box can reach the daemon via buildWorkerPollCommand's default-route resolution. Throwaway box, existing daemon, torn down after.
| Probe (from inside the box) | Result |
|---|---|
default-route resolution (ip route show default) |
<host> — the backend, as expected |
box → http://<host>:8080 |
live HTTP response (TCP + HTTP both work) |
GET /v1/backends (unauthenticated) |
HTTP 401 — the real daemon API, auth-gated |
listener identity on :8080 |
the containarium daemon process |
So the worker's POST /v1/agent-tasks/lease + …/complete with the minted agents:run Bearer would land on the daemon's /v1/* API and authenticate. Transport assumption holds.
Not exercised: the actual lease/complete against the new /v1/agent-tasks/* endpoints — that needs a daemon build carrying this branch (the lab host runs a released daemon without them). The queue + credential-scoped auth loop itself is already covered end-to-end by agent_queue_server_test.go.