codeburn/mac/Sources/CodeBurnMenubar/Data/Telemetry.swift
Resham Joshi 2fc62f7876
0.9.25: correctness, energy, menu bar and installer fixes (#1511)
* 0.9.25: correctness + provider fixes, energy guard, logo, default DMG

CLI/engine fixes: local-model savings count reasoning; ZCode reasoning billed;
codex-credits version-boundary tiering; GLM alias repointed to z.ai discounted rate;
openclaw/pi/qwen timestamp mtime fallback; hermes delta timestamp;
sqlite/warp/hermes busy+permission reclassified as failed (no sealed empty);
codex-cache dev+ino; cursor-cache WAL; antigravity server TTL; quota timeout+
grokbot footer+claude near-expiry; serve RSS guard env+hysteresis; CSV export
subfolder; providers pane fixed window; Warp credit-based cost.
Desktop/menubar: unified flame logo; default DMG; docs signed+notarized.

* menubar: one running instance, self-relaunch on language change

The single-instance guard only retired strictly older copies, so two login
items starting in the same instant both survived, and a copy whose bundle
identity did not survive install was never seen at all. The decision is now
total (newest launch wins, ties break on pid) and peers are matched on bundle
id or executable name. --keep-both opts one launch out.

Language changes no longer have the desktop app quit and reopen the menu bar,
which made macOS attribute the new process to the desktop app and re-ask for
the data-access grant. The menu bar relaunches itself via the existing remote
command key; older menu bars that do not watch the key fall back to the old
path after 5s.

* desktop: export toast shows the saved path; resource buckets on app_close

The export toast showed the folder that was picked, not the dated subfolder
the files land in. The handler already parsed the CLI's saved line; it now
returns that path and the toast uses it.

app_close carries cpu/mem (and serveCpu/serveMem when serve reported) as bucket
labels, sampled on the existing flush interval. serve adds cpuSec and peak rssMb
to its answer frames. Unmeasured figures are omitted, never sent as zero.

* menubar guard never leaves zero instances; installer replaces in place; JSON export into folder

Guard: an unreadable launch date on either side could make two copies retire
each other. Unreadable peers now count only when their pid is lower, and a copy
that cannot read its own date retires lower pids only, so a disagreement keeps
at least one instance.

Installer: an existing bundle in /Applications or ~/Applications is replaced
where it lives instead of a second copy being added. The old bundle is moved
aside and restored on any failure; a cross-volume move falls back to a copy that
is re-verified with codesign. An unwritable location is never escalated: the
install goes to ~/Applications and the leftover is named.

Export: -o pointing at an existing folder writes the JSON inside it. Short
sessions omit the cpu buckets. setLanguage reopens the menu bar if its own
relaunch never comes back. AppRelaunch passes the bundle path as an argument.

* menubar guard orders by kernel start time; installer opens the copy that exists and recovers a killed install

Guard: launch dates could be unreadable and pid order can disagree with start
order after pid reuse, so two copies could still retire each other. Ordering now
uses the kernel-reported process start time, which both sides read identically.
A copy whose start time cannot be read is left out, so the worst case is two
menu bars, never none.

Installer: with the only copy in an unwritable /Applications, a plain run tried
to open a path that did not exist. It now opens the installed copy. A placement
killed mid-way is recovered on the next run, a concurrent install is refused,
and a failed restore names where the previous bundle is. Process matching uses
the exact executable name.

Export: a trailing separator names a folder. Telemetry cpu is measured from
telemetry start.

* installer: a recycled pid can no longer block installs; language change waits for the replacement process

An aside left by an earlier install counted as an active install whenever its
pid was live, so once that pid was reused every install was refused for good.
It now also has to be recent (ctime within 10 minutes), a pid we may not signal
is not ours, the refusal names the file, and an orphan that cannot be cleared is
reported instead of aborting the install.

setLanguage confirmed the relaunch with a matcher the dying process still
satisfied, so the reopen fallback never fired. It now waits for a pid that was
not running before.

* desktop: only the headline refreshes live; act, yield and the optimize scan move to slower tiers

An open window ran act report (a fresh CLI process each time), yield and an
optimize scan on every refresh. The headline poll now passes --no-optimize and
takes the snapshot path; act report refreshes on mount, on manual refresh and
every 10 minutes, yield every 5. The optimize scan runs at most once a day per
query scope, is stored in userData with its time, and is forced fresh by the
Optimize page or a manual refresh. Figures that are not live show their age,
and no stored scan means no savings clause rather than a zero.

On battery the live interval doubles; the chosen cadence stays the base.

* desktop: a stored optimize scan is only valid on the day it was computed; cadence changes no longer refetch

The scan cache is keyed on the argv, which says 'today' or '30days', not a date,
so yesterday's scan was served as today's. It is now reused only on the same
local day. Stored findings in Signals carry their age, a failed scan shows the
error instead of scanning forever, and the cold-boot label uses the time the app
received the payload.

usePolled fetched immediately whenever its interval changed, so every power
transition (and launching on battery) refetched every live hook and could spawn
an extra act report. Fetching and timer arming are now separate effects.
Optimize's yield is on the same 5 minute tier as Overview.

* desktop: re-ask for the optimize scan on the first render of a new local day

An app left open across midnight kept yesterday's stored savings under Today,
because the scan was only requested on mount, scope change or manual refresh.
The local day is now one of the hook's triggers; the previous day's figure is
dropped while the new scan runs.

* serve: a menubar poll is never answered from the status snapshot

Inside its settle window the status snapshot returns the pre-change payload on
purpose. The resident serve process then memoized that answer as clean, so after
a single write followed by quiet the headline stayed on the old figure until the
next write or the five minute memo cap. The snapshot is for one-shot runs; the
resident process already holds the parsed state, so it now skips it.

* tests stop writing the real serve pid file; superseded caches are swept; installer notes reach the desktop card

ServeConnection takes its pid file as a parameter, so the Swift tests no longer
share ~/.cache/codeburn/menubar-serve.pid with a running menu bar, and the
reaper only signals a record that is a serve --stdio command.

Superseded session-cache, codex-results and cursor-results files (100MB+ each)
are removed once untouched for 14 days, at most once a day. daily-cache and the
hermes ledger are never swept: they carry sealed days a version bump adopts.

The menu bar installer's leftover-copy advice is emitted as a marked line under
CODEBURN_PROGRESS and shown on the desktop card. The skipped-refresh NSLog is
debug only. The app's vitest workers turn off Node's built-in web storage.

* menubar: anonymous usage statistics, under the same consent as the desktop and the Windows tray

The Swift menu bar sent nothing, so every codeburn-menubar row was a Windows
tray. It now follows the desktop's telemetry.v1.json when that exists (read
only: same install id, same on/off, onboarded required) and otherwise keeps its
own state with the same region default (off in EU/EEA/UK/CH and when the region
is unknown). Settings gains one Privacy toggle, disabled when the desktop owns
the choice. Events use the tray's names; the daily usage snapshot is the CLI's
block passed through a sanitizer and is left to the desktop when it is the
consent source. Debug builds never send.

