File size: 26,311 Bytes
c920df3
daae64a
 
85f794d
c920df3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5981db
 
 
 
 
 
 
 
 
 
 
 
85f794d
a5981db
 
 
 
 
 
 
 
 
 
85f794d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
daae64a
 
 
 
 
85f794d
 
 
a5981db
 
 
 
 
 
 
 
 
 
 
 
 
85f794d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5981db
 
 
 
85f794d
a5981db
85f794d
 
 
 
 
 
a5981db
 
 
 
 
 
 
 
 
85f794d
 
a5981db
 
 
 
 
85f794d
 
a5981db
 
85f794d
a5981db
 
 
 
 
 
 
 
 
85f794d
a5981db
 
 
 
 
 
 
 
 
 
c920df3
 
 
 
 
 
 
 
 
 
 
 
 
7f72224
c920df3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import { API_V1, CREDENTIALS, UNAUTHORIZED_EVENT } from "../apiContract";
import type { Field, FilterNode, FilterTree } from "../customer-grid/types";
import { isFilterGroup } from "../customer-grid/types";
import { matchFilterTree } from "../customer-grid/useVisibleRows";

export interface ActivityTarget {
  tagName: string;
  role?: string;
  type?: string;
  id?: string;
  name?: string;
  className?: string;
  "aria-label"?: string;
  data?: Record<string, string>;
  path?: string[];
}

export interface MovementSample {
  kind: "hover" | "scroll" | "focus" | "keypress" | "pointer";
  ts: number;
  target: ActivityTarget;
  x?: number;
  y?: number;
  key?: string;
}

export interface ActivityEvent {
  eventId?: string;
  kind: "route" | "click" | "api" | "error";
  ts: number;
  route: string;
  from?: string;
  to?: string;
  ms?: number;
  target?: ActivityTarget;
  method?: string;
  url?: string;
  status?: number;
  message?: string;
  source?: string;
  line?: number;
  name?: string;
  movement?: MovementSample[];
  movementDropped?: number;
}

export interface ActivityLimits {
  retentionDays: number;
  maxSessions: number;
  maxEventsPerSession: number;
  maxMovementSamples: number;
  movementWindowSeconds: number;
  pageSize: number;
}

export interface ActivityEntry {
  event: ActivityEvent;
  actor: string;
  role?: string;
  sessionId: string;
  limits: ActivityLimits;
}

export type ActivityTimeframe = "all" | "today" | "week" | "month";
export interface ActivityFilters {
  search: string;
  actor: string;
  action: ActivityEvent["kind"] | "all";
  timeframe: ActivityTimeframe;
  conditions?: FilterTree;
}

export const DEFAULT_FILTERS: ActivityFilters = {
  search: "", actor: "", action: "all", timeframe: "all", conditions: { conj: "and", nodes: [] },
};

export function activityFilterFields(users: string[], admin: boolean): Field[] {
  return [
    { key: "actor", label: "User", type: "select", source: "overlay", options: users, filterable: admin },
    { key: "action", label: "Action", type: "select", source: "overlay", options: Object.values(ACTIVITY_ACTIONS) },
    { key: "timeframe", label: "Timeframe", type: "date", source: "overlay" },
  ];
}

export function activityFieldConditionCount(nodes: FilterNode[], key: string): number {
  return nodes.reduce((count, node) => count + (isFilterGroup(node)
    ? activityFieldConditionCount(node.children, key) : Number(node.colId === key)), 0);
}

function localDay(ts: number): string {
  const date = new Date(ts);
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
}

export const ACTIVITY_ACTIONS: Record<ActivityEvent["kind"], string> = {
  route: "Navigation",
  click: "Click",
  api: "Request",
  error: "Error",
};

