@camstack/addon-provider-onvif 1.2.58 → 1.2.59

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 (3) hide show
  1. package/dist/addon.js +391 -24
  2. package/dist/addon.mjs +391 -24
  3. package/package.json +1 -1
package/dist/addon.js CHANGED
@@ -12602,6 +12602,35 @@ var deviceProviderCapability = {
12602
12602
  name: string(),
12603
12603
  type: string()
12604
12604
  }))),
12605
+ /**
12606
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12607
+ * touching no other device this provider owns.
12608
+ *
12609
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12610
+ * migrated numbers: after `swapIds` the runner's live instance still
12611
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12612
+ * registrations and its log tags), and a live object cannot be renumbered.
12613
+ * Before this method the only flush was restarting the whole owning addon
12614
+ * — which took every camera the provider owns down with it (28 devices
12615
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12616
+ * same day ~27 devices' native caps did not come back on their own).
12617
+ *
12618
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12619
+ * that changes. The reply carries the id the device answers on NOW.
12620
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12621
+ * instance (if any), then re-create from the persisted row: the same
12622
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12623
+ * An RPC, never an event: a dropped event would leave the runner writing
12624
+ * against the wrong camera (D8).
12625
+ *
12626
+ * Construction can dial hardware, and the migrated source is
12627
+ * characteristically dead — the timeout covers a full activate window
12628
+ * rather than the 60 s default.
12629
+ */
12630
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12631
+ kind: "mutation",
12632
+ timeoutMs: 3 * 6e4
12633
+ }),
12605
12634
  supportsDiscovery: method(object({}), boolean()),
