@inflatable-cookie/longhorn 0.5.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 (167) 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/inject.ts +4 -1
  6. package/src/agent-control/selection.ts +42 -25
  7. package/src/agent-control/shim.ts +428 -198
  8. package/src/bridge/direct.ts +1 -1
  9. package/src/bridge/generated/fields.ts +4 -3
  10. package/src/bridge/generated/protocol.ts +62 -7
  11. package/src/bridge/generated/variant-fields.ts +1 -1
  12. package/src/bridge/job.ts +49 -12
  13. package/src/bridge/loopback.ts +7 -25
  14. package/src/bridge/operation.ts +13 -43
  15. package/src/bridge/router.ts +10 -23
  16. package/src/bridge/runtime/connection.ts +78 -109
  17. package/src/bridge/runtime/policy.ts +8 -28
  18. package/src/bridge/runtime/retry.ts +6 -18
  19. package/src/bridge/session.ts +4 -14
  20. package/src/bridge/stream/connection.ts +24 -55
  21. package/src/bridge/stream/source.ts +2 -6
  22. package/src/bridge/stream/tracker.ts +12 -12
  23. package/src/bridge/supervision.ts +104 -97
  24. package/src/bridge/validation/base.ts +21 -63
  25. package/src/bridge/validation/jobs.ts +11 -25
  26. package/src/bridge/validation/negotiation.ts +28 -43
  27. package/src/bridge/validation/operations.ts +29 -65
  28. package/src/bridge/validation/streams.ts +3 -10
  29. package/src/commands/client.ts +37 -28
  30. package/src/commands/controller.ts +119 -96
  31. package/src/commands/generated/fields.ts +7 -6
  32. package/src/commands/generated/protocol.ts +28 -1
  33. package/src/commands/generated/variant-fields.ts +1 -1
  34. package/src/commands/keyboard.ts +94 -100
  35. package/src/commands/physical-code.ts +11 -0
  36. package/src/commands/ports.ts +8 -13
  37. package/src/commands/projectors.ts +100 -75
  38. package/src/commands/validation.ts +434 -241
  39. package/src/config/client.ts +9 -22
  40. package/src/config/generated/base.ts +22 -6
  41. package/src/config/generated/fields.ts +4 -3
  42. package/src/config/generated/label-template.ts +1 -1
  43. package/src/config/generated/labels.ts +1 -1
  44. package/src/config/generated/protocol.ts +1 -1
  45. package/src/config/generated/restore.ts +35 -12
  46. package/src/config/generated/variant-fields.ts +1 -1
  47. package/src/config/names.ts +6 -12
  48. package/src/config/validation/commands.ts +11 -25
  49. package/src/config/validation/outcomes.ts +163 -70
  50. package/src/config/validation/primitives.ts +35 -80
  51. package/src/config/validation/projection.ts +100 -128
  52. package/src/core/connection.ts +187 -83
  53. package/src/core/controller-lifecycle.ts +144 -0
  54. package/src/core/generated/identifiers.ts +4 -0
  55. package/src/core/identifiers.ts +40 -0
  56. package/src/core/index.ts +10 -1
  57. package/src/core/json-value.ts +58 -0
  58. package/src/core/listeners.ts +35 -0
  59. package/src/core/record.ts +80 -0
  60. package/src/core/transport.ts +9 -11
  61. package/src/core/wire-copy.ts +7 -0
  62. package/src/history/client.ts +11 -13
  63. package/src/history/controller.ts +106 -108
  64. package/src/history/direct.ts +1 -3
  65. package/src/history/generated/fields.ts +4 -3
  66. package/src/history/generated/protocol.ts +8 -5
  67. package/src/history/generated/variant-fields.ts +1 -1
  68. package/src/history/ports.ts +7 -4
  69. package/src/history/serialized.ts +9 -13
  70. package/src/history/validation.ts +69 -75
  71. package/src/history-tree/client.ts +89 -11
  72. package/src/history-tree/controller.ts +380 -96
  73. package/src/history-tree/direct.ts +3 -1
  74. package/src/history-tree/generated/fields.ts +5 -3
  75. package/src/history-tree/generated/protocol.ts +43 -7
  76. package/src/history-tree/generated/variant-fields.ts +7 -1
  77. package/src/history-tree/ports.ts +12 -5
  78. package/src/history-tree/serialized.ts +39 -11
  79. package/src/history-tree/validation.ts +286 -78
  80. package/src/layout/generated/protocol.ts +4 -3
  81. package/src/layout/generated/variant-fields.ts +47 -0
  82. package/src/layout/index.ts +1 -1
  83. package/src/layout/ratio.ts +10 -9
  84. package/src/layout/validation.ts +98 -32
  85. package/src/layout/visibility.ts +1 -2
  86. package/src/licence/client.ts +48 -7
  87. package/src/licence/controller.ts +127 -70
  88. package/src/licence/direct.ts +3 -1
  89. package/src/licence/generated/fields.ts +4 -3
  90. package/src/licence/generated/key-conformance.json +28 -0
  91. package/src/licence/generated/protocol.ts +9 -5
  92. package/src/licence/generated/variant-fields.ts +1 -1
  93. package/src/licence/key.ts +42 -18
  94. package/src/licence/ports.ts +10 -3
  95. package/src/licence/serialized.ts +32 -9
  96. package/src/licence/validation.ts +134 -43
  97. package/src/native-content/client.ts +207 -234
  98. package/src/native-content/generated/fields.ts +6 -6
  99. package/src/native-content/generated/protocol.ts +16 -5
  100. package/src/native-content/generated/variant-fields.ts +1 -1
  101. package/src/native-content/ports.ts +11 -16
  102. package/src/native-content/serialized.ts +12 -19
  103. package/src/native-content/validation/base.ts +57 -80
  104. package/src/native-content/validation/messages.ts +24 -35
  105. package/src/native-content/validation/state.ts +31 -33
  106. package/src/notifications/client.ts +85 -51
  107. package/src/notifications/controller.ts +400 -145
  108. package/src/notifications/direct.ts +3 -1
  109. package/src/notifications/generated/fields.ts +4 -3
  110. package/src/notifications/generated/labels.ts +1 -1
  111. package/src/notifications/generated/protocol.ts +5 -1
  112. package/src/notifications/generated/variant-fields.ts +1 -1
  113. package/src/notifications/ports.ts +9 -4
  114. package/src/notifications/serialized.ts +11 -9
  115. package/src/notifications/tone.ts +1 -1
  116. package/src/notifications/validation.ts +165 -71
  117. package/src/operation/bridge.ts +187 -60
  118. package/src/operation/client.ts +48 -62
  119. package/src/operation/controller.ts +67 -97
  120. package/src/operation/direct.ts +1 -3
  121. package/src/operation/generated/fields.ts +4 -3
  122. package/src/operation/generated/labels.ts +1 -1
  123. package/src/operation/generated/protocol.ts +5 -1
  124. package/src/operation/generated/variant-fields.ts +1 -1
  125. package/src/operation/ports.ts +12 -10
  126. package/src/operation/serialized.ts +8 -9
  127. package/src/operation/validation.ts +189 -104
  128. package/src/settings/client.ts +11 -44
  129. package/src/settings/connection.ts +35 -53
  130. package/src/settings/generated/fields.ts +5 -4
  131. package/src/settings/generated/protocol.ts +14 -5
  132. package/src/settings/generated/variant-fields.ts +1 -1
  133. package/src/settings/names.ts +2 -4
  134. package/src/settings/registry.ts +23 -25
  135. package/src/settings/validation/authority.ts +21 -19
  136. package/src/settings/validation/command.ts +22 -41
  137. package/src/settings/validation/error.ts +2 -8
  138. package/src/settings/validation/primitives.ts +46 -97
  139. package/src/settings/validation/projection.ts +8 -24
  140. package/src/settings/validation/registry.ts +33 -79
  141. package/src/surface-transfer/client.ts +6 -18
  142. package/src/surface-transfer/generated/fields.ts +3 -3
  143. package/src/surface-transfer/generated/protocol.ts +3 -3
  144. package/src/surface-transfer/generated/variant-fields.ts +1 -1
  145. package/src/surface-transfer/validation.ts +91 -86
  146. package/src/surfaces/client.ts +33 -55
  147. package/src/surfaces/generated/fields.ts +3 -3
  148. package/src/surfaces/generated/protocol.ts +1 -1
  149. package/src/surfaces/generated/variant-fields.ts +1 -1
  150. package/src/surfaces/validation.ts +227 -99
  151. package/src/transfer/client.ts +26 -60
  152. package/src/transfer/connection.ts +29 -51
  153. package/src/transfer/generated/fields.ts +7 -6
  154. package/src/transfer/generated/protocol.ts +13 -5
  155. package/src/transfer/generated/variant-fields.ts +1 -1
  156. package/src/transfer/index.ts +8 -1
  157. package/src/transfer/payload.ts +20 -35
  158. package/src/transfer/validation.ts +169 -119
  159. package/src/update/client.ts +86 -9
  160. package/src/update/controller.ts +264 -116
  161. package/src/update/direct.ts +3 -1
  162. package/src/update/generated/fields.ts +5 -4
  163. package/src/update/generated/protocol.ts +25 -9
  164. package/src/update/generated/variant-fields.ts +4 -2
  165. package/src/update/ports.ts +14 -6
  166. package/src/update/serialized.ts +36 -10
  167. package/src/update/validation.ts +118 -48
