#!/usr/bin/env bash # build-snap.sh [VERSION]: the elpis-agent snap (classic confinement): the desktop app with Electron, the engine, its # provider bridge (tools/elpis-claude) and a terminal launcher. Packed with `snap pack` from a prepared folder, so no # build VM is needed. Put GitHub's build of this commit in out/ first: `node scripts/actions-build.mts` in apps/elpis-desktop. # ENGINE_DIR a folder with codex, codex-code-mode-host and bwrap (a CI build); default: the installed engine. # ELECTRON_DIST an Electron folder; default: the checkout's, else the installed app's (no download). # ELPIS_STAGE_DIR only lay the package out in this new folder and stop before `snap pack` (the AppImage is made # from it: scripts/build-appimage.sh, run by .github/workflows/desktop-packages.yml). # Not run on Masih's laptop: GitHub builds the desktop packages (docs/LOCAL_BUILD_RULES.md). set -euo pipefail VERSION=${1:-0.5.3} R=$(cd "$(dirname "$0")/.." && pwd -P); APP=$R/apps/elpis-desktop commit=$(git -C "$R" rev-parse HEAD) built=$(cat "$APP/out/.commit" 2>/dev/null || true) [[ "$built" == "$commit" ]] || { [[ -n "$built" ]] && git -C "$R" diff --quiet "$built" "$commit" -- apps/elpis-desktop ':!apps/elpis-desktop/scripts'; } \ || { echo "apps/elpis-desktop/out is not built from $commit: put GitHub's build there, (cd apps/elpis-desktop && node scripts/actions-build.mts)" >&2; exit 1; } if [[ -n "${ENGINE_DIR:-}" ]]; then codex=$ENGINE_DIR/codex; host=$ENGINE_DIR/codex-code-mode-host; bwrap=$ENGINE_DIR/bwrap else installed=$(dirname "$(readlink -f ~/.local/bin/elpis-next)"); codex=$installed/codex; host=$installed/codex-code-mode-host; bwrap=$installed/../codex-resources/bwrap; fi for f in "$codex" "$host" "$bwrap"; do [[ -x "$f" ]] || { echo "missing engine file: $f" >&2; exit 1; }; done bridge=$R/tools/elpis-claude [[ -d "$bridge/node_modules/@agentclientprotocol/claude-agent-acp" ]] || { echo "run npm ci --omit=dev in tools/elpis-claude" >&2; exit 1; } mkdir -p "$HOME/.cache/elpis-tests"; STAGE=$(mktemp -d "$HOME/.cache/elpis-tests/elpis-snap.XXXXXX"); S=${ELPIS_STAGE_DIR:-$STAGE/prime} [[ -z "${ELPIS_STAGE_DIR:-}" || ! -e "$S" ]] || { echo "ELPIS_STAGE_DIR must not exist yet: $S" >&2; exit 1; } OUT=${ELPIS_SNAP_DIR:-$HOME/.cache/elpis-tests/snap}; mkdir -p "$OUT" # The desktop app and Electron, the renderer with this checkout's path replaced (as build-deb.sh does). electron=${ELECTRON_DIST:-$APP/node_modules/electron/dist} [[ -x "$electron/electron" ]] || electron=$(dirname "$(readlink -f "$(dirname "$(readlink -f ~/.local/bin/elpis-app)")/runtime/electron/electron")") cp -r "$APP/out" "$STAGE/out" real_app=$(dirname "$(readlink -f "$APP/node_modules")") (rg -0 -l -F -e "$APP" -e "$real_app" "$STAGE/out" || true) | xargs -0 -r sed -i -e "s|$APP|/snap/elpis-agent/current/desktop/app|g" -e "s|$real_app|/snap/elpis-agent/current/desktop/app|g" mkdir -p "$S" node "$APP/desktop/package.mts" --source "$APP" --destination "$S/desktop" --electron-dist "$electron" --out-directory "$STAGE/out" > "$STAGE/manifest.json" find "$S/desktop/runtime/electron/locales" -name '*.pak' ! -name 'en-US.pak' -delete # The engine, laid out as an installed bundle: runtimeTools() finds tools/elpis-claude beside package/. mkdir -p "$S/engine/package/bin" "$S/engine/package/codex-resources" "$S/engine/tools" install -m 0755 "$codex" "$S/engine/package/bin/codex" install -m 0755 "$host" "$S/engine/package/bin/codex-code-mode-host" install -m 0755 "$bwrap" "$S/engine/package/codex-resources/bwrap" ln -s package/bin/codex "$S/engine/elpis-next" rsync -aL --exclude 'claude-agent-sdk-linux-*' --exclude test --exclude spike --exclude '*.orig' --exclude '*.test.*' "$bridge/" "$S/engine/tools/elpis-claude/" mkdir -p "$S/engine/tools/elpis-claude/matilda" cp "$R/apps/elpis-desktop/bridge/matilda-goals.mts" "$S/engine/tools/elpis-claude/matilda/" # matilda-sessions.mts only re-exports the router's discovery module, so the module itself goes in its place (as build-deb.sh does). cp "$R/apps/elpis-desktop/bridge/router/executor/discovery.mts" "$S/engine/tools/elpis-claude/matilda/matilda-sessions.mts" # Launchers. Node is Electron's own (ELECTRON_RUN_AS_NODE), so the host needs no Node.js. mkdir -p "$S/bin" "$S/meta/gui" # The same launchers serve the snap ($SNAP) and the AppImage ($APPDIR; scripts/build-appimage.sh). cat > "$S/bin/node" <<'NODE' #!/bin/sh ELECTRON_RUN_AS_NODE=1 exec "${SNAP:-$APPDIR}/desktop/runtime/electron/electron" "$@" NODE cat > "$S/bin/elpis-app" <<'APPL' #!/bin/sh R=${SNAP:-$APPDIR} # ELPIS_TUI_BIN: the chat menu's Open in > Elpis TUI runs this package's terminal app. export ELPIS_ENGINE_BIN="$R/engine/elpis-next" ELPIS_NODE_BIN="$R/bin/node" ELPIS_TUI_BIN="$R/bin/elpis" PATH="$R/bin:$PATH" # A snap or AppImage cannot ship Chromium's setuid sandbox helper, and Ubuntu 24.04 blocks the user-namespace sandbox for apps # without an AppArmor profile; only there does the window run without Chromium's sandbox (it would abort otherwise). [ "$(cat /proc/sys/kernel/apparmor_restrict_unprivileged_userns 2>/dev/null)" = 1 ] && set -- --no-sandbox "$@" exec "$R/desktop/launch" "$@" APPL cat > "$S/bin/elpis" <<'TUI' #!/usr/bin/env bash # The terminal app: the provider bridge first (shared with the desktop app), then the TUI on it. R=${SNAP:-$APPDIR}; REAL=$R/engine/elpis-next; BRIDGE_DIR=$R/engine/tools/elpis-claude; NODE=$R/bin/node export ELPIS_DESKTOP_APP="${ELPIS_DESKTOP_APP:-$R/bin/elpis-app}" # /app opens this package's desktop app for a in "$@"; do case "$a" in -h|--help|-V|--version) exec "$REAL" "$@" ;; esac; done case "${1:-}" in ""|-*|resume|fork) ;; *) exec "$REAL" "$@" ;; esac out="$(ELPIS_ENGINE_BIN="$REAL" NODE_USE_ENV_PROXY=1 "$NODE" "$BRIDGE_DIR/shared-runtime.mts" "$BRIDGE_DIR/acp-bridge.mts")" || exit $? engine="$REAL" case "$out" in *$'\n'"engine "*) engine="${out#*$'\n'engine }"; out="${out%%$'\n'*}" ;; *$'\n'*) echo 'elpis: unexpected bridge reply' >&2; exit 1 ;; esac case "$out" in unix:///*) ;; *) echo 'elpis: unexpected bridge endpoint' >&2; exit 1 ;; esac ELPIS_ENGINE_BIN="$engine" exec "$REAL" --elpis-local-bridge --remote "$out" "$@" TUI cat > "$S/bin/love-spot" <<'LOVEAPPL' #!/bin/sh R=${SNAP:-$APPDIR} exec "$R/bin/elpis-app" --love-spot-app "$@" LOVEAPPL cat > "$S/bin/elpis-workflows" <<'WFAPPL' #!/bin/sh R=${SNAP:-$APPDIR} exec "$R/bin/elpis-app" --workflows-app "$@" WFAPPL chmod 0755 "$S/bin/"* cp "$APP/desktop/elpis-icon.png" "$S/meta/gui/icon.png" cp "$APP/desktop/love-spot-icon.png" "$S/meta/gui/love-spot.png" cp "$APP/desktop/love-spot-icon.svg" "$S/meta/gui/love-spot.svg" cat > "$S/meta/gui/elpis-agent.desktop" <<'ENTRY' [Desktop Entry] Type=Application Name=Elpis Comment=Coding agents you can see and check Exec=elpis-agent %U Icon=${SNAP}/meta/gui/icon.png Terminal=false Categories=Development; StartupWMClass=elpis-desktop ENTRY cat > "$S/meta/gui/love-spot.desktop" <<'LOVEENTRY' [Desktop Entry] Type=Application Name=Love Spot GenericName=Excalidraw Comment=Shared drawing board with Matilda and multi-provider AI accounts Exec=elpis-agent.love-spot %U Icon=${SNAP}/meta/gui/love-spot.svg Terminal=false Categories=Graphics;Development; StartupWMClass=excalidraw LOVEENTRY cat > "$S/meta/gui/workflows.desktop" <<'WFENTRY' [Desktop Entry] Type=Application Name=Elpis Workflows GenericName=Multiagent Control Center Comment=Design and run multi-agent teams across GPT, Claude and Gemini Exec=elpis-agent.workflows %U Icon=${SNAP}/meta/gui/icon.png Terminal=false Categories=Development; StartupWMClass=elpis-workflows WFENTRY cat > "$S/meta/snap.yaml" <&2; exit 1; } # Licence notices for everything the snap carries or bundles. node "$APP/scripts/third-party-notices.mts" "$S" "$S/THIRD-PARTY-NOTICES.txt"; cp "$R/LICENSE" "$S/LICENSE" # No personal data: nothing may name the builder's home folder or git email. (On GitHub the home is the runner's, # /home/runner, which the engine's build paths name; it is no one's.) email=$(git -C "$R" config user.email || true) if [[ "${GITHUB_ACTIONS:-}" != true ]] && rg -l -F -e "$HOME/" ${email:+-e "$email"} "$S" || find "$S" -type l -lname "$HOME/*" | rg .; then echo "The snap names the builder's home or email; not built." >&2; exit 1; fi if [[ -n "${ELPIS_STAGE_DIR:-}" ]]; then gio trash "$STAGE" 2>/dev/null || rm -rf "$STAGE"; echo "$S staged from ${commit:0:9}"; exit 0; fi snap pack --compression=xz "$S" "$OUT" >/dev/null f="$OUT/elpis-agent_${VERSION}_amd64.snap" (cd "$OUT" && sha256sum "$(basename "$f")" > "$(basename "$f").sha256") gio trash "$STAGE" echo "$f $(du -h "$f" | cut -f1) from ${commit:0:9}"