12606
12635
  /**
12607
12636
  * Run a network scan. `params` carries optional provider-specific scan
@@ -12929,7 +12958,8 @@ method(object({
12929
12958
  targetId: number()
12930
12959
  }), MigrateDeviceResultSchema, {
12931
12960
  kind: "mutation",
12932
- auth: "admin"
12961
+ auth: "admin",
12962
+ timeoutMs: 12 * 6e4
12933
12963
  }), method(DeviceRegisterPayloadSchema, _void(), { kind: "mutation" }), method(DeviceRemovePayloadSchema, _void(), { kind: "mutation" }), method(DevicePersistConfigPayloadSchema, _void(), { kind: "mutation" }), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), DeviceMetaSchema.nullable()), method(object({
12934
12964
  deviceId: number(),
12935
12965
  name: string()
@@ -29283,6 +29313,147 @@ var DeviceConfig = class DeviceConfig {
29283
29313
  }
29284
29314
  };
29285
29315
  /**
29316
+ * Delays before retry rounds 1..N — the round count IS the bound.
29317
+ * 10 s catches "the hub was busy for a moment"; the full schedule
29318
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
29319
+ * per attempt) covers a device-manager lock held for minutes — the
29320
+ * 2026-09-04 outage's migration hold was ~3.5 min.
29321
+ */
29322
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
29323
+ 1e4,
29324
+ 3e4,
29325
+ 9e4
29326
+ ];
29327
+ /** Abortable sleep — resolves early (never rejects) on abort. */
29328
+ function sleep$1(ms, signal) {
29329
+ return new Promise((resolve) => {
29330
+ if (signal.aborted) {
29331
+ resolve();
29332
+ return;
29333
+ }
29334
+ const onAbort = () => {
29335
+ clearTimeout(timer);
29336
+ resolve();
29337
+ };
29338
+ const timer = setTimeout(() => {
29339
+ signal.removeEventListener("abort", onAbort);
29340
+ resolve();
29341
+ }, ms);
29342
+ timer.unref?.();
29343
+ signal.addEventListener("abort", onAbort, { once: true });
29344
+ });
29345
+ }
29346
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
29347
+ * not reject (callers wrap their own try/catch). */
29348
+ async function runWithConcurrency(items, width, fn) {
29349
+ const queue = [...items];
29350
+ const laneCount = Math.max(1, Math.min(width, queue.length));
29351
+ const lane = async () => {
29352
+ for (;;) {
29353
+ const item = queue.shift();
29354
+ if (item === void 0) return;
29355
+ await fn(item);
29356
+ }
29357
+ };
29358
+ await Promise.all(Array.from({ length: laneCount }, lane));
29359
+ }
29360
+ var DeviceRestoreRetryScheduler = class {
29361
+ #logger;
29362
+ #attempt;
29363
+ #onPermanentFailure;
29364
+ #delaysMs;
29365
+ #concurrency;
29366
+ #now;
29367
+ #abort = new AbortController();
29368
+ constructor(options) {
29369
+ this.#logger = options.logger;
29370
+ this.#attempt = options.attempt;
29371
+ this.#onPermanentFailure = options.onPermanentFailure;
29372
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
29373
+ this.#concurrency = options.concurrency ?? 4;
29374
+ this.#now = options.now ?? Date.now;
29375
+ }
29376
+ /** Stop retrying (shutdown). Pending entries are NOT marked
29377
+ * permanently failed — the next boot restores them from disk. */
29378
+ cancel() {
29379
+ this.#abort.abort();
29380
+ }
29381
+ /**
29382
+ * Run the bounded retry rounds. Resolves when every entry has either
29383
+ * restored, been marked permanently failed, or the scheduler was
29384
+ * cancelled. Never rejects.
29385
+ */
29386
+ async run(initialFailures) {
29387
+ let pending = initialFailures.map((failure) => ({
29388
+ saved: failure.saved,
29389
+ lastError: failure.error,
29390
+ attempts: 1
29391
+ }));
29392
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
29393
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
29394
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
29395
+ if (this.#abort.signal.aborted) break;
29396
+ pending = await this.#runRound(pending, round);
29397
+ }
29398
+ if (this.#abort.signal.aborted) return [];
29399
+ const terminal = pending.map((entry) => ({
29400
+ deviceId: entry.saved.id,
29401
+ stableId: entry.saved.stableId,
29402
+ type: String(entry.saved.type),
29403
+ attempts: entry.attempts,
29404
+ lastError: entry.lastError,
29405
+ failedAt: this.#now()
29406
+ }));
29407
+ for (const failure of terminal) this.#onPermanentFailure(failure);
29408
+ return terminal;
29409
+ }
29410
+ /** One retry round: parents first (phase 0), then hub-adopted
29411
+ * children (phase 1) — a child's attempt depends on its parent
29412
+ * having landed, exactly like the initial two-pass restore. */
29413
+ async #runRound(pending, round) {
29414
+ const next = [];
29415
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
29416
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
29417
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
29418
+ if (this.#abort.signal.aborted) {
29419
+ next.push(entry);
29420
+ return;
29421
+ }
29422
+ const attemptNo = entry.attempts + 1;
29423
+ try {
29424
+ await this.#attempt(entry.saved);
29425
+ this.#logger.info("Device restored on retry", {
29426
+ tags: {
29427
+ deviceId: entry.saved.id,
29428
+ stableId: entry.saved.stableId
29429
+ },
29430
+ meta: { attempt: attemptNo }
29431
+ });
29432
+ } catch (err) {
29433
+ const lastError = err instanceof Error ? err.message : String(err);
29434
+ const remainingRetries = this.#delaysMs.length - (round + 1);
29435
+ this.#logger.warn("Device restore retry failed", {
29436
+ tags: {
29437
+ deviceId: entry.saved.id,
29438
+ stableId: entry.saved.stableId
29439
+ },
29440
+ meta: {
29441
+ attempt: attemptNo,
29442
+ remainingRetries,
29443
+ error: lastError
29444
+ }
29445
+ });
29446
+ next.push({
29447
+ saved: entry.saved,
29448
+ lastError,
29449
+ attempts: attemptNo
29450
+ });
29451
+ }
29452
+ });
29453
+ return next;
29454
+ }
29455
+ };
29456
+ /**
29286
29457
  * Convert an IDevice to the flat DeviceSummary shape expected by the
29287
29458
  * device-provider cap router. Shared across all providers.
29288
29459
  */
@@ -29331,6 +29502,7 @@ var BaseDeviceProvider = class extends BaseAddon {
29331
29502
  }];
29332
29503
  }
