@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,5 +1,8 @@
1
1
  import { SETTINGS_FIELDS } from "../generated/fields.ts";
2
- import { SETTINGS_MUTATION_TIMINGS } from "../generated/protocol.ts";
2
+ import {
3
+ SETTINGS_HARD_MAXIMUM_OPAQUE_VALUE_BYTES,
4
+ SETTINGS_MUTATION_TIMINGS,
5
+ } from "../generated/protocol.ts";
3
6
  import type { SettingsRegistrySnapshot, SettingsLimits } from "../generated/protocol.ts";
4
7
  import { incompatible } from "./error.ts";
5
8
  import {
@@ -8,14 +11,13 @@ import {
8
11
  boundedArray,
9
12
  definition,
10
13
  identity,
11
- integer,
14
+ order,
12
15
  known,
13
16
  positive,
14
17
  protocolVersion,
15
18
  record,
16
19
  text,
17
20
  unsigned,
18
- HARD_MAXIMUM_OPAQUE_VALUE_BYTES,
19
21
  } from "./primitives.ts";
20
22
 
21
23
  const DIGEST = /^[a-f0-9]{64}$/;
@@ -55,18 +57,18 @@ export function assertValidSettingsRegistrySnapshot(
55
57
  const module = record(value, SETTINGS_FIELDS.SettingsModuleDefinition);
56
58
  identity(module.id);
57
59
  text(module.label, limits.maximumLabelBytes);
58
- integer(module.order);
60
+ order(module.order);
59
61
  });
60
62
  sections.forEach((value) => {
61
63
  const section = record(value, SETTINGS_FIELDS.SettingsSectionDefinition);
62
64
  identity(section.id);
63
65
  identity(section.moduleId);
64
66
  text(section.label, limits.maximumLabelBytes);
65
- integer(section.order);
67
+ order(section.order);
66
68
  });
