@inflatable-cookie/longhorn 0.6.0 → 0.7.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 (133) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -7
  3. package/src/agent-control/generated/protocol.ts +228 -0
  4. package/src/agent-control/index.ts +1 -5
  5. package/src/agent-control/selection.ts +10 -8
  6. package/src/agent-control/shim.ts +149 -77
  7. package/src/bridge/generated/fields.ts +1 -1
  8. package/src/bridge/generated/protocol.ts +55 -4
  9. package/src/bridge/generated/variant-fields.ts +1 -1
  10. package/src/bridge/job.ts +49 -2
  11. package/src/bridge/runtime/connection.ts +11 -14
  12. package/src/bridge/stream/connection.ts +14 -23
  13. package/src/bridge/stream/tracker.ts +5 -0
  14. package/src/bridge/supervision.ts +102 -80
  15. package/src/bridge/validation/base.ts +7 -27
  16. package/src/bridge/validation/negotiation.ts +1 -6
  17. package/src/bridge/validation/operations.ts +2 -14
  18. package/src/commands/client.ts +16 -2
  19. package/src/commands/controller.ts +96 -58
  20. package/src/commands/generated/fields.ts +4 -4
  21. package/src/commands/generated/protocol.ts +24 -1
  22. package/src/commands/generated/variant-fields.ts +1 -1
  23. package/src/commands/keyboard.ts +4 -13
  24. package/src/commands/physical-code.ts +11 -0
  25. package/src/commands/ports.ts +2 -1
  26. package/src/commands/projectors.ts +52 -29
  27. package/src/commands/validation.ts +408 -188
  28. package/src/config/client.ts +3 -3
  29. package/src/config/generated/base.ts +22 -6
  30. package/src/config/generated/fields.ts +2 -1
  31. package/src/config/generated/label-template.ts +1 -1
  32. package/src/config/generated/labels.ts +1 -1
  33. package/src/config/generated/protocol.ts +1 -1
  34. package/src/config/generated/restore.ts +35 -12
  35. package/src/config/generated/variant-fields.ts +1 -1
  36. package/src/config/validation/commands.ts +11 -13
  37. package/src/config/validation/outcomes.ts +147 -46
  38. package/src/config/validation/primitives.ts +11 -39
  39. package/src/config/validation/projection.ts +44 -44
  40. package/src/core/connection.ts +124 -29
  41. package/src/core/controller-lifecycle.ts +144 -0
  42. package/src/core/generated/identifiers.ts +4 -0
  43. package/src/core/identifiers.ts +40 -0
  44. package/src/core/index.ts +10 -1
  45. package/src/core/json-value.ts +58 -0
  46. package/src/core/listeners.ts +35 -0
  47. package/src/core/record.ts +76 -0
  48. package/src/core/transport.ts +10 -1
  49. package/src/core/wire-copy.ts +1 -0
  50. package/src/history/client.ts +11 -7
  51. package/src/history/controller.ts +72 -74
  52. package/src/history/generated/fields.ts +2 -1
  53. package/src/history/generated/protocol.ts +8 -5
  54. package/src/history/generated/variant-fields.ts +1 -1
  55. package/src/history/ports.ts +7 -2
  56. package/src/history/validation.ts +15 -37
  57. package/src/history-tree/client.ts +15 -13
  58. package/src/history-tree/controller.ts +131 -90
  59. package/src/history-tree/generated/fields.ts +3 -1
  60. package/src/history-tree/generated/protocol.ts +43 -7
  61. package/src/history-tree/generated/variant-fields.ts +7 -1
  62. package/src/history-tree/ports.ts +9 -4
  63. package/src/history-tree/validation.ts +44 -35
  64. package/src/layout/generated/protocol.ts +2 -1
  65. package/src/layout/generated/variant-fields.ts +47 -0
  66. package/src/layout/index.ts +1 -1
  67. package/src/layout/ratio.ts +9 -2
  68. package/src/layout/validation.ts +88 -13
  69. package/src/licence/client.ts +11 -7
  70. package/src/licence/controller.ts +64 -43
  71. package/src/licence/generated/fields.ts +2 -1
  72. package/src/licence/generated/protocol.ts +8 -4
  73. package/src/licence/generated/variant-fields.ts +1 -1
  74. package/src/licence/key.ts +2 -6
  75. package/src/licence/ports.ts +7 -2
  76. package/src/licence/validation.ts +27 -20
  77. package/src/native-content/client.ts +32 -1
  78. package/src/native-content/generated/fields.ts +4 -4
  79. package/src/native-content/generated/protocol.ts +16 -5
  80. package/src/native-content/generated/variant-fields.ts +1 -1
  81. package/src/native-content/ports.ts +2 -1
  82. package/src/native-content/validation/base.ts +16 -46
  83. package/src/native-content/validation/messages.ts +3 -0
  84. package/src/notifications/client.ts +17 -39
  85. package/src/notifications/controller.ts +266 -165
  86. package/src/notifications/generated/fields.ts +2 -1
  87. package/src/notifications/generated/labels.ts +1 -1
  88. package/src/notifications/generated/protocol.ts +5 -1
  89. package/src/notifications/generated/variant-fields.ts +1 -1
  90. package/src/notifications/ports.ts +2 -1
  91. package/src/notifications/tone.ts +1 -1
  92. package/src/notifications/validation.ts +41 -51
  93. package/src/operation/bridge.ts +79 -19
  94. package/src/operation/client.ts +17 -45
  95. package/src/operation/controller.ts +38 -59
  96. package/src/operation/generated/fields.ts +2 -1
  97. package/src/operation/generated/labels.ts +1 -1
  98. package/src/operation/generated/protocol.ts +5 -1
  99. package/src/operation/generated/variant-fields.ts +1 -1
  100. package/src/operation/ports.ts +2 -1
  101. package/src/operation/validation.ts +48 -50
  102. package/src/settings/connection.ts +10 -14
  103. package/src/settings/generated/fields.ts +2 -1
  104. package/src/settings/generated/protocol.ts +5 -1
  105. package/src/settings/generated/variant-fields.ts +1 -1
  106. package/src/settings/validation/authority.ts +2 -2
  107. package/src/settings/validation/command.ts +5 -4
  108. package/src/settings/validation/primitives.ts +32 -67
  109. package/src/settings/validation/registry.ts +13 -11
  110. package/src/surface-transfer/generated/fields.ts +1 -1
  111. package/src/surface-transfer/generated/protocol.ts +3 -3
  112. package/src/surface-transfer/generated/variant-fields.ts +1 -1
  113. package/src/surface-transfer/validation.ts +76 -53
  114. package/src/surfaces/client.ts +26 -38
  115. package/src/surfaces/generated/fields.ts +1 -1
  116. package/src/surfaces/generated/protocol.ts +1 -1
  117. package/src/surfaces/generated/variant-fields.ts +1 -1
  118. package/src/surfaces/validation.ts +198 -61
  119. package/src/transfer/connection.ts +25 -39
  120. package/src/transfer/generated/fields.ts +2 -1
  121. package/src/transfer/generated/protocol.ts +9 -1
  122. package/src/transfer/generated/variant-fields.ts +1 -1
  123. package/src/transfer/index.ts +8 -1
  124. package/src/transfer/payload.ts +12 -9
  125. package/src/transfer/validation.ts +146 -60
  126. package/src/update/client.ts +43 -16
  127. package/src/update/controller.ts +187 -104
  128. package/src/update/generated/fields.ts +2 -1
  129. package/src/update/generated/protocol.ts +19 -7
  130. package/src/update/generated/variant-fields.ts +3 -2
  131. package/src/update/ports.ts +11 -7
  132. package/src/update/serialized.ts +8 -6
  133. package/src/update/validation.ts +18 -28
