Skip to content

latent.serve.registration

The pod's side of the register / heartbeat exchange (ADR-006 D3, ADR-009 D1, ADR-010).

The pod advertises its own hmac_secret (K) + origin + version + url + wire_protocols to Studio's POST /api/agents/register, authenticated by the projectKey (AGENT_STUDIO_API_KEY). It then re-registers at ttl/2 to stay alive; repeated failures trip a circuit breaker, which flips serving readiness (/readyz → 503) so the runtime falls back. Studio stores K (it needs the actual secret to sign — symmetric HMAC, ADR-009 D1) and routes to the pod while the agent_versions row is fresh.

Classes

CircuitBreaker

CircuitBreaker(threshold: int = 3)

Trips after threshold consecutive failures; resets on any success.

RegisterRequest

RegisterRequest(name: str, version: str, url: str, wire_protocols: list[str], origin: str, hmac_secret: str, knowledge_digest: str | None = None)

RegistrationClient

RegistrationClient(studio_url: str | None = None, api_key: str | None = None, client: httpx.AsyncClient | None = None, breaker: CircuitBreaker | None = None, register_path: str = AGENT_REGISTER_PATH)

Registers a served pod with Studio and keeps the registration alive.

client is injectable (e.g. an httpx.AsyncClient over an in-process ASGI transport in tests) — real HTTP, no mocks.

Methods

CircuitBreaker.record_failure

record_failure() -> bool

Record a failure; return True only on the transition into tripped.

Edge-triggered (not level) so a on_lost side effect fires once per trip — not on every failure after the threshold — and fires again only after a recovery (record_success) re-crosses the threshold.

CircuitBreaker.record_success

record_success() -> None

CircuitBreaker.tripped

RegistrationClient.deregister

deregister(name: str, version: str, origin: str) -> None

Best-effort de-registration on shutdown (signal handler / Ctrl-C).

RegistrationClient.register

register(req: RegisterRequest | ExtensionRegisterRequest) -> int

POST the registration; return the TTL (seconds). Raises on HTTP error.

The response carries {ttl, status} (ADR-013 D1): status is the version's lifecycle state as Studio now sees it (built/dormant/ healthy/active/...). An agent registration never activates a version; an extension registration carrying a newer release does, in Studio's registration transaction. Either way the pod logs status for visibility and otherwise ignores it (ADR-013 D12 pod-side note).

RegistrationClient.run_heartbeat

run_heartbeat(req: RegisterRequest | ExtensionRegisterRequest, readiness: ReadinessProbe | None = None, on_registered: Callable[[int], None] | None = None, on_lost: Callable[[], Awaitable[None]] | Callable[[], None] | None = None, max_iterations: int | None = None, sleep: Callable[[float], Awaitable[None]] = asyncio.sleep, rand: Callable[[], float] = random.random) -> None

Register, then re-register at ttl/2 until cancelled.

Each cycle first consults readiness (the served agent's ready()): a not-ready result gates the very first registration (a pod that can't serve never enters the registry) and, later, treats a health loss exactly like a failed re-registration. on_registered(ttl) fires after each successful registration (callers print the Studio URL on the first one). On a failed or not-ready cycle the circuit breaker advances; once tripped, on_lost fires (flip /readyz → 503) and the loop backs off (capped exponential + full jitter, growing with the failure count) so a wedged pod applies real back-pressure instead of spinning, then keeps trying so the pod can recover. max_iterations bounds the loop in tests; production passes None (run until cancelled). sleep and rand are injectable seams (default: real asyncio.sleep / random.random).

RegistrationClient.supervise

supervise(req: RegisterRequest, readiness: ReadinessProbe | None = None, on_registered: Callable[[int], None] | None = None, on_lost: Callable[[], Awaitable[None]] | Callable[[], None] | None = None, on_teardown: Callable[[], None] | None = None, max_iterations: int | None = None, sleep: Callable[[float], Awaitable[None]] = asyncio.sleep) -> None

The full serving lifecycle: register, keep alive, always clean up on exit.

Shared by --share (dev) and deployed pods. Runs the health-gated heartbeat, then in a finally runs on_teardown (e.g. tear down a tunnel) so nothing is leaked on shutdown or error. A dev process also de-registers there. A deployed pod does not: its row is shared by every replica of the version and by the pod replacing it, so a DELETE on one pod's exit would take the version out of routing. A lapsed lease drops the row from routing until a pod registers again.

RegistrationClient.supervise_serving

supervise_serving(req: RegisterRequest, readiness: ReadinessProbe | None = None, set_ready: Callable[[bool], None], on_announce: Callable[[int], None] | None = None, on_teardown: Callable[[], None] | None = None, max_iterations: int | None = None, sleep: Callable[[float], Awaitable[None]] = asyncio.sleep) -> None

:meth:supervise wired to a served pod's /readyz (ADR-012 D4).

This is the exact readiness↔registration coupling both serve_deployed and serve --share need, in one tested place: a successful registration flips serving readiness on (set_ready(True), plus a one-time on_announce for the dev loop's "live" banner), and a lost registration (breaker tripped) flips it off — so /readyz reflects registered AND healthy, and the consuming runtime falls back when it goes 503. set_ready is the seam (typically lambda r: setattr(app.state.server, "ready", r)) that keeps this module free of a FastAPI import.

Attributes

AGENT_REGISTER_PATH

BASE_BACKOFF_S

EXTENSION_REGISTER_PATH

MAX_BACKOFF_S

MIN_TTL_S

ReadinessProbe