Skip to content

The agent that grows with you

fix(desktop): scope shared metrics consent to profile (#130714)

Tranquil-Flow1e8e4fbe9dbc
diff --git a/apps/desktop/src/app/contrib/hooks/use-desktop-metrics.test.tsx b/apps/desktop/src/app/contrib/hooks/use-desktop-metrics.test.tsx
new file mode 100644
index 00000000000..c4782c8e66a
--- /dev/null
+++ b/apps/desktop/src/app/contrib/hooks/use-desktop-metrics.test.tsx
@@ -0,0 +1,41 @@
+import { cleanup, renderHook, waitFor } from '@testing-library/react'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { requestGatewayForAgent } from '@/store/gateway'
+import { $sharedMetricsConsent, type SharedMetricsConsent } from '@/store/shared-metrics'
+
+import { useDesktopMetrics } from './use-desktop-metrics'
+
+vi.mock('@/store/gateway', async importActual => ({
+ ...(await importActual<Record<string, unknown>>()),
+ requestGatewayForAgent: vi.fn()
+}))
+
+const CONSENT: SharedMetricsConsent = { enabled: true, send: false, decided: true }
+
+afterEach(() => {
+ cleanup()
+ vi.mocked(requestGatewayForAgent).mockReset()
+ $sharedMetricsConsent.set(null)
+})
+
+describe('useDesktopMetrics', () => {
+ it('reads the shared metrics consent from the focused profile', async () => {
+ vi.mocked(requestGatewayForAgent).mockResolvedValue(CONSENT as never)
+
+ renderHook(() =>
+ useDesktopMetrics({
+ enabled: true,
+ gatewayOpen: true,
+ pathname: '/settings',
+ profile: 'havoc'
+ })
+ )
+
+ await waitFor(() =>
+ expect(requestGatewayForAgent).toHaveBeenCalledWith(null, 'havoc', 'shared_metrics.status', {
+ profile: 'havoc'
+ })
+ )
+ })
+})
diff --git a/apps/desktop/src/app/contrib/hooks/use-desktop-metrics.ts b/apps/desktop/src/app/contrib/hooks/use-desktop-metrics.ts
index d37f698a68e..b873fbcd8ec 100644
--- a/apps/desktop/src/app/contrib/hooks/use-desktop-metrics.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-desktop-metrics.ts
@@ -33,7 +33,12 @@ import { SESSION_SEARCH_FOCUS_EVENT } from '@/store/layout'
import { $profiles, $profilesByConnection, normalizeProfileKey } from '@/store/profile'
import { $modelPickerOpen, $sessionPickerOpen } from '@/store/session'
import { $switcherOpen } from '@/store/session-switcher'
-import { $sharedMetricsConsent, readSharedMetricsConsent, type SharedMetricsConsent } from '@/store/shared-metrics'
+import {
+ $sharedMetricsConsent,
+ readSharedMetricsConsent,
+ type SharedMetricsConsent,
+ sharedMetricsProfileRequester
+} from '@/store/shared-metrics'
import { observeOnboardingMetrics } from './desktop-onboarding-metrics'
@@ -108,8 +113,11 @@ export function useDesktopMetrics({
// Pinned to the focused (connection, profile): never whichever session tile is focused.
const request = useMemo(
() =>
- <T>(method: string, params: Record<string, unknown> = {}) =>
- requestGatewayForAgent<T>(connectionId, profileKey, method, params),
+ sharedMetricsProfileRequester(
+ <T>(method: string, params: Record<string, unknown> = {}) =>
+ requestGatewayForAgent<T>(connectionId, profileKey, method, params),
+ profileKey
+ ),
[connectionId, profileKey]
)
diff --git a/apps/desktop/src/app/settings/shared-metrics-settings.test.tsx b/apps/desktop/src/app/settings/shared-metrics-settings.test.tsx
new file mode 100644
index 00000000000..0274ded19a4
--- /dev/null
+++ b/apps/desktop/src/app/settings/shared-metrics-settings.test.tsx
@@ -0,0 +1,77 @@
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { en } from '@/i18n/en'
+import { requestGatewayForAgent } from '@/store/gateway'
+import { $activeGatewayProfile } from '@/store/profile'
+import { $settingsScopeOverride } from '@/store/settings-scope'
+import type { SharedMetricsConsent } from '@/store/shared-metrics'
+
+import { SharedMetricsSettings } from './shared-metrics-settings'
+
+vi.mock('@/store/gateway', async importActual => ({
+ ...(await importActual<Record<string, unknown>>()),
+ requestGatewayForAgent: vi.fn()
+}))
+
+const initialProfile = $activeGatewayProfile.get()
+const copy = en.sharedMetrics
+
+function installGatewayMock(initial: SharedMetricsConsent) {
+ let stored = initial
+
+ vi.mocked(requestGatewayForAgent).mockImplementation(async (_connectionId, _profile, method, params) => {
+ if (method === 'shared_metrics.set') {
+ const enabled = params?.enabled === true
+ stored = { enabled, send: enabled && params?.send === true, decided: true }
+ }
+
+ return stored as never
+ })
+}
+
+beforeEach(() => {
+ $activeGatewayProfile.set('havoc')
+ $settingsScopeOverride.set(null)
+})
+
+afterEach(() => {
+ cleanup()
+ vi.mocked(requestGatewayForAgent).mockReset()
+ $settingsScopeOverride.set(null)
+ $activeGatewayProfile.set(initialProfile)
+})
+
+describe('SharedMetricsSettings', () => {
+ it('reads and writes shared metrics consent for the settings profile', async () => {
+ installGatewayMock({ enabled: false, send: false, decided: false })
+
+ render(<SharedMetricsSettings />)
+
+ await waitFor(() =>
+ expect(requestGatewayForAgent).toHaveBeenCalledWith(
+ null,
+ 'havoc',
+ 'shared_metrics.status',
+ { profile: 'havoc' },
+ undefined,
+ undefined,
+ { spawnPriority: 'foreground' }
+ )
+ )
+
+ fireEvent.click(screen.getByRole('switch', { name: copy.collectLabel }))
+
+ await waitFor(() =>
+ expect(requestGatewayForAgent).toHaveBeenCalledWith(
+ null,
+ 'havoc',
+ 'shared_metrics.set',
+ { enabled: true, send: false, first_run: false, profile: 'havoc' },
+ undefined,
+ undefined,
+ { spawnPriority: 'foreground' }
+ )
+ )
+ })
+})
diff --git a/apps/desktop/src/app/settings/shared-metrics-settings.tsx b/apps/desktop/src/app/settings/shared-metrics-settings.tsx
index fa1cfb52a2e..4b37c729c27 100644
--- a/apps/desktop/src/app/settings/shared-metrics-settings.tsx
+++ b/apps/desktop/src/app/settings/shared-metrics-settings.tsx
@@ -1,5 +1,5 @@
import { useStore } from '@nanostores/react'
-import { useCallback, useEffect, useState } from 'react'
+import { useEffect, useMemo, useState } from 'react'
import { DocsLink } from '@/components/onboarding/flow'
import { useI18n } from '@/i18n'
@@ -13,7 +13,8 @@ import {
readSharedMetricsConsent,
saveSharedMetricsConsent,
SHARED_METRICS_DOCS_URL,
- type SharedMetricsConsent
+ type SharedMetricsConsent,
+ sharedMetricsProfileRequester
} from '@/store/shared-metrics'
import { ToggleRow } from './primitives'
@@ -33,11 +34,15 @@ export function SharedMetricsSettings() {
const [loaded, setLoaded] = useState(false)
const [busy, setBusy] = useState(false)
- const request = useCallback(
- <T,>(method: string, params: Record<string, unknown> = {}) =>
- requestGatewayForAgent<T>(connectionId, scopeProfile, method, params, undefined, undefined, {
- spawnPriority: 'foreground'
- }),
+ const request = useMemo(
+ () =>
+ sharedMetricsProfileRequester(
+ <T,>(method: string, params: Record<string, unknown> = {}) =>
+ requestGatewayForAgent<T>(connectionId, scopeProfile, method, params, undefined, undefined, {
+ spawnPriority: 'foreground'
+ }),
+ scopeProfile
+ ),
[connectionId, scopeProfile]
)
diff --git a/apps/desktop/src/components/shared-metrics/consent-dialog.test.tsx b/apps/desktop/src/components/shared-metrics/consent-dialog.test.tsx
index 22f7cc674a4..66542a23520 100644
--- a/apps/desktop/src/components/shared-metrics/consent-dialog.test.tsx
+++ b/apps/desktop/src/components/shared-metrics/consent-dialog.test.tsx
@@ -67,8 +67,9 @@ describe('SharedMetricsConsentDialog', () => {
it('details decide nothing when closed and record a chosen answer as both opt-ins', async () => {
const { calls, requestGateway } = backend({ enabled: false, send: false, decided: false })
- render(<SharedMetricsConsentDialog enabled profile="default" requestGateway={requestGateway} />)
+ render(<SharedMetricsConsentDialog enabled profile="havoc" requestGateway={requestGateway} />)
await waitFor(() => expect(sharedMetricsOfferPending($sharedMetricsConsent.get())).toBe(true))
+ expect(calls.find(c => c.method === 'shared_metrics.status')?.params).toEqual({ profile: 'havoc' })
$sharedMetricsDetailsOpen.set(true)
await screen.findByRole('dialog')
@@ -88,7 +89,8 @@ describe('SharedMetricsConsentDialog', () => {
expect(calls.find(c => c.method === 'shared_metrics.set')?.params).toEqual({
enabled: true,
send: false,
- first_run: true
+ first_run: true,
+ profile: 'havoc'
})
})
})
diff --git a/apps/desktop/src/components/shared-metrics/consent-dialog.tsx b/apps/desktop/src/components/shared-metrics/consent-dialog.tsx
index 71704a4f696..09c9b931c3c 100644
--- a/apps/desktop/src/components/shared-metrics/consent-dialog.tsx
+++ b/apps/desktop/src/components/shared-metrics/consent-dialog.tsx
@@ -1,5 +1,5 @@
import { useStore } from '@nanostores/react'
-import { useEffect, useId, useState } from 'react'
+import { useEffect, useId, useMemo, useState } from 'react'
import { DocsLink } from '@/components/onboarding/flow'
import { Button } from '@/components/ui/button'
@@ -25,6 +25,7 @@ import {
SHARED_METRICS_DOCS_URL,
type SharedMetricsChoice,
sharedMetricsOfferPending,
+ sharedMetricsProfileRequester,
type SharedMetricsRequester
} from '@/store/shared-metrics'
@@ -60,6 +61,11 @@ export function SharedMetricsConsentDialog({ enabled, profile, requestGateway }:
const [expanded, setExpanded] = useState(true)
const [saving, setSaving] = useState(false)
+ const scopedRequest = useMemo(
+ () => sharedMetricsProfileRequester(requestGateway, profile),
+ [profile, requestGateway]
+ )
+
// Never over the provider picker, the free-tier welcome or the guided chat:
// the question belongs to the moment after setup.
const onboardingSettled =
@@ -79,14 +85,14 @@ export function SharedMetricsConsentDialog({ enabled, profile, requestGateway }:
let cancelled = false
- void readSharedMetricsConsent(requestGateway).then(next => {
+ void readSharedMetricsConsent(scopedRequest).then(next => {
if (!cancelled) {
$sharedMetricsConsent.set(next)
}
})
return () => void (cancelled = true)
- }, [ready, profile, requestGateway])
+ }, [ready, profile, scopedRequest])
if (!ready || !detailsOpen || !sharedMetricsOfferPending(consent)) {
return null
@@ -100,7 +106,7 @@ export function SharedMetricsConsentDialog({ enabled, profile, requestGateway }:
setSaving(true)
try {
- await answerSharedMetricsOffer(requestGateway, choice)
+ await answerSharedMetricsOffer(scopedRequest, choice)
} catch (err) {
notifyError(err, copy.saveFailed)
} finally {
diff --git a/apps/desktop/src/components/shared-metrics/consent-strip.test.tsx b/apps/desktop/src/components/shared-metrics/consent-strip.test.tsx
new file mode 100644
index 00000000000..7b74b3184da
--- /dev/null
+++ b/apps/desktop/src/components/shared-metrics/consent-strip.test.tsx
@@ -0,0 +1,61 @@
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { en } from '@/i18n/en'
+import { $activeGatewayProfile } from '@/store/profile'
+import { $sharedMetricsConsent, type SharedMetricsConsent } from '@/store/shared-metrics'
+
+import { SharedMetricsConsentStrip } from './consent-strip'
+
+const requestGateway = vi.fn()
+
+vi.mock('@/app/gateway/hooks/use-gateway-request', () => ({
+ useGatewayRequest: () => ({ requestGateway })
+}))
+
+const copy = en.sharedMetrics
+const initialProfile = $activeGatewayProfile.get()
+
+function installGatewayMock(initial: SharedMetricsConsent) {
+ let stored = initial
+
+ requestGateway.mockImplementation(async (method: string, params?: Record<string, unknown>) => {
+ if (method === 'shared_metrics.set') {
+ const enabled = params?.enabled === true
+ stored = { enabled, send: enabled && params?.send === true, decided: true }
+ }
+
+ return stored
+ })
+}
+
+beforeEach(() => {
+ $activeGatewayProfile.set('havoc')
+ $sharedMetricsConsent.set({ enabled: false, send: false, decided: false })
+})
+
+afterEach(() => {
+ cleanup()
+ requestGateway.mockReset()
+ $activeGatewayProfile.set(initialProfile)
+ $sharedMetricsConsent.set(null)
+})
+
+describe('SharedMetricsConsentStrip', () => {
+ it('records the chosen answer on the focused profile', async () => {
+ installGatewayMock({ enabled: false, send: false, decided: false })
+
+ render(<SharedMetricsConsentStrip />)
+
+ fireEvent.click(screen.getByRole('button', { name: copy.stripChoices.local }))
+
+ await waitFor(() =>
+ expect(requestGateway).toHaveBeenCalledWith('shared_metrics.set', {
+ enabled: true,
+ send: false,
+ first_run: true,
+ profile: 'havoc'
+ })
+ )
+ })
+})
\ No newline at end of file
diff --git a/apps/desktop/src/components/shared-metrics/consent-strip.tsx b/apps/desktop/src/components/shared-metrics/consent-strip.tsx
index f9b4c8b71b6..397b0f950b8 100644
--- a/apps/desktop/src/components/shared-metrics/consent-strip.tsx
+++ b/apps/desktop/src/components/shared-metrics/consent-strip.tsx
@@ -1,4 +1,5 @@
-import { useState } from 'react'
+import { useStore } from '@nanostores/react'
+import { useMemo, useState } from 'react'
import { useGatewayRequest } from '@/app/gateway/hooks/use-gateway-request'
import { StatusRow } from '@/components/chat/status-row'
@@ -6,7 +7,13 @@ import { Button } from '@/components/ui/button'
import { Codicon } from '@/components/ui/codicon'
import { useI18n } from '@/i18n'
import { notifyError } from '@/store/notifications'
-import { $sharedMetricsDetailsOpen, answerSharedMetricsOffer, type SharedMetricsChoice } from '@/store/shared-metrics'
+import { $activeGatewayProfile, normalizeProfileKey } from '@/store/profile'
+import {
+ $sharedMetricsDetailsOpen,
+ answerSharedMetricsOffer,
+ type SharedMetricsChoice,
+ sharedMetricsProfileRequester
+} from '@/store/shared-metrics'
const CHOICES: readonly SharedMetricsChoice[] = ['share', 'local', 'off']
@@ -20,13 +27,19 @@ export function SharedMetricsConsentStrip() {
const { requestGateway } = useGatewayRequest()
const { t } = useI18n()
const copy = t.sharedMetrics
+ const profile = normalizeProfileKey(useStore($activeGatewayProfile))
const [saving, setSaving] = useState(false)
+ const scopedRequest = useMemo(
+ () => sharedMetricsProfileRequester(requestGateway, profile),
+ [profile, requestGateway]
+ )
+
const choose = async (choice: SharedMetricsChoice) => {
setSaving(true)
try {
- await answerSharedMetricsOffer(requestGateway, choice)
+ await answerSharedMetricsOffer(scopedRequest, choice)
} catch (err) {
notifyError(err, copy.saveFailed)
} finally {
diff --git a/apps/desktop/src/store/shared-metrics.ts b/apps/desktop/src/store/shared-metrics.ts
index 973df6f6d76..6588d570125 100644
--- a/apps/desktop/src/store/shared-metrics.ts
+++ b/apps/desktop/src/store/shared-metrics.ts
@@ -19,6 +19,13 @@ export const SHARED_METRICS_CHOICES: Record<SharedMetricsChoice, { enabled: bool
export type SharedMetricsRequester = <T = unknown>(method: string, params?: Record<string, unknown>) => Promise<T>
+export function sharedMetricsProfileRequester(
+ request: SharedMetricsRequester,
+ profile: string
+): SharedMetricsRequester {
+ return (method, params = {}) => request(method, { ...params, profile })
+}
+
function isConsent(value: unknown): value is SharedMetricsConsent {
return typeof value === 'object' && value !== null && typeof (value as SharedMetricsConsent).decided === 'boolean'
}