Skip to content

refactor(host): route app paths and version through an AppEnvironment port - #15918

Merged
nwparker merged 6 commits into
nwparker/secret-store-portfrom
nwparker/app-environment-port
Aug 22, 2026
Merged

nwparker merged 6 commits into
nwparker/secret-store-portfrom
nwparker/app-environment-port

Conversation

@nwparker

@nwparker nwparker commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​73 $\color{#cf222e}{\Huge{\mathbf{−}}}$​60 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​13
Prod 16 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​235 $\color{#cf222e}{\Huge{\mathbf{−}}}$​61 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​174

Stacked on #15916 (nwparker/secret-store-port). Review that one first; this PR's diff assumes its src/main/host/ directory and vitest setup file exist.

ELI5

Everywhere Orca needs to know "where do I keep my data?" it asks Electron directly: app.getPath('userData'). That happens 37 times across 17 files. It is the single biggest reason the Orca runtime can only run inside Electron.

This PR does the same thing #15916 did for secrets: put a small interface in the middle. Code now asks "the app environment" for the path. On the desktop that's a direct pass-through to electron.app — same paths, same behaviour. A plain-Node server can answer differently.

What changed

src/shared/app-environment.ts The port: getPath, getAppPath, getVersion, isPackaged, onWillQuit, exit, getAppMetrics.
src/main/host/electron-app-environment.ts Desktop adapter — pass-through to electron.app.
src/main/index.ts Installs it beside the secret store, at the top of pre-ready bootstrap.
9 modules migrated telemetry, opencode/mimo/pi hook services, terminal-history-paths, terminal-scrollback-snapshots, cli-installer, clipboard-image-temp-file, memory/collector.
config/scripts/vitest-host-ports-setup.ts Renamed from vitest-secret-store-setup.ts; now installs both ports.

What is deliberately not migrated

src/main/browser/** — left on electron.app on purpose. That cluster is Chromium-adjacent by nature: cookie jars, download destinations, offscreen pages. A Node backend does not ship it at all, so porting it buys nothing and churns three heavily-mocked suites. I started that migration, measured the cost, and reverted it — the honest boundary is that browser/ is the desktop cluster.

Also left for later: call sites that additionally touch app.asar path literals or app.setName (ssh-relay-deploy, ssh-remote-cli-host-passthrough, persistence/loading-store/user-data-path, speech/stt-service, wsl-hook-relay-launch). Those need more than a mechanical swap.

Two judgement calls worth reviewing

getAppMetrics stayed on the port rather than being injected. It is Chromium process metrics, so it looks like it does not belong — but memory/collector.ts is its only caller and reads it from module scope. Adding an injection mechanism for one call site is more code than one extra port member. A Node host returns [], having no Chromium processes to measure.

getAppEnvironment() throws when uninstalled. Same reasoning as the secret store: a silent default resolves userData to the wrong directory and the caller writes real user state there before anyone notices.

Proof of no regressions

Check Result
Every touched area (window, browser, telemetry, cli, memory, pi, opencode, mimo, terminal paths, shared) 159 files / 1651 tests pass
tsc -p config/tsconfig.node.json clean
tsc -p config/tsconfig.tc.web.json clean
oxlint (full repo) clean

Why the desktop cannot behave differently: the adapter is a pass-through with no added logic. Paths still resolve at call time, not install time, so the existing app.setName / initDataPath ordering that userData depends on is unchanged — that ordering is called out in a comment at the install site.

Elegance pass

The naive migration had every affected suite restate all seven port members — ~7 lines × N files of boilerplate, which pushed clipboard-ipc-handlers.test.ts past the 800-line max-lines budget. Rather than add a max-lines disable (forbidden by AGENTS.md, and the wrong fix), the setup file now exports installFakeAppEnvironment(overrides) so a suite states only the member it cares about. Net: fewer lines than before, and the file is back under budget at 897→ within limit.

User-facing trade-offs

None. No UI, wire-format, on-disk-format, or CLI change.

Review loops

Round Reviewer Outcome
1 Grok 4.6 (adversarial, design) Flagged the singleton-vs-injection split as "worst of both", and that AppEnvironment was sitting in the tree uninstalled. The second point was correct and is fixed here — it is installed. On the first: the split is deliberate and documented in the design doc.
2 Self, post-implementation Caught the browser/** churn as not worth its cost; reverted. Caught the max-lines breach; fixed by removing boilerplate rather than suppressing.

Verdict: ready to merge, after #15916.

Follow-ups


Commits added after this PR was opened

Review found three things after the initial push; all are fixed here rather than deferred.

Commit Why
fix(typecheck): list the vitest host-ports setup in the node project CI caught what my local check missed. Three suites import installFakeAppEnvironment from config/scripts, which is outside tsconfig.node.json's include list → TS6307. I had verified with tsc --composite false, which does not reproduce it; only pnpm typecheck (what CI runs) does. Fixed by listing the one file, matching how that config already pins individual files.
refactor(host): drop two unused AppEnvironment exports hasAppEnvironment() and resetAppEnvironmentForTests() had zero callers — mirror-symmetry with the secret store rather than need.
test(terminal-history): install the AppEnvironment fake instead of mocking electron A real regression my targeted checks missed. Three terminal-history suites mocked electron.app.getPath to a fixture dir; that mock is now inert, so the global default's temp dir won and broke the WSL path assertions and every deletion count. Found only by a full-suite run — which is the argument for running the whole suite on a refactor this wide.

After the last of these, I computed the precise affected set rather than spot-checking: every test file that transitively imports one of the 9 migrated modules (81 files). All 81 pass — 849 tests, 0 failures.


Final regression evidence

Check Result
Full local suite, whole stack, run alone 6,070 files / 57,140 tests pass, 0 failures
Baseline: full local suite on main 57,173 tests pass, 0 failures — same shape
pnpm typecheck (node + web + cli projects) clean
oxlint (full repo) clean

@nwparker

Copy link
Copy Markdown
Contributor Author

Post-review fix pushed: the shared vitest setup allocated a mkdtempSync directory at module scope — evaluated once per test environment, i.e. per test file, not per worker — with nothing removing it, so a full ~6,000-file run left thousands behind. Now removed in afterAll. Proven with an isolated TMPDIR: a three-file run previously added directories and now leaves zero.

Readiness review of this stack returned no P0 and reported sections 03 (security) and 06 (backcompat / data loss) clean.

@nwparker
nwparker force-pushed the nwparker/app-environment-port branch from b7e5155 to e74e60c Compare August 22, 2026 10:22
@nwparker
nwparker force-pushed the nwparker/app-environment-port branch 2 times, most recently from a5c8f3d to cd29acb Compare August 22, 2026 23:11
… port

`app.getPath('userData')` is the single largest Electron coupling in the main
process — 37 call sites — and it is one of the things stopping the Orca runtime
from booting on plain Node. Give it the same treatment as SecretStore.

- `src/shared/app-environment.ts` — the port plus a settable registry, covering
  the members the runtime's module graph actually reads: paths, app path,
  version, packaged flag, shutdown hook, exit, and Chromium process metrics.
  `getAppEnvironment()` throws until installed, for the same reason the secret
  store does: a silent default resolves `userData` to the wrong directory and the
  caller writes real state there before anyone notices. No `node:` imports,
  because `src/shared/**` is in the web build graph.
- `src/main/host/electron-app-environment.ts` — the desktop adapter, a
  pass-through to `electron.app`.
- 9 modules migrated: telemetry, opencode/mimo/pi hook services,
  terminal-history-paths, terminal-scrollback-snapshots, cli-installer,
  clipboard-image-temp-file, memory/collector.

Deliberately NOT migrated: `src/main/browser/**`. That cluster is Chromium-
adjacent by nature — cookie jars, download destinations, offscreen pages — and a
Node backend does not ship it at all, so porting it buys nothing and churns
heavily-mocked suites. Also left alone for now: the call sites that additionally
touch `app.asar` path literals or `app.setName`, which need more than a
mechanical swap.

`getAppMetrics` stays on the port rather than being injected because
memory/collector.ts is its only caller and reads it from module scope; a Node
host returns [], having no Chromium processes to measure.

Test wiring: the secret-store setup file becomes `vitest-host-ports-setup.ts` and
installs both ports, exporting `fakeAppEnvironment`/`installFakeAppEnvironment`
so suites needing one specific member state only that instead of restating all
seven — which is boilerplate, and had pushed one suite past the max-lines budget.

Verified: 159 files / 1651 tests pass across every touched area; `tsc` clean on
both the node and web projects; `oxlint` clean.
Three suites import `installFakeAppEnvironment` from config/scripts, but that
directory is outside tsconfig.node.json's include list, so composite typecheck
failed with TS6307. Listing the one file matches how this config already pins
individual files it needs.

Local `tsc --composite false` does not reproduce this — only `pnpm typecheck`
does, which is what CI runs.
hasAppEnvironment() and resetAppEnvironmentForTests() had zero callers. The
secret-store equivalents are used, so these were mirror-symmetry rather than
need; add them back when something actually needs them.
…cking electron

These three suites mocked `electron.app.getPath` to point at a fixture dir. The
production module now reads the port, so the mock was inert and the global test
default's temp dir won — which broke the WSL path assertions and every deletion
count.

Found by a full-suite run, not by the targeted checks around the migrated modules,
which is the argument for running the whole suite on a refactor this wide.
The setup allocated a mkdtemp directory at module scope, which vitest evaluates
once per test *environment* — one per test file, not one per worker. Nothing
removed them, so a full 6,000-file run left thousands behind.

Proven: with an isolated TMPDIR, a three-file run previously added directories and
now leaves zero.
Same reason as the SecretStore: vi.resetModules() rebuilds the module registry,
and an environment installed before the reset read back as uninstalled.
@nwparker
nwparker force-pushed the nwparker/app-environment-port branch from cd29acb to 66c5ccc Compare August 22, 2026 23:24
@nwparker
nwparker merged commit 73fc142 into nwparker/secret-store-port Aug 22, 2026
47 checks passed
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