29333
29504
  async onShutdown() {
29505
+ this.cancelRestoreRetries();
29334
29506
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
29335
29507
  for (const device of devices) try {
29336
29508
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -29348,9 +29520,16 @@ var BaseDeviceProvider = class extends BaseAddon {
29348
29520
  async start() {}
29349
29521
  async stop() {}
29350
29522
  async getStatus() {
29523
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
29524
+ const summary = this.restoreFailureSummary();
29525
+ if (summary === null) return {
29526
+ connected: true,
29527
+ deviceCount: all.length
29528
+ };
29351
29529
  return {
29352
29530
  connected: true,
29353
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
29531
+ deviceCount: all.length,
29532
+ error: summary
29354
29533
  };
29355
29534
  }
29356
29535
  async getDevices() {
@@ -29440,8 +29619,137 @@ var BaseDeviceProvider = class extends BaseAddon {
29440
29619
  };
29441
29620
  }
29442
29621
  async restoreDevices(savedDevices) {
29443
- await this.onRestoreDevices(savedDevices);
29444
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
29622
+ const report = await this.onRestoreDevices(savedDevices);
29623
+ if (savedDevices.length === 0) return;
29624
+ if (report && report.failedCount > 0) {
29625
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
29626
+ return;
29627
+ }
29628
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
29629
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
29630
+ }
29631
+ /** Retry schedule. Overridable (tests use millisecond delays). */
29632
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
29633
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
29634
+ * never re-stampede full-width while the initial pass does (D167). */
29635
+ restoreRetryConcurrency = 4;
29636
+ _restoreRetryScheduler = null;
29637
+ _restoreRetryCompletion = null;
29638
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
29639
+ /** Settles when the background retry rounds finish (or `null` when
29640
+ * nothing failed). Exposed for tests and subclass diagnostics —
29641
+ * boot NEVER awaits this: the runner's post-init handshake goes out
29642
+ * with the devices that restored, and a late success is announced
29643
+ * through the `native-cap-change` → `updateCaps` path. */
29644
+ get restoreRetryCompletion() {
29645
+ return this._restoreRetryCompletion;
29646
+ }
29647
+ /** Devices that exhausted the retry bound this process lifetime. */
29648
+ get permanentRestoreFailures() {
29649
+ return [...this._permanentRestoreFailures.values()];
29650
+ }
29651
+ /** One-line operator-facing summary for `getStatus().error`, or
29652
+ * `null` when every device restored. */
29653
+ restoreFailureSummary() {
29654
+ if (this._permanentRestoreFailures.size === 0) return null;
29655
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
29656
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
29657
+ }
29658
+ cancelRestoreRetries() {
29659
+ this._restoreRetryScheduler?.cancel();
29660
+ this._restoreRetryScheduler = null;
29661
+ }
29662
+ recordPermanentRestoreFailure(failure) {
29663
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
29664
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
29665
+ tags: {
29666
+ deviceId: failure.deviceId,
29667
+ stableId: failure.stableId
29668
+ },
29669
+ meta: {
29670
+ type: failure.type,
29671
+ attempts: failure.attempts,
29672
+ error: failure.lastError
29673
+ }
29674
+ });
29675
+ }
29676
+ scheduleRestoreRetries(failures, attempt) {
29677
+ const scheduler = new DeviceRestoreRetryScheduler({
29678
+ logger: this.ctx.logger,
29679
+ delaysMs: this.restoreRetryDelaysMs,
29680
+ concurrency: this.restoreRetryConcurrency,
29681
+ attempt,
29682
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
29683
+ });
29684
+ this._restoreRetryScheduler = scheduler;
29685
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
29686
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
29687
+ });
29688
+ }
29689
+ /**
29690
+ * Tear down and reconstruct ONE device from its persisted rows — the
29691
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
29692
+ * and no other device this provider owns is disturbed.
29693
+ *
29694
+ * Keyed by `stableId` because the caller's whole reason to be here is that
29695
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
29696
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
29697
+ * whatever number the row carries NOW. The teardown is `decommission` —
29698
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
29699
+ * unregisters native caps, drops the registry entry) — and the rebuild is
29700
+ * the boot restore's own `create()` path, including its pass 2: first-class
29701
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
29702
+ * parent by the cascade and must be re-created explicitly, because only
29703
+ * accessory children come back through `getAccessoryChildren()`.
29704
+ *
29705
+ * Reloading an accessory child directly is refused (no device class) —
29706
+ * reload its parent instead.
29707
+ */
29708
+ async reloadDevice(input) {
29709
+ const { stableId } = input;
29710
+ const devices = this.ctx.kernel.devices;
29711
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
29712
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
29713
+ if (live) await devices.decommission(live.id);
29714
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
29715
+ addonId: this.addonId,
29716
+ stableId
29717
+ });
29718
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
29719
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
29720
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
29721
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
29722
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
29723
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
29724
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
29725
+ for (const row of rows) {
29726
+ if (row.parentDeviceId !== id) continue;
29727
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
29728
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
29729
+ if (!ChildClass) continue;
29730
+ try {
29731
+ await devices.create(row.stableId, ChildClass, {}, id);
29732
+ } catch (err) {
29733
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
29734
+ tags: {
29735
+ deviceId: row.id,
29736
+ stableId: row.stableId
29737
+ },
29738
+ meta: {
29739
+ parentDeviceId: id,
29740
+ error: err instanceof Error ? err.message : String(err)
29741
+ }
29742
+ });
29743
+ }
29744
+ }
29745
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
29746
+ tags: { deviceId: id },
29747
+ meta: {
29748
+ stableId,
29749
+ type: meta.type
29750
+ }
29751
+ });
29752
+ return { deviceId: id };
29445
29753
  }
