Skip to content

Agent Mesh

examples/sandboxed-agent: review fixes and a tighter cut

Antonio Ojea592c9f9f4fb9
diff --git a/development/examples/sandboxed-agent/agent.py b/development/examples/sandboxed-agent/agent.py
index 1c9e3dff..f2f354a9 100644
--- a/development/examples/sandboxed-agent/agent.py
+++ b/development/examples/sandboxed-agent/agent.py
@@ -125,35 +125,39 @@ async def github(session, method, path):
say(f"← {resp.status} {verdict or resp.text[:120]}")
+async def run(session, argv):
+ say(f"on the mesh as {session.peer_id}")
+ step = argv[0] if argv else "all"
+ if step == "models":
+ await models(session)
+ elif step == "ask":
+ await ask(session, " ".join(argv[1:]) or "In one sentence, in English: why should an agent never hold API credentials?")
+ elif step == "tool":
+ await tool(session)
+ elif step == "github":
+ await github(session, argv[1].upper(), argv[2])
+ elif step == "all":
+ await models(session)
+ await ask(session, "In one sentence, in English: why should an agent never hold API credentials?")
+ await tool(session)
+ await github(session, "GET", "/repos/google/sam/pulls?state=open&per_page=1")
+ await github(session, "POST", "/repos/google/sam/pulls")
+ await github(session, "GET", "/user")
+ else:
+ raise SystemExit(__doc__)
+
+
async def main(argv):
+ joined = False
try:
- session_cm = mesh.join()
- session = await session_cm.__aenter__()
+ async with mesh.join() as session:
+ joined = True
+ await run(session, argv)
except RuntimeError as err:
+ if joined:
+ raise
# Every router refused this identity: the admin cut it off.
raise SystemExit(f"cut off from the mesh: {str(err).splitlines()[0].rstrip(':')}")
- try:
- say(f"on the mesh as {session.peer_id}")
- step = argv[0] if argv else "all"
- if step == "models":
- await models(session)
- elif step == "ask":
- await ask(session, " ".join(argv[1:]) or "In one sentence, in English: why should an agent never hold API credentials?")
- elif step == "tool":
- await tool(session)
- elif step == "github":
- await github(session, argv[1].upper(), argv[2])
- elif step == "all":
- await models(session)
- await ask(session, "In one sentence, in English: why should an agent never hold API credentials?")
- await tool(session)
- await github(session, "GET", "/repos/google/sam/pulls?state=open&per_page=1")
- await github(session, "POST", "/repos/google/sam/pulls")
- await github(session, "GET", "/user")
- else:
- raise SystemExit(__doc__)
- finally:
- await session_cm.__aexit__(None, None, None)
trio.run(main, sys.argv[1:])
diff --git a/development/examples/sandboxed-agent/audit.py b/development/examples/sandboxed-agent/audit.py
index 9a44617e..478adb1d 100644
--- a/development/examples/sandboxed-agent/audit.py
+++ b/development/examples/sandboxed-agent/audit.py
@@ -8,31 +8,38 @@ import json
import os
import re
import sys
+from contextlib import nullcontext
AUDIT = re.compile(r"Audit Traceability\s+(\{.*\})\s*$")
KEEP = re.compile(r"\[Egress\] (Serving|Withdrawn|Assignments)|peer banned|SAM Node Online|PeerID:")
-# AUDIT_RAW=<file> keeps the unfiltered log next to the filtered view.
-raw = open(os.environ["AUDIT_RAW"], "a") if os.environ.get("AUDIT_RAW") else None
-for raw_line in sys.stdin:
- if raw:
- raw.write(raw_line)
- raw.flush()
- line = raw_line.rstrip("\n")
+
+def verdict(line):
m = AUDIT.search(line)
if m:
try:
d = json.loads(m.group(1))
except json.JSONDecodeError:
- continue
- verdict = d.get("decision", "?").upper()
+ return None
who = f"{d.get('role') or '-'} {d.get('peer_id', '')[:12]}…"
what = " ".join(x for x in (d.get("method"), d.get("path")) if x) or d.get("protocol", "")
- print(f"{verdict:<5} {who:<22} {what:<40} {d.get('target', '')}", flush=True)
- elif "peer banned" in line:
+ return f"{d.get('decision', '?').upper():<5} {who:<22} {what:<40} {d.get('target', '')}"
+ if "peer banned" in line:
peer = re.search(r'"peer":\s*"([^"]+)"', line)
- print(f"BANNED {peer.group(1) if peer else ''}: the control plane cut this member off", flush=True)
- elif KEEP.search(line):
+ return f"BANNED {peer.group(1) if peer else ''}: the control plane cut this member off"
+ if KEEP.search(line):
# Drop the timestamp and logger columns; keep the message.
- msg = line.split("\t")[-1] if "\t" in line else line
- print(msg.strip(), flush=True)
+ return (line.split("\t")[-1] if "\t" in line else line).strip()
+ return None
+
+
+# AUDIT_RAW=<file> keeps the unfiltered log next to the filtered view.
+raw_path = os.environ.get("AUDIT_RAW")
+with (open(raw_path, "a") if raw_path else nullcontext()) as raw:
+ for raw_line in sys.stdin:
+ if raw:
+ raw.write(raw_line)
+ raw.flush()
+ out = verdict(raw_line.rstrip("\n"))
+ if out:
+ print(out, flush=True)
diff --git a/development/examples/sandboxed-agent/record.sh b/development/examples/sandboxed-agent/record.sh
index f1ae9941..fb5a84e7 100755
--- a/development/examples/sandboxed-agent/record.sh
+++ b/development/examples/sandboxed-agent/record.sh
@@ -35,6 +35,8 @@ OUT=$HERE/out
SESSION=samdemo
COLS=${COLS:-190}
ROWS=${ROWS:-54}
+# Playback speed of the rendered video; the subtitles are scaled to match.
+SPEED=${SPEED:-1.25}
CAPTION_FILE=/tmp/samdemo-caption.txt
SRT=$OUT/demo.srt
GITHUB_TOKEN_FILE=${GITHUB_TOKEN_FILE:-$HOME/.config/sam-demo/github-ro}
@@ -130,8 +132,8 @@ caption() {
caption_prev_at=$now
}
-srt_time() { # seconds since REC_START -> HH:MM:SS,mmm
- python3 -c 'import sys; t=float(sys.argv[1])-float(sys.argv[2]); t=max(t,0); h=int(t//3600); m=int(t%3600//60); s=t%60; print(f"{h:02d}:{m:02d}:{int(s):02d},{int((s-int(s))*1000):03d}")' "$1" "$REC_START"
+srt_time() { # seconds since REC_START, at playback speed -> HH:MM:SS,mmm
+ python3 -c 'import sys; t=(float(sys.argv[1])-float(sys.argv[2]))/float(sys.argv[3]); t=max(t,0); h=int(t//3600); m=int(t%3600//60); s=t%60; print(f"{h:02d}:{m:02d}:{int(s):02d},{int((s-int(s))*1000):03d}")' "$1" "$REC_START" "$SPEED"
}
srt_entry() {
@@ -216,14 +218,14 @@ sleep 2
REC_START=$(date +%s.%N)
caption "An agent decides at run time which API it calls, and with what. You cannot review that in a pull request. Security wants it sandboxed; the developer wants the model, the tools and the internal API it needs."
-pause 7
-caption "The usual bridge is a VPN into the office: slow to develop against, and it turns the sandbox into a door with credentials inside."
pause 6
+caption "The usual bridge is a VPN into the office: slow to develop against, and it turns the sandbox into a door with credentials inside."
+pause 5
# 1. Two machines
caption "Left: the office. Right: a developer sandbox somewhere else, here a GitHub codespace. It has no route into the office."
expect "$SANDBOX" "curl -m 3 http://office-llm.corp.internal:11434/v1/models" 'Could not resolve host|Connection timed out' 10
-pause 3
+pause 2
# 2. The mesh and the policy
caption "The admin starts a mesh: a control plane, a router and a console, on a public https URL, with the policy in policy.json. No standing join token."
@@ -236,70 +238,70 @@ for i in $(seq 1 30); do
sleep 2
if (( i == 30 )); then echo "the tunnel at $URL never answered; abort this take" >&2; exit 1; fi
done
-pause 3
+pause 2
caption "One token per member, minted for its role. The office node gets sam:role:node and the label site=office; the agent gets the role agent."
type_in "$ADMIN" "export URL=$URL"
expect "$ADMIN" "make tokens URL=\$URL" 'tokens in' 30
# The developer receives the token out of band; here, over ssh.
gh codespace ssh -c "$CODESPACE" -- 'cat > ~/sandbox/agent-token; chmod 600 ~/sandbox/agent-token' < "$DEMO_DIR/agent-token" 2>/dev/null
-pause 2
+pause 1
caption "The policy: the agent may call the model, the MCP server and api.github.com by name, and api.github.com only with GET under /repos/google/sam/. The destination is served by nodes labelled site=office."
type_in "$ADMIN" "jq -c '.roles[1].allowed_services, .roles[1].http[0], .egress[0]' policy.json"
-pause 8
+pause 6
caption "The office node fronts a model and an MCP server on loopback. api.github.com is assigned to it by the policy; its read-only token is a file on this machine, read by the node, never by an agent."
type_in "$NODE" "export URL=$URL"
expect "$NODE" "make pep URL=\$URL 2>&1 | python3 audit.py" 'SAM Node Online' 60
-pause 4
+pause 3
# 3. The developer's program joins
caption "The agent is an ordinary Python program with the SDK. The admin handed the developer the single-use token out of band. It enrolls once and gets an identity: no sidecar, no proxy variables, no VPN."
type_in "$SANDBOX" "export SAM_CONTROL_PLANE_URL=$URL SAM_BOOTSTRAP_TOKEN_PATH=~/sandbox/agent-token"
expect "$SANDBOX" "python agent.py models" '← 200 gemma3' 90
PEER=$(grab "$SANDBOX" 'on the mesh as 12D3KooW[A-Za-z0-9]+' | awk '{print $NF}')
-pause 3
+pause 2
# 4. A model, a tool
caption "A chat completion runs on the office workstation. The answer is different every take; that is the point."
expect "$SANDBOX" "python agent.py ask" '← 200 ' 90
-pause 5
+pause 3
caption "A tool call reaches the MCP server in the office. On the left, every decision is one line in the node's log."
expect "$SANDBOX" "python agent.py tool" 'sum of 2 and 3' 60
-pause 4
+pause 2
# 5. External API
caption "An external API. GitHub answers 200: the node presented the office's token. The request the sandbox sent had none."
expect "$SANDBOX" "python agent.py github GET '/repos/google/sam/pulls?state=open&per_page=1'" '← 200 #' 60
-pause 4
+pause 3
caption "The agent may try anything. POST is outside the grant; /user is outside the grant. The network answers 403 before GitHub hears of it."
expect "$SANDBOX" "python agent.py github POST /repos/google/sam/pulls" 'http_request_denied' 60
expect "$SANDBOX" "python agent.py github GET /user" 'http_request_denied' 60
wait_for "$NODE" 'DENY.*GET /user' 30
-pause 5
+pause 3
# 6. Revoke
caption "The admin takes api.github.com off the mesh: one policy change. The node withdraws it within seconds."
expect "$ADMIN" "make revoke URL=\$URL" 'success' 30
wait_for "$NODE" 'Withdrawn egress://api.github.com' 60
-pause 2
+pause 1
caption "The same request finds no service. Nothing to revoke in the sandbox: it never had anything."
expect "$SANDBOX" "python agent.py github GET '/repos/google/sam/pulls?state=open&per_page=1'" '← 404' 60
-pause 4
+pause 3
# 7. Ban
caption "And when the admin decides this agent is done: the identity is banned, and no router admits it again."
expect "$ADMIN" "make ban URL=\$URL PEER=$PEER" 'banned' 30
wait_for "$NODE" 'BANNED' 30
expect "$SANDBOX" "python agent.py github GET /user" 'cut off from the mesh' 90
-pause 4
+pause 3
# 8. Close
caption "The developer used their own sandbox and wrote a plain program. The admin wrote one policy document and read one log. The credential never left the office."
-pause 8
+pause 6
caption "sam-mesh.dev"
-pause 4
+pause 3
# Ends the recording: the attached client exits with the session.
END=$(date +%s.%N)
@@ -319,7 +321,7 @@ kept = [lines[0]] + [l for l in lines[1:] if json.loads(l)[0] <= cutoff]
open(path, "w").write("\n".join(kept) + "\n")
EOF
-agg --cols "$COLS" --rows "$ROWS" --font-size 14 --theme monokai --idle-time-limit 30 --last-frame-duration 3 "$OUT/demo.cast" "$OUT/demo.gif"
+agg --cols "$COLS" --rows "$ROWS" --font-size 14 --theme monokai --idle-time-limit 30 --speed "$SPEED" --last-frame-duration 3 "$OUT/demo.cast" "$OUT/demo.gif"
ffmpeg -y -loglevel error -i "$OUT/demo.gif" -movflags faststart -pix_fmt yuv420p \
-vf 'scale=trunc(iw/2)*2:trunc(ih/2)*2' "$OUT/demo.mp4"
echo "recorded: $OUT/demo.cast $OUT/demo.gif $OUT/demo.mp4 $SRT"
diff --git a/site/static/demo-sandboxed-agent.mp4 b/site/static/demo-sandboxed-agent.mp4
index 60ac6f86..34440cfc 100644
Binary files a/site/static/demo-sandboxed-agent.mp4 and b/site/static/demo-sandboxed-agent.mp4 differ