A resident CLI that gets disabled for the run now says so in the log.

* menubar statistics: private network session, abandoned batch on opt-out, a desktop No survives the desktop going away

The sender used URLSession.shared, so a server or CDN cookie and the default
User-Agent rode along and would have outlived an opt-out. It now owns an
ephemeral session with no cookies, no cache, a fixed agent string and no
redirects. A batch that is out when the toggle goes off is dropped rather than
re-queued under the fresh id. The desktop's answer is recorded locally, so a
missing or damaged state file never falls back to the region default, and the
standalone id is persisted when minted. The toggle stays usable as a local off
switch under desktop consent. Copy states what this app actually sends.

* vercel gateway spend is shown on its own row and kept out of totals by default

The gateway reports daily aggregates per model, with nothing to match against
the local tools that were pointed at it, so every headline counted that traffic
twice. Totals now leave the gateway slice out when all providers are shown; the
row keeps its full amount and is marked not in total, --provider vercel-gateway
is unchanged, and codeburn gateway-totals include folds it back in. The daily
cache still stores the slice, so the choice applies retroactively. Reports with
no gateway credential are byte-identical.

The gateway's request_count now feeds the call count instead of each daily row
counting as one call. Hermes: a mid-session price change lands on the day it is
observed and sealed days are never rewritten; stated in the code and changelog.

* gateway exclusion applies to every report through the parse, and the detail panels follow the headline

The first pass narrowed only the day totals, so By tool, top models, top
projects, activities, status json projects and any project-filtered table still
carried gateway spend next to a headline that did not, and By tool shares could
pass 100%. The rule now lives in parseAllSessions: with all providers in scope
and the opt-in off, aggregate-only sessions are dropped after the memo, and the
callers that derive cached days opt out so stored slices are unchanged.
day.projects is reduced like models. The gateway stays visible once, as a
labelled not-in-total row with no share, and standalone reports note the
excluded amount on stderr so machine output is unchanged.

* dashboard: the background history index follows the gateway exclusion too

The index keeps the whole corpus so it can fill the daily cache, but its
per-period projection handed that unfiltered list to the project, model and
activity panels, so they regained gateway spend once the index landed.

* ci: a hung lock suite fails in 90s and retries once with a warning; slow Windows tests get measured limits

The cache-lock step failed 6 of the last 60 runs with no output for five minutes
after the vitest banner; passing runs take 12-18s on every Node version. Each
attempt is now capped at 90s and retried once, with a warning whenever that
happens. On windows-latest a single CLI spawn takes 1.4-12s, so the four-spawn
status snapshot test and the c6 parser suite get limits taken from those runs,
each with its reason. Both test jobs print their ten slowest cases.

* desktop: Privacy & data uses the same rows as the rest of Settings

The pane mixed two static claims with two controls in its own layout. It now
uses the about-row pattern every other pane uses: title and hint on the left,
one control on the right. The claims became the subtitle. Rows: anonymous usage
statistics (toggle, link to what is sent), device sharing state with Manage,
local report snapshots with Clear, export with Open export.

* desktop: Privacy & data keeps one bounded column and rows that never squeeze

The card stretched with the window, stranding each control hundreds of pixels
from its text. This pane's heading, subtitle and card now share a 760px column
and one text measure. Rows stack on their own width, because the window can
never be narrow enough for the old viewport rule to fire; row buttons do not
wrap. The what-is-sent link sits on its own line, and the snapshot and export
descriptions fit their measure and say this machine.

* menubar tests clean up their scratch defaults and leave the real Keychain alone

Four test files never removed their UserDefaults suites and the rest removed the
domain but left the plist, so thousands of files piled up in
~/Library/Preferences. A shared helper forgets a suite and deletes its file, and
every test that makes a suite now uses it. cfprefsd still rewrites an empty plist
when the test process exits, so a run leaves about a hundred 42-byte files with
no test data in them. The real-Keychain round trip only runs with
CODEBURN_TEST_REAL_KEYCHAIN=1.