29446
29754
  /**
29447
29755
  * Restore devices from persisted state. Two-pass:
@@ -29467,55 +29775,108 @@ var BaseDeviceProvider = class extends BaseAddon {
29467
29775
  * accessory-spawn flow handles via the parent's
29468
29776
  * `getAccessoryChildren()`. Override only when the default doesn't
29469
29777
  * fit.
29778
+ *
29779
+ * A row that fails either pass is NOT terminal (D347): it is handed
29780
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
29781
+ * Only after the bound is exhausted is the device marked permanently
29782
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
29783
+ * `getStatus().error`.
29470
29784
  */
29471
29785
  async onRestoreDevices(savedDevices) {
29472
29786
  const restored = /* @__PURE__ */ new Set();
29787
+ const failures = [];
29788
+ const attemptRestore = async (saved) => {
29789
+ if (restored.has(saved.id)) return;
29790
+ const Class = this.deviceClasses[saved.type];
29791
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
29792
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
29793
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
29794
+ restored.add(saved.id);
29795
+ };
29473
29796
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
29474
29797
  const restoreOne = async (saved) => {
29475
- const Class = this.deviceClasses[saved.type];
29476
- if (!Class) {
29798
+ if (!this.deviceClasses[saved.type]) {
29477
29799
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
29478
- tags: { stableId: saved.stableId },
29800
+ tags: {
29801
+ deviceId: saved.id,
29802
+ stableId: saved.stableId
29803
+ },
29479
29804
  meta: { type: saved.type }
29480
29805
  });
29481
29806
  return;
29482
29807
  }
29483
29808
  try {
29484
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
29485
- restored.add(saved.id);
29809
+ await attemptRestore(saved);
29486
29810
  } catch (err) {
29487
- this.ctx.logger.warn("Failed to restore device", {
29488
- tags: { stableId: saved.stableId },
29811
+ const error = err instanceof Error ? err.message : String(err);
29812
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
29813
+ tags: {
29814
+ deviceId: saved.id,
29815
+ stableId: saved.stableId
29816
+ },
29489
29817
  meta: {
29490
29818
  type: saved.type,
29491
- error: err instanceof Error ? err.message : String(err)
29819
+ attempt: 1,
29820
+ error
29492
29821
  }
29493
29822
  });
29823
+ failures.push({
29824
+ saved,
29825
+ error
29826
+ });
29494
29827
  }
29495
29828
  };
29496
29829
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
29830
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
29497
29831
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
29498
29832
  for (const saved of childRows) {
29499
- const Class = this.deviceClasses[saved.type];
29500
- if (!Class) continue;
29833
+ if (!this.deviceClasses[saved.type]) continue;
29501
29834
  if (saved.parentDeviceId === null) continue;
29502
- if (!restored.has(saved.parentDeviceId)) continue;
29503
- try {
29504
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
29505
- restored.add(saved.id);
29506
- } catch (err) {
29507
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
29835
+ if (restored.has(saved.parentDeviceId)) {
29836
+ try {
29837
+ await attemptRestore(saved);
29838
+ } catch (err) {
29839
+ const error = err instanceof Error ? err.message : String(err);
29840
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
29841
+ tags: {
29842
+ deviceId: saved.id,
29843
+ stableId: saved.stableId,
29844
+ parentDeviceId: saved.parentDeviceId
29845
+ },
29846
+ meta: {
29847
+ type: saved.type,
29848
+ attempt: 1,
29849
+ error
29850
+ }
29851
+ });
29852
+ failures.push({
29853
+ saved,
29854
+ error
29855
+ });
29856
+ }
29857
+ continue;
29858
+ }
29859
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
29860
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
29508
29861
  tags: {
29862
+ deviceId: saved.id,
29509
29863
  stableId: saved.stableId,
29510
29864
  parentDeviceId: saved.parentDeviceId
29511
29865
  },
29512
- meta: {
29513
- type: saved.type,
29514
- error: err instanceof Error ? err.message : String(err)
29515
- }
29866
+ meta: { type: saved.type }
29867
+ });
29868
+ failures.push({
29869
+ saved,
29870
+ error: `parent device ${saved.parentDeviceId} not restored`
29516
29871
  });
29872
+ continue;
29517
29873
  }
29518
29874
  }
29875
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
29876
+ return {
29877
+ restoredCount: restored.size,
29878
+ failedCount: failures.length
29879
+ };
29519
29880
  }
29520
29881
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
29521
29882
  toSummary(device) {
@@ -31344,6 +31705,12 @@ Object.freeze({
31344
31705
  addonId: null,
31345
31706
  access: "view"
31346
31707
  },
31708
+ "deviceProvider.reloadDevice": {
31709
+ capName: "device-provider",
31710
+ capScope: "system",
31711
+ addonId: null,
31712
+ access: "create"
31713
+ },
31347
31714
  "deviceProvider.start": {
31348
31715
  capName: "device-provider",
31349
31716
  capScope: "system",
package/dist/addon.mjs CHANGED
@@ -12603,6 +12603,35 @@ var deviceProviderCapability = {
12603
12603
  name: string(),
12604
12604
  type: string()
12605
12605
  }))),
