@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,16 +1,19 @@
1
+ import type { ConnectionFailureReporter } from "../core/connection.ts";
2
+ import { ControllerLifecycle } from "../core/controller-lifecycle.ts";
1
3
  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,
4
+ import { UPDATE_PROTOCOL_VERSION } from "./generated/protocol.ts";
5
+ import type {
6
+ Channel,
7
+ DeferralCause,
8
+ UpdateAvailabilityProjection,
9
+ UpdateOutcomeProjection,
10
+ UpdateProgressEvent,
11
+ UpdateProgressProjection,
12
+ UpdateRejectionCode,
13
+ UpdateSnapshot,
14
+ UpdateStagedArtifactProjection,
12
15
  } from "./generated/protocol.ts";
13
- import type { UpdatePort, UpdateUnlisten } from "./ports.ts";
16
+ import type { UpdatePort } from "./ports.ts";
14
17
 
15
18
  export type UpdateControllerStatus =
16
19
  | { readonly kind: "idle" }
@@ -33,6 +36,7 @@ export type UpdateCommandOutcome =
33
36
  readonly status: "rejected";
34
37
  readonly snapshot: UpdateSnapshot;
35
38
  readonly code: UpdateRejectionCode;
39
+ readonly detail: string | null;
36
40
  }
37
41
  | { readonly status: "failed"; readonly error: unknown };
38
42
 
@@ -57,10 +61,8 @@ export type UpdatePresence = "hidden" | "quiet" | "attention";
57
61
  * update `status` for callers that observe the controller instead:
58
62
  *
59
63
  * - **Committed.** State moved. Read the snapshot.
60
- * - **Committed with a deferral.** The install did not happen and that is not
61
- * a failure — Card 154 step 6. The gate refused because the user has work in
62
- * flight, and `deferral` says so. A surface that reports this as an error
63
- * tells a customer their update is broken when nothing is.
64
+ * - **Committed with a deferral.** The gate postponed the install. Read
65
+ * `deferral` for the reason.
64
66
  * - **Rejected.** The authority refused. `lastRejection` carries the code.
65
67
  *
66
68
  * - **Failed.** The transport or local precondition failed. The error is in
