Repository navigation
Local development
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.
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.
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.
Agents run as Substrate actors on a local kind cluster (rootless podman works).
source deploy/local/env.sh
-
env.shsetsATE_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, thenATE_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.shsupplies the router's allowed clients). Alsogo install ./cmd/kubectl-ateforkubectl ate. The prebuilt images and--sandbox-classes=gvisorskip the ko and micro-VM builds. The worker pool pulls its worker image from ghcr.io;deploy.shstill builds the agent image intolocalhost:5001. - Rootless podman:
create-kind-cluster.shstops atchmod /dev/kvm(not permitted). Finish its last steps by hand: write the registryhosts.tomlon the node, connectkind-registryto thekindnetwork, and create thelocal-registry-hostingConfigMap. - 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.shbuilds the agent image, applies the worker pool withkubectl apply -k deploy/local, creates the atespace, access policy and actor template in ate-api withuv run python -m api.substrate_sync, and starts the API withcompose.yamlplusdeploy/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; setCONTAINER_TOOL=podmanfor podman.kubectl atemust be onPATHfore2e.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 overlaydeploy/local/kustomization.yamlonly sets the worker image to a tag in the local registry: Substrate accepts any non-emptyworkerImage. -
substrate_syncneeds 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.shmints a 10-minute token forate-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.shgives the API the identityhomework/apiwith 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-ignoreddeploy/local/.identity/. -
deploy/local/forward.shkeeps port-forwards to ate-api (8443) and the router (9443) up, on127.0.0.1only.deploy.shstarts it. The API uses the host network to reach them, and binds to127.0.0.1itself, so nothing reaches the LAN. - Actors reach the Context API and nothing else. The API listens on
127.0.0.1of the host, and the kind network has no route there, so the path is: actor, atunnel, atenet-egress, the Servicecontext-api(no selector;deploy.shwrites its EndpointSlice with the address of thecontext-relaycontainer on thekindnetwork),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 (onehttprule for that host and port, seeegress_rulesinapps/api/src/api/agents.py) right afterCreateActor, 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 onesCONTEXT_API_URLgives 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.comgives NXDOMAIN, the relay's bare IP gives403 egress denied, the Service name works. -
deploy/local/e2e.shsends 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/jobsand 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.yamlruns 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=1024andfs.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,URXin the router's Envoy log.deploy.shrunsdeploy/local/workers.sh --fix, which finds workers whose registered IP differs from their pod's and recreates them;e2e.shruns it as a check only. Rerundeploy.shtoo if the context relay was recreated on its own, since thecontext-apiEndpointSlice holds the relay's IP.
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=podmanuses podman, which needssystemctl --user enable --now podman.socket. - The module's
emulator = truemode swaps what floci does not support: the Cloud SQL socket volume and Secret Manager references become a plainhost:portdatabase connection and plain environment values.