* desktop: the resident parser retires after 15 idle minutes and restarts on demand

The renderer stops polling while the window is hidden, but the serve child stayed
resident with its parsed cache, one to two GB on a large corpus. After 15 minutes
without a request it is stopped the way a settings restart stops it, without
spending the unexpected-death budget, and the next request starts a new resident
through the existing lazy path, never a one-shot. CODEBURN_SERVE_IDLE_MS tunes or
disables it.

* desktop: every settings pane keeps a bounded column, and dark buttons have an edge

Cards stretched with the window, so on a wide display controls sat up to 1300px
from their labels and intro lines ran past 140 characters. All panes now share
one left-aligned column (760px, 980px for projects, aliases and pricing, which
hold wide rows) and one text measure. Rows stack on their own width instead of a
viewport rule the 900px minimum window could never reach, and a long path in
Projects no longer overflows the pane.

In dark mode the secondary button matched or sat below its surface, and was
invisible on the plugin install dialog. It gets a face and a border from two new
tokens; light is unchanged. The onboarding consent label matches Settings.

* desktop: this device shows its fingerprint on its own line

The local name and the fingerprint are two adjacent inline spans, so they read as
one run of text.

* menubar: the resident parser retires after 15 idle minutes and restarts on demand

The menu bar kept its serve child, and the parsed cache with it, for the whole
run, even with the popover closed and nothing changing. After 15 minutes without
a request the child is retired without counting as a death, and the next request
starts a new resident through ensureStarted, never the one-shot fallback, whose
snapshot settle window would make the figure lag a whole tick. The window sits
above the longest gap between real requests, so a child stays warm while work is
happening. CodeBurnServeIdleSeconds tunes or disables it.

* serve: a poll re-checks only what the watcher saw change, and shard writes are coalesced

With a session being written, every poll re-listed about 2,400 directories and
re-fingerprinted about 6,000 transcripts, then rewrote the whole current-month
shard (49MB on a large corpus) and read it back on the next poll. The resident
process now reuses the previous listing and fingerprints for paths its root
watcher proves untouched, and falls back to the full sweep whenever the watcher
is missing, unhealthy, overflowed, armed after the sweep, does not cover the
path, the memo is older than 4.5 minutes, or the process was suspended. Shard
publication is held for up to 90s and flushed when stdin closes; a held write is
dropped rather than written over something another process published. One-shot
runs are unchanged. Active poll cost 0.71 to 0.33 CPU-s at a 30s cadence;
time-to-reflect unchanged at about 1.2s across append, new dir, new file, delete,
rename-over, in-flight, watcher overflow and unhealthy watcher.

Changelog: state the telemetry default as it is in this version.

* a retired parser gets to publish its held shards, and durable providers are never held

Both apps retired the serve child with SIGTERM before its stdin-close flush could
run, so every retire discarded the coalesced window. They now close stdin, give
the child a bounded grace to publish and exit, and signal it only if it is still
running; a settings restart does the same. Measured: flushed 0/5 before, 5/5
after, for both clients.

copilot's cache entries are the only record once Copilot prunes its own files, so
a held window that was dropped (retire, crash, kill -9) before the source was
pruned lost that spend for good. A window that a durable provider dirtied is
published on the poll that parsed it, as before coalescing existed. The RSS guard
publishes a held window before it clears the caches instead of discarding it.

* serve: release the lifetime parse after the fill, and make the collection real

The RSS guard dropped its memos and then called globalThis.gc, which exists only
under --expose-gc; no launcher passes it, so the collection had never run and the
freed pages never went back. On a cold start the resident idled at 2.2GB with
364MB of live heap: clearing every memo moved RSS by 0MB, a collection alone by
700MB.

After the background fill has sealed its summaries the process now publishes any
held shard window, drops the memos it can re-read from disk (the cold start had
loaded the cache with scope 'all', which then served every later poll), and
collects; it collects again a minute later for as long as a pass returns 64MB,
at most four times. Cold idle 2198MB to 700MB, warm 900MB to 781MB. Reports are
leaf-identical cold and warm, time-to-reflect unchanged, a released month scope
reloads in 0.3-0.5s.

* warp: the billed cost reaches reports and survives the cache

The provider derived a dollar cost from Warp's own billing record (provider cents,
charged usage, or credits), but warp was missing from the reported-cost
pass-through, so the cached call kept no cost and every report, cold or warm,
priced the conversation from its tokens. A fixture billed at $1.22 reported
$0.15. A call whose cost came from a billing record now keeps it; a call that
only has the token floor stores none and is still re-priced when prices change.

Warp entries re-parse once (provider parse version), and the daily cache moves to
v34 so days whose Warp source still exists re-derive; call counts do not move, so
nothing is double counted or dropped.

* openclaw and pi: a reported cost survives the cache like every other reported cost

Both providers read the cost their tool recorded for a message, and both were
missing from the reported-cost pass-through, so reports priced every call from
tokens instead. Fixtures reported $0.011 against $0.056 (openclaw) and $0.013
against $0.410 (pi). A call with a reported cost now keeps it; a call with none,
or an explicit zero, stores none and is still re-priced when prices change. omp
had been added to the list in #1150 and pi, the same parser, was never revisited.

The provider list is one named set, the unreachable providerCallToTurn is gone,
and both providers re-parse once; the v34 daily cache re-derives their days with
call counts unchanged.

