Skip to content

perf(desktop): slim bundles and fetch postgres for local mode - #23

Merged
nixuuu merged 3 commits into
devfrom
perf/desktop-bundle-size
Nov 3, 2026
Merged

nixuuu merged 3 commits into
devfrom
perf/desktop-bundle-size

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 3, 2026

Copy link
Copy Markdown
Member

Shrinks the desktop app (macOS 0.2.0-nightly.5 measured 1.8 GiB on disk) and follows the product decision that agent CLIs are never bundled.

  • One Bun runtime + four JS bundles (controller, gateway, web, worker) instead of five compiled binaries each embedding Bun.
  • No app node_modules in app.asar/app.asar.unpacked; runtime node_modules only for what the bundles load at runtime.
  • No agent CLI in the app: the Claude Agent SDK platform binary (297 MB) is gone; the worker always passes pathToClaudeCodeExecutable to the installed claude (codex/grok were already from PATH); a missing CLI is reported by the provider probe.
  • PostgreSQL is not shipped: it is downloaded only when local mode is chosen (pinned version, SHA-512 verified, atomic install, resumable, offline once cached, progress and clear errors in desktop setup); remote mode downloads nothing; a pinned major change refuses to start before touching data.
  • Package guards: size budget, no node_modules in asar, no universal Mach-O in arm64 builds, no bundled agent CLI.

Linux AppImage unpacked: 1468 → 418 MiB (AppImage 586 → 184 MiB). macOS estimated 1.8 GiB → ~400 MiB (+ ~70–110 MiB Postgres cache only in local mode).
Tests: check:tmp 687 pass / 0 fail; desktop + guards 15 pass (real Postgres start from the downloaded layout, hash mismatch, interrupted download, offline, remote without download, durability, shutdown).

Draft until verified on macOS: Developer ID signing/notarization of the single Bun, Postgres download under quarantine (ad-hoc signing, ICU symlinks), local mode start, installed Claude/Codex/Grok turns (checklist in docs/release.md).

@nixuuu
nixuuu force-pushed the perf/desktop-bundle-size branch from 46f5e15 to cd72d9a Compare November 3, 2026 17:11
@nixuuu
nixuuu marked this pull request as ready for review November 3, 2026 17:11
@nixuuu
nixuuu merged commit d5e3046 into dev Nov 3, 2026
3 checks passed
@nixuuu
nixuuu deleted the perf/desktop-bundle-size branch November 3, 2026 17:16
nixuuu added a commit that referenced this pull request Nov 3, 2026
Since #23 each desktop artifact also carries
`package-sizes-<target>.json` (uploaded for the job log).
`publish-desktop` rejected it as "Unexpected desktop asset", so the
nightlies after #23/#28/#30 published server artifacts but no desktop
installers. The merge step now skips the size reports; the
update-metadata test covers it.
nixuuu added a commit that referenced this pull request Nov 6, 2026
macOS checks each newly written executable on its first launch. A cold
`postgres --version` took 3-3.6 s on an idle Mac; on a freshly installed,
busy Mac it went past the old 10 s limit per binary and failed with a
generic error. Each retry extracts new files, so it pays the cost again.

- Run the three `--version` checks in one hard 3 min budget, with a
  "Checking PostgreSQL with macOS security…" status after 5 s.
- SIGKILL on timeout. Waiting for the exit and the output is bounded too,
  so a retry cannot hang.
- Name the cause (timeout, blocked by the OS, failed run, wrong version)
  and log the binary, exit code or signal, the timeout, the timing and
  trimmed stdout/stderr.
- Remove com.apple.quarantine from the verified, extracted runtime. A
  quarantined binary waited in syspolicyd until killed.
- Drop Electron's IPC prefix from setup errors.

Fixes: #23
nixuuu added a commit that referenced this pull request Nov 6, 2026
## Evidence

A colleague ran nightly `0.2.0-nightly.92` on a fresh MacBook Pro (M5
Pro, macOS 27.0.1). The first local setup failed twice with `Downloaded
PostgreSQL could not run on this computer`. The desktop log had only
that generic line. "Try again" sometimes looked stuck on "Setting up
PostgreSQL…". The 7th attempt worked, and `initdb`, postgres,
migrations, gateway and worker then started normally.

Measured on a Mac Studio, macOS 27.0.1, idle, using the installer's own
steps in a temp dir (real npm download, SHA-512, extract,
`preparePostgres`, `--version`):

| Run | `postgres` | `pg_ctl` | `initdb` |
| --- | --- | --- | --- |
| cold, fresh files (3 runs) | 3010–3608 ms | 146–162 ms | 309–431 ms |
| second launch, same files | 20–24 ms | 3–5 ms | 12–14 ms |
| fixed installer end to end, same pin already installed by Canopy
elsewhere | 3376 ms | 146 ms | 374 ms |
| fresh files with `com.apple.quarantine` | hung until killed at 180 s
(SIGTERM, exit 143); syspolicyd logged `Unable to initialize qtn_proc`
every 2 s | | |
| quarantine set, then removed with `xattr -dr` | 3010 ms | 151 ms | 309
ms |