package/README.md CHANGED
@@ -9,8 +9,8 @@ effigy check:bindings
9
9
  effigy test:ts
10
10
  ```
11
11
 
12
- Every domain is a subpath. `@inflatable-cookie/longhorn` itself resolves to
13
- `core`, the base every other domain builds on.
12
+ Every domain is a subpath. The bare package name resolves to nothing: import
13
+ `/core`, the base every other domain builds on, by its subpath.
14
14
 
15
15
  | Subpath | Domain |
16
16
  | --- | --- |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inflatable-cookie/longhorn",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -9,13 +9,7 @@
9
9
  },
10
10
  "homepage": "https://github.com/inflatable-cookie/longhorn#readme",
11
11
  "type": "module",
12
- "types": "./src/core/index.ts",
13
12
  "exports": {
14
- ".": {
15
- "types": "./src/core/index.ts",
16
- "import": "./src/core/index.ts",
17
- "default": "./src/core/index.ts"
18
- },
19
13
  "./agent-control": {
20
14
  "types": "./src/agent-control/index.ts",
21
15
  "import": "./src/agent-control/index.ts",
@@ -0,0 +1,228 @@
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
+
3
+ export type ElementRef = string;
4
+
5
+ export type WebviewLabel = string;
6
+
7
+ export type SelectionId = string;
8
+
9
+ export type WindowId = string;
10
+
11
+ export type CommandId = string;
12
+
13
+ export type SemanticNode = {
14
+ /**
15
+ * Reference stamped into the live DOM for this element.
16
+ */
17
+ elementRef: ElementRef,
18
+ /**
19
+ * Accessibility-style role, e.g. `button`, `textbox`.
20
+ */
21
+ role: string,
22
+ /**
23
+ * Accessible name, when the element has one.
24
+ */
25
+ name: string | null,
26
+ /**
27
+ * Current value, when the role carries one.
28
+ */
29
+ value: string | null,
30
+ /**
31
+ * State tokens, e.g. `disabled`, `checked`, `expanded`.
32
+ */
33
+ states: Array<string>,
34
+ /**
35
+ * Child elements.
36
+ */
37
+ children: Array<SemanticNode>, };
38
+
39
+ export type PageState = {
40
+ /**
41
+ * Current document URL.
42
+ */
43
+ url: string,
44
+ /**
45
+ * Current document title.
46
+ */
47
+ title: string, };
48
+
49
+ export type PageEvent = { "kind": "console",
50
+ /**
51
+ * Sequence of the message within the document.
52
+ */
53
+ seq: number,
54
+ /**
55
+ * Console level reported by the page.
56
+ */
57
+ level: string,
58
+ /**
59
+ * Text logged by the page.
60
+ */
61
+ text: string, } | { "kind": "error",
62
+ /**
63
+ * Sequence of the error within the document.
64
+ */
65
+ seq: number,
66
+ /**
67
+ * Error message reported by the page.
68
+ */
69
+ message: string, } | { "kind": "navigation",
70
+ /**
71
+ * Sequence of the navigation within the document.
72
+ */
73
+ seq: number,
74
+ /**
75
+ * New page URL.
76
+ */
77
+ url: string, };
78
+
79
+ export type PageEventBatch = {
80
+ /**
81
+ * Identity of the document whose ring produced this batch.
82
+ */
83
+ epoch: string,
84
+ /**
85
+ * Events after the requested cursor, or all retained events after a reload.
86
+ */
87
+ events: Array<PageEvent>,
88
+ /**
89
+ * Sequence number the ring will assign to the next event.
90
+ */
91
+ nextSeq: number,
92
+ /**
93
+ * Count of events evicted from this document's ring.
94
+ */
95
+ dropped: number, };
96
+
97
+ export type WaitPredicate = { "predicate": "refResolve",
98
+ /**
99
+ * Element expected to resolve.
100
+ */
101
+ element: ElementRef, } | { "predicate": "refAbsent",
102
+ /**
103
+ * Element expected to be gone.
104
+ */
105
+ element: ElementRef, } | { "predicate": "pageUrlContains",
106
+ /**
107
+ * Substring expected in the URL.
108
+ */
109
+ needle: string, } | { "predicate": "pageTitleContains",
110
+ /**
111
+ * Substring expected in the title.
112
+ */
113
+ needle: string, };
114
+
115
+ export type FileInputFile = {
116
+ /**
117
+ * File name presented to the page (`File.name`).
118
+ */
119
+ name: string,
120
+ /**
121
+ * Optional media type presented to the page (`File.type`). Serialized as
122
+ * `null` when absent, so the shim receives the shape its binding declares.
123
+ */
124
+ mediaType: string | null,
125
+ /**
126
+ * Standard base64 of the file bytes.
127
+ */
128
+ contentBase64: string, };
129
+
130
+ export type ToolError = { "error": "unresolvedRef",
131
+ /**
132
+ * Ref that failed to resolve.
133
+ */
134
+ element: ElementRef, } | { "error": "unknownWindow",
135
+ /**
136
+ * Window that was not found.
137
+ */
138
+ window: WindowId, } | { "error": "unknownWebview",
139
+ /**
140
+ * Webview label that was not found.
141
+ */
142
+ webview: WebviewLabel, } | { "error": "waitTimeout",
143
+ /**
144
+ * Bound that elapsed, in milliseconds.
145
+ */
146
+ timeoutMs: number, } | { "error": "evaluationFailed",
147
+ /**
148
+ * Host-provided failure detail.
149
+ */
150
+ message: string, } | { "error": "commandFailed",
151
+ /**
152
+ * Command that failed.
153
+ */
154
+ command: CommandId,
155
+ /**
156
+ * Host-provided failure detail.
157
+ */
158
+ message: string, } | { "error": "unsupported",
159
+ /**
160
+ * What cannot be served, and why.
161
+ */
162
+ message: string, } | { "error": "hostFailure",
163
+ /**
164
+ * Host-provided failure detail.
165
+ */
166
+ message: string, } | { "error": "unknownSelection",
167
+ /**
168
+ * Id that was not found.
169
+ */
170
+ id: SelectionId, } | { "error": "expiredSelection",
171
+ /**
172
+ * Id that had already expired.
173
+ */
174
+ id: SelectionId, } | { "error": "settledSelection",
175
+ /**
176
+ * Id that had already settled.
177
+ */
178
+ id: SelectionId, } | { "error": "malformedSelection",
179
+ /**
180
+ * What was wrong with the answer.
181
+ */
182
+ message: string, } | { "error": "cancelledSelection",
183
+ /**
184
+ * Id that was cancelled before settlement.
185
+ */
186
+ id: SelectionId, } | { "error": "invalidArgument",
187
+ /**
188
+ * What was invalid about the argument.
189
+ */
190
+ message: string, };
191
+
192
+ export type SelectionKind = "open" | "save";
193
+
194
+ export type SelectionFilter = {
195
+ /**
196
+ * Filter display name.
197
+ */
198
+ name: string,
199
+ /**
200
+ * File extensions without a leading dot.
201
+ */
202
+ extensions: Array<string>, };
203
+
204
+ export type BeginSelectionArgs = {
205
+ /**
206
+ * Open or save.
207
+ */
208
+ kind: SelectionKind,
209
+ /**
210
+ * Directory intent; only meaningful for open.
211
+ */
212
+ directory: boolean,
213
+ /**
214
+ * Multiple paths; only meaningful for open.
215
+ */
216
+ multiple: boolean,
217
+ /**
218
+ * Optional extension filters. Hints for the agent; not enforced.
219
+ */
220
+ filters: Array<SelectionFilter>,
221
+ /**
222
+ * Dialog title, when the caller supplied one.
223
+ */
224
+ title: string | null,
225
+ /**
226
+ * Default-path hint, never an answer.
227
+ */
228
+ defaultPath: string | null, };
@@ -10,15 +10,11 @@ export {
10
10
  TRUNCATED_ROLE,
11
11
  installAgentControlShim,
12
12
  type AgentControlApi,
13
- type FileInputFile,
14
- type PageEvent,
15
- type PageState,
16
- type SemanticNode,
17
13
  type ShimResult,
18
14
  type ShimWorld,
19
15
  type ToolErrorBody,
20
- type WaitPredicate,
21
16
  } from "./shim.ts";
17
+ export type * from "./generated/protocol.ts";
22
18
  export {
23
19
  BEGIN_SELECTION_COMMAND,
24
20
  SELECTION_ORIGIN_AGENT,
@@ -4,6 +4,7 @@
4
4
  //! opens. Human-originated calls pass the original options to the plugin
5
5
  //! unchanged. Origin is the in-page shim, not an active-server flag.
6
6
 
7
+ import type { BeginSelectionArgs, SelectionFilter, SelectionKind } from "./generated/protocol.ts";
7
8
  import { SHIM_GLOBAL } from "./shim.ts";
8
9
  import type { AgentControlApi } from "./shim.ts";
9
10
 
@@ -22,10 +23,7 @@ export const SELECTION_ORIGIN_HUMAN = "human";
22
23
  /** Serializable origin a consumer passes into its own command arguments. */
23
24
  export type SelectionOrigin = typeof SELECTION_ORIGIN_AGENT | typeof SELECTION_ORIGIN_HUMAN;
24
25
 
25
- export type DialogFilter = {
26
- name: string;
27
- extensions: Array<string>;
28
- };
26
+ export type DialogFilter = SelectionFilter;
29
27
 
30
28
  export type OpenDialogOptions = {
31
29
  title?: string;
@@ -72,15 +70,19 @@ export function currentSelectionOrigin(world: object = globalThis): SelectionOri
72
70
  return isAgentOriginated(world) ? SELECTION_ORIGIN_AGENT : SELECTION_ORIGIN_HUMAN;
73
71
  }
74
72
 
75
- function beginOptions(kind: "open" | "save", options: OpenDialogOptions | undefined) {
73
+ function beginOptions(
74
+ kind: SelectionKind,
75
+ options: OpenDialogOptions | undefined,
76
+ ): BeginSelectionArgs {
76
77
  const open = kind === "open" ? options : undefined;
78
+ const { filters = [], title, defaultPath } = options ?? {};
77
79
  return {
78
80
  kind,
79
81
  directory: open?.directory ?? false,
80
82
  multiple: open?.multiple ?? false,
81
- filters: options?.filters ?? [],
82
- title: options?.title,
83
- defaultPath: options?.defaultPath,
83
+ filters,
84
+ title: title ?? null,
85
+ defaultPath: defaultPath ?? null,
84
86
  };
85
87
  }
86
88
 
@@ -6,6 +6,17 @@
6
6
  //! table that outlives the DOM. Truncation is a sentinel child with role
7
7
  //! `truncated` (the core vocabulary has no truncation field; a silent cap
8
8
  //! is forbidden). Synthetic events are untrusted: `isTrusted` stays false.
9
+ //! Every shape that crosses to Rust is a generated binding (contract 010).
10
+
11
+ import type {
12
+ FileInputFile,
13
+ PageEvent,
14
+ PageEventBatch,
15
+ PageState,
16
+ SemanticNode,
17
+ ToolError,
18
+ WaitPredicate,
19
+ } from "./generated/protocol.ts";
9
20
 
10
21
  export const REF_ATTR = "data-longhorn-agent-ref";
11
22
  export const REF_SEQ_ATTR = "data-longhorn-agent-ref-seq";
@@ -35,43 +46,14 @@ function prop<T>(element: Element, name: string): T | undefined {
35
46
  return Reflect.get(element, name) as T | undefined;
36
47
  }
37
48
 
38
- export type ToolErrorBody =
39
- | { error: "unresolvedRef"; element: string }
40
- | { error: "unsupported"; message: string };
49
+ /** The tool failures the page itself can report. */
50
+ export type ToolErrorBody = Extract<ToolError, { error: "unresolvedRef" | "unsupported" }>;
41
51
 
42
52
  export type ShimOk<T> = { ok: true } & T;
43
53
  export type ShimErr = { ok: false; error: ToolErrorBody };
44
54
  export type ShimResult<T> = ShimOk<T> | ShimErr;
45
55
  export type ActionOk = ShimOk<Record<string, never>>;
46
56
 
47
- export type SemanticNode = {
48
- elementRef: string;
49
- role: string;
50
- name?: string;
51
- value?: string;
52
- states: Array<string>;
53
- children: Array<SemanticNode>;
54
- };
55
-
56
- export type PageState = { url: string; title: string };
57
-
58
- export type PageEvent =
59
- | { seq: number; kind: "console"; level: string; text: string }
60
- | { seq: number; kind: "error"; message: string }
61
- | { seq: number; kind: "navigation"; url: string };
62
-
63
- export type WaitPredicate =
64
- | { predicate: "refResolve"; element: string }
65
- | { predicate: "refAbsent"; element: string }
66
- | { predicate: "pageUrlContains"; needle: string }
67
- | { predicate: "pageTitleContains"; needle: string };
68
-
69
- export type FileInputFile = {
70
- name: string;
71
- mediaType?: string;
72
- contentBase64: string;
73
- };
74
-
75
57
  export type AgentControlApi = {
76
58
  snapshot: () => ShimResult<{ page: PageState; root: SemanticNode }>;
77
59
  click: (element: string) => ShimResult<Record<string, never>>;
@@ -91,15 +73,7 @@ export type AgentControlApi = {
91
73
  waitFor: (predicate: WaitPredicate) => ShimResult<{ holds: boolean }>;
92
74
  markAgentOrigin: () => void;
93
75
  isAgentOriginated: () => boolean;
94
- readEvents: (
95
- sinceSeq?: number,
96
- sinceEpoch?: string | null,
97
- ) => {
98
- epoch: string;
99
- events: Array<PageEvent>;
100
- nextSeq: number;
101
- dropped: number;
102
- };
76
+ readEvents: (sinceSeq?: number, sinceEpoch?: string | null) => PageEventBatch;
103
77
  };
104
78
 
105
79
  export type ShimWorld = {
@@ -141,7 +115,21 @@ function resolveRef(document: Document, element: string): Element | null {
141
115
  if (element === TRUNCATED_REF || element.length === 0) {
142
116
  return null;
143
117
  }
144
- return document.querySelector(`[${REF_ATTR}="${element}"]`);
118
+ return findByAttribute(document.querySelectorAll(`[${REF_ATTR}]`), REF_ATTR, element);
119
+ }
120
+
121
+ function findByAttribute(
122
+ candidates: NodeListOf<Element>,
123
+ attribute: string,
124
+ value: string,
125
+ ): Element | null {
126
+ // Attribute values are data and may contain characters that CSS normalizes.
127
+ for (const candidate of candidates) {
128
+ if (candidate.getAttribute(attribute) === value) {
129
+ return candidate;
130
+ }
131
+ }
132
+ return null;
145
133
  }
146
134
 
147
135
  function refPrefix(document: Document): string {
@@ -190,6 +178,15 @@ function isVisible(element: Element): boolean {
190
178
  return style.display !== "none" && style.visibility !== "hidden";
191
179
  }
192
180
 
181
+ /**
182
+ * Whether this element itself declares an editable host. `false` and invalid
183
+ * values do not: `contenteditable="false"` is how a page opts a region out.
184
+ */
185
+ function declaresEditable(element: Element): boolean {
186
+ const value = element.getAttribute("contenteditable")?.toLowerCase();
187
+ return value === "" || value === "true" || value === "plaintext-only";
188
+ }
189
+
193
190
  function implicitRole(element: Element): string | null {
194
191
  const tag = element.tagName;
195
192
  if (tag === "A") {
@@ -275,7 +272,7 @@ function implicitRole(element: Element): string | null {
275
272
  }
276
273
  return "textbox";
277
274
  }
278
- if (element.hasAttribute("contenteditable")) {
275
+ if (declaresEditable(element)) {
279
276
  return "textbox";
280
277
  }
281
278
  return null;
@@ -311,7 +308,7 @@ function associatedLabel(element: Element): string | undefined {
311
308
  const id = element.id;
312
309
  const document = element.ownerDocument;
313
310
  if (id) {
314
- const label = document.querySelector(`label[for="${id}"]`);
311
+ const label = findByAttribute(document.querySelectorAll("label[for]"), "for", id);
315
312
  const text = label?.textContent?.replaceAll(/\s+/g, " ").trim();
316
313
  if (text) {
317
314
  return text;
@@ -391,7 +388,7 @@ function valueOf(element: Element): string | undefined {
391
388
  const first = selected && selected.length > 0 ? selected[0] : undefined;
392
389
  return first?.textContent?.trim() ?? prop<string>(element, "value");
393
390
  }
394
- if (element.hasAttribute("contenteditable")) {
391
+ if (declaresEditable(element)) {
395
392
  return element.textContent ?? undefined;
396
393
  }
397
394
  return undefined;
@@ -466,6 +463,8 @@ function truncatedNode(): SemanticNode {
466
463
  return {
467
464
  elementRef: TRUNCATED_REF,
468
465
  role: TRUNCATED_ROLE,
466
+ name: null,
467
+ value: null,
469
468
  states: [],
470
469
  children: [],
471
470
  };
@@ -499,17 +498,12 @@ function walk(
499
498
  const node: SemanticNode = {
500
499
  elementRef: stamp(element, document),
501
500
  role: roleOf(element) ?? element.tagName.toLowerCase(),
501
+ name: accessibleName(element) ?? null,
502
+ // An empty value is reported the same way as no value.
503
+ value: valueOf(element) || null,
502
504
  states: statesOf(element, document),
503
505
  children: [],
504
506
  };
505
- const name = accessibleName(element);
506
- if (name) {
507
- node.name = name;
508
- }
509
- const value = valueOf(element);
510
- if (value !== undefined && value.length > 0) {
511
- node.value = value;
512
- }
513
507
  if (depth >= MAX_DEPTH) {
514
508
  if (element.children.length > 0) {
515
509
  budget.truncated = true;
@@ -629,24 +623,79 @@ function click(world: ShimWorld, element: string): ShimResult<Record<string, nev
629
623
  return ok();
630
624
  }
631
625
 
632
- function insertText(element: Element, text: string): void {
633
- if (element.tagName === "INPUT" || element.tagName === "TEXTAREA") {
634
- const current = prop<string>(element, "value") ?? "";
635
- const start = prop<number | null>(element, "selectionStart") ?? current.length;
626
+ /** Input types whose value is not typed text. Keys reach them; nothing is inserted. */
627
+ const UNTYPED_INPUT_TYPES = new Set([
628
+ "button",
629
+ "checkbox",
630
+ "hidden",
631
+ "image",
632
+ "radio",
633
+ "reset",
634
+ "submit",
635
+ ]);
636
+
637
+ /** Input types that hold one structured value, which typed text replaces. */
638
+ const REPLACED_INPUT_TYPES = new Set([
639
+ "color",
640
+ "date",
641
+ "datetime-local",
642
+ "month",
643
+ "range",
644
+ "time",
645
+ "week",
646
+ ]);
647
+
648
+ function inputType(element: Element): string {
649
+ return (prop<string>(element, "type") ?? element.getAttribute("type") ?? "text").toLowerCase();
650
+ }
651
+
652
+ /**
653
+ * Returns the function that types one character into the element, which
654
+ * answers whether the element took it.
655
+ */
656
+ function textInserter(element: Element): (text: string) => boolean {
657
+ if (element.tagName !== "INPUT" && element.tagName !== "TEXTAREA") {
658
+ return (text) => {
659
+ if (!declaresEditable(element) && prop<boolean>(element, "isContentEditable") !== true) {
660
+ return false;
661
+ }
662
+ element.textContent = `${element.textContent ?? ""}${text}`;
663
+ return true;
664
+ };
665
+ }
666
+ const type = element.tagName === "INPUT" ? inputType(element) : "textarea";
667
+ if (UNTYPED_INPUT_TYPES.has(type)) {
668
+ return () => false;
669
+ }
670
+ const read = () => prop<string>(element, "value") ?? "";
671
+ // Types without a selection (email, number, date, ...) report a null
672
+ // `selectionStart` and throw from `setSelectionRange`. They also blank a value
673
+ // that is not complete yet, so the typed text is held here and assigned whole.
674
+ let held = REPLACED_INPUT_TYPES.has(type) ? "" : read();
675
+ let seen = read();
676
+ return (text) => {
677
+ const current = read();
678
+ const start = prop<number | null>(element, "selectionStart");
679
+ if (typeof start !== "number") {
680
+ // The page rewrote the field between keys; continue from what it wrote.
681
+ if (current !== seen) {
682
+ held = current;
683
+ }
684
+ held += text;
685
+ Object.assign(element, { value: held });
686
+ seen = read();
687
+ return true;
688
+ }
636
689
  const end = prop<number | null>(element, "selectionEnd") ?? start;
637
- const next = `${current.slice(0, start)}${text}${current.slice(end)}`;
638
- Object.assign(element, { value: next });
690
+ Object.assign(element, { value: `${current.slice(0, start)}${text}${current.slice(end)}` });
639
691
  const cursor = start + text.length;
640
692
  prop<(this: Element, start: number, end: number) => void>(element, "setSelectionRange")?.call(
641
693
  element,
642
694
  cursor,
643
695
  cursor,
644
696
  );
645
- return;
646
- }
647
- if (element.hasAttribute("contenteditable") || prop<boolean>(element, "isContentEditable")) {
648
- element.textContent = `${element.textContent ?? ""}${text}`;
649
- }
697
+ return true;
698
+ };
650
699
  }
651
700
 
652
701
  function typeInto(
@@ -658,14 +707,23 @@ function typeInto(
658
707
  if (!node) {
659
708
  return unresolved(element);
660
709
  }
710
+ if (node.tagName === "INPUT" && inputType(node) === "file") {
711
+ return unsupported("type cannot set an input type=file; use set_file_input");
712
+ }
661
713
  focus(node);
714
+ const insert = textInserter(node);
715
+ let changed = false;
662
716
  for (const char of text) {
663
- dispatchKey(world, node, "keydown", char, []);
664
- insertText(node, char);
665
- dispatchInput(world, node, char);
717
+ // A cancelled keydown suppresses the text, as it does for a real key.
718
+ if (dispatchKey(world, node, "keydown", char, []) && insert(char)) {
719
+ changed = true;
720
+ dispatchInput(world, node, char);
721
+ }
666
722
  dispatchKey(world, node, "keyup", char, []);
667
723
  }
668
- node.dispatchEvent(new world.Event("change", { bubbles: true }));
724
+ if (changed) {
725
+ node.dispatchEvent(new world.Event("change", { bubbles: true }));
726
+ }
669
727
  return ok();
670
728
  }
671
729
 
@@ -735,8 +793,9 @@ function drag(
735
793
  dispatchMouse(world, target, "pointermove", to);
736
794
  dispatchMouse(world, target, "mousemove", to);
737
795
  // 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: () => "" };
796
+ const transfer: Pick<DataTransfer, "setData" | "getData"> = world.DataTransfer
797
+ ? new world.DataTransfer()
798
+ : { setData() {}, getData: () => "" };
740
799
  const dragEvent = (type: string, current: Element, point: { x: number; y: number }) => {
741
800
  const event = new world.MouseEvent(type, {
742
801
  bubbles: true,
@@ -802,8 +861,7 @@ function setFileInput(
802
861
  if (!node) {
803
862
  return unresolved(element);
804
863
  }
805
- const inputType = (prop<string>(node, "type") ?? node.getAttribute("type") ?? "").toLowerCase();
806
- if (node.tagName !== "INPUT" || inputType !== "file") {
864
+ if (node.tagName !== "INPUT" || inputType(node) !== "file") {
807
865
  return unsupported("set_file_input requires an input type=file");
808
866
  }
809
867
  if (!Array.isArray(files) || files.length === 0) {
@@ -813,9 +871,8 @@ function setFileInput(
813
871
  if (files.length > 1 && !multiple) {
814
872
  return unsupported("set_file_input received multiple files for an input without multiple");
815
873
  }
816
- const FileCtor = world.File ?? (typeof File === "function" ? File : undefined);
817
- const DataTransferCtor =
818
- world.DataTransfer ?? (typeof DataTransfer === "function" ? DataTransfer : undefined);
874
+ // Constructed in the page's own realm, or the input refuses the FileList.
875
+ const { File: FileCtor, DataTransfer: DataTransferCtor } = world;
819
876
  if (!FileCtor || !DataTransferCtor) {
820
877
  return unsupported("set_file_input needs File and DataTransfer in the page");
821
878
  }
@@ -874,6 +931,19 @@ function waitFor(world: ShimWorld, predicate: WaitPredicate): ShimResult<{ holds
874
931
  }
875
932
  }
876
933
 
934
+ /**
935
+ * Text for a value the page logged or threw. `String` throws for a
936
+ * prototype-less object or a throwing `toString`, and that must not escape into
937
+ * the page's own `console` call.
938
+ */
939
+ function describeValue(value: unknown): string {
940
+ try {
941
+ return String(value);
942
+ } catch {
943
+ return "[unprintable]";
944
+ }
945
+ }
946
+
877
947
  function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
878
948
  const existing = world[SHIM_GLOBAL];
879
949
  if (existing) {
@@ -905,7 +975,7 @@ function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
905
975
  seq: nextSeq,
906
976
  kind: "console",
907
977
  level,
908
- text: args.map(String).join(" "),
978
+ text: args.map(describeValue).join(" "),
909
979
  });
910
980
  original?.(...args);
911
981
  };
@@ -918,11 +988,13 @@ function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
918
988
 
919
989
  world.addEventListener("error", (event) => {
920
990
  const message =
921
- "message" in event && typeof event.message === "string" ? event.message : String(event);
991
+ "message" in event && typeof event.message === "string"
992
+ ? event.message
993
+ : describeValue(event);
922
994
  push({ seq: nextSeq, kind: "error", message });
923
995
  });
924
996
  world.addEventListener("unhandledrejection", (event) => {
925
- const reason = "reason" in event ? String(event.reason) : "unhandledrejection";
997
+ const reason = "reason" in event ? describeValue(event.reason) : "unhandledrejection";
926
998
  push({ seq: nextSeq, kind: "error", message: reason });
927
999
  });
928
1000
 
@@ -1,4 +1,4 @@
1
- // @generated by `effigy generate:bridge`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Allowed field names per protocol type, from the Rust structs.
3
3
  // A `record()` given one of these rejects unknown and missing keys —
4
4
  // contract 010's Boundary Validation Target.