Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(home): bring the orb back when the browser restores its WebGL context
trohitg3343175ddb4d
diff --git a/client/src/features/home/orb/__tests__/orb.test.ts b/client/src/features/home/orb/__tests__/orb.test.tsindex 694d51b3..af9f1f7e 100644--- a/client/src/features/home/orb/__tests__/orb.test.ts+++ b/client/src/features/home/orb/__tests__/orb.test.ts@@ -2,20 +2,23 @@* The orb's lifecycle (orb.ts): stages and slots are stacks, so a screen* mounted over another borrows the orb and hands it back; the engine loads* once; a failed engine load shows the mark until the next stage tries- * again, while no WebGL, reduced motion and a lost context hold for good.+ * again; a lost WebGL context shows it until the browser hands the context+ * back; no WebGL and reduced motion hold for good.*/import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { act, renderHook } from '@testing-library/react';+type ContextChange = (lost: boolean) => void;+const engine = { attach: vi.fn(), detach: vi.fn(), dispose: vi.fn() };-const createOrbEngine = vi.fn((onLost: () => void) => {- lose = onLost;+const createOrbEngine = vi.fn((onContextChange: ContextChange) => {+ contextChange = onContextChange;return engine;});-let lose: () => void = () => {};+let contextChange: ContextChange = () => {};-vi.mock('../orbEngine', () => ({ createOrbEngine: (onLost: () => void) => createOrbEngine(onLost) }));+vi.mock('../orbEngine', () => ({ createOrbEngine: (onContextChange: ContextChange) => createOrbEngine(onContextChange) }));import { setReducedMotion } from '@/test/waapi';import { mountOrb, orbState, popOrbSlot, pushOrbSlot, resetOrbForTests, unmountOrb, useOrbFallback } from '../orb';@@ -152,15 +155,18 @@ describe('the static mark', () => {expect(result.current).toBe(true);});- it('holds after the WebGL context is lost', async () => {+ it('shows while the browser has the WebGL context, and the same orb comes back with it', async () => {const { result } = renderHook(() => useOrbFallback());- mountOrb(element());+ const home = element();+ mountOrb(home);await loaded();- act(() => lose());- expect(engine.dispose).toHaveBeenCalledTimes(1);+ act(() => contextChange(true));expect(result.current).toBe(true);- mountOrb(element());- await loaded();+ expect(engine.dispose).not.toHaveBeenCalled();++ act(() => contextChange(false));+ expect(result.current).toBe(false);expect(createOrbEngine).toHaveBeenCalledTimes(1);+ expect(engine.attach).toHaveBeenLastCalledWith(home);});});diff --git a/client/src/features/home/orb/orb.ts b/client/src/features/home/orb/orb.tsindex 39e5a4cc..5b84a67f 100644--- a/client/src/features/home/orb/orb.ts+++ b/client/src/features/home/orb/orb.ts@@ -10,10 +10,12 @@* Leaving every stage keeps the engine for the next one; the app disposes* it (App.tsx).*- * Slots show the static mark instead when the orb cannot run. No WebGL,- * reduced motion and a lost WebGL context hold for the session; an engine- * chunk that failed to load shows the mark until the next stage mounts,- * which tries again.+ * Slots show the static mark instead when the orb cannot run. No WebGL and+ * reduced motion hold for the session. While the browser has taken the+ * WebGL context away (after sleep, or a GPU switch), the mark shows until it+ * hands the context back, and the same engine carries on. An engine chunk+ * that failed to load shows the mark until the next stage mounts, which+ * tries again.*/import { useSyncExternalStore } from 'react';@@ -75,7 +77,10 @@ interface OrbEngine {dispose(): void;}-type Fallback = 'none' | 'missing' | 'latched';+/** Why slots show the mark: the engine's chunk did not load (`missing`),+ * the orb cannot run here (`latched`), or the browser has the WebGL+ * context for now (`lost`). */+type Fallback = 'none' | 'missing' | 'latched' | 'lost';let engine: OrbEngine | null = null;let loading = false;@@ -126,11 +131,7 @@ function show(): void {import('./orbEngine').then(({ createOrbEngine }) => {loading = false;- engine = createOrbEngine(() => {- engine?.dispose();- engine = null;- setFallback('latched');- });+ engine = createOrbEngine((lost) => setFallback(lost ? 'lost' : 'none'));setFallback('none');show();})diff --git a/client/src/features/home/orb/orbEngine.ts b/client/src/features/home/orb/orbEngine.tsindex 5c57a7d5..b1fd40ee 100644--- a/client/src/features/home/orb/orbEngine.ts+++ b/client/src/features/home/orb/orbEngine.ts@@ -20,6 +20,12 @@* leaves a 1x1 buffer. Frames stop while the page is hidden or the window* is blurred (lib/pageActivity).*+ * The browser can take the WebGL context away (after sleep, or when a+ * laptop switches GPU) and hand it back. The engine asks for it back and+ * says when it goes and returns (`onContextChange`); meanwhile three draws+ * nothing, and it rebuilds the scene's GPU resources once the context is+ * back.+ ** Changes from the prototype's three r149 for current three:* - colour management off and sRGB output, so colours read as authored;* - lights are physically based now: intensities x PI, and the point@@ -140,18 +146,21 @@ function crescentGeometry(headAngle: number): BufferGeometry {return geometry;}-export function createOrbEngine(onLost: () => void) {+export function createOrbEngine(onContextChange: (lost: boolean) => void) {const R = new WebGLRenderer({ antialias: true, alpha: true });R.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));R.outputColorSpace = SRGBColorSpace;R.toneMapping = ACESFilmicToneMapping;R.toneMappingExposure = 1.1;R.domElement.style.cssText = 'position:absolute;inset:0;width:100%;height:100%';+ // preventDefault asks the browser to hand the context back.const lost = (event: Event) => {event.preventDefault();- onLost();+ onContextChange(true);};+ const restored = () => onContextChange(false);R.domElement.addEventListener('webglcontextlost', lost);+ R.domElement.addEventListener('webglcontextrestored', restored);const scene = new Scene();const cam = new PerspectiveCamera(35, 1, 0.1, 100);@@ -523,6 +532,7 @@ export function createOrbEngine(onLost: () => void) {this.detach();unsubscribe();R.domElement.removeEventListener('webglcontextlost', lost);+ R.domElement.removeEventListener('webglcontextrestored', restored);scene.traverse((object) => {const mesh = object as Mesh;mesh.geometry?.dispose();diff --git a/docs-internal/normal_mode.md b/docs-internal/normal_mode.mdindex d60bbbdf..0747721a 100644--- a/docs-internal/normal_mode.md+++ b/docs-internal/normal_mode.md@@ -197,10 +197,14 @@ colour; in dark the orb keeps colours of its own (a purple-to-cyan ring; pink,yellow and green heads) with white particles and packets; in light it isglossy piano-black under a white rim light. Its glows and particles blendadditively in dark and normally in light (additive glow vanishes on white),-fading through zero at the midpoint of a theme change. Without WebGL, under-reduced motion, or after a lost WebGL context, the slot shows the static mark-for the session; when the engine's chunk fails to load it shows the mark until-the next stage mounts and tries again (`orb/__tests__/orb.test.ts`).+fading through zero at the midpoint of a theme change. Without WebGL or under+reduced motion the slot shows the static mark for the session. When the+browser takes the WebGL context away (after sleep, or when a laptop switches+GPU), the engine asks for it back (`preventDefault` on `webglcontextlost`) and+the slot shows the mark until `webglcontextrestored`; then the same engine+carries on, three.js having rebuilt the scene's GPU resources. When the+engine's chunk fails to load the slot shows the mark until the next stage+mounts and tries again (`orb/__tests__/orb.test.ts`).## The Workspace