@@ -71,12 +73,12 @@ export class UpdateController {
71
73
  readonly #observers = new Set<() => void>();
72
74
  #status: UpdateControllerStatus = { kind: "idle" };
73
75
  #snapshot?: UpdateSnapshot;
74
- #lastRejection?: UpdateRejectionCode;
75
- #started = false;
76
- #lifecycle = 0;
76
+ #lastRejection: UpdateRejectionCode | undefined;
77
+ #lastRejectionDetail: string | null = null;
78
+ readonly #lifecycle = new ControllerLifecycle();
77
79
  #commandSequence = 0;
80
+ #refreshRevision = 0;
78
81
  #pending = false;
79
- #unlisten: UpdateUnlisten[] = [];
80
82
  /**
81
83
  * The most recent live progress report.
82
84
  *
@@ -85,15 +87,21 @@ export class UpdateController {
85
87
  * said before the transfer began. It is cleared when the command that
86
88
  * started the transfer settles, so a stale report cannot outlive it.
87
89
  */
88
- #liveProgress?: UpdateProgressProjection;
90
+ #liveProgress: UpdateProgressProjection | undefined;
89
91
 
90
92
  constructor(options: UpdateControllerOptions) {
91
93
  this.#client = new UpdateClient(options.port);
92
94
  }
93
95
 
94
- get status(): UpdateControllerStatus { return this.#status; }
95
- get snapshot(): UpdateSnapshot | undefined { return this.#snapshot; }
96
- get availability(): UpdateAvailabilityProjection | undefined { return this.#snapshot?.availability; }
96
+ get status(): UpdateControllerStatus {
97
+ return this.#lifecycle.eventFailure ?? this.#status;
98
+ }
99
+ get snapshot(): UpdateSnapshot | undefined {
100
+ return this.#snapshot;
101
+ }
102
+ get availability(): UpdateAvailabilityProjection | undefined {
103
+ return this.#snapshot?.availability;
104
+ }
97
105
  /**
98
106
  * What the transfer is doing right now.
99
107
  *
@@ -101,38 +109,53 @@ export class UpdateController {
101
109
  * fresh as the last commit and the transfer deliberately does not hold the
102
110
  * authority while it runs.
103
111
  */
104
- get progress(): UpdateProgressProjection | undefined { return this.#liveProgress ?? this.#snapshot?.progress; }
112
+ get progress(): UpdateProgressProjection | undefined {
113
+ return this.#liveProgress ?? this.#snapshot?.progress;
114
+ }
105
115
  /** 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; }
116
+ get staged(): UpdateStagedArtifactProjection | undefined {
117
+ return this.#snapshot?.staged ?? undefined;
118
+ }
119
+ get channel(): Channel | undefined {
120
+ return this.#snapshot?.channel;
121
+ }
122
+ get installedVersion(): string | undefined {
123
+ return this.#snapshot?.installedVersion;
124
+ }
125
+ get pending(): boolean {
126
+ return this.#pending;
127
+ }
110
128
 
111
129
  /**
112
130
  * Why the last install did not proceed, when it did not.
113
131
  *
114
- * Present alongside a *committed* outcome. Card 154 step 6: a refused
115
- * restart shows its reason and is not a failure.
132
+ * The reason a committed command postponed installation.
116
133
  */
117
134
  get deferral(): { readonly version: string; readonly cause: DeferralCause } | undefined {
118
135
  return this.#snapshot?.deferral ?? undefined;
119
136
  }
120
137
 
121
138
  /** The latest refusal, cleared as soon as the next command starts. */
122
- get lastRejection(): UpdateRejectionCode | undefined { return this.#lastRejection; }
139
+ get lastRejection(): UpdateRejectionCode | undefined {
140
+ return this.#lastRejection;
141
+ }
142
+
143
+ /** Installer detail from the latest rejection, when supplied. */
144
+ get lastRejectionDetail(): string | null {
145
+ return this.#lastRejectionDetail;
146
+ }
123
147
 
124
148
  /**
125
149
  * Whether this install sits ahead of the channel it follows.
126
150
  *
127
- * Card 154 step 5 calls this the single most likely support question the
128
- * feature generates: an install on `1.3.0-nightly.4` that selects production
129
- * receives nothing until `1.3.0` ships, which is correct and reads as a
130
- * broken updater unless the surface says so. Exposed as its own read so a
131
- * surface cannot render it as "no update available" by omission.
151
+ * A nightly install following production waits for the channel to catch up.
152
+ * The surface can explain this state using the installed and channel versions.
132
153
  */
133
154
  get aheadOfChannel(): { readonly installed: string; readonly channel: string } | undefined {
134
155
  const availability = this.#snapshot?.availability;
135
- if (availability?.state !== "aheadOfChannel") return undefined;
156
+ if (availability?.state !== "aheadOfChannel") {
157
+ return undefined;
158
+ }
136
159
  return { installed: availability.installed, channel: availability.channel };
137
160
  }
138
161
 
@@ -156,7 +179,9 @@ export class UpdateController {
156
179
  */
157
180
  get presence(): UpdatePresence {
158
181
  const snapshot = this.#snapshot;
159
- if (snapshot === undefined) return "hidden";
182
+ if (snapshot === undefined) {
183
+ return "hidden";
184
+ }
160
185
 
161
186
  switch (this.progress?.state) {
162
187
  case "downloading":
@@ -170,7 +195,9 @@ export class UpdateController {
170
195
  break;
171
196
  }
172
197
 
173
- if (snapshot.availability.state !== "offer") return "hidden";
198
+ if (snapshot.availability.state !== "offer") {
199
+ return "hidden";
200
+ }
174
201
  // Postponed by the operator, not by the gate. A gate deferral is transient
175
202
  // and the offer is still the answer; `userPostponed` is a decision.
176
203
  return snapshot.deferral?.cause.cause === "userPostponed" ? "quiet" : "attention";
@@ -181,84 +208,110 @@ export class UpdateController {
181
208
  return () => this.#observers.delete(observer);
182
209
  }
183
210
 
184
- async start(): Promise<void> {
185
- if (this.#started) return;
186
- this.#started = true;
187
- const lifecycle = ++this.#lifecycle;
188
- this.#setStatus({ kind: "loading" });
189
- try {
190
- const unlisten = await this.#client.listen(() => void this.refresh());
191
- // The listen resolved after a stop, or after a restart replaced this
192
- // lifetime. Contract 013: an adapter that outlives its owner keeps
193
- // writing into state nobody is reading.
194
- if (!this.#started || lifecycle !== this.#lifecycle) {
195
- await unlisten();
196
- return;
197
- }
198
- this.#unlisten.push(unlisten);
199
- const unlistenProgress = await this.#client.listenProgress((event) => {
200
- if (!this.#started || lifecycle !== this.#lifecycle) return;
201
- this.#liveProgress = event.progress;
202
- this.#notify();
203
- });
204
- if (!this.#started || lifecycle !== this.#lifecycle) {
205
- await unlistenProgress();
206
- return;
207
- }
208
- this.#unlisten.push(unlistenProgress);
209
- await this.refresh();
210
- } catch (error) {
211
- if (this.#started && lifecycle === this.#lifecycle) this.#setStatus({ kind: "failed", error });
212
- }
211
+ start(): Promise<void> {
212
+ return this.#lifecycle.start({
213
+ loading: () => this.#setStatus({ kind: "loading" }),
214
+ connect: async ({ isCurrent, own, reportFailure }) => {
215
+ const unlisten = await this.#client.listen(() => {
216
+ if (isCurrent()) {
217
+ void this.refresh();
218
+ }
219
+ }, reportFailure);
220
+ await own(unlisten);
221
+ if (isCurrent()) {
222
+ await this.refresh();
223
+ }
224
+ },
225
+ failed: (error) => this.#setStatus({ kind: "failed", error }),
226
+ });
213
227
  }
214
228
 
215
- async stop(): Promise<void> {
216
- this.#started = false;
217
- this.#lifecycle += 1;
218
- const unlisten = this.#unlisten;
219
- this.#unlisten = [];
220
- for (const stop of unlisten) await stop();
221
- this.#setStatus({ kind: "idle" });
229
+ /** Replaces the current subscription, including after a reported failure. */
230
+ restart(): Promise<void> {
231
+ return this.#lifecycle.restart(
232
+ this.status,
233
+ () => this.stop(),
234
+ () => this.start(),
235
+ );
236
+ }
237
+
238
+ stop(): Promise<void> {
239
+ return this.#lifecycle.stop(() => {
240
+ this.#pending = false;
241
+ this.#liveProgress = undefined;
242
+ this.#setStatus({ kind: "idle" });
243
+ });
222
244
  }
223
245
 
224
246
  async refresh(): Promise<void> {
225
- const lifecycle = this.#lifecycle;
247
+ if (!this.#lifecycle.active) {
248
+ return;
249
+ }
250
+ const lifecycle = this.#lifecycle.revision;
251
+ const revision = ++this.#refreshRevision;
252
+ const isCurrent = () =>
253
+ this.#lifecycle.isCurrent(lifecycle) && revision === this.#refreshRevision;
226
254
  try {
227
255
  const snapshot = await this.#client.snapshot();
228
- if (lifecycle !== this.#lifecycle) return;
256
+ if (!isCurrent()) {
257
+ return;
258
+ }
259
+ if (snapshot.authorityEpoch !== this.#snapshot?.authorityEpoch) {
260
+ this.#liveProgress = undefined;
261
+ }
229
262
  this.#snapshot = snapshot;
230
263
  this.#setStatus({ kind: "ready" });
231
264
  } catch (error) {
232
- if (lifecycle === this.#lifecycle) this.#setStatus({ kind: "failed", error });
265
+ if (isCurrent()) {
266
+ this.#setStatus({ kind: "failed", error });
267
+ }
233
268
  }
234
269
  }
235
270
 
236
271
  /** Asks the source for the channel's current manifest. */
237
272
  async check(): Promise<UpdateCommandOutcome> {
238
- return this.#command((client, epoch) =>
239
- client.check({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch }),
273
+ return this.#command((client, { authorityEpoch }) =>
274
+ client.check({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch }),
240
275
  );
241
276
  }
242
277
 
243
278
  /** Follows a different channel from now on. */
244
279
  async selectChannel(channel: Channel): Promise<UpdateCommandOutcome> {
245
- return this.#command((client, epoch) =>
246
- client.selectChannel({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, channel }),
280
+ return this.#command((client, { authorityEpoch }) =>
281
+ client.selectChannel({
282
+ protocolVersion: UPDATE_PROTOCOL_VERSION,
283
+ authorityEpoch,
284
+ channel,
285
+ }),
247
286
  );
248
287
  }
249
288
 
250
289
  /** Declines a version for now. */
251
290
  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 }),
254
- true,
291
+ return this.#command(
292
+ (client, { authorityEpoch }) =>
293
+ client.defer({
294
+ protocolVersion: UPDATE_PROTOCOL_VERSION,
295
+ authorityEpoch,
296
+ version,
297
+ cause,
298
+ }),
299
+ { committedMayDefer: true },
255
300
  );
256
301
  }
257
302
 
258
303
  /** Fetches, verifies and retains an update for a later apply. */
259
304
  async prepare(version: string): Promise<UpdateCommandOutcome> {
260
- return this.#command((client, epoch) =>
261
- client.prepare({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, version }),
305
+ return this.#command((client, snapshot, progress) =>
306
+ client.prepareWithProgress(
307
+ {
308
+ protocolVersion: UPDATE_PROTOCOL_VERSION,
309
+ authorityEpoch: snapshot.authorityEpoch,
310
+ version,
311
+ },
312
+ progress.report,
313
+ progress.fail,
314
+ ),
262
315
  );
263
316
  }
