From c7ee49d57c7e549312522ff83d2077acee3191d3 Mon Sep 17 00:00:00 2001 From: freebuff-rune Date: Tue, 17 Nov 2026 14:54:16 -0500 Subject: [PATCH 1/4] Document the desktop orchestrator's shell contract A desktop shell has to start the orchestrator, keep it alive and talk to it, and none of that boundary is written down anywhere public: the boot phase names, the readiness budgets, the stdin bootstrap line, the HMAC lifetime socket and the x-freebuff-launch-id gate on /api/* are all re-derived by reading a shipped install. This documents the boundary as observed from one, so a client, a test harness or a native shell can drive the same server. No user data, no server code. --- docs/desktop-shell-contract.md | 100 +++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 docs/desktop-shell-contract.md diff --git a/docs/desktop-shell-contract.md b/docs/desktop-shell-contract.md new file mode 100644 index 0000000000..56770a7326 --- /dev/null +++ b/docs/desktop-shell-contract.md @@ -0,0 +1,100 @@ +# The desktop orchestrator's shell contract + +The desktop app is a shell around a local HTTP server. The shell is what starts it, keeps it +alive, and talks to it; the server — a Bun process shipped with the desktop build — is the part +that holds projects, threads and sessions. This document is the observable contract *of that +shell boundary*, recovered from a shipped install, so that other clients (a test harness, a +native shell, an automation script) can drive the same server without re-deriving it. + +Nothing here is private: it is the shape of a loopback interface, not any user's data. + +## The two-process shape + +``` +shell ──spawn──▶ bun resources/orchestrator/orchestrator.js (cwd = …/orchestrator) + │ │ stdout = boot progress lines + │ └─▶ http://127.0.0.1: (Bun.serve, host 127.0.0.1) + └── owns the lifetime socket; the server stops when it closes +``` + +The shell picks a free loopback port and passes it as `PORT`. The server binds `127.0.0.1` only +and rejects requests whose `Host` header is not a loopback host. + +## Boot progress on stdout + +The server prints one whitelisted phase per step; anything else on stdout is ignored by the +shell. + +``` +[orchestrator-phase] +``` + +The phases, in order: `started, bootstrap, profile_lock, auth, recover_setups, terminals, +listen, projects_boot_start, projects_boot_end`. Only these nine names are reported. + +When the HTTP server is up it prints: + +``` +[orchestrator-ready] {"launchId":…,"pid":…,"port":…} +``` + +The shell's two budgets, from the shipped startup code: + +1. spawn → ready line: **30 seconds** +2. ready line → first healthy `GET /healthz`: **60 seconds** + +`/healthz` answers plain `ok` when the server has no launch id, or +`{"ok":true,"launchId":…,"pid":…,"port":…}` when it does. + +## Secrets: the stdin bootstrap line + +Launch secrets are **not** passed in the environment, because any process of the same user can +read a child's starting environment. They ride the first stdin line: + +```json +{"shellLifetimeToken":"…","launchId":"…","cdpBridgeToken":null,"mcpConsentToken":null} +``` + +A bare token string is also accepted as the lifetime token alone. + +## Lifetime: how a dead shell takes its server down + +The server is told to be shell-managed via `FREEBUFF_SHELL_LIFETIME_PORT`; when set, the server +shuts down once the socket closes — a killed shell cannot orphan a server. To keep a foreign +process from holding the server hostage, the socket is challenged: + +1. server writes `challenge\n` (32 random bytes, base64url); +2. client answers `clientChallenge:HMAC-SHA256(token, "client:"+challenge+":"+clientChallenge)`; +3. server replies `HMAC-SHA256(token, "server:"+challenge+":"+clientChallenge)`; +4. then the client pings with `ping\n` every 3 s; the server tolerates 10 s of silence. + +## The API gate + +Every `/api/*` path is refused with `401 unauthorized` unless the request carries +`x-freebuff-launch-id: ` (the value from the bootstrap line) or the +`freebuff_launch_` cookie. A server started with no launch id admits everything. + +## Environment + +| Variable | Meaning | +|---|---| +| `PORT` | loopback port to bind | +| `FREEBUFF_DESKTOP_STATE_PATH` | profile path, default `~/.config/freebuff-desktop/state.json` | +| `FREEBUFF_PROFILE_LOCK_WAIT_MS` | wait on the profile lock before failing (exit 75 + a stderr explanation if another orchestrator holds it) | +| `FREEBUFF_SHELL_LIFETIME_PORT` | lifetime server port (see above) | +| `FREEBUFF_UI_DIR` | where `ui/` lives, served at `/` | +| `CODEBUFF_WASM_DIR`, `CODEBUFF_TREE_SITTER_WASM_PATH` | tree-sitter wasm used by code features | +| `FREEBUFF_BUN_PATH` | override the bundled Bun | + +## Why this exists + +A shell boundary like this is the kind of thing a port, a test harness or an automation script +re-derives one frustrating session at a time. This page fixes the shape in one place. It came +from observing a shipped install, so if the real shell ever changes the handshake, this page +should be updated with it. + +A reference implementation of all of the above is ~500 lines of dependency-light Rust: it spawns +the same server, keeps it alive and reports its state in a sidebar. It is deliberately not part of +this document — whether a native shell belongs in this repository is the question the accompanying +issue asks. Neither the shell code nor the server code is required to use this contract; only the +words above. \ No newline at end of file From c1d1b35f7c0e8ee1c56f97ef6dd8561ddfefa369 Mon Sep 17 00:00:00 2001 From: phantomic12 <76127596+phantomic12@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:50:38 -0500 Subject: [PATCH 2/4] Record where each claim in the shell contract came from The review asked for provenance: which build this was checked against, and what to grep to spot-check a claim without re-deriving it. Adds the desktop version (0.0.203), the SHA-256 of each artifact read, and a table of the literal behind every constant in the document; the bundle keeps its source paths, so src/server/boot-phase.ts and src/server/services/orchestrator-lock.ts are named as the places to look. Also states the staleness risk plainly: no handshake carries a protocol version, so a build that renames a phase or moves a budget breaks a client silently. The shape is what to rely on; the constants are what to re-read per release. And scopes the page away from the direction question in the companion issue -- a harness or a diagnostics client needs these facts regardless. --- docs/desktop-shell-contract.md | 68 +++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/docs/desktop-shell-contract.md b/docs/desktop-shell-contract.md index 56770a7326..d9b0cdb0bb 100644 --- a/docs/desktop-shell-contract.md +++ b/docs/desktop-shell-contract.md @@ -8,6 +8,56 @@ native shell, an automation script) can drive the same server without re-derivin Nothing here is private: it is the shape of a loopback interface, not any user's data. +## Provenance + +Written against the installed **Freebuff Desktop `0.0.203`** build. Every claim below is a literal in +one of these files, recorded with the hash of the copy it was read from: + +| Artifact | SHA-256 (first 16) | +|---|---| +| `resources/app.asar` (the Electron shell) | `af684513edb0c024` | +| `app/electron/orchestrator-startup.cjs` | `debeb69d7d935a81` | +| `app/electron/shell-lifetime.cjs` | `9affdeff0595e928` | +| `app/electron/launch-bootstrap.cjs` | `e7b2390dbc95f2ae` | +| `resources/orchestrator/orchestrator.js` (the bundled server) | `6a34e09a2722c87b` | + +The bundle keeps its source paths, which are the fastest way to check these against the real tree: +`src/server/boot-phase.ts` (the phase list), `src/server/services/orchestrator-lock.ts` (the profile +lock and the exit code), and the shell modules named above. + +| Claim | Where | Literal | +|---|---|---| +| the nine phases, in order | `orchestrator-startup.cjs:22` | `const BOOT_PHASES = new Set([` … `'projects_boot_end',` | +| phase line prefix | `orchestrator-startup.cjs:19` | `const PHASE_PREFIX = '[orchestrator-phase] '` | +| 30 s spawn → ready | `orchestrator-startup.cjs:35` | `const READY_LINE_BUDGET_MS = 30_000` | +| 60 s ready → `/healthz` | `orchestrator-startup.cjs:37` | `const HEALTH_BUDGET_MS = 60_000` | +| ready line shape | `orchestrator.js` | ``console.log(`[orchestrator-ready] ${JSON.stringify({ launchId: LAUNCH_ID, pid: process.pid, port: serverPort })}`)`` | +| stdin bootstrap line | `launch-bootstrap.cjs:20` | `function launchBootstrapLine({ shellLifetimeToken, launchId, cdpBridgeToken, mcpConsentToken })` | +| 3 s ping | `shell-lifetime.cjs:9` | `const DEFAULT_PING_MS = 3000` | +| HMAC labels | `shell-lifetime.cjs:37,46` | `` `client:${challenge}:${clientChallenge}` ``, `` `server:${challenge}:${clientChallenge}` `` | +| `/api/*` gate | `orchestrator.js` | `pathname.startsWith("/api/")` | +| header and cookie names | `orchestrator.js` | `var LAUNCH_TOKEN_HEADER = "x-freebuff-launch-id"`, `` `freebuff_launch_${port}` `` | +| 401 body | `orchestrator.js` | `json4({ error: "invalid launch id" }, 401)` | +| exit code 75 | `orchestrator.js` | `var PROFILE_BUSY_EXIT = 75` … `process.exit(PROFILE_BUSY_EXIT)` | +| profile lock file | `orchestrator.js` | `` `${statePath}.orchestrator-lock.sqlite` ``, opened with `PRAGMA busy_timeout = 0` | +| lock wait default | `main.cjs:932` | `FREEBUFF_PROFILE_LOCK_WAIT_MS: '10000'` | + +Re-checking a claim is one grep, no build: + +```bash +npx @electron/asar extract "/resources/app.asar" app +grep -n "READY_LINE_BUDGET_MS\|HEALTH_BUDGET_MS" app/electron/orchestrator-startup.cjs +grep -n "PROFILE_BUSY_EXIT\|invalid launch id\|LAUNCH_TOKEN_HEADER" \ + "/resources/orchestrator/orchestrator.js" +``` + +**Across versions.** None of these constants is negotiated — no handshake carries a protocol +version — so a build that renames a phase or moves a budget breaks a client silently instead of +saying so. This page is a snapshot of one build, and where it disagrees with a newer one, the build +is right. The parts worth relying on are the shape: a phase line on stdout, a ready line carrying a +launch id, a bootstrap line on stdin, an HMAC lifetime socket, and a launch-id header on `/api/*`. +The constants next to them are the part to re-read when a release changes. + ## The two-process shape ``` @@ -86,15 +136,13 @@ Every `/api/*` path is refused with `401 unauthorized` unless the request carrie | `CODEBUFF_WASM_DIR`, `CODEBUFF_TREE_SITTER_WASM_PATH` | tree-sitter wasm used by code features | | `FREEBUFF_BUN_PATH` | override the bundled Bun | -## Why this exists +## Scope -A shell boundary like this is the kind of thing a port, a test harness or an automation script -re-derives one frustrating session at a time. This page fixes the shape in one place. It came -from observing a shipped install, so if the real shell ever changes the handshake, this page -should be updated with it. +This documents a boundary that exists today, and it is useful whether or not a native shell is ever +built: a test harness, a diagnostics client or an automation script needs the same facts, and none +of them needs the direction question answered first. Nothing here argues for a particular shell. -A reference implementation of all of the above is ~500 lines of dependency-light Rust: it spawns -the same server, keeps it alive and reports its state in a sidebar. It is deliberately not part of -this document — whether a native shell belongs in this repository is the question the accompanying -issue asks. Neither the shell code nor the server code is required to use this contract; only the -words above. \ No newline at end of file +A reference implementation of all of the above is ~500 lines of dependency-light Rust: it spawns the +same server, keeps it alive and reports its state in a sidebar. It is deliberately not part of this +document. Neither the shell code nor the server code is required to use this contract; only the +words above. From c3239f2f1bfa148c47a28580a10a8748d7c975a2 Mon Sep 17 00:00:00 2001 From: phantomic12 <76127596+phantomic12@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:48:25 -0500 Subject: [PATCH 3/4] Note the orchestrator bundle that replaced the one this page was read from An update landed while this page was under review: orchestrator.js moved from 6a34e09a2722c87b to c0267ead585538d3 (the shell's app.asar hash did not change). Re-read every literal against the newer bundle -- the phases, both budgets, the launch-id header, PROFILE_BUSY_EXIT = 75 and its stderr line are all still there, unchanged -- so the page still describes the current build, and the hash pair records that it was checked rather than assumed. --- docs/desktop-shell-contract.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/desktop-shell-contract.md b/docs/desktop-shell-contract.md index d9b0cdb0bb..d6d42ce415 100644 --- a/docs/desktop-shell-contract.md +++ b/docs/desktop-shell-contract.md @@ -19,7 +19,14 @@ one of these files, recorded with the hash of the copy it was read from: | `app/electron/orchestrator-startup.cjs` | `debeb69d7d935a81` | | `app/electron/shell-lifetime.cjs` | `9affdeff0595e928` | | `app/electron/launch-bootstrap.cjs` | `e7b2390dbc95f2ae` | -| `resources/orchestrator/orchestrator.js` (the bundled server) | `6a34e09a2722c87b` | +| `resources/orchestrator/orchestrator.js` (the bundled server) | `6a34e09a2722c87b`, then `c0267ead585538d3` | + +The orchestrator bundle is the one file here that an update replaces on its own: while this page +was under review the install moved from `6a34e09a2722c87b` to `c0267ead585538d3` (the shell's +`app.asar` hash above did not change). Every literal in the table below was re-read in the newer +bundle and every one of them is still there, unchanged — the phases, both budgets, the launch-id +header, the profile-lock exit code and its stderr line. So the page still describes the current +build, and the pair of hashes is the record of that check. The bundle keeps its source paths, which are the fastest way to check these against the real tree: `src/server/boot-phase.ts` (the phase list), `src/server/services/orchestrator-lock.ts` (the profile From 7aa062ecb86a8ded8c8ed9ab32e122e096e8ea03 Mon Sep 17 00:00:00 2001 From: Yoav Date: Sun, 29 Nov 2026 00:31:35 -0500 Subject: [PATCH 4/4] Re-read the page against 0.0.206, and add the gates it was missing The install moved from 0.0.203 to 0.0.206 while this was open: `app.asar` `af684513` to `b768b8c7`, `orchestrator.js` `c0267ead` to `4ea1f56d`, `orchestrator-startup.cjs` `debeb69d` to `e5731573`. Every literal in the table was re-read against the newer build; all of them are still there, but two moved (`HEALTH_BUDGET_MS` is line 52 now, the lock wait is `main.cjs:1015`), and the provenance block is now written against 0.0.206 with the current hashes. Two things are new rather than corrected: - `resources/orchestrator/build-info.json` ships beside the server and names the build: `{commitSha, builtAt, apiOrigin, webOrigin}`. That is a better anchor than a file hash for "which build is this claim true of", so the page now says to log it, and keeps the hashes as one release's record. - Three gates that were missing entirely: the Host header must be a loopback host *with the server's port* (403 `forbidden host`), `/api/*` refuses a foreign `Origin` but allows none at all (403 `forbidden origin`), and `/healthz` itself wants the launch-id header once a launch id exists. A client that sends `Host: 127.0.0.1` without the port, or skips the header on the health check, gets a refusal that reads like a bug. Also documents hot bundles, which are the one way the server that runs differs from the file shipped beside the app: `orchestrator-loader.mjs` when `FREEBUFF_ORCHESTRATOR_BUNDLE_DIR` is offered, `ORCHESTRATOR_SHELL_ABI = 1`, the bundle's own `ui/`, the `[orchestrator-bundle]` line, and the two env knobs. Still documentation only: no path outside `docs/` is touched. --- docs/desktop-shell-contract.md | 149 ++++++++++++++++++++++++++------- 1 file changed, 120 insertions(+), 29 deletions(-) diff --git a/docs/desktop-shell-contract.md b/docs/desktop-shell-contract.md index d6d42ce415..f4b6509dcc 100644 --- a/docs/desktop-shell-contract.md +++ b/docs/desktop-shell-contract.md @@ -10,23 +10,33 @@ Nothing here is private: it is the shape of a loopback interface, not any user's ## Provenance -Written against the installed **Freebuff Desktop `0.0.203`** build. Every claim below is a literal in +Written against the installed **Freebuff Desktop `0.0.206`** build. Every claim below is a literal in one of these files, recorded with the hash of the copy it was read from: | Artifact | SHA-256 (first 16) | |---|---| -| `resources/app.asar` (the Electron shell) | `af684513edb0c024` | -| `app/electron/orchestrator-startup.cjs` | `debeb69d7d935a81` | +| `resources/app.asar` (the Electron shell) | `b768b8c718179c6e` | +| `app/electron/orchestrator-startup.cjs` | `e5731573abb8a3b4` | | `app/electron/shell-lifetime.cjs` | `9affdeff0595e928` | | `app/electron/launch-bootstrap.cjs` | `e7b2390dbc95f2ae` | -| `resources/orchestrator/orchestrator.js` (the bundled server) | `6a34e09a2722c87b`, then `c0267ead585538d3` | +| `resources/orchestrator/orchestrator.js` (the bundled server) | `4ea1f56d54dda08b` | +| `resources/orchestrator/build-info.json` | commit `17d3359f1349b40a`, built `2026-11-26T19:57:12Z` | -The orchestrator bundle is the one file here that an update replaces on its own: while this page -was under review the install moved from `6a34e09a2722c87b` to `c0267ead585538d3` (the shell's -`app.asar` hash above did not change). Every literal in the table below was re-read in the newer -bundle and every one of them is still there, unchanged — the phases, both budgets, the launch-id -header, the profile-lock exit code and its stderr line. So the page still describes the current -build, and the pair of hashes is the record of that check. +**Name the build, not the hash.** `resources/orchestrator/build-info.json` is written at build time +and ships beside the server: + +```json +{"commitSha":"17d3359f1349b40a2e2a4c82a2ef621f5e49a415", + "builtAt":"2026-11-26T19:57:12.527Z", + "apiOrigin":"https://www.codebuff.com","webOrigin":"https://freebuff.com"} +``` + +That is the durable anchor. The hashes above are one release's record; a client that reports +`commitSha` and `builtAt` from this file can say which server it actually ran, and a bug report can +be tied to a commit instead of to "whatever was installed". The shell does the same: it reads +`build-info.json` for its own crash grouping. Two releases are already enough to show why the hashes +are not the anchor — `0.0.203` shipped `app.asar` `af684513edb0c024` and `orchestrator.js` +`c0267ead585538d3`, and neither is the file above. The bundle keeps its source paths, which are the fastest way to check these against the real tree: `src/server/boot-phase.ts` (the phase list), `src/server/services/orchestrator-lock.ts` (the profile @@ -37,17 +47,22 @@ lock and the exit code), and the shell modules named above. | the nine phases, in order | `orchestrator-startup.cjs:22` | `const BOOT_PHASES = new Set([` … `'projects_boot_end',` | | phase line prefix | `orchestrator-startup.cjs:19` | `const PHASE_PREFIX = '[orchestrator-phase] '` | | 30 s spawn → ready | `orchestrator-startup.cjs:35` | `const READY_LINE_BUDGET_MS = 30_000` | -| 60 s ready → `/healthz` | `orchestrator-startup.cjs:37` | `const HEALTH_BUDGET_MS = 60_000` | +| 60 s ready → `/healthz` | `orchestrator-startup.cjs:52` | `const HEALTH_BUDGET_MS = 60_000` | | ready line shape | `orchestrator.js` | ``console.log(`[orchestrator-ready] ${JSON.stringify({ launchId: LAUNCH_ID, pid: process.pid, port: serverPort })}`)`` | | stdin bootstrap line | `launch-bootstrap.cjs:20` | `function launchBootstrapLine({ shellLifetimeToken, launchId, cdpBridgeToken, mcpConsentToken })` | | 3 s ping | `shell-lifetime.cjs:9` | `const DEFAULT_PING_MS = 3000` | | HMAC labels | `shell-lifetime.cjs:37,46` | `` `client:${challenge}:${clientChallenge}` ``, `` `server:${challenge}:${clientChallenge}` `` | -| `/api/*` gate | `orchestrator.js` | `pathname.startsWith("/api/")` | -| header and cookie names | `orchestrator.js` | `var LAUNCH_TOKEN_HEADER = "x-freebuff-launch-id"`, `` `freebuff_launch_${port}` `` | -| 401 body | `orchestrator.js` | `json4({ error: "invalid launch id" }, 401)` | -| exit code 75 | `orchestrator.js` | `var PROFILE_BUSY_EXIT = 75` … `process.exit(PROFILE_BUSY_EXIT)` | -| profile lock file | `orchestrator.js` | `` `${statePath}.orchestrator-lock.sqlite` ``, opened with `PRAGMA busy_timeout = 0` | -| lock wait default | `main.cjs:932` | `FREEBUFF_PROFILE_LOCK_WAIT_MS: '10000'` | +| `/api/*` gate | `orchestrator.js:98701` | `pathname.startsWith("/api/")` | +| header and cookie names | `orchestrator.js:98704–98706` | `var LAUNCH_TOKEN_HEADER = "x-freebuff-launch-id"`, `` `freebuff_launch_${port}` `` | +| 401 body | `orchestrator.js:253740` | `json4({ error: "invalid launch id" }, 401)` | +| exit code 75 | `orchestrator.js:252976` | `var PROFILE_BUSY_EXIT = 75` … `process.exit(PROFILE_BUSY_EXIT)` | +| profile lock file | `orchestrator.js:240134` | `` `${statePath}.orchestrator-lock.sqlite` ``, opened with `PRAGMA busy_timeout = 0` | +| lock wait default | `main.cjs:1015` | `FREEBUFF_PROFILE_LOCK_WAIT_MS: '10000'` | +| bundle entry, when offered | `main.cjs:620` | `bundleArgs: bunLaunchArgs(path.join(orchDir, 'orchestrator-loader.mjs'))` | +| bundle protocol | `orchestrator-bundle.cjs:36` | `const ORCHESTRATOR_SHELL_ABI = 1` | +| host gate | `orchestrator.js` | `isAllowedLoopbackHost(req.headers.get("host"), guard.port)`, `json4({ error: "forbidden host" }, 403)` | +| origin gate | `orchestrator.js` | `ALLOWED_FETCH_SITES = new Set(["same-origin", "none"])`, `refuse(403, "forbidden origin")` | +| `/healthz` gate | `orchestrator.js` | `isAuthorizedApiRequest(LAUNCH_ID, req.headers.get("x-freebuff-launch-id"))` | Re-checking a claim is one grep, no build: @@ -56,14 +71,16 @@ npx @electron/asar extract "/resources/app.asar" app grep -n "READY_LINE_BUDGET_MS\|HEALTH_BUDGET_MS" app/electron/orchestrator-startup.cjs grep -n "PROFILE_BUSY_EXIT\|invalid launch id\|LAUNCH_TOKEN_HEADER" \ "/resources/orchestrator/orchestrator.js" +cat "/resources/orchestrator/build-info.json" ``` -**Across versions.** None of these constants is negotiated — no handshake carries a protocol -version — so a build that renames a phase or moves a budget breaks a client silently instead of -saying so. This page is a snapshot of one build, and where it disagrees with a newer one, the build -is right. The parts worth relying on are the shape: a phase line on stdout, a ready line carrying a -launch id, a bootstrap line on stdin, an HMAC lifetime socket, and a launch-id header on `/api/*`. -The constants next to them are the part to re-read when a release changes. +**Across versions.** None of the handshake constants is negotiated — no boot handshake carries a +protocol version — so a build that renames a phase or moves a budget breaks a client silently +instead of saying so. `build-info.json` is what makes that recoverable after the fact: a client logs +it once at boot, and a mismatch is then "this build" rather than "some build". The parts worth +relying on are the shape: a phase line on stdout, a ready line carrying a launch id, a bootstrap +line on stdin, an HMAC lifetime socket, a launch-id header on `/api/*`, and the bundle handshake +below. The constants next to them are the part to re-read when a release changes. ## The two-process shape @@ -74,8 +91,11 @@ shell ──spawn──▶ bun resources/orchestrator/orchestrator.js (cwd = └── owns the lifetime socket; the server stops when it closes ``` -The shell picks a free loopback port and passes it as `PORT`. The server binds `127.0.0.1` only -and rejects requests whose `Host` header is not a loopback host. +The shell picks a free loopback port and passes it as `PORT`. The server binds `127.0.0.1` only. + +One exception to the entry point above: when the shell has a downloaded *hot bundle* to run, it +launches `orchestrator-loader.mjs` instead. See "Hot bundles" below — a client that skips it still +works, it just runs the server shipped with the app rather than the one the app would run. ## Boot progress on stdout @@ -101,7 +121,8 @@ The shell's two budgets, from the shipped startup code: 2. ready line → first healthy `GET /healthz`: **60 seconds** `/healthz` answers plain `ok` when the server has no launch id, or -`{"ok":true,"launchId":…,"pid":…,"port":…}` when it does. +`{"ok":true,"launchId":…,"pid":…,"port":…}` when it does — and in that case it wants the launch-id +header too (see "The gates on every request"). ## Secrets: the stdin bootstrap line @@ -125,12 +146,79 @@ process from holding the server hostage, the socket is challenged: 3. server replies `HMAC-SHA256(token, "server:"+challenge+":"+clientChallenge)`; 4. then the client pings with `ping\n` every 3 s; the server tolerates 10 s of silence. -## The API gate +## The gates on every request -Every `/api/*` path is refused with `401 unauthorized` unless the request carries -`x-freebuff-launch-id: ` (the value from the bootstrap line) or the +Three checks run before a route does, and each one has its own answer, so a client that gets a `403` +can tell which gate it hit. All three body shapes are `{"error": …}`. + +**1. Host.** The `Host` header must name a loopback host — `127.0.0.1`, `localhost` or `::1` — *and* +carry the server's port, since `isAllowedLoopbackHost` compares the port it parsed (default 80) with +its own. Otherwise: + +``` +403 {"error":"forbidden host"} +``` + +This applies to `/healthz` as well, so a client that connects to `127.0.0.1:` and sends +`Host: 127.0.0.1` without the port is refused even though the socket is right. + +**2. Origin.** `/api/*` also checks the browser's provenance. A **missing** `Origin` is allowed — +which is what a native client sends — but a present one must be exactly +`http://127.0.0.1:` or `http://localhost:`, or a loopback dev-UI origin the shell named in +`FREEBUFF_DESKTOP_UI_ORIGIN`. A present `Sec-Fetch-Site` must be `same-origin` or `none`, and an +`Origin` carrying credentials is refused. Otherwise: + +``` +403 {"error":"forbidden origin"} +``` + +So a native client should not send `Origin` to the local server at all. (The hosted cloud API is the +opposite: it *requires* its own `Origin`.) + +**3. Launch id.** Every `/api/*` path is refused with `401 {"error":"invalid launch id"}` unless the +request carries `x-freebuff-launch-id: ` (the value from the bootstrap line) or the `freebuff_launch_` cookie. A server started with no launch id admits everything. +`/healthz` sits between the two: it takes the Host gate, and when a launch id exists it also wants the +header (401 otherwise); when the id matches it answers +`{"ok":true,"launchId":…,"pid":…,"port":…}`, and a server started with no launch id answers plain +`ok`. A health check that works without the header is a signed-out server, not a healthy one. + +## Hot bundles: which server a launch runs + +A packaged app can run a downloaded `orchestrator.js` instead of the shipped one, and it does so +without an app update. The rules matter to any shell that wants to run what the user's app runs: + +- The shell downloads a bundle into `/orchestrator-bundles//` and offers that + folder by setting `FREEBUFF_ORCHESTRATOR_BUNDLE_DIR` on the launch. The launch entry then becomes + `resources/orchestrator/orchestrator-loader.mjs`, not `orchestrator.js`. +- The bundle carries its own `ui/` as well as its `orchestrator.js`, because the server serves its + own renderer: a new server beside an old `ui/` is a pairing no release tested. +- Nothing from a bundle is used unverified. The loader rewrites the *shipped* entry path to the + bundle's verified bytes through a Bun runtime plugin, so `import.meta.dir` and everything resolved + relative to it (`wasm/`, `vendor/ripgrep`) still come from the signed app. `orchestrator.js` is + hashed over the bytes that run and every `ui/` file over the bytes served. +- The bundle's `ui/` and its hashes reach the server through a global (`Symbol.for( + 'freebuff.orchestrator-bundle')`), never the environment, which every child would inherit. +- The loader removes `FREEBUFF_ORCHESTRATOR_BUNDLE_DIR` before the orchestrator starts, and logs one + line saying which it ran: + + ``` + [orchestrator-bundle] running + [orchestrator-bundle] rejected (); running the shipped orchestrator + ``` + +- The shell reads a refusal from that line and drops the bundle; a bundle whose launch fails is + dropped too, and a dropped bundle is never offered again. Bundles are also re-checked on an + interval (`FREEBUFF_BUNDLE_CHECK_MS`, default every 2 hours, minimum 30 s). +- A bundle declares which shell ABI it expects: `ORCHESTRATOR_SHELL_ABI` is `1` today. A bundle + built for a different value is rejected rather than half-run. + +A client that ignores all of this still has a working server — the shipped one. It just will not be +the same server, or the same behaviour, as the Electron shell on a machine that received a bundle. +That is worth logging: `build-info.json` says which commit the shipped server is, and the +`[orchestrator-bundle]` line says whether that server is the one that ran. + ## Environment | Variable | Meaning | @@ -140,6 +228,9 @@ Every `/api/*` path is refused with `401 unauthorized` unless the request carrie | `FREEBUFF_PROFILE_LOCK_WAIT_MS` | wait on the profile lock before failing (exit 75 + a stderr explanation if another orchestrator holds it) | | `FREEBUFF_SHELL_LIFETIME_PORT` | lifetime server port (see above) | | `FREEBUFF_UI_DIR` | where `ui/` lives, served at `/` | +| `FREEBUFF_APP_VERSION` | the installed app version, so telemetry groups by release | +| `FREEBUFF_ORCHESTRATOR_BUNDLE_DIR` | the hot bundle to run; read and removed by the loader | +| `FREEBUFF_BUNDLE_CHECK_MS` | how often the shell looks for a new bundle (default 2 h, floor 30 s) | | `CODEBUFF_WASM_DIR`, `CODEBUFF_TREE_SITTER_WASM_PATH` | tree-sitter wasm used by code features | | `FREEBUFF_BUN_PATH` | override the bundled Bun |