The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
hn 0.1.32: hn measures its flows and milestones like the Desktop (#1346)
lingrdeb43ba821725
diff --git a/backend/README.md b/backend/README.mdindex ef6823ea6..9a361a7b5 100644--- a/backend/README.md+++ b/backend/README.md@@ -161,6 +161,13 @@ model changes and history. Only exact known error codes select a fixed failure care never read. The response timer measures the first text event after an observed turn start,not model tokens or output quality; tool-only turns can have no text response.+hn (the `tui` surface) adds fixed `ui_tui_*` flow and `health_tui_*` milestone counters through its+local daemon's queue, under the same account choice: connections, computer linking, viewer opening,+app start, workspace ready, resume recovery, and once-per-session workspace-ready, first-work and+first-response milestones. It records only while signed in and sends nothing observed before sign-in.+Its counters go in reports of their own, so a backend without the `tui` names rejects only those; the+daemon then drops them until the next UTC day or its restart, and records no delivery failure for it.+No prompt, response text, transcript, code, file or project name, path, tool argument, error text,engine name, or stable user/device/agent identifier is a report field. Agent and launch identifiersexist only in bounded in-memory maps (1,000 entries each) and never enter disk queues or HTTP reports.diff --git a/backend/src/analytics/dailyProductReview.test.ts b/backend/src/analytics/dailyProductReview.test.tsindex e4536178c..fd7784efa 100644--- a/backend/src/analytics/dailyProductReview.test.ts+++ b/backend/src/analytics/dailyProductReview.test.ts@@ -56,6 +56,17 @@ it('keeps feedback denominators and setup coverage separate from daemon participexpect(r.flows.find(f => f.channel === 'setup' && f.surface === 'desktop' && f.flow === 'sign_in')).toMatchObject({ started: 5, unmatchedStarts: 3 })expect(r.flows.find(f => f.channel === 'ui' && f.flow === 'update_stage')?.started).toBeNull()})+it('shows hn\'s app flows beside the other apps, with no signed-out setup row for it', () => {+ // hn counts a start when the flow begins and its outcome when it ends, as the Desktop does: one still+ // open (or ended under another account) is an unmatched start.+ const r = dailyProductReview([+ row('2026-10-12', 'ui_tui_workspace_connect_started', 4), row('2026-10-12', 'ui_tui_workspace_connect_ok', 3),+ row('2026-10-12', 'ui_tui_workspace_connect_under_1s', 3),+ ], new Date('2026-10-13'))+ expect(r.flows.find(f => f.channel === 'ui' && f.surface === 'tui' && f.flow === 'workspace_connect'))+ .toMatchObject({ started: 4, unmatchedStarts: 1, outcomes: { ok: 3, failed: 0 }, durationBuckets: { under_1s: 3, over_1m: 0 } })+ expect(r.flows.some(f => f.channel === 'setup' && f.surface === 'tui')).toBe(false)+})it('uses complete calendar months rather than adding daily installations into MAU', () => {const r = dailyProductReview([row('2026-08-12', 'monthly_working_installations', 10), row('2026-09-12', 'monthly_working_installations', 8),diff --git a/backend/src/analytics/dailyProductReview.ts b/backend/src/analytics/dailyProductReview.tsindex 80464b6b6..39ae89f8b 100644--- a/backend/src/analytics/dailyProductReview.ts+++ b/backend/src/analytics/dailyProductReview.ts@@ -1,4 +1,4 @@-import { PRODUCT_FEEDBACK, PRODUCT_FEEDBACK_REASONS, PRODUCT_FEEDBACK_OUTCOMES, PRODUCT_USAGE_ACTIONS, PRODUCT_USAGE_AUTOMATIC_ACTIONS, PRODUCT_USAGE_FLOWS, PRODUCT_USAGE_SETUP_FLOWS, PRODUCT_USAGE_SURFACES, PRODUCT_USAGE_USER_OPENED_ACTIONS } from '../types/productUsage.js'+import { PRODUCT_FEEDBACK, PRODUCT_FEEDBACK_REASONS, PRODUCT_FEEDBACK_OUTCOMES, PRODUCT_USAGE_ACTIONS, PRODUCT_USAGE_AUTOMATIC_ACTIONS, PRODUCT_USAGE_FLOWS, PRODUCT_USAGE_SETUP_FLOWS, PRODUCT_USAGE_SURFACES, PRODUCT_USAGE_UI_SURFACES, PRODUCT_USAGE_USER_OPENED_ACTIONS } from '../types/productUsage.js'import { DAILY_PRODUCT_METRICS } from './dailyProductUsage.js'type Row = { dayUtc: Date; metric: string; value: number }@@ -89,7 +89,8 @@ export function dailyProductReview(rows: Row[], now: Date) {percent: completeMonth.counters.monthly_working_installations ? rate(monthReturn, monthDenominator) : null,definition: 'Participating installations with work in two consecutive complete UTC calendar months; late uploads and measurement rollout affect coverage.',},- flows: ['setup', 'ui'].flatMap(channel => PRODUCT_USAGE_SURFACES.flatMap(surface => PRODUCT_USAGE_FLOWS+ // hn has app flows but no signed-out setup channel.+ flows: ['setup', 'ui'].flatMap(channel => ((channel === 'setup' ? PRODUCT_USAGE_SURFACES : PRODUCT_USAGE_UI_SURFACES) as readonly string[]).flatMap(surface => PRODUCT_USAGE_FLOWS.filter(flow => channel !== 'setup' || (PRODUCT_USAGE_SETUP_FLOWS as readonly string[]).includes(flow)).map(flow => {const prefix = `${channel}_${surface}_${flow}_`diff --git a/backend/src/analytics/productHealth.test.ts b/backend/src/analytics/productHealth.test.tsindex 91077cf28..80a6e2bce 100644--- a/backend/src/analytics/productHealth.test.ts+++ b/backend/src/analytics/productHealth.test.ts@@ -11,6 +11,15 @@ it('keeps the CLI contract identical and every Desktop enum in the server vocabuconst flows = readFileSync('../desktop/lib/measurement/product_measurement.dart', 'utf8')for (const flow of HEALTH_FLOWS) expect(flows).toContain(`'${flow}'`)})+it('shows hn\'s session milestones beside the Desktop\'s', () => {+ const row = (metric: string, value: number) => ({ dayUtc: new Date('2026-10-01'), metric, value })+ const data = dailyProductHealth([row('health_tui_funnel_first_work', 3), row('health_tui_first_work_under_1m', 2),+ row('health_desktop_funnel_first_work', 7)], new Date('2026-10-04'))+ const [, desktop, tui] = data.daily[0].activation+ expect(desktop.milestones.first_work).toBe(7)+ expect(tui).toMatchObject({ milestones: { first_work: 3, setup_seen: null }, waitBuckets: { first_work_under_1m: 2 } })+ expect(tui.population).toBe('Consented signed-in hn sessions')+})it('creates two independent margins with no joint dimensions or arbitrary labels', () => {expect(healthBreakdowns(context, 'turns_started')).toEqual(['breakdown:release:desktop:1.2.73:turns_started', 'breakdown:platform:desktop:macos:turns_started'])expect(healthBreakdowns({ ...context, channel: 'development' }, 'turns_started')).toEqual([])diff --git a/backend/src/analytics/productHealth.ts b/backend/src/analytics/productHealth.tsindex 45341aeb6..98bc3fa6d 100644--- a/backend/src/analytics/productHealth.ts+++ b/backend/src/analytics/productHealth.ts@@ -45,8 +45,9 @@ export function dailyProductHealth(rows: Row[], now: Date) {interpretation: 'Start and result reports can arrive separately or cross a choice/day boundary. A difference is incomplete observation, not a failed action.' }}),signals: Object.fromEntries(Object.entries(c).filter(([k]) => k.startsWith('health_') || k.startsWith('request_'))),- activation: ['setup_desktop', 'health_desktop'].map(prefix => ({- population: prefix === 'setup_desktop' ? 'Voluntary signed-out setup episodes' : 'Consented signed-in app sessions',+ activation: ['setup_desktop', 'health_desktop', 'health_tui'].map(prefix => ({+ population: prefix === 'setup_desktop' ? 'Voluntary signed-out setup episodes'+ : prefix === 'health_tui' ? 'Consented signed-in hn sessions' : 'Consented signed-in app sessions',milestones: Object.fromEntries(['setup_seen', 'sign_in_seen', 'workspace_ready', 'first_work', 'first_response'].map(step => [step, c[`${prefix}_funnel_${step}`] ?? null])),waitBuckets: Object.fromEntries(['first_work', 'first_response'].flatMap(step => ['under_1m', '1m_to_5m', 'over_5m'].map(bucket => [`${step}_${bucket}`, c[`${prefix}_${step}_${bucket}`] ?? null]))),definition: 'Locally deduplicated episodes, not a joined per-person funnel. Stages can cross UTC days; no daily conversion ratio is inferred.',diff --git a/backend/src/routes/productUsage.test.ts b/backend/src/routes/productUsage.test.tsindex d11cbff02..86d994dcb 100644--- a/backend/src/routes/productUsage.test.ts+++ b/backend/src/routes/productUsage.test.ts@@ -104,7 +104,9 @@ it('accepts only explicitly enabled setup counts without credentials or accountexpect(methods.productUsageChoice).not.toHaveBeenCalled()expect(methods.ingestProductUsage).not.toHaveBeenCalled()for (const payload of [{ ...setup, consent: false }, { ...setup, account: 'PRIVATE_CANARY' },- { ...setup, counters: { turns_started: 1 } }, { ...setup, counters: { setup_desktop_sign_in_started: 1001 } }]) {+ { ...setup, counters: { turns_started: 1 } }, { ...setup, counters: { setup_desktop_sign_in_started: 1001 } },+ // hn has no signed-out channel: it counts only through its signed-in daemon.+ { ...setup, counters: { setup_tui_sign_in_started: 1 } }, { ...setup, counters: { ui_tui_sign_in_started: 1 } }]) {const response = await sendSetup(payload)expect(response.statusCode).toBe(400)expect(response.body).not.toContain('PRIVATE_CANARY')diff --git a/backend/src/types/productHealthContract.ts b/backend/src/types/productHealthContract.tsindex 1e3a7dca0..79d7edaec 100644--- a/backend/src/types/productHealthContract.ts+++ b/backend/src/types/productHealthContract.ts@@ -35,7 +35,8 @@ export type ResumeReason = typeof RESUME_REASONS[number]export const PRODUCT_HEALTH_METRICS = [...CLI_REPORT_FAILURES.map(reason => `health_cli_report_failure_${reason}` as const),...HEALTH_SOURCES.flatMap(source => RESUME_REASONS.map(reason => `request_${source}_resume_reason_${reason}` as const)),- ...(['cli', 'desktop'] as const).flatMap(surface => HEALTH_MEASURES.map(m => `health_${surface}_${m}` as const)),+ // hn's (`tui`) come through its daemon's queue, under that daemon's context, never one of their own.+ ...(['cli', 'desktop', 'tui'] as const).flatMap(surface => HEALTH_MEASURES.map(m => `health_${surface}_${m}` as const)),...HEALTH_SOURCES.flatMap(source => HEALTH_ACTIONS.flatMap(action => [...HEALTH_RESULTS.map(result => `request_${source}_${action}_${result}` as const),...HEALTH_FAILURES.map(failure => `request_${source}_${action}_error_${failure}` as const),diff --git a/backend/src/types/productUsage.ts b/backend/src/types/productUsage.tsindex 7301152cb..f894a011f 100644--- a/backend/src/types/productUsage.ts+++ b/backend/src/types/productUsage.ts@@ -18,6 +18,9 @@ export const PRODUCT_USAGE_FEATURE_MEASURES = ['installation_days', 'first_insta* clients released before the split still send the plain names for every automatic read. */export const PRODUCT_USAGE_USER_OPENED_ACTIONS = ['store_list', 'model_list'] as const satisfies typeof PRODUCT_USAGE_AUTOMATIC_ACTIONSexport const PRODUCT_USAGE_SURFACES = ['desktop', 'phone', 'web'] as const+/** hn counts its flows through its own daemon (`product_observe`), signed in only: it has the UI+ * names but no signed-out setup channel, so it is not one of the setup surfaces above. */+export const PRODUCT_USAGE_UI_SURFACES = [...PRODUCT_USAGE_SURFACES, 'tui'] as const/** Flows that can finish before an account exists, so a signed-out report may carry them. The* sign_in_<method> flows split the phone's welcome screen by the button used; sign_in stays the total. */export const PRODUCT_USAGE_SETUP_FLOWS = ['setup', 'setup_harness', 'setup_tmux', 'setup_clipboard', 'sign_in',@@ -30,7 +33,7 @@ export const PRODUCT_USAGE_FLOWS = [...PRODUCT_USAGE_SETUP_FLOWS,...HEALTH_FLOWS, ...PRODUCT_USAGE_ONBOARDING_FLOWS] as constexport const PRODUCT_USAGE_FLOW_MEASURES = ['started', 'ok', 'failed', 'cancelled', 'unknown','under_1s', '1s_to_10s', '10s_to_1m', 'over_1m'] as const-export const PRODUCT_USAGE_UI_METRICS = PRODUCT_USAGE_SURFACES.flatMap(surface =>+export const PRODUCT_USAGE_UI_METRICS = PRODUCT_USAGE_UI_SURFACES.flatMap(surface =>PRODUCT_USAGE_FLOWS.flatMap(flow => PRODUCT_USAGE_FLOW_MEASURES.map(measure => `ui_${surface}_${flow}_${measure}` as const)))// A signed-out report is explicitly enabled on this app. It cannot carry account/work metrics.export const PRODUCT_USAGE_SETUP_METRICS = [...PRODUCT_USAGE_SURFACES.flatMap(surface =>diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.tsindex 5ead8e01d..b25adaac8 100644--- a/cli/src/backendSocket.spec.ts+++ b/cli/src/backendSocket.spec.ts@@ -4137,4 +4137,28 @@ describe('opening the app', () => {socket.registerLocalClient('local:again', sink)expect(opened).toEqual(['desktop', 'tui', 'desktop'])})++ it('counts hn\'s own observations from hn alone, as fixed counters', async () => {+ const socket = new BackendSocket('fixture')+ const counted: string[] = []+ socket.useGateway({ holdRequests: () => {}, serveThisComputerOnly: () => {}, localClients: () => {}, windowOpened: () => {},+ usageCounter: (metric: string) => { counted.push(metric) } } as never)+ const sink = { sendFrame: () => true, sendBinary: () => true }+ socket.registerLocalClient('local:tui', sink, { surface: 'tui' })+ socket.registerLocalClient('local:app', sink)+ socket.registerLocalClient('local:tool', sink, { tool: true })+ const flow = { flow: 'remote_connect', outcome: 'failed', durationMs: 12_000 }+ for (const id of ['local:app', 'local:tool', 'web-1', 'local:gone']) socket.observeProduct(id, flow)+ expect(counted).toEqual([])+ socket.observeProduct('local:tui', { flow: 'remote_connect' })+ socket.observeProduct('local:tui', flow)+ socket.observeProduct('local:tui', { measure: 'funnel_workspace_ready' })+ socket.observeProduct('local:tui', { measure: 'PRIVATE_CANARY' })+ socket.observeProduct('local:tui', { ...flow, agentId: 'PRIVATE_CANARY' })+ expect(counted).toEqual(['ui_tui_remote_connect_started', 'ui_tui_remote_connect_failed', 'ui_tui_remote_connect_10s_to_1m',+ 'health_tui_funnel_workspace_ready'])+ await socket.unregisterLocalClient('local:tui')+ socket.observeProduct('local:tui', flow)+ expect(counted).toHaveLength(4)+ })})diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.tsindex 1b62d93b8..4b982769f 100644--- a/cli/src/backendSocket.ts+++ b/cli/src/backendSocket.ts@@ -1,4 +1,4 @@-import { productUsageIntent, productUsageLaunch, productUsageRequest } from './lib/productUsageSignals.js'+import { productObservation, productUsageIntent, productUsageLaunch, productUsageRequest } from './lib/productUsageSignals.js'import { isLocalOnly, withoutUpdate } from './lib/cardUpdate.js'import type { PurgeAgentService } from './lib/purgeAgentService.js'import { MESSAGES_REQUESTS, SHARE_REQUESTS, type Asker, type LocalWindows, type WindowSurface, type BackendNotice, type GatewayEvents, type GatewayPort, type ModelsPort, type RemoteClient, type RemoteRole, type RemoteTransport } from './core/api.js'@@ -642,6 +642,14 @@ export class BackendSocket {/** The windows attached right now — for a listener that arrives after some of them did. */localClientIds(): string[] { return [...this.localClients.keys()].filter((connId) => !this.toolClients.has(connId)) }+ /** hn's own measurement of its flows (`product_observe`, consumed by localWsServer.ts): counted only+ * for a connection that selected this computer as hn, into the gateway's product-usage queue, which+ * counts nothing while it has none (signed out) or the account's choice is off. Never answered. */+ observeProduct(connId: string, payload: unknown): void {+ if (!this.tuiClients.has(connId)) return+ for (const metric of productObservation(payload)) this.toGateway('usage', (gateway) => gateway.usageCounter?.(metric))+ }+/** Routes a request to the service that answers it, in its own process (core/serviceLinks.ts) or in* this one (core/serviceHost.ts): false when none does and the socket answers it itself. */serviceRouter: ((type: string, payload: Record<string, unknown>, asker: Asker, reply: (result: Record<string, unknown>) => void) => boolean) | null = nulldiff --git a/cli/src/gateway/productUsage.spec.ts b/cli/src/gateway/productUsage.spec.tsindex bf8b4ae8e..632fcb764 100644--- a/cli/src/gateway/productUsage.spec.ts+++ b/cli/src/gateway/productUsage.spec.ts@@ -97,6 +97,77 @@ it('discards a permanently invalid packet so later reports and loss counts can rexpect(totals().health_cli_report_failure_invalid).toBe(1)})+it('sends hn\'s counters alone, so a backend from before `tui` refuses only them', async () => {+ const tui = (metric: string) => metric.startsWith('ui_tui_') || metric.startsWith('health_tui_')+ await usage.sync()+ usage.counter('ui_tui_sign_in_started'); usage.counter('search_requests'); usage.counter('health_tui_funnel_first_work')+ usage.counter('ui_desktop_sign_in_ok'); usage.counter('ui_tui_sign_in_ok'); usage.counter('tui_opens')+ // That backend's strict vocabulary: one unknown name makes the whole report invalid.+ const accepted: Record<string, number>[] = []+ request.mockImplementation(async (method: string, _path: string, body?: unknown) => {+ if (method === 'GET') return { status: 200, body: { data: { enabled: true, revision: 0 } } }+ const counters = (body as { counters: Record<string, number> }).counters+ sent.push(structuredClone(body))+ if (Object.keys(counters).some(tui)) return { status: 400, body: { error: { code: 'INVALID_USAGE_REPORT' } } }+ accepted.push(counters)+ return { status: 200, body: { data: { accepted: true } } }+ })+ for (let i = 0; i < 4; i++) await usage.sync()+ for (const report of sent as { counters: Record<string, number> }[]) {+ const names = Object.keys(report.counters)+ expect(names.every(tui) || !names.some(tui), names.join()).toBe(true)+ }+ const kept = Object.assign({}, ...accepted)+ expect(kept).toMatchObject({ search_requests: 1, ui_desktop_sign_in_ok: 1, tui_opens: 1 })+ expect(Object.keys(kept).some(tui)).toBe(false)+ // Its refusal is hn's alone: no delivery failure is counted for it.+ expect(kept.health_cli_report_rejected).toBeUndefined()+ expect(kept.health_cli_report_failure_invalid).toBeUndefined()+ expect(JSON.parse(readFileSync(file, 'utf8'))).toMatchObject({ pending: null, days: {} })+ // Refused for the rest of the UTC day: hn's counts are dropped, not sent again or kept to age.+ const refusals = () => (sent as { counters: Record<string, number> }[]).filter(r => Object.keys(r.counters).some(tui)).length+ expect(refusals()).toBe(1)+ usage.counter('ui_tui_viewer_open_started'); usage.counter('search_requests')+ for (let i = 0; i < 3; i++) await usage.sync()+ expect(refusals()).toBe(1)+ expect(JSON.stringify(JSON.parse(readFileSync(file, 'utf8')).days)).not.toContain('_tui_')+ // The next UTC day (an updated backend, say) takes them again.+ now += 86_400_000+ request.mockImplementation(async (method: string, _path: string, body?: unknown) => {+ if (method === 'GET') return { status: 200, body: { data: { enabled: true, revision: 0 } } }+ sent.push(structuredClone(body))+ return { status: 200, body: { data: { accepted: true } } }+ })+ usage.counter('ui_tui_viewer_open_started')+ for (let i = 0; i < 3; i++) await usage.sync()+ expect(totals().ui_tui_viewer_open_started).toBe(1)+})++it('holds hn\'s observations until the first choice read: counted when sharing is on, discarded when off', async () => {+ usage.counter('ui_tui_remote_connect_started'); usage.counter('health_tui_funnel_first_work'); usage.counter('search_requests')+ await usage.sync(); await usage.sync()+ expect(totals()).toMatchObject({ ui_tui_remote_connect_started: 1, health_tui_funnel_first_work: 1 })+ expect(totals().search_requests).toBeUndefined()++ await usage.stop(); sent = []; enabled = false+ rmSync(file, { force: true })+ usage = createProductUsage({ file, now: () => now, request })+ for (let i = 0; i < 70; i++) usage.counter('ui_tui_remote_connect_started')+ await usage.sync()+ enabled = true+ await usage.sync(); await usage.sync()+ expect(totals().ui_tui_remote_connect_started).toBeUndefined()+})++it('keeps holding hn\'s observations through a failed choice read, bounded, and counts them once sharing is known on', async () => {+ request.mockResolvedValueOnce({ status: 503, body: {} })+ for (let i = 0; i < 70; i++) usage.counter('ui_tui_remote_connect_started')+ await usage.sync()+ expect(sent).toEqual([])+ await usage.sync(); await usage.sync(); await usage.sync()+ expect(totals().ui_tui_remote_connect_started).toBe(64)+})+it.each([[503, 'USAGE_WRITE_CONFLICT', 'write_conflict'], [503, 'USAGE_TRANSACTION_FAILED', 'transaction'],[504, 'BACKEND_TIMEOUT', 'timeout'], [502, 'BACKEND_UNREACHABLE', 'unreachable'],diff --git a/cli/src/gateway/productUsage.ts b/cli/src/gateway/productUsage.tsindex afe354e99..952d30977 100644--- a/cli/src/gateway/productUsage.ts+++ b/cli/src/gateway/productUsage.ts@@ -23,6 +23,7 @@ const requestActions = new Map<string, Reach>([...PRODUCT_USAGE_ACTIONS.filter(action => !PRODUCT_USAGE_AUTOMATIC_ACTIONS.includes(action)).map(action => [`${action}_requests`, action] as const),...PRODUCT_USAGE_USER_OPENED_ACTIONS.map(action => [`${action}_user_requests`, `${action}_user`] as const),])+const isTuiMetric = (metric: string): boolean => metric.startsWith('ui_tui_') || metric.startsWith('health_tui_')const key = (now: number) => new Date(now).toISOString().slice(0, 10)const week = (now: number) => { const d = new Date(now); return key(now - ((d.getUTCDay() + 6) % 7) * DAY) }type Counters = Partial<Record<ProductUsageMetric, number>>@@ -73,6 +74,11 @@ export function createProductUsage(deps: ProductUsageDeps) {let state = fresh(-1), enabled = false, stopped = false, active = falselet syncing: Promise<void> | null = null, persistTimer: ReturnType<typeof setTimeout> | undefinedlet writing: Promise<boolean> | null = null, writeAgain = false+ // hn's counters: those it observed before the first choice read, held in RAM until that is known+ // (hn is often opened at the daemon's start), and the UTC day a backend from before `tui` refused+ // them. Neither is persisted; a restart tries again.+ let choiceKnown = false, tuiHeld: ProductUsageMetric[] = [], tuiRefusedDay: string | null = null+ const tuiRefused = (): boolean => tuiRefusedDay === key(now())const running = new Map<string, { started: number; response: boolean; error: boolean; waited: boolean }>()const launches = new Map<string, { started: number; done: boolean; waits: Set<string> }>()try {@@ -221,6 +227,13 @@ export function createProductUsage(deps: ProductUsageDeps) {if (response.status !== 200 || typeof body?.enabled !== 'boolean'|| !Number.isSafeInteger(body.revision) || body.revision < 0) { enabled = false; running.clear(); launches.clear(); health.clearObservations(); return }await choice(body)+ if (!choiceKnown) {+ // Counted only now that sharing is known to be on; discarded when it is off.+ choiceKnown = true+ const held = tuiHeld+ tuiHeld = []+ if (enabled && !tuiRefused()) for (const metric of held) add(metric)+ }if (!enabled) returnif (context) {health.start(context.release); health.checkpoint(); health.sample(deps.sample)@@ -234,10 +247,19 @@ export function createProductUsage(deps: ProductUsageDeps) {if (now() - launch.started >= 35 * 60_000) launches.delete(id)}if (active) markActive()+ // Refused today: hn's counts go, rather than waiting to become a past day's report.+ if (tuiRefused()) for (const day of Object.keys(state.days)) {+ for (const metric of Object.keys(state.days[day])) if (isTuiMetric(metric)) delete state.days[day][metric as ProductUsageMetric]+ if (!Object.keys(state.days[day]).length) { delete state.days[day]; delete state.dayContexts[day] }+ }if (!state.pending) {const day = Object.keys(state.days).sort().find(d => validDay(d) && Object.keys(state.days[d]).length)if (!day) return- const counters = Object.fromEntries(Object.entries(state.days[day]).slice(0, MAX_REPORT_METRICS)) as Counters+ // hn's counters travel alone: a backend from before `tui` refuses its whole report (400), and+ // that refusal drops the report, so it must never carry the daemon's or the Desktop's counts.+ const entries = Object.entries(state.days[day])+ const own = entries.filter(([metric]) => !isTuiMetric(metric))+ const counters = Object.fromEntries((own.length ? own : entries).slice(0, MAX_REPORT_METRICS)) as Countersconst reportContext = state.dayContexts[day] ?? contextstate.pending = { schemaVersion: 1, revision: state.revision, id: uuid(), day, counters, ...(reportContext ? { context: reportContext } : {}) }for (const metric of Object.keys(counters)) delete state.days[day][metric as ProductUsageMetric]@@ -253,6 +275,11 @@ export function createProductUsage(deps: ProductUsageDeps) {if (answer.status === 200 && result?.accepted === true) { state.pending = null; await persist() }else if (result?.reason === 'CHOICE_CHANGED') { enabled = false; state = fresh(-1); running.clear(); launches.clear(); health.reset(); await persist() }else if (result?.reason === 'EXPIRED') { state.pending = null; add('health_cli_report_expired'); await persist() }+ else if (answer.status === 400 && Object.keys(report.counters).length && Object.keys(report.counters).every(isTuiMetric)) {+ // A backend from before `tui` knows none of hn's names. That is not a failed delivery, so it+ // is not counted as one: the report goes, and hn's counts with it until the next UTC day.+ state.pending = null; tuiRefusedDay = key(now()); await persist()+ }else {add('health_cli_report_rejected')add(`health_cli_report_failure_${reportFailure(answer)}`)@@ -271,7 +298,12 @@ export function createProductUsage(deps: ProductUsageDeps) {return {sync,counter(metric: ProductUsageMetric): void {- if (!enabled || stopped || !metricSet.has(metric)) return+ if (stopped || !metricSet.has(metric)) return+ if (isTuiMetric(metric)) {+ if (!enabled) { if (!choiceKnown && tuiHeld.length < 64) tuiHeld.push(metric); return }+ if (tuiRefused()) return+ }+ if (!enabled) returnadd(metric)const action = requestActions.get(metric)if (action && state.featureDays[action] !== key(now())) {@@ -381,7 +413,7 @@ export function createProductUsage(deps: ProductUsageDeps) {}},stop(discard = false): Promise<boolean> {- stopped = true; enabled = false; clearInterval(timer); running.clear(); launches.clear(); health.stop()+ stopped = true; enabled = false; clearInterval(timer); running.clear(); launches.clear(); health.stop(); tuiHeld = []if (discard) { state = fresh(-1); health.reset() }return persist()},diff --git a/cli/src/lib/appProductUsageReport.spec.ts b/cli/src/lib/appProductUsageReport.spec.tsindex 893154a22..cf430708a 100644--- a/cli/src/lib/appProductUsageReport.spec.ts+++ b/cli/src/lib/appProductUsageReport.spec.ts@@ -19,9 +19,10 @@ it('admits the current native report and every fixed Desktop health measure', ()}})+const appMetrics = PRODUCT_USAGE_UI_METRICS.filter(metric => !metric.startsWith('ui_tui_'))it('keeps all legacy app UI counters compatible, in bounded batches', () => {- for (let i = 0; i < PRODUCT_USAGE_UI_METRICS.length; i += 32) {- const counters = Object.fromEntries(PRODUCT_USAGE_UI_METRICS.slice(i, i + 32).map(key => [key, 1000]))+ for (let i = 0; i < appMetrics.length; i += 32) {+ const counters = Object.fromEntries(appMetrics.slice(i, i + 32).map(key => [key, 1000]))expect(isAppProductUsageReport({ ...old, counters })).toBe(true)}expect(isAppProductUsageReport({ ...native, counters: old.counters })).toBe(true)@@ -40,6 +41,9 @@ it('refuses arbitrary context, non-Desktop attribution, daemon counts and malforcontext({ channel: 'PRIVATE_CANARY' }), context({ release: undefined }),counters('turns_started'), counters('request_app_create_started'), counters('health_cli_collector_days'),counters('setup_desktop_sign_in_ok'), counters('ui_phone_sign_in_ok'), counters('ui_web_sign_in_ok'),+ // hn's are the daemon's to count from hn's own frame, never an app's report.+ counters('ui_tui_sign_in_ok'), counters('health_tui_funnel_first_work'),+ { ...old, counters: { ui_tui_sign_in_ok: 1 } }, { ...old, counters: { health_tui_funnel_first_work: 1 } },counters('PRIVATE_CANARY'), counters('health_desktop_collector_days', 'PRIVATE_CANARY'),...[0, -1, 1001, 1.5, NaN, Infinity].map(value => counters('health_desktop_collector_days', value)),{ ...old, counters: { health_desktop_collector_days: 1 } },diff --git a/cli/src/lib/appProductUsageReport.ts b/cli/src/lib/appProductUsageReport.tsindex 753e21286..d03f68039 100644--- a/cli/src/lib/appProductUsageReport.ts+++ b/cli/src/lib/appProductUsageReport.ts@@ -1,7 +1,8 @@import { cleanUsageContext, HEALTH_MEASURES } from './productHealthContract.js'import { PRODUCT_USAGE_UI_METRICS, type ProductUsageReport } from './productUsageContract.js'-const uiMetrics = new Set<string>(PRODUCT_USAGE_UI_METRICS)+// hn's counts reach the daemon's own queue only through hn's `product_observe`; no app can file them.+const uiMetrics = new Set<string>(PRODUCT_USAGE_UI_METRICS.filter(metric => !metric.startsWith('ui_tui_')))const desktopMetrics = new Set<string>([...PRODUCT_USAGE_UI_METRICS.filter(metric => metric.startsWith('ui_desktop_')),...HEALTH_MEASURES.map(measure => `health_desktop_${measure}`),diff --git a/cli/src/lib/productHealthContract.ts b/cli/src/lib/productHealthContract.tsindex 1e3a7dca0..79d7edaec 100644--- a/cli/src/lib/productHealthContract.ts+++ b/cli/src/lib/productHealthContract.ts@@ -35,7 +35,8 @@ export type ResumeReason = typeof RESUME_REASONS[number]export const PRODUCT_HEALTH_METRICS = [...CLI_REPORT_FAILURES.map(reason => `health_cli_report_failure_${reason}` as const),...HEALTH_SOURCES.flatMap(source => RESUME_REASONS.map(reason => `request_${source}_resume_reason_${reason}` as const)),- ...(['cli', 'desktop'] as const).flatMap(surface => HEALTH_MEASURES.map(m => `health_${surface}_${m}` as const)),+ // hn's (`tui`) come through its daemon's queue, under that daemon's context, never one of their own.+ ...(['cli', 'desktop', 'tui'] as const).flatMap(surface => HEALTH_MEASURES.map(m => `health_${surface}_${m}` as const)),...HEALTH_SOURCES.flatMap(source => HEALTH_ACTIONS.flatMap(action => [...HEALTH_RESULTS.map(result => `request_${source}_${action}_${result}` as const),...HEALTH_FAILURES.map(failure => `request_${source}_${action}_error_${failure}` as const),diff --git a/cli/src/lib/productUsageContract.ts b/cli/src/lib/productUsageContract.tsindex 7301152cb..f894a011f 100644--- a/cli/src/lib/productUsageContract.ts+++ b/cli/src/lib/productUsageContract.ts@@ -18,6 +18,9 @@ export const PRODUCT_USAGE_FEATURE_MEASURES = ['installation_days', 'first_insta* clients released before the split still send the plain names for every automatic read. */export const PRODUCT_USAGE_USER_OPENED_ACTIONS = ['store_list', 'model_list'] as const satisfies typeof PRODUCT_USAGE_AUTOMATIC_ACTIONSexport const PRODUCT_USAGE_SURFACES = ['desktop', 'phone', 'web'] as const+/** hn counts its flows through its own daemon (`product_observe`), signed in only: it has the UI+ * names but no signed-out setup channel, so it is not one of the setup surfaces above. */+export const PRODUCT_USAGE_UI_SURFACES = [...PRODUCT_USAGE_SURFACES, 'tui'] as const/** Flows that can finish before an account exists, so a signed-out report may carry them. The* sign_in_<method> flows split the phone's welcome screen by the button used; sign_in stays the total. */export const PRODUCT_USAGE_SETUP_FLOWS = ['setup', 'setup_harness', 'setup_tmux', 'setup_clipboard', 'sign_in',@@ -30,7 +33,7 @@ export const PRODUCT_USAGE_FLOWS = [...PRODUCT_USAGE_SETUP_FLOWS,...HEALTH_FLOWS, ...PRODUCT_USAGE_ONBOARDING_FLOWS] as constexport const PRODUCT_USAGE_FLOW_MEASURES = ['started', 'ok', 'failed', 'cancelled', 'unknown','under_1s', '1s_to_10s', '10s_to_1m', 'over_1m'] as const-export const PRODUCT_USAGE_UI_METRICS = PRODUCT_USAGE_SURFACES.flatMap(surface =>+export const PRODUCT_USAGE_UI_METRICS = PRODUCT_USAGE_UI_SURFACES.flatMap(surface =>PRODUCT_USAGE_FLOWS.flatMap(flow => PRODUCT_USAGE_FLOW_MEASURES.map(measure => `ui_${surface}_${flow}_${measure}` as const)))// A signed-out report is explicitly enabled on this app. It cannot carry account/work metrics.export const PRODUCT_USAGE_SETUP_METRICS = [...PRODUCT_USAGE_SURFACES.flatMap(surface =>diff --git a/cli/src/lib/productUsageSignals.spec.ts b/cli/src/lib/productUsageSignals.spec.tsindex b0431aa1c..6f2af2315 100644--- a/cli/src/lib/productUsageSignals.spec.ts+++ b/cli/src/lib/productUsageSignals.spec.ts@@ -1,6 +1,7 @@import { expect, it, vi } from 'vitest'-import { productUsageIntent, productUsageLaunch, productUsageRequest } from './productUsageSignals.js'-import { PRODUCT_USAGE_METRICS } from './productUsageContract.js'+import { productObservation, productUsageIntent, productUsageLaunch, productUsageRequest } from './productUsageSignals.js'+import { PRODUCT_USAGE_FLOWS, PRODUCT_USAGE_METRICS, PRODUCT_USAGE_SETUP_METRICS } from './productUsageContract.js'+import { HEALTH_MEASURES } from './productHealthContract.js'it('separates automated polling, tool and app RPCs without inspecting error objects or content', () => {const emit = vi.fn()@@ -115,3 +116,41 @@ it('never coerces private reply values and leaves successful resumes without a dexpect(emit.mock.calls.flat()).toContain('request_app_resume_ok')expect(emit.mock.calls.flat().some(name => name.includes('_reason_'))).toBe(false)})++it('names every flow and health measure for hn, and no signed-out setup counter', () => {+ const metrics = new Set<string>(PRODUCT_USAGE_METRICS)+ for (const flow of PRODUCT_USAGE_FLOWS) for (const measure of ['started', 'ok', 'failed', 'cancelled', 'unknown', 'under_1s', 'over_1m']) {+ expect(metrics.has(`ui_tui_${flow}_${measure}`), `${flow}_${measure}`).toBe(true)+ }+ for (const measure of HEALTH_MEASURES) expect(metrics.has(`health_tui_${measure}`), measure).toBe(true)+ expect(PRODUCT_USAGE_SETUP_METRICS.some(metric => metric.includes('_tui_'))).toBe(false)+})++it('reduces hn\'s observations to the Desktop\'s flow and milestone counters, and nothing else', () => {+ // Started when it begins, as the Desktop's `begin()` counts it; the outcome alone when it ends.+ expect(productObservation({ flow: 'computer_link' })).toEqual(['ui_tui_computer_link_started'])+ expect(productObservation({ flow: 'computer_link', outcome: 'ok', durationMs: 999 }))+ .toEqual(['ui_tui_computer_link_ok', 'ui_tui_computer_link_under_1s'])+ expect(productObservation({ durationMs: 1000, outcome: 'failed', flow: 'remote_connect' }))+ .toEqual(['ui_tui_remote_connect_failed', 'ui_tui_remote_connect_1s_to_10s'])+ expect(productObservation({ flow: 'resume_recovery', outcome: 'cancelled', durationMs: 59_999 })[1]).toBe('ui_tui_resume_recovery_10s_to_1m')+ expect(productObservation({ flow: 'app_start', outcome: 'unknown', durationMs: 60_000 })[1]).toBe('ui_tui_app_start_over_1m')+ expect(productObservation({ measure: 'funnel_first_work' })).toEqual(['health_tui_funnel_first_work'])+ expect(productObservation({ measure: 'first_response_1m_to_5m' })).toEqual(['health_tui_first_response_1m_to_5m'])+ const invalid = [null, undefined, 'sign_in', [], {}, { flow: 'PRIVATE_CANARY', outcome: 'ok', durationMs: 1 },+ { flow: 'sign_in', outcome: 'started', durationMs: 1 }, { flow: 'sign_in', outcome: 'PRIVATE_CANARY', durationMs: 1 },+ ...[-1, NaN, Infinity, '1', null].map(durationMs => ({ flow: 'sign_in', outcome: 'ok', durationMs })),+ { flow: 'sign_in', outcome: 'ok' }, { flow: 'sign_in', outcome: 'ok', durationMs: 1, path: 'PRIVATE_CANARY' },+ { flow: 'sign_in', durationMs: 1 }, { flow: 'sign_in', path: 'PRIVATE_CANARY' }, { flow: 'PRIVATE_CANARY' }, { flow: 1 },+ { measure: 'PRIVATE_CANARY' }, { measure: 'tui_opens' }, { measure: 'funnel_first_work', agentId: 'PRIVATE_CANARY' },+ { measure: 'funnel_first_work', flow: 'sign_in', outcome: 'ok', durationMs: 1 }, { flow: 'toString', outcome: 'ok', durationMs: 1 },+ { measure: 'constructor' }]+ for (const value of invalid) expect(productObservation(value), JSON.stringify(value)).toEqual([])+ const all = new Set<string>(PRODUCT_USAGE_METRICS)+ for (const flow of PRODUCT_USAGE_FLOWS) {+ for (const metric of productObservation({ flow })) expect(all.has(metric), metric).toBe(true)+ for (const outcome of ['ok', 'failed', 'cancelled', 'unknown']) {+ for (const metric of productObservation({ flow, outcome, durationMs: 0 })) expect(all.has(metric), metric).toBe(true)+ }+ }+})diff --git a/cli/src/lib/productUsageSignals.ts b/cli/src/lib/productUsageSignals.tsindex 19d3d7316..cb194e9c1 100644--- a/cli/src/lib/productUsageSignals.ts+++ b/cli/src/lib/productUsageSignals.ts@@ -1,5 +1,5 @@-import type { HealthSource, ResumeReason } from './productHealthContract.js'-import { PRODUCT_USAGE_USER_OPENED_ACTIONS, type ProductUsageMetric } from './productUsageContract.js'+import { HEALTH_MEASURES, type HealthSource, type ResumeReason } from './productHealthContract.js'+import { PRODUCT_USAGE_FLOWS, PRODUCT_USAGE_USER_OPENED_ACTIONS, type ProductUsageMetric } from './productUsageContract.js'const actions = { agent_create: 'create', agent_resume: 'resume', agent_restart: 'restart',agent_retarget: 'model_change', agent_recent: 'history',@@ -112,3 +112,29 @@ export function productUsageIntent(type: string, intent: unknown, agent: unknownelse if (source === 'tool') emit({ id: agent, phase: 'work_requested', origin: 'tool' })} catch { /* Optional observation cannot block a message. */ }}++const OBSERVED_OUTCOMES = ['ok', 'failed', 'cancelled', 'unknown']+/**+ * hn's own observation (`product_observe`), reduced to the counters the Desktop's `begin()` and+ * `milestone()` count, each when it happens: `{flow}` is a flow's start, `{flow, outcome, durationMs}`+ * its outcome and duration bucket, `{measure}` one health measure. Anything else — an unknown name,+ * a bad duration, a field more or less than its shape has — is nothing, so no label, text or number+ * of hn's own becomes a counter.+ */+export function productObservation(payload: unknown): ProductUsageMetric[] {+ if (!payload || typeof payload !== 'object' || Array.isArray(payload)) return []+ const p = payload as Record<string, unknown>+ const fields = Object.keys(p).sort().join()+ if (fields === 'measure') {+ return typeof p.measure === 'string' && (HEALTH_MEASURES as readonly string[]).includes(p.measure)+ ? [`health_tui_${p.measure}` as ProductUsageMetric] : []+ }+ if (typeof p.flow !== 'string' || !(PRODUCT_USAGE_FLOWS as readonly string[]).includes(p.flow)) return []+ const prefix = `ui_tui_${p.flow}_`+ if (fields === 'flow') return [`${prefix}started` as ProductUsageMetric]+ if (fields !== 'durationMs,flow,outcome' || typeof p.outcome !== 'string' || !OBSERVED_OUTCOMES.includes(p.outcome)+ || typeof p.durationMs !== 'number' || !Number.isFinite(p.durationMs) || p.durationMs < 0) return []+ const ms = p.durationMs+ return [`${prefix}${p.outcome}`,+ `${prefix}${ms < 1000 ? 'under_1s' : ms < 10_000 ? '1s_to_10s' : ms < 60_000 ? '10s_to_1m' : 'over_1m'}`] as ProductUsageMetric[]+}diff --git a/cli/src/localWsServer.spec.ts b/cli/src/localWsServer.spec.tsindex fdccad422..2d12dc3b0 100644--- a/cli/src/localWsServer.spec.ts+++ b/cli/src/localWsServer.spec.ts@@ -43,6 +43,10 @@ class FakeBackend implements LocalWsBackend {setLocalTerminalFocus(connId: string, agentId: string | null): void {this.focuses.push([connId, agentId])}+ observed: Array<[string, unknown]> = []+ observeProduct(connId: string, payload: unknown): void {+ this.observed.push([connId, payload])+ }}function onceOpen(ws: WebSocket): Promise<void> {@@ -995,6 +999,37 @@ describe('local CLI WebSocket', () => {}})+ it('takes hn\'s product_observe over the owner\'s socket from a window here, and never passes it on', async () => {+ const backend = new FakeBackend(), relayed: Frame[] = []+ const relayPool = {+ acquire: async (_id: string, _env: string, _select: Frame, sink: LocalClientSink) => {+ sink.sendFrame({ type: 'connected', payload: { machineId: 'remote', e2ee: false } })+ return { send: async (frame: Frame) => { relayed.push(frame) }, sendBinary: async () => {}, detach: () => {} }+ },+ acquireIsolated: async () => { throw new Error('unused') }, invalidate: () => {},+ }+ const w = await unixWorld(backend, { autonomousEnv: 'test', relayPool: relayPool as unknown as NonNullable<LocalWsServerOptions['relayPool']> })+ try {+ const observe = { type: 'product_observe', payload: { flow: 'viewer_open', outcome: 'ok', durationMs: 12 } }+ const hn = await w.open(w.unix, { machineId, client: 'tui' })+ const hnId = backend.connId+ const others = [await w.open(w.tcp, { machineId, client: 'tui' }), await w.open(w.unix, { machineId, tool: true }),+ await w.open(w.unix, { machineId: 'remote', client: 'tui' })]+ // Each connection's frames are handled in order: its marker arriving means its observation was handled.+ for (const [i, ws] of [hn, ...others].entries()) {+ ws.send(JSON.stringify(observe))+ ws.send(JSON.stringify({ type: 'agents_list', payload: { requestId: `after-${i}` } }))+ }+ await vi.waitFor(() => expect([...backend.frames, ...relayed].map(f => (f.payload as { requestId?: string }).requestId).sort())+ .toEqual(['after-0', 'after-1', 'after-2', 'after-3']))+ expect(backend.observed).toEqual([[hnId, observe.payload]])+ expect([...backend.frames, ...relayed].some(f => f.type === 'product_observe')).toBe(false)+ for (const ws of [hn, ...others]) ws.close()+ } finally {+ await w.cleanup()+ }+ })+it('carries the window\'s answer about a spoken task, and keeps it off the wire', async () => {const backend = new FakeBackend()const replies: Array<{ voiceId: string; reply: unknown }> = []diff --git a/cli/src/localWsServer.ts b/cli/src/localWsServer.tsindex 1fc76b680..83c9224b3 100644--- a/cli/src/localWsServer.ts+++ b/cli/src/localWsServer.ts@@ -42,6 +42,8 @@ export interface LocalWsBackend {handleLocalBinary: (connId: string, frame: TerminalBinaryClear) => Promise<void>/** The agent this window has focused, or null — which of its terminals gets the short output window. */setLocalTerminalFocus?: (connId: string, agentId: string | null) => void+ /** hn's `product_observe`, from a connection on this computer's own socket bound to this machine. */+ observeProduct?: (connId: string, payload: unknown) => void}export interface LocalWsServerOptions {@@ -512,6 +514,15 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO}return}+ // hn's own measurement, for this computer's product-usage queue alone: one-way, never answered,+ // never relayed to another machine or handed to the backend socket's dispatch. Over the owner's+ // socket only, from a window (not a tool) bound to this machine; the backend checks it is hn's.+ if (parsed?.type === 'product_observe') {+ if (trusted && !tool && !relay && boundMachineId === options.machineId && windowSinks.has(connId)) {+ options.backend.observeProduct?.(connId, parsed.payload)+ }+ return+ }// THE APP MOVED — tell whoever wants to follow it, before the frame is dispatched either way.// Sniffed here rather than in the backend socket because that path never sees a RELAYED machine's// frames: those are forwarded upstream a few lines below and would be invisible, which is exactlydiff --git a/docs/plans/2026-10-15-tui-product-measurement.md b/docs/plans/2026-10-15-tui-product-measurement.mdnew file mode 100644index 000000000..7e3407494--- /dev/null+++ b/docs/plans/2026-10-15-tui-product-measurement.md@@ -0,0 +1,80 @@+# hn product measurement, like the Desktop's++Requested 2026-10-15. Scope: the hn TUI, the CLI/daemon that counts for it, and the backend vocabulary.+Desktop, phone and web keep their instrumentation. Same policy as+[product-measurement.md](../product-measurement.md): fixed counters per UTC day, sent only to+Harness's backend, governed by the account's product-usage choice. No identity, content, paths,+event history, join keys or arbitrary properties.++## What hn already contributes (unchanged, never counted again by hn)++The daemon measures every request at its boundary (`backendSocket.dispatchDown`): create, resume,+restart, model change, fork, close, delete, search, Store, model list/download/start/stop, messages.+It counts `tui_opens` and active days from `machine_select {client:'tui'}`.++## Added++1. **Vocabulary.** `tui` joins the UI surfaces (`ui_tui_<flow>_<measure>`) and the health surfaces+ (`health_tui_<measure>`), in the CLI contract and its backend copy, kept identical.+2. **Transport.** hn sends a one-way `product_observe` frame on its existing local link. The daemon+ accepts it only from a trusted local client that selected the machine as `tui` (never relayed,+ never a tool), validates it against the fixed vocabulary, and adds counters to its own+ product-usage queue — the one that already persists to disk, honours the account choice and+ retries. Nothing is counted while the daemon has no product-usage queue (signed out). Frames that+ arrive before its first choice read wait in RAM (64 at most): counted if sharing is on, dropped if off.+ Each shape is exact (a missing or extra field is nothing), and each counter is counted when it+ happens, as the Desktop's `begin()` and its outcome:+ - `{flow}` → `ui_tui_<flow>_started`, sent when the flow begins;+ - `{flow, outcome, durationMs}` → `ui_tui_<flow>_<outcome>` and the duration bucket (`under_1s`,+ `1s_to_10s`, `10s_to_1m`, `over_1m`), sent when it ends;+ - `{measure}` → `health_tui_<measure>`, health measures only.+3. **Isolation from an older backend.** The backend validates counter names strictly, and a 400 makes+ the daemon drop the report. `ui_tui_*` and `health_tui_*` counters therefore always go in reports+ of their own, so a backend that predates `tui` refuses only those, never the Desktop's or the+ daemon's counts. That refusal is not counted as a failed delivery: the daemon drops hn's counts+ until the next UTC day (or its restart) instead of retrying them or letting them age into late+ reports.+4. **hn observations**, with the Desktop's definitions (`product_measurement.dart`,+ `product_runtime_health.dart`). Signed in only, with no signed-out opt-in and no hn privacy screen:+ anything observed while signed out is dropped when it happens, never held for later, and a+ flow begun under one account counts no outcome under the next. A sign-in, sign-out or account+ change (the daemon answering for another account, even with no sign-out seen) discards what is+ waiting, as the Desktop's account change does.+ - flows: `workspace_connect` (this computer's daemon link), `remote_connect` (another machine's+ link status, as the Desktop measures it; not the Connect-a-machine prompt, which ends in+ `computer_link` and would count twice), `computer_link`, `viewer_open` (the viewer opened in a+ browser, or its link shown over SSH; failed when it could not open or with the harness's launch+ error; not a copied link or `hn view -p`; a browser that failed to open stays `failed` even+ though its link is then shown, while over SSH showing the link is `ok`), `workspace_ready` (sign-in to a machine connected),+ `app_start` (launch to that same moment), `resume_recovery` (ok at the harness's next live turn;+ failed when the daemon refuses; cancelled by another resume; unknown after 15 minutes). Any flow+ open 15 minutes is unknown. hn's first-run OpenCode task is not the Desktop's `setup` and is+ not measured.+ - milestones, once per app session (sign-in) as on the Desktop: `funnel_workspace_ready`,+ `funnel_first_work` (the first live `turn_started` of any kind), `funnel_first_response` (the+ first text of a live turn), with the first-work/first-response buckets timed from the session's+ first milestone. Nothing is kept on disk.+ - Not observed: the sign-in itself and `funnel_sign_in_seen` (both happen signed out; the Desktop+ counts them only with its signed-out opt-in), updates (the daemon replaces hn's binary), pane+ layout, keys, commands.+5. **Hints on requests hn already sends**, as the Desktop does: `usageIntent: 'interactive'` on a+ message the person typed and submitted in hn's own Message, Send or Broadcast box (never the same+ text from `send-task`, `broadcast` or a script), and `userAction: true` on the Store and+ model-list requests a person opens. Automatic reads stay unmarked.++## Limits++- Milestones are not reset when the account's sharing choice changes, only at a sign-in, sign-out or+ account change (hn does not see the choice; the daemon applies it).+- hn's queue (64) and the daemon's pre-choice buffer (64) are RAM only; exiting loses them. When+ hn's queue is full its last place says so: `health_tui_observation_overflow`, once per session.++## Rollout++The backend change must be deployed for `tui` counters to be accepted; until then they are refused+alone (point 3). An older daemon ignores `product_observe`; an older hn sends none.++## Validation++CLI/backend typecheck and affected specs (contract conformance, the daemon frame, report isolation),+hn `cargo test --locked` and the native fixtures, PR CI, independent review.diff --git a/docs/product-measurement.md b/docs/product-measurement.mdindex 313dd6317..cdda7c1fa 100644--- a/docs/product-measurement.md+++ b/docs/product-measurement.md@@ -113,6 +113,21 @@ for all three phases. Counts expire afteryesterday; app exit loses RAM counts. Update-stage outcomes may be lost during immediate exit;never delay an update to flush analytics. There is no persistent app identifier or local event log.+hn reports as the `tui` surface: fixed `ui_tui_*` flow and `health_tui_*` milestone counters only,+sent as one-way `product_observe` frames to this computer's daemon, which accepts them only from hn+over the owner's socket, never relays them, and adds them to its own queue under the account's+choice; those arriving before its first choice read wait in RAM (64 at most) until it is known.+hn records only while signed in: nothing observed before sign-in is kept or sent, and a sign-in,+sign-out or account change discards what is waiting, so its own sign-in is not observed. Flows are+workspace and remote connections, computer linking, viewer opening (in a browser, or its link shown+over SSH), app start, workspace ready and resume recovery, each started when it begins as on the+Desktop; milestones are workspace ready, first work and first response, once per app session, with+the Desktop's definitions. Milestones are not reset when the sharing choice changes within a session.+At most 64 observations wait in hn's RAM (a full queue drops the rest and says so once per session,+`health_tui_observation_overflow`); nothing is written to disk. Its counters travel in reports of+their own. A backend that predates `tui` refuses only those, and the daemon then drops hn's counts+until the next UTC day or its own restart, without counting a failed delivery.+The signed-out endpoint permits only setup counters and explicit consent, rejects auth/cookieheaders, limits bodies to 4 KiB and values to 1,000, and admits at most 120 writes/minute and fourconcurrent writes per server process. No IP-based analytics or new rate-limit identity is stored.diff --git a/tui/Cargo.lock b/tui/Cargo.lockindex 29e8d1e22..a061bf828 100644--- a/tui/Cargo.lock+++ b/tui/Cargo.lock@@ -654,7 +654,7 @@ dependencies = [[[package]]name = "harness-tui"-version = "0.1.31"+version = "0.1.32"dependencies = ["alacritty_terminal","base64",diff --git a/tui/Cargo.toml b/tui/Cargo.tomlindex a3ef857cf..ad593b2f5 100644--- a/tui/Cargo.toml+++ b/tui/Cargo.toml@@ -1,6 +1,6 @@[package]name = "harness-tui"-version = "0.1.31"+version = "0.1.32"edition = "2024"[dependencies]diff --git a/tui/src/account_scope.rs b/tui/src/account_scope.rsindex 9d06819d7..f058062e6 100644--- a/tui/src/account_scope.rs+++ b/tui/src/account_scope.rs@@ -153,6 +153,8 @@ impl App {let next = Identity { machine:machine.into(), local_only };let Some(old) = self.daemon_identity.replace(next) else { return };self.account_epoch = self.account_epoch.wrapping_add(1);+ // Another account, even with no sign-out between: none of the last one's measurement is its.+ crate::measure::account_changed(self);self.removed_machines.clear();let migrate = (old.local_only || local_only || status["computerId"].as_str() == Some(old.machine.as_str())).then_some(old.machine.as_str());diff --git a/tui/src/app.rs b/tui/src/app.rsindex 45dd7cf95..c85a6168f 100644--- a/tui/src/app.rs+++ b/tui/src/app.rs@@ -903,6 +903,8 @@ pub struct App {pub controls: crate::workspace_controls::State,pub agent_switch: crate::agent_switch::State,pub hardware: crate::hardware::State,+ /// hn's product measurement, waiting for this computer's daemon (measure.rs).+ pub measure: crate::measure::State,}impl App {@@ -1132,6 +1134,7 @@ impl App {controls: Default::default(),agent_switch: Default::default(),hardware: Default::default(),+ measure: Default::default(),}}@@ -1371,6 +1374,7 @@ impl App {let attempts = self.links.get(machine_id).map(|s| s.attempts).unwrap_or(0);self.links.insert(machine_id.to_string(), LinkState { link: Some(link), generation: self.generation, attempts, retry_at: None });if let Some(machine) = self.fleet.machine_mut(machine_id) { machine.reach = Reach::Connecting }+ crate::measure::dialling(self, machine_id);}/// This computer's daemon can come back under another machine id: `harness login` restarts it@@ -1387,6 +1391,7 @@ impl App {/// what was read in them. Its link and harness rows go; the daemon lists them again under `id`.fn move_machine(&mut self, old: &str, id: &str) {if let Some(state) = self.links.remove(old) { if let Some(link) = state.link { link.close() } }+ crate::measure::forget(self, old);self.rtt.remove(old);self.homes.remove(old);self.seen_rostered.remove(old);@@ -1441,6 +1446,12 @@ impl App {// before the replacement connection has a new generation.let current = self.links.get(&machine_id).is_some_and(|s| s.generation == generation && s.link.is_some());if !current { return }+ // A dial's answer (a link closing after it connected is not one).+ match &event {+ MachineEvent::Connected => crate::measure::dialled(self, &machine_id, true),+ MachineEvent::Failed(_) => crate::measure::dialled(self, &machine_id, false),+ _ => {}+ }match event {MachineEvent::Connected => {if let Some(state) = self.links.get_mut(&machine_id) { state.attempts = 0 }@@ -1594,6 +1605,15 @@ impl App {self.relist(machine_id);}"turn_started" | "turn_heartbeat" | "tool_start" | "tool_end" | "text_delta" | "thinking_title" => {+ // A live turn and its text (not a re-read): first work, first response and a resume's+ // outcome, as the desktop app reads them (measure.rs). Text is looked at only while a+ // first response is still being watched for: nothing per delta otherwise.+ let measured = match ty { "turn_started" => true, "text_delta" => crate::measure::watching(self), _ => false };+ if measured && payload["replay"] != true {+ if let Some(agent) = payload.get("agentId").and_then(Value::as_str) {+ if ty == "turn_started" { crate::measure::turn_started(self, machine_id, agent) } else { crate::measure::text(self, machine_id, agent) }+ }+ }if let Some(agent) = self.fleet.event_agent(machine_id, &payload) {let now = fleet::now_ms();// A turn begun (or found running): the state's clock starts, what it says anew.@@ -1637,6 +1657,8 @@ impl App {}}"turn_ended" => {+ // Its turn is no longer live: later text is not the first response (measure.rs).+ if let Some(agent) = payload.get("agentId").and_then(Value::as_str) { crate::measure::turn_ended(self, machine_id, agent) }let visible = self.visible_agents();let looking = self.focused().filter(|_| self.terminal_focused).and_then(|f| self.panes.get(&f)).map(|p| (p.machine_id.clone(), p.agent_id.clone()));let opened: Vec<(String, String)> = self.panes.values().map(|p| (p.machine_id.clone(), p.agent_id.clone())).collect();@@ -2132,10 +2154,19 @@ impl App {let epoch = self.account_epoch;let generation = link.generation;let expected_agent = agent_id.clone();+ // resume_recovery is ok at the harness's next live turn (measure.rs), as the desktop app counts it.+ crate::measure::resuming(self, &machine_id, &agent_id);self.spawn(async move { link.rpc("agent_resume", json!({ "agentId": agent_id }), Duration::from_secs(120)).await }, move |app, reply| {if let Some(visit) = visit { crate::shell_context::resumed(app, &visit); }if app.account_epoch != epoch || app.connection_generation(&machine_id) != Some(generation)|| app.panes.get(&pane_id).is_none_or(|p| p.agent_id != expected_agent || p.machine_id != machine_id || p.open_token != token) { return }+ // Failed only when the daemon refused it, as the desktop app reads a refusal. Its internal+ // errors, no answer in time and a lost connection leave the outcome to the next turn.+ let refused = match &reply {+ Err(e) => !matches!(e.code.as_str(), "INTERNAL" | "CREATION_STORAGE_FAILED" | "RESTART_FAILED" | "TIMEOUT" | "DISCONNECTED"),+ Ok(r) => r["state"] == "failed" && r.pointer("/failure/code").is_some_and(Value::is_string),+ };+ if refused { crate::measure::resume_failed(app, &machine_id, &expected_agent) }if let Some(pane) = app.panes.get_mut(&pane_id) { pane.opening = false; }match reply {Ok(_) => { app.relist(&machine_id); app.open_stream(pane_id, true) }@@ -5675,6 +5706,7 @@ impl App {/// other harnesses go with the account.pub(crate) fn follow_account(&mut self, signed_in: bool) {let was = std::mem::replace(&mut self.signed_in, signed_in);+ if was != signed_in { crate::measure::account_changed(self) }if self.desk_mode != DeskMode::Account || (was == signed_in && self.session_desk == signed_in) { return }if signed_in { self.enter_desk() } else { self.leave_desk() }}@@ -6387,6 +6419,7 @@ impl App {crate::shell_context::tick(self);crate::agent_switch::tick(self);crate::hardware::tick(self);+ crate::measure::tick(self);self.maybe_start_shell();self.release_waiting();self.release_cli();@@ -6779,6 +6812,30 @@ mod recovery_tests {app}+ /// Another account on this computer with no sign-out between (hn sees only the daemon's new+ /// identity): none of the first account's measurement — its flows under way, its milestones,+ /// what waits to be sent — is the second's (measure.rs).+ #[tokio::test]+ async fn an_account_switch_without_a_sign_out_ends_the_last_accounts_measurement() {+ let (sink, _) = tokio::sync::mpsc::unbounded_channel();+ let mut app = App::new(19813, sink, (80, 24));+ app.adopt_daemon_identity(&json!({"machineId":"account-a","computerId":"computer","signedIn":true}));+ app.follow_account(true);+ let flow = crate::measure::begin(&mut app, "computer_link");+ crate::measure::turn_started(&mut app, "account-a", "a1");+ app.adopt_daemon_identity(&json!({"machineId":"account-b","computerId":"computer","signedIn":true}));+ app.follow_account(true);+ flow.finish(&mut app, crate::measure::Outcome::Ok);+ app.fleet.local_id = "account-b".into();+ app.fleet.machines.push(Machine { shared: false, id: "account-b".into(), name: "b".into(), local: true, status: "running".into(), reach: Reach::Ready });+ crate::measure::turn_started(&mut app, "account-b", "b1");+ crate::measure::tick(&mut app);+ let sent = &app.measure.sent;+ assert!(!sent.iter().any(|v| v["flow"] == "computer_link"), "A's flow, start or outcome: {sent:?}");+ assert_eq!(sent.iter().filter(|v| v["measure"] == "funnel_first_work").count(), 1, "B's own first work only: {sent:?}");+ assert_eq!(sent.iter().filter(|v| **v == json!({ "flow": "workspace_ready" })).count(), 1, "B's own workspace_ready: {sent:?}");+ }+/// `harness login` restarts this computer's daemon under the account's machine id. hn follows/// it: the panes reopen on the new id, the old one is gone, and `daemon down` clears once the/// new link connects.diff --git a/tui/src/devices.rs b/tui/src/devices.rsindex d19585c01..6ef7d1b8c 100644--- a/tui/src/devices.rs+++ b/tui/src/devices.rs@@ -773,6 +773,7 @@ pub fn link_connect(app: &mut App, machine: String, name: String, password: Striif !name.is_empty() && name != machine { args.push(format!("--name={name}")) }note(app, format!("Linking {name}… it continues if you close this"));again(app);+ let measured = crate::measure::begin(app, "computer_link");ask(app, Req::Cli { args, stdin: Some(password) }, Some(link_line), move |app, reply| {let result = match &reply {Reply::Cli { out, err, .. } => match last_json(out) {@@ -783,6 +784,7 @@ pub fn link_connect(app: &mut App, machine: String, name: String, password: Stri},_ => Err("harness link connect failed".into()),};+ measured.finish(app, if result.is_ok() { crate::measure::Outcome::Ok } else { crate::measure::Outcome::Failed });match &result {Ok(_) => {note(app, format!("Linked {name}"));diff --git a/tui/src/dial.rs b/tui/src/dial.rsindex 7283029e1..d3de13d12 100644--- a/tui/src/dial.rs+++ b/tui/src/dial.rs@@ -301,7 +301,7 @@ fn voice(app: &mut App, p: &Value) {picker.heading = Some(picker.title.clone());app.toast = None;app.dial.voice = Some(id.clone());- app.modal = Some(Modal::Picker { kind: PickerKind::Route { text, voice: Some(id) }, picker });+ app.modal = Some(Modal::Picker { kind: PickerKind::Route { text, voice: Some(id), typed: false }, picker });if !app.terminal_focused { crate::bell() }});}diff --git a/tui/src/input.rs b/tui/src/input.rsindex 64a5d2dcb..d6725115a 100644--- a/tui/src/input.rs+++ b/tui/src/input.rs@@ -1036,7 +1036,8 @@ pub fn launch(app: &mut App, prefix: &str, filter: Filter) {fn prepare(app: &mut App, kind: &PickerKind) {match kind {PickerKind::Machines => app.refresh_machines(),- PickerKind::Store => load_dsh(app, app.fleet.local_id.clone()),+ // A person opened the Store: its read says so (`userAction`), as the desktop's Store does.+ PickerKind::Store => load_dsh(app, app.fleet.local_id.clone(), true),PickerKind::Models => load_models(app),_ => {}}@@ -1258,12 +1259,15 @@ fn agent_rpc_on(app: &mut App, machine: String, agent: String, ty: &'static str,});}-pub(crate) fn load_dsh(app: &mut App, machine: String) {+/// The Store's catalog from [machine]. [user_action]: a person opened the Store, the one read that+/// counts toward its reach; a refresh after an install or a form's own read is automatic.+pub(crate) fn load_dsh(app: &mut App, machine: String, user_action: bool) {let Some(link) = app.link(&machine) else { return };let id = machine.clone();let mark = loading(format!("dsh {machine}"), true);+ let request = dsh_list_request(user_action);app.spawn(async move {- let dsh = link.rpc("dsh_list", json!({}), Duration::from_secs(20)).await;+ let dsh = link.rpc("dsh_list", request, Duration::from_secs(20)).await;let home = link.rpc("fs_list_dir", json!({}), Duration::from_secs(20)).await;(dsh, home)}, move |app, (dsh, home)| {@@ -1790,7 +1794,7 @@ fn prompt_mouse(app: &mut App, mouse: MouseEvent) {let Some(Modal::Prompt(mut p)) = app.modal.take() else { return };if !r.contains(at) { prompt_cancelled(app, &p); return after_prompt(app) }match p.row().click(a.row, at) {- Some(1) => submit_prompt(app, p),+ Some(1) => submit_prompt(app, p, true),Some(_) => prompt_cancelled(app, &p),None => {if a.input.is_some_and(|i| i.contains(at)) { p.buttons = false }@@ -1817,7 +1821,7 @@ fn prompt_key(app: &mut App, key: KeyEvent, mut p: Prompt) {if let PromptKind::Command { digits: true, .. } = &p.kind {let digit = matches!(key.code, KeyCode::Char(c) if c.is_ascii_digit()) && !ctrl && !alt;if !digit {- submit_prompt(app, p);+ submit_prompt(app, p, true);app.sync_copy_modal();return on_key(app, key);}@@ -1840,7 +1844,7 @@ fn prompt_key(app: &mut App, key: KeyEvent, mut p: Prompt) {if p.buttons {let mut row = p.row();match row.key(key.code, key.modifiers) {- crate::buttons::Answer::Chosen(1) => return submit_prompt(app, p),+ crate::buttons::Answer::Chosen(1) => return submit_prompt(app, p, true),crate::buttons::Answer::Chosen(_) | crate::buttons::Answer::Cancel => return prompt_cancelled(app, &p),crate::buttons::Answer::Moved => p.chosen = row.chosen,crate::buttons::Answer::Ignored => {}@@ -1882,7 +1886,7 @@ fn prompt_key(app: &mut App, key: KeyEvent, mut p: Prompt) {if !p.value.is_empty() && matches!(p.kind, PromptKind::Command { .. } | PromptKind::Tree { .. }) { add_history(app, ptype, &p.value) }// An incremental prompt has done its work as it went.if incremental { return }- submit_prompt(app, p);+ submit_prompt(app, p, true);return;}// BSpace and C-h: the character before the cursor (none, and the prompt stays, at its@@ -1961,7 +1965,7 @@ fn prompt_key(app: &mut App, key: KeyEvent, mut p: Prompt) {// command-prompt -1: the first character typed is the answer.if single && appended {if p.value.chars().count() != 1 { return }- submit_prompt(app, p);+ submit_prompt(app, p, true);return;}if changed && incremental { prompt_changed(app, &p, prefix) }@@ -3250,11 +3254,10 @@ fn choose(app: &mut App, kind: PickerKind, mut picker: Picker, choice: Choice) {let label = row.get("name").and_then(|v| v.as_str()).unwrap_or(&dsh).to_string();create(app, local, What { engine, dsh: Some(dsh), label }, None, None);}- PickerKind::Route { text, voice } => {+ PickerKind::Route { text, voice, typed } => {let Some((machine, agent)) = id.as_deref().and_then(split_key) else { return keep(app, kind, picker) };if let Some(voice) = voice { return crate::dial::send_spoken(app, &voice, &machine, &agent, &text) }- if let Some(link) = app.link(&machine) {- link.send("message", json!({ "agentId": agent, "content": text }));+ if send_message(app, &machine, &agent, &text, typed) {let name = app.fleet.agent(&machine, &agent).map(|a| a.name.clone()).unwrap_or_default();app.say(format!("Sent to {name}"), theme::ONLINE);}@@ -3284,7 +3287,7 @@ fn store_install(app: &mut App, kind: PickerKind, mut picker: Picker) {let (l2, id2) = (local.clone(), id.clone());app.spawn(async move { link.rpc("dsh_install", json!({ "id": id2 }), Duration::from_secs(600)).await }, move |app, reply| {match reply { Ok(_) => app.say(format!("Installed {id}"), theme::ONLINE), Err(e) => app.say(format!("Install failed: {e}"), theme::DANGER) }- load_dsh(app, l2);+ load_dsh(app, l2, false);});}app.modal = Some(Modal::Picker { kind, picker });@@ -3295,7 +3298,9 @@ fn prompt_cancelled(app: &mut App, p: &Prompt) {if let PromptKind::Api(draft) = &p.kind { crate::models::api_cancelled(app, draft) }}-fn submit_prompt(app: &mut App, p: Prompt) {+/// [typed]: the person submitted it (Enter, a click); false for a prompt a command fills and submits+/// (`send-task`, `broadcast`), whose text is never marked as the person's.+fn submit_prompt(app: &mut App, p: Prompt, typed: bool) {let value = p.value.trim().to_string();match p.kind {// Answered by a key press in prompt_key; nothing to submit.@@ -3328,8 +3333,7 @@ fn submit_prompt(app: &mut App, p: Prompt) {}PromptKind::Message { machine, agent } => {if value.trim().is_empty() { return }- if let Some(link) = app.link(&machine) {- link.send("message", json!({ "agentId": agent, "content": value }));+ if send_message(app, &machine, &agent, &value, typed) {let name = app.fleet.agent(&machine, &agent).map(|a| a.name.clone()).unwrap_or_default();app.say(format!("Sent to {name}"), theme::ONLINE);}@@ -3372,8 +3376,7 @@ fn submit_prompt(app: &mut App, p: Prompt) {let sure = reply.get("confidence").and_then(serde_json::Value::as_f64).unwrap_or(0.0) >= 0.85;let pick = reply.get("agentId").and_then(serde_json::Value::as_str).zip(reply.get("machineId").and_then(serde_json::Value::as_str)).map(|(a, m)| (m.to_string(), a.to_string()));if let Some((machine, agent)) = pick.filter(|(m, a)| sure && app.fleet.agent(m, a).is_some()) {- if let Some(link) = app.link(&machine) {- link.send("message", json!({ "agentId": agent, "content": value }));+ if send_message(app, &machine, &agent, &value, typed) {let name = app.fleet.agent(&machine, &agent).map(|a| a.name.clone()).unwrap_or_default();let why = reply.get("reason").and_then(serde_json::Value::as_str).map(|r| format!(" — {r}")).unwrap_or_default();app.toast = None;@@ -3388,7 +3391,7 @@ fn submit_prompt(app: &mut App, p: Prompt) {picker.hints = vec![("enter", "send")];picker.heading = Some(picker.title.clone());app.toast = None;- app.modal = Some(Modal::Picker { kind: PickerKind::Route { text: value, voice: None }, picker });+ app.modal = Some(Modal::Picker { kind: PickerKind::Route { text: value, voice: None, typed }, picker });}Err(e) => app.say(format!("Could not route it: {e}"), theme::DANGER),});@@ -3396,12 +3399,34 @@ fn submit_prompt(app: &mut App, p: Prompt) {PromptKind::Broadcast => {if value.is_empty() { return }let targets: Vec<(String, String)> = app.tab().panes().iter().filter_map(|id| app.panes.get(id)).map(|p| (p.machine_id.clone(), p.agent_id.clone())).collect();- for (machine, agent) in &targets { if let Some(link) = app.link(machine) { link.send("message", json!({ "agentId": agent, "content": value })); } }+ for (machine, agent) in &targets { send_message(app, machine, agent, &value, typed); }app.say(format!("Sent to {} harness{}", targets.len(), if targets.len() == 1 { "" } else { "es" }), theme::ONLINE);}}}+/// A message from one of hn's own boxes. [typed]: the person typed and submitted it there, so it is+/// marked as theirs (`usageIntent`), as the desktop's composer marks its own, and the daemon counts+/// it as interactive work. A command's or a script's text is never marked. False when [machine] is+/// not connected.+fn send_message(app: &mut App, machine: &str, agent: &str, text: &str, typed: bool) -> bool {+ let message = message_request(agent, text, typed);+ #[cfg(test)]+ SENT_MESSAGES.with(|sent| sent.borrow_mut().push(message.clone()));+ app.link(machine).is_some_and(|link| link.send("message", message))+}++#[cfg(test)]+thread_local! { static SENT_MESSAGES: std::cell::RefCell<Vec<Value>> = const { std::cell::RefCell::new(Vec::new()) }; }++fn message_request(agent: &str, text: &str, typed: bool) -> Value {+ if typed { json!({ "agentId": agent, "content": text, "usageIntent": "interactive" }) } else { json!({ "agentId": agent, "content": text }) }+}++fn dsh_list_request(user_action: bool) -> Value {+ if user_action { json!({ "userAction": true }) } else { json!({}) }+}+// ── what the command layer calls ─────────────────────────────────────────────@@ -3627,12 +3652,13 @@ pub fn rename_focused(app: &mut App, name: &str) {});}+/// `send-task text` and `broadcast text`: a command's text, never marked as a person's (send_message).pub fn route_task(app: &mut App, text: String) {- submit_prompt(app, Prompt::status(PromptKind::Send, "", &text));+ submit_prompt(app, Prompt::status(PromptKind::Send, "", &text), false);}pub fn broadcast(app: &mut App, text: &str) {- submit_prompt(app, Prompt::status(PromptKind::Broadcast, "", text));+ submit_prompt(app, Prompt::status(PromptKind::Broadcast, "", text), false);}/// The focused pane's harness, when it shows one.@@ -3729,6 +3755,38 @@ pub fn menu_mouse(app: &mut App, m: &crate::mouse::Event) {mod tests {use super::*;+ /// What the person typed into hn's own box is marked as theirs, as the desktop's composer marks+ /// its own; the same text from a command (`broadcast`, `send-task`) never is. The Store's read says+ /// a person opened it only when one did.+ #[tokio::test]+ async fn only_a_message_the_person_typed_is_marked_and_only_an_opened_store_is_a_persons() {+ assert_eq!(message_request("a1", "fix it", true), json!({ "agentId": "a1", "content": "fix it", "usageIntent": "interactive" }));+ assert_eq!(message_request("a1", "fix it", false), json!({ "agentId": "a1", "content": "fix it" }));+ assert_eq!(dsh_list_request(true), json!({ "userAction": true }));+ assert_eq!(dsh_list_request(false), json!({}));+ let (sink, _) = tokio::sync::mpsc::unbounded_channel();+ let mut app = App::new(19812, sink, (100, 30));+ app.fleet.machines.push(crate::fleet::Machine { shared: false, id: "m".into(), name: "m".into(), local: false, status: "online".into(), reach: crate::fleet::Reach::Ready });+ let mut tab = crate::app::Tab::with_wid("work", 1);+ tab.root = Some(crate::layout::Node::new(1, 100, 28));+ tab.focus = Some(1);+ app.panes.insert(1, crate::pane::Pane::new(1, "m", "a1", 100, 28));+ app.tabs = vec![tab];+ app.active = 0;+ assert!(!send_message(&mut app, "m", "a1", "hi", true), "not connected: nothing is sent");+ app.connect("m");+ app.fleet.machine_mut("m").unwrap().reach = crate::fleet::Reach::Ready;+ let marked = || SENT_MESSAGES.with(|sent| sent.borrow_mut().drain(..).map(|m| m.get("usageIntent").is_some()).collect::<Vec<_>>());+ marked();+ broadcast(&mut app, "from a command");+ assert_eq!(marked(), [false], "`hn broadcast text` is a command's");+ submit_prompt(&mut app, Prompt::status(PromptKind::Broadcast, "", "typed"), true);+ submit_prompt(&mut app, Prompt::status(PromptKind::Message { machine: "m".into(), agent: "a1".into() }, "", "typed"), true);+ assert_eq!(marked(), [true, true], "the person's broadcast and message boxes");+ submit_prompt(&mut app, Prompt::status(PromptKind::Message { machine: "m".into(), agent: "a1".into() }, "", "scripted"), false);+ assert_eq!(marked(), [false]);+ }+/// A new shell on this computer never starts in a folder that is gone (a cleaned /tmp, a/// deleted worktree): it starts at home. Another computer's folders are its daemon's to check.#[test]diff --git a/tui/src/main.rs b/tui/src/main.rsindex a9c22b2f2..e70400118 100644--- a/tui/src/main.rs+++ b/tui/src/main.rs@@ -41,6 +41,7 @@ mod layout;mod desk_layout;mod local;mod machine_menu;+mod measure;mod modal;mod buttons;mod dialog;@@ -449,6 +450,8 @@ async fn run(config: config::Config) -> io::Result<()> {// Its ids ($N @N %N) from this server name's counters: unique among its clients.ids::use_file(&app::sessions_path(None));let mut app = app::App::new(port, tx.clone(), size);+ // app_start runs from the process's start, as the Desktop's launch clock does.+ app.measure.since = started;app.first_session();// `hn <command>` from a shell comes in here.let socket = ipc::serve(tx.clone(), port);diff --git a/tui/src/measure.rs b/tui/src/measure.rsnew file mode 100644index 000000000..64a4219fe--- /dev/null+++ b/tui/src/measure.rs@@ -0,0 +1,455 @@+//! hn's product measurement, the Desktop's (desktop/lib/measurement/product_measurement.dart and+//! product_runtime_health.dart) in a terminal: a flow's start, outcome and coarse duration, and the+//! first-use milestones once per app session. Each is a `product_observe` frame on this computer's+//! own daemon link, which counts it in the daemon's product-usage queue under the account's choice+//! (cli/src/localWsServer.ts) and drops anything outside the fixed vocabulary. Signed in only, as the+//! Desktop's account channel: what happens while hn is signed out is never recorded, and a sign-in,+//! sign-out or account change ends what the previous one had under way. Only fixed names and a duration leave hn: no+//! machine, harness, path, text or event history, and nothing is kept on disk.++use std::collections::{HashMap, HashSet, VecDeque};+use std::time::{Duration, Instant};++use serde_json::{json, Value};++use crate::app::App;++/// Observations waiting for the daemon, at most: the Desktop keeps 64 in flight. More are dropped.+const QUEUE_CAP: usize = 64;+/// Connection and resume observations open at once, at most, as the Desktop's maps.+const OPEN_CAP: usize = 64;+/// A flow still open after this long is unknown, as the Desktop's are after 15 minutes.+const EXPIRE: Duration = Duration::from_secs(15 * 60);++pub const WORKSPACE_READY: &str = "funnel_workspace_ready";+pub const FIRST_WORK: &str = "funnel_first_work";+pub const FIRST_RESPONSE: &str = "funnel_first_response";++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum Outcome { Ok, Failed, Cancelled, Unknown }++impl Outcome {+ fn wire(self) -> &'static str {+ match self { Outcome::Ok => "ok", Outcome::Failed => "failed", Outcome::Cancelled => "cancelled", Outcome::Unknown => "unknown" }+ }+}++/// A flow under way: its name, when it began, and the sign-in it began under. Its start is counted+/// when it begins, as the Desktop's `begin()` counts it; one begun signed out counts nothing, start+/// included, and one finished under another account counts no outcome (the Desktop's account epoch).+#[derive(Debug)]+pub struct Flow { name: &'static str, at: Instant, epoch: Option<u64> }++pub fn begin(app: &mut App, flow: &'static str) -> Flow {+ let epoch = recording(app).then_some(app.measure.epoch);+ if epoch.is_some() { record(app, json!({ "flow": flow })) }+ Flow { name: flow, at: Instant::now(), epoch }+}++impl Flow {+ pub fn finish(self, app: &mut App, outcome: Outcome) {+ if self.epoch.is_some_and(|e| e == app.measure.epoch) { observed(app, self.name, outcome, self.at.elapsed()) }+ }+}++/// A flow begun and finished at once (the Desktop's `begin(flow)(outcome)`).+pub fn once(app: &mut App, flow: &'static str, outcome: Outcome) { begin(app, flow).finish(app, outcome) }++pub struct State {+ /// When hn started (main.rs sets the process's own start): app_start runs from it, as the+ /// Desktop's launch clock does.+ pub since: Instant,+ /// Bumped at every sign-in and sign-out, as the Desktop's account epoch.+ epoch: u64,+ queue: VecDeque<Value>,+ /// The queue has dropped an observation this sign-in, and said so (`observation_overflow`).+ overflowed: bool,+ /// The milestones this sign-in has counted, and when its first one was: first work and first+ /// response are timed from it (the Desktop's `_funnelSeen` / `_funnelStarted`).+ funnel_seen: HashSet<&'static str>,+ funnel_since: Option<Instant>,+ /// Harnesses with a live turn (a `turn_started` that was not a re-read), until it ends: the first+ /// text from one of them is the first response (the Desktop's `liveTurnAgents`).+ live: HashSet<(String, String)>,+ /// workspace_ready under way since this sign-in, and whether this sign-in reached it.+ workspace: Option<Flow>,+ ready_counted: bool,+ /// Connections being dialled (workspace_connect, remote_connect), by machine.+ dialling: HashMap<String, Flow>,+ /// Resumes waiting for their harness's next turn, oldest first.+ resumes: Vec<((String, String), Flow)>,+ #[cfg(test)]+ pub sent: Vec<Value>,+}++impl Default for State {+ fn default() -> State {+ State { since: Instant::now(), epoch: 0, queue: VecDeque::new(), overflowed: false, funnel_seen: HashSet::new(), funnel_since: None,+ live: HashSet::new(), workspace: None, ready_counted: false, dialling: HashMap::new(), resumes: Vec::new(),+ #[cfg(test)]+ sent: Vec::new() }+ }+}++/// Recorded at all: signed in, and a terminal (hn with no terminal is a script's server).+fn recording(app: &App) -> bool { app.signed_in && !app.headless }++/// A flow that has finished, [took] long.+fn observed(app: &mut App, flow: &'static str, outcome: Outcome, took: Duration) {+ let ms = took.as_millis().min(u64::MAX as u128) as u64;+ record(app, json!({ "flow": flow, "outcome": outcome.wire(), "durationMs": ms }));+}++/// A first-use milestone, once per sign-in of this app session. First work and first response add+/// how long after this session's first milestone they came, in the Desktop's buckets.+fn milestone(app: &mut App, measure: &'static str) {+ if !recording(app) || !app.measure.funnel_seen.insert(measure) { return }+ let since = *app.measure.funnel_since.get_or_insert_with(Instant::now);+ record(app, json!({ "measure": measure }));+ let step = match measure { FIRST_WORK => "first_work", FIRST_RESPONSE => "first_response", _ => return };+ let secs = since.elapsed().as_secs();+ let bucket = if secs < 60 { "under_1m" } else if secs < 300 { "1m_to_5m" } else { "over_5m" };+ record(app, json!({ "measure": format!("{step}_{bucket}") }));+}++/// The daemon on this computer signed in or out, or now answers for another account (app.rs+/// `follow_account`, account_scope.rs `adopt_daemon_identity`): what the previous one had (waiting,+/// under way, counted) goes with it, and a signed-in account starts its own workspace_ready.+pub fn account_changed(app: &mut App) {+ let m = &mut app.measure;+ m.epoch += 1;+ m.queue.clear();+ m.funnel_seen.clear();+ m.funnel_since = None;+ m.live.clear();+ m.workspace = None;+ m.ready_counted = false;+ m.overflowed = false;+ m.dialling.clear();+ m.resumes.clear();+ if recording(app) {+ let flow = begin(app, "workspace_ready");+ app.measure.workspace = Some(flow);+ }+}++/// A first response is still being watched for: signed in, not yet counted, a turn live.+pub fn watching(app: &App) -> bool { !app.measure.live.is_empty() && recording(app) }++fn is_live(app: &App, machine: &str, agent: &str) -> bool {+ app.measure.live.iter().any(|(m, a)| m == machine && a == agent)+}++/// A harness's live turn began: the first work, and the next turn a resume waited for.+pub fn turn_started(app: &mut App, machine: &str, agent: &str) {+ if !recording(app) { return }+ milestone(app, FIRST_WORK);+ if !app.measure.funnel_seen.contains(FIRST_RESPONSE) && !is_live(app, machine, agent) {+ app.measure.live.insert((machine.to_string(), agent.to_string()));+ }+ if let Some(i) = app.measure.resumes.iter().position(|((m, a), _)| m == machine && a == agent) {+ let (_, flow) = app.measure.resumes.remove(i);+ flow.finish(app, Outcome::Ok);+ }+}++pub fn turn_ended(app: &mut App, machine: &str, agent: &str) {+ if app.measure.live.is_empty() { return }+ app.measure.live.retain(|(m, a)| m != machine || a != agent);+}++/// Text from a harness's turn (not a re-read): the first response, when that turn is live. Called for+/// every text delta, so it costs nothing once there is no live turn to watch (signed out, or the+/// first response already counted).+pub fn text(app: &mut App, machine: &str, agent: &str) {+ if !watching(app) || !is_live(app, machine, agent) { return }+ app.measure.live.clear();+ milestone(app, FIRST_RESPONSE);+}++/// A resume asked for: ok at the harness's next live turn. Another for the same harness cancels the+/// one before; past the bound the oldest is unknown and the overflow is said.+pub fn resuming(app: &mut App, machine: &str, agent: &str) {+ if !recording(app) { return }+ let key = (machine.to_string(), agent.to_string());+ if app.measure.resumes.len() >= OPEN_CAP {+ record(app, json!({ "measure": "observation_overflow" }));+ let (_, oldest) = app.measure.resumes.remove(0);+ oldest.finish(app, Outcome::Unknown);+ }+ if let Some(i) = app.measure.resumes.iter().position(|(k, _)| *k == key) {+ let (_, before) = app.measure.resumes.remove(i);+ before.finish(app, Outcome::Cancelled);+ }+ let flow = begin(app, "resume_recovery");+ app.measure.resumes.push((key, flow));+}++/// The daemon refused the resume.+pub fn resume_failed(app: &mut App, machine: &str, agent: &str) {+ let key = (machine.to_string(), agent.to_string());+ if let Some(i) = app.measure.resumes.iter().position(|(k, _)| *k == key) {+ let (_, flow) = app.measure.resumes.remove(i);+ flow.finish(app, Outcome::Failed);+ }+}++/// hn dials [machine]: this computer's daemon (workspace_connect) or another machine (remote_connect).+/// hn's own shells are not a connection.+pub fn dialling(app: &mut App, machine: &str) {+ if !recording(app) || crate::local::is_local(machine) || app.measure.dialling.len() >= OPEN_CAP || app.measure.dialling.contains_key(machine) { return }+ let flow = begin(app, if machine == app.fleet.local_id { "workspace_connect" } else { "remote_connect" });+ app.measure.dialling.insert(machine.into(), flow);+}++/// [machine]'s link connected, or failed before it did.+pub fn dialled(app: &mut App, machine: &str, connected: bool) {+ if let Some(flow) = app.measure.dialling.remove(machine) { flow.finish(app, if connected { Outcome::Ok } else { Outcome::Failed }) }+}++/// [machine]'s link was let go without an answer (this computer took another machine id).+pub fn forget(app: &mut App, machine: &str) { app.measure.dialling.remove(machine); }++/// Every tick: the workspace becoming ready, flows open too long, and sending what waits.+pub fn tick(app: &mut App) {+ if !recording(app) { return }+ // The Desktop's `ready()`: signed in with a machine connected. app_start is the launch to here.+ if !app.measure.ready_counted && app.fleet.machines.iter().any(|m| m.usable() && !crate::local::is_local(&m.id)) {+ app.measure.ready_counted = true;+ if let Some(flow) = app.measure.workspace.take() { flow.finish(app, Outcome::Ok) }+ // Started and finished at once, as the Desktop's `observedStartup` counts it.+ let took = app.measure.since.elapsed();+ record(app, json!({ "flow": "app_start" }));+ observed(app, "app_start", Outcome::Ok, took);+ milestone(app, WORKSPACE_READY);+ }+ if app.measure.workspace.as_ref().is_some_and(|f| f.at.elapsed() >= EXPIRE) {+ if let Some(flow) = app.measure.workspace.take() { flow.finish(app, Outcome::Unknown) }+ }+ let late: Vec<String> = app.measure.dialling.iter().filter(|(_, f)| f.at.elapsed() >= EXPIRE).map(|(m, _)| m.clone()).collect();+ for machine in late { if let Some(flow) = app.measure.dialling.remove(&machine) { flow.finish(app, Outcome::Unknown) } }+ while app.measure.resumes.first().is_some_and(|(_, f)| f.at.elapsed() >= EXPIRE) {+ let (_, flow) = app.measure.resumes.remove(0);+ flow.finish(app, Outcome::Unknown);+ }+ flush(app);+}++/// The queue's last place is kept for `observation_overflow`: the first observation it has no room+/// for is dropped and said, once per sign-in, so the loss shows beside what was counted.+fn record(app: &mut App, payload: Value) {+ if !recording(app) { return }+ let m = &mut app.measure;+ if m.queue.len() + 1 < QUEUE_CAP { m.queue.push_back(payload); return }+ if !m.overflowed && m.queue.len() < QUEUE_CAP {+ m.overflowed = true;+ m.queue.push_back(json!({ "measure": "observation_overflow" }));+ }+}++/// What waits goes once this computer's daemon link is connected.+fn flush(app: &mut App) {+ let local = app.fleet.local_id.clone();+ if app.measure.queue.is_empty() || crate::local::is_local(&local) || !app.fleet.machine(&local).is_some_and(|m| m.usable()) { return }+ while let Some(payload) = app.measure.queue.pop_front() {+ if !deliver(app, &local, payload.clone()) { app.measure.queue.push_front(payload); break }+ }+}++fn deliver(app: &mut App, machine: &str, payload: Value) -> bool {+ // Under test no daemon is told: what would be sent is written down.+ #[cfg(test)]+ { let _ = machine; app.measure.sent.push(payload); true }+ #[cfg(not(test))]+ { app.link(machine).is_some_and(|link| link.send("product_observe", payload)) }+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::fleet::{Machine, Reach};++ fn fixture() -> App {+ let (sink, _) = tokio::sync::mpsc::unbounded_channel();+ let mut app = App::new(19811, sink, (80, 24));+ app.fleet.local_id = "this-computer".into();+ app.fleet.machines.push(Machine { shared: false, id: "this-computer".into(), name: "Mac".into(), local: true, status: "running".into(), reach: Reach::Connecting });+ app+ }+ fn sign_in(app: &mut App) { app.follow_account(true); }+ fn connected(app: &mut App) { app.fleet.machine_mut("this-computer").unwrap().reach = Reach::Ready; }+ fn measures(app: &App) -> Vec<&str> { app.measure.sent.iter().filter_map(|v| v["measure"].as_str()).collect() }+ fn flows(app: &App) -> Vec<(&str, &str)> { app.measure.sent.iter().filter_map(|v| Some((v["flow"].as_str()?, v["outcome"].as_str()?))).collect() }+ fn starts(app: &App) -> Vec<&str> { app.measure.sent.iter().filter(|v| v.as_object().unwrap().len() == 1).filter_map(|v| v["flow"].as_str()).collect() }+ fn outcome<'a>(app: &'a App, flow: &str) -> &'a Value { app.measure.sent.iter().find(|v| v["flow"] == flow && v.get("outcome").is_some()).unwrap() }++ #[tokio::test]+ async fn nothing_recorded_signed_out_counts_even_after_a_sign_in() {+ let mut app = fixture();+ let flow = begin(&mut app, "computer_link");+ once(&mut app, "viewer_open", Outcome::Ok);+ resuming(&mut app, "this-computer", "a");+ dialling(&mut app, "this-computer");+ turn_started(&mut app, "this-computer", "a");+ tick(&mut app);+ assert!(app.measure.queue.is_empty(), "not even a start: {:?}", app.measure.queue);+ sign_in(&mut app);+ flow.finish(&mut app, Outcome::Ok);+ dialled(&mut app, "this-computer", true);+ turn_ended(&mut app, "this-computer", "a");+ text(&mut app, "this-computer", "a");+ assert_eq!(app.measure.queue, [json!({ "flow": "workspace_ready" })], "only the sign-in's own start");+ }++ #[tokio::test]+ async fn a_flow_starts_when_it_begins_and_its_outcome_counts_only_under_the_same_account() {+ let mut app = fixture();+ sign_in(&mut app);+ connected(&mut app);+ let link = begin(&mut app, "computer_link");+ let late = begin(&mut app, "remote_connect");+ tick(&mut app);+ assert_eq!(starts(&app), ["workspace_ready", "computer_link", "remote_connect", "app_start"], "started at begin()");+ link.finish(&mut app, Outcome::Ok);+ once(&mut app, "viewer_open", Outcome::Failed);+ tick(&mut app);+ assert_eq!(outcome(&app, "computer_link")["outcome"], "ok");+ assert!(outcome(&app, "computer_link")["durationMs"].as_u64().unwrap() < 1000);+ assert!(starts(&app).contains(&"viewer_open") && flows(&app).contains(&("viewer_open", "failed")));+ assert!(app.measure.sent.iter().all(|v| v.as_object().unwrap().len() <= 3), "names, an outcome and a duration only");+ app.follow_account(false);+ late.finish(&mut app, Outcome::Ok);+ sign_in(&mut app);+ tick(&mut app);+ assert!(!flows(&app).iter().any(|(f, _)| *f == "remote_connect"), "the late flow belonged to the old sign-in");+ }++ #[tokio::test]+ async fn the_queue_is_bounded_says_its_overflow_once_and_goes_with_its_sign_in() {+ let overflow = json!({ "measure": "observation_overflow" });+ let overflows = |app: &App| app.measure.sent.iter().filter(|v| **v == overflow).count();+ let mut app = fixture();+ sign_in(&mut app);+ for _ in 0..QUEUE_CAP + 10 { once(&mut app, "remote_connect", Outcome::Failed) }+ assert_eq!(app.measure.queue.len(), QUEUE_CAP);+ assert_eq!(app.measure.queue.back(), Some(&overflow), "the last place says the loss");+ // Sent, then full again: the loss was said this session, so nothing more is.+ connected(&mut app);+ tick(&mut app);+ for _ in 0..QUEUE_CAP + 10 { once(&mut app, "remote_connect", Outcome::Failed) }+ tick(&mut app);+ assert_eq!(overflows(&app), 1);+ assert_eq!(app.measure.sent.len(), 2 * (QUEUE_CAP - 1) + 1);+ // A new sign-in is a new session: its own overflow is said again.+ app.follow_account(false);+ assert!(app.measure.queue.is_empty(), "the next sign-in is not told the last one's");+ sign_in(&mut app);+ for _ in 0..QUEUE_CAP { once(&mut app, "remote_connect", Outcome::Failed) }+ tick(&mut app);+ assert_eq!(overflows(&app), 2);+ }++ #[tokio::test]+ async fn the_workspace_is_ready_at_the_first_connection_signed_in_with_app_start_from_launch() {+ let mut app = fixture();+ app.measure.since = Instant::now() - Duration::from_secs(12);+ sign_in(&mut app);+ tick(&mut app);+ assert!(app.measure.sent.is_empty());+ connected(&mut app);+ tick(&mut app);+ tick(&mut app);+ assert_eq!(flows(&app), [("workspace_ready", "ok"), ("app_start", "ok")]);+ assert_eq!(starts(&app), ["workspace_ready", "app_start"]);+ assert!(outcome(&app, "workspace_ready")["durationMs"].as_u64().unwrap() < 12_000, "workspace_ready runs from the sign-in");+ assert!(outcome(&app, "app_start")["durationMs"].as_u64().unwrap() >= 12_000, "app_start runs from the launch");+ assert_eq!(measures(&app), [WORKSPACE_READY]);+ // Signed out and in again: a new sign-in, measured again, as the Desktop's runtime health is.+ app.follow_account(false);+ sign_in(&mut app);+ tick(&mut app);+ assert_eq!(measures(&app), [WORKSPACE_READY, WORKSPACE_READY]);+ }++ #[tokio::test]+ async fn milestones_count_once_per_session_from_live_turns_with_the_desktops_buckets() {+ let mut app = fixture();+ sign_in(&mut app);+ // The funnel's clock: its first milestone, here six minutes ago.+ app.measure.funnel_since = Some(Instant::now() - Duration::from_secs(6 * 60));+ turn_started(&mut app, "m", "a1");+ turn_started(&mut app, "m", "a2");+ turn_ended(&mut app, "m", "a1");+ text(&mut app, "m", "a1");+ assert!(watching(&app));+ text(&mut app, "m", "a2");+ text(&mut app, "m", "a2");+ turn_started(&mut app, "m", "a3");+ // Counted: nothing watches text deltas for the rest of the session.+ assert!(!watching(&app));+ connected(&mut app);+ tick(&mut app);+ assert_eq!(measures(&app), [FIRST_WORK, "first_work_over_5m", FIRST_RESPONSE, "first_response_over_5m", WORKSPACE_READY]);+ // Another sign-in, or the next hn, counts them again: once per session, not per installation.+ app.follow_account(false);+ sign_in(&mut app);+ turn_started(&mut app, "m", "a1");+ text(&mut app, "m", "a1");+ tick(&mut app);+ assert_eq!(measures(&app)[5..], [FIRST_WORK, "first_work_under_1m", FIRST_RESPONSE, "first_response_under_1m", WORKSPACE_READY]);+ }++ #[tokio::test]+ async fn a_resume_is_ok_at_its_next_turn_failed_when_refused_and_cancelled_when_asked_again() {+ let mut app = fixture();+ sign_in(&mut app);+ connected(&mut app);+ resuming(&mut app, "m", "a");+ turn_started(&mut app, "m", "other");+ turn_started(&mut app, "m", "a");+ resuming(&mut app, "m", "b");+ resume_failed(&mut app, "m", "b");+ resuming(&mut app, "m", "c");+ resuming(&mut app, "m", "c");+ app.measure.resumes[0].1.at -= EXPIRE;+ tick(&mut app);+ let resumes: Vec<&str> = flows(&app).into_iter().filter(|(f, _)| *f == "resume_recovery").map(|(_, o)| o).collect();+ assert_eq!(resumes, ["ok", "failed", "cancelled", "unknown"]);+ for i in 0..OPEN_CAP + 1 { resuming(&mut app, "m", &format!("x{i}")); tick(&mut app) }+ assert!(measures(&app).contains(&"observation_overflow"));+ assert_eq!(app.measure.resumes.len(), OPEN_CAP);+ }++ #[tokio::test]+ async fn a_dial_is_this_computers_or_another_machines_connection() {+ let mut app = fixture();+ sign_in(&mut app);+ dialling(&mut app, "this-computer");+ dialled(&mut app, "this-computer", true);+ dialling(&mut app, "peer");+ dialled(&mut app, "peer", false);+ dialling(&mut app, "peer");+ dialled(&mut app, "peer", true);+ dialled(&mut app, "peer", false);+ dialling(&mut app, crate::local::MACHINE);+ dialled(&mut app, crate::local::MACHINE, true);+ connected(&mut app);+ tick(&mut app);+ let dials: Vec<(&str, &str)> = flows(&app).into_iter().filter(|(f, _)| f.ends_with("_connect")).collect();+ assert_eq!(dials, [("workspace_connect", "ok"), ("remote_connect", "failed"), ("remote_connect", "ok")]);+ }++ #[tokio::test]+ async fn hn_with_no_terminal_observes_nothing() {+ let mut app = fixture();+ app.headless = true;+ sign_in(&mut app);+ connected(&mut app);+ once(&mut app, "viewer_open", Outcome::Ok);+ turn_started(&mut app, "this-computer", "a");+ dialling(&mut app, "this-computer");+ dialled(&mut app, "this-computer", true);+ tick(&mut app);+ assert!(app.measure.queue.is_empty() && app.measure.sent.is_empty());+ }+}diff --git a/tui/src/modal.rs b/tui/src/modal.rsindex e7c17a35d..85d9c9a5d 100644--- a/tui/src/modal.rs+++ b/tui/src/modal.rs@@ -51,8 +51,9 @@ pub enum PickerKind {Commands,/// Keybinds: the prefixes and each command's key after them — Enter on one changes it.Keybinds,- /// A task routed to a harness; [voice]: the dial's spoken task it answers.- Route { text: String, voice: Option<String> },+ /// A task routed to a harness; [voice]: the dial's spoken task it answers; [typed]: the person+ /// typed it in hn's Send box (not `send-task` from a command), so the message is marked theirs.+ Route { text: String, voice: Option<String>, typed: bool },/// `show-messages`, `list-keys`, `choose-buffer`.Messages,Keys,diff --git a/tui/src/models.rs b/tui/src/models.rsindex eed77eb79..0775e6aa5 100644--- a/tui/src/models.rs+++ b/tui/src/models.rs@@ -1525,7 +1525,7 @@ pub fn on_set_up(app: &mut App, machine: &str, reply: Result<Value, RpcError>) {crate::input::refill(app);}}- if !needs_set_up(app) { read_grids(app, machine) }+ if !needs_set_up(app) { read_grids(app, machine, false) }}/// What a Use is doing, for the footer: `Downloading 42%…`, `Starting…`, `Moving it…`.@@ -1712,11 +1712,14 @@ pub fn refresh_inventory(app: &mut App) {for machine in machines { read_local(app, &machine); }}-fn read_grids(app: &mut App, machine: &str) {+/// [user_action]: a person opened the Models view, the one read that counts toward the model list's+/// reach (`userAction`, as the desktop marks its own); its polls and a Use's reads are automatic.+fn read_grids(app: &mut App, machine: &str, user_action: bool) {app.models_view.grids_reading.insert(machine.to_string());let m = machine.to_string();// rowState: offline rows come as `unavailable`, and this window's pushes in the same form.- if !read_rpc(app, machine, "grids", "grid_models_list", json!({ "rowState": true }), Duration::from_secs(30), move |app, r| on_grids(app, &m, r)) {+ let request = if user_action { json!({ "rowState": true, "userAction": true }) } else { json!({ "rowState": true }) };+ if !read_rpc(app, machine, "grids", "grid_models_list", request, Duration::from_secs(30), move |app, r| on_grids(app, &m, r)) {app.models_view.grids_reading.remove(machine);app.models_view.grids_at.insert(machine.to_string(), Instant::now());}@@ -2043,7 +2046,7 @@ pub fn advance(app: &mut App) {Next::Stop => { step(app, Step::Stop); if let Some((other, _)) = &job.stops { act(app, other, "stop") } }Next::Start => { step(app, Step::Start); act_on(app, &job.model, "start", job.community.is_some()) }// (On the community grid, this computer's list says when it serves there: read it again.)- Next::Serve => { step(app, Step::Serve); if job.community.is_some() { read_local(app, &machine) } else { read_grids(app, &machine) } }+ Next::Serve => { step(app, Step::Serve); if job.community.is_some() { read_local(app, &machine) } else { read_grids(app, &machine, false) } }Next::Retarget { grid, model } => {step(app, Step::Switch);retarget(app, &job.machine, json!({ "agentId": job.agent, "gridModel": model, "gridName": grid }), &job.name, true);@@ -2064,7 +2067,7 @@ pub fn open(app: &mut App) {let machine = app.fleet.local_id.clone();if machine.is_empty() || app.link(&machine).is_none() { return }if !app.models_view.reading.contains(&machine) { read_local(app, &machine) }- if !app.models_view.grids_reading.contains(&machine) && due(app.models_view.grids_at.get(&machine), Duration::from_secs(20), Instant::now()) { read_grids(app, &machine) }+ if !app.models_view.grids_reading.contains(&machine) && due(app.models_view.grids_at.get(&machine), Duration::from_secs(20), Instant::now()) { read_grids(app, &machine, true) }read_apis(app);}@@ -2106,7 +2109,7 @@ pub fn tick(app: &mut App) {let apis_due = apis_due(app, open);if !waking { app.models_view.follow_until = None }if local_due { read_local(app, &machine) }- if grids_due { read_grids(app, &machine) }+ if grids_due { read_grids(app, &machine, false) }if apis_due { read_apis(app) }advance(app);}@@ -3361,6 +3364,24 @@ mod tests {assert!(!due(Some(&now), Duration::from_secs(4), now));}+ /// The model list's reach is a person opening the view (`userAction`, as the desktop's); the open+ /// view's own polls are automatic reads, counted for reliability only.+ #[tokio::test]+ async fn only_opening_the_view_marks_its_grid_read_as_a_persons() {+ let mut app = app();+ app.connect("local");+ app.fleet.machine_mut("local").unwrap().reach = Reach::Ready;+ let grids = |app: &App| app.models_view.sent.iter().filter(|s| s.1 == "grid_models_list").map(|s| s.2.clone()).collect::<Vec<_>>();+ open(&mut app);+ assert_eq!(grids(&app), [json!({ "rowState": true, "userAction": true })]);+ on_grids(&mut app, "local", Ok(grids_reply(&[])));+ app.modal = Some(crate::modal::Modal::Picker { kind: crate::modal::PickerKind::Models, picker: Picker::new("models", "") });+ app.models_view.grids_at.insert("local".into(), Instant::now() - Duration::from_secs(30));+ tick(&mut app);+ assert_eq!(grids(&app).last(), Some(&json!({ "rowState": true })));+ assert_eq!(grids(&app).len(), 2);+ }+/// `grid_models_changed` is the whole list: taken as it is, the rows follow.#[test]fn a_push_replaces_the_grids() {diff --git a/tui/src/new_harness.rs b/tui/src/new_harness.rsindex e1d3bc5c6..3ff6e0890 100644--- a/tui/src/new_harness.rs+++ b/tui/src/new_harness.rs@@ -469,7 +469,7 @@ pub fn open(app: &mut App, machine: Option<String>, cwd: Option<String>) {};let machine = form.draft.machine.clone();store_form(app, form);- crate::input::load_dsh(app, machine);+ crate::input::load_dsh(app, machine, false);}fn make_form(app: &mut App, machine: Option<String>, cwd: Option<String>, surface: Surface) -> Option<Box<Form>> {@@ -1182,7 +1182,7 @@ fn set_machine(app: &mut App, form: &mut Form, machine: &str) {form.profiles = Value::Null;form.draft.profile = None;form.resource_generation.clear();- crate::input::load_dsh(app, machine.into());+ crate::input::load_dsh(app, machine.into(), false);refresh_form(app, form);}fn set_engine(form: &mut Form, engine: &str) {diff --git a/tui/src/new_harness/welcome.rs b/tui/src/new_harness/welcome.rsindex ee57e697c..b0ab0f21d 100644--- a/tui/src/new_harness/welcome.rs+++ b/tui/src/new_harness/welcome.rs@@ -51,7 +51,7 @@ pub(crate) fn ensure(app: &mut App, from: Option<(String, String)>, cwd: Option<form.first_run = first;let machine = form.draft.machine.clone();store_form(app, form);- crate::input::load_dsh(app, machine);+ crate::input::load_dsh(app, machine, false);}}diff --git a/tui/src/shell_context/composition.rs b/tui/src/shell_context/composition.rsindex 7145f938b..9e8813fc0 100644--- a/tui/src/shell_context/composition.rs+++ b/tui/src/shell_context/composition.rs@@ -207,7 +207,7 @@ pub(super) fn list(app: &mut App, request: Request) {// (only New Harness read it), so its folders stayed "still loading" and only its agents'// scratch folders were offered.if kind == "folder" && !app.homes.contains_key(&machine) && app.shell_context.homes_asked.insert(machine.clone()) {- crate::input::load_dsh(app, machine.clone());+ crate::input::load_dsh(app, machine.clone(), false);}let browsing = kind == "folder" && crate::shell_picker::folder_parts(query).0.is_some();let parent = if kind == "folder" {diff --git a/tui/src/viewer.rs b/tui/src/viewer.rsindex b5c87c048..f0abf4829 100644--- a/tui/src/viewer.rs+++ b/tui/src/viewer.rs@@ -88,9 +88,17 @@ async fn open(url: &str) -> bool {}pub fn show(app: &mut App, key: (String, String), options: Options) {+ use crate::measure::Outcome;let Some(agent) = app.fleet.agent(&key.0, &key.1) else { return app.error("can't find harness") };+ // viewer_open (measure.rs), as the Desktop counts its viewer pane opened for the person: the+ // browser opening it, or its link shown to open (over SSH); failed when it could not open, or+ // with the harness's launch error. Opening is not rendering. A copied link or a shell asking for+ // it (`hn view -p`) opens nothing.+ let launch_error = !agent.viewer_error.is_empty();+ let opens = opens_for_person(app.capture.is_some(), &options);let url = match destination(agent, &app.fleet.local_id, &app.viewer_web_url, options.web || options.copy || (app.capture.is_none() && ssh())) {- Ok(url) => url, Err(e) => return app.error(e),+ Ok(url) => url,+ Err(e) => { if opens && launch_error { crate::measure::once(app, "viewer_open", Outcome::Failed) } return app.error(e) }};if app.capture.is_some() || options.print { app.print("Viewer", vec![url]); return }if options.copy {@@ -98,12 +106,21 @@ pub fn show(app: &mut App, key: (String, String), options: Options) {app.say("Viewer link sent to the terminal clipboard", crate::theme::ONLINE);return}- if ssh() { show_link(app, &url); return }- app.spawn(async move { let opened = open(&url).await; (url, opened) }, |app, (url, opened)| {+ if ssh() {+ show_link(app, &url);+ crate::measure::once(app, "viewer_open", if launch_error { Outcome::Failed } else { Outcome::Ok });+ return+ }+ let measured = crate::measure::begin(app, "viewer_open");+ app.spawn(async move { let opened = open(&url).await; (url, opened) }, move |app, (url, opened)| {+ measured.finish(app, if opened && !launch_error { Outcome::Ok } else { Outcome::Failed });if opened { app.say("Opened viewer in your browser", crate::theme::ONLINE) } else { show_link(app, &url) }});}+/// Whether showing a viewer opens it for the person: not a shell's capture, `-p` or a copied link.+fn opens_for_person(captured: bool, options: &Options) -> bool { !captured && !options.print && !options.copy }+fn show_link(app: &mut App, url: &str) {app.print("Viewer", vec!["Open this link in your browser:".into(), url.into(), String::new(), "Use :view -c to copy the browser link. Sign in and link this machine if asked.".into()]);}@@ -181,6 +198,33 @@ mod tests {assert!(web_link(base, "m", "a").is_err(), "{base}");}}+ /// viewer_open counts a viewer opened for the person, as the Desktop's pane: never a copied link+ /// or a shell's `-p`; a harness's launch error is a failed open.+ #[tokio::test]+ async fn only_a_viewer_opened_for_the_person_is_measured() {+ assert!(opens_for_person(false, &Options::default()));+ assert!(opens_for_person(false, &Options { web: true, ..Options::default() }));+ assert!(!opens_for_person(false, &Options { copy: true, ..Options::default() }));+ assert!(!opens_for_person(false, &Options { print: true, ..Options::default() }));+ assert!(!opens_for_person(true, &Options::default()));+ let (sink, _) = tokio::sync::mpsc::unbounded_channel();+ let mut app = App::new(19814, sink, (80, 24));+ app.viewer_web_url = "https://harness.example".into();+ app.fleet.local_id = "m".into();+ app.fleet.machines.push(crate::fleet::Machine { shared: false, id: "m".into(), name: "m".into(), local: true, status: "running".into(), reach: crate::fleet::Reach::Ready });+ app.follow_account(true);+ // (A copied link is not shown here: it would be written to the terminal running the tests.)+ for (id, row) in [("printed", json!({"id": "printed", "viewerName": "3D Viewer"})), ("broken", json!({"id": "broken", "viewerError": "the viewer did not start"}))] {+ let agent = crate::fleet::agent_from("m", &row, None);+ app.fleet.agents.insert(agent.key(), agent);+ show(&mut app, ("m".into(), id.into()), Options { print: id == "printed", ..Options::default() });+ }+ crate::measure::tick(&mut app);+ let viewer: Vec<&serde_json::Value> = app.measure.sent.iter().filter(|v| v["flow"] == "viewer_open").collect();+ assert_eq!(viewer.len(), 2, "the broken one's start and failure, nothing for the printed link: {viewer:?}");+ assert_eq!(viewer[1]["outcome"], "failed");+ }+#[test]fn no_viewer_is_an_error_and_waiting_viewer_gets_its_own_page() {let mut a = crate::fleet::agent_from("m", &json!({"id":"a"}), None);