264
317
 
@@ -266,64 +319,133 @@ export class UpdateController {
266
319
  * Applies the retained staged artifact, replacing the application.
267
320
  *
268
321
  * A gate refusal is a deferral: the artifact stays staged and the surface
269
- * can offer the restart again.
322
+ * can offer the restart again. A version that is not the staged one fails
323
+ * with `UpdateNotStagedError` and nothing is sent.
270
324
  */
271
325
  async apply(version: string): Promise<UpdateCommandOutcome> {
272
- return this.#command((client, epoch) =>
273
- client.apply({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, version }),
274
- true,
326
+ return this.#command(
327
+ (client, snapshot) =>
328
+ client.apply({
329
+ protocolVersion: UPDATE_PROTOCOL_VERSION,
330
+ authorityEpoch: snapshot.authorityEpoch,
331
+ version,
332
+ digest: stagedArtifact(snapshot, version).digest,
333
+ }),
334
+ {
335
+ committedMayDefer: true,
336
+ precondition: (snapshot) => {
337
+ stagedArtifact(snapshot, version);
338
+ },
339
+ },
275
340
  );
276
341
  }
277
342
 
278
343
  /** Discards the retained staged artifact. */
279
344
  async cancel(): Promise<UpdateCommandOutcome> {
280
- return this.#command((client, epoch) =>
281
- client.cancel({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch }),
345
+ return this.#command((client, { authorityEpoch }) =>
346
+ client.cancel({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch }),
282
347
  );
283
348
  }
284
349
 
285
350
  async #command(
286
- run: (client: UpdateClient, epoch: number) => Promise<UpdateOutcomeProjection>,
287
- committedMayDefer = false,
351
+ run: (
352
+ client: UpdateClient,
353
+ snapshot: UpdateSnapshot,
354
+ progress: {
355
+ readonly report: (event: UpdateProgressEvent) => void;
356
+ readonly fail: ConnectionFailureReporter;
357
+ },
358
+ ) => Promise<UpdateOutcomeProjection>,
359
+ options: {
360
+ readonly committedMayDefer?: boolean;
361
+ /** Throws when local state rules the command out before anything is sent. */
362
+ readonly precondition?: (snapshot: UpdateSnapshot) => void;
363
+ } = {},
288
364
  ): Promise<UpdateCommandOutcome> {
289
- const commandSequence = ++this.#commandSequence;
365
+ if (!this.#lifecycle.active) {
366
+ // Nothing is sent and nothing is written: a stopped controller stays idle.
367
+ return { status: "failed", error: new UpdateControllerUnavailableError() };
368
+ }
290
369
  this.#lastRejection = undefined;
291
- const epoch = this.#snapshot?.authorityEpoch;
292
- if (epoch === undefined) {
293
- // No snapshot means no epoch to send, and inventing one would have the
294
- // authority refuse it as stale. Say what is actually wrong.
295
- const error = new Error("update state has not been read yet");
370
+ this.#lastRejectionDetail = null;
371
+ const snapshot = this.#snapshot;
372
+ // A local failure takes no command sequence, so a command already in
373
+ // flight keeps ownership of `pending` and of its result.
374
+ try {
375
+ if (snapshot === undefined) {
376
+ // No snapshot means no epoch to send, and inventing one would have the
377
+ // authority refuse it as stale. Say what is actually wrong.
378
+ throw new Error("update state has not been read yet");
379
+ }
380
+ options.precondition?.(snapshot);
381
+ } catch (error) {
296
382
  this.#setStatus({ kind: "failed", error });
297
383
  return { status: "failed", error };
298
384
  }
299
- const lifecycle = this.#lifecycle;
385
+ const commandSequence = ++this.#commandSequence;
386
+ const lifecycle = this.#lifecycle.revision;
387
+ const isCurrent = () =>
388
+ lifecycle === this.#lifecycle.revision && commandSequence === this.#commandSequence;
389
+ const reportProgress = (event: UpdateProgressEvent) => {
390
+ if (
391
+ this.#lifecycle.active &&
392
+ isCurrent() &&
393
+ this.#pending &&
394
+ event.authorityEpoch === snapshot.authorityEpoch &&
395
+ event.authorityEpoch === this.#snapshot?.authorityEpoch
396
+ ) {
397
+ this.#liveProgress = event.progress;
398
+ this.#notify();
399
+ }
400
+ };
401
+ const failProgress: ConnectionFailureReporter = ({ error }) => {
402
+ if (this.#lifecycle.isCurrent(lifecycle)) {
403
+ this.#lifecycle.recordEventFailure(error);
404
+ this.#liveProgress = undefined;
405
+ this.#notify();
406
+ }
407
+ };
300
408
  this.#pending = true;
301
409
  this.#liveProgress = undefined;
302
410
  this.#notify();
303
411
  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 };
310
- if (lifecycle === this.#lifecycle && commandSequence === this.#commandSequence) {
412
+ const outcome = await run(this.#client, snapshot, {
413
+ report: reportProgress,
414
+ fail: failProgress,
415
+ });
416
+ const result: UpdateCommandOutcome =
417
+ outcome.status === "rejected"
418
+ ? {
419
+ status: "rejected",
420
+ snapshot: outcome.snapshot,
421
+ code: outcome.code,
422
+ detail: outcome.detail,
423
+ }
424
+ : options.committedMayDefer && outcome.snapshot.deferral !== null
425
+ ? {
426
+ status: "committedWithDeferral",
427
+ snapshot: outcome.snapshot,
428
+ deferral: outcome.snapshot.deferral,
429
+ }
430
+ : { status: "committed", snapshot: outcome.snapshot };
431
+ if (isCurrent()) {
432
+ this.#pending = false;
433
+ this.#refreshRevision += 1;
311
434
  this.#snapshot = outcome.snapshot;
312
435
  this.#liveProgress = undefined;
313
436
  this.#lastRejection = result.status === "rejected" ? result.code : undefined;
437
+ this.#lastRejectionDetail = result.status === "rejected" ? result.detail : null;
314
438
  this.#setStatus({ kind: "ready" });
315
439
  }
316
440
  return result;
317
441
  } catch (error) {
318
- if (lifecycle === this.#lifecycle && commandSequence === this.#commandSequence) {
442
+ if (isCurrent()) {
443
+ this.#refreshRevision += 1;
444
+ this.#pending = false;
445
+ this.#liveProgress = undefined;
319
446
  this.#setStatus({ kind: "failed", error });
320
447
  }
321
448
  return { status: "failed", error };
322
- } finally {
323
- if (lifecycle === this.#lifecycle && commandSequence === this.#commandSequence) {
324
- this.#pending = false;
325
- this.#notify();
326
- }
327
449
  }
328
450
  }
