← All Labs Lab 10 / 28 Serve habitats over A2A
Lab 10 · Durable agents · Folded back

Serve habitats over A2A

One HabitatHost opens each habitat's SQLite file on its first request, once, only for safe ids, and serves it as an A2A 1.0 agent whose card points at a reachable endpoint.

Folded back Needs: chat Smoke: pnpm exec tsx examples/10-habitats-over-a2a/host.ts --smoke Run: pnpm exec tsx examples/10-habitats-over-a2a/host.ts Source: examples/10-habitats-over-a2a
bash
pnpm exec tsx examples/10-habitats-over-a2a/host.ts --smoke

One HabitatHost serves every habitat under /habitats/<id>/ on one Node HTTP server (createHttpHandler). Each habitat is a DurableHabitat with its own habitats/<id>/habitat.sqlite, opened when the first request for it arrives. The model is faux, so the lab runs offline.

CheckWhat it shows
lazy-openNothing is open and no data directory exists before the first request
card-reachableThe agent card's endpoint is the address the request came in on
a2a-answerA2A 1.0 SendMessage to that endpoint completes with the habitat's answer
own-sqliteThe habitat got its own habitats/research/habitat.sqlite
open-onceTwo simultaneous first requests to a new habitat open its file once
id-traversal-refusedAn id like ..%2F..%2Fescaped gets 400 and creates nothing

Revert the HabitatHost fix in packages/habitat/src/durable/durable-habitat.ts and four checks fail.

Prompt

Coding-Agent Prompt · Ready to Paste
Build examples/10-habitats-over-a2a/host.ts: one @umwelten/habitat HabitatHost serving many SQLite-backed DurableHabitats as A2A 1.0 agents on one Node HTTP server via createHttpHandler, with a faux model so it runs offline. Check, printing PASS/FAIL per check and exiting 1 on any failure: nothing opens before the first request; each habitat gets its own <dataDir>/<id>/habitat.sqlite; the agent card's endpoint is reachable and SendMessage to it completes; two simultaneous first requests open the habitat once; an id that decodes to a path outside dataDir is refused and creates nothing. Strict TypeScript, no any, clean up the temp dir in finally.
Works in Pi, Claude Code, Codex & Antigravity Offline mock first → --live opt-in

What we learned

The answer

One HabitatHost serves many habitats from one process, each from its own SQLite file opened on demand. Going through the lab found three bugs in HabitatHost.createHttpHandler, all fixed in the package with a regression test (durable-habitat.test.ts, "serves only safe habitat ids over HTTP…"):

  • Path traversal through the habitat id. The id from /habitats/<id>/ was percent-decoded and joined onto dataDir, so /habitats/..%2F..%2Fescaped/… opened (and created) a habitat directory outside the data directory. Ids must now match ^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$; anything else gets 400, and HabitatHost.get() rejects it too.
  • Concurrent first requests opened the file twice. Two requests for a not-yet-open habitat each ran DurableHabitat.open on the same habitat.sqlite, which breaks pi-durable's one-writer rule. First opens in flight are now shared.
  • The agent card advertised an unreachable endpoint. Without a publicOrigin, the card said http://127.0.0.1/habitats/<id>/a2a, with no port, so a client following the card got connection refused. It now uses the request's Host.

What going through it found about the lab

  • The lab checked nothing. It printed the card name and the raw task JSON and always exited 0. It now has six PASS/FAIL checks and exits 1 on failure.
  • The old README and prompt promised more than the code did (repo guards, probes, ui:// resources). Those are Labs 09, 07 and 13; this lab now tests only the host.
  • The existing host test never used createHttpHandler. It wired one habitat's A2A surface to a server by hand, which is why none of these bugs showed up.

Gotchas

  • Behind a proxy, set publicOrigin. The Host fallback is right for direct access; a proxy that rewrites Host would advertise its internal address.
  • No auth by default. Without a bearerToken from configure, each habitat allows unauthenticated loopback callers only.

Not tested

  • Idle eviction and reopen through HTTP (Lab 08 covers eviction directly).
  • Authenticated habitats (bearerToken), and many habitats at once.
  • A live model.

Smoke run

Visual Target Wireframe ASCII Layout
  PASS  lazy-open: before any request: 0 open, data dir exists=false
  PASS  card-reachable: card endpoint http://127.0.0.1:33607/habitats/research/a2a
  PASS  a2a-answer: TASK_STATE_COMPLETED "Answer 1 from a SQLite-backed habitat."
  PASS  own-sqlite: habitats/research/habitat.sqlite exists=true
  PASS  open-once: statuses 200, 200; habitat opened 1 time(s)
  PASS  id-traversal-refused: HTTP 400; open: [research, support]; data dir: [research, support]

  6/6 passed

Before the fix, 3/7: card-reachable, card-endpoint-answers, open-once and id-traversal-refused failed.