12606
+ /**
12607
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12608
+ * touching no other device this provider owns.
12609
+ *
12610
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12611
+ * migrated numbers: after `swapIds` the runner's live instance still
12612
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12613
+ * registrations and its log tags), and a live object cannot be renumbered.
12614
+ * Before this method the only flush was restarting the whole owning addon
12615
+ * — which took every camera the provider owns down with it (28 devices
12616
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12617
+ * same day ~27 devices' native caps did not come back on their own).
12618
+ *
12619
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12620
+ * that changes. The reply carries the id the device answers on NOW.
12621
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12622
+ * instance (if any), then re-create from the persisted row: the same
12623
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12624
+ * An RPC, never an event: a dropped event would leave the runner writing
12625
+ * against the wrong camera (D8).
12626
+ *
12627
+ * Construction can dial hardware, and the migrated source is
12628
+ * characteristically dead — the timeout covers a full activate window
12629
+ * rather than the 60 s default.
12630
+ */
12631
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12632
+ kind: "mutation",
12633
+ timeoutMs: 3 * 6e4
12634
+ }),
12606
12635
  supportsDiscovery: method(object({}), boolean()),
12607
12636
  /**
12608
12637
  * Run a network scan. `params` carries optional provider-specific scan
@@ -12930,7 +12959,8 @@ method(object({
12930
12959
  targetId: number()
12931
12960
  }), MigrateDeviceResultSchema, {
12932
12961
  kind: "mutation",
12933
- auth: "admin"
12962
+ auth: "admin",
12963
+ timeoutMs: 12 * 6e4
12934
12964
  }), method(DeviceRegisterPayloadSchema, _void(), { kind: "mutation" }), method(DeviceRemovePayloadSchema, _void(), { kind: "mutation" }), method(DevicePersistConfigPayloadSchema, _void(), { kind: "mutation" }), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), DeviceMetaSchema.nullable()), method(object({
12935
12965
  deviceId: number(),
12936
12966
  name: string()
@@ -29284,6 +29314,147 @@ var DeviceConfig = class DeviceConfig {
29284
29314
  }
29285
29315
  };
29286
29316
  /**
29317
+ * Delays before retry rounds 1..N — the round count IS the bound.
29318
+ * 10 s catches "the hub was busy for a moment"; the full schedule
29319
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
29320
+ * per attempt) covers a device-manager lock held for minutes — the
29321
+ * 2026-09-04 outage's migration hold was ~3.5 min.
29322
+ */
29323
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
29324
+ 1e4,
29325
+ 3e4,
29326
+ 9e4
29327
+ ];
29328
+ /** Abortable sleep — resolves early (never rejects) on abort. */
29329
+ function sleep$1(ms, signal) {
29330
+ return new Promise((resolve) => {
29331
+ if (signal.aborted) {
29332
+ resolve();
29333
+ return;
29334
+ }
29335
+ const onAbort = () => {
29336
+ clearTimeout(timer);
29337
+ resolve();
29338
+ };
29339
+ const timer = setTimeout(() => {
29340
+ signal.removeEventListener("abort", onAbort);
29341
+ resolve();
29342
+ }, ms);
29343
+ timer.unref?.();
29344
+ signal.addEventListener("abort", onAbort, { once: true });
29345
+ });
29346
+ }
29347
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
29348
+ * not reject (callers wrap their own try/catch). */
29349
+ async function runWithConcurrency(items, width, fn) {
29350
+ const queue = [...items];
29351
+ const laneCount = Math.max(1, Math.min(width, queue.length));
29352
+ const lane = async () => {
29353
+ for (;;) {
29354
+ const item = queue.shift();
29355
+ if (item === void 0) return;
29356
+ await fn(item);
29357
+ }
29358
+ };
29359
+ await Promise.all(Array.from({ length: laneCount }, lane));
29360
+ }
29361
+ var DeviceRestoreRetryScheduler = class {
29362
+ #logger;
29363
+ #attempt;
29364
+ #onPermanentFailure;
29365
+ #delaysMs;
29366
+ #concurrency;
29367
+ #now;
29368
+ #abort = new AbortController();
29369
+ constructor(options) {
29370
+ this.#logger = options.logger;
29371
+ this.#attempt = options.attempt;
29372
+ this.#onPermanentFailure = options.onPermanentFailure;
29373
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
29374
+ this.#concurrency = options.concurrency ?? 4;
29375
+ this.#now = options.now ?? Date.now;
29376
+ }
29377
+ /** Stop retrying (shutdown). Pending entries are NOT marked
29378
+ * permanently failed — the next boot restores them from disk. */
29379
+ cancel() {
29380
+ this.#abort.abort();
29381
+ }
29382
+ /**
29383
+ * Run the bounded retry rounds. Resolves when every entry has either
29384
+ * restored, been marked permanently failed, or the scheduler was
29385
+ * cancelled. Never rejects.
29386
+ */
29387
+ async run(initialFailures) {
29388
+ let pending = initialFailures.map((failure) => ({
29389
+ saved: failure.saved,
29390
+ lastError: failure.error,
29391
+ attempts: 1
29392
+ }));
29393
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
29394
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
29395
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
29396
+ if (this.#abort.signal.aborted) break;
29397
+ pending = await this.#runRound(pending, round);
29398
+ }
29399
+ if (this.#abort.signal.aborted) return [];
29400
+ const terminal = pending.map((entry) => ({
29401
+ deviceId: entry.saved.id,
29402
+ stableId: entry.saved.stableId,
29403
+ type: String(entry.saved.type),
29404
+ attempts: entry.attempts,
29405
+ lastError: entry.lastError,
29406
+ failedAt: this.#now()
29407
+ }));
29408
+ for (const failure of terminal) this.#onPermanentFailure(failure);
29409
+ return terminal;
29410
+ }
29411
+ /** One retry round: parents first (phase 0), then hub-adopted
29412
+ * children (phase 1) — a child's attempt depends on its parent
29413
+ * having landed, exactly like the initial two-pass restore. */
29414
+ async #runRound(pending, round) {
29415
+ const next = [];
29416
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
29417
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
29418
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
29419
+ if (this.#abort.signal.aborted) {
29420
+ next.push(entry);
29421
+ return;
29422
+ }
29423
+ const attemptNo = entry.attempts + 1;
29424
+ try {
29425
+ await this.#attempt(entry.saved);
29426
+ this.#logger.info("Device restored on retry", {
29427
+ tags: {
29428
+ deviceId: entry.saved.id,
29429
+ stableId: entry.saved.stableId
29430
+ },
29431
+ meta: { attempt: attemptNo }
29432
+ });
29433
+ } catch (err) {
29434
+ const lastError = err instanceof Error ? err.message : String(err);
29435
+ const remainingRetries = this.#delaysMs.length - (round + 1);
29436
+ this.#logger.warn("Device restore retry failed", {
29437
+ tags: {
29438
+ deviceId: entry.saved.id,
29439
+ stableId: entry.saved.stableId
29440
+ },
29441
+ meta: {
29442
+ attempt: attemptNo,
29443
+ remainingRetries,
29444
+ error: lastError
29445
+ }
29446
+ });
29447
+ next.push({
29448
+ saved: entry.saved,
29449
+ lastError,
29450
+ attempts: attemptNo
29451
+ });
29452
+ }
29453
+ });
29454
+ return next;
29455
+ }
29456
+ };
29457
+ /**
29287
29458
  * Convert an IDevice to the flat DeviceSummary shape expected by the
29288
29459
  * device-provider cap router. Shared across all providers.
29289
29460
  */
