Skip to content

latent.serve.nonce_studio

Studio-backed :class:~latent.serve.auth.NonceStore for multi-replica anti-replay.

The per-process :class:~latent.serve.auth.InMemoryNonceStore guards a single pod: behind N replicas, a captured signature replayed to a different replica within the skew window slips past. This store moves the replay set into the studio's Postgres — the studio owns the database, so the pod never touches it directly; it POSTs each nonce to a studio HTTP route and every replica shares one view. A nonce burned on one pod is rejected on all.

The check-and-add is POST {studio_url}/api/serving/nonce with the pod's agent-studio key (the same credential the :class:~latent.serve.registration.RegistrationClient uses) — body {"nonce", "ttl_seconds"}, response {"first_use": bool} (true = accept, first use; false = replay). The studio route does the atomic first-insert-wins in Postgres.

Fail open, but classify the failure. The studio is already the per-turn routing dependency, so a brief nonce-route outage should degrade latency, not correctness: on any error this store returns True (accept) rather than 401-ing legitimate traffic on a studio blip. It still distinguishes why: a 4xx (wrong/rotated key, or an undeployed route) is a NON-self-healing config fault — logged at ERROR and counted for alerting — while a 5xx / timeout / connect error is a transient blip logged at WARNING. Failing closed is wrong either way: a deploy-ordering blip would 401 real traffic, and the proxy doesn't map 401 to a local fallback.

Isolated in its own module (not :mod:latent.serve.auth) so importing auth loads neither httpx nor the workspace config; they are imported only when a studio-backed store is actually built.

Classes

StudioNonceStore

StudioNonceStore(studio_url: str | None = None, api_key: str | None = None, client: httpx.AsyncClient | None = None, ttl_s: int = 2 * DEFAULT_MAX_SKEW_S, timeout_s: float = DEFAULT_TIMEOUT_S, max_connections: int = DEFAULT_MAX_CONNECTIONS, max_keepalive_connections: int = DEFAULT_MAX_KEEPALIVE_CONNECTIONS)

Cluster-wide replay guard backed by Postgres via the studio's nonce route.

Holds ONE long-lived, pooled :class:httpx.AsyncClient for the store's whole lifetime and reuses it across requests — the nonce route is on the per-turn hot path, so a fresh client (new TCP+TLS handshake) per turn is wasteful.

client is injectable (e.g. an httpx.AsyncClient over an in-process ASGI transport or a mock transport in tests), mirroring :class:~latent.serve.registration.RegistrationClient. An injected client is the caller's to close; a client the store builds itself is closed by :meth:aclose.

Methods

StudioNonceStore.aclose

aclose() -> None

Close the pooled client — but only the one the store built itself.

An injected client belongs to the caller (tests, or a shared client); the store must not close what it doesn't own.

StudioNonceStore.check_and_add

check_and_add(nonce: str, now: float) -> bool

True if the nonce was unseen cluster-wide (studio first_use).

now is unused — the studio owns expiry via ttl_seconds — but kept to satisfy the :class:~latent.serve.auth.NonceStore signature the in-memory store needs.

Fails closed on any backend failure — raises :class:~latent.serve.auth.NonceStoreUnavailable so a turn that cannot be replay-checked does not run (it may carry tool side effects). The server maps that to a 503 the RemoteAgentProxy treats as pod-unavailable (fallback / retry), not a 401. Failure cause is still distinguished for alerting: a 4xx (wrong/rotated project key, or the nonce route isn't deployed) is logged at ERROR and bumps :attr:config_faults; a 5xx / timeout / connect / decode error is logged at WARNING.

Attributes

DEFAULT_MAX_CONNECTIONS

DEFAULT_MAX_KEEPALIVE_CONNECTIONS

DEFAULT_TIMEOUT_S