Skip to content

Architecture

voxix edited this page Nov 16, 2026 · 14 revisions

Architecture

web (SPA) ──/api/** + Firebase ID token──▶ API (FastAPI) ──▶ Postgres, object storage
                                            │      ▲
                              start/resume  │      │ Context API (per-round token)
                                            ▼      │
                                  agent actor (Substrate, gVisor) ──egress gateway──▶ OpenRouter, search API
                                            │
                                  run_code via the API ──▶ sandbox actor (no network)

Components

Web app (apps/web). Vite + React + TypeScript single-page app. Mobile-first: take a photo, submit, come back later. Polls the API for finished jobs (TanStack Query refetchInterval) and renders results as Markdown with KaTeX. Its API client is generated from contracts/openapi.json; that file is the only interface between frontend and backend.

API (apps/api). One FastAPI app with three parts:

  • Jobs API: submit, list, poll (updated_since), get, accept, feedback. Students see only their own jobs. It answers at once; the agent works in the background and the web app polls.
  • Dispatcher: runs each round on the job's agent actor, saves the answer, and suspends or deletes the actor (see Job lifecycle).
  • Context API: the tools agents call, as POST /v1/context/<tool> (get_task, read_artifact, write_artifact, get_profile, get_subject_memory, update_memory, list_history, search_history). It is not in contracts/openapi.json: only agents call it. Each role reaches only the tools its agent wraps. run_code is not built.

It also enforces the spend limits. Postgres is the source of truth; a Substrate snapshot only makes resuming faster.

Agent actor (apps/agent). One Substrate actor (gVisor sandbox) per job. It serves /readyz and POST /run, whose body and reply are AgentRun and AgentAnswer in packages/shared. The actor runs the orchestrator and the workers as asyncio tasks in one process. With an OpenRouter key (sent by the API with each round) it calls the models and their tools; without one it waits 10 seconds and returns a fixed sample answer, and a task with the whole word "fail" gets no answer. With a Context API to reach, it reads the task and profile and writes a draft through it, and shows the student's feedback in its answer. AgentRun and its context grant forbid unknown fields, so the API and agent images deploy together. See Agents.

Sandbox actor. Runs model-written code for the solver role. A separate actor with no EgressPolicy (so no network) and no tokens or user data. An actor can't call another actor directly, so run_code goes agent → Context API → sandbox actor.

How they communicate

From → to Transport Auth
Web → API HTTPS, same origin under /api/** (no CORS) Firebase ID token in Authorization: Bearer, no cookies
API → ate-api (create, suspend, delete actors) gRPC, TLS Bearer token; an access policy makes its identity editor of the justpass-agents atespace. On Cloud Run a Google ID token for the API's service account, fetched from the metadata server (1 h, renewed by the API); locally a 24 h Kubernetes service account token for homework/api
API → agent actor Substrate atenet router mTLS, the API's SPIFFE ID must be on the router allowlist. On Cloud Run the client certificate comes from our own CA (Terraform tls), SPIFFE ID ns/homework/sa/api, which the router trusts through --ingress-client-ca-file
Agent → Context API HTTPS HMAC-signed token for one round of one job (2 h)
Agent → OpenRouter / search Substrate egress policy (openrouter.ai:443, TLS passthrough) The API sends the key with each round; the actor holds it for that round
API → sandbox actor Substrate atenet router mTLS, same as above

The API verifies Firebase ID tokens itself (PyJWT against Google's signing keys), see apps/api/src/api/auth/firebase.py. The CurrentUser dependency (auth/current_user.py) guards every Jobs API endpoint: 401 for a missing or invalid token, 503 when Google's keys cannot be fetched. Jobs are scoped to the token's uid. The API serves the same routes with and without the /api prefix, because Firebase Hosting forwards it.

Hosting

  • Web: Firebase Hosting (static files on a CDN) with a /api/** rewrite to the API, so both share one HTTPS domain. Rewrite limits: 60 s per request, all cookies except __session are stripped. Every PR can get a preview URL.
  • API: Cloud Run (serverless). Not on the Substrate cluster: Substrate changes the control plane, and a cluster for the API alone is overkill.
  • Agents: Substrate v1.0.0 on one zonal Standard GKE 1.37 cluster, managed by Terraform (deploy/terraform/modules/cluster). One c3-standard-4 node pool, always 1 node, autoscaling to 3: Substrate's control plane and atelet need a node, so it never scales to zero. Substrate's Postgres is the substrate database on the API's Cloud SQL instance (IAM auth through a proxy sidecar), and actor snapshots go to Cloud Storage buckets. Substrate is the only thing on the cluster: the API and Postgres never run there, locally included. Without Substrate (compose, CI, a laptop), AGENT_URL sends every job to one plain agent server instead, which has nothing to suspend or delete.
  • Data: Cloud SQL Postgres (databases app and substrate on one instance), Cloud Storage buckets app-data and snapshots.
  • Region: EU.
  • Local: everything runs on a laptop (Postgres in compose, Firebase Auth Emulator, Substrate on kind). See Local development.

API to Substrate

Cloud Run reaches ate-api and the router through internal load balancers in the cluster VPC, using Cloud Run direct VPC egress. A private Cloud DNS zone ate-system.svc. maps the Substrate service names to the load balancers, so the servers' certificate names match. ate-api accepts the API's Google ID tokens through an extra JWT provider, so there is no stored token to renew. The router gets a client certificate from our own CA with SPIFFE ID ns/homework/sa/api.

API on Cloud Run

  • Public invoker: the API verifies Firebase tokens itself.
  • Min 1 instance with CPU always allocated, because the dispatcher sweeps in the background.
  • Cloud SQL through the unix socket volume; secrets from Secret Manager. Cloud SQL accepts encrypted connections only.
  • Terraform deploys the image with the environment, from the digest CI resolves (see CI).
  • Terraform state lives in a GCS bucket made by hand. It holds the secrets, so it has versioning, uniform bucket-level access and read access for admins only.

Clone this wiki locally