@camstack/addon-provider-hikvision 1.2.67 → 1.2.68

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
@@ -13100,6 +13100,35 @@ var deviceProviderCapability = {
13100
13100
  name: string(),
13101
13101
  type: string()
13102
13102
  }))),
13103
+ /**
13104
+ * Tear down and reconstruct ONE device in place from its persisted rows —
13105
+ * touching no other device this provider owns.
13106
+ *
13107
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
13108
+ * migrated numbers: after `swapIds` the runner's live instance still
13109
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
13110
+ * registrations and its log tags), and a live object cannot be renumbered.
13111
+ * Before this method the only flush was restarting the whole owning addon
13112
+ * — which took every camera the provider owns down with it (28 devices
13113
+ * for one migrated camera, measured 2026-09-04, and the morning of the
13114
+ * same day ~27 devices' native caps did not come back on their own).
13115
+ *
13116
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
13117
+ * that changes. The reply carries the id the device answers on NOW.
13118
+ * Implemented once in `BaseDeviceProvider` — decommission the live
13119
+ * instance (if any), then re-create from the persisted row: the same
13120
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
13121
+ * An RPC, never an event: a dropped event would leave the runner writing
13122
+ * against the wrong camera (D8).
13123
+ *
13124
+ * Construction can dial hardware, and the migrated source is
13125
+ * characteristically dead — the timeout covers a full activate window
13126
+ * rather than the 60 s default.
13127
+ */
13128
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
13129
+ kind: "mutation",
13130
+ timeoutMs: 3 * 6e4
13131
+ }),
13103
13132
  supportsDiscovery: method(object({}), boolean()),
