Skip to content

Local development

voxix edited this page Nov 16, 2026 · 15 revisions

Local development

Python services

uv sync
cp .env.example .env
docker compose up -d --wait db
uv run --env-file .env alembic upgrade head
uv run --env-file .env pytest
uv run --env-file .env uvicorn agent.server:app --port 8001 &   # the agent, AGENT_URL in .env; OPENROUTER_API_KEY in .env turns on the model
uv run --env-file .env uvicorn api.main:app --reload            # http://localhost:8000

Or run the API and one agent in containers next to the database: docker compose up -d --build api (also on localhost:8000; it applies the migrations on start). podman compose works the same.

.env has two Context API settings. CONTEXT_TOKEN_SECRET signs the agent's tokens (at least 32 characters, for example openssl rand -hex 32); compose refuses to start the API without it. CONTEXT_API_URL is where the agent reaches the API (http://localhost:8000; compose sets http://api:8000). Leave it empty and the agent gets no Context API and works from the task text. On kind, deploy/local/compose.yaml sets it to http://context-api.justpass-agents.svc.cluster.local:8000, the Service the actors reach (see Substrate on kind below). It must be a DNS name without a trailing dot, not an IP address: Substrate refuses those in an egress rule, so the API refuses to start.

Ports taken? API_PORT moves the API for compose and pnpm dev. DB_PORT moves Postgres; change the port in .env too. /health says the process is up; /ready also checks the database, and is what the compose healthcheck uses.

Tests create and drop their own database on TEST_DATABASE_URL. Without the env file the database tests are skipped.

Web app

