From 8a3a4577e460015fcdf342d65c4c957fbb3db3c5 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:30:56 -0800 Subject: [PATCH] Honor paired desktop consent when chat host runs telemetry --- .../remote-accepted-prompts.md | 2 +- .../telemetry/structured-chat-dispatch.ts | 8 +- .../structured-chat-prompt-owner.test.ts | 155 ++++++++++++++++++ 3 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 src/main/telemetry/structured-chat-prompt-owner.test.ts diff --git a/docs/agents-md-refs/protected-telemetry-milestones/remote-accepted-prompts.md b/docs/agents-md-refs/protected-telemetry-milestones/remote-accepted-prompts.md index 5695b9d066f..d894ae0f1a5 100644 --- a/docs/agents-md-refs/protected-telemetry-milestones/remote-accepted-prompts.md +++ b/docs/agents-md-refs/protected-telemetry-milestones/remote-accepted-prompts.md @@ -2,7 +2,7 @@ The committed journal acceptance observer in `native-chat-prompt-submissions.ts` owns the evidence. A fresh personal send from a transport-proven desktop or phone retains its device class and consent-owning desktop in memory until provider acceptance. Queue conversion keeps the original draft's provenance. Reservation, handover, rejected sends, withdrawn drafts, conversation commands, unattended sends and operation replay do not emit an accepted prompt observation. -An initialized desktop records `native_chat_prompt_submitted` through its existing telemetry client, including when consent is disabled (the client drops the observation). A headless host instead uses the existing live structured-chat relay on `runtime.clientEvents.subscribe`. The additive `structured-chat-prompt-telemetry-v1` capability isolates the new event from older readers; older hosts simply omit it. Prompt delivery and journal storage are unchanged. +A paired desktop owner always receives its observation through the existing live structured-chat relay on `runtime.clientEvents.subscribe`, even when the execution host also has initialized desktop telemetry. That host's installation and consent do not own the paired desktop's activity. Only sends without a paired desktop owner, such as the local desktop or its directly paired phone, use the host's initialized telemetry client; disabled consent drops them without a relay fallback. The additive `structured-chat-prompt-telemetry-v1` capability isolates the new event from older readers; older hosts simply omit it. Prompt delivery and journal storage are unchanged. The event contains only `type`, a fresh observation UUID, `agent_kind` and `client` (`desktop` or `mobile`). No prompt text, session/message/request identifiers, paths, fingerprints, timestamps or device identifiers cross this observation channel. The owner routing key stays on the host: a paired desktop uses its authenticated device ID, and a delegated phone uses its authenticated parent desktop ID. Only an authenticated runtime desktop stream with that owner and the new capability receives the event. Another desktop, a phone stream, the CLI or a declared client without proven identity cannot receive it. A phone paired directly to the local desktop follows the existing local telemetry path. diff --git a/src/main/telemetry/structured-chat-dispatch.ts b/src/main/telemetry/structured-chat-dispatch.ts index 1c2785e0a03..97ffb5378ad 100644 --- a/src/main/telemetry/structured-chat-dispatch.ts +++ b/src/main/telemetry/structured-chat-dispatch.ts @@ -65,14 +65,12 @@ export function recordRemoteStructuredChatTelemetry(value: unknown): boolean { return true } -/** Only the committed acceptance observer supplies these facts; remote hosts have no desktop consent. */ +/** A paired owner retains consent authority even when this host also runs a desktop. */ export function recordStructuredChatPromptAccepted( source: EventProps<'native_chat_prompt_submitted'>, ownerKey: string | undefined ): void { - if (isTelemetryInitialized()) { - track('native_chat_prompt_submitted', source) - } else if (ownerKey) { + if (ownerKey) { const event: StructuredChatPromptTelemetryEvent = { type: 'structuredChatPromptAccepted', id: randomUUID(), @@ -80,6 +78,8 @@ export function recordStructuredChatPromptAccepted( client: source.client } relayStructuredChatTelemetry(event, ownerKey) + } else if (isTelemetryInitialized()) { + track('native_chat_prompt_submitted', source) } } diff --git a/src/main/telemetry/structured-chat-prompt-owner.test.ts b/src/main/telemetry/structured-chat-prompt-owner.test.ts new file mode 100644 index 00000000000..ff0cdbed471 --- /dev/null +++ b/src/main/telemetry/structured-chat-prompt-owner.test.ts @@ -0,0 +1,155 @@ +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import type { RpcContext } from '../runtime/rpc/core' +import type { RpcCallerIdentity } from '../runtime/rpc/rpc-caller-identity' +import { structuredCallerFor } from '../runtime/rpc/methods/structured-sessions/creation/structured-agent-session-gate' +import type { StructuredChatTelemetryEvent } from '../../shared/structured-chat-dispatch-telemetry' +import { _setCommonPropsForTests } from './client' +import { + BASE_COMMON, + cleanupTelemetryClientTest, + setupTelemetryClientTest, + type TelemetryClientTestState +} from './client-test-harness' +import { + recordRemoteStructuredChatTelemetry, + recordStructuredChatPromptAccepted, + subscribeStructuredChatTelemetry +} from './structured-chat-dispatch' + +const HOST_INSTALL = '11111111-1111-4111-8111-111111111111' +const OWNER_INSTALL = '22222222-2222-4222-8222-222222222222' +let active: TelemetryClientTestState | undefined +let relayed: { event: StructuredChatTelemetryEvent; owner: string }[] +let unsubscribe: () => void + +beforeEach(() => { + vi.useFakeTimers() + vi.setSystemTime(new Date('2026-11-08T23:58:00Z')) + relayed = [] + unsubscribe = subscribeStructuredChatTelemetry((event, owner) => relayed.push({ event, owner })) +}) +afterEach(() => { + unsubscribe() + if (active) { + cleanupTelemetryClientTest(active.envStash) + active = undefined + } + vi.useRealTimers() +}) + +function desktop(installId: string, optedIn: boolean): TelemetryClientTestState { + if (active) { + cleanupTelemetryClientTest(active.envStash) + } + active = setupTelemetryClientTest({ + installId, + optedIn, + existedBeforeTelemetryRelease: false, + protectedMilestones: { installCohort: 'fresh' } + }) + _setCommonPropsForTests({ ...BASE_COMMON, install_id: installId }) + return active +} + +function accept( + caller: RpcCallerIdentity, + clientKind?: 'mobile' | 'runtime', + pairedDeviceParentId?: string +): void { + const context: Partial = { caller, clientKind, pairedDeviceParentId } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: caller derivation reads only the supplied transport identity fields. + const identity = structuredCallerFor(context as RpcContext) + if (!identity.userClient) { + throw new Error('expected a transport-proven user') + } + recordStructuredChatPromptAccepted( + { agent_kind: 'codex', runtime: 'unknown', client: identity.userClient }, + identity.telemetryOwnerKey + ) +} + +const captured = (state: TelemetryClientTestState, name: string) => + state.mock.capture.mock.calls.map(([event]) => event).filter((event) => event.event === name) + +it('lets the originating desktop opt in even when its initialized execution host opts out', () => { + const host = desktop(HOST_INSTALL, false) + accept({ kind: 'paired-device', deviceId: 'originating-desktop' }, 'runtime') + expect(relayed).toEqual([ + { + owner: 'originating-desktop', + event: { + type: 'structuredChatPromptAccepted', + id: expect.any(String), + agent_kind: 'codex', + client: 'desktop' + } + } + ]) + expect(host.settings.telemetry?.protectedMilestones).toEqual({ installCohort: 'fresh' }) + expect(host.mock.capture.mock.calls).toEqual([]) + + const owner = desktop(OWNER_INSTALL, true) + recordRemoteStructuredChatTelemetry(relayed[0].event) + expect(owner.settings.telemetry?.protectedMilestones).toEqual({ + installCohort: 'fresh', + firstAgentInteractionDay: 20_765, + meaningfulActivityDay: 20_765 + }) + expect(captured(owner, 'first_agent_interaction')[0].properties).toMatchObject({ + install_id: OWNER_INSTALL, + source: 'native_chat' + }) + expect(captured(owner, 'meaningful_activity_day')[0].properties).toMatchObject({ + install_id: OWNER_INSTALL, + trigger: 'native_chat' + }) +}) + +it('respects a delegated phone owners opt-out even when its initialized execution host opts in', () => { + const host = desktop(HOST_INSTALL, true) + accept({ kind: 'paired-device', deviceId: 'delegated-phone' }, 'mobile', 'originating-desktop') + expect(relayed).toEqual([ + { + owner: 'originating-desktop', + event: { + type: 'structuredChatPromptAccepted', + id: expect.any(String), + agent_kind: 'codex', + client: 'mobile' + } + } + ]) + expect(host.settings.telemetry?.protectedMilestones).toEqual({ installCohort: 'fresh' }) + expect(host.mock.capture.mock.calls).toEqual([]) + + const owner = desktop(OWNER_INSTALL, false) + recordRemoteStructuredChatTelemetry(relayed[0].event) + expect(owner.settings.telemetry?.protectedMilestones).toEqual({ installCohort: 'fresh' }) + expect(owner.mock.capture.mock.calls).toEqual([]) +}) + +it('keeps the local desktop and its directly paired phone on host installation consent', () => { + const host = desktop(HOST_INSTALL, true) + accept({ kind: 'desktop' }) + vi.setSystemTime(new Date('2026-11-09T00:01:00Z')) + accept({ kind: 'paired-device', deviceId: 'direct-phone' }, 'mobile') + expect(relayed).toEqual([]) + expect(host.settings.telemetry?.protectedMilestones).toEqual({ + installCohort: 'fresh', + firstAgentInteractionDay: 20_765, + meaningfulActivityDay: 20_766 + }) + expect(captured(host, 'first_agent_interaction')[0].properties).toMatchObject({ + install_id: HOST_INSTALL, + source: 'native_chat' + }) + expect( + captured(host, 'meaningful_activity_day').map((event) => ({ + install_id: event.properties.install_id, + trigger: event.properties.trigger + })) + ).toEqual([ + { install_id: HOST_INSTALL, trigger: 'native_chat' }, + { install_id: HOST_INSTALL, trigger: 'mobile_chat' } + ]) +})