Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.
Rust acpagent-client-protocolai-agentsai-assistantclaudeclaude-codecodexcoding-agentscursorgeminigemini-cligrok-bot-alternativeiosiphonelinuxmacosmcpopen-sourcerustself-hosted
feat(desktop): report what came of a window whose page crashed
leepokai87d6cb2ee8f7
diff --git a/apps/desktop/e2e/window-recovery.spec.ts b/apps/desktop/e2e/window-recovery.spec.tsnew file mode 100644index 00000000..66c830a5--- /dev/null+++ b/apps/desktop/e2e/window-recovery.spec.ts@@ -0,0 +1,24 @@+import { test, expect } from '@playwright/test'+import { attach } from './sandbox.ts'++/** Whether the window shows the app; a crashed page doesn't answer. */+async function showsTheApp(): Promise<boolean> {+ const { browser, page } = await attach()+ try {+ return await page.locator('.sidebar-account').waitFor({ timeout: 2000 }).then(() => true, () => false)+ } finally {+ await browser.close()+ }+}++// A few runs within a minute leave the window down, as it should: the reloads in a row count (renderer-recovery.ts).+test('a window whose page crashed loads it again', async () => {+ const { browser, page } = await attach()+ const crashed = page.waitForEvent('crash')+ const cdp = await page.context().newCDPSession(page)+ // It never answers: the page that would goes down.+ void cdp.send('Page.crash').catch(() => {})+ await crashed+ await browser.close()+ await expect.poll(showsTheApp, { timeout: 20_000 }).toBe(true)+})diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.tsindex c67bb40b..1fe35da2 100644--- a/apps/desktop/src/main/index.ts+++ b/apps/desktop/src/main/index.ts@@ -43,7 +43,7 @@ if (!app.isPackaged && process.env.CODYNC_REMOTE_DEBUG) app.commandLine.appendSwconst host = new HostController()registerErrorReports(host)-registerRendererRecovery()+registerRendererRecovery(host)host.on('setupFailed', reportHostSetupFailure)const account = new AccountService()const updates = new Updates(host)diff --git a/apps/desktop/src/main/renderer-recovery.ts b/apps/desktop/src/main/renderer-recovery.tsindex b0c929aa..6e073c03 100644--- a/apps/desktop/src/main/renderer-recovery.ts+++ b/apps/desktop/src/main/renderer-recovery.ts@@ -1,21 +1,15 @@-import { app, type RenderProcessGoneDetails, type WebContents } from 'electron'--/** Why a window's page can die and come back by loading again; `clean-exit` and `killed` mean it was ended on purpose. */-const RECOVERABLE: ReadonlySet<RenderProcessGoneDetails['reason']> = new Set(['crashed', 'oom', 'abnormal-exit', 'memory-eviction'])-/** Reloads in a row: a page that crashes again after these stays down instead of looping. */-const MAX_RELOADS = 2-/** A page that stayed up this long since its reload counts as recovered: its next crash starts the count again. */-const RECOVERED_MS = 60_000+import { app } from 'electron'+import { trackBody } from '../shared/analytics'+import { pageRecovery } from '../shared/renderer-recovery'+import type { HostController } from './host-controller'/** A window whose page crashed loads it again instead of staying blank (error-reports.ts reports the crash). */-export function registerRendererRecovery() {- const reloads = new WeakMap<WebContents, { at: number; count: number }>()+export function registerRendererRecovery(host: HostController) {+ const onPageGone = pageRecovery((outcome) => {+ // Best effort: the host may be down or restarting, and the crash itself is reported apart.+ host.call('track', trackBody({ event: 'window_recovery', outcome })).catch(() => {})+ })app.on('render-process-gone', (_e, contents, { reason }) => {- if (!RECOVERABLE.has(reason) || contents.isDestroyed()) return- const last = reloads.get(contents)- const count = last && Date.now() - last.at < RECOVERED_MS ? last.count + 1 : 1- if (count > MAX_RELOADS) return- reloads.set(contents, { at: Date.now(), count })- contents.reload()+ if (!contents.isDestroyed()) onPageGone(contents, reason)})}diff --git a/apps/desktop/src/shared/analytics.ts b/apps/desktop/src/shared/analytics.tsindex 7ac27098..444c5609 100644--- a/apps/desktop/src/shared/analytics.ts+++ b/apps/desktop/src/shared/analytics.ts@@ -35,6 +35,9 @@ export type VoiceStuck = 'none' | 'connecting' | 'reconnecting' | 'speaking'/** Why a call's engine gave up. */export type VoiceFailure = 'none' | 'microphone' | 'connection' | 'provider'+/** What came of a window whose page crashed: it loaded again, or crashed too often and stays down. */+export type WindowRecoveryOutcome = 'reloaded' | 'gave_up'+export type AnalyticsEvent =| { event: 'app_opened'; at_login: boolean }| { event: 'window_opened' }@@ -49,6 +52,7 @@ export type AnalyticsEvent =| { event: 'screen_viewed'; screen: Screen }| { event: 'voice_call_started'; engine: VoiceCallEngine }| { event: 'voice_call_quality'; engine: VoiceCallEngine; ready_ms?: number; reconnects: number; stuck: VoiceStuck; failure: VoiceFailure }+ | { event: 'window_recovery'; outcome: WindowRecoveryOutcome }/** `track`'s body: the event with its properties, from the desktop app. */export const trackBody = (event: AnalyticsEvent) => ({ ...event, app: 'desktop' as const })diff --git a/apps/desktop/src/shared/renderer-recovery.test.ts b/apps/desktop/src/shared/renderer-recovery.test.tsnew file mode 100644index 00000000..88ce90d4--- /dev/null+++ b/apps/desktop/src/shared/renderer-recovery.test.ts@@ -0,0 +1,61 @@+import assert from 'node:assert/strict'+import { test } from 'node:test'+import { afterPageGone, pageRecovery } from './renderer-recovery.ts'+import type { WindowRecoveryOutcome } from './analytics.ts'++test('a page ended on purpose stays gone without counting as a failure', () => {+ assert.equal(afterPageGone('clean-exit', undefined, 0), null)+ assert.equal(afterPageGone('killed', undefined, 0), null)+})++test('a crashed page reloads twice in a row, then stays down', () => {+ for (const reason of ['crashed', 'oom', 'abnormal-exit', 'memory-eviction']) {+ const first = afterPageGone(reason, undefined, 1000)+ assert.deepEqual(first, { at: 1000, count: 1 })+ const second = afterPageGone(reason, { at: 1000, count: 1 }, 2000)+ assert.deepEqual(second, { at: 2000, count: 2 })+ assert.equal(afterPageGone(reason, { at: 2000, count: 2 }, 3000), 'stay_down')+ }+})++test('a page that stayed up a minute starts the count again', () => {+ assert.equal(afterPageGone('crashed', { at: 0, count: 2 }, 59_999), 'stay_down')+ assert.deepEqual(afterPageGone('crashed', { at: 0, count: 2 }, 60_000), { at: 60_000, count: 1 })+})++/** A recovery with a fake page: what it reported, how often it reloaded, and who waits for the page to load. */+function recovering() {+ const reports: WindowRecoveryOutcome[] = []+ const page = {+ reloads: 0,+ waiting: [] as (() => void)[],+ reload() { this.reloads++ },+ once(_event: 'did-finish-load', listener: () => void) { this.waiting.push(listener) },+ }+ return { reports, page, gone: pageRecovery((outcome) => reports.push(outcome)) }+}++test('a recovery reports once, when its page has loaded, however often the reload crashed', () => {+ const { reports, page, gone } = recovering()+ gone(page, 'crashed')+ gone(page, 'crashed')+ assert.equal(page.reloads, 2)+ assert.deepEqual(reports, [])+ assert.equal(page.waiting.length, 1)+ page.waiting[0]!()+ assert.deepEqual(reports, ['reloaded'])+})++test('a page that keeps crashing stays down and says so', () => {+ const { reports, page, gone } = recovering()+ for (let crash = 0; crash < 3; crash++) gone(page, 'crashed')+ assert.equal(page.reloads, 2)+ assert.deepEqual(reports, ['gave_up'])+})++test('a page ended on purpose is neither reloaded nor reported', () => {+ const { reports, page, gone } = recovering()+ gone(page, 'clean-exit')+ assert.equal(page.reloads, 0)+ assert.deepEqual(reports, [])+})diff --git a/apps/desktop/src/shared/renderer-recovery.ts b/apps/desktop/src/shared/renderer-recovery.tsnew file mode 100644index 00000000..98022648--- /dev/null+++ b/apps/desktop/src/shared/renderer-recovery.ts@@ -0,0 +1,45 @@+import type { WindowRecoveryOutcome } from './analytics.ts'++/** Why a window's page can die and come back by loading again; `clean-exit` and `killed` mean it was ended on purpose. */+const RECOVERABLE: ReadonlySet<string> = new Set(['crashed', 'oom', 'abnormal-exit', 'memory-eviction'])+/** Reloads in a row: a page that crashes again after these stays down instead of looping. */+const MAX_RELOADS = 2+/** A page that stayed up this long since its reload counts as recovered: its next crash starts the count again. */+const RECOVERED_MS = 60_000++/** A window's reloads in a row, and when the last one was. */+export type Reloads = { at: number; count: number }++/** What a page gone for `reason` does: nothing, reload (with its new count), or stay down. */+export function afterPageGone(reason: string, last: Reloads | undefined, now: number): Reloads | 'stay_down' | null {+ if (!RECOVERABLE.has(reason)) return null+ const count = last && now - last.at < RECOVERED_MS ? last.count + 1 : 1+ return count > MAX_RELOADS ? 'stay_down' : { at: now, count }+}++/** The part of a window's web contents recovery uses. */+type Page = { reload(): void; once(event: 'did-finish-load', listener: () => void): unknown }++/** What to do with a page gone for a reason: reload it or let it stay down, reporting one outcome per recovery. */+export function pageRecovery(report: (outcome: WindowRecoveryOutcome) => void) {+ const reloads = new WeakMap<Page, Reloads>()+ const loading = new WeakSet<Page>()+ return (page: Page, reason: string) => {+ const next = afterPageGone(reason, reloads.get(page), Date.now())+ if (next === null) return+ if (next === 'stay_down') {+ report('gave_up')+ return+ }+ reloads.set(page, next)+ // A reload that crashes before it loads leaves this waiting for the next one.+ if (!loading.has(page)) {+ loading.add(page)+ page.once('did-finish-load', () => {+ loading.delete(page)+ report('reloaded')+ })+ }+ page.reload()+ }+}diff --git a/docs/features/analytics.md b/docs/features/analytics.mdindex 56c706c3..beda9c4b 100644--- a/docs/features/analytics.md+++ b/docs/features/analytics.md@@ -104,7 +104,8 @@ builds PostHog frames itself (oldest first, the failing one last): generic `custrendering (mechanism `react`: the chat and pairing windows show *Something went wrong* with*Try again* instead of going blank), and windows or helpers that crashed (`RendererGone`,`GPUGone`, … with Electron's reason), through `reportError`. A window whose page crashed loads- it again (`renderer-recovery.ts`), at most twice in a row before it has stayed up a minute. Not+ it again (`renderer-recovery.ts`), at most twice in a row before it has stayed up a minute, and+ says what came of it (`window_recovery`). Notreported: error events that carry no error (the ResizeObserver loop notice), and helpers thatexited normally or were ended from outside (`clean-exit`, `killed`: logout, shutdown, an update),and a GPU process that couldn't launch (the app goes on with software rendering).diff --git a/docs/reference/analytics-events.md b/docs/reference/analytics-events.mdindex c3ad8b20..a640720b 100644--- a/docs/reference/analytics-events.md+++ b/docs/reference/analytics-events.md@@ -35,6 +35,7 @@ host answers and sends it once the host is up.|---|---|---|---|| `app_opened` | host `track` (desktop, TUI), iOS, Android | Desktop: each launch, once the host answers. TUI: each start. Phones: each return to the foreground. | desktop `at_login` (opened by the login item); phones `notifications` (`allowed`/`denied`/`not_asked`), `widgets` (count on the Home Screen) || `window_opened` | host `track` (desktop) | The chat window is shown after being hidden (tray, dock, shortcut, second launch). A long-running menu bar app opens it many times per launch. | — |+| `window_recovery` | host `track` (desktop) | A window's page crashed (`RendererGone` in error tracking): it loaded again, or crashed too often and stays down | `outcome` (`reloaded`/`gave_up`) || `host_started` | host | Each start of the host process | `first_run` (the first start of a new install; `open_store` marks a new host id with an empty `analytics_version`), `updated_from` (the previous version, when it changed), `service` (started by launchd, systemd or the Windows runner: `XPC_SERVICE_NAME`, `SYSTEMD_EXEC_PID`, `CODYNC_SERVICE`) || `host_setup_failed` | desktop main | The desktop app couldn't get a working host | `stage` (`missing_binary`/`install`/`verify`/`not_responding`/`environment`) || `onboarding_step_viewed` | iOS, Android | An onboarding screen appears. `welcome` is also the sign-in screen, so it's sent again after each sign-out (builds with accounts require sign-in): count persons, or the first one per person, for first runs | `step` (`welcome`/`choose`/`install`/`scan`; `choose`: the account has computers, ask one or pair a new one) |diff --git a/host/src/analytics.rs b/host/src/analytics.rsindex 82a38897..64941623 100644--- a/host/src/analytics.rs+++ b/host/src/analytics.rs@@ -84,6 +84,8 @@ pub enum Event {VoiceCallFinished,/// How one engine of a call fared (time to ready, reconnects, a phase stuck, failure).VoiceCallQuality,+ /// A desktop window's crashed page loaded again, or crashed too often and stays down.+ WindowRecovery,RemoteScreenOpened,ScreenTakenOver,ConnectorInstalled,@@ -154,6 +156,7 @@ impl Event {Self::VoiceCallStarted => "voice_call_started",Self::VoiceCallFinished => "voice_call_finished",Self::VoiceCallQuality => "voice_call_quality",+ Self::WindowRecovery => "window_recovery",Self::RemoteScreenOpened => "remote_screen_opened",Self::ScreenTakenOver => "screen_taken_over",Self::ConnectorInstalled => "connector_installed",diff --git a/host/src/analytics/reported.rs b/host/src/analytics/reported.rsindex 60403cac..67104b9c 100644--- a/host/src/analytics/reported.rs+++ b/host/src/analytics/reported.rs@@ -82,10 +82,23 @@ pub enum Reported {stuck: VoiceStuck,failure: VoiceFailure,},+ WindowRecovery {+ outcome: WindowRecoveryOutcome,+ },}// Value lists. A value a newer client adds arrives as `other` instead of being refused.+/// What came of a desktop window whose page crashed: it loaded again, or crashed too often and stays down.+#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "snake_case")]+pub enum WindowRecoveryOutcome {+ Reloaded,+ GaveUp,+ #[serde(other)]+ Other,+}+#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]#[serde(rename_all = "snake_case")]pub enum Provider {@@ -295,6 +308,8 @@ mod tests {"reconnects": 1, "stuck": "none", "failure": "none"}),Event::VoiceCallQuality,),+ (json!({"event": "window_recovery", "app": "desktop", "outcome": "reloaded"}), Event::WindowRecovery),+ (json!({"event": "window_recovery", "app": "desktop", "outcome": "gave_up"}), Event::WindowRecovery),];for (body, event) in cases {let mut expected = body.clone();