@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,6 +1,6 @@
1
1
  import type { BridgeHelloRequest } from "./generated/protocol.ts";
2
2
  import type { BridgeOperationDescriptor } from "./operation.ts";
3
- import { BridgeHostRouter } from "./router.ts";
3
+ import type { BridgeHostRouter } from "./router.ts";
4
4
 
5
5
  export class DirectBridgeAdapter {
6
6
  readonly #router: BridgeHostRouter;
@@ -1,9 +1,9 @@
1
- // @generated by `effigy generate:bridge`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Allowed field names per protocol type, from the Rust structs.
3
3
  // A `record()` given one of these rejects unknown and missing keys —
4
4
  // contract 010's Boundary Validation Target.
5
5
 
6
- export const BRIDGE_FIELDS: Record<string, readonly string[]> = {
6
+ export const BRIDGE_FIELDS = {
7
7
  "BridgeHostDescriptor": ["hostInstanceId", "form"],
8
8
  "BridgeConnectionStatus": ["state", "reason"],
9
9
  "DomainAuthorityDescriptor": ["domainId", "scopeId", "availability", "readAuthority", "writeAuthority", "executionAuthority", "authorityEpoch", "authoritativeRevision"],
@@ -29,4 +29,5 @@ export const BRIDGE_FIELDS: Record<string, readonly string[]> = {
29
29
  "BridgeCancellationReceipt": ["requestId", "targetRequestId", "jobId", "outcome"],
30
30
  "BridgeServiceRequest": ["action", "credentialRef"],
31
31
  "BridgeServiceTransitionReceipt": ["generation", "ownership", "action", "previous", "current", "outcome"],
32
- };
32
+ "BridgeHostError": ["code", "message", "retryable"],
33
+ } as const;
@@ -1,8 +1,8 @@
1
- // @generated by `effigy generate:bridge`; 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 BRIDGE_PROTOCOL_VERSION = 1 as const;
5
- export const BRIDGE_MAXIMUM_OPAQUE_ID_BYTES = 128 as const;
5
+ export const BRIDGE_DOMAIN_ID_RULE = {"maximumBytes":128,"segmentAdditional":"0123456789_-","segmentInitial":"abcdefghijklmnopqrstuvwxyz","separator":"."} as const;
6
6
  // Wire-visible bounds. The Rust constants are the authority; a
7
7
  // hand-copied literal in a validator is drift waiting to happen.
8
8
  export const BRIDGE_MAXIMUM_REQUESTED_DOMAINS = 256 as const;
@@ -16,7 +16,7 @@ export const BRIDGE_MAXIMUM_FAILURE_MESSAGE_BYTES = 4096 as const;
16
16
  export const BRIDGE_MAXIMUM_DEDUPLICATION_ENTRIES = 65536 as const;
17
17
  export const BRIDGE_HOST_FORMS = ["direct","tauriLocal","localService","remote","localFirst"] as const;
18
18
  export const BRIDGE_CONNECTION_STATES = ["idle","connecting","negotiating","ready","degraded","reconnecting","offline","incompatible","unauthorized","failed","closed"] as const;
19
- export const BRIDGE_CONNECTION_REASONS = ["connectRequested","transportReady","negotiationAccepted","capabilityChanged","transportLost","retryScheduled","versionMismatch","authorizationRejected","hostFailure","shutdown"] as const;
19
+ export const BRIDGE_CONNECTION_REASONS = ["connectRequested","transportReady","negotiationAccepted","capabilityChanged","transportLost","retryScheduled","retryForbidden","versionMismatch","authorizationRejected","hostFailure","shutdown"] as const;
20
20
  export const BRIDGE_AUTHENTICATION_POSTURES = ["notRequired","required","authenticated","rejected"] as const;
21
21
  export const BRIDGE_DOMAIN_AVAILABILITIES = ["available","degraded","offline"] as const;
22
22
  export const BRIDGE_READ_AUTHORITIES = ["none","projection","authoritative"] as const;
@@ -33,7 +33,7 @@ export const BRIDGE_SERVICE_OWNERSHIPS = ["ownedLocal","externalLocal","external
33
33
  export const BRIDGE_SERVICE_STATES = ["absent","starting","attaching","awaitingReadiness","ready","restarting","reconnecting","stopped","failed"] as const;
34
34
  export const BRIDGE_SERVICE_ACTIONS = ["spawn","attach","checkReadiness","restart","reconnect","shutdown"] as const;
35
35
  export const BRIDGE_SERVICE_FAILURE_CODES = ["spawnFailed","attachFailed","readinessFailed","serviceExited","restartFailed","reconnectFailed","shutdownFailed"] as const;
36
- export const BRIDGE_SNAPSHOT_DECISIONS = ["accepted","acceptedResnapshotRequired","supersededSession","wrongDomain"] as const;
36
+ export const BRIDGE_SNAPSHOT_DECISIONS = ["accepted","acceptedResnapshotRequired","supersededSession","wrongDomain","staleSnapshot"] as const;
37
37
  export const BRIDGE_STREAM_DECISIONS = ["apply","ignoreDuplicate","ignoreStale","ignoreSupersededSession","ignoreWrongDomain","resnapshotGap","resnapshotNewEpoch","resnapshotRequired"] as const;
38
38
  export const BRIDGE_PROGRESS_DECISIONS = ["accept","ignoreWrongCorrelation","ignoreAfterTerminal"] as const;
39
39
  export const BRIDGE_TERMINAL_DECISIONS = ["accept","ignoreWrongCorrelation","ignoreAlreadyTerminal"] as const;
@@ -80,7 +80,7 @@ form: BridgeHostForm, };
80
80
 
81
81
  export type BridgeConnectionState = "idle" | "connecting" | "negotiating" | "ready" | "degraded" | "reconnecting" | "offline" | "incompatible" | "unauthorized" | "failed" | "closed";
82
82
 
83
- export type BridgeConnectionReason = "connectRequested" | "transportReady" | "negotiationAccepted" | "capabilityChanged" | "transportLost" | "retryScheduled" | "versionMismatch" | "authorizationRejected" | "hostFailure" | "shutdown";
83
+ export type BridgeConnectionReason = "connectRequested" | "transportReady" | "negotiationAccepted" | "capabilityChanged" | "transportLost" | "retryScheduled" | "retryForbidden" | "versionMismatch" | "authorizationRejected" | "hostFailure" | "shutdown";
84
84
 
85
85
  export type BridgeConnectionStatus = { state: BridgeConnectionState, reason: BridgeConnectionReason | null, };
86
86
 
@@ -166,7 +166,7 @@ export type BridgeSnapshotEnvelope<P> = { cursor: BridgeStreamCursor, payload: P
166
166
 
167
167
  export type BridgeEventEnvelope<P> = { cursor: BridgeStreamCursor, payload: P, };
168
168
 
169
- export type BridgeSnapshotDecision = "accepted" | "acceptedResnapshotRequired" | "supersededSession" | "wrongDomain";
169
+ export type BridgeSnapshotDecision = "accepted" | "acceptedResnapshotRequired" | "supersededSession" | "wrongDomain" | "staleSnapshot";
170
170
 
171
171
  export type BridgeStreamDecision = "apply" | "ignoreDuplicate" | "ignoreStale" | "ignoreSupersededSession" | "ignoreWrongDomain" | "resnapshotGap" | "resnapshotNewEpoch" | "resnapshotRequired";
172
172
 
@@ -199,6 +199,10 @@ export type BridgeServiceRequest = { action: BridgeServiceAction, credentialRef:
199
199
  export type BridgeServiceGeneration = number;
200
200
 
201
201
  export type BridgeServiceTransitionReceipt = { generation: BridgeServiceGeneration, ownership: BridgeServiceOwnership, action: BridgeServiceAction, previous: BridgeServiceState, current: BridgeServiceState, outcome: BridgeServiceOutcome, };
202
+
203
+ export type BridgeHostErrorCode = "authorityUnavailable" | "invalidSession" | "unknownDomain" | "unknownRoute" | "capabilityUnavailable" | "readDenied" | "writeDenied" | "executionDenied" | "staleAuthority" | "invalidAuthority" | "invalidReply" | "payloadCodec" | "stateUnavailable" | "eventUnavailable" | "eventPublication" | "invalidRegistration";
204
+
205
+ export type BridgeHostError = { "code": BridgeHostErrorCode, "message": string, "retryable": boolean, };
202
206
  export const BRIDGE_ADMITTED_CONNECTION_REASONS: Record<
203
207
  BridgeConnectionState,
204
208
  readonly (BridgeConnectionReason | null)[]
@@ -209,10 +213,61 @@ export const BRIDGE_ADMITTED_CONNECTION_REASONS: Record<
209
213
  "ready": ["negotiationAccepted", "capabilityChanged"],
210
214
  "degraded": ["capabilityChanged", "transportLost", "hostFailure"],
211
215
  "reconnecting": ["retryScheduled", "transportLost"],
212
- "offline": ["transportLost"],
216
+ "offline": ["transportLost", "retryForbidden"],
213
217
  "incompatible": ["versionMismatch"],
214
218
  "unauthorized": ["authorizationRejected"],
215
219
  "failed": ["hostFailure"],
216
220
  "closed": ["shutdown"],
217
221
  };
218
222
 
223
+ export const BRIDGE_ADMITTED_CONNECTION_TRANSITIONS: Record<
224
+ BridgeConnectionState,
225
+ readonly BridgeConnectionState[]
226
+ > = {
227
+ "idle": ["connecting", "closed"],
228
+ "connecting": ["negotiating", "reconnecting", "offline", "failed", "closed"],
229
+ "negotiating": ["ready", "reconnecting", "offline", "incompatible", "unauthorized", "failed", "closed"],
230
+ "ready": ["degraded", "reconnecting", "offline", "failed", "closed"],
231
+ "degraded": ["ready", "reconnecting", "offline", "failed", "closed"],
232
+ "reconnecting": ["negotiating", "reconnecting", "offline", "failed", "closed"],
233
+ "offline": ["reconnecting", "failed", "closed"],
234
+ "incompatible": ["closed"],
235
+ "unauthorized": ["closed"],
236
+ "failed": ["closed"],
237
+ "closed": [],
238
+ };
239
+
240
+ export const BRIDGE_SERVICE_UNIT_OUTCOMES = ["accepted","ready","notReady","stopped"] as const;
241
+ export const BRIDGE_SERVICE_FAILURE_STATE: BridgeServiceState = "failed";
242
+ export const BRIDGE_SERVICE_INCARNATION_STATES: readonly BridgeServiceState[] = ["starting","attaching","restarting","reconnecting"];
243
+
244
+ export const BRIDGE_SERVICE_ADMISSIONS: Record<
245
+ BridgeServiceAction,
246
+ {
247
+ readonly ownerships: readonly BridgeServiceOwnership[];
248
+ readonly states: readonly BridgeServiceState[];
249
+ readonly requiresOwnedLifecycle: boolean;
250
+ }
251
+ > = {
252
+ "spawn": {"ownerships":["ownedLocal"],"states":["absent","stopped","failed"],"requiresOwnedLifecycle":true},
253
+ "attach": {"ownerships":["externalLocal","externalRemote"],"states":["absent","stopped","failed"],"requiresOwnedLifecycle":false},
254
+ "checkReadiness": {"ownerships":["ownedLocal","externalLocal","externalRemote"],"states":["starting","attaching","awaitingReadiness","restarting","reconnecting","ready"],"requiresOwnedLifecycle":false},
255
+ "restart": {"ownerships":["ownedLocal"],"states":["ready","failed"],"requiresOwnedLifecycle":true},
256
+ "reconnect": {"ownerships":["ownedLocal","externalLocal","externalRemote"],"states":["ready","failed","stopped"],"requiresOwnedLifecycle":false},
257
+ "shutdown": {"ownerships":["ownedLocal"],"states":["starting","attaching","awaitingReadiness","ready","restarting","reconnecting","failed"],"requiresOwnedLifecycle":true},
258
+ };
259
+
260
+ export const BRIDGE_SERVICE_ADMITTED_OBSERVATIONS: readonly (readonly [
261
+ BridgeServiceAction,
262
+ BridgeServiceOutcome,
263
+ BridgeServiceState,
264
+ ])[] = [
265
+ ["spawn","accepted","starting"],
266
+ ["attach","accepted","attaching"],
267
+ ["checkReadiness","ready","ready"],
268
+ ["checkReadiness","notReady","awaitingReadiness"],
269
+ ["restart","accepted","restarting"],
270
+ ["reconnect","accepted","reconnecting"],
271
+ ["shutdown","stopped","stopped"],
272
+ ];
273
+
@@ -1,4 +1,4 @@
1
- // @generated by `effigy generate:bridge`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Allowed field names per 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.
package/src/bridge/job.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { isRecord } from "../core/record.ts";
1
2
  import type {
2
3
  BridgeJobId,
3
4
  BridgeJobTerminalDecision,
@@ -6,6 +7,13 @@ import type {
6
7
  BridgeProgressEvent,
7
8
  BridgeRequestId,
8
9
  } from "./generated/protocol.ts";
10
+ import { incompatible, opaqueId } from "./validation/base.ts";
11
+
12
+ /** The identifiers that tie a progress or terminal event to one job. */
13
+ interface BridgeJobCorrelation {
14
+ readonly requestId: BridgeRequestId;
15
+ readonly jobId: BridgeJobId;
16
+ }
9
17
 
10
18
  export class BridgeJobTracker {
11
19
  readonly #requestId: BridgeRequestId;
@@ -17,25 +25,45 @@ export class BridgeJobTracker {
17
25
  this.#jobId = jobId;
18
26
  }
19
27
 
20
- classifyProgress<P>(
21
- event: BridgeProgressEvent<P>,
22
- ): BridgeProgressDecision {
28
+ /**
29
+ * Whether a raw progress or terminal event belongs to this job, read before
30
+ * any payload is decoded. An identifier that reads as another request or job
31
+ * makes the event foreign, whatever the other identifier is, so another job's
32
+ * event is refused without its payload being decoded. An event is admitted
33
+ * only when both identifiers read and match. One that names no other job and
34
+ * has an unreadable identifier throws as malformed.
35
+ */
36
+ admits(value: unknown): boolean {
37
+ if (!isRecord(value)) {
38
+ incompatible("invalid_object", value);
39
+ }
40
+ const requestId = readOpaqueId(value.requestId);
41
+ const jobId = readOpaqueId(value.jobId);
23
42
  if (
24
- event.requestId !== this.#requestId ||
25
- event.jobId !== this.#jobId
43
+ (requestId !== undefined && requestId !== this.#requestId) ||
44
+ (jobId !== undefined && jobId !== this.#jobId)
26
45
  ) {
46
+ return false;
47
+ }
48
+ if (requestId === undefined || jobId === undefined) {
49
+ incompatible("invalid_id", value);
50
+ }
51
+ return true;
52
+ }
53
+
54
+ #matches(event: BridgeJobCorrelation): boolean {
55
+ return event.requestId === this.#requestId && event.jobId === this.#jobId;
56
+ }
57
+
58
+ classifyProgress<P>(event: BridgeProgressEvent<P>): BridgeProgressDecision {
59
+ if (!this.#matches(event)) {
27
60
  return "ignoreWrongCorrelation";
28
61
  }
29
62
  return this.#terminal ? "ignoreAfterTerminal" : "accept";
30
63
  }
31
64
 
32
- classifyTerminal<S, D>(
33
- event: BridgeJobTerminalEvent<S, D>,
34
- ): BridgeJobTerminalDecision {
35
- if (
36
- event.requestId !== this.#requestId ||
37
- event.jobId !== this.#jobId
38
- ) {
65
+ classifyTerminal<S, D>(event: BridgeJobTerminalEvent<S, D>): BridgeJobTerminalDecision {
66
+ if (!this.#matches(event)) {
39
67
  return "ignoreWrongCorrelation";
40
68
  }
41
69
  if (this.#terminal) {
@@ -49,3 +77,12 @@ export class BridgeJobTracker {
49
77
  return this.#terminal;
50
78
  }
51
79
  }
80
+
81
+ /** The identifier, or `undefined` when it is not a readable opaque id. */
82
+ function readOpaqueId(value: unknown): string | undefined {
83
+ try {
84
+ return opaqueId(value);
85
+ } catch {
86
+ return undefined;
87
+ }
88
+ }
@@ -1,6 +1,6 @@
1
1
  import type { BridgeHelloRequest } from "./generated/protocol.ts";
2
2
  import type { BridgeOperationDescriptor } from "./operation.ts";
3
- import { BridgeHostRouter } from "./router.ts";
3
+ import type { BridgeHostRouter } from "./router.ts";
4
4
 
5
5
  export type BridgeSerializationPhase =
6
6
  | "negotiation_request"
@@ -30,15 +30,9 @@ export class SerializedLoopbackBridgeAdapter {
30
30
  }
31
31
 
32
32
  async negotiate(request: BridgeHelloRequest): Promise<unknown> {
33
- const encodedRequest = serialize(
34
- request,
35
- "negotiation_request",
36
- );
33
+ const encodedRequest = serialize(request, "negotiation_request");
37
34
  const reply = await this.#router.negotiate(JSON.parse(encodedRequest));
38
- return deserialize(
39
- serialize(reply, "negotiation_reply"),
40
- "negotiation_reply",
41
- );
35
+ return deserialize(serialize(reply, "negotiation_reply"), "negotiation_reply");
42
36
  }
43
37
 
44
38
  async invoke<Request, Reply>(
@@ -46,21 +40,12 @@ export class SerializedLoopbackBridgeAdapter {
46
40
  request: Request,
47
41
  ): Promise<unknown> {
48
42
  const encodedRequest = serialize(request, "operation_request");
49
- const reply = await this.#router.invoke(
50
- operation.route,
51
- JSON.parse(encodedRequest),
52
- );
53
- return deserialize(
54
- serialize(reply, "operation_reply"),
55
- "operation_reply",
56
- );
43
+ const reply = await this.#router.invoke(operation.route, JSON.parse(encodedRequest));
44
+ return deserialize(serialize(reply, "operation_reply"), "operation_reply");
57
45
  }
58
46
  }
59
47
 
60
- export function serialize(
61
- value: unknown,
62
- phase: BridgeSerializationPhase,
63
- ): string {
48
+ export function serialize(value: unknown, phase: BridgeSerializationPhase): string {
64
49
  try {
65
50
  const encoded = JSON.stringify(value);
66
51
  if (encoded === undefined) {
@@ -72,10 +57,7 @@ export function serialize(
72
57
  }
73
58
  }
74
59
 
75
- export function deserialize(
76
- value: string,
77
- phase: BridgeSerializationPhase,
78
- ): unknown {
60
+ export function deserialize(value: string, phase: BridgeSerializationPhase): unknown {
79
61
  try {
80
62
  return JSON.parse(value);
81
63
  } catch (error) {
@@ -1,3 +1,4 @@
1
+ import { isRecord } from "../core/record.ts";
1
2
  import type {
2
3
  BridgeCommandDelivery,
3
4
  BridgeCommandEnvelope,
@@ -9,7 +10,7 @@ import type {
9
10
  DomainId,
10
11
  } from "./generated/protocol.ts";
11
12
  import type { BridgeCodec } from "./validation.ts";
12
- import { BridgeSession } from "./session.ts";
13
+ import type { BridgeSession } from "./session.ts";
13
14
 
14
15
  export type BridgeOperationKind = "query" | "command" | "cancellation";
15
16
 
@@ -44,52 +45,27 @@ export class BridgeDomainClient {
44
45
  ): Promise<Reply> {
45
46
  const checkedRequest = operation.request.parse(request);
46
47
  this.#assertAuthority(operation, checkedRequest);
47
- return operation.reply.parse(
48
- await this.#adapter.invoke(operation, checkedRequest),
49
- );
48
+ return operation.reply.parse(await this.#adapter.invoke(operation, checkedRequest));
50
49
  }
51
50
 
52
51
  #assertAuthority<Request, Reply>(
53
52
  operation: BridgeOperationDescriptor<Request, Reply>,
54
53
  request: Request,
55
54
  ): void {
56
- if (
57
- !this.#session.supports(
58
- operation.domainId,
59
- operation.requiredCapability,
60
- )
61
- ) {
62
- throw new BridgeAuthorityError(
63
- "capability_unavailable",
64
- operation.domainId,
65
- );
55
+ if (!this.#session.supports(operation.domainId, operation.requiredCapability)) {
56
+ throw new BridgeAuthorityError("capability_unavailable", operation.domainId);
66
57
  }
67
58
  const authority = this.#session.authority(operation.domainId);
68
- const context = (
69
- request as {
70
- readonly context?: {
71
- readonly domainId?: unknown;
72
- readonly sessionId?: unknown;
73
- };
74
- }
75
- ).context;
59
+ const envelope = isRecord(request) ? request : undefined;
60
+ const context = isRecord(envelope?.context) ? envelope.context : undefined;
76
61
  if (context?.domainId !== operation.domainId) {
77
- throw new BridgeAuthorityError(
78
- "request_domain_mismatch",
79
- operation.domainId,
80
- );
62
+ throw new BridgeAuthorityError("request_domain_mismatch", operation.domainId);
81
63
  }
82
64
  if (context.sessionId !== this.#session.receipt.sessionId) {
83
- throw new BridgeAuthorityError(
84
- "request_session_mismatch",
85
- operation.domainId,
86
- );
65
+ throw new BridgeAuthorityError("request_session_mismatch", operation.domainId);
87
66
  }
88
67
  if (authority === undefined || authority.availability === "offline") {
89
- throw new BridgeAuthorityError(
90
- "authority_unavailable",
91
- operation.domainId,
92
- );
68
+ throw new BridgeAuthorityError("authority_unavailable", operation.domainId);
93
69
  }
94
70
  if (operation.kind === "query" && authority.readAuthority === "none") {
95
71
  throw new BridgeAuthorityError("read_denied", operation.domainId);
@@ -98,15 +74,11 @@ export class BridgeDomainClient {
98
74
  if (authority.writeAuthority !== "authoritative") {
99
75
  throw new BridgeAuthorityError("write_denied", operation.domainId);
100
76
  }
101
- const envelope = request as BridgeCommandEnvelope<unknown>;
102
- if (envelope.authorityEpoch !== authority.authorityEpoch) {
77
+ if (envelope?.authorityEpoch !== authority.authorityEpoch) {
103
78
  throw new BridgeAuthorityError("stale_authority", operation.domainId);
104
79
  }
105
80
  }
106
- if (
107
- operation.kind === "cancellation" &&
108
- authority.executionAuthority !== "executor"
109
- ) {
81
+ if (operation.kind === "cancellation" && authority.executionAuthority !== "executor") {
110
82
  throw new BridgeAuthorityError("execution_denied", operation.domainId);
111
83
  }
112
84
  }
@@ -160,7 +132,5 @@ export function classifyBridgeQueryRetry<P>(
160
132
  retryClass: BridgeRetryClass,
161
133
  adapterAllowsRetry: boolean,
162
134
  ): BridgeQueryRetryDecision {
163
- return adapterAllowsRetry && retryClass !== "never"
164
- ? "retry"
165
- : "doNotRetry";
135
+ return adapterAllowsRetry && retryClass !== "never" ? "retry" : "doNotRetry";
166
136
  }
@@ -1,20 +1,11 @@
1
- import type {
2
- BridgeHelloRequest,
3
- BridgeNegotiationReceipt,
4
- } from "./generated/protocol.ts";
5
- import {
6
- parseBridgeHelloRequest,
7
- parseBridgeNegotiationReceipt,
8
- } from "./validation.ts";
1
+ import type { BridgeHelloRequest, BridgeNegotiationReceipt } from "./generated/protocol.ts";
2
+ import { parseBridgeHelloRequest, parseBridgeNegotiationReceipt } from "./validation.ts";
9
3
  import type { BridgeOperationDescriptor } from "./operation.ts";
10
4
 
11
- type BridgeOperationHandler<Request, Reply> = (
12
- request: Request,
13
- ) => Reply | Promise<Reply>;
5
+ type BridgeOperationHandler<Request, Reply> = (request: Request) => Reply | Promise<Reply>;
14
6
 
15
7
  interface RegisteredOperation {
16
- readonly descriptor: BridgeOperationDescriptor<unknown, unknown>;
17
- readonly handler: BridgeOperationHandler<unknown, unknown>;
8
+ invoke(value: unknown): Promise<unknown>;
18
9
  }
19
10
 
20
11
  export class BridgeHostRouter {
@@ -39,8 +30,10 @@ export class BridgeHostRouter {
39
30
  throw new Error(`duplicate bridge operation route: ${descriptor.route}`);
40
31
  }
41
32
  const registration: RegisteredOperation = {
42
- descriptor: descriptor as BridgeOperationDescriptor<unknown, unknown>,
43
- handler: handler as BridgeOperationHandler<unknown, unknown>,
33
+ async invoke(value) {
34
+ const request = descriptor.request.parse(value);
35
+ return descriptor.reply.parse(await handler(request));
36
+ },
44
37
  };
45
38
  this.#operations.set(descriptor.route, registration);
46
39
  return () => {
@@ -52,10 +45,7 @@ export class BridgeHostRouter {
52
45
 
53
46
  async negotiate(value: unknown): Promise<BridgeNegotiationReceipt> {
54
47
  const request = parseBridgeHelloRequest(value);
55
- return parseBridgeNegotiationReceipt(
56
- await this.#negotiate(request),
57
- request,
58
- );
48
+ return parseBridgeNegotiationReceipt(await this.#negotiate(request), request);
59
49
  }
60
50
 
61
51
  async invoke(route: string, value: unknown): Promise<unknown> {
@@ -63,9 +53,6 @@ export class BridgeHostRouter {
63
53
  if (registration === undefined) {
64
54
  throw new Error(`unknown bridge operation route: ${route}`);
65
55
  }
66
- const request = registration.descriptor.request.parse(value);
67
- return registration.descriptor.reply.parse(
68
- await registration.handler(request),
69
- );
56
+ return registration.invoke(value);
70
57
  }
71
58
  }