@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
@@ -1,9 +1,15 @@
1
- import { isRecord } from "../core/record.ts";
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";
2
4
  import {
3
5
  COMMAND_VARIANT_FIELDS,
4
6
  COMMAND_VARIANT_FIELDS_DISCRIMINANTS,
5
7
  } from "./generated/variant-fields.ts";
6
8
  import {
9
+ COMMAND_ARGUMENT_SCHEMA_SHAPES,
10
+ COMMAND_AVAILABILITY_REASON_KINDS,
11
+ COMMAND_AVAILABILITY_STATES,
12
+ COMMAND_BINDING_SOURCE_KINDS,
7
13
  COMMAND_KEYMAP_DURABILITIES,
8
14
  COMMAND_KEYMAP_LOAD_ORIGINS,
9
15
  COMMAND_KEYMAP_LOAD_STATUSES,
@@ -14,6 +20,13 @@ import {
14
20
  COMMAND_KEYMAP_PROTOCOL_VERSION,
15
21
  COMMAND_KEYMAP_RECOVERY_CODES,
16
22
  COMMAND_KEYMAP_REJECTION_CODES,
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,
17
30
  } from "./generated/protocol.ts";
18
31
  import type {
19
32
  CommandCatalogueSnapshot,
@@ -29,6 +42,11 @@ import type {
29
42
  CommandKeymapSnapshot,
30
43
  } from "./generated/protocol.ts";
31
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;
32
50
 
33
51
  export class CommandProtocolValidationError extends Error {
34
52
  readonly code = "command_protocol_incompatible";
@@ -49,17 +67,8 @@ export function assertValidCommandCatalogue(
49
67
  version(record.protocolVersion, "$.protocolVersion");
50
68
  finiteInteger(record.registryGeneration, "$.registryGeneration");
51
69
  digest(record.registryDigest, "$.registryDigest");
52
- array(record.commands, "$.commands").forEach((command, index) => {
53
- const item = object(command, `$.commands[${index}]`);
54
- text(item.id, `$.commands[${index}].id`);
55
- text(item.label, `$.commands[${index}].label`);
56
- argumentSchema(item.arguments, `$.commands[${index}].arguments`);
57
- });
58
- array(record.presets, "$.presets").forEach((preset, index) => {
59
- const item = object(preset, `$.presets[${index}]`);
60
- text(item.id, `$.presets[${index}].id`);
61
- positiveInteger(item.version, `$.presets[${index}].version`);
62
- });
70
+ list(record.commands, "$.commands", discoveryRecord);
71
+ list(record.presets, "$.presets", presetRecord);
63
72
  }
64
73
 
65
74
  export function assertValidCommandAvailabilitySnapshot(
@@ -69,41 +78,16 @@ export function assertValidCommandAvailabilitySnapshot(
69
78
  finiteInteger(record.registryGeneration, "$.registryGeneration");
70
79
  finiteInteger(record.contextRevision, "$.contextRevision");
71
80
  let previous = "";
72
- array(record.records, "$.records").forEach((entry, index) => {
73
- const item = object(entry, `$.records[${index}]`);
74
- const commandId = text(item.commandId, `$.records[${index}].commandId`);
75
- if (index > 0 && commandId <= previous) {
76
- fail(`$.records[${index}].commandId`, "expected unique command-id order");
77
- }
78
- previous = commandId;
79
- const availability = object(item.availability, `$.records[${index}].availability`);
80
- member(
81
- availability.state,
82
- ["available", "unavailable", "hidden", "unsupported"],
83
- `$.records[${index}].availability.state`,
84
- );
85
- if ((availability.state === "available") !== (availability.reason === null)) {
86
- fail(
87
- `$.records[${index}].availability.reason`,
88
- "available requires no reason; every other state requires one",
89
- );
90
- }
91
- if (availability.reason !== null) {
92
- const reason = object(availability.reason, `$.records[${index}].availability.reason`);
93
- if (reason.detail !== null) {
94
- text(reason.detail, `$.records[${index}].availability.reason.detail`);
95
- }
96
- const code = object(reason.code, `$.records[${index}].availability.reason.code`);
97
- member(
98
- code.kind,
99
- ["contextNotAllowed", "missingCapability", "consumer"],
100
- `$.records[${index}].availability.reason.code.kind`,
101
- );
102
- if (code.kind === "consumer") {
103
- text(code.code, `$.records[${index}].availability.reason.code.code`);
104
- }
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");
105
86
  }
106
- });
87
+ previous = id;
88
+ availability(item.availability, `${path}.availability`);
89
+ };
90
+ list(record.records, "$.records", checkRecord, COMMAND_MAXIMUM_AVAILABILITY_RECORDS);
107
91
  }
108
92
 
109
93
  export function assertValidCommandCatalogueChangedEvent(
@@ -126,56 +110,31 @@ export function assertValidCommandKeymapChangedEvent(
126
110
  export function assertValidCommandKeymapSnapshot(
127
111
  value: unknown,
128
112
  ): asserts value is CommandKeymapSnapshot {
129
- const record = object(value, "$", COMMANDS_FIELDS.CommandKeymapSnapshot);
130
- version(record.protocolVersion, "$.protocolVersion");
131
- finiteInteger(record.registryGeneration, "$.registryGeneration");
132
- digest(record.registryDigest, "$.registryDigest");
133
- const state = object(record.state, "$.state");
134
- finiteInteger(state.revision, "$.state.revision");
135
- text(state.activePresetId, "$.state.activePresetId");
136
- array(state.overrides, "$.state.overrides").forEach((override, index) =>
137
- keymapOverride(override, `$.state.overrides[${index}]`),
138
- );
139
- positiveInteger(record.activePresetVersion, "$.activePresetVersion");
140
- array(record.bindings, "$.bindings").forEach((binding, index) =>
141
- effectiveBinding(binding, `$.bindings[${index}]`),
142
- );
143
- array(record.conflicts, "$.conflicts");
144
- const origin = object(record.origin, "$.origin");
145
- member(origin.kind, COMMAND_KEYMAP_LOAD_ORIGINS, "$.origin.kind");
146
- object(origin, "$.origin", variantKeys("CommandKeymapLoadOrigin", origin, "$.origin"));
147
- if (origin.kind === "migrated") {
148
- positiveInteger(origin.from, "$.origin.from");
149
- positiveInteger(origin.to, "$.origin.to");
150
- }
151
- array(record.diagnostics, "$.diagnostics");
113
+ keymapSnapshot(value, "$");
152
114
  }
153
115
 
154
116
  export function assertValidCommandKeymapPreview(
155
117
  value: unknown,
156
118
  ): asserts value is CommandKeymapPreview {
157
- baseRequest(value, "$", COMMANDS_FIELDS.CommandKeymapPreview);
158
- keymapPatch(object(value, "$").patch, "$.patch");
119
+ const record = object(value, "$", COMMANDS_FIELDS.CommandKeymapPreview);
120
+ baseRequest(record, "$");
121
+ keymapPatch(record.patch, "$.patch");
159
122
  }
160
123
 
161
124
  export function assertValidCommandKeymapCommit(
162
125
  value: unknown,
163
126
  ): asserts value is CommandKeymapCommit {
164
127
  const record = object(value, "$", COMMANDS_FIELDS.CommandKeymapCommit);
165
- text(record.requestId, "$.requestId");
166
- const evidence = object(record.evidence, "$.evidence");
167
- finiteInteger(evidence.registryGeneration, "$.evidence.registryGeneration");
168
- finiteInteger(evidence.keymapRevision, "$.evidence.keymapRevision");
169
- text(evidence.activePresetId, "$.evidence.activePresetId");
170
- positiveInteger(evidence.activePresetVersion, "$.evidence.activePresetVersion");
171
- digest(evidence.patchDigest, "$.evidence.patchDigest");
128
+ version(record.protocolVersion, "$.protocolVersion");
129
+ identifier(record.requestId, "$.requestId");
130
+ commitEvidence(record.evidence, "$.evidence");
172
131
  keymapPatch(record.patch, "$.patch");
173
132
  }
174
133
 
175
134
  export function assertValidCommandKeymapReset(value: unknown): asserts value is CommandKeymapReset {
176
135
  const record = object(value, "$", COMMANDS_FIELDS.CommandKeymapReset);
177
- text(record.requestId, "$.requestId");
178
- baseRequest(value, "$");
136
+ identifier(record.requestId, "$.requestId");
137
+ baseRequest(record, "$");
179
138
  }
180
139
 
181
140
  export function assertValidCommandKeymapPreviewResult(
@@ -184,13 +143,12 @@ export function assertValidCommandKeymapPreviewResult(
184
143
  const record = object(value, "$");
185
144
  member(record.status, COMMAND_KEYMAP_PREVIEW_STATUSES, "$.status");
186
145
  object(record, "$", variantKeys("CommandKeymapPreviewResult", record, "$"));
187
- assertValidCommandKeymapSnapshot(record.snapshot);
146
+ keymapSnapshot(record.snapshot, "$.snapshot");
188
147
  if (record.status === "accepted") {
189
- const evidence = object(record.evidence, "$.evidence");
190
- digest(evidence.patchDigest, "$.evidence.patchDigest");
148
+ commitEvidence(record.evidence, "$.evidence");
191
149
  } else if (record.status === "rejected") {
192
150
  rejection(record.rejection, "$.rejection");
193
- array(record.conflicts, "$.conflicts");
151
+ list(record.conflicts, "$.conflicts", conflict);
194
152
  }
195
153
  }
196
154
 
@@ -201,10 +159,9 @@ export function assertValidCommandKeymapLoadOutcome(
201
159
  member(record.status, COMMAND_KEYMAP_LOAD_STATUSES, "$.status");
202
160
  object(record, "$", variantKeys("CommandKeymapLoadOutcome", record, "$"));
203
161
  if (record.status === "loaded") {
204
- assertValidCommandKeymapSnapshot(record.snapshot);
162
+ keymapSnapshot(record.snapshot, "$.snapshot");
205
163
  } else if (record.status === "recovery") {
206
- const recovery = object(record.recovery, "$.recovery");
207
- member(recovery.code, COMMAND_KEYMAP_RECOVERY_CODES, "$.recovery.code");
164
+ recoveryState(record.recovery, "$.recovery");
208
165
  } else {
209
166
  text(record.detail, "$.detail");
210
167
  }
@@ -216,39 +173,126 @@ export function assertValidCommandKeymapMutationResult(
216
173
  const record = object(value, "$");
217
174
  member(record.status, COMMAND_KEYMAP_MUTATION_STATUSES, "$.status");
218
175
  object(record, "$", variantKeys("CommandKeymapMutationResult", record, "$"));
219
- assertValidCommandKeymapSnapshot(record.snapshot);
176
+ keymapSnapshot(record.snapshot, "$.snapshot");
220
177
  if (record.status === "applied") {
221
- const receipt = object(record.receipt, "$.receipt");
222
- member(receipt.outcome, COMMAND_KEYMAP_MUTATION_OUTCOMES, "$.receipt.outcome");
223
- member(receipt.durability, COMMAND_KEYMAP_DURABILITIES, "$.receipt.durability");
178
+ mutationReceipt(record.receipt, "$.receipt");
224
179
  } else if (record.status === "rejected") {
225
180
  rejection(record.rejection, "$.rejection");
226
- array(record.conflicts, "$.conflicts");
181
+ list(record.conflicts, "$.conflicts", conflict);
182
+ }
183
+ }
184
+
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`);
227
247
  }
228
248
  }
229
249
 
230
- function baseRequest(value: unknown, path: string, allowed?: ReadonlyArray<string>): void {
231
- const record = object(value, path, allowed);
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`);
258
+ finiteInteger(record.registryGeneration, `${path}.registryGeneration`);
259
+ finiteInteger(record.keymapRevision, `${path}.keymapRevision`);
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);
232
266
  finiteInteger(record.registryGeneration, `${path}.registryGeneration`);
233
267
  finiteInteger(record.keymapRevision, `${path}.keymapRevision`);
234
- text(record.activePresetId, `${path}.activePresetId`);
235
- positiveInteger(record.activePresetVersion, `${path}.activePresetVersion`);
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`);
236
288
  }
237
289
 
238
290
  function keymapPatch(value: unknown, path: string): void {
239
291
  const record = object(value, path, COMMANDS_FIELDS.CommandKeymapPatch);
240
- if (record.activePresetId !== null && typeof record.activePresetId !== "string") {
241
- fail(`${path}.activePresetId`, "expected string or null");
242
- }
243
- if (typeof record.clearOverrides !== "boolean") {
244
- fail(`${path}.clearOverrides`, "expected boolean");
245
- }
246
- array(record.removeBindingIds, `${path}.removeBindingIds`).forEach((id, index) =>
247
- text(id, `${path}.removeBindingIds[${index}]`),
248
- );
249
- array(record.upsertOverrides, `${path}.upsertOverrides`).forEach((override, index) =>
250
- keymapOverride(override, `${path}.upsertOverrides[${index}]`),
251
- );
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);
252
296
  }
253
297
 
254
298
  function keymapOverride(value: unknown, path: string): void {
@@ -256,52 +300,160 @@ function keymapOverride(value: unknown, path: string): void {
256
300
  member(record.kind, COMMAND_KEYMAP_OVERRIDE_KINDS, `${path}.kind`);
257
301
  object(record, path, variantKeys("CommandKeymapOverride", record, path));
258
302
  if (record.kind === "add") {
259
- bindingDefinition(record.binding, `${path}.binding`);
260
- } else {
261
- text(record.bindingId, `${path}.bindingId`);
262
- if (record.kind === "replace") {
263
- bindingDefinition(record.replacement, `${path}.replacement`, false);
264
- }
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`);
265
320
  }
266
321
  }
267
322
 
268
- function bindingDefinition(value: unknown, path: string, requireId = true): void {
269
- const record = object(value, path);
270
- if (requireId) {
271
- text(record.id, `${path}.id`);
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");
272
333
  }
273
- text(record.commandId, `${path}.commandId`);
274
- text(record.contextId, `${path}.contextId`);
275
- const trigger = object(record.trigger, `${path}.trigger`);
276
- text(trigger.code, `${path}.trigger.code`);
277
334
  }
278
335
 
279
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 {
280
347
  const record = object(value, path);
281
- text(record.id, `${path}.id`);
282
- const source = object(record.source, `${path}.source`);
283
- member(source.kind, ["preset", "replacement", "addedOverride"], `${path}.source.kind`);
284
- object(source, `${path}.source`, variantKeys("CommandBindingSource", source, `${path}.source`));
285
- bindingDefinition(
286
- {
287
- ...record,
288
- commandId: object(record.invocation, `${path}.invocation`).commandId,
289
- },
290
- path,
291
- );
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);
292
385
  }
293
386
 
294
387
  function argumentSchema(value: unknown, path: string): void {
295
388
  const record = object(value, path);
296
- member(record.shape, ["none", "object"], `${path}.shape`);
389
+ member(record.shape, COMMAND_ARGUMENT_SCHEMA_SHAPES, `${path}.shape`);
297
390
  object(record, path, variantKeys("CommandArgumentSchema", record, path));
298
391
  if (record.shape === "object") {
299
- array(record.fields, `${path}.fields`);
392
+ list(record.fields, `${path}.fields`, argumentField);
300
393
  }
301
394
  }
302
395
 
303
- 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 {
304
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 {
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);
305
457
  member(record.code, COMMAND_KEYMAP_REJECTION_CODES, `${path}.code`);
306
458
  text(record.detail, `${path}.detail`);
307
459
  }
@@ -318,66 +470,142 @@ function digest(value: unknown, path: string): void {
318
470
  }
319
471
  }
