From ecddd977aa569b26608eadffdb9a79b1237950aa Mon Sep 17 00:00:00 2001 From: ryan-di Date: Sat, 7 Nov 2026 09:29:45 +0800 Subject: [PATCH 1/5] feat: attribute proactive greeting telemetry to eligible user workspace pairs --- .../comfyDesktopBridge.d.ts | 4 + .../lib/ipc/registerTelemetryHandlers.test.ts | 68 +++++++- src/main/lib/ipc/registerTelemetryHandlers.ts | 32 +++- src/main/lib/proactiveGreetingTelemetry.ts | 58 +++++++ src/main/lib/telemetry.test.ts | 148 ++++++++++++++++++ src/main/lib/telemetry.ts | 43 ++++- src/preload/comfyPreload.test.ts | 24 +++ src/preload/comfyPreload.ts | 3 + src/types/comfyDesktopBridge.ts | 4 + 9 files changed, 379 insertions(+), 5 deletions(-) create mode 100644 src/main/lib/proactiveGreetingTelemetry.ts diff --git a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts index ac6c9fd02..b2025e769 100644 --- a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts +++ b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts @@ -158,6 +158,10 @@ export interface ComfyDesktop2LogsBridge { onOutput(callback: (msg: LogsOutputMsg) => void): () => void } export interface ComfyDesktop2TelemetryBridge { + /** Supports validated user/workspace attribution for the proactive greeting experiment. */ + readonly proactiveGreetingExperimentVersion?: 1 + /** Current analytics eligibility only; capture rechecks consent and actor before delivery. */ + canEnrollProactiveGreeting?(userId: string): Promise capture(event: string, properties?: ComfyDesktop2TelemetryProperties): void /** Capture a hosted-frontend exception through Desktop's privacy and release-context boundary. */ captureException?(error: ComfyDesktop2Error, properties?: ComfyDesktop2TelemetryProperties): void diff --git a/src/main/lib/ipc/registerTelemetryHandlers.test.ts b/src/main/lib/ipc/registerTelemetryHandlers.test.ts index ca3f08967..8369b2103 100644 --- a/src/main/lib/ipc/registerTelemetryHandlers.test.ts +++ b/src/main/lib/ipc/registerTelemetryHandlers.test.ts @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' const mocks = vi.hoisted(() => ({ capture: vi.fn(), + canEnrollProactiveGreeting: vi.fn(() => true), captureExceptionAndForward: vi.fn((_error: unknown, _properties: unknown) => true), findEntryByComfySender: vi.fn(), getFlag: vi.fn(), @@ -9,6 +10,7 @@ const mocks = vi.hoisted(() => ({ on: vi.fn(), recordExposure: vi.fn(), reportFirebaseAuthState: vi.fn(), + viewsReportingFirebaseUser: vi.fn((): unknown[] => []), registerPersonProperties: vi.fn() })) @@ -28,12 +30,14 @@ vi.mock('../telemetry', () => ({ // pull in telemetry.ts's electron/posthog-node imports under the stub mock. asDeployment: (v: unknown) => (v === 'local' || v === 'cloud' || v === 'remote' ? v : null), emit: mocks.capture, + canEnrollProactiveGreeting: mocks.canEnrollProactiveGreeting, captureExceptionAndForward: mocks.captureExceptionAndForward, registerPersonProperties: mocks.registerPersonProperties })) vi.mock('../firebaseAuthIdentity', () => ({ - reportFirebaseAuthState: mocks.reportFirebaseAuthState + reportFirebaseAuthState: mocks.reportFirebaseAuthState, + viewsReportingFirebaseUser: mocks.viewsReportingFirebaseUser })) vi.mock('../experiments', () => ({ @@ -74,6 +78,68 @@ describe('registerTelemetryHandlers', () => { registerTelemetryHandlers() }) + it('admits flat pair attribution only from the main frame of its authenticated hosted view', () => { + const event = identityEvent('https://cloud.comfy.org/') as { sender: unknown } + const properties = { + proactive_greeting_user_id: 'user-a', + proactive_greeting_workspace_id: 'workspace-a', + proactive_greeting_group_key: '["user-a","workspace-a"]', + proactive_greeting_variant: 'generic', + canvas_state: 'workflow' + } + mocks.findEntryByComfySender.mockReturnValue({ sourceCategory: 'local' }) + mocks.viewsReportingFirebaseUser.mockReturnValue([event.sender]) + listener('telemetry:capture')(event, { event: '$experiment_exposure', properties }) + expect(mocks.capture).toHaveBeenCalledWith('$experiment_exposure', { + ...properties, + deployment: 'local' + }) + mocks.capture.mockClear() + mocks.viewsReportingFirebaseUser.mockReturnValue([]) + listener('telemetry:capture')(event, { event: '$experiment_exposure', properties }) + expect(mocks.capture).not.toHaveBeenCalled() + mocks.viewsReportingFirebaseUser.mockReturnValue([event.sender]) + listener('telemetry:capture')( + { ...event, senderFrame: null }, + { event: '$experiment_exposure', properties } + ) + expect(mocks.capture).not.toHaveBeenCalled() + }) + + it('rejects malformed or incomplete experiment events before the generic relay', () => { + listener('telemetry:capture')(null, { + event: '$experiment_exposure', + properties: { + $feature_flag: 'agent-proactive-greeting-v1' + } + }) + listener('telemetry:capture')(null, { + event: 'app:agent_message_sent', + properties: { + proactive_greeting_user_id: 'user-a' + } + }) + expect(mocks.capture).not.toHaveBeenCalled() + }) + + it('answers enrollment eligibility only for the authenticated hosted main-frame actor', () => { + const handler = mocks.handle.mock.calls.find( + ([channel]) => channel === 'telemetry:can-enroll-proactive-greeting' + )![1] as (event: unknown, user: unknown) => boolean + const event = identityEvent('https://cloud.comfy.org/') as { sender: unknown } + mocks.findEntryByComfySender.mockReturnValue({ sourceCategory: 'local' }) + mocks.viewsReportingFirebaseUser.mockReturnValue([event.sender]) + mocks.canEnrollProactiveGreeting.mockReturnValue(true) + expect(handler(event, 'user-a')).toBe(true) + expect(handler(event, ' user-a ')).toBe(false) + expect(handler({ ...event, senderFrame: null }, 'user-a')).toBe(false) + mocks.canEnrollProactiveGreeting.mockReturnValue(false) + expect(handler(event, 'user-a')).toBe(false) + mocks.canEnrollProactiveGreeting.mockReturnValue(true) + mocks.viewsReportingFirebaseUser.mockReturnValue([]) + expect(handler(event, 'user-a')).toBe(false) + }) + it('caps event property keys, arrays, and strings', () => { const properties: Record = { long: 'x'.repeat(3000), diff --git a/src/main/lib/ipc/registerTelemetryHandlers.ts b/src/main/lib/ipc/registerTelemetryHandlers.ts index 31dffbaa5..37086ad38 100644 --- a/src/main/lib/ipc/registerTelemetryHandlers.ts +++ b/src/main/lib/ipc/registerTelemetryHandlers.ts @@ -10,7 +10,8 @@ import { type ExperimentExposureSource } from '../experiments' import { normalizeExceptionContext } from '../../../shared/piiScrub' -import { reportFirebaseAuthState } from '../firebaseAuthIdentity' +import { reportFirebaseAuthState, viewsReportingFirebaseUser } from '../firebaseAuthIdentity' +import { parseProactiveGreetingAttribution } from '../proactiveGreetingTelemetry' import type { ComfyDesktop2FirebaseAuthState } from '../../../types/comfyDesktopBridge' import { normalizePostHogUserId } from '../opaqueIdentifier' @@ -29,7 +30,9 @@ function asString(value: unknown): string | null { return typeof value === 'string' && value.length > 0 ? value : null } -function firebaseAuthReporterFrame(event: IpcMainEvent): WebFrameMain | null { +type HostedSenderEvent = Pick + +function firebaseAuthReporterFrame(event: HostedSenderEvent): WebFrameMain | null { const frame = event.senderFrame const mainFrame = event.sender.mainFrame if (!frame || frame.processId !== mainFrame.processId || frame.routingId !== mainFrame.routingId) @@ -191,11 +194,34 @@ function withPlatformAxes( return properties } +function isProactiveGreetingSender(event: HostedSenderEvent, userId: string): boolean { + return Boolean( + event && + firebaseAuthReporterFrame(event) && + findEntryByComfySender(event.sender) && + viewsReportingFirebaseUser(userId).includes(event.sender) + ) +} + export function registerTelemetryHandlers(): void { + ipcMain.handle('telemetry:can-enroll-proactive-greeting', (event, value: unknown) => { + const userId = normalizePostHogUserId(value) + return ( + userId !== null && + userId === value && + isProactiveGreetingSender(event, userId) && + mainTelemetry.canEnrollProactiveGreeting(userId) + ) + }) + ipcMain.on('telemetry:capture', (event, payload: CapturePayload) => { const eventName = asString(payload?.event) if (!eventName) return - mainTelemetry.emit(eventName, withPlatformAxes(asProps(payload.properties), event?.sender)) + const properties = asProps(payload.properties) + const attribution = parseProactiveGreetingAttribution(eventName, properties) + if (attribution === null) return + if (attribution && !isProactiveGreetingSender(event, attribution.userId)) return + mainTelemetry.emit(eventName, withPlatformAxes(properties, event?.sender)) }) ipcMain.on('telemetry:captureException', (event, payload: CaptureExceptionPayload) => { diff --git a/src/main/lib/proactiveGreetingTelemetry.ts b/src/main/lib/proactiveGreetingTelemetry.ts new file mode 100644 index 000000000..d20c91111 --- /dev/null +++ b/src/main/lib/proactiveGreetingTelemetry.ts @@ -0,0 +1,58 @@ +import { normalizeOpaqueIdentifier, normalizePostHogUserId } from './opaqueIdentifier' + +export const PROACTIVE_GREETING_FLAG = 'agent-proactive-greeting-v1' +const PREFIX = 'proactive_greeting_' +const VARIANTS = new Set(['control', 'generic', 'canvas-prefill', 'canvas-only']) + +export interface ProactiveGreetingAttribution { + userId: string + workspaceId: string + groupKey: string + variant: string +} + +/** undefined is unrelated telemetry; null is a malformed experiment event. */ +export function parseProactiveGreetingAttribution( + event: string, + properties: Record +): ProactiveGreetingAttribution | null | undefined { + const marked = Object.keys(properties).some((key) => key.startsWith(PREFIX)) + const ownExposure = + event === '$experiment_exposure' && properties.$feature_flag === PROACTIVE_GREETING_FLAG + if (!marked && !ownExposure) return undefined + if (event !== '$experiment_exposure' && event !== 'app:agent_message_sent') return null + const userId = normalizePostHogUserId(properties.proactive_greeting_user_id) + const workspaceId = normalizeOpaqueIdentifier(properties.proactive_greeting_workspace_id, 256) + const groupKey = properties.proactive_greeting_group_key + const variant = properties.proactive_greeting_variant + if ( + !userId || + userId !== properties.proactive_greeting_user_id || + !workspaceId || + workspaceId !== properties.proactive_greeting_workspace_id || + typeof groupKey !== 'string' || + groupKey !== JSON.stringify([userId, workspaceId]) || + typeof variant !== 'string' || + !VARIANTS.has(variant) + ) + return null + if ( + event === '$experiment_exposure' && + properties.canvas_state !== 'empty' && + properties.canvas_state !== 'workflow' + ) + return null + return { userId, workspaceId, groupKey, variant } +} + +/** Construct reserved PostHog properties only after validating the originating actor. */ +export function proactiveGreetingProperties(attribution: ProactiveGreetingAttribution) { + return { + distinct_id: attribution.userId, + $user_id: attribution.userId, + user_id: attribution.userId, + workspace_id: attribution.workspaceId, + $groups: { user_workspace: attribution.groupKey }, + [`$feature/${PROACTIVE_GREETING_FLAG}`]: attribution.variant + } +} diff --git a/src/main/lib/telemetry.test.ts b/src/main/lib/telemetry.test.ts index 70c1d7732..c559553e6 100644 --- a/src/main/lib/telemetry.test.ts +++ b/src/main/lib/telemetry.test.ts @@ -339,6 +339,154 @@ function setupTelemetry(options: SetupTelemetryOptions = {}): void { telemetry._test_resetVolumeGuards() } +describe('proactive greeting pair attribution', () => { + const exposure = '$experiment_exposure' + function pair(workspace = 'workspace-a', user = 'firebase-user'): Record { + return { + proactive_greeting_user_id: user, + proactive_greeting_workspace_id: workspace, + proactive_greeting_group_key: JSON.stringify([user, workspace]), + proactive_greeting_variant: 'control', + canvas_state: 'empty' + } + } + function signIn(): void { + setupTelemetry() + telemetry.applyFirebaseUserConsensus('firebase-user') + captured.length = 0 + } + const exposures = () => captured.filter((call) => call.event === exposure) + + it('only permits enrollment for the current authenticated actor with granted analytics consent', () => { + setupTelemetry() + expect(telemetry.canEnrollProactiveGreeting('test-distinct-id')).toBe(false) + telemetry.applyFirebaseUserConsensus('firebase-user') + expect(telemetry.canEnrollProactiveGreeting('firebase-user')).toBe(true) + expect(telemetry.canEnrollProactiveGreeting('other-user')).toBe(false) + for (const consent of ['denied', 'undecided'] as const) { + telemetry.setConsentState(consent) + expect(telemetry.canEnrollProactiveGreeting('firebase-user')).toBe(false) + } + telemetry.setConsentState('granted') + telemetry.applyFirebasePendingConsensus() + expect(telemetry.canEnrollProactiveGreeting('firebase-user')).toBe(false) + telemetry.applyFirebaseUserConsensus('firebase-user') + expect(telemetry.canEnrollProactiveGreeting('firebase-user')).toBe(true) + setupTelemetry({ env: { COMFY_DESKTOP_POSTHOG_ENABLED: '0' } }) + telemetry.applyFirebaseUserConsensus('firebase-user') + expect(telemetry.canEnrollProactiveGreeting('firebase-user')).toBe(false) + }) + + it.each(['control', 'generic', 'canvas-prefill', 'canvas-only'])( + 'attributes %s exposure and send to the canonical user and frozen workspace', + (variant) => { + signIn() + const properties = { + ...pair(), + proactive_greeting_variant: variant, + distinct_id: 'spoofed-user', + $user_id: 'spoofed-user' + } + telemetry.capture(exposure, properties) + telemetry.capture('app:agent_message_sent', properties) + expect(captured).toHaveLength(2) + for (const call of captured) { + expect(call.distinctId).toBe('firebase-user') + expect(call.properties).toMatchObject({ + $groups: { user_workspace: '["firebase-user","workspace-a"]' }, + '$feature/agent-proactive-greeting-v1': variant, + user_id: 'firebase-user', + distinct_id: 'firebase-user', + $user_id: 'firebase-user', + workspace_id: 'workspace-a' + }) + } + expect(exposures()[0]?.properties).toMatchObject({ + $feature_flag: 'agent-proactive-greeting-v1', + $feature_flag_response: variant + }) + } + ) + + it.each([ + { proactive_greeting_group_key: '["different-user","workspace-a"]' }, + { proactive_greeting_workspace_id: '' }, + { proactive_greeting_variant: 'unknown' }, + { proactive_greeting_user_id: ' firebase-user ' }, + { canvas_state: 'unknown' } + ])('rejects malformed attribution %j', (invalid) => { + signIn() + expect(telemetry.capture(exposure, { ...pair(), ...invalid })).toBe(false) + expect(exposures()).toHaveLength(0) + }) + + it('deduplicates only successful exposure admission per pair and variant in this process', () => { + signIn() + posthogClientMock.failNextCaptures = 1 + expect(telemetry.capture(exposure, pair())).toBe(false) + expect(telemetry.capture(exposure, pair())).toBe(true) + expect(telemetry.capture(exposure, pair())).toBe(false) + telemetry.capture(exposure, pair('workspace-b')) + expect(exposures()).toHaveLength(2) + telemetry.capture('app:agent_message_sent', pair()) + telemetry.capture('app:agent_message_sent', pair()) + expect(captured.filter((call) => call.event === 'app:agent_message_sent')).toHaveLength(2) + }) + + it.each(['denied', 'undecided'] as const)( + 'does not consume exposure while consent is %s', + (consent) => { + signIn() + telemetry.setConsentState(consent) + expect(telemetry.capture(exposure, pair())).toBe(false) + expect(exposures()).toHaveLength(0) + telemetry.setConsentState('granted') + expect(telemetry.capture(exposure, pair())).toBe(true) + } + ) + + it('drops old-user events after account switch rather than attributing them to the new user', () => { + signIn() + telemetry.applyFirebaseUserConsensus('other-user') + expect(telemetry.capture('app:agent_message_sent', pair())).toBe(false) + expect(telemetry.capture(exposure, pair())).toBe(false) + expect(exposures()).toHaveLength(0) + }) + + it('holds and deduplicates exposure during quarantine, retaining originating workspace', () => { + signIn() + telemetry.applyFirebasePendingConsensus() + telemetry.capture(exposure, pair()) + telemetry.capture(exposure, pair()) + telemetry.capture('app:agent_message_sent', pair('workspace-b')) + expect(exposures()).toHaveLength(0) + telemetry.applyFirebaseUserConsensus('firebase-user') + expect(exposures()).toHaveLength(1) + expect( + captured.find((call) => call.event === 'app:agent_message_sent')?.properties + ).toMatchObject({ + $groups: { user_workspace: '["firebase-user","workspace-b"]' } + }) + }) + + it('does not replay quarantined exposure after consent is revoked', () => { + signIn() + telemetry.applyFirebasePendingConsensus() + telemetry.capture(exposure, pair()) + telemetry.setConsentState('denied') + telemetry.applyFirebaseUserConsensus('firebase-user') + expect(exposures()).toHaveLength(0) + }) + + it('discards quarantined old-user attribution when consensus changes account', () => { + signIn() + telemetry.applyFirebasePendingConsensus() + telemetry.capture(exposure, pair()) + telemetry.applyFirebaseUserConsensus('other-user') + expect(exposures()).toHaveLength(0) + }) +}) + afterEach(() => { anonymousIdentityMock.index = 0 anonymousIdentityMock.fail = false diff --git a/src/main/lib/telemetry.ts b/src/main/lib/telemetry.ts index 73941b6dd..d324244c3 100644 --- a/src/main/lib/telemetry.ts +++ b/src/main/lib/telemetry.ts @@ -110,6 +110,11 @@ */ import { app } from 'electron' import { PostHog } from 'posthog-node' +import { + parseProactiveGreetingAttribution, + proactiveGreetingProperties, + PROACTIVE_GREETING_FLAG +} from './proactiveGreetingTelemetry' import { AsyncLocalStorage } from 'node:async_hooks' // PostHog's `FeatureFlagValue` lives in `@posthog/core` and is not @@ -190,6 +195,7 @@ export function _resetTelemetryRelayTargets(): void { * close an in-flight PostHog client - tests bring their own mocked one. */ export function _resetForTest(): void { + proactiveGreetingExposures.clear() client = null distinctId = null anonymousDistinctId = null @@ -1415,6 +1421,20 @@ function capturePersonProperties( return capture('comfy.desktop.person.set', properties) } +// Process-local dedup only: this is not a cross-install enrollment ledger. +const proactiveGreetingExposures = new Set() + +/** Read-only eligibility: never bind an identity or bypass the host's consent owner. */ +export function canEnrollProactiveGreeting(userId: string): boolean { + return ( + canEmit() && + consentState === 'granted' && + boundUserId === userId && + distinctId === userId && + !firebaseWritesQuarantined() + ) +} + /** * Returns whether the event was admitted. A `false` means it was dropped * (consent gate, volume guard, or SDK failure); one-shot callers use this to @@ -1433,6 +1453,8 @@ function captureEvent(event: string, properties: TelemetryContext, forward: bool recordIpcInvocation(`telemetry:${event}`, properties) if (!canEmit() || !distinctId) return false if (!isAllowedToFire(event)) return false + const attribution = parseProactiveGreetingAttribution(event, properties) + if (attribution === null || (attribution && attribution.userId !== boundUserId)) return false if (!_checkRateLimit(event)) return false if (firebaseWritesQuarantined()) { if ( @@ -1454,6 +1476,14 @@ function deliverEvent( timestamp: Date | null ): boolean { try { + // Recheck after quarantine as well. A delayed event must never acquire a new actor. + const attribution = parseProactiveGreetingAttribution(event, properties) + if (attribution === null || (attribution && attribution.userId !== boundUserId)) return false + const exposureKey = + attribution && event === '$experiment_exposure' + ? JSON.stringify([attribution.groupKey, attribution.variant]) + : null + if (exposureKey && proactiveGreetingExposures.has(exposureKey)) return false // Per-call properties override defaults on key collision - callers // that explicitly pass `app_version` (e.g. session-start payload, // legacy event re-emitters) win. @@ -1465,9 +1495,20 @@ function deliverEvent( client!.capture({ distinctId: distinctId!, event, - properties: scrubProperties(merged), + properties: { + ...scrubProperties(merged), + ...(attribution ? proactiveGreetingProperties(attribution) : {}), + ...(exposureKey + ? { + $feature_flag: PROACTIVE_GREETING_FLAG, + $feature_flag_response: attribution!.variant + } + : {}) + }, ...(timestamp ? { timestamp } : {}) }) + // An SDK rejection or consent/quarantine rejection must not consume enrollment. + if (exposureKey) proactiveGreetingExposures.add(exposureKey) if (forward) forwardToRenderer(event, properties) return true } catch { diff --git a/src/preload/comfyPreload.test.ts b/src/preload/comfyPreload.test.ts index 9fc33f096..951c5f251 100644 --- a/src/preload/comfyPreload.test.ts +++ b/src/preload/comfyPreload.test.ts @@ -78,6 +78,30 @@ describe('comfyPreload model access bridge', () => { mocks.send.mockReset() }) + it('queries current host enrollment eligibility without reporting an exposure', async () => { + mocks.invoke.mockResolvedValue(false) + expect(await hostedBridge().Telemetry.canEnrollProactiveGreeting!('user-a')).toBe(false) + expect(mocks.invoke).toHaveBeenCalledWith('telemetry:can-enroll-proactive-greeting', 'user-a') + expect(mocks.send).not.toHaveBeenCalled() + }) + + it('advertises pair attribution and relays its flat fields through the existing capture channel', () => { + const bridge = hostedBridge() + expect(bridge.Telemetry.proactiveGreetingExperimentVersion).toBe(1) + const properties = { + proactive_greeting_user_id: 'user-a', + proactive_greeting_workspace_id: 'workspace-a', + proactive_greeting_group_key: '["user-a","workspace-a"]', + proactive_greeting_variant: 'control', + canvas_state: 'empty' + } + bridge.Telemetry.capture('$experiment_exposure', properties) + expect(mocks.send).toHaveBeenCalledWith('telemetry:capture', { + event: '$experiment_exposure', + properties + }) + }) + it('forwards hosted frontend exceptions through the scrubbed Desktop telemetry channel', () => { const bridge = hostedBridge() const error = new Error('boom') diff --git a/src/preload/comfyPreload.ts b/src/preload/comfyPreload.ts index cbd3612e0..916a3328a 100644 --- a/src/preload/comfyPreload.ts +++ b/src/preload/comfyPreload.ts @@ -101,6 +101,9 @@ const captureException: NonNullable => + ipcRenderer.invoke('telemetry:can-enroll-proactive-greeting', userId), capture: (event, properties): void => sendTelemetry('telemetry:capture', { event, properties }), captureException, reportFirebaseAuthState diff --git a/src/types/comfyDesktopBridge.ts b/src/types/comfyDesktopBridge.ts index 814dc904e..e9e78072a 100644 --- a/src/types/comfyDesktopBridge.ts +++ b/src/types/comfyDesktopBridge.ts @@ -160,6 +160,10 @@ export interface ComfyDesktop2LogsBridge { } export interface ComfyDesktop2TelemetryBridge { + /** Supports validated user/workspace attribution for the proactive greeting experiment. */ + readonly proactiveGreetingExperimentVersion?: 1 + /** Current analytics eligibility only; capture rechecks consent and actor before delivery. */ + canEnrollProactiveGreeting?(userId: string): Promise capture(event: string, properties?: ComfyDesktop2TelemetryProperties): void /** Capture a hosted-frontend exception through Desktop's privacy and release-context boundary. */ captureException?(error: ComfyDesktop2Error, properties?: ComfyDesktop2TelemetryProperties): void From 25ffaadd0949e08bcd0a0564f9aba2b783789764 Mon Sep 17 00:00:00 2001 From: ryan-di Date: Sat, 7 Nov 2026 09:46:10 +0800 Subject: [PATCH 2/5] chore: publish proactive greeting bridge capability in types 0.7.0 --- 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 93c5e1d04..ad59ecade 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.6.0", + "version": "0.7.0", "description": "TypeScript definitions for the Comfy Desktop hosted frontend bridge", "type": "module", "main": "./index.js", From d9688d163277210d72924bb858bfc2c2009453d0 Mon Sep 17 00:00:00 2001 From: ryan-di Date: Sat, 7 Nov 2026 12:12:51 +0800 Subject: [PATCH 3/5] fix: pass experiment groups through the PostHog capture API --- src/main/lib/telemetry.test.ts | 11 +++++------ src/main/lib/telemetry.ts | 1 + 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/lib/telemetry.test.ts b/src/main/lib/telemetry.test.ts index c559553e6..c45890b85 100644 --- a/src/main/lib/telemetry.test.ts +++ b/src/main/lib/telemetry.test.ts @@ -3,6 +3,7 @@ import fs from 'fs' import os from 'os' import path from 'path' import { EventEmitter } from 'events' +import type { PostHog } from 'posthog-node' import type { TelemetryValue } from './telemetry' import { DEFAULT_POSTHOG_API_KEY, DEFAULT_POSTHOG_HOST } from '../../shared/posthogConfig' @@ -45,12 +46,7 @@ function makeStubWebContents(): { } } -interface CapturedCall { - distinctId: string - event: string - properties?: Record - timestamp?: Date -} +type CapturedCall = Parameters[0] const captured: CapturedCall[] = [] interface IdentifyCall { @@ -392,6 +388,9 @@ describe('proactive greeting pair attribution', () => { expect(captured).toHaveLength(2) for (const call of captured) { expect(call.distinctId).toBe('firebase-user') + expect(call.groups).toEqual({ + user_workspace: '["firebase-user","workspace-a"]' + }) expect(call.properties).toMatchObject({ $groups: { user_workspace: '["firebase-user","workspace-a"]' }, '$feature/agent-proactive-greeting-v1': variant, diff --git a/src/main/lib/telemetry.ts b/src/main/lib/telemetry.ts index d324244c3..ca041d875 100644 --- a/src/main/lib/telemetry.ts +++ b/src/main/lib/telemetry.ts @@ -1495,6 +1495,7 @@ function deliverEvent( client!.capture({ distinctId: distinctId!, event, + ...(attribution ? { groups: { user_workspace: attribution.groupKey } } : {}), properties: { ...scrubProperties(merged), ...(attribution ? proactiveGreetingProperties(attribution) : {}), From 94e277d4f81d3250aa375e1e6559db9c78ad6447 Mon Sep 17 00:00:00 2001 From: ryan-di Date: Tue, 10 Nov 2026 07:25:12 +0800 Subject: [PATCH 4/5] fix: acknowledge and refresh proactive greeting admission --- .../comfyDesktopBridge.d.ts | 14 +- src/main/host/attach.ts | 3 + src/main/host/registry.ts | 2 + src/main/lib/firebaseAuthIdentity.test.ts | 28 ++ src/main/lib/firebaseAuthIdentity.ts | 40 +++ .../lib/ipc/registerTelemetryHandlers.test.ts | 284 ++++++++++++++++-- src/main/lib/ipc/registerTelemetryHandlers.ts | 202 ++++++++++++- src/main/lib/proactiveGreetingTelemetry.ts | 4 + src/main/lib/telemetry.test.ts | 86 ++++++ src/main/lib/telemetry.ts | 134 +++++++-- src/preload/comfyPreload.test.ts | 46 ++- src/preload/comfyPreload.ts | 14 +- src/types/comfyDesktopBridge.ts | 15 +- 13 files changed, 801 insertions(+), 71 deletions(-) diff --git a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts index b2025e769..5594b2e55 100644 --- a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts +++ b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts @@ -157,11 +157,19 @@ export interface ComfyDesktop2LogsBridge { openPopout(): Promise onOutput(callback: (msg: LogsOutputMsg) => void): () => void } +export type ComfyDesktop2ProactiveGreetingAdmission = 'allowed' | 'denied' | 'verify-firebase' export interface ComfyDesktop2TelemetryBridge { /** Supports validated user/workspace attribution for the proactive greeting experiment. */ - readonly proactiveGreetingExperimentVersion?: 1 - /** Current analytics eligibility only; capture rechecks consent and actor before delivery. */ - canEnrollProactiveGreeting?(userId: string): Promise + readonly proactiveGreetingExperimentVersion?: 1 | 2 + /** Current analytics eligibility. Pass Firebase proof only in response to verify-firebase. */ + getProactiveGreetingAdmission?( + userId: string, + firebaseIdToken?: string + ): Promise + /** Acknowledges local handoff to analytics, including a previously admitted pair and arm. */ + enrollProactiveGreeting?(properties: ComfyDesktop2TelemetryProperties): Promise + /** Signals that the hosted view should recheck its current actor's eligibility. */ + onProactiveGreetingAdmissionChanged?(callback: () => void): () => void capture(event: string, properties?: ComfyDesktop2TelemetryProperties): void /** Capture a hosted-frontend exception through Desktop's privacy and release-context boundary. */ captureException?(error: ComfyDesktop2Error, properties?: ComfyDesktop2TelemetryProperties): void diff --git a/src/main/host/attach.ts b/src/main/host/attach.ts index a353b8ff0..6bb143573 100644 --- a/src/main/host/attach.ts +++ b/src/main/host/attach.ts @@ -152,6 +152,7 @@ export function attachInstall(entry: ComfyWindowEntry, opts: AttachInstallOpts): // truth for `getEntryByInstallationId(id)` — keep it in lockstep // with `entry.installationId` (detach symmetrically clears both). entry.installationId = installationId + entry.sourceId = installation.sourceId entry.comfyUrl = comfyUrl entry.titleBarText = installation.name entry.sourceCategory = sourceMap[installation.sourceId]?.category ?? null @@ -214,6 +215,7 @@ export function attachInstall(entry: ComfyWindowEntry, opts: AttachInstallOpts): // release-cache resolves in the background). const onInstallationUpdated = (updated: InstallationRecord): void => { if (updated.id !== entry.installationId) return + entry.sourceId = updated.sourceId const nextTabText = updated.name if (nextTabText !== entry.titleBarText) { entry.titleBarText = nextTabText @@ -704,6 +706,7 @@ export function attachInstall(entry: ComfyWindowEntry, opts: AttachInstallOpts): entry.installationId = null } entry.comfyUrl = '' + delete entry.sourceId } return true } diff --git a/src/main/host/registry.ts b/src/main/host/registry.ts index 94df42e08..390a28faf 100644 --- a/src/main/host/registry.ts +++ b/src/main/host/registry.ts @@ -108,6 +108,8 @@ export interface ComfyWindowEntry { * `_installCleanup` clears it — that invariant is what makes attachInstall's * already-attached guard work. */ installationId: string | null + /** Source of the attached installation, never the chooser's preview. */ + sourceId?: string /** Partition the comfyView was constructed with (pinned; Electron can't * change it without rebuilding). A chooser-pick claim must reject any install * whose partition differs, else session data leaks across partition buckets. */ diff --git a/src/main/lib/firebaseAuthIdentity.test.ts b/src/main/lib/firebaseAuthIdentity.test.ts index 5d6894f43..0c04f82c4 100644 --- a/src/main/lib/firebaseAuthIdentity.test.ts +++ b/src/main/lib/firebaseAuthIdentity.test.ts @@ -48,6 +48,7 @@ import { bindMainVerifiedFirebaseUser, deactivateFirebaseAuthReporter, getFirebaseIdentityConsensus, + getUnverifiedLocalFirebaseSignInReport, observeFirebaseIdentityConsensus, PENDING_CONSENSUS_DEADLINE_MS, reportFirebaseAuthState as recordFirebaseAuthState, @@ -204,6 +205,33 @@ describe('firebaseAuthIdentity consensus', () => { verifiedLocalPersistence.succeeds = true }) + it('keeps unverified Local reports out of consensus and expires them on sign-out or navigation', () => { + const local = new FakeWebContents('http://127.0.0.1:8188/') + const contents = local.asWebContents() + const signedIn = { status: 'signed_in', userId: 'F' } as const + activate(local) + reportFirebaseAuthState(contents, signedIn) + const initialReport = getUnverifiedLocalFirebaseSignInReport(contents)! + expect(initialReport.userId).toBe('F') + expect(viewsReportingFirebaseUser('F')).toEqual([]) + expect(telemetry.applyFirebaseUserConsensus).not.toHaveBeenCalled() + + reportFirebaseAuthState(contents, { status: 'signed_out' }) + expect(getUnverifiedLocalFirebaseSignInReport(contents)).toBeNull() + reportFirebaseAuthState(contents, signedIn) + const currentReport = getUnverifiedLocalFirebaseSignInReport(contents)! + expect(currentReport).not.toBe(initialReport) + + local.navigate(local.getURL()) + expect(getUnverifiedLocalFirebaseSignInReport(contents)).toBeNull() + reportFirebaseAuthState(contents, signedIn) + expect(getUnverifiedLocalFirebaseSignInReport(contents)).not.toBe(currentReport) + expect(viewsReportingFirebaseUser('F')).toEqual([]) + expect(telemetry.applyFirebaseUserConsensus).not.toHaveBeenCalled() + expect(telemetry.bindUserId).not.toHaveBeenCalled() + expect(verifiedLocalUsers.size).toBe(0) + }) + it('waits for every live trusted reporter before binding one agreed user', () => { const first = new FakeWebContents(cloudUrl) const second = new FakeWebContents(cloudUrl) diff --git a/src/main/lib/firebaseAuthIdentity.ts b/src/main/lib/firebaseAuthIdentity.ts index c2df4ea6c..c53c05535 100644 --- a/src/main/lib/firebaseAuthIdentity.ts +++ b/src/main/lib/firebaseAuthIdentity.ts @@ -10,6 +10,11 @@ import { readVerifiedLocalFirebaseUser } from './verifiedLocalFirebaseAuth' +interface LocalFirebaseSignInReport { + readonly userId: string + readonly origin: string +} + interface Reporter { eligible: boolean active: boolean @@ -18,6 +23,7 @@ interface Reporter { pendingExpired: boolean localReportingAuthorized: boolean localExpectedUserId: string | null + localSignInReport: LocalFirebaseSignInReport | null /** Where this view was last seen signed in; storage is per origin, and a view can navigate. */ signedInOrigin: string | null awaitingCommittedFrame: boolean @@ -254,6 +260,24 @@ function currentMainFrame(webContents: WebContents): FirebaseAuthFrameIdentity { } } +/** A current report is only a candidate for verification, never proof of identity. */ +export function getUnverifiedLocalFirebaseSignInReport( + webContents: WebContents +): LocalFirebaseSignInReport | null { + const reporter = reporters.get(webContents) + if (!reporter?.eligible || reporter.localReportingAuthorized || webContents.isDestroyed()) { + return null + } + const report = reporter.localSignInReport + return report && + report.origin === originOf(webContents.getURL()) && + !reporter.awaitingCommittedFrame && + reporter.mainFrameNavigationsInFlight === 0 && + isSameFrame(reporter.committedFrame, currentMainFrame(webContents)) + ? report + : null +} + function failureTerminalKey( errorCode: number, validatedURL: string, @@ -610,6 +634,7 @@ export function trackFirebaseAuthReporter(webContents: WebContents): void { pendingExpired: false, localReportingAuthorized: false, localExpectedUserId: null, + localSignInReport: null, signedInOrigin: null, awaitingCommittedFrame: true, mainFrameNavigationsInFlight: 0, @@ -620,6 +645,7 @@ export function trackFirebaseAuthReporter(webContents: WebContents): void { state: { status: 'pending' }, onDidStartNavigation: (details) => { if (!details.isMainFrame || details.isSameDocument) return + reporter.localSignInReport = null if ( reporter.mainFrameNavigationsInFlight === 0 && !reporter.awaitingCommittedFrame && @@ -745,6 +771,7 @@ export function activateFirebaseAuthReporter(webContents: WebContents): void { reporter.pendingExpired = false reporter.awaitingCommittedFrame = true reporter.committedFrame = null + reporter.localSignInReport = null reporter.recoverableState = null reporter.committedCandidate = null reporter.active = refreshReporterAuthScope(reporter, webContents.getURL()) @@ -758,6 +785,7 @@ export function deactivateFirebaseAuthReporter(webContents: WebContents): void { if (!reporter) return reporter.eligible = false reporter.active = false + reporter.localSignInReport = null reporter.state = { status: 'pending' } mainVerifiedStates.delete(webContents) reconcile() @@ -773,6 +801,18 @@ export function reportFirebaseAuthState( const reporter = reporters.get(webContents) if (!reporter?.eligible) return const currentOrigin = originOf(webContents.getURL()) + if ( + currentOrigin !== null && + isLoopbackOrigin(currentOrigin) && + !reporter.awaitingCommittedFrame && + reporter.mainFrameNavigationsInFlight === 0 && + isSameFrame(reporter.committedFrame, frame) + ) { + if (state.status !== 'signed_in') reporter.localSignInReport = null + else if (reporter.localSignInReport?.userId !== state.userId) { + reporter.localSignInReport = { userId: state.userId, origin: currentOrigin } + } + } const trustedCloud = isTrustedCloudUrl(webContents.getURL()) const trustedLocal = currentOrigin !== null && isLoopbackOrigin(currentOrigin) && reporter.localReportingAuthorized diff --git a/src/main/lib/ipc/registerTelemetryHandlers.test.ts b/src/main/lib/ipc/registerTelemetryHandlers.test.ts index 8369b2103..c846293e4 100644 --- a/src/main/lib/ipc/registerTelemetryHandlers.test.ts +++ b/src/main/lib/ipc/registerTelemetryHandlers.test.ts @@ -2,9 +2,22 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' const mocks = vi.hoisted(() => ({ capture: vi.fn(), + captureVerifiedProactiveGreeting: vi.fn(), canEnrollProactiveGreeting: vi.fn(() => true), + canCaptureVerifiedProactiveGreeting: vi.fn(() => true), + enrollProactiveGreeting: vi.fn(() => true), + getProactiveGreetingActor: vi.fn<() => string | null>(() => null), + getProactiveGreetingConsentRevision: vi.fn<() => number | null>(() => 1), + getUnverifiedLocalFirebaseSignInReport: vi.fn(), + getFirebaseIdentityConsensus: vi.fn<() => { status: string; userId?: string }>(() => ({ + status: 'unknown' + })), + observeFirebaseIdentityConsensus: vi.fn(), + lookupAccount: vi.fn(), + observeProactiveGreetingAdmissionChanged: vi.fn(), captureExceptionAndForward: vi.fn((_error: unknown, _properties: unknown) => true), findEntryByComfySender: vi.fn(), + comfyWindows: new Map(), getFlag: vi.fn(), handle: vi.fn(), on: vi.fn(), @@ -22,7 +35,8 @@ vi.mock('electron', () => ({ })) vi.mock('../../host/registry', () => ({ - findEntryByComfySender: mocks.findEntryByComfySender + findEntryByComfySender: mocks.findEntryByComfySender, + comfyWindows: mocks.comfyWindows })) vi.mock('../telemetry', () => ({ @@ -30,14 +44,27 @@ vi.mock('../telemetry', () => ({ // pull in telemetry.ts's electron/posthog-node imports under the stub mock. asDeployment: (v: unknown) => (v === 'local' || v === 'cloud' || v === 'remote' ? v : null), emit: mocks.capture, + captureVerifiedProactiveGreeting: mocks.captureVerifiedProactiveGreeting, canEnrollProactiveGreeting: mocks.canEnrollProactiveGreeting, + canCaptureVerifiedProactiveGreeting: mocks.canCaptureVerifiedProactiveGreeting, + enrollProactiveGreeting: mocks.enrollProactiveGreeting, + getProactiveGreetingActor: mocks.getProactiveGreetingActor, + getProactiveGreetingConsentRevision: mocks.getProactiveGreetingConsentRevision, + observeProactiveGreetingAdmissionChanged: mocks.observeProactiveGreetingAdmissionChanged, captureExceptionAndForward: mocks.captureExceptionAndForward, registerPersonProperties: mocks.registerPersonProperties })) vi.mock('../firebaseAuthIdentity', () => ({ reportFirebaseAuthState: mocks.reportFirebaseAuthState, - viewsReportingFirebaseUser: mocks.viewsReportingFirebaseUser + viewsReportingFirebaseUser: mocks.viewsReportingFirebaseUser, + getUnverifiedLocalFirebaseSignInReport: mocks.getUnverifiedLocalFirebaseSignInReport, + getFirebaseIdentityConsensus: mocks.getFirebaseIdentityConsensus, + observeFirebaseIdentityConsensus: mocks.observeFirebaseIdentityConsensus +})) + +vi.mock('../../auth/desktopLoginCode/customTokenSignIn', () => ({ + lookupAccount: mocks.lookupAccount })) vi.mock('../experiments', () => ({ @@ -55,13 +82,44 @@ function listener(channel: string): IpcListener { return call![1] as IpcListener } +function handler( + channel: string +): (event: unknown, payload: unknown, token?: unknown) => Result { + const call = mocks.handle.mock.calls.find(([name]) => name === channel) + expect(call).toBeDefined() + return call![1] +} + +const getAdmission = () => handler>('telemetry:get-proactive-greeting-admission') + +const experimentProperties = { + proactive_greeting_user_id: 'user-a', + proactive_greeting_workspace_id: 'workspace-a', + proactive_greeting_group_key: '["user-a","workspace-a"]', + proactive_greeting_variant: 'generic', + canvas_state: 'workflow', + copy_locale: 'en' +} + +const standaloneHost = { + installationId: 'i', + sourceCategory: 'local', + sourceId: 'standalone', + comfyUrl: 'http://127.0.0.1:8000/' +} + function identityEvent( url: string, mainFrame: boolean = true, equivalentWrapper: boolean = false ): unknown { const senderMainFrame = { url, processId: 100, routingId: 200 } - const sender = { mainFrame: senderMainFrame } + const sender = { + mainFrame: senderMainFrame, + getURL: () => url, + isDestroyed: () => false, + send: vi.fn() + } return { sender, senderFrame: mainFrame @@ -75,24 +133,24 @@ function identityEvent( describe('registerTelemetryHandlers', () => { beforeEach(() => { vi.clearAllMocks() + mocks.comfyWindows.clear() + mocks.getProactiveGreetingActor.mockReturnValue(null) + mocks.getProactiveGreetingConsentRevision.mockReturnValue(1) + mocks.getUnverifiedLocalFirebaseSignInReport.mockReset().mockReturnValue(null) + mocks.getFirebaseIdentityConsensus.mockReturnValue({ status: 'unknown' }) + mocks.lookupAccount.mockReset() registerTelemetryHandlers() }) it('admits flat pair attribution only from the main frame of its authenticated hosted view', () => { const event = identityEvent('https://cloud.comfy.org/') as { sender: unknown } - const properties = { - proactive_greeting_user_id: 'user-a', - proactive_greeting_workspace_id: 'workspace-a', - proactive_greeting_group_key: '["user-a","workspace-a"]', - proactive_greeting_variant: 'generic', - canvas_state: 'workflow' - } - mocks.findEntryByComfySender.mockReturnValue({ sourceCategory: 'local' }) + const properties = experimentProperties + mocks.findEntryByComfySender.mockReturnValue({ installationId: 'i', sourceCategory: 'cloud' }) mocks.viewsReportingFirebaseUser.mockReturnValue([event.sender]) listener('telemetry:capture')(event, { event: '$experiment_exposure', properties }) expect(mocks.capture).toHaveBeenCalledWith('$experiment_exposure', { ...properties, - deployment: 'local' + deployment: 'cloud' }) mocks.capture.mockClear() mocks.viewsReportingFirebaseUser.mockReturnValue([]) @@ -122,22 +180,204 @@ describe('registerTelemetryHandlers', () => { expect(mocks.capture).not.toHaveBeenCalled() }) - it('answers enrollment eligibility only for the authenticated hosted main-frame actor', () => { - const handler = mocks.handle.mock.calls.find( - ([channel]) => channel === 'telemetry:can-enroll-proactive-greeting' - )![1] as (event: unknown, user: unknown) => boolean + it('answers enrollment eligibility only for the authenticated hosted main-frame actor', async () => { + const admission = getAdmission() const event = identityEvent('https://cloud.comfy.org/') as { sender: unknown } - mocks.findEntryByComfySender.mockReturnValue({ sourceCategory: 'local' }) + mocks.findEntryByComfySender.mockReturnValue({ installationId: 'i', sourceCategory: 'cloud' }) mocks.viewsReportingFirebaseUser.mockReturnValue([event.sender]) mocks.canEnrollProactiveGreeting.mockReturnValue(true) - expect(handler(event, 'user-a')).toBe(true) - expect(handler(event, ' user-a ')).toBe(false) - expect(handler({ ...event, senderFrame: null }, 'user-a')).toBe(false) + expect(await admission(event, 'user-a')).toBe('allowed') + expect(await admission(event, ' user-a ')).toBe('denied') + expect(await admission({ ...event, senderFrame: null }, 'user-a')).toBe('denied') mocks.canEnrollProactiveGreeting.mockReturnValue(false) - expect(handler(event, 'user-a')).toBe(false) + expect(await admission(event, 'user-a')).toBe('denied') mocks.canEnrollProactiveGreeting.mockReturnValue(true) mocks.viewsReportingFirebaseUser.mockReturnValue([]) - expect(handler(event, 'user-a')).toBe(false) + expect(await admission(event, 'user-a', 'unused-token')).toBe('denied') + expect(mocks.lookupAccount).not.toHaveBeenCalled() + }) + + it('returns the actual main admission result for a valid exposure with effective copy locale', () => { + const enroll = handler('telemetry:enroll-proactive-greeting') + const event = identityEvent('http://127.0.0.1:8000/graph') as { sender: unknown } + mocks.findEntryByComfySender.mockReturnValue(standaloneHost) + mocks.viewsReportingFirebaseUser.mockReturnValue([event.sender]) + mocks.enrollProactiveGreeting.mockReturnValueOnce(false).mockReturnValueOnce(true) + expect(enroll(event, experimentProperties)).toBe(false) + expect(enroll(event, experimentProperties)).toBe(true) + expect(mocks.enrollProactiveGreeting).toHaveBeenLastCalledWith({ + ...experimentProperties, + deployment: 'local' + }) + expect(enroll(event, {})).toBe(false) + expect(enroll({ ...event, senderFrame: null }, experimentProperties)).toBe(false) + expect(mocks.enrollProactiveGreeting).toHaveBeenCalledTimes(2) + expect(mocks.capture).not.toHaveBeenCalled() + }) + + it('requests proof only for a consented standalone view reporting the requested Firebase UID', async () => { + const event = identityEvent(standaloneHost.comfyUrl) as { sender: unknown } + const report = { userId: 'user-a', origin: 'http://127.0.0.1:8000' } + mocks.findEntryByComfySender.mockReturnValue(standaloneHost) + mocks.viewsReportingFirebaseUser.mockReturnValue([]) + const admission = getAdmission() + + expect(await admission(event, 'user-a', 'unused-token')).toBe('denied') + mocks.getUnverifiedLocalFirebaseSignInReport.mockReturnValue(report) + expect(await admission(event, 'other-user', 'unused-token')).toBe('denied') + mocks.getProactiveGreetingConsentRevision.mockReturnValue(null) + expect(await admission(event, 'user-a', 'unused-token')).toBe('denied') + mocks.getProactiveGreetingConsentRevision.mockReturnValue(1) + expect(await admission(event, 'user-a')).toBe('verify-firebase') + expect(mocks.lookupAccount).not.toHaveBeenCalled() + + mocks.lookupAccount.mockResolvedValueOnce({ localId: 'wrong-user' }) + expect(await admission(event, 'user-a', 'wrong-token')).toBe('denied') + expect(await admission(event, 'user-a')).toBe('verify-firebase') + + mocks.lookupAccount.mockResolvedValueOnce({ localId: 'user-a' }) + expect(await admission(event, 'user-a', 'valid-token')).toBe('allowed') + expect(await admission(event, 'user-a')).toBe('allowed') + expect(mocks.reportFirebaseAuthState).not.toHaveBeenCalled() + + const scopedProperties = { ...experimentProperties, deployment: 'local' } + expect(handler('telemetry:enroll-proactive-greeting')(event, experimentProperties)).toBe(true) + expect(mocks.enrollProactiveGreeting).toHaveBeenCalledWith(scopedProperties, 'user-a') + listener('telemetry:capture')(event, { + event: 'app:agent_message_sent', + properties: experimentProperties + }) + expect(mocks.captureVerifiedProactiveGreeting).toHaveBeenCalledWith( + 'app:agent_message_sent', + scopedProperties, + 'user-a' + ) + listener('telemetry:capture')(event, { event: 'ordinary:event' }) + expect(mocks.capture).toHaveBeenCalledExactlyOnceWith('ordinary:event', { deployment: 'local' }) + }) + + it.each(['new report', 'new consent revision', 'conflicting consensus'])( + 'denies a delayed proof after a %s even when the user and consent values match', + async (change) => { + const event = identityEvent(standaloneHost.comfyUrl) + const report = { userId: 'user-a', origin: 'http://127.0.0.1:8000' } + mocks.findEntryByComfySender.mockReturnValue(standaloneHost) + mocks.viewsReportingFirebaseUser.mockReturnValue([]) + mocks.getUnverifiedLocalFirebaseSignInReport.mockReturnValue(report) + const lookup = Promise.withResolvers<{ localId: string }>() + mocks.lookupAccount.mockReturnValueOnce(lookup.promise) + const result = getAdmission()(event, 'user-a', 'valid-token') + expect(mocks.lookupAccount).toHaveBeenCalledTimes(1) + if (change === 'new report') { + mocks.getUnverifiedLocalFirebaseSignInReport.mockReturnValue({ ...report }) + } else if (change === 'new consent revision') { + mocks.getProactiveGreetingConsentRevision.mockReturnValue(2) + } else { + mocks.getFirebaseIdentityConsensus.mockReturnValue({ status: 'conflicted' }) + } + lookup.resolve({ localId: 'user-a' }) + expect(await result).toBe('denied') + expect(handler('telemetry:enroll-proactive-greeting')(event, experimentProperties)).toBe( + false + ) + } + ) + + it('invalidates a denied probe when the first unverified Local Firebase report arrives', async () => { + const event = identityEvent(standaloneHost.comfyUrl) as { sender: { send: unknown } } + const report = { userId: 'user-a', origin: 'http://127.0.0.1:8000' } + mocks.findEntryByComfySender.mockReturnValue(standaloneHost) + mocks.viewsReportingFirebaseUser.mockReturnValue([]) + const admission = getAdmission() + expect(await admission(event, 'user-a')).toBe('denied') + mocks.reportFirebaseAuthState.mockImplementationOnce(() => { + mocks.getUnverifiedLocalFirebaseSignInReport.mockReturnValue(report) + }) + listener('telemetry:firebaseAuthState')(event, { status: 'signed_in', userId: 'user-a' }) + expect(event.sender.send).toHaveBeenCalledExactlyOnceWith( + 'telemetry:proactive-greeting-admission-changed' + ) + expect(await admission(event, 'user-a')).toBe('verify-firebase') + expect(mocks.lookupAccount).not.toHaveBeenCalled() + }) + + it.each([ + { + installationId: null, + sourceId: 'standalone', + sourceCategory: 'local', + comfyUrl: 'http://127.0.0.1:8000/' + }, + { + installationId: 'i', + sourceId: 'portable', + sourceCategory: 'local', + comfyUrl: 'http://127.0.0.1:8000/' + }, + { + installationId: 'i', + sourceId: 'git', + sourceCategory: 'local', + comfyUrl: 'http://127.0.0.1:8000/' + }, + { installationId: 'i', sourceCategory: 'remote', comfyUrl: 'http://127.0.0.1:8000/' }, + { + installationId: 'i', + sourceId: 'standalone', + sourceCategory: 'local', + comfyUrl: 'http://127.0.0.1:9000/' + }, + { installationId: 'i', sourceCategory: 'cloud', comfyUrl: 'http://127.0.0.1:8000/' } + ])('denies experiment IPC from a view outside the managed host scope %j', async (entry) => { + const event = identityEvent('http://127.0.0.1:8000/') as { sender: unknown } + mocks.findEntryByComfySender.mockReturnValue(entry) + mocks.viewsReportingFirebaseUser.mockReturnValue([event.sender]) + expect(await getAdmission()(event, 'user-a', 'unused-token')).toBe('denied') + expect(handler('telemetry:enroll-proactive-greeting')(event, experimentProperties)).toBe(false) + listener('telemetry:capture')(event, { + event: '$experiment_exposure', + properties: experimentProperties + }) + expect(mocks.enrollProactiveGreeting).not.toHaveBeenCalled() + expect(mocks.capture).not.toHaveBeenCalled() + expect(mocks.lookupAccount).not.toHaveBeenCalled() + }) + + it('invalidates supported hosted views after main admission or view identity changes', async () => { + const url = 'https://cloud.comfy.org/' + const event = identityEvent(url) as { sender: object } + const send = vi.fn() + const remoteSend = vi.fn() + Object.assign(event.sender, { isDestroyed: () => false, getURL: () => url, send }) + const entry = { + installationId: 'cloud-i', + sourceCategory: 'cloud', + comfyView: { webContents: event.sender } + } + mocks.comfyWindows.set(1, entry) + mocks.comfyWindows.set(2, { + installationId: 'remote-i', + sourceCategory: 'remote', + comfyView: { webContents: { isDestroyed: () => false, getURL: () => url, send: remoteSend } } + }) + + const mainAdmissionChanged = mocks.observeProactiveGreetingAdmissionChanged.mock.calls[0]![0] + mainAdmissionChanged() + await Promise.resolve() + expect(send).toHaveBeenCalledExactlyOnceWith('telemetry:proactive-greeting-admission-changed') + expect(remoteSend).not.toHaveBeenCalled() + + send.mockClear() + mocks.findEntryByComfySender.mockReturnValue(entry) + mocks.getProactiveGreetingActor.mockReturnValue('user-a') + mocks.viewsReportingFirebaseUser.mockReturnValue([]) + mocks.reportFirebaseAuthState.mockImplementationOnce(() => { + mocks.viewsReportingFirebaseUser.mockReturnValue([event.sender]) + }) + listener('telemetry:firebaseAuthState')(event, { status: 'signed_in', userId: 'user-a' }) + await Promise.resolve() + expect(send).toHaveBeenCalledExactlyOnceWith('telemetry:proactive-greeting-admission-changed') + expect(remoteSend).not.toHaveBeenCalled() }) it('caps event property keys, arrays, and strings', () => { diff --git a/src/main/lib/ipc/registerTelemetryHandlers.ts b/src/main/lib/ipc/registerTelemetryHandlers.ts index 37086ad38..7393062bb 100644 --- a/src/main/lib/ipc/registerTelemetryHandlers.ts +++ b/src/main/lib/ipc/registerTelemetryHandlers.ts @@ -2,7 +2,7 @@ // identity, consent, and dedup live in one place. Capture messages are // fire-and-forget (ipcMain.on). import { ipcMain, type IpcMainEvent, type WebContents, type WebFrameMain } from 'electron' -import { findEntryByComfySender } from '../../host/registry' +import { comfyWindows, findEntryByComfySender, type ComfyWindowEntry } from '../../host/registry' import * as mainTelemetry from '../telemetry' import { getFlagAsync as getExperimentFlagAsync, @@ -10,10 +10,23 @@ import { type ExperimentExposureSource } from '../experiments' import { normalizeExceptionContext } from '../../../shared/piiScrub' -import { reportFirebaseAuthState, viewsReportingFirebaseUser } from '../firebaseAuthIdentity' +import { + getFirebaseIdentityConsensus, + getUnverifiedLocalFirebaseSignInReport, + observeFirebaseIdentityConsensus, + reportFirebaseAuthState, + viewsReportingFirebaseUser +} from '../firebaseAuthIdentity' import { parseProactiveGreetingAttribution } from '../proactiveGreetingTelemetry' -import type { ComfyDesktop2FirebaseAuthState } from '../../../types/comfyDesktopBridge' +import type { + ComfyDesktop2FirebaseAuthState, + ComfyDesktop2ProactiveGreetingAdmission +} from '../../../types/comfyDesktopBridge' import { normalizePostHogUserId } from '../opaqueIdentifier' +import { isTrustedCloudUrl } from '../trustedCloudUrl' +import { isLoopbackOrigin } from '../verifiedLocalFirebaseAuth' +import { lookupAccount } from '../../auth/desktopLoginCode/customTokenSignIn' +import { getFirebaseConfig } from '../../auth/firebaseBridge/config' interface CapturePayload { event?: unknown @@ -194,23 +207,160 @@ function withPlatformAxes( return properties } +function isProactiveGreetingHost(entry: ComfyWindowEntry, url: string): boolean { + if (!entry.installationId) return false + if (entry.sourceCategory === 'cloud') return isTrustedCloudUrl(url) + if (entry.sourceCategory !== 'local' || entry.sourceId !== 'standalone') return false + const origin = URL.parse(url)?.origin + return !!origin && isLoopbackOrigin(origin) && URL.parse(entry.comfyUrl)?.origin === origin +} + function isProactiveGreetingSender(event: HostedSenderEvent, userId: string): boolean { - return Boolean( - event && - firebaseAuthReporterFrame(event) && - findEntryByComfySender(event.sender) && + const frame = event && firebaseAuthReporterFrame(event) + if (!frame) return false + const entry = findEntryByComfySender(event.sender) + return ( + !!entry && + isProactiveGreetingHost(entry, frame.url) && + viewsReportingFirebaseUser(userId).includes(event.sender) + ) +} + +const verifyingLocalViews = new WeakSet() +const verifiedLocalReports = new WeakSet() + +function canUseLocalGreetingProof(userId: string): boolean { + const consensus = getFirebaseIdentityConsensus() + return ( + consensus.status !== 'pending' && + consensus.status !== 'conflicted' && + (consensus.status !== 'signed_in' || consensus.userId === userId) && + mainTelemetry.canCaptureVerifiedProactiveGreeting(userId) + ) +} + +function hasLocalGreetingProof(event: HostedSenderEvent, userId: string): boolean { + const frame = event && firebaseAuthReporterFrame(event) + if (!frame) return false + const entry = findEntryByComfySender(event.sender) + if (!entry || entry.sourceId !== 'standalone' || !isProactiveGreetingHost(entry, frame.url)) + return false + const report = getUnverifiedLocalFirebaseSignInReport(event.sender) + return ( + report?.userId === userId && + verifiedLocalReports.has(report) && + canUseLocalGreetingProof(userId) + ) +} + +async function getProactiveGreetingAdmission( + event: HostedSenderEvent, + value: unknown, + token: unknown +): Promise { + const userId = normalizePostHogUserId(value) + if (!userId || userId !== value) return 'denied' + const frame = firebaseAuthReporterFrame(event) + const entry = findEntryByComfySender(event.sender) + if (!frame || !entry || !isProactiveGreetingHost(entry, frame.url)) return 'denied' + if ( + mainTelemetry.canEnrollProactiveGreeting(userId) && viewsReportingFirebaseUser(userId).includes(event.sender) ) + return 'allowed' + if (entry.sourceId !== 'standalone' || entry.sourceCategory !== 'local') return 'denied' + + const revision = mainTelemetry.getProactiveGreetingConsentRevision() + const report = getUnverifiedLocalFirebaseSignInReport(event.sender) + if (revision === null || report?.userId !== userId || !canUseLocalGreetingProof(userId)) + return 'denied' + if (verifiedLocalReports.has(report)) return 'allowed' + if (token === undefined) return 'verify-firebase' + if (typeof token !== 'string' || !token.length || token.length > 16_384) return 'denied' + if (verifyingLocalViews.has(event.sender)) return 'denied' + + const { processId, routingId } = frame + const { installationId } = entry + verifyingLocalViews.add(event.sender) + try { + const account = await lookupAccount(getFirebaseConfig('prod').apiKey, token) + if ( + account.localId !== userId || + event.sender.isDestroyed() || + mainTelemetry.getProactiveGreetingConsentRevision() !== revision || + getUnverifiedLocalFirebaseSignInReport(event.sender) !== report || + !canUseLocalGreetingProof(userId) + ) + return 'denied' + const currentFrame = event.sender.mainFrame + if ( + currentFrame.processId !== processId || + currentFrame.routingId !== routingId || + URL.parse(currentFrame.url)?.origin !== report.origin || + findEntryByComfySender(event.sender) !== entry || + entry.installationId !== installationId || + entry.sourceId !== 'standalone' || + !isProactiveGreetingHost(entry, currentFrame.url) + ) + return 'denied' + verifiedLocalReports.add(report) + notifyProactiveGreetingAdmissionChanged(event.sender) + return 'allowed' + } catch { + // The request and response can contain credentials. Reject without logging. + return 'denied' + } finally { + verifyingLocalViews.delete(event.sender) + const currentReport = getUnverifiedLocalFirebaseSignInReport(event.sender) + const currentEntry = findEntryByComfySender(event.sender) + if ( + currentReport && + currentEntry && + isProactiveGreetingHost(currentEntry, currentReport.origin) && + (currentReport !== report || mainTelemetry.getProactiveGreetingConsentRevision() !== revision) + ) + notifyProactiveGreetingAdmissionChanged(event.sender) + } +} + +function notifyProactiveGreetingAdmissionChanged(contents: WebContents): void { + if (contents.isDestroyed()) return + try { + contents.send('telemetry:proactive-greeting-admission-changed') + } catch { + // The hosted view can close before the notification is sent. + } } export function registerTelemetryHandlers(): void { - ipcMain.handle('telemetry:can-enroll-proactive-greeting', (event, value: unknown) => { - const userId = normalizePostHogUserId(value) + let admissionChangeScheduled = false + function scheduleAdmissionChange(): void { + if (admissionChangeScheduled) return + admissionChangeScheduled = true + queueMicrotask(() => { + admissionChangeScheduled = false + for (const entry of comfyWindows.values()) { + const contents = entry.comfyView.webContents + if (contents.isDestroyed() || !isProactiveGreetingHost(entry, contents.getURL())) continue + notifyProactiveGreetingAdmissionChanged(contents) + } + }) + } + mainTelemetry.observeProactiveGreetingAdmissionChanged(scheduleAdmissionChange) + observeFirebaseIdentityConsensus(scheduleAdmissionChange) + + ipcMain.handle('telemetry:get-proactive-greeting-admission', getProactiveGreetingAdmission) + + ipcMain.handle('telemetry:enroll-proactive-greeting', (event, payload: unknown) => { + const properties = asProps(payload) + const attribution = parseProactiveGreetingAttribution('$experiment_exposure', properties) + if (!attribution) return false + const eventProperties = withPlatformAxes(properties, event.sender) + if (isProactiveGreetingSender(event, attribution.userId)) + return mainTelemetry.enrollProactiveGreeting(eventProperties) return ( - userId !== null && - userId === value && - isProactiveGreetingSender(event, userId) && - mainTelemetry.canEnrollProactiveGreeting(userId) + hasLocalGreetingProof(event, attribution.userId) && + mainTelemetry.enrollProactiveGreeting(eventProperties, attribution.userId) ) }) @@ -220,8 +370,17 @@ export function registerTelemetryHandlers(): void { const properties = asProps(payload.properties) const attribution = parseProactiveGreetingAttribution(eventName, properties) if (attribution === null) return - if (attribution && !isProactiveGreetingSender(event, attribution.userId)) return - mainTelemetry.emit(eventName, withPlatformAxes(properties, event?.sender)) + const eventProperties = withPlatformAxes(properties, event?.sender) + if (attribution && !isProactiveGreetingSender(event, attribution.userId)) { + if (hasLocalGreetingProof(event, attribution.userId)) + mainTelemetry.captureVerifiedProactiveGreeting( + eventName, + eventProperties, + attribution.userId + ) + return + } + mainTelemetry.emit(eventName, eventProperties) }) ipcMain.on('telemetry:captureException', (event, payload: CaptureExceptionPayload) => { @@ -246,11 +405,24 @@ export function registerTelemetryHandlers(): void { if (!frame) return const state = asFirebaseAuthState(payload) if (!state) return + const actor = mainTelemetry.getProactiveGreetingActor() + const wasAdmitted = actor !== null && isProactiveGreetingSender(event, actor) + const entry = findEntryByComfySender(event.sender) + const canVerify = + !!entry && entry.sourceId === 'standalone' && isProactiveGreetingHost(entry, frame.url) + const previousReport = canVerify ? getUnverifiedLocalFirebaseSignInReport(event.sender) : null reportFirebaseAuthState( event.sender, { processId: frame.processId, routingId: frame.routingId }, state ) + // This view can join or leave the admitted actor without changing process consensus. + const canVerifyChanged = + canVerify && getUnverifiedLocalFirebaseSignInReport(event.sender) !== previousReport + const actorChanged = actor !== null && isProactiveGreetingSender(event, actor) !== wasAdmitted + if (!admissionChangeScheduled && (canVerifyChanged || actorChanged)) { + notifyProactiveGreetingAdmissionChanged(event.sender) + } }) // Flag lookup for renderer A/B branches; awaits the boot fetch so a query diff --git a/src/main/lib/proactiveGreetingTelemetry.ts b/src/main/lib/proactiveGreetingTelemetry.ts index d20c91111..601ce5888 100644 --- a/src/main/lib/proactiveGreetingTelemetry.ts +++ b/src/main/lib/proactiveGreetingTelemetry.ts @@ -11,6 +11,10 @@ export interface ProactiveGreetingAttribution { variant: string } +export function proactiveGreetingExposureKey(attribution: ProactiveGreetingAttribution): string { + return JSON.stringify([attribution.groupKey, attribution.variant]) +} + /** undefined is unrelated telemetry; null is a malformed experiment event. */ export function parseProactiveGreetingAttribution( event: string, diff --git a/src/main/lib/telemetry.test.ts b/src/main/lib/telemetry.test.ts index c45890b85..a81189073 100644 --- a/src/main/lib/telemetry.test.ts +++ b/src/main/lib/telemetry.test.ts @@ -432,6 +432,92 @@ describe('proactive greeting pair attribution', () => { expect(captured.filter((call) => call.event === 'app:agent_message_sent')).toHaveLength(2) }) + it('acknowledges an exposure only after handoff and rechecks consent on duplicates', () => { + signIn() + posthogClientMock.failNextCaptures = 1 + expect(telemetry.enrollProactiveGreeting(pair())).toBe(false) + expect(telemetry.enrollProactiveGreeting(pair())).toBe(true) + expect(telemetry.enrollProactiveGreeting(pair())).toBe(true) + expect(exposures()).toHaveLength(1) + + telemetry.setConsentState('denied') + expect(telemetry.enrollProactiveGreeting(pair())).toBe(false) + telemetry.setConsentState('granted') + expect(telemetry.enrollProactiveGreeting(pair())).toBe(true) + telemetry.applyFirebaseUserConsensus('other-user') + expect(telemetry.enrollProactiveGreeting(pair())).toBe(false) + expect(exposures()).toHaveLength(1) + }) + + it('rejects rather than queues enrollment during pending identity consensus', () => { + signIn() + telemetry.applyFirebasePendingConsensus() + expect(telemetry.enrollProactiveGreeting(pair())).toBe(false) + telemetry.applyFirebaseUserConsensus('firebase-user') + expect(exposures()).toHaveLength(0) + expect(telemetry.enrollProactiveGreeting(pair())).toBe(true) + }) + + it('reports admission changes on binding, consent changes and quarantine', () => { + setupTelemetry() + const observed: boolean[] = [] + const unsubscribe = telemetry.observeProactiveGreetingAdmissionChanged(() => + observed.push(telemetry.canEnrollProactiveGreeting('firebase-user')) + ) + telemetry.applyFirebaseUserConsensus('firebase-user') + telemetry.setConsentState('denied') + telemetry.setConsentState('denied') + telemetry.setConsentState('granted') + telemetry.applyFirebasePendingConsensus() + telemetry.applyFirebaseUserConsensus('firebase-user') + bindTestAnonymous('replacement-anonymous-id') + unsubscribe() + telemetry.applyFirebaseUserConsensus('firebase-user') + expect(observed).toEqual([true, false, true, false, true, false]) + }) + + it('notifies consent transitions without a bound user and changes proof revision on regrant', () => { + setupTelemetry({ consent: 'denied' }) + const changes = vi.fn() + const unsubscribe = telemetry.observeProactiveGreetingAdmissionChanged(changes) + expect(telemetry.getProactiveGreetingConsentRevision()).toBeNull() + telemetry.setConsentState('granted') + const revision = telemetry.getProactiveGreetingConsentRevision() + expect(revision).not.toBeNull() + telemetry.setConsentState('denied') + expect(telemetry.getProactiveGreetingConsentRevision()).toBeNull() + telemetry.setConsentState('granted') + expect(telemetry.getProactiveGreetingConsentRevision()).not.toBe(revision) + expect(changes).toHaveBeenCalledTimes(3) + unsubscribe() + }) + + it('attributes a verified Local experiment without binding or identifying ordinary telemetry', () => { + setupTelemetry() + captured.length = 0 + const treatment = { ...pair(), proactive_greeting_variant: 'generic', copy_locale: 'en' } + expect(telemetry.enrollProactiveGreeting(treatment, 'firebase-user')).toBe(true) + expect( + telemetry.captureVerifiedProactiveGreeting( + 'app:agent_message_sent', + treatment, + 'firebase-user' + ) + ).toBe(true) + expect(telemetry.capture('ordinary:event')).toBe(true) + expect(captured.map((call) => call.distinctId)).toEqual([ + 'firebase-user', + 'firebase-user', + 'test-distinct-id' + ]) + expect(exposures()[0]).toMatchObject({ + groups: { user_workspace: '["firebase-user","workspace-a"]' }, + properties: { copy_locale: 'en' } + }) + expect(identifies).toHaveLength(0) + expect(telemetry.getProactiveGreetingActor()).toBeNull() + }) + it.each(['denied', 'undecided'] as const)( 'does not consume exposure while consent is %s', (consent) => { diff --git a/src/main/lib/telemetry.ts b/src/main/lib/telemetry.ts index ca041d875..0869baed5 100644 --- a/src/main/lib/telemetry.ts +++ b/src/main/lib/telemetry.ts @@ -112,6 +112,7 @@ import { app } from 'electron' import { PostHog } from 'posthog-node' import { parseProactiveGreetingAttribution, + proactiveGreetingExposureKey, proactiveGreetingProperties, PROACTIVE_GREETING_FLAG } from './proactiveGreetingTelemetry' @@ -196,6 +197,10 @@ export function _resetTelemetryRelayTargets(): void { */ export function _resetForTest(): void { proactiveGreetingExposures.clear() + proactiveGreetingAdmissionObservers.clear() + observedProactiveGreetingActor = null + observedProactiveGreetingConsentRevision = null + consentRevision = 0 client = null distinctId = null anonymousDistinctId = null @@ -687,18 +692,21 @@ export function _test_resetVolumeGuards(): void { */ export function setConsentState(state: ConsentState): void { const previous = consentState + if (state !== previous) ++consentRevision consentState = state if (state !== 'granted') { // Best-effort flush so already-queued events still go out before we // start suppressing. void client?.flush().catch(() => {}) if (state === 'denied') discardDeferredTelemetry() + publishProactiveGreetingAdmission() return } // Transitioned to granted. Ship anything we held back. if (previous !== 'granted') { tryFlushDeferred() } + publishProactiveGreetingAdmission() } /** @@ -1098,8 +1106,8 @@ export function bindAnonymousId( ...properties } } - if (!canEmit()) return - tryFlushDeferred() + if (canEmit()) tryFlushDeferred() + publishProactiveGreetingAdmission() } /** @@ -1167,6 +1175,15 @@ function applyFirebaseUserBinding( userId: string, emitLoginEvent: boolean, properties: Record = {} +): void { + updateFirebaseUserBinding(userId, emitLoginEvent, properties) + publishProactiveGreetingAdmission() +} + +function updateFirebaseUserBinding( + userId: string, + emitLoginEvent: boolean, + properties: Record ): void { // Resolved consensus always ends the pending quarantine, even when this // binding cannot proceed (denied consent, unusable UID) — otherwise @@ -1291,6 +1308,7 @@ export function bindUserId(userId: string, properties: Record() +const proactiveGreetingAdmissionObservers = new Set<() => void>() +let observedProactiveGreetingActor: string | null = null +let observedProactiveGreetingConsentRevision: number | null = null +let consentRevision = 0 + +export function getProactiveGreetingConsentRevision(): number | null { + return canEmit() && consentState === 'granted' ? consentRevision : null +} /** Read-only eligibility: never bind an identity or bypass the host's consent owner. */ +export function getProactiveGreetingActor(): string | null { + if ( + getProactiveGreetingConsentRevision() === null || + boundUserId !== distinctId || + firebaseWritesQuarantined() + ) + return null + return boundUserId +} + export function canEnrollProactiveGreeting(userId: string): boolean { + return getProactiveGreetingActor() === userId +} + +export function observeProactiveGreetingAdmissionChanged(observer: () => void): () => void { + proactiveGreetingAdmissionObservers.add(observer) + return () => proactiveGreetingAdmissionObservers.delete(observer) +} + +function publishProactiveGreetingAdmission(): void { + const actor = getProactiveGreetingActor() + const revision = getProactiveGreetingConsentRevision() + if ( + actor === observedProactiveGreetingActor && + revision === observedProactiveGreetingConsentRevision + ) + return + observedProactiveGreetingActor = actor + observedProactiveGreetingConsentRevision = revision + for (const observer of proactiveGreetingAdmissionObservers) observer() +} + +export function canCaptureVerifiedProactiveGreeting(userId: string): boolean { return ( - canEmit() && - consentState === 'granted' && - boundUserId === userId && - distinctId === userId && - !firebaseWritesQuarantined() + getProactiveGreetingConsentRevision() !== null && + !firebaseConsensusPending && + (boundUserId === null || boundUserId === userId) ) } +/** Acknowledge local SDK handoff only. Pending identity cannot admit an exposure. */ +export function enrollProactiveGreeting( + properties: TelemetryContext, + verifiedActor?: string +): boolean { + const event = '$experiment_exposure' + const attribution = parseProactiveGreetingAttribution(event, properties) + if (!attribution) return false + const allowed = verifiedActor + ? attribution.userId === verifiedActor && canCaptureVerifiedProactiveGreeting(verifiedActor) + : canEnrollProactiveGreeting(attribution.userId) + if (!allowed) return false + if (proactiveGreetingExposures.has(proactiveGreetingExposureKey(attribution))) return true + return captureEvent(event, properties, false, verifiedActor) +} + +/** Captures only scoped experiment events, without altering the ordinary telemetry actor. */ +export function captureVerifiedProactiveGreeting( + event: string, + properties: TelemetryContext, + verifiedActor: string +): boolean { + return captureEvent(event, properties, false, verifiedActor) +} + /** * Returns whether the event was admitted. A `false` means it was dropped * (consent gate, volume guard, or SDK failure); one-shot callers use this to @@ -1447,14 +1530,25 @@ export function capture(event: string, properties: TelemetryContext = {}): boole return captureEvent(event, properties, false) } -function captureEvent(event: string, properties: TelemetryContext, forward: boolean): boolean { +function captureEvent( + event: string, + properties: TelemetryContext, + forward: boolean, + verifiedActor?: string +): boolean { // E2E only (no-op otherwise): lets a spec assert an event was raised without consent or a // reachable PostHog. recordIpcInvocation(`telemetry:${event}`, properties) - if (!canEmit() || !distinctId) return false + if (!canEmit() || !(verifiedActor ?? distinctId)) return false if (!isAllowedToFire(event)) return false const attribution = parseProactiveGreetingAttribution(event, properties) - if (attribution === null || (attribution && attribution.userId !== boundUserId)) return false + if ( + attribution === null || + (attribution && attribution.userId !== (verifiedActor ?? boundUserId)) + ) + return false + if (verifiedActor && (!attribution || !canCaptureVerifiedProactiveGreeting(verifiedActor))) + return false if (!_checkRateLimit(event)) return false if (firebaseWritesQuarantined()) { if ( @@ -1464,7 +1558,7 @@ function captureEvent(event: string, properties: TelemetryContext, forward: bool _recordCapturedEvent(event) return true } - if (!deliverEvent(event, properties, forward, null)) return false + if (!deliverEvent(event, properties, forward, null, verifiedActor)) return false _recordCapturedEvent(event) return true } @@ -1473,15 +1567,20 @@ function deliverEvent( event: string, properties: TelemetryContext, forward: boolean, - timestamp: Date | null + timestamp: Date | null, + verifiedActor?: string ): boolean { try { // Recheck after quarantine as well. A delayed event must never acquire a new actor. const attribution = parseProactiveGreetingAttribution(event, properties) - if (attribution === null || (attribution && attribution.userId !== boundUserId)) return false + if ( + attribution === null || + (attribution && attribution.userId !== (verifiedActor ?? boundUserId)) + ) + return false const exposureKey = attribution && event === '$experiment_exposure' - ? JSON.stringify([attribution.groupKey, attribution.variant]) + ? proactiveGreetingExposureKey(attribution) : null if (exposureKey && proactiveGreetingExposures.has(exposureKey)) return false // Per-call properties override defaults on key collision - callers @@ -1491,13 +1590,14 @@ function deliverEvent( // to derive country (`disableGeoip: false` at init). The raw IP and all // sub-country geo are then discarded by an ingestion transformation, so // only the country code/name is retained. See the init comment. - const merged = enforcePersonProcessingPolicy({ ...defaultEventProperties, ...properties }) + const merged = { ...defaultEventProperties, ...properties } + const policyProperties = verifiedActor ? merged : enforcePersonProcessingPolicy(merged) client!.capture({ - distinctId: distinctId!, + distinctId: (verifiedActor ?? distinctId)!, event, ...(attribution ? { groups: { user_workspace: attribution.groupKey } } : {}), properties: { - ...scrubProperties(merged), + ...scrubProperties(policyProperties), ...(attribution ? proactiveGreetingProperties(attribution) : {}), ...(exposureKey ? { diff --git a/src/preload/comfyPreload.test.ts b/src/preload/comfyPreload.test.ts index 951c5f251..2972cc61a 100644 --- a/src/preload/comfyPreload.test.ts +++ b/src/preload/comfyPreload.test.ts @@ -78,16 +78,29 @@ describe('comfyPreload model access bridge', () => { mocks.send.mockReset() }) - it('queries current host enrollment eligibility without reporting an exposure', async () => { - mocks.invoke.mockResolvedValue(false) - expect(await hostedBridge().Telemetry.canEnrollProactiveGreeting!('user-a')).toBe(false) - expect(mocks.invoke).toHaveBeenCalledWith('telemetry:can-enroll-proactive-greeting', 'user-a') + it('queries current host enrollment eligibility and forwards identity proof when requested', async () => { + const admission = hostedBridge().Telemetry.getProactiveGreetingAdmission! + mocks.invoke.mockResolvedValueOnce('verify-firebase').mockResolvedValueOnce('allowed') + expect(await admission('user-a')).toBe('verify-firebase') + expect(await admission('user-a', 'id-token')).toBe('allowed') + expect(mocks.invoke).toHaveBeenNthCalledWith( + 1, + 'telemetry:get-proactive-greeting-admission', + 'user-a', + undefined + ) + expect(mocks.invoke).toHaveBeenNthCalledWith( + 2, + 'telemetry:get-proactive-greeting-admission', + 'user-a', + 'id-token' + ) expect(mocks.send).not.toHaveBeenCalled() }) - it('advertises pair attribution and relays its flat fields through the existing capture channel', () => { + it('advertises acknowledged enrollment and preserves the ordinary capture channel', async () => { const bridge = hostedBridge() - expect(bridge.Telemetry.proactiveGreetingExperimentVersion).toBe(1) + expect(bridge.Telemetry.proactiveGreetingExperimentVersion).toBe(2) const properties = { proactive_greeting_user_id: 'user-a', proactive_greeting_workspace_id: 'workspace-a', @@ -95,13 +108,30 @@ describe('comfyPreload model access bridge', () => { proactive_greeting_variant: 'control', canvas_state: 'empty' } - bridge.Telemetry.capture('$experiment_exposure', properties) + mocks.invoke.mockResolvedValue(true) + expect(await bridge.Telemetry.enrollProactiveGreeting!(properties)).toBe(true) + expect(mocks.invoke).toHaveBeenCalledWith('telemetry:enroll-proactive-greeting', properties) + expect(mocks.send).not.toHaveBeenCalled() + bridge.Telemetry.capture('app:agent_message_sent', properties) expect(mocks.send).toHaveBeenCalledWith('telemetry:capture', { - event: '$experiment_exposure', + event: 'app:agent_message_sent', properties }) }) + it('relays admission invalidations without exposing IPC data and unsubscribes', () => { + const callback = vi.fn() + const unsubscribe = hostedBridge().Telemetry.onProactiveGreetingAdmissionChanged!(callback) + const channel = 'telemetry:proactive-greeting-admission-changed' + const handler = mocks.on.mock.calls.find(([name]) => name === channel)?.[1] as ( + event: unknown + ) => void + handler({ sender: 'internal' }) + expect(callback).toHaveBeenCalledExactlyOnceWith() + unsubscribe() + expect(mocks.removeListener).toHaveBeenCalledWith(channel, handler) + }) + it('forwards hosted frontend exceptions through the scrubbed Desktop telemetry channel', () => { const bridge = hostedBridge() const error = new Error('boom') diff --git a/src/preload/comfyPreload.ts b/src/preload/comfyPreload.ts index 916a3328a..24dd9f3ac 100644 --- a/src/preload/comfyPreload.ts +++ b/src/preload/comfyPreload.ts @@ -101,9 +101,17 @@ const captureException: NonNullable => - ipcRenderer.invoke('telemetry:can-enroll-proactive-greeting', userId), + proactiveGreetingExperimentVersion: 2, + getProactiveGreetingAdmission: (userId, firebaseIdToken) => + ipcRenderer.invoke('telemetry:get-proactive-greeting-admission', userId, firebaseIdToken), + enrollProactiveGreeting: (properties): Promise => + ipcRenderer.invoke('telemetry:enroll-proactive-greeting', properties), + onProactiveGreetingAdmissionChanged: (callback) => { + const channel = 'telemetry:proactive-greeting-admission-changed' + const handler = (): void => callback() + ipcRenderer.on(channel, handler) + return () => ipcRenderer.removeListener(channel, handler) + }, capture: (event, properties): void => sendTelemetry('telemetry:capture', { event, properties }), captureException, reportFirebaseAuthState diff --git a/src/types/comfyDesktopBridge.ts b/src/types/comfyDesktopBridge.ts index e9e78072a..72e4d93f3 100644 --- a/src/types/comfyDesktopBridge.ts +++ b/src/types/comfyDesktopBridge.ts @@ -159,11 +159,20 @@ export interface ComfyDesktop2LogsBridge { onOutput(callback: (msg: LogsOutputMsg) => void): () => void } +export type ComfyDesktop2ProactiveGreetingAdmission = 'allowed' | 'denied' | 'verify-firebase' + export interface ComfyDesktop2TelemetryBridge { /** Supports validated user/workspace attribution for the proactive greeting experiment. */ - readonly proactiveGreetingExperimentVersion?: 1 - /** Current analytics eligibility only; capture rechecks consent and actor before delivery. */ - canEnrollProactiveGreeting?(userId: string): Promise + readonly proactiveGreetingExperimentVersion?: 1 | 2 + /** Current analytics eligibility. Pass Firebase proof only in response to verify-firebase. */ + getProactiveGreetingAdmission?( + userId: string, + firebaseIdToken?: string + ): Promise + /** Acknowledges local handoff to analytics, including a previously admitted pair and arm. */ + enrollProactiveGreeting?(properties: ComfyDesktop2TelemetryProperties): Promise + /** Signals that the hosted view should recheck its current actor's eligibility. */ + onProactiveGreetingAdmissionChanged?(callback: () => void): () => void capture(event: string, properties?: ComfyDesktop2TelemetryProperties): void /** Capture a hosted-frontend exception through Desktop's privacy and release-context boundary. */ captureException?(error: ComfyDesktop2Error, properties?: ComfyDesktop2TelemetryProperties): void From 0c4fe925ca5c3fa65dccd6329461fee98b6f21e5 Mon Sep 17 00:00:00 2001 From: ryan-di Date: Tue, 10 Nov 2026 08:05:17 +0800 Subject: [PATCH 5/5] fix: honor quit cancellation before draining telemetry --- src/main/lib/telemetry.test.ts | 67 +++++++++++++++++++++++++++++++++- src/main/lib/telemetry.ts | 3 +- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/main/lib/telemetry.test.ts b/src/main/lib/telemetry.test.ts index a81189073..2e19e2c43 100644 --- a/src/main/lib/telemetry.test.ts +++ b/src/main/lib/telemetry.test.ts @@ -7,11 +7,16 @@ import type { PostHog } from 'posthog-node' import type { TelemetryValue } from './telemetry' import { DEFAULT_POSTHOG_API_KEY, DEFAULT_POSTHOG_HOST } from '../../shared/posthogConfig' +const electronAppMock = vi.hoisted(() => ({ + on: vi.fn<(event: string, listener: (event: Electron.Event) => void) => void>(), + quit: vi.fn() +})) + vi.mock('electron', () => ({ app: { getPath: () => path.join(os.tmpdir(), 'launcher-test'), isPackaged: true, - on: () => {} + ...electronAppMock }, BrowserWindow: { getAllWindows: () => [] } })) @@ -84,6 +89,7 @@ const posthogConstructorCalls: Array<{ apiKey: string; options: Record ({ failNextCaptures: 0, failNextFlushes: 0, + shutdown: vi.fn<() => Promise>(() => Promise.resolve()), autoFailNextIdentifies: 0, featureFlagResult: undefined as | { enabled: boolean; variant?: string; payload?: unknown } @@ -163,7 +169,7 @@ vi.mock('posthog-node', () => ({ return Promise.resolve() } shutdown(): Promise { - return Promise.resolve() + return posthogClientMock.shutdown() } getFeatureFlagResult( key: string, @@ -492,6 +498,62 @@ describe('proactive greeting pair attribution', () => { unsubscribe() }) + it('honors a canceled quit, then drains and reissues a confirmed quit once', async () => { + vi.useFakeTimers() + try { + signIn() + telemetry.installAppHooks() + const onBeforeQuit = electronAppMock.on.mock.calls.find( + ([event]) => event === 'before-quit' + )![1] + const canceled = { defaultPrevented: true, preventDefault: vi.fn() } + onBeforeQuit(canceled) + await vi.runAllTimersAsync() + expect(posthogClientMock.shutdown).not.toHaveBeenCalled() + expect(electronAppMock.quit).not.toHaveBeenCalled() + expect(telemetry.canEnrollProactiveGreeting('firebase-user')).toBe(true) + expect(telemetry.capture('after.canceled.quit')).toBe(true) + + const drain = Promise.withResolvers() + posthogClientMock.shutdown.mockReturnValueOnce(drain.promise) + const confirmed = { defaultPrevented: false, preventDefault: vi.fn() } + const reissued = { defaultPrevented: false, preventDefault: vi.fn() } + electronAppMock.quit.mockImplementationOnce(() => onBeforeQuit(reissued)) + onBeforeQuit(confirmed) + await vi.advanceTimersByTimeAsync(0) + expect(confirmed.preventDefault).toHaveBeenCalledTimes(1) + expect(posthogClientMock.shutdown).toHaveBeenCalledTimes(1) + expect(electronAppMock.quit).not.toHaveBeenCalled() + + drain.resolve() + await vi.runAllTimersAsync() + expect(electronAppMock.quit).toHaveBeenCalledTimes(1) + expect(posthogClientMock.shutdown).toHaveBeenCalledTimes(1) + expect(reissued.preventDefault).not.toHaveBeenCalled() + } finally { + vi.useRealTimers() + } + }) + + it.each(['success', 'failure'])( + 'shutdown invalidates admission after an SDK drain %s', + async (outcome) => { + signIn() + const changes: boolean[] = [] + const unsubscribe = telemetry.observeProactiveGreetingAdmissionChanged(() => + changes.push(telemetry.canEnrollProactiveGreeting('firebase-user')) + ) + if (outcome === 'failure') { + posthogClientMock.shutdown.mockRejectedValueOnce(new Error('offline')) + } + await telemetry.shutdown('quit') + expect(posthogClientMock.shutdown).toHaveBeenCalledTimes(1) + expect(changes).toEqual([false]) + expect(telemetry.canEnrollProactiveGreeting('firebase-user')).toBe(false) + unsubscribe() + } + ) + it('attributes a verified Local experiment without binding or identifying ordinary telemetry', () => { setupTelemetry() captured.length = 0 @@ -577,6 +639,7 @@ afterEach(() => { anonymousIdentityMock.fail = false posthogClientMock.failNextCaptures = 0 posthogClientMock.failNextFlushes = 0 + posthogClientMock.shutdown.mockReset().mockResolvedValue(undefined) posthogClientMock.autoFailNextIdentifies = 0 posthogClientMock.featureFlagResult = undefined posthogClientMock.featureFlagBehavior = 'resolve' diff --git a/src/main/lib/telemetry.ts b/src/main/lib/telemetry.ts index 0869baed5..66fb66c82 100644 --- a/src/main/lib/telemetry.ts +++ b/src/main/lib/telemetry.ts @@ -2226,6 +2226,7 @@ export async function shutdown(reason: string): Promise { } finally { client = null initialized = false + publishProactiveGreetingAdmission() } } @@ -2258,7 +2259,7 @@ export function installAppHooks(): void { beforeQuitHooked = true app.on('before-quit', (event) => { - if (drainingForQuit || !client) return + if (event.defaultPrevented || drainingForQuit || !client) return drainingForQuit = true event.preventDefault() const drainPromise = shutdown('quit').catch(() => {})