See apps/web/README.md. pnpm dev serves on localhost:5173 and proxies /api/** to the API on localhost:8000. pnpm e2e runs the Playwright tests against that API (CI runs them against the compose api service). The fake agent takes 10 s a round, so they run in parallel and take about two minutes. They are timing sensitive: don't build images at the same time.

Auth

The compose service auth runs the Firebase Auth Emulator (project demo-justpass, 127.0.0.1:9099), and the api service runs in emulator mode against it. apps/web/.env.development points the dev server at the emulator, so the web app signs in anonymously or with an email link (the link shows up in the emulator's log). e2e.sh and Playwright get tokens from the emulator. The API refuses emulator mode with a non-demo-* project ID.

Substrate on kind

Agents run as Substrate actors on a local kind cluster (rootless podman works).

source deploy/local/env.sh
  • env.sh sets ATE_INGRESS_ALLOWED_SPIFFE_IDS, the clients the atenet router accepts. The scripts use kubectl's current context, so point it at the kind cluster first.
  • Check out Substrate v1.0.0 and, from its root: hack/create-kind-cluster.sh, then ATE_INSTALL_KIND=true go run ./cmd/ate-setup deploy ate-system --credential-provider='{"name":"k8s.io"}' --image-repo ghcr.io/agent-substrate/substrate --image-tag v1.0.0 --sandbox-classes=gvisor (env.sh supplies the router's allowed clients). Also go install ./cmd/kubectl-ate for kubectl ate. The prebuilt images and --sandbox-classes=gvisor skip the ko and micro-VM builds. The worker pool pulls its worker image from ghcr.io; deploy.sh still builds the agent image into localhost:5001.
  • Rootless podman: create-kind-cluster.sh stops at chmod /dev/kvm (not permitted). Finish its last steps by hand: write the registry hosts.toml on the node, connect kind-registry to the kind network, and create the local-registry-hosting ConfigMap.
  • Only Substrate and the agents run on the cluster. The API and Postgres run in containers next to it, as production has them on Cloud Run and Cloud SQL.
  • deploy/local/deploy.sh builds the agent image, applies the worker pool with kubectl apply -k deploy/local, creates the atespace, access policy and actor template in ate-api with uv run python -m api.substrate_sync, and starts the API with compose.yaml plus deploy/local/compose.yaml. Run it again after a change: each agent image or size gets a new template, because templates are immutable, and the unchanged parts stay as they are. It uses docker; set CONTAINER_TOOL=podman for podman. kubectl ate must be on PATH for e2e.sh.
  • Agent sizing: the worker pool (deploy/substrate/agent-pool.yaml, a kustomize base) and the template (deploy/substrate/agents.yaml, which also holds the atespace and access policy) are plain files, with no templating. The sandbox must fit inside its worker, and a unit test fails if the two drift apart. The local overlay deploy/local/kustomization.yaml only sets the worker image to a tag in the local registry: Substrate accepts any non-empty workerImage.
  • substrate_sync needs an owner: creating an atespace takes global owner, and its access policy atespace owner, while the API's own identity is only an editor. deploy.sh mints a 10-minute token for ate-client, a bootstrap owner of the kind cluster, and deletes it afterwards. A changed access policy is replaced (with the stored uid and version as preconditions); a template is never changed. In production this would run in CI as an identity in ate-api's bootstrap owners; that is not built yet.
  • deploy/local/identity.sh gives the API the identity homework/api with no pod: a service account token for ate-api, and a client certificate for the router from a PodCertificateRequest. They last 24 hours; rerun it after that, and the API picks them up without a restart. The files, with a private key, stay in the git-ignored deploy/local/.identity/.
  • deploy/local/forward.sh keeps port-forwards to ate-api (8443) and the router (9443) up, on 127.0.0.1 only. deploy.sh starts it. The API uses the host network to reach them, and binds to 127.0.0.1 itself, so nothing reaches the LAN.
  • Actors reach the Context API and nothing else. The API listens on 127.0.0.1 of the host, and the kind network has no route there, so the path is: actor, atunnel, atenet-egress, the Service context-api (no selector; deploy.sh writes its EndpointSlice with the address of the context-relay container on the kind network), context-relay (nginx: only POST to /v1/context/, everything else is refused), a unix socket in a volume, context-uds (nginx on the host network), the API. The API never listens on the LAN. Each actor gets an egress policy (one http rule for that host and port, see egress_rules in apps/api/src/api/agents.py) right after CreateActor, before its first request wakes it: an actor with no policy gets NXDOMAIN and refused connections. Every round checks the policy again: if the actor's rules differ from the ones CONTEXT_API_URL gives now, the API replaces them (UpdateActorEgressPolicy), so a changed URL reaches actors that already exist. Deleting the actor deletes its policy. By hand from an actor: example.com gives NXDOMAIN, the relay's bare IP gives 403 egress denied, the Service name works.
  • deploy/local/e2e.sh sends jobs through the API and checks each job's actor: 10 s to the answer, suspended, a draft artifact per round written through the Context API, the actor's egress policy, the egress tunnel in the atenet-egress logs, feedback wakes it, accepted deletes it, the relay answering 404 for /jobs and 401 for a Context call without a token, and four jobs at once on two workers.
  • The router only accepts mTLS. deploy/local/router-client.yaml runs a debug pod with the API's identity (spiffe://cluster.local/ns/homework/sa/api); its header shows the curl to call an actor.
  • Actor commands: kubectl ate create actor ..., kubectl ate suspend actor ... --mode=sync; the next request resumes it.
  • On Linux, if Envoy crashes on inotify, raise fs.inotify.max_user_instances=1024 and fs.inotify.max_user_watches=524288.
  • After a host reboot, start the registry, the kind node and the buildx builder containers again. Pods get new IPs but workers keep the ones they registered, so every request to an actor fails with a 503 and UF,URX in the router's Envoy log. deploy.sh runs deploy/local/workers.sh --fix, which finds workers whose registered IP differs from their pod's and recreates them; e2e.sh runs it as a check only. Rerun deploy.sh too if the context relay was recreated on its own, since the context-api EndpointSlice holds the relay's IP.

Testing the GCP setup locally

deploy/gcp/local-e2e.sh runs the Terraform module and the deploy script against floci-gcp, an emulator that runs Cloud SQL and Cloud Run as containers. It starts floci-gcp, builds the API image, applies deploy/terraform/local, deploys with the script CI uses and checks that the API answers from its database. local-e2e.sh down removes everything. CI runs it as gcp-e2e.

  • It uses docker; ENGINE=podman uses podman, which needs systemctl --user enable --now podman.socket.
  • The module's emulator = true mode swaps what floci does not support: the Cloud SQL socket volume and Secret Manager references become a plain host:port database connection and plain environment values.

Clone this wiki locally