an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions
HTML agent-harnessagent-orchestrationagentsai-agentsautonomous-agentsclaude-codecodexcoding-agentsdesktop-appdeveloper-toolselectrongemini-cliharnessharness-engineeringlocal-firstmemorymulti-agentopencodeorchestrationtypescript
fix(office): the floor goes blank forever when Chromium evicts its WebGL context
Chaitanya Giri71324c6c76be
diff --git a/package.json b/package.jsonindex 2ce3431b..16116829 100644--- a/package.json+++ b/package.json@@ -19,7 +19,7 @@"typecheck:node": "tsc --noEmit -p tsconfig.node.json","typecheck:web": "tsc --noEmit -p tsconfig.web.json","typecheck": "npm run typecheck:node && npm run typecheck:web",- "test:focused": "node --test test/codex-remote.test.cjs test/control.test.cjs test/provider-automation.test.cjs test/provider-config.test.cjs test/queue-delivery.test.cjs test/terminal-automation.test.cjs test/terminal-recovery.test.cjs test/roster.test.cjs test/proc-kill.test.cjs",+ "test:focused": "node --test test/codex-remote.test.cjs test/control.test.cjs test/provider-automation.test.cjs test/provider-config.test.cjs test/queue-delivery.test.cjs test/terminal-automation.test.cjs test/terminal-recovery.test.cjs test/roster.test.cjs test/proc-kill.test.cjs test/office-gl-recovery.test.cjs","dist": "npm run build && electron-builder","dist:mac": "npm run build && electron-builder --mac","dist:win": "npm run build && electron-builder --win",diff --git a/src/renderer/src/scene/office/OfficeFloor.tsx b/src/renderer/src/scene/office/OfficeFloor.tsxindex b4163b13..7bd5ce68 100644--- a/src/renderer/src/scene/office/OfficeFloor.tsx+++ b/src/renderer/src/scene/office/OfficeFloor.tsx@@ -1,4 +1,4 @@-import { useEffect, useRef } from 'react';+import { useEffect, useRef, useState } from 'react';import { Application, Container, Graphics, Ticker, Texture } from 'pixi.js';// PixiJS uses new Function() internally, blocked by Electron CSP — this patches it.import 'pixi.js/unsafe-eval';@@ -12,6 +12,7 @@ import { hexToNumber, DEFAULT_CHARACTER } from './cast';import { pickSoloLine, pickExchange, type BreakSpot } from './cafeteriaLines';import { colors } from '@/design/tokens';import { loadTheme, resolveThemeMap, themeTilesetUrls } from './themeLoader';+import { installContextLossRecovery } from './glRecovery';import type { Tile, Facing, ErrandKind, ErrandSpot } from './themeRegistry';// The map, tileset atlases, desk-claim order, errand spots, coffee-economy@@ -159,6 +160,10 @@ export function OfficeFloor() {const hostRef = useRef<HTMLDivElement | null>(null);const appRef = useRef<Application | null>(null);const mountIdRef = useRef(0);+ // Bumped when the WebGL context is evicted; a dep of the effect below, so the+ // whole scene is torn down and rebuilt through the existing mount path rather+ // than through a second, parallel recovery routine.+ const [glGeneration, setGlGeneration] = useState(0);// The active office theme (store mirror of config.officeTheme). Changing it// tears down and rebuilds the whole scene on the new map/cast (see deps below).const officeTheme = useStore((s) => s.officeTheme);@@ -201,6 +206,27 @@ export function OfficeFloor() {while (host.firstChild) host.removeChild(host.firstChild);host.appendChild(app.canvas);+ // This canvas holds the OLDEST WebGL context in the process (it is built at+ // startup), so it is the one Chromium evicts once enough xterm terminals —+ // each of which takes a context via @xterm/addon-webgl — are open. Pixi+ // reports nothing when that happens: the floor just goes blank forever.+ // Rebuild instead. See glRecovery.ts.+ (app as any).__glRecovery = installContextLossRecovery(app.canvas, {+ onRebuild: () => { if (mountIdRef.current === mountId) setGlGeneration((n) => n + 1); },+ onGiveUp: () => {+ if (mountIdRef.current !== mountId) return;+ const note = document.createElement('div');+ note.style.cssText =+ 'position:absolute;inset:0;display:flex;align-items:center;justify-content:center;' ++ 'padding:24px;color:#ffd0b5;font-family:monospace;font-size:13px;text-align:center;white-space:pre-wrap;';+ note.textContent =+ 'The office floor lost its GPU context.\n\n' ++ 'Too many terminals are using the GPU at once.\n' ++ 'Close a few agent terminals, or restart the app, to bring it back.';+ host.appendChild(note);+ }+ });+// Load tilesets in theme order (texture[i] lines up with map tilesets[i]).const tilesetTextures = await Promise.all(themeTilesetUrls(theme).map(loadTexture),@@ -1655,6 +1681,7 @@ export function OfficeFloor() {mountIdRef.current++;const a = appRef.current;if (a) {+ (a as any).__glRecovery?.();(a as any).__resize?.disconnect?.();try { (a as any).__unsub?.(); } catch { /* noop */ }try { (a as any).__offMessage?.(); } catch { /* noop */ }@@ -1664,7 +1691,7 @@ export function OfficeFloor() {appRef.current = null;while (host.firstChild) host.removeChild(host.firstChild);};- }, [officeTheme]);+ }, [officeTheme, glGeneration]);return (<divdiff --git a/src/renderer/src/scene/office/glRecovery.ts b/src/renderer/src/scene/office/glRecovery.tsnew file mode 100644index 00000000..d9957dc3--- /dev/null+++ b/src/renderer/src/scene/office/glRecovery.ts@@ -0,0 +1,78 @@+/**+ * Surviving a lost WebGL context.+ *+ * Chromium caps how many WebGL contexts one renderer process may hold (~16) and,+ * when a new one pushes past the cap, it EVICTS THE OLDEST — logging only+ * "WARNING: Too many active WebGL contexts. Oldest context will be lost."+ *+ * The office floor's context is created at app startup, so it is always the+ * oldest one alive. Every terminal xterm opens takes another context+ * (@xterm/addon-webgl), so on a busy floor — enough agents, or a second window —+ * the office is the first thing evicted. Pixi does not notice: no exception, no+ * rejected promise, no failure banner. The canvas simply stops drawing and the+ * office goes blank until the app is restarted. That is the blank-office bug.+ *+ * xterm already handles this by falling back to its DOM renderer. Pixi has no+ * such fallback, so the scene has to be rebuilt instead. This module is only the+ * event wiring, kept separate from OfficeFloor so the recovery policy — cancel+ * the event, debounce, cap the retries, give up loudly — is testable against a+ * plain EventTarget with no browser, no GPU and no Pixi.+ */++export interface GlRecoveryOptions {+ /** Rebuild attempts before we stop fighting for a context. */+ maxRebuilds?: number;+ /** Wait before rebuilding: the eviction arrives mid-storm (several contexts+ * are usually created at once), and claiming one back immediately just races+ * the terminals that displaced us. */+ delayMs?: number;+ /** Rebuild the scene (in OfficeFloor: bump the effect's generation dep). */+ onRebuild: () => void;+ /** Called once, when the retry budget is spent — the caller should say+ * something visible rather than leaving a silently blank canvas. */+ onGiveUp?: () => void;+ /** Injectable for tests. */+ schedule?: (fn: () => void, ms: number) => unknown;+ log?: (msg: string) => void;+}++export const DEFAULT_MAX_REBUILDS = 3;+export const DEFAULT_REBUILD_DELAY_MS = 1500;++/** Listen for context loss on `canvas` and rebuild. Returns an uninstall fn;+ * call it from the effect cleanup so a torn-down scene stops responding. */+export function installContextLossRecovery(+ canvas: EventTarget,+ opts: GlRecoveryOptions+): () => void {+ const max = opts.maxRebuilds ?? DEFAULT_MAX_REBUILDS;+ const delay = opts.delayMs ?? DEFAULT_REBUILD_DELAY_MS;+ const schedule = opts.schedule ?? ((fn, ms) => setTimeout(fn, ms));+ const log = opts.log ?? ((m: string) => console.warn(m));++ let rebuilds = 0;+ let live = true;++ const onLost = (e: Event) => {+ // WITHOUT preventDefault the browser will never hand the context back — the+ // canvas is dead for good. This one line is the difference between a+ // recoverable scene and a permanently blank one.+ e.preventDefault();+ if (!live) return;+ if (rebuilds >= max) {+ log(`[OfficeFloor] WebGL context lost again after ${max} rebuilds — too many live contexts on this floor; giving up until restart`);+ opts.onGiveUp?.();+ live = false;+ return;+ }+ rebuilds += 1;+ log(`[OfficeFloor] WebGL context lost (Chromium evicted the oldest context) — rebuilding the scene, attempt ${rebuilds}/${max}`);+ schedule(() => { if (live) opts.onRebuild(); }, delay);+ };++ canvas.addEventListener('webglcontextlost', onLost as EventListener, false);+ return () => {+ live = false;+ canvas.removeEventListener('webglcontextlost', onLost as EventListener, false);+ };+}diff --git a/test/office-gl-recovery.test.cjs b/test/office-gl-recovery.test.cjsnew file mode 100644index 00000000..fcafe367--- /dev/null+++ b/test/office-gl-recovery.test.cjs@@ -0,0 +1,129 @@+/**+ * The blank-office bug.+ *+ * Chromium caps live WebGL contexts per renderer process (~16) and evicts the+ * OLDEST when a new one pushes past the cap. The office floor's context is+ * created at app startup, so it is always the oldest; every terminal xterm opens+ * takes another one (@xterm/addon-webgl). On a busy floor the office is evicted,+ * Pixi says nothing at all, and the canvas is blank until the app restarts.+ *+ * CONFIRMED against the shipped v0.3.5 build over CDP: creating 24 extra WebGL+ * contexts produced "WARNING: Too many active WebGL contexts. Oldest context will+ * be lost." and fired `webglcontextlost` on the office canvas, while the terminal+ * logged its own graceful "[terminal] webgl context lost — falling back to DOM+ * renderer". The office had no such fallback. These tests pin the one it has now.+ */+const test = require('node:test');+const assert = require('node:assert');+const load = require('./load-ts.cjs');++const {+ installContextLossRecovery, DEFAULT_MAX_REBUILDS, DEFAULT_REBUILD_DELAY_MS+} = load('src/renderer/src/scene/office/glRecovery.ts');++/** A canvas stand-in: EventTarget is all the recovery code touches. */+function fakeCanvas() { return new EventTarget(); }+function lose(canvas) {+ // cancelable so defaultPrevented actually reports whether preventDefault ran —+ // that call is what allows the browser to hand the context back at all.+ const e = new Event('webglcontextlost', { cancelable: true });+ canvas.dispatchEvent(e);+ return e;+}+/** Collect scheduled callbacks instead of waiting on real timers. */+function fakeClock() {+ const queue = [];+ return {+ schedule: (fn, ms) => { queue.push({ fn, ms }); return queue.length; },+ runAll: () => { const q = queue.splice(0); q.forEach((j) => j.fn()); return q; },+ get pending() { return queue.length; },+ get delays() { return queue.map((j) => j.ms); }+ };+}++test('a lost context rebuilds the scene instead of leaving it blank', () => {+ const canvas = fakeCanvas();+ const clock = fakeClock();+ let rebuilds = 0;+ installContextLossRecovery(canvas, { onRebuild: () => rebuilds++, schedule: clock.schedule, log: () => {} });++ lose(canvas);+ assert.equal(rebuilds, 0, 'rebuild must be deferred, not immediate');+ assert.equal(clock.pending, 1);+ clock.runAll();+ assert.equal(rebuilds, 1, 'the scene never came back');+});++test('preventDefault is called — without it the context is gone for good', () => {+ const canvas = fakeCanvas();+ installContextLossRecovery(canvas, { onRebuild: () => {}, schedule: () => {}, log: () => {} });+ assert.equal(lose(canvas).defaultPrevented, true);+});++test('the rebuild is delayed so it does not race the eviction storm', () => {+ const canvas = fakeCanvas();+ const clock = fakeClock();+ installContextLossRecovery(canvas, { onRebuild: () => {}, schedule: clock.schedule, log: () => {} });+ lose(canvas);+ // Several contexts are usually created at once; claiming one straight back+ // just loses it again to the next terminal in the same burst.+ assert.ok(clock.delays[0] >= 500, `rebuild delay too short: ${clock.delays[0]}`);+ assert.equal(clock.delays[0], DEFAULT_REBUILD_DELAY_MS);+});++test('retries are capped, then it gives up LOUDLY rather than staying blank', () => {+ const canvas = fakeCanvas();+ const clock = fakeClock();+ let rebuilds = 0, gaveUp = 0;+ const logs = [];+ installContextLossRecovery(canvas, {+ onRebuild: () => rebuilds++, onGiveUp: () => gaveUp++,+ schedule: clock.schedule, log: (m) => logs.push(m)+ });++ for (let i = 0; i < DEFAULT_MAX_REBUILDS; i++) { lose(canvas); clock.runAll(); }+ assert.equal(rebuilds, DEFAULT_MAX_REBUILDS);+ assert.equal(gaveUp, 0, 'gave up while it still had budget');++ lose(canvas);+ clock.runAll();+ assert.equal(gaveUp, 1, 'a silently blank canvas is the bug — it must surface');+ assert.equal(rebuilds, DEFAULT_MAX_REBUILDS, 'kept rebuilding past the cap');++ // And it stays given-up: no infinite fight for a context we cannot keep.+ lose(canvas); clock.runAll();+ assert.equal(gaveUp, 1);+ assert.equal(rebuilds, DEFAULT_MAX_REBUILDS);+ assert.ok(logs.some((m) => /giving up/i.test(m)), 'nothing explains the blank floor');+});++test('uninstalling stops recovery — a torn-down scene must not resurrect itself', () => {+ const canvas = fakeCanvas();+ const clock = fakeClock();+ let rebuilds = 0;+ const off = installContextLossRecovery(canvas, { onRebuild: () => rebuilds++, schedule: clock.schedule, log: () => {} });++ // Loss already in flight when the component unmounts: the queued rebuild must+ // not fire against a destroyed Pixi app.+ lose(canvas);+ off();+ clock.runAll();+ assert.equal(rebuilds, 0, 'rebuilt after teardown');++ lose(canvas);+ assert.equal(clock.pending, 0, 'still listening after uninstall');+});++test('recovery survives repeated losses across rebuilds, one budget per install', () => {+ const canvas = fakeCanvas();+ const clock = fakeClock();+ let rebuilds = 0;+ installContextLossRecovery(canvas, { onRebuild: () => rebuilds++, schedule: clock.schedule, log: () => {}, maxRebuilds: 1 });+ lose(canvas); clock.runAll();+ assert.equal(rebuilds, 1);+ // A fresh install (what the next mount does) gets its own budget.+ const canvas2 = fakeCanvas();+ installContextLossRecovery(canvas2, { onRebuild: () => rebuilds++, schedule: clock.schedule, log: () => {}, maxRebuilds: 1 });+ lose(canvas2); clock.runAll();+ assert.equal(rebuilds, 2);+});