2025-12-19 22:13:13 +01:00
---
summary: "Bundled bun gateway: packaging, launchd, signing, and bytecode"
read_when:
- Packaging Clawdis.app
- Debugging the bundled gateway binary
- Changing bun build flags or codesigning
---
# Bundled bun Gateway (macOS)
2025-12-20 19:35:33 +00:00
Goal: ship **Clawdis.app** with a self-contained relay binary that can run both the CLI and the Gateway daemon. No global `npm install -g clawdis` , no system Node requirement.
2025-12-19 22:13:13 +01:00
## What gets bundled
App bundle layout:
2025-12-20 02:08:04 +00:00
- `Clawdis.app/Contents/Resources/Relay/clawdis`
2025-12-20 19:35:33 +00:00
- bun `--compile` relay executable built from `dist/macos/relay.js`
- Supports:
- `clawdis …` (CLI)
- `clawdis gateway-daemon …` (LaunchAgent daemon)
2025-12-19 22:13:13 +01:00
- `Clawdis.app/Contents/Resources/Relay/package.json`
2025-12-22 21:02:48 +00:00
- tiny “p runtime compatibility” file (see below)
2025-12-19 22:13:13 +01:00
- `Clawdis.app/Contents/Resources/Relay/theme/`
2025-12-22 21:02:48 +00:00
- p TUI theme payload (optional, but strongly recommended)
2025-12-19 22:13:13 +01:00
Why the sidecar files matter:
2025-12-22 21:02:48 +00:00
- The embedded p runtime detects “bun binary mode” and then looks for `package.json` + `theme/` **next to `process.execPath`** (i.e. next to `clawdis` ).
- So even if bun can embed assets, the runtime expects filesystem paths. Keep the sidecar files.
2025-12-19 22:13:13 +01:00
## Build pipeline
Packaging script:
- `scripts/package-mac-app.sh`
It builds:
- TS: `pnpm exec tsc`
- Swift app + helper: `swift build …`
2025-12-20 19:35:33 +00:00
- bun relay: `bun build dist/macos/relay.js --compile --bytecode …`
2025-12-19 22:13:13 +01:00
Important bundler flags:
- `--compile` : produces a standalone executable
- `--bytecode` : reduces startup time / parsing overhead (works here)
- externals:
2025-12-20 19:35:33 +00:00
- `-e electron`
- Reason: avoid bundling Electron stubs in the relay binary
2025-12-19 22:13:13 +01:00
Version injection:
- `--define "__CLAWDIS_VERSION__=\"<pkg version>\""`
- `src/version.ts` also supports `__CLAWDIS_VERSION__` (and `CLAWDIS_BUNDLED_VERSION` ) so `--version` doesn’ t depend on reading `package.json` at runtime.
## Launchd (Gateway as LaunchAgent)
Label:
- `com.steipete.clawdis.gateway`
Plist location (per-user):
- `~/Library/LaunchAgents/com.steipete.clawdis.gateway.plist`
Manager:
- `apps/macos/Sources/Clawdis/GatewayLaunchAgentManager.swift`
Behavior:
- “Clawdis Active” enables/disables the LaunchAgent.
- App quit does **not** stop the gateway (launchd keeps it alive).
Logging:
- launchd stdout/err: `/tmp/clawdis/clawdis-gateway.log`
Default LaunchAgent env:
- `CLAWDIS_IMAGE_BACKEND=sips` (avoid sharp native addon under bun)
## Codesigning (hardened runtime + bun)
Symptom (when mis-signed):
- `Ran out of executable memory …` on launch
Fix:
- The bun executable needs JIT-ish permissions under hardened runtime.
2025-12-20 19:35:33 +00:00
- `scripts/codesign-mac-app.sh` signs `Relay/clawdis` with:
2025-12-19 22:13:13 +01:00
- `com.apple.security.cs.allow-jit`
- `com.apple.security.cs.allow-unsigned-executable-memory`
## Image processing under bun
Problem:
- bun can’ t load some native Node addons like `sharp` (and we don’ t want to ship native addon trees for the gateway).
Solution:
- Central helper `src/media/image-ops.ts`
- Prefers `/usr/bin/sips` on macOS (esp. when running under bun)
- Falls back to `sharp` when available (Node/dev)
- Used by:
- `src/web/media.ts` (optimize inbound/outbound images)
- `src/browser/screenshot.ts`
- `src/agents/pi-tools.ts` (image sanitization)
2025-12-20 19:35:33 +00:00
## Browser control server
2025-12-19 22:13:13 +01:00
2025-12-20 19:35:33 +00:00
The Gateway starts the browser control server (loopback only) from `src/gateway/server.ts` .
It’ s started from the relay daemon process, so the relay binary includes Playwright deps.
2025-12-19 22:13:13 +01:00
## Tests / smoke checks
From a packaged app (local build):
```bash
2025-12-20 19:35:33 +00:00
dist/Clawdis.app/Contents/Resources/Relay/clawdis --version
2025-12-19 22:13:13 +01:00
CLAWDIS_SKIP_PROVIDERS=1 \
CLAWDIS_SKIP_CANVAS_HOST=1 \
2025-12-20 19:35:33 +00:00
dist/Clawdis.app/Contents/Resources/Relay/clawdis gateway-daemon --port 18999 --bind loopback
2025-12-19 22:13:13 +01:00
```
Then, in another shell:
```bash
pnpm -s clawdis gateway call health --url ws://127.0.0.1:18999 --timeout 3000
```
## Repo hygiene
Bun may leave dotfiles like `*.bun-build` in the repo root or subfolders.
- These are ignored via `.gitignore` (`*.bun-build` ).
2025-12-19 22:39:30 +01:00
## DMG styling (human installer)
`scripts/create-dmg.sh` styles the DMG via Finder AppleScript.
Rules of thumb:
- Use a **72dpi** background image that matches the Finder window size in points.
- Preferred asset: `assets/dmg-background-small.png` (**500× 320**).
- Default icon positions: app `{125,160}` , Applications `{375,160}` .