From 35e7ac44fcf87dc2bff586a33822d35d0c91d362 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Fri, 13 Nov 2026 22:14:00 -0800 Subject: [PATCH 01/10] feat(apps): let an app page report usage events through Desktop telemetry The Apps rewrite (#2082, #2083) dropped window.comfyDesktopApps, and with it the only way an app page's run_started, example_used and open_in_comfyui events reached PostHog. The pages have sent nothing since. An app page now gets window.comfyDesktopTelemetry (ComfyDesktopHostedTelemetryBridge, bridge types 0.11.0) with one call, track(name, props). It is a separate object, not comfyDesktopApps: the website treats any comfyDesktopApps as the old full bridge. Desktop takes an event only from the hosted Apps page while Apps is on (the same guard as the local bridge), and only run_started, example_used and open_in_comfyui; Desktop already sends app_opened itself. Properties are checked the way the website's track.js checks them: app (catalog id slug) required, where cloud|local, model slug, quality fast|best, seconds 1..86400 rounded, example slug. Anything else is dropped. The event goes out as comfy.desktop.apps. through the main-process capture, which sends nothing unless the person allows telemetry. The app id is sent as app_id, the key Desktop's own app_opened and app_page_failed use, so the events join in PostHog. Like app_opened, these are product events and are not mirrored to Datadog. The validation is ported from the unmerged #2054. packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts was edited by hand to match what bridge-types:gen writes; bridge-types:check in CI confirms it. --- .../comfyui-desktop-bridge-types/README.md | 21 +- .../comfyDesktopBridge.d.ts | 12 ++ src/main/apps/appPageEvents.test.ts | 193 ++++++++++++++++++ src/main/apps/appPageEvents.ts | 75 +++++++ src/main/lib/ipc/index.ts | 2 + .../ipc/registerAppsTelemetryHandlers.test.ts | 143 +++++++++++++ .../lib/ipc/registerAppsTelemetryHandlers.ts | 29 +++ src/preload/appsPreload.ts | 6 + src/types/comfyDesktopBridge.ts | 13 ++ 9 files changed, 488 insertions(+), 6 deletions(-) create mode 100644 src/main/apps/appPageEvents.test.ts create mode 100644 src/main/apps/appPageEvents.ts create mode 100644 src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts create mode 100644 src/main/lib/ipc/registerAppsTelemetryHandlers.ts diff --git a/packages/comfyui-desktop-bridge-types/README.md b/packages/comfyui-desktop-bridge-types/README.md index b90ae42aa..1a099b542 100644 --- a/packages/comfyui-desktop-bridge-types/README.md +++ b/packages/comfyui-desktop-bridge-types/README.md @@ -41,20 +41,23 @@ while omitting part of it. ## Comfy pages hosted in Desktop's Apps view -A Comfy page that Desktop shows in its Apps view gets two objects instead, and -nothing else of the bridge above: `window.comfyDesktopAuth`, Desktop's account -session, and `window.comfyDesktopLocal`, a ComfyUI on this computer. +A Comfy page that Desktop shows in its Apps view gets three objects instead, +and nothing else of the bridge above: `window.comfyDesktopAuth`, Desktop's +account session, `window.comfyDesktopLocal`, a ComfyUI on this computer, and +`window.comfyDesktopTelemetry`, the page's usage events. ```ts import type { ComfyDesktopHostedAuthBridge, - ComfyDesktopHostedLocalBridge + ComfyDesktopHostedLocalBridge, + ComfyDesktopHostedTelemetryBridge } from '@comfyorg/comfyui-desktop-bridge-types' declare global { interface Window { comfyDesktopAuth?: ComfyDesktopHostedAuthBridge comfyDesktopLocal?: ComfyDesktopHostedLocalBridge + comfyDesktopTelemetry?: ComfyDesktopHostedTelemetryBridge } } ``` @@ -83,8 +86,14 @@ the workflow, or `{ opened: false, error }` with the reason in words. Both members are absent on Desktop builds that predate them, so a page checks for them and keeps its file download as the fallback. -Both objects are absent in a browser, so a page keeps its own sign-in and its -own way to reach a local ComfyUI as the fallback. +`track(name, props)` reports what the person does on the page: `run_started`, +`example_used` or `open_in_comfyui`, with the app's catalog id in `app`. +Desktop checks each property, drops anything it does not know, and sends the +event as `comfy.desktop.apps.` only when the person allows telemetry. +Desktop sends `app_opened` itself when it opens the page. + +All three objects are absent in a browser, so a page keeps its own sign-in and +its own way to reach a local ComfyUI as the fallback, and sends no events. ## Provenance diff --git a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts index 84e8d6586..6b0f658d2 100644 --- a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts +++ b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts @@ -305,6 +305,18 @@ export interface ComfyDesktopHostedLocalBridge { /** The person's ComfyUI builds on this computer. Absent on Desktop builds that predate it. */ localBuilds?(): Promise } +/** + * Usage events from a Comfy page hosted in Desktop's Apps view. Desktop + * exposes it to that page as `window.comfyDesktopTelemetry` and sends an event + * as `comfy.desktop.apps.` only when the person allows telemetry. + * Desktop takes `run_started` (`where`, and optionally `model`, `quality` and + * `seconds`), `example_used` (`example`) and `open_in_comfyui` (`where`), each + * with the app's catalog id in `app`. It drops any other event and any other + * property, and sends `app_opened` itself. + */ +export interface ComfyDesktopHostedTelemetryBridge { + track(name: string, props?: Record): void +} export interface ComfyDesktop2TerminalBridge { subscribe(installationId?: string): Promise unsubscribe(installationId?: string): Promise diff --git a/src/main/apps/appPageEvents.test.ts b/src/main/apps/appPageEvents.test.ts new file mode 100644 index 000000000..b19728568 --- /dev/null +++ b/src/main/apps/appPageEvents.test.ts @@ -0,0 +1,193 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import os from 'os' +import path from 'path' + +vi.mock('electron', () => ({ + app: { + getPath: () => path.join(os.tmpdir(), 'launcher-test'), + isPackaged: true, + on: () => {} + }, + BrowserWindow: { getAllWindows: () => [] } +})) + +const sdkCaptures = vi.hoisted( + () => [] as Array<{ event: string; properties?: Record }> +) + +vi.mock('posthog-node', () => ({ + PostHog: class { + on(): () => void { + return () => {} + } + capture(call: { event: string; properties?: Record }): void { + sdkCaptures.push(call) + } + identify(): void {} + flush(): Promise { + return Promise.resolve() + } + shutdown(): Promise { + return Promise.resolve() + } + } +})) + +import * as telemetry from '../lib/telemetry' +import { APP_PAGE_EVENT_PREFIX, checkAppPageEvent, trackAppPageEvent } from './appPageEvents' + +describe('checkAppPageEvent', () => { + it('keeps a run start with every property it knows, under the Apps prefix', () => { + expect( + checkAppPageEvent('run_started', { + app: 'remove-background', + where: 'cloud', + model: 'birefnet', + quality: 'best', + seconds: 12.4 + }) + ).toEqual({ + event: 'comfy.desktop.apps.run_started', + properties: { + app_id: 'remove-background', + where: 'cloud', + model: 'birefnet', + quality: 'best', + seconds: 12 + } + }) + }) + + it('keeps an example use and an open in ComfyUI', () => { + expect(checkAppPageEvent('example_used', { app: 'genjutsu', example: 'dance-1' })).toEqual({ + event: `${APP_PAGE_EVENT_PREFIX}example_used`, + properties: { app_id: 'genjutsu', example: 'dance-1' } + }) + expect(checkAppPageEvent('open_in_comfyui', { app: 'genjutsu', where: 'local' })).toEqual({ + event: `${APP_PAGE_EVENT_PREFIX}open_in_comfyui`, + properties: { app_id: 'genjutsu', where: 'local' } + }) + }) + + it.each([ + ['app_opened', { app: 'genjutsu' }], + ['app_page_failed', { app: 'genjutsu' }], + ['run_finished', { app: 'genjutsu', where: 'cloud' }], + ['toString', { app: 'genjutsu', where: 'cloud' }], + ['', { app: 'genjutsu' }], + [42, { app: 'genjutsu' }] + ])('drops the event %s, which a page may not send', (name, props) => { + expect(checkAppPageEvent(name, props)).toBeNull() + }) + + it('leaves app_opened to Desktop, which sends it when it opens the page', () => { + expect(checkAppPageEvent('app_opened', { app: 'genjutsu' })).toBeNull() + }) + + it.each([ + ['no app', { where: 'cloud' }], + ['an app that is not a slug', { app: 'Remove Background', where: 'cloud' }], + ['an app that is too long', { app: 'a'.repeat(65), where: 'cloud' }], + ['no where', { app: 'genjutsu' }], + ['a where that is neither cloud nor local', { app: 'genjutsu', where: 'mars' }], + ['properties that are not an object', 'genjutsu'], + ['properties that are a list', ['genjutsu', 'cloud']], + ['no properties', undefined] + ])('drops a run start with %s', (_why, props) => { + expect(checkAppPageEvent('run_started', props)).toBeNull() + }) + + it('drops an example use whose example is not a slug', () => { + expect(checkAppPageEvent('example_used', { app: 'genjutsu', example: '../x' })).toBeNull() + }) + + it('drops properties it does not know and bad optional ones, keeping the event', () => { + expect( + checkAppPageEvent('run_started', { + app: 'genjutsu', + where: 'local', + model: 'My Model.safetensors', + quality: 'ultra', + seconds: 86_401, + prompt: 'a cat on the moon', + path: '/Users/someone/Pictures/me.png', + app_id: 'other-app', + example: 'dance-1' + }) + ).toEqual({ + event: 'comfy.desktop.apps.run_started', + properties: { app_id: 'genjutsu', where: 'local' } + }) + }) + + it.each([ + [0, undefined], + [-3, undefined], + [Number.NaN, undefined], + [Number.POSITIVE_INFINITY, undefined], + ['12', undefined], + [0.2, 1], + [59.5, 60], + [86_400, 86_400] + ])('reads seconds %s as %s', (seconds, kept) => { + expect( + checkAppPageEvent('run_started', { app: 'genjutsu', where: 'cloud', seconds })?.properties[ + 'seconds' + ] + ).toBe(kept) + }) +}) + +describe('trackAppPageEvent through Desktop telemetry', () => { + beforeEach(() => { + process.env['POSTHOG_API_KEY'] = 'test-key' + process.env['POSTHOG_ENABLED'] = '1' + telemetry._resetForTest() + telemetry.initTelemetry({ appVersion: '0.0.0', appEnv: 'test', isPackaged: true }) + }) + + afterEach(() => { + telemetry._resetForTest() + sdkCaptures.length = 0 + delete process.env['POSTHOG_API_KEY'] + delete process.env['POSTHOG_ENABLED'] + }) + + function appCaptures(): Array<{ event: string; properties?: Record }> { + return sdkCaptures.filter((c) => c.event.startsWith(APP_PAGE_EVENT_PREFIX)) + } + + it('sends a good event to PostHog when the person allows telemetry', () => { + telemetry.setConsentState('granted') + telemetry.bindAnonymousId('anon-1', 'anon-1', {}) + + expect( + trackAppPageEvent('run_started', { app: 'genjutsu', where: 'cloud', prompt: 'a cat' }) + ).toBe(true) + + const sent = appCaptures() + expect(sent.map((c) => c.event)).toEqual(['comfy.desktop.apps.run_started']) + expect(sent[0]?.properties).toMatchObject({ app_id: 'genjutsu', where: 'cloud' }) + expect(sent[0]?.properties).not.toHaveProperty('prompt') + }) + + it.each(['denied', 'undecided'] as const)('sends nothing while consent is %s', (consent) => { + telemetry.setConsentState(consent) + telemetry.bindAnonymousId('anon-1', 'anon-1', {}) + + expect(trackAppPageEvent('run_started', { app: 'genjutsu', where: 'cloud' })).toBe(false) + expect(trackAppPageEvent('example_used', { app: 'genjutsu', example: 'dance-1' })).toBe(false) + + expect(appCaptures()).toEqual([]) + }) + + it('sends nothing for an event it drops, even with consent', () => { + telemetry.setConsentState('granted') + telemetry.bindAnonymousId('anon-1', 'anon-1', {}) + + expect(trackAppPageEvent('app_opened', { app: 'genjutsu' })).toBe(false) + expect(trackAppPageEvent('mystery', { app: 'genjutsu' })).toBe(false) + + expect(appCaptures()).toEqual([]) + }) +}) diff --git a/src/main/apps/appPageEvents.ts b/src/main/apps/appPageEvents.ts new file mode 100644 index 000000000..d8a4c364e --- /dev/null +++ b/src/main/apps/appPageEvents.ts @@ -0,0 +1,75 @@ +import * as mainTelemetry from '../lib/telemetry' +import type { TelemetryContext } from '../lib/telemetry' + +export const APP_PAGE_EVENT_PREFIX = 'comfy.desktop.apps.' + +const SLUG = /^[a-z0-9][a-z0-9._-]{0,63}$/ +const MAX_SECONDS = 86_400 + +type Read = (value: unknown) => string | number | undefined + +const slug: Read = (value) => (typeof value === 'string' && SLUG.test(value) ? value : undefined) + +const oneOf = + (...allowed: readonly string[]): Read => + (value) => + typeof value === 'string' && allowed.includes(value) ? value : undefined + +const seconds: Read = (value) => + typeof value === 'number' && Number.isFinite(value) && value > 0 && value <= MAX_SECONDS + ? Math.max(1, Math.round(value)) + : undefined + +const READ: Readonly> = { + where: oneOf('cloud', 'local'), + model: slug, + quality: oneOf('fast', 'best'), + seconds, + example: slug +} + +interface PageEvent { + need: readonly string[] + may: readonly string[] +} + +const PAGE_EVENTS: ReadonlyMap = new Map([ + ['run_started', { need: ['where'], may: ['model', 'quality', 'seconds'] }], + ['example_used', { need: ['example'], may: [] }], + ['open_in_comfyui', { need: ['where'], may: [] }] +]) + +function isPlainRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +export interface CheckedPageEvent { + event: string + properties: TelemetryContext +} + +export function checkAppPageEvent(name: unknown, props: unknown): CheckedPageEvent | null { + if (typeof name !== 'string') return null + const spec = PAGE_EVENTS.get(name) + if (!spec) return null + const given = isPlainRecord(props) ? props : {} + const appId = slug(given['app']) + if (appId === undefined) return null + const properties: TelemetryContext = { app_id: appId } + for (const key of [...spec.need, ...spec.may]) { + const raw = given[key] + const value = raw === undefined || raw === null ? undefined : READ[key](raw) + if (value !== undefined) properties[key] = value + else if (spec.need.includes(key)) return null + } + return { event: `${APP_PAGE_EVENT_PREFIX}${name}`, properties } +} + +export function trackAppPageEvent(name: unknown, props: unknown): boolean { + try { + const checked = checkAppPageEvent(name, props) + return checked ? mainTelemetry.capture(checked.event, checked.properties) : false + } catch { + return false + } +} diff --git a/src/main/lib/ipc/index.ts b/src/main/lib/ipc/index.ts index 62f7dcd5b..ea21ede8b 100644 --- a/src/main/lib/ipc/index.ts +++ b/src/main/lib/ipc/index.ts @@ -40,6 +40,7 @@ import { registerEmbeddedSessionHandlers } from './registerEmbeddedSessionHandle import { registerCatalogHandlers } from './registerCatalogHandlers' import { registerAppsHandlers } from './registerAppsHandlers' import { registerAppsLocalHandlers } from './registerAppsLocalHandlers' +import { registerAppsTelemetryHandlers } from './registerAppsTelemetryHandlers' import { reconcileAdoptedSettings } from '../desktopAdopt' import { finalizeComfyBuilderRecovery, @@ -287,5 +288,6 @@ export function register(callbacks: RegisterCallbacks = {}): Promise { registerCatalogHandlers() registerAppsHandlers() registerAppsLocalHandlers() + registerAppsTelemetryHandlers() return startupRecovery.then(() => undefined) } diff --git a/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts new file mode 100644 index 000000000..cd32e35dc --- /dev/null +++ b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts @@ -0,0 +1,143 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +type Listener = (event: unknown, payload: unknown) => void + +const mocks = vi.hoisted(() => ({ + listeners: new Map(), + isAppsPage: true, + appsEnabled: true, + capture: vi.fn(() => true) +})) + +vi.mock('electron', () => ({ + ipcMain: { + on: (channel: string, listener: Listener) => mocks.listeners.set(channel, listener) + } +})) +vi.mock('../../apps/appsConfig', () => ({ areAppsEnabled: async () => mocks.appsEnabled })) +vi.mock('../../apps/appsPages', () => ({ isAppsPageSender: () => mocks.isAppsPage })) +vi.mock('../telemetry', () => ({ capture: mocks.capture })) + +import { + APPS_TELEMETRY_TRACK_CHANNEL, + registerAppsTelemetryHandlers, + trackFromAppsPage +} from './registerAppsTelemetryHandlers' + +const EVENT = { sender: {}, senderFrame: {} } as unknown as Electron.IpcMainEvent +const RUN = { name: 'run_started', props: { app: 'remove-background', where: 'cloud' } } + +beforeEach(() => { + vi.clearAllMocks() + mocks.listeners.clear() + mocks.isAppsPage = true + mocks.appsEnabled = true + registerAppsTelemetryHandlers() +}) + +describe('usage events from the hosted Apps page', () => { + it('listens on the channel the Apps preload sends on', async () => { + const listener = mocks.listeners.get(APPS_TELEMETRY_TRACK_CHANNEL) + expect(APPS_TELEMETRY_TRACK_CHANNEL).toBe('apps-telemetry:track') + expect(listener).toBeTypeOf('function') + + listener?.(EVENT, RUN) + + await vi.waitFor(() => + expect(mocks.capture).toHaveBeenCalledWith('comfy.desktop.apps.run_started', { + app_id: 'remove-background', + where: 'cloud' + }) + ) + }) + + it('sends a good event through Desktop telemetry with the Apps prefix', async () => { + expect( + await trackFromAppsPage(EVENT, { + name: 'run_started', + props: { app: 'genjutsu', where: 'local', model: 'scail-2', quality: 'fast', seconds: 90 } + }) + ).toBe(true) + expect( + await trackFromAppsPage(EVENT, { + name: 'example_used', + props: { app: 'genjutsu', example: 'dance-1' } + }) + ).toBe(true) + expect( + await trackFromAppsPage(EVENT, { + name: 'open_in_comfyui', + props: { app: 'genjutsu', where: 'cloud' } + }) + ).toBe(true) + + expect(mocks.capture.mock.calls).toEqual([ + [ + 'comfy.desktop.apps.run_started', + { app_id: 'genjutsu', where: 'local', model: 'scail-2', quality: 'fast', seconds: 90 } + ], + ['comfy.desktop.apps.example_used', { app_id: 'genjutsu', example: 'dance-1' }], + ['comfy.desktop.apps.open_in_comfyui', { app_id: 'genjutsu', where: 'cloud' }] + ]) + }) + + it.each([ + ['a sender that is not the hosted Apps page', { isAppsPage: false, appsEnabled: true }], + ['the Apps page while Apps is switched off', { isAppsPage: true, appsEnabled: false }] + ])('refuses %s', async (_who, state) => { + Object.assign(mocks, state) + + expect(await trackFromAppsPage(EVENT, RUN)).toBe(false) + + expect(mocks.capture).not.toHaveBeenCalled() + }) + + it.each([ + ['app_opened, which Desktop sends itself', { name: 'app_opened', props: { app: 'genjutsu' } }], + [ + 'an event it does not know', + { name: 'run_finished', props: { app: 'genjutsu', where: 'cloud' } } + ], + ['a payload with no name', { props: { app: 'genjutsu', where: 'cloud' } }], + ['a payload that is not an object', 'run_started'], + ['no payload', undefined] + ])('drops %s', async (_what, payload) => { + expect(await trackFromAppsPage(EVENT, payload)).toBe(false) + + expect(mocks.capture).not.toHaveBeenCalled() + }) + + it('drops bad and unknown properties before sending', async () => { + await trackFromAppsPage(EVENT, { + name: 'run_started', + props: { + app: 'genjutsu', + where: 'cloud', + model: 'Not A Slug', + quality: 'ultra', + seconds: -1, + prompt: 'a cat on the moon', + file: '/Users/someone/me.png' + } + }) + + expect(mocks.capture).toHaveBeenCalledWith('comfy.desktop.apps.run_started', { + app_id: 'genjutsu', + where: 'cloud' + }) + }) + + it('drops an event whose app or required property is bad', async () => { + await trackFromAppsPage(EVENT, { + name: 'run_started', + props: { app: 'Genjutsu!', where: 'cloud' } + }) + await trackFromAppsPage(EVENT, { + name: 'open_in_comfyui', + props: { app: 'genjutsu', where: 'web' } + }) + await trackFromAppsPage(EVENT, { name: 'example_used', props: { app: 'genjutsu' } }) + + expect(mocks.capture).not.toHaveBeenCalled() + }) +}) diff --git a/src/main/lib/ipc/registerAppsTelemetryHandlers.ts b/src/main/lib/ipc/registerAppsTelemetryHandlers.ts new file mode 100644 index 000000000..d0fe78c03 --- /dev/null +++ b/src/main/lib/ipc/registerAppsTelemetryHandlers.ts @@ -0,0 +1,29 @@ +import { ipcMain } from 'electron' +import type { IpcMainEvent } from 'electron' + +import { trackAppPageEvent } from '../../apps/appPageEvents' +import { areAppsEnabled } from '../../apps/appsConfig' +import { isAppsPageSender } from '../../apps/appsPages' + +export const APPS_TELEMETRY_TRACK_CHANNEL = 'apps-telemetry:track' + +interface TrackPayload { + name?: unknown + props?: unknown +} + +async function isFromAppsPage(event: IpcMainEvent): Promise { + return isAppsPageSender(event.sender, event.senderFrame) && (await areAppsEnabled()) +} + +export async function trackFromAppsPage(event: IpcMainEvent, payload: unknown): Promise { + if (!(await isFromAppsPage(event))) return false + const { name, props } = (payload && typeof payload === 'object' ? payload : {}) as TrackPayload + return trackAppPageEvent(name, props) +} + +export function registerAppsTelemetryHandlers(): void { + ipcMain.on(APPS_TELEMETRY_TRACK_CHANNEL, (event, payload: unknown) => { + trackFromAppsPage(event, payload).catch(() => {}) + }) +} diff --git a/src/preload/appsPreload.ts b/src/preload/appsPreload.ts index 4b3de63e9..f3f81c348 100644 --- a/src/preload/appsPreload.ts +++ b/src/preload/appsPreload.ts @@ -4,6 +4,7 @@ import type { ComfyDesktop2AuthState, ComfyDesktopHostedAuthBridge, ComfyDesktopHostedLocalBridge, + ComfyDesktopHostedTelemetryBridge, ComfyDesktopLocalEvent } from '../types/comfyDesktopBridge' @@ -37,5 +38,10 @@ const local: ComfyDesktopHostedLocalBridge = { } } +const telemetry: ComfyDesktopHostedTelemetryBridge = { + track: (name, props) => ipcRenderer.send('apps-telemetry:track', { name, props }) +} + contextBridge.exposeInMainWorld('comfyDesktopAuth', auth) contextBridge.exposeInMainWorld('comfyDesktopLocal', local) +contextBridge.exposeInMainWorld('comfyDesktopTelemetry', telemetry) diff --git a/src/types/comfyDesktopBridge.ts b/src/types/comfyDesktopBridge.ts index 564cf5cdf..3a7d9feb7 100644 --- a/src/types/comfyDesktopBridge.ts +++ b/src/types/comfyDesktopBridge.ts @@ -300,6 +300,19 @@ export interface ComfyDesktopHostedLocalBridge { localBuilds?(): Promise } +/** + * Usage events from a Comfy page hosted in Desktop's Apps view. Desktop + * exposes it to that page as `window.comfyDesktopTelemetry` and sends an event + * as `comfy.desktop.apps.` only when the person allows telemetry. + * Desktop takes `run_started` (`where`, and optionally `model`, `quality` and + * `seconds`), `example_used` (`example`) and `open_in_comfyui` (`where`), each + * with the app's catalog id in `app`. It drops any other event and any other + * property, and sends `app_opened` itself. + */ +export interface ComfyDesktopHostedTelemetryBridge { + track(name: string, props?: Record): void +} + export interface ComfyDesktop2TerminalBridge { subscribe(installationId?: string): Promise unsubscribe(installationId?: string): Promise From dba7c577cc08bfe60471c2efce29ef44645a6d84 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Fri, 13 Nov 2026 22:36:41 -0800 Subject: [PATCH 02/10] feat(apps): take any well-formed page event, with the app id Desktop holds, and record sidebar page opens The Apps page bridge took three named events. A new event on an app page then needed a Desktop release. Desktop now takes any event whose name matches ^[a-z][a-z0-9_]{0,47}$ and sends it as comfy.desktop.apps.. app_opened and app_page_failed are refused: Desktop sends them itself, and a page must not fake them. Each event keeps at most 16 properties, each named ^[a-z][a-z0-9_]{0,31}$ with a boolean, a finite number or a slug of at most 64 characters. Free text, other values and the property names Desktop's telemetry sets itself are dropped. The event still goes through the main-process capture, so consent, the anonymous and signed-in ids, scrubbing and rate limits stay Desktop's. app_id now comes from Desktop: the app it opened in that page's view (appIdOfPage). An app or app_id from the page is ignored, and an event from a view Desktop holds no app for is dropped. The sidebar now records comfy.desktop.sidebar.page_opened with page and from_page for each page the person opens from it (Home, Apps, Assets, Builds), and nothing for a click on the page already shown. It goes through emitTelemetryAction and telemetry:capture, which take any event name. The bridge types .d.ts is still edited by hand to match what bridge-types:gen writes. --- .../comfyui-desktop-bridge-types/README.md | 13 +- .../comfyDesktopBridge.d.ts | 12 +- src/main/apps/appPageEvents.test.ts | 189 ++++++++++-------- src/main/apps/appPageEvents.ts | 85 ++++---- src/main/apps/appPageView.test.ts | 21 ++ src/main/apps/appPageView.ts | 7 + .../ipc/registerAppsTelemetryHandlers.test.ts | 99 +++++---- .../lib/ipc/registerAppsTelemetryHandlers.ts | 5 +- src/renderer/src/panel/PanelApp.test.ts | 25 +++ src/renderer/src/panel/PanelApp.vue | 6 + src/types/comfyDesktopBridge.ts | 12 +- 11 files changed, 274 insertions(+), 200 deletions(-) diff --git a/packages/comfyui-desktop-bridge-types/README.md b/packages/comfyui-desktop-bridge-types/README.md index 1a099b542..ddbadcaf6 100644 --- a/packages/comfyui-desktop-bridge-types/README.md +++ b/packages/comfyui-desktop-bridge-types/README.md @@ -86,11 +86,14 @@ the workflow, or `{ opened: false, error }` with the reason in words. Both members are absent on Desktop builds that predate them, so a page checks for them and keeps its file download as the fallback. -`track(name, props)` reports what the person does on the page: `run_started`, -`example_used` or `open_in_comfyui`, with the app's catalog id in `app`. -Desktop checks each property, drops anything it does not know, and sends the -event as `comfy.desktop.apps.` only when the person allows telemetry. -Desktop sends `app_opened` itself when it opens the page. +`track(name, props)` reports what the person does on the page, such as +`track('click', { target: 'run' })`. Desktop sends it as +`comfy.desktop.apps.` only when the person allows telemetry, and adds +`app_id`, the app Desktop opened in that view. A name is lowercase letters, +digits and `_`, up to 48 characters; `app_opened` and `app_page_failed` are +Desktop's own and are refused. Desktop keeps up to 16 properties, each a +boolean, a finite number or a slug of up to 64 characters, and drops free +text, any other value, and an `app` or `app_id` from the page. All three objects are absent in a browser, so a page keeps its own sign-in and its own way to reach a local ComfyUI as the fallback, and sends no events. diff --git a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts index 6b0f658d2..9ed0b0835 100644 --- a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts +++ b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts @@ -309,13 +309,15 @@ export interface ComfyDesktopHostedLocalBridge { * Usage events from a Comfy page hosted in Desktop's Apps view. Desktop * exposes it to that page as `window.comfyDesktopTelemetry` and sends an event * as `comfy.desktop.apps.` only when the person allows telemetry. - * Desktop takes `run_started` (`where`, and optionally `model`, `quality` and - * `seconds`), `example_used` (`example`) and `open_in_comfyui` (`where`), each - * with the app's catalog id in `app`. It drops any other event and any other - * property, and sends `app_opened` itself. + * `name` matches `^[a-z][a-z0-9_]{0,47}$`; `app_opened` and `app_page_failed` + * are refused because Desktop sends them itself. Desktop keeps at most 16 + * properties, each named `^[a-z][a-z0-9_]{0,31}$` with a boolean, a finite + * number or a slug of at most 64 characters (`^[a-z0-9][a-z0-9._-]{0,63}$`), + * and drops anything else. Desktop sets `app_id` itself, to the app it opened + * in that view; an `app` or `app_id` from the page is ignored. */ export interface ComfyDesktopHostedTelemetryBridge { - track(name: string, props?: Record): void + track(name: string, props?: Record): void } export interface ComfyDesktop2TerminalBridge { subscribe(installationId?: string): Promise diff --git a/src/main/apps/appPageEvents.test.ts b/src/main/apps/appPageEvents.test.ts index b19728568..9db93d85d 100644 --- a/src/main/apps/appPageEvents.test.ts +++ b/src/main/apps/appPageEvents.test.ts @@ -34,107 +34,127 @@ vi.mock('posthog-node', () => ({ })) import * as telemetry from '../lib/telemetry' -import { APP_PAGE_EVENT_PREFIX, checkAppPageEvent, trackAppPageEvent } from './appPageEvents' +import { + APP_PAGE_EVENT_PREFIX, + DESKTOP_APP_EVENTS, + MAX_PAGE_EVENT_PROPERTIES, + checkAppPageEvent, + trackAppPageEvent +} from './appPageEvents' + +const APP = 'remove-background' describe('checkAppPageEvent', () => { - it('keeps a run start with every property it knows, under the Apps prefix', () => { + it('sends a page event under the Apps prefix with the app id Desktop holds', () => { expect( - checkAppPageEvent('run_started', { - app: 'remove-background', - where: 'cloud', - model: 'birefnet', - quality: 'best', - seconds: 12.4 - }) + checkAppPageEvent( + 'run_started', + { where: 'cloud', model: 'birefnet', seconds: 12.4, fast: true }, + APP + ) ).toEqual({ event: 'comfy.desktop.apps.run_started', - properties: { - app_id: 'remove-background', - where: 'cloud', - model: 'birefnet', - quality: 'best', - seconds: 12 - } + properties: { where: 'cloud', model: 'birefnet', seconds: 12.4, fast: true, app_id: APP } }) }) - it('keeps an example use and an open in ComfyUI', () => { - expect(checkAppPageEvent('example_used', { app: 'genjutsu', example: 'dance-1' })).toEqual({ - event: `${APP_PAGE_EVENT_PREFIX}example_used`, - properties: { app_id: 'genjutsu', example: 'dance-1' } - }) - expect(checkAppPageEvent('open_in_comfyui', { app: 'genjutsu', where: 'local' })).toEqual({ - event: `${APP_PAGE_EVENT_PREFIX}open_in_comfyui`, - properties: { app_id: 'genjutsu', where: 'local' } - }) - }) + it.each(['click', 'run_started', 'example_used', 'a', `a${'b'.repeat(47)}`, 'step_2_done'])( + 'accepts the event name %s', + (name) => { + expect(checkAppPageEvent(name, {}, APP)?.event).toBe(`${APP_PAGE_EVENT_PREFIX}${name}`) + } + ) it.each([ - ['app_opened', { app: 'genjutsu' }], - ['app_page_failed', { app: 'genjutsu' }], - ['run_finished', { app: 'genjutsu', where: 'cloud' }], - ['toString', { app: 'genjutsu', where: 'cloud' }], - ['', { app: 'genjutsu' }], - [42, { app: 'genjutsu' }] - ])('drops the event %s, which a page may not send', (name, props) => { - expect(checkAppPageEvent(name, props)).toBeNull() + ['too long', `a${'b'.repeat(48)}`], + ['starting with a digit', '2fast'], + ['starting with an underscore', '_hidden'], + ['with capitals', 'RunStarted'], + ['with a dot', 'apps.run_started'], + ['with a dash', 'run-started'], + ['with a space', 'run started'], + ['empty', ''], + ['not a string', 42] + ])('refuses an event name %s', (_why, name) => { + expect(checkAppPageEvent(name, {}, APP)).toBeNull() }) - it('leaves app_opened to Desktop, which sends it when it opens the page', () => { - expect(checkAppPageEvent('app_opened', { app: 'genjutsu' })).toBeNull() + it.each([...DESKTOP_APP_EVENTS])('refuses %s, which only Desktop sends', (name) => { + expect(['app_opened', 'app_page_failed']).toContain(name) + expect(checkAppPageEvent(name, {}, APP)).toBeNull() }) - it.each([ - ['no app', { where: 'cloud' }], - ['an app that is not a slug', { app: 'Remove Background', where: 'cloud' }], - ['an app that is too long', { app: 'a'.repeat(65), where: 'cloud' }], - ['no where', { app: 'genjutsu' }], - ['a where that is neither cloud nor local', { app: 'genjutsu', where: 'mars' }], - ['properties that are not an object', 'genjutsu'], - ['properties that are a list', ['genjutsu', 'cloud']], - ['no properties', undefined] - ])('drops a run start with %s', (_why, props) => { - expect(checkAppPageEvent('run_started', props)).toBeNull() + it('takes app_id from Desktop and ignores the app the page names', () => { + expect( + checkAppPageEvent('click', { app: 'other-app', app_id: 'other-app', target: 'run' }, APP) + ).toEqual({ + event: 'comfy.desktop.apps.click', + properties: { target: 'run', app_id: APP } + }) }) - it('drops an example use whose example is not a slug', () => { - expect(checkAppPageEvent('example_used', { app: 'genjutsu', example: '../x' })).toBeNull() + it.each([ + ['no app id', null], + ['an app id that is not a slug', 'Remove Background'] + ])('drops the event when Desktop holds %s for the page', (_why, appId) => { + expect(checkAppPageEvent('click', { target: 'run' }, appId)).toBeNull() }) - it('drops properties it does not know and bad optional ones, keeping the event', () => { + it('drops free text, bad values and bad property names, keeping the event', () => { expect( - checkAppPageEvent('run_started', { - app: 'genjutsu', - where: 'local', - model: 'My Model.safetensors', - quality: 'ultra', - seconds: 86_401, - prompt: 'a cat on the moon', - path: '/Users/someone/Pictures/me.png', - app_id: 'other-app', - example: 'dance-1' - }) + checkAppPageEvent( + 'run_started', + { + where: 'local', + prompt: 'a cat on the moon', + file: '/Users/someone/Pictures/me.png', + name: 'My Model.safetensors', + long: 'a'.repeat(65), + nan: Number.NaN, + forever: Number.POSITIVE_INFINITY, + nothing: null, + list: ['a'], + nested: { a: 1 }, + Capital: 'x', + '1st': 'x', + [`a${'b'.repeat(32)}`]: 'x', + $set: 'x', + app_version: '9.9.9', + installation_id: 'fake', + ok: false, + count: 0 + }, + APP + ) ).toEqual({ event: 'comfy.desktop.apps.run_started', - properties: { app_id: 'genjutsu', where: 'local' } + properties: { where: 'local', ok: false, count: 0, app_id: APP } + }) + }) + + it('keeps a 64-character slug and a 32-character property name', () => { + const key = `a${'b'.repeat(31)}` + const value = `a${'b'.repeat(63)}` + expect(checkAppPageEvent('click', { [key]: value }, APP)?.properties).toEqual({ + [key]: value, + app_id: APP }) }) + it(`keeps at most ${MAX_PAGE_EVENT_PROPERTIES} properties besides app_id`, () => { + const many = Object.fromEntries(Array.from({ length: 20 }, (_, i) => [`p${i}`, i])) + const kept = checkAppPageEvent('click', many, APP)?.properties ?? {} + expect(Object.keys(kept)).toHaveLength(MAX_PAGE_EVENT_PROPERTIES + 1) + expect(kept).toMatchObject({ p0: 0, p15: 15, app_id: APP }) + expect(kept).not.toHaveProperty('p16') + }) + it.each([ - [0, undefined], - [-3, undefined], - [Number.NaN, undefined], - [Number.POSITIVE_INFINITY, undefined], - ['12', undefined], - [0.2, 1], - [59.5, 60], - [86_400, 86_400] - ])('reads seconds %s as %s', (seconds, kept) => { - expect( - checkAppPageEvent('run_started', { app: 'genjutsu', where: 'cloud', seconds })?.properties[ - 'seconds' - ] - ).toBe(kept) + ['not an object', 'run'], + ['a list', ['run']], + ['missing', undefined] + ])('sends only app_id when the properties are %s', (_why, props) => { + expect(checkAppPageEvent('click', props, APP)?.properties).toEqual({ app_id: APP }) }) }) @@ -161,13 +181,11 @@ describe('trackAppPageEvent through Desktop telemetry', () => { telemetry.setConsentState('granted') telemetry.bindAnonymousId('anon-1', 'anon-1', {}) - expect( - trackAppPageEvent('run_started', { app: 'genjutsu', where: 'cloud', prompt: 'a cat' }) - ).toBe(true) + expect(trackAppPageEvent('run_started', { where: 'cloud', prompt: 'a cat' }, APP)).toBe(true) const sent = appCaptures() expect(sent.map((c) => c.event)).toEqual(['comfy.desktop.apps.run_started']) - expect(sent[0]?.properties).toMatchObject({ app_id: 'genjutsu', where: 'cloud' }) + expect(sent[0]?.properties).toMatchObject({ app_id: APP, where: 'cloud' }) expect(sent[0]?.properties).not.toHaveProperty('prompt') }) @@ -175,18 +193,19 @@ describe('trackAppPageEvent through Desktop telemetry', () => { telemetry.setConsentState(consent) telemetry.bindAnonymousId('anon-1', 'anon-1', {}) - expect(trackAppPageEvent('run_started', { app: 'genjutsu', where: 'cloud' })).toBe(false) - expect(trackAppPageEvent('example_used', { app: 'genjutsu', example: 'dance-1' })).toBe(false) + expect(trackAppPageEvent('run_started', { where: 'cloud' }, APP)).toBe(false) + expect(trackAppPageEvent('click', { target: 'run' }, APP)).toBe(false) expect(appCaptures()).toEqual([]) }) - it('sends nothing for an event it drops, even with consent', () => { + it('sends nothing for an event it refuses, even with consent', () => { telemetry.setConsentState('granted') telemetry.bindAnonymousId('anon-1', 'anon-1', {}) - expect(trackAppPageEvent('app_opened', { app: 'genjutsu' })).toBe(false) - expect(trackAppPageEvent('mystery', { app: 'genjutsu' })).toBe(false) + expect(trackAppPageEvent('app_opened', {}, APP)).toBe(false) + expect(trackAppPageEvent('Not An Event', {}, APP)).toBe(false) + expect(trackAppPageEvent('click', {}, null)).toBe(false) expect(appCaptures()).toEqual([]) }) diff --git a/src/main/apps/appPageEvents.ts b/src/main/apps/appPageEvents.ts index d8a4c364e..b9a41fa2e 100644 --- a/src/main/apps/appPageEvents.ts +++ b/src/main/apps/appPageEvents.ts @@ -2,45 +2,34 @@ import * as mainTelemetry from '../lib/telemetry' import type { TelemetryContext } from '../lib/telemetry' export const APP_PAGE_EVENT_PREFIX = 'comfy.desktop.apps.' +export const MAX_PAGE_EVENT_PROPERTIES = 16 +const EVENT_NAME = /^[a-z][a-z0-9_]{0,47}$/ +const PROPERTY_NAME = /^[a-z][a-z0-9_]{0,31}$/ const SLUG = /^[a-z0-9][a-z0-9._-]{0,63}$/ -const MAX_SECONDS = 86_400 -type Read = (value: unknown) => string | number | undefined +export const DESKTOP_APP_EVENTS: ReadonlySet = new Set(['app_opened', 'app_page_failed']) +const DESKTOP_PROPERTIES: ReadonlySet = new Set(['app', 'app_id']) -const slug: Read = (value) => (typeof value === 'string' && SLUG.test(value) ? value : undefined) +type PropertyValue = boolean | number | string -const oneOf = - (...allowed: readonly string[]): Read => - (value) => - typeof value === 'string' && allowed.includes(value) ? value : undefined - -const seconds: Read = (value) => - typeof value === 'number' && Number.isFinite(value) && value > 0 && value <= MAX_SECONDS - ? Math.max(1, Math.round(value)) - : undefined - -const READ: Readonly> = { - where: oneOf('cloud', 'local'), - model: slug, - quality: oneOf('fast', 'best'), - seconds, - example: slug +function isPlainRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) } -interface PageEvent { - need: readonly string[] - may: readonly string[] +function propertyValue(value: unknown): PropertyValue | undefined { + if (typeof value === 'boolean') return value + if (typeof value === 'number') return Number.isFinite(value) ? value : undefined + if (typeof value === 'string') return SLUG.test(value) ? value : undefined + return undefined } -const PAGE_EVENTS: ReadonlyMap = new Map([ - ['run_started', { need: ['where'], may: ['model', 'quality', 'seconds'] }], - ['example_used', { need: ['example'], may: [] }], - ['open_in_comfyui', { need: ['where'], may: [] }] -]) - -function isPlainRecord(value: unknown): value is Record { - return typeof value === 'object' && value !== null && !Array.isArray(value) +function isPageProperty(key: string): boolean { + return ( + PROPERTY_NAME.test(key) && + !DESKTOP_PROPERTIES.has(key) && + !mainTelemetry.DEFAULT_EVENT_PROPERTY_NAMES.has(key) + ) } export interface CheckedPageEvent { @@ -48,26 +37,32 @@ export interface CheckedPageEvent { properties: TelemetryContext } -export function checkAppPageEvent(name: unknown, props: unknown): CheckedPageEvent | null { - if (typeof name !== 'string') return null - const spec = PAGE_EVENTS.get(name) - if (!spec) return null - const given = isPlainRecord(props) ? props : {} - const appId = slug(given['app']) - if (appId === undefined) return null - const properties: TelemetryContext = { app_id: appId } - for (const key of [...spec.need, ...spec.may]) { - const raw = given[key] - const value = raw === undefined || raw === null ? undefined : READ[key](raw) - if (value !== undefined) properties[key] = value - else if (spec.need.includes(key)) return null +export function checkAppPageEvent( + name: unknown, + props: unknown, + appId: string | null +): CheckedPageEvent | null { + if (typeof name !== 'string' || !EVENT_NAME.test(name) || DESKTOP_APP_EVENTS.has(name)) { + return null + } + if (appId === null || !SLUG.test(appId)) return null + const properties: TelemetryContext = {} + let kept = 0 + for (const [key, raw] of Object.entries(isPlainRecord(props) ? props : {})) { + if (kept === MAX_PAGE_EVENT_PROPERTIES) break + if (!isPageProperty(key)) continue + const value = propertyValue(raw) + if (value === undefined) continue + properties[key] = value + kept++ } + properties['app_id'] = appId return { event: `${APP_PAGE_EVENT_PREFIX}${name}`, properties } } -export function trackAppPageEvent(name: unknown, props: unknown): boolean { +export function trackAppPageEvent(name: unknown, props: unknown, appId: string | null): boolean { try { - const checked = checkAppPageEvent(name, props) + const checked = checkAppPageEvent(name, props, appId) return checked ? mainTelemetry.capture(checked.event, checked.properties) : false } catch { return false diff --git a/src/main/apps/appPageView.test.ts b/src/main/apps/appPageView.test.ts index 1c094fad9..e2b467a76 100644 --- a/src/main/apps/appPageView.test.ts +++ b/src/main/apps/appPageView.test.ts @@ -61,6 +61,7 @@ import { APP_PAGE_FAILED_EVENT, APP_PAGE_STATUS_CHANNEL, _resetForTest, + appIdOfPage, captureAppPage, hideAppPage, showAppPage @@ -751,3 +752,23 @@ describe('isAppsPageSender', () => { expect(isAppsPageSender(page, null)).toBe(false) }) }) + +describe('the app a hosted page shows', () => { + it('is the app Desktop opened in that view, and follows it to the next app', async () => { + expect(appIdOfPage(panelContents())).toBeNull() + + await openCutout() + const page = hostedPage().webContents as unknown as WebContents + expect(appIdOfPage(page)).toBe(CUTOUT) + + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + expect(appIdOfPage(page)).toBe(UPSCALE) + }) + + it('is no app for contents Desktop does not host', async () => { + await openCutout() + + expect(appIdOfPage(panelContents())).toBeNull() + expect(appIdOfPage(new FakeContents() as unknown as WebContents)).toBeNull() + }) +}) diff --git a/src/main/apps/appPageView.ts b/src/main/apps/appPageView.ts index a41b6d98d..67ed09bcf 100644 --- a/src/main/apps/appPageView.ts +++ b/src/main/apps/appPageView.ts @@ -277,6 +277,13 @@ function concealWhenPanelGoes(windowKey: number, panel: WebContents): void { }) } +export function appIdOfPage(contents: WebContents): string | null { + for (const hosted of hostedByWindow.values()) { + if (hosted.view.webContents === contents) return hosted.appId + } + return null +} + export async function showAppPage( panel: WebContents, appId: unknown, diff --git a/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts index cd32e35dc..66460b47b 100644 --- a/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts +++ b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts @@ -6,7 +6,8 @@ const mocks = vi.hoisted(() => ({ listeners: new Map(), isAppsPage: true, appsEnabled: true, - capture: vi.fn(() => true) + pageAppId: 'remove-background' as string | null, + capture: vi.fn<(event: string, properties: unknown) => boolean>(() => true) })) vi.mock('electron', () => ({ @@ -16,7 +17,11 @@ vi.mock('electron', () => ({ })) vi.mock('../../apps/appsConfig', () => ({ areAppsEnabled: async () => mocks.appsEnabled })) vi.mock('../../apps/appsPages', () => ({ isAppsPageSender: () => mocks.isAppsPage })) -vi.mock('../telemetry', () => ({ capture: mocks.capture })) +vi.mock('../../apps/appPageView', () => ({ appIdOfPage: () => mocks.pageAppId })) +vi.mock('../telemetry', () => ({ + capture: mocks.capture, + DEFAULT_EVENT_PROPERTY_NAMES: new Set(['app_version', 'installation_id']) +})) import { APPS_TELEMETRY_TRACK_CHANNEL, @@ -25,13 +30,14 @@ import { } from './registerAppsTelemetryHandlers' const EVENT = { sender: {}, senderFrame: {} } as unknown as Electron.IpcMainEvent -const RUN = { name: 'run_started', props: { app: 'remove-background', where: 'cloud' } } +const RUN = { name: 'run_started', props: { where: 'cloud' } } beforeEach(() => { vi.clearAllMocks() mocks.listeners.clear() mocks.isAppsPage = true mocks.appsEnabled = true + mocks.pageAppId = 'remove-background' registerAppsTelemetryHandlers() }) @@ -45,45 +51,48 @@ describe('usage events from the hosted Apps page', () => { await vi.waitFor(() => expect(mocks.capture).toHaveBeenCalledWith('comfy.desktop.apps.run_started', { - app_id: 'remove-background', - where: 'cloud' + where: 'cloud', + app_id: 'remove-background' }) ) }) - it('sends a good event through Desktop telemetry with the Apps prefix', async () => { - expect( - await trackFromAppsPage(EVENT, { - name: 'run_started', - props: { app: 'genjutsu', where: 'local', model: 'scail-2', quality: 'fast', seconds: 90 } - }) - ).toBe(true) + it('sends any well-formed event through Desktop telemetry with the Apps prefix', async () => { + expect(await trackFromAppsPage(EVENT, { name: 'click', props: { target: 'run' } })).toBe(true) expect( await trackFromAppsPage(EVENT, { name: 'example_used', - props: { app: 'genjutsu', example: 'dance-1' } - }) - ).toBe(true) - expect( - await trackFromAppsPage(EVENT, { - name: 'open_in_comfyui', - props: { app: 'genjutsu', where: 'cloud' } + props: { example: 'dance-1', first: true, index: 2 } }) ).toBe(true) expect(mocks.capture.mock.calls).toEqual([ + ['comfy.desktop.apps.click', { target: 'run', app_id: 'remove-background' }], [ - 'comfy.desktop.apps.run_started', - { app_id: 'genjutsu', where: 'local', model: 'scail-2', quality: 'fast', seconds: 90 } - ], - ['comfy.desktop.apps.example_used', { app_id: 'genjutsu', example: 'dance-1' }], - ['comfy.desktop.apps.open_in_comfyui', { app_id: 'genjutsu', where: 'cloud' }] + 'comfy.desktop.apps.example_used', + { example: 'dance-1', first: true, index: 2, app_id: 'remove-background' } + ] ]) }) + it('fills app_id from the app Desktop opened in that view, not from the page', async () => { + mocks.pageAppId = 'upscale' + + await trackFromAppsPage(EVENT, { + name: 'click', + props: { app: 'remove-background', app_id: 'remove-background', target: 'run' } + }) + + expect(mocks.capture).toHaveBeenCalledWith('comfy.desktop.apps.click', { + target: 'run', + app_id: 'upscale' + }) + }) + it.each([ - ['a sender that is not the hosted Apps page', { isAppsPage: false, appsEnabled: true }], - ['the Apps page while Apps is switched off', { isAppsPage: true, appsEnabled: false }] + ['a sender that is not the hosted Apps page', { isAppsPage: false }], + ['the Apps page while Apps is switched off', { appsEnabled: false }], + ['a page Desktop holds no app for', { pageAppId: null }] ])('refuses %s', async (_who, state) => { Object.assign(mocks, state) @@ -93,12 +102,10 @@ describe('usage events from the hosted Apps page', () => { }) it.each([ - ['app_opened, which Desktop sends itself', { name: 'app_opened', props: { app: 'genjutsu' } }], - [ - 'an event it does not know', - { name: 'run_finished', props: { app: 'genjutsu', where: 'cloud' } } - ], - ['a payload with no name', { props: { app: 'genjutsu', where: 'cloud' } }], + ['app_opened, which Desktop sends itself', { name: 'app_opened', props: {} }], + ['app_page_failed, which Desktop sends itself', { name: 'app_page_failed', props: {} }], + ['a name outside the pattern', { name: 'Run Started', props: {} }], + ['a payload with no name', { props: { where: 'cloud' } }], ['a payload that is not an object', 'run_started'], ['no payload', undefined] ])('drops %s', async (_what, payload) => { @@ -107,37 +114,21 @@ describe('usage events from the hosted Apps page', () => { expect(mocks.capture).not.toHaveBeenCalled() }) - it('drops bad and unknown properties before sending', async () => { + it('drops free text and properties Desktop sets itself before sending', async () => { await trackFromAppsPage(EVENT, { name: 'run_started', props: { - app: 'genjutsu', where: 'cloud', - model: 'Not A Slug', - quality: 'ultra', - seconds: -1, prompt: 'a cat on the moon', - file: '/Users/someone/me.png' + file: '/Users/someone/me.png', + app_version: '9.9.9', + installation_id: 'fake' } }) expect(mocks.capture).toHaveBeenCalledWith('comfy.desktop.apps.run_started', { - app_id: 'genjutsu', - where: 'cloud' + where: 'cloud', + app_id: 'remove-background' }) }) - - it('drops an event whose app or required property is bad', async () => { - await trackFromAppsPage(EVENT, { - name: 'run_started', - props: { app: 'Genjutsu!', where: 'cloud' } - }) - await trackFromAppsPage(EVENT, { - name: 'open_in_comfyui', - props: { app: 'genjutsu', where: 'web' } - }) - await trackFromAppsPage(EVENT, { name: 'example_used', props: { app: 'genjutsu' } }) - - expect(mocks.capture).not.toHaveBeenCalled() - }) }) diff --git a/src/main/lib/ipc/registerAppsTelemetryHandlers.ts b/src/main/lib/ipc/registerAppsTelemetryHandlers.ts index d0fe78c03..fbe2c10c9 100644 --- a/src/main/lib/ipc/registerAppsTelemetryHandlers.ts +++ b/src/main/lib/ipc/registerAppsTelemetryHandlers.ts @@ -2,6 +2,7 @@ import { ipcMain } from 'electron' import type { IpcMainEvent } from 'electron' import { trackAppPageEvent } from '../../apps/appPageEvents' +import { appIdOfPage } from '../../apps/appPageView' import { areAppsEnabled } from '../../apps/appsConfig' import { isAppsPageSender } from '../../apps/appsPages' @@ -18,8 +19,10 @@ async function isFromAppsPage(event: IpcMainEvent): Promise { export async function trackFromAppsPage(event: IpcMainEvent, payload: unknown): Promise { if (!(await isFromAppsPage(event))) return false + const appId = appIdOfPage(event.sender) + if (appId === null) return false const { name, props } = (payload && typeof payload === 'object' ? payload : {}) as TrackPayload - return trackAppPageEvent(name, props) + return trackAppPageEvent(name, props, appId) } export function registerAppsTelemetryHandlers(): void { diff --git a/src/renderer/src/panel/PanelApp.test.ts b/src/renderer/src/panel/PanelApp.test.ts index f7fc00b63..ff3d9d08b 100644 --- a/src/renderer/src/panel/PanelApp.test.ts +++ b/src/renderer/src/panel/PanelApp.test.ts @@ -62,6 +62,9 @@ vi.mock('../views/ChooserView.vue', () => ({ '
' } })) +vi.mock('./AssetsView.vue', () => ({ + default: { name: 'AssetsView', template: '
' } +})) vi.mock('./BuildDetailsView.vue', () => ({ default: { name: 'BuildDetailsView', @@ -1570,6 +1573,28 @@ describe('PanelApp', () => { ) }) + it('records each sidebar page the person opens, and not a click on the page already shown', async () => { + window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') + vi.mocked(window.api.getAppsEnabled).mockResolvedValue(true) + const wrapper = mountPanel() + await flushPromises() + const events = captureTelemetry() + const opened = () => + events.filter(({ actionName }) => actionName === 'comfy.desktop.sidebar.page_opened') + + for (const page of ['home', 'apps', 'apps', 'assets', 'chooser', 'chooser', 'home']) { + await wrapper.get(`[data-testid="app-sidebar-${page}"]`).trigger('click') + await flushPromises() + } + + expect(opened().map(({ context }) => context)).toEqual([ + { page: 'apps', from_page: 'home' }, + { page: 'assets', from_page: 'apps' }, + { page: 'chooser', from_page: 'assets' }, + { page: 'home', from_page: 'chooser' } + ]) + }) + it('returns to the list of builds from All builds on Home', async () => { window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') const wrapper = mountPanel() diff --git a/src/renderer/src/panel/PanelApp.vue b/src/renderer/src/panel/PanelApp.vue index f4d57d20a..e5c4fa06b 100644 --- a/src/renderer/src/panel/PanelApp.vue +++ b/src/renderer/src/panel/PanelApp.vue @@ -237,7 +237,13 @@ function openAppFromHome(app: AppCard): void { dashboardHomePage.value = 'apps' } +const SIDEBAR_PAGE_OPENED_EVENT = 'comfy.desktop.sidebar.page_opened' + function handleSidebarNavigate(page: DashboardPage): void { + const fromPage = sidebarPage.value + if (page !== fromPage) { + emitTelemetryAction(SIDEBAR_PAGE_OPENED_EVENT, { page, from_page: fromPage }) + } closeBuildDetails() if (page === 'apps') appsVisits.value += 1 if (page === 'home' || page === 'apps' || page === 'chooser' || page === 'assets') diff --git a/src/types/comfyDesktopBridge.ts b/src/types/comfyDesktopBridge.ts index 3a7d9feb7..3c934410f 100644 --- a/src/types/comfyDesktopBridge.ts +++ b/src/types/comfyDesktopBridge.ts @@ -304,13 +304,15 @@ export interface ComfyDesktopHostedLocalBridge { * Usage events from a Comfy page hosted in Desktop's Apps view. Desktop * exposes it to that page as `window.comfyDesktopTelemetry` and sends an event * as `comfy.desktop.apps.` only when the person allows telemetry. - * Desktop takes `run_started` (`where`, and optionally `model`, `quality` and - * `seconds`), `example_used` (`example`) and `open_in_comfyui` (`where`), each - * with the app's catalog id in `app`. It drops any other event and any other - * property, and sends `app_opened` itself. + * `name` matches `^[a-z][a-z0-9_]{0,47}$`; `app_opened` and `app_page_failed` + * are refused because Desktop sends them itself. Desktop keeps at most 16 + * properties, each named `^[a-z][a-z0-9_]{0,31}$` with a boolean, a finite + * number or a slug of at most 64 characters (`^[a-z0-9][a-z0-9._-]{0,63}$`), + * and drops anything else. Desktop sets `app_id` itself, to the app it opened + * in that view; an `app` or `app_id` from the page is ignored. */ export interface ComfyDesktopHostedTelemetryBridge { - track(name: string, props?: Record): void + track(name: string, props?: Record): void } export interface ComfyDesktop2TerminalBridge { From 4799265a7e4214aea570f1313078061d0105b861 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Fri, 13 Nov 2026 22:55:00 -0800 Subject: [PATCH 03/10] fix(apps): give a page event the app whose page is loaded, not the app being loaded The self-review of #2087 found that app_id came from the app Desktop had asked the view to load. That id is set before loadURL starts. So when a person switched apps while an upload finished, the old page's run_started went out under the new app's id. The hosted page now records the app of the document that committed (loadedAppPage): it is cleared when Desktop starts a load and set when that load's main-frame navigation commits, with a document number. A navigation the page makes within its own address keeps the app with a new document number; one to another address drops it. The handler reads the loaded page when the message arrives and again after the sender check, and drops the event if the two differ, so an event from a page replaced while Desktop was checking it is dropped too. Tests drive the switch through the real hosted page: the old page's event during the next app's load is dropped, and the next page's event carries the next app's id once its page commits. --- .../comfyui-desktop-bridge-types/README.md | 3 +- .../comfyDesktopBridge.d.ts | 5 +- src/main/apps/appPageView.test.ts | 109 ++++++++++++++++-- src/main/apps/appPageView.ts | 48 +++++++- .../ipc/registerAppsTelemetryHandlers.test.ts | 40 +++++-- .../lib/ipc/registerAppsTelemetryHandlers.ts | 15 ++- src/types/comfyDesktopBridge.ts | 5 +- 7 files changed, 194 insertions(+), 31 deletions(-) diff --git a/packages/comfyui-desktop-bridge-types/README.md b/packages/comfyui-desktop-bridge-types/README.md index ddbadcaf6..953bd51f6 100644 --- a/packages/comfyui-desktop-bridge-types/README.md +++ b/packages/comfyui-desktop-bridge-types/README.md @@ -89,7 +89,8 @@ them and keeps its file download as the fallback. `track(name, props)` reports what the person does on the page, such as `track('click', { target: 'run' })`. Desktop sends it as `comfy.desktop.apps.` only when the person allows telemetry, and adds -`app_id`, the app Desktop opened in that view. A name is lowercase letters, +`app_id`, the app whose page Desktop loaded in that view. An event the page +sends while the view loads another app is dropped. A name is lowercase letters, digits and `_`, up to 48 characters; `app_opened` and `app_page_failed` are Desktop's own and are refused. Desktop keeps up to 16 properties, each a boolean, a finite number or a slug of up to 64 characters, and drops free diff --git a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts index 9ed0b0835..3ac841d8d 100644 --- a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts +++ b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts @@ -313,8 +313,9 @@ export interface ComfyDesktopHostedLocalBridge { * are refused because Desktop sends them itself. Desktop keeps at most 16 * properties, each named `^[a-z][a-z0-9_]{0,31}$` with a boolean, a finite * number or a slug of at most 64 characters (`^[a-z0-9][a-z0-9._-]{0,63}$`), - * and drops anything else. Desktop sets `app_id` itself, to the app it opened - * in that view; an `app` or `app_id` from the page is ignored. + * and drops anything else. Desktop sets `app_id` itself, to the app whose + * page it loaded in that view; an `app` or `app_id` from the page is ignored, + * and an event sent while the view loads another app is dropped. */ export interface ComfyDesktopHostedTelemetryBridge { track(name: string, props?: Record): void diff --git a/src/main/apps/appPageView.test.ts b/src/main/apps/appPageView.test.ts index e2b467a76..92ebf6aec 100644 --- a/src/main/apps/appPageView.test.ts +++ b/src/main/apps/appPageView.test.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'node:events' import { beforeEach, describe, expect, it, vi } from 'vitest' -import type { WebContents, WebFrameMain } from 'electron' +import type { IpcMainEvent, WebContents, WebFrameMain } from 'electron' type PermissionCheck = (contents: unknown, permission: string, requestingOrigin: string) => boolean type PermissionRequest = ( @@ -41,7 +41,10 @@ vi.mock('../lib/comfyDownloadManager', () => ({ attachSessionDownloadHandler: mocks.attachDownloads })) vi.mock('../lib/contextMenu', () => ({ attachContextMenu: vi.fn() })) -vi.mock('../lib/telemetry', () => ({ capture: mocks.capture })) +vi.mock('../lib/telemetry', () => ({ + capture: mocks.capture, + DEFAULT_EVENT_PROPERTY_NAMES: new Set() +})) vi.mock('./appsCatalog', () => ({ pageUrlForListedApp: (id: string) => mocks.pages.get(id) ?? null })) @@ -61,11 +64,12 @@ import { APP_PAGE_FAILED_EVENT, APP_PAGE_STATUS_CHANNEL, _resetForTest, - appIdOfPage, captureAppPage, hideAppPage, + loadedAppPage, showAppPage } from './appPageView' +import { trackFromAppsPage } from '../lib/ipc/registerAppsTelemetryHandlers' const APPS_URL = 'https://comfy.org/apps/' const CUTOUT = 'remove-background' @@ -753,22 +757,105 @@ describe('isAppsPageSender', () => { }) }) -describe('the app a hosted page shows', () => { - it('is the app Desktop opened in that view, and follows it to the next app', async () => { - expect(appIdOfPage(panelContents())).toBeNull() +const HDR = 'sdr-to-hdr' +const HDR_URL = `${APPS_URL}sdr-to-hdr/desktop.html` +const RUN = { name: 'run_started', props: { where: 'cloud' } } + +function eventFrom(contents: FakeContents): IpcMainEvent { + return { + sender: contents, + senderFrame: { ...contents.mainFrame, url: contents.url } + } as unknown as IpcMainEvent +} + +function runsSent(): unknown[][] { + return mocks.capture.mock.calls.filter(([name]) => name === 'comfy.desktop.apps.run_started') +} +describe('the app a hosted page has loaded', () => { + beforeEach(() => { + mocks.pages.set(HDR, HDR_URL) + }) + + it('is no app until the page Desktop loads commits, then the app with that document', async () => { await openCutout() const page = hostedPage().webContents as unknown as WebContents - expect(appIdOfPage(page)).toBe(CUTOUT) + expect(loadedAppPage(page)).toBeNull() - await showAppPage(panelContents(), UPSCALE, PLACEMENT) - expect(appIdOfPage(page)).toBe(UPSCALE) + finishLoading() + + expect(loadedAppPage(page)).toEqual({ appId: CUTOUT, document: 1 }) + }) + + it('is no app while the next app loads, then the next app once its page commits', async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents as unknown as WebContents + + await showAppPage(panelContents(), HDR, PLACEMENT) + expect(loadedAppPage(page)).toBeNull() + + finishLoading(HDR_URL) + expect(loadedAppPage(page)).toEqual({ appId: HDR, document: 2 }) + }) + + it("keeps the app when the page moves within its own address, and drops it when the page goes to another app's", async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents + + page.emit('did-navigate', {}, `${CUTOUT_URL}?example=dog`, 200) + expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: CUTOUT, document: 2 }) + + page.emit('did-navigate', {}, HDR_URL, 200) + expect(loadedAppPage(page as unknown as WebContents)).toBeNull() }) it('is no app for contents Desktop does not host', async () => { await openCutout() + finishLoading() + + expect(loadedAppPage(panelContents())).toBeNull() + expect(loadedAppPage(new FakeContents() as unknown as WebContents)).toBeNull() + }) +}) + +describe('usage events from a hosted page that switches apps', () => { + beforeEach(() => { + mocks.pages.set(HDR, HDR_URL) + }) + + it('drops what the old page sends while the next app loads, and tags the next page with its own app', async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents + + await trackFromAppsPage(eventFrom(page), RUN) + expect(runsSent()).toEqual([ + ['comfy.desktop.apps.run_started', { where: 'cloud', app_id: CUTOUT }] + ]) + + await showAppPage(panelContents(), HDR, PLACEMENT) + expect(await trackFromAppsPage(eventFrom(page), RUN)).toBe(false) + expect(runsSent()).toHaveLength(1) + + finishLoading(HDR_URL) + await trackFromAppsPage(eventFrom(page), RUN) + expect(runsSent()).toEqual([ + ['comfy.desktop.apps.run_started', { where: 'cloud', app_id: CUTOUT }], + ['comfy.desktop.apps.run_started', { where: 'cloud', app_id: HDR }] + ]) + }) + + it('drops an event whose page was replaced while Desktop was checking it', async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents + + const sending = trackFromAppsPage(eventFrom(page), RUN) + page.emit('did-navigate', {}, CUTOUT_URL, 200) - expect(appIdOfPage(panelContents())).toBeNull() - expect(appIdOfPage(new FakeContents() as unknown as WebContents)).toBeNull() + expect(await sending).toBe(false) + expect(runsSent()).toEqual([]) }) }) diff --git a/src/main/apps/appPageView.ts b/src/main/apps/appPageView.ts index 67ed09bcf..7ded3706c 100644 --- a/src/main/apps/appPageView.ts +++ b/src/main/apps/appPageView.ts @@ -29,9 +29,17 @@ const PAGE_PERMISSIONS: ReadonlySet = new Set(['clipboard-sanitized-writ type PageFailure = 'http_status' | 'network' | 'crashed' | 'redirected' +export interface LoadedAppPage { + appId: string + document: number +} + interface HostedPage { view: WebContentsView appId: string | null + loaded: LoadedAppPage | null + loadStarted: boolean + documents: number status: AppPageStatus wanted: boolean focusWhenReady: boolean @@ -111,13 +119,43 @@ function fail(windowKey: number, hosted: HostedPage, reason: PageFailure): void setStatus(windowKey, hosted, 'failed') } +const pagePath = (url: string): string | null => { + try { + const { origin, pathname } = new URL(url) + return origin + pathname.replace(/(\/index)?\.html$|\/$/, '') + } catch { + return null + } +} + +function isAppsOwnPage(appId: string, url: string): boolean { + const own = pageUrlForListedApp(appId) + return own !== null && pagePath(own) !== null && pagePath(own) === pagePath(url) +} + +function commitDocument(hosted: HostedPage, url: string): void { + hosted.documents += 1 + const appId = hosted.appId + const started = hosted.loadStarted + hosted.loadStarted = false + hosted.loaded = + appId !== null && (started || isAppsOwnPage(appId, url)) + ? { appId, document: hosted.documents } + : null +} + function load(windowKey: number, hosted: HostedPage, url: string): void { + hosted.loaded = null + hosted.loadStarted = true setStatus(windowKey, hosted, 'loading') void hosted.view.webContents.loadURL(url).catch(() => {}) } function closeForList(windowKey: number, hosted: HostedPage, pageStillShowsApp: boolean): void { - if (!pageStillShowsApp) hosted.appId = null + if (!pageStillShowsApp) { + hosted.appId = null + hosted.loaded = null + } conceal(hosted) panelOf(windowKey)?.send(APP_PAGE_CLOSED_CHANNEL) } @@ -150,6 +188,9 @@ function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation const hosted: HostedPage = { view, appId: null, + loaded: null, + loadStarted: false, + documents: 0, status: 'loading', wanted: false, focusWhenReady: false, @@ -180,6 +221,7 @@ function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation return { action: 'deny' } }) contents.on('did-navigate', (_event, url, httpResponseCode) => { + commitDocument(hosted, url) if (isAppsListUrl(location, url)) closeForList(windowKey, hosted, false) else if (httpResponseCode >= FIRST_HTTP_ERROR_STATUS) fail(windowKey, hosted, 'http_status') }) @@ -277,9 +319,9 @@ function concealWhenPanelGoes(windowKey: number, panel: WebContents): void { }) } -export function appIdOfPage(contents: WebContents): string | null { +export function loadedAppPage(contents: WebContents): LoadedAppPage | null { for (const hosted of hostedByWindow.values()) { - if (hosted.view.webContents === contents) return hosted.appId + if (hosted.view.webContents === contents) return hosted.loaded } return null } diff --git a/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts index 66460b47b..f0fb5fc19 100644 --- a/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts +++ b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts @@ -1,12 +1,17 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' type Listener = (event: unknown, payload: unknown) => void +interface LoadedPage { + appId: string + document: number +} const mocks = vi.hoisted(() => ({ listeners: new Map(), isAppsPage: true, appsEnabled: true, - pageAppId: 'remove-background' as string | null, + page: { appId: 'remove-background', document: 1 } as LoadedPage | null, + whileAuthorizing: () => {}, capture: vi.fn<(event: string, properties: unknown) => boolean>(() => true) })) @@ -15,9 +20,14 @@ vi.mock('electron', () => ({ on: (channel: string, listener: Listener) => mocks.listeners.set(channel, listener) } })) -vi.mock('../../apps/appsConfig', () => ({ areAppsEnabled: async () => mocks.appsEnabled })) +vi.mock('../../apps/appsConfig', () => ({ + areAppsEnabled: async () => { + mocks.whileAuthorizing() + return mocks.appsEnabled + } +})) vi.mock('../../apps/appsPages', () => ({ isAppsPageSender: () => mocks.isAppsPage })) -vi.mock('../../apps/appPageView', () => ({ appIdOfPage: () => mocks.pageAppId })) +vi.mock('../../apps/appPageView', () => ({ loadedAppPage: () => mocks.page })) vi.mock('../telemetry', () => ({ capture: mocks.capture, DEFAULT_EVENT_PROPERTY_NAMES: new Set(['app_version', 'installation_id']) @@ -37,7 +47,8 @@ beforeEach(() => { mocks.listeners.clear() mocks.isAppsPage = true mocks.appsEnabled = true - mocks.pageAppId = 'remove-background' + mocks.page = { appId: 'remove-background', document: 1 } + mocks.whileAuthorizing = () => {} registerAppsTelemetryHandlers() }) @@ -75,8 +86,8 @@ describe('usage events from the hosted Apps page', () => { ]) }) - it('fills app_id from the app Desktop opened in that view, not from the page', async () => { - mocks.pageAppId = 'upscale' + it('fills app_id from the app Desktop loaded in that view, not from the page', async () => { + mocks.page = { appId: 'upscale', document: 3 } await trackFromAppsPage(EVENT, { name: 'click', @@ -92,7 +103,7 @@ describe('usage events from the hosted Apps page', () => { it.each([ ['a sender that is not the hosted Apps page', { isAppsPage: false }], ['the Apps page while Apps is switched off', { appsEnabled: false }], - ['a page Desktop holds no app for', { pageAppId: null }] + ['a page Desktop holds no loaded app for, as while the next app loads', { page: null }] ])('refuses %s', async (_who, state) => { Object.assign(mocks, state) @@ -101,6 +112,21 @@ describe('usage events from the hosted Apps page', () => { expect(mocks.capture).not.toHaveBeenCalled() }) + it.each([ + ['the next app loads', () => (mocks.page = null)], + [ + 'the page is replaced by another document', + () => (mocks.page = { appId: 'remove-background', document: 2 }) + ], + ['the view moves to another app', () => (mocks.page = { appId: 'upscale', document: 2 })] + ])('drops an event when %s while Desktop checks the sender', async (_what, change) => { + mocks.whileAuthorizing = change + + expect(await trackFromAppsPage(EVENT, RUN)).toBe(false) + + expect(mocks.capture).not.toHaveBeenCalled() + }) + it.each([ ['app_opened, which Desktop sends itself', { name: 'app_opened', props: {} }], ['app_page_failed, which Desktop sends itself', { name: 'app_page_failed', props: {} }], diff --git a/src/main/lib/ipc/registerAppsTelemetryHandlers.ts b/src/main/lib/ipc/registerAppsTelemetryHandlers.ts index fbe2c10c9..01450e6c4 100644 --- a/src/main/lib/ipc/registerAppsTelemetryHandlers.ts +++ b/src/main/lib/ipc/registerAppsTelemetryHandlers.ts @@ -2,7 +2,8 @@ import { ipcMain } from 'electron' import type { IpcMainEvent } from 'electron' import { trackAppPageEvent } from '../../apps/appPageEvents' -import { appIdOfPage } from '../../apps/appPageView' +import { loadedAppPage } from '../../apps/appPageView' +import type { LoadedAppPage } from '../../apps/appPageView' import { areAppsEnabled } from '../../apps/appsConfig' import { isAppsPageSender } from '../../apps/appsPages' @@ -17,12 +18,16 @@ async function isFromAppsPage(event: IpcMainEvent): Promise { return isAppsPageSender(event.sender, event.senderFrame) && (await areAppsEnabled()) } +function isSamePage(now: LoadedAppPage | null, sent: LoadedAppPage): boolean { + return now !== null && now.appId === sent.appId && now.document === sent.document +} + export async function trackFromAppsPage(event: IpcMainEvent, payload: unknown): Promise { - if (!(await isFromAppsPage(event))) return false - const appId = appIdOfPage(event.sender) - if (appId === null) return false + const sentFrom = loadedAppPage(event.sender) + if (sentFrom === null || !(await isFromAppsPage(event))) return false + if (!isSamePage(loadedAppPage(event.sender), sentFrom)) return false const { name, props } = (payload && typeof payload === 'object' ? payload : {}) as TrackPayload - return trackAppPageEvent(name, props, appId) + return trackAppPageEvent(name, props, sentFrom.appId) } export function registerAppsTelemetryHandlers(): void { diff --git a/src/types/comfyDesktopBridge.ts b/src/types/comfyDesktopBridge.ts index 3c934410f..f23f8d647 100644 --- a/src/types/comfyDesktopBridge.ts +++ b/src/types/comfyDesktopBridge.ts @@ -308,8 +308,9 @@ export interface ComfyDesktopHostedLocalBridge { * are refused because Desktop sends them itself. Desktop keeps at most 16 * properties, each named `^[a-z][a-z0-9_]{0,31}$` with a boolean, a finite * number or a slug of at most 64 characters (`^[a-z0-9][a-z0-9._-]{0,63}$`), - * and drops anything else. Desktop sets `app_id` itself, to the app it opened - * in that view; an `app` or `app_id` from the page is ignored. + * and drops anything else. Desktop sets `app_id` itself, to the app whose + * page it loaded in that view; an `app` or `app_id` from the page is ignored, + * and an event sent while the view loads another app is dropped. */ export interface ComfyDesktopHostedTelemetryBridge { track(name: string, props?: Record): void From 194e1bcc1c30a8f926d6a8429bed910aca483f59 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Fri, 13 Nov 2026 23:09:23 -0800 Subject: [PATCH 04/10] fix(apps): keep the app id when an app drawn by a hash route is reopened from the list The self-review of #2087 at 5b854de found that an app whose page is the default hash route (#/) lost its app id after the person went back to the list and reopened it. Reopening is a same-document hash navigation, which fires did-navigate-in-page and not did-navigate, so the loaded page was never set and every later event was dropped. A main-frame did-navigate-in-page that lands exactly on the address Desktop is loading now sets the loaded app, with the same document number. Any other hash change, or one in a subframe, sets nothing, so the old page of a cross-document load still gets no app until that load commits. --- src/main/apps/appPageView.test.ts | 34 +++++++++++++++++++++++++++++++ src/main/apps/appPageView.ts | 16 ++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/apps/appPageView.test.ts b/src/main/apps/appPageView.test.ts index 92ebf6aec..73473329a 100644 --- a/src/main/apps/appPageView.test.ts +++ b/src/main/apps/appPageView.test.ts @@ -847,6 +847,40 @@ describe('usage events from a hosted page that switches apps', () => { ]) }) + it('tags events again after the person goes back to the list and reopens an app drawn by a hash route', async () => { + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + finishLoading(UPSCALE_URL) + const page = hostedPage().webContents + + page.url = `${APPS_URL}#/` + page.emit('did-navigate-in-page', {}, page.url, true) + expect(loadedAppPage(page as unknown as WebContents)).toBeNull() + + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + expect(await trackFromAppsPage(eventFrom(page), RUN)).toBe(false) + + page.url = UPSCALE_URL + page.emit('did-navigate-in-page', {}, UPSCALE_URL, true) + expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: UPSCALE, document: 1 }) + + await trackFromAppsPage(eventFrom(page), RUN) + expect(runsSent()).toEqual([ + ['comfy.desktop.apps.run_started', { where: 'cloud', app_id: UPSCALE }] + ]) + }) + + it('takes no app from a hash change that is not the route Desktop is loading', async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents + + await showAppPage(panelContents(), HDR, PLACEMENT) + page.emit('did-navigate-in-page', {}, `${CUTOUT_URL}#step-2`, true) + page.emit('did-navigate-in-page', {}, HDR_URL, false) + + expect(loadedAppPage(page as unknown as WebContents)).toBeNull() + }) + it('drops an event whose page was replaced while Desktop was checking it', async () => { await openCutout() finishLoading() diff --git a/src/main/apps/appPageView.ts b/src/main/apps/appPageView.ts index 7ded3706c..374669cb1 100644 --- a/src/main/apps/appPageView.ts +++ b/src/main/apps/appPageView.ts @@ -39,6 +39,7 @@ interface HostedPage { appId: string | null loaded: LoadedAppPage | null loadStarted: boolean + loadingUrl: string | null documents: number status: AppPageStatus wanted: boolean @@ -138,15 +139,25 @@ function commitDocument(hosted: HostedPage, url: string): void { const appId = hosted.appId const started = hosted.loadStarted hosted.loadStarted = false + hosted.loadingUrl = null hosted.loaded = appId !== null && (started || isAppsOwnPage(appId, url)) ? { appId, document: hosted.documents } : null } +function commitRoute(hosted: HostedPage, url: string): void { + const appId = hosted.appId + if (!hosted.loadStarted || appId === null || url !== hosted.loadingUrl) return + hosted.loadStarted = false + hosted.loadingUrl = null + hosted.loaded = { appId, document: hosted.documents } +} + function load(windowKey: number, hosted: HostedPage, url: string): void { hosted.loaded = null hosted.loadStarted = true + hosted.loadingUrl = url setStatus(windowKey, hosted, 'loading') void hosted.view.webContents.loadURL(url).catch(() => {}) } @@ -190,6 +201,7 @@ function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation appId: null, loaded: null, loadStarted: false, + loadingUrl: null, documents: 0, status: 'loading', wanted: false, @@ -226,7 +238,9 @@ function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation else if (httpResponseCode >= FIRST_HTTP_ERROR_STATUS) fail(windowKey, hosted, 'http_status') }) contents.on('did-navigate-in-page', (_event, url, isMainFrame) => { - if (isMainFrame && isAppsListUrl(location, url)) closeForList(windowKey, hosted, false) + if (!isMainFrame) return + if (isAppsListUrl(location, url)) closeForList(windowKey, hosted, false) + else commitRoute(hosted, url) }) contents.on('dom-ready', () => { if (hosted.status !== 'loading' || !isInAppsScope(location, contents.getURL())) return From 82a6084e1c8a22e9aab40684b9345cff54667fb4 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Fri, 13 Nov 2026 23:29:07 -0800 Subject: [PATCH 05/10] fix(apps): keep catalog app ids, limit page events, and drop the app when a page leaves its route Three review comments on #2087: - Desktop checked the app id it holds with the page's slug rule (lower case, up to 64 characters), so a catalog app such as HDR, or one with a 65-character id, lost every page event. That id is now checked with the catalog's own rule (APP_ID_PATTERN, moved to shared/appsLocation); values from the page keep the slug rule. - A page could send error, which becomes comfy.desktop.apps.error. The shared telemetry limit lets every .error event through, so a page could use up the 5,000-event session cap and silence Desktop's own telemetry. error is now refused, and page events have their own limits before the shared capture: 60 a minute for one app, whatever the names, and 1,000 a session in all. - A move within the page (did-navigate-in-page) kept the loaded app even when it left that app's route, so a hash-route app moving to another app's route sent the old app id. An in-page move now keeps the app only inside its route: for a hash-route app, the same hash or a sub-route of it (#/upscale/...), not #/upscale-pro; for a page app, the same page with any query or hash. A cross-document navigation uses the same rule. --- .../comfyui-desktop-bridge-types/README.md | 3 +- .../comfyDesktopBridge.d.ts | 4 +- src/main/apps/appPageEvents.test.ts | 20 +++++++- src/main/apps/appPageEvents.ts | 38 ++++++++++++-- src/main/apps/appPageView.test.ts | 28 +++++++++++ src/main/apps/appPageView.ts | 35 +++++++++---- src/main/apps/appsCatalog.ts | 3 +- .../ipc/registerAppsTelemetryHandlers.test.ts | 50 +++++++++++++++++++ src/shared/appsLocation.ts | 1 + src/types/comfyDesktopBridge.ts | 4 +- 10 files changed, 168 insertions(+), 18 deletions(-) diff --git a/packages/comfyui-desktop-bridge-types/README.md b/packages/comfyui-desktop-bridge-types/README.md index 953bd51f6..98627e5f9 100644 --- a/packages/comfyui-desktop-bridge-types/README.md +++ b/packages/comfyui-desktop-bridge-types/README.md @@ -92,7 +92,8 @@ them and keeps its file download as the fallback. `app_id`, the app whose page Desktop loaded in that view. An event the page sends while the view loads another app is dropped. A name is lowercase letters, digits and `_`, up to 48 characters; `app_opened` and `app_page_failed` are -Desktop's own and are refused. Desktop keeps up to 16 properties, each a +Desktop's own and are refused, and so is `error`. Desktop sends at most 60 +events a minute for one app and 1,000 page events a session. Desktop keeps up to 16 properties, each a boolean, a finite number or a slug of up to 64 characters, and drops free text, any other value, and an `app` or `app_id` from the page. diff --git a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts index 3ac841d8d..047a9b7bf 100644 --- a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts +++ b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts @@ -310,7 +310,9 @@ export interface ComfyDesktopHostedLocalBridge { * exposes it to that page as `window.comfyDesktopTelemetry` and sends an event * as `comfy.desktop.apps.` only when the person allows telemetry. * `name` matches `^[a-z][a-z0-9_]{0,47}$`; `app_opened` and `app_page_failed` - * are refused because Desktop sends them itself. Desktop keeps at most 16 + * are refused because Desktop sends them itself, and so is `error`. Desktop + * sends at most 60 events a minute for one app and 1,000 page events a + * session, and drops the rest. Desktop keeps at most 16 * properties, each named `^[a-z][a-z0-9_]{0,31}$` with a boolean, a finite * number or a slug of at most 64 characters (`^[a-z0-9][a-z0-9._-]{0,63}$`), * and drops anything else. Desktop sets `app_id` itself, to the app whose diff --git a/src/main/apps/appPageEvents.test.ts b/src/main/apps/appPageEvents.test.ts index 9db93d85d..185a2608f 100644 --- a/src/main/apps/appPageEvents.test.ts +++ b/src/main/apps/appPageEvents.test.ts @@ -38,6 +38,8 @@ import { APP_PAGE_EVENT_PREFIX, DESKTOP_APP_EVENTS, MAX_PAGE_EVENT_PROPERTIES, + UNLIMITED_EVENT_NAMES, + _resetPageEventLimitsForTest, checkAppPageEvent, trackAppPageEvent } from './appPageEvents' @@ -95,11 +97,26 @@ describe('checkAppPageEvent', () => { it.each([ ['no app id', null], - ['an app id that is not a slug', 'Remove Background'] + ['an app id the catalog would not list', 'Remove Background'], + ['an app id with a dot', 'remove.background'], + ['an app id longer than the catalog allows', `a${'b'.repeat(128)}`] ])('drops the event when Desktop holds %s for the page', (_why, appId) => { expect(checkAppPageEvent('click', { target: 'run' }, appId)).toBeNull() }) + it.each(['HDR', 'Upscale_4x', `a${'b'.repeat(127)}`])( + 'keeps the catalog app id %s as Desktop holds it', + (appId) => { + expect(checkAppPageEvent('click', {}, appId)?.properties).toEqual({ app_id: appId }) + } + ) + + it('refuses error, whose full name the shared telemetry limit lets through unlimited', () => { + expect(UNLIMITED_EVENT_NAMES.has('error')).toBe(true) + expect(checkAppPageEvent('error', {}, APP)).toBeNull() + expect(checkAppPageEvent('run_error', {}, APP)?.event).toBe('comfy.desktop.apps.run_error') + }) + it('drops free text, bad values and bad property names, keeping the event', () => { expect( checkAppPageEvent( @@ -163,6 +180,7 @@ describe('trackAppPageEvent through Desktop telemetry', () => { process.env['POSTHOG_API_KEY'] = 'test-key' process.env['POSTHOG_ENABLED'] = '1' telemetry._resetForTest() + _resetPageEventLimitsForTest() telemetry.initTelemetry({ appVersion: '0.0.0', appEnv: 'test', isPackaged: true }) }) diff --git a/src/main/apps/appPageEvents.ts b/src/main/apps/appPageEvents.ts index b9a41fa2e..5cf52127d 100644 --- a/src/main/apps/appPageEvents.ts +++ b/src/main/apps/appPageEvents.ts @@ -1,14 +1,19 @@ +import { APP_ID_PATTERN } from '../../shared/appsLocation' import * as mainTelemetry from '../lib/telemetry' import type { TelemetryContext } from '../lib/telemetry' export const APP_PAGE_EVENT_PREFIX = 'comfy.desktop.apps.' export const MAX_PAGE_EVENT_PROPERTIES = 16 +export const PAGE_EVENTS_PER_MINUTE = 60 +export const PAGE_EVENTS_PER_SESSION = 1_000 +const MINUTE_MS = 60_000 const EVENT_NAME = /^[a-z][a-z0-9_]{0,47}$/ const PROPERTY_NAME = /^[a-z][a-z0-9_]{0,31}$/ const SLUG = /^[a-z0-9][a-z0-9._-]{0,63}$/ export const DESKTOP_APP_EVENTS: ReadonlySet = new Set(['app_opened', 'app_page_failed']) +export const UNLIMITED_EVENT_NAMES: ReadonlySet = new Set(['error']) const DESKTOP_PROPERTIES: ReadonlySet = new Set(['app', 'app_id']) type PropertyValue = boolean | number | string @@ -42,10 +47,15 @@ export function checkAppPageEvent( props: unknown, appId: string | null ): CheckedPageEvent | null { - if (typeof name !== 'string' || !EVENT_NAME.test(name) || DESKTOP_APP_EVENTS.has(name)) { + if ( + typeof name !== 'string' || + !EVENT_NAME.test(name) || + DESKTOP_APP_EVENTS.has(name) || + UNLIMITED_EVENT_NAMES.has(name) + ) { return null } - if (appId === null || !SLUG.test(appId)) return null + if (appId === null || !APP_ID_PATTERN.test(appId)) return null const properties: TelemetryContext = {} let kept = 0 for (const [key, raw] of Object.entries(isPlainRecord(props) ? props : {})) { @@ -60,10 +70,32 @@ export function checkAppPageEvent( return { event: `${APP_PAGE_EVENT_PREFIX}${name}`, properties } } +const sentThisMinute = new Map() +let sentThisSession = 0 + +function withinPageLimits(appId: string, now: number): boolean { + if (sentThisSession >= PAGE_EVENTS_PER_SESSION) return false + const recent = (sentThisMinute.get(appId) ?? []).filter((at) => at > now - MINUTE_MS) + if (recent.length >= PAGE_EVENTS_PER_MINUTE) { + sentThisMinute.set(appId, recent) + return false + } + recent.push(now) + sentThisMinute.set(appId, recent) + sentThisSession++ + return true +} + +export function _resetPageEventLimitsForTest(): void { + sentThisMinute.clear() + sentThisSession = 0 +} + export function trackAppPageEvent(name: unknown, props: unknown, appId: string | null): boolean { try { const checked = checkAppPageEvent(name, props, appId) - return checked ? mainTelemetry.capture(checked.event, checked.properties) : false + if (!checked || appId === null || !withinPageLimits(appId, Date.now())) return false + return mainTelemetry.capture(checked.event, checked.properties) } catch { return false } diff --git a/src/main/apps/appPageView.test.ts b/src/main/apps/appPageView.test.ts index 73473329a..a6702c8cd 100644 --- a/src/main/apps/appPageView.test.ts +++ b/src/main/apps/appPageView.test.ts @@ -811,6 +811,34 @@ describe('the app a hosted page has loaded', () => { expect(loadedAppPage(page as unknown as WebContents)).toBeNull() }) + it('keeps the app while the page moves within its own page, by query or hash', async () => { + await openCutout() + finishLoading() + const page = hostedPage().webContents + + page.emit('did-navigate-in-page', {}, `${CUTOUT_URL}?example=dog`, true) + page.emit('did-navigate-in-page', {}, `${CUTOUT_URL}#result`, true) + + expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: CUTOUT, document: 1 }) + }) + + it("keeps a hash-route app within its route, and drops it on another app's route", async () => { + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + finishLoading(UPSCALE_URL) + const page = hostedPage().webContents as unknown as WebContents + const moveTo = (url: string) => + hostedPage().webContents.emit('did-navigate-in-page', {}, url, true) + + moveTo(`${UPSCALE_URL}/step-2`) + expect(loadedAppPage(page)).toEqual({ appId: UPSCALE, document: 1 }) + + moveTo(`${APPS_URL}#/upscale-pro`) + expect(loadedAppPage(page)).toBeNull() + + moveTo(`${APPS_URL}#/other-app`) + expect(loadedAppPage(page)).toBeNull() + }) + it('is no app for contents Desktop does not host', async () => { await openCutout() finishLoading() diff --git a/src/main/apps/appPageView.ts b/src/main/apps/appPageView.ts index 374669cb1..29c90879d 100644 --- a/src/main/apps/appPageView.ts +++ b/src/main/apps/appPageView.ts @@ -129,9 +129,21 @@ const pagePath = (url: string): string | null => { } } -function isAppsOwnPage(appId: string, url: string): boolean { +const routeOf = (url: string): string => { + try { + return new URL(url).hash + } catch { + return '' + } +} + +function isInAppRoute(appId: string, url: string): boolean { const own = pageUrlForListedApp(appId) - return own !== null && pagePath(own) !== null && pagePath(own) === pagePath(url) + if (own === null || pagePath(own) === null || pagePath(own) !== pagePath(url)) return false + const route = routeOf(own) + if (!route.startsWith('#/')) return true + const at = routeOf(url) + return at === route || at.startsWith(`${route}/`) || at.startsWith(`${route}?`) } function commitDocument(hosted: HostedPage, url: string): void { @@ -141,17 +153,22 @@ function commitDocument(hosted: HostedPage, url: string): void { hosted.loadStarted = false hosted.loadingUrl = null hosted.loaded = - appId !== null && (started || isAppsOwnPage(appId, url)) + appId !== null && (started || isInAppRoute(appId, url)) ? { appId, document: hosted.documents } : null } -function commitRoute(hosted: HostedPage, url: string): void { +function moveWithinDocument(hosted: HostedPage, url: string): void { const appId = hosted.appId - if (!hosted.loadStarted || appId === null || url !== hosted.loadingUrl) return - hosted.loadStarted = false - hosted.loadingUrl = null - hosted.loaded = { appId, document: hosted.documents } + if (hosted.loadStarted) { + if (appId === null || url !== hosted.loadingUrl) return + hosted.loadStarted = false + hosted.loadingUrl = null + hosted.loaded = { appId, document: hosted.documents } + return + } + hosted.loaded = + appId !== null && isInAppRoute(appId, url) ? { appId, document: hosted.documents } : null } function load(windowKey: number, hosted: HostedPage, url: string): void { @@ -240,7 +257,7 @@ function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation contents.on('did-navigate-in-page', (_event, url, isMainFrame) => { if (!isMainFrame) return if (isAppsListUrl(location, url)) closeForList(windowKey, hosted, false) - else commitRoute(hosted, url) + else moveWithinDocument(hosted, url) }) contents.on('dom-ready', () => { if (hosted.status !== 'loading' || !isInAppsScope(location, contents.getURL())) return diff --git a/src/main/apps/appsCatalog.ts b/src/main/apps/appsCatalog.ts index fcfa48f36..1b0abf147 100644 --- a/src/main/apps/appsCatalog.ts +++ b/src/main/apps/appsCatalog.ts @@ -1,4 +1,4 @@ -import { isAppsListUrl, isInAppsScope } from '../../shared/appsLocation' +import { APP_ID_PATTERN, isAppsListUrl, isInAppsScope } from '../../shared/appsLocation' import type { AppsLocation } from '../../shared/appsLocation' import type { AppCard } from '../../types/ipc' import { fetchJSON } from '../lib/fetch' @@ -9,7 +9,6 @@ const FETCH_TIMEOUT_MS = 8000 const CACHE_TTL_MS = 5 * 60 * 1000 const MAX_APPS = 200 const MAX_TEXT_LENGTH = 4096 -const APP_ID_PATTERN = /^[a-z0-9][a-z0-9_-]{0,127}$/i interface ListedApp { card: AppCard diff --git a/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts index f0fb5fc19..b7b90f608 100644 --- a/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts +++ b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts @@ -33,6 +33,12 @@ vi.mock('../telemetry', () => ({ DEFAULT_EVENT_PROPERTY_NAMES: new Set(['app_version', 'installation_id']) })) +import { + PAGE_EVENTS_PER_MINUTE, + PAGE_EVENTS_PER_SESSION, + _resetPageEventLimitsForTest, + trackAppPageEvent +} from '../../apps/appPageEvents' import { APPS_TELEMETRY_TRACK_CHANNEL, registerAppsTelemetryHandlers, @@ -49,6 +55,7 @@ beforeEach(() => { mocks.appsEnabled = true mocks.page = { appId: 'remove-background', document: 1 } mocks.whileAuthorizing = () => {} + _resetPageEventLimitsForTest() registerAppsTelemetryHandlers() }) @@ -158,3 +165,46 @@ describe('usage events from the hosted Apps page', () => { }) }) }) + +describe('how many page events Desktop sends', () => { + const sent = () => mocks.capture.mock.calls.length + + it(`sends at most ${PAGE_EVENTS_PER_MINUTE} a minute for one app, whatever their names, then again a minute later`, () => { + vi.useFakeTimers() + try { + vi.setSystemTime(new Date('2026-11-14T10:00:00Z')) + for (let i = 0; i <= PAGE_EVENTS_PER_MINUTE; i++) { + trackAppPageEvent(`step_${i}`, {}, 'genjutsu') + } + expect(sent()).toBe(PAGE_EVENTS_PER_MINUTE) + + expect(trackAppPageEvent('click', {}, 'upscale')).toBe(true) + + vi.setSystemTime(new Date('2026-11-14T10:01:01Z')) + expect(trackAppPageEvent('click', {}, 'genjutsu')).toBe(true) + expect(sent()).toBe(PAGE_EVENTS_PER_MINUTE + 2) + } finally { + vi.useRealTimers() + } + }) + + it(`sends at most ${PAGE_EVENTS_PER_SESSION} page events in all, so pages cannot use up Desktop's own telemetry`, () => { + vi.useFakeTimers() + try { + for (let i = 0; i < PAGE_EVENTS_PER_SESSION + 5; i++) { + vi.setSystemTime(new Date(Date.UTC(2026, 10, 14, 10, 0, 0) + i * 60_000)) + trackAppPageEvent('click', {}, 'genjutsu') + } + expect(sent()).toBe(PAGE_EVENTS_PER_SESSION) + } finally { + vi.useRealTimers() + } + }) + + it('counts no event it refuses', () => { + for (let i = 0; i < PAGE_EVENTS_PER_MINUTE; i++) trackAppPageEvent('error', {}, 'genjutsu') + + expect(trackAppPageEvent('click', {}, 'genjutsu')).toBe(true) + expect(sent()).toBe(1) + }) +}) diff --git a/src/shared/appsLocation.ts b/src/shared/appsLocation.ts index e4f342ad4..b17948bc2 100644 --- a/src/shared/appsLocation.ts +++ b/src/shared/appsLocation.ts @@ -1,4 +1,5 @@ export const DEFAULT_APPS_URL = 'https://comfy.org/apps/' +export const APP_ID_PATTERN = /^[a-z0-9][a-z0-9_-]{0,127}$/i export interface AppsLocation { url: string diff --git a/src/types/comfyDesktopBridge.ts b/src/types/comfyDesktopBridge.ts index f23f8d647..6f1c71606 100644 --- a/src/types/comfyDesktopBridge.ts +++ b/src/types/comfyDesktopBridge.ts @@ -305,7 +305,9 @@ export interface ComfyDesktopHostedLocalBridge { * exposes it to that page as `window.comfyDesktopTelemetry` and sends an event * as `comfy.desktop.apps.` only when the person allows telemetry. * `name` matches `^[a-z][a-z0-9_]{0,47}$`; `app_opened` and `app_page_failed` - * are refused because Desktop sends them itself. Desktop keeps at most 16 + * are refused because Desktop sends them itself, and so is `error`. Desktop + * sends at most 60 events a minute for one app and 1,000 page events a + * session, and drops the rest. Desktop keeps at most 16 * properties, each named `^[a-z][a-z0-9_]{0,31}$` with a boolean, a finite * number or a slug of at most 64 characters (`^[a-z0-9][a-z0-9._-]{0,63}$`), * and drops anything else. Desktop sets `app_id` itself, to the app whose From 2dbf3994475f9be7fd274832dfecbe574ce407e9 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Fri, 13 Nov 2026 23:37:12 -0800 Subject: [PATCH 06/10] fix(apps): count only sent page events against the limits, keep the app on a redirected page, and document the name and key rules Four review comments on #2087: - The page limits counted an event before telemetry decided to send it. With consent off, 1,000 events used up the session quota, and after consent was given every page event stayed blocked until Desktop restarted. Only events telemetry sends now count. - When a listed page redirected to another allowed address, a later move within the page (such as #result) was checked against the catalog address and dropped the app. The committed address is now the baseline the route is checked against; the catalog address is used only before any page of the app has committed. - The bridge types README now says a name and a property key must start with a lowercase letter, with both patterns, and that the page is not told when an event is dropped. --- .../comfyui-desktop-bridge-types/README.md | 23 +++++++++++---- src/main/apps/appPageEvents.ts | 28 ++++++++++++------- src/main/apps/appPageView.test.ts | 10 +++++++ src/main/apps/appPageView.ts | 27 ++++++++++++++---- .../ipc/registerAppsTelemetryHandlers.test.ts | 20 +++++++++++++ 5 files changed, 86 insertions(+), 22 deletions(-) diff --git a/packages/comfyui-desktop-bridge-types/README.md b/packages/comfyui-desktop-bridge-types/README.md index 98627e5f9..0e1ab3319 100644 --- a/packages/comfyui-desktop-bridge-types/README.md +++ b/packages/comfyui-desktop-bridge-types/README.md @@ -90,12 +90,23 @@ them and keeps its file download as the fallback. `track('click', { target: 'run' })`. Desktop sends it as `comfy.desktop.apps.` only when the person allows telemetry, and adds `app_id`, the app whose page Desktop loaded in that view. An event the page -sends while the view loads another app is dropped. A name is lowercase letters, -digits and `_`, up to 48 characters; `app_opened` and `app_page_failed` are -Desktop's own and are refused, and so is `error`. Desktop sends at most 60 -events a minute for one app and 1,000 page events a session. Desktop keeps up to 16 properties, each a -boolean, a finite number or a slug of up to 64 characters, and drops free -text, any other value, and an `app` or `app_id` from the page. +sends while the view loads another app is dropped. + +- A name starts with a lowercase letter, then lowercase letters, digits and + `_`, up to 48 characters in all (`^[a-z][a-z0-9_]{0,47}$`), so `2_click` is + dropped. `app_opened` and `app_page_failed` are Desktop's own and are + refused, and so is `error`. +- Desktop keeps up to 16 properties. A key starts with a lowercase letter, + then lowercase letters, digits and `_`, up to 32 characters in all + (`^[a-z][a-z0-9_]{0,31}$`), so `{ Run: true }` is dropped. A value is a + boolean, a finite number or a slug of up to 64 characters + (`^[a-z0-9][a-z0-9._-]{0,63}$`). Free text, any other key or value, and an + `app` or `app_id` from the page are dropped. +- Desktop sends at most 60 events a minute for one app and 1,000 page events a + session. Events telemetry did not send, as while consent is off, do not + count. + +The page is not told when Desktop drops an event. All three objects are absent in a browser, so a page keeps its own sign-in and its own way to reach a local ComfyUI as the fallback, and sends no events. diff --git a/src/main/apps/appPageEvents.ts b/src/main/apps/appPageEvents.ts index 5cf52127d..e163ffd90 100644 --- a/src/main/apps/appPageEvents.ts +++ b/src/main/apps/appPageEvents.ts @@ -73,17 +73,22 @@ export function checkAppPageEvent( const sentThisMinute = new Map() let sentThisSession = 0 -function withinPageLimits(appId: string, now: number): boolean { - if (sentThisSession >= PAGE_EVENTS_PER_SESSION) return false +function recentFor(appId: string, now: number): number[] { const recent = (sentThisMinute.get(appId) ?? []).filter((at) => at > now - MINUTE_MS) - if (recent.length >= PAGE_EVENTS_PER_MINUTE) { - sentThisMinute.set(appId, recent) - return false - } - recent.push(now) sentThisMinute.set(appId, recent) + return recent +} + +function isUnderPageLimits(appId: string, now: number): boolean { + return ( + sentThisSession < PAGE_EVENTS_PER_SESSION && + recentFor(appId, now).length < PAGE_EVENTS_PER_MINUTE + ) +} + +function countSent(appId: string, now: number): void { + recentFor(appId, now).push(now) sentThisSession++ - return true } export function _resetPageEventLimitsForTest(): void { @@ -94,8 +99,11 @@ export function _resetPageEventLimitsForTest(): void { export function trackAppPageEvent(name: unknown, props: unknown, appId: string | null): boolean { try { const checked = checkAppPageEvent(name, props, appId) - if (!checked || appId === null || !withinPageLimits(appId, Date.now())) return false - return mainTelemetry.capture(checked.event, checked.properties) + const now = Date.now() + if (!checked || appId === null || !isUnderPageLimits(appId, now)) return false + const sent = mainTelemetry.capture(checked.event, checked.properties) + if (sent) countSent(appId, now) + return sent } catch { return false } diff --git a/src/main/apps/appPageView.test.ts b/src/main/apps/appPageView.test.ts index a6702c8cd..934db99c8 100644 --- a/src/main/apps/appPageView.test.ts +++ b/src/main/apps/appPageView.test.ts @@ -822,6 +822,16 @@ describe('the app a hosted page has loaded', () => { expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: CUTOUT, document: 1 }) }) + it('keeps the app on the address its page was redirected to, through moves within it', async () => { + await openCutout() + const moved = `${APPS_URL}remove-background/v2/desktop.html` + finishLoading(moved) + const page = hostedPage().webContents + + page.emit('did-navigate-in-page', {}, `${moved}#result`, true) + expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: CUTOUT, document: 1 }) + }) + it("keeps a hash-route app within its route, and drops it on another app's route", async () => { await showAppPage(panelContents(), UPSCALE, PLACEMENT) finishLoading(UPSCALE_URL) diff --git a/src/main/apps/appPageView.ts b/src/main/apps/appPageView.ts index 29c90879d..4d5a63947 100644 --- a/src/main/apps/appPageView.ts +++ b/src/main/apps/appPageView.ts @@ -40,6 +40,7 @@ interface HostedPage { loaded: LoadedAppPage | null loadStarted: boolean loadingUrl: string | null + routeBase: string | null documents: number status: AppPageStatus wanted: boolean @@ -137,23 +138,33 @@ const routeOf = (url: string): string => { } } -function isInAppRoute(appId: string, url: string): boolean { - const own = pageUrlForListedApp(appId) - if (own === null || pagePath(own) === null || pagePath(own) !== pagePath(url)) return false - const route = routeOf(own) +function isInAppRoute(base: string, url: string): boolean { + if (pagePath(base) === null || pagePath(base) !== pagePath(url)) return false + const route = routeOf(base) if (!route.startsWith('#/')) return true const at = routeOf(url) return at === route || at.startsWith(`${route}/`) || at.startsWith(`${route}?`) } +function routeBaseOf(hosted: HostedPage): string | null { + if (hosted.routeBase !== null) return hosted.routeBase + return hosted.appId === null ? null : pageUrlForListedApp(hosted.appId) +} + +function isInLoadedRoute(hosted: HostedPage, url: string): boolean { + const base = routeBaseOf(hosted) + return base !== null && isInAppRoute(base, url) +} + function commitDocument(hosted: HostedPage, url: string): void { hosted.documents += 1 const appId = hosted.appId const started = hosted.loadStarted hosted.loadStarted = false hosted.loadingUrl = null + if (started) hosted.routeBase = url hosted.loaded = - appId !== null && (started || isInAppRoute(appId, url)) + appId !== null && (started || isInLoadedRoute(hosted, url)) ? { appId, document: hosted.documents } : null } @@ -164,17 +175,19 @@ function moveWithinDocument(hosted: HostedPage, url: string): void { if (appId === null || url !== hosted.loadingUrl) return hosted.loadStarted = false hosted.loadingUrl = null + hosted.routeBase = url hosted.loaded = { appId, document: hosted.documents } return } hosted.loaded = - appId !== null && isInAppRoute(appId, url) ? { appId, document: hosted.documents } : null + appId !== null && isInLoadedRoute(hosted, url) ? { appId, document: hosted.documents } : null } function load(windowKey: number, hosted: HostedPage, url: string): void { hosted.loaded = null hosted.loadStarted = true hosted.loadingUrl = url + hosted.routeBase = null setStatus(windowKey, hosted, 'loading') void hosted.view.webContents.loadURL(url).catch(() => {}) } @@ -183,6 +196,7 @@ function closeForList(windowKey: number, hosted: HostedPage, pageStillShowsApp: if (!pageStillShowsApp) { hosted.appId = null hosted.loaded = null + hosted.routeBase = null } conceal(hosted) panelOf(windowKey)?.send(APP_PAGE_CLOSED_CHANNEL) @@ -219,6 +233,7 @@ function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation loaded: null, loadStarted: false, loadingUrl: null, + routeBase: null, documents: 0, status: 'loading', wanted: false, diff --git a/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts index b7b90f608..6a7ce7be9 100644 --- a/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts +++ b/src/main/lib/ipc/registerAppsTelemetryHandlers.test.ts @@ -56,6 +56,7 @@ beforeEach(() => { mocks.page = { appId: 'remove-background', document: 1 } mocks.whileAuthorizing = () => {} _resetPageEventLimitsForTest() + mocks.capture.mockReturnValue(true) registerAppsTelemetryHandlers() }) @@ -201,6 +202,25 @@ describe('how many page events Desktop sends', () => { } }) + it('counts no event telemetry did not send, as while consent is off', () => { + vi.useFakeTimers() + try { + mocks.capture.mockReturnValue(false) + for (let i = 0; i < PAGE_EVENTS_PER_SESSION + 5; i++) { + vi.setSystemTime(new Date(Date.UTC(2026, 10, 14, 10, 0, 0) + i * 60_000)) + trackAppPageEvent('click', {}, 'genjutsu') + } + for (let i = 0; i < PAGE_EVENTS_PER_MINUTE; i++) { + trackAppPageEvent(`step_${i}`, {}, 'genjutsu') + } + + mocks.capture.mockReturnValue(true) + expect(trackAppPageEvent('click', {}, 'genjutsu')).toBe(true) + } finally { + vi.useRealTimers() + } + }) + it('counts no event it refuses', () => { for (let i = 0; i < PAGE_EVENTS_PER_MINUTE; i++) trackAppPageEvent('error', {}, 'genjutsu') From 6861ebc871ca4db045592206605903ead4ddb3f0 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Fri, 13 Nov 2026 23:44:00 -0800 Subject: [PATCH 07/10] docs(bridge-types): list the keys Desktop sets itself and the shared per-name limit --- packages/comfyui-desktop-bridge-types/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/comfyui-desktop-bridge-types/README.md b/packages/comfyui-desktop-bridge-types/README.md index 0e1ab3319..2e3f9f9f4 100644 --- a/packages/comfyui-desktop-bridge-types/README.md +++ b/packages/comfyui-desktop-bridge-types/README.md @@ -102,9 +102,15 @@ sends while the view loads another app is dropped. boolean, a finite number or a slug of up to 64 characters (`^[a-z0-9][a-z0-9._-]{0,63}$`). Free text, any other key or value, and an `app` or `app_id` from the page are dropped. +- Desktop sets these keys on every event itself, so the page's value for any + of them is dropped: `app_version`, `app_channel`, `app_env`, `is_packaged`, + `platform`, `arch`, `client`, `installation_id` and `sac_state`. - Desktop sends at most 60 events a minute for one app and 1,000 page events a session. Events telemetry did not send, as while consent is off, do not count. +- Desktop's telemetry also sends at most 60 of one event name a minute, shared + by every app. If one app sends 60 `click` events in a minute, a `click` from + another app in that minute is dropped too. The page is not told when Desktop drops an event. From 22e865e0fe198b4d6725b35ce338f730ba7874d3 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Sun, 15 Nov 2026 05:03:56 -0500 Subject: [PATCH 08/10] fix(panel): record a sidebar page open only for a click in the sidebar Main now opens Apps from See all apps on Home through the sidebar's navigate handler, so that click was recorded as comfy.desktop.sidebar.page_opened. The event now comes from the sidebar's own navigate binding, and See all apps sends nothing. --- src/renderer/src/panel/PanelApp.test.ts | 16 ++++++++++++++++ src/renderer/src/panel/PanelApp.vue | 8 ++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/renderer/src/panel/PanelApp.test.ts b/src/renderer/src/panel/PanelApp.test.ts index ff3d9d08b..01fa8ef78 100644 --- a/src/renderer/src/panel/PanelApp.test.ts +++ b/src/renderer/src/panel/PanelApp.test.ts @@ -1595,6 +1595,22 @@ describe('PanelApp', () => { ]) }) + it('does not record a sidebar page open for See all apps on Home', async () => { + window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') + vi.mocked(window.api.getAppsEnabled).mockResolvedValue(true) + const wrapper = mountPanel() + await flushPromises() + const events = captureTelemetry() + + await wrapper.get('[data-testid="home-see-all-apps"]').trigger('click') + await flushPromises() + + expect(wrapper.get('[data-testid="apps-view"]').isVisible()).toBe(true) + expect( + events.filter(({ actionName }) => actionName === 'comfy.desktop.sidebar.page_opened') + ).toEqual([]) + }) + it('returns to the list of builds from All builds on Home', async () => { window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') const wrapper = mountPanel() diff --git a/src/renderer/src/panel/PanelApp.vue b/src/renderer/src/panel/PanelApp.vue index e5c4fa06b..30b4358d3 100644 --- a/src/renderer/src/panel/PanelApp.vue +++ b/src/renderer/src/panel/PanelApp.vue @@ -239,11 +239,15 @@ function openAppFromHome(app: AppCard): void { const SIDEBAR_PAGE_OPENED_EVENT = 'comfy.desktop.sidebar.page_opened' -function handleSidebarNavigate(page: DashboardPage): void { +function handleSidebarClick(page: DashboardPage): void { const fromPage = sidebarPage.value if (page !== fromPage) { emitTelemetryAction(SIDEBAR_PAGE_OPENED_EVENT, { page, from_page: fromPage }) } + handleSidebarNavigate(page) +} + +function handleSidebarNavigate(page: DashboardPage): void { closeBuildDetails() if (page === 'apps') appsVisits.value += 1 if (page === 'home' || page === 'apps' || page === 'chooser' || page === 'assets') @@ -509,7 +513,7 @@ onUnmounted(() => { :active-page="sidebarPage" :active-build-id="sidebarBuildId" :make-room="appIsOpen" - @navigate="handleSidebarNavigate" + @navigate="handleSidebarClick" @open-build="openBuildFromSidebar" @new-build="handleChooserShowNewInstall" /> From f1b2aa7c5714f83879011ae979536bdcfd5f9e51 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Sun, 15 Nov 2026 08:22:27 -0500 Subject: [PATCH 09/10] fix(apps): keep a hash-route app through a query change or a child route under a trailing slash --- src/main/apps/appPageView.test.ts | 44 +++++++++++++++++++++++++++++++ src/main/apps/appPageView.ts | 4 +-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/apps/appPageView.test.ts b/src/main/apps/appPageView.test.ts index 934db99c8..68e2a28de 100644 --- a/src/main/apps/appPageView.test.ts +++ b/src/main/apps/appPageView.test.ts @@ -849,6 +849,50 @@ describe('the app a hosted page has loaded', () => { expect(loadedAppPage(page)).toBeNull() }) + it.each([ + [`${UPSCALE_URL}?mode=before`, `${UPSCALE_URL}?mode=after`], + [`${UPSCALE_URL}/`, `${UPSCALE_URL}/step-2`] + ])('keeps a hash-route app listed at %s while it moves to %s', async (listedUrl, nextUrl) => { + mocks.pages.set(UPSCALE, listedUrl) + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + finishLoading(listedUrl) + const page = hostedPage().webContents + + page.url = nextUrl + page.emit('did-navigate-in-page', {}, nextUrl, true) + + expect(loadedAppPage(page as unknown as WebContents)).toEqual({ appId: UPSCALE, document: 1 }) + await trackFromAppsPage(eventFrom(page), RUN) + expect(runsSent()).toEqual([ + ['comfy.desktop.apps.run_started', { where: 'cloud', app_id: UPSCALE }] + ]) + }) + + it.each([`${UPSCALE_URL}?mode=before`, `${UPSCALE_URL}/`])( + "drops a hash-route app listed at %s on another app's route and on the list", + async (listedUrl) => { + mocks.pages.set(UPSCALE, listedUrl) + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + finishLoading(listedUrl) + const page = hostedPage().webContents as unknown as WebContents + const moveTo = (url: string) => + hostedPage().webContents.emit('did-navigate-in-page', {}, url, true) + + moveTo(`${APPS_URL}#/upscale-pro`) + expect(loadedAppPage(page)).toBeNull() + + moveTo(listedUrl) + expect(loadedAppPage(page)).toEqual({ appId: UPSCALE, document: 1 }) + + moveTo(`${APPS_URL}#/other-app?mode=before`) + expect(loadedAppPage(page)).toBeNull() + + moveTo(listedUrl) + moveTo(`${APPS_URL}#/`) + expect(loadedAppPage(page)).toBeNull() + } + ) + it('is no app for contents Desktop does not host', async () => { await openCutout() finishLoading() diff --git a/src/main/apps/appPageView.ts b/src/main/apps/appPageView.ts index 4d5a63947..d51c47db4 100644 --- a/src/main/apps/appPageView.ts +++ b/src/main/apps/appPageView.ts @@ -132,7 +132,7 @@ const pagePath = (url: string): string | null => { const routeOf = (url: string): string => { try { - return new URL(url).hash + return (new URL(url).hash.split('?', 1)[0] ?? '').replace(/^(#\/.+?)\/+$/, '$1') } catch { return '' } @@ -143,7 +143,7 @@ function isInAppRoute(base: string, url: string): boolean { const route = routeOf(base) if (!route.startsWith('#/')) return true const at = routeOf(url) - return at === route || at.startsWith(`${route}/`) || at.startsWith(`${route}?`) + return at === route || at.startsWith(`${route}/`) } function routeBaseOf(hosted: HostedPage): string | null { From 2dbe9b3ae673c55862407404033a51d511c04c56 Mon Sep 17 00:00:00 2001 From: Vinh Trinh Date: Sun, 15 Nov 2026 21:01:27 -0500 Subject: [PATCH 10/10] chore(bridge-types): bump to 0.13.0 for the hosted telemetry bridge Main published 0.12.0 with the installing state, so the telemetry bridge needs the next minor. --- packages/comfyui-desktop-bridge-types/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/comfyui-desktop-bridge-types/package.json b/packages/comfyui-desktop-bridge-types/package.json index 68294220e..bea6b0a86 100644 --- a/packages/comfyui-desktop-bridge-types/package.json +++ b/packages/comfyui-desktop-bridge-types/package.json @@ -1,6 +1,6 @@ { "name": "@comfyorg/comfyui-desktop-bridge-types", - "version": "0.12.0", + "version": "0.13.0", "description": "TypeScript definitions for the Comfy Desktop hosted frontend bridge", "type": "module", "main": "./index.js",