@@ -29332,6 +29503,7 @@ var BaseDeviceProvider = class extends BaseAddon {
29332
29503
  }];
29333
29504
  }
29334
29505
  async onShutdown() {
29506
+ this.cancelRestoreRetries();
29335
29507
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
29336
29508
  for (const device of devices) try {
29337
29509
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -29349,9 +29521,16 @@ var BaseDeviceProvider = class extends BaseAddon {
29349
29521
  async start() {}
29350
29522
  async stop() {}
29351
29523
  async getStatus() {
29524
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
29525
+ const summary = this.restoreFailureSummary();
29526
+ if (summary === null) return {
29527
+ connected: true,
29528
+ deviceCount: all.length
29529
+ };
29352
29530
  return {
29353
29531
  connected: true,
29354
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
29532
+ deviceCount: all.length,
29533
+ error: summary
29355
29534
  };
29356
29535
  }
29357
29536
  async getDevices() {
@@ -29441,8 +29620,137 @@ var BaseDeviceProvider = class extends BaseAddon {
29441
29620
  };
29442
29621
  }
29443
29622
  async restoreDevices(savedDevices) {
29444
- await this.onRestoreDevices(savedDevices);
29445
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
29623
+ const report = await this.onRestoreDevices(savedDevices);
29624
+ if (savedDevices.length === 0) return;
29625
+ if (report && report.failedCount > 0) {
29626
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
29627
+ return;
29628
+ }
29629
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
29630
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
29631
+ }
29632
+ /** Retry schedule. Overridable (tests use millisecond delays). */
29633
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
29634
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
29635
+ * never re-stampede full-width while the initial pass does (D167). */
29636
+ restoreRetryConcurrency = 4;
29637
+ _restoreRetryScheduler = null;
29638
+ _restoreRetryCompletion = null;
29639
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
29640
+ /** Settles when the background retry rounds finish (or `null` when
29641
+ * nothing failed). Exposed for tests and subclass diagnostics —
29642
+ * boot NEVER awaits this: the runner's post-init handshake goes out
29643
+ * with the devices that restored, and a late success is announced
29644
+ * through the `native-cap-change` → `updateCaps` path. */
29645
+ get restoreRetryCompletion() {
29646
+ return this._restoreRetryCompletion;
29647
+ }
29648
+ /** Devices that exhausted the retry bound this process lifetime. */
29649
+ get permanentRestoreFailures() {
29650
+ return [...this._permanentRestoreFailures.values()];
29651
+ }
29652
+ /** One-line operator-facing summary for `getStatus().error`, or
29653
+ * `null` when every device restored. */
29654
+ restoreFailureSummary() {
29655
+ if (this._permanentRestoreFailures.size === 0) return null;
29656
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
29657
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
29658
+ }
29659
+ cancelRestoreRetries() {
29660
+ this._restoreRetryScheduler?.cancel();
29661
+ this._restoreRetryScheduler = null;
29662
+ }
29663
+ recordPermanentRestoreFailure(failure) {
29664
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
29665
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
29666
+ tags: {
29667
+ deviceId: failure.deviceId,
29668
+ stableId: failure.stableId
29669
+ },
29670
+ meta: {
29671
+ type: failure.type,
29672
+ attempts: failure.attempts,
29673
+ error: failure.lastError
29674
+ }
29675
+ });
29676
+ }
29677
+ scheduleRestoreRetries(failures, attempt) {
29678
+ const scheduler = new DeviceRestoreRetryScheduler({
29679
+ logger: this.ctx.logger,
29680
+ delaysMs: this.restoreRetryDelaysMs,
29681
+ concurrency: this.restoreRetryConcurrency,
29682
+ attempt,
29683
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
29684
+ });
29685
+ this._restoreRetryScheduler = scheduler;
29686
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
29687
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
29688
+ });
29689
+ }
29690
+ /**
29691
+ * Tear down and reconstruct ONE device from its persisted rows — the
29692
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
29693
+ * and no other device this provider owns is disturbed.
29694
+ *
29695
+ * Keyed by `stableId` because the caller's whole reason to be here is that
29696
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
29697
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
29698
+ * whatever number the row carries NOW. The teardown is `decommission` —
29699
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
29700
+ * unregisters native caps, drops the registry entry) — and the rebuild is
29701
+ * the boot restore's own `create()` path, including its pass 2: first-class
29702
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
29703
+ * parent by the cascade and must be re-created explicitly, because only
29704
+ * accessory children come back through `getAccessoryChildren()`.
29705
+ *
29706
+ * Reloading an accessory child directly is refused (no device class) —
29707
+ * reload its parent instead.
29708
+ */
29709
+ async reloadDevice(input) {
29710
+ const { stableId } = input;
29711
+ const devices = this.ctx.kernel.devices;
29712
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
29713
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
29714
+ if (live) await devices.decommission(live.id);
29715
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
29716
+ addonId: this.addonId,
29717
+ stableId
29718
+ });
29719
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
29720
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
29721
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
29722
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
29723
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
29724
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
29725
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
29726
+ for (const row of rows) {
29727
+ if (row.parentDeviceId !== id) continue;
29728
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
29729
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
29730
+ if (!ChildClass) continue;
29731
+ try {
29732
+ await devices.create(row.stableId, ChildClass, {}, id);
29733
+ } catch (err) {
29734
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
29735
+ tags: {
29736
+ deviceId: row.id,
29737
+ stableId: row.stableId
29738
+ },
29739
+ meta: {
29740
+ parentDeviceId: id,
29741
+ error: err instanceof Error ? err.message : String(err)
29742
+ }
29743
+ });
29744
+ }
29745
+ }
29746
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
29747
+ tags: { deviceId: id },
29748
+ meta: {
29749
+ stableId,
29750
+ type: meta.type
29751
+ }
29752
+ });
29753
+ return { deviceId: id };
29446
29754
  }
