Public: anyone can read this; the crew can change it.
Anonymous, opt-in app telemetry: a route to receive daily summaries, and an owner view #17967
Merged 13h 37m ago, live in production
scout/forge:scout/17962-anonymous-opt-in-app-telemetry-a-route-t → main 0 commits fork 3 threads · 3 unresolved
- docs/design/audit.md
- docs/features/apple-usage.md
- forge.json
- src/actions/apple_usage.ts
- src/contract/openapi/routes/index.ts
- src/contract/openapi/routes/postApiV1TelemetryApple.ts
- src/core/apple_usage.ts
- src/core/migration-pins/20261203090000-apple-usage.pin
- src/core/migrations/20261203090000-apple-usage.ts
- src/generated/screens.ts
- src/http/api.ts
- src/http/api/telemetry_apple.ts
- src/http/lazy_lists.ts
- src/http/limits.ts
- src/http/marks/admin.json
- src/http/routes/admin.ts
- src/kernel/body_caps.ts
- src/tui/screens/appleusage.ts
- src/view/policy.ts
- src/web/pages/admin-limits.ts
- src/web/pages/admin-telemetry.ts
- test/apple-usage-route.test.ts
- test/apple-usage.test.ts
- test/forge-looks.test.ts
- test/palette-roster.test.ts
docs/design/audit.md +1
| @@ -135,6 +135,7 @@ Machine routes are not pages and have no entry: `/audit.csv`, | ||
| 135 | 135 | - `/resilience` — `pages/resilience.ts` · each protection in `docs/THREATS.md` against the last gate's tests · `/settings/resilience` (similar name, different content) · none seen. |
| 136 | 136 | - `/maintenance` — `pages/maintenance.ts` · prepare the forge for a power-off and end it · `/resilience`, `/settings/backups` · none seen. |
| 137 | 137 | - `/admin/limits` — `pages/admin-limits.ts` · every limit with value, default and reason · `/settings/capacity` (also a numbers-you-can-change page) · none seen. |
| 138 | - `/admin/telemetry` — `pages/admin-telemetry.ts` · the Apple app's anonymous usage, per day and per version · `/admin/limits` (also an owner-only table page) · none seen. | |
| 138 | 139 | - `/admin/flows` — `pages/flows-all.ts` · every repository's flow lines with state and last moved, by kind and stale · `/:owner/:name/flows` · none seen. |
| 139 | 140 | - `/admin/abstractions` — `pages/abstractions.ts` · the forge's tables, one row each: stage, rows, first and last row, use in 28 days, what builds on what · Settings › Tables (This forge) · none seen. |
| 140 | 141 | - `/admin/data` — `pages/data.ts` · the forge's own database: tables, rows, read-only SQL, secrets masked · `/data` (the Forge data button) · none seen; shares its body (`pages/data_view.ts`) with `/data` and `/:owner/:name/data`. |
docs/features/apple-usage.md added +19
| @@ -0,0 +1,19 @@ | ||
| 1 | --- | |
| 2 | topic: Every setting, by topic | |
| 3 | row: Anonymous Apple app usage | |
| 4 | --- | |
| 5 | # Anonymous Apple app usage | |
| 6 | ||
| 7 | People opt in once in the forge Apple app (and can change it in its Settings). When they do, the app sends one summary a day to the forge they are signed in to, and only that forge: `POST /api/v1/telemetry/apple`, open to signed-out callers. | |
| 8 | ||
| 9 | json | |
| 10 | {"v":1,"day":"2026-12-03","app":{"version":"1.4","build":"212"},"os":"iOS 26.1","device":"iPhone", | |
| 11 | "counts":{"screen.now":12,"queue.cleared":7},"timings":{"code.open.p50":120},"metrickit":{}} | |
| 12 | ||
| 13 | ||
| 14 | - **Kept:** per UTC day, app version (with build) and device kind (`iPhone`, `iPad`, `Mac`, `Watch`, `Vision`, `TV`, else `other`): the counts' sums, how many summaries came, and each timing in a histogram bucket. Nothing else: no account, address or device id. `os` and `metrickit` are checked and dropped. | |
| 15 | - **Bounded:** 8 KB a body, 10 a minute from one address (the address is counted in memory for that minute and never stored), 64 names a summary, 20 versions a day and 150 names a version (the rest fold into `(other)` and `(more)`, so a flood of made-up versions or names spoils only itself), 400 days kept. A bad body is a 422 naming the field's rule; success is 204. | |
| 16 | - **Read it:** a site admin opens `/admin/telemetry` (`src/web/pages/admin-telemetry.ts`, linked from Limits; the last 14 days: summaries a day, and per version the counts and the p50 and p90 of the summaries' own timings: a p50 there is the middle of the devices' daily p50s) or `App usage` in the terminal (`src/tui/screens/appleusage.ts`). Both read `readAppleUsage` in `src/actions/apple_usage.ts`. | |
| 17 | - **Other forges:** a person signed in to their own forge sends to that forge only. Nothing is forwarded. | |
| 18 | ||
| 19 | Not done: a switch for the owner to refuse summaries (the app stays dormant until the route answers 2xx), and the MetricKit payload is not kept. |
forge.json +2 −2
| @@ -37,8 +37,8 @@ | ||
| 37 | 37 | { "name": "heartbeat", "cron": "5 7 * * *", "timezone": "UTC", "path": "/jobs/heartbeat", "timeout": 120, "retries": 3, "catchUp": "6h" } |
| 38 | 38 | ], |
| 39 | 39 | "app": { |
| 40 | "looks": ["/", "/docs", "/explore", "/board", "/cockpit", "/notes", "/review", "/changelog", "/apps", "/security", "/system", "/admin/limits", "/maintenance", "/talk", "/flow", "/resilience", "/boards?help", "/settings/crew", "/settings/crew?help", "/settings/captain", "/settings/security/controls", "/settings/grants", "/preview-admin/sample", "/preview-admin/sample/drop", "/preview-admin/sample/data-move", "/preview-admin/sample/cut-over", "/preview-admin/sample/pulls/3", "/preview-admin/sample/next?pick", "/preview-admin/sample/bets", "/advisories", "/looks/missing", "/looks/failed"], | |
| 41 | "looksAsOwner": ["/", "/board", "/cockpit", "/flow", "/boards?help", "/settings/crew", "/settings/crew?help", "/settings/captain", "/settings/security/controls", "/settings/grants", "/talk", "/system", "/admin/limits", "/maintenance", "/resilience", "/preview-admin/sample", "/preview-admin/sample/drop", "/preview-admin/sample/data-move", "/preview-admin/sample/cut-over", "/preview-admin/sample/pulls/3", "/preview-admin/sample/next?pick", "/preview-admin/sample/bets", "/advisories"], | |
| 40 | "looks": ["/", "/admin/telemetry", "/explore", "/board", "/cockpit", "/notes", "/review", "/changelog", "/apps", "/security", "/system", "/admin/limits", "/maintenance", "/talk", "/flow", "/resilience", "/boards?help", "/settings/crew", "/settings/crew?help", "/settings/captain", "/settings/security/controls", "/settings/grants", "/preview-admin/sample", "/preview-admin/sample/drop", "/preview-admin/sample/data-move", "/preview-admin/sample/cut-over", "/preview-admin/sample/pulls/3", "/preview-admin/sample/next?pick", "/preview-admin/sample/bets", "/advisories", "/looks/missing", "/looks/failed"], | |
| 41 | "looksAsOwner": ["/", "/board", "/cockpit", "/flow", "/boards?help", "/settings/crew", "/settings/crew?help", "/settings/captain", "/settings/security/controls", "/settings/grants", "/talk", "/system", "/admin/limits", "/admin/telemetry", "/maintenance", "/resilience", "/preview-admin/sample", "/preview-admin/sample/drop", "/preview-admin/sample/data-move", "/preview-admin/sample/cut-over", "/preview-admin/sample/pulls/3", "/preview-admin/sample/next?pick", "/preview-admin/sample/bets", "/advisories"], | |
| 42 | 42 | "run": "bun run src/main.ts", |
| 43 | 43 | "install": "", |
| 44 | 44 | "health": "/healthz", |
src/actions/apple_usage.ts added +16
| @@ -0,0 +1,16 @@ | ||
| 1 | /** | |
| 2 | * What the Apple app's anonymous summaries add up to (#17962), for a forge admin: the one gate the | |
| 3 | * page (/admin/telemetry) and the terminal screen share. Anyone else, an agent included, is told | |
| 4 | * the same as for a page that is not there. | |
| 5 | */ | |
| 6 | ||
| 7 | import type { App } from "../app.ts"; | |
| 8 | import { type UsageRead, readUsage } from "../core/apple_usage.ts"; | |
| 9 | import type { User } from "../core/users.ts"; | |
| 10 | ||
| 11 | export const USAGE_DAYS = 14; | |
| 12 | ||
| 13 | export function readAppleUsage(app: App, user: User | null | undefined): { ok: true; value: UsageRead } | { ok: false; error: "not_found" } { | |
| 14 | if (user?.isAdmin !== true || user.kind === "agent") return { ok: false, error: "not_found" }; | |
| 15 | return { ok: true, value: readUsage(app.db, USAGE_DAYS) }; | |
| 16 | } |
src/contract/openapi/routes/index.ts +1
| @@ -498,6 +498,7 @@ export { route as postApiV1RunnerJobsIdHand } from "./postApiV1RunnerJobsIdHand. | ||
| 498 | 498 | export { route as postApiV1Sessions } from "./postApiV1Sessions.ts"; |
| 499 | 499 | export { route as postApiV1Share } from "./postApiV1Share.ts"; |
| 500 | 500 | export { route as postApiV1Start } from "./postApiV1Start.ts"; |
| 501 | export { route as postApiV1TelemetryApple } from "./postApiV1TelemetryApple.ts"; | |
| 501 | 502 | export { route as postApiV1Thoughts } from "./postApiV1Thoughts.ts"; |
| 502 | 503 | export { route as postApiV1Timers } from "./postApiV1Timers.ts"; |
| 503 | 504 | export { route as postApiV1TimersFinish } from "./postApiV1TimersFinish.ts"; |
src/contract/openapi/routes/postApiV1TelemetryApple.ts added +14
| @@ -0,0 +1,14 @@ | ||
| 1 | import type { Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "post", | |
| 5 | path: "/api/v1/telemetry/apple", | |
| 6 | summary: "The Apple app's anonymous daily usage summary; open to signed-out callers", | |
| 7 | detail: "Body `{v: 1, day: \"YYYY-MM-DD\", app: {version, build}, os, device, counts: {name: whole number}, timings: {name: ms}, metrickit?: {}}`. Nothing about the sender is kept: no account, address or device id. `os` and `metrickit` are checked and dropped. At most 8 KB and 10 summaries a minute from one address. Answers 204.", | |
| 8 | auth: false, | |
| 9 | role: "anonymous", | |
| 10 | status: 204, | |
| 11 | body: { v: "integer", day: "string", app: "object", os: "string", device: "string", counts: "object?", timings: "object?", metrickit: "object?" }, | |
| 12 | refusals: { invalid_body: "the body is not a JSON object (422)", invalid_v: "v is not 1 (422)", invalid_day: "day is not a UTC date from two days ago to tomorrow (422)", invalid_app: "app is not {version, build} (422)", invalid_os: "os is not a string of 1 to 40 characters (422)", invalid_device: "device is not a string of 1 to 20 characters (422)", invalid_counts: "counts holds more than 64 names, a malformed name or a value that is not a whole number up to a billion (422)", invalid_timings: "timings holds more than 64 names, a malformed name or a value that is not 0 to 86400000 ms (422)", invalid_metrickit: "metrickit is not an object (422)", too_large: "the body is over 8 KB (413)", rate_limited: "429: too many summaries from one address or in all" }, | |
| 13 | refuses: [422], | |
| 14 | }; |
src/core/apple_usage.ts added +234
| @@ -0,0 +1,234 @@ | ||
| 1 | /** | |
| 2 | * The Apple app's anonymous daily summary (#17962): what is accepted, what is kept and how it | |
| 3 | * reads. | |
| 4 | * | |
| 5 | * Kept per UTC day, app version (with build) and device kind only: a count's total, how many | |
| 6 | * summaries came, and each timing as a histogram bucket (so a percentile of the summaries' own | |
| 7 | * timings can be read without keeping any one summary). The `os` and `metrickit` a summary carries | |
| 8 | * are checked and dropped, and nothing about who sent it is ever an argument here: no account, no | |
| 9 | * address, no device id. A day keeps at most `MAX_VERSIONS` distinct versions and a version at most | |
| 10 | * `MAX_VERSION_NAMES` names a day; past either, the newcomer folds into `(other)` or `(more)`. The | |
| 11 | * cap is per version, not per day, so a flood of made-up versions fills only its own slots and | |
| 12 | * never takes a real version's rows that arrived earlier. Days past `KEEP_DAYS` are dropped as new | |
| 13 | * ones arrive. | |
| 14 | */ | |
| 15 | ||
| 16 | import type { Database } from "bun:sqlite"; | |
| 17 | ||
| 18 | export const USAGE_SCHEMA = 1; | |
| 19 | export const KEEP_DAYS = 400; | |
| 20 | export const MAX_VERSIONS = 20; | |
| 21 | export const MAX_VERSION_NAMES = 150; | |
| 22 | export const MAX_NAMES = 64; | |
| 23 | const DEVICES = new Set(["iPhone", "iPad", "Mac", "Watch", "Vision", "TV"]); | |
| 24 | const NAME = /^[A-Za-z][A-Za-z0-9_]{0,31}(\.[A-Za-z0-9_]{1,32}){0,4}$/; | |
| 25 | const VERSION = /^\d{1,4}(\.\d{1,4}){0,3}$/; | |
| 26 | const BUILD = /^[0-9A-Za-z.]{1,16}$/; | |
| 27 | const DAY = /^\d{4}-\d{2}-\d{2}$/; | |
| 28 | const MAX_COUNT = 1_000_000_000; | |
| 29 | const MAX_MS = 86_400_000; | |
| 30 | const OVERFLOW_NAME = "(more)"; | |
| 31 | const OVERFLOW_VERSION = "(other)"; | |
| 32 | ||
| 33 | export interface UsageReport { | |
| 34 | readonly day: string; | |
| 35 | /** `1.4 (212)`: the version and its build. */ | |
| 36 | readonly version: string; | |
| 37 | /** A device kind from the list, or `other`. */ | |
| 38 | readonly device: string; | |
| 39 | readonly counts: readonly (readonly [string, number])[]; | |
| 40 | readonly timings: readonly (readonly [string, number])[]; | |
| 41 | } | |
| 42 | ||
| 43 | export const USAGE_ERRORS = { | |
| 44 | invalid_body: "the body is not a JSON object", | |
| 45 | invalid_v: `v is ${USAGE_SCHEMA}`, | |
| 46 | invalid_day: "day is a UTC date, YYYY-MM-DD, from two days ago to tomorrow", | |
| 47 | invalid_app: "app is {version, build}: a dotted number and a short build", | |
| 48 | invalid_os: "os is a string of at most 40 characters", | |
| 49 | invalid_device: "device is a string of at most 20 characters", | |
| 50 | invalid_counts: `counts is an object of at most ${MAX_NAMES} names (letters, digits, dots) to whole numbers from 0 to ${MAX_COUNT}`, | |
| 51 | invalid_timings: `timings is an object of at most ${MAX_NAMES} names (letters, digits, dots) to milliseconds from 0 to ${MAX_MS}`, | |
| 52 | invalid_metrickit: "metrickit is an object", | |
| 53 | } as const; | |
| 54 | export type UsageError = keyof typeof USAGE_ERRORS; | |
| 55 | ||
| 56 | const isObject = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null && !Array.isArray(v); | |
| 57 | ||
| 58 | function named(v: unknown, max: number, whole: boolean): (readonly [string, number])[] | undefined { | |
| 59 | if (v === undefined) return []; | |
| 60 | if (!isObject(v)) return undefined; | |
| 61 | const entries = Object.entries(v); | |
| 62 | if (entries.length > MAX_NAMES) return undefined; | |
| 63 | const out: [string, number][] = []; | |
| 64 | for (const [k, n] of entries) { | |
| 65 | if (!NAME.test(k) || typeof n !== "number" || !Number.isFinite(n) || n < 0 || n > max) return undefined; | |
| 66 | if (whole && !Number.isInteger(n)) return undefined; | |
| 67 | out.push([k, Math.round(n)]); | |
| 68 | } | |
| 69 | return out; | |
| 70 | } | |
| 71 | ||
| 72 | const dayWords = (ms: number): string => new Date(ms).toISOString().slice(0, 10); | |
| 73 | ||
| 74 | /** A body, read at the boundary: the report to keep, or the first thing wrong with it. */ | |
| 75 | export function parseUsage(input: unknown, now: Date = new Date()): { ok: true; value: UsageReport } | { ok: false; error: UsageError } { | |
| 76 | const bad = (error: UsageError) => ({ ok: false as const, error }); | |
| 77 | if (!isObject(input)) return bad("invalid_body"); | |
| 78 | if (input.v !== USAGE_SCHEMA) return bad("invalid_v"); | |
| 79 | const day = input.day; | |
| 80 | const t = now.getTime(); | |
| 81 | if (typeof day !== "string" || !DAY.test(day) || day < dayWords(t - 2 * 86_400_000) || day > dayWords(t + 86_400_000) || Number.isNaN(Date.parse(day))) { | |
| 82 | return bad("invalid_day"); | |
| 83 | } | |
| 84 | const app = input.app; | |
| 85 | if (!isObject(app) || typeof app.version !== "string" || !VERSION.test(app.version)) return bad("invalid_app"); | |
| 86 | const build = typeof app.build === "number" ? String(app.build) : app.build; | |
| 87 | if (typeof build !== "string" || !BUILD.test(build)) return bad("invalid_app"); | |
| 88 | if (typeof input.os !== "string" || input.os.length < 1 || input.os.length > 40) return bad("invalid_os"); | |
| 89 | if (typeof input.device !== "string" || input.device.length < 1 || input.device.length > 20) return bad("invalid_device"); | |
| 90 | const counts = named(input.counts, MAX_COUNT, true); | |
| 91 | if (counts === undefined) return bad("invalid_counts"); | |
| 92 | const timings = named(input.timings, MAX_MS, false); | |
| 93 | if (timings === undefined) return bad("invalid_timings"); | |
| 94 | if (input.metrickit !== undefined && !isObject(input.metrickit)) return bad("invalid_metrickit"); | |
| 95 | return { | |
| 96 | ok: true, | |
| 97 | value: { day, version: `${app.version} (${build})`, device: DEVICES.has(input.device) ? input.device : "other", counts, timings }, | |
| 98 | }; | |
| 99 | } | |
| 100 | ||
| 101 | /** A timing's bucket: half-powers of two, so a bucket is within 41% of its members. */ | |
| 102 | export const bucketOf = (ms: number): number => Math.floor(Math.log2(ms + 1) * 2); | |
| 103 | /** What a bucket reads as: its upper edge, in milliseconds. */ | |
| 104 | export const bucketMs = (b: number): number => Math.round(2 ** ((b + 1) / 2) - 1); | |
| 105 | ||
| 106 | /** Keep one report: its counts summed, its timings in buckets, one more summary received. */ | |
| 107 | export function recordUsage(db: Database, r: UsageReport, now: Date = new Date()): void { | |
| 108 | const run = db.transaction(() => { | |
| 109 | const first = db.query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM apple_usage WHERE day = ?1").get(r.day)?.n === 0; | |
| 110 | if (first) db.query("DELETE FROM apple_usage WHERE day < ?1").run(dayWords(now.getTime() - KEEP_DAYS * 86_400_000)); | |
| 111 | const versions = db | |
| 112 | .query<{ version: string }, [string, string]>("SELECT DISTINCT version FROM apple_usage WHERE day = ?1 AND version <> ?2") | |
| 113 | .all(r.day, OVERFLOW_VERSION) | |
| 114 | .map((x) => x.version); | |
| 115 | const version = versions.includes(r.version) || versions.length < MAX_VERSIONS ? r.version : OVERFLOW_VERSION; | |
| 116 | const known = new Set( | |
| 117 | db | |
| 118 | .query<{ name: string }, [string, string]>("SELECT DISTINCT name FROM apple_usage WHERE day = ?1 AND version = ?2 AND name <> ''") | |
| 119 | .all(r.day, version) | |
| 120 | .map((x) => x.name), | |
| 121 | ); | |
| 122 | const nameFor = (name: string): string => { | |
| 123 | if (version === OVERFLOW_VERSION || known.has(name)) return name; | |
| 124 | if (known.size >= MAX_VERSION_NAMES) return OVERFLOW_NAME; | |
| 125 | known.add(name); | |
| 126 | return name; | |
| 127 | }; | |
| 128 | const bump = db.query<unknown, [string, string, string, string, string, number, number]>( | |
| 129 | `INSERT INTO apple_usage (day, version, device, name, kind, bucket, n, total) VALUES (?1, ?2, ?3, ?4, ?5, ?6, 1, ?7) | |
| 130 | ON CONFLICT (day, version, device, name, kind, bucket) DO UPDATE SET n = n + 1, total = total + ?7`, | |
| 131 | ); | |
| 132 | bump.run(r.day, version, r.device, "", "r", 0, 0); | |
| 133 | for (const [name, n] of r.counts) bump.run(r.day, version, r.device, nameFor(name), "c", 0, n); | |
| 134 | for (const [name, ms] of r.timings) bump.run(r.day, version, r.device, nameFor(name), "t", bucketOf(ms), Math.round(ms)); | |
| 135 | }); | |
| 136 | run(); | |
| 137 | } | |
| 138 | ||
| 139 | export interface UsageCount { | |
| 140 | readonly name: string; | |
| 141 | readonly total: number; | |
| 142 | } | |
| 143 | export interface UsageTiming { | |
| 144 | readonly name: string; | |
| 145 | /** Summaries that carried it. */ | |
| 146 | readonly n: number; | |
| 147 | readonly p50: number; | |
| 148 | readonly p90: number; | |
| 149 | } | |
| 150 | export interface UsageVersion { | |
| 151 | readonly version: string; | |
| 152 | /** Summaries received, by device kind. */ | |
| 153 | readonly devices: Readonly<Record<string, number>>; | |
| 154 | readonly reports: number; | |
| 155 | readonly counts: readonly UsageCount[]; | |
| 156 | readonly timings: readonly UsageTiming[]; | |
| 157 | } | |
| 158 | export interface UsageDay { | |
| 159 | readonly day: string; | |
| 160 | readonly reports: number; | |
| 161 | /** The day's counts, all versions, most first. */ | |
| 162 | readonly counts: readonly UsageCount[]; | |
| 163 | } | |
| 164 | export interface UsageRead { | |
| 165 | readonly days: readonly UsageDay[]; | |
| 166 | readonly versions: readonly UsageVersion[]; | |
| 167 | } | |
| 168 | ||
| 169 | interface Row { | |
| 170 | day: string; | |
| 171 | version: string; | |
| 172 | device: string; | |
| 173 | name: string; | |
| 174 | kind: string; | |
| 175 | bucket: number; | |
| 176 | n: number; | |
| 177 | total: number; | |
| 178 | } | |
| 179 | ||
| 180 | function percentile(buckets: readonly { bucket: number; n: number }[], p: number): number { | |
| 181 | const all = buckets.reduce((s, b) => s + b.n, 0); | |
| 182 | const want = Math.ceil(all * p); | |
| 183 | let seen = 0; | |
| 184 | for (const b of [...buckets].sort((x, y) => x.bucket - y.bucket)) { | |
| 185 | seen += b.n; | |
| 186 | if (seen >= want) return bucketMs(b.bucket); | |
| 187 | } | |
| 188 | return 0; | |
| 189 | } | |
| 190 | ||
| 191 | const byTotal = (a: UsageCount, b: UsageCount) => b.total - a.total || a.name.localeCompare(b.name); | |
| 192 | ||
| 193 | /** | |
| 194 | * The last `days` UTC days (today included): daily totals, and per version its counts and timing | |
| 195 | * percentiles. | |
| 196 | */ | |
| 197 | export function readUsage(db: Database, days = 14, now: Date = new Date()): UsageRead { | |
| 198 | const from = dayWords(now.getTime() - (days - 1) * 86_400_000); | |
| 199 | const rows = db.query<Row, [string]>("SELECT * FROM apple_usage WHERE day >= ?1").all(from); | |
| 200 | const perDay = new Map<string, { reports: number; counts: Map<string, number> }>(); | |
| 201 | const perVersion = new Map<string, { devices: Record<string, number>; counts: Map<string, number>; timings: Map<string, { bucket: number; n: number }[]> }>(); | |
| 202 | for (const r of rows) { | |
| 203 | const d = perDay.get(r.day) ?? { reports: 0, counts: new Map() }; | |
| 204 | perDay.set(r.day, d); | |
| 205 | const v = perVersion.get(r.version) ?? { devices: {} as Record<string, number>, counts: new Map(), timings: new Map() }; | |
| 206 | perVersion.set(r.version, v); | |
| 207 | if (r.kind === "r") { | |
| 208 | d.reports += r.n; | |
| 209 | v.devices[r.device] = (v.devices[r.device] ?? 0) + r.n; | |
| 210 | } else if (r.kind === "c") { | |
| 211 | d.counts.set(r.name, (d.counts.get(r.name) ?? 0) + r.total); | |
| 212 | v.counts.set(r.name, (v.counts.get(r.name) ?? 0) + r.total); | |
| 213 | } else { | |
| 214 | const list = v.timings.get(r.name) ?? []; | |
| 215 | list.push({ bucket: r.bucket, n: r.n }); | |
| 216 | v.timings.set(r.name, list); | |
| 217 | } | |
| 218 | } | |
| 219 | const counts = (m: Map<string, number>): UsageCount[] => [...m].map(([name, total]) => ({ name, total })).sort(byTotal); | |
| 220 | return { | |
| 221 | days: [...perDay].map(([day, d]) => ({ day, reports: d.reports, counts: counts(d.counts) })).sort((a, b) => (a.day < b.day ? 1 : -1)), | |
| 222 | versions: [...perVersion] | |
| 223 | .map(([version, v]) => ({ | |
| 224 | version, | |
| 225 | devices: v.devices, | |
| 226 | reports: Object.values(v.devices).reduce((s, n) => s + n, 0), | |
| 227 | counts: counts(v.counts), | |
| 228 | timings: [...v.timings] | |
| 229 | .map(([name, b]) => ({ name, n: b.reduce((s, x) => s + x.n, 0), p50: percentile(b, 0.5), p90: percentile(b, 0.9) })) | |
| 230 | .sort((a, b) => a.name.localeCompare(b.name)), | |
| 231 | })) | |
| 232 | .sort((a, b) => b.reports - a.reports || a.version.localeCompare(b.version)), | |
| 233 | }; | |
| 234 | } |
src/core/migration-pins/20261203090000-apple-usage.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 0391a71fa8a05a2cf73f4fff5048b3bc260632062dd17dce5684a9d1bb229810 |
src/core/migrations/20261203090000-apple-usage.ts added +17
| @@ -0,0 +1,17 @@ | ||
| 1 | // The Apple app's anonymous daily summaries (#17962), as aggregates only: one row per day, app | |
| 2 | // version, device kind, name and kind (`c` a count's total, `t` a timing's histogram bucket, `r` | |
| 3 | // the summaries received). No account, address or device identifier is a column. Bounded per day by | |
| 4 | // `recordUsage` (src/core/apple_usage.ts). Additive: a new table. | |
| 5 | export default ` | |
| 6 | CREATE TABLE IF NOT EXISTS apple_usage ( | |
| 7 | day TEXT NOT NULL, | |
| 8 | version TEXT NOT NULL, | |
| 9 | device TEXT NOT NULL, | |
| 10 | name TEXT NOT NULL, | |
| 11 | kind TEXT NOT NULL, | |
| 12 | bucket INTEGER NOT NULL DEFAULT 0, | |
| 13 | n INTEGER NOT NULL DEFAULT 0, | |
| 14 | total INTEGER NOT NULL DEFAULT 0, | |
| 15 | PRIMARY KEY (day, version, device, name, kind, bucket) | |
| 16 | ) WITHOUT ROWID; | |
| 17 | `; |
src/generated/screens.ts +134 −132
| @@ -7,132 +7,133 @@ import * as s2 from "../tui/screens/account_access.ts"; | ||
| 7 | 7 | import * as s3 from "../tui/screens/advisories.ts"; |
| 8 | 8 | import * as s4 from "../tui/screens/agents.ts"; |
| 9 | 9 | import * as s5 from "../tui/screens/app_templates.ts"; |
| 10 | import * as s6 from "../tui/screens/apps_crew.ts"; | |
| 11 | import * as s7 from "../tui/screens/archived.ts"; | |
| 12 | import * as s8 from "../tui/screens/ask_data.ts"; | |
| 13 | import * as s9 from "../tui/screens/audit.ts"; | |
| 14 | import * as s10 from "../tui/screens/bets.ts"; | |
| 15 | import * as s11 from "../tui/screens/billing.ts"; | |
| 16 | import * as s12 from "../tui/screens/billinglinks.ts"; | |
| 17 | import * as s13 from "../tui/screens/bills.ts"; | |
| 18 | import * as s14 from "../tui/screens/boards.ts"; | |
| 19 | import * as s15 from "../tui/screens/bottleneck.ts"; | |
| 20 | import * as s16 from "../tui/screens/buckets.ts"; | |
| 21 | import * as s17 from "../tui/screens/bugs.ts"; | |
| 22 | import * as s18 from "../tui/screens/bulk.ts"; | |
| 23 | import * as s19 from "../tui/screens/captain.ts"; | |
| 24 | import * as s20 from "../tui/screens/childcredit.ts"; | |
| 25 | import * as s21 from "../tui/screens/claim_link.ts"; | |
| 26 | import * as s22 from "../tui/screens/claim_start.ts"; | |
| 27 | import * as s23 from "../tui/screens/client_clouds.ts"; | |
| 28 | import * as s24 from "../tui/screens/cloud.ts"; | |
| 29 | import * as s25 from "../tui/screens/cloudtree.ts"; | |
| 30 | import * as s26 from "../tui/screens/codes.ts"; | |
| 31 | import * as s27 from "../tui/screens/connections.ts"; | |
| 32 | import * as s28 from "../tui/screens/controls.ts"; | |
| 33 | import * as s29 from "../tui/screens/cores.ts"; | |
| 34 | import * as s30 from "../tui/screens/crewbudgets.ts"; | |
| 35 | import * as s31 from "../tui/screens/crewfallback.ts"; | |
| 36 | import * as s32 from "../tui/screens/crewhandover.ts"; | |
| 37 | import * as s33 from "../tui/screens/crewkey.ts"; | |
| 38 | import * as s34 from "../tui/screens/crewsplits.ts"; | |
| 39 | import * as s35 from "../tui/screens/crewtop.ts"; | |
| 40 | import * as s36 from "../tui/screens/dataset.ts"; | |
| 41 | import * as s37 from "../tui/screens/design.ts"; | |
| 42 | import * as s38 from "../tui/screens/design_gallery.ts"; | |
| 43 | import * as s39 from "../tui/screens/devices.ts"; | |
| 44 | import * as s40 from "../tui/screens/disks.ts"; | |
| 45 | import * as s41 from "../tui/screens/displays.ts"; | |
| 46 | import * as s42 from "../tui/screens/domains.ts"; | |
| 47 | import * as s43 from "../tui/screens/dotfiles.ts"; | |
| 48 | import * as s44 from "../tui/screens/drift.ts"; | |
| 49 | import * as s45 from "../tui/screens/dutybudgets.ts"; | |
| 50 | import * as s46 from "../tui/screens/dutyswitch.ts"; | |
| 51 | import * as s47 from "../tui/screens/economics.ts"; | |
| 52 | import * as s48 from "../tui/screens/edgeonly.ts"; | |
| 53 | import * as s49 from "../tui/screens/experiments.ts"; | |
| 54 | import * as s50 from "../tui/screens/featurefeed.ts"; | |
| 55 | import * as s51 from "../tui/screens/features.ts"; | |
| 56 | import * as s52 from "../tui/screens/featureswitch.ts"; | |
| 57 | import * as s53 from "../tui/screens/feed.ts"; | |
| 58 | import * as s54 from "../tui/screens/flowsall.ts"; | |
| 59 | import * as s55 from "../tui/screens/footing.ts"; | |
| 60 | import * as s56 from "../tui/screens/forgeready.ts"; | |
| 61 | import * as s57 from "../tui/screens/forges.ts"; | |
| 62 | import * as s58 from "../tui/screens/forgeview.ts"; | |
| 63 | import * as s59 from "../tui/screens/front.ts"; | |
| 64 | import * as s60 from "../tui/screens/frontms.ts"; | |
| 65 | import * as s61 from "../tui/screens/funnel.ts"; | |
| 66 | import * as s62 from "../tui/screens/games.ts"; | |
| 67 | import * as s63 from "../tui/screens/gifts.ts"; | |
| 68 | import * as s64 from "../tui/screens/health_voice.ts"; | |
| 69 | import * as s65 from "../tui/screens/home_pins.ts"; | |
| 70 | import * as s66 from "../tui/screens/hostreach.ts"; | |
| 71 | import * as s67 from "../tui/screens/infra.ts"; | |
| 72 | import * as s68 from "../tui/screens/inventory.ts"; | |
| 73 | import * as s69 from "../tui/screens/invites.ts"; | |
| 74 | import * as s70 from "../tui/screens/knobs.ts"; | |
| 75 | import * as s71 from "../tui/screens/lapsed.ts"; | |
| 76 | import * as s72 from "../tui/screens/limits.ts"; | |
| 77 | import * as s73 from "../tui/screens/loopstalls.ts"; | |
| 78 | import * as s74 from "../tui/screens/madefrom.ts"; | |
| 79 | import * as s75 from "../tui/screens/maintainruns.ts"; | |
| 80 | import * as s76 from "../tui/screens/maintenance.ts"; | |
| 81 | import * as s77 from "../tui/screens/maintenanceweek.ts"; | |
| 82 | import * as s78 from "../tui/screens/mergedbranches.ts"; | |
| 83 | import * as s79 from "../tui/screens/mirrorpacks.ts"; | |
| 84 | import * as s80 from "../tui/screens/model.ts"; | |
| 85 | import * as s81 from "../tui/screens/money.ts"; | |
| 86 | import * as s82 from "../tui/screens/newissue.ts"; | |
| 87 | import * as s83 from "../tui/screens/next.ts"; | |
| 88 | import * as s84 from "../tui/screens/nowlist.ts"; | |
| 89 | import * as s85 from "../tui/screens/offers.ts"; | |
| 90 | import * as s86 from "../tui/screens/offsite.ts"; | |
| 91 | import * as s87 from "../tui/screens/orggrants.ts"; | |
| 92 | import * as s88 from "../tui/screens/orgteams.ts"; | |
| 93 | import * as s89 from "../tui/screens/originahead.ts"; | |
| 94 | import * as s90 from "../tui/screens/owner.ts"; | |
| 95 | import * as s91 from "../tui/screens/packrepos.ts"; | |
| 96 | import * as s92 from "../tui/screens/payments.ts"; | |
| 97 | import * as s93 from "../tui/screens/people.ts"; | |
| 98 | import * as s94 from "../tui/screens/pinned.ts"; | |
| 99 | import * as s95 from "../tui/screens/planner.ts"; | |
| 100 | import * as s96 from "../tui/screens/planrow.ts"; | |
| 101 | import * as s97 from "../tui/screens/plans.ts"; | |
| 102 | import * as s98 from "../tui/screens/previewdirs.ts"; | |
| 103 | import * as s99 from "../tui/screens/profile.ts"; | |
| 104 | import * as s100 from "../tui/screens/quotas.ts"; | |
| 105 | import * as s101 from "../tui/screens/recoverykit.ts"; | |
| 106 | import * as s102 from "../tui/screens/refused.ts"; | |
| 107 | import * as s103 from "../tui/screens/registry_items.ts"; | |
| 108 | import * as s104 from "../tui/screens/repolist.ts"; | |
| 109 | import * as s105 from "../tui/screens/requests.ts"; | |
| 110 | import * as s106 from "../tui/screens/resilience.ts"; | |
| 111 | import * as s107 from "../tui/screens/returning.ts"; | |
| 112 | import * as s108 from "../tui/screens/returns.ts"; | |
| 113 | import * as s109 from "../tui/screens/scorecard.ts"; | |
| 114 | import * as s110 from "../tui/screens/scrapes.ts"; | |
| 115 | import * as s111 from "../tui/screens/secrets.ts"; | |
| 116 | import * as s112 from "../tui/screens/sentinel.ts"; | |
| 117 | import * as s113 from "../tui/screens/series.ts"; | |
| 118 | import * as s114 from "../tui/screens/ship_time.ts"; | |
| 119 | import * as s115 from "../tui/screens/slowpages.ts"; | |
| 120 | import * as s116 from "../tui/screens/sold.ts"; | |
| 121 | import * as s117 from "../tui/screens/spend.ts"; | |
| 122 | import * as s118 from "../tui/screens/stalepulls.ts"; | |
| 123 | import * as s119 from "../tui/screens/subscriptions.ts"; | |
| 124 | import * as s120 from "../tui/screens/system.ts"; | |
| 125 | import * as s121 from "../tui/screens/tab.ts"; | |
| 126 | import * as s122 from "../tui/screens/teams.ts"; | |
| 127 | import * as s123 from "../tui/screens/telemetry.ts"; | |
| 128 | import * as s124 from "../tui/screens/tell_undo.ts"; | |
| 129 | import * as s125 from "../tui/screens/traffic.ts"; | |
| 130 | import * as s126 from "../tui/screens/trafficview.ts"; | |
| 131 | import * as s127 from "../tui/screens/valves.ts"; | |
| 132 | import * as s128 from "../tui/screens/waiting.ts"; | |
| 133 | import * as s129 from "../tui/screens/waiting_for_me.ts"; | |
| 134 | import * as s130 from "../tui/screens/wayback.ts"; | |
| 135 | import * as s131 from "../tui/screens/weekcard.ts"; | |
| 10 | import * as s6 from "../tui/screens/appleusage.ts"; | |
| 11 | import * as s7 from "../tui/screens/apps_crew.ts"; | |
| 12 | import * as s8 from "../tui/screens/archived.ts"; | |
| 13 | import * as s9 from "../tui/screens/ask_data.ts"; | |
| 14 | import * as s10 from "../tui/screens/audit.ts"; | |
| 15 | import * as s11 from "../tui/screens/bets.ts"; | |
| 16 | import * as s12 from "../tui/screens/billing.ts"; | |
| 17 | import * as s13 from "../tui/screens/billinglinks.ts"; | |
| 18 | import * as s14 from "../tui/screens/bills.ts"; | |
| 19 | import * as s15 from "../tui/screens/boards.ts"; | |
| 20 | import * as s16 from "../tui/screens/bottleneck.ts"; | |
| 21 | import * as s17 from "../tui/screens/buckets.ts"; | |
| 22 | import * as s18 from "../tui/screens/bugs.ts"; | |
| 23 | import * as s19 from "../tui/screens/bulk.ts"; | |
| 24 | import * as s20 from "../tui/screens/captain.ts"; | |
| 25 | import * as s21 from "../tui/screens/childcredit.ts"; | |
| 26 | import * as s22 from "../tui/screens/claim_link.ts"; | |
| 27 | import * as s23 from "../tui/screens/claim_start.ts"; | |
| 28 | import * as s24 from "../tui/screens/client_clouds.ts"; | |
| 29 | import * as s25 from "../tui/screens/cloud.ts"; | |
| 30 | import * as s26 from "../tui/screens/cloudtree.ts"; | |
| 31 | import * as s27 from "../tui/screens/codes.ts"; | |
| 32 | import * as s28 from "../tui/screens/connections.ts"; | |
| 33 | import * as s29 from "../tui/screens/controls.ts"; | |
| 34 | import * as s30 from "../tui/screens/cores.ts"; | |
| 35 | import * as s31 from "../tui/screens/crewbudgets.ts"; | |
| 36 | import * as s32 from "../tui/screens/crewfallback.ts"; | |
| 37 | import * as s33 from "../tui/screens/crewhandover.ts"; | |
| 38 | import * as s34 from "../tui/screens/crewkey.ts"; | |
| 39 | import * as s35 from "../tui/screens/crewsplits.ts"; | |
| 40 | import * as s36 from "../tui/screens/crewtop.ts"; | |
| 41 | import * as s37 from "../tui/screens/dataset.ts"; | |
| 42 | import * as s38 from "../tui/screens/design.ts"; | |
| 43 | import * as s39 from "../tui/screens/design_gallery.ts"; | |
| 44 | import * as s40 from "../tui/screens/devices.ts"; | |
| 45 | import * as s41 from "../tui/screens/disks.ts"; | |
| 46 | import * as s42 from "../tui/screens/displays.ts"; | |
| 47 | import * as s43 from "../tui/screens/domains.ts"; | |
| 48 | import * as s44 from "../tui/screens/dotfiles.ts"; | |
| 49 | import * as s45 from "../tui/screens/drift.ts"; | |
| 50 | import * as s46 from "../tui/screens/dutybudgets.ts"; | |
| 51 | import * as s47 from "../tui/screens/dutyswitch.ts"; | |
| 52 | import * as s48 from "../tui/screens/economics.ts"; | |
| 53 | import * as s49 from "../tui/screens/edgeonly.ts"; | |
| 54 | import * as s50 from "../tui/screens/experiments.ts"; | |
| 55 | import * as s51 from "../tui/screens/featurefeed.ts"; | |
| 56 | import * as s52 from "../tui/screens/features.ts"; | |
| 57 | import * as s53 from "../tui/screens/featureswitch.ts"; | |
| 58 | import * as s54 from "../tui/screens/feed.ts"; | |
| 59 | import * as s55 from "../tui/screens/flowsall.ts"; | |
| 60 | import * as s56 from "../tui/screens/footing.ts"; | |
| 61 | import * as s57 from "../tui/screens/forgeready.ts"; | |
| 62 | import * as s58 from "../tui/screens/forges.ts"; | |
| 63 | import * as s59 from "../tui/screens/forgeview.ts"; | |
| 64 | import * as s60 from "../tui/screens/front.ts"; | |
| 65 | import * as s61 from "../tui/screens/frontms.ts"; | |
| 66 | import * as s62 from "../tui/screens/funnel.ts"; | |
| 67 | import * as s63 from "../tui/screens/games.ts"; | |
| 68 | import * as s64 from "../tui/screens/gifts.ts"; | |
| 69 | import * as s65 from "../tui/screens/health_voice.ts"; | |
| 70 | import * as s66 from "../tui/screens/home_pins.ts"; | |
| 71 | import * as s67 from "../tui/screens/hostreach.ts"; | |
| 72 | import * as s68 from "../tui/screens/infra.ts"; | |
| 73 | import * as s69 from "../tui/screens/inventory.ts"; | |
| 74 | import * as s70 from "../tui/screens/invites.ts"; | |
| 75 | import * as s71 from "../tui/screens/knobs.ts"; | |
| 76 | import * as s72 from "../tui/screens/lapsed.ts"; | |
| 77 | import * as s73 from "../tui/screens/limits.ts"; | |
| 78 | import * as s74 from "../tui/screens/loopstalls.ts"; | |
| 79 | import * as s75 from "../tui/screens/madefrom.ts"; | |
| 80 | import * as s76 from "../tui/screens/maintainruns.ts"; | |
| 81 | import * as s77 from "../tui/screens/maintenance.ts"; | |
| 82 | import * as s78 from "../tui/screens/maintenanceweek.ts"; | |
| 83 | import * as s79 from "../tui/screens/mergedbranches.ts"; | |
| 84 | import * as s80 from "../tui/screens/mirrorpacks.ts"; | |
| 85 | import * as s81 from "../tui/screens/model.ts"; | |
| 86 | import * as s82 from "../tui/screens/money.ts"; | |
| 87 | import * as s83 from "../tui/screens/newissue.ts"; | |
| 88 | import * as s84 from "../tui/screens/next.ts"; | |
| 89 | import * as s85 from "../tui/screens/nowlist.ts"; | |
| 90 | import * as s86 from "../tui/screens/offers.ts"; | |
| 91 | import * as s87 from "../tui/screens/offsite.ts"; | |
| 92 | import * as s88 from "../tui/screens/orggrants.ts"; | |
| 93 | import * as s89 from "../tui/screens/orgteams.ts"; | |
| 94 | import * as s90 from "../tui/screens/originahead.ts"; | |
| 95 | import * as s91 from "../tui/screens/owner.ts"; | |
| 96 | import * as s92 from "../tui/screens/packrepos.ts"; | |
| 97 | import * as s93 from "../tui/screens/payments.ts"; | |
| 98 | import * as s94 from "../tui/screens/people.ts"; | |
| 99 | import * as s95 from "../tui/screens/pinned.ts"; | |
| 100 | import * as s96 from "../tui/screens/planner.ts"; | |
| 101 | import * as s97 from "../tui/screens/planrow.ts"; | |
| 102 | import * as s98 from "../tui/screens/plans.ts"; | |
| 103 | import * as s99 from "../tui/screens/previewdirs.ts"; | |
| 104 | import * as s100 from "../tui/screens/profile.ts"; | |
| 105 | import * as s101 from "../tui/screens/quotas.ts"; | |
| 106 | import * as s102 from "../tui/screens/recoverykit.ts"; | |
| 107 | import * as s103 from "../tui/screens/refused.ts"; | |
| 108 | import * as s104 from "../tui/screens/registry_items.ts"; | |
| 109 | import * as s105 from "../tui/screens/repolist.ts"; | |
| 110 | import * as s106 from "../tui/screens/requests.ts"; | |
| 111 | import * as s107 from "../tui/screens/resilience.ts"; | |
| 112 | import * as s108 from "../tui/screens/returning.ts"; | |
| 113 | import * as s109 from "../tui/screens/returns.ts"; | |
| 114 | import * as s110 from "../tui/screens/scorecard.ts"; | |
| 115 | import * as s111 from "../tui/screens/scrapes.ts"; | |
| 116 | import * as s112 from "../tui/screens/secrets.ts"; | |
| 117 | import * as s113 from "../tui/screens/sentinel.ts"; | |
| 118 | import * as s114 from "../tui/screens/series.ts"; | |
| 119 | import * as s115 from "../tui/screens/ship_time.ts"; | |
| 120 | import * as s116 from "../tui/screens/slowpages.ts"; | |
| 121 | import * as s117 from "../tui/screens/sold.ts"; | |
| 122 | import * as s118 from "../tui/screens/spend.ts"; | |
| 123 | import * as s119 from "../tui/screens/stalepulls.ts"; | |
| 124 | import * as s120 from "../tui/screens/subscriptions.ts"; | |
| 125 | import * as s121 from "../tui/screens/system.ts"; | |
| 126 | import * as s122 from "../tui/screens/tab.ts"; | |
| 127 | import * as s123 from "../tui/screens/teams.ts"; | |
| 128 | import * as s124 from "../tui/screens/telemetry.ts"; | |
| 129 | import * as s125 from "../tui/screens/tell_undo.ts"; | |
| 130 | import * as s126 from "../tui/screens/traffic.ts"; | |
| 131 | import * as s127 from "../tui/screens/trafficview.ts"; | |
| 132 | import * as s128 from "../tui/screens/valves.ts"; | |
| 133 | import * as s129 from "../tui/screens/waiting.ts"; | |
| 134 | import * as s130 from "../tui/screens/waiting_for_me.ts"; | |
| 135 | import * as s131 from "../tui/screens/wayback.ts"; | |
| 136 | import * as s132 from "../tui/screens/weekcard.ts"; | |
| 136 | 137 | |
| 137 | 138 | /** Each screen file that declares palette entries: its module and the export names. */ |
| 138 | 139 | export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readonly string[]])[] = [ |
| @@ -157,8 +158,8 @@ export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readon | ||
| 157 | 158 | [s18, ["entry"]], |
| 158 | 159 | [s19, ["entry"]], |
| 159 | 160 | [s20, ["entry"]], |
| 160 | [s21, ["entry","recoveryEntry"]], | |
| 161 | [s22, ["entry"]], | |
| 161 | [s21, ["entry"]], | |
| 162 | [s22, ["entry","recoveryEntry"]], | |
| 162 | 163 | [s23, ["entry"]], |
| 163 | 164 | [s24, ["entry"]], |
| 164 | 165 | [s25, ["entry"]], |
| @@ -216,11 +217,11 @@ export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readon | ||
| 216 | 217 | [s77, ["entry"]], |
| 217 | 218 | [s78, ["entry"]], |
| 218 | 219 | [s79, ["entry"]], |
| 219 | [s80, ["modelGrantEntry","modelCeilingEntry"]], | |
| 220 | [s81, ["entry"]], | |
| 220 | [s80, ["entry"]], | |
| 221 | [s81, ["modelGrantEntry","modelCeilingEntry"]], | |
| 221 | 222 | [s82, ["entry"]], |
| 222 | [s83, ["nextAllEntry"]], | |
| 223 | [s84, ["entry"]], | |
| 223 | [s83, ["entry"]], | |
| 224 | [s84, ["nextAllEntry"]], | |
| 224 | 225 | [s85, ["entry"]], |
| 225 | 226 | [s86, ["entry"]], |
| 226 | 227 | [s87, ["entry"]], |
| @@ -268,4 +269,5 @@ export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readon | ||
| 268 | 269 | [s129, ["entry"]], |
| 269 | 270 | [s130, ["entry"]], |
| 270 | 271 | [s131, ["entry"]], |
| 272 | [s132, ["entry"]], | |
| 271 | 273 | ]; |
src/http/api.ts +2
| @@ -361,6 +361,7 @@ import { chapterNames } from "../core/chapters.ts"; | ||
| 361 | 361 | import { takeOrder } from "../crew/take_order.ts"; |
| 362 | 362 | import { forgeCarries } from "../origins/words.ts"; |
| 363 | 363 | import { forgeOriginRoutes } from "./api/forge_origin.ts"; |
| 364 | import { appleTelemetryRoutes } from "./api/telemetry_apple.ts"; | |
| 364 | 365 | import { pushDeviceRoutes } from "./api/push_devices.ts"; |
| 365 | 366 | import { userRoutes } from "./api/user.ts"; |
| 366 | 367 | import { appRoutes } from "./api/app.ts"; |
| @@ -1268,6 +1269,7 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo | ||
| 1268 | 1269 | |
| 1269 | 1270 | userRoutes(router); |
| 1270 | 1271 | pushDeviceRoutes(router); |
| 1272 | appleTelemetryRoutes(router); | |
| 1271 | 1273 | forgeOriginRoutes(router); |
| 1272 | 1274 | adminRoutes(router); |
| 1273 | 1275 | runnerRoutes(router); |
src/http/api/telemetry_apple.ts added +52
| @@ -0,0 +1,52 @@ | ||
| 1 | /** | |
| 2 | * `POST /api/v1/telemetry/apple`: the Apple app's anonymous daily summary (#17962). Open to anyone, | |
| 3 | * signed in or not; whoever sends it is never read: no account, no address, no device id reaches | |
| 4 | * `recordUsage` (src/core/apple_usage.ts). The address is used only to count a minute's tries in | |
| 5 | * memory (30 a minute would be the page-view beacon's; here `RATE_CAPS.appleUsage`), and is | |
| 6 | * dropped with the request. At most `BODY_CAPS.appleUsage` bytes. Answers 204. | |
| 7 | */ | |
| 8 | ||
| 9 | import { USAGE_ERRORS, parseUsage, recordUsage } from "../../core/apple_usage.ts"; | |
| 10 | import { RateLimiter } from "../../core/ratelimit.ts"; | |
| 11 | import type { App } from "../../app.ts"; | |
| 12 | import type { Handler } from "../context.ts"; | |
| 13 | import { BODY_CAPS, RATE_CAPS, readBounded } from "../limits.ts"; | |
| 14 | import { clientBucket } from "../peer.ts"; | |
| 15 | import { apiError, tryAgain } from "../respond.ts"; | |
| 16 | import type { Router } from "../router.ts"; | |
| 17 | ||
| 18 | const limiter = (perMinute: number) => new RateLimiter({ perMinute, adminPerMinute: perMinute, enabled: true }); | |
| 19 | const limits = new WeakMap<App, { client: RateLimiter; forge: RateLimiter }>(); | |
| 20 | ||
| 21 | export function appleTelemetryRoutes(router: Router<Handler>): void { | |
| 22 | router.add("POST", "/api/v1/telemetry/apple", async (ctx) => { | |
| 23 | const { request, app } = ctx; | |
| 24 | let pair = limits.get(app); | |
| 25 | if (pair === undefined) { | |
| 26 | pair = { client: limiter(RATE_CAPS.appleUsage.perClient), forge: limiter(RATE_CAPS.appleUsage.perForge) }; | |
| 27 | limits.set(app, pair); | |
| 28 | } | |
| 29 | const verdicts = [ | |
| 30 | { v: pair.client.take(clientBucket(request), "ro"), of: `${RATE_CAPS.appleUsage.perClient} summaries a minute from one address` }, | |
| 31 | { v: pair.forge.take("forge", "ro"), of: `${RATE_CAPS.appleUsage.perForge} summaries a minute in all` }, | |
| 32 | ]; | |
| 33 | for (const { v, of } of verdicts) { | |
| 34 | if (!v.allowed) { | |
| 35 | return tryAgain("rate_limited", 429, `too many summaries: at most ${of}`, Math.max(1, v.retryAfter)); | |
| 36 | } | |
| 37 | } | |
| 38 | const cap = BODY_CAPS.appleUsage.bytes; | |
| 39 | const bytes = await readBounded(request, cap); | |
| 40 | if (bytes === "too_large") return apiError("too_large", 413, `summary too large: at most ${cap / 1024} KB (${cap} bytes)`); | |
| 41 | let input: unknown; | |
| 42 | try { | |
| 43 | input = JSON.parse(new TextDecoder().decode(bytes)); | |
| 44 | } catch { | |
| 45 | return apiError("invalid_body", 422, USAGE_ERRORS.invalid_body); | |
| 46 | } | |
| 47 | const read = parseUsage(input); | |
| 48 | if (!read.ok) return apiError(read.error, 422, USAGE_ERRORS[read.error]); | |
| 49 | recordUsage(app.db, read.value); | |
| 50 | return new Response(null, { status: 204, headers: { "cache-control": "no-store" } }); | |
| 51 | }); | |
| 52 | } |
src/http/lazy_lists.ts +2
| @@ -342,6 +342,7 @@ export const ADMIN_ROUTES = [ | ||
| 342 | 342 | ["GET", "/admin/limits"], |
| 343 | 343 | ["GET", "/admin/abstractions"], |
| 344 | 344 | ["GET", "/admin/flows"], |
| 345 | ["GET", "/admin/telemetry"], | |
| 345 | 346 | ["POST", "/admin/limits"], |
| 346 | 347 | ["POST", "/admin/quotas"], |
| 347 | 348 | ["POST", "/admin/plans"], |
| @@ -1235,6 +1236,7 @@ export const API_ROUTES = [ | ||
| 1235 | 1236 | ["GET", "/api/v1/user/devices"], |
| 1236 | 1237 | ["POST", "/api/v1/user/devices"], |
| 1237 | 1238 | ["DELETE", "/api/v1/user/devices/:id"], |
| 1239 | ["POST", "/api/v1/telemetry/apple"], | |
| 1238 | 1240 | ["GET", "/api/v1/version"], |
| 1239 | 1241 | ["GET", "/api/v1/user/repos"], |
| 1240 | 1242 | ["GET", "/api/v1/admin/pinned"], |
src/http/limits.ts +5
| @@ -47,6 +47,11 @@ export const RATE_CAPS = { | ||
| 47 | 47 | perForge: 600, |
| 48 | 48 | reason: "a page reports what its policy blocked, a handful a load; more from one address is a flood", |
| 49 | 49 | }, |
| 50 | appleUsage: { | |
| 51 | perClient: 10, | |
| 52 | perForge: 300, | |
| 53 | reason: "the app sends one summary a day; ten a minute from one address leaves room for retries and a shared network", | |
| 54 | }, | |
| 50 | 55 | } as const satisfies Record<string, { perClient: number; perForge: number; reason: string }>; |
| 51 | 56 | |
| 52 | 57 | const closing = new WeakSet<Request>(); |
src/http/marks/admin.json +2 −1
src/http/routes/admin.ts +2
| @@ -8,6 +8,7 @@ import { systemPage, systemStream, heapCensusSubmit } from "../../web/pages/syst | ||
| 8 | 8 | import { dataPage, adminDataPage } from "../../web/pages/data.ts"; |
| 9 | 9 | import { abstractionsPage } from "../../web/pages/abstractions.ts"; |
| 10 | 10 | import { limitsPage, limitsSet, quotasSet, signupSet, agentStartSet, billingLinksSet, inviteMint, inviteRevoke } from "../../web/pages/admin-limits.ts"; |
| 11 | import { telemetryPage } from "../../web/pages/admin-telemetry.ts"; | |
| 11 | 12 | import { allFlowsPage } from "../../web/pages/flows-all.ts"; |
| 12 | 13 | import { plansSet } from "../../web/settings/plans.ts"; |
| 13 | 14 | import type { Handler } from "../context.ts"; |
| @@ -26,6 +27,7 @@ export function adminRoutes(router: Router<Handler>, knownPatterns: () => string | ||
| 26 | 27 | router.add("GET", "/admin/limits", limitsPage); |
| 27 | 28 | router.add("GET", "/admin/abstractions", abstractionsPage); |
| 28 | 29 | router.add("GET", "/admin/flows", allFlowsPage); |
| 30 | router.add("GET", "/admin/telemetry", telemetryPage); | |
| 29 | 31 | router.add("POST", "/admin/limits", limitsSet); |
| 30 | 32 | router.add("POST", "/admin/quotas", quotasSet); |
| 31 | 33 | router.add("POST", "/admin/plans", plansSet); |
src/kernel/body_caps.ts +4
| @@ -63,6 +63,10 @@ export const BODY_CAPS = { | ||
| 63 | 63 | bytes: 8 * KIB, |
| 64 | 64 | reason: "a browser's Content-Security-Policy report: one violation, its URLs and a sample", |
| 65 | 65 | }, |
| 66 | appleUsage: { | |
| 67 | bytes: 8 * KIB, | |
| 68 | reason: "the Apple app's anonymous daily summary: a few dozen named counts and timings, so this is generous", | |
| 69 | }, | |
| 66 | 70 | gameSync: { |
| 67 | 71 | bytes: 2 * MIB, |
| 68 | 72 | reason: "a game box agent's sync: up to 200 log lines for each of its services, their stats and command results", |
src/tui/screens/appleusage.ts added +65
| @@ -0,0 +1,65 @@ | ||
| 1 | /** | |
| 2 | * The Apple app's anonymous usage in the terminal, a forge admin's (#17962): per app version its | |
| 3 | * summaries, counts and timing percentiles, as /admin/telemetry shows them (`readAppleUsage` is the | |
| 4 | * one gate). Read-only; `j`/`k` move. | |
| 5 | */ | |
| 6 | ||
| 7 | import { USAGE_DAYS, readAppleUsage } from "../../actions/apple_usage.ts"; | |
| 8 | import type { UsageRead } from "../../core/apple_usage.ts"; | |
| 9 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 10 | import { binding } from "../app/keys.ts"; | |
| 11 | import { style } from "../term/style.ts"; | |
| 12 | import { truncate } from "../term/width.ts"; | |
| 13 | import type { Session } from "../session.ts"; | |
| 14 | import { fill, len, split } from "../ui/layout.ts"; | |
| 15 | import { ListState, drawList, type ListRow } from "./common.ts"; | |
| 16 | import type { PaletteEntry } from "./entries.ts"; | |
| 17 | ||
| 18 | /** The rows: each version with its counts and timings under it, then each day's summaries. */ | |
| 19 | export function usageRows(read: UsageRead): ListRow[] { | |
| 20 | const versions = read.versions.map((v): ListRow => ({ | |
| 21 | title: v.version, | |
| 22 | secondary: `${v.reports} summaries`, | |
| 23 | meta: Object.entries(v.devices).map(([d, n]) => `${d} ${n}`).join(" · "), | |
| 24 | below: [ | |
| 25 | ...v.counts.slice(0, 8).map((c) => `${c.name} ${c.total}`), | |
| 26 | ...v.timings.map((t) => `${t.name} p50 ${t.p50} ms · p90 ${t.p90} ms (${t.n})`), | |
| 27 | ], | |
| 28 | })); | |
| 29 | const days = read.days.map((d): ListRow => ({ title: d.day, secondary: `${d.reports} summaries`, meta: d.counts.slice(0, 3).map((c) => `${c.name} ${c.total}`).join(" · ") })); | |
| 30 | return [...versions, ...days]; | |
| 31 | } | |
| 32 | ||
| 33 | export function appleUsageScreen(f: Session, ctx: Ctx): Screen { | |
| 34 | const state = new ListState(); | |
| 35 | return { | |
| 36 | title: "App usage", | |
| 37 | keys: [binding(["j", "k"], "move cursor")], | |
| 38 | render(area, s) { | |
| 39 | const c = ctx.theme.c; | |
| 40 | const [head, list] = split(area, "v", [len(1), fill(1)]); | |
| 41 | const r = readAppleUsage(f.app, f.user); | |
| 42 | const text = !r.ok | |
| 43 | ? "App usage is for a forge admin." | |
| 44 | : r.value.versions.length === 0 | |
| 45 | ? "Nothing yet: summaries come when people opt in in the Apple app." | |
| 46 | : `Anonymous summaries from the Apple app, the last ${USAGE_DAYS} days, per version then per day.`; | |
| 47 | s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); | |
| 48 | drawList(s, list!, c, state, r.ok ? usageRows(r.value) : []); | |
| 49 | }, | |
| 50 | key(e) { | |
| 51 | const r = readAppleUsage(f.app, f.user); | |
| 52 | return state.key(e, r.ok ? usageRows(r.value).length : 0); | |
| 53 | }, | |
| 54 | }; | |
| 55 | } | |
| 56 | ||
| 57 | export const entry: PaletteEntry = { | |
| 58 | rank: 535, | |
| 59 | id: "app-usage", | |
| 60 | title: "App usage", | |
| 61 | hint: "the Apple app's anonymous usage: counts and timings per version", | |
| 62 | after: ["limits"], | |
| 63 | offered: (session) => session.user?.isAdmin === true && session.user.kind !== "agent", | |
| 64 | run: (session, ctx) => ctx.push(appleUsageScreen(session, ctx)), | |
| 65 | }; |
src/view/policy.ts +5
| @@ -43,6 +43,11 @@ export function privacyRows(contact: string): PolicyRow[] { | ||
| 43 | 43 | label: "The Apple app", |
| 44 | 44 | words: "It is a client of the forge you sign in to. What it sends to that forge is kept by that forge, as described on this page.", |
| 45 | 45 | }, |
| 46 | { | |
| 47 | label: "Usage in the Apple app", | |
| 48 | words: | |
| 49 | "Only if you opt in (one question after you sign in; change it in the app's Settings), the app sends the forge you are signed in to one summary a day: counts of what was used and timings. It is kept as totals per day, app version and device kind. No account, address or device identifier is stored, and it goes to no other forge. Usage Data and Diagnostics, not linked to you, not used for tracking.", | |
| 50 | }, | |
| 46 | 51 | { |
| 47 | 52 | label: "Tracking and ads", |
| 48 | 53 | words: "No ads. No tracking across sites. Nothing about you is sold or shared.", |
src/web/pages/admin-limits.ts +1
| @@ -218,6 +218,7 @@ export function limitsPage(ctx: Ctx): Response { | ||
| 218 | 218 | items.length === 0 ? null : html`<section class="settings-block"><h2>${title}</h2><ul class="list">${items}</ul></section>`; |
| 219 | 219 | const body = html`<p class="small muted mb"> |
| 220 | 220 | Every limit you can hit, and why. A change applies to the next request that meets it; nothing needs a restart. |
| 221 | <a href="/admin/telemetry">App usage</a> | |
| 221 | 222 | </p> |
| 222 | 223 | <dl class="status-rows">${signupRow}${agentStartRow}${billingRow}${inviteRow}${quotaRow}${plansRow(ctx)}</dl> |
| 223 | 224 | ${historyBlock("Invite links made", inviteRows)} |
src/web/pages/admin-telemetry.ts added +66
| @@ -0,0 +1,66 @@ | ||
| 1 | /** | |
| 2 | * GET /admin/telemetry: what the Apple app's anonymous daily summaries add up to (#17962), for a | |
| 3 | * forge admin. Daily totals, and per app version the counts and the timings' percentiles (of the | |
| 4 | * summaries' own timings: a p50 here is the middle of the devices' daily p50s). Anyone else gets | |
| 5 | * the not-found page. The terminal's door is `src/tui/screens/appleusage.ts`. | |
| 6 | */ | |
| 7 | ||
| 8 | import { USAGE_DAYS, readAppleUsage } from "../../actions/apple_usage.ts"; | |
| 9 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 10 | import type { Ctx } from "../../http/context.ts"; | |
| 11 | import { notFoundPage, pageHead } from "../chrome.ts"; | |
| 12 | import { html, type Html } from "../html.ts"; | |
| 13 | import { page } from "../layout.ts"; | |
| 14 | import { statusRow } from "./security.ts"; | |
| 15 | ||
| 16 | const TOP = 12; | |
| 17 | ||
| 18 | export function telemetryPage(ctx: Ctx): Response { | |
| 19 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 20 | const read = timePhase(ctx.request, "usage", () => readAppleUsage(ctx.app, ctx.auth.user)); | |
| 21 | if (!read.ok) return notFoundPage(ctx); | |
| 22 | const { days, versions } = read.value; | |
| 23 | const rendering = performance.now(); | |
| 24 | const rows = (items: Html[]): Html => html`<dl class="status-rows">${items}</dl>`; | |
| 25 | const body = | |
| 26 | versions.length === 0 | |
| 27 | ? html`<p class="muted" id="telemetry-none">Nothing yet. Summaries arrive when people opt in to share usage in the Apple app.</p>` | |
| 28 | : html`<section class="settings-block"> | |
| 29 | <h2>By day</h2> | |
| 30 | ${rows( | |
| 31 | days.map((d) => | |
| 32 | statusRow( | |
| 33 | d.day, | |
| 34 | html`${String(d.reports)} ${d.reports === 1 ? "summary" : "summaries"}${d.counts.length === 0 ? "" : " · "}<span class="muted">${d.counts | |
| 35 | .slice(0, 5) | |
| 36 | .map((c) => `${c.name} ${c.total}`) | |
| 37 | .join(" · ")}</span>`, | |
| 38 | ), | |
| 39 | ), | |
| 40 | )} | |
| 41 | </section> | |
| 42 | ${versions.map( | |
| 43 | (v) => html`<section class="settings-block"> | |
| 44 | <h2>${v.version}</h2> | |
| 45 | <p class="small muted">${String(v.reports)} summaries · ${Object.entries(v.devices) | |
| 46 | .map(([d, n]) => `${d} ${n}`) | |
| 47 | .join(" · ")}</p> | |
| 48 | ${rows(v.counts.slice(0, TOP).map((c) => statusRow(c.name, html`<span class="mono">${String(c.total)}</span>`)))} | |
| 49 | ${rows( | |
| 50 | v.timings.map((t) => | |
| 51 | statusRow(t.name, html`<span class="mono">p50 ${String(t.p50)} ms · p90 ${String(t.p90)} ms</span> <span class="muted">${String(t.n)} summaries</span>`), | |
| 52 | ), | |
| 53 | )} | |
Principles 7 and 3: the per-version table mixes counts and timings in one grid, and the count rows carry two empty |
||
| 54 | </section>`, | |
| 55 | )}`; | |
| 56 | const out = page({ | |
| 57 | ctx, | |
| 58 | title: "App usage", | |
| 59 | body: html`${pageHead("App usage", { | |
| 60 | sub: `What the Apple app's opted-in people used, per day and per version, the last ${USAGE_DAYS} days.`, | |
| 61 | })} | |
| 62 | ${body}`, | |
| 63 | }); | |
| 64 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 65 | return out; | |
Principles 1 and 8: the sub is two sentences. The second ("Kept per day, app version and device kind only: no account, address or device id") repeats what /privacy and docs/features/apple-usage.md already say, and the p50 caveat sits only in a code comment. Cut it to one plain sentence on what the page is for, for example "What the Apple app's opted-in people used, per day and per version." |
||
| 66 | } |
test/apple-usage-route.test.ts added +119
| @@ -0,0 +1,119 @@ | ||
| 1 | // The Apple app's anonymous summary route, its owner page and its terminal door (#17962). | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { readAppleUsage } from "../src/actions/apple_usage.ts"; | |
| 8 | import { createToken } from "../src/core/tokens.ts"; | |
| 9 | import { createUser, type User } from "../src/core/users.ts"; | |
| 10 | import { BODY_CAPS, RATE_CAPS } from "../src/http/limits.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { usageRows } from "../src/tui/screens/appleusage.ts"; | |
| 13 | ||
| 14 | let dir = ""; | |
| 15 | let app: App; | |
| 16 | let servers: Servers; | |
| 17 | let base = ""; | |
| 18 | let admin = ""; | |
| 19 | let member = ""; | |
| 20 | let owner: User; | |
| 21 | let bob: User; | |
| 22 | ||
| 23 | beforeAll(async () => { | |
| 24 | dir = await mkdtemp(join(tmpdir(), "apple-usage-")); | |
| 25 | app = createApp({ dataDir: dir, port: 0 }); | |
| 26 | servers = await serve(app); | |
| 27 | base = `http://127.0.0.1:${servers.port}`; | |
| 28 | const mk = async (username: string, extra: Record<string, unknown> = {}) => { | |
| 29 | const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", ...extra }); | |
| 30 | if (!u.ok) throw new Error(u.error); | |
| 31 | return u.value; | |
| 32 | }; | |
| 33 | owner = await mk("alice", { adminIfFirst: true }); | |
| 34 | bob = await mk("bob"); | |
| 35 | admin = createToken(app.db, owner.id, "a", "admin").token; | |
| 36 | member = createToken(app.db, bob.id, "b", "admin").token; | |
| 37 | }, 30_000); | |
| 38 | afterAll(async () => { | |
| 39 | await servers.stop(); | |
| 40 | app.close(); | |
| 41 | await rm(dir, { recursive: true, force: true }); | |
| 42 | }); | |
| 43 | ||
| 44 | const today = () => new Date().toISOString().slice(0, 10); | |
| 45 | const summary = (over: Record<string, unknown> = {}) => ({ | |
| 46 | v: 1, | |
| 47 | day: today(), | |
| 48 | app: { version: "1.4", build: "212" }, | |
| 49 | os: "iOS 26.1", | |
| 50 | device: "iPhone", | |
| 51 | counts: { "screen.now": 12 }, | |
| 52 | timings: { "code.open.p50": 120 }, | |
| 53 | ...over, | |
| 54 | }); | |
| 55 | const send = (body: unknown, headers: Record<string, string> = {}) => | |
| 56 | fetch(`${base}/api/v1/telemetry/apple`, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: typeof body === "string" ? body : JSON.stringify(body) }); | |
| 57 | const rows = () => app.db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM apple_usage").get()!.n; | |
| 58 | ||
| 59 | test("a signed-out summary is kept as 204 and the owner reads it on the page and in the terminal's rows", async () => { | |
| 60 | const res = await send(summary()); | |
| 61 | expect(res.status).toBe(204); | |
| 62 | expect(rows()).toBeGreaterThan(0); | |
| 63 | const page = await fetch(`${base}/admin/telemetry`, { headers: { authorization: `Bearer ${admin}` } }); | |
| 64 | expect(page.status).toBe(200); | |
| 65 | const text = await page.text(); | |
| 66 | expect(text).toContain("1.4 (212)"); | |
| 67 | expect(text).toContain("screen.now"); | |
| 68 | expect(text).toContain("code.open.p50"); | |
| 69 | const read = readAppleUsage(app, owner); | |
| 70 | expect(read.ok).toBe(true); | |
| 71 | if (read.ok) expect(usageRows(read.value).map((r) => r.title)).toContain("1.4 (212)"); | |
| 72 | }); | |
| 73 | ||
| 74 | test("a member and a stranger are refused the page, and the terminal reads nothing", async () => { | |
| 75 | expect((await fetch(`${base}/admin/telemetry`, { headers: { authorization: `Bearer ${member}` } })).status).toBe(403); | |
| 76 | expect((await fetch(`${base}/admin/telemetry`)).status).toBeOneOf([401, 404]); | |
| 77 | expect(readAppleUsage(app, bob)).toEqual({ ok: false, error: "not_found" }); | |
| 78 | expect(readAppleUsage(app, null)).toEqual({ ok: false, error: "not_found" }); | |
| 79 | }); | |
| 80 | ||
| 81 | test("a signed-in sender is kept just the same: nothing about the account is stored", async () => { | |
| 82 | const before = JSON.stringify(app.db.query("SELECT * FROM apple_usage").all()); | |
| 83 | expect((await send(summary(), { authorization: `Bearer ${member}` })).status).toBe(204); | |
| 84 | const after = JSON.stringify(app.db.query("SELECT * FROM apple_usage").all()); | |
| 85 | expect(after).not.toBe(before); | |
| 86 | expect(after).not.toContain(bob.id); | |
| 87 | expect(after).not.toContain("bob"); | |
| 88 | expect(after).not.toContain("127.0.0.1"); | |
| 89 | }); | |
| 90 | ||
| 91 | test("a bad body is a 422 naming its rule, and stores nothing", async () => { | |
| 92 | const before = rows(); | |
| 93 | const res = await send(summary({ counts: { "a b": 1 } })); | |
| 94 | expect(res.status).toBe(422); | |
| 95 | expect(((await res.json()) as { error: string }).error).toBe("invalid_counts"); | |
| 96 | expect((await send("{nope")).status).toBe(422); | |
| 97 | expect(rows()).toBe(before); | |
| 98 | }); | |
| 99 | ||
| 100 | test("a body over 8 KB is a 413 naming the limit", async () => { | |
| 101 | const cap = BODY_CAPS.appleUsage.bytes; | |
| 102 | expect(cap).toBe(8 * 1024); | |
| 103 | const res = await send(summary({ os: "x".repeat(cap) })); | |
| 104 | expect(res.status).toBe(413); | |
| 105 | expect(await res.text()).toContain("8 KB"); | |
| 106 | }); | |
| 107 | ||
| 108 | test("an address past its minute's summaries gets a 429 with Retry-After", async () => { | |
| 109 | let last = 0; | |
| 110 | let wait: string | null = null; | |
| 111 | for (let i = 0; i < RATE_CAPS.appleUsage.perClient + 2; i++) { | |
| 112 | const res = await send(summary()); | |
| 113 | last = res.status; | |
| 114 | wait = res.headers.get("Retry-After"); | |
| 115 | if (last === 429) break; | |
| 116 | } | |
| 117 | expect(last).toBe(429); | |
| 118 | expect(Number(wait)).toBeGreaterThanOrEqual(1); | |
| 119 | }); |
test/apple-usage.test.ts added +105
| @@ -0,0 +1,105 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { Database } from "bun:sqlite"; | |
| 3 | import { MAX_VERSIONS, MAX_VERSION_NAMES, type UsageError, bucketMs, bucketOf, parseUsage, readUsage, recordUsage } from "../src/core/apple_usage.ts"; | |
| 4 | import migration from "../src/core/migrations/20261203090000-apple-usage.ts"; | |
| 5 | ||
| 6 | const NOW = new Date("2026-12-03T12:00:00Z"); | |
| 7 | const body = (over: Record<string, unknown> = {}) => ({ | |
| 8 | v: 1, | |
| 9 | day: "2026-12-03", | |
| 10 | app: { version: "1.4", build: "212" }, | |
| 11 | os: "iOS 26.1", | |
| 12 | device: "iPhone", | |
| 13 | counts: { "screen.now": 12, "queue.cleared": 7 }, | |
| 14 | timings: { "code.open.p50": 120 }, | |
| 15 | ...over, | |
| 16 | }); | |
| 17 | const fresh = () => { | |
| 18 | const db = new Database(":memory:"); | |
| 19 | db.exec(migration); | |
| 20 | return db; | |
| 21 | }; | |
| 22 | const keep = (db: Database, over: Record<string, unknown> = {}) => { | |
| 23 | const r = parseUsage(body(over), NOW); | |
| 24 | if (!r.ok) throw new Error(r.error); | |
| 25 | recordUsage(db, r.value, NOW); | |
| 26 | }; | |
| 27 | ||
| 28 | describe("parseUsage", () => { | |
| 29 | test("a well-formed summary is read, an unknown device is other", () => { | |
| 30 | const r = parseUsage(body({ device: "Toaster", metrickit: { x: 1 } }), NOW); | |
| 31 | expect(r).toMatchObject({ ok: true, value: { version: "1.4 (212)", device: "other" } }); | |
| 32 | }); | |
| 33 | test.each<[Record<string, unknown>, UsageError]>([ | |
| 34 | [{ v: 2 }, "invalid_v"], | |
| 35 | [{ day: "2026-12-09" }, "invalid_day"], | |
| 36 | [{ day: "2026-02-31x" }, "invalid_day"], | |
| 37 | [{ app: { version: "x", build: "1" } }, "invalid_app"], | |
| 38 | [{ os: "" }, "invalid_os"], | |
| 39 | [{ counts: { "a b": 1 } }, "invalid_counts"], | |
| 40 | [{ counts: { a: 1.5 } }, "invalid_counts"], | |
| 41 | [{ counts: { a: -1 } }, "invalid_counts"], | |
| 42 | [{ timings: { a: "fast" } }, "invalid_timings"], | |
| 43 | [{ metrickit: [] }, "invalid_metrickit"], | |
| 44 | ])("%j is %s", (over, error) => { | |
| 45 | expect(parseUsage(body(over), NOW)).toEqual({ ok: false, error }); | |
| 46 | }); | |
| 47 | test("more than 64 names is refused", () => { | |
| 48 | const counts = Object.fromEntries(Array.from({ length: 65 }, (_, i) => [`n${i}`, 1])); | |
| 49 | expect(parseUsage(body({ counts }), NOW)).toEqual({ ok: false, error: "invalid_counts" }); | |
| 50 | }); | |
| 51 | }); | |
| 52 | ||
| 53 | describe("recordUsage and readUsage", () => { | |
| 54 | test("counts sum, summaries count and a timing reads as a percentile", () => { | |
| 55 | const db = fresh(); | |
| 56 | keep(db); | |
| 57 | keep(db, { counts: { "screen.now": 3 }, timings: { "code.open.p50": 400 }, device: "Mac" }); | |
| 58 | const read = readUsage(db, 14, NOW); | |
| 59 | expect(read.days).toEqual([{ day: "2026-12-03", reports: 2, counts: [{ name: "screen.now", total: 15 }, { name: "queue.cleared", total: 7 }] }]); | |
| 60 | const v = read.versions[0]!; | |
| 61 | expect(v).toMatchObject({ version: "1.4 (212)", reports: 2, devices: { iPhone: 1, Mac: 1 } }); | |
| 62 | const t = v.timings[0]!; | |
| 63 | expect(t.n).toBe(2); | |
| 64 | expect(t.p50).toBe(bucketMs(bucketOf(120))); | |
| 65 | expect(t.p90).toBe(bucketMs(bucketOf(400))); | |
| 66 | }); | |
| 67 | test("nothing identifying is a column", () => { | |
| 68 | const db = fresh(); | |
| 69 | keep(db); | |
| 70 | const cols = db.query<{ name: string }, []>("PRAGMA table_info(apple_usage)").all().map((c) => c.name); | |
| 71 | expect(cols).toEqual(["day", "version", "device", "name", "kind", "bucket", "n", "total"]); | |
| 72 | }); | |
| 73 | test("a flood of made-up versions does not hide a version that came earlier", () => { | |
| 74 | const db = fresh(); | |
| 75 | keep(db, { app: { version: "1.4", build: "212" }, counts: { real: 5 } }); | |
| 76 | for (let i = 0; i < MAX_VERSIONS + 30; i++) keep(db, { app: { version: `9.${i}`, build: "1" }, counts: { fake: 1 } }); | |
| 77 | keep(db, { app: { version: "1.4", build: "212" }, counts: { real: 2 } }); | |
| 78 | const read = readUsage(db, 14, NOW); | |
| 79 | expect(read.versions.find((v) => v.version === "1.4 (212)")?.counts).toEqual([{ name: "real", total: 7 }]); | |
| 80 | expect(read.versions.map((v) => v.version)).toContain("(other)"); | |
| 81 | expect(read.versions.filter((v) => v.version !== "(other)")).toHaveLength(MAX_VERSIONS); | |
| 82 | }); | |
| 83 | test("a flood of made-up names spoils only its own version", () => { | |
| 84 | const db = fresh(); | |
| 85 | keep(db, { app: { version: "2.0", build: "1" }, counts: { real: 5 } }); | |
| 86 | for (let i = 0; i < 4; i++) { | |
| 87 | const counts = Object.fromEntries(Array.from({ length: 64 }, (_, j) => [`junk${i}x${j}`, 1])); | |
| 88 | keep(db, { app: { version: "6.6", build: "6" }, counts }); | |
| 89 | } | |
| 90 | const read = readUsage(db, 14, NOW); | |
| 91 | const junk = read.versions.find((v) => v.version === "6.6 (6)")!; | |
| 92 | expect(junk.counts.length).toBeLessThanOrEqual(MAX_VERSION_NAMES + 1); | |
| 93 | expect(junk.counts.map((c) => c.name)).toContain("(more)"); | |
| 94 | keep(db, { app: { version: "2.0", build: "1" }, counts: { another: 1 } }); | |
| 95 | expect(readUsage(db, 14, NOW).versions.find((v) => v.version === "2.0 (1)")?.counts.map((c) => c.name).sort()).toEqual(["another", "real"]); | |
| 96 | }); | |
| 97 | test("a day beyond the window is not read, and old days are dropped", () => { | |
| 98 | const db = fresh(); | |
| 99 | keep(db, { day: "2026-12-01" }); | |
| 100 | expect(readUsage(db, 1, NOW).days).toEqual([]); | |
| 101 | db.query("UPDATE apple_usage SET day = '2025-01-01'").run(); | |
| 102 | keep(db); | |
| 103 | expect(db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM apple_usage WHERE day < '2026-01-01'").get()!.n).toBe(0); | |
| 104 | }); | |
| 105 | }); |
test/forge-looks.test.ts +2 −1
| @@ -25,6 +25,7 @@ const NOT_A_PAGE: Record<string, string> = { | ||
| 25 | 25 | "/home": "the home's old address: a redirect to /, which is pictured", |
| 26 | 26 | "/desk": "a POST that leaves a note on the desk; the desk is read on the home (/), which is pictured; covered by test/desk.test.ts", |
| 27 | 27 | "/client-clouds": "no picture: not in app.looks; a guest sees the not-found page, and the page is covered by test/client-cloud.test.ts", |
| 28 | "/docs": "no picture: its slot went to /admin/telemetry, the App usage page (#17962); static docs, covered by test/docs-index.test.ts", | |
| 28 | 29 | "/design": "no picture: not in app.looks (its slots are full); the Design tab is covered by test/design-theme.test.ts", |
| 29 | 30 | "/feed": "no picture: its slot went to /docs; the page is covered by its own tests", |
| 30 | 31 | "/front": "no picture: its slot went to /settings/security/controls, the security controls (#13001); covered by its own tests", |
| @@ -211,7 +212,7 @@ test("`/` signed out is its own look at phone and desktop, taken with no session | ||
| 211 | 212 | test("a look below the top level is the preview seed's, taken as the owner", () => { |
| 212 | 213 | const { looks, looksAsOwner } = manifest(); |
| 213 | 214 | for (const p of looks.filter((p) => !isTopLevel(p))) { |
| 214 | expect(p.startsWith("/preview-admin/sample") || p === "/settings/crew" || p === "/settings/captain" || p === "/settings/security/controls" || p === "/settings/grants" || p === "/admin/limits" || p === "/admin/abstractions" || p === "/looks/missing" || p === "/looks/failed" || p === "/nope/nothing" || isHelpLook(p)).toBe(true); | |
| 215 | expect(p.startsWith("/preview-admin/sample") || p === "/settings/crew" || p === "/settings/captain" || p === "/settings/security/controls" || p === "/settings/grants" || p === "/admin/limits" || p === "/admin/telemetry" || p === "/admin/abstractions" || p === "/looks/missing" || p === "/looks/failed" || p === "/nope/nothing" || isHelpLook(p)).toBe(true); | |
| 215 | 216 | // the not-found and failed pages are the same page to everyone: no owner needed |
| 216 | 217 | if (!p.startsWith("/looks/") && p !== "/nope/nothing") expect(looksAsOwner).toContain(p); |
| 217 | 218 | } |
test/palette-roster.test.ts +1
| @@ -201,6 +201,7 @@ test("a site-admin person is offered the whole roster", () => { | ||
| 201 | 201 | ["account-access", "Account access", "search people, reset access, end sessions"], |
| 202 | 202 | ["quotas", "Account quotas", "what one account may own, and who changed it"], |
| 203 | 203 | ["limits", "Limits", "what the forge caps, with default and bounds; change one"], |
| 204 | ["app-usage", "App usage", "the Apple app's anonymous usage: counts and timings per version"], | |
| 204 | 205 | ["plans", "Plans", "free, small, medium and large: size, price, crew credit, forges; change one"], |
| 205 | 206 | ["economics", "Economics", "the profit slider's position, the margin, the owned-node label and the prices; change one"], |
| 206 | 207 | ["flows-all", "Flows across the forge", "every repository's flow lines, by kind and stale"], |
Principle 7 (rows over boxes): the By day table is a list of facts. Render it as rows (a
dl/list row per day: day, summaries, most counted), not a table with a caption and scroll wrapper.