Files
davidandClaude Opus 5.5 b609252e1c Pin srt to a tested version; support only Pop!_OS 26.04 and later
srt is pinned to 0.0.83, the version the smoketest passed with. A computer
with another version sees the agents item as to do in configure, which
installs the pinned one. Move the pin only after smoketest/run 50 80 passes.

The installer and configure stop on Pop!_OS (or Ubuntu) older than 26.04.
The smoketest builds on Ubuntu 26.04, with checks that 24.04 is refused.
26.04's own AppArmor profile lets bubblewrap make user namespaces, so the
connector's profile for it, and the code that offered to add it, are gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 20:49:54 -07:00

270 lines
10 KiB
Bash

#!/bin/bash
# configure reads TITLE and DESCRIPTION
# shellcheck disable=SC2034
TITLE="AI agents"
# shellcheck disable=SC2034
DESCRIPTION="Install Claude Code, opencode and Hermes, sandboxed"
# Exists once you've run this, even if you installed no agents
AGENTS_CONF="$CONF_DIR/agents"
CLAUDE_SETTINGS="$HOME/.claude/settings.json"
BASE_CONFIG="$CONNECTOR/sandbox/config/base.json"
BASHRC="$HOME/.bashrc"
BLOCK_START="# >>> Federated Connector: agents >>>"
BLOCK_END="# <<< Federated Connector: agents <<<"
source "$CONNECTOR/configure.d/lib/json.sh"
source "$CONNECTOR/configure.d/lib/tools.sh"
# name|title|installer|installer's arguments. Each agent's own installer, from
# its makers. --non-interactive skips Hermes' setup questions and its gateway
# service; configure hermes chooses a model provider afterwards.
AGENTS=(
"claude|Claude Code (Anthropic)|https://claude.ai/install.sh|"
"opencode|opencode|https://opencode.ai/install|"
"hermes|Hermes Agent (Nous Research)|https://hermes-agent.nousresearch.com/install.sh|--non-interactive"
)
# srt (and Claude Code's own sandbox) use bubblewrap for the files an agent
# can see, socat to link it to the network proxy, and ripgrep to find the
# files it must not change. Node.js' own builds need libatomic.
SANDBOX_PACKAGES=(bubblewrap socat ripgrep libatomic1)
# --- Typing an agent's name runs it in the sandbox -------------------------------
# The block goes last in ~/.bashrc, so agents' installers adding their own
# folders to PATH don't come before it
path_block() {
cat << EOF
$BLOCK_START
# Typing an AI agent's name runs it in the sandbox: configure agents
export PATH="$SANDBOX_BIN:\$PATH"
$BLOCK_END
EOF
}
on_path() {
[ -f "$BASHRC" ] && [ "$(grep -v '^[[:space:]]*$' "$BASHRC" | tail -n 4)" = "$(path_block)" ]
}
put_on_path() {
local tmp
touch "$BASHRC" || return 1
tmp="$(mktemp)" || return 1
awk -v start="$BLOCK_START" -v end="$BLOCK_END" \
'$0 == start { skip = 1 } !skip { print } $0 == end { skip = 0 }' "$BASHRC" > "$tmp"
{ cat "$tmp"; echo; path_block; } > "$BASHRC"
rm -f "$tmp"
echo "Agents run in the sandbox in new terminals. To run one without it: unsandboxed <agent>"
}
# --- bubblewrap and Ubuntu's AppArmor ---------------------------------------------
# Pop!_OS 26.04 lets bubblewrap make user namespaces: Ubuntu's AppArmor
# profile for it (bwrap-userns-restrict) allows them
bwrap_works() {
bwrap --ro-bind / / --unshare-user --unshare-net true 2> /dev/null
}
# --- Claude Code -------------------------------------------------------------------
# Claude Code runs every command it starts in its own sandbox, also built on
# bubblewrap and socat; it can't run inside srt because bubblewrap can't be
# nested. Anything already in the sandbox section of your settings wins over
# these defaults.
claude_sandbox_on() {
jq -e '.sandbox.enabled == true' "$CLAUDE_SETTINGS" &> /dev/null
}
turn_on_claude_sandbox() {
local domains
domains="$(jq '.network.allowedDomains' "$BASE_CONFIG")" || return 1
edit_json "$CLAUDE_SETTINGS" '{}' --argjson domains "$domains" \
'.sandbox = ({enabled: true, network: {allowedDomains: $domains}} * (.sandbox // {}))' || return 1
echo "Claude Code runs the commands it starts in its own sandbox."
}
# A desktop notification when Claude Code needs you or is done, through
# system/claude-notify so that clicking one brings back the app Claude runs in.
# The hook runs in a shell, which expands ~.
# shellcheck disable=SC2088
CLAUDE_NOTIFY="~/.local/share/federated-connector/connector/system/claude-notify"
OLD_NOTIFY_INPUT="notify-send 'Claude Code' 'Needs your input'"
OLD_NOTIFY_DONE="notify-send 'Claude Code' 'Task completed'"
# Settings written before claude-notify ran notify-send itself, so clicking
# did nothing: true while either of those two hooks is still there
old_claude_notifications() {
jq -e --arg input "$OLD_NOTIFY_INPUT" --arg completed "$OLD_NOTIFY_DONE" \
'[.hooks.Notification[]?.hooks[]?, .hooks.Stop[]?.hooks[]? | objects | .command]
| any(. == $input or . == $completed)' "$CLAUDE_SETTINGS" &> /dev/null
}
# Your own settings are left alone: the hooks are added only if you have no
# settings yet, and changed only where they're still the ones written here
claude_notifications() {
if [ ! -f "$CLAUDE_SETTINGS" ]; then
edit_json "$CLAUDE_SETTINGS" '{}' \
--arg needs "$CLAUDE_NOTIFY 'Needs your input'" \
--arg finished "$CLAUDE_NOTIFY 'Task completed'" \
'.hooks = {
Notification: [{matcher: "", hooks: [{type: "command", command: $needs}]}],
Stop: [{matcher: "", hooks: [{type: "command", command: $finished}]}]
}' || return 1
echo "Claude Code shows a notification when it needs you and when it's done."
elif old_claude_notifications; then
edit_json "$CLAUDE_SETTINGS" '{}' \
--arg old_input "$OLD_NOTIFY_INPUT" --arg new_input "$CLAUDE_NOTIFY 'Needs your input'" \
--arg old_done "$OLD_NOTIFY_DONE" --arg new_done "$CLAUDE_NOTIFY 'Task completed'" '
def swap(old; new):
if type == "object" and .command == old then .command = new else . end;
(.hooks.Notification[]?.hooks[]?) |= swap($old_input; $new_input)
| (.hooks.Stop[]?.hooks[]?) |= swap($old_done; $new_done)' || return 1
echo "Clicking Claude Code's notifications brings back the app it runs in."
fi
}
# --- Hermes' gateway and Cline Desktop -----------------------------------------------
# Hermes' gateway acts on messages from outside this machine, so it runs in
# the sandbox too: a systemd drop-in starts it through sandbox/bin/hermes. It
# also needs to reach the Matrix server in Hermes' settings, which differs on
# each machine. Hermes can change those settings itself, so the server isn't
# allowed from them; configure hermes asks you (see sandbox/hermes-homeserver.sh).
# For other messaging services, add their domains to
# ~/.config/federated-connector/sandbox/hermes.json.
source "$CONNECTOR/sandbox/hermes-homeserver.sh"
gateway_dropin() {
cat << EOF
# Written by Federated Connector (configure agents): run Hermes' gateway in the sandbox
[Service]
ExecStart=
ExecStart=$SANDBOX_BIN/hermes gateway run
EOF
}
gateway_ok() {
[ ! -f "$HERMES_GATEWAY_UNIT" ] || [ "$(cat "$HERMES_GATEWAY_DROPIN" 2> /dev/null)" = "$(gateway_dropin)" ]
}
sandbox_gateway() {
local homeserver
if ! gateway_ok; then
hermes_carry_over_homeserver
mkdir -p "$(dirname "$HERMES_GATEWAY_DROPIN")" && gateway_dropin > "$HERMES_GATEWAY_DROPIN" \
&& systemctl --user daemon-reload || return 1
echo "Hermes' gateway runs in the sandbox (from its next start)."
fi
[ -f "$HERMES_GATEWAY_UNIT" ] || return 0
homeserver="$(hermes_homeserver)"
if [ -n "$homeserver" ] && ! hermes_homeserver_allowed "$homeserver"; then
echo "Hermes' settings name the Matrix server $homeserver, which its gateway can't reach until you allow it: configure hermes"
fi
}
# Cline Desktop is opened from the app menu, so an entry of the same name in
# ~/.local/share/applications, which wins over the package's, runs it in the
# sandbox. It's removed when the package is.
CLINE_DESKTOP="/usr/share/applications/cline-desktop.desktop"
CLINE_ENTRY="$HOME/.local/share/applications/cline-desktop.desktop"
cline_entry() {
sed "s#^Exec=.*#Exec=$SANDBOX_BIN/cline-desktop %U#" "$CLINE_DESKTOP"
}
cline_ok() {
if [ -f "$CLINE_DESKTOP" ]; then
[ "$(cat "$CLINE_ENTRY" 2> /dev/null)" = "$(cline_entry)" ]
else
[ ! -f "$CLINE_ENTRY" ]
fi
}
sandbox_cline() {
cline_ok && return 0
if [ -f "$CLINE_DESKTOP" ]; then
mkdir -p "$(dirname "$CLINE_ENTRY")" && cline_entry > "$CLINE_ENTRY" || return 1
echo "Cline Desktop runs in the sandbox."
else
rm -f "$CLINE_ENTRY"
fi
}
# --- The item -------------------------------------------------------------------------
sandbox_ready() {
srt_current && on_path && pkg_installed "${SANDBOX_PACKAGES[@]}" && bwrap_works \
&& { ! agent_installed claude || { claude_sandbox_on && ! old_claude_notifications; }; } \
&& gateway_ok && cline_ok
}
is_configured() {
[ -f "$AGENTS_CONF" ] && sandbox_ready
}
# Download the installer, then run it, rather than piping it into bash
install_agent() {
local title="$1" url="$2" args="$3" tmp status
tmp="$(mktemp -d)" || return 1
echo "Downloading $title's installer from $url"
if curl -fsSL --max-time 120 -o "$tmp/install.sh" "$url"; then
# shellcheck disable=SC2086
(cd "$tmp" && bash ./install.sh $args)
status=$?
else
echo "Couldn't download it."
status=1
fi
rm -rf "$tmp"
return $status
}
run_configure() {
local entry name title url args status=0
mkdir -p "$CONF_DIR" && touch "$AGENTS_CONF" || return 1
echo "AI coding agents, run in a sandbox: an agent can change files only in the folder"
echo "you start it in, can't read your keys, passwords, browsers, mail or messages, and"
echo "reaches only AI providers and developer sites."
echo
for entry in "${AGENTS[@]}"; do
IFS='|' read -r name title url args <<< "$entry"
if agent_installed "$name"; then
echo " $title: installed"
elif confirm_yes "Install $title?"; then
if ! install_agent "$title" "$url" "$args" || ! agent_installed "$name"; then
echo -e "\e[31m$title was not installed.\e[0m"
status=1
fi
fi
done
echo
pkg_ensure "The sandbox" "${SANDBOX_PACKAGES[@]}" || return 1
if ! bwrap_works; then
echo -e "\e[31mbubblewrap can't make user namespaces on this computer, so the sandbox can't run.\e[0m"
return 1
fi
srt_ensure || { echo -e "\e[31mWithout srt, opencode and Hermes can't run in the sandbox.\e[0m"; return 1; }
if ! on_path; then
echo "Typing opencode or hermes should run it in the sandbox. That takes a line at the"
echo "end of ~/.bashrc."
if confirm_yes "Add it?"; then
put_on_path || status=1
else
echo "Then run them sandboxed as $SANDBOX_BIN/<agent>."
fi
fi
if agent_installed claude; then
claude_notifications || status=1
claude_sandbox_on || turn_on_claude_sandbox || status=1
fi
sandbox_gateway || status=1
sandbox_cline || status=1
echo
echo "To sign in to them: configure claude, configure opencode, configure hermes"
return $status
}