29447
29755
  /**
29448
29756
  * Restore devices from persisted state. Two-pass:
@@ -29468,55 +29776,108 @@ var BaseDeviceProvider = class extends BaseAddon {
29468
29776
  * accessory-spawn flow handles via the parent's
29469
29777
  * `getAccessoryChildren()`. Override only when the default doesn't
29470
29778
  * fit.
29779
+ *
29780
+ * A row that fails either pass is NOT terminal (D347): it is handed
29781
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
29782
+ * Only after the bound is exhausted is the device marked permanently
29783
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
29784
+ * `getStatus().error`.
29471
29785
  */
29472
29786
  async onRestoreDevices(savedDevices) {
29473
29787
  const restored = /* @__PURE__ */ new Set();
29788
+ const failures = [];
29789
+ const attemptRestore = async (saved) => {
29790
+ if (restored.has(saved.id)) return;
29791
+ const Class = this.deviceClasses[saved.type];
29792
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
29793
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
29794
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
29795
+ restored.add(saved.id);
29796
+ };
29474
29797
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
29475
29798
  const restoreOne = async (saved) => {
29476
- const Class = this.deviceClasses[saved.type];
29477
- if (!Class) {
29799
+ if (!this.deviceClasses[saved.type]) {
29478
29800
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
29479
- tags: { stableId: saved.stableId },
29801
+ tags: {
29802
+ deviceId: saved.id,
29803
+ stableId: saved.stableId
29804
+ },
29480
29805
  meta: { type: saved.type }
29481
29806
  });
29482
29807
  return;
29483
29808
  }
29484
29809
  try {
29485
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
29486
- restored.add(saved.id);
29810
+ await attemptRestore(saved);
29487
29811
  } catch (err) {
29488
- this.ctx.logger.warn("Failed to restore device", {
29489
- tags: { stableId: saved.stableId },
29812
+ const error = err instanceof Error ? err.message : String(err);
29813
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
29814
+ tags: {
29815
+ deviceId: saved.id,
29816
+ stableId: saved.stableId
29817
+ },
29490
29818
  meta: {
29491
29819
  type: saved.type,
29492
- error: err instanceof Error ? err.message : String(err)
29820
+ attempt: 1,
29821
+ error
29493
29822
  }
29494
29823
  });
29824
+ failures.push({
29825
+ saved,
29826
+ error
29827
+ });
29495
29828
  }
29496
29829
  };