320
472
 
321
- 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 {
322
496
  if (typeof value !== "string" || value.length === 0) {
323
497
  fail(path, "expected non-empty string");
324
498
  }
325
- return value;
326
499
  }
327
500
 
328
- function positiveInteger(value: unknown, path: string): void {
329
- finiteInteger(value, path);
330
- if (value < 1) {
331
- 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
+ }
509
+ }
510
+
511
+ function diagnosticText(value: unknown, path: string): void {
512
+ if (
513
+ typeof value !== "string" ||
514
+ value.length === 0 ||
515
+ exceedsBytes(value, COMMAND_MAXIMUM_DIAGNOSTIC_BYTES)
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");
332
536
  }
333
537
  }
334
538
 
335
- function finiteInteger(value: unknown, path: string): asserts value is number {
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 {
336
554
  if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
337
555
  fail(path, "expected non-negative safe integer");
338
556
  }
339
557
  }
340
558
 
341
- /**
342
- * Rejects a non-object, an unknown key, and a missing key.
343
- *
344
- * `allowed` comes from the generated field map, so the keys accepted are the
345
- * Rust struct's and nothing else — contract 010's Boundary Validation Target.
346
- * Passing no list keeps shape-only behaviour for the tagged unions, whose
347
- * allowed keys depend on their discriminant and so are not one flat set.
348
- */
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
+
349
581
  function object(
350
582
  value: unknown,
351
583
  path: string,
352
584
  allowed?: ReadonlyArray<string>,
353
585
  ): Record<string, unknown> {
354
- if (!isRecord(value)) {
355
- fail(path, "expected object");
356
- }
357
- const record = value;
358
- if (allowed === undefined) {
359
- return record;
360
- }
361
-
362
- const permitted = new Set(allowed);
363
- for (const key of Object.keys(record)) {
364
- if (!permitted.has(key)) {
365
- fail(`${path}.${key}`, "unknown field");
366
- }
367
- }
368
- for (const key of allowed) {
369
- if (!(key in record)) {
370
- fail(`${path}.${key}`, "missing field");
371
- }
372
- }
373
- 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;
374
591
  }
