@inflatable-cookie/longhorn 0.4.3 → 0.6.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 (126) hide show
  1. package/package.json +1 -1
  2. package/src/agent-control/inject.ts +4 -1
  3. package/src/agent-control/selection.ts +35 -20
  4. package/src/agent-control/shim.ts +302 -144
  5. package/src/bridge/direct.ts +1 -1
  6. package/src/bridge/generated/fields.ts +3 -2
  7. package/src/bridge/generated/protocol.ts +7 -3
  8. package/src/bridge/job.ts +4 -14
  9. package/src/bridge/loopback.ts +7 -25
  10. package/src/bridge/operation.ts +13 -43
  11. package/src/bridge/router.ts +10 -23
  12. package/src/bridge/runtime/connection.ts +77 -105
  13. package/src/bridge/runtime/policy.ts +8 -28
  14. package/src/bridge/runtime/retry.ts +6 -18
  15. package/src/bridge/session.ts +4 -14
  16. package/src/bridge/stream/connection.ts +16 -38
  17. package/src/bridge/stream/source.ts +2 -6
  18. package/src/bridge/stream/tracker.ts +7 -12
  19. package/src/bridge/supervision.ts +11 -26
  20. package/src/bridge/validation/base.ts +17 -39
  21. package/src/bridge/validation/jobs.ts +11 -25
  22. package/src/bridge/validation/negotiation.ts +27 -37
  23. package/src/bridge/validation/operations.ts +28 -52
  24. package/src/bridge/validation/streams.ts +3 -10
  25. package/src/commands/client.ts +21 -26
  26. package/src/commands/controller.ts +32 -47
  27. package/src/commands/generated/fields.ts +3 -2
  28. package/src/commands/generated/protocol.ts +4 -0
  29. package/src/commands/keyboard.ts +92 -89
  30. package/src/commands/ports.ts +6 -12
  31. package/src/commands/projectors.ts +50 -48
  32. package/src/commands/validation.ts +70 -97
  33. package/src/config/client.ts +6 -19
  34. package/src/config/generated/fields.ts +2 -2
  35. package/src/config/names.ts +6 -12
  36. package/src/config/validation/commands.ts +5 -17
  37. package/src/config/validation/outcomes.ts +31 -39
  38. package/src/config/validation/primitives.ts +36 -53
  39. package/src/config/validation/projection.ts +66 -94
  40. package/src/core/connection.ts +70 -61
  41. package/src/core/record.ts +4 -0
  42. package/src/core/transport.ts +4 -15
  43. package/src/core/wire-copy.ts +6 -0
  44. package/src/history/client.ts +6 -12
  45. package/src/history/controller.ts +46 -46
  46. package/src/history/direct.ts +1 -3
  47. package/src/history/generated/fields.ts +2 -2
  48. package/src/history/ports.ts +4 -6
  49. package/src/history/serialized.ts +9 -13
  50. package/src/history/validation.ts +71 -55
  51. package/src/history-tree/client.ts +87 -11
  52. package/src/history-tree/controller.ts +313 -70
  53. package/src/history-tree/direct.ts +3 -1
  54. package/src/history-tree/generated/fields.ts +2 -2
  55. package/src/history-tree/ports.ts +3 -1
  56. package/src/history-tree/serialized.ts +39 -11
  57. package/src/history-tree/validation.ts +262 -63
  58. package/src/layout/generated/protocol.ts +2 -2
  59. package/src/layout/ratio.ts +2 -8
  60. package/src/layout/validation.ts +10 -19
  61. package/src/layout/visibility.ts +1 -2
  62. package/src/licence/client.ts +44 -7
  63. package/src/licence/controller.ts +76 -40
  64. package/src/licence/direct.ts +3 -1
  65. package/src/licence/generated/fields.ts +2 -2
  66. package/src/licence/generated/key-conformance.json +28 -0
  67. package/src/licence/generated/protocol.ts +1 -1
  68. package/src/licence/key.ts +40 -12
  69. package/src/licence/ports.ts +3 -1
  70. package/src/licence/serialized.ts +32 -9
  71. package/src/licence/validation.ts +120 -36
  72. package/src/native-content/client.ts +178 -236
  73. package/src/native-content/generated/fields.ts +2 -2
  74. package/src/native-content/ports.ts +9 -15
  75. package/src/native-content/serialized.ts +12 -19
  76. package/src/native-content/validation/base.ts +57 -50
  77. package/src/native-content/validation/messages.ts +21 -35
  78. package/src/native-content/validation/state.ts +31 -33
  79. package/src/notifications/client.ts +87 -31
  80. package/src/notifications/controller.ts +215 -61
  81. package/src/notifications/direct.ts +3 -1
  82. package/src/notifications/generated/fields.ts +2 -2
  83. package/src/notifications/ports.ts +7 -3
  84. package/src/notifications/serialized.ts +11 -9
  85. package/src/notifications/validation.ts +147 -43
  86. package/src/operation/bridge.ts +117 -50
  87. package/src/operation/client.ts +38 -24
  88. package/src/operation/controller.ts +33 -42
  89. package/src/operation/direct.ts +1 -3
  90. package/src/operation/generated/fields.ts +2 -2
  91. package/src/operation/ports.ts +10 -9
  92. package/src/operation/serialized.ts +8 -9
  93. package/src/operation/validation.ts +152 -65
  94. package/src/settings/client.ts +11 -44
  95. package/src/settings/connection.ts +26 -40
  96. package/src/settings/generated/fields.ts +3 -3
  97. package/src/settings/generated/protocol.ts +9 -4
  98. package/src/settings/names.ts +2 -4
  99. package/src/settings/registry.ts +23 -25
  100. package/src/settings/validation/authority.ts +19 -17
  101. package/src/settings/validation/command.ts +17 -37
  102. package/src/settings/validation/error.ts +2 -8
  103. package/src/settings/validation/primitives.ts +26 -42
  104. package/src/settings/validation/projection.ts +8 -24
  105. package/src/settings/validation/registry.ts +25 -73
  106. package/src/surface-transfer/client.ts +6 -18
  107. package/src/surface-transfer/generated/fields.ts +2 -2
  108. package/src/surface-transfer/validation.ts +31 -49
  109. package/src/surfaces/client.ts +12 -22
  110. package/src/surfaces/generated/fields.ts +2 -2
  111. package/src/surfaces/validation.ts +40 -49
  112. package/src/transfer/client.ts +26 -60
  113. package/src/transfer/connection.ts +12 -20
  114. package/src/transfer/generated/fields.ts +5 -5
  115. package/src/transfer/generated/protocol.ts +4 -4
  116. package/src/transfer/payload.ts +13 -31
  117. package/src/transfer/validation.ts +39 -75
  118. package/src/update/client.ts +59 -9
  119. package/src/update/controller.ts +113 -48
  120. package/src/update/direct.ts +3 -1
  121. package/src/update/generated/fields.ts +3 -3
  122. package/src/update/generated/protocol.ts +7 -3
  123. package/src/update/generated/variant-fields.ts +1 -0
  124. package/src/update/ports.ts +6 -2
  125. package/src/update/serialized.ts +34 -10
  126. package/src/update/validation.ts +115 -35
@@ -1,17 +1,14 @@
1
- import {
2
- isNewerOperationSnapshot,
3
- OperationClient,
4
- type OperationSubscription,
5
- } from "./client.ts";
6
- import {
7
- OPERATION_PROTOCOL_VERSION,
8
- type OperationCancellationResult,
9
- type OperationEntryProjection,
10
- type OperationId,
11
- type OperationMutationResult,
12
- type OperationRejection,
13
- type OperationRequestId,
14
- type OperationSnapshot,
1
+ import { isNewerOperationSnapshot, OperationClient } from "./client.ts";
2
+ import type { OperationSubscription } from "./client.ts";
3
+ import { OPERATION_PROTOCOL_VERSION } from "./generated/protocol.ts";
4
+ import type {
5
+ OperationCancellationResult,
6
+ OperationEntryProjection,
7
+ OperationId,
8
+ OperationMutationResult,
9
+ OperationRejection,
10
+ OperationRequestId,
11
+ OperationSnapshot,
15
12
  } from "./generated/protocol.ts";
16
13
  import type { OperationPort } from "./ports.ts";
17
14
 
@@ -67,11 +64,11 @@ export class OperationController {
67
64
  return this.#snapshot;
68
65
  }
69
66
 
70
- get active(): readonly OperationEntryProjection[] {
67
+ get active(): ReadonlyArray<OperationEntryProjection> {
71
68
  return this.#snapshot?.active ?? [];
72
69
  }
73
70
 
74
- get recent(): readonly OperationEntryProjection[] {
71
+ get recent(): ReadonlyArray<OperationEntryProjection> {
75
72
  return this.#snapshot?.recent ?? [];
76
73
  }
77
74
 
@@ -80,11 +77,13 @@ export class OperationController {
80
77
  }
81
78
 
82
79
  get selected(): OperationEntryProjection | undefined {
83
- if (this.#selectedOperationId === undefined) return undefined;
80
+ if (this.#selectedOperationId === undefined) {
81
+ return undefined;
82
+ }
84
83
  return this.#entry(this.#selectedOperationId);
85
84
  }
86
85
 
87
- get pendingCommands(): readonly OperationPendingCommand[] {
86
+ get pendingCommands(): ReadonlyArray<OperationPendingCommand> {
88
87
  return [...this.#pending.values()];
89
88
  }
90
89
 
@@ -102,7 +101,9 @@ export class OperationController {
102
101
  }
103
102
 
104
103
  async start(): Promise<void> {
105
- if (this.#started) return;
104
+ if (this.#started) {
105
+ return;
106
+ }
106
107
  this.#started = true;
107
108
  const lifecycleRevision = ++this.#lifecycleRevision;
108
109
  this.#setStatus({ kind: "loading" });
@@ -191,23 +192,17 @@ export class OperationController {
191
192
  operationId,
192
193
  expectedOperationRevision: operation.revision,
193
194
  } as const;
194
- return this.#runCommand(
195
- { requestId: command.requestId, operationId, kind: "dismissal" },
196
- () => this.#client.mutate(command),
195
+ return this.#runCommand({ requestId: command.requestId, operationId, kind: "dismissal" }, () =>
196
+ this.#client.mutate(command),
197
197
  );
198
198
  }
199
199
 
200
- async #runCommand<
201
- Result extends OperationCancellationResult | OperationMutationResult,
202
- >(
200
+ async #runCommand<Result extends OperationCancellationResult | OperationMutationResult>(
203
201
  pending: OperationPendingCommand,
204
202
  run: () => Promise<Result>,
205
203
  ): Promise<Result> {
206
204
  if (this.#hasPending(pending.operationId, pending.kind)) {
207
- throw new OperationControllerCommandPendingError(
208
- pending.operationId,
209
- pending.kind,
210
- );
205
+ throw new OperationControllerCommandPendingError(pending.operationId, pending.kind);
211
206
  }
212
207
  const lifecycleRevision = this.#lifecycleRevision;
213
208
  const commandRevision = ++this.#commandRevision;
@@ -220,10 +215,7 @@ export class OperationController {
220
215
  const result = await run();
221
216
  if (this.#isCurrentLifecycle(lifecycleRevision)) {
222
217
  this.#install(result.snapshot);
223
- if (
224
- commandRevision === this.#commandRevision &&
225
- result.status === "rejected"
226
- ) {
218
+ if (commandRevision === this.#commandRevision && result.status === "rejected") {
227
219
  this.#commandRejection = { ...pending, rejection: result.rejection };
228
220
  }
229
221
  }
@@ -256,22 +248,19 @@ export class OperationController {
256
248
  }
257
249
 
258
250
  #entry(operationId: OperationId): OperationEntryProjection | undefined {
259
- return [...this.active, ...this.recent].find(
260
- (entry) => entry.operationId === operationId,
261
- );
251
+ return [...this.active, ...this.recent].find((entry) => entry.operationId === operationId);
262
252
  }
263
253
 
264
- #hasPending(
265
- operationId: OperationId,
266
- kind: OperationPendingCommandKind,
267
- ): boolean {
254
+ #hasPending(operationId: OperationId, kind: OperationPendingCommandKind): boolean {
268
255
  return [...this.#pending.values()].some(
269
256
  (pending) => pending.operationId === operationId && pending.kind === kind,
270
257
  );
271
258
  }
272
259
 
273
260
  #install(snapshot: OperationSnapshot): void {
274
- if (!isNewerOperationSnapshot(snapshot, this.#snapshot)) return;
261
+ if (!isNewerOperationSnapshot(snapshot, this.#snapshot)) {
262
+ return;
263
+ }
275
264
  this.#snapshot = snapshot;
276
265
  if (
277
266
  this.#selectedOperationId !== undefined &&
@@ -292,7 +281,9 @@ export class OperationController {
292
281
  }
293
282
 
294
283
  #notify(): void {
295
- for (const observer of this.#observers) observer();
284
+ for (const observer of this.#observers) {
285
+ observer();
286
+ }
296
287
  }
297
288
  }
298
289
 
@@ -8,8 +8,6 @@ export interface DirectOperationHandlers {
8
8
  readonly nextRequestId: OperationPort["nextRequestId"];
9
9
  }
10
10
 
11
- export function createDirectOperationPort(
12
- handlers: DirectOperationHandlers,
13
- ): OperationPort {
11
+ export function createDirectOperationPort(handlers: DirectOperationHandlers): OperationPort {
14
12
  return { ...handlers };
15
13
  }
@@ -3,7 +3,7 @@
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 OPERATION_FIELDS: Record<string, readonly string[]> = {
6
+ export const OPERATION_FIELDS = {
7
7
  "OperationAuthorityProjection": ["authorityId", "authorityEpoch"],
8
8
  "OperationPhaseProgressProjection": ["phaseId", "label", "completed", "total"],
9
9
  "OperationProgressProjection": ["sequence", "overall", "phase"],
@@ -17,4 +17,4 @@ export const OPERATION_FIELDS: Record<string, readonly string[]> = {
17
17
  "OperationRejection": ["code", "detail", "refreshRequired"],
18
18
  "OperationCancellationReceiptProjection": ["operationId", "outcome", "previousState", "committedState", "previousOperationRevision", "committedOperationRevision", "previousCatalogueRevision", "committedCatalogueRevision", "evicted"],
19
19
  "OperationChangedEvent": ["protocolVersion", "requestId", "authority", "previousCatalogueRevision", "committedCatalogueRevision", "operationId", "kind"],
20
- };
20
+ } as const;
@@ -4,6 +4,9 @@ import type {
4
4
  OperationMutationCommand,
5
5
  OperationRequestId,
6
6
  OperationSnapshotQuery,
7
+ OperationSnapshotResponse,
8
+ OperationMutationResult,
9
+ OperationCancellationResult,
7
10
  } from "./generated/protocol.ts";
8
11
 
9
12
  export type OperationUnlisten = () => void | Promise<void>;
@@ -12,18 +15,16 @@ export interface OperationPort {
12
15
  snapshot(query: OperationSnapshotQuery): Promise<unknown>;
13
16
  mutate(command: OperationMutationCommand): Promise<unknown>;
14
17
  cancel(command: OperationCancellationCommand): Promise<unknown>;
15
- listen?(
16
- listener: (event: unknown) => void,
17
- ): OperationUnlisten | Promise<OperationUnlisten>;
18
+ listen?:
19
+ | ((listener: (event: unknown) => void) => OperationUnlisten | Promise<OperationUnlisten>)
20
+ | undefined;
18
21
  nextRequestId(): OperationRequestId;
19
22
  }
20
23
 
21
24
  export interface CheckedOperationPort {
22
- snapshot(query: OperationSnapshotQuery): Promise<import("./generated/protocol.ts").OperationSnapshotResponse>;
23
- mutate(command: OperationMutationCommand): Promise<import("./generated/protocol.ts").OperationMutationResult>;
24
- cancel(command: OperationCancellationCommand): Promise<import("./generated/protocol.ts").OperationCancellationResult>;
25
- listen?(
26
- listener: (event: OperationChangedEvent) => void,
27
- ): Promise<OperationUnlisten>;
25
+ snapshot(query: OperationSnapshotQuery): Promise<OperationSnapshotResponse>;
26
+ mutate(command: OperationMutationCommand): Promise<OperationMutationResult>;
27
+ cancel(command: OperationCancellationCommand): Promise<OperationCancellationResult>;
28
+ listen?(listener: (event: OperationChangedEvent) => void): Promise<OperationUnlisten>;
28
29
  nextRequestId(): OperationRequestId;
29
30
  }
@@ -1,3 +1,4 @@
1
+ import { wireCopy } from "../core/wire-copy.ts";
1
2
  import type {
2
3
  OperationCancellationCommand,
3
4
  OperationMutationCommand,
@@ -13,27 +14,25 @@ export class SerializedOperationPort implements OperationPort {
13
14
  }
14
15
 
15
16
  async snapshot(query: OperationSnapshotQuery): Promise<unknown> {
16
- return clone(await this.#inner.snapshot(clone(query)));
17
+ return wireCopy(await this.#inner.snapshot(wireCopy(query)));
17
18
  }
18
19
 
19
20
  async mutate(command: OperationMutationCommand): Promise<unknown> {
20
- return clone(await this.#inner.mutate(clone(command)));
21
+ return wireCopy(await this.#inner.mutate(wireCopy(command)));
21
22
  }
22
23
 
23
24
  async cancel(command: OperationCancellationCommand): Promise<unknown> {
24
- return clone(await this.#inner.cancel(clone(command)));
25
+ return wireCopy(await this.#inner.cancel(wireCopy(command)));
25
26
  }
26
27
 
27
28
  async listen(listener: (event: unknown) => void): Promise<OperationUnlisten> {
28
- if (this.#inner.listen === undefined) return () => {};
29
- return this.#inner.listen((event) => listener(clone(event)));
29
+ if (this.#inner.listen === undefined) {
30
+ return () => {};
31
+ }
32
+ return this.#inner.listen((event) => listener(wireCopy(event)));
30
33
  }
31
34
 
32
35
  nextRequestId(): string {
33
36
  return this.#inner.nextRequestId();
34
37
  }
35
38
  }
36
-
37
- function clone<Value>(value: Value): Value {
38
- return JSON.parse(JSON.stringify(value)) as Value;
39
- }
@@ -1,5 +1,9 @@
1
+ import { isRecord } from "../core/record.ts";
1
2
  import { OPERATION_FIELDS } from "./generated/fields.ts";
2
- import { OPERATION_VARIANT_FIELDS, OPERATION_VARIANT_FIELDS_DISCRIMINANTS } from "./generated/variant-fields.ts";
3
+ import {
4
+ OPERATION_VARIANT_FIELDS,
5
+ OPERATION_VARIANT_FIELDS_DISCRIMINANTS,
6
+ } from "./generated/variant-fields.ts";
3
7
  import {
4
8
  OPERATION_CANCELLATION_OUTCOMES,
5
9
  OPERATION_CANCELLATION_STATUSES,
@@ -12,27 +16,26 @@ import {
12
16
  OPERATION_PROTOCOL_VERSION,
13
17
  OPERATION_REJECTION_CODES,
14
18
  OPERATION_STATES,
15
- type OperationCancellationCommand,
16
- type OperationCancellationResult,
17
- type OperationChangedEvent,
18
- type OperationEntryProjection,
19
- type OperationMutationCommand,
20
- type OperationMutationResult,
21
- type OperationSnapshot,
22
- type OperationSnapshotQuery,
23
- type OperationSnapshotResponse,
19
+ } from "./generated/protocol.ts";
20
+ import type {
21
+ OperationCancellationCommand,
22
+ OperationCancellationResult,
23
+ OperationChangedEvent,
24
+ OperationEntryProjection,
25
+ OperationMutationCommand,
26
+ OperationMutationResult,
27
+ OperationSnapshot,
28
+ OperationSnapshotQuery,
29
+ OperationSnapshotResponse,
24
30
  } from "./generated/protocol.ts";
25
31
 
26
- const FORBIDDEN_PAYLOAD_KEYS = new Set([
27
- "payload",
28
- "result",
29
- "artifact",
30
- "report",
31
- "log",
32
- ]);
32
+ const FORBIDDEN_PAYLOAD_KEYS = new Set(["payload", "result", "artifact", "report", "log"]);
33
33
 
34
34
  export class OperationProtocolValidationError extends Error {
35
- constructor(readonly path: string, message: string) {
35
+ constructor(
36
+ readonly path: string,
37
+ message: string,
38
+ ) {
36
39
  super(`incompatible operation protocol at ${path}: ${message}`);
37
40
  this.name = "OperationProtocolValidationError";
38
41
  }
@@ -55,26 +58,28 @@ export function assertValidOperationSnapshotResponse(
55
58
  assertValidOperationSnapshot(object.snapshot);
56
59
  }
57
60
 
58
- export function assertValidOperationSnapshot(
59
- value: unknown,
60
- ): asserts value is OperationSnapshot {
61
+ export function assertValidOperationSnapshot(value: unknown): asserts value is OperationSnapshot {
61
62
  assertPayloadFree(value);
62
63
  const object = exactObject(value, "$", OPERATION_FIELDS.OperationSnapshot);
63
64
  protocol(object.protocolVersion, "$.protocolVersion");
64
65
  authority(object.authority, "$.authority");
65
66
  natural(object.catalogueRevision, "$.catalogueRevision");
66
67
  boolean(object.closed, "$.closed");
67
- const limits = exactObject(object.limits, "$.limits", OPERATION_FIELDS.OperationCatalogueLimitsProjection);
68
+ const limits = exactObject(
69
+ object.limits,
70
+ "$.limits",
71
+ OPERATION_FIELDS.OperationCatalogueLimitsProjection,
72
+ );
68
73
  natural(limits.maximumActiveOperations, "$.limits.maximumActiveOperations");
69
74
  natural(limits.maximumTerminalOperations, "$.limits.maximumTerminalOperations");
70
75
  natural(limits.maximumTerminalEncodedWeight, "$.limits.maximumTerminalEncodedWeight");
71
76
  natural(object.terminalEvictionCount, "$.terminalEvictionCount");
72
77
  natural(object.retainedTerminalEncodedWeight, "$.retainedTerminalEncodedWeight");
73
78
  array(object.active, "$.active").forEach((entry, index) =>
74
- operationEntry(entry, `$.active[${index}]`)
79
+ operationEntry(entry, `$.active[${index}]`),
75
80
  );
76
81
  array(object.recent, "$.recent").forEach((entry, index) =>
77
- operationEntry(entry, `$.recent[${index}]`)
82
+ operationEntry(entry, `$.recent[${index}]`),
78
83
  );
79
84
  }
80
85
 
@@ -96,14 +101,20 @@ export function assertValidOperationMutationCommand(
96
101
  nullableText(object.scopeId, "$.scopeId");
97
102
  text(object.label, "$.label");
98
103
  member(object.initialState, OPERATION_STATES, "$.initialState");
99
- member(object.cancellationSupport, ["supported", "unsupported"] as const, "$.cancellationSupport");
104
+ member(
105
+ object.cancellationSupport,
106
+ ["supported", "unsupported"] as const,
107
+ "$.cancellationSupport",
108
+ );
100
109
  nullableText(object.retryOf, "$.retryOf");
101
110
  break;
102
111
  case "progress":
103
112
  exactKeys(object, "$", variantKeys("OperationMutationCommand", object, "$"));
104
113
  operationTarget(object);
105
114
  progressOverall(object.overall, "$.overall");
106
- if (object.phase !== null) phase(object.phase, "$.phase");
115
+ if (object.phase !== null) {
116
+ phase(object.phase, "$.phase");
117
+ }
107
118
  break;
108
119
  case "transition":
109
120
  exactKeys(object, "$", variantKeys("OperationMutationCommand", object, "$"));
@@ -113,10 +124,17 @@ export function assertValidOperationMutationCommand(
113
124
  case "changeRetention":
114
125
  exactKeys(object, "$", variantKeys("OperationMutationCommand", object, "$"));
115
126
  natural(object.expectedCatalogueRevision, "$.expectedCatalogueRevision");
127
+ // oxlint-disable-next-line quality/no-known-value-widening -- Runtime validation also covers structurally typed callers and untrusted wire values.
116
128
  assertValidOperationSnapshot({
117
- protocolVersion: 1, authority: object.authority, catalogueRevision: 0,
118
- closed: false, limits: object.limits, terminalEvictionCount: 0,
119
- retainedTerminalEncodedWeight: 0, active: [], recent: [],
129
+ protocolVersion: 1,
130
+ authority: object.authority,
131
+ catalogueRevision: 0,
132
+ closed: false,
133
+ limits: object.limits,
134
+ terminalEvictionCount: 0,
135
+ retainedTerminalEncodedWeight: 0,
136
+ active: [],
137
+ recent: [],
120
138
  });
121
139
  break;
122
140
  case "dismiss":
@@ -126,7 +144,9 @@ export function assertValidOperationMutationCommand(
126
144
  case "teardown":
127
145
  exactKeys(object, "$", variantKeys("OperationMutationCommand", object, "$"));
128
146
  natural(object.expectedCatalogueRevision, "$.expectedCatalogueRevision");
129
- array(object.resolutions, "$.resolutions").forEach((resolution, index) => teardownResolution(resolution, `$.resolutions[${index}]`));
147
+ array(object.resolutions, "$.resolutions").forEach((resolution, index) =>
148
+ teardownResolution(resolution, `$.resolutions[${index}]`),
149
+ );
130
150
  break;
131
151
  }
132
152
  }
@@ -145,8 +165,7 @@ export function assertValidOperationCancellationCommand(
145
165
  export function assertValidOperationMutationResult(
146
166
  value: unknown,
147
167
  ): asserts value is OperationMutationResult {
148
- resultBase(value, OPERATION_MUTATION_STATUSES);
149
- const object = value as Record<string, unknown>;
168
+ const object = resultBase(value, OPERATION_MUTATION_STATUSES);
150
169
  if (object.status === "committed") {
151
170
  exactKeys(object, "$", variantKeys("OperationMutationResult", object, "$"));
152
171
  mutationReceipt(object.receipt, "$.receipt");
@@ -159,8 +178,7 @@ export function assertValidOperationMutationResult(
159
178
  export function assertValidOperationCancellationResult(
160
179
  value: unknown,
161
180
  ): asserts value is OperationCancellationResult {
162
- resultBase(value, OPERATION_CANCELLATION_STATUSES);
163
- const object = value as Record<string, unknown>;
181
+ const object = resultBase(value, OPERATION_CANCELLATION_STATUSES);
164
182
  if (object.status === "committed") {
165
183
  exactKeys(object, "$", variantKeys("OperationCancellationResult", object, "$"));
166
184
  cancellationReceipt(object.receipt, "$.receipt");
@@ -189,12 +207,13 @@ export function assertPayloadFree(value: unknown): void {
189
207
  visit(value, "$", new Set());
190
208
  }
191
209
 
192
- function resultBase(value: unknown, statuses: readonly string[]): void {
210
+ function resultBase(value: unknown, statuses: ReadonlyArray<string>): Record<string, unknown> {
193
211
  assertPayloadFree(value);
194
212
  const object = record(value, "$");
195
213
  member(object.status, statuses, "$.status");
196
214
  text(object.requestId, "$.requestId");
197
215
  assertValidOperationSnapshot(object.snapshot);
216
+ return object;
198
217
  }
199
218
 
200
219
  function operationEntry(value: unknown, path: string): asserts value is OperationEntryProjection {
@@ -204,23 +223,35 @@ function operationEntry(value: unknown, path: string): asserts value is Operatio
204
223
  text(object.kindId, `${path}.kindId`);
205
224
  nullableText(object.scopeId, `${path}.scopeId`);
206
225
  text(object.label, `${path}.label`);
207
- member(object.cancellationSupport, ["supported", "unsupported"] as const, `${path}.cancellationSupport`);
226
+ member(
227
+ object.cancellationSupport,
228
+ ["supported", "unsupported"] as const,
229
+ `${path}.cancellationSupport`,
230
+ );
208
231
  nullableText(object.retryOf, `${path}.retryOf`);
209
232
  natural(object.sequence, `${path}.sequence`);
210
233
  natural(object.revision, `${path}.revision`);
211
234
  natural(object.lastChangedCatalogueRevision, `${path}.lastChangedCatalogueRevision`);
212
235
  member(object.state, OPERATION_STATES, `${path}.state`);
213
- const progress = exactObject(object.progress, `${path}.progress`, OPERATION_FIELDS.OperationProgressProjection);
236
+ const progress = exactObject(
237
+ object.progress,
238
+ `${path}.progress`,
239
+ OPERATION_FIELDS.OperationProgressProjection,
240
+ );
214
241
  natural(progress.sequence, `${path}.progress.sequence`);
215
242
  progressOverall(progress.overall, `${path}.progress.overall`);
216
- if (progress.phase !== null) phase(progress.phase, `${path}.progress.phase`);
243
+ if (progress.phase !== null) {
244
+ phase(progress.phase, `${path}.progress.phase`);
245
+ }
217
246
  natural(object.encodedMetadataWeight, `${path}.encodedMetadataWeight`);
218
247
  }
219
248
 
220
249
  function progressOverall(value: unknown, path: string): void {
221
250
  const object = record(value, path);
222
251
  member(object.kind, OPERATION_PROGRESS_KINDS, `${path}.kind`);
223
- if (object.kind === "indeterminate") exactKeys(object, path, variantKeys("OperationOverallProgressProjection", object, path));
252
+ if (object.kind === "indeterminate") {
253
+ exactKeys(object, path, variantKeys("OperationOverallProgressProjection", object, path));
254
+ }
224
255
  if (object.kind === "units") {
225
256
  exactKeys(object, path, variantKeys("OperationOverallProgressProjection", object, path));
226
257
  finite(object.completed, `${path}.completed`);
@@ -270,16 +301,23 @@ function mutationReceipt(value: unknown, path: string): void {
270
301
  operationEntry(object.operation, `${path}.operation`);
271
302
  catalogueRevisionPair(object, path);
272
303
  break;
273
- case "progressed":
304
+ case "progressed": {
274
305
  exactKeys(object, path, variantKeys("OperationMutationReceiptProjection", object, path));
275
306
  operationRevisionPair(object, path);
276
307
  natural(object.previousProgressSequence, `${path}.previousProgressSequence`);
277
- const progress = exactObject(object.committedProgress, `${path}.committedProgress`, OPERATION_FIELDS.OperationProgressProjection);
308
+ const progress = exactObject(
309
+ object.committedProgress,
310
+ `${path}.committedProgress`,
311
+ OPERATION_FIELDS.OperationProgressProjection,
312
+ );
278
313
  natural(progress.sequence, `${path}.committedProgress.sequence`);
279
314
  progressOverall(progress.overall, `${path}.committedProgress.overall`);
280
- if (progress.phase !== null) phase(progress.phase, `${path}.committedProgress.phase`);
315
+ if (progress.phase !== null) {
316
+ phase(progress.phase, `${path}.committedProgress.phase`);
317
+ }
281
318
  catalogueRevisionPair(object, path);
282
319
  break;
320
+ }
283
321
  case "transitioned":
284
322
  exactKeys(object, path, variantKeys("OperationMutationReceiptProjection", object, path));
285
323
  member(object.previousState, OPERATION_STATES, `${path}.previousState`);
@@ -304,7 +342,9 @@ function mutationReceipt(value: unknown, path: string): void {
304
342
  case "tornDown":
305
343
  exactKeys(object, path, variantKeys("OperationMutationReceiptProjection", object, path));
306
344
  catalogueRevisionPair(object, path);
307
- array(object.outcomes, `${path}.outcomes`).forEach((outcome, index) => teardownOutcome(outcome, `${path}.outcomes[${index}]`));
345
+ array(object.outcomes, `${path}.outcomes`).forEach((outcome, index) =>
346
+ teardownOutcome(outcome, `${path}.outcomes[${index}]`),
347
+ );
308
348
  removals(object.evicted, `${path}.evicted`);
309
349
  break;
310
350
  }
@@ -358,7 +398,9 @@ function teardownOutcome(value: unknown, path: string): void {
358
398
  }
359
399
  text(object.operationId, `${path}.operationId`);
360
400
  natural(object.previousOperationRevision, `${path}.previousOperationRevision`);
361
- if (object.kind === "completed") natural(object.committedOperationRevision, `${path}.committedOperationRevision`);
401
+ if (object.kind === "completed") {
402
+ natural(object.committedOperationRevision, `${path}.committedOperationRevision`);
403
+ }
362
404
  }
363
405
 
364
406
  function limits(value: unknown, path: string): void {
@@ -388,21 +430,33 @@ function authority(value: unknown, path: string): void {
388
430
  const object = exactObject(value, path, OPERATION_FIELDS.OperationAuthorityProjection);
389
431
  text(object.authorityId, `${path}.authorityId`);
390
432
  natural(object.authorityEpoch, `${path}.authorityEpoch`);
391
- if (object.authorityEpoch === 0) fail(`${path}.authorityEpoch`, "must be nonzero");
433
+ if (object.authorityEpoch === 0) {
434
+ fail(`${path}.authorityEpoch`, "must be nonzero");
435
+ }
392
436
  }
393
437
 
394
438
  function protocol(value: unknown, path: string): void {
395
439
  natural(value, path);
396
- if (value !== OPERATION_PROTOCOL_VERSION) fail(path, `expected ${OPERATION_PROTOCOL_VERSION}`);
440
+ if (value !== OPERATION_PROTOCOL_VERSION) {
441
+ fail(path, `expected ${OPERATION_PROTOCOL_VERSION}`);
442
+ }
397
443
  }
398
444
 
399
- function exactObject(value: unknown, path: string, keys: readonly string[]): Record<string, unknown> {
445
+ function exactObject(
446
+ value: unknown,
447
+ path: string,
448
+ keys: ReadonlyArray<string>,
449
+ ): Record<string, unknown> {
400
450
  const object = record(value, path);
401
451
  exactKeys(object, path, keys);
402
452
  return object;
403
453
  }
404
454
 
405
- function exactKeys(object: Record<string, unknown>, path: string, keys: readonly string[]): void {
455
+ function exactKeys(
456
+ object: Record<string, unknown>,
457
+ path: string,
458
+ keys: ReadonlyArray<string>,
459
+ ): void {
406
460
  const actual = Object.keys(object).sort();
407
461
  const expected = [...keys].sort();
408
462
  if (actual.length !== expected.length || actual.some((key, index) => key !== expected[index])) {
@@ -411,48 +465,74 @@ function exactKeys(object: Record<string, unknown>, path: string, keys: readonly
411
465
  }
412
466
 
413
467
  function record(value: unknown, path: string): Record<string, unknown> {
414
- if (typeof value !== "object" || value === null || Array.isArray(value)) fail(path, "expected object");
415
- return value as Record<string, unknown>;
468
+ if (!isRecord(value)) {
469
+ fail(path, "expected object");
470
+ }
471
+ return value;
416
472
  }
417
473
 
418
- function array(value: unknown, path: string): unknown[] {
419
- if (!Array.isArray(value)) fail(path, "expected array");
474
+ function array(value: unknown, path: string): Array<unknown> {
475
+ if (!Array.isArray(value)) {
476
+ fail(path, "expected array");
477
+ }
420
478
  return value;
421
479
  }
422
480
 
423
481
  function text(value: unknown, path: string): asserts value is string {
424
- if (typeof value !== "string" || value.length === 0) fail(path, "expected non-empty string");
482
+ if (typeof value !== "string" || value.length === 0) {
483
+ fail(path, "expected non-empty string");
484
+ }
425
485
  }
426
486
 
427
487
  function nullableText(value: unknown, path: string): void {
428
- if (value !== null) text(value, path);
488
+ if (value !== null) {
489
+ text(value, path);
490
+ }
429
491
  }
430
492
 
431
493
  function natural(value: unknown, path: string): asserts value is number {
432
- if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) fail(path, "expected non-negative safe integer");
494
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
495
+ fail(path, "expected non-negative safe integer");
496
+ }
433
497
  }
434
498
 
435
499
  function finite(value: unknown, path: string): asserts value is number {
436
- if (typeof value !== "number" || !Number.isFinite(value)) fail(path, "expected finite number");
500
+ if (typeof value !== "number" || !Number.isFinite(value)) {
501
+ fail(path, "expected finite number");
502
+ }
437
503
  }
438
504
 
439
505
  function boolean(value: unknown, path: string): asserts value is boolean {
440
- if (typeof value !== "boolean") fail(path, "expected boolean");
506
+ if (typeof value !== "boolean") {
507
+ fail(path, "expected boolean");
508
+ }
441
509
  }
442
510
 
443
- function member(value: unknown, values: readonly string[], path: string): asserts value is string {
444
- if (typeof value !== "string" || !values.includes(value)) fail(path, `expected one of ${values.join(",")}`);
511
+ function member(
512
+ value: unknown,
513
+ values: ReadonlyArray<string>,
514
+ path: string,
515
+ ): asserts value is string {
516
+ if (typeof value !== "string" || !values.includes(value)) {
517
+ fail(path, `expected one of ${values.join(",")}`);
518
+ }
445
519
  }
446
520
 
447
521
  function visit(value: unknown, path: string, seen: Set<object>): void {
448
- if (typeof value !== "object" || value === null) return;
449
- if (seen.has(value)) fail(path, "cyclic value");
522
+ if (typeof value !== "object" || value === null) {
523
+ return;
524
+ }
525
+ if (seen.has(value)) {
526
+ fail(path, "cyclic value");
527
+ }
450
528
  seen.add(value);
451
529
  if (Array.isArray(value)) {
452
530
  value.forEach((entry, index) => visit(entry, `${path}[${index}]`, seen));
453
531
  } else {
454
532
  for (const [key, entry] of Object.entries(value)) {
455
- if (FORBIDDEN_PAYLOAD_KEYS.has(key)) fail(`${path}.${key}`, "product payload field is forbidden");
533
+ if (FORBIDDEN_PAYLOAD_KEYS.has(key)) {
534
+ fail(`${path}.${key}`, "product payload field is forbidden");
535
+ }
456
536
  visit(entry, `${path}.${key}`, seen);
457
537
  }
458
538
  }
@@ -471,9 +551,16 @@ function fail(path: string, message: string): never {
471
551
  * discriminant first. Failing loudly beats a hand-written list, which is what
472
552
  * these were and what drifted in `history-tree`.
473
553
  */
474
- function variantKeys(type: string, object: Record<string, unknown>, path: string): readonly string[] {
554
+ function variantKeys(
555
+ type: string,
556
+ object: Record<string, unknown>,
557
+ path: string,
558
+ ): ReadonlyArray<string> {
475
559
  const discriminant = object[OPERATION_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
476
- const keys = OPERATION_VARIANT_FIELDS[type]?.[discriminant as string];
477
- if (keys === undefined) fail(path, `no generated fields for ${type}.${String(discriminant)}`);
560
+ const keys =
561
+ typeof discriminant === "string" ? OPERATION_VARIANT_FIELDS[type]?.[discriminant] : undefined;
562
+ if (keys === undefined) {
563
+ fail(path, `no generated fields for ${type}.${String(discriminant)}`);
564
+ }
478
565
  return keys;
479
566
  }