Skip to content

Agent Mesh

scale: one node per host, one boundary per agent

Antonio Ojea2127803f8918
diff --git a/scripts/build-rootfs.sh b/scripts/build-rootfs.sh
index 13b354b0..900bc4bf 100755
--- a/scripts/build-rootfs.sh
+++ b/scripts/build-rootfs.sh
@@ -4,29 +4,40 @@ set -e
# Usage: ./scripts/build-rootfs.sh [GCS_URL]
GCS_URL=${1:-}
-echo "Building Alpine Linux rootfs with Python, tun2proxy v0.8.3, and Chaos Agent..."
-cat << 'EOF' > Dockerfile
-FROM alpine:3.18
-RUN apk add --no-cache python3 py3-pip openrc iproute2 socat curl
+# The guest ships the canonical agent harness, not the chaos agent. The chaos
+# agent is driven by a real model and is the right tool for asking whether the
+# mesh survives an autonomous caller; it is the wrong one for a scale run,
+# because it never issues the same request twice and needs model credentials
+# inside the sandbox, which is the arrangement this design exists to avoid.
+AGENT_SRC="${AGENT_SRC:-development/examples/agent-harness}"
+
+echo "Building Alpine rootfs: python and ${AGENT_SRC}..."
+cat << EOF > Dockerfile
+FROM golang:1.26.5 AS init
+WORKDIR /src
+COPY cmd/nano-init/go.mod cmd/nano-init/go.sum ./
+RUN go mod download
+COPY cmd/nano-init/ ./
+RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /nano-init .
-# Install tun2proxy
-RUN apk add --no-cache unzip \
- && curl -sSL -o /tmp/tun.zip https://github.com/tun2proxy/tun2proxy/releases/download/v0.8.3/tun2proxy-x86_64-unknown-linux-musl.zip \
- && unzip /tmp/tun.zip tun2proxy-bin -d /tmp/ \
- && mv /tmp/tun2proxy-bin /usr/local/bin/tun2proxy \
- && chmod +x /usr/local/bin/tun2proxy \
- && rm /tmp/tun.zip
+FROM alpine:3.18
+# python for the agent, and nothing else. nano-init carries its own TCP stack
+# and speaks netlink directly, so there is no tun2proxy to download, no socat
+# and no iproute2. That also removes the last unverified binary from this
+# image: tun2proxy arrived by curl from a GitHub release with no checksum.
+RUN apk add --no-cache python3 py3-pip
-COPY cmd/chaos-agent /app/chaos-agent
-RUN pip3 install --no-cache-dir -r /app/chaos-agent/requirements.txt --break-system-packages
+COPY ${AGENT_SRC} /app/agent
+RUN pip3 install --no-cache-dir -r /app/agent/requirements.txt --break-system-packages
+COPY --from=init /nano-init /usr/local/bin/nano-init
RUN rm -f /sbin/init
COPY --chmod=755 scripts/microvm-init.sh /sbin/init
EOF
# Build the docker container
-docker build -t chaos-rootfs -f Dockerfile .
-CONTAINER_ID=$(docker create chaos-rootfs)
+docker build -t agent-rootfs -f Dockerfile .
+CONTAINER_ID=$(docker create agent-rootfs)
docker export $CONTAINER_ID > rootfs.tar
docker rm $CONTAINER_ID
diff --git a/scripts/launch-microvms.sh b/scripts/launch-microvms.sh
index d16b4b2e..eb747e91 100755
--- a/scripts/launch-microvms.sh
+++ b/scripts/launch-microvms.sh
@@ -1,83 +1,143 @@
#!/bin/bash
-set -e
+# Launch N agent sandboxes on one host.
+#
+# The shape here is the point, and it is not what this script used to do. It
+# ran one sam-node per microVM, which made every agent a mesh member: its own
+# enrolment, its own libp2p host, its own place in the DHT. That does not reach
+# a thousand agents on a host, and it measures the wrong thing besides. An
+# agent is a principal, not a peer.
+#
+# So: one sam-node for the host, which is the mesh member, and one sam-box per
+# agent, which holds no mesh identity at all and names its agent on every
+# request. Adding an agent costs a boundary, not an enrolment.
+set -euo pipefail
COUNT=${1:-1}
-WORKDIR="/opt/microvm"
+WORKDIR="${WORKDIR:-/opt/microvm}"
+CONTROL_PLANE="${CONTROL_PLANE:-https://bananas.sam-mesh.dev}"
+AGENT_DOMAIN="${AGENT_DOMAIN:-scale.sam-mesh.dev}"
+
+# Firecracker defaults to 128 MiB, which silently decides how many agents fit
+# on a host. Saying it out loud makes density a parameter of the experiment
+# rather than a property of the tool.
+VM_MEM_MIB="${VM_MEM_MIB:-256}"
+VM_VCPUS="${VM_VCPUS:-1}"
if [ ! -d "$WORKDIR" ]; then
- echo "Error: $WORKDIR does not exist. Did cloud-init finish?"
+ echo "Error: $WORKDIR does not exist. Did cloud-init finish?" >&2
exit 1
fi
-echo "=== Launching $COUNT MicroVMs ==="
-for i in $(seq 1 $COUNT); do
+NODE_UDS="/var/run/sam-node.sock"
+NODE_DIR="/var/lib/sam-node"
+
+echo "=== One node for the host ==="
+mkdir -p "$NODE_DIR"
+if ! pgrep -f "sam-node run .*${NODE_UDS}" >/dev/null; then
+ rm -f "$NODE_UDS"
+ sam-node run \
+ --data-dir "$NODE_DIR" \
+ --control-plane "$CONTROL_PLANE" \
+ --bind-addr "" \
+ --socket-path "$NODE_UDS" \
+ > /var/log/sam-node.log 2>&1 &
+
+ # The boundaries are useless before the node answers, and starting a
+ # thousand of them against a socket that does not exist yet produces a
+ # thousand identical failures instead of one clear one.
+ for _ in $(seq 1 600); do
+ [ -S "$NODE_UDS" ] && break
+ sleep 0.1
+ done
+ [ -S "$NODE_UDS" ] || {
+ echo "node never bound $NODE_UDS" >&2
+ tail -20 /var/log/sam-node.log >&2
+ exit 1
+ }
+fi
+echo "Node ready at $NODE_UDS"
+
+fc_put() {
+ curl -sf -X PUT --unix-socket "$1" \
+ "http://localhost$2" -H 'Content-Type: application/json' -d "$3" > /dev/null
+}
+
+echo "=== Launching $COUNT agent sandboxes ==="
+for i in $(seq 1 "$COUNT"); do
VM_ID="vm-$i"
- # Firecracker's vsock device multiplexes guest->host connections onto
- # "<uds_path>_<port>", so the boundary has to listen on that exact name for
+ # Firecracker's vsock multiplexes guest connections onto
+ # "<uds_path>_<port>", so the boundary must listen on that exact name for
# the guest's connections to CID 2 port 1080 to arrive.
VSOCK_UDS="/var/run/sam-$VM_ID.vsock"
BOUNDARY_UDS="${VSOCK_UDS}_1080"
- NODE_UDS="/var/run/sam-node-$VM_ID.sock"
API_SOCKET="/tmp/firecracker-$VM_ID.socket"
+ BUNDLE="/var/run/sam-$VM_ID.bundle.yaml"
+
+ rm -f "$VSOCK_UDS" "$BOUNDARY_UDS" "$API_SOCKET"
+
+ # Each sandbox is a different agent, because a thousand sandboxes sharing
+ # one identity would tell the mesh it is serving one agent very hard. The
+ # identifier is dot-separated so one rule can match the whole population:
+ # *.scale.sam-mesh.dev admits these and nothing else.
+ #
+ # An empty egress allowance is the interesting default: these agents reach
+ # the mesh and nothing else, so anything they get to is something policy
+ # granted.
+ cat > "$BUNDLE" <<EOF
+version: v1
+agent:
+ id: agent-${i}.${AGENT_DOMAIN}
+egress:
+ allow: []
+EOF
+
+ # There is no credential issuer in this harness, and the flag says so
+ # rather than a default quietly meaning it.
+ sam-box run \
+ --socket "$BOUNDARY_UDS" \
+ --sidecar-socket "$NODE_UDS" \
+ --bundle "$BUNDLE" \
+ --insecure-unverified-bundle \
+ > "/var/log/sam-box-$VM_ID.log" 2>&1 &
+
+ firecracker --api-sock "$API_SOCKET" > "/var/log/fc-$VM_ID.log" 2>&1 &
+
+ for _ in $(seq 1 100); do
+ [ -S "$API_SOCKET" ] && break
+ sleep 0.05
+ done
+
+ fc_put "$API_SOCKET" /boot-source "{
+ \"kernel_image_path\": \"$WORKDIR/vmlinux.bin\",
+ \"boot_args\": \"console=ttyS0 reboot=k panic=1 pci=off\"
+ }"
+
+ fc_put "$API_SOCKET" /machine-config "{
+ \"vcpu_count\": $VM_VCPUS,
+ \"mem_size_mib\": $VM_MEM_MIB
+ }"
+
+ cp "$WORKDIR/rootfs.ext4" "$WORKDIR/rootfs-$VM_ID.ext4"
+ fc_put "$API_SOCKET" /drives/rootfs "{
+ \"drive_id\": \"rootfs\",
+ \"path_on_host\": \"$WORKDIR/rootfs-$VM_ID.ext4\",
+ \"is_root_device\": true,
+ \"is_read_only\": false
+ }"
+
+ # Guest CID 3 is the first available guest CID. Connections the guest makes
+ # to CID 2 surface on the host as "<uds_path>_<port>".
+ fc_put "$API_SOCKET" /vsock "{
+ \"vsock_id\": \"vsock0\",
+ \"guest_cid\": 3,
+ \"uds_path\": \"$VSOCK_UDS\"
+ }"
+
+ fc_put "$API_SOCKET" /actions '{ "action_type": "InstanceStart" }'
- # Clean up old sockets
- rm -f $VSOCK_UDS $BOUNDARY_UDS $NODE_UDS $API_SOCKET
-
- # The node owns the mesh identity; sam-box holds none and simply consumes
- # the node's API socket on the agent's behalf.
- mkdir -p /tmp/sam-node-$VM_ID
- sam-node run --data-dir "/tmp/sam-node-$VM_ID" \
- --control-plane "https://bananas.sam-mesh.dev" \
- --bind-addr "" --socket-path "$NODE_UDS" \
- --log-level debug > /var/log/sam-node-$VM_ID.log 2>&1 &
-
- sam-box run --socket "$BOUNDARY_UDS" --sidecar-socket "$NODE_UDS" \
- --log-level debug > /var/log/sam-box-$VM_ID.log 2>&1 &
-
- # Start Firecracker
- firecracker --api-sock $API_SOCKET > /var/log/fc-$VM_ID.log 2>&1 &
- sleep 1
-
- # Setup VM Kernel
- curl -s -X PUT --unix-socket $API_SOCKET \
- http://localhost/boot-source \
- -H 'Content-Type: application/json' \
- -d "{
- \"kernel_image_path\": \"$WORKDIR/vmlinux.bin\",
- \"boot_args\": \"console=ttyS0 reboot=k panic=1 pci=off\"
- }" > /dev/null
-
- # Setup VM Rootfs (using a copy so it's writable per VM)
- cp $WORKDIR/rootfs.ext4 $WORKDIR/rootfs-$VM_ID.ext4
- curl -s -X PUT --unix-socket $API_SOCKET \
- http://localhost/drives/rootfs \
- -H 'Content-Type: application/json' \
- -d "{
- \"drive_id\": \"rootfs\",
- \"path_on_host\": \"$WORKDIR/rootfs-$VM_ID.ext4\",
- \"is_root_device\": true,
- \"is_read_only\": false
- }" > /dev/null
-
- # Setup VM VSOCK
- # Guest CID 3 is typically the first available guest CID. Connections the
- # guest makes to CID 2 surface on the host as "<uds_path>_<port>".
- curl -s -X PUT --unix-socket $API_SOCKET \
- http://localhost/vsock \
- -H 'Content-Type: application/json' \
- -d "{
- \"vsock_id\": \"vsock0\",
- \"guest_cid\": 3,
- \"uds_path\": \"$VSOCK_UDS\"
- }" > /dev/null
-
- # Start VM
- curl -s -X PUT --unix-socket $API_SOCKET \
- http://localhost/actions \
- -H 'Content-Type: application/json' \
- -d '{ "action_type": "InstanceStart" }' > /dev/null
-
- echo "Started MicroVM $VM_ID via VSOCK -> sam-box gateway."
+ echo "Started $VM_ID as agent-${i}.${AGENT_DOMAIN} (${VM_VCPUS} vCPU, ${VM_MEM_MIB} MiB)"
done
-echo "=== All MicroVMs running! ==="
+echo "=== $COUNT sandboxes running against one node ==="
+echo "How many agents the node thinks it is serving:"
+echo " grep sam_node_agents_seen <(curl -s --unix-socket $NODE_UDS http://localhost/metrics)"
diff --git a/scripts/microvm-init.sh b/scripts/microvm-init.sh
old mode 100644
new mode 100755
index d0ec90f2..c1d8b51e
--- a/scripts/microvm-init.sh
+++ b/scripts/microvm-init.sh
@@ -1,70 +1,21 @@
#!/bin/sh
-# PID 1 in an agent sandbox.
+# /sbin/init in an agent microVM.
#
-# Its whole job is to give the guest a default route that leads to the sandbox
-# boundary, and then get out of the way. What it deliberately does not do is
-# reach into the agent: no proxy variables in its environment, no resolver
-# rewritten under it, nothing preloaded into its address space. An agent that
-# has to cooperate with its own confinement is not confined, because the next
-# library that ignores the convention, or the next subprocess that clears the
-# environment, is outside it.
+# All this does is name the boundary and the agent, because the kernel starts
+# init with no arguments. Everything that used to be here -- the tun, the
+# routes, the resolver, the proxy plumbing -- is nano-init's job now, in one
+# tested binary shared with the container sandbox rather than sixty lines of
+# shell that drifted out of step with the design twice.
#
-# So the agent below is unmodified. It makes ordinary requests to ordinary
-# names, and every one of them leaves through tun0 because there is nowhere
-# else for them to go.
+# The boundary is a Unix socket on the host. Firecracker's vsock multiplexes
+# guest connections onto "<uds_path>_<port>", so a guest connection to CID 2
+# port 1080 arrives on the host at the path sam-box serves.
set -eu
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
-ip link set dev lo up
-
-# The boundary is a Unix socket on the host. Firecracker's vsock multiplexes
-# guest connections onto "<uds_path>_<port>", so a connection to CID 2 port
-# 1080 arrives on the host at the path sam-box serves. socat is only plumbing:
-# it decides nothing and sees nothing the boundary would not.
-socat TCP-LISTEN:1080,bind=127.0.0.1,fork,reuseaddr VSOCK-CONNECT:2:1080 &
-
-# One interface, one route, and it leads to the boundary. There is no NIC in
-# this guest, so this is not the preferred way out; it is the only one.
-#
-# Every address here is link-local, because that is what these addresses are
-# for: RFC 3927 describes a single link with no router, which is exactly what a
-# tun to the boundary is. Nothing is routable and nothing can be mistaken for a
-# real destination — a sandbox numbered out of 10.0.0.0/8 would eventually be
-# deployed somewhere that already uses it.
-ip tuntap add dev tun0 mode tun
-ip link set dev tun0 up
-ip addr add 169.254.1.1/30 dev tun0
-ip route add default dev tun0
-
-# The resolver address is a fiction that never receives a packet: virtual DNS
-# answers inside tun2proxy and hands the name to the boundary instead. That is
-# the point — mesh.sam.alt has no address of its own, and the boundary is what
-# turns the name into a provider.
-echo "nameserver 169.254.1.53" > /etc/resolv.conf
-
-# socks5, because that is what sam-box speaks. The previous version of this
-# script said http, which the boundary has never spoken, and then had the agent
-# call the bridge directly, which went around the tun it had just built.
-tun2proxy --tun tun0 --proxy socks5://127.0.0.1:1080 --dns virtual \
- --dns-addr 169.254.1.53 --virtual-dns-pool 169.254.64.0/18 &
-
-# Wait for the route to exist, so the agent's first request fails for real
-# reasons rather than for being early.
-for _ in $(seq 1 100); do
- [ -d /sys/class/net/tun0 ] && break
- sleep 0.05
-done
-
-echo "Sandbox ready; starting agent." > /dev/console
-
-# The agent is given a task and nothing else. It reaches the mesh by name,
-# holds no credentials, and does not know it is in a virtual machine.
-cd /app/agent
-python3 agent.py "${AGENT_TASK:-Describe the tools you have and what each is for.}" \
- > /dev/console 2>&1 || echo "agent exited non-zero" > /dev/console
-
-echo "Agent finished. Powering off." > /dev/console
-reboot -f
+exec /usr/local/bin/nano-init run vsock://2:1080 \
+ python3 /app/agent/agent.py "${AGENT_TASK:-Describe the tools you have and what each is for.}" \
+ > /dev/console 2>&1
diff --git a/site/content/docs/user/running-agents.md b/site/content/docs/user/running-agents.md
index 554a1a0e..e018162c 100644
--- a/site/content/docs/user/running-agents.md
+++ b/site/content/docs/user/running-agents.md
@@ -196,10 +196,11 @@ docker run --rm \
worked because the container could route somewhere, it would be proving nothing.
Notice what is *not* passed: no API key, no mesh token, no endpoint, and **no
-proxy variable**. The sandbox builds one `tun0`, makes it the default route and
-points `tun2socks` at the boundary. The agent asks for `mesh.sam.alt` like any
-other host and its traffic leaves through the tun because there is nowhere else
-for it to go.
+proxy variable**. `nano-init` runs as PID 1, builds one `tun0`, makes it the
+default route and terminates the sandbox's TCP itself, opening a SOCKS5 flow to
+the boundary per connection. The agent asks for `mesh.sam.alt` like any other
+host and its traffic leaves through the tun because there is nowhere else for
+it to go.
That distinction is worth dwelling on, because the obvious alternative is to set
`HTTP_PROXY` and be done. SAM used to do exactly that, and it was the wrong
@@ -209,6 +210,10 @@ clears its environment, the next protocol that is not HTTP — each one is outsi
the boundary. Routing does not have that failure mode, because nothing has to
agree to it.
+The sandbox image needs nothing else: no tun2proxy, no socat, no iproute2. That
+matters more than tidiness, since image size is what decides how many agents fit
+on a host.
+
### 4. Watch what it did
```bash
@@ -227,36 +232,40 @@ rely on. Firecracker gives each agent its own kernel for a few tens of
milliseconds of boot time.
The layering is unchanged, and that is the useful part — the microVM swaps out
-how the sandbox reaches the socket, not what the agent does:
+how the sandbox reaches the socket, not what the agent does. The same
+`nano-init` binary runs as PID 1 in both:
```mermaid
flowchart LR
subgraph vm["Firecracker microVM: own kernel, no NIC"]
A["agent harness"]
- T["tun2socks<br/>tun0, default route"]
+ T["nano-init<br/>tun0, default route<br/>gVisor TCP stack"]
A -- "plain HTTP to mesh.sam.alt" --> T
end
T -- "vsock CID 2" --> V["host: &lt;uds&gt;_1080"]
V --> B["sam-box"]
```
-The guest has no network device at all. `tun2socks` presents a `tun0` that is
-the default route and forwards everything to the SOCKS5 boundary over vsock. The
-harness makes ordinary HTTP requests to `mesh.sam.alt` and needs no
-configuration, because from inside the VM there is nothing else it could be
+The guest has no network device at all. `nano-init` presents a `tun0` that is
+the default route, terminates TCP on it, and opens a SOCKS5 flow to the
+boundary for each connection. The harness makes ordinary HTTP requests and needs
+no configuration, because from inside the VM there is nothing else it could be
doing.
+The only difference from the container is how the boundary is named: a path when
+it is bind-mounted in, `vsock://2:1080` when there is no shared filesystem.
+Nothing else in the sandbox knows which kind it is.
+
Names are resolved by the boundary rather than in the guest, which is why the
-sandbox needs no resolver: `tun2socks` runs with virtual DNS, so `mesh.sam.alt`
-is handed to the boundary as a name instead of being looked up and failing.
+sandbox needs no resolver: `nano-init` answers with a placeholder address per
+name and remembers the pairing, so `mesh.sam.alt` reaches the boundary as a
+name and the boundary chooses a provider for it.
Every address inside the sandbox is link-local (`169.254.0.0/16`), which is
what those addresses are for: RFC 3927 describes a single link with no router,
and a tun to the boundary is exactly that. It also means nothing in the sandbox
can be confused with a real destination — a sandbox numbered out of
-`10.0.0.0/8` will eventually be deployed somewhere that already uses it. The
-resolver address is a fiction that never receives a packet; virtual DNS answers
-inside `tun2socks`.
+`10.0.0.0/8` will eventually be deployed somewhere that already uses it.
One detail costs people an afternoon: Firecracker's vsock multiplexes guest
connections onto **`<uds_path>_<port>`** on the host. A guest connecting to CID