329
451
 
@@ -333,6 +455,32 @@ export class UpdateController {
333
455
  }
334
456
 
335
457
  #notify(): void {
336
- for (const observer of this.#observers) observer();
458
+ for (const observer of this.#observers) {
459
+ observer();
460
+ }
461
+ }
462
+ }
463
+
464
+ /** A command was issued outside a started lifetime. */
465
+ export class UpdateControllerUnavailableError extends Error {
466
+ constructor() {
467
+ super("update controller is not started");
468
+ this.name = "UpdateControllerUnavailableError";
469
+ }
470
+ }
471
+
472
+ /** `apply` named a version that is not the staged artifact. */
473
+ export class UpdateNotStagedError extends Error {
474
+ constructor(readonly version: string) {
475
+ super(`no staged update artifact for version: ${version}`);
476
+ this.name = "UpdateNotStagedError";
477
+ }
478
+ }
479
+
480
+ function stagedArtifact(snapshot: UpdateSnapshot, version: string): UpdateStagedArtifactProjection {
481
+ const staged = snapshot.staged;
482
+ if (staged?.version !== version) {
483
+ throw new UpdateNotStagedError(version);
337
484
  }
485
+ return staged;
338
486
  }
@@ -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
+ }
@@ -1,9 +1,9 @@
1
- // @generated by `effigy generate:update`; 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 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,8 @@ 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
+ "UpdateHostError": ["code", "message", "retryable"],
19
+ } as const;
@@ -1,16 +1,16 @@
1
- // @generated by `effigy generate:update`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Rust serde types are the wire authority. Versions are strings, as
3
3
  // `semver::Version` serialises them.