Files extracted by Canopy carry only `com.apple.provenance`, not
quarantine. `preparePostgres` (thin plus ad-hoc signing of 113 Mach-O
files) takes 4.4–5 s.

## Root cause

macOS checks each newly written executable on its first launch, and the
slow part is `postgres`, which loads ICU, OpenSSL and libxml2. The cost
depends on the files, not their content: every retry extracts a new
runtime and pays it again. The smoke check gave each binary a fixed 10 s
`AbortSignal.timeout`. On a freshly installed, busy Mac that first
launch went past 10 s. Any timeout or kill turned into the same generic
error, with no detail in the log. Later attempts passed once the Mac was
less busy, not because of a cache.

## Fix

- `postgres-check.ts`: the three `--version` checks share one hard
budget of 3 min. After 5 s the setup screen shows "Checking PostgreSQL
with macOS security… This can take a minute the first time."
- On timeout the child gets SIGKILL. Waiting for its exit and for
stdout/stderr is bounded by a 2 s grace, so a retry cannot hang.
Cancelling kills the running check at once.
- The error names the cause: timeout, blocked by the OS (signal), failed
run, or wrong version. `logs/desktop.log` gets the binary, the exit code
or signal, whether the timeout fired, the elapsed time, and trimmed
stdout/stderr with the runtime path replaced by `<runtime>`. A
successful install logs the first-launch times.
- On darwin, `preparePostgres` removes `com.apple.quarantine` from the
extracted runtime. Integrity still comes from the pinned SHA-512,
checked before extraction.
- Retries already reused the verified `package.tgz.partial`; a test now
covers it, and that no `extracted/` or `install/` directory is left
after a failure.
- The setup screen drops Electron's `Error invoking remote method
'canopy:configure': Error:` prefix.
- Confirmed that another PostgreSQL cannot interfere: binaries run by
absolute path, the service env allowlist has no `PG*` or `DYLD*`,
Postgres uses a random port on `127.0.0.1` with no Unix socket (`-k
""`), and it has its own data dir. Documented in `docs/desktop.md`.

`Fixes: #23`: #23 moved PostgreSQL from the signed bundle to a runtime
download, which runs freshly written binaries through the 10 s check.

## Tests

`apps/desktop/test/postgres-check.test.ts` uses an injectable spawn and
short budgets:
- a launch stuck until SIGKILL;
- a process that never exits or closes its output, which stays bounded;
- a slow launch that shows progress;
- signal, exit and version classification, with no path in the message;
- cancellation;
- a real installer retry against the fixture registry: the first attempt
fails, the download is reused (1 request), and the stage directories are
cleaned up.

`bun install`, `gen:check`, `typecheck`, `lint`, `format:check`,
`check:tmp` (1530 pass, 0 fail) and the web build are all clean.

## Not verified

- A real fresh macOS 27 MacBook running the packaged app, where the
controller is a child of Canopy.app. The Mac Studio tests ran over SSH.
The 3 min budget is a judgement, not a measured worst case.
- What a GUI-session app sees when it launches a quarantined binary (a
Gatekeeper dialog or a block). Canopy's extraction does not add
quarantine, so the removal is a precaution.
- The hang on "Try again" could not be reproduced directly. The old code
could appear stuck for up to about 35 s (5 s of synchronous preparation
plus 3 × 10 s with no new status) and had no bound on reading stdout.
Both are now bounded and visible.

## Copy audit (docs/copy.md §8)

| Before | After | Why |
| --- | --- | --- |
| `Downloaded PostgreSQL could not run on this computer. Check the
desktop logs and OS security policy, then try again.` (every failure) |
timeout: `PostgreSQL did not respond within 3 min. macOS may still be
checking it for security. Try again.` | names the likely cause; one idea
per sentence |
| same | signal: `macOS stopped PostgreSQL from running. Check System
Settings → Privacy & Security, then try again.` (other OS: `Your
computer stopped PostgreSQL from running. Check your security software,
then try again.`) | concrete action and settings path |
| same | wrong version: `Downloaded PostgreSQL reports an unexpected
version. Try again.` | separate cause |
| same | other failures: `Downloaded PostgreSQL could not run on this
computer. Check the desktop logs, then try again.` | drops the vague "OS
security policy" |
| — | progress after 5 s: `Checking PostgreSQL with macOS security… This
can take a minute the first time.` (other OS: `Checking PostgreSQL… This
can take a minute the first time.`) | §4.6: -ing verb with ellipsis,
says it happens only the first time |
| `Error invoking remote method 'canopy:configure': Error: …` | `…` | no
internal jargon on the surface |

Checks: sentence case; each error ends with an action; no paths or
jargon; no humour in errors; sentences ≤ 20 words. Glossary: no new
terms. The texts sit in the existing startup alert and status line, so
the layout is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant