@inflatable-cookie/longhorn 0.5.0 → 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,3 +1,4 @@
1
+ import { isRecord } from "../core/record.ts";
1
2
  import {
2
3
  TRANSFER_VARIANT_FIELDS,
3
4
  TRANSFER_VARIANT_FIELDS_DISCRIMINANTS,
@@ -13,16 +14,19 @@ import {
13
14
  TRANSFER_PROTOCOL_VERSION,
14
15
  TRANSFER_SESSION_RESPONSE_STATUSES,
15
16
  TRANSFER_TARGET_BINDING_KINDS,
16
- type PanelTransferResponse,
17
- type TransferAbort,
18
- type TransferCancelResponse,
19
- type TransferClientSnapshot,
20
- type TransferCommitSelector,
21
- type TransferLeaseResponse,
22
- type TransferSessionResponse,
23
- type TransferTargetBinding,
17
+ } from "./generated/protocol.ts";
18
+ import type {
19
+ PanelTransferResponse,
20
+ TransferAbort,
21
+ TransferCancelResponse,
22
+ TransferClientSnapshot,
23
+ TransferCommitSelector,
24
+ TransferLeaseResponse,
25
+ TransferSessionResponse,
26
+ TransferTargetBinding,
24
27
  } from "./generated/protocol.ts";
25
28
  import { TRANSFER_FIELDS } from "./generated/fields.ts";
29
+ import { assertValidLayoutMutationRejectionCode } from "@inflatable-cookie/longhorn/layout";
26
30
 
27
31
  export type TransferProtocolValidationCode =
28
32
  | "unsupported_protocol_version"
@@ -53,10 +57,7 @@ export function assertTransferProtocolVersion(
53
57
  version: unknown,
54
58
  ): asserts version is typeof TRANSFER_PROTOCOL_VERSION {
55
59
  if (version !== TRANSFER_PROTOCOL_VERSION) {
56
- throw new TransferProtocolValidationError(
57
- "unsupported_protocol_version",
58
- version,
59
- );
60
+ throw new TransferProtocolValidationError("unsupported_protocol_version", version);
60
61
  }
61
62
  }
62
63
 
@@ -64,11 +65,7 @@ export function assertValidTransferTargetBinding(
64
65
  value: unknown,
65
66
  ): asserts value is TransferTargetBinding {
66
67
  const candidate = record(value);
67
- assertKnown(
68
- candidate.kind,
69
- TRANSFER_TARGET_BINDING_KINDS,
70
- "unknown_target_binding",
71
- );
68
+ assertKnown(candidate.kind, TRANSFER_TARGET_BINDING_KINDS, "unknown_target_binding");
72
69
  record(candidate, variantKeys("TransferTargetBinding", candidate));
73
70
  }
74
71
 
@@ -77,16 +74,8 @@ export function assertValidTransferClientSnapshot(
77
74
  ): asserts value is TransferClientSnapshot {
78
75
  const snapshot = record(value, TRANSFER_FIELDS.TransferClientSnapshot);
79
76
  assertTransferProtocolVersion(snapshot.protocol_version);
80
- if (
81
- typeof snapshot.client_id !== "string" ||
82
- !unsignedInteger(snapshot.client_epoch) ||
83
- (snapshot.current_lease_generation !== null &&
84
- !unsignedInteger(snapshot.current_lease_generation))
85
- ) {
86
- throw new TransferProtocolValidationError(
87
- "invalid_client_snapshot",
88
- value,
89
- );
77
+ if (typeof snapshot.client_id !== "string" || !unsignedInteger(snapshot.client_epoch)) {
78
+ throw new TransferProtocolValidationError("invalid_client_snapshot", value);
90
79
  }
91
80
  }
92
81
 
@@ -94,38 +83,23 @@ export function assertValidTransferCommitSelector(
94
83
  value: unknown,
95
84
  ): asserts value is TransferCommitSelector {
96
85
  const candidate = record(value);
97
- assertKnown(
98
- candidate.kind,
99
- TRANSFER_COMMIT_SELECTOR_KINDS,
100
- "unknown_commit_selector",
101
- );
86
+ assertKnown(candidate.kind, TRANSFER_COMMIT_SELECTOR_KINDS, "unknown_commit_selector");
102
87
  record(candidate, variantKeys("TransferCommitSelector", candidate));
103
88
  }
104
89
 
105
- export function assertValidTransferAbort(
106
- value: unknown,
107
- ): asserts value is TransferAbort {
90
+ export function assertValidTransferAbort(value: unknown): asserts value is TransferAbort {
108
91
  const abort = record(value, TRANSFER_FIELDS.TransferAbort);
109
92
  assertTransferProtocolVersion(abort.protocol_version);
110
93
  const source = record(abort.source);
111
- assertKnown(
112
- source.domain,
113
- TRANSFER_ABORT_DOMAINS,
114
- "unknown_abort_domain",
115
- );
94
+ assertKnown(source.domain, TRANSFER_ABORT_DOMAINS, "unknown_abort_domain");
116
95
  record(source, variantKeys("TransferAbortSource", source));
117
96
  if (source.domain === "transfer") {
118
- assertKnown(
119
- source.code,
120
- TRANSFER_ERROR_CODES,
121
- "unknown_transfer_error_code",
122
- );
97
+ assertKnown(source.code, TRANSFER_ERROR_CODES, "unknown_transfer_error_code");
123
98
  } else {
124
- assertKnown(
125
- source.code,
126
- PANEL_TRANSFER_ERROR_CODES,
127
- "unknown_panel_error_code",
128
- );
99
+ assertKnown(source.code, PANEL_TRANSFER_ERROR_CODES, "unknown_panel_error_code");
100
+ }
101
+ if (abort.layout_code !== null) {
102
+ assertValidLayoutMutationRejectionCode(abort.layout_code);
129
103
  }
130
104
  }
131
105
 
@@ -170,9 +144,7 @@ export function assertValidTransferCancelResponse(
170
144
  "TransferCancelResponse",
171
145
  );
172
146
  if (response.status === "cancelled") {
173
- assertTransferProtocolVersion(
174
- record(response.cancellation).protocol_version,
175
- );
147
+ assertTransferProtocolVersion(record(response.cancellation).protocol_version);
176
148
  } else {
177
149
  assertValidTransferAbort(response.abort);
178
150
  }
@@ -189,9 +161,8 @@ export function assertValidPanelTransferResponse(
189
161
  if (response.status === "committed") {
190
162
  const completion = record(response.completion, TRANSFER_FIELDS.PanelTransferCompletion);
191
163
  assertTransferProtocolVersion(completion.protocol_version);
192
- assertValidTransferTargetBinding(
193
- record(record(completion.target).binding),
194
- );
164
+ // oxlint-disable-next-line quality/no-known-value-widening -- Runtime validation also covers structurally typed callers and untrusted wire values.
165
+ assertValidTransferTargetBinding(record(record(completion.target).binding));
195
166
  } else {
196
167
  assertValidTransferAbort(response.abort);
197
168
  }
@@ -199,7 +170,7 @@ export function assertValidPanelTransferResponse(
199
170
 
200
171
  function responseWithStatus(
201
172
  value: unknown,
202
- statuses: readonly string[],
173
+ statuses: ReadonlyArray<string>,
203
174
  type: string,
204
175
  ): Record<string, unknown> {
205
176
  const response = record(value);
@@ -216,12 +187,10 @@ function responseWithStatus(
216
187
  * A missing entry means the generator failed to read the union; every caller
217
188
  * runs `assertKnown` over the discriminant above this.
218
189
  */
219
- function variantKeys(
220
- type: string,
221
- value: Record<string, unknown>,
222
- ): readonly string[] {
190
+ function variantKeys(type: string, value: Record<string, unknown>): ReadonlyArray<string> {
223
191
  const discriminant = value[TRANSFER_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
224
- const keys = TRANSFER_VARIANT_FIELDS[type]?.[discriminant as string];
192
+ const keys =
193
+ typeof discriminant === "string" ? TRANSFER_VARIANT_FIELDS[type]?.[discriminant] : undefined;
225
194
  if (keys === undefined) {
226
195
  throw new TransferProtocolValidationError("unknown_response_status", {
227
196
  type,
@@ -233,7 +202,7 @@ function variantKeys(
233
202
 
234
203
  function assertKnown(
235
204
  value: unknown,
236
- known: readonly string[],
205
+ known: ReadonlyArray<string>,
237
206
  code: TransferProtocolValidationCode,
238
207
  ): asserts value is string {
239
208
  if (typeof value !== "string" || !known.includes(value)) {
@@ -249,15 +218,14 @@ function assertKnown(
249
218
  * Passing no list keeps shape-only behaviour for the tagged unions, whose
250
219
  * allowed keys depend on their discriminant and so are not one flat set.
251
220
  */
252
- function record(
253
- value: unknown,
254
- allowed?: readonly string[],
255
- ): Record<string, unknown> {
256
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
221
+ function record(value: unknown, allowed?: ReadonlyArray<string>): Record<string, unknown> {
222
+ if (!isRecord(value)) {
257
223
  throw new TransferProtocolValidationError("invalid_object", value);
258
224
  }
259
- const result = value as Record<string, unknown>;
260
- if (allowed === undefined) return result;
225
+ const result = value;
226
+ if (allowed === undefined) {
227
+ return result;
228
+ }
261
229
 
262
230
  const permitted = new Set(allowed);
263
231
  for (const key of Object.keys(result)) {
@@ -274,9 +242,5 @@ function record(
274
242
  }
275
243
 
276
244
  function unsignedInteger(value: unknown): value is number {
277
- return (
278
- typeof value === "number" &&
279
- Number.isSafeInteger(value) &&
280
- value >= 0
281
- );
245
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
282
246
  }
@@ -26,13 +26,63 @@ import {
26
26
 
27
27
  export class UpdateClient implements CheckedUpdatePort {
28
28
  constructor(readonly port: UpdatePort) {}
29
- async snapshot(): Promise<UpdateSnapshot> { const value = await this.port.snapshot(); assertUpdateSnapshot(value); return value; }
30
- async check(command: UpdateCheckCommand): Promise<UpdateOutcomeProjection> { assertUpdateCheckCommand(command); const value = await this.port.check(command); assertUpdateOutcome(value); return value; }
31
- async selectChannel(command: UpdateSelectChannelCommand): Promise<UpdateOutcomeProjection> { assertUpdateSelectChannelCommand(command); const value = await this.port.selectChannel(command); assertUpdateOutcome(value); return value; }
32
- async defer(command: UpdateDeferCommand): Promise<UpdateOutcomeProjection> { assertUpdateDeferCommand(command); const value = await this.port.defer(command); assertUpdateOutcome(value); return value; }
33
- async prepare(command: UpdatePrepareCommand): Promise<UpdateOutcomeProjection> { assertUpdatePrepareCommand(command); const value = await this.port.prepare(command); assertUpdateOutcome(value); return value; }
34
- async apply(command: UpdateApplyCommand): Promise<UpdateOutcomeProjection> { assertUpdateApplyCommand(command); const value = await this.port.apply(command); assertUpdateOutcome(value); return value; }
35
- async cancel(command: UpdateCancelCommand): Promise<UpdateOutcomeProjection> { assertUpdateCancelCommand(command); const value = await this.port.cancel(command); assertUpdateOutcome(value); return value; }
36
- async listen(listener: (event: UpdateChangedEvent) => void): Promise<UpdateUnlisten> { if (this.port.listen === undefined) return () => {}; return this.port.listen((value) => { assertUpdateChangedEvent(value); listener(value); }); }
37
- async listenProgress(listener: (event: UpdateProgressEvent) => void): Promise<UpdateUnlisten> { if (this.port.listenProgress === undefined) return () => {}; return this.port.listenProgress((value) => { assertUpdateProgressEvent(value); listener(value); }); }
29
+ async snapshot(): Promise<UpdateSnapshot> {
30
+ const value = await this.port.snapshot();
31
+ assertUpdateSnapshot(value);
32
+ return value;
33
+ }
34
+ async check(command: UpdateCheckCommand): Promise<UpdateOutcomeProjection> {
35
+ assertUpdateCheckCommand(command);
36
+ const value = await this.port.check(command);
37
+ assertUpdateOutcome(value);
38
+ return value;
39
+ }
40
+ async selectChannel(command: UpdateSelectChannelCommand): Promise<UpdateOutcomeProjection> {
41
+ assertUpdateSelectChannelCommand(command);
42
+ const value = await this.port.selectChannel(command);
43
+ assertUpdateOutcome(value);
44
+ return value;
45
+ }
46
+ async defer(command: UpdateDeferCommand): Promise<UpdateOutcomeProjection> {
47
+ assertUpdateDeferCommand(command);
48
+ const value = await this.port.defer(command);
49
+ assertUpdateOutcome(value);
50
+ return value;
51
+ }
52
+ async prepare(command: UpdatePrepareCommand): Promise<UpdateOutcomeProjection> {
53
+ assertUpdatePrepareCommand(command);
54
+ const value = await this.port.prepare(command);
55
+ assertUpdateOutcome(value);
56
+ return value;
57
+ }
58
+ async apply(command: UpdateApplyCommand): Promise<UpdateOutcomeProjection> {
59
+ assertUpdateApplyCommand(command);
60
+ const value = await this.port.apply(command);
61
+ assertUpdateOutcome(value);
62
+ return value;
63
+ }
64
+ async cancel(command: UpdateCancelCommand): Promise<UpdateOutcomeProjection> {
65
+ assertUpdateCancelCommand(command);
66
+ const value = await this.port.cancel(command);
67
+ assertUpdateOutcome(value);
68
+ return value;
69
+ }
70
+ async listen(listener: (event: UpdateChangedEvent) => void): Promise<UpdateUnlisten> {
71
+ if (this.port.listen === undefined) {
72
+ return () => {};
73
+ }
74
+ return this.port.listen((value) => {
75
+ assertUpdateChangedEvent(value);
76
+ listener(value);
77
+ });
78
+ }
79
+ async listenProgress(listener: (event: UpdateProgressEvent) => void): Promise<UpdateUnlisten> {
80
+ if (this.port.listenProgress === undefined) {
81
+ return () => {};
82
+ }
83
+ return this.port.listenProgress((value) => {
84
+ assertUpdateProgressEvent(value);
85
+ listener(value);
86
+ });
87
+ }
38
88
  }
@@ -1,14 +1,14 @@
1
1
  import { UpdateClient } from "./client.ts";
2
- import {
3
- UPDATE_PROTOCOL_VERSION,
4
- type Channel,
5
- type DeferralCause,
6
- type UpdateAvailabilityProjection,
7
- type UpdateOutcomeProjection,
8
- type UpdateProgressProjection,
9
- type UpdateRejectionCode,
10
- type UpdateSnapshot,
11
- type UpdateStagedArtifactProjection,
2
+ import { UPDATE_PROTOCOL_VERSION } from "./generated/protocol.ts";
3
+ import type {
4
+ Channel,
5
+ DeferralCause,
6
+ UpdateAvailabilityProjection,
7
+ UpdateOutcomeProjection,
8
+ UpdateProgressProjection,
9
+ UpdateRejectionCode,
10
+ UpdateSnapshot,
11
+ UpdateStagedArtifactProjection,
12
12
  } from "./generated/protocol.ts";
13
13
  import type { UpdatePort, UpdateUnlisten } from "./ports.ts";
14
14
 
@@ -71,12 +71,12 @@ export class UpdateController {
71
71
  readonly #observers = new Set<() => void>();
72
72
  #status: UpdateControllerStatus = { kind: "idle" };
73
73
  #snapshot?: UpdateSnapshot;
74
- #lastRejection?: UpdateRejectionCode;
74
+ #lastRejection: UpdateRejectionCode | undefined;
75
75
  #started = false;
76
76
  #lifecycle = 0;
77
77
  #commandSequence = 0;
78
78
  #pending = false;
79
- #unlisten: UpdateUnlisten[] = [];
79
+ #unlisten: Array<UpdateUnlisten> = [];
80
80
  /**
81
81
  * The most recent live progress report.
82
82
  *
@@ -85,15 +85,21 @@ export class UpdateController {
85
85
  * said before the transfer began. It is cleared when the command that
86
86
  * started the transfer settles, so a stale report cannot outlive it.
87
87
  */
88
- #liveProgress?: UpdateProgressProjection;
88
+ #liveProgress: UpdateProgressProjection | undefined;
89
89
 
90
90
  constructor(options: UpdateControllerOptions) {
91
91
  this.#client = new UpdateClient(options.port);
92
92
  }
93
93
 
94
- get status(): UpdateControllerStatus { return this.#status; }
95
- get snapshot(): UpdateSnapshot | undefined { return this.#snapshot; }
96
- get availability(): UpdateAvailabilityProjection | undefined { return this.#snapshot?.availability; }
94
+ get status(): UpdateControllerStatus {
95
+ return this.#status;
96
+ }
97
+ get snapshot(): UpdateSnapshot | undefined {
98
+ return this.#snapshot;
99
+ }
100
+ get availability(): UpdateAvailabilityProjection | undefined {
101
+ return this.#snapshot?.availability;
102
+ }
97
103
  /**
98
104
  * What the transfer is doing right now.
99
105
  *
@@ -101,12 +107,22 @@ export class UpdateController {
101
107
  * fresh as the last commit and the transfer deliberately does not hold the
102
108
  * authority while it runs.
103
109
  */
104
- get progress(): UpdateProgressProjection | undefined { return this.#liveProgress ?? this.#snapshot?.progress; }
110
+ get progress(): UpdateProgressProjection | undefined {
111
+ return this.#liveProgress ?? this.#snapshot?.progress;
112
+ }
105
113
  /** The retained verified artifact, when one is staged. */
106
- get staged(): UpdateStagedArtifactProjection | undefined { return this.#snapshot?.staged ?? undefined; }
107
- get channel(): Channel | undefined { return this.#snapshot?.channel; }
108
- get installedVersion(): string | undefined { return this.#snapshot?.installedVersion; }
109
- get pending(): boolean { return this.#pending; }
114
+ get staged(): UpdateStagedArtifactProjection | undefined {
115
+ return this.#snapshot?.staged ?? undefined;
116
+ }
117
+ get channel(): Channel | undefined {
118
+ return this.#snapshot?.channel;
119
+ }
120
+ get installedVersion(): string | undefined {
121
+ return this.#snapshot?.installedVersion;
122
+ }
123
+ get pending(): boolean {
124
+ return this.#pending;
125
+ }
110
126
 
111
127
  /**
112
128
  * Why the last install did not proceed, when it did not.
@@ -119,7 +135,9 @@ export class UpdateController {
119
135
  }
120
136
 
121
137
  /** The latest refusal, cleared as soon as the next command starts. */
122
- get lastRejection(): UpdateRejectionCode | undefined { return this.#lastRejection; }
138
+ get lastRejection(): UpdateRejectionCode | undefined {
139
+ return this.#lastRejection;
140
+ }
123
141
 
124
142
  /**
125
143
  * Whether this install sits ahead of the channel it follows.
@@ -132,7 +150,9 @@ export class UpdateController {
132
150
  */
133
151
  get aheadOfChannel(): { readonly installed: string; readonly channel: string } | undefined {
134
152
  const availability = this.#snapshot?.availability;
135
- if (availability?.state !== "aheadOfChannel") return undefined;
153
+ if (availability?.state !== "aheadOfChannel") {
154
+ return undefined;
155
+ }
136
156
  return { installed: availability.installed, channel: availability.channel };
137
157
  }
138
158
 
@@ -156,7 +176,9 @@ export class UpdateController {
156
176
  */
157
177
  get presence(): UpdatePresence {
158
178
  const snapshot = this.#snapshot;
159
- if (snapshot === undefined) return "hidden";
179
+ if (snapshot === undefined) {
180
+ return "hidden";
181
+ }
160
182
 
161
183
  switch (this.progress?.state) {
162
184
  case "downloading":
@@ -170,7 +192,9 @@ export class UpdateController {
170
192
  break;
171
193
  }
172
194
 
173
- if (snapshot.availability.state !== "offer") return "hidden";
195
+ if (snapshot.availability.state !== "offer") {
196
+ return "hidden";
197
+ }
174
198
  // Postponed by the operator, not by the gate. A gate deferral is transient
175
199
  // and the offer is still the answer; `userPostponed` is a decision.
176
200
  return snapshot.deferral?.cause.cause === "userPostponed" ? "quiet" : "attention";
@@ -182,7 +206,9 @@ export class UpdateController {
182
206
  }
183
207
 
184
208
  async start(): Promise<void> {
185
- if (this.#started) return;
209
+ if (this.#started) {
210
+ return;
211
+ }
186
212
  this.#started = true;
187
213
  const lifecycle = ++this.#lifecycle;
188
214
  this.#setStatus({ kind: "loading" });
@@ -197,7 +223,9 @@ export class UpdateController {
197
223
  }
198
224
  this.#unlisten.push(unlisten);
199
225
  const unlistenProgress = await this.#client.listenProgress((event) => {
200
- if (!this.#started || lifecycle !== this.#lifecycle) return;
226
+ if (!this.#started || lifecycle !== this.#lifecycle) {
227
+ return;
228
+ }
201
229
  this.#liveProgress = event.progress;
202
230
  this.#notify();
203
231
  });
@@ -208,7 +236,9 @@ export class UpdateController {
208
236
  this.#unlisten.push(unlistenProgress);
209
237
  await this.refresh();
210
238
  } catch (error) {
211
- if (this.#started && lifecycle === this.#lifecycle) this.#setStatus({ kind: "failed", error });
239
+ if (this.#started && lifecycle === this.#lifecycle) {
240
+ this.#setStatus({ kind: "failed", error });
241
+ }
212
242
  }
213
243
  }
214
244
 
@@ -217,7 +247,9 @@ export class UpdateController {
217
247
  this.#lifecycle += 1;
218
248
  const unlisten = this.#unlisten;
219
249
  this.#unlisten = [];
220
- for (const stop of unlisten) await stop();
250
+ for (const stop of unlisten) {
251
+ await stop();
252
+ }
221
253
  this.#setStatus({ kind: "idle" });
222
254
  }
223
255
 
@@ -225,11 +257,15 @@ export class UpdateController {
225
257
  const lifecycle = this.#lifecycle;
226
258
  try {
227
259
  const snapshot = await this.#client.snapshot();
228
- if (lifecycle !== this.#lifecycle) return;
260
+ if (lifecycle !== this.#lifecycle) {
261
+ return;
262
+ }
229
263
  this.#snapshot = snapshot;
230
264
  this.#setStatus({ kind: "ready" });
231
265
  } catch (error) {
232
- if (lifecycle === this.#lifecycle) this.#setStatus({ kind: "failed", error });
266
+ if (lifecycle === this.#lifecycle) {
267
+ this.#setStatus({ kind: "failed", error });
268
+ }
233
269
  }
234
270
  }
235
271
 
@@ -243,14 +279,24 @@ export class UpdateController {
243
279
  /** Follows a different channel from now on. */
244
280
  async selectChannel(channel: Channel): Promise<UpdateCommandOutcome> {
245
281
  return this.#command((client, epoch) =>
246
- client.selectChannel({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, channel }),
282
+ client.selectChannel({
283
+ protocolVersion: UPDATE_PROTOCOL_VERSION,
284
+ authorityEpoch: epoch,
285
+ channel,
286
+ }),
247
287
  );
248
288
  }
249
289
 
250
290
  /** Declines a version for now. */
251
291
  async defer(version: string, cause: DeferralCause): Promise<UpdateCommandOutcome> {
252
- return this.#command((client, epoch) =>
253
- client.defer({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, version, cause }),
292
+ return this.#command(
293
+ (client, epoch) =>
294
+ client.defer({
295
+ protocolVersion: UPDATE_PROTOCOL_VERSION,
296
+ authorityEpoch: epoch,
297
+ version,
298
+ cause,
299
+ }),
254
300
  true,
255
301
  );
256
302
  }
@@ -269,10 +315,18 @@ export class UpdateController {
269
315
  * can offer the restart again.
270
316
  */
271
317
  async apply(version: string): Promise<UpdateCommandOutcome> {
272
- return this.#command((client, epoch) =>
273
- client.apply({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, version }),
274
- true,
275
- );
318
+ return this.#command(async (client, epoch, snapshot) => {
319
+ const staged = snapshot.staged;
320
+ if (staged?.version !== version) {
321
+ return { status: "rejected", code: "noOffer", snapshot };
322
+ }
323
+ return client.apply({
324
+ protocolVersion: UPDATE_PROTOCOL_VERSION,
325
+ authorityEpoch: epoch,
326
+ version,
327
+ digest: staged.digest,
328
+ });
329
+ }, true);
276
330
  }
277
331
 
278
332
  /** Discards the retained staged artifact. */
@@ -283,13 +337,17 @@ export class UpdateController {
283
337
  }
284
338
 
285
339
  async #command(
286
- run: (client: UpdateClient, epoch: number) => Promise<UpdateOutcomeProjection>,
340
+ run: (
341
+ client: UpdateClient,
342
+ epoch: number,
343
+ snapshot: UpdateSnapshot,
344
+ ) => Promise<UpdateOutcomeProjection>,
287
345
  committedMayDefer = false,
288
346
  ): Promise<UpdateCommandOutcome> {
289
347
  const commandSequence = ++this.#commandSequence;
290
348
  this.#lastRejection = undefined;
291
- const epoch = this.#snapshot?.authorityEpoch;
292
- if (epoch === undefined) {
349
+ const snapshot = this.#snapshot;
350
+ if (snapshot === undefined) {
293
351
  // No snapshot means no epoch to send, and inventing one would have the
294
352
  // authority refuse it as stale. Say what is actually wrong.
295
353
  const error = new Error("update state has not been read yet");
@@ -301,12 +359,17 @@ export class UpdateController {
301
359
  this.#liveProgress = undefined;
302
360
  this.#notify();
303
361
  try {
304
- const outcome = await run(this.#client, epoch);
305
- const result: UpdateCommandOutcome = outcome.status === "rejected"
306
- ? { status: "rejected", snapshot: outcome.snapshot, code: outcome.code }
307
- : committedMayDefer && outcome.snapshot.deferral !== null
308
- ? { status: "committedWithDeferral", snapshot: outcome.snapshot, deferral: outcome.snapshot.deferral }
309
- : { status: "committed", snapshot: outcome.snapshot };
362
+ const outcome = await run(this.#client, snapshot.authorityEpoch, snapshot);
363
+ const result: UpdateCommandOutcome =
364
+ outcome.status === "rejected"
365
+ ? { status: "rejected", snapshot: outcome.snapshot, code: outcome.code }
366
+ : committedMayDefer && outcome.snapshot.deferral !== null
367
+ ? {
368
+ status: "committedWithDeferral",
369
+ snapshot: outcome.snapshot,
370
+ deferral: outcome.snapshot.deferral,
371
+ }
372
+ : { status: "committed", snapshot: outcome.snapshot };
310
373
  if (lifecycle === this.#lifecycle && commandSequence === this.#commandSequence) {
311
374
  this.#snapshot = outcome.snapshot;
312
375
  this.#liveProgress = undefined;
@@ -333,6 +396,8 @@ export class UpdateController {
333
396
  }
334
397
 
335
398
  #notify(): void {
336
- for (const observer of this.#observers) observer();
399
+ for (const observer of this.#observers) {
400
+ observer();
401
+ }
337
402
  }
338
403
  }
@@ -1,3 +1,5 @@
1
1
  import type { UpdatePort } from "./ports.ts";
2
2
  export type DirectUpdateHandlers = UpdatePort;
3
- export function createDirectUpdatePort(handlers: DirectUpdateHandlers): UpdatePort { return handlers; }
3
+ export function createDirectUpdatePort(handlers: DirectUpdateHandlers): UpdatePort {
4
+ return handlers;
5
+ }
@@ -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 UPDATE_FIELDS: Record<string, readonly string[]> = {
6
+ export const UPDATE_FIELDS = {
7
7
  "UpdateDeferralProjection": ["version", "cause"],
8
8
  "UpdateStagedArtifactProjection": ["version", "channel", "digest"],
9
9
  "UpdateProgressEvent": ["protocolVersion", "authorityEpoch", "progress"],
@@ -12,7 +12,7 @@ export const UPDATE_FIELDS: Record<string, readonly string[]> = {
12
12
  "UpdateSelectChannelCommand": ["protocolVersion", "authorityEpoch", "channel"],
13
13
  "UpdateDeferCommand": ["protocolVersion", "authorityEpoch", "version", "cause"],
14
14
  "UpdatePrepareCommand": ["protocolVersion", "authorityEpoch", "version"],
15
- "UpdateApplyCommand": ["protocolVersion", "authorityEpoch", "version"],
15
+ "UpdateApplyCommand": ["protocolVersion", "authorityEpoch", "version", "digest"],
16
16
  "UpdateCancelCommand": ["protocolVersion", "authorityEpoch"],
17
17
  "UpdateChangedEvent": ["protocolVersion", "authorityEpoch", "kind"],
18
- };
18
+ } as const;
@@ -7,7 +7,7 @@ export const UPDATE_CHANNELS = ["production","beta","nightly"] as const;
7
7
  export const UPDATE_OFFER_REASONS = ["staged","belowMinimumVersion","userInitiated"] as const;
8
8
  export const UPDATE_INSTALL_MANAGERS = ["macAppStore","homebrewCask","flatpak","snap","appImage","nix","linuxDistribution"] as const;
9
9
  export const UPDATE_DEFERRAL_CAUSES = ["userPostponed","workInFlight","installationNotWritable","externallyManaged","installFailed"] as const;
10
- export const UPDATE_AVAILABILITY_STATES = ["offer","upToDate","aheadOfChannel","withheldByRollout","managedElsewhere"] as const;
10
+ export const UPDATE_AVAILABILITY_STATES = ["notChecked","offer","upToDate","aheadOfChannel","withheldByRollout","managedElsewhere"] as const;
11
11
  export const UPDATE_PROGRESS_STATES = ["idle","downloading","verifying","readyToInstall","installing"] as const;
12
12
  export const UPDATE_INSTALL_AUTHORIZATION_STATUSES = ["held","deferred"] as const;
13
13
  export const UPDATE_REJECTION_CODES = ["staleAuthority","noOffer","unavailable","channelMismatch","unreachable","signatureRejected","notWritable","installFailed"] as const;
@@ -44,7 +44,7 @@ command: string | null, } | { "cause": "installFailed",
44
44
  */
45
45
  detail: string, };
46
46
 
47
- export type UpdateAvailabilityProjection = { "state": "offer",
47
+ export type UpdateAvailabilityProjection = { "state": "notChecked" } | { "state": "offer",
48
48
  /**
49
49
  * The version offered.
50
50
  */
@@ -253,7 +253,11 @@ authorityEpoch: number,
253
253
  /**
254
254
  * The version the caller believes is staged.
255
255
  */
256
- version: string, };
256
+ version: string,
257
+ /**
258
+ * SHA-256 of the staged bytes observed by the caller, lowercase hex.
259
+ */
260
+ digest: string, };
257
261
 
258
262
  export type UpdateCancelCommand = {
259
263
  /**
@@ -12,6 +12,7 @@ export const UPDATE_VARIANT_FIELDS: Record<string, Record<string, readonly strin
12
12
  "installFailed": ["cause", "detail"],
13
13
  },
14
14
  "UpdateAvailabilityProjection": {
15
+ "notChecked": ["state"],
15
16
  "offer": ["state", "version", "reason", "notes"],
16
17
  "upToDate": ["state"],
17
18
  "aheadOfChannel": ["state", "installed", "channel"],
@@ -27,8 +27,12 @@ export interface UpdatePort {
27
27
  prepare(command: UpdatePrepareCommand): Promise<unknown>;
28
28
  apply(command: UpdateApplyCommand): Promise<unknown>;
29
29
  cancel(command: UpdateCancelCommand): Promise<unknown>;
30
- listen?(listener: (event: unknown) => void): UpdateUnlisten | Promise<UpdateUnlisten>;
31
- listenProgress?(listener: (event: unknown) => void): UpdateUnlisten | Promise<UpdateUnlisten>;
30
+ listen?:
31
+ | ((listener: (event: unknown) => void) => UpdateUnlisten | Promise<UpdateUnlisten>)
32
+ | undefined;
33
+ listenProgress?:
34
+ | ((listener: (event: unknown) => void) => UpdateUnlisten | Promise<UpdateUnlisten>)
35
+ | undefined;
32
36
  }
33
37
 
34
38
  /** The same seam after validation. */