@oatscenter/trellis 0.100.0-rc.54 → 0.100.0-rc.55

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 (34) hide show
  1. package/auth/connection.js +3 -3
  2. package/backoff.d.ts +19 -0
  3. package/backoff.js +32 -0
  4. package/device.d.ts +1 -1
  5. package/device.js +18 -4
  6. package/index.d.ts +1 -1
  7. package/internal_sdk/generated/_internal/evidence.js +4 -4
  8. package/internal_sdk/generated/apis/auth/mod.d.ts +110 -3
  9. package/internal_sdk/generated/apis/auth/mod.js +77 -1
  10. package/internal_sdk/generated/apps/cli/mod.d.ts +55 -2
  11. package/internal_sdk/generated/apps/cli/mod.js +26 -0
  12. package/internal_sdk/generated/apps/console/mod.d.ts +55 -2
  13. package/internal_sdk/generated/apps/console/mod.js +26 -0
  14. package/internal_sdk/generated/package.json +2 -2
  15. package/internal_sdk/generated/participants/eventsRuntime/mod.d.ts +5 -0
  16. package/internal_sdk/generated/participants/eventsRuntime/mod.js +5 -0
  17. package/internal_sdk/generated/participants/jobsRuntime/mod.d.ts +5 -0
  18. package/internal_sdk/generated/participants/jobsRuntime/mod.js +5 -0
  19. package/internal_sdk/generated/participants/platform/mod.d.ts +51 -4
  20. package/internal_sdk/generated/participants/platform/mod.js +7 -2
  21. package/internal_sdk/generated/types/_internal/p0.d.ts +666 -0
  22. package/internal_sdk/generated/types/_internal/p0.js +204 -0
  23. package/internal_sdk/generated/types/index.d.ts +70 -0
  24. package/internal_sdk/generated/types/index.js +35 -0
  25. package/live/client_open.js +5 -4
  26. package/package.json +2 -2
  27. package/service/config.d.ts +118 -11
  28. package/service/config.js +484 -48
  29. package/service/mod.d.ts +1 -1
  30. package/service/runtime/enrollment.js +6 -8
  31. package/service/runtime/service.d.ts +3 -1
  32. package/service/runtime/service.js +24 -6
  33. package/session.d.ts +7 -0
  34. package/session.js +32 -4
@@ -9,6 +9,7 @@ import { base64urlDecode, base64urlEncode, sha256, utf8 } from "./utils.js";
9
9
  import { initializeProtocolWasm, signProvisionedAdmissionWasm, verifyOAuthAdmissionWasm, createSessionAuthorityHandleWasm, assertSessionAuthorityHandleCurrentWasm, transportAuthorizationDigestWasm, classifyTransportAuthorizationWasm, } from "./protocol_wasm.js";
10
10
  import { loadDefaultRuntimeTransport, } from "../runtime_transport.js";
11
11
  import { TransportGenerationManager, } from "../transport/generations.js";
12
+ import { Backoff } from "../backoff.js";
12
13
  /** Subjects on which Auth announces a new provider of an API the session calls. */
13
14
  const PROVIDERS_NOTICE_SUBJECT_PREFIX = "trellis.auth.providers.";
14
15
  const ISSUER_NOTICE_SUBJECT = "trellis.auth.issuers";