@@ -1,8 +1,15 @@
1
+ import { isOpaqueId } from "../core/identifiers.ts";
2
+ import { isJsonValue, isUnicodeText } from "../core/json-value.ts";
3
+ import { checkWireFields, readWireRecord, unsigned32, wireVariantFields } from "../core/record.ts";
1
4
  import {
2
5
  COMMAND_VARIANT_FIELDS,
3
6
  COMMAND_VARIANT_FIELDS_DISCRIMINANTS,
4
7
  } from "./generated/variant-fields.ts";
5
8
  import {
9
+ COMMAND_ARGUMENT_SCHEMA_SHAPES,
10
+ COMMAND_AVAILABILITY_REASON_KINDS,
11
+ COMMAND_AVAILABILITY_STATES,
12
+ COMMAND_BINDING_SOURCE_KINDS,
6
13
  COMMAND_KEYMAP_DURABILITIES,
7
14
  COMMAND_KEYMAP_LOAD_ORIGINS,
8
15
  COMMAND_KEYMAP_LOAD_STATUSES,
@@ -13,24 +20,41 @@ import {
13
20
  COMMAND_KEYMAP_PROTOCOL_VERSION,
14
21
  COMMAND_KEYMAP_RECOVERY_CODES,
15
22
  COMMAND_KEYMAP_REJECTION_CODES,
16
- type CommandCatalogueSnapshot,
17
- type CommandAvailabilitySnapshot,
18
- type CommandCatalogueChangedEvent,
19
- type CommandKeymapChangedEvent,
20
- type CommandKeymapCommit,
21
- type CommandKeymapLoadOutcome,
22
- type CommandKeymapMutationResult,
23
- type CommandKeymapPreview,
24
- type CommandKeymapPreviewResult,
25
- type CommandKeymapReset,
26
- type CommandKeymapSnapshot,
23
+ COMMAND_MAXIMUM_ARGUMENT_DEPTH,
24
+ COMMAND_MAXIMUM_AVAILABILITY_RECORDS,
25
+ COMMAND_MAXIMUM_DIAGNOSTIC_BYTES,
26
+ COMMAND_MAXIMUM_KEYWORD_BYTES,
27
+ COMMAND_PLATFORM_SCOPES,
28
+ COMMAND_PLATFORMS,
29
+ COMMAND_TEXT_INPUT_POLICIES,
30
+ } from "./generated/protocol.ts";
31
+ import type {
32
+ CommandCatalogueSnapshot,
33
+ CommandAvailabilitySnapshot,
34
+ CommandCatalogueChangedEvent,
35
+ CommandKeymapChangedEvent,
36
+ CommandKeymapCommit,
37
+ CommandKeymapLoadOutcome,
38
+ CommandKeymapMutationResult,
39
+ CommandKeymapPreview,
40
+ CommandKeymapPreviewResult,
41
+ CommandKeymapReset,
42
+ CommandKeymapSnapshot,
27
43
  } from "./generated/protocol.ts";
28
44
  import { COMMANDS_FIELDS } from "./generated/fields.ts";
45
+ import { isPhysicalCode } from "./physical-code.ts";
46
+
47
+ // Rust holds command integers as `i64`. `i64::MAX` reaches JavaScript rounded up
48
+ // to 2^63, so that endpoint is admitted.
49
+ const INTEGER_ENDPOINT = 2 ** 63;
29
50
 
30
51
  export class CommandProtocolValidationError extends Error {
31
52
  readonly code = "command_protocol_incompatible";
32
53
 
33
- constructor(readonly path: string, detail: string) {
54
+ constructor(
55
+ readonly path: string,
56
+ detail: string,
57
+ ) {
34
58
  super(`${path}: ${detail}`);
35
59
  this.name = "CommandProtocolValidationError";
36
60
  }
@@ -43,17 +67,8 @@ export function assertValidCommandCatalogue(
43
67
  version(record.protocolVersion, "$.protocolVersion");
44
68
  finiteInteger(record.registryGeneration, "$.registryGeneration");
45
69
  digest(record.registryDigest, "$.registryDigest");
46
- array(record.commands, "$.commands").forEach((command, index) => {
47
- const item = object(command, `$.commands[${index}]`);
48
- text(item.id, `$.commands[${index}].id`);
49
- text(item.label, `$.commands[${index}].label`);
50
- argumentSchema(item.arguments, `$.commands[${index}].arguments`);
51
- });
52
- array(record.presets, "$.presets").forEach((preset, index) => {
53
- const item = object(preset, `$.presets[${index}]`);
54
- text(item.id, `$.presets[${index}].id`);
55
- positiveInteger(item.version, `$.presets[${index}].version`);
56
- });
70
+ list(record.commands, "$.commands", discoveryRecord);
71
+ list(record.presets, "$.presets", presetRecord);
57
72
  }
58
73
 
59
74
  export function assertValidCommandAvailabilitySnapshot(
@@ -63,62 +78,16 @@ export function assertValidCommandAvailabilitySnapshot(
63
78
  finiteInteger(record.registryGeneration, "$.registryGeneration");
64
79
  finiteInteger(record.contextRevision, "$.contextRevision");
65
80
  let previous = "";
66
- array(record.records, "$.records").forEach((entry, index) => {
67
- const item = object(entry, `$.records[${index}]`);
68
- const commandId = text(item.commandId, `$.records[${index}].commandId`);
69
- if (index > 0 && commandId <= previous) {
70
- fail(
71
- `$.records[${index}].commandId`,
72
- "expected unique command-id order",
73
- );
74
- }
75
- previous = commandId;
76
- const availability = object(
77
- item.availability,
78
- `$.records[${index}].availability`,
79
- );
80
- member(
81
- availability.state,
82
- ["available", "unavailable", "hidden", "unsupported"],
83
- `$.records[${index}].availability.state`,
84
- );
85
- if (
86
- (availability.state === "available") !==
87
- (availability.reason === null)
88
- ) {
89
- fail(
90
- `$.records[${index}].availability.reason`,
91
- "available requires no reason; every other state requires one",
92
- );
93
- }
94
- if (availability.reason !== null) {
95
- const reason = object(
96
- availability.reason,
97
- `$.records[${index}].availability.reason`,
98
- );
99
- if (reason.detail !== null) {
100
- text(
101
- reason.detail,
102
- `$.records[${index}].availability.reason.detail`,
103
- );
104
- }
105
- const code = object(
106
- reason.code,
107
- `$.records[${index}].availability.reason.code`,
108
- );
109
- member(
110
- code.kind,
111
- ["contextNotAllowed", "missingCapability", "consumer"],
112
- `$.records[${index}].availability.reason.code.kind`,
113
- );
114
- if (code.kind === "consumer") {
115
- text(
116
- code.code,
117
- `$.records[${index}].availability.reason.code.code`,
118
- );
119
- }
81
+ const checkRecord = (entry: unknown, path: string): void => {
82
+ const item = object(entry, path, COMMANDS_FIELDS.CommandAvailabilityRecord);
83
+ const id = commandId(item.commandId, `${path}.commandId`);
84
+ if (previous !== "" && id <= previous) {
85
+ fail(`${path}.commandId`, "expected unique command-id order");
120
86
  }
121
- });
87
+ previous = id;
88
+ availability(item.availability, `${path}.availability`);
89
+ };
90
+ list(record.records, "$.records", checkRecord, COMMAND_MAXIMUM_AVAILABILITY_RECORDS);
122
91
  }
123
92
 
124
93
  export function assertValidCommandCatalogueChangedEvent(
@@ -141,61 +110,31 @@ export function assertValidCommandKeymapChangedEvent(
141
110
  export function assertValidCommandKeymapSnapshot(
142
111
  value: unknown,
143
112
  ): asserts value is CommandKeymapSnapshot {
144
- const record = object(value, "$", COMMANDS_FIELDS.CommandKeymapSnapshot);
145
- version(record.protocolVersion, "$.protocolVersion");
146
- finiteInteger(record.registryGeneration, "$.registryGeneration");
147
- digest(record.registryDigest, "$.registryDigest");
148
- const state = object(record.state, "$.state");
149
- finiteInteger(state.revision, "$.state.revision");
150
- text(state.activePresetId, "$.state.activePresetId");
151
- array(state.overrides, "$.state.overrides").forEach((override, index) =>
152
- keymapOverride(override, `$.state.overrides[${index}]`),
153
- );
154
- positiveInteger(record.activePresetVersion, "$.activePresetVersion");
155
- array(record.bindings, "$.bindings").forEach((binding, index) =>
156
- effectiveBinding(binding, `$.bindings[${index}]`),
157
- );
158
- array(record.conflicts, "$.conflicts");
159
- const origin = object(record.origin, "$.origin");
160
- member(origin.kind, COMMAND_KEYMAP_LOAD_ORIGINS, "$.origin.kind");
161
- object(origin, "$.origin", variantKeys("CommandKeymapLoadOrigin", origin, "$.origin"));
162
- if (origin.kind === "migrated") {
163
- positiveInteger(origin.from, "$.origin.from");
164
- positiveInteger(origin.to, "$.origin.to");
165
- }
166
- array(record.diagnostics, "$.diagnostics");
113
+ keymapSnapshot(value, "$");
167
114
  }
168
115
 
169
116
  export function assertValidCommandKeymapPreview(
170
117
  value: unknown,
171
118
  ): asserts value is CommandKeymapPreview {
172
- baseRequest(value, "$", COMMANDS_FIELDS.CommandKeymapPreview);
173
- keymapPatch(object(value, "$").patch, "$.patch");
119
+ const record = object(value, "$", COMMANDS_FIELDS.CommandKeymapPreview);
120
+ baseRequest(record, "$");
121
+ keymapPatch(record.patch, "$.patch");
174
122
  }
175
123
 
176
124
  export function assertValidCommandKeymapCommit(
177
125
  value: unknown,
178
126
  ): asserts value is CommandKeymapCommit {
179
127
  const record = object(value, "$", COMMANDS_FIELDS.CommandKeymapCommit);
180
- text(record.requestId, "$.requestId");
181
- const evidence = object(record.evidence, "$.evidence");
182
- finiteInteger(evidence.registryGeneration, "$.evidence.registryGeneration");
183
- finiteInteger(evidence.keymapRevision, "$.evidence.keymapRevision");
184
- text(evidence.activePresetId, "$.evidence.activePresetId");
185
- positiveInteger(
186
- evidence.activePresetVersion,
187
- "$.evidence.activePresetVersion",
188
- );
189
- digest(evidence.patchDigest, "$.evidence.patchDigest");
128
+ version(record.protocolVersion, "$.protocolVersion");
129
+ identifier(record.requestId, "$.requestId");
130
+ commitEvidence(record.evidence, "$.evidence");
190
131
  keymapPatch(record.patch, "$.patch");
191
132
  }
192
133
 
193
- export function assertValidCommandKeymapReset(
194
- value: unknown,
195
- ): asserts value is CommandKeymapReset {
134
+ export function assertValidCommandKeymapReset(value: unknown): asserts value is CommandKeymapReset {
196
135
  const record = object(value, "$", COMMANDS_FIELDS.CommandKeymapReset);
197
- text(record.requestId, "$.requestId");
198
- baseRequest(value, "$");
136
+ identifier(record.requestId, "$.requestId");
137
+ baseRequest(record, "$");
199
138
  }
200
139
 
201
140
  export function assertValidCommandKeymapPreviewResult(
@@ -204,13 +143,12 @@ export function assertValidCommandKeymapPreviewResult(
204
143
  const record = object(value, "$");
205
144
  member(record.status, COMMAND_KEYMAP_PREVIEW_STATUSES, "$.status");
206
145
  object(record, "$", variantKeys("CommandKeymapPreviewResult", record, "$"));
207
- assertValidCommandKeymapSnapshot(record.snapshot);
146
+ keymapSnapshot(record.snapshot, "$.snapshot");
208
147
  if (record.status === "accepted") {
209
- const evidence = object(record.evidence, "$.evidence");
210
- digest(evidence.patchDigest, "$.evidence.patchDigest");
148
+ commitEvidence(record.evidence, "$.evidence");
211
149
  } else if (record.status === "rejected") {
212
150
  rejection(record.rejection, "$.rejection");
213
- array(record.conflicts, "$.conflicts");
151
+ list(record.conflicts, "$.conflicts", conflict);
214
152
  }
215
153
  }
216
154
 
@@ -221,10 +159,9 @@ export function assertValidCommandKeymapLoadOutcome(
221
159
  member(record.status, COMMAND_KEYMAP_LOAD_STATUSES, "$.status");
222
160
  object(record, "$", variantKeys("CommandKeymapLoadOutcome", record, "$"));
223
161
  if (record.status === "loaded") {
224
- assertValidCommandKeymapSnapshot(record.snapshot);
162
+ keymapSnapshot(record.snapshot, "$.snapshot");
225
163
  } else if (record.status === "recovery") {
226
- const recovery = object(record.recovery, "$.recovery");
227
- member(recovery.code, COMMAND_KEYMAP_RECOVERY_CODES, "$.recovery.code");
164
+ recoveryState(record.recovery, "$.recovery");
228
165
  } else {
229
166
  text(record.detail, "$.detail");
230
167
  }
@@ -236,55 +173,126 @@ export function assertValidCommandKeymapMutationResult(
236
173
  const record = object(value, "$");
237
174
  member(record.status, COMMAND_KEYMAP_MUTATION_STATUSES, "$.status");
238
175
  object(record, "$", variantKeys("CommandKeymapMutationResult", record, "$"));
239
- assertValidCommandKeymapSnapshot(record.snapshot);
176
+ keymapSnapshot(record.snapshot, "$.snapshot");
240
177
  if (record.status === "applied") {
241
- const receipt = object(record.receipt, "$.receipt");
242
- member(
243
- receipt.outcome,
244
- COMMAND_KEYMAP_MUTATION_OUTCOMES,
245
- "$.receipt.outcome",
246
- );
247
- member(
248
- receipt.durability,
249
- COMMAND_KEYMAP_DURABILITIES,
250
- "$.receipt.durability",
251
- );
178
+ mutationReceipt(record.receipt, "$.receipt");
252
179
  } else if (record.status === "rejected") {
253
180
  rejection(record.rejection, "$.rejection");
254
- array(record.conflicts, "$.conflicts");
181
+ list(record.conflicts, "$.conflicts", conflict);
255
182
  }
256
183
  }
257
184
 
258
- function baseRequest(
259
- value: unknown,
260
- path: string,
261
- allowed?: readonly string[],
262
- ): void {
263
- const record = object(value, path, allowed);
185
+ function discoveryRecord(value: unknown, path: string): void {
186
+ const record = object(value, path, COMMANDS_FIELDS.CommandDiscoveryRecord);
187
+ commandId(record.id, `${path}.id`);
188
+ text(record.label, `${path}.label`);
189
+ nullable(record.description, `${path}.description`, string);
190
+ list(record.categoryPath, `${path}.categoryPath`, identifier);
191
+ list(record.keywords, `${path}.keywords`, keyword);
192
+ nullable(record.icon, `${path}.icon`, string);
193
+ flags(record.visibility, `${path}.visibility`, COMMANDS_FIELDS.CommandVisibility);
194
+ member(record.textInputPolicy, COMMAND_TEXT_INPUT_POLICIES, `${path}.textInputPolicy`);
195
+ argumentSchema(record.arguments, `${path}.arguments`);
196
+ }
197
+
198
+ function presetRecord(value: unknown, path: string): void {
199
+ const record = object(value, path, COMMANDS_FIELDS.CommandKeymapPresetRecord);
200
+ identifier(record.id, `${path}.id`);
201
+ schemaVersion(record.version, `${path}.version`);
202
+ }
203
+
204
+ function availability(value: unknown, path: string): void {
205
+ const record = object(value, path, COMMANDS_FIELDS.CommandAvailability);
206
+ member(record.state, COMMAND_AVAILABILITY_STATES, `${path}.state`);
207
+ if ((record.state === "available") !== (record.reason === null)) {
208
+ fail(`${path}.reason`, "available requires no reason; every other state requires one");
209
+ }
210
+ if (record.reason === null) {
211
+ return;
212
+ }
213
+ const reason = object(record.reason, `${path}.reason`, COMMANDS_FIELDS.CommandAvailabilityReason);
214
+ nullable(reason.detail, `${path}.reason.detail`, diagnosticText);
215
+ const codePath = `${path}.reason.code`;
216
+ const code = object(reason.code, codePath);
217
+ member(code.kind, COMMAND_AVAILABILITY_REASON_KINDS, `${codePath}.kind`);
218
+ object(code, codePath, variantKeys("CommandAvailabilityReasonCode", code, codePath));
219
+ if (code.kind === "consumer") {
220
+ identifier(code.code, `${codePath}.code`);
221
+ }
222
+ }
223
+
224
+ function keymapSnapshot(value: unknown, path: string): void {
225
+ const record = object(value, path, COMMANDS_FIELDS.CommandKeymapSnapshot);
226
+ version(record.protocolVersion, `${path}.protocolVersion`);
227
+ finiteInteger(record.registryGeneration, `${path}.registryGeneration`);
228
+ digest(record.registryDigest, `${path}.registryDigest`);
229
+ const state = object(record.state, `${path}.state`, COMMANDS_FIELDS.CommandKeymapState);
230
+ finiteInteger(state.revision, `${path}.state.revision`);
231
+ identifier(state.activePresetId, `${path}.state.activePresetId`);
232
+ list(state.overrides, `${path}.state.overrides`, keymapOverride);
233
+ schemaVersion(record.activePresetVersion, `${path}.activePresetVersion`);
234
+ list(record.bindings, `${path}.bindings`, effectiveBinding);
235
+ list(record.conflicts, `${path}.conflicts`, conflict);
236
+ loadOrigin(record.origin, `${path}.origin`);
237
+ list(record.diagnostics, `${path}.diagnostics`, keymapDiagnostic);
238
+ }
239
+
240
+ function loadOrigin(value: unknown, path: string): void {
241
+ const record = object(value, path);
242
+ member(record.kind, COMMAND_KEYMAP_LOAD_ORIGINS, `${path}.kind`);
243
+ object(record, path, variantKeys("CommandKeymapLoadOrigin", record, path));
244
+ if (record.kind === "migrated") {
245
+ schemaVersion(record.from, `${path}.from`);
246
+ schemaVersion(record.to, `${path}.to`);
247
+ }
248
+ }
249
+
250
+ function keymapDiagnostic(value: unknown, path: string): void {
251
+ const record = object(value, path, COMMANDS_FIELDS.CommandKeymapDiagnostic);
252
+ string(record.code, `${path}.code`);
253
+ string(record.detail, `${path}.detail`);
254
+ }
255
+
256
+ function baseRequest(record: Record<string, unknown>, path: string): void {
257
+ version(record.protocolVersion, `${path}.protocolVersion`);
264
258
  finiteInteger(record.registryGeneration, `${path}.registryGeneration`);
265
259
  finiteInteger(record.keymapRevision, `${path}.keymapRevision`);
266
- text(record.activePresetId, `${path}.activePresetId`);
267
- positiveInteger(record.activePresetVersion, `${path}.activePresetVersion`);
260
+ identifier(record.activePresetId, `${path}.activePresetId`);
261
+ schemaVersion(record.activePresetVersion, `${path}.activePresetVersion`);
262
+ }
263
+
264
+ function commitEvidence(value: unknown, path: string): void {
265
+ const record = object(value, path, COMMANDS_FIELDS.CommandKeymapCommitEvidence);
266
+ finiteInteger(record.registryGeneration, `${path}.registryGeneration`);
267
+ finiteInteger(record.keymapRevision, `${path}.keymapRevision`);
268
+ identifier(record.activePresetId, `${path}.activePresetId`);
269
+ schemaVersion(record.activePresetVersion, `${path}.activePresetVersion`);
270
+ digest(record.patchDigest, `${path}.patchDigest`);
271
+ }
272
+
273
+ function mutationReceipt(value: unknown, path: string): void {
274
+ const record = object(value, path, COMMANDS_FIELDS.CommandKeymapMutationReceipt);
275
+ identifier(record.requestId, `${path}.requestId`);
276
+ finiteInteger(record.previousRevision, `${path}.previousRevision`);
277
+ finiteInteger(record.committedRevision, `${path}.committedRevision`);
278
+ member(record.outcome, COMMAND_KEYMAP_MUTATION_OUTCOMES, `${path}.outcome`);
279
+ member(record.durability, COMMAND_KEYMAP_DURABILITIES, `${path}.durability`);
280
+ nullable(record.patchDigest, `${path}.patchDigest`, digest);
281
+ }
282
+
283
+ function recoveryState(value: unknown, path: string): void {
284
+ const record = object(value, path, COMMANDS_FIELDS.CommandKeymapRecovery);
285
+ member(record.code, COMMAND_KEYMAP_RECOVERY_CODES, `${path}.code`);
286
+ string(record.detail, `${path}.detail`);
287
+ boolean(record.sourcePreserved, `${path}.sourcePreserved`);
268
288
  }
269
289
 
270
290
  function keymapPatch(value: unknown, path: string): void {
271
291
  const record = object(value, path, COMMANDS_FIELDS.CommandKeymapPatch);
272
- if (
273
- record.activePresetId !== null &&
274
- typeof record.activePresetId !== "string"
275
- ) {
276
- fail(`${path}.activePresetId`, "expected string or null");
277
- }
278
- if (typeof record.clearOverrides !== "boolean") {
279
- fail(`${path}.clearOverrides`, "expected boolean");
280
- }
281
- array(record.removeBindingIds, `${path}.removeBindingIds`).forEach(
282
- (id, index) => text(id, `${path}.removeBindingIds[${index}]`),
283
- );
284
- array(record.upsertOverrides, `${path}.upsertOverrides`).forEach(
285
- (override, index) =>
286
- keymapOverride(override, `${path}.upsertOverrides[${index}]`),
287
- );
292
+ nullable(record.activePresetId, `${path}.activePresetId`, identifier);
293
+ boolean(record.clearOverrides, `${path}.clearOverrides`);
294
+ list(record.removeBindingIds, `${path}.removeBindingIds`, identifier);
295
+ list(record.upsertOverrides, `${path}.upsertOverrides`, keymapOverride);
288
296
  }
289
297
 
290
298
  function keymapOverride(value: unknown, path: string): void {
@@ -292,52 +300,160 @@ function keymapOverride(value: unknown, path: string): void {
292
300
  member(record.kind, COMMAND_KEYMAP_OVERRIDE_KINDS, `${path}.kind`);
293
301
  object(record, path, variantKeys("CommandKeymapOverride", record, path));
294
302
  if (record.kind === "add") {
295
- bindingDefinition(record.binding, `${path}.binding`);
296
- } else {
297
- text(record.bindingId, `${path}.bindingId`);
298
- if (record.kind === "replace") {
299
- bindingDefinition(record.replacement, `${path}.replacement`, false);
300
- }
303
+ const binding = object(
304
+ record.binding,
305
+ `${path}.binding`,
306
+ COMMANDS_FIELDS.CommandBindingDefinition,
307
+ );
308
+ identifier(binding.id, `${path}.binding.id`);
309
+ bindingPayload(binding, `${path}.binding`);
310
+ return;
311
+ }
312
+ identifier(record.bindingId, `${path}.bindingId`);
313
+ if (record.kind === "replace") {
314
+ const replacement = object(
315
+ record.replacement,
316
+ `${path}.replacement`,
317
+ COMMANDS_FIELDS.CommandBindingReplacement,
318
+ );
319
+ bindingPayload(replacement, `${path}.replacement`);
301
320
  }
302
321
  }
303
322
 
304
- function bindingDefinition(
305
- value: unknown,
306
- path: string,
307
- requireId = true,
308
- ): void {
309
- const record = object(value, path);
310
- if (requireId) text(record.id, `${path}.id`);
311
- text(record.commandId, `${path}.commandId`);
312
- text(record.contextId, `${path}.contextId`);
313
- const trigger = object(record.trigger, `${path}.trigger`);
314
- text(trigger.code, `${path}.trigger.code`);
323
+ /** The members a binding definition and its replacement share. */
324
+ function bindingPayload(record: Record<string, unknown>, path: string): void {
325
+ member(record.platform, COMMAND_PLATFORM_SCOPES, `${path}.platform`);
326
+ keyTrigger(record.trigger, `${path}.trigger`);
327
+ identifier(record.contextId, `${path}.contextId`);
328
+ commandId(record.commandId, `${path}.commandId`);
329
+ // The sealed registry validates these against the command's schema. Here they
330
+ // only have to reach it unchanged.
331
+ if (!isJsonValue(record.arguments, COMMAND_MAXIMUM_ARGUMENT_DEPTH)) {
332
+ fail(`${path}.arguments`, "expected a JSON value");
333
+ }
315
334
  }
316
335
 
317
336
  function effectiveBinding(value: unknown, path: string): void {
337
+ const record = object(value, path, COMMANDS_FIELDS.CommandEffectiveBinding);
338
+ identifier(record.id, `${path}.id`);
339
+ bindingSource(record.source, `${path}.source`);
340
+ member(record.platform, COMMAND_PLATFORM_SCOPES, `${path}.platform`);
341
+ keyTrigger(record.trigger, `${path}.trigger`);
342
+ identifier(record.contextId, `${path}.contextId`);
343
+ invocation(record.invocation, `${path}.invocation`);
344
+ }
345
+
346
+ function bindingSource(value: unknown, path: string): void {
318
347
  const record = object(value, path);
319
- text(record.id, `${path}.id`);
320
- const source = object(record.source, `${path}.source`);
321
- member(source.kind, ["preset", "replacement", "addedOverride"], `${path}.source.kind`);
322
- object(source, `${path}.source`, variantKeys("CommandBindingSource", source, `${path}.source`));
323
- bindingDefinition(
324
- {
325
- ...record,
326
- commandId: object(record.invocation, `${path}.invocation`).commandId,
327
- },
328
- path,
329
- );
348
+ member(record.kind, COMMAND_BINDING_SOURCE_KINDS, `${path}.kind`);
349
+ object(record, path, variantKeys("CommandBindingSource", record, path));
350
+ if (record.kind !== "addedOverride") {
351
+ identifier(record.presetId, `${path}.presetId`);
352
+ schemaVersion(record.presetVersion, `${path}.presetVersion`);
353
+ }
354
+ }
355
+
356
+ function keyTrigger(value: unknown, path: string): void {
357
+ const record = object(value, path, COMMANDS_FIELDS.CommandKeyTrigger);
358
+ physicalCode(record.code, `${path}.code`);
359
+ flags(record.modifiers, `${path}.modifiers`, COMMANDS_FIELDS.CommandTriggerModifiers);
360
+ }
361
+
362
+ function keyChord(value: unknown, path: string): void {
363
+ const record = object(value, path, COMMANDS_FIELDS.CommandKeyChord);
364
+ physicalCode(record.code, `${path}.code`);
365
+ flags(record.modifiers, `${path}.modifiers`, COMMANDS_FIELDS.CommandModifiers);
366
+ }
367
+
368
+ function invocation(value: unknown, path: string): void {
369
+ const record = object(value, path, COMMANDS_FIELDS.CommandInvocation);
370
+ commandId(record.commandId, `${path}.commandId`);
371
+ const values = object(record.arguments, `${path}.arguments`);
372
+ for (const [field, argument] of Object.entries(values)) {
373
+ identifier(field, `${path}.arguments.${field}`);
374
+ argumentValue(argument, `${path}.arguments.${field}`);
375
+ }
376
+ }
377
+
378
+ function conflict(value: unknown, path: string): void {
379
+ const record = object(value, path, COMMANDS_FIELDS.CommandKeymapConflict);
380
+ member(record.platform, COMMAND_PLATFORMS, `${path}.platform`);
381
+ keyChord(record.chord, `${path}.chord`);
382
+ identifier(record.contextId, `${path}.contextId`);
383
+ list(record.bindingIds, `${path}.bindingIds`, identifier);
384
+ list(record.invocations, `${path}.invocations`, invocation);
330
385
  }
331
386
 
332
387
  function argumentSchema(value: unknown, path: string): void {
333
388
  const record = object(value, path);
334
- member(record.shape, ["none", "object"], `${path}.shape`);
389
+ member(record.shape, COMMAND_ARGUMENT_SCHEMA_SHAPES, `${path}.shape`);
335
390
  object(record, path, variantKeys("CommandArgumentSchema", record, path));
336
- if (record.shape === "object") array(record.fields, `${path}.fields`);
391
+ if (record.shape === "object") {
392
+ list(record.fields, `${path}.fields`, argumentField);
393
+ }
337
394
  }
338
395
 
339
- function rejection(value: unknown, path: string): void {
396
+ function argumentField(value: unknown, path: string): void {
397
+ const record = object(value, path, COMMANDS_FIELDS.CommandArgumentField);
398
+ identifier(record.id, `${path}.id`);
399
+ boolean(record.required, `${path}.required`);
400
+ nullable(record.default, `${path}.default`, argumentValue);
401
+ argumentKind(record.kind, `${path}.kind`);
402
+ }
403
+
404
+ // Both switches fail closed: a variant Rust adds is refused until it has a case.
405
+ function argumentKind(value: unknown, path: string): void {
406
+ const record = object(value, path);
407
+ object(record, path, variantKeys("CommandArgumentKind", record, path));
408
+ switch (record.kind) {
409
+ case "boolean":
410
+ return;
411
+ case "number":
412
+ nullable(record.minimum, `${path}.minimum`, finiteNumber);
413
+ nullable(record.maximum, `${path}.maximum`, finiteNumber);
414
+ return;
415
+ case "integer":
416
+ nullable(record.minimum, `${path}.minimum`, integer);
417
+ nullable(record.maximum, `${path}.maximum`, integer);
418
+ return;
419
+ case "string":
420
+ finiteInteger(record.maximumBytes, `${path}.maximumBytes`);
421
+ return;
422
+ case "enum":
423
+ list(record.values, `${path}.values`, identifier);
424
+ return;
425
+ default:
426
+ fail(`${path}.kind`, "unknown argument kind");
427
+ }
428
+ }
429
+
430
+ function argumentValue(value: unknown, path: string): void {
340
431
  const record = object(value, path);
432
+ object(record, path, variantKeys("CommandArgumentValue", record, path));
433
+ const valuePath = `${path}.value`;
434
+ switch (record.kind) {
435
+ case "boolean":
436
+ boolean(record.value, valuePath);
437
+ return;
438
+ case "number":
439
+ finiteNumber(record.value, valuePath);
440
+ return;
441
+ case "integer":
442
+ integer(record.value, valuePath);
443
+ return;
444
+ case "string":
445
+ string(record.value, valuePath);
446
+ return;
447
+ case "enum":
448
+ identifier(record.value, valuePath);
449
+ return;
450
+ default:
451
+ fail(`${path}.kind`, "unknown argument value kind");
452
+ }
453
+ }
454
+
455
+ function rejection(value: unknown, path: string): void {
456
+ const record = object(value, path, COMMANDS_FIELDS.CommandKeymapRejection);
341
457
  member(record.code, COMMAND_KEYMAP_REJECTION_CODES, `${path}.code`);
342
458
  text(record.detail, `${path}.detail`);
343
459
  }
@@ -354,63 +470,145 @@ function digest(value: unknown, path: string): void {
354
470
  }
355
471
  }
356
472
 
357
- function text(value: unknown, path: string): string {
473
+ function identifier(value: unknown, path: string): void {
474
+ if (!isOpaqueId(value)) {
475
+ fail(path, "expected bounded lowercase opaque id");
476
+ }
477
+ }
478
+
479
+ // Command ids are the one identifier injected ports may supply outside the
480
+ // opaque grammar: contract 006 has the scorer fold them for search.
481
+ function commandId(value: unknown, path: string): string {
482
+ text(value, path);
483
+ if (!isUnicodeText(value)) {
484
+ fail(path, "expected Unicode text");
485
+ }
486
+ return value;
487
+ }
488
+
489
+ function string(value: unknown, path: string): asserts value is string {
490
+ if (typeof value !== "string") {
491
+ fail(path, "expected string");
492
+ }
493
+ }
494
+
495
+ function text(value: unknown, path: string): asserts value is string {
358
496
  if (typeof value !== "string" || value.length === 0) {
359
497
  fail(path, "expected non-empty string");
360
498
  }
361
- return value;
362
499
  }
363
500
 
364
- function positiveInteger(value: unknown, path: string): void {
365
- finiteInteger(value, path);
366
- if ((value as number) < 1) fail(path, "expected positive integer");
501
+ function keyword(value: unknown, path: string): void {
502
+ if (
503
+ typeof value !== "string" ||
504
+ exceedsBytes(value, COMMAND_MAXIMUM_KEYWORD_BYTES) ||
505
+ /^\p{White_Space}*$/u.test(value)
506
+ ) {
507
+ fail(path, "expected bounded non-blank keyword");
508
+ }
367
509
  }
368
510
 
369
- function finiteInteger(value: unknown, path: string): void {
511
+ function diagnosticText(value: unknown, path: string): void {
370
512
  if (
371
- typeof value !== "number" ||
372
- !Number.isSafeInteger(value) ||
373
- value < 0
513
+ typeof value !== "string" ||
514
+ value.length === 0 ||
515
+ exceedsBytes(value, COMMAND_MAXIMUM_DIAGNOSTIC_BYTES)
374
516
  ) {
517
+ fail(path, "expected bounded non-empty diagnostic");
518
+ }
519
+ }
520
+
521
+ // A string never encodes to fewer bytes than its length, so an oversized one is
522
+ // refused without encoding it.
523
+ function exceedsBytes(value: string, maximum: number): boolean {
524
+ return value.length > maximum || new TextEncoder().encode(value).length > maximum;
525
+ }
526
+
527
+ function physicalCode(value: unknown, path: string): void {
528
+ if (!isPhysicalCode(value)) {
529
+ fail(path, "expected physical key code");
530
+ }
531
+ }
532
+
533
+ function boolean(value: unknown, path: string): void {
534
+ if (typeof value !== "boolean") {
535
+ fail(path, "expected boolean");
536
+ }
537
+ }
538
+
539
+ /** A record whose every generated member is a boolean. */
540
+ function flags(value: unknown, path: string, fields: ReadonlyArray<string>): void {
541
+ const record = object(value, path, fields);
542
+ for (const field of fields) {
543
+ boolean(record[field], `${path}.${field}`);
544
+ }
545
+ }
546
+
547
+ function schemaVersion(value: unknown, path: string): void {
548
+ if (!unsigned32(value) || value === 0) {
549
+ fail(path, "expected positive schema version");
550
+ }
551
+ }
552
+
553
+ function finiteInteger(value: unknown, path: string): void {
554
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
375
555
  fail(path, "expected non-negative safe integer");
376
556
  }
377
557
  }
378
558
 
379
- /**
380
- * Rejects a non-object, an unknown key, and a missing key.
381
- *
382
- * `allowed` comes from the generated field map, so the keys accepted are the
383
- * Rust struct's and nothing else — contract 010's Boundary Validation Target.
384
- * Passing no list keeps shape-only behaviour for the tagged unions, whose
385
- * allowed keys depend on their discriminant and so are not one flat set.
386
- */
559
+ function integer(value: unknown, path: string): void {
560
+ if (typeof value !== "number" || !Number.isInteger(value) || Math.abs(value) > INTEGER_ENDPOINT) {
561
+ fail(path, "expected 64-bit integer");
562
+ }
563
+ }
564
+
565
+ function finiteNumber(value: unknown, path: string): void {
566
+ if (!Number.isFinite(value)) {
567
+ fail(path, "expected finite number");
568
+ }
569
+ }
570
+
571
+ function nullable(
572
+ value: unknown,
573
+ path: string,
574
+ check: (value: unknown, path: string) => void,
575
+ ): void {
576
+ if (value !== null) {
577
+ check(value, path);
578
+ }
579
+ }
580
+
387
581
  function object(
388
582
  value: unknown,
389
583
  path: string,
390
- allowed?: readonly string[],
584
+ allowed?: ReadonlyArray<string>,
391
585
  ): Record<string, unknown> {
392
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
393
- fail(path, "expected object");
394
- }
395
- const record = value as Record<string, unknown>;
396
- if (allowed === undefined) return record;
586
+ const result = readWireRecord(value, () => fail(path, "expected object"));
587
+ checkWireFields(result, allowed, (code, key) =>
588
+ fail(`${path}.${key}`, code === "unknown_field" ? "unknown field" : "missing field"),
589
+ );
590
+ return result;
591
+ }
397
592
 
398
- const permitted = new Set(allowed);
399
- for (const key of Object.keys(record)) {
400
- if (!permitted.has(key)) fail(`${path}.${key}`, "unknown field");
593
+ function list(
594
+ value: unknown,
595
+ path: string,
596
+ check: (value: unknown, path: string) => void,
597
+ maximum = Number.POSITIVE_INFINITY,
598
+ ): void {
599
+ if (!Array.isArray(value)) {
600
+ fail(path, "expected array");
401
601
  }
402
- for (const key of allowed) {
403
- if (!(key in record)) fail(`${path}.${key}`, "missing field");
602
+ if (value.length > maximum) {
603
+ fail(path, `expected at most ${maximum} entries`);
604
+ }
605
+ // `entries` visits a hole as `undefined`, which every check refuses.
606
+ for (const [index, entry] of value.entries()) {
607
+ check(entry, `${path}[${index}]`);
404
608
  }
405
- return record;
406
- }
407
-
408
- function array(value: unknown, path: string): unknown[] {
409
- if (!Array.isArray(value)) fail(path, "expected array");
410
- return value;
411
609
  }
412
610
 
413
- function member<const T extends readonly string[]>(
611
+ function member<const T extends ReadonlyArray<string>>(
414
612
  value: unknown,
415
613
  choices: T,
416
614
  path: string,
@@ -424,21 +622,16 @@ function fail(path: string, detail: string): never {
424
622
  throw new CommandProtocolValidationError(path, detail);
425
623
  }
426
624
 
427
- /**
428
- * Allowed keys for one tagged-union variant, from the generated map, with the
429
- * discriminant's name read from the map too. This domain tags on `kind`,
430
- * `shape` and `status`.
431
- *
432
- * A missing entry means the generator failed to read the union — every caller
433
- * runs `member()` over the discriminant above this call.
434
- */
435
625
  function variantKeys(
436
626
  type: string,
437
627
  value: Record<string, unknown>,
438
628
  path: string,
439
- ): readonly string[] {
440
- const discriminant = value[COMMAND_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
441
- const keys = COMMAND_VARIANT_FIELDS[type]?.[discriminant as string];
442
- if (keys === undefined) fail(path, `no generated fields for ${type}.${String(discriminant)}`);
443
- return keys;
629
+ ): ReadonlyArray<string> {
630
+ return wireVariantFields(
631
+ COMMAND_VARIANT_FIELDS,
632
+ COMMAND_VARIANT_FIELDS_DISCRIMINANTS,
633
+ type,
634
+ value,
635
+ (discriminant) => fail(path, `no generated fields for ${type}.${String(discriminant)}`),
636
+ );
444
637
  }