13104
13133
  /**
13105
13134
  * Run a network scan. `params` carries optional provider-specific scan
@@ -13427,7 +13456,8 @@ method(object({
13427
13456
  targetId: number()
13428
13457
  }), MigrateDeviceResultSchema, {
13429
13458
  kind: "mutation",
13430
- auth: "admin"
13459
+ auth: "admin",
13460
+ timeoutMs: 12 * 6e4
13431
13461
  }), 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({
13432
13462
  deviceId: number(),
13433
13463
  name: string()
@@ -33252,6 +33282,147 @@ var BaseDevice = class {
33252
33282
  }
33253
33283
  };
33254
33284
  /**
33285
+ * Delays before retry rounds 1..N — the round count IS the bound.
33286
+ * 10 s catches "the hub was busy for a moment"; the full schedule
33287
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
33288
+ * per attempt) covers a device-manager lock held for minutes — the
33289
+ * 2026-09-04 outage's migration hold was ~3.5 min.
33290
+ */
33291
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
33292
+ 1e4,
33293
+ 3e4,
33294
+ 9e4
33295
+ ];
33296
+ /** Abortable sleep — resolves early (never rejects) on abort. */
33297
+ function sleep$1(ms, signal) {
33298
+ return new Promise((resolve) => {
33299
+ if (signal.aborted) {
33300
+ resolve();
33301
+ return;
33302
+ }
33303
+ const onAbort = () => {
33304
+ clearTimeout(timer);
33305
+ resolve();
33306
+ };
33307
+ const timer = setTimeout(() => {
33308
+ signal.removeEventListener("abort", onAbort);
33309
+ resolve();
33310
+ }, ms);
33311
+ timer.unref?.();
33312
+ signal.addEventListener("abort", onAbort, { once: true });
33313
+ });
33314
+ }
33315
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
33316
+ * not reject (callers wrap their own try/catch). */
33317
+ async function runWithConcurrency(items, width, fn) {
33318
+ const queue = [...items];
33319
+ const laneCount = Math.max(1, Math.min(width, queue.length));
33320
+ const lane = async () => {
33321
+ for (;;) {
33322
+ const item = queue.shift();
33323
+ if (item === void 0) return;
33324
+ await fn(item);
33325
+ }
33326
+ };
33327
+ await Promise.all(Array.from({ length: laneCount }, lane));
33328
+ }
33329
+ var DeviceRestoreRetryScheduler = class {
33330
+ #logger;
33331
+ #attempt;
33332
+ #onPermanentFailure;
33333
+ #delaysMs;
33334
+ #concurrency;
33335
+ #now;
33336
+ #abort = new AbortController();
33337
+ constructor(options) {
33338
+ this.#logger = options.logger;
33339
+ this.#attempt = options.attempt;
33340
+ this.#onPermanentFailure = options.onPermanentFailure;
33341
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
33342
+ this.#concurrency = options.concurrency ?? 4;
33343
+ this.#now = options.now ?? Date.now;
33344
+ }
33345
+ /** Stop retrying (shutdown). Pending entries are NOT marked
33346
+ * permanently failed — the next boot restores them from disk. */
33347
+ cancel() {
33348
+ this.#abort.abort();
33349
+ }
33350
+ /**
33351
+ * Run the bounded retry rounds. Resolves when every entry has either
33352
+ * restored, been marked permanently failed, or the scheduler was
33353
+ * cancelled. Never rejects.
33354
+ */
33355
+ async run(initialFailures) {
33356
+ let pending = initialFailures.map((failure) => ({
33357
+ saved: failure.saved,
33358
+ lastError: failure.error,
33359
+ attempts: 1
33360
+ }));
33361
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
33362
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
33363
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
33364
+ if (this.#abort.signal.aborted) break;
33365
+ pending = await this.#runRound(pending, round);
33366
+ }
33367
+ if (this.#abort.signal.aborted) return [];
33368
+ const terminal = pending.map((entry) => ({
33369
+ deviceId: entry.saved.id,
33370
+ stableId: entry.saved.stableId,
33371
+ type: String(entry.saved.type),
33372
+ attempts: entry.attempts,
33373
+ lastError: entry.lastError,
33374
+ failedAt: this.#now()
33375
+ }));
33376
+ for (const failure of terminal) this.#onPermanentFailure(failure);
33377
+ return terminal;
33378
+ }
33379
+ /** One retry round: parents first (phase 0), then hub-adopted
33380
+ * children (phase 1) — a child's attempt depends on its parent
33381
+ * having landed, exactly like the initial two-pass restore. */
33382
+ async #runRound(pending, round) {
33383
+ const next = [];
33384
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
33385
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
33386
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
33387
+ if (this.#abort.signal.aborted) {
33388
+ next.push(entry);
33389
+ return;
33390
+ }
33391
+ const attemptNo = entry.attempts + 1;
33392
+ try {
33393
+ await this.#attempt(entry.saved);
33394
+ this.#logger.info("Device restored on retry", {
33395
+ tags: {
33396
+ deviceId: entry.saved.id,
33397
+ stableId: entry.saved.stableId
33398
+ },
33399
+ meta: { attempt: attemptNo }
33400
+ });
33401
+ } catch (err) {
33402
+ const lastError = err instanceof Error ? err.message : String(err);
33403
+ const remainingRetries = this.#delaysMs.length - (round + 1);
33404
+ this.#logger.warn("Device restore retry failed", {
33405
+ tags: {
33406
+ deviceId: entry.saved.id,
33407
+ stableId: entry.saved.stableId
33408
+ },
33409
+ meta: {
33410
+ attempt: attemptNo,
33411
+ remainingRetries,
33412
+ error: lastError
33413
+ }
33414
+ });
33415
+ next.push({
33416
+ saved: entry.saved,
33417
+ lastError,
33418
+ attempts: attemptNo
33419
+ });
33420
+ }
33421
+ });
33422
+ return next;
33423
+ }
33424
+ };
33425
+ /**
33255
33426
  * Convert an IDevice to the flat DeviceSummary shape expected by the
33256
33427
  * device-provider cap router. Shared across all providers.
33257
33428
  */
@@ -33300,6 +33471,7 @@ var BaseDeviceProvider = class extends BaseAddon {
33300
33471
  }];
33301
33472
  }