@@ -662,7 +663,7 @@ export async function connectAdmitted(options) {
662
663
  ? AbortSignal.any([options.signal, controller.signal])
663
664
  : controller.signal;
664
665
  void (async () => {
665
- let delay = 100;
666
+ const backoff = new Backoff(100, 5_000);
666
667
  while (!signal.aborted && !manager.isClosed() && owner.mirrorFailed) {
667
668
  try {
668
669
  const lease = await manager.acquirePublishedAttachment();
@@ -693,12 +694,11 @@ export async function connectAdmitted(options) {
693
694
  signal.removeEventListener("abort", done);
694
695
  resolve();
695
696
  };
696
- const timer = setTimeout(done, delay);
697
+ const timer = setTimeout(done, backoff.failed());
697
698
  signal.addEventListener("abort", done, { once: true });
698
699
  if (signal.aborted)
699
700
  done();
700
701
  });
701
- delay = Math.min(delay * 2, 5_000);
702
702
  }
703
703
  }
704
704
  })().finally(() => {
package/backoff.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The waits between attempts of work that keeps failing: `firstMs` after the
3
+ * first failure, doubling with each further failure up to `longestMs`, and
4
+ * back to `firstMs` once the work succeeds.
5
+ *
6
+ * Retry loops in the SDK share this so they all wait the same way (see
7
+ * `design/core/engineering-patterns.md`). It is internal to the package.
8
+ */
9
+ export declare class Backoff {
10
+ #private;
11
+ /** Waits that start at `firstMs` and double up to `longestMs`. */
12
+ constructor(firstMs: number, longestMs: number);
13
+ /** The wait the next failure returns, in milliseconds, without counting one. */
14
+ get nextDelayMs(): number;
15
+ /** Counts a failure and returns how long to wait, in milliseconds, before trying again. */
16
+ failed(): number;
17
+ /** Starts the waits over after the work succeeded. */
18
+ succeeded(): void;
19
+ }
package/backoff.js ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The waits between attempts of work that keeps failing: `firstMs` after the
3
+ * first failure, doubling with each further failure up to `longestMs`, and
4
+ * back to `firstMs` once the work succeeds.
5
+ *
6
+ * Retry loops in the SDK share this so they all wait the same way (see
7
+ * `design/core/engineering-patterns.md`). It is internal to the package.
8
+ */
9
+ export class Backoff {
10
+ #firstMs;
11
+ #longestMs;
12
+ #failures = 0;
13
+ /** Waits that start at `firstMs` and double up to `longestMs`. */
14
+ constructor(firstMs, longestMs) {
15
+ this.#firstMs = firstMs;
16
+ this.#longestMs = longestMs;
17
+ }
18
+ /** The wait the next failure returns, in milliseconds, without counting one. */
19
+ get nextDelayMs() {
20
+ return Math.min(this.#firstMs * 2 ** Math.min(this.#failures, 31), this.#longestMs);
21
+ }
22
+ /** Counts a failure and returns how long to wait, in milliseconds, before trying again. */
23
+ failed() {
24
+ const delay = this.nextDelayMs;
25
+ this.#failures += 1;
26
+ return delay;
27
+ }
28
+ /** Starts the waits over after the work succeeded. */
29
+ succeeded() {
30
+ this.#failures = 0;
31
+ }
32
+ }
package/device.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import "./auth/protocol_wasm_provider.ts";
2
2
  import type { NatsConnection } from "@nats-io/nats-core";
3
3
  export { checkDeviceActivation } from "./device/activation.ts";
4
- export type { ConfigChange, ParticipantConfig, ParticipantConfigOf, } from "./service/config.ts";
4
+ export type { ConfigChange, ConfigCheck, ConfigCheckContext, DeriveOptions, DerivedConfig, ParticipantConfig, ParticipantConfigOf, } from "./service/config.ts";
5
5
  export type { CheckDeviceActivationArgs, TrellisDeviceActivatedStatus, TrellisDeviceActivationRequiredStatus, TrellisDeviceActivationStatus, TrellisDeviceNotReadyStatus, } from "./device/activation.ts";
6
6
  import { AsyncResult, UnexpectedError } from "@oatscenter/result";
7
7
  import { type ProvisionedConnectionOptions } from "./auth/connection.ts";
package/device.js CHANGED
@@ -21,7 +21,7 @@ import { Value } from "typebox/value";
21
21
  import { observeTrellisConnection, transitionConnectionAvailability, } from "./connection.js";
22
22
  import { createCallerRuntime } from "./caller.js";
23
23
  import { getParticipantRuntime, participantEvidence, } from "./participant_runtime/participant.js";
24
- import { loadConfig, NO_CONFIG, ownReading, } from "./service/config.js";
24
+ import { ConfigHandle, loadConfig, ownReading, } from "./service/config.js";
25
25
  import { API as AUTH_API } from "./internal_sdk/generated/apis/auth/mod.js";
26
26
  function deviceConnectResult(promise) {
27
27
  return AsyncResult.from(promise.then((value) => Result.ok(value), (cause) => Result.err(cause instanceof TransportError
@@ -372,7 +372,11 @@ export async function connectDeviceWithDeps(args, deps) {
372
372
  participantId: args.participant.identity,
373
373
  fields: configFields,
374
374
  read,
375
+ report: (outcome) => trellis.reportOwnConfig(outcome),
375
376
  log: resolveDeviceLogger(args.log),
377
+ // A device has no serving phase: reading its settings is putting
378
+ // them to use, after the checks it registers at startup.
379
+ startsOnRead: true,
376
380
  });
377
381
  config.watch({
378
382
  transport: manager,
@@ -383,9 +387,12 @@ export async function connectDeviceWithDeps(args, deps) {
383
387
  read: async (atConnect) => ownReading(await read(atConnect)),
384
388
  timeoutMs: args.timeout ?? 30_000,
385
389
  });
386
- const watching = config;
387
- void manager.closed().finally(() => watching.stop());
388
390
  }
391
+ // A device without settings still has a handle of its own, so what it
392
+ // registers or derives goes with this connection.
393
+ const settings = config ??
394
+ new ConfigHandle({}, { values: {}, revision: {} }, resolveDeviceLogger(args.log));
395
+ void manager.closed().finally(() => settings.retire());
389
396
  const health = new ServiceHealthRuntime({
390
397
  serviceName: args.participant.identity,
391
398
  kind: "device",
@@ -415,6 +422,13 @@ export async function connectDeviceWithDeps(args, deps) {
415
422
  resolveDeviceLogger(args.log).warn({ error }, "Device heartbeat publication failed");
416
423
  }
417
424
  };
425
+ // A revision the device's own checks rejected degrades its health until
426
+ // one is accepted, and each outcome is reported at once.
427
+ if (config) {
428
+ const watched = config;
429
+ health.add("settings", () => watched.healthCheck());
430
+ watched.onOutcome(() => void publishHealth());
431
+ }
418
432
  await publishHealth();
419
433
  const heartbeat = setInterval(() => void publishHealth(), health.publishIntervalMs);
420
434
  void manager.closed().finally(() => clearInterval(heartbeat));
@@ -429,7 +443,7 @@ export async function connectDeviceWithDeps(args, deps) {
429
443
  return Object.assign(createCallerRuntime(trellis, args.participant), {
430
444
  health,
431
445
  companion,
432
- config: config ?? NO_CONFIG,
446
+ config: settings,
433
447
  });
434
448
  }
435
449
  catch (error) {
package/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  /** Portable Trellis application API. */
2
2
  export * from "./portable.ts";
3
- export type { AppConfigOf, ConfigChange, ConfigRevision, ParticipantConfig, } from "./service/config.ts";
3
+ export type { AppConfigOf, ConfigChange, ConfigCheck, ConfigCheckContext, ConfigRevision, DeriveOptions, DerivedConfig, ParticipantConfig, } from "./service/config.ts";