67
- renderers.forEach(definition);
68
- scopes.forEach(definition);
69
- capabilities.forEach(definition);
69
+ renderers.forEach((value) => definition(value, SETTINGS_FIELDS.SettingsRendererDefinition));
70
+ scopes.forEach((value) => definition(value, SETTINGS_FIELDS.SettingsScopeDefinition));
71
+ capabilities.forEach((value) => definition(value, SETTINGS_FIELDS.SettingsCapabilityDefinition));
70
72
  applyUnits.forEach((value) => {
71
73
  const unit = record(value, SETTINGS_FIELDS.SettingsApplyUnitDefinition);
72
74
  identity(unit.id);
@@ -85,7 +87,7 @@ function validatePage(value: unknown, limits: SettingsLimits): void {
85
87
  identity(page.sectionId);
86
88
  identity(page.rendererId);
87
89
  text(page.label, limits.maximumLabelBytes);
88
- integer(page.order);
90
+ order(page.order);
89
91
  boundedArray(page.keywords, limits.maximumKeywordsPerPage, "invalid_registry").forEach(
90
92
  (keyword) => text(keyword, limits.maximumKeywordBytes),
91
93
  );
@@ -95,7 +97,7 @@ function validatePage(value: unknown, limits: SettingsLimits): void {
95
97
  if (anchor.label !== null) {
96
98
  text(anchor.label, limits.maximumLabelBytes);
97
99
  }
98
- integer(anchor.order);
100
+ order(anchor.order);
99
101
  });
100
102
  arraysOfIdentities(page.requiredCapabilities, page.readableScopeIds, page.writableApplyUnitIds);
101
103
  const features = record(page.features, SETTINGS_FIELDS.SettingsPageFeatures);
@@ -109,7 +111,7 @@ function validatePage(value: unknown, limits: SettingsLimits): void {
109
111
  function settingsLimits(value: unknown): SettingsLimits {
110
112
  const limits = record(value, SETTINGS_FIELDS.SettingsLimits);
111
113
  assertPositiveLimits(limits);
112
- if (limits.maximumOpaqueValueBytes > HARD_MAXIMUM_OPAQUE_VALUE_BYTES) {
114
+ if (limits.maximumOpaqueValueBytes > SETTINGS_HARD_MAXIMUM_OPAQUE_VALUE_BYTES) {
113
115
  incompatible("invalid_registry", limits.maximumOpaqueValueBytes);
114
116
  }
115
117
  return limits;
@@ -1,4 +1,4 @@
1
- // @generated by `effigy generate:surface-transfer`; 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.
@@ -1,4 +1,4 @@
1
- // @generated by `effigy generate:surface-transfer`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Rust serde types are the wire authority.
3
3
 
4
4
  import type { SurfaceDocument, SurfaceId, SurfaceMutationRejectionCode, SurfaceRevision } from "@inflatable-cookie/longhorn/surfaces";
@@ -8,11 +8,11 @@ export const SURFACE_TRANSFER_TARGET_KINDS = ["existing","provisioned"] as const
8
8
  export const SURFACE_TRANSFER_ABORT_DOMAINS = ["transfer","surface_transfer"] as const;
9
9
  export const SURFACE_SESSION_RESPONSE_STATUSES = ["started","aborted"] as const;
10
10
  export const SURFACE_TRANSFER_RESPONSE_STATUSES = ["committed","aborted"] as const;
11
- export const SURFACE_TRANSFER_ERROR_CODES = ["invalid_binding_snapshot","unknown_host_binding","stale_host_binding","surface_load_failed","surface_unavailable","unknown_surface","source_changed","unsupported_source","unsupported_target","cross_document","stale_surface_revision","ineligible_target","target_changed","invalid_insertion_position","empty_display_disabled","no_empty_display_target","ambiguous_empty_display_target","provision_failed","provision_receipt_mismatch","surface_mutation_rejected","publication_failed","host_reconciliation_required","transfer_rejected"] as const;
11
+ export const SURFACE_TRANSFER_ERROR_CODES = ["invalid_binding_snapshot","unknown_host_binding","stale_host_binding","surface_load_failed","surface_unavailable","unknown_surface","source_changed","unsupported_source","unsupported_target","cross_document","stale_surface_revision","ineligible_target","target_changed","invalid_insertion_position","empty_display_disabled","no_empty_display_target","provision_failed","provision_receipt_mismatch","surface_mutation_rejected","publication_failed","host_reconciliation_required","transfer_rejected"] as const;
12
12
 
13
13
  export type DisplayId = string;
14
14
 
15
- export type SurfaceTransferErrorCode = "invalid_binding_snapshot" | "unknown_host_binding" | "stale_host_binding" | "surface_load_failed" | "surface_unavailable" | "unknown_surface" | "source_changed" | "unsupported_source" | "unsupported_target" | "cross_document" | "stale_surface_revision" | "ineligible_target" | "target_changed" | "invalid_insertion_position" | "empty_display_disabled" | "no_empty_display_target" | "ambiguous_empty_display_target" | "provision_failed" | "provision_receipt_mismatch" | "surface_mutation_rejected" | "publication_failed" | "host_reconciliation_required" | "transfer_rejected";
15
+ export type SurfaceTransferErrorCode = "invalid_binding_snapshot" | "unknown_host_binding" | "stale_host_binding" | "surface_load_failed" | "surface_unavailable" | "unknown_surface" | "source_changed" | "unsupported_source" | "unsupported_target" | "cross_document" | "stale_surface_revision" | "ineligible_target" | "target_changed" | "invalid_insertion_position" | "empty_display_disabled" | "no_empty_display_target" | "provision_failed" | "provision_receipt_mismatch" | "surface_mutation_rejected" | "publication_failed" | "host_reconciliation_required" | "transfer_rejected";
16
16
 
17
17
  export type SurfaceSessionStartRequest = { protocol_version: TransferProtocolVersion, request_id: TransferRequestId, surface_id: SurfaceId, };
18
18
 
@@ -1,4 +1,4 @@
1
- // @generated by `effigy generate:surface-transfer`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Allowed field names per tagged-union variant, from the Rust enums.
3
3
  // Keyed by type, then by discriminant. The discriminant itself is in
4
4
  // each list, because `exact()` compares every key.
@@ -1,4 +1,10 @@
1
- import { isRecord } from "../core/record.ts";
1
+ import { isOpaqueId } from "../core/identifiers.ts";
2
+ import {
3
+ checkWireFields,
4
+ readWireRecord,
5
+ unsignedInteger,
6
+ wireVariantFields,
7
+ } from "../core/record.ts";
2
8
  import {
3
9
  SURFACE_TRANSFER_VARIANT_FIELDS,
4
10
  SURFACE_TRANSFER_VARIANT_FIELDS_DISCRIMINANTS,
@@ -19,10 +25,16 @@ import type {
19
25
  import { SURFACE_TRANSFER_FIELDS } from "./generated/fields.ts";
20
26
  import {
21
27
  TRANSFER_ERROR_CODES,
22
- assertValidTransferTargetBinding,
23
28
  assertTransferProtocolVersion,
29
+ assertValidTransferCommittedTarget,
30
+ assertValidTransferScreenPoint,
31
+ assertValidTransferSessionStarted,
32
+ isTransferSessionId,
24
33
  } from "@inflatable-cookie/longhorn/transfer";
25
- import { assertValidSurfaceMutationRejectionCode } from "@inflatable-cookie/longhorn/surfaces";
34
+ import {
35
+ assertValidSurfaceDocument,
36
+ assertValidSurfaceMutationRejectionCode,
37
+ } from "@inflatable-cookie/longhorn/surfaces";
26
38
 
27
39
  export type SurfaceTransferProtocolValidationCode =
28
40
  | "unknown_target"
@@ -56,8 +68,15 @@ export function assertValidSurfaceTransferTarget(
56
68
  assertKnown(target.kind, SURFACE_TRANSFER_TARGET_KINDS, "unknown_target");
57
69
  record(target, variantKeys("SurfaceTransferTarget", target));
58
70
  if (target.kind === "existing") {
59
- assertValidTransferTargetBinding(record(record(target.target).binding));
71
+ assertValidTransferCommittedTarget(target.target);
72
+ return;
60
73
  }
74
+ assertValidTransferScreenPoint(target.drop_point);
75
+ assertFields(record(target.provisioning, SURFACE_TRANSFER_FIELDS.SurfaceProvisioningCompletion), {
76
+ window_id: isOpaqueId,
77
+ host_binding_id: isOpaqueId,
78
+ display_id: isOpaqueId,
79
+ });
61
80
  }
62
81
 
63
82
  export function assertValidSurfaceTransferAbort(
@@ -65,6 +84,13 @@ export function assertValidSurfaceTransferAbort(
65
84
  ): asserts value is SurfaceTransferAbort {
66
85
  const abort = record(value, SURFACE_TRANSFER_FIELDS.SurfaceTransferAbort);
67
86
  assertTransferProtocolVersion(abort.protocol_version);
87
+ assertFields(abort, {
88
+ request_id: isOpaqueId,
89
+ message: isString,
90
+ retryable: isBoolean,
91
+ session_consumed: isBoolean,
92
+ reconciliation_required: isBoolean,
93
+ });
68
94
  const source = record(abort.source);
69
95
  assertKnown(source.domain, SURFACE_TRANSFER_ABORT_DOMAINS, "unknown_abort_domain");
70
96
  record(source, variantKeys("SurfaceTransferAbortSource", source));
@@ -87,9 +113,7 @@ export function assertValidSurfaceSessionResponse(
87
113
  "SurfaceSessionResponse",
88
114
  );
89
115
  if (response.status === "started") {
90
- const session = record(response.session);
91
- assertTransferProtocolVersion(session.protocol_version);
92
- assertTransferProtocolVersion(record(session.payload).protocol_version);
116
+ assertValidTransferSessionStarted(response.session);
93
117
  } else {
94
118
  assertValidSurfaceTransferAbort(response.abort);
95
119
  }
@@ -109,6 +133,15 @@ export function assertValidSurfaceTransferResponse(
109
133
  SURFACE_TRANSFER_FIELDS.SurfaceTransferCompletion,
110
134
  );
111
135
  assertTransferProtocolVersion(completion.protocol_version);
136
+ assertFields(completion, {
137
+ request_id: isOpaqueId,
138
+ session_id: isTransferSessionId,
139
+ source_host_binding_id: isOpaqueId,
140
+ target_host_binding_id: isOpaqueId,
141
+ previous_revision: unsignedInteger,
142
+ committed_revision: unsignedInteger,
143
+ });
144
+ assertValidSurfaceDocument(completion.authoritative_document);
112
145
  assertValidSurfaceTransferTarget(completion.target);
113
146
  } else {
114
147
  assertValidSurfaceTransferAbort(response.abort);
@@ -126,27 +159,19 @@ function responseWithStatus(
126
159
  return response;
127
160
  }
128
161
 
129
- /**
130
- * Allowed keys for one tagged-union variant, from the generated map, with the
131
- * discriminant's name read from the map too. This domain uses three: `kind`,
132
- * `status`, and `domain` on the abort source.
133
- *
134
- * A missing entry means the generator failed to read the union — every caller
135
- * runs `assertKnown` over the discriminant above this.
136
- */
137
162
  function variantKeys(type: string, value: Record<string, unknown>): ReadonlyArray<string> {
138
- const discriminant = value[SURFACE_TRANSFER_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
139
- const keys =
140
- typeof discriminant === "string"
141
- ? SURFACE_TRANSFER_VARIANT_FIELDS[type]?.[discriminant]
142
- : undefined;
143
- if (keys === undefined) {
144
- throw new SurfaceTransferProtocolValidationError("unknown_response_status", {
145
- type,
146
- discriminant,
147
- });
148
- }
149
- return keys;
163
+ return wireVariantFields(
164
+ SURFACE_TRANSFER_VARIANT_FIELDS,
165
+ SURFACE_TRANSFER_VARIANT_FIELDS_DISCRIMINANTS,
166
+ type,
167
+ value,
168
+ (discriminant) => {
169
+ throw new SurfaceTransferProtocolValidationError("unknown_response_status", {
170
+ type,
171
+ discriminant,
172
+ });
173
+ },
174
+ );
150
175
  }
151
176
 
152
177
  function assertKnown(
@@ -159,33 +184,31 @@ function assertKnown(
159
184
  }
160
185
  }
161
186
 
162
- /**
163
- * Rejects a non-object, an unknown key, and a missing key.
164
- *
165
- * `allowed` comes from the generated field map, so the keys accepted are the
166
- * Rust struct's and nothing else — contract 010's Boundary Validation Target.
167
- * Passing no list keeps shape-only behaviour for the tagged unions, whose
168
- * allowed keys depend on their discriminant and so are not one flat set.
169
- */
170
- function record(value: unknown, allowed?: ReadonlyArray<string>): Record<string, unknown> {
171
- if (!isRecord(value)) {
172
- throw new SurfaceTransferProtocolValidationError("invalid_object", value);
173
- }
174
- const result = value;
175
- if (allowed === undefined) {
176
- return result;
177
- }
178
-
179
- const permitted = new Set(allowed);
180
- for (const key of Object.keys(result)) {
181
- if (!permitted.has(key)) {
182
- throw new SurfaceTransferProtocolValidationError("unknown_field", { key, value });
183
- }
184
- }
185
- for (const key of allowed) {
186
- if (!(key in result)) {
187
- throw new SurfaceTransferProtocolValidationError("missing_field", { key, value });
187
+ function assertFields(
188
+ value: Record<string, unknown>,
189
+ checks: Readonly<Record<string, (member: unknown) => boolean>>,
190
+ ): void {
191
+ for (const [key, admits] of Object.entries(checks)) {
192
+ if (!admits(value[key])) {
193
+ throw new SurfaceTransferProtocolValidationError("invalid_object", { key, value });
188
194
  }
189
195
  }
196
+ }
197
+
198
+ function isString(value: unknown): boolean {
199
+ return typeof value === "string";
200
+ }
201
+
202
+ function isBoolean(value: unknown): boolean {
203
+ return typeof value === "boolean";
204
+ }
205
+
206
+ function record(value: unknown, allowed?: ReadonlyArray<string>): Record<string, unknown> {
207
+ const result = readWireRecord(value, () => {
208
+ throw new SurfaceTransferProtocolValidationError("invalid_object", value);
209
+ });
210
+ checkWireFields(result, allowed, (code, key) => {
211
+ throw new SurfaceTransferProtocolValidationError(code, { key, value });
212
+ });
190
213
  return result;
191
214
  }
@@ -1,10 +1,11 @@
1
- import { CheckedSnapshotConnection } from "@inflatable-cookie/longhorn/core";
1
+ import { CheckedSnapshotConnection, CheckedSubscription } from "@inflatable-cookie/longhorn/core";
2
2
  import type {
3
3
  ConnectionFailure,
4
4
  ConnectionFailureReporter,
5
5
  EventTransport,
6
6
  } from "@inflatable-cookie/longhorn/core";
7
7
 
8
+ import { wireCopy } from "../core/wire-copy.ts";
8
9
  import type {
9
10
  SurfaceMutationRequest,
10
11
  SurfaceMutationResponse,
@@ -15,6 +16,7 @@ import {
15
16
  assertValidSurfaceMutationCommand,
16
17
  assertValidSurfaceMutationResponse,
17
18
  assertValidSurfaceSnapshot,
19
+ SurfaceProtocolValidationError,
18
20
  } from "./validation.ts";
19
21
 
20
22
  export const SURFACE_SNAPSHOT_COMMAND = "longhorn_surfaces_snapshot";
@@ -42,10 +44,19 @@ export class SurfaceClient {
42
44
 
43
45
  async mutate(request: SurfaceMutationRequest): Promise<SurfaceMutationResponse> {
44
46
  assertValidSurfaceMutationCommand(request.command);
47
+ // The caller keeps its object and a transport may serialize later, so send
48
+ // a copy taken now and correlate against the id it was sent with.
49
+ const dispatched = wireCopy(request);
50
+ const requestId = dispatched.request_id;
45
51
  const value = await this.#transport.invoke(SURFACE_MUTATE_COMMAND, {
46
- request,
52
+ request: dispatched,
47
53
  });
48
54
  assertValidSurfaceMutationResponse(value);
55
+ const echoed =
56
+ value.status === "committed" ? value.receipt.request_id : value.rejection.request_id;
57
+ if (echoed !== requestId) {
58
+ throw new SurfaceProtocolValidationError("uncorrelated_response", value);
59
+ }
49
60
  return value;
50
61
  }
51
62
 
@@ -53,42 +64,19 @@ export class SurfaceClient {
53
64
  listener: (snapshot: SurfaceSnapshot) => void,
54
65
  onFailure?: ConnectionFailureReporter,
55
66
  ): SurfaceSubscription {
56
- return new SurfaceSnapshotSubscription(this, this.#transport, listener, onFailure);
57
- }
58
- }
59
-
60
- class SurfaceSnapshotSubscription implements SurfaceSubscription {
61
- readonly ready: Promise<void>;
62
- readonly #connection: CheckedSnapshotConnection<SurfaceSnapshot>;
63
-
64
- constructor(
65
- client: SurfaceClient,
66
- transport: EventTransport,
67
- listener: (snapshot: SurfaceSnapshot) => void,
68
- onFailure?: ConnectionFailureReporter,
69
- ) {
70
- this.#connection = new CheckedSnapshotConnection({
71
- listen: (receive) => transport.listen(SURFACE_CHANGED_EVENT, receive),
72
- loadSnapshot: () => client.snapshot(),
73
- validateSnapshot: parseSurfaceSnapshot,
74
- handleEvent: surfaceEventAction,
75
- isNewer: isNewerSurfaceSnapshot,
76
- onSnapshot: listener,
77
- onFailure,
78
- });
79
- this.ready = this.#connection.ready.then(() => undefined);
80
- }
81
-
82
- current(): SurfaceSnapshot | undefined {
83
- return this.#connection.current();
84
- }
85
-
86
- failures(): ReadonlyArray<ConnectionFailure> {
87
- return this.#connection.failures();
88
- }
89
-
90
- dispose(): Promise<void> {
91
- return this.#connection.dispose();
67
+ const transport = this.#transport;
68
+ return new CheckedSubscription(
69
+ new CheckedSnapshotConnection({
70
+ listen: (receive) => transport.listen(SURFACE_CHANGED_EVENT, receive),
71
+ loadSnapshot: () => this.snapshot(),
72
+ validateSnapshot: parseSurfaceSnapshot,
73
+ handleEvent: surfaceEventAction,
74
+ isNewer: isNewerSurfaceSnapshot,
75
+ onSnapshot: listener,
76
+ onFailure,
77
+ }),
78
+ () => {},
79
+ );
92
80
  }
93
81
  }
94
82
 
@@ -1,4 +1,4 @@
1
- // @generated by `effigy generate:surfaces`; 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.
@@ -1,4 +1,4 @@
1
- // @generated by `effigy generate:surfaces`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Rust serde types are the wire authority.
3
3
 
4
4
  export const SURFACE_PROTOCOL_VERSION = 1 as const;
@@ -1,4 +1,4 @@
1
- // @generated by `effigy generate:surfaces`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Allowed field names per tagged-union variant, from the Rust enums.
3
3
  // Keyed by type, then by discriminant. The discriminant itself is in
4
4
  // each list, because `exact()` compares every key.