33302
33473
  async onShutdown() {
33474
+ this.cancelRestoreRetries();
33303
33475
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
33304
33476
  for (const device of devices) try {
33305
33477
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -33317,9 +33489,16 @@ var BaseDeviceProvider = class extends BaseAddon {
33317
33489
  async start() {}
33318
33490
  async stop() {}
33319
33491
  async getStatus() {
33492
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
33493
+ const summary = this.restoreFailureSummary();
33494
+ if (summary === null) return {
33495
+ connected: true,
33496
+ deviceCount: all.length
33497
+ };
33320
33498
  return {
33321
33499
  connected: true,
33322
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
33500
+ deviceCount: all.length,
33501
+ error: summary
33323
33502
  };
33324
33503
  }
33325
33504
  async getDevices() {
@@ -33409,8 +33588,137 @@ var BaseDeviceProvider = class extends BaseAddon {
33409
33588
  };
33410
33589
  }
33411
33590
  async restoreDevices(savedDevices) {
33412
- await this.onRestoreDevices(savedDevices);
33413
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
33591
+ const report = await this.onRestoreDevices(savedDevices);
33592
+ if (savedDevices.length === 0) return;
33593
+ if (report && report.failedCount > 0) {
33594
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
33595
+ return;
33596
+ }
33597
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
33598
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
33599
+ }
33600
+ /** Retry schedule. Overridable (tests use millisecond delays). */
33601
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
33602
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
33603
+ * never re-stampede full-width while the initial pass does (D167). */
33604
+ restoreRetryConcurrency = 4;
33605
+ _restoreRetryScheduler = null;
33606
+ _restoreRetryCompletion = null;
33607
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
33608
+ /** Settles when the background retry rounds finish (or `null` when
33609
+ * nothing failed). Exposed for tests and subclass diagnostics —
33610
+ * boot NEVER awaits this: the runner's post-init handshake goes out
33611
+ * with the devices that restored, and a late success is announced
33612
+ * through the `native-cap-change` → `updateCaps` path. */
33613
+ get restoreRetryCompletion() {
33614
+ return this._restoreRetryCompletion;
33615
+ }
33616
+ /** Devices that exhausted the retry bound this process lifetime. */
33617
+ get permanentRestoreFailures() {
33618
+ return [...this._permanentRestoreFailures.values()];
33619
+ }
33620
+ /** One-line operator-facing summary for `getStatus().error`, or
33621
+ * `null` when every device restored. */
33622
+ restoreFailureSummary() {
33623
+ if (this._permanentRestoreFailures.size === 0) return null;
33624
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
33625
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
33626
+ }
33627
+ cancelRestoreRetries() {
33628
+ this._restoreRetryScheduler?.cancel();
33629
+ this._restoreRetryScheduler = null;
33630
+ }
33631
+ recordPermanentRestoreFailure(failure) {
33632
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
33633
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
33634
+ tags: {
33635
+ deviceId: failure.deviceId,
33636
+ stableId: failure.stableId
33637
+ },
33638
+ meta: {
33639
+ type: failure.type,
33640
+ attempts: failure.attempts,
33641
+ error: failure.lastError
33642
+ }
33643
+ });
33644
+ }
33645
+ scheduleRestoreRetries(failures, attempt) {
33646
+ const scheduler = new DeviceRestoreRetryScheduler({
33647
+ logger: this.ctx.logger,
33648
+ delaysMs: this.restoreRetryDelaysMs,
33649
+ concurrency: this.restoreRetryConcurrency,
33650
+ attempt,
33651
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
33652
+ });
33653
+ this._restoreRetryScheduler = scheduler;
33654
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
33655
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
33656
+ });
33657
+ }
33658
+ /**
33659
+ * Tear down and reconstruct ONE device from its persisted rows — the
33660
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
33661
+ * and no other device this provider owns is disturbed.
33662
+ *
33663
+ * Keyed by `stableId` because the caller's whole reason to be here is that
33664
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
33665
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
33666
+ * whatever number the row carries NOW. The teardown is `decommission` —
33667
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
33668
+ * unregisters native caps, drops the registry entry) — and the rebuild is
33669
+ * the boot restore's own `create()` path, including its pass 2: first-class
33670
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
33671
+ * parent by the cascade and must be re-created explicitly, because only
33672
+ * accessory children come back through `getAccessoryChildren()`.
33673
+ *
33674
+ * Reloading an accessory child directly is refused (no device class) —
33675
+ * reload its parent instead.
33676
+ */
33677
+ async reloadDevice(input) {
33678
+ const { stableId } = input;
33679
+ const devices = this.ctx.kernel.devices;
33680
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
33681
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
33682
+ if (live) await devices.decommission(live.id);
33683
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
33684
+ addonId: this.addonId,
33685
+ stableId
33686
+ });
33687
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
33688
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
33689
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
33690
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
33691
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
33692
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
33693
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
33694
+ for (const row of rows) {
33695
+ if (row.parentDeviceId !== id) continue;
33696
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
33697
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
33698
+ if (!ChildClass) continue;
33699
+ try {
33700
+ await devices.create(row.stableId, ChildClass, {}, id);
33701
+ } catch (err) {
33702
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
33703
+ tags: {
33704
+ deviceId: row.id,
33705
+ stableId: row.stableId
33706
+ },
33707
+ meta: {
33708
+ parentDeviceId: id,
33709
+ error: err instanceof Error ? err.message : String(err)
33710
+ }
33711
+ });
33712
+ }
33713
+ }
33714
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
33715
+ tags: { deviceId: id },
33716
+ meta: {
33717
+ stableId,
33718
+ type: meta.type
33719
+ }
33720
+ });
33721
+ return { deviceId: id };
33414
33722
  }
33415
33723
  /**
33416
33724
  * Restore devices from persisted state. Two-pass:
@@ -33436,55 +33744,108 @@ var BaseDeviceProvider = class extends BaseAddon {
33436
33744
  * accessory-spawn flow handles via the parent's
33437
33745
  * `getAccessoryChildren()`. Override only when the default doesn't
33438
33746
  * fit.
33747
+ *
33748
+ * A row that fails either pass is NOT terminal (D347): it is handed
33749
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
33750
+ * Only after the bound is exhausted is the device marked permanently
33751
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
33752
+ * `getStatus().error`.
33439
33753
  */