4
4
 
5
5
  export const UPDATE_PROTOCOL_VERSION = 1 as const;
6
6
  export const UPDATE_CHANNELS = ["production","beta","nightly"] as const;
7
7
  export const UPDATE_OFFER_REASONS = ["staged","belowMinimumVersion","userInitiated"] as const;
8
- export const UPDATE_INSTALL_MANAGERS = ["macAppStore","homebrewCask","flatpak","snap","appImage","nix","linuxDistribution"] as const;
8
+ export const UPDATE_INSTALL_MANAGERS = ["macAppStore","homebrewCask","homebrewFormula","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","unavailableForTarget","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
- export const UPDATE_REJECTION_CODES = ["staleAuthority","noOffer","unavailable","channelMismatch","unreachable","signatureRejected","notWritable","installFailed"] as const;
13
+ export const UPDATE_REJECTION_CODES = ["staleAuthority","noOffer","unavailable","channelMismatch","unreachable","signatureRejected","notWritable","malformedArtifact","installFailed"] as const;
14
14
  export const UPDATE_OUTCOME_STATUSES = ["committed","rejected"] as const;
15
15
  export const UPDATE_CHANGED_KINDS = ["checked","channelSelected","deferred","progressed"] as const;
16
16
 
@@ -20,7 +20,7 @@ export type Channel = "production" | "beta" | "nightly";
20
20
 
21
21
  export type OfferReason = "staged" | "belowMinimumVersion" | "userInitiated";
22
22
 
23
- export type InstallManager = "macAppStore" | "homebrewCask" | "flatpak" | "snap" | "appImage" | "nix" | "linuxDistribution";
23
+ export type InstallManager = "macAppStore" | "homebrewCask" | "homebrewFormula" | "flatpak" | "snap" | "appImage" | "nix" | "linuxDistribution";
24
24
 
25
25
  export type DeferralCause = { "cause": "userPostponed" } | { "cause": "workInFlight",
26
26
  /**
@@ -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
  */
@@ -56,7 +56,11 @@ reason: OfferReason,
56
56
  /**
57
57
  * Release notes, when published.
58
58
  */
59
- notes: string | null, } | { "state": "upToDate" } | { "state": "aheadOfChannel",
59
+ notes: string | null, } | { "state": "unavailableForTarget",
60
+ /**
61
+ * The release version.
62
+ */
63
+ version: string, } | { "state": "upToDate" } | { "state": "aheadOfChannel",
60
64
  /**
61
65
  * What is installed.
62
66
  */
@@ -253,7 +257,11 @@ authorityEpoch: number,
253
257
  /**
254
258
  * The version the caller believes is staged.
255
259
  */
256
- version: string, };
260
+ version: string,
261
+ /**
262
+ * SHA-256 of the staged bytes observed by the caller, lowercase hex.
263
+ */
264
+ digest: string, };
257
265
 
258
266
  export type UpdateCancelCommand = {
259
267
  /**
@@ -271,7 +279,7 @@ export type UpdateInstallAuthorizationProjection = { "status": "held" } | { "sta
271
279
  */
272
280
  cause: DeferralCause, };
273
281
 
274
- export type UpdateRejectionCode = "staleAuthority" | "noOffer" | "unavailable" | "channelMismatch" | "unreachable" | "signatureRejected" | "notWritable" | "installFailed";
282
+ export type UpdateRejectionCode = "staleAuthority" | "noOffer" | "unavailable" | "channelMismatch" | "unreachable" | "signatureRejected" | "notWritable" | "malformedArtifact" | "installFailed";
275
283
 
276
284
  export type UpdateOutcomeProjection = { "status": "committed",
277
285
  /**
@@ -282,6 +290,10 @@ snapshot: UpdateSnapshot, } | { "status": "rejected",
282
290
  * What a surface should tell the operator.
283
291
  */
284
292
  code: UpdateRejectionCode,
293
+ /**
294
+ * Installer detail with common credential shapes redacted, when supplied.
295
+ */
296
+ detail: string | null,
285
297
  /**
286
298
  * The state as it remains.
287
299
  */
@@ -305,3 +317,7 @@ authorityEpoch: number,
305
317
  * Coarse invalidation category.
306
318
  */
307
319
  kind: UpdateChangedKind, };
320
+
321
+ export type UpdateHostErrorCode = "authorityUnavailable" | "stateUnavailable" | "eventPublication";
322
+
323
+ export type UpdateHostError = { "code": UpdateHostErrorCode, "message": string, "retryable": boolean, };