Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: drive the post-update dashboard auto-open in-process, not over HTTP
Adam Eivy031434cc3f62
diff --git a/scripts/open-ui-in-browser.js b/scripts/open-ui-in-browser.jsindex 73c70ef49f..dc9b855aef 100644--- a/scripts/open-ui-in-browser.js+++ b/scripts/open-ui-in-browser.js@@ -5,12 +5,26 @@//// Fail-soft: every failure path logs and exits 0 — never break setup/update// because Chrome happens to be unreachable.+//+// Drives browserService.navigateToUrlPinned() IN-PROCESS rather than POSTing+// to the running server's `/api/browser/navigate` route. That route sits+// behind the instance-password auth gate (server/services/authGate.js) when+// one is set, and this script — a local maintenance step run by the same OS+// user as the server, never crossing a network trust boundary — has no+// plaintext password to present. Every navigate attempt over HTTP would 401+// forever, silently leaving Chrome on a blank tab even after retrying for the+// full budget below. Calling the service function directly sidesteps the gate+// (there's no HTTP hop to gate) while still applying the same SSRF pinning+// (`assertPublicHttpUrl` + `verifyRemoteIp`) the route applies.import { join, dirname } from 'node:path';import { fileURLToPath } from 'node:url';import { hasTailscaleCert } from '../lib/tailscale-https.js';import { certPaths } from '../lib/certPaths.js';import { getCliSetupGuide } from './setup-guide.js';import { isDirectlyInvoked } from './lib/directInvocation.js';+import { navigateToUrlPinned } from '../server/services/browserService.js';+import { assertPublicHttpUrl } from '../server/lib/safeUrlFetch.js';+import { isBlockedIngestHost } from '../server/lib/catalogValidation.js';const ROOT = dirname(dirname(fileURLToPath(import.meta.url)));const { dir: CERT_DIR } = certPaths(join(ROOT, 'data'));@@ -27,10 +41,14 @@ const API_TIMEOUT_MS = 90_000;// racing the exact moment CPU/disk are busiest (npm install, client build).// browser/server.js's own launch-wait is 10s; a single fixed-timeout attempt// from here regularly lost that race and left Chrome open with no tab-// pointed anywhere. Retry navigate on a short interval instead of trying once.+// pointed anywhere. Retry the navigate on a short interval instead of trying+// once.const NAVIGATE_RETRY_TOTAL_MS = 60_000;const NAVIGATE_RETRY_INTERVAL_MS = 2_000;-const NAVIGATE_ATTEMPT_TIMEOUT_MS = 5_000;+// Mirrors NAVIGATE_SETTLE_MS in server/routes/browser.js — the settle window+// the pin holds open after the first document response so a client-side+// redirect that fires just after load is captured and IP-verified too.+const NAVIGATE_SETTLE_MS = 1_000;const POLL_INTERVAL_MS = 500;async function poll(checkFn, label, timeoutMs) {@@ -54,53 +72,31 @@ async function ping(url, fetchImpl = fetch) {}/**- * POST `navigateUrl` on a fixed interval until it succeeds or the overall+ * Call `navigateFn` on a fixed interval until it resolves or the overall* budget expires. A single early failure (Chrome still cold-launching, CDP* not listening yet) must not be treated as final — the caller only gets one* shot at auto-opening the dashboard per restart, so this has to outlast the* slowest realistic Chrome cold-start rather than the fastest.* @param {object} opts- * @param {string} opts.navigateUrl - the PortOS `/api/browser/navigate` endpoint- * @param {string} opts.targetUrl - the dashboard URL to open+ * @param {() => Promise<unknown>} opts.navigateFn - performs one navigate attempt, throwing on failure* @param {number} opts.totalTimeoutMs - overall retry budget* @param {number} opts.intervalMs - delay between attempts- * @param {number} opts.attemptTimeoutMs - abort timeout for a single attempt- * @param {typeof fetch} [opts.fetchImpl]* @param {(ms: number) => Promise<void>} [opts.sleep]- * @returns {Promise<{ok: boolean, status?: number, body?: string, error?: string}>}+ * @returns {Promise<{ok: boolean, page?: unknown, error?: string}>}*/export async function navigateWithRetry({- navigateUrl,- targetUrl,+ navigateFn,totalTimeoutMs,intervalMs,- attemptTimeoutMs,- fetchImpl = fetch,sleep = (ms) => new Promise(r => setTimeout(r, ms)),}) {const deadline = Date.now() + totalTimeoutMs;- let lastFailure = { ok: false, error: 'navigate was never attempted' };+ let lastError = new Error('navigate was never attempted');while (true) {- const controller = new AbortController();- const timer = setTimeout(() => controller.abort(), attemptTimeoutMs);- let networkError = null;- const res = await fetchImpl(navigateUrl, {- method: 'POST',- headers: { 'Content-Type': 'application/json' },- body: JSON.stringify({ url: targetUrl }),- signal: controller.signal,- }).catch(err => { networkError = err; return null; });- clearTimeout(timer);-- if (res) {- if (res.ok) return { ok: true, status: res.status };- const body = await res.text().catch(() => '');- lastFailure = { ok: false, status: res.status, body: body.slice(0, 200) };- } else {- lastFailure = { ok: false, error: networkError?.message || 'unknown fetch error' };- }+ const page = await navigateFn().catch(err => { lastError = err; return undefined; });+ if (page !== undefined) return { ok: true, page };- if (Date.now() + intervalMs >= deadline) return lastFailure;+ if (Date.now() + intervalMs >= deadline) return { ok: false, error: lastError?.message || 'unknown error' };await sleep(intervalMs);}}@@ -130,22 +126,30 @@ async function main() {const TARGET_URL = setupGuide.trustedUrl || LOCAL_BASE;const apiHealthUrl = `${LOCAL_BASE}/api/system/health`;- const navigateUrl = `${LOCAL_BASE}/api/browser/navigate`;const apiReady = await poll(() => ping(apiHealthUrl), 'PortOS API', API_TIMEOUT_MS);if (!apiReady) process.exit(0);+ // Fail-soft: a target URL this script itself derived should never trip the+ // SSRF guard, but if it somehow does, don't burn the retry budget re-trying+ // a URL that will never pass.+ const unsafeReason = await assertPublicHttpUrl(TARGET_URL).then(() => null).catch(err => err.message || 'unsafe URL');+ if (unsafeReason) {+ console.warn(`⚠️ Refusing to auto-open ${TARGET_URL} (${unsafeReason}) — open it manually.`);+ process.exit(0);+ }+const result = await navigateWithRetry({- navigateUrl,- targetUrl: TARGET_URL,+ navigateFn: () => navigateToUrlPinned(TARGET_URL, {+ verifyRemoteIp: (ip) => !isBlockedIngestHost(ip),+ settleMs: NAVIGATE_SETTLE_MS,+ }),totalTimeoutMs: NAVIGATE_RETRY_TOTAL_MS,intervalMs: NAVIGATE_RETRY_INTERVAL_MS,- attemptTimeoutMs: NAVIGATE_ATTEMPT_TIMEOUT_MS,});if (!result.ok) {- const detail = result.error || `HTTP ${result.status}: ${result.body}`;- console.warn(`⚠️ Could not auto-open the dashboard within ${NAVIGATE_RETRY_TOTAL_MS / 1000}s (${detail}) — open it manually: ${TARGET_URL}`);+ console.warn(`⚠️ Could not auto-open the dashboard within ${NAVIGATE_RETRY_TOTAL_MS / 1000}s (${result.error}) — open it manually: ${TARGET_URL}`);process.exit(0);}diff --git a/scripts/open-ui-in-browser.test.js b/scripts/open-ui-in-browser.test.jsindex 8c453cc208..7636cf0974 100644--- a/scripts/open-ui-in-browser.test.js+++ b/scripts/open-ui-in-browser.test.js@@ -2,36 +2,33 @@ import { describe, expect, it, vi } from 'vitest';import { navigateWithRetry } from './open-ui-in-browser.js';const baseOpts = {- navigateUrl: 'http://localhost:5553/api/browser/navigate',- targetUrl: 'https://host.example-tailnet.ts.net:5555',totalTimeoutMs: 10_000,intervalMs: 1_000,- attemptTimeoutMs: 1_000,sleep: vi.fn().mockResolvedValue(undefined),};describe('navigateWithRetry', () => {it('resolves immediately when the first attempt succeeds', async () => {- const fetchImpl = vi.fn().mockResolvedValue({ ok: true, status: 200 });- const result = await navigateWithRetry({ ...baseOpts, fetchImpl });- expect(result).toEqual({ ok: true, status: 200 });- expect(fetchImpl).toHaveBeenCalledTimes(1);+ const navigateFn = vi.fn().mockResolvedValue({ id: 'tab-1', url: 'https://host.example-tailnet.ts.net:5555' });+ const result = await navigateWithRetry({ ...baseOpts, navigateFn });+ expect(result).toEqual({ ok: true, page: { id: 'tab-1', url: 'https://host.example-tailnet.ts.net:5555' } });+ expect(navigateFn).toHaveBeenCalledTimes(1);expect(baseOpts.sleep).not.toHaveBeenCalled();});it('retries past a cold-launching browser (Chrome not up yet) and eventually succeeds', async () => {// Simulates the update.sh race: Chrome is still cold-launching so the first- // couple of navigate attempts fail (connection refused), then CDP comes up.+ // couple of navigate attempts fail (CDP connection refused), then it comes up.const sleep = vi.fn().mockResolvedValue(undefined);- const fetchImpl = vi.fn()- .mockRejectedValueOnce(new Error('connect ECONNREFUSED'))- .mockRejectedValueOnce(new Error('connect ECONNREFUSED'))- .mockResolvedValueOnce({ ok: true, status: 200 });+ const navigateFn = vi.fn()+ .mockRejectedValueOnce(new Error('CDP open-blank failed: connect ECONNREFUSED'))+ .mockRejectedValueOnce(new Error('CDP open-blank failed: connect ECONNREFUSED'))+ .mockResolvedValueOnce({ id: 'tab-1', url: 'https://host.example-tailnet.ts.net:5555' });- const result = await navigateWithRetry({ ...baseOpts, sleep, fetchImpl });+ const result = await navigateWithRetry({ ...baseOpts, sleep, navigateFn });- expect(result).toEqual({ ok: true, status: 200 });- expect(fetchImpl).toHaveBeenCalledTimes(3);+ expect(result).toEqual({ ok: true, page: { id: 'tab-1', url: 'https://host.example-tailnet.ts.net:5555' } });+ expect(navigateFn).toHaveBeenCalledTimes(3);expect(sleep).toHaveBeenCalledTimes(2);expect(sleep).toHaveBeenCalledWith(1_000);});@@ -40,35 +37,30 @@ describe('navigateWithRetry', () => {let now = 0;vi.spyOn(Date, 'now').mockImplementation(() => now);const sleep = vi.fn().mockImplementation(async (ms) => { now += ms; });- const fetchImpl = vi.fn().mockRejectedValue(new Error('connect ECONNREFUSED'));+ const navigateFn = vi.fn().mockRejectedValue(new Error('CDP open-blank failed: connect ECONNREFUSED'));const result = await navigateWithRetry({...baseOpts,totalTimeoutMs: 3_000,intervalMs: 1_000,sleep,- fetchImpl,+ navigateFn,});expect(result.ok).toBe(false);- expect(result.error).toBe('connect ECONNREFUSED');+ expect(result.error).toBe('CDP open-blank failed: connect ECONNREFUSED');// Budget of 3000ms at 1000ms/attempt: attempts stop once the next sleep// would cross the deadline, so it must not spin forever.- expect(fetchImpl.mock.calls.length).toBeGreaterThan(0);- expect(fetchImpl.mock.calls.length).toBeLessThan(10);+ expect(navigateFn.mock.calls.length).toBeGreaterThan(0);+ expect(navigateFn.mock.calls.length).toBeLessThan(10);vi.restoreAllMocks();});- it('treats a non-ok HTTP response (e.g. UNSAFE_URL 400) as a retryable failure, not a throw', async () => {- const fetchImpl = vi.fn().mockResolvedValue({- ok: false,- status: 400,- text: () => Promise.resolve('{"error":"UNSAFE_URL"}'),- });- const result = await navigateWithRetry({ ...baseOpts, totalTimeoutMs: 500, intervalMs: 1000, fetchImpl });+ it('surfaces a thrown refusal (e.g. the SSRF pin rejecting a redirect) as a retryable failure, not a crash', async () => {+ const navigateFn = vi.fn().mockRejectedValue(new Error('refusing to ingest: response IP not allowed'));+ const result = await navigateWithRetry({ ...baseOpts, totalTimeoutMs: 500, intervalMs: 1000, navigateFn });expect(result.ok).toBe(false);- expect(result.status).toBe(400);- expect(result.body).toContain('UNSAFE_URL');+ expect(result.error).toContain('refusing to ingest');});});