33440
33754
  async onRestoreDevices(savedDevices) {
33441
33755
  const restored = /* @__PURE__ */ new Set();
33756
+ const failures = [];
33757
+ const attemptRestore = async (saved) => {
33758
+ if (restored.has(saved.id)) return;
33759
+ const Class = this.deviceClasses[saved.type];
33760
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
33761
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
33762
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33763
+ restored.add(saved.id);
33764
+ };
33442
33765
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
33443
33766
  const restoreOne = async (saved) => {
33444
- const Class = this.deviceClasses[saved.type];
33445
- if (!Class) {
33767
+ if (!this.deviceClasses[saved.type]) {
33446
33768
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
33447
- tags: { stableId: saved.stableId },
33769
+ tags: {
33770
+ deviceId: saved.id,
33771
+ stableId: saved.stableId
33772
+ },
33448
33773
  meta: { type: saved.type }
33449
33774
  });
33450
33775
  return;
33451
33776
  }
33452
33777
  try {
33453
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
33454
- restored.add(saved.id);
33778
+ await attemptRestore(saved);
33455
33779
  } catch (err) {
33456
- this.ctx.logger.warn("Failed to restore device", {
33457
- tags: { stableId: saved.stableId },
33780
+ const error = err instanceof Error ? err.message : String(err);
33781
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
33782
+ tags: {
33783
+ deviceId: saved.id,
33784
+ stableId: saved.stableId
33785
+ },
33458
33786
  meta: {
33459
33787
  type: saved.type,
33460
- error: err instanceof Error ? err.message : String(err)
33788
+ attempt: 1,
33789
+ error
33461
33790
  }
33462
33791
  });
33792
+ failures.push({
33793
+ saved,
33794
+ error
33795
+ });
33463
33796
  }
33464
33797
  };
33465
33798
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
33799
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
33466
33800
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
33467
33801
  for (const saved of childRows) {
33468
- const Class = this.deviceClasses[saved.type];
33469
- if (!Class) continue;
33802
+ if (!this.deviceClasses[saved.type]) continue;
33470
33803
  if (saved.parentDeviceId === null) continue;
33471
- if (!restored.has(saved.parentDeviceId)) continue;
33472
- try {
33473
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33474
- restored.add(saved.id);
33475
- } catch (err) {
33476
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
33804
+ if (restored.has(saved.parentDeviceId)) {
33805
+ try {
33806
+ await attemptRestore(saved);
33807
+ } catch (err) {
33808
+ const error = err instanceof Error ? err.message : String(err);
33809
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
33810
+ tags: {
33811
+ deviceId: saved.id,
33812
+ stableId: saved.stableId,
33813
+ parentDeviceId: saved.parentDeviceId
33814
+ },
33815
+ meta: {
33816
+ type: saved.type,
33817
+ attempt: 1,
33818
+ error
33819
+ }
33820
+ });
33821
+ failures.push({
33822
+ saved,
33823
+ error
33824
+ });
33825
+ }
33826
+ continue;
33827
+ }
33828
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
33829
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
33477
33830
  tags: {
33831
+ deviceId: saved.id,
33478
33832
  stableId: saved.stableId,
33479
33833
  parentDeviceId: saved.parentDeviceId
33480
33834
  },
33481
- meta: {
33482
- type: saved.type,
33483
- error: err instanceof Error ? err.message : String(err)
33484
- }
33835
+ meta: { type: saved.type }
33485
33836
  });
33837
+ failures.push({
33838
+ saved,
33839
+ error: `parent device ${saved.parentDeviceId} not restored`
33840
+ });
33841
+ continue;
33486
33842
  }
33487
33843
  }
33844
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
33845
+ return {
33846
+ restoredCount: restored.size,
33847
+ failedCount: failures.length
33848
+ };
33488
33849
  }
33489
33850
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
33490
33851
  toSummary(device) {
@@ -35465,6 +35826,12 @@ Object.freeze({
35465
35826
  addonId: null,
35466
35827
  access: "view"
35467
35828
  },
35829
+ "deviceProvider.reloadDevice": {
35830
+ capName: "device-provider",
35831
+ capScope: "system",
35832
+ addonId: null,
35833
+ access: "create"
35834
+ },
35468
35835
  "deviceProvider.start": {
35469
35836
  capName: "device-provider",
35470
35837
  capScope: "system",
package/dist/addon.mjs CHANGED
@@ -13101,6 +13101,35 @@ var deviceProviderCapability = {
13101
13101
  name: string(),
13102
13102
  type: string()
13103
13103
  }))),
13104
+ /**
13105
+ * Tear down and reconstruct ONE device in place from its persisted rows —
13106
+ * touching no other device this provider owns.
13107
+ *
13108
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
13109
+ * migrated numbers: after `swapIds` the runner's live instance still
13110
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
13111
+ * registrations and its log tags), and a live object cannot be renumbered.
13112
+ * Before this method the only flush was restarting the whole owning addon
13113
+ * — which took every camera the provider owns down with it (28 devices
13114
+ * for one migrated camera, measured 2026-09-04, and the morning of the
13115
+ * same day ~27 devices' native caps did not come back on their own).
13116
+ *
13117
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
13118
+ * that changes. The reply carries the id the device answers on NOW.
13119
+ * Implemented once in `BaseDeviceProvider` — decommission the live
13120
+ * instance (if any), then re-create from the persisted row: the same
13121
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
13122
+ * An RPC, never an event: a dropped event would leave the runner writing
13123
+ * against the wrong camera (D8).
13124
+ *
13125
+ * Construction can dial hardware, and the migrated source is
13126
+ * characteristically dead — the timeout covers a full activate window
13127
+ * rather than the 60 s default.
13128
+ */
13129
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
13130
+ kind: "mutation",
13131
+ timeoutMs: 3 * 6e4
13132
+ }),
13104
13133
  supportsDiscovery: method(object({}), boolean()),
13105
13134
  /**
13106
13135
  * Run a network scan. `params` carries optional provider-specific scan
@@ -13428,7 +13457,8 @@ method(object({
13428
13457
  targetId: number()
13429
13458
  }), MigrateDeviceResultSchema, {
13430
13459
  kind: "mutation",
13431
- auth: "admin"
13460
+ auth: "admin",
13461
+ timeoutMs: 12 * 6e4
13432
13462
  }), 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({
13433
13463
  deviceId: number(),
13434
13464
  name: string()
@@ -33253,6 +33283,147 @@ var BaseDevice = class {
33253
33283
  }
33254
33284
  };
33255
33285
  /**
33286
+ * Delays before retry rounds 1..N — the round count IS the bound.
33287
+ * 10 s catches "the hub was busy for a moment"; the full schedule
33288
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
33289
+ * per attempt) covers a device-manager lock held for minutes — the
33290
+ * 2026-09-04 outage's migration hold was ~3.5 min.
33291
+ */
33292
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
33293
+ 1e4,
33294
+ 3e4,
33295
+ 9e4
33296
+ ];
33297
+ /** Abortable sleep — resolves early (never rejects) on abort. */
33298
+ function sleep$1(ms, signal) {
33299
+ return new Promise((resolve) => {
33300
+ if (signal.aborted) {
33301
+ resolve();
33302
+ return;
33303
+ }
33304
+ const onAbort = () => {
33305
+ clearTimeout(timer);
33306
+ resolve();
33307
+ };
33308
+ const timer = setTimeout(() => {
33309
+ signal.removeEventListener("abort", onAbort);
33310
+ resolve();
33311
+ }, ms);
33312
+ timer.unref?.();
33313
+ signal.addEventListener("abort", onAbort, { once: true });
33314
+ });
33315
+ }
33316
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
33317
+ * not reject (callers wrap their own try/catch). */
33318
+ async function runWithConcurrency(items, width, fn) {
33319
+ const queue = [...items];
33320
+ const laneCount = Math.max(1, Math.min(width, queue.length));
33321
+ const lane = async () => {
33322
+ for (;;) {
33323
+ const item = queue.shift();
33324
+ if (item === void 0) return;
33325
+ await fn(item);
33326
+ }
33327
+ };
33328
+ await Promise.all(Array.from({ length: laneCount }, lane));
33329
+ }
33330
+ var DeviceRestoreRetryScheduler = class {
33331
+ #logger;
33332
+ #attempt;
33333
+ #onPermanentFailure;
33334
+ #delaysMs;
33335
+ #concurrency;
33336
+ #now;
33337
+ #abort = new AbortController();
33338
+ constructor(options) {
33339
+ this.#logger = options.logger;
33340
+ this.#attempt = options.attempt;
33341
+ this.#onPermanentFailure = options.onPermanentFailure;
33342
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
33343
+ this.#concurrency = options.concurrency ?? 4;
33344
+ this.#now = options.now ?? Date.now;
33345
+ }
33346
+ /** Stop retrying (shutdown). Pending entries are NOT marked
33347
+ * permanently failed — the next boot restores them from disk. */
33348
+ cancel() {
33349
+ this.#abort.abort();
33350
+ }
33351
+ /**
33352
+ * Run the bounded retry rounds. Resolves when every entry has either
33353
+ * restored, been marked permanently failed, or the scheduler was
33354
+ * cancelled. Never rejects.
33355
+ */
33356
+ async run(initialFailures) {
33357
+ let pending = initialFailures.map((failure) => ({
33358
+ saved: failure.saved,
33359
+ lastError: failure.error,
33360
+ attempts: 1
33361
+ }));
33362
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
33363
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
33364
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
33365
+ if (this.#abort.signal.aborted) break;
33366
+ pending = await this.#runRound(pending, round);
33367
+ }
33368
+ if (this.#abort.signal.aborted) return [];
33369
+ const terminal = pending.map((entry) => ({
33370
+ deviceId: entry.saved.id,
33371
+ stableId: entry.saved.stableId,
33372
+ type: String(entry.saved.type),
33373
+ attempts: entry.attempts,
33374
+ lastError: entry.lastError,
33375
+ failedAt: this.#now()
33376
+ }));
33377
+ for (const failure of terminal) this.#onPermanentFailure(failure);
33378
+ return terminal;
33379
+ }
33380
+ /** One retry round: parents first (phase 0), then hub-adopted
33381
+ * children (phase 1) — a child's attempt depends on its parent
33382
+ * having landed, exactly like the initial two-pass restore. */
33383
+ async #runRound(pending, round) {
33384
+ const next = [];
33385
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
33386
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
33387
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
33388
+ if (this.#abort.signal.aborted) {
33389
+ next.push(entry);
33390
+ return;
33391
+ }
33392
+ const attemptNo = entry.attempts + 1;
33393
+ try {
33394
+ await this.#attempt(entry.saved);
33395
+ this.#logger.info("Device restored on retry", {
33396
+ tags: {
33397
+ deviceId: entry.saved.id,
33398
+ stableId: entry.saved.stableId
33399
+ },
33400
+ meta: { attempt: attemptNo }
33401
+ });
33402
+ } catch (err) {
33403
+ const lastError = err instanceof Error ? err.message : String(err);
33404
+ const remainingRetries = this.#delaysMs.length - (round + 1);
33405
+ this.#logger.warn("Device restore retry failed", {
33406
+ tags: {
33407
+ deviceId: entry.saved.id,
33408
+ stableId: entry.saved.stableId
33409
+ },
33410
+ meta: {
33411
+ attempt: attemptNo,
33412
+ remainingRetries,
33413
+ error: lastError
33414
+ }
33415
+ });
33416
+ next.push({
33417
+ saved: entry.saved,
33418
+ lastError,
33419
+ attempts: attemptNo
33420
+ });
33421
+ }
33422
+ });
33423
+ return next;
33424
+ }
33425
+ };
33426
+ /**
33256
33427
  * Convert an IDevice to the flat DeviceSummary shape expected by the
33257
33428
  * device-provider cap router. Shared across all providers.
33258
33429
  */