* test: pin parse count per report and per resident poll
2026-09-21 00:16:21 +02:00

782 lines
31 KiB
Swift

import Foundation
/// Anonymous, consent-gated product telemetry for the menu bar app.
///
/// The macOS twin of `windows/src-tauri/src/telemetry.rs`: same endpoint, same
/// `app.name`, same event names, same sanitizer, same day granularity, so the
/// two trays land in one table and separate on `app.platform`.
///
/// Privacy invariants (enforced here, not by the caller):
///
/// - Nothing is sent while the toggle is off. When the desktop app's own state
/// file exists, its decision and its install id are used and this app never
/// writes that file: one decision covers both, and the two apps' events join
/// on one id. Standalone, the decision lives in this app's own defaults and
/// defaults off for EU / EEA / UK / CH and for an unknown region.
/// - The only identifier is a random id minted locally. Switching the toggle
/// off mints a fresh one so past and future data cannot be linked.
/// - Events carry a day-granularity date only, and every prop goes through the
/// whitelist sanitizer below. No paths, no session content, no exact amounts.
/// - Debug builds and `swift run` never send (`CODEBURN_TELEMETRY_DEV=1`
/// overrides, for end-to-end testing).
// MARK: - JSON
/// Enough of a JSON value to carry the CLI's `telemetrySnapshot` block from the
/// payload decoder to the wire without this app knowing — or re-deriving —
/// anything about what is in it.
enum JSONValue: Codable, Sendable, Equatable {
case null
case bool(Bool)
case int(Int)
case double(Double)
case string(String)
case array([JSONValue])
case object([String: JSONValue])
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if container.decodeNil() { self = .null; return }
if let value = try? container.decode(Bool.self) { self = .bool(value); return }
if let value = try? container.decode(Int.self) { self = .int(value); return }
if let value = try? container.decode(Double.self) { self = .double(value); return }
if let value = try? container.decode(String.self) { self = .string(value); return }
if let value = try? container.decode([JSONValue].self) { self = .array(value); return }
self = .object(try container.decode([String: JSONValue].self))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .null: try container.encodeNil()
case .bool(let value): try container.encode(value)
case .int(let value): try container.encode(value)
case .double(let value): try container.encode(value)
case .string(let value): try container.encode(value)
case .array(let value): try container.encode(value)
case .object(let value): try container.encode(value)
}
}
}
// MARK: - Consent
enum TelemetryConsentSource: String, Sendable {
/// The desktop app decided, and this app is only reading its answer.
case desktop
/// A standalone menu bar install, which decides for itself.
case app
}
struct TelemetryConsent: Equatable, Sendable {
let source: TelemetryConsentSource
let installID: String
/// The effective answer: the decision that owns this install, less this
/// app's own veto.
let enabled: Bool
/// Desktop: that app's consent screen has been answered. App: a decision
/// has been recorded here, by the toggle or inherited from a desktop file
/// this app has seen.
let decided: Bool
/// The desktop app's own answer is no. The local toggle can veto that app's
/// yes; it can never overturn its no, so the toggle reads as a readout.
let lockedOff: Bool
/// `requiresExplicitDecision` is the one expression standing between
/// today's behaviour and "a standalone install must be asked first": see
/// `Telemetry.standaloneRequiresExplicitDecision`.
func canTrack(requiresExplicitDecision: Bool) -> Bool {
guard enabled else { return false }
switch source {
case .desktop: return decided
case .app: return decided || !requiresExplicitDecision
}
}
}
/// The desktop app's `telemetry.v1.json`, of which only these three fields
/// matter here. Read-only, always: that app owns the file and rewrites it whole.
struct DesktopTelemetryState: Equatable, Sendable {
let installID: String
let enabled: Bool
let onboarded: Bool
}
// MARK: - The queue
struct TelemetryEvent: Codable, Equatable, Sendable {
let name: String
let day: String
let props: [String: JSONValue]
}
private struct TelemetryEnvelope: Encodable {
struct App: Encodable {
let name: String
let version: String
let platform: String
let arch: String
let country: String?
}
let schema: Int
let installId: String
let app: App
let events: [TelemetryEvent]
}
enum TelemetryPostOutcome: Sendable {
case sent
/// A 4xx: this batch will never be accepted.
case rejected
/// A 5xx, a timeout or no network: worth another beat.
case retry
}
protocol TelemetryTransport: Sendable {
func post(
_ body: Data,
to endpoint: URL,
timeout: TimeInterval,
completion: @escaping @Sendable (TelemetryPostOutcome) -> Void
)
}
/// A session of this app's own, never `URLSession.shared`.
///
/// The shared session carries a process-wide cookie jar and response cache, so
/// a `Set-Cookie` from the endpoint or from a CDN in front of it would outlive
/// both an opt-out and the install id that rotates with it — a stable
/// identifier arriving by the back door. The default User-Agent is a second
/// one: it spells out the CFNetwork and Darwin kernel build. Ephemeral storage,
/// no cookies, no cache, a fixed agent, and no redirect followed at all —
/// `telemetry.rs` pins https across redirects, and refusing them outright is
/// the same guarantee with less to get wrong.
final class URLSessionTelemetryTransport: NSObject, TelemetryTransport, URLSessionTaskDelegate, @unchecked Sendable {
static func makeConfiguration() -> URLSessionConfiguration {
let configuration = URLSessionConfiguration.ephemeral
configuration.httpCookieStorage = nil
configuration.httpShouldSetCookies = false
configuration.httpCookieAcceptPolicy = .never
configuration.urlCache = nil
configuration.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
configuration.tlsMinimumSupportedProtocolVersion = .TLSv12
configuration.httpAdditionalHeaders = [
"Content-Type": "application/json",
"User-Agent": Telemetry.appName,
]
return configuration
}
private let session = URLSession(configuration: makeConfiguration())
func post(
_ body: Data,
to endpoint: URL,
timeout: TimeInterval,
completion: @escaping @Sendable (TelemetryPostOutcome) -> Void
) {
var request = URLRequest(url: endpoint, timeoutInterval: timeout)
request.httpMethod = "POST"
request.httpBody = body
let task = session.dataTask(with: request) { _, response, _ in
guard let status = (response as? HTTPURLResponse)?.statusCode else {
completion(.retry)
return
}
if (200..<300).contains(status) {
completion(.sent)
} else if (400..<500).contains(status) {
completion(.rejected)
} else {
completion(.retry)
}
}
task.delegate = self
task.resume()
}
/// Refused, not followed: a redirect names a different endpoint than the one
/// this app decided to trust, and downgrading to plain http is one of the
/// things it could name.
func urlSession(
_ session: URLSession,
task: URLSessionTask,
willPerformHTTPRedirection response: HTTPURLResponse,
newRequest request: URLRequest,
completionHandler: @escaping (URLRequest?) -> Void
) {
completionHandler(nil)
}
}
/// What the Privacy section renders. `source` decides whether the toggle is
/// live or a readout of the desktop app's decision.
struct TelemetryStatus: Equatable, Sendable {
let enabled: Bool
let source: TelemetryConsentSource
/// The desktop app says no. The toggle shows that answer and cannot change
/// it; every other combination leaves the toggle usable as an off switch.
let isLocked: Bool
}
// MARK: - The client
@MainActor
final class Telemetry {
/// The app's instance. A build that can never send has no business reading
/// the desktop app's state file either, which is also what keeps `swift run`
/// and `swift test` away from it — the suites inject their own everything.
static let shared = Telemetry(
desktopStateURL: defaultMaySend() ? defaultDesktopStateURL : nil
)
static let endpoint = URL(string: "https://api.codeburn.app/v1/telemetry")!
static let schema = 1
/// What separates these rows from the desktop app's (`codeburn-desktop`).
/// Also the transport's User-Agent, which is built off the main actor.
nonisolated static let appName = "codeburn-menubar"
static let releaseBundleID = "org.agentseal.codeburn-menubar"
/// EU-27 + EEA (IS, LI, NO) + UK + CH: the conservative "default off"
/// region. Copied from `app/electron/telemetry.ts`; the suite asserts the
/// two lists are still the same set.
static let defaultOffCountries: Set<String> = [
"AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR",
"HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK",
"SI", "ES", "SE", "IS", "LI", "NO", "GB", "CH",
]
/// Every event this app may send. An unknown name is dropped rather than
/// forwarded, so a typo at a call site cannot invent a metric. `update_click`,
/// `glance_open`, `dock_provider_switch` and `dock_drag_end` have no call
/// site here yet: they are the Windows tray's vocabulary for the same
/// surfaces, kept so the two apps stay one list.
static let eventNames: Set<String> = [
"app_open", "app_close", "popover_open", "settings_open", "update_click",
"glance_open", "dock_enabled", "dock_disabled", "dock_provider_switch",
"dock_drag_end", "usage_snapshot",
]
static let maxQueue = 200
static let maxString = 64
static let maxArray = 12
static let maxKeys = 16
/// How deep a container may sit below the props object. The daily usage
/// snapshot is the deepest shape either app sends: props -> models[] ->
/// model -> tasks[] -> task. Anything deeper is dropped whole.
static let maxDepth = 5
/// Belt and braces on top of the per-level caps.
static let maxLeaves = 1_000
static let httpTimeout: TimeInterval = 10
/// Quit waits for the last batch, so it gets a timeout nobody would notice.
static let quitTimeout: TimeInterval = 1.5
static let enabledKey = "CodeBurnTelemetryEnabled"
/// Set once any decision has been recorded here — the toggle's, or the
/// desktop app's, inherited the first time a valid state file is seen.
static let decidedKey = "CodeBurnTelemetryDecided"
/// This app's veto over the desktop app's yes. Kept apart from
/// `enabledKey` so it survives the desktop answer being re-inherited.
static let localOptOutKey = "CodeBurnTelemetryLocalOptOut"
static let installIDKey = "CodeBurnTelemetryInstallId"
static let lastSnapshotDayKey = "CodeBurnTelemetryLastSnapshotDay"
/// Whether a standalone install has to be asked before anything is sent, or
/// whether the region default is itself the answer. Today it is the region
/// default, as on the Windows tray before its notice appears; flipping this
/// to `true` is the whole of "standalone needs a first-run notice", and
/// nothing else reads the `decidedKey` marker.
static let standaloneRequiresExplicitDecision = false
private let defaults: UserDefaults
private let desktopStateURL: URL?
private let country: String?
private let appVersion: String
private let endpoint: URL
private let transport: TelemetryTransport
/// False in a debug build or a `swift run`: the queue still fills, so the
/// wiring can be exercised, but nothing leaves the machine.
private let maySend: Bool
private let now: @Sendable () -> Date
private let requiresExplicitDecision: Bool
private var consent: TelemetryConsent
/// The consent this run actually sends on.
private var canTrack: Bool {
consent.canTrack(requiresExplicitDecision: requiresExplicitDecision)
}
/// Memory only, unlike the Windows tray's file-backed queue: a run that
/// dies, or whose quit flush does not land inside its timeout, loses that
/// session's events and that day's `usage_snapshot` with them.
private var queue: [TelemetryEvent] = []
private var openedAt: Date
private var flushing = false
/// Set when the toggle goes off while a batch is out. That batch is dropped
/// when it comes back rather than restored, mirroring `Queue::clear`'s
/// `abandon_in_flight` in the Windows tray.
private var abandonInFlight = false
/// Consecutive failed sends, which is what the beat's backoff is computed
/// from, and how many beats are still owed to it.
private var failures = 0
private var beatsOwed = 0
private var flushBeat: NSBackgroundActivityScheduler?
init(
defaults: UserDefaults = .standard,
desktopStateURL: URL? = Telemetry.defaultDesktopStateURL,
region: String? = Locale.current.region?.identifier,
appVersion: String = AppVersion.normalizedBundleShortVersion,
endpoint: URL = Telemetry.endpoint,
transport: TelemetryTransport = URLSessionTelemetryTransport(),
maySend: Bool = Telemetry.defaultMaySend(),
now: @escaping @Sendable () -> Date = Date.init,
requiresExplicitDecision: Bool = Telemetry.standaloneRequiresExplicitDecision
) {
self.defaults = defaults
self.desktopStateURL = desktopStateURL
self.country = Telemetry.normalizedCountry(region)
self.appVersion = appVersion
self.endpoint = endpoint
self.transport = transport
self.maySend = maySend
self.now = now
self.requiresExplicitDecision = requiresExplicitDecision
self.openedAt = now()
self.consent = Telemetry.resolveConsent(
desktop: Telemetry.readDesktopState(at: desktopStateURL),
defaults: defaults,
country: Telemetry.normalizedCountry(region)
)
reresolve()
}
// MARK: Environment
/// `~/Library/Application Support/codeburn-desktop/telemetry.v1.json`, which
/// is Electron's `userData` for that app (its package name) plus the file
/// name its Telemetry class writes.
static var defaultDesktopStateURL: URL? {
FileManager.default
.urls(for: .applicationSupportDirectory, in: .userDomainMask)
.first?
.appendingPathComponent("codeburn-desktop")
.appendingPathComponent("telemetry.v1.json")
}
/// Mirrors the desktop app's "packaged only" rule with this app's
/// equivalent: a release build running out of the signed bundle.
static func defaultMaySend(
environment: [String: String] = ProcessInfo.processInfo.environment
) -> Bool {
if environment["CODEBURN_TELEMETRY_DEV"] == "1" { return true }
#if DEBUG
return false
#else
return Bundle.main.bundleIdentifier == releaseBundleID
#endif
}
/// Only a two-letter alpha region is a country here, so the UN M.49 forms
/// (`419`) come back as unknown rather than as a country nobody can join on.
static func normalizedCountry(_ region: String?) -> String? {
guard let region, region.count == 2,
region.allSatisfy({ $0.isASCII && $0.isLetter }) else { return nil }
return region.uppercased()
}
/// An unknown region is the conservative case and defaults off, exactly as
/// the desktop does.
static func defaultEnabled(for country: String?) -> Bool {
guard let country else { return false }
return !defaultOffCountries.contains(country.uppercased())
}
// MARK: Consent
/// A version this build does not understand, a missing id or a missing
/// toggle all mean "the desktop app has not decided", which sends the
/// resolution on to this app's own defaults rather than guessing on the
/// desktop app's behalf.
static func parseDesktopState(_ data: Data) -> DesktopTelemetryState? {
guard let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
root["version"] as? Int == 1,
let installID = root["installId"] as? String, !installID.isEmpty,
let enabled = root["enabled"] as? Bool else { return nil }
let onboardedAt = root["onboardedAt"] as? String
return DesktopTelemetryState(
installID: installID,
enabled: enabled,
onboarded: !(onboardedAt ?? "").isEmpty
)
}
static func readDesktopState(at url: URL?) -> DesktopTelemetryState? {
guard let url, let data = try? Data(contentsOf: url) else { return nil }
return parseDesktopState(data)
}
/// The one decision every send is gated on. A valid desktop file wins
/// outright, so a menu bar app running beside the desktop app never asks its
/// own question.
static func resolveConsent(
desktop: DesktopTelemetryState?,
defaults: UserDefaults,
country: String?
) -> TelemetryConsent {
let vetoed = defaults.bool(forKey: localOptOutKey)
if let desktop {
return TelemetryConsent(
source: .desktop,
installID: desktop.installID,
enabled: desktop.enabled && !vetoed,
decided: desktop.onboarded,
lockedOff: !desktop.enabled
)
}
let stored = defaults.string(forKey: installIDKey)
// A decision recorded here wins over the region default, which is what
// keeps a desktop app's "no" standing after that app is deleted: its
// answer was inherited locally the last time its file was read.
let recorded = defaults.object(forKey: enabledKey) as? Bool
return TelemetryConsent(
source: .app,
installID: (stored?.isEmpty == false ? stored : nil) ?? UUID().uuidString,
enabled: (recorded ?? defaultEnabled(for: country)) && !vetoed,
decided: defaults.bool(forKey: decidedKey),
lockedOff: false
)
}
/// Re-reads the desktop app's file, because it can appear or change while
/// this app runs: the desktop app can be installed after the menu bar app,
/// and its consent screen is answered in a different process.
@discardableResult
private func reresolve() -> TelemetryConsent {
let desktop = Telemetry.readDesktopState(at: desktopStateURL)
var fresh = Telemetry.resolveConsent(
desktop: desktop,
defaults: defaults,
country: country
)
if let desktop {
inheritDesktopDecision(desktop)
} else if consent.source == .app,
defaults.string(forKey: Telemetry.installIDKey) == nil {
// A defaults write that did not land leaves this app's id unstored,
// and resolving would mint a fresh one every time it is asked. Keep
// the one this run already has.
fresh = TelemetryConsent(
source: .app,
installID: consent.installID,
enabled: fresh.enabled,
decided: fresh.decided,
lockedOff: false
)
}
// One install, one id: stored the moment it is minted, not only at
// init, so a run that starts beside the desktop app and outlives it
// does not mint a new id at every resolve.
if fresh.source == .app,
defaults.string(forKey: Telemetry.installIDKey) != fresh.installID {
defaults.set(fresh.installID, forKey: Telemetry.installIDKey)
}
consent = fresh
return fresh
}
/// Records the desktop app's answer here as well. Deleting that app leaves
/// its `telemetry.v1.json` behind on a Mac, but a file that is removed or
/// corrupted would otherwise hand the question back to the region default
/// — turning an explicit "no" into a "yes" on the next resolve.
private func inheritDesktopDecision(_ desktop: DesktopTelemetryState) {
if defaults.object(forKey: Telemetry.enabledKey) as? Bool != desktop.enabled {
defaults.set(desktop.enabled, forKey: Telemetry.enabledKey)
}
if desktop.onboarded, !defaults.bool(forKey: Telemetry.decidedKey) {
defaults.set(true, forKey: Telemetry.decidedKey)
}
}
func status() -> TelemetryStatus {
let consent = reresolve()
return TelemetryStatus(
enabled: consent.enabled,
source: consent.source,
isLocked: consent.lockedOff
)
}
/// The settings toggle.
///
/// Standalone it is the decision: off empties the queue, disowns a batch
/// that is still in flight and mints a fresh install id, so past and future
/// data cannot be linked. Under the desktop app's decision it is a veto and
/// nothing more — it can switch this app off while that app says yes, never
/// on while it says no, and it neither rotates an id that app owns nor
/// writes that app's file.
func setEnabled(_ enabled: Bool) {
let consent = reresolve()
switch consent.source {
case .desktop:
guard !consent.lockedOff else { return }
defaults.set(!enabled, forKey: Telemetry.localOptOutKey)
if !enabled { stopSending(rotatingInstallID: false) }
case .app:
defaults.set(enabled, forKey: Telemetry.enabledKey)
defaults.set(true, forKey: Telemetry.decidedKey)
defaults.set(false, forKey: Telemetry.localOptOutKey)
if !enabled { stopSending(rotatingInstallID: true) }
}
reresolve()
}
/// Everything recorded under the answer being withdrawn goes, including a
/// batch that is already out: it would otherwise come back on a retry and
/// be posted under the id that replaced it.
private func stopSending(rotatingInstallID: Bool) {
queue.removeAll()
abandonInFlight = flushing
if rotatingInstallID {
defaults.set(UUID().uuidString, forKey: Telemetry.installIDKey)
}
}
// MARK: Sanitizing
/// Whitelist sanitizer. Keeps short strings, finite numbers and booleans,
/// plus objects and arrays of them nested up to `maxDepth`, each level
/// capped by `maxKeys` / `maxArray` and the whole event by `maxLeaves`.
/// Everything else is dropped. Port of `sanitizeProps` in the desktop app.
static func sanitizeProps(_ props: JSONValue) -> [String: JSONValue] {
guard case .object(let fields) = props else { return [:] }
var budget = maxLeaves
return sanitizeObject(fields, depth: 1, budget: &budget)
}
private static func sanitizeObject(
_ fields: [String: JSONValue],
depth: Int,
budget: inout Int
) -> [String: JSONValue] {
var out: [String: JSONValue] = [:]
// Sorted so the key cap keeps the same sixteen keys the Windows tray
// does, whose serde map is likewise ordered by key.
for key in fields.keys.sorted() {
if out.count >= maxKeys { break }
guard let clean = sanitizeValue(fields[key]!, depth: depth, budget: &budget) else {
continue
}
out[truncate(key)] = clean
}
return out
}
private static func sanitizeValue(
_ value: JSONValue,
depth: Int,
budget: inout Int
) -> JSONValue? {
switch value {
case .null:
return nil
case .bool, .int:
guard budget > 0 else { return nil }
budget -= 1
return value
case .double(let number):
guard budget > 0, number.isFinite else { return nil }
budget -= 1
return value
case .string(let text):
guard budget > 0 else { return nil }
budget -= 1
return .string(truncate(text))
case .array(let entries):
// Only leaves are allowed at the bottom; a container here is dropped
// whole rather than truncated to something that reads complete.
guard depth < maxDepth else { return nil }
var items: [JSONValue] = []
for entry in entries.prefix(maxArray) {
if let clean = sanitizeValue(entry, depth: depth + 1, budget: &budget) {
items.append(clean)
}
}
return items.isEmpty ? nil : .array(items)
case .object(let fields):
guard depth < maxDepth else { return nil }
let flat = sanitizeObject(fields, depth: depth + 1, budget: &budget)
return flat.isEmpty ? nil : .object(flat)
}
}
private static func truncate(_ text: String) -> String {
String(text.prefix(maxString))
}
// MARK: Tracking
/// `YYYY-MM-DD` in the machine's own timezone, the desktop app's day key.
static func dayKey(_ date: Date) -> String {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd"
return formatter.string(from: date)
}
/// Queues one event. An unknown name, junk props or a withheld decision are
/// all dropped here rather than reaching the wire.
func track(_ name: String, _ props: JSONValue = .object([:])) {
guard Telemetry.eventNames.contains(name), canTrack else { return }
// The oldest event gives way at the cap, so the queue always carries the
// most recent window rather than freezing at whatever filled it first.
if queue.count >= Telemetry.maxQueue { queue.removeFirst() }
queue.append(TelemetryEvent(
name: name,
day: Telemetry.dayKey(now()),
props: Telemetry.sanitizeProps(props)
))
}
/// The CLI's daily aggregate, forwarded verbatim and at most once a day. The
/// desktop app sends the same block from its own process when it is the one
/// that decided, and two apps sending it under one install id would double
/// every figure in it.
func trackUsageSnapshot(_ snapshot: JSONValue?) {
guard let snapshot, case .object = snapshot else { return }
guard canTrack, consent.source == .app else { return }
let day = Telemetry.dayKey(now())
guard defaults.string(forKey: Telemetry.lastSnapshotDayKey) != day else { return }
defaults.set(day, forKey: Telemetry.lastSnapshotDayKey)
track("usage_snapshot", snapshot)
}
/// Session length, in whole minutes, for the last flush before the process
/// goes.
func trackClose() {
let minutes = Int((now().timeIntervalSince(openedAt) / 60).rounded())
track("app_close", .object(["sessionMinutes": .int(minutes)]))
}
// MARK: Flushing
/// The flush beat, hung off the same background-activity scheduler the
/// refresh backstop uses: coalescible, tolerant, and no new fast timer in an
/// app that is measured on idle energy.
func start() {
track("app_open")
guard flushBeat == nil else { return }
let scheduler = NSBackgroundActivityScheduler(
identifier: "\(Telemetry.releaseBundleID).telemetry")
scheduler.repeats = true
scheduler.interval = 300
scheduler.tolerance = 60
scheduler.qualityOfService = .background
scheduler.schedule { [weak self] completion in
Task { @MainActor in
self?.runFlushBeat()
completion(.finished)
}
}
flushBeat = scheduler
}
/// Beats to sit out after `failures` consecutive failed sends: the Windows
/// tray's doubling backoff, in five-minute beats and capped at half an hour,
/// so an endpoint that has been down all day is still asked twice an hour.
static func beatsToSkip(failures: Int) -> Int {
guard failures > 0 else { return 0 }
return min(1 << min(failures - 1, 8), 6) - 1
}
func runFlushBeat() {
if beatsOwed > 0 {
beatsOwed -= 1
return
}
flush()
}
/// Best-effort batch POST. A 4xx drops the batch, because retrying a payload
/// the server has already refused would wedge the queue at its cap; a 5xx or
/// a dead network keeps it for the next beat.
func flush() {
// Also where a decision made elsewhere is picked up: the desktop app can
// be installed, or answer its consent screen, while this app is running.
reresolve()
guard maySend, !flushing, let (body, batch) = makeBatch() else { return }
flushing = true
abandonInFlight = false
transport.post(body, to: endpoint, timeout: Telemetry.httpTimeout) { [weak self] outcome in
Task { @MainActor in
self?.settle(outcome, batch: batch)
}
}
}
/// The last flush, on the way out. Bounded so quitting never waits on a slow
/// network, and never restored: the process is going either way.
func flushOnQuit(timeout: TimeInterval = Telemetry.quitTimeout) {
trackClose()
reresolve()
guard maySend, let (body, _) = makeBatch() else { return }
let done = DispatchSemaphore(value: 0)
transport.post(body, to: endpoint, timeout: timeout) { _ in done.signal() }
_ = done.wait(timeout: .now() + timeout)
}
private func makeBatch() -> (Data, [TelemetryEvent])? {
guard canTrack, !queue.isEmpty else { return nil }
let batch = queue
let envelope = TelemetryEnvelope(
schema: Telemetry.schema,
installId: consent.installID,
app: TelemetryEnvelope.App(
name: Telemetry.appName,
version: appVersion,
platform: "darwin",
arch: Telemetry.arch,
country: country
),
events: batch
)
guard let body = try? JSONEncoder().encode(envelope) else { return nil }
queue.removeAll()
return (body, batch)
}
private func settle(_ outcome: TelemetryPostOutcome, batch: [TelemetryEvent]) {
flushing = false
if abandonInFlight {
abandonInFlight = false
failures = 0
beatsOwed = 0
return
}
switch outcome {
case .sent, .rejected:
// The endpoint answered, so it is up: only a refused payload is
// gone, and the next batch is a different payload.
failures = 0
beatsOwed = 0
case .retry:
// Back in front of whatever was queued while the batch was out.
queue = Array((batch + queue).suffix(Telemetry.maxQueue))
failures += 1
beatsOwed = Telemetry.beatsToSkip(failures: failures)
}
}
private static var arch: String {
#if arch(arm64)
"arm64"
#else
"x64"
#endif
}
/// Visible for tests.
var queuedEvents: [TelemetryEvent] { queue }
}