29497
29830
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
29831
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
29498
29832
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
29499
29833
  for (const saved of childRows) {
29500
- const Class = this.deviceClasses[saved.type];
29501
- if (!Class) continue;
29834
+ if (!this.deviceClasses[saved.type]) continue;
29502
29835
  if (saved.parentDeviceId === null) continue;
29503
- if (!restored.has(saved.parentDeviceId)) continue;
29504
- try {
29505
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
29506
- restored.add(saved.id);
29507
- } catch (err) {
29508
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
29836
+ if (restored.has(saved.parentDeviceId)) {
29837
+ try {
29838
+ await attemptRestore(saved);
29839
+ } catch (err) {
29840
+ const error = err instanceof Error ? err.message : String(err);
29841
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
29842
+ tags: {
29843
+ deviceId: saved.id,
29844
+ stableId: saved.stableId,
29845
+ parentDeviceId: saved.parentDeviceId
29846
+ },
29847
+ meta: {
29848
+ type: saved.type,
29849
+ attempt: 1,
29850
+ error
29851
+ }
29852
+ });
29853
+ failures.push({
29854
+ saved,
29855
+ error
29856
+ });
29857
+ }
29858
+ continue;
29859
+ }
29860
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
29861
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
29509
29862
  tags: {
29863
+ deviceId: saved.id,
29510
29864
  stableId: saved.stableId,
29511
29865
  parentDeviceId: saved.parentDeviceId
29512
29866
  },
29513
- meta: {
29514
- type: saved.type,
29515
- error: err instanceof Error ? err.message : String(err)
29516
- }
29867
+ meta: { type: saved.type }
29868
+ });
29869
+ failures.push({
29870
+ saved,
29871
+ error: `parent device ${saved.parentDeviceId} not restored`
29517
29872
  });
29873
+ continue;
29518
29874
  }
29519
29875
  }
29876
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
29877
+ return {
29878
+ restoredCount: restored.size,
29879
+ failedCount: failures.length
29880
+ };
29520
29881
  }
29521
29882
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
29522
29883
  toSummary(device) {
@@ -31345,6 +31706,12 @@ Object.freeze({
31345
31706
  addonId: null,
31346
31707
  access: "view"
31347
31708
  },
31709
+ "deviceProvider.reloadDevice": {
31710
+ capName: "device-provider",
31711
+ capScope: "system",
31712
+ addonId: null,
31713
+ access: "create"
31714
+ },
31348
31715
  "deviceProvider.start": {
31349
31716
  capName: "device-provider",
31350
31717
  capScope: "system",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-provider-onvif",
3
- "version": "1.2.58",
3
+ "version": "1.2.59",
4
4
  "description": "ONVIF camera device provider addon for CamStack",
5
5
  "keywords": [
6
6
  "camstack",