@@ -33301,6 +33472,7 @@ var BaseDeviceProvider = class extends BaseAddon {
33301
33472
  }];
33302
33473
  }
33303
33474
  async onShutdown() {
33475
+ this.cancelRestoreRetries();
33304
33476
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
33305
33477
  for (const device of devices) try {
33306
33478
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -33318,9 +33490,16 @@ var BaseDeviceProvider = class extends BaseAddon {
33318
33490
  async start() {}
33319
33491
  async stop() {}
33320
33492
  async getStatus() {
33493
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
33494
+ const summary = this.restoreFailureSummary();
33495
+ if (summary === null) return {
33496
+ connected: true,
33497
+ deviceCount: all.length
33498
+ };
33321
33499
  return {
33322
33500
  connected: true,
33323
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
33501
+ deviceCount: all.length,
33502
+ error: summary
33324
33503
  };
33325
33504
  }
33326
33505
  async getDevices() {
@@ -33410,8 +33589,137 @@ var BaseDeviceProvider = class extends BaseAddon {
33410
33589
  };
33411
33590
  }
33412
33591
  async restoreDevices(savedDevices) {
33413
- await this.onRestoreDevices(savedDevices);
33414
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
33592
+ const report = await this.onRestoreDevices(savedDevices);
33593
+ if (savedDevices.length === 0) return;
33594
+ if (report && report.failedCount > 0) {
33595
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
33596
+ return;
33597
+ }
33598
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
33599
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
33600
+ }
33601
+ /** Retry schedule. Overridable (tests use millisecond delays). */
33602
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
33603
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
33604
+ * never re-stampede full-width while the initial pass does (D167). */
33605
+ restoreRetryConcurrency = 4;
33606
+ _restoreRetryScheduler = null;
33607
+ _restoreRetryCompletion = null;
33608
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
33609
+ /** Settles when the background retry rounds finish (or `null` when
33610
+ * nothing failed). Exposed for tests and subclass diagnostics —
33611
+ * boot NEVER awaits this: the runner's post-init handshake goes out
33612
+ * with the devices that restored, and a late success is announced
33613
+ * through the `native-cap-change` → `updateCaps` path. */
33614
+ get restoreRetryCompletion() {
33615
+ return this._restoreRetryCompletion;
33616
+ }
33617
+ /** Devices that exhausted the retry bound this process lifetime. */
33618
+ get permanentRestoreFailures() {
33619
+ return [...this._permanentRestoreFailures.values()];
33620
+ }
33621
+ /** One-line operator-facing summary for `getStatus().error`, or
33622
+ * `null` when every device restored. */
33623
+ restoreFailureSummary() {
33624
+ if (this._permanentRestoreFailures.size === 0) return null;
33625
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
33626
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
33627
+ }
33628
+ cancelRestoreRetries() {
33629
+ this._restoreRetryScheduler?.cancel();
33630
+ this._restoreRetryScheduler = null;
33631
+ }
33632
+ recordPermanentRestoreFailure(failure) {
33633
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
33634
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
33635
+ tags: {
33636
+ deviceId: failure.deviceId,
33637
+ stableId: failure.stableId
33638
+ },
33639
+ meta: {
33640
+ type: failure.type,
33641
+ attempts: failure.attempts,
33642
+ error: failure.lastError
33643
+ }
33644
+ });
33645
+ }
33646
+ scheduleRestoreRetries(failures, attempt) {
33647
+ const scheduler = new DeviceRestoreRetryScheduler({
33648
+ logger: this.ctx.logger,
33649
+ delaysMs: this.restoreRetryDelaysMs,
33650
+ concurrency: this.restoreRetryConcurrency,
33651
+ attempt,
33652
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
33653
+ });
33654
+ this._restoreRetryScheduler = scheduler;
33655
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
33656
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
33657
+ });
33658
+ }
33659
+ /**
33660
+ * Tear down and reconstruct ONE device from its persisted rows — the
33661
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
33662
+ * and no other device this provider owns is disturbed.
33663
+ *
33664
+ * Keyed by `stableId` because the caller's whole reason to be here is that
33665
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
33666
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
33667
+ * whatever number the row carries NOW. The teardown is `decommission` —
33668
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
33669
+ * unregisters native caps, drops the registry entry) — and the rebuild is
33670
+ * the boot restore's own `create()` path, including its pass 2: first-class
33671
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
33672
+ * parent by the cascade and must be re-created explicitly, because only
33673
+ * accessory children come back through `getAccessoryChildren()`.
33674
+ *
33675
+ * Reloading an accessory child directly is refused (no device class) —
33676
+ * reload its parent instead.
33677
+ */
33678
+ async reloadDevice(input) {
33679
+ const { stableId } = input;
33680
+ const devices = this.ctx.kernel.devices;
33681
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
33682
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
33683
+ if (live) await devices.decommission(live.id);
33684
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
33685
+ addonId: this.addonId,
33686
+ stableId
33687
+ });
33688
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
33689
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
33690
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
33691
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
33692
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
33693
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
33694
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
33695
+ for (const row of rows) {
33696
+ if (row.parentDeviceId !== id) continue;
33697
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
33698
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
33699
+ if (!ChildClass) continue;
33700
+ try {
33701
+ await devices.create(row.stableId, ChildClass, {}, id);
33702
+ } catch (err) {
33703
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
33704
+ tags: {
33705
+ deviceId: row.id,
33706
+ stableId: row.stableId
33707
+ },
33708
+ meta: {
33709
+ parentDeviceId: id,
33710
+ error: err instanceof Error ? err.message : String(err)
33711
+ }
33712
+ });
33713
+ }
33714
+ }
33715
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
33716
+ tags: { deviceId: id },
33717
+ meta: {
33718
+ stableId,
33719
+ type: meta.type
33720
+ }
33721
+ });
33722
+ return { deviceId: id };
33415
33723
  }
