diff --git a/development/examples/share-local-model/SCRIPT.md b/development/examples/share-local-model/SCRIPT.md new file mode 100644 index 000000000..406f25a52 --- /dev/null +++ b/development/examples/share-local-model/SCRIPT.md @@ -0,0 +1,141 @@ +# Share a local model: refused, then granted by name + +A two-minute recording on one machine. Node A serves a model that runs on +its own loopback. Node B, another member of the mesh, asks for it and is +refused, because nothing is reachable by default. The operator grants that +one service by name, and the same request goes through. Two agents on node B +then find the model through node B's MCP server. + +`record.sh` produces it with [VHS](https://github.com/charmbracelet/vhs). The +scenes below are what it types; the captions are in `captions.txt`. + +## Before recording + +```bash +make build # ./bin/agentmesh-one and ./bin/agentmesh-node +ollama pull gemma3:1b # Ollama listening on 127.0.0.1:11434 +``` + +Tools: `vhs`, `ttyd`, `ffmpeg`, `tmux`, `jq`, `python3`, `curl`, and the +DejaVu Sans Mono font. + +Scenes 9 and 10 use two agent CLIs, Antigravity CLI and OpenCode, configured +with node B's MCP server: + +- URL: `http://127.0.0.1:8080/mcp` +- Header: `X-Mesh-Authentication: Bearer `, where the token is the + content of `$WORK_DIR/api-token` (`record.sh` creates it on the first run) + +Both commands must work in the recording's shells. If they are not on `PATH` +(for example Windows binaries called from WSL), put wrapper functions in a +file and pass it as `AGENTS_RC`. To record without them, set `AGENTS=0`; the +video then ends after step 8. + +## Scenes + +The panes: control plane (top left), node A (bottom left), node B (top right) +and the operator, then the agents on node B (bottom right). Every node's log +goes through `audit.py`, which prints one line per authorization decision +(`ALLOW`, `DENY`) and a line when the node loads a changed policy. + +### 1. The policy + +```bash +cat mesh-policy.json +``` + +Members hold the role `mesh:role:node`. They may publish `inference://*` but +may call only `system://mesh.catalog`. + +### 2. One command starts the mesh + +```bash +agentmesh-one --data-dir ~/data/one --port 9000 --policy-file mesh-policy.json \ + --admin-token-path ~/data/admin-token --no-join-token --enroll-qr=false --log-level warn +``` + +The admin token comes from a file, so the banner does not print it, and there +is no standing join token. + +### 3. One enrollment token per node + +```bash +umask 077; for n in node-a node-b; do agentmesh-one token create --server $URL \ + --admin-token-path ~/data/admin-token --description $n | awk '/^Token:/ {print $2}' > ~/data/$n.token; done +stat -c '%A %s bytes %n' data/*.token +``` + +### 4. Node A publishes its model + +```bash +cat node-a.yaml +agentmesh-node run --control-plane $URL --bootstrap-token-path ~/data/node-a.token \ + --config node-a.yaml --data-dir ~/data/node-a --bind-addr= --allow-loopback \ + --listen /ip4/127.0.0.1/tcp/0 --control-plane-sync-interval 5s 2>&1 | python3 audit.py +``` + +### 5. Node B joins + +```bash +agentmesh-node run --control-plane $URL --bootstrap-token-path ~/data/node-b.token \ + --data-dir ~/data/node-b --bind-addr 127.0.0.1:8080 --api-token-path api-token \ + --allow-loopback --listen /ip4/127.0.0.1/tcp/0 --control-plane-sync-interval 5s 2>&1 | python3 audit.py +``` + +### 6. Refused + +```bash +cat hello.json +curl -s --unix-socket ~/data/node-b/agentmesh.sock localhost/v1/chat/completions \ + -d @hello.json | jq -r '.choices[0].message.content // .error.message' +``` + +Node B answers that no provider serves the model. Node A's pane shows `DENY`. + +### 7. Grant one service by name + +```bash +./grant.sh inference://laptop-llm +``` + +`grant.sh` reads the policy from `GET /admin/policy`, adds the service to +`mesh:role:node` and posts it to `POST /policies`. Both node panes show the +policy update within seconds. + +### 8. The same request is allowed + +The same `curl`: node A's pane shows `ALLOW` and the model answers. + +### 9. An agent: Antigravity CLI + +```bash +antigravity -p "Using the agentmesh tools, which AI models are on my mesh and which node serves them? Answer in two short lines of plain text, no markdown." +``` + +### 10. A second agent: OpenCode + +```bash +opencode run "Using the agentmesh tools, list the services on my mesh and the node that serves each. Two short lines of plain text, no markdown." +``` + +## Notes + +- The nodes pull from the control plane every 5 seconds in this demo + (`--control-plane-sync-interval 5s`; the default is 15 minutes), and a + policy event brings the pull forward, so the grant lands within seconds. +- The model's answer is different every take. + +## Recording + +```bash +./record.sh # all ten steps +AGENTS=0 ./record.sh # steps 1 to 8 +AGENTS_RC=~/agents.sh ./record.sh # agent wrappers from a file +PUBLISH=0 ./record.sh # do not copy the video to site/static +AGENTMESH_BIN=/usr/local/bin ./record.sh # installed binaries instead of ./bin +``` + +The work directory is `$WORK_DIR` (default `~/agentmesh-share-local-model`). +The video is written to `out/demo.mp4` there and, unless `PUBLISH=0`, copied +to `site/static/demo-share-local-model.mp4`; `out/thumbnail.png` is a frame +from step 8. diff --git a/development/examples/share-local-model/audit.py b/development/examples/share-local-model/audit.py new file mode 100644 index 000000000..e71141c91 --- /dev/null +++ b/development/examples/share-local-model/audit.py @@ -0,0 +1,83 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Reads an agentmesh-node log on stdin and prints the lines a viewer needs: +what the node serves, one line per authorization decision, and policy updates. + + agentmesh-node run ... 2>&1 | python3 audit.py + +Adapted from ../sandboxed-agent/audit.py. Decisions on the internal +system://mesh.catalog lookups are not shown (they are routine allows that +would crowd the pane), and a line is printed when the node loads a changed +policy. On a terminal, ALLOW, DENY and policy updates are colored. +Set AUDIT_RAW= to keep the unfiltered log as well. +""" + +import json +import os +import re +import sys +from contextlib import nullcontext + +AUDIT = re.compile(r"Audit Traceability\s+(\{.*\})\s*$") +KEEP = re.compile(r"Agent Mesh Node Online|PeerID:|Mesh policy changed|peer banned") +COLOR = { + "ALLOW": "\033[1;38;2;74;222;128m", # green + "DENY": "\033[1;38;2;248;113;113m", # red + "POLICY": "\033[1;38;2;250;204;21m", # yellow +} +RESET = "\033[0m" +# Colors only on a terminal, so a log written to a file stays plain text. +TTY = sys.stdout.isatty() + + +def paint(kind, text): + return f"{COLOR[kind]}{text}{RESET}" if TTY else text + + +def verdict(line): + m = AUDIT.search(line) + if m: + try: + d = json.loads(m.group(1)) + except json.JSONDecodeError: + return None + target = d.get("target", "") + if target.startswith("system://"): + 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", "") + decision = d.get("decision", "?").upper() + word = paint(decision, f"{decision:<5}") if decision in COLOR else f"{decision:<5}" + return f"{word} {who:<28} {what:<26} {target}" + if "Mesh policy changed" in line: + return paint("POLICY", "Policy updated by the control plane") + if "peer banned" in line: + peer = re.search(r'"peer":\s*"([^"]+)"', 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. + return (line.split("\t")[-1] if "\t" in line else line).strip() + return None + + +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/share-local-model/caption.py b/development/examples/share-local-model/caption.py new file mode 100644 index 000000000..0e34c382b --- /dev/null +++ b/development/examples/share-local-model/caption.py @@ -0,0 +1,58 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Caption pane for the recording: shows the text in a file, typed at reading +speed, and redraws whenever the file changes. + + python3 caption.py + +Adapted from the caption pane in ../multi-tier-guardrails/record.sh. A +caption that starts with "Step n/m · " draws that label in an accent color. +""" + +import sys +import textwrap +import time + +path, cols, cps = sys.argv[1], int(sys.argv[2]), float(sys.argv[3]) +sys.stdout.write("\033[?25l") # no cursor in the caption pane +last = None +while True: + try: + with open(path, encoding="utf-8") as f: + text = f.read().strip() + except FileNotFoundError: + text = "" + if text != last: + last = text + time.sleep(0.4) # let the recording show the pane before typing starts + sys.stdout.write("\033[2J\033[H\n") + label, sep, body = text.partition(" · ") + if not (sep and label.startswith("Step ")): + label, body = "", text + width = cols - 6 - (len(label) + 3 if label else 0) + first = True + for line in textwrap.wrap(body, width)[:3]: + sys.stdout.write(" ") + if first and label: + sys.stdout.write(f"\033[1;38;2;34;211;238m{label}\033[0m\033[38;2;148;163;184m · \033[0m") + first = False + sys.stdout.write("\033[1m") + for ch in line: + sys.stdout.write(ch) + sys.stdout.flush() + time.sleep(1 / cps) + sys.stdout.write("\033[0m\n") + sys.stdout.flush() + time.sleep(0.1) diff --git a/development/examples/share-local-model/captions.txt b/development/examples/share-local-model/captions.txt new file mode 100644 index 000000000..c5f12b523 --- /dev/null +++ b/development/examples/share-local-model/captions.txt @@ -0,0 +1,12 @@ +Agent Mesh connects AI agents, tools and models by service name. Nothing is reachable until a policy allows it. In this demo, node B asks to use a model that node A serves. +Step 1/10 · The mesh policy. Members may publish models, but may not call them. Deny by default. +Step 2/10 · One command starts the control plane, a router and the web console. The admin token is read from a file, so it is never printed. +Step 3/10 · The operator mints one single-use enrollment token per node. The tokens are written to files, not to the screen. +Step 4/10 · Node A publishes its local Ollama model as the service laptop-llm. Ollama stays on 127.0.0.1. Node A's pane shows every authorization decision. +Step 5/10 · Node B joins with its own identity. It offers the agents on its machine an OpenAI-compatible API and an MCP server. +Step 6/10 · Node B asks for the model. Node A checks node B's credential against the policy and refuses the call: DENY. +Step 7/10 · The operator grants one service, by name. No restart, no new credentials. Both nodes load the new policy within seconds. +Step 8/10 · The same request now reaches the model on node A over an authenticated peer-to-peer link: ALLOW. +Step 9/10 · An AI agent uses the mesh through node B's MCP server. Antigravity CLI finds the model and the node that serves it. +Step 10/10 · A second agent, OpenCode, uses the same MCP server. Its project config allows the agentmesh tools and nothing else. +Agent Mesh: an identity per node, policy on service names, deny by default. github.com/google/agentmesh · google.github.io/agentmesh diff --git a/development/examples/share-local-model/ctl.sh b/development/examples/share-local-model/ctl.sh new file mode 100755 index 000000000..9846ebf20 --- /dev/null +++ b/development/examples/share-local-model/ctl.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Off-camera controls that demo.tape calls through tmux run-shell: +# ctl.sh go focus a pane (layout.sh stores the pane ids) +# ctl.sh cap show line n of captions.txt in the caption pane + +set -o errexit +set -o nounset +set -o pipefail + +case "${1:-}" in + go) tmux select-pane -t "$(tmux show -gv "@${2:?pane name}")" ;; + cap) sed -n "${2:?caption number}p" "$(tmux show -gv @captions)" > "$(tmux show -gv @caption_file)" ;; + *) echo "usage: ctl.sh go | cap " >&2; exit 2 ;; +esac diff --git a/development/examples/share-local-model/demo-bashrc b/development/examples/share-local-model/demo-bashrc new file mode 100644 index 000000000..0dbab9ae7 --- /dev/null +++ b/development/examples/share-local-model/demo-bashrc @@ -0,0 +1,40 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Shell for every pane of the recording. layout.sh starts it as: +# env DEMO_HOME= DEMO_HOST= bash --rcfile demo-bashrc +# Every Agent Mesh command in the video is typed as shown; nothing here wraps them. + +KIT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# AGENTMESH_BIN and AGENTS_RC, written by record.sh. +# shellcheck source=/dev/null +source "$KIT/paths.env" +export PATH="$AGENTMESH_BIN:$PATH" +export URL="http://127.0.0.1:9000" + +# The demo home is the home of these shells, so the prompt reads ~ and the +# files the video uses are right there. +export HOME="${DEMO_HOME:?DEMO_HOME is not set}" +export HISTFILE="$HOME/.bash_history" +export PS1="\[\e[1;32m\]\u@${DEMO_HOST:-demo}\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]\\$ " +unset PROMPT_COMMAND + +# Optional: wrappers that make antigravity and opencode callable (see SCRIPT.md). +if [[ -n "${AGENTS_RC:-}" && -r "$AGENTS_RC" ]]; then + # shellcheck source=/dev/null + source "$AGENTS_RC" +fi + +cd ~ || return +clear diff --git a/development/examples/share-local-model/demo.tape b/development/examples/share-local-model/demo.tape new file mode 100644 index 000000000..0aee6eb30 --- /dev/null +++ b/development/examples/share-local-model/demo.tape @@ -0,0 +1,226 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# The share-local-model recording, for VHS (https://github.com/charmbracelet/vhs). +# record.sh runs it from the work directory it prepares. Pane focus and +# caption changes are off camera (Hide) through ctl.sh; every Agent Mesh +# command is typed on camera as shown. The scenes are described in SCRIPT.md. + +Output "out/raw.mp4" + +Require tmux +Require jq +Require python3 +Require agentmesh-one +Require agentmesh-node + +Set Shell "bash" +Set Width 1920 +Set Height 1080 +Set FontFamily "DejaVu Sans Mono" +Set FontSize 16 +Set Padding 14 +Set Framerate 30 +Set TypingSpeed 40ms +Set Theme "Dracula" +Set CursorBlink false + +# --- setup, off camera ---------------------------------------------------------------- +Hide +Type "bash kit/layout.sh" +Enter +Sleep 3s +Show +Sleep 1s + +# --- intro -------------------------------------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} cap 1"` +Enter +Show +Sleep 9s + +# --- step 1: a locked-down policy --------------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} cap 2"` +Enter +Show +Sleep 4s +Type "cat mesh-policy.json" +Enter +Sleep 5s + +# --- step 2: one command starts the mesh ---------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} go cp; bash #{@ctl} cap 3"` +Enter +Show +Sleep 4s +Type@25ms "agentmesh-one --data-dir ~/data/one --port 9000 --policy-file mesh-policy.json --admin-token-path ~/data/admin-token --no-join-token --enroll-qr=false --log-level warn" +Enter +Wait+Screen@60s /mesh is ready/ +Sleep 3s + +# --- step 3: one enrollment token per node ------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} go op; bash #{@ctl} cap 4"` +Enter +Show +Sleep 3s +Type "clear" +Enter +Type@25ms "umask 077; for n in node-a node-b; do agentmesh-one token create --server $URL --admin-token-path ~/data/admin-token --description $n | awk '/^Token:/ {print $2}' > ~/data/$n.token; done" +Enter +Sleep 2s +Type "stat -c '%A %s bytes %n' data/*.token" +Enter +Wait+Screen@20s /node-b\.token/ +Sleep 3s + +# --- step 4: node A publishes its model ---------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} go na; bash #{@ctl} cap 5"` +Enter +Show +Sleep 4s +Type "cat node-a.yaml" +Enter +Sleep 3s +Type@25ms "agentmesh-node run --control-plane $URL --bootstrap-token-path ~/data/node-a.token --config node-a.yaml --data-dir ~/data/node-a --bind-addr= --allow-loopback --listen /ip4/127.0.0.1/tcp/0 --control-plane-sync-interval 5s 2>&1 | python3 audit.py" +Enter +Wait+Screen@90s /Node Online/ +Sleep 3s + +# --- step 5: node B joins --------------------------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} go nb; bash #{@ctl} cap 6"` +Enter +Show +Sleep 3s +Type@25ms "agentmesh-node run --control-plane $URL --bootstrap-token-path ~/data/node-b.token --data-dir ~/data/node-b --bind-addr 127.0.0.1:8080 --api-token-path api-token --allow-loopback --listen /ip4/127.0.0.1/tcp/0 --control-plane-sync-interval 5s 2>&1 | python3 audit.py" +Enter +Wait+Screen@90s /(?s)Node Online.*Node Online/ +Sleep 3s + +# --- step 6: refused ---------------------------------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} go op; bash #{@ctl} cap 7"` +Enter +Show +Sleep 3s +Type "clear" +Enter +Type "cat hello.json" +Enter +Sleep 2s +Type@25ms `curl -s --unix-socket ~/data/node-b/agentmesh.sock localhost/v1/chat/completions -d @hello.json | jq -r '.choices[0].message.content // .error.message'` +Sleep 500ms +Enter +Wait+Screen@90s /not served by any provider/ +Wait+Screen@30s /DENY/ +Sleep 5s + +# --- step 7: grant one service by name ------------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} cap 8"` +Enter +Show +Sleep 4s +Type "./grant.sh inference://laptop-llm" +Enter +Wait+Screen@30s /Granted/ +Wait+Screen@120s /(?s)Policy updated by the control plane.*Policy updated by the control plane/ +Sleep 4s + +# --- step 8: the same request is allowed ------------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} cap 9"` +Enter +Show +Sleep 3s +Up 2 +Sleep 1s +Enter +Wait+Screen@120s /ALLOW .*chat\/completions/ +Wait+Screen@120s /(?m)@node-b:~\$ *$/ +Sleep 2s +Screenshot "out/thumbnail.png" +Sleep 3s + +# >>> agents (record.sh drops this block when AGENTS=0) +# --- step 9: an AI agent on node B: Antigravity ------------------------------------------ +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} cap 10"` +Enter +Show +Type "clear" +Enter +Sleep 4s +Type `antigravity -p "Using the agentmesh tools, which AI models are on my mesh and which node serves them? Answer in two short lines of plain text, no markdown."` +Sleep 500ms +Enter +Sleep 3s +Hide +Wait+Screen@300s /(?m)@node-b:~\$ *$/ +Show +Sleep 7s + +# --- step 10: a second agent: OpenCode --------------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} cap 11"` +Enter +Show +Type "clear" +Enter +Sleep 4s +Type `opencode run "Using the agentmesh tools, list the services on my mesh and the node that serves each. Two short lines of plain text, no markdown."` +Sleep 500ms +Enter +Sleep 4s +Hide +Wait+Screen@300s /(?m)@node-b:~\$ *$/ +Show +Sleep 8s +# <<< agents + +# --- close ----------------------------------------------------------------------------------- +Hide +Ctrl+B +Type ":" +Type@1ms `run-shell "bash #{@ctl} cap 12"` +Enter +Show +Sleep 7s diff --git a/development/examples/share-local-model/grant.sh b/development/examples/share-local-model/grant.sh new file mode 100755 index 000000000..d1392ad90 --- /dev/null +++ b/development/examples/share-local-model/grant.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Adds one service pattern to what mesh:role:node may call, through the admin API. +# ./grant.sh grants inference://laptop-llm +# ./grant.sh 'inference://*' grants every inference service +# The admin token is read from its file and passed to curl in a header file, +# so it never appears in the process list or the shell history. + +set -o errexit +set -o nounset +set -o pipefail + +PATTERN=${1:-inference://laptop-llm} +URL=${URL:-http://127.0.0.1:9000} +TOKEN_FILE=${TOKEN_FILE:-$HOME/data/admin-token} + +fail() { echo "grant: $*" >&2; exit 1; } + +[[ -r "$TOKEN_FILE" ]] || fail "admin token not readable at $TOKEN_FILE; is agentmesh-one running?" + +umask 077 +hdr=$(mktemp) +cur=$(mktemp) +new=$(mktemp) +trap 'rm -f "$hdr" "$cur" "$new"' EXIT + +printf 'Authorization: Bearer %s\n' "$(<"$TOKEN_FILE")" > "$hdr" + +curl -sS --fail-with-body -H @"$hdr" "$URL/admin/policy" -o "$cur" \ + || fail "could not read the policy from $URL/admin/policy" +jq -e '.roles[] | select(.name == "mesh:role:node")' "$cur" >/dev/null \ + || fail "the policy has no mesh:role:node role" +jq --arg p "$PATTERN" ' + (.roles[] | select(.name == "mesh:role:node") | .allowed_services) + |= ((. // []) + [$p] | unique) +' "$cur" > "$new" +curl -sS --fail-with-body -H @"$hdr" -H 'Content-Type: application/json' \ + --data-binary @"$new" "$URL/policies" >/dev/null \ + || fail "the control plane refused the new policy" + +echo "Granted: mesh:role:node may now call $PATTERN" +jq '.roles[] | select(.name == "mesh:role:node")' "$new" diff --git a/development/examples/share-local-model/hello.json b/development/examples/share-local-model/hello.json new file mode 100644 index 000000000..761ea275c --- /dev/null +++ b/development/examples/share-local-model/hello.json @@ -0,0 +1 @@ +{"model": "gemma3:1b", "messages": [{"role": "user", "content": "Say hello to the mesh in five words. No emoji."}]} diff --git a/development/examples/share-local-model/layout.sh b/development/examples/share-local-model/layout.sh new file mode 100755 index 000000000..b3c639242 --- /dev/null +++ b/development/examples/share-local-model/layout.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Builds the tmux session for the recording and attaches to it. demo.tape runs +# it from the work directory that record.sh prepares. +# +# +---------------------------+-------------------------------+ +# | control plane | node B | +# +---------------------------+-------------------------------+ +# | node A | operator, then agents on B | +# +---------------------------+-------------------------------+ +# | caption (full width, typed at reading speed) | +# +-----------------------------------------------------------+ +# +# Each shell gets a host label for its prompt (user@node-a:~$) and the demo +# home as its home. Pane ids are stored as @cp @na @nb @op for ctl.sh. + +set -o errexit +set -o nounset +set -o pipefail + +KIT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +WORK_DIR=$(dirname "$KIT") +SESSION=share-local-model +COLS=$(tput cols) +ROWS=$(tput lines) +sh_for() { echo "env DEMO_HOME=$WORK_DIR/home DEMO_HOST=$1 bash --rcfile $KIT/demo-bashrc"; } + +tmux kill-session -t "$SESSION" 2>/dev/null || true +tmux -f "$KIT/tmux.conf" new-session -d -s "$SESSION" -x "$COLS" -y "$ROWS" "$(sh_for control-plane)" +tmux set -g @ctl "$KIT/ctl.sh" +tmux set -g @captions "$KIT/captions.txt" +tmux set -g @caption_file "$WORK_DIR/caption.txt" + +CP=$(tmux display -p -t "$SESSION:0.0" '#{pane_id}') +CAP=$(tmux split-window -d -P -F '#{pane_id}' -v -f -l 5 -t "$CP" \ + "python3 $KIT/caption.py $WORK_DIR/caption.txt $COLS 38") +NB=$(tmux split-window -d -P -F '#{pane_id}' -h -l 52% -t "$CP" "$(sh_for node-b)") +NA=$(tmux split-window -d -P -F '#{pane_id}' -v -l 60% -t "$CP" "$(sh_for node-a)") +OP=$(tmux split-window -d -P -F '#{pane_id}' -v -l 70% -t "$NB" "$(sh_for node-b)") +tmux set -g @cp "$CP" +tmux set -g @na "$NA" +tmux set -g @nb "$NB" +tmux set -g @op "$OP" + +tmux select-pane -t "$CP" -T "control plane · agentmesh-one" +tmux select-pane -t "$NA" -T "node A · serves a local model (Ollama, gemma3:1b)" +tmux select-pane -t "$NB" -T "node B · another member of the mesh" +tmux select-pane -t "$OP" -T "operator, then the agents on node B" +tmux select-pane -t "$CAP" -T "" +tmux select-pane -t "$OP" + +exec tmux attach -t "$SESSION" diff --git a/development/examples/share-local-model/mesh-policy.json b/development/examples/share-local-model/mesh-policy.json new file mode 100644 index 000000000..d8df5fdb8 --- /dev/null +++ b/development/examples/share-local-model/mesh-policy.json @@ -0,0 +1,20 @@ +{ + "roles": [ + { + "name": "mesh-admin", + "allowed_services": ["*"], + "allowed_targets": ["*"] + }, + { + "name": "mesh:role:router", + "allowed_services": ["*"], + "allowed_targets": ["*"] + }, + { + "name": "mesh:role:node", + "allowed_services": ["system://mesh.catalog"], + "allowed_targets": ["*"], + "allowed_publish": ["inference://*"] + } + ] +} diff --git a/development/examples/share-local-model/node-a.yaml b/development/examples/share-local-model/node-a.yaml new file mode 100644 index 000000000..a885ad1ef --- /dev/null +++ b/development/examples/share-local-model/node-a.yaml @@ -0,0 +1,6 @@ +version: "v1alpha1" +services: + - type: inference + name: laptop-llm + description: "Ollama on my laptop" + target_url: "http://127.0.0.1:11434" diff --git a/development/examples/share-local-model/record.sh b/development/examples/share-local-model/record.sh new file mode 100755 index 000000000..50a704d82 --- /dev/null +++ b/development/examples/share-local-model/record.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Records SCRIPT.md for the share-local-model example with VHS and publishes +# it as site/static/demo-share-local-model.mp4. +# +# ./record.sh all ten steps (needs antigravity and opencode) +# AGENTS=0 ./record.sh steps 1 to 8, without the agent scenes +# PUBLISH=0 ./record.sh record, but leave site/static untouched + +set -o errexit +set -o nounset +set -o pipefail + +HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +REPO_ROOT=$(cd "$HERE/../../.." && pwd) +WORK_DIR=${WORK_DIR:-$HOME/agentmesh-share-local-model} +KIT=$WORK_DIR/kit +DEMO_HOME=$WORK_DIR/home +OUT=$WORK_DIR/out +MODEL=${MODEL:-gemma3:1b} +AGENTS=${AGENTS:-1} +AGENTS_RC=${AGENTS_RC:-} +PUBLISH=${PUBLISH:-1} +API_TOKEN_FILE=${API_TOKEN_FILE:-$WORK_DIR/api-token} +AGENTMESH_BIN=${AGENTMESH_BIN:-$REPO_ROOT/bin} +SESSION=share-local-model + +fail() { echo "record: $*" >&2; exit 1; } + +for t in vhs ttyd ffmpeg ffprobe tmux jq python3 curl; do + command -v "$t" >/dev/null || fail "$t is not installed" +done +if [[ "$AGENTMESH_BIN" == "$REPO_ROOT/bin" ]] && + [[ ! -x "$AGENTMESH_BIN/agentmesh-one" || ! -x "$AGENTMESH_BIN/agentmesh-node" ]]; then + make -C "$REPO_ROOT" build +fi +[[ -x "$AGENTMESH_BIN/agentmesh-one" && -x "$AGENTMESH_BIN/agentmesh-node" ]] \ + || fail "agentmesh-one and agentmesh-node not found in $AGENTMESH_BIN" +export PATH="$AGENTMESH_BIN:$PATH" + +if pgrep -x agentmesh-one >/dev/null || pgrep -x agentmesh-node >/dev/null; then + fail "an agentmesh process is already running; stop it before recording" +fi +curl -fsS http://127.0.0.1:11434/api/version >/dev/null \ + || fail "Ollama is not answering on 127.0.0.1:11434" +# Load the model now so the first answer on camera is quick. +curl -fsS http://127.0.0.1:11434/api/generate \ + -d "{\"model\":\"$MODEL\",\"prompt\":\"Reply with OK.\",\"stream\":false,\"keep_alive\":\"30m\"}" >/dev/null \ + || fail "$MODEL did not answer; run: ollama pull $MODEL" + +rm -rf "$KIT" "$DEMO_HOME" "$OUT" +mkdir -p "$KIT" "$DEMO_HOME/data" "$OUT" +cp "$HERE"/{demo.tape,layout.sh,ctl.sh,tmux.conf,demo-bashrc,caption.py,captions.txt} "$KIT/" +cp "$HERE"/{mesh-policy.json,node-a.yaml,hello.json,audit.py,grant.sh} "$DEMO_HOME/" +chmod +x "$DEMO_HOME/grant.sh" + +if [[ "$AGENTS" == 1 ]]; then + if [[ -n "$AGENTS_RC" ]]; then + [[ -r "$AGENTS_RC" ]] || fail "AGENTS_RC=$AGENTS_RC is not readable" + elif ! command -v antigravity >/dev/null || ! command -v opencode >/dev/null; then + fail "antigravity and opencode are not on PATH; set AGENTS_RC or AGENTS=0 (see SCRIPT.md)" + fi +else + # Without the agent scenes: drop them from the tape and renumber the captions. + sed -i '/^# >>> agents/,/^# <<< agents/d' "$KIT/demo.tape" + sed -i 's#^Step \([0-9]*\)/10 #Step \1/8 #' "$KIT/captions.txt" +fi +printf 'AGENTMESH_BIN=%q\nAGENTS_RC=%q\n' "$AGENTMESH_BIN" "$AGENTS_RC" > "$KIT/paths.env" + +# Node B's local API token. It persists across takes, because the agents' +# MCP configuration sends it to node B. +if [[ ! -s "$API_TOKEN_FILE" ]]; then + (umask 077; od -An -tx1 -N32 /dev/urandom | tr -d ' \n' > "$API_TOKEN_FILE"; echo >> "$API_TOKEN_FILE") +fi +install -m 600 "$API_TOKEN_FILE" "$DEMO_HOME/api-token" +# A fresh admin token for this take, read from a file so the banner and the +# shell history never show it. +(umask 077; od -An -tx1 -N32 /dev/urandom | tr -d ' \n' > "$DEMO_HOME/data/admin-token"; echo >> "$DEMO_HOME/data/admin-token") +: > "$WORK_DIR/caption.txt" + +cleanup() { tmux kill-session -t "$SESSION" 2>/dev/null || true; } +trap cleanup EXIT + +(cd "$WORK_DIR" && vhs "$KIT/demo.tape") +cleanup + +[[ -s "$OUT/raw.mp4" ]] || fail "vhs wrote no video" +dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$OUT/raw.mp4") +fade_out=$(awk -v d="$dur" 'BEGIN { printf "%.2f", d - 1.0 }') +ffmpeg -v error -y -i "$OUT/raw.mp4" \ + -vf "fade=t=in:st=0:d=0.8,fade=t=out:st=${fade_out}:d=1.0" \ + -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -movflags +faststart \ + "$OUT/demo.mp4" + +if [[ "$PUBLISH" == 1 ]]; then + cp "$OUT/demo.mp4" "$REPO_ROOT/site/static/demo-share-local-model.mp4" + echo "recorded and published: $REPO_ROOT/site/static/demo-share-local-model.mp4" +else + echo "recorded: $OUT/demo.mp4" +fi diff --git a/development/examples/share-local-model/tmux.conf b/development/examples/share-local-model/tmux.conf new file mode 100644 index 000000000..da60b24f4 --- /dev/null +++ b/development/examples/share-local-model/tmux.conf @@ -0,0 +1,31 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# tmux look for the recording: titled panes, a caption pane, no status bar. +set -g default-terminal "tmux-256color" +set -g base-index 0 +set -g pane-base-index 0 +set -g mouse off +set -g escape-time 0 +set -g allow-rename off +set -g allow-set-title off +set -g status off +set -g message-style "bg=#0e7490,fg=#ffffff" + +# Pane titles on the top border; the active pane is highlighted. +# The caption pane has an empty title and shows a plain border. +set -g pane-border-status top +set -g pane-border-format "#{?pane_title, #[bold]#{pane_title} ,}" +set -g pane-border-style "fg=#4b5563" +set -g pane-active-border-style "fg=#22d3ee" diff --git a/site/content/docs/use-cases/share-local-model.md b/site/content/docs/use-cases/share-local-model.md new file mode 100644 index 000000000..53dab4512 --- /dev/null +++ b/site/content/docs/use-cases/share-local-model.md @@ -0,0 +1,56 @@ +--- +title: "Share a Local Model" +linkTitle: "Share a Local Model" +weight: 6 +--- + +One node serves a model that runs on its own machine, on loopback. Another +member of the mesh asks for it and is refused, because nothing is reachable +by default. The operator grants that one service by name, and the same +request goes through. Agents that use node B then find the model through +node B's MCP server. The recording runs the control plane and both nodes on +one machine. + + + +Source: [`development/examples/share-local-model/`](https://github.com/google/agentmesh/tree/main/development/examples/share-local-model). +The scenes, the captions and the recording recipe are in its +[`SCRIPT.md`](https://github.com/google/agentmesh/blob/main/development/examples/share-local-model/SCRIPT.md). + +## The idea + +A model on a workstation is useful to more people than the one who runs it, +but opening its port lets anyone who reaches the port use it. Here the model +stays on 127.0.0.1. The node next to it publishes it under a name, +`inference://laptop-llm`, and the policy decides who may call that name. + +## The pieces + +**One policy** ([`mesh-policy.json`](https://github.com/google/agentmesh/blob/main/development/examples/share-local-model/mesh-policy.json)). +Members hold the role `mesh:role:node`: they may publish `inference://*` but +may call only `system://mesh.catalog`. + +**Node A** ([`node-a.yaml`](https://github.com/google/agentmesh/blob/main/development/examples/share-local-model/node-a.yaml)) +publishes Ollama at `http://127.0.0.1:11434` as `laptop-llm`. + +**Node B** offers the agents on its machine an OpenAI-compatible API +(`/v1/chat/completions`) and an MCP server (`/mcp`). + +**[`audit.py`](https://github.com/google/agentmesh/blob/main/development/examples/share-local-model/audit.py)** +filters each node's log to one line per authorization decision, plus a line +when the node loads a changed policy. + +## What happens + +1. Node B asks its own socket for `gemma3:1b`. Node A checks node B's + credential against the policy and refuses: `DENY`. +2. The operator adds `inference://laptop-llm` to the node role through the + admin API (`POST /policies`). Both nodes load the new policy within + seconds: the demo pulls every 5 seconds (`--control-plane-sync-interval 5s`, + 15 minutes by default), and a policy event brings the pull forward. +3. The same request: `ALLOW`, and the answer comes from the model on node A + over an authenticated peer-to-peer link. +4. Antigravity CLI and OpenCode, pointed at node B's MCP server, find the + model and the node that serves it. diff --git a/site/layouts/index.html b/site/layouts/index.html index 6a06ca689..69dcd2f3d 100644 --- a/site/layouts/index.html +++ b/site/layouts/index.html @@ -394,6 +394,10 @@

Recorded walkthroughs

Warm agent pools across nodes

See use cases
+ diff --git a/site/static/demo-share-local-model.mp4 b/site/static/demo-share-local-model.mp4 new file mode 100644 index 000000000..c432d3d83 Binary files /dev/null and b/site/static/demo-share-local-model.mp4 differ