Amp orbs
Use Orble boxes to host Amp orbs. Guest memory and process state are preserved when an orb is paused, and Amp project snapshots are stored as Orble snapshots.
Create a provider token in Amp under Settings → Orbs → Add Provider, and an Orble API key in Settings. Run dispatch.sh on an always-on machine with Bash, curl and jq installed. Run ./dispatch.sh prepare once to create the amp-orb snapshot used for new orbs.
$ curl -fsSLO https://orble.tech/amp/dispatch.sh && chmod +x dispatch.sh$ export ORBLE_API_KEY=orble_...$ ./dispatch.sh prepare$ export AMP_ORB_PROVIDER_TOKEN=aop_live_...$ amp orb provider "$PWD/dispatch.sh"
#!/usr/bin/env bash
# Amp orbs on Orble (https://orble.tech/docs?tab=amp). Needs bash, curl, jq and ORBLE_API_KEY.
# ./dispatch.sh prepare once: saves the amp-orb snapshot new orbs start from
# amp orb provider "$PWD/dispatch.sh" then: Amp runs it for each spawn, pause and delete
# Each orb is a box named amp-<hash of ORB_ID>, kept awake while Amp uses it. A pause suspends it with its memory,
# so the orb's programs keep running when it wakes, and saves Amp's project snapshots as Orble snapshots.
set -euo pipefail
API="${ORBLE_API_URL:-https://orble.tech}"
BASE="${ORBLE_AMP_SNAPSHOT:-amp-orb}"
KEEP="${ORBLE_AMP_KEEP_SNAPSHOTS:-3}" # project snapshots to keep; Amp never deletes them
OP="${1:-${ORB_OPERATION:-}}"
hash() { printf %s "$1" | sha256sum | cut -c1-12; }
BOX="amp-$(hash "${ORB_ID:-prepare}")"
[ "$OP" = prepare ] && BOX="amp-orb-$(hash "$$$(date +%s%N)" | cut -c1-6)"
SNAP=""
[ -n "${ORB_SNAPSHOT_ID:-}" ] && SNAP="amps-$(hash "$ORB_SNAPSHOT_ID")"
# api METHOD PATH [JSON]: Orble's answer on stdout; fails on an error, or on a 404 unless ALLOW404 is set.
api() {
local out code
out=$(curl -sS -X "$1" "$API$2" -H "Authorization: Bearer $ORBLE_API_KEY" -H 'Content-Type: application/json' \
${3:+--data "$3"} -w '\n%{http_code}')
code=${out##*$'\n'} out=${out%$'\n'*}
if [ "$code" -ge 300 ] && ! { [ "$code" = 404 ] && [ -n "${ALLOW404:-}" ]; }; then
echo "orble: $1 $2: $code $out" >&2
return 1
fi
printf '%s' "$out"
}
has_box() { [ -n "$(ALLOW404=1 api GET "/v1/boxes/$BOX" | jq -r '.name // empty')" ]; }
has_snap() { api GET /v1/snapshots | jq -e --arg n "$1" 'any(.[]; .name == $n or .alias == $n)' >/dev/null; }
# run SCRIPT: runs a script in the box as root, with ORB_TOKEN and AMP_URL set, and prints its output.
run() {
local r
r=$(api POST "/v1/boxes/$BOX/exec" "$(jq -n --arg c "echo $(printf %s "$1" | base64 -w0) | base64 -d | sudo -E bash" \
--arg t "${ORB_TOKEN:-}" --arg u "${AMP_URL:-}" \
'{command: $c, timeout_s: 240, env: ({ORB_TOKEN: $t, AMP_URL: $u} | with_entries(select(.value != "")))}')")
jq -r '.stdout, .stderr | select(. != null and . != "")' <<<"$r" >&2
[ "$(jq -r '.exit_code // 0' <<<"$r")" = 0 ]
}
# What an orb needs (ampcode.com/docs/orbs/self-hosted): a "user" account with sudo and its own home, amp at
# /usr/local/bin/amp, and Node. Orble's image answers any unknown user name as its own user, agent, so useradd would
# refuse; the entries are written directly.
SETUP='
if ! grep -q "^user:" /etc/passwd; then
! mountpoint -q /home/user || umount /home/user
echo "user:x:1001:1001::/home/user:/bin/bash" >>/etc/passwd
echo "user:x:1001:" >>/etc/group
echo "user:!:20000:0:99999:7:::" >>/etc/shadow
usermod -aG docker user
mkdir -p /home/user && cp -an /etc/skel/. /home/user/ && chown -R user:user /home/user
echo "user ALL=(ALL) NOPASSWD:ALL" >/etc/sudoers.d/amp-user && chmod 440 /etc/sudoers.d/amp-user
fi
if [ ! -x /usr/local/bin/amp ]; then
curl -fsSL https://ampcode.com/install.sh | HOME=/root bash >/dev/null 2>&1
install -m 755 /root/.amp/bin/amp /usr/local/bin/amp
fi
for t in node npm npx; do
[ -e /usr/local/bin/$t ] || ln -s "$(sudo -iu agent mise which $t)" /usr/local/bin/$t
done
chmod o+rx /home/agent /home/agent/.local /home/agent/.local/share /home/agent/.local/share/mise
'
case "$OP" in
prepare) # a box with SETUP done, saved as the snapshot new orbs start from
api POST /v1/boxes "$(jq -n --arg n "$BOX" '{name: $n}')" >/dev/null
trap 'ALLOW404=1 api DELETE "/v1/boxes/$BOX" >/dev/null' EXIT
run "set -e; $SETUP"
ALLOW404=1 api DELETE "/v1/snapshots/$BASE" >/dev/null || true
api POST "/v1/boxes/$BOX/snapshot" "$(jq -n --arg n "$BASE" '{name: $n}')" >/dev/null
echo "saved $BASE"
;;
spawn)
fresh=""
if ! has_box; then
from=""
if [ -n "$SNAP" ] && has_snap "$SNAP"; then
from=$SNAP
elif has_snap "$BASE"; then
from=$BASE
fi
size=$(case "${ORB_TYPE:-}" in small) echo s ;; medium) echo m ;; large) echo l ;; esac)
api POST /v1/boxes "$(jq -n --arg n "$BOX" --arg f "$from" --arg s "$size" \
'{name: $n} + (if $f != "" then {from: $f} else {} end) + (if $s != "" then {size: $s} else {} end)')" >/dev/null
echo "$ORB_ID: made $BOX${from:+ from $from}" >&2
fresh=1
fi
api PATCH "/v1/boxes/$BOX" '{"keep_awake": true}' >/dev/null # wakes a suspended box too
# A box from a project snapshot carries the processes of the orb it was saved from, and a woken box its last
# bootstrap, which would compete with the new one: stop those, and leave the woken box's other programs running.
run "set -e
if [ -n '$fresh' ] && grep -q '^user:' /etc/passwd; then pkill -KILL -u user || true
else pkill -f '^amp orb bootstrap' || true; fi
$SETUP
cd /home/user
sudo -u user -H --preserve-env=ORB_TOKEN,AMP_URL setsid bash -lc 'exec amp orb bootstrap' \
</dev/null >>/home/user/.amp-bootstrap.log 2>&1 &
echo started amp orb bootstrap"
;;
pause)
has_box || exit 0
api PATCH "/v1/boxes/$BOX" '{"keep_awake": false}' >/dev/null
api POST "/v1/boxes/$BOX/suspend" >/dev/null
if [ -n "$SNAP" ] && ! has_snap "$SNAP"; then
api POST "/v1/boxes/$BOX/snapshot" "$(jq -n --arg n "$SNAP" '{name: $n}')" >/dev/null
echo "$ORB_ID: saved project snapshot $SNAP" >&2
api GET /v1/snapshots | jq -r --argjson k "$KEEP" \
'[.[] | select(.name | startswith("amps-"))] | sort_by(.created_at) | reverse | .[$k:][] | .name' |
while read -r old; do
ALLOW404=1 api DELETE "/v1/snapshots/$old" >/dev/null && echo "$ORB_ID: deleted snapshot $old" >&2
done
fi
;;
delete)
ALLOW404=1 api DELETE "/v1/boxes/$BOX" >/dev/null
;;
*)
echo "usage: $0 prepare, or run by amp orb provider" >&2
exit 2
;;
esac
SpawnCreate the thread's box from its project snapshot, or from
amp-orbwhen no project snapshot exists. Automatic suspension is disabled while the orb is activePauseSuspend the box with memory preserved and save a project snapshot when requested by Amp. The three most recent snapshots are retained by default; configure retention with
ORBLE_AMP_KEEP_SNAPSHOTSDeleteDelete the box when the associated Amp thread is deleted
Orb typesUse
small,mediumorlargefor S, M or L boxes. If no type is specified, use the size of theamp-orbsnapshotUnsupportedAmp's Desktop tab