33416
33724
  /**
33417
33725
  * Restore devices from persisted state. Two-pass:
@@ -33437,55 +33745,108 @@ var BaseDeviceProvider = class extends BaseAddon {
33437
33745
  * accessory-spawn flow handles via the parent's
33438
33746
  * `getAccessoryChildren()`. Override only when the default doesn't
33439
33747
  * fit.
33748
+ *
33749
+ * A row that fails either pass is NOT terminal (D347): it is handed
33750
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
33751
+ * Only after the bound is exhausted is the device marked permanently
33752
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
33753
+ * `getStatus().error`.
33440
33754
  */
33441
33755
  async onRestoreDevices(savedDevices) {
33442
33756
  const restored = /* @__PURE__ */ new Set();
33757
+ const failures = [];
33758
+ const attemptRestore = async (saved) => {
33759
+ if (restored.has(saved.id)) return;
33760
+ const Class = this.deviceClasses[saved.type];
33761
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
33762
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
33763
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33764
+ restored.add(saved.id);
33765
+ };
33443
33766
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
33444
33767
  const restoreOne = async (saved) => {
33445
- const Class = this.deviceClasses[saved.type];
33446
- if (!Class) {
33768
+ if (!this.deviceClasses[saved.type]) {
33447
33769
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
33448
- tags: { stableId: saved.stableId },
33770
+ tags: {
33771
+ deviceId: saved.id,
33772
+ stableId: saved.stableId
33773
+ },
33449
33774
  meta: { type: saved.type }
33450
33775
  });
33451
33776
  return;
33452
33777
  }
33453
33778
  try {
33454
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
33455
- restored.add(saved.id);
33779
+ await attemptRestore(saved);
33456
33780
  } catch (err) {
33457
- this.ctx.logger.warn("Failed to restore device", {
33458
- tags: { stableId: saved.stableId },
33781
+ const error = err instanceof Error ? err.message : String(err);
33782
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
33783
+ tags: {
33784
+ deviceId: saved.id,
33785
+ stableId: saved.stableId
33786
+ },
33459
33787
  meta: {
33460
33788
  type: saved.type,
33461
- error: err instanceof Error ? err.message : String(err)
33789
+ attempt: 1,
33790
+ error
33462
33791
  }
33463
33792
  });
33793
+ failures.push({
33794
+ saved,
33795
+ error
33796
+ });
33464
33797
  }
33465
33798
  };
