ugly-app 0.1.1067 → 0.1.1069

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/dist/cli/version.d.ts +1 -1
  2. package/dist/cli/version.js +1 -1
  3. package/dist/client/Logger.d.ts.map +1 -1
  4. package/dist/client/Logger.js +3 -0
  5. package/dist/client/Logger.js.map +1 -1
  6. package/dist/client/audio/sttAudioGate.d.ts +21 -0
  7. package/dist/client/audio/sttAudioGate.d.ts.map +1 -0
  8. package/dist/client/audio/sttAudioGate.js +66 -0
  9. package/dist/client/audio/sttAudioGate.js.map +1 -0
  10. package/dist/client/audio/useSTT.d.ts.map +1 -1
  11. package/dist/client/audio/useSTT.js +35 -11
  12. package/dist/client/audio/useSTT.js.map +1 -1
  13. package/dist/client/audio/useSTT.test.d.ts +2 -0
  14. package/dist/client/audio/useSTT.test.d.ts.map +1 -0
  15. package/dist/client/audio/useSTT.test.js +143 -0
  16. package/dist/client/audio/useSTT.test.js.map +1 -0
  17. package/dist/client/auth/silentAuthIframe.d.ts +19 -0
  18. package/dist/client/auth/silentAuthIframe.d.ts.map +1 -0
  19. package/dist/client/auth/silentAuthIframe.js +62 -0
  20. package/dist/client/auth/silentAuthIframe.js.map +1 -0
  21. package/dist/client/bootstrapApp.d.ts.map +1 -1
  22. package/dist/client/bootstrapApp.js +2 -67
  23. package/dist/client/bootstrapApp.js.map +1 -1
  24. package/dist/client/inspect/safearea.d.ts +5 -0
  25. package/dist/client/inspect/safearea.d.ts.map +1 -1
  26. package/dist/client/inspect/safearea.js +47 -4
  27. package/dist/client/inspect/safearea.js.map +1 -1
  28. package/dist/inspect/agent/bundle-entry.js +11 -3
  29. package/dist/inspect/agent/bundle-entry.js.map +1 -1
  30. package/dist/inspect/agent/console.d.ts.map +1 -1
  31. package/dist/inspect/agent/console.js +22 -10
  32. package/dist/inspect/agent/console.js.map +1 -1
  33. package/dist/inspect/agent/guard.d.ts +9 -0
  34. package/dist/inspect/agent/guard.d.ts.map +1 -0
  35. package/dist/inspect/agent/guard.js +56 -0
  36. package/dist/inspect/agent/guard.js.map +1 -0
  37. package/dist/inspect/agent/network.d.ts.map +1 -1
  38. package/dist/inspect/agent/network.js +61 -30
  39. package/dist/inspect/agent/network.js.map +1 -1
  40. package/dist/inspect/agent/perfAgent.d.ts +2 -0
  41. package/dist/inspect/agent/perfAgent.d.ts.map +1 -1
  42. package/dist/inspect/agent/perfAgent.js +103 -88
  43. package/dist/inspect/agent/perfAgent.js.map +1 -1
  44. package/dist/inspect/agent/picker.d.ts.map +1 -1
  45. package/dist/inspect/agent/picker.js +5 -4
  46. package/dist/inspect/agent/picker.js.map +1 -1
  47. package/dist/inspect/agent/profile.d.ts.map +1 -1
  48. package/dist/inspect/agent/profile.js +9 -1
  49. package/dist/inspect/agent/profile.js.map +1 -1
  50. package/dist/inspect/agent/vitals.d.ts.map +1 -1
  51. package/dist/inspect/agent/vitals.js +2 -1
  52. package/dist/inspect/agent/vitals.js.map +1 -1
  53. package/dist/inspect/agent-bundle.js +3 -3
  54. package/dist/inspect/host-bundle.js +9 -9
  55. package/dist/native/server/binaries/catalog.d.ts +4 -3
  56. package/dist/native/server/binaries/catalog.d.ts.map +1 -1
  57. package/dist/native/server/binaries/catalog.js +51 -15
  58. package/dist/native/server/binaries/catalog.js.map +1 -1
  59. package/dist/server/ownerNotify.d.ts +21 -0
  60. package/dist/server/ownerNotify.d.ts.map +1 -1
  61. package/dist/server/ownerNotify.js +45 -18
  62. package/dist/server/ownerNotify.js.map +1 -1
  63. package/package.json +1 -1
  64. package/src/cli/version.ts +1 -1
  65. package/src/client/Logger.ts +3 -0
  66. package/src/client/audio/sttAudioGate.test.ts +75 -0
  67. package/src/client/audio/sttAudioGate.ts +83 -0
  68. package/src/client/audio/useSTT.test.tsx +167 -0
  69. package/src/client/audio/useSTT.ts +43 -11
  70. package/src/client/auth/silentAuthIframe.test.ts +82 -0
  71. package/src/client/auth/silentAuthIframe.ts +62 -0
  72. package/src/client/bootstrapApp.tsx +2 -65
  73. package/src/client/inspect/safearea.ts +52 -3
  74. package/src/inspect/agent/bundle-entry.ts +10 -3
  75. package/src/inspect/agent/console.test.ts +103 -0
  76. package/src/inspect/agent/console.ts +33 -19
  77. package/src/inspect/agent/guard.test.ts +68 -0
  78. package/src/inspect/agent/guard.ts +65 -0
  79. package/src/inspect/agent/network.ts +83 -52
  80. package/src/inspect/agent/perfAgent.test.ts +22 -0
  81. package/src/inspect/agent/perfAgent.ts +136 -112
  82. package/src/inspect/agent/picker.ts +5 -4
  83. package/src/inspect/agent/profile.ts +8 -1
  84. package/src/inspect/agent/vitals.ts +4 -1
  85. package/src/native/server/binaries/catalog.test.ts +118 -0
  86. package/src/native/server/binaries/catalog.ts +54 -17
  87. package/src/server/ownerNotify.test.ts +92 -0
  88. package/src/server/ownerNotify.ts +60 -15
