@inflatable-cookie/longhorn 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/package.json +1 -1
  2. package/src/agent-control/inject.ts +4 -1
  3. package/src/agent-control/selection.ts +35 -20
  4. package/src/agent-control/shim.ts +302 -144
  5. package/src/bridge/direct.ts +1 -1
  6. package/src/bridge/generated/fields.ts +3 -2
  7. package/src/bridge/generated/protocol.ts +7 -3
  8. package/src/bridge/job.ts +4 -14
  9. package/src/bridge/loopback.ts +7 -25
  10. package/src/bridge/operation.ts +13 -43
  11. package/src/bridge/router.ts +10 -23
  12. package/src/bridge/runtime/connection.ts +77 -105
  13. package/src/bridge/runtime/policy.ts +8 -28
  14. package/src/bridge/runtime/retry.ts +6 -18
  15. package/src/bridge/session.ts +4 -14
  16. package/src/bridge/stream/connection.ts +16 -38
  17. package/src/bridge/stream/source.ts +2 -6
  18. package/src/bridge/stream/tracker.ts +7 -12
  19. package/src/bridge/supervision.ts +11 -26
  20. package/src/bridge/validation/base.ts +17 -39
  21. package/src/bridge/validation/jobs.ts +11 -25
  22. package/src/bridge/validation/negotiation.ts +27 -37
  23. package/src/bridge/validation/operations.ts +28 -52
  24. package/src/bridge/validation/streams.ts +3 -10
  25. package/src/commands/client.ts +21 -26
  26. package/src/commands/controller.ts +32 -47
  27. package/src/commands/generated/fields.ts +3 -2
  28. package/src/commands/generated/protocol.ts +4 -0
  29. package/src/commands/keyboard.ts +92 -89
  30. package/src/commands/ports.ts +6 -12
  31. package/src/commands/projectors.ts +50 -48
  32. package/src/commands/validation.ts +70 -97
  33. package/src/config/client.ts +6 -19
  34. package/src/config/generated/fields.ts +2 -2
  35. package/src/config/names.ts +6 -12
  36. package/src/config/validation/commands.ts +5 -17
  37. package/src/config/validation/outcomes.ts +31 -39
  38. package/src/config/validation/primitives.ts +36 -53
  39. package/src/config/validation/projection.ts +66 -94
  40. package/src/core/connection.ts +70 -61
  41. package/src/core/record.ts +4 -0
  42. package/src/core/transport.ts +4 -15
  43. package/src/core/wire-copy.ts +6 -0
  44. package/src/history/client.ts +6 -12
  45. package/src/history/controller.ts +46 -46
  46. package/src/history/direct.ts +1 -3
  47. package/src/history/generated/fields.ts +2 -2
  48. package/src/history/ports.ts +4 -6
  49. package/src/history/serialized.ts +9 -13
  50. package/src/history/validation.ts +71 -55
  51. package/src/history-tree/client.ts +87 -11
  52. package/src/history-tree/controller.ts +313 -70
  53. package/src/history-tree/direct.ts +3 -1
  54. package/src/history-tree/generated/fields.ts +2 -2
  55. package/src/history-tree/ports.ts +3 -1
  56. package/src/history-tree/serialized.ts +39 -11
  57. package/src/history-tree/validation.ts +262 -63
  58. package/src/layout/generated/protocol.ts +2 -2
  59. package/src/layout/ratio.ts +2 -8
  60. package/src/layout/validation.ts +10 -19
  61. package/src/layout/visibility.ts +1 -2
  62. package/src/licence/client.ts +44 -7
  63. package/src/licence/controller.ts +76 -40
  64. package/src/licence/direct.ts +3 -1
  65. package/src/licence/generated/fields.ts +2 -2
  66. package/src/licence/generated/key-conformance.json +28 -0
  67. package/src/licence/generated/protocol.ts +1 -1
  68. package/src/licence/key.ts +40 -12
  69. package/src/licence/ports.ts +3 -1
  70. package/src/licence/serialized.ts +32 -9
  71. package/src/licence/validation.ts +120 -36
  72. package/src/native-content/client.ts +178 -236
  73. package/src/native-content/generated/fields.ts +2 -2
  74. package/src/native-content/ports.ts +9 -15
  75. package/src/native-content/serialized.ts +12 -19
  76. package/src/native-content/validation/base.ts +57 -50
  77. package/src/native-content/validation/messages.ts +21 -35
  78. package/src/native-content/validation/state.ts +31 -33
  79. package/src/notifications/client.ts +87 -31
  80. package/src/notifications/controller.ts +215 -61
  81. package/src/notifications/direct.ts +3 -1
  82. package/src/notifications/generated/fields.ts +2 -2
  83. package/src/notifications/ports.ts +7 -3
  84. package/src/notifications/serialized.ts +11 -9
  85. package/src/notifications/validation.ts +147 -43
  86. package/src/operation/bridge.ts +117 -50
  87. package/src/operation/client.ts +38 -24
  88. package/src/operation/controller.ts +33 -42
  89. package/src/operation/direct.ts +1 -3
  90. package/src/operation/generated/fields.ts +2 -2
  91. package/src/operation/ports.ts +10 -9
  92. package/src/operation/serialized.ts +8 -9
  93. package/src/operation/validation.ts +152 -65
  94. package/src/settings/client.ts +11 -44
  95. package/src/settings/connection.ts +26 -40
  96. package/src/settings/generated/fields.ts +3 -3
  97. package/src/settings/generated/protocol.ts +9 -4
  98. package/src/settings/names.ts +2 -4
  99. package/src/settings/registry.ts +23 -25
  100. package/src/settings/validation/authority.ts +19 -17
  101. package/src/settings/validation/command.ts +17 -37
  102. package/src/settings/validation/error.ts +2 -8
  103. package/src/settings/validation/primitives.ts +26 -42
  104. package/src/settings/validation/projection.ts +8 -24
  105. package/src/settings/validation/registry.ts +25 -73
  106. package/src/surface-transfer/client.ts +6 -18
  107. package/src/surface-transfer/generated/fields.ts +2 -2
  108. package/src/surface-transfer/validation.ts +31 -49
  109. package/src/surfaces/client.ts +12 -22
  110. package/src/surfaces/generated/fields.ts +2 -2
  111. package/src/surfaces/validation.ts +40 -49
  112. package/src/transfer/client.ts +26 -60
  113. package/src/transfer/connection.ts +12 -20
  114. package/src/transfer/generated/fields.ts +5 -5
  115. package/src/transfer/generated/protocol.ts +4 -4
  116. package/src/transfer/payload.ts +13 -31
  117. package/src/transfer/validation.ts +39 -75
  118. package/src/update/client.ts +59 -9
  119. package/src/update/controller.ts +113 -48
  120. package/src/update/direct.ts +3 -1
  121. package/src/update/generated/fields.ts +3 -3
  122. package/src/update/generated/protocol.ts +7 -3
  123. package/src/update/generated/variant-fields.ts +1 -0
  124. package/src/update/ports.ts +6 -2
  125. package/src/update/serialized.ts +34 -10
  126. package/src/update/validation.ts +115 -35
