Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
Report managed agents and template-made rooms, and point managed servers at the current relay (#963)
Christian McDermott8851266699f0
diff --git a/console/AGENTS.md b/console/AGENTS.mdindex 03ead0808..e029b4740 100644--- a/console/AGENTS.md+++ b/console/AGENTS.md@@ -432,10 +432,10 @@ pnpm run linta restart offered, never applied by restarting the user's server for them. Whether itis out of step is read off the running container, not inferred from what we last wrote —and a host that cannot be read is reported as unknown, never as agreeing.-- **The toggle defaults to off.** The payload carries a random per-install id, which makes- it pseudonymous personal data under GDPR/nFADP; an opt-out default does not cover that.- Flipping the default back to on is a product decision that requires the id to go first,- not a code change.+- **The toggle defaults to on.** Usage data is opt-out: a first-run notice says what is+ shared and carries the off switch, and the same toggle lives in Settings. That default is+ a product decision, and so is what identifies an install (today, only the random+ per-install id). Changing either is a product and privacy call, not a code change.- **What a telemetry payload may contain.** Add an event only by adding it to the closedcatalogue in `src/main/core/telemetry/events.ts`: its property types are literal unions,numbers and booleans, and `TELEMETRY_EVENT_PROPERTIES` names the same fields as data, whichdiff --git a/console/apps/switch-console-desktop/src/main/core/agent-migration/managed-agent-location.ts b/console/apps/switch-console-desktop/src/main/core/agent-migration/managed-agent-location.tsnew file mode 100644index 000000000..7bb741895--- /dev/null+++ b/console/apps/switch-console-desktop/src/main/core/agent-migration/managed-agent-location.ts@@ -0,0 +1,23 @@+import { embeddedControllerService } from '@main/core/embedded-controller/embedded-controllers';+import type { TelemetryLocationKind } from '@main/core/telemetry/events';+import { log } from '@main/lib/logger';++/**+ * Where managed agents on `serverId` run, from one read of this computer's+ * enrollment: `local` on its controller, `remote` elsewhere, `unknown` (logged)+ * when the read fails.+ */+export async function managedAgentLocator(+ serverId: string+): Promise<(controllerId: string) => TelemetryLocationKind> {+ try {+ const thisComputer = await embeddedControllerService.enrolledControllerId(serverId);+ return (controllerId) => (thisComputer === controllerId ? 'local' : 'remote');+ } catch (error) {+ log.warn('Could not tell whether managed agents run on this computer; reporting unknown', {+ event: 'telemetry_managed_agent_location',+ error: String(error),+ });+ return () => 'unknown';+ }+}diff --git a/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent-service.test.ts b/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent-service.test.tsindex 53ce74b1a..f108a3abb 100644--- a/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent-service.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent-service.test.ts@@ -1,8 +1,10 @@import { beforeEach, describe, expect, it } from 'vitest';+import type { TelemetryAgentCreateFailure } from '@main/core/telemetry/events';import type { TargetLookup } from './agent-migration-service';import {type AddManagedAgentParams,type ManagedCreateOutcome,+ managedCreateFailureOf,type NewManagedAgentDeps,NewManagedAgentService,} from './new-managed-agent-service';@@ -38,15 +40,20 @@ const READY: TargetLookup = {controller: { controllerId: 'controller-1', state: 'running' },};+/** What the session seam throws when there is no sign-in to use. */+class SignedOut extends Error {}+type Config = {lookup: TargetLookup;management: boolean;- createOutcome: ManagedCreateOutcome;+ createOutcome: ManagedCreateOutcome | Error;};type Harness = {deps: NewManagedAgentDeps;created: unknown[];+ /** The failure reason of every `agent_created` reported, in order. */+ reported: TelemetryAgentCreateFailure[];set(patch: Partial<Config>): void;};@@ -57,8 +64,10 @@ function harness(): Harness {createOutcome: { kind: 'created', switchAgentId: 'switch-9' },};const created: unknown[] = [];+ const reported: TelemetryAgentCreateFailure[] = [];return {created,+ reported,set: (patch) => Object.assign(config, patch),deps: {workspaceFor: async () => 'workspace-1',@@ -66,8 +75,11 @@ function harness(): Harness {managementAvailable: async () => config.management,create: async (_workspaceId, body) => {created.push(body);+ if (config.createOutcome instanceof Error) throw config.createOutcome;return config.createOutcome;},+ reportCreate: (_input, failure) => reported.push(failure),+ thrownFailure: (error) => (error instanceof SignedOut ? 'unauthenticated' : 'error'),log: { info: () => {}, warn: () => {}, error: () => {} },},};@@ -134,7 +146,11 @@ describe('NewManagedAgentService.add', () => {it('shows a refusal of the agent or its placement in Switch’s words', async () => {h.set({- createOutcome: { kind: 'refused', message: 'Claude Code is not logged in on laptop.' },+ createOutcome: {+ kind: 'refused',+ message: 'Claude Code is not logged in on laptop.',+ reason: 'refused',+ },});expect(await new NewManagedAgentService(h.deps).add(PARAMS)).toEqual({kind: 'error',@@ -147,6 +163,7 @@ describe('NewManagedAgentService.add', () => {createOutcome: {kind: 'refused',message: "advanced_config: codex has no setting 'tools' (it takes effort, verbosity)",+ reason: 'invalid',},});const result = await new NewManagedAgentService(h.deps).add({@@ -168,6 +185,96 @@ describe('NewManagedAgentService.add', () => {});});+describe('NewManagedAgentService.add reporting', () => {+ let h: Harness;+ beforeEach(() => {+ h = harness();+ });++ it('reports an agent it created', async () => {+ await new NewManagedAgentService(h.deps).add(PARAMS);+ expect(h.reported).toEqual(['none']);+ });++ it('reports a taken name as a name conflict', async () => {+ h.set({ createOutcome: { kind: 'name-conflict' } });+ await new NewManagedAgentService(h.deps).add(PARAMS);+ expect(h.reported).toEqual(['name_conflict']);+ });++ it('reports a refusal under the reason Switch gave, not as a fault', async () => {+ for (const reason of ['refused', 'invalid', 'unauthenticated'] as const) {+ const run = harness();+ run.set({ createOutcome: { kind: 'refused', message: 'No.', reason } });+ await new NewManagedAgentService(run.deps).add(PARAMS);+ expect(run.reported).toEqual([reason]);+ }+ });++ it('reports instructions it turned away before asking the server as invalid', async () => {+ await new NewManagedAgentService(h.deps).add({+ ...PARAMS,+ instructions: 'x'.repeat(33 * 1024),+ });+ expect(h.reported).toEqual(['invalid']);+ });++ it('reports a create the session seam refused for want of a sign-in as unauthenticated', async () => {+ h.set({ createOutcome: new SignedOut('Not signed in to this Switch server.') });+ await expect(new NewManagedAgentService(h.deps).add(PARAMS)).rejects.toThrow('Not signed in');+ expect(h.reported).toEqual(['unauthenticated']);+ });++ it('reports a create that threw, once, and still throws it', async () => {+ h.set({ createOutcome: new Error('connect ECONNREFUSED') });+ await expect(new NewManagedAgentService(h.deps).add(PARAMS)).rejects.toThrow('ECONNREFUSED');+ expect(h.reported).toEqual(['error']);+ });+});++describe('managedCreateFailureOf', () => {+ const failure = (+ status: number | undefined,+ code: string | null,+ kind: 'http' | 'unauthorized' | 'network' = 'http'+ ) => managedCreateFailureOf({ kind, status, code });++ it('reads a 409 that is a validation error as the name taken', () => {+ expect(failure(409, 'validation_error')).toBe('name-conflict');+ expect(failure(409, null)).toBe('name-conflict');+ });++ it('reads a 409 that names the machine as a refused placement, not a taken name', () => {+ for (const code of [+ 'controller_offline',+ 'controller_revoked',+ 'provider_not_installed',+ 'provider_login_expired',+ ])+ expect(failure(409, code)).toBe('refused');+ });++ it('files an expired session apart from a placement', () => {+ expect(failure(401, null, 'unauthorized')).toBe('unauthenticated');+ });++ it('files fields Switch would not accept as invalid', () => {+ expect(failure(422, 'validation_error')).toBe('invalid');+ expect(failure(400, 'definition_invalid')).toBe('invalid');+ expect(failure(400, null)).toBe('invalid');+ });++ it('files anything else Switch turned down as refused', () => {+ expect(failure(403, 'forbidden')).toBe('refused');+ });++ it('is not a refusal when the server could not be reached or failed', () => {+ expect(failure(undefined, null, 'network')).toBeNull();+ expect(failure(undefined, null, 'unauthorized')).toBeNull();+ expect(failure(503, 'internal')).toBeNull();+ });+});+describe('NewManagedAgentService.machineFor', () => {const ref = { serverId: 'server-1', workspaceId: 'workspace-1', sshHost: null };diff --git a/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent-service.ts b/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent-service.tsindex bef50e9d6..166e3c05c 100644--- a/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent-service.ts+++ b/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent-service.ts@@ -1,4 +1,6 @@import type { AdvancedConfig } from '@switch-console/plugins/agents';+import type { GatewayErrorKind } from '@main/core/switch-servers/gateway-client';+import type { TelemetryAgentCreateFailure } from '@main/core/telemetry/events';import type { NewAgentMachine } from '@shared/core/agent-migration/agent-migration';import type { AgentProviderId } from '@shared/core/providers/agent-provider-registry';import type { UiEntryPoint } from '@shared/core/telemetry/reporting';@@ -36,11 +38,38 @@ export type AddManagedAgentResult =| { kind: 'name-conflict' }| { kind: 'error'; message: string };+/** Why Switch refused a managed agent, as `agent_created` reports it. */+export type ManagedRefusal = Extract<+ TelemetryAgentCreateFailure,+ 'unauthenticated' | 'invalid' | 'refused'+>;++/**+ * What a failed create means, or null for one to rethrow. Switch answers a+ * taken name and a refused placement both with a 409, told apart by the reason+ * code: a taken name is a validation error.+ */+export function managedCreateFailureOf(error: {+ kind: GatewayErrorKind;+ status: number | undefined;+ /** The management API's reason code, when the response carried one. */+ code: string | null;+}): 'name-conflict' | ManagedRefusal | null {+ if (error.status === undefined || error.status >= 500) return null;+ if (error.kind === 'unauthorized') return 'unauthenticated';+ if (error.status === 409) {+ return error.code === 'validation_error' || error.code === null ? 'name-conflict' : 'refused';+ }+ if (error.code === 'validation_error' || error.code === 'definition_invalid') return 'invalid';+ if (error.code === null && (error.status === 400 || error.status === 422)) return 'invalid';+ return 'refused';+}+export type ManagedCreateOutcome =| { kind: 'created'; switchAgentId: string }| { kind: 'name-conflict' }/** Switch refused the agent or its placement, in its own words. */- | { kind: 'refused'; message: string };+ | { kind: 'refused'; message: string; reason: ManagedRefusal };export type NewManagedAgentDeps = {/** The workspace the server's agents belong to. */@@ -60,6 +89,10 @@ export type NewManagedAgentDeps = {definition: ManagedDefinition & { isolation: 'shared' | 'isolated' };}): Promise<ManagedCreateOutcome>;+ /** Reports the attempt as `agent_created`; never awaited. */+ reportCreate(input: AddManagedAgentParams, failure: TelemetryAgentCreateFailure): void;+ /** A create that threw, as a reason: a missing sign-in, or else a fault. */+ thrownFailure(error: unknown): Extract<TelemetryAgentCreateFailure, 'unauthenticated' | 'error'>;log: MigrationLog;};@@ -94,11 +127,22 @@ export class NewManagedAgentService {* refuses it in its own words.*/async add(input: AddManagedAgentParams): Promise<AddManagedAgentResult> {+ try {+ return await this.createAgent(input);+ } catch (error) {+ this.deps.reportCreate(input, this.deps.thrownFailure(error));+ throw error;+ }+ }++ private async createAgent(input: AddManagedAgentParams): Promise<AddManagedAgentResult> {const workspaceId = await this.deps.workspaceFor(input.serverId);try {assertInstructionsFit(input.instructions);} catch (error) {+ // The same mistake Switch would refuse as an invalid field, caught first.+ this.deps.reportCreate(input, 'invalid');return { kind: 'error', message: message(error) };}const definition: ManagedDefinition = {@@ -120,12 +164,19 @@ export class NewManagedAgentService {desired_state: 'running',definition: { ...definition, isolation: input.isolation },});- if (created.kind === 'name-conflict') return { kind: 'name-conflict' };- if (created.kind === 'refused') return { kind: 'error', message: created.message };+ if (created.kind === 'name-conflict') {+ this.deps.reportCreate(input, 'name_conflict');+ return { kind: 'name-conflict' };+ }+ if (created.kind === 'refused') {+ this.deps.reportCreate(input, created.reason);+ return { kind: 'error', message: created.message };+ }this.deps.log.info('Created a managed agent', {switchAgentId: created.switchAgentId,controllerId: input.machineId,});+ this.deps.reportCreate(input, 'none');return {kind: 'created',serverId: input.serverId,diff --git a/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent.ts b/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent.tsindex 0b123f9a7..536a58b82 100644--- a/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent.ts+++ b/console/apps/switch-console-desktop/src/main/core/agent-migration/new-managed-agent.ts@@ -3,13 +3,18 @@ import {createManagedAgent,fetchManagementControllers,GatewayError,+ managementErrorCode,managementErrorMessage,} from '@main/core/switch-servers/gateway-client';+import { agentTypeOf } from '@main/core/telemetry/agent-type';+import { entryPointOf } from '@main/core/telemetry/narrow';+import { trackEventAt } from '@main/core/telemetry/telemetry-service';import { withReachableWorkspaceSession } from '@main/core/workspaces/workspace-session';import { requireWorkspaceForServer } from '@main/core/workspaces/workspaces-store';import { log } from '@main/lib/logger';import { resolveMachine } from './agent-migration';-import { NewManagedAgentService } from './new-managed-agent-service';+import { managedAgentLocator } from './managed-agent-location';+import { managedCreateFailureOf, NewManagedAgentService } from './new-managed-agent-service';export const newManagedAgentService = new NewManagedAgentService({workspaceFor: async (serverId) => (await requireWorkspaceForServer(serverId)).id,@@ -29,12 +34,35 @@ export const newManagedAgentService = new NewManagedAgentService({try {return { kind: 'created', switchAgentId: await createManagedAgent(server, body) };} catch (error) {- if (error instanceof GatewayError && error.status === 409) return { kind: 'name-conflict' };- if (error instanceof GatewayError && error.status !== undefined && error.status < 500)- return { kind: 'refused', message: managementErrorMessage(error) };- throw error;+ if (!(error instanceof GatewayError)) throw error;+ const failure = managedCreateFailureOf({+ kind: error.kind,+ status: error.status,+ code: managementErrorCode(error),+ });+ if (failure === null) throw error;+ if (failure === 'name-conflict') return { kind: 'name-conflict' };+ return { kind: 'refused', message: managementErrorMessage(error), reason: failure };}}),+ reportCreate: (input, failure_reason) => {+ const at = Date.now();+ void managedAgentLocator(input.serverId).then((locate) =>+ trackEventAt(+ 'agent_created',+ {+ agent_type: agentTypeOf(input.providerId),+ location: locate(input.machineId),+ outcome: failure_reason === 'none' ? 'success' : 'failure',+ failure_reason,+ entry_point: entryPointOf(input.entryPoint),+ },+ at+ )+ );+ },+ thrownFailure: (error) =>+ error instanceof GatewayError && error.kind === 'unauthorized' ? 'unauthenticated' : 'error',log: {info: (message, fields) => log.info(message, { event: 'new_managed_agent', ...fields }),warn: (message, fields) => log.warn(message, { event: 'new_managed_agent', ...fields }),diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.test.tsindex 746efcdc9..ef79bff89 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.test.ts@@ -41,6 +41,7 @@ describe('buildEnvFile', () => {telemetryEnabled: false,telemetryEnvironment: 'prod',telemetryInternal: false,+ telemetryEndpoint: 'https://relay.example.test',});const vars = Object.fromEntries(env@@ -205,6 +206,7 @@ describe('buildEnvFile', () => {telemetryEnabled: true,telemetryEnvironment: 'prod',telemetryInternal: false,+ telemetryEndpoint: 'https://relay.example.test',});expect(sharing).toContain('TELEMETRY_ENABLED=true');});@@ -221,6 +223,7 @@ describe('buildEnvFile', () => {telemetryEnabled: true,telemetryEnvironment: 'local',telemetryInternal: false,+ telemetryEndpoint: 'https://relay.example.test',});expect(fromDevBuild).toContain('TELEMETRY_ENVIRONMENT=local');@@ -239,6 +242,7 @@ describe('buildEnvFile', () => {telemetryEnabled: true,telemetryEnvironment: 'prod',telemetryInternal: true,+ telemetryEndpoint: 'https://relay.example.test',});expect(forStaff).toContain('TELEMETRY_INTERNAL=true');@@ -246,6 +250,11 @@ describe('buildEnvFile', () => {expect(composeYaml).toMatch(/^\s+TELEMETRY_INTERNAL:\s*$/m);});+ it('names the relay the server reports to, rather than leaving it to the server’s default', () => {+ expect(vars.TELEMETRY_ENDPOINT).toBe('https://relay.example.test');+ expect(composeYaml).toMatch(/^\s+TELEMETRY_ENDPOINT:\s*$/m);+ });+it('is the only thing the compose file needs to forward the gate', () => {// A value in the `.env` that the `switch` service does not name never// reaches the container, which is the failure mode this pairing exists to@@ -273,6 +282,7 @@ describe('readStackEnv', () => {telemetryEnabled: true,telemetryEnvironment: 'prod',telemetryInternal: false,+ telemetryEndpoint: 'https://relay.example.test',});it('reads back exactly what buildEnvFile wrote', () => {@@ -387,6 +397,7 @@ describe('keysDisagreeing', () => {telemetryEnabled: true,telemetryEnvironment: 'prod',telemetryInternal: false,+ telemetryEndpoint: 'https://relay.example.test',});it('finds nothing wrong with a copy of the same settings', () => {diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.tsindex 02d92fa92..5acd8d2e8 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/env-file.ts@@ -32,6 +32,8 @@ export type LocalServerEnvParams = {telemetryEnvironment: TelemetryFlintEnv;/** Whether the person this server runs for is staff, as Console tells it. */telemetryInternal: boolean;+ /** The relay the server sends its usage to: the one the app reports to. */+ telemetryEndpoint: string;};/**@@ -62,6 +64,9 @@ export const MANAGED_SERVER_FEATURE_FLAGS = managedServerFeatureFlags(* reading the choice back off a host has to look for the same key. */export const TELEMETRY_ENABLED_KEY = 'TELEMETRY_ENABLED';+/** The variable the server reads its relay address from, read back for the same reason. */+export const TELEMETRY_ENDPOINT_KEY = 'TELEMETRY_ENDPOINT';+/*** Render the `.env` docker compose interpolates for the local stack. Pure — the* host/user values are fixed by the compose contract, ports are chosen per@@ -78,6 +83,7 @@ export function buildEnvFile(params: LocalServerEnvParams): string {telemetryEnabled,telemetryEnvironment,telemetryInternal,+ telemetryEndpoint,} = params;return ['# Generated by Switch Console local-server mode — do not edit by hand.',@@ -136,6 +142,8 @@ export function buildEnvFile(params: LocalServerEnvParams): string {`${TELEMETRY_ENABLED_KEY}=${telemetryEnabled ? 'true' : 'false'}`,`TELEMETRY_ENVIRONMENT=${telemetryEnvironment}`,`TELEMETRY_INTERNAL=${telemetryInternal ? 'true' : 'false'}`,+ // Not left to the server's default, which is whatever its version shipped with.+ `${TELEMETRY_ENDPOINT_KEY}=${telemetryEndpoint}`,// Tells the server's agents that Switch Console runs it, so switch-buddy// can tell this apart from Switch Cloud and a self-hosted server.'MANAGED_BY_CONSOLE=true',diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.tsindex 58805216b..72eefcd4d 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/local-server-service.ts@@ -492,6 +492,8 @@ export class LocalServerService {// Nobody else shares the local stack, so there is nothing to lock.result = await startStack({ ...common, lease: null });}+ // A move that worked leaves a native server; one that failed, the Docker one.+ const ranNative = move ? moveFailure === null : native;if (result.kind === 'docker-unavailable') {this.setStatus({ phase: 'error', error: result.detail });this.failUpgrade(result.detail);@@ -527,7 +529,10 @@ export class LocalServerService {deployedVersion: checkoutRoot !== null ? CHECKOUT_IMAGE_TAG : managedSwitchVersion(),drift: null,upgrade: null,- deployedTelemetry: { known: true, enabled: result.telemetryEnabled },+ // Read back off the server, not assumed from what was written.+ deployedTelemetry: ranNative+ ? await readNativeTelemetry(layout)+ : await readDeployedTelemetry(host),notice:moveFailure !== null? `Could not move this server off Docker, so it is still running on Docker: ` +diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/core-settings.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/core-settings.tsindex 223f9d0b9..e995fcd2f 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/core-settings.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/core-settings.ts@@ -1,6 +1,10 @@import type { TelemetryFlintEnv } from '../../telemetry/config';import { LOCAL_SERVER_ADMIN_EMAIL, LOCAL_SERVER_BIND_ADDR } from '../constants';-import { MANAGED_SERVER_FEATURE_FLAGS, TELEMETRY_ENABLED_KEY } from '../env-file';+import {+ MANAGED_SERVER_FEATURE_FLAGS,+ TELEMETRY_ENABLED_KEY,+ TELEMETRY_ENDPOINT_KEY,+} from '../env-file';import type { LocalServerSecrets } from '../secret-values';import { DATABASE, OWNER_ROLE, RUNTIME_ROLE } from './postgres-cluster';@@ -15,6 +19,8 @@ export type NativeCoreSettingsParams = {telemetryEnabled: boolean;telemetryEnvironment: TelemetryFlintEnv;telemetryInternal: boolean;+ /** The relay the server sends its usage to: the one the app reports to. */+ telemetryEndpoint: string;};export function nativeApiUrl(apiPort: number): string {@@ -60,6 +66,8 @@ export function buildNativeCoreSettings(params: NativeCoreSettingsParams): Recor[TELEMETRY_ENABLED_KEY]: telemetryEnabled ? 'true' : 'false',TELEMETRY_ENVIRONMENT: params.telemetryEnvironment,TELEMETRY_INTERNAL: params.telemetryInternal ? 'true' : 'false',+ // Named for the same reason the Docker stack's `.env` names it.+ [TELEMETRY_ENDPOINT_KEY]: params.telemetryEndpoint,// Tells the server's agents that Switch Console runs it, as the Docker// stack's `.env` does.MANAGED_BY_CONSOLE: 'true',diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/migrate-from-docker.integration.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/migrate-from-docker.integration.test.tsindex 7d0753cad..a7336091e 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/migrate-from-docker.integration.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/migrate-from-docker.integration.test.ts@@ -81,6 +81,7 @@ describe.skipIf(!process.env.SWITCH_MIGRATION_INTEGRATION)('moving off Docker',telemetryEnabled: false,telemetryEnvironment: 'local',telemetryInternal: false,+ telemetryEndpoint: 'https://relay.example.test',}));diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-stack.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-stack.tsindex b8fa5a8b1..fde6efc09 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-stack.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-stack.ts@@ -16,11 +16,16 @@ import {} from '@shared/core/managed-switch-server/managed-switch-server';import { MAX_START_FAILURE_LINE } from '@shared/core/managed-switch-server/start-failure';import type { ManagedServerRef } from '@shared/core/switch-servers/switch-servers';-import { currentFlintEnv } from '../../telemetry/config';+import {+ currentFlintEnv,+ resolveTelemetryConfig,+ sameMachineServerTelemetryEndpointFor,+ TELEMETRY_RELAY_ORIGIN,+} from '../../telemetry/config';import { currentInternalFlag } from '../../telemetry/internal-account';import { classifyVersionDrift, type DeployedVersion } from '../deployed-version';import { detectDocker } from '../docker';-import { envFlag, readEnvValue, TELEMETRY_ENABLED_KEY } from '../env-file';+import { readEnvValue } from '../env-file';import { errorText } from '../error-text';import type { LocalServerPorts } from '../free-port';import type { ServerHost } from '../host/types';@@ -34,7 +39,7 @@ import { registerManagedServer } from '../pipeline';import { clearPorts, resolvePorts } from '../ports';import { clearSecrets, loadOrCreateSecrets } from '../secrets';import { managedSwitchVersion } from '../server-version';-import { telemetryConsent } from '../telemetry-consent';+import { deployedTelemetryOf, telemetryConsent } from '../telemetry-consent';import { materializeBundle, pruneBundleCopies, readLocalServerBundle } from './bundle';import {CORE_ENV_FILE,@@ -412,6 +417,7 @@ export async function startNativeStack(opts: NativeStartOptions): Promise<NativetelemetryEnabled,telemetryEnvironment: currentFlintEnv(),telemetryInternal: (await currentInternalFlag()) === 'true',+ telemetryEndpoint: nativeServerTelemetryEndpoint(),});await writeDeployedVersion(layout, install.version);onMessage('Starting switch-core…');@@ -577,10 +583,23 @@ export async function nativeStartedAt(layout: NativeLayout): Promise<string | nu/** What the running switch-core does about usage data, read from the* environment it was started with — which is what it read at boot. */+/** The relay, or a dev run's local listener unless no server could post to it. */+export function nativeServerTelemetryEndpoint(): string {+ const endpoint = sameMachineServerTelemetryEndpointFor(resolveTelemetryConfig());+ if (endpoint !== null) return endpoint;+ log.warn(+ 'SWITCHDASH_TELEMETRY_ENDPOINT is not an http(s) URL; the local server reports to the relay',+ {+ event: 'telemetry_endpoint_override_ignored',+ }+ );+ return TELEMETRY_RELAY_ORIGIN;+}+export async function readNativeTelemetry(layout: NativeLayout): Promise<DeployedTelemetry> {try {const env = await readFile(path.join(layout.run, CORE_ENV_FILE), 'utf8');- return { known: true, enabled: envFlag(readEnvValue(env, TELEMETRY_ENABLED_KEY)) };+ return deployedTelemetryOf((key) => readEnvValue(env, key), nativeServerTelemetryEndpoint());} catch (error) {return { known: false, reason: `${CORE_ENV_FILE}: ${errorText(error)}` };}diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-toolchain.integration.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-toolchain.integration.test.tsindex 03b25855d..e066d2789 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-toolchain.integration.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-toolchain.integration.test.ts@@ -87,6 +87,7 @@ describe.skipIf(!process.env.SWITCH_NATIVE_INTEGRATION)('native toolchain', () =telemetryEnabled: false,telemetryEnvironment: 'local',telemetryInternal: false,+ telemetryEndpoint: 'https://relay.example.test',}));expect(await waitForHealth(nativeApiUrl(ports.api), { timeoutMs: 120_000 })).toBe(true);@@ -113,6 +114,7 @@ describe.skipIf(!process.env.SWITCH_NATIVE_INTEGRATION)('native toolchain', () =telemetryEnabled: false,telemetryEnvironment: 'local',telemetryInternal: false,+ telemetryEndpoint: 'https://relay.example.test',}));expect(await waitForHealth(nativeApiUrl(ports.api), { timeoutMs: 120_000 })).toBe(true);diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native.test.tsindex 9c077d1ed..3b15a98ea 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native.test.ts@@ -30,6 +30,7 @@ describe('buildNativeCoreSettings', () => {telemetryEnabled: false,telemetryEnvironment: 'local',telemetryInternal: false,+ telemetryEndpoint: 'https://relay.example.test',});it('holds every listener to loopback', () => {@@ -62,6 +63,10 @@ describe('buildNativeCoreSettings', () => {it('says "no" to telemetry explicitly rather than by omission', () => {expect(settings.TELEMETRY_ENABLED).toBe('false');});++ it('names the relay the server reports to, as the Docker stack does', () => {+ expect(settings.TELEMETRY_ENDPOINT).toBe('https://relay.example.test');+ });});describe('mattermost', () => {diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.tsindex 7458cd955..e251f9fa9 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts@@ -14,7 +14,7 @@ import {type StartLocalServerResult,} from '@shared/core/managed-switch-server/managed-switch-server';import type { ManagedServerRef } from '@shared/core/switch-servers/switch-servers';-import { currentFlintEnv } from '../telemetry/config';+import { currentFlintEnv, TELEMETRY_RELAY_ORIGIN } from '../telemetry/config';import { currentInternalFlag } from '../telemetry/internal-account';import { bundledComposeYaml } from './bundled-compose';import { checkoutBuildOverrideYaml } from './checkout-build';@@ -604,6 +604,8 @@ export async function startStack(opts: StartStackOptions): Promise<StartLocalSertelemetryEnabled,telemetryEnvironment: currentFlintEnv(),telemetryInternal: (await currentInternalFlag()) === 'true',+ // A container or another host cannot reach a dev listener on this machine.+ telemetryEndpoint: TELEMETRY_RELAY_ORIGIN,});await host.writeFile(ENV_FILE_NAME, env, 0o600);diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/remote-server-service.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/remote-server-service.tsindex 509b7edfe..50a3cb80f 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/remote-server-service.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/remote-server-service.ts@@ -847,7 +847,8 @@ export class RemoteServerService {deployedVersion: managedSwitchVersion(),drift: null,upgrade: null,- deployedTelemetry: { known: true, enabled: result.telemetryEnabled },+ // Read back off the server, not assumed from what was written.+ deployedTelemetry: await readDeployedTelemetry(host),notice: result.warning,});this.releaseRefused(sshHost, result.serverId);diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/stack-state.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/stack-state.test.tsindex c2e76bb91..068f5c2e7 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/stack-state.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/stack-state.test.ts@@ -54,6 +54,7 @@ function envFor(overrides: Partial<LocalServerSecrets> = {}, version = '0.27.0')telemetryEnabled: false,telemetryEnvironment: 'prod',telemetryInternal: false,+ telemetryEndpoint: 'https://relay.example.test',});}diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/telemetry-consent.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/telemetry-consent.test.tsindex 91de2e5d9..6d76cc5aa 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/telemetry-consent.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/telemetry-consent.test.ts@@ -19,6 +19,7 @@ vi.mock('@main/core/settings/settings-service', () => ({}));const { readDeployedTelemetry } = await import('./telemetry-consent');+const { TELEMETRY_RELAY_ORIGIN } = await import('../telemetry/config');function host(readFile: () => Promise<string | null>): ServerHost {return { label: 'this computer', readFile } as unknown as ServerHost;@@ -34,7 +35,11 @@ describe('readDeployedTelemetry', () => {it('reads the running container, which is what is actually sending', async () => {runningServiceEnvMock.mockResolvedValue(new Map([['TELEMETRY_ENABLED', 'true']]));- expect(await readDeployedTelemetry(host(noEnvFile))).toEqual({ known: true, enabled: true });+ expect(await readDeployedTelemetry(host(noEnvFile))).toEqual({+ known: true,+ enabled: true,+ endpointCurrent: false,+ });});it('prefers the container over a .env that disagrees with it', async () => {@@ -47,7 +52,7 @@ describe('readDeployedTelemetry', () => {host(() => Promise.resolve('TELEMETRY_ENABLED=false\n')));- expect(result).toEqual({ known: true, enabled: true });+ expect(result).toEqual({ known: true, enabled: true, endpointCurrent: false });});it('treats a container without the variable as not sharing', async () => {@@ -55,13 +60,21 @@ describe('readDeployedTelemetry', () => {// is genuinely not reporting — this is a reading, not a fallback.runningServiceEnvMock.mockResolvedValue(new Map([['SWITCH_VERSION', '0.11.0']]));- expect(await readDeployedTelemetry(host(noEnvFile))).toEqual({ known: true, enabled: false });+ expect(await readDeployedTelemetry(host(noEnvFile))).toEqual({+ known: true,+ enabled: false,+ endpointCurrent: false,+ });});it.each(['1', 'TRUE', ' yes ', 'on'])('reads %s as sharing', async (value) => {runningServiceEnvMock.mockResolvedValue(new Map([['TELEMETRY_ENABLED', value]]));- expect(await readDeployedTelemetry(host(noEnvFile))).toEqual({ known: true, enabled: true });+ expect(await readDeployedTelemetry(host(noEnvFile))).toEqual({+ known: true,+ enabled: true,+ endpointCurrent: false,+ });});it('falls back to the .env when no container is up', async () => {@@ -71,7 +84,36 @@ describe('readDeployedTelemetry', () => {host(() => Promise.resolve('SWITCH_VERSION=0.11.0\nTELEMETRY_ENABLED=true\n')));- expect(result).toEqual({ known: true, enabled: true });+ expect(result).toEqual({ known: true, enabled: true, endpointCurrent: false });+ });++ it('reads whether a Docker or SSH server sends to the relay', async () => {+ runningServiceEnvMock.mockResolvedValue(+ new Map([+ ['TELEMETRY_ENABLED', 'true'],+ ['TELEMETRY_ENDPOINT', TELEMETRY_RELAY_ORIGIN],+ ])+ );++ expect(await readDeployedTelemetry(host(noEnvFile))).toEqual({+ known: true,+ enabled: true,+ endpointCurrent: true,+ });+ });++ it('counts a server sending elsewhere, or on its own default, as not current', async () => {+ for (const endpoint of ['https://relay.retired.example', null]) {+ runningServiceEnvMock.mockResolvedValue(+ new Map([+ ['TELEMETRY_ENABLED', 'true'],+ ...(endpoint === null ? [] : [['TELEMETRY_ENDPOINT', endpoint] as [string, string]]),+ ])+ );+ expect(await readDeployedTelemetry(host(noEnvFile))).toMatchObject({+ endpointCurrent: false,+ });+ }});it('reports a host it could not read, naming both failures', async () => {diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/telemetry-consent.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/telemetry-consent.tsindex 117b9bcf5..565a73373 100644--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/telemetry-consent.ts+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/telemetry-consent.ts@@ -1,8 +1,9 @@import { appSettingsService } from '@main/core/settings/settings-service';import type { DeployedTelemetry } from '@shared/core/managed-switch-server/managed-switch-server';+import { TELEMETRY_RELAY_ORIGIN } from '../telemetry/config';import { runningServiceEnv } from './compose';import { CORE_SERVICE, ENV_FILE_NAME } from './constants';-import { envFlag, readEnvValue, TELEMETRY_ENABLED_KEY } from './env-file';+import { envFlag, readEnvValue, TELEMETRY_ENABLED_KEY, TELEMETRY_ENDPOINT_KEY } from './env-file';import { errorText } from './error-text';import type { ServerHost } from './host/types';@@ -23,6 +24,18 @@ export async function telemetryConsent(): Promise<boolean> {return (await appSettingsService.get('telemetry')).enabled;}+/** Whether a stack's settings share, and send to `expected`; naming no address is not current. */+export function deployedTelemetryOf(+ read: (key: string) => string | null | undefined,+ expected: string+): DeployedTelemetry {+ return {+ known: true,+ enabled: envFlag(read(TELEMETRY_ENABLED_KEY)),+ endpointCurrent: read(TELEMETRY_ENDPOINT_KEY) === expected,+ };+}+/*** Whether the stack on `host` is actually sharing usage data.*@@ -39,7 +52,7 @@ export async function readDeployedTelemetry(host: ServerHost): Promise<DeployedTtry {const env = await runningServiceEnv(host, CORE_SERVICE);- if (env) return { known: true, enabled: envFlag(env.get(TELEMETRY_ENABLED_KEY)) };+ if (env) return deployedTelemetryOf((key) => env.get(key), TELEMETRY_RELAY_ORIGIN);failures.push(`the ${CORE_SERVICE} service has no running container`);} catch (error) {failures.push(`container environment: ${errorText(error)}`);@@ -48,7 +61,7 @@ export async function readDeployedTelemetry(host: ServerHost): Promise<DeployedTtry {const env = await host.readFile(ENV_FILE_NAME);if (env !== null) {- return { known: true, enabled: envFlag(readEnvValue(env, TELEMETRY_ENABLED_KEY)) };+ return deployedTelemetryOf((key) => readEnvValue(env, key), TELEMETRY_RELAY_ORIGIN);}failures.push(`${ENV_FILE_NAME}: not on the host`);} catch (error) {diff --git a/console/apps/switch-console-desktop/src/main/core/settings/controller.ts b/console/apps/switch-console-desktop/src/main/core/settings/controller.tsindex 582ffe8f0..35212d51c 100644--- a/console/apps/switch-console-desktop/src/main/core/settings/controller.ts+++ b/console/apps/switch-console-desktop/src/main/core/settings/controller.ts@@ -64,9 +64,8 @@ export const appSettingsController = createRPCController({* control sits in the same rows the editors do, so the two have to be* comparable.*- * Resetting `telemetry` is not an agreement to share usage data and is not- * reported as one. Its default is off and never-asked, so a reset can only- * ever land on the answer that has nothing to report.+ * The usage-data setting has no Reset control, and a reset is never reported+ * as consent.*/reset: async <T extends AppSettingsKey>(key: T): Promise<void> => {await appSettingsService.reset(key);diff --git a/console/apps/switch-console-desktop/src/main/core/telemetry/config.test.ts b/console/apps/switch-console-desktop/src/main/core/telemetry/config.test.tsindex 4ce817536..a266849ce 100644--- a/console/apps/switch-console-desktop/src/main/core/telemetry/config.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/telemetry/config.test.ts@@ -2,7 +2,9 @@ import { describe, expect, it } from 'vitest';import {flintEnvFor,resolveTelemetryEnvironment,+ sameMachineServerTelemetryEndpointFor,TELEMETRY_RELAY_ENDPOINT,+ TELEMETRY_RELAY_ORIGIN,type TelemetryBuildChannel,type TelemetryEnvironment,} from './config';@@ -98,3 +100,37 @@ describe('the Amplitude project a build reports to', () => {expect(resolution.enabled && resolution.config.flintEnv).toBe('dev');});});++describe('where a server on this machine sends its usage', () => {+ it('is the relay the app reports to, as the base the server appends its path to', () => {+ expect(sameMachineServerTelemetryEndpointFor(resolve('stable'))).toBe(TELEMETRY_RELAY_ORIGIN);+ expect(TELEMETRY_RELAY_ENDPOINT).toBe(`${TELEMETRY_RELAY_ORIGIN}/v1/logs`);+ });++ it('follows a dev run pointed at a local listener, path and all dropped', () => {+ const resolution = resolve('dev', {+ SWITCHDASH_TELEMETRY_DEV: '1',+ SWITCHDASH_TELEMETRY_ENDPOINT: 'http://127.0.0.1:9009/v1/logs',+ });+ expect(sameMachineServerTelemetryEndpointFor(resolution)).toBe('http://127.0.0.1:9009');+ });++ it('gives up a listener no server could post to, for the caller to replace', () => {+ for (const listener of [+ '127.0.0.1:9009/v1/logs',+ 'localhost:9009',+ 'not a url',+ 'ftp://x.test',+ ]) {+ const resolution = resolve('dev', {+ SWITCHDASH_TELEMETRY_DEV: '1',+ SWITCHDASH_TELEMETRY_ENDPOINT: listener,+ });+ expect(sameMachineServerTelemetryEndpointFor(resolution)).toBeNull();+ }+ });++ it('still names the relay for a build that sends nothing itself', () => {+ expect(sameMachineServerTelemetryEndpointFor(resolve('dev'))).toBe(TELEMETRY_RELAY_ORIGIN);+ });+});diff --git a/console/apps/switch-console-desktop/src/main/core/telemetry/config.ts b/console/apps/switch-console-desktop/src/main/core/telemetry/config.tsindex aa5aea42b..02e930c30 100644--- a/console/apps/switch-console-desktop/src/main/core/telemetry/config.ts+++ b/console/apps/switch-console-desktop/src/main/core/telemetry/config.ts@@ -18,9 +18,10 @@ const viteEnv = (import.meta as ImportMetaWithEnv).env;** The hostname is written down in an open-source repository on purpose: anyone* reading this learns where the app reports to, which users are entitled to- * know. switch-core's usage telemetry defaults to the same relay.+ * know.*/-export const TELEMETRY_RELAY_ENDPOINT = 'https://telemetry.switchagents.ai/v1/logs';+export const TELEMETRY_RELAY_ORIGIN = 'https://telemetry.switchagents.ai';+export const TELEMETRY_RELAY_ENDPOINT = `${TELEMETRY_RELAY_ORIGIN}/v1/logs`;/** Which build sent an event, so a developer's app cannot pollute real numbers. */export type TelemetryBuildChannel = 'dev' | 'canary' | 'stable';@@ -130,3 +131,21 @@ export function resolveTelemetryConfig(): TelemetryResolution {env: process.env,});}++/**+ * Where a server running as a process on this machine sends its usage: the+ * origin of wherever this app sends its own (switch-core appends the path), or+ * null when that is not an http(s) URL. Docker and SSH servers always get+ * {@link TELEMETRY_RELAY_ORIGIN}: their loopback is not this machine's.+ */+export function sameMachineServerTelemetryEndpointFor(+ resolution: TelemetryResolution+): string | null {+ if (!resolution.enabled) return TELEMETRY_RELAY_ORIGIN;+ try {+ const url = new URL(resolution.config.endpoint);+ return url.protocol === 'http:' || url.protocol === 'https:' ? url.origin : null;+ } catch {+ return null;+ }+}diff --git a/console/apps/switch-console-desktop/src/main/core/telemetry/events.ts b/console/apps/switch-console-desktop/src/main/core/telemetry/events.tsindex 16a5db59b..5b83fa194 100644--- a/console/apps/switch-console-desktop/src/main/core/telemetry/events.ts+++ b/console/apps/switch-console-desktop/src/main/core/telemetry/events.ts@@ -69,6 +69,10 @@ export type TelemetryAgentCreateFailure =*/| 'not_configured'| 'agent_not_on_server'+ /** A managed agent Switch would not place on the machine. */+ | 'refused'+ /** A managed agent whose own fields Switch rejected: its name, description or settings. */+ | 'invalid'| 'error';export type TelemetrySessionStartFailure =@@ -195,6 +199,9 @@ export type TelemetryRoomCreateFailure =| 'unreachable'| 'error';+/** How a room came to be made: the Create room dialog, or a template run. */+export type TelemetryRoomCreateSource = 'dialog' | 'template';+/** How someone signed in. Not a setting — which of the two forms they used. */export type TelemetryAuthMethod = 'password' | 'oidc' | 'signup';@@ -469,6 +476,7 @@ export type TelemetryEventMap = {/*** A room was created on a server. `bridge_unavailable` is the failure worth* watching: it means the messaging platform, not the app, refused.+ * A template run reports each room it made, and nothing when it fails.*/room_created: {server_kind: TelemetryServerKind;@@ -477,6 +485,7 @@ export type TelemetryEventMap = {has_instructions: boolean;outcome: TelemetryOutcome;failure_reason: TelemetryRoomCreateFailure;+ source: TelemetryRoomCreateSource;};room_deleted: {server_kind: TelemetryServerKind;@@ -652,6 +661,7 @@ export const TELEMETRY_EVENT_PROPERTIES = {'has_instructions','outcome','failure_reason',+ 'source',],room_deleted: ['server_kind', 'outcome'],room_agents_added: ['agent_count', 'direction'],diff --git a/console/apps/switch-console-desktop/src/main/core/telemetry/telemetry-service.ts b/console/apps/switch-console-desktop/src/main/core/telemetry/telemetry-service.tsindex bc2b25cb8..53c36540c 100644--- a/console/apps/switch-console-desktop/src/main/core/telemetry/telemetry-service.ts+++ b/console/apps/switch-console-desktop/src/main/core/telemetry/telemetry-service.ts@@ -39,6 +39,17 @@ class TelemetryService {async track<K extends TelemetryEventName>(name: K,properties: TelemetryEventMap[K]+ ): Promise<void> {+ // Before the gate, which reads the database: the event happened now, not+ // whenever the reads that describe it finish.+ return this.trackAt(name, properties, Date.now());+ }++ /** {@link track}, for something that happened at `timeMs`. */+ async trackAt<K extends TelemetryEventName>(+ name: K,+ properties: TelemetryEventMap[K],+ timeMs: number): Promise<void> {const resolution = resolveTelemetryConfig();if (!resolution.enabled) {@@ -46,10 +57,6 @@ class TelemetryService {return;}- // Before the gate, which reads the database: the event happened now, not- // whenever the reads that describe it finish.- const timeMs = Date.now();-if (!(await isTelemetryAllowed())) return;const payload = buildOtlpPayload(name, properties, {@@ -72,9 +79,11 @@ class TelemetryService {* The same two refusals `track` applies, asked in advance. Only the* once-per-install events need this: they spend a record that is never given* back, so spending it on a send that consent then discards would retire the- * event for the life of the install. Nothing else should call it — everywhere- * else, asking first and sending second is two reads of a gate that can change- * in between, and `track` already reads it at the only moment that matters.+ * event for the life of the install. A report that must ask a server for what+ * it describes uses it too, to skip asking when nothing could be sent. Nothing+ * else should call it — everywhere else, asking first and sending second is+ * two reads of a gate that can change in between, and `track` already reads+ * it at the only moment that matters.*/async canSend(): Promise<boolean> {return resolveTelemetryConfig().enabled && (await isTelemetryAllowed());@@ -122,7 +131,16 @@ export function trackEvent<K extends TelemetryEventName>(name: K,properties: TelemetryEventMap[K]): void {- void telemetryService.track(name, properties).catch((error: unknown) => {+ trackEventAt(name, properties, Date.now());+}++/** {@link trackEvent} for something that happened at `timeMs`, reported later. */+export function trackEventAt<K extends TelemetryEventName>(+ name: K,+ properties: TelemetryEventMap[K],+ timeMs: number+): void {+ void telemetryService.trackAt(name, properties, timeMs).catch((error: unknown) => {log.warn('telemetry: event not sent', {event: 'telemetry_send_failed',telemetryEvent: name,diff --git a/console/apps/switch-console-desktop/src/main/core/workspaces/controller.test.ts b/console/apps/switch-console-desktop/src/main/core/workspaces/controller.test.tsindex beff67c38..e07f0d349 100644--- a/console/apps/switch-console-desktop/src/main/core/workspaces/controller.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/workspaces/controller.test.ts@@ -13,6 +13,18 @@ const addBridgeTeam = vi.hoisted(() => vi.fn());const removeBridgeTeam = vi.hoisted(() => vi.fn());const updateBridgeOnServer = vi.hoisted(() => vi.fn());const saveTeamsPackage = vi.hoisted(() => vi.fn());+const createRoomFromTemplate = vi.hoisted(() => vi.fn());+const runTemplateWithAgents = vi.hoisted(() => vi.fn());+const runTemplateDocument = vi.hoisted(() => vi.fn());+const fetchRoomDetail = vi.hoisted(() => vi.fn());+const fetchManagedAgent = vi.hoisted(() => vi.fn());+const managedAgentLocator = vi.hoisted(() =>+ vi.fn(async () => (controllerId: string) => (controllerId === 'c1' ? 'local' : 'remote'))+);+const trackEventAt = vi.hoisted(() =>+ vi.fn((name: string, properties: unknown, _at: number) => trackEvent(name, properties))+);+const canSend = vi.hoisted(() => vi.fn(async () => true));const getServer = vi.hoisted(() => vi.fn());const getSessionCookie = vi.hoisted(() => vi.fn());const requireWorkspace = vi.hoisted(() => vi.fn());@@ -50,7 +62,14 @@ vi.mock('@main/core/agents/write-switch-settings', () => ({vi.mock('@main/core/fs/impl/ssh-fs', () => ({ SshFileSystem: vi.fn() }));vi.mock('@main/core/locations/location-transport', () => ({ sshConnectionIdForHost: vi.fn() }));vi.mock('@main/core/ssh/connect/connect-agent-ssh', () => ({ ensureSshConnected: vi.fn() }));-vi.mock('@main/core/telemetry/telemetry-service', () => ({ trackEvent }));+vi.mock('@main/core/telemetry/telemetry-service', () => ({+ trackEvent,+ trackEventAt,+ telemetryService: { canSend },+}));+// The provider registry is built at app startup, which a test does not run.+vi.mock('@main/core/telemetry/agent-type', () => ({ agentTypeOf: (id: string) => id }));+vi.mock('@main/core/agent-migration/managed-agent-location', () => ({ managedAgentLocator }));vi.mock('@main/core/managed-switch-server/managed-server-status', () => ({isManagedServerRunning: vi.fn(() => true),managedServerHostBlocked,@@ -83,7 +102,7 @@ vi.mock('@main/core/switch-servers/gateway-client', () => ({connectServiceWithKey,disconnectGitHub,disconnectService,- createRoomFromTemplate: vi.fn(),+ createRoomFromTemplate,decodeJwtTenantId: vi.fn(() => null),deleteRoom,fetchAddressingPolicy: vi.fn(),@@ -98,7 +117,8 @@ vi.mock('@main/core/switch-servers/gateway-client', () => ({fetchWorkspaceServices,fetchMyIdentities: vi.fn(),fetchRoomAgentIds: vi.fn(),- fetchRoomDetail: vi.fn(),+ fetchManagedAgent,+ fetchRoomDetail,fetchRoomGroups: vi.fn(),fetchRoomRoles: vi.fn(),fetchRooms: vi.fn(),@@ -108,6 +128,8 @@ vi.mock('@main/core/switch-servers/gateway-client', () => ({releaseBridgeIdentity: vi.fn(),removeBridgeTeam,removeRoomAgent: vi.fn(),+ runTemplateDocument,+ runTemplateWithAgents,setWorkspaceService,switchTenant,updateAddressingPolicy: vi.fn(),@@ -325,6 +347,245 @@ describe('distributed Microsoft Teams team placement', () => {});});+describe('reporting what a template made', () => {+ const createdAgent = {+ slot: 'pm',+ agentId: 'a1',+ name: 'pm',+ machine: { id: 'c1', name: 'laptop' },+ listening: true,+ };++ beforeEach(() => {+ vi.clearAllMocks();+ canSend.mockResolvedValue(true);+ managedServerHostBlocked.mockReturnValue(null);+ getServer.mockResolvedValue(server({}));+ requireWorkspace.mockResolvedValue(workspace());+ fetchRoomDetail.mockImplementation(async (_server: unknown, roomId: string) => ({+ id: roomId,+ agentCount: roomId === 'r1' ? 2 : 0,+ bridgeType: roomId === 'r1' ? 'slack' : null,+ instructions: roomId === 'r1' ? 'Triage incoming bugs.' : null,+ }));+ });++ it('reports each room a template made, as the server holds it', async () => {+ createRoomFromTemplate.mockResolvedValue({+ kind: 'group',+ groupId: 'g1',+ groupName: 'G',+ rooms: [+ { roomId: 'r1', roomName: 'A', failedAttachments: [] },+ { roomId: 'r2', roomName: 'B', failedAttachments: [] },+ ],+ errors: [],+ });++ await workspacesController.createRoomFromTemplate('ws', 'version: 1', {}, 'Triage');++ await vi.waitFor(() => expect(trackEvent).toHaveBeenCalledTimes(2));+ expect(trackEvent).toHaveBeenCalledWith('room_created', {+ server_kind: 'remote_managed',+ bridge_platform: 'slack',+ agent_count: 2,+ has_instructions: true,+ outcome: 'success',+ failure_reason: 'none',+ source: 'template',+ });+ expect(trackEvent).toHaveBeenCalledWith('room_created', {+ server_kind: 'remote_managed',+ bridge_platform: 'unknown',+ agent_count: 0,+ has_instructions: false,+ outcome: 'success',+ failure_reason: 'none',+ source: 'template',+ });+ });++ it('reports the agents a run made from what the run declared, reading none back', async () => {+ runTemplateWithAgents.mockResolvedValue({+ kind: 'created',+ createdAgents: [createdAgent],+ room: null,+ });++ await workspacesController.runTemplateWithAgents('ws', {+ agents: [{ slot: 'pm', provider: 'codex' } as never],+ yamlText: null,+ inputs: {},+ templateName: 'T',+ entryPoint: 'template',+ });++ await vi.waitFor(() =>+ expect(trackEvent).toHaveBeenCalledWith('agent_created', {+ agent_type: 'codex',+ location: 'local',+ outcome: 'success',+ failure_reason: 'none',+ entry_point: 'template',+ })+ );+ expect(managedAgentLocator).toHaveBeenCalledWith('srv');+ expect(fetchManagedAgent).not.toHaveBeenCalled();+ });++ it('reads back the provider of an agent a document run chose', async () => {+ runTemplateDocument.mockResolvedValue({+ kind: 'created',+ createdAgents: [createdAgent],+ room: { kind: 'room', roomId: 'r1', roomName: 'A', failedAttachments: [] },+ });+ fetchManagedAgent.mockResolvedValue({ agentId: 'a1', provider: 'claude', controllerId: 'c1' });++ await workspacesController.runTemplateDocument('ws', {+ template: 'version: 2',+ inputs: {},+ filled: {},+ templateName: 'Buddy',+ entryPoint: 'onboarding',+ });++ await vi.waitFor(() => expect(trackEvent).toHaveBeenCalledTimes(2));+ expect(trackEvent).toHaveBeenCalledWith('agent_created', {+ agent_type: 'claude',+ location: 'local',+ outcome: 'success',+ failure_reason: 'none',+ entry_point: 'onboarding',+ });+ expect(trackEvent).toHaveBeenCalledWith(+ 'room_created',+ expect.objectContaining({ source: 'template', agent_count: 2 })+ );+ });++ it('stamps what a run made with when the run finished, not when the reads came back', async () => {+ const now = vi.spyOn(Date, 'now').mockReturnValue(1_000);+ fetchRoomDetail.mockImplementation(async (_server: unknown, roomId: string) => {+ now.mockReturnValue(9_000);+ return { id: roomId, agentCount: 1, bridgeType: null, instructions: null };+ });+ createRoomFromTemplate.mockResolvedValue({+ kind: 'room',+ roomId: 'r1',+ roomName: 'A',+ failedAttachments: [],+ });++ await workspacesController.createRoomFromTemplate('ws', 'version: 1', {}, 'T');++ await vi.waitFor(() =>+ expect(trackEventAt).toHaveBeenCalledWith('room_created', expect.anything(), 1_000)+ );+ now.mockRestore();+ });++ it('does not switch back to the run’s workspace to describe it', async () => {+ requireWorkspace.mockResolvedValue(workspace('tenant-a'));+ // As when the person has already moved on to another workspace.+ getSessionCookie.mockResolvedValue('cookie-for-another-workspace');+ createRoomFromTemplate.mockResolvedValue({+ kind: 'room',+ roomId: 'r1',+ roomName: 'A',+ failedAttachments: [],+ });++ await workspacesController.createRoomFromTemplate('ws', 'version: 1', {}, 'T');++ await vi.waitFor(() => expect(canSend).toHaveBeenCalled());+ await new Promise((resolve) => setTimeout(resolve, 0));+ // Once, for the run itself; never again for the report.+ expect(switchTenant).toHaveBeenCalledTimes(1);+ expect(fetchRoomDetail).not.toHaveBeenCalled();+ expect(trackEvent).not.toHaveBeenCalled();+ });++ it('asks the server nothing when nothing it read could be sent', async () => {+ canSend.mockResolvedValue(false);+ runTemplateDocument.mockResolvedValue({+ kind: 'created',+ createdAgents: [createdAgent],+ room: { kind: 'room', roomId: 'r1', roomName: 'A', failedAttachments: [] },+ });++ await workspacesController.runTemplateDocument('ws', {+ template: 'version: 2',+ inputs: {},+ filled: {},+ templateName: 'T',+ entryPoint: 'template',+ });++ await vi.waitFor(() => expect(canSend).toHaveBeenCalled());+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(fetchRoomDetail).not.toHaveBeenCalled();+ expect(fetchManagedAgent).not.toHaveBeenCalled();+ expect(trackEvent).not.toHaveBeenCalled();+ });++ it('keeps the entry point out of what is sent to the server', async () => {+ runTemplateWithAgents.mockResolvedValue({ kind: 'created', createdAgents: [], room: null });++ await workspacesController.runTemplateWithAgents('ws', {+ agents: [],+ yamlText: null,+ inputs: {},+ templateName: 'T',+ entryPoint: 'template',+ });++ expect(runTemplateWithAgents).toHaveBeenCalledWith(expect.anything(), {+ agents: [],+ yamlText: null,+ inputs: {},+ templateName: 'T',+ });+ });++ it('reports nothing for a run the server refused, which made nothing', async () => {+ runTemplateDocument.mockResolvedValue({+ kind: 'refused',+ code: 'agents_not_created',+ message: 'No.',+ problems: [],+ leftBehind: [],+ });++ await workspacesController.runTemplateDocument('ws', {+ template: 'version: 2',+ inputs: {},+ filled: {},+ templateName: 'T',+ entryPoint: 'template',+ });++ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(canSend).not.toHaveBeenCalled();+ expect(trackEvent).not.toHaveBeenCalled();+ });++ it('does not report a room it could not read back with its details guessed', async () => {+ createRoomFromTemplate.mockResolvedValue({+ kind: 'room',+ roomId: 'r1',+ roomName: 'A',+ failedAttachments: [],+ });+ fetchRoomDetail.mockRejectedValue(new Error('no route to host'));++ await workspacesController.createRoomFromTemplate('ws', 'version: 1', {}, 'T');++ await vi.waitFor(() => expect(fetchRoomDetail).toHaveBeenCalled());+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(trackEvent).not.toHaveBeenCalled();+ });+});+describe('an action a workspace whose host has gone down cannot take', () => {const blocked = {sshHost: 'h',@@ -363,6 +624,7 @@ describe('an action a workspace whose host has gone down cannot take', () => {has_instructions: false,outcome: 'failure',failure_reason: 'unreachable',+ source: 'dialog',}));});diff --git a/console/apps/switch-console-desktop/src/main/core/workspaces/controller.ts b/console/apps/switch-console-desktop/src/main/core/workspaces/controller.tsindex 564357181..496ba3327 100644--- a/console/apps/switch-console-desktop/src/main/core/workspaces/controller.ts+++ b/console/apps/switch-console-desktop/src/main/core/workspaces/controller.ts@@ -1,3 +1,4 @@+import { managedAgentLocator } from '@main/core/agent-migration/managed-agent-location';import { appService } from '@main/core/app/service';import { backfillAgentIcons } from '@main/core/switch-servers/backfill-agent-icons';import { withResolvedHomeUrls } from '@main/core/switch-servers/bridge-home-url';@@ -35,6 +36,7 @@ import {beginMessagingAppInstall,fetchBridgeTypes,fetchConnectedChats,+ fetchManagedAgent,fetchMessagingApps,fetchMyIdentities,fetchRoomAgentIds,@@ -89,6 +91,7 @@ import {} from '@main/core/switch-servers/service-browser-flow';import { saveTeamsPackage } from '@main/core/switch-servers/teams-package';import { updateBridgeOnServer } from '@main/core/switch-servers/update-bridge';+import { agentTypeOf } from '@main/core/telemetry/agent-type';import { bridgePlatformOfType } from '@main/core/telemetry/bridge-platform';import type {TelemetryBridgeFailure,@@ -96,10 +99,12 @@ import type {TelemetryOutcome,TelemetryRoomAgentsDirection,TelemetryRoomCreateFailure,+ TelemetryRoomCreateSource,TelemetryServerKind,} from '@main/core/telemetry/events';-import { roomAgentsDirectionOf } from '@main/core/telemetry/narrow';-import { trackEvent } from '@main/core/telemetry/telemetry-service';+import { entryPointOf, roomAgentsDirectionOf } from '@main/core/telemetry/narrow';+import { telemetryService, trackEvent, trackEventAt } from '@main/core/telemetry/telemetry-service';+import { log } from '@main/lib/logger';import type {SlackOwnAppSetup,SlackTokensChecked,@@ -142,6 +147,7 @@ import type {UpdateBridgeResult,UpdateRoomParams,} from '@shared/core/switch-servers/switch-servers';+import type { UiEntryPoint } from '@shared/core/telemetry/reporting';import type { Workspace } from '@shared/core/workspaces/workspaces';import { createRPCController } from '@shared/lib/ipc/rpc';import {@@ -155,6 +161,7 @@ import {import { listServersWithoutMembership } from './reconcile-workspaces';import {withReachableWorkspaceSession,+ withWorkspaceIfSelected,withWorkspaceSession,workspaceServer,} from './workspace-session';@@ -236,13 +243,14 @@ function bridgePlatformOnServer(* Report once the platform is known, without anyone waiting for it.** The event arrives a moment after the action rather than with it, which costs- * nothing: it carries its own timestamp, taken when the work happened.+ * nothing: it carries its own timestamp, taken here, when the work happened.*/function reportWithBridge(platform: Promise<TelemetryBridgePlatform>,- emit: (platform: TelemetryBridgePlatform) => void+ emit: (platform: TelemetryBridgePlatform, at: number) => void): void {- void platform.then(emit).catch(() => {});+ const at = Date.now();+ void platform.then((known) => emit(known, at)).catch(() => {});}/** The same, for a room create whose other properties are already known. */@@ -254,15 +262,163 @@ function reportRoomCreated(agent_count: number;has_instructions: boolean;failure_reason: TelemetryRoomCreateFailure;+ source: TelemetryRoomCreateSource;}): void {- reportWithBridge(bridgePlatformOf(workspaceId, bridgeId), (bridge_platform) =>- trackEvent('room_created', {- ...rest,- bridge_platform,- outcome: rest.failure_reason === 'none' ? 'success' : 'failure',- })+ reportWithBridge(bridgePlatformOf(workspaceId, bridgeId), (bridge_platform, at) =>+ trackEventAt(+ 'room_created',+ {+ ...rest,+ bridge_platform,+ outcome: rest.failure_reason === 'none' ? 'success' : 'failure',+ },+ at+ )+ );+}++/** The rooms a template run made: one, a group's, or none for a run with no room half. */+function templateRoomIds(room: ProvisionFromTemplateResult | null): string[] {+ if (room === null) return [];+ return room.kind === 'room' ? [room.roomId] : room.rooms.map((r) => r.roomId);+}++type CreatedTemplateAgent = Extract<+ TemplateRunResult,+ { kind: 'created' }+>['createdAgents'][number];++/** What a template made, as the reports below need it. */+type TemplateMade = {+ room: ProvisionFromTemplateResult | null;+ /** Null for a template that makes rooms only. */+ agents: {+ created: CreatedTemplateAgent[];+ /** Each agent's provider by slot, where the caller declared it. */+ declaredProviders: ReadonlyMap<string, string>;+ /** Where the run was started. */+ entryPoint: UiEntryPoint;+ } | null;+};++function logUnreported(what: string, error: unknown): void {+ log.warn(`${what} a template made could not be read back to report it`, {+ event: 'telemetry_template_made',+ errorCode: error instanceof GatewayError ? error.kind : 'error',+ error: String(error),+ });+}++/**+ * Report each room and agent a template made. What the run's result does not+ * say is read back, only when an event could be sent and only while the run's+ * workspace is still selected; what cannot be read is logged, not guessed.+ */+async function reportTemplateMade(+ workspaceId: string,+ server: SwitchServer,+ made: TemplateMade,+ at: number+): Promise<void> {+ const roomIds = templateRoomIds(made.room);+ const agents = made.agents;+ if (roomIds.length === 0 && (agents === null || agents.created.length === 0)) return;+ if (!(await telemetryService.canSend())) return;++ const read = await withWorkspaceIfSelected(workspaceId, (selected) =>+ Promise.all([+ Promise.allSettled(roomIds.map((roomId) => fetchRoomDetail(selected, roomId))),+ Promise.allSettled(+ (agents?.created ?? []).map(async (agent) => {+ const declared = agents?.declaredProviders.get(agent.slot);+ if (declared !== undefined) return declared;+ const managed = await fetchManagedAgent(selected, agent.agentId);+ if (!managed) throw new Error('The agent the run made is not a managed agent.');+ return managed.provider;+ })+ ),+ ]));+ if (read === null) {+ log.warn('What a template made was not reported: its workspace is no longer selected', {+ event: 'telemetry_template_made',+ errorCode: 'workspace_not_selected',+ });+ return;+ }+ const [rooms, providers] = read;++ const serverKind = serverKindOf(server);+ for (const room of rooms) {+ if (room.status === 'rejected') {+ logUnreported('A room', room.reason);+ continue;+ }+ trackEventAt(+ 'room_created',+ {+ server_kind: serverKind,+ bridge_platform: bridgePlatformOfType(room.value.bridgeType),+ agent_count: room.value.agentCount,+ has_instructions: (room.value.instructions?.trim().length ?? 0) > 0,+ outcome: 'success',+ failure_reason: 'none',+ source: 'template',+ },+ at+ );+ }++ if (agents === null || agents.created.length === 0) return;+ const locate = await managedAgentLocator(server.id);+ for (const [i, provider] of providers.entries()) {+ if (provider.status === 'rejected') {+ logUnreported('An agent', provider.reason);+ continue;+ }+ trackEventAt(+ 'agent_created',+ {+ agent_type: agentTypeOf(provider.value),+ location: locate(agents.created[i].machine.id),+ outcome: 'success',+ failure_reason: 'none',+ entry_point: entryPointOf(agents.entryPoint),+ },+ at+ );+ }+}++/** Run a template call, then report what it made (null: nothing) without waiting. */+async function runTemplateReported<R>(+ workspaceId: string,+ run: (server: SwitchServer) => Promise<R>,+ made: (result: R) => TemplateMade | null+): Promise<R> {+ const { result, server } = await withWorkspaceSession(workspaceId, async (leased) => ({+ result: await run(leased),+ server: leased,+ }));+ const report = made(result);+ if (report !== null) {+ void reportTemplateMade(workspaceId, server, report, Date.now()).catch((error: unknown) =>+ logUnreported('What', error)+ );+ }+ return result;+}++/** What a run that may create agents made: nothing when it was refused. */+function madeByRun(agents: {+ entryPoint: UiEntryPoint;+ declaredProviders: ReadonlyMap<string, string>;+}): (result: TemplateRunResult) => TemplateMade | null {+ return (result) =>+ result.kind === 'created'+ ? { room: result.room, agents: { created: result.createdAgents, ...agents } }+ : null;}/**@@ -532,6 +688,7 @@ export const workspacesController = createRPCController({agent_count: params.agentIds.length,has_instructions: (params.instructions?.trim().length ?? 0) > 0,failure_reason,+ source: 'dialog',});const unreachable = hostUnreachable(known);@@ -566,8 +723,10 @@ export const workspacesController = createRPCController({inputs: Record<string, string | number | boolean>,templateName: string): Promise<ProvisionFromTemplateResult> =>- withWorkspaceSession(workspaceId, (server) =>- createRoomFromTemplate(server, yamlText, inputs, templateName)+ runTemplateReported(+ workspaceId,+ (server) => createRoomFromTemplate(server, yamlText, inputs, templateName),+ (room) => ({ room, agents: null })),/**@@ -582,9 +741,19 @@ export const workspacesController = createRPCController({yamlText: string | null;inputs: Record<string, string | number | boolean>;templateName: string;+ /** Where the run was started, for the agents it makes. */+ entryPoint: UiEntryPoint;}- ): Promise<TemplateRunResult> =>- withWorkspaceSession(workspaceId, (server) => runTemplateWithAgents(server, body)),+ ): Promise<TemplateRunResult> => {+ const { entryPoint, ...run } = body;+ // The caller named each agent's provider, so none has to be read back.+ const declaredProviders = new Map(run.agents.map((agent) => [agent.slot, agent.provider]));+ return runTemplateReported(+ workspaceId,+ (server) => runTemplateWithAgents(server, run),+ madeByRun({ entryPoint, declaredProviders })+ );+ },/** Run a template from its document: the server works the agents and rooms out of it. */runTemplateDocument: (@@ -594,9 +763,18 @@ export const workspacesController = createRPCController({inputs: Record<string, string | number | boolean>;filled: Record<string, string>;templateName: string;+ /** Where the run was started, for the agents it makes. */+ entryPoint: UiEntryPoint;}- ): Promise<TemplateRunResult> =>- withWorkspaceSession(workspaceId, (server) => runTemplateDocument(server, body)),+ ): Promise<TemplateRunResult> => {+ const { entryPoint, ...run } = body;+ // The server picks providers from the document's conditions, so they are read back.+ return runTemplateReported(+ workspaceId,+ (server) => runTemplateDocument(server, run),+ madeByRun({ entryPoint, declaredProviders: new Map() })+ );+ },/** The runs the user may see, or null when the server does not record runs. */listTemplateRuns: (params: { workspaceId: string }): Promise<TemplateRun[] | null> =>@@ -796,11 +974,15 @@ export const workspacesController = createRPCController({);// Reported after the lease, not inside it: the platform lookup takes a lease// of its own, and the bridge is still there to read afterwards.- reportWithBridge(bridgePlatformOf(params.workspaceId, params.bridgeId), (bridge_platform) =>- trackEvent('bridge_identity_claimed', {- bridge_platform,- outcome: result.kind === 'claimed' ? 'success' : 'failure',- })+ reportWithBridge(bridgePlatformOf(params.workspaceId, params.bridgeId), (bridge_platform, at) =>+ trackEventAt(+ 'bridge_identity_claimed',+ {+ bridge_platform,+ outcome: result.kind === 'claimed' ? 'success' : 'failure',+ },+ at+ ));return result;},diff --git a/console/apps/switch-console-desktop/src/main/core/workspaces/workspace-session.test.ts b/console/apps/switch-console-desktop/src/main/core/workspaces/workspace-session.test.tsindex d53d17ee5..9d89c4c42 100644--- a/console/apps/switch-console-desktop/src/main/core/workspaces/workspace-session.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/workspaces/workspace-session.test.ts@@ -32,6 +32,7 @@ import { assertedTenant } from './asserted-tenant';import {forgetServerSession,withReachableWorkspaceSession,+ withWorkspaceIfSelected,withWorkspaceSession,} from './workspace-session';@@ -310,3 +311,56 @@ describe('withReachableWorkspaceSession', () => {expect(switchTenant).toHaveBeenCalledWith(expect.anything(), 'tenant-a');});});++describe('withWorkspaceIfSelected', () => {+ it('runs against the workspace the session already selects', async () => {+ workspace('ws-a', 'tenant-a');+ selected = 'tenant-a';++ await expect(withWorkspaceIfSelected('ws-a', async () => 'read')).resolves.toBe('read');+ expect(switchTenant).not.toHaveBeenCalled();+ });++ it('never switches back to a workspace the person has moved on from', async () => {+ workspace('ws-a', 'tenant-a');+ workspace('ws-b', 'tenant-b');+ selected = 'tenant-b';+ const ran = vi.fn();++ await expect(withWorkspaceIfSelected('ws-a', ran)).resolves.toBeNull();+ expect(ran).not.toHaveBeenCalled();+ expect(switchTenant).not.toHaveBeenCalled();+ });++ it('holds no lease, so a switch the person starts does not wait for it', async () => {+ workspace('ws-a', 'tenant-a');+ workspace('ws-b', 'tenant-b');+ selected = 'tenant-a';+ const read = deferred<string>();++ const describing = withWorkspaceIfSelected('ws-a', () => read.promise);+ await settle();+ const moving = withWorkspaceSession('ws-b', async () => 'moved');++ await expect(moving).resolves.toBe('moved');+ expect(selected).toBe('tenant-b');+ read.resolve('late');+ await expect(describing).resolves.toBe('late');+ });++ it('runs on a server holding one workspace, which selects nothing', async () => {+ workspace('ws-only', null);++ await expect(withWorkspaceIfSelected('ws-only', async () => 'read')).resolves.toBe('read');+ });++ it('does nothing while the managed host is down', async () => {+ workspace('ws-a', 'tenant-a');+ selected = 'tenant-a';+ hostUnreachable.mockReturnValue(new Error('build-box is not reachable'));+ const ran = vi.fn();++ await expect(withWorkspaceIfSelected('ws-a', ran)).resolves.toBeNull();+ expect(ran).not.toHaveBeenCalled();+ });+});diff --git a/console/apps/switch-console-desktop/src/main/core/workspaces/workspace-session.ts b/console/apps/switch-console-desktop/src/main/core/workspaces/workspace-session.tsindex 58beb8aa8..bcd52993d 100644--- a/console/apps/switch-console-desktop/src/main/core/workspaces/workspace-session.ts+++ b/console/apps/switch-console-desktop/src/main/core/workspaces/workspace-session.ts@@ -188,6 +188,25 @@ export async function withWorkspaceSession<T>(}}+/**+ * Run `fn` only if the session already selects `workspaceId`, without switching+ * or taking a lease; null otherwise. A switch can land mid-call, so use it only+ * for reads by id, which fail rather than answer for another workspace.+ */+export async function withWorkspaceIfSelected<T>(+ workspaceId: string,+ fn: (server: SwitchServer) => Promise<T>+): Promise<T | null> {+ const workspace = await requireAddressableWorkspace(workspaceId);+ const server = await requireServer(workspace.serverId);+ if (hostUnreachable(server)) return null;+ if (workspace.tenantId !== null) {+ const cookie = await getSessionCookie(server.id);+ if (!cookie || decodeJwtTenantId(cookie) !== workspace.tenantId) return null;+ }+ return fn(server);+}+/*** {@link withWorkspaceSession}, refusing to touch the gateway while the host* the server is managed on is unreachable (CHOO-1780).diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/buddy-setup-modal.tsx b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/buddy-setup-modal.tsxindex d48e17f65..9146c0c00 100644--- a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/buddy-setup-modal.tsx+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/buddy-setup-modal.tsx@@ -23,7 +23,12 @@ export function BuddySetupModal({<DialogTitle>Connect an AI provider to switch-buddy</DialogTitle></DialogHeader><DialogContentArea className="pt-0">- <ProviderSetup serverId={serverId} workspaceId={workspaceId} onCreated={onSuccess} />+ <ProviderSetup+ serverId={serverId}+ workspaceId={workspaceId}+ entryPoint="switch_buddy"+ onCreated={onSuccess}+ /></DialogContentArea><DialogFooter><Button variant="outline" onClick={onClose}>diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/create-buddy.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/create-buddy.tsindex bf977f7ca..c865abe27 100644--- a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/create-buddy.ts+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/create-buddy.ts@@ -1,5 +1,6 @@import { workspacesStore } from '@renderer/features/workspaces/workspaces-store';import { rpc } from '@renderer/lib/ipc';+import type { UiEntryPoint } from '@shared/core/telemetry/reporting';import { BUDDY_TEMPLATE_ID, buddyAgentName, isBuddy } from './buddy-identity';import { reportBuddyStep } from './buddy-telemetry';@@ -22,6 +23,8 @@ export async function createBuddy(input: {machineId: string;provider: string;person: { name: string | null; email: string | null };+ /** Where the person set the buddy up: the first run, or the buddy's chat later. */+ entryPoint: UiEntryPoint;}): Promise<{ agentId: string; name: string }> {try {const created = await createBuddyAgent(input);@@ -39,6 +42,7 @@ async function createBuddyAgent(input: {machineId: string;provider: string;person: { name: string | null; email: string | null };+ entryPoint: UiEntryPoint;}): Promise<{ agentId: string; name: string }> {const existing = (await rpc.managedAgents.list(input.serverId)) ?? [];const mine = existing.find(isBuddy);@@ -55,6 +59,7 @@ async function createBuddyAgent(input: {inputs: { agent_name: name, provider: input.provider, machine: input.machineId },filled: {},templateName: template.name,+ entryPoint: input.entryPoint,});if (result.kind === 'created') {const created = result.createdAgents[0];diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/meet-buddy-page.tsx b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/meet-buddy-page.tsxindex 14a258963..c607a35d1 100644--- a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/meet-buddy-page.tsx+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/meet-buddy-page.tsx@@ -136,6 +136,7 @@ export const MeetBuddyPage = observer(function MeetBuddyPage({machineId: computer.machine.id,provider: providerId,person: { name: user?.name ?? null, email: user?.email ?? null },+ entryPoint: 'onboarding',}));} catch (cause) {@@ -170,7 +171,12 @@ export const MeetBuddyPage = observer(function MeetBuddyPage({}>{header}- <ProviderSetup serverId={serverId} workspaceId={workspaceId} onCreated={onCreated} />+ <ProviderSetup+ serverId={serverId}+ workspaceId={workspaceId}+ entryPoint="onboarding"+ onCreated={onCreated}+ /></OnboardingSurface>);diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/provider-setup.tsx b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/provider-setup.tsxindex 2124f2773..3fd1a4786 100644--- a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/provider-setup.tsx+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/provider-setup.tsx@@ -13,6 +13,7 @@ import { cn } from '@renderer/utils/utils';import type { AgentInstallError } from '@shared/core/providers/agent-payload';import { agentProviders, requireProvider } from '@shared/core/providers/agent-provider-registry';import { linksIn } from '@shared/core/providers/provider-login';+import type { UiEntryPoint } from '@shared/core/telemetry/reporting';import { providerLoginExitChannel, providerLoginLineChannel } from '@shared/events/appEvents';import { type BuddyProvider, buddyProviders } from './buddy-providers';import { reportBuddyStep } from './buddy-telemetry';@@ -44,10 +45,13 @@ type Login = {export const ProviderSetup = observer(function ProviderSetup({serverId,workspaceId,+ entryPoint,onCreated,}: {serverId: string;workspaceId: string;+ /** Where this card is shown: the first run, or the buddy's chat later. */+ entryPoint: UiEntryPoint;onCreated: (agent: { agentId: string; name: string }) => void;}) {const { state: computer, recheck } = useThisComputer(serverId, workspaceId);@@ -147,6 +151,7 @@ export const ProviderSetup = observer(function ProviderSetup({machineId: computer.machine.id,provider: provider.id,person: { name: user?.name ?? null, email: user?.email ?? null },+ entryPoint,}));} catch (cause) {diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/TelemetryConsentNotice.tsx b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/TelemetryConsentNotice.tsxindex 696e18bb4..e9d6a2538 100644--- a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/TelemetryConsentNotice.tsx+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/TelemetryConsentNotice.tsx@@ -32,15 +32,20 @@ export function TelemetryConsentNotice({title: "Can't tell whether this server shares usage data",body: `Reading the setting off the server failed (${notice.reason}). Restarting applies your current choice, and makes it certain.`,}- : notice.consent+ : notice.kind === 'stale-endpoint'? {- title: 'This server is not sharing usage data yet',- body: 'Your choice to share covers the servers Switch Console runs for you, but this one has been up since before you made it. Restart to apply it.',+ title: 'This server is not sending usage data to the current address',+ body: 'Its usage data may not be arriving. Restart to point it at the address Switch Console uses now.',}- : {- title: 'This server is still sharing usage data',- body: 'You turned sharing off. This server has been up since before that and keeps the setting it started with until it restarts. Restart to apply your choice.',- };+ : notice.consent+ ? {+ title: 'This server is not sharing usage data yet',+ body: 'Your choice to share covers the servers Switch Console runs for you, but this one has been up since before you made it. Restart to apply it.',+ }+ : {+ title: 'This server is still sharing usage data',+ body: 'You turned sharing off. This server has been up since before that and keeps the setting it started with until it restarts. Restart to apply your choice.',+ };return (<Alert variant="warning">diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/managed-telemetry-notice.test.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/managed-telemetry-notice.test.tsindex 1d9136724..aa783b07c 100644--- a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/managed-telemetry-notice.test.ts+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/managed-telemetry-notice.test.ts@@ -6,7 +6,7 @@ describe('managedTelemetryNotice', () => {expect(managedTelemetryNotice({running: false,- deployed: { known: true, enabled: true },+ deployed: { known: true, enabled: true, endpointCurrent: true },consent: false,sharedWithOthers: false,})@@ -31,7 +31,7 @@ describe('managedTelemetryNotice', () => {expect(managedTelemetryNotice({running: true,- deployed: { known: true, enabled: consent },+ deployed: { known: true, enabled: consent, endpointCurrent: true },consent,sharedWithOthers: false,})@@ -43,7 +43,7 @@ describe('managedTelemetryNotice', () => {expect(managedTelemetryNotice({running: true,- deployed: { known: true, enabled: false },+ deployed: { known: true, enabled: false, endpointCurrent: true },consent: true,sharedWithOthers: false,})@@ -56,7 +56,7 @@ describe('managedTelemetryNotice', () => {expect(managedTelemetryNotice({running: true,- deployed: { known: true, enabled: true },+ deployed: { known: true, enabled: true, endpointCurrent: true },consent: false,sharedWithOthers: false,})@@ -78,7 +78,7 @@ describe('managedTelemetryNotice', () => {expect(managedTelemetryNotice({running: true,- deployed: { known: true, enabled: false },+ deployed: { known: true, enabled: false, endpointCurrent: true },consent: true,sharedWithOthers: true,})@@ -89,10 +89,54 @@ describe('managedTelemetryNotice', () => {expect(managedTelemetryNotice({running: true,- deployed: { known: true, enabled: true },+ deployed: { known: true, enabled: true, endpointCurrent: true },consent: false,sharedWithOthers: true,})).toEqual({ kind: 'stale', consent: false });});++ it('reports a server sharing to an address this build no longer names', () => {+ expect(+ managedTelemetryNotice({+ running: true,+ deployed: { known: true, enabled: true, endpointCurrent: false },+ consent: true,+ sharedWithOthers: false,+ })+ ).toEqual({ kind: 'stale-endpoint' });+ });++ it('applies the relay on a shared server too, since a restart keeps a sharing server sharing', () => {+ expect(+ managedTelemetryNotice({+ running: true,+ deployed: { known: true, enabled: true, endpointCurrent: false },+ consent: true,+ sharedWithOthers: true,+ })+ ).toEqual({ kind: 'stale-endpoint' });+ });++ it('does not chase the address of a server that is not sharing', () => {+ expect(+ managedTelemetryNotice({+ running: true,+ deployed: { known: true, enabled: false, endpointCurrent: false },+ consent: false,+ sharedWithOthers: false,+ })+ ).toEqual({ kind: 'in-step' });+ });++ it('puts an opt-out first when a server is still sharing to an old address', () => {+ expect(+ managedTelemetryNotice({+ running: true,+ deployed: { known: true, enabled: true, endpointCurrent: false },+ consent: false,+ sharedWithOthers: false,+ })+ ).toEqual({ kind: 'stale', consent: false });+ });});diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/managed-telemetry-notice.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/managed-telemetry-notice.tsindex 2bebacd04..883de713c 100644--- a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/managed-telemetry-notice.ts+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/managed-telemetry-notice.ts@@ -8,16 +8,18 @@ import type { DeployedTelemetry } from '@shared/core/managed-switch-server/manag* gave and the answer the server is acting on can differ, and this is where* that gap is turned into something to show.*- * The three outcomes are deliberately distinct. A server in step says nothing.+ * The outcomes are deliberately distinct. A server in step says nothing.* A server out of step says which way, because "not sharing yet" and "still* sharing after you said no" are not the same news. And a server we could not* read says so rather than borrowing either answer — the whole point of- * checking is that assuming was what went wrong.+ * checking is that assuming was what went wrong. A server sending somewhere+ * other than this build's relay is out of step too.*/export type ManagedTelemetryNotice =| { kind: 'in-step' }| { kind: 'unknown'; reason: string }- | { kind: 'stale'; consent: boolean };+ | { kind: 'stale'; consent: boolean }+ | { kind: 'stale-endpoint' };export function managedTelemetryNotice(args: {/** Whether the managed stack is up. A stopped server transmits nothing, so it@@ -36,6 +38,7 @@ export function managedTelemetryNotice(args: {const { running, deployed, consent, sharedWithOthers } = args;if (!running || deployed === null) return { kind: 'in-step' };if (!deployed.known) return { kind: 'unknown', reason: deployed.reason };+ if (deployed.enabled && consent && !deployed.endpointCurrent) return { kind: 'stale-endpoint' };if (deployed.enabled === consent) return { kind: 'in-step' };if (consent && sharedWithOthers) return { kind: 'in-step' };return { kind: 'stale', consent };diff --git a/console/apps/switch-console-desktop/src/renderer/features/templates/template-use-view.tsx b/console/apps/switch-console-desktop/src/renderer/features/templates/template-use-view.tsxindex 331d07f30..1719c2c28 100644--- a/console/apps/switch-console-desktop/src/renderer/features/templates/template-use-view.tsx+++ b/console/apps/switch-console-desktop/src/renderer/features/templates/template-use-view.tsx@@ -1098,6 +1098,7 @@ const TemplateUsePanel = observer(function TemplateUsePanel() {yamlText: room?.coreYaml ?? null,inputs: room?.inputs ?? {},templateName: loaded.name,+ entryPoint: 'template',});} catch (e) {for (const { i } of pending) setSlot(i, { status: 'failed', step: null });@@ -1250,6 +1251,7 @@ const TemplateUsePanel = observer(function TemplateUsePanel() {inputs,filled,templateName: loaded.name,+ entryPoint: 'template',});} catch (e) {for (const { i } of pending) setSlot(i, { status: 'failed', step: null });@@ -1377,7 +1379,7 @@ const TemplateUsePanel = observer(function TemplateUsePanel() {instructions: slot.entry.instructions,definitionAttributes: {},providerConfig: null,- entryPoint: 'server_page',+ entryPoint: 'template',// Only a single-agent template can be loaded again for this// agent's instructions; a team template has no one entry to offer.templateOrigin: loaded.singular ? loaded.origin : null,diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/agent-chats-sections.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/agent-chats-sections.test.tsxindex 97af62094..9c1488959 100644--- a/console/apps/switch-console-desktop/src/renderer/tests/browser/agent-chats-sections.test.tsx+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/agent-chats-sections.test.tsx@@ -87,6 +87,11 @@ vi.mock('@renderer/features/managed-agents/managed-agent-page', () => ({</div>),}));+// The profile's avatar asks the server whether third-party avatars are on.+vi.mock('@renderer/lib/stores/use-avatar-settings', async (importOriginal) => ({+ ...(await importOriginal<object>()),+ useAvatarSettings: () => ({ thirdPartyAvatarsEnabled: null, error: null, retry: () => {} }),+}));vi.mock('@renderer/features/chats/components/chat-panel', async (importOriginal) => ({...(await importOriginal<object>()),ChatPanel: () => null,@@ -194,6 +199,13 @@ it('calls the machine "This computer" when the agent runs here, keeping its nameagent={{...AGENT,machine: { id: 'machine-1', name: 'CC6G0XR0YF', kind: 'local', state: 'online' },+ status: {+ process: 'running',+ attached: true,+ reason: null,+ detail: null,+ directory: null,+ },}}/>)@@ -237,7 +249,7 @@ const room = (roomId: string, roomName: string): RemoteAgentRoom => ({/** A row in the agent's Rooms list, by the room's name. */const roomRow = (name: string) =>[...container.querySelectorAll<HTMLElement>('section[aria-label="Rooms"] > div')].find(- (row) => row.textContent === name+ (row) => row.textContent?.trim() === name)!;const embeddedRoom = () => container.querySelector<HTMLElement>('[data-testid="embedded-room"]');diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/connectors-page.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/connectors-page.test.tsxindex 853af862f..dfabcf20d 100644--- a/console/apps/switch-console-desktop/src/renderer/tests/browser/connectors-page.test.tsx+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/connectors-page.test.tsx@@ -66,6 +66,8 @@ vi.mock('@renderer/features/switch-servers/switch-servers-store', () => ({{ id: 'server', name: 'Dev Server', managed: server.managed, managementKind: 'local' },];},+ isConnected: () => true,+ statusFor: () => undefined,},}));@@ -579,7 +581,7 @@ it('shows a connection the workspace has switched off as off, not as connected',expect(page.querySelector('[aria-label="Atlassian connected"]')).toBeNull();expect(button(/^Atlassian/, grid(page)!)!.textContent).toContain('Off in this workspace');expect(button(/^Atlassian/, grid(page)!)!.textContent).toContain(- 'A workspace admin has switched it off.'+ 'Ask a workspace admin to turn it on.');});diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/managed-agent-page.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/managed-agent-page.test.tsxindex 8cd78dc5e..89ee27757 100644--- a/console/apps/switch-console-desktop/src/renderer/tests/browser/managed-agent-page.test.tsx+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/managed-agent-page.test.tsx@@ -189,8 +189,7 @@ it('lays out the agent the way a Console agent’s page does', () => {expect(text).toContain('on laptop');expect(text).toContain('Writes PRDs');expect(container.querySelector('textarea')?.value).toBe('Be brief.');- for (const heading of ['General', 'Sessions'])- expect([...container.querySelectorAll('h2')].map((h) => h.textContent)).toContain(heading);+ expect([...container.querySelectorAll('h2')].map((h) => h.textContent)).toContain('General');for (const row of ['Auto mode','Bypass permissions',diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/onboarding-drag-region.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/onboarding-drag-region.test.tsxindex 53d001322..7c487ae9d 100644--- a/console/apps/switch-console-desktop/src/renderer/tests/browser/onboarding-drag-region.test.tsx+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/onboarding-drag-region.test.tsx@@ -2,6 +2,7 @@ import { act, type ReactNode } from 'react';import { createRoot } from 'react-dom/client';import { afterEach, describe, expect, it } from 'vitest';import { OnboardingSurface, type WizardExit } from '@renderer/lib/ui/wizard-frame';+import '@renderer/index.css';/*** The first-run pages fill a frameless window, and the centred page columndiff --git a/console/apps/switch-console-desktop/src/shared/core/managed-switch-server/managed-switch-server.ts b/console/apps/switch-console-desktop/src/shared/core/managed-switch-server/managed-switch-server.tsindex 9f0475160..53bc5c2e6 100644--- a/console/apps/switch-console-desktop/src/shared/core/managed-switch-server/managed-switch-server.ts+++ b/console/apps/switch-console-desktop/src/shared/core/managed-switch-server/managed-switch-server.ts@@ -134,7 +134,13 @@ export type CheckoutBuild = {* unread host must not render as one that agrees.*/export type DeployedTelemetry =- | { known: true; enabled: boolean }+ | {+ known: true;+ enabled: boolean;+ /** Whether it sends to the relay this build reports to. A stack set up by+ * an older build may name none and run on its own version's default. */+ endpointCurrent: boolean;+ }| { known: false; reason: string };/**diff --git a/console/apps/switch-console-desktop/src/shared/core/telemetry/reporting.ts b/console/apps/switch-console-desktop/src/shared/core/telemetry/reporting.tsindex c9a29b883..f3d7e2c43 100644--- a/console/apps/switch-console-desktop/src/shared/core/telemetry/reporting.ts+++ b/console/apps/switch-console-desktop/src/shared/core/telemetry/reporting.ts@@ -28,6 +28,10 @@ export const UI_ENTRY_POINTS = ['session_list','room_row','link',+ /** The template Use page, which can create agents along with its rooms. */+ 'template',+ /** switch-buddy's own chat, which offers to set the buddy up after onboarding skipped it. */+ 'switch_buddy','unknown',] as const;diff --git a/console/packages/agent-providers/src/codex/fake-app-server.ts b/console/packages/agent-providers/src/codex/fake-app-server.tsindex f14bfbd7e..b13841c59 100644--- a/console/packages/agent-providers/src/codex/fake-app-server.ts+++ b/console/packages/agent-providers/src/codex/fake-app-server.ts@@ -20,6 +20,7 @@ export class FakeAppServer extends EventEmitter {readonly stderr = new PassThrough();readonly received: FakeJsonRpcMessage[] = [];private buffer = '';+ private exited = false;constructor() {super();@@ -37,13 +38,22 @@ export class FakeAppServer extends EventEmitter {index = this.buffer.indexOf('\n');}});+ // Like the real binary, which stops when its input closes. That is all a+ // Windows shutdown sends before the kill that follows two seconds later.+ this.stdin.on('finish', () => this.exit(0, null));}kill(signal?: NodeJS.Signals): boolean {- this.emit('exit', null, signal ?? 'SIGTERM');+ this.exit(null, signal ?? 'SIGTERM');return true;}+ private exit(code: number | null, signal: NodeJS.Signals | null): void {+ if (this.exited) return;+ this.exited = true;+ this.emit('exit', code, signal);+ }+send(message: FakeJsonRpcMessage): void {this.stdout.write(`${JSON.stringify({ jsonrpc: '2.0', ...message })}\n`);}diff --git a/docs/TELEMETRY.md b/docs/TELEMETRY.mdindex 3987cc0c5..1e30a6f35 100644--- a/docs/TELEMETRY.md+++ b/docs/TELEMETRY.md@@ -94,7 +94,7 @@ id, no IP field, no Switch identity.| `location` | `local`, `remote`, `unknown`. **Never a path, directory or project name.** || `server_kind` | `local`, `remote_managed`, `external`. **Never a server name or URL.** || `bridge_platform` | A platform key the server lists as one of its registered platforms (`slack`, `mattermost`, `discord`, `teams`, `telegram`, and any platform added since), else `other`, or `unknown` when it cannot be read. Always a short lowercase key. **Never a workspace or channel name.** |-| `entry_point` | `command_palette`, `sidebar`, `server_page`, `onboarding`, `agent_page`, `session_list`, `room_row`, `link`, `unknown` |+| `entry_point` | `command_palette`, `sidebar`, `server_page`, `onboarding`, `agent_page`, `session_list`, `room_row`, `link`, `template`, `switch_buddy`, `unknown` || `target` | `local`, `remote`, `unknown` |### 3.3 Every event and its fields@@ -131,7 +131,7 @@ id, no IP field, no Switch identity.| Event | Fields, with example values ||---|---|-| `agent_created` | `agent_type`: `codex` · `location`: `remote` · `outcome`: `failure` · `failure_reason`: `none` / `unauthenticated` / `name_conflict` / `credentials_conflict` / `already_configured` / `invalid_name` / `directory_unusable` / `not_configured` / `agent_not_on_server` / `error` · `entry_point`: `sidebar` |+| `agent_created` | `agent_type`: `codex` · `location`: `remote` · `outcome`: `failure` · `failure_reason`: `none` / `unauthenticated` / `name_conflict` / `credentials_conflict` / `already_configured` / `invalid_name` / `directory_unusable` / `not_configured` / `agent_not_on_server` / `refused` / `invalid` / `error` · `entry_point`: `sidebar` || `agent_removed` | `agent_type` · `location` · `delete_in_switch`: `true` · `trigger`: `user` / `server_teardown` · `outcome` · `failure_reason`: `none` / `not_linked_to_switch` / `gateway_unauthorized` / `gateway_http` / `gateway_network` / `error` || `agent_reset` | `agent_type` · `outcome` · `failure_reason`: `none` / `agent_not_found` / `not_remote` / `connect` / `error` || `agent_cli_action` | `agent_type` · `target`: `local` / `remote` · `install_method`: `homebrew` / `npm` / `winget` / `powershell` / `apt` / `curl` / `pip` / `cargo` / `installer-macos` / `installer-windows` / `installer-linux` / `other` / `unspecified` · `action`: `install` / `update` / `uninstall` · `outcome` · `failure_reason`: `none` / `unknown_dependency` / `no_install_command` / `no_update_strategy` / `no_uninstall_strategy` / `no_uninstall_command` / `permission_denied` / `command_failed` / `process_open_failed` / `not_detected_after_install` / `not_detected_after_update` / `still_present` / `error` · `duration_ms`: `8421` |@@ -172,7 +172,7 @@ sign-in records `invalid_credentials` — not the username tried, not the server| `bridge_connected` | `bridge`: `collaboration` · `bridge_platform`: `slack` · `outcome`: `failure` · `failure_reason`: `none` / `unauthenticated` / `forbidden` / `invalid` / `error` || `bridge_disconnected` | `bridge`: `collaboration` · `bridge_platform`: `mattermost` · `outcome` || `bridge_identity_claimed` | `bridge_platform` · `outcome` |-| `room_created` | `server_kind`: `local` · `bridge_platform`: `slack` · `agent_count`: `3` · `has_instructions`: `true` · `outcome` · `failure_reason`: `none` / `unauthenticated` / `bridge_unavailable` / `invalid` / `unreachable` / `error` |+| `room_created` | `server_kind`: `local` · `bridge_platform`: `slack` · `agent_count`: `3` · `has_instructions`: `true` · `outcome` · `failure_reason`: `none` / `unauthenticated` / `bridge_unavailable` / `invalid` / `unreachable` / `error` · `source`: `dialog` / `template` || `room_deleted` | `server_kind` · `outcome` || `room_agents_added` | `agent_count`: `2` · `direction`: `agents_to_room` / `room_to_agents` |@@ -180,6 +180,17 @@ A room creation tells us *"someone made a Slack-bridged room with 3 agents and iworked"*. It does not tell us the room name, the channel, the workspace, or whichagents.+`source` says whether the room came from the Create room dialog or from a+template. A template run reports each room it made, and each agent as+`agent_created` with `entry_point` `template` — or, for switch-buddy, `onboarding`+from the first run and `switch_buddy` from its chat later. What a run does not+already say — a room's platform, agent count and whether it has instructions, the+provider of an agent the server chose — is read back from the server once it+exists, only when sharing is on, and only while the run's workspace is still the+one open: the app never switches back to it just to describe the run. A template+run that fails reports nothing: it made nothing, and how many rooms it would have+made is not known.+**Remote hosts**| Event | Fields, with example values |@@ -240,7 +251,11 @@ Switch Console can run a Switch server for the user — as containers on their omachine, or on a host they have given it over SSH. That server has product-usagetelemetry of its own, off unless its configuration turns it on, and the Console'stoggle is what turns it on: the answer is written into the server's configuration-every time the Console starts it, in both directions. "No" is written down+every time the Console starts it, in both directions, together with the address+to send to — the same relay the app reports to, rather than whatever the+server's own version defaults to. A server that was already running when the+Console started naming the address is flagged on its page like any other+out-of-step setting, with the restart offered. "No" is written downexplicitly rather than left out, because a stack started under a "yes" wouldotherwise keep the setting it started with.