← All Examples Example 09 / 12 Host-Agnostic DurableHabitat & A2A 1.0 Multi-Agent Host
Prompt-First Example 09 · Durable Habitats · pi-durable · A2A 1.0 Host

Host-Agnostic
DurableHabitat & A2A 1.0

Multiplex SQLite-backed Pi Durable agents on one Node HTTP server with Owned/Mounted repository guards, ui:// resources, and A2A 1.0 JSON-RPC.

File: examples/durable-habitat/host.ts Recreate: pnpm tsx examples/durable-habitat/host.ts Packages: @umwelten/habitat @umwelten/protocols @earendil-works/pi-durable
01 — The Prompt You Give Pi
“Stand up a multi-tenant HabitatHost over @earendil-works/pi-durable that lazily opens per-Habitat SQLite databases on HTTP requests, enforces Owned read/write vs. Mounted read-only repository paths via a ToolTask beforeTool hook, and exposes A2A 1.0 Agent Cards (/.well-known/agent-card.json) plus JSON-RPC SendMessage (/habitats/:id/a2a) — with zero Cloudflare Worker dependencies.”
Paste directly into pi or your coding agent with @umwelten/* installed. 6 min walkthrough
02 — The Example That Gets Built

Live workspace source: examples/durable-habitat/host.ts

Read directly from disk at site-build time — byte-for-byte identical to the typechecked script in this repo.

TypeScript examples/durable-habitat/host.ts
pnpm tsx examples/durable-habitat/host.ts
1#!/usr/bin/env node
2/**
3 * Host-Agnostic `DurableHabitat` & `HabitatHost` Demo (`@earendil-works/pi-durable`).
4 *
5 * Multiplexes stateful SQLite-backed Habitats on a single Node HTTP server with:
6 *   - Owned read/write vs. Mounted read-only repository guards (`ToolTask` hook)
7 *   - Turn Fan-out `Annotation` probes
8 *   - `ui://` UI resource publication (`ConversationUIResourcesDoc`)
9 *   - A2A 1.0 JSON-RPC + Agent Card serving (`@leocavalcante/pi-a2a/durable`)
10 *
11 * Run: pnpm tsx examples/durable-habitat/host.ts
12 */
13
14import { createServer } from "node:http";
15import { mkdtemp, rm } from "node:fs/promises";
16import { tmpdir } from "node:os";
17import { join } from "node:path";
18import { createModels, fauxAssistantMessage, fauxProvider } from "@earendil-works/pi-ai";
19import { HabitatHost } from "@umwelten/habitat";
20
21const dataDir = await mkdtemp(join(tmpdir(), "umwelten-durable-host-"));
22const faux = fauxProvider({ provider: "faux", models: [{ id: "habitat-v1" }] });
23faux.setResponses([
24  fauxAssistantMessage("Hello from a SQLite-backed DurableHabitat over A2A 1.0!"),
25]);
26
27const host = new HabitatHost({
28  dataDir,
29  defaultModel: { provider: "faux", modelId: "habitat-v1" },
30  modelsFactory: () => {
31    const m = createModels();
32    m.setProvider(faux.provider);
33    return m;
34  },
35});
36
37const httpHandler = host.createHttpHandler((id) => ({
38  name: `Habitat ${id}`,
39  publicOrigin: `http://127.0.0.1:0/habitats/${id}`,
40}));
41
42const server = createServer((req, res) => {
43  void httpHandler(req, res);
44});
45
46await new Promise<void>((r) => server.listen(0, "127.0.0.1", r));
47const port = (server.address() as { port: number }).port;
48
49try {
50  // 1. Discover the Habitat's A2A 1.0 Agent Card
51  const cardRes = await fetch(
52    `http://127.0.0.1:${port}/habitats/research/.well-known/agent-card.json`,
53  );
54  const card = (await cardRes.json()) as { name: string };
55  console.log("Agent Card:", card.name);
56
57  // 2. Send an A2A 1.0 `SendMessage` JSON-RPC request
58  const rpcRes = await fetch(`http://127.0.0.1:${port}/habitats/research/a2a`, {
59    method: "POST",
60    headers: {
61      "content-type": "application/json",
62      "a2a-version": "1.0",
63    },
64    body: JSON.stringify({
65      jsonrpc: "2.0",
66      id: "req-1",
67      method: "SendMessage",
68      params: {
69        message: {
70          messageId: "msg-1",
71          role: "ROLE_USER",
72          parts: [{ text: "Ping DurableHabitat!" }],
73        },
74      },
75    }),
76  });
77  const rpcBody = await rpcRes.json();
78  console.log("A2A 1.0 Task Result:", JSON.stringify(rpcBody, null, 2));
79} finally {
80  server.close();
81  await host.closeAll();
82  await rm(dataDir, { recursive: true, force: true });
83}
03 — Captured Execution Telemetry

Captured stdout from running pnpm tsx examples/durable-habitat/host.ts

Captured Run Output pnpm tsx examples/durable-habitat/host.ts
Agent Card: Habitat research
A2A 1.0 Task Result: {
  "jsonrpc": "2.0",
  "id": "req-1",
  "result": {
    "task": {
      "id": "ba543585-5c87-4768-81fb-0c6ac491f4d2",
      "contextId": "c4d5e3ee-247c-4ace-a8bd-7dce40cfeebd",
      "status": {
        "state": "TASK_STATE_COMPLETED",
        "message": {
          "messageId": "answer-18",
          "contextId": "c4d5e3ee-247c-4ace-a8bd-7dce40cfeebd",
          "taskId": "ba543585-5c87-4768-81fb-0c6ac491f4d2",
          "role": "ROLE_AGENT",
          "parts": [
            {
              "text": "Hello from a SQLite-backed DurableHabitat over A2A 1.0!",
              "mediaType": "text/plain"
            }
          ]
        },
        "timestamp": "2026-11-22T15:24:18.030Z"
      },
      "artifacts": [
        {
          "artifactId": "answer",
          "name": "answer",
          "description": "Final text from the committed Durable submission",
          "parts": [
            {
              "text": "Hello from a SQLite-backed DurableHabitat over A2A 1.0!",
              "mediaType": "text/plain"
            }
          ]
        }
      ],
      "history": [
        {
          "messageId": "msg-1",
          "contextId": "c4d5e3ee-247c-4ace-a8bd-7dce40cfeebd",
          "taskId": "ba543585-5c87-4768-81fb-0c6ac491f4d2",
          "role": "ROLE_USER",
          "parts": [
            {
              "text": "Ping DurableHabitat!",
              "mediaType": "text/plain"
            }
          ]
        },
        {
          "messageId": "answer-18",
          "contextId": "c4d5e3ee-247c-4ace-a8bd-7dce40cfeebd",
          "taskId": "ba543585-5c87-4768-81fb-0c6ac491f4d2",
          "role": "ROLE_AGENT",
          "parts": [
            {
              "text": "Hello from a SQLite-backed DurableHabitat over A2A 1.0!",
              "mediaType": "text/plain"
            }
          ]
        }
      ],
      "metadata": {
        "messageId": "msg-1"
      }
    }
  }
}
04 — How & Why This Extends Pi

Architectural notes

Rebasing @umwelten/habitat onto @earendil-works/pi-durable cut 59,492 lines down to 4,099 lines while keeping DurableHabitat strictly generic over Storage and ExecutionEnv.