@@ -29,26 +29,10 @@ const SKIP_TAGS = new Set([
29
29
  "IFRAME",
30
30
  ]);
31
31
 
32
- type FormControl = Element & {
33
- value: string;
34
- disabled?: boolean;
35
- checked?: boolean;
36
- readOnly?: boolean;
37
- type?: string;
38
- selectionStart?: number | null;
39
- selectionEnd?: number | null;
40
- setSelectionRange?: (start: number, end: number) => void;
41
- selectedOptions?: ArrayLike<Element>;
42
- selected?: boolean;
43
- };
44
-
45
- function isFormControl(element: Element): element is FormControl {
46
- const tag = element.tagName;
47
- return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || tag === "OPTION";
48
- }
49
-
50
32
  function prop<T>(element: Element, name: string): T | undefined {
51
- return (element as unknown as Record<string, T | undefined>)[name];
33
+ // DOM access is structural across webviews and test windows. Callers select each property by element role.
34
+ // oxlint-disable-next-line quality/no-reflect-get,typescript/no-unsafe-type-assertion -- The selected DOM interface is the contract for the property.
35
+ return Reflect.get(element, name) as T | undefined;
52
36
  }
53
37
 
54
38
  export type ToolErrorBody =
@@ -65,8 +49,8 @@ export type SemanticNode = {
65
49
  role: string;
66
50
  name?: string;
67
51
  value?: string;
68
- states: string[];
69
- children: SemanticNode[];
52
+ states: Array<string>;
53
+ children: Array<SemanticNode>;
70
54
  };
71
55
 
72
56
  export type PageState = { url: string; title: string };