export function activityTargetName(target?: ActivityTarget): string {
  if (!target) return "Element";
  if (target["aria-label"]) return target["aria-label"];
  const section = target.data?.["data-section"];
  const sections: Record<string, string> = { account: "Account", scope: "Your access", security: "Security",
    shortcuts: "Shortcuts", activity: "Activity log", users: "Users", padmin: "Platform admin" };
  if (section && sections[section]) return sections[section];
  const names: Record<string, string> = {
    button: "Button", a: "Link", input: "Input", textarea: "Text field", select: "Selection",
    option: "Option", grid: "Table", gridcell: "Cell", cell: "Cell", textbox: "Text field",
    searchbox: "Search", dialog: "Dialog", menuitem: "Menu item", document: "Page",
    td: "Cell", th: "Column", canvas: "Table", checkbox: "Checkbox", tab: "Tab",
  };
  return names[target.role || ""] || names[target.tagName] || "Page control";
}

function activityLocationName(value: string): string {
  const labels: Record<string, string> = {
    home: "Home", auth: "Account", login: "Sign in", logout: "Sign out", me: "Account",
    settings: "Settings", account: "Account", scope: "Access", security: "Security",
    shortcuts: "Shortcuts", activity: "Activity log", sessions: "Activity log", users: "Users",
    database: "Database", databases: "Databases", grid: "Table", table: "Table", tables: "Tables",
    records: "Records", rows: "Records", fields: "Fields", columns: "Fields", cells: "Cells",
    sheet: "Sheet", sheets: "Sheets", view: "View", views: "Views", data: "Data",
    applications: "Apps", apps: "Apps", finance: "Finance", orders: "Orders", inventory: "Inventory",
    customers: "Customers", products: "Products", sales: "Sales", collections: "Collections",
    procurement: "Procurement", agents: "Agents", agent: "Agent", automations: "Automations",
    automation: "Automation", connectors: "Connectors", keychains: "Connections", external: "API activity",
    connections: "Connections", subscriptions: "Subscription", billing: "Billing", health: "Service status",
    notifications: "Notifications", nav: "Navigation", workspace: "Workspace", permissions: "Permissions",
    preferences: "Preferences", reports: "Reports", catalog: "Catalog", assets: "Application resources",
  };
  const parts = value.replace(/^#/, "").split(/[?#]/)[0].split("/").filter(Boolean);
  for (const part of parts.reverse()) {
    const label = labels[part.toLowerCase()];
    if (label) return label;
  }
  return "Application";
}

export function activityDescription(event: ActivityEvent): { action: string; target: string } {
  switch (event.kind) {
    case "route": return { action: "Opened", target: activityLocationName(event.to || event.route) };
    case "click": return { action: "Clicked", target: activityTargetName(event.target) };
    case "api": {
      const actions: Record<string, string> = { GET: "Viewed", HEAD: "Checked", POST: "Submitted", PUT: "Updated", PATCH: "Updated", DELETE: "Deleted" };
      const failed = event.status === 0 || (event.status ?? 0) >= 400;
      return { action: failed ? "Requested" : actions[event.method || "GET"] || "Requested", target: activityLocationName(event.url || event.route) };
    }
    case "error": return { action: "Encountered an error in", target: activityLocationName(event.route) };
  }
}

export function filterActivity(entries: ActivityEntry[], filters: ActivityFilters, now = Date.now()): ActivityEntry[] {
  const start = new Date(now);
  start.setHours(0, 0, 0, 0);
  if (filters.timeframe === "week") start.setDate(start.getDate() - 6);
  if (filters.timeframe === "month") start.setDate(start.getDate() - 29);
  const terms = filters.search.trim().toLocaleLowerCase().split(/\s+/).filter(Boolean);
  const fields = new Map(activityFilterFields([], true).map(field => [field.key, field]));
  const today = localDay(now);
  return entries.filter(entry => {
    const { event, actor } = entry;
    if (filters.actor && actor !== filters.actor) return false;
    if (filters.action !== "all" && event.kind !== filters.action) return false;
    if (filters.timeframe !== "all" && (event.ts < start.getTime() || event.ts > now)) return false;
    if (filters.conditions && !matchFilterTree(filters.conditions.nodes, filters.conditions.conj ?? "and",
      { pid: 0, actor, action: ACTIVITY_ACTIONS[event.kind], timeframe: localDay(event.ts) }, fields, { today })) return false;
    if (!terms.length) return true;
    const description = activityDescription(event);
    const searchable = [actor, entry.role, ACTIVITY_ACTIONS[event.kind], description.action, description.target,
      event.route, event.source, event.status].join(" ").toLocaleLowerCase();
    return terms.every(term => searchable.includes(term));
  });
}

export function groupActivityByDay(entries: ActivityEntry[]): Array<{ day: string; label: string; entries: ActivityEntry[] }> {
  const groups = new Map<string, { day: string; label: string; entries: ActivityEntry[] }>();
  for (const entry of entries) {
    const date = new Date(entry.event.ts);
    const day = localDay(entry.event.ts);
    let group = groups.get(day);
    if (!group) {
      group = { day, label: date.toLocaleDateString([], { weekday: "long", month: "long", day: "numeric", year: "numeric" }), entries: [] };
      groups.set(day, group);
    }
    group.entries.push(entry);
  }
  return Array.from(groups.values());
}

const WINDOW_MS = 30_000;
const MAX_MOVEMENT = 500;
const MAX_ACTIONS = 200;
const MAX_BATCH = 100;
const MAX_BATCH_BYTES = 60 * 1024; // Fits the browser's shared keepalive/beacon budget.
const SESSION_KEY = "loopable.activity.session";
const ENDPOINT = `${API_V1}/activity`;
const SECRET = /\bbearer\s+\S+|\beyJ[A-Za-z0-9_-]*\.|\blp[bcor]1\.[A-Za-z0-9._~-]+|Atzr\|/i;
const CLASSES = /^(?:set|shell|sheet|cg|pg|activity|lp|is)-[a-z][a-z0-9-]*$/;
const PRIVATE_DATA = /value|text|content|html|password|token|secret|credential|cookie|authorization|payload|clipboard|body|query|cell|key/i;
const PROTECTED = 'input,textarea,select,option,[contenteditable]:not([contenteditable="false"]),[role="textbox"],[role="searchbox"],[role="gridcell"],[role="cell"],[role="grid"],td,th,canvas,.sheet-cell,.cg-cell,.gdg-input';
const LABELS = new Set(["Open", "Close", "Save", "Cancel", "Delete", "Edit", "Search", "Settings", "Close settings", "Activity log", "Refresh", "Previous", "Next", "Add", "Remove", "Menu", "More", "Retry"]);
const DATA_ENUMS: Record<string, Set<string>> = {
  "data-section": new Set(["account", "scope", "security", "shortcuts", "activity", "users", "padmin"]),
  "data-state": new Set(["open", "closed", "active", "inactive", "loading", "ready"]),
  "data-view-kind": new Set(["sheet", "grid", "table", "chart", "timeline"]),
};
const ROUTE_SEGMENTS = new Set("api v1 auth me login logout refresh nav workspace settings account scope security shortcuts activity sessions users database databases grid sheet sheets view views table tables records rows columns fields cells query sort filter data applications finance facts orders inventory customers products sales collections procurement agents agent automation automations connectors keychains external oauth connections admin operator subscriptions billing health status run runs events logs actions export import search schema preferences folders pages charts catalog reports reconciliation permissions detail assets src settings ActivityLogPage.tsx activityLog.ts main.tsx".split(" "));
const ERROR_NAMES = new Set(["Error", "TypeError", "ReferenceError", "SyntaxError", "RangeError", "URIError", "EvalError", "AggregateError", "NetworkError", "HttpError", "ResourceError"]);
const ERROR_MESSAGES = new Set(["Application error.", "Unhandled rejection.", "Script error.", "A request failed.", "A network request failed.", "A resource failed to load."]);

let installed = false;
let reportError: ((detail: ActivityErrorDetail) => void) | undefined;

export interface ActivityErrorDetail {
  message?: string;
  source?: string;
  line?: number;
  name?: string;
}

/** Error boundaries may use this door without passing props, state, or stacks. */
export function reportActivityError(detail: ActivityErrorDetail): void {
  try { reportError?.(detail); } catch { /* Telemetry never throws into the application. */ }
}

function text(value: unknown, max = 256): string {
  if (typeof value !== "string" || SECRET.test(value)) return "";
  return value.replace(/[\u0000-\u001f\u007f\u2013\u2014]/g, " ").slice(0, max);
}

function routePath(value: string): string {
  const safe = text(value);
  if (!safe) return "";
  try {
    const raw = safe.startsWith("#") ? safe.slice(1).split(/[?#]/)[0] : new URL(safe, window.location.origin).pathname;
    return ("/" + raw.split("/").filter(Boolean).map(part =>
      ROUTE_SEGMENTS.has(part) || /^[a-zA-Z0-9_-]+\.(js|css|tsx|ts)$/.test(part) ? part : ":item",
    ).join("/")).slice(0, 256);
  } catch { return ""; }
}

function classes(element: Element): string {
  return text(element.getAttribute("class")).split(/\s+/).filter(part => CLASSES.test(part)).join(" ").slice(0, 128);
}

function exceptionLocation(error: unknown): Pick<ActivityErrorDetail, "source" | "line"> {
  if (!(error instanceof Error) || typeof error.stack !== "string") return {};
  // Skip the message line. Extract only the first frame's location, never the stack.
  for (const frame of error.stack.split("\n").slice(1, 7)) {
    const match = frame.match(/((?:https?:\/\/|\/)[^\s)]+):(\d+):\d+\)?$/);
    if (match) return { source: routePath(match[1]), line: Number(match[2]) };
  }
  return {};
}

/** Never read displayed text or any control/cell content, even to redact it. */
function identity(target: EventTarget | null): ActivityTarget {
  const element = target instanceof Element ? target : null;
  if (!element) return { tagName: "document" };
  const protectedParent = element.closest(PROTECTED);
  const tagName = element.tagName.toLowerCase();
  const result: ActivityTarget = { tagName };
  const role = text(element.getAttribute("role"), 32);
  if (role) result.role = role;
  if (protectedParent) {
    // Preserve context on descendants, including portalled contenteditable editors.
    result.role = protectedParent.matches('[role="grid"],[role="gridcell"],[role="cell"],td,th,canvas,.sheet-cell,.cg-cell') ? "gridcell" : "textbox";
  }
  const type = text(element.getAttribute("type"), 32);
  if (type) result.type = type;
  const path: string[] = [];
  for (let ancestor = element.parentElement; ancestor && path.length < 5; ancestor = ancestor.parentElement) {
    const className = protectedParent ? "" : classes(ancestor);
    path.push(ancestor.tagName.toLowerCase() + (className ? "." + className.replace(/ /g, ".") : ""));
  }
  result.path = path;
  if (protectedParent) return result;
  const className = classes(element);
  if (className) result.className = className;
  for (const key of ["id", "name"] as const) {
    const val = text(element.getAttribute(key), 80);
    if (val === "root" || CLASSES.test(val)) result[key] = val;
  }
  const label = text(element.getAttribute("aria-label"));
  if (LABELS.has(label)) result["aria-label"] = label;
  const data: Record<string, string> = {};
  for (const attr of Array.from(element.attributes)) {
    if (Object.keys(data).length >= 16) break;
    if (!/^data-[a-z][a-z-]{0,47}$/.test(attr.name) || PRIVATE_DATA.test(attr.name.slice(5))) continue;
    // Only enum-valued chrome attributes are read. Others describe presence only.
    const val = DATA_ENUMS[attr.name] ? text(element.getAttribute(attr.name)) : "present";
    if (val) data[attr.name] = DATA_ENUMS[attr.name]?.has(val) ? val : "present";
  }
  if (Object.keys(data).length) result.data = data;
  return result;
}

function tabSession(): string {
  try {
    const saved = window.sessionStorage.getItem(SESSION_KEY);
    if (saved && /^[a-f0-9-]{32,36}$/.test(saved)) return saved;
  } catch { /* Storage can be disabled; use a memory-only random tab id. */ }
  const bytes = new Uint8Array(16);
  window.crypto.getRandomValues(bytes);
  const id = Array.from(bytes, byte => byte.toString(16).padStart(2, "0")).join("");
  try { window.sessionStorage.setItem(SESSION_KEY, id); } catch { /* Keep collecting in memory. */ }
  return id;
}

export function installActivityLog(): void {
  if (typeof window === "undefined" || installed) return;
  installed = true;
  try { startCollector(); } catch { /* Startup telemetry must never block React. */ }
}

function startCollector(): void {
  const sessionId = tabSession();
  const nativeFetch = window.fetch.bind(window);
  let actions: ActivityEvent[] = [];
  let movement: MovementSample[] = [];
  let evictedMovement: Array<{ second: number; count: number }> = [];
  let dropped = 0;
  let inFlight = false;
  let generation = 0;
  let lastFlush = 0;
  let scheduled: number | undefined;
  let previousRoute = routePath(window.location.hash || window.location.pathname);
  let previousLocation = window.location.hash || window.location.pathname;
  let routeSince = performance.now();
  const currentRoute = () => routePath(window.location.hash || window.location.pathname);
  const safely = (work: () => void) => { try { work(); } catch { dropped++; } };

  function action(event: ActivityEvent): void {
    if (actions.length >= MAX_ACTIONS) { actions.shift(); dropped++; }
    actions.push(event);
  }

  function prune(now: number): void {
    movement = movement.filter(sample => sample.ts >= now - WINDOW_MS);
    evictedMovement = evictedMovement.filter(bucket => bucket.second >= Math.floor((now - WINDOW_MS) / 1000));
  }

  function move(sample: MovementSample): void {
    prune(sample.ts);
    if (movement.length >= MAX_MOVEMENT) {
      const removed = movement.shift();
      if (removed) {
        const second = Math.floor(removed.ts / 1000);
        const bucket = evictedMovement.at(-1);
        if (bucket?.second === second) bucket.count++;
        else evictedMovement.push({ second, count: 1 });
      }
    }
    movement.push(sample);
  }

  function flush(unload = false): void {
    if (!actions.length || (inFlight && !unload)) return;
    if (!unload && Date.now() - lastFlush < 1000) {
      if (scheduled === undefined) scheduled = window.setTimeout(() => { scheduled = undefined; safely(() => flush()); }, 1000);
      return;
    }
    lastFlush = Date.now();
    // Consume before sending. Failed telemetry is counted, never queued for retries.
    const batch = actions.splice(0, MAX_BATCH);
    const lost = dropped;
    dropped = 0;
    let body = "";
    while (batch.length) {
      body = JSON.stringify({ sessionId, events: batch, dropped: lost });
      if (new Blob([body]).size <= MAX_BATCH_BYTES) break;
      const largest = batch.reduce((a, b) => (a.movement?.length ?? 0) > (b.movement?.length ?? 0) ? a : b);
      if (largest.movement?.length) {
        // Keep the whole time interval under the byte budget by thinning samples.
        const before = largest.movement.length;
        largest.movement = largest.movement.filter((_, index) => index % 2 === 0);
        if (before === 1) largest.movement = [];
        largest.movementDropped = (largest.movementDropped ?? 0) + before - largest.movement.length;
      } else { batch.shift(); dropped++; }
    }
    if (!batch.length) { dropped += lost; return; }
    if (unload && typeof navigator.sendBeacon === "function") {
      try {
        if (navigator.sendBeacon(ENDPOINT, new Blob([body], { type: "application/json" }))) {
          dropped += actions.length;
          actions = [];
          return;
        }
      } catch { /* Fall back once to fetch. */ }
    }
    const sentGeneration = generation;
    inFlight = true;
    void nativeFetch(ENDPOINT, { method: "POST", credentials: CREDENTIALS,
      headers: { "Content-Type": "application/json" }, body, keepalive: unload,
    }).then(response => {
      if (!response.ok && sentGeneration === generation) dropped += batch.length + lost;
    }).catch(() => { if (sentGeneration === generation) dropped += batch.length + lost; })
      .finally(() => {
        inFlight = false;
        if (actions.some(event => event.kind === "error")) safely(() => flush());
      });
  }

  reportError = detail => safely(() => {
    const now = Date.now();
    prune(now);
    const name = text(detail.name, 64);
    const message = text(detail.message);
    action({ kind: "error", ts: now, route: currentRoute(), name: ERROR_NAMES.has(name) ? name : "Error",
      // Exception messages can interpolate a cell; only fixed diagnostic copy is safe.
      message: ERROR_MESSAGES.has(message) ? message : "Application error.",
      source: routePath(detail.source ?? ""), line: Number.isFinite(detail.line) ? Math.max(0, Math.round(detail.line!)) : 0,
      movement: movement.map(sample => ({ ...sample })), movementDropped: evictedMovement.reduce((sum, bucket) => sum + bucket.count, 0),
    });
    flush();
  });

  function boundary(): void {
    generation++;
    actions = [];
    movement = [];
    evictedMovement = [];
    dropped = 0;
    previousRoute = currentRoute();
    previousLocation = window.location.hash || window.location.pathname;
    routeSince = performance.now();
  }

  window.addEventListener(UNAUTHORIZED_EVENT, boundary);
  window.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
    let path = "";
    let method = "GET";
    let ownFlush = false;
    safely(() => {
      const rawUrl = input instanceof Request ? input.url : String(input);
      const pathname = new URL(rawUrl, window.location.origin).pathname;
      ownFlush = pathname.replace(/\/$/, "") === ENDPOINT;
      path = routePath(rawUrl);
      method = text(init?.method ?? (input instanceof Request ? input.method : "GET"), 16).toUpperCase();
      if (/\/auth\/(?:login|logout)$/.test(pathname)) boundary();
    });
    const started = performance.now();
    const requestGeneration = generation;
    try {
      const response = await nativeFetch(input, init);
      if (!ownFlush && requestGeneration === generation) safely(() => {
        action({ kind: "api", ts: Date.now(), route: currentRoute(), method, url: path,
          status: response.status, ms: Math.round(performance.now() - started) });
        if (response.status >= 400 && response.status !== 401) reportActivityError({ name: "HttpError", message: "A request failed.", source: path });
        if (response.status === 401) boundary();
      });
      return response;
    } catch (error) {
      if (!ownFlush && requestGeneration === generation) safely(() => {
        action({ kind: "api", ts: Date.now(), route: currentRoute(), method, url: path,
          status: 0, ms: Math.round(performance.now() - started) });
        // Aborted component reads are normal navigation, not user-visible errors.
        const aborted = init?.signal?.aborted || (input instanceof Request && input.signal.aborted)
          || (error instanceof Error && error.name === "AbortError");
        if (!aborted) {
          reportActivityError({ name: "NetworkError", message: "A network request failed.", source: path });
        }
      });
      throw error;
    }
  };

  function routeChanged(): void {
    safely(() => {
      const route = currentRoute();
      const location = window.location.hash || window.location.pathname;
      if (location === previousLocation) return; // popstate and hashchange may describe one move.
      const now = performance.now();
      action({ kind: "route", ts: Date.now(), route, from: previousRoute, to: route, ms: Math.round(now - routeSince) });
      previousRoute = route;
      previousLocation = location;
      routeSince = now;
    });
  }
  window.addEventListener("hashchange", routeChanged);
  window.addEventListener("popstate", routeChanged);
  action({ kind: "route", ts: Date.now(), route: previousRoute, from: "", to: previousRoute, ms: 0 });
  document.addEventListener("click", event => safely(() => action({ kind: "click", ts: Date.now(), route: currentRoute(), target: identity(event.target) })), true);

  let lastPointer = 0;
  let lastHover = 0;
  let hovered: EventTarget | null = null;
  let cursor: ActivityTarget = { tagName: "document" };
  let cursorPosition: { x: number; y: number } | undefined;
  document.addEventListener("pointermove", event => safely(() => {
    const now = Date.now();
    if (now - lastPointer < 100) return;
    lastPointer = now;
    cursorPosition = { x: event.clientX, y: event.clientY };
    cursor = identity(document.elementFromPoint(event.clientX, event.clientY) ?? event.target);
    move({ kind: "pointer", ts: now, x: Math.round(event.clientX), y: Math.round(event.clientY), target: cursor });
    if ((event.target !== hovered && now - lastHover >= 250) || now - lastHover >= 1000) {
      hovered = event.target;
      lastHover = now;
      move({ kind: "hover", ts: now, target: cursor });
    }
  }), { capture: true, passive: true });
  let lastScroll = 0;
  document.addEventListener("scroll", event => safely(() => {
    const now = Date.now();
    if (now - lastScroll < 100) return;
    lastScroll = now;
    const element = event.target instanceof Element ? event.target : document.scrollingElement;
    cursor = identity(cursorPosition ? document.elementFromPoint(cursorPosition.x, cursorPosition.y) : event.target);
    move({ kind: "scroll", ts: now, x: Math.round(element?.scrollLeft ?? window.scrollX),
      y: Math.round(element?.scrollTop ?? window.scrollY), target: cursor });
  }), { capture: true, passive: true });
  document.addEventListener("focusin", event => safely(() => move({ kind: "focus", ts: Date.now(), target: identity(event.target) })), true);
  document.addEventListener("keydown", event => safely(() => {
    const target = identity(event.target);
    const editable = target.role === "textbox" || target.role === "gridcell";
    let key = event.key === " " ? "Space" : text(event.key, 32);
    // Printable edit sequences could reconstruct the entire cell. Keep the gesture only.
    const shortcut = (event.ctrlKey || event.metaKey) && !event.altKey;
    if (editable && (event.key.length === 1 || event.isComposing) && !shortcut) key = "Character";
    else {
      const modifiers = [[event.ctrlKey, "Control"], [event.altKey, "Alt"], [event.shiftKey, "Shift"], [event.metaKey, "Meta"]] as const;
      key = [...modifiers.filter(([active, name]) => active && name !== key).map(([, name]) => name), key].join("+");
    }
    move({ kind: "keypress", ts: Date.now(), target, key });
  }), true);
  window.addEventListener("error", event => safely(() => {
    if (event instanceof ErrorEvent) reportActivityError({ message: event.message, name: event.error instanceof Error ? event.error.name : "Error", source: event.filename, line: event.lineno });
    else reportActivityError({ name: "ResourceError", message: "A resource failed to load." });
  }), true);
  window.addEventListener("unhandledrejection", event => safely(() => reportActivityError({
    name: event.reason instanceof Error ? event.reason.name : "Error", message: "Unhandled rejection.",
    ...exceptionLocation(event.reason),
  })));
  window.setInterval(() => safely(() => flush()), 10_000);
  document.addEventListener("visibilitychange", () => { if (document.visibilityState === "hidden") safely(() => flush(true)); });
  window.addEventListener("pagehide", () => safely(() => flush(true)));
}