375
592
 
376
- function array(value: unknown, path: string): Array<unknown> {
593
+ function list(
594
+ value: unknown,
595
+ path: string,
596
+ check: (value: unknown, path: string) => void,
597
+ maximum = Number.POSITIVE_INFINITY,
598
+ ): void {
377
599
  if (!Array.isArray(value)) {
378
600
  fail(path, "expected array");
379
601
  }
380
- return value;
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}]`);
608
+ }
381
609
  }
382
610
 
383
611
  function member<const T extends ReadonlyArray<string>>(
@@ -394,24 +622,16 @@ function fail(path: string, detail: string): never {
394
622
  throw new CommandProtocolValidationError(path, detail);
395
623
  }
396
624
 
397
- /**
398
- * Allowed keys for one tagged-union variant, from the generated map, with the
399
- * discriminant's name read from the map too. This domain tags on `kind`,
400
- * `shape` and `status`.
401
- *
402
- * A missing entry means the generator failed to read the union — every caller
403
- * runs `member()` over the discriminant above this call.
404
- */
405
625
  function variantKeys(
406
626
  type: string,
407
627
  value: Record<string, unknown>,
408
628
  path: string,
409
629
  ): ReadonlyArray<string> {
410
- const discriminant = value[COMMAND_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
411
- const keys =
412
- typeof discriminant === "string" ? COMMAND_VARIANT_FIELDS[type]?.[discriminant] : undefined;
413
- if (keys === undefined) {
414
- fail(path, `no generated fields for ${type}.${String(discriminant)}`);
415
- }
416
- return keys;
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
+ );
417
637
  }