33466
33799
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
33800
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
33467
33801
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
33468
33802
  for (const saved of childRows) {
33469
- const Class = this.deviceClasses[saved.type];
33470
- if (!Class) continue;
33803
+ if (!this.deviceClasses[saved.type]) continue;
33471
33804
  if (saved.parentDeviceId === null) continue;
33472
- if (!restored.has(saved.parentDeviceId)) continue;
33473
- try {
33474
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33475
- restored.add(saved.id);
33476
- } catch (err) {
33477
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
33805
+ if (restored.has(saved.parentDeviceId)) {
33806
+ try {
33807
+ await attemptRestore(saved);
33808
+ } catch (err) {
33809
+ const error = err instanceof Error ? err.message : String(err);
33810
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
33811
+ tags: {
33812
+ deviceId: saved.id,
33813
+ stableId: saved.stableId,
33814
+ parentDeviceId: saved.parentDeviceId
33815
+ },
33816
+ meta: {
33817
+ type: saved.type,
33818
+ attempt: 1,
33819
+ error
33820
+ }
33821
+ });
33822
+ failures.push({
33823
+ saved,
33824
+ error
33825
+ });
33826
+ }
33827
+ continue;
33828
+ }
33829
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
33830
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
33478
33831
  tags: {
33832
+ deviceId: saved.id,
33479
33833
  stableId: saved.stableId,
33480
33834
  parentDeviceId: saved.parentDeviceId
33481
33835
  },
33482
- meta: {
33483
- type: saved.type,
33484
- error: err instanceof Error ? err.message : String(err)
33485
- }
33836
+ meta: { type: saved.type }
33486
33837
  });
33838
+ failures.push({
33839
+ saved,
33840
+ error: `parent device ${saved.parentDeviceId} not restored`
33841
+ });
33842
+ continue;
33487
33843
  }
33488
33844
  }
33845
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
33846
+ return {
33847
+ restoredCount: restored.size,
33848
+ failedCount: failures.length
33849
+ };
33489
33850
  }
33490
33851
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
33491
33852
  toSummary(device) {
@@ -35466,6 +35827,12 @@ Object.freeze({
35466
35827
  addonId: null,
35467
35828
  access: "view"
35468
35829
  },
35830
+ "deviceProvider.reloadDevice": {
35831
+ capName: "device-provider",
35832
+ capScope: "system",
35833
+ addonId: null,
35834
+ access: "create"
35835
+ },
35469
35836
  "deviceProvider.start": {
35470
35837
  capName: "device-provider",
35471
35838
  capScope: "system",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-provider-hikvision",
3
- "version": "1.2.67",
3
+ "version": "1.2.68",
4
4
  "description": "Hikvision camera device provider addon for CamStack — ISAPI over HTTP(S) with digest auth (snapshot, alarm stream, RTSP discovery)",
5
5
  "keywords": [
6
6
  "camstack",