@@ -2,6 +2,7 @@
2
2
  import { useCallback, useEffect, useRef, useState } from 'react';
3
3
  import type { UglyBotSocket } from '../uglyBotSocket.js';
4
4
  import { AudioRecorder } from './AudioRecorder.js';
5
+ import { createSttAudioGate, type SttAudioGate } from './sttAudioGate.js';
5
6
 
6
7
  interface STTOptions {
7
8
  mode?: 'realtime' | 'batch' | 'auto';
@@ -32,6 +33,8 @@ export function useSTT(socket: UglyBotSocket, options: STTOptions = {}) {
32
33
  const cancelledRef = useRef(false);
33
34
  const unsubsRef = useRef<(() => void)[]>([]);
34
35
  const watchdogRef = useRef<ReturnType<typeof setTimeout> | null>(null);
36
+ // Holds this session's recorder chunks until the server owns the stream.
37
+ const gateRef = useRef<SttAudioGate | null>(null);
35
38
 
36
39
  function cleanupListeners() {
37
40
  for (const unsub of unsubsRef.current) unsub();
@@ -62,17 +65,30 @@ export function useSTT(socket: UglyBotSocket, options: STTOptions = {}) {
62
65
  `[STT] start streamId=${streamId} mode=${mode} vad=${enableVAD} lang=${options.lang ?? 'auto'}`,
63
66
  );
64
67
 
65
- // Start mic acquisition in parallel with socket connection for faster startup
68
+ // Start mic acquisition in parallel with socket connection for faster startup.
69
+ // Chunks are gated until stt:ready: the socket may still be connecting (a
70
+ // frame sent then is dropped: "[UglyBotSocket] send DROPPED stt:audio") and
71
+ // the server discards audio for a stream it has not opened yet. Queue in
72
+ // order, flush in order once the server owns the stream, bounded (see gate).
66
73
  if (!recorderRef.current) recorderRef.current = new AudioRecorder();
74
+ gateRef.current?.discard();
75
+ const gate = createSttAudioGate({
76
+ send: (audio) => socket.send('stt:audio', { streamId, audio }),
77
+ onOverflow: (max) =>
78
+ console.warn(
79
+ `[STT] ${max} chunks buffered with no stt:ready streamId=${streamId}: dropping the oldest`,
80
+ ),
81
+ });
82
+ gateRef.current = gate;
67
83
  let chunkCount = 0;
68
84
  const micReady = recorderRef.current.start((audio) => {
69
85
  chunkCount++;
70
86
  if (chunkCount <= 3 || chunkCount % 50 === 0) {
71
87
  console.log(
72
- `[STT] audio chunk #${chunkCount} (${audio.length} b64 chars)`,
88
+ `[STT] audio chunk #${chunkCount} (${audio.length} b64 chars)${gate.opened ? '' : ' buffered until stt:ready'}`,
73
89
  );
74
90
  }
75
- socket.send('stt:audio', { streamId, audio });
91
+ gate.push(audio);
76
92
  });
77
93
 
78
94
  await socket.acquire();
@@ -82,6 +98,7 @@ export function useSTT(socket: UglyBotSocket, options: STTOptions = {}) {
82
98
  // flight. Do not resurrect that cancelled stream by installing listeners
83
99
  // and sending stt:start after acquire() eventually resolves.
84
100
  if (cancelledRef.current || streamIdRef.current !== streamId) {
101
+ gate.discard();
85
102
  recorderRef.current?.stop();
86
103
  socket.release();
87
104
  return;
@@ -94,6 +111,7 @@ export function useSTT(socket: UglyBotSocket, options: STTOptions = {}) {
94
111
  streamIdRef.current = null;
95
112
  cancelledRef.current = true;
96
113
  clearWatchdog();
114
+ gate.discard();
97
115
  recorderRef.current?.stop();
98
116
  setError(message);
99
117
  setListening(false);
@@ -143,14 +161,10 @@ export function useSTT(socket: UglyBotSocket, options: STTOptions = {}) {
143
161
  }),
144
162
  socket.on('stt:error', (data) => {
145
163
  if (data.streamId !== streamId) return;
146
- streamIdRef.current = null;
147
- cancelledRef.current = true;
148
- clearWatchdog();
149
164
  console.error(`[STT] error streamId=${streamId}: ${data.message}`);
150
- setError(data.message);
151
- setListening(false);
152
- cleanupListeners();
153
- socket.release();
165
+ // Same teardown as every other failure: the mic must not stay hot and
166
+ // buffered audio must not chase a stream the server has closed.
167
+ fail(data.message);
154
168
  }),
155
169
  socket.on('stt:stopped', (data) => {
156
170
  if (data.streamId !== streamId) return;
@@ -185,7 +199,12 @@ export function useSTT(socket: UglyBotSocket, options: STTOptions = {}) {
185
199
  const readyStart = Date.now();
186
200
  await new Promise<void>((resolve) => {
187
201
  const poll = setInterval(() => {
188
- if (readyRef.current || Date.now() - readyStart > 5000) {
202
+ // stop()/fail() while waiting: leave now instead of idling out the window.
203
+ if (
204
+ readyRef.current ||
205
+ cancelledRef.current ||
206
+ Date.now() - readyStart > 5000
207
+ ) {
189
208
  clearInterval(poll);
190
209
  resolve();
191
210
  }
@@ -202,9 +221,20 @@ export function useSTT(socket: UglyBotSocket, options: STTOptions = {}) {
202
221
 
203
222
  if (cancelledRef.current) {
204
223
  console.log('[STT] cancelled before recording started');
224
+ gate.discard();
205
225
  return;
206
226
  }
207
227
 
228
+ // The server owns the stream (or we are proceeding without proof): release
229
+ // what the mic captured while we waited, in order, then stream live.
230
+ const buffered = gate.pending;
231
+ gate.open();
232
+ if (buffered > 0) {
233
+ console.log(
234
+ `[STT] flushed ${buffered} buffered chunk(s) streamId=${streamId}`,
235
+ );
236
+ }
237
+
208
238
  await micReady;
209
239
  console.log('[STT] recording started');
210
240
  }, [socket, listening, options.mode, options.lang, options.enableVAD]);
@@ -217,6 +247,7 @@ export function useSTT(socket: UglyBotSocket, options: STTOptions = {}) {
217
247
  streamIdRef.current = null;
218
248
  cancelledRef.current = true;
219
249
  clearWatchdog();
250
+ gateRef.current?.discard();
220
251
  recorderRef.current?.stop();
221
252
  setListening(false);
222
253
  setIsFinal(true);
@@ -230,6 +261,7 @@ export function useSTT(socket: UglyBotSocket, options: STTOptions = {}) {
230
261
  useEffect(() => {
231
262
  return () => {
232
263
  clearWatchdog();
264
+ gateRef.current?.discard();
233
265
  recorderRef.current?.stop();
234
266
  if (streamIdRef.current) {
235
267
  socket.send('stt:stop', { streamId: streamIdRef.current });
@@ -0,0 +1,82 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
2
+
3
+ interface FakeIframe {
4
+ style: Record<string, string>;
5
+ attrs: Record<string, string>;
6
+ credentialless?: boolean;
7
+ src: string;
8
+ setAttribute(k: string, v: string): void;
9
+ remove: ReturnType<typeof vi.fn>;
10
+ }
11
+
12
+ describe('silentAuthViaIframe', () => {
13
+ let listeners: ((e: MessageEvent) => void)[];
14
+ let appended: FakeIframe[];
15
+
16
+ beforeEach(() => {
17
+ vi.resetModules();
18
+ listeners = [];
19
+ appended = [];
20
+ vi.stubGlobal('window', {
21
+ location: { origin: 'https://love.ugly.bot' },
22
+ // The framework default: every app is cross-origin isolated.
23
+ crossOriginIsolated: true,
24
+ addEventListener: (_t: string, fn: (e: MessageEvent) => void) =>
25
+ listeners.push(fn),
26
+ removeEventListener: (_t: string, fn: (e: MessageEvent) => void) => {
27
+ listeners = listeners.filter((l) => l !== fn);
28
+ },
29
+ });
30
+ vi.stubGlobal('document', {
31
+ createElement: (): FakeIframe => {
32
+ const f: FakeIframe = {
33
+ style: {},
34
+ attrs: {},
35
+ src: '',
36
+ setAttribute(k, v) {
37
+ this.attrs[k] = v;
38
+ },
39
+ remove: vi.fn(),
40
+ };
41
+ return f;
42
+ },
43
+ body: { appendChild: (f: FakeIframe) => appended.push(f) },
44
+ });
45
+ });
46
+ afterEach(() => vi.unstubAllGlobals());
47
+
48
+ it('loads the ugly.bot frame WITH credentials on a cross-origin-isolated page', async () => {
49
+ const { silentAuthViaIframe } = await import('./silentAuthIframe');
50
+ const p = silentAuthViaIframe('https://ugly.bot');
51
+ const frame = appended[0]!;
52
+ expect(frame.src).toBe(
53
+ 'https://ugly.bot/oauth/silent?origin=https%3A%2F%2Flove.ugly.bot&mode=iframe',
54
+ );
55
+ // A credentialless frame never carries the ugly.bot cookie, so silent SSO
56
+ // always came back anonymous on COI apps (feedback ce9498b2).
57
+ expect(frame.credentialless).toBeUndefined();
58
+ expect(frame.attrs.credentialless).toBeUndefined();
59
+
60
+ for (const l of listeners)
61
+ l({
62
+ origin: 'https://ugly.bot',
63
+ data: { type: 'ugly-bot-silent-auth', code: 'abc' },
64
+ } as MessageEvent);
65
+ await expect(p).resolves.toBe('abc');
66
+ expect(frame.remove).toHaveBeenCalled();
67
+ });
68
+
69
+ it('ignores messages from other origins and times out to null', async () => {
70
+ vi.useFakeTimers();
71
+ const { silentAuthViaIframe } = await import('./silentAuthIframe');
72
+ const p = silentAuthViaIframe('https://ugly.bot', 100);
73
+ for (const l of listeners)
74
+ l({
75
+ origin: 'https://evil.example',
76
+ data: { type: 'ugly-bot-silent-auth', code: 'x' },
77
+ } as MessageEvent);
78
+ vi.advanceTimersByTime(150);
79
+ await expect(p).resolves.toBeNull();
80
+ vi.useRealTimers();
81
+ });
82
+ });
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Load ugly.bot's silent-auth endpoint in a hidden iframe and resolve with the
3
+ * one-time code it postMessages back (or null if there's no ugly.bot session, or
4
+ * on timeout). This is the SAME mechanism as auto-login (a same-site iframe that
5
+ * reads the live ugly.bot cookie), repurposed so it ALSO serves as the
6
+ * keep-the-token-fresh / account-sync check on every boot.
7
+ *
8
+ * Cross-origin-isolated apps (COEP `require-corp`, the framework default): the
9
+ * iframe must load WITH credentials. ugly.bot's `/oauth/silent?mode=iframe`
10
+ * asserts its own `Cross-Origin-Embedder-Policy: require-corp` +
11
+ * `Cross-Origin-Resource-Policy: cross-origin`, so it embeds under a
12
+ * require-corp parent as an ordinary iframe on every engine. Do NOT mark it
13
+ * `credentialless`: that attribute dates from when the framework stamped COEP
14
+ * `credentialless`, and it strips the ugly.bot cookie, so the endpoint always
15
+ * answered "no session" and no COI app (love, ink, search, fit, ...) could
16
+ * auto-login from an existing ugly.bot session (feedback ce9498b2).
17
+ */
18
+ export function silentAuthViaIframe(
19
+ uglyBotUrl: string,
20
+ timeoutMs = 6000,
21
+ ): Promise<string | null> {
22
+ return new Promise((resolve) => {
23
+ if (typeof document === 'undefined') {
24
+ resolve(null);
25
+ return;
26
+ }
27
+ const origin = window.location.origin;
28
+ let botOrigin: string;
29
+ try {
30
+ botOrigin = new URL(uglyBotUrl).origin;
31
+ } catch {
32
+ resolve(null);
33
+ return;
34
+ }
35
+ const src = `${botOrigin}/oauth/silent?origin=${encodeURIComponent(origin)}&mode=iframe`;
36
+ const iframe = document.createElement('iframe');
37
+ iframe.style.display = 'none';
38
+ iframe.setAttribute('aria-hidden', 'true');
39
+ iframe.setAttribute('tabindex', '-1');
40
+
41
+ let settled = false;
42
+ const finish = (code: string | null): void => {
43
+ if (settled) return;
44
+ settled = true;
45
+ window.removeEventListener('message', onMessage);
46
+ clearTimeout(timer);
47
+ iframe.remove();
48
+ resolve(code);
49
+ };
50
+ const onMessage = (e: MessageEvent): void => {
51
+ if (e.origin !== botOrigin) return;
52
+ const data = e.data as { type?: string; code?: string | null } | null;
53
+ if (data?.type === 'ugly-bot-silent-auth') {
54
+ finish(typeof data.code === 'string' ? data.code : null);
55
+ }
56
+ };
57
+ window.addEventListener('message', onMessage);
58
+ const timer = setTimeout(() => finish(null), timeoutMs);
59
+ iframe.src = src;
60
+ document.body.appendChild(iframe);
61
+ });
62
+ }
@@ -25,6 +25,7 @@ import { type StringsProviderConfig } from './StringsProvider.js';
25
25
  import { installUglyStudioPreviewBridge } from './uglyStudioPreviewBridge.js';
26
26
  import { installFeedbackBridge } from './feedbackBridge.js';
27
27
  import { createUglyBotSocket } from './uglyBotSocket.js';
28
+ import { silentAuthViaIframe } from './auth/silentAuthIframe.js';
28
29
  import { attachDebugStream } from './debugStreamClient.js';
29
30
  import {
30
31
  installProxyClient,
@@ -203,71 +204,7 @@ function markReconciled(userId: string): void {
203
204
  }
204
205
 
205
206
  /**
206
- * Load ugly.bot's silent-auth endpoint in a hidden iframe and resolve with the
207
- * one-time code it postMessages back (or null if there's no ugly.bot session, or
208
- * on timeout). This is the SAME mechanism as auto-login — a same-site iframe that
209
- * reads the live ugly.bot cookie — repurposed here so it ALSO serves as the
210
- * keep-the-token-fresh / account-sync check on every boot.
211
- */
212
- function silentAuthViaIframe(uglyBotUrl: string): Promise<string | null> {
213
- return new Promise((resolve) => {
214
- if (typeof document === 'undefined') {
215
- resolve(null);
216
- return;
217
- }
218
- const origin = window.location.origin;
219
- let botOrigin: string;
220
- try {
221
- botOrigin = new URL(uglyBotUrl).origin;
222
- } catch {
223
- resolve(null);
224
- return;
225
- }
226
- const src = `${botOrigin}/oauth/silent?origin=${encodeURIComponent(origin)}&mode=iframe`;
227
- const iframe = document.createElement('iframe');
228
- iframe.style.display = 'none';
229
- iframe.setAttribute('aria-hidden', 'true');
230
- iframe.setAttribute('tabindex', '-1');
231
- // A cross-origin-isolated page stamps COEP, which otherwise HARD-BLOCKS this
232
- // cross-origin auth iframe (silent SSO) — failing every authed RPC. Marking the
233
- // iframe `credentialless` lets it load under COEP credentialless instead of being
234
- // blocked. It loads without third-party cookies, so silent SSO resolves anonymous
235
- // (fine for public/COI apps) rather than the whole app breaking on a blocked frame.
236
- if (typeof window !== 'undefined' && window.crossOriginIsolated) {
237
- try {
238
- (iframe as unknown as { credentialless: boolean }).credentialless =
239
- true;
240
- } catch {
241
- /* older engines */
242
- }
243
- iframe.setAttribute('credentialless', '');
244
- }
245
-
246
- let settled = false;
247
- const finish = (code: string | null): void => {
248
- if (settled) return;
249
- settled = true;
250
- window.removeEventListener('message', onMessage);
251
- clearTimeout(timer);
252
- iframe.remove();
253
- resolve(code);
254
- };
255
- const onMessage = (e: MessageEvent): void => {
256
- if (e.origin !== botOrigin) return;
257
- const data = e.data as { type?: string; code?: string | null } | null;
258
- if (data?.type === 'ugly-bot-silent-auth') {
259
- finish(typeof data.code === 'string' ? data.code : null);
260
- }
261
- };
262
- window.addEventListener('message', onMessage);
263
- const timer = setTimeout(() => finish(null), 6000);
264
- iframe.src = src;
265
- document.body.appendChild(iframe);
266
- });
267
- }
268
-
269
- /**
270
- * Boot-time silent auth + account-sync via the iframe above. One mechanism, three
207
+ * Boot-time silent auth + account-sync via `silentAuthViaIframe`. One mechanism, three
271
208
  * jobs: (1) logged-out → auto-login if a ugly.bot session exists; (2) logged-in,
272
209
  * same account → redeem a fresh code so the session cookie/token stays valid;
273
210
  * (3) logged-in, DIFFERENT ugly.bot account → reload onto the live account (the
@@ -30,9 +30,7 @@ export function scanSafeArea(state: InspectState): SafeAreaViolation[] {
30
30
  // its actual buttons into the safe region. Audit the children, not the
31
31
  // non-actionable sheet container itself.
32
32
  if (el.hasAttribute('data-safe-area-container')) continue;
33
- const style = window.getComputedStyle(el);
34
- if (style.display === 'none' || style.visibility === 'hidden') continue;
35
- if (parseFloat(style.opacity) === 0) continue;
33
+ if (!isRendered(el)) continue;
36
34
  const r = el.getBoundingClientRect();
37
35
  if (r.width <= 0 || r.height <= 0) continue;
38
36
 
@@ -75,6 +73,57 @@ export function scanSafeArea(state: InspectState): SafeAreaViolation[] {
75
73
  return out;
76
74
  }
77
75
 
76
+ // ─── Visibility ──────────────────────────────────────────────────────────────
77
+ // Only a control the user can see and tap can be "stuck". Chrome keeps the
78
+ // content of a closed <details> in layout (content-visibility, not display:none),
79
+ // so its hidden items still report rects: on duetdrive.co the closed scene
80
+ // picker's buttons (y=598/659, h=61) were flagged as stuck under the home
81
+ // indicator. Judge visibility before judging position.
82
+
83
+ /** Whether `el` is actually rendered for the user: not display:none, hidden,
84
+ * transparent, inside a closed <details>, or otherwise skipped by the engine. */
85
+ export function isRendered(el: Element): boolean {
86
+ const style = window.getComputedStyle(el);
87
+ if (style.display === 'none' || style.visibility === 'hidden') return false;
88
+ if (parseFloat(style.opacity) === 0) return false;
89
+ if (hiddenByClosedDetails(el)) return false;
90
+ // The engine's own answer where it exists (Chrome 105+, Safari 17.4+): it also
91
+ // covers content-visibility and other skipped subtrees this walk cannot see.
92
+ const check = (
93
+ el as Element & {
94
+ checkVisibility?: (opts?: Record<string, boolean>) => boolean;
95
+ }
96
+ ).checkVisibility;
97
+ if (typeof check === 'function') {
98
+ try {
99
+ return check.call(el, {
100
+ contentVisibilityAuto: true,
101
+ visibilityProperty: true,
102
+ opacityProperty: true,
103
+ });
104
+ } catch {
105
+ // an engine with an older signature: the checks above already ran
106
+ }
107
+ }
108
+ return true;
109
+ }
110
+
111
+ /** True when `el` sits inside a closed <details> and is not its <summary>. */
112
+ export function hiddenByClosedDetails(el: Element): boolean {
113
+ let node: Element | null = el;
114
+ while (node) {
115
+ const parent: Element | null = node.parentElement;
116
+ if (
117
+ parent?.tagName === 'DETAILS' &&
118
+ !(parent as HTMLDetailsElement).open &&
119
+ node.tagName !== 'SUMMARY'
120
+ )
121
+ return true;
122
+ node = parent;
123
+ }
124
+ return false;
125
+ }
126
+
78
127
  // ─── Reachability ────────────────────────────────────────────────────────────
79
128
  // An interactive element overlapping the notch/home-indicator is only tappable if
80
129
  // the user can scroll it clear. It's STUCK (unreachable) when it's pinned itself,
@@ -1,8 +1,15 @@
1
1
  // Entry for the pre-bundled, injectable agent IIFE. The ugly-app build esbuilds
2
2
  // this into dist/inspect/agent-bundle.js (a single self-contained script with no
3
- // imports), which native shells read as text and inject at document-start —
4
- // no runtime bundler needed on the host side.
3
+ // imports), which native shells read as text and inject at document-start, so
4
+ // no runtime bundler is needed on the host side.
5
5
 
6
+ import { reportAgentError } from './guard.js';
6
7
  import { installInspectAgent } from './install.js';
7
8
 
8
- installInspectAgent();
9
+ try {
10
+ installInspectAgent();
11
+ } catch (err) {
12
+ // Injected script text is cross-origin to the page: an uncaught throw here
13
+ // would reach the app as a bare "Script error." (see guard.ts).
14
+ reportAgentError('install', err);
15
+ }
@@ -0,0 +1,103 @@
1
+ // @vitest-environment jsdom
2
+ //
3
+ // The agent's window 'error' listener is invoked by the browser, from injected
4
+ // script text. If the host's onEntry callback throws inside it, the throw must
5
+ // be reported with its message, not escape as a second, opaque "Script error.".
6
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
7
+ import { RingBuffer } from '../../client/inspect/buffer.js';
8
+ import type { ConsoleEntry } from '../protocol.js';
9
+ import { installConsoleCapture } from './console.js';
10
+ import { resetAgentErrorReports } from './guard.js';
11
+ import { ObjectRegistry } from './serialize.js';
12
+
13
+ describe('console capture boundaries', () => {
14
+ let stop: () => void = () => {};
15
+ let nativeError: ReturnType<typeof vi.spyOn>;
16
+
17
+ beforeEach(() => {
18
+ resetAgentErrorReports();
19
+ nativeError = vi.spyOn(console, 'error').mockImplementation(() => {});
20
+ });
21
+ afterEach(() => {
22
+ stop();
23
+ nativeError.mockRestore();
24
+ });
25
+
26
+ it('a host onEntry that throws on a window error event is reported, not rethrown', () => {
27
+ stop = installConsoleCapture({
28
+ buffer: new RingBuffer<ConsoleEntry>(10),
29
+ registry: new ObjectRegistry(),
30
+ nodeIdFor: () => 'n1',
31
+ onEntry: (e) => {
32
+ if (e.text === 'page crashed') throw new Error('bridge gone');
33
+ },
34
+ now: () => 0,
35
+ });
36
+
37
+ expect(() =>
38
+ window.dispatchEvent(
39
+ new ErrorEvent('error', { message: 'page crashed' }),
40
+ ),
41
+ ).not.toThrow();
42
+
43
+ const report = nativeError.mock.calls.find((c) =>
44
+ String(c[0]).startsWith('[ugly-inspect-agent]'),
45
+ );
46
+ expect(report?.[0]).toBe(
47
+ '[ugly-inspect-agent] console:error-event threw: bridge gone',
48
+ );
49
+ });
50
+ it('records a censored "Script error." event (no error object, no stack) without throwing', () => {
51
+ // Production: every opaque error event made this listener throw a TypeError
52
+ // (reading `url` of an empty stack), doubling each "Script error.".
53
+ const entries: ConsoleEntry[] = [];
54
+ stop = installConsoleCapture({
55
+ buffer: new RingBuffer<ConsoleEntry>(10),
56
+ registry: new ObjectRegistry(),
57
+ nodeIdFor: () => 'n1',
58
+ onEntry: (e) => entries.push(e),
59
+ now: () => 0,
60
+ });
61
+
62
+ window.dispatchEvent(new ErrorEvent('error', { message: 'Script error.' }));
63
+
64
+ const report = nativeError.mock.calls.find((c) =>
65
+ String(c[0]).startsWith('[ugly-inspect-agent]'),
66
+ );
67
+ expect(report).toBeUndefined();
68
+ const recorded = entries.find((e) => e.text === 'Script error.');
69
+ expect(recorded).toBeDefined();
70
+ expect(recorded?.level).toBe('error');
71
+ expect(recorded?.source).toBeUndefined();
72
+ expect(recorded?.stack).toBeUndefined();
73
+ });
74
+
75
+ it('keeps the browser-reported location for an uncaught error that names a file', () => {
76
+ const entries: ConsoleEntry[] = [];
77
+ stop = installConsoleCapture({
78
+ buffer: new RingBuffer<ConsoleEntry>(10),
79
+ registry: new ObjectRegistry(),
80
+ nodeIdFor: () => 'n1',
81
+ onEntry: (e) => entries.push(e),
82
+ now: () => 0,
83
+ });
84
+ const error = new Error('boom');
85
+ error.stack = 'Error: boom\n at fn (https://app.test/a.js:10:5)';
86
+ window.dispatchEvent(
87
+ new ErrorEvent('error', {
88
+ message: 'Uncaught Error: boom',
89
+ filename: 'https://app.test/a.js',
90
+ lineno: 10,
91
+ colno: 5,
92
+ error,
93
+ }),
94
+ );
95
+ const recorded = entries.find((e) => e.text === 'Uncaught Error: boom');
96
+ expect(recorded?.source).toEqual({
97
+ url: 'https://app.test/a.js',
98
+ line: 10,
99
+ col: 5,
100
+ });
101
+ expect(recorded?.stack?.[0]?.functionName).toBe('fn');
102
+ });
103
+ });
@@ -9,6 +9,7 @@ import type {
9
9
  SourceLoc,
10
10
  StackFrame,
11
11
  } from '../protocol.js';
12
+ import { guarded } from './guard.js';
12
13
  import { type ObjectRegistry, serialize } from './serialize.js';
13
14
 
14
15
  export interface ConsoleCaptureDeps {
@@ -44,22 +45,27 @@ export function installConsoleCapture(deps: ConsoleCaptureDeps): () => void {
44
45
  };
45
46
  }
46
47
 
47
- const onError = (e: ErrorEvent): void => {
48
+ // Listeners the browser invokes directly: a throw here (a host onEntry that
49
+ // fails, say) would itself surface as an opaque window error.
50
+ const onError = guarded('console:error-event', (e: ErrorEvent): void => {
48
51
  record('error', [e.message], {
49
52
  stack: parseStack((e.error as Error | undefined)?.stack),
50
53
  source: { url: e.filename || '', line: e.lineno || 0, col: e.colno || 0 },
51
54
  });
52
- };
53
- const onRejection = (e: PromiseRejectionEvent): void => {
54
- const reason = e.reason as Error | undefined;
55
- record(
56
- 'error',
57
- [`Unhandled rejection: ${reason?.message ?? String(e.reason)}`],
58
- {
59
- stack: parseStack(reason?.stack),
60
- },
61
- );
62
- };
55
+ });
56
+ const onRejection = guarded(
57
+ 'console:unhandledrejection',
58
+ (e: PromiseRejectionEvent): void => {
59
+ const reason = e.reason as Error | undefined;
60
+ record(
61
+ 'error',
62
+ [`Unhandled rejection: ${reason?.message ?? String(e.reason)}`],
63
+ {
64
+ stack: parseStack(reason?.stack),
65
+ },
66
+ );
67
+ },
68
+ );
63
69
  if (typeof window !== 'undefined') {
64
70
  window.addEventListener('error', onError);
65
71
  window.addEventListener('unhandledrejection', onRejection);
@@ -74,6 +80,20 @@ export function installConsoleCapture(deps: ConsoleCaptureDeps): () => void {
74
80
  serialize(a, { registry: deps.registry, nodeIdFor: deps.nodeIdFor }),
75
81
  );
76
82
  const stack = extra?.stack ?? parseStack(new Error().stack, 1);
83
+ // The browser's own location for an uncaught error (filename/lineno/colno)
84
+ // wins when it names a file; otherwise the top captured frame. A censored
85
+ // "Script error." event has neither: no `error` object, so no stack, and an
86
+ // empty filename. The previous form read `stack[0]!.url` whenever a source
87
+ // was passed, so the agent's window 'error' listener threw a TypeError on
88
+ // every such event. That throw came from injected script text and so was
89
+ // itself reported as a second opaque "Script error." (seen in pairs in
90
+ // production).
91
+ const top = stack[0];
92
+ const source = extra?.source?.url
93
+ ? extra.source
94
+ : top
95
+ ? { url: top.url, line: top.line, col: top.col }
96
+ : undefined;
77
97
  const entry: ConsoleEntry = {
78
98
  seq: ++seq,
79
99
  ts: deps.now(),
@@ -81,13 +101,7 @@ export function installConsoleCapture(deps: ConsoleCaptureDeps): () => void {
81
101
  args: serialized,
82
102
  text: renderText(args),
83
103
  stack: stack.length ? stack : undefined,
84
- // Preserve the original precedence exactly: the condition is `extra?.source ?? stack[0]`,
85
- // and when truthy the value is always built from stack[0] (asserted — non-null iff the
86
- // frames array is non-empty, which the condition already implies in practice).
87
- source:
88
- (extra?.source ?? stack[0])
89
- ? { url: stack[0]!.url, line: stack[0]!.line, col: stack[0]!.col }
90
- : undefined,
104
+ source,
91
105
  };
92
106
  deps.buffer.push(entry);
93
107
  deps.onEntry?.(entry);