/** * api-base-owner — single source of truth for the renderer's API base URL. * * The renderer ("the WebView") needs a stable answer to "what loopback * port is the agent API listening on?". Five sites used to push this * answer independently: * 1. HTML inject before any renderer JS runs (static-server path) * 2. RPC push from `handleHomeWindowAgentReady` * 3. RPC push from a runtime-mode change handler * 4. RPC push from desktop-session priming * 5. RPC push from the menu-action runtime restart handler * * Each site decided independently when to push and what value to push. * The five copies were the disease behind the port-shift renderer * disconnect MASTER.md §0 documents; until *one* module owns the value * the renderer is reading, every new push site is a fresh chance to ship * the wrong port. * * This module owns: * - the *current* API base + token (module singleton) * - the HTML inject snippet for the static server (base only — never the * token, so served documents stay credential-free, #33034) * - the per-window push (delegates to the existing * `pushApiBaseToRenderer` RPC plumbing in `../api-base.ts`) — the only * path that carries the token to the webview * * Callers say `setCurrent(base, token)` to update, then either inject * via `injectIntoHtml(html)` (production static server) or push via * `pushToWindow(win)` (any time after a window mounts). The HTML inject * AND the RPC push read the same singleton, so the renderer can never * see two sources of truth. */ import { normalizeApiBase, pushApiBaseToRenderer, resolveDesktopRuntimeMode, resolveDesktopRuntimeModeSignal, } from "../api-base"; import { readDesktopEnvFlag } from "../desktop-env-flags"; import { getPersistedDeployment } from "../persisted-deployment"; import { getStartupTraceConfig } from "../startup-trace"; interface ApiBaseSnapshot { base: string | null; token: string; } let current: ApiBaseSnapshot = { base: null, token: "" }; // `JSON.stringify` is not a code sanitizer (CWE-94): it leaves `<`, `>`, and // the U+2028 / U+2029 line separators raw, so a base/token value containing // ``, `