@@ -94,7 +78,7 @@ export type AgentControlApi = {
94
78
  type: (element: string, text: string) => ShimResult<Record<string, never>>;
95
79
  press: (
96
80
  key: string,
97
- modifiers?: string[],
81
+ modifiers?: Array<string>,
98
82
  element?: string | null,
99
83
  ) => ShimResult<Record<string, never>>;
100
84
  scroll: (
@@ -103,18 +87,23 @@ export type AgentControlApi = {
103
87
  element?: string | null,
104
88
  ) => ShimResult<Record<string, never>>;
105
89
  drag: (source: string, target: string) => ShimResult<Record<string, never>>;
106
- setFileInput: (element: string, files: FileInputFile[]) => ShimResult<Record<string, never>>;
90
+ setFileInput: (element: string, files: Array<FileInputFile>) => ShimResult<Record<string, never>>;
107
91
  waitFor: (predicate: WaitPredicate) => ShimResult<{ holds: boolean }>;
108
92
  markAgentOrigin: () => void;
109
93
  isAgentOriginated: () => boolean;
110
- readEvents: (sinceSeq?: number) => {
111
- events: PageEvent[];
94
+ readEvents: (
95
+ sinceSeq?: number,
96
+ sinceEpoch?: string | null,
97
+ ) => {
98
+ epoch: string;
99
+ events: Array<PageEvent>;
112
100
  nextSeq: number;
113
101
  dropped: number;
114
102
  };
115
103
  };
116
104
 
117
105
  export type ShimWorld = {
106
+ [SHIM_GLOBAL]?: AgentControlApi;
118
107
  document: Document;
119
108
  location: { href: string };
120
109
  console: Pick<Console, "log" | "info" | "warn" | "error" | "debug">;
@@ -144,17 +133,21 @@ function unsupported(message: string): ShimResult<never> {
144
133
  }
145
134
 
146
135
  function ok(): ActionOk {
136
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- The wire success marker has no payload; the public ActionOk type represents that as Record<string, never>.
147
137
  return { ok: true } as ActionOk;
148
138
  }
149
139
 
150
140
  function resolveRef(document: Document, element: string): Element | null {
151
- if (element === TRUNCATED_REF || element.length === 0) return null;
141
+ if (element === TRUNCATED_REF || element.length === 0) {
142
+ return null;
143
+ }
152
144
  return document.querySelector(`[${REF_ATTR}="${element}"]`);
153
145
  }
154
146
 
155
147
  function refPrefix(document: Document): string {
156
- const view = document.defaultView as unknown as Record<string, unknown> | null;
157
- const value = view?.[REF_PREFIX_GLOBAL];
148
+ const view = document.defaultView;
149
+ // oxlint-disable-next-line quality/no-reflect-get -- The prefix is stamped on this window by the Rust host.
150
+ const value: unknown = view ? Reflect.get(view, REF_PREFIX_GLOBAL) : undefined;
158
151
  return typeof value === "string" ? value : "";
159
152
  }
160
153
 
@@ -170,7 +163,9 @@ function allocRef(document: Document): string {
170
163
 
171
164
  function stamp(element: Element, document: Document): string {
172
165
  const existing = element.getAttribute(REF_ATTR);
173
- if (existing) return existing;
166
+ if (existing) {
167
+ return existing;
168
+ }
174
169
  const id = allocRef(document);
175
170
  element.setAttribute(REF_ATTR, id);
176
171
  return id;
@@ -182,67 +177,132 @@ function computedStyle(element: Element): CSSStyleDeclaration | null {
182
177
  }
183
178
 
184
179
  function isVisible(element: Element): boolean {
185
- if (element.hasAttribute("hidden")) return false;
186
- if (element.getAttribute("aria-hidden") === "true") return false;
180
+ if (element.hasAttribute("hidden")) {
181
+ return false;
182
+ }
183
+ if (element.getAttribute("aria-hidden") === "true") {
184
+ return false;
185
+ }
187
186
  const style = computedStyle(element);
188
- if (!style) return true;
187
+ if (!style) {
188
+ return true;
189
+ }
189
190
  return style.display !== "none" && style.visibility !== "hidden";
190
191
  }
191
192
 
192
193
  function implicitRole(element: Element): string | null {
193
194
  const tag = element.tagName;
194
- if (tag === "A") return element.hasAttribute("href") ? "link" : null;
195
- if (tag === "BUTTON") return "button";
196
- if (tag === "TEXTAREA") return "textbox";
197
- if (tag === "SELECT") return "combobox";
198
- if (tag === "OPTION") return "option";
199
- if (tag === "SUMMARY") return "button";
200
- if (tag === "DETAILS") return "group";
201
- if (tag === "NAV") return "navigation";
202
- if (tag === "MAIN") return "main";
203
- if (tag === "HEADER") return "banner";
204
- if (tag === "FOOTER") return "contentinfo";
205
- if (tag === "FORM") return "form";
206
- if (tag === "UL" || tag === "OL") return "list";
207
- if (tag === "LI") return "listitem";
208
- if (tag === "TABLE") return "table";
209
- if (tag === "DIALOG") return "dialog";
210
- if (tag === "IMG") return "image";
211
- if (tag === "H1" || tag === "H2" || tag === "H3" || tag === "H4" || tag === "H5" || tag === "H6") {
195
+ if (tag === "A") {
196
+ return element.hasAttribute("href") ? "link" : null;
197
+ }
198
+ if (tag === "BUTTON") {
199
+ return "button";
200
+ }
201
+ if (tag === "TEXTAREA") {
202
+ return "textbox";
203
+ }
204
+ if (tag === "SELECT") {
205
+ return "combobox";
206
+ }
207
+ if (tag === "OPTION") {
208
+ return "option";
209
+ }
210
+ if (tag === "SUMMARY") {
211
+ return "button";
212
+ }
213
+ if (tag === "DETAILS") {
214
+ return "group";
215
+ }
216
+ if (tag === "NAV") {
217
+ return "navigation";
218
+ }
219
+ if (tag === "MAIN") {
220
+ return "main";
221
+ }
222
+ if (tag === "HEADER") {
223
+ return "banner";
224
+ }
225
+ if (tag === "FOOTER") {
226
+ return "contentinfo";
227
+ }
228
+ if (tag === "FORM") {
229
+ return "form";
230
+ }
231
+ if (tag === "UL" || tag === "OL") {
232
+ return "list";
233
+ }
234
+ if (tag === "LI") {
235
+ return "listitem";
236
+ }
237
+ if (tag === "TABLE") {
238
+ return "table";
239
+ }
240
+ if (tag === "DIALOG") {
241
+ return "dialog";
242
+ }
243
+ if (tag === "IMG") {
244
+ return "image";
245
+ }
246
+ if (
247
+ tag === "H1" ||
248
+ tag === "H2" ||
249
+ tag === "H3" ||
250
+ tag === "H4" ||
251
+ tag === "H5" ||
252
+ tag === "H6"
253
+ ) {
212
254
  return "heading";
213
255
  }
214
256
  if (tag === "INPUT") {
215
257
  const type = (element.getAttribute("type") ?? "text").toLowerCase();
216
- if (type === "checkbox") return "checkbox";
217
- if (type === "radio") return "radio";
218
- if (type === "number") return "spinbutton";
219
- if (type === "range") return "slider";
258
+ if (type === "checkbox") {
259
+ return "checkbox";
260
+ }
261
+ if (type === "radio") {
262
+ return "radio";
263
+ }
264
+ if (type === "number") {
265
+ return "spinbutton";
266
+ }
267
+ if (type === "range") {
268
+ return "slider";
269
+ }
220
270
  if (type === "button" || type === "submit" || type === "reset" || type === "file") {
221
271
  return "button";
222
272
  }
223
- if (type === "hidden") return null;
273
+ if (type === "hidden") {
274
+ return null;
275
+ }
276
+ return "textbox";
277
+ }
278
+ if (element.hasAttribute("contenteditable")) {
224
279
  return "textbox";
225
280
  }
226
- if (element.hasAttribute("contenteditable")) return "textbox";
227
281
  return null;
228
282
  }
229
283
 
230
284
  function roleOf(element: Element): string | null {
231
285
  const explicit = element.getAttribute("role");
232
- if (explicit === "none" || explicit === "presentation") return null;
233
- if (explicit) return explicit;
286
+ if (explicit === "none" || explicit === "presentation") {
287
+ return null;
288
+ }
289
+ if (explicit) {
290
+ return explicit;
291
+ }
234
292
  return implicitRole(element);
235
293
  }
236
294
 
237
295
  function labelledByText(element: Element): string | undefined {
238
296
  const ids = element.getAttribute("aria-labelledby");
239
- if (!ids) return undefined;
297
+ if (!ids) {
298
+ return undefined;
299
+ }
240
300
  const document = element.ownerDocument;
241
301
  const text = ids
242
302
  .split(/\s+/)
243
303
  .map((id) => document.getElementById(id)?.textContent ?? "")
244
304
  .join(" ")
245
- .replace(/\s+/g, " ")
305
+ .replaceAll(/\s+/g, " ")
246
306
  .trim();
247
307
  return text.length > 0 ? text : undefined;
248
308
  }
@@ -252,27 +312,39 @@ function associatedLabel(element: Element): string | undefined {
252
312
  const document = element.ownerDocument;
253
313
  if (id) {
254
314
  const label = document.querySelector(`label[for="${id}"]`);
255
- const text = label?.textContent?.replace(/\s+/g, " ").trim();
256
- if (text) return text;
315
+ const text = label?.textContent?.replaceAll(/\s+/g, " ").trim();
316
+ if (text) {
317
+ return text;
318
+ }
257
319
  }
258
320
  const parent = element.closest("label");
259
321
  if (parent) {
260
- const text = parent.textContent?.replace(/\s+/g, " ").trim();
261
- if (text) return text;
322
+ const text = parent.textContent?.replaceAll(/\s+/g, " ").trim();
323
+ if (text) {
324
+ return text;
325
+ }
262
326
  }
263
327
  return undefined;
264
328
  }
265
329
 
266
330
  function accessibleName(element: Element): string | undefined {
267
331
  const labelled = labelledByText(element);
268
- if (labelled) return labelled;
332
+ if (labelled) {
333
+ return labelled;
334
+ }
269
335
  const aria = element.getAttribute("aria-label")?.trim();
270
- if (aria) return aria;
336
+ if (aria) {
337
+ return aria;
338
+ }
271
339
  const label = associatedLabel(element);
272
- if (label) return label;
340
+ if (label) {
341
+ return label;
342
+ }
273
343
  if (element.tagName === "IMG") {
274
344
  const alt = element.getAttribute("alt")?.trim();
275
- if (alt) return alt;
345
+ if (alt) {
346
+ return alt;
347
+ }
276
348
  }
277
349
  const role = roleOf(element);
278
350
  if (
@@ -283,25 +355,37 @@ function accessibleName(element: Element): string | undefined {
283
355
  role === "tab" ||
284
356
  role === "listitem"
285
357
  ) {
286
- const text = element.textContent?.replace(/\s+/g, " ").trim();
287
- if (text) return text;
358
+ const text = element.textContent?.replaceAll(/\s+/g, " ").trim();
359
+ if (text) {
360
+ return text;
361
+ }
288
362
  }
289
363
  const title = element.getAttribute("title")?.trim();
290
- if (title) return title;
364
+ if (title) {
365
+ return title;
366
+ }
291
367
  const placeholder = element.getAttribute("placeholder")?.trim();
292
- if (placeholder) return placeholder;
368
+ if (placeholder) {
369
+ return placeholder;
370
+ }
293
371
  return undefined;
294
372
  }
295
373
 
296
374
  function valueOf(element: Element): string | undefined {
297
375
  const now = element.getAttribute("aria-valuenow");
298
- if (now) return now;
376
+ if (now) {
377
+ return now;
378
+ }
299
379
  if (element.tagName === "INPUT") {
300
380
  const type = (prop<string>(element, "type") ?? "text").toLowerCase();
301
- if (type === "checkbox" || type === "radio") return undefined;
381
+ if (type === "checkbox" || type === "radio") {
382
+ return undefined;
383
+ }
384
+ return prop<string>(element, "value");
385
+ }
386
+ if (element.tagName === "TEXTAREA") {
302
387
  return prop<string>(element, "value");
303
388
  }
304
- if (element.tagName === "TEXTAREA") return prop<string>(element, "value");
305
389
  if (element.tagName === "SELECT") {
306
390
  const selected = prop<ArrayLike<Element>>(element, "selectedOptions");
307
391
  const first = selected && selected.length > 0 ? selected[0] : undefined;
@@ -313,9 +397,11 @@ function valueOf(element: Element): string | undefined {
313
397
  return undefined;
314
398
  }
315
399
 
316
- function statesOf(element: Element, document: Document): string[] {
317
- const states: string[] = [];
318
- if (isVisible(element)) states.push("visible");
400
+ function statesOf(element: Element, document: Document): Array<string> {
401
+ const states: Array<string> = [];
402
+ if (isVisible(element)) {
403
+ states.push("visible");
404
+ }
319
405
  if (
320
406
  element.hasAttribute("disabled") ||
321
407
  element.getAttribute("aria-disabled") === "true" ||
@@ -323,12 +409,21 @@ function statesOf(element: Element, document: Document): string[] {
323
409
  ) {
324
410
  states.push("disabled");
325
411
  }
326
- if (prop<boolean>(element, "checked") === true || element.getAttribute("aria-checked") === "true") {
412
+ if (
413
+ prop<boolean>(element, "checked") === true ||
414
+ element.getAttribute("aria-checked") === "true"
415
+ ) {
327
416
  states.push("checked");
328
417
  }
329
- if (document.activeElement === element) states.push("focused");
330
- if (element.getAttribute("aria-expanded") === "true") states.push("expanded");
331
- if (element.getAttribute("aria-pressed") === "true") states.push("pressed");
418
+ if (document.activeElement === element) {
419
+ states.push("focused");
420
+ }
421
+ if (element.getAttribute("aria-expanded") === "true") {
422
+ states.push("expanded");
423
+ }
424
+ if (element.getAttribute("aria-pressed") === "true") {
425
+ states.push("pressed");
426
+ }
332
427
  if (
333
428
  element.getAttribute("aria-selected") === "true" ||
334
429
  prop<boolean>(element, "selected") === true
@@ -345,13 +440,25 @@ function statesOf(element: Element, document: Document): string[] {
345
440
  }
346
441
 
347
442
  function isInteresting(element: Element): boolean {
348
- if (SKIP_TAGS.has(element.tagName)) return false;
443
+ if (SKIP_TAGS.has(element.tagName)) {
444
+ return false;
445
+ }
349
446
  // Labels contribute their text to the control's name; they are not nodes.
350
- if (element.tagName === "LABEL") return false;
351
- if (roleOf(element)) return true;
352
- if (accessibleName(element)) return true;
353
- if (element.tagName === "INPUT") return true;
354
- if (element.hasAttribute("tabindex")) return true;
447
+ if (element.tagName === "LABEL") {
448
+ return false;
449
+ }
450
+ if (roleOf(element)) {
451
+ return true;
452
+ }
453
+ if (accessibleName(element)) {
454
+ return true;
455
+ }
456
+ if (element.tagName === "INPUT") {
457
+ return true;
458
+ }
459
+ if (element.hasAttribute("tabindex")) {
460
+ return true;
461
+ }
355
462
  return false;
356
463
  }
357
464
 
@@ -369,14 +476,18 @@ function walk(
369
476
  document: Document,
370
477
  depth: number,
371
478
  budget: Budget,
372
- ): SemanticNode[] {
373
- if (SKIP_TAGS.has(element.tagName) || !isVisible(element)) return [];
479
+ ): Array<SemanticNode> {
480
+ if (SKIP_TAGS.has(element.tagName) || !isVisible(element)) {
481
+ return [];
482
+ }
374
483
  const interesting = isInteresting(element) || depth === 0;
375
484
  if (!interesting) {
376
- const promoted: SemanticNode[] = [];
485
+ const promoted: Array<SemanticNode> = [];
377
486
  for (const child of Array.from(element.children)) {
378
487
  promoted.push(...walk(child, document, depth, budget));
379
- if (budget.truncated) break;
488
+ if (budget.truncated) {
489
+ break;
490
+ }
380
491
  }
381
492
  return promoted;
382
493
  }
@@ -392,9 +503,13 @@ function walk(
392
503
  children: [],
393
504
  };
394
505
  const name = accessibleName(element);
395
- if (name) node.name = name;
506
+ if (name) {
507
+ node.name = name;
508
+ }
396
509
  const value = valueOf(element);
397
- if (value !== undefined && value.length > 0) node.value = value;
510
+ if (value !== undefined && value.length > 0) {
511
+ node.value = value;
512
+ }
398
513
  if (depth >= MAX_DEPTH) {
399
514
  if (element.children.length > 0) {
400
515
  budget.truncated = true;
@@ -425,7 +540,7 @@ function snapshot(world: ShimWorld): ShimResult<{ page: PageState; root: Semanti
425
540
  };
426
541
  }
427
542
 
428
- function centerOf(element: Element): { x: number; y: number } {
543
+ function centerOf(element: Element) {
429
544
  const rect = element.getBoundingClientRect();
430
545
  return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
431
546
  }
@@ -456,7 +571,7 @@ function dispatchKey(
456
571
  element: Element,
457
572
  type: string,
458
573
  key: string,
459
- modifiers: string[],
574
+ modifiers: Array<string>,
460
575
  ): boolean {
461
576
  const event = new world.KeyboardEvent(type, {
462
577
  bubbles: true,
@@ -494,7 +609,9 @@ function focus(element: Element): void {
494
609
 
495
610
  function click(world: ShimWorld, element: string): ShimResult<Record<string, never>> {
496
611
  const node = resolveRef(world.document, element);
497
- if (!node) return unresolved(element);
612
+ if (!node) {
613
+ return unresolved(element);
614
+ }
498
615
  focus(node);
499
616
  dispatchMouse(world, node, "pointerdown");
500
617
  dispatchMouse(world, node, "mousedown");
@@ -504,8 +621,11 @@ function click(world: ShimWorld, element: string): ShimResult<Record<string, nev
504
621
  // (submit, checkbox toggle, link navigation) that a synthetic MouseEvent
505
622
  // does not. isTrusted stays false.
506
623
  const nativeClick = prop<(this: Element) => void>(node, "click");
507
- if (nativeClick) nativeClick.call(node);
508
- else dispatchMouse(world, node, "click");
624
+ if (nativeClick) {
625
+ nativeClick.call(node);
626
+ } else {
627
+ dispatchMouse(world, node, "click");
628
+ }
509
629
  return ok();
510
630
  }
511
631
 
@@ -515,12 +635,13 @@ function insertText(element: Element, text: string): void {
515
635
  const start = prop<number | null>(element, "selectionStart") ?? current.length;
516
636
  const end = prop<number | null>(element, "selectionEnd") ?? start;
517
637
  const next = `${current.slice(0, start)}${text}${current.slice(end)}`;
518
- (element as FormControl).value = next;
638
+ Object.assign(element, { value: next });
519
639
  const cursor = start + text.length;
520
- prop<(this: Element, start: number, end: number) => void>(
640
+ prop<(this: Element, start: number, end: number) => void>(element, "setSelectionRange")?.call(
521
641
  element,
522
- "setSelectionRange",
523
- )?.call(element, cursor, cursor);
642
+ cursor,
643
+ cursor,
644
+ );
524
645
  return;
525
646
  }
526
647
  if (element.hasAttribute("contenteditable") || prop<boolean>(element, "isContentEditable")) {
@@ -534,7 +655,9 @@ function typeInto(
534
655
  text: string,
535
656
  ): ShimResult<Record<string, never>> {
536
657
  const node = resolveRef(world.document, element);
537
- if (!node) return unresolved(element);
658
+ if (!node) {
659
+ return unresolved(element);
660
+ }
538
661
  focus(node);
539
662
  for (const char of text) {
540
663
  dispatchKey(world, node, "keydown", char, []);
@@ -549,13 +672,15 @@ function typeInto(
549
672
  function press(
550
673
  world: ShimWorld,
551
674
  key: string,
552
- modifiers: string[] = [],
675
+ modifiers: Array<string> = [],
553
676
  element?: string | null,
554
677
  ): ShimResult<Record<string, never>> {
555
678
  let node: Element | null;
556
679
  if (element) {
557
680
  node = resolveRef(world.document, element);
558
- if (!node) return unresolved(element);
681
+ if (!node) {
682
+ return unresolved(element);
683
+ }
559
684
  } else {
560
685
  node = world.document.activeElement ?? world.document.body ?? world.document.documentElement;
561
686
  }
@@ -573,7 +698,9 @@ function scroll(
573
698
  ): ShimResult<Record<string, never>> {
574
699
  if (element) {
575
700
  const node = resolveRef(world.document, element);
576
- if (!node) return unresolved(element);
701
+ if (!node) {
702
+ return unresolved(element);
703
+ }
577
704
  node.scrollTop += deltaY;
578
705
  node.scrollLeft += deltaX;
579
706
  node.dispatchEvent(new world.Event("scroll", { bubbles: true }));
@@ -592,9 +719,13 @@ function drag(
592
719
  targetRef: string,
593
720
  ): ShimResult<Record<string, never>> {
594
721
  const source = resolveRef(world.document, sourceRef);
595
- if (!source) return unresolved(sourceRef);
722
+ if (!source) {
723
+ return unresolved(sourceRef);
724
+ }
596
725
  const target = resolveRef(world.document, targetRef);
597
- if (!target) return unresolved(targetRef);
726
+ if (!target) {
727
+ return unresolved(targetRef);
728
+ }
598
729
  const from = centerOf(source);
599
730
  const to = centerOf(target);
600
731
  // Two-point untrusted path: pointer/mouse at the source center, then the
@@ -603,10 +734,9 @@ function drag(
603
734
  dispatchMouse(world, source, "mousedown", from);
604
735
  dispatchMouse(world, target, "pointermove", to);
605
736
  dispatchMouse(world, target, "mousemove", to);
606
- const transfer: DataTransfer =
607
- typeof DataTransfer === "function"
608
- ? new DataTransfer()
609
- : ({ setData() {}, getData() { return ""; } } as unknown as DataTransfer);
737
+ // Only the two methods are read, so a page without DataTransfer gets a stand-in with just those.
738
+ const transfer: Pick<DataTransfer, "setData" | "getData"> =
739
+ typeof DataTransfer === "function" ? new DataTransfer() : { setData() {}, getData: () => "" };
610
740
  const dragEvent = (type: string, current: Element, point: { x: number; y: number }) => {
611
741
  const event = new world.MouseEvent(type, {
612
742
  bubbles: true,
@@ -632,7 +762,9 @@ function decodeBase64(content: string): Uint8Array<ArrayBuffer> | null {
632
762
  try {
633
763
  const binary = atob(content);
634
764
  const bytes = new Uint8Array(binary.length);
635
- for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);
765
+ for (let i = 0; i < binary.length; i += 1) {
766
+ bytes[i] = binary.charCodeAt(i);
767
+ }
636
768
  return bytes;
637
769
  } catch {
638
770
  return null;
@@ -644,13 +776,19 @@ function acceptMatches(fileName: string, mediaType: string, accept: string): boo
644
776
  .split(",")
645
777
  .map((token) => token.trim())
646
778
  .filter((token) => token.length > 0);
647
- if (tokens.length === 0) return true;
779
+ if (tokens.length === 0) {
780
+ return true;
781
+ }
648
782
  const name = fileName.toLowerCase();
649
783
  const type = mediaType.toLowerCase();
650
784
  return tokens.some((token) => {
651
785
  const needle = token.toLowerCase();
652
- if (needle.startsWith(".")) return name.endsWith(needle);
653
- if (needle.endsWith("/*")) return type.startsWith(needle.slice(0, -1));
786
+ if (needle.startsWith(".")) {
787
+ return name.endsWith(needle);
788
+ }
789
+ if (needle.endsWith("/*")) {
790
+ return type.startsWith(needle.slice(0, -1));
791
+ }
654
792
  return type === needle;
655
793
  });
656
794
  }
@@ -658,10 +796,12 @@ function acceptMatches(fileName: string, mediaType: string, accept: string): boo
658
796
  function setFileInput(
659
797
  world: ShimWorld,
660
798
  element: string,
661
- files: FileInputFile[],
799
+ files: Array<FileInputFile>,
662
800
  ): ShimResult<Record<string, never>> {
663
801
  const node = resolveRef(world.document, element);
664
- if (!node) return unresolved(element);
802
+ if (!node) {
803
+ return unresolved(element);
804
+ }
665
805
  const inputType = (prop<string>(node, "type") ?? node.getAttribute("type") ?? "").toLowerCase();
666
806
  if (node.tagName !== "INPUT" || inputType !== "file") {
667
807
  return unsupported("set_file_input requires an input type=file");
@@ -682,7 +822,9 @@ function setFileInput(
682
822
  const accept = node.getAttribute("accept") ?? "";
683
823
  const transfer = new DataTransferCtor();
684
824
  for (const file of files) {
685
- if (!file.name) return unsupported("set_file_input file name must be non-empty");
825
+ if (!file.name) {
826
+ return unsupported("set_file_input file name must be non-empty");
827
+ }
686
828
  const bytes = decodeBase64(file.contentBase64);
687
829
  if (!bytes) {
688
830
  return unsupported(`set_file_input file ${JSON.stringify(file.name)} is not valid base64`);
@@ -691,10 +833,12 @@ function setFileInput(
691
833
  if (!acceptMatches(file.name, mediaType, accept)) {
692
834
  return unsupported(`set_file_input file ${JSON.stringify(file.name)} does not match accept`);
693
835
  }
694
- transfer.items.add(new FileCtor([bytes], file.name, mediaType ? { type: mediaType } : undefined));
836
+ transfer.items.add(
837
+ new FileCtor([bytes], file.name, mediaType ? { type: mediaType } : undefined),
838
+ );
695
839
  }
696
840
  try {
697
- (node as HTMLInputElement).files = transfer.files;
841
+ Object.assign(node, { files: transfer.files });
698
842
  } catch (error) {
699
843
  return unsupported(
700
844
  `the page refused FileList assignment from a constructed DataTransfer: ${String(error)}`,
@@ -731,10 +875,16 @@ function waitFor(world: ShimWorld, predicate: WaitPredicate): ShimResult<{ holds
731
875
  }
732
876
 
733
877
  function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
734
- const existing = world[SHIM_GLOBAL] as AgentControlApi | undefined;
735
- if (existing) return existing.readEvents.bind(existing);
878
+ const existing = world[SHIM_GLOBAL];
879
+ if (existing) {
880
+ return existing.readEvents.bind(existing);
881
+ }
736
882
 
737
- const ring: PageEvent[] = [];
883
+ const epoch =
884
+ typeof crypto !== "undefined" && typeof crypto.randomUUID === "function"
885
+ ? crypto.randomUUID()
886
+ : `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
887
+ const ring: Array<PageEvent> = [];
738
888
  let nextSeq = 1;
739
889
  let dropped = 0;
740
890
 
@@ -750,12 +900,12 @@ function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
750
900
 
751
901
  const wrap = (level: "log" | "info" | "warn" | "error" | "debug") => {
752
902
  const original = world.console[level]?.bind(world.console);
753
- world.console[level] = (...args: unknown[]) => {
903
+ world.console[level] = (...args: Array<unknown>) => {
754
904
  push({
755
905
  seq: nextSeq,
756
906
  kind: "console",
757
907
  level,
758
- text: args.map((value) => String(value)).join(" "),
908
+ text: args.map(String).join(" "),
759
909
  });
760
910
  original?.(...args);
761
911
  };
@@ -768,20 +918,20 @@ function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
768
918
 
769
919
  world.addEventListener("error", (event) => {
770
920
  const message =
771
- "message" in event && typeof event.message === "string"
772
- ? event.message
773
- : String(event);
921
+ "message" in event && typeof event.message === "string" ? event.message : String(event);
774
922
  push({ seq: nextSeq, kind: "error", message });
775
923
  });
776
924
  world.addEventListener("unhandledrejection", (event) => {
777
- const reason =
778
- "reason" in event ? String((event as PromiseRejectionEvent).reason) : "unhandledrejection";
925
+ const reason = "reason" in event ? String(event.reason) : "unhandledrejection";
779
926
  push({ seq: nextSeq, kind: "error", message: reason });
780
927
  });
781
928
 
782
929
  const notifyNavigation = () => {
783
930
  push({ seq: nextSeq, kind: "navigation", url: world.location.href });
784
931
  };
932
+ // Record the initial document load so full-document navigations and reloads
933
+ // are observable through `longhorn://agent-control/navigation` as seq 1.
934
+ notifyNavigation();
785
935
  world.addEventListener("hashchange", notifyNavigation);
786
936
  world.addEventListener("popstate", notifyNavigation);
787
937
  const history = world.history;
@@ -796,21 +946,27 @@ function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
796
946
  notifyNavigation();
797
947
  }) as History["replaceState"];
798
948
 
799
- return (sinceSeq = 0) => ({
800
- events: ring.filter((event) => event.seq > sinceSeq),
801
- nextSeq,
802
- dropped,
803
- });
949
+ return (sinceSeq = 0, sinceEpoch?: string | null) => {
950
+ const effectiveSince =
951
+ sinceEpoch !== undefined && sinceEpoch !== null && sinceEpoch !== epoch ? 0 : sinceSeq;
952
+ return {
953
+ epoch,
954
+ events: ring.filter((event) => event.seq > effectiveSince),
955
+ nextSeq,
956
+ dropped,
957
+ };
958
+ };
804
959
  }
805
960
 
806
- function installOriginTracking(world: ShimWorld): Pick<
807
- AgentControlApi,
808
- "markAgentOrigin" | "isAgentOriginated"
809
- > {
961
+ function installOriginTracking(
962
+ world: ShimWorld,
963
+ ): Pick<AgentControlApi, "markAgentOrigin" | "isAgentOriginated"> {
810
964
  let agentOriginSeq = 0;
811
965
  let humanOriginSeq = 0;
812
966
  const markHuman = (event: Event) => {
813
- if (event.isTrusted) humanOriginSeq = agentOriginSeq + 1;
967
+ if (event.isTrusted) {
968
+ humanOriginSeq = agentOriginSeq + 1;
969
+ }
814
970
  };
815
971
  world.addEventListener("pointerdown", markHuman, true);
816
972
  world.addEventListener("keydown", markHuman, true);
@@ -824,8 +980,10 @@ function installOriginTracking(world: ShimWorld): Pick<
824
980
  }
825
981
 
826
982
  export function installAgentControlShim(world: ShimWorld): AgentControlApi {
827
- const existing = world[SHIM_GLOBAL] as AgentControlApi | undefined;
828
- if (existing) return existing;
983
+ const existing = world[SHIM_GLOBAL];
984
+ if (existing) {
985
+ return existing;
986
+ }
829
987
  const readEvents = installEventRing(world);
830
988
  const origin = installOriginTracking(world);
831
989
  const api: AgentControlApi = {