@inflatable-cookie/longhorn 0.2.2 → 0.3.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inflatable-cookie/longhorn",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -3,6 +3,7 @@ import {
3
3
  LICENCE_PROTOCOL_VERSION,
4
4
  type HeldLicenceProjection,
5
5
  type LicenceCredentialProjection,
6
+ type LicenceOutcomeProjection,
6
7
  type LicenceRejectionCode,
7
8
  type LicenceSeatProjection,
8
9
  type LicenceSnapshot,
@@ -21,6 +22,15 @@ export interface LicenceControllerOptions {
21
22
  readonly port: LicencePort;
22
23
  }
23
24
 
25
+ export type LicenceCommandOutcome =
26
+ | { readonly status: "committed"; readonly snapshot: LicenceSnapshot }
27
+ | {
28
+ readonly status: "rejected";
29
+ readonly snapshot: LicenceSnapshot;
30
+ readonly code: LicenceRejectionCode;
31
+ }
32
+ | { readonly status: "failed"; readonly error: unknown };
33
+
24
34
  /**
25
35
  * What the operator should be told, if anything.
26
36
  *
@@ -52,6 +62,7 @@ export class LicenceController {
52
62
  #lastRejection?: LicenceRejectionCode;
53
63
  #started = false;
54
64
  #lifecycle = 0;
65
+ #commandSequence = 0;
55
66
  #pending = false;
56
67
  #unlisten: LicenceUnlisten[] = [];
57
68
 
@@ -65,6 +76,7 @@ export class LicenceController {
65
76
  get usability(): LicenceUsabilityProjection | undefined { return this.licence?.usability; }
66
77
  get trustBasis(): LicenceTrustBasisProjection | undefined { return this.licence?.trustBasis; }
67
78
  get pending(): boolean { return this.#pending; }
79
+ /** The latest refusal, cleared as soon as the next command starts. */
68
80
  get lastRejection(): LicenceRejectionCode | undefined { return this.#lastRejection; }
69
81
 
70
82
  /** Whether a licence is held at all. Absence is not a usability state. */
@@ -199,8 +211,8 @@ export class LicenceController {
199
211
  async activate(
200
212
  credential: LicenceCredentialProjection,
201
213
  label: string | null = null,
202
- ): Promise<void> {
203
- await this.#command((client, epoch) =>
214
+ ): Promise<LicenceCommandOutcome> {
215
+ return this.#command((client, epoch) =>
204
216
  client.activate({
205
217
  protocolVersion: LICENCE_PROTOCOL_VERSION,
206
218
  authorityEpoch: epoch,
@@ -217,8 +229,8 @@ export class LicenceController {
217
229
  * without a support conversation. Separate from `deactivate`, which leaves
218
230
  * the machine you are sitting at.
219
231
  */
220
- async releaseSeat(machineId: string): Promise<void> {
221
- await this.#command((client, epoch) =>
232
+ async releaseSeat(machineId: string): Promise<LicenceCommandOutcome> {
233
+ return this.#command((client, epoch) =>
222
234
  client.releaseSeat({
223
235
  protocolVersion: LICENCE_PROTOCOL_VERSION,
224
236
  authorityEpoch: epoch,
@@ -228,15 +240,15 @@ export class LicenceController {
228
240
  }
229
241
 
230
242
  /** Releases this machine's seat. */
231
- async deactivate(): Promise<void> {
232
- await this.#command((client, epoch) =>
243
+ async deactivate(): Promise<LicenceCommandOutcome> {
244
+ return this.#command((client, epoch) =>
233
245
  client.deactivate({ protocolVersion: LICENCE_PROTOCOL_VERSION, authorityEpoch: epoch }),
234
246
  );
235
247
  }
236
248
 
237
249
  /** Re-checks the lease now. */
238
- async refreshLease(): Promise<void> {
239
- await this.#command((client, epoch) =>
250
+ async refreshLease(): Promise<LicenceCommandOutcome> {
251
+ return this.#command((client, epoch) =>
240
252
  client.refresh({ protocolVersion: LICENCE_PROTOCOL_VERSION, authorityEpoch: epoch }),
241
253
  );
242
254
  }
@@ -247,8 +259,8 @@ export class LicenceController {
247
259
  * `null` clears the label back to unnamed. The label stays the customer's
248
260
  * word: this is the one write that can change it after activation.
249
261
  */
250
- async renameSeat(machineId: string, label: string | null): Promise<void> {
251
- await this.#command((client, epoch) =>
262
+ async renameSeat(machineId: string, label: string | null): Promise<LicenceCommandOutcome> {
263
+ return this.#command((client, epoch) =>
252
264
  client.renameSeat({
253
265
  protocolVersion: LICENCE_PROTOCOL_VERSION,
254
266
  authorityEpoch: epoch,
@@ -262,28 +274,39 @@ export class LicenceController {
262
274
  run: (
263
275
  client: LicenceClient,
264
276
  epoch: number,
265
- ) => Promise<import("./generated/protocol.ts").LicenceOutcomeProjection>,
266
- ): Promise<void> {
277
+ ) => Promise<LicenceOutcomeProjection>,
278
+ ): Promise<LicenceCommandOutcome> {
279
+ const commandSequence = ++this.#commandSequence;
280
+ this.#lastRejection = undefined;
267
281
  const epoch = this.#snapshot?.authorityEpoch;
268
282
  if (epoch === undefined) {
269
283
  // Inventing one would have the authority refuse it as stale, which reads
270
284
  // as a protocol fault rather than as "nothing has been read yet".
271
- this.#setStatus({ kind: "failed", error: new Error("licence state has not been read yet") });
272
- return;
285
+ const error = new Error("licence state has not been read yet");
286
+ this.#setStatus({ kind: "failed", error });
287
+ return { status: "failed", error };
273
288
  }
274
289
  const lifecycle = this.#lifecycle;
275
290
  this.#pending = true;
276
291
  this.#notify();
277
292
  try {
278
293
  const outcome = await run(this.#client, epoch);
279
- if (lifecycle !== this.#lifecycle) return;
280
- this.#snapshot = outcome.snapshot;
281
- this.#lastRejection = outcome.status === "rejected" ? outcome.code : undefined;
282
- this.#setStatus({ kind: "ready" });
294
+ const result: LicenceCommandOutcome = outcome.status === "rejected"
295
+ ? { status: "rejected", snapshot: outcome.snapshot, code: outcome.code }
296
+ : { status: "committed", snapshot: outcome.snapshot };
297
+ if (lifecycle === this.#lifecycle && commandSequence === this.#commandSequence) {
298
+ this.#snapshot = outcome.snapshot;
299
+ this.#lastRejection = result.status === "rejected" ? result.code : undefined;
300
+ this.#setStatus({ kind: "ready" });
301
+ }
302
+ return result;
283
303
  } catch (error) {
284
- if (lifecycle === this.#lifecycle) this.#setStatus({ kind: "failed", error });
304
+ if (lifecycle === this.#lifecycle && commandSequence === this.#commandSequence) {
305
+ this.#setStatus({ kind: "failed", error });
306
+ }
307
+ return { status: "failed", error };
285
308
  } finally {
286
- if (lifecycle === this.#lifecycle) {
309
+ if (lifecycle === this.#lifecycle && commandSequence === this.#commandSequence) {
287
310
  this.#pending = false;
288
311
  this.#notify();
289
312
  }
@@ -4,6 +4,7 @@ import {
4
4
  type Channel,
5
5
  type DeferralCause,
6
6
  type UpdateAvailabilityProjection,
7
+ type UpdateOutcomeProjection,
7
8
  type UpdateProgressProjection,
8
9
  type UpdateRejectionCode,
9
10
  type UpdateSnapshot,
@@ -21,6 +22,20 @@ export interface UpdateControllerOptions {
21
22
  readonly port: UpdatePort;
22
23
  }
23
24
 
25
+ export type UpdateCommandOutcome =
26
+ | { readonly status: "committed"; readonly snapshot: UpdateSnapshot }
27
+ | {
28
+ readonly status: "committedWithDeferral";
29
+ readonly snapshot: UpdateSnapshot;
30
+ readonly deferral: NonNullable<UpdateSnapshot["deferral"]>;
31
+ }
32
+ | {
33
+ readonly status: "rejected";
34
+ readonly snapshot: UpdateSnapshot;
35
+ readonly code: UpdateRejectionCode;
36
+ }
37
+ | { readonly status: "failed"; readonly error: unknown };
38
+
24
39
  /**
25
40
  * What an update indicator should do.
26
41
  *
@@ -38,18 +53,18 @@ export type UpdatePresence = "hidden" | "quiet" | "attention";
38
53
  /**
39
54
  * Holds update state for a surface and keeps the three outcomes apart.
40
55
  *
41
- * A command can end three ways, and collapsing any two of them produces a
42
- * wrong message:
56
+ * Commands resolve with an outcome, including transport failures, and also
57
+ * update `status` for callers that observe the controller instead:
43
58
  *
44
59
  * - **Committed.** State moved. Read the snapshot.
45
60
  * - **Committed with a deferral.** The install did not happen and that is not
46
61
  * a failure — Card 154 step 6. The gate refused because the user has work in
47
62
  * flight, and `deferral` says so. A surface that reports this as an error
48
63
  * tells a customer their update is broken when nothing is.
49
- * - **Rejected.** The authority refused. `lastRejection` carries the code, and
50
- * this is the only one of the three that is a fault.
64
+ * - **Rejected.** The authority refused. `lastRejection` carries the code.
51
65
  *
52
- * Transport failures are the fourth thing, and land in `status`.
66
+ * - **Failed.** The transport or local precondition failed. The error is in
67
+ * the outcome and in `status`.
53
68
  */
54
69
  export class UpdateController {
55
70
  readonly #client: UpdateClient;
@@ -59,6 +74,7 @@ export class UpdateController {
59
74
  #lastRejection?: UpdateRejectionCode;
60
75
  #started = false;
61
76
  #lifecycle = 0;
77
+ #commandSequence = 0;
62
78
  #pending = false;
63
79
  #unlisten: UpdateUnlisten[] = [];
64
80
  /**
@@ -102,7 +118,7 @@ export class UpdateController {
102
118
  return this.#snapshot?.deferral ?? undefined;
103
119
  }
104
120
 
105
- /** The last refusal, cleared by the next command that commits. */
121
+ /** The latest refusal, cleared as soon as the next command starts. */
106
122
  get lastRejection(): UpdateRejectionCode | undefined { return this.#lastRejection; }
107
123
 
108
124
  /**
@@ -218,29 +234,30 @@ export class UpdateController {
218
234
  }
219
235
 
220
236
  /** Asks the source for the channel's current manifest. */
221
- async check(): Promise<void> {
222
- await this.#command((client, epoch) =>
237
+ async check(): Promise<UpdateCommandOutcome> {
238
+ return this.#command((client, epoch) =>
223
239
  client.check({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch }),
224
240
  );
225
241
  }
226
242
 
227
243
  /** Follows a different channel from now on. */
228
- async selectChannel(channel: Channel): Promise<void> {
229
- await this.#command((client, epoch) =>
244
+ async selectChannel(channel: Channel): Promise<UpdateCommandOutcome> {
245
+ return this.#command((client, epoch) =>
230
246
  client.selectChannel({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, channel }),
231
247
  );
232
248
  }
233
249
 
234
250
  /** Declines a version for now. */
235
- async defer(version: string, cause: DeferralCause): Promise<void> {
236
- await this.#command((client, epoch) =>
251
+ async defer(version: string, cause: DeferralCause): Promise<UpdateCommandOutcome> {
252
+ return this.#command((client, epoch) =>
237
253
  client.defer({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, version, cause }),
254
+ true,
238
255
  );
239
256
  }
240
257
 
241
258
  /** Fetches, verifies and retains an update for a later apply. */
242
- async prepare(version: string): Promise<void> {
243
- await this.#command((client, epoch) =>
259
+ async prepare(version: string): Promise<UpdateCommandOutcome> {
260
+ return this.#command((client, epoch) =>
244
261
  client.prepare({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, version }),
245
262
  );
246
263
  }
@@ -251,28 +268,33 @@ export class UpdateController {
251
268
  * A gate refusal is a deferral: the artifact stays staged and the surface
252
269
  * can offer the restart again.
253
270
  */
254
- async apply(version: string): Promise<void> {
255
- await this.#command((client, epoch) =>
271
+ async apply(version: string): Promise<UpdateCommandOutcome> {
272
+ return this.#command((client, epoch) =>
256
273
  client.apply({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, version }),
274
+ true,
257
275
  );
258
276
  }
259
277
 
260
278
  /** Discards the retained staged artifact. */
261
- async cancel(): Promise<void> {
262
- await this.#command((client, epoch) =>
279
+ async cancel(): Promise<UpdateCommandOutcome> {
280
+ return this.#command((client, epoch) =>
263
281
  client.cancel({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch }),
264
282
  );
265
283
  }
266
284
 
267
285
  async #command(
268
- run: (client: UpdateClient, epoch: number) => Promise<import("./generated/protocol.ts").UpdateOutcomeProjection>,
269
- ): Promise<void> {
286
+ run: (client: UpdateClient, epoch: number) => Promise<UpdateOutcomeProjection>,
287
+ committedMayDefer = false,
288
+ ): Promise<UpdateCommandOutcome> {
289
+ const commandSequence = ++this.#commandSequence;
290
+ this.#lastRejection = undefined;
270
291
  const epoch = this.#snapshot?.authorityEpoch;
271
292
  if (epoch === undefined) {
272
293
  // No snapshot means no epoch to send, and inventing one would have the
273
294
  // authority refuse it as stale. Say what is actually wrong.
274
- this.#setStatus({ kind: "failed", error: new Error("update state has not been read yet") });
275
- return;
295
+ const error = new Error("update state has not been read yet");
296
+ this.#setStatus({ kind: "failed", error });
297
+ return { status: "failed", error };
276
298
  }
277
299
  const lifecycle = this.#lifecycle;
278
300
  this.#pending = true;
@@ -280,15 +302,25 @@ export class UpdateController {
280
302
  this.#notify();
281
303
  try {
282
304
  const outcome = await run(this.#client, epoch);
283
- if (lifecycle !== this.#lifecycle) return;
284
- this.#snapshot = outcome.snapshot;
285
- this.#liveProgress = undefined;
286
- this.#lastRejection = outcome.status === "rejected" ? outcome.code : undefined;
287
- this.#setStatus({ kind: "ready" });
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) {
311
+ this.#snapshot = outcome.snapshot;
312
+ this.#liveProgress = undefined;
313
+ this.#lastRejection = result.status === "rejected" ? result.code : undefined;
314
+ this.#setStatus({ kind: "ready" });
315
+ }
316
+ return result;
288
317
  } catch (error) {
289
- if (lifecycle === this.#lifecycle) this.#setStatus({ kind: "failed", error });
318
+ if (lifecycle === this.#lifecycle && commandSequence === this.#commandSequence) {
319
+ this.#setStatus({ kind: "failed", error });
320
+ }
321
+ return { status: "failed", error };
290
322
  } finally {
291
- if (lifecycle === this.#lifecycle) {
323
+ if (lifecycle === this.#lifecycle && commandSequence === this.#commandSequence) {
292
324
  this.#pending = false;
293
325
  this.#notify();
294
326
  }