@camstack/addon-provider-velux 0.2.59 → 0.2.60

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
@@ -12812,6 +12812,35 @@ var deviceProviderCapability = {
12812
12812
  name: string(),
12813
12813
  type: string()
12814
12814
  }))),
12815
+ /**
12816
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12817
+ * touching no other device this provider owns.
12818
+ *
12819
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12820
+ * migrated numbers: after `swapIds` the runner's live instance still
12821
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12822
+ * registrations and its log tags), and a live object cannot be renumbered.
12823
+ * Before this method the only flush was restarting the whole owning addon
12824
+ * — which took every camera the provider owns down with it (28 devices
12825
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12826
+ * same day ~27 devices' native caps did not come back on their own).
12827
+ *
12828
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12829
+ * that changes. The reply carries the id the device answers on NOW.
12830
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12831
+ * instance (if any), then re-create from the persisted row: the same
12832
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12833
+ * An RPC, never an event: a dropped event would leave the runner writing
12834
+ * against the wrong camera (D8).
12835
+ *
12836
+ * Construction can dial hardware, and the migrated source is
12837
+ * characteristically dead — the timeout covers a full activate window
12838
+ * rather than the 60 s default.
12839
+ */
12840
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12841
+ kind: "mutation",
12842
+ timeoutMs: 3 * 6e4
12843
+ }),
12815
12844
  supportsDiscovery: method(object({}), boolean()),
12816
12845
  /**
12817
12846
  * Run a network scan. `params` carries optional provider-specific scan
@@ -13139,7 +13168,8 @@ method(object({
13139
13168
  targetId: number()
13140
13169
  }), MigrateDeviceResultSchema, {
13141
13170
  kind: "mutation",
13142
- auth: "admin"
13171
+ auth: "admin",
13172
+ timeoutMs: 12 * 6e4
13143
13173
  }), 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({
13144
13174
  deviceId: number(),
13145
13175
  name: string()
@@ -32495,6 +32525,147 @@ var BaseDevice = class {
32495
32525
  }
32496
32526
  };
32497
32527
  /**
32528
+ * Delays before retry rounds 1..N — the round count IS the bound.
32529
+ * 10 s catches "the hub was busy for a moment"; the full schedule
32530
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
32531
+ * per attempt) covers a device-manager lock held for minutes — the
32532
+ * 2026-09-04 outage's migration hold was ~3.5 min.
32533
+ */
32534
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
32535
+ 1e4,
32536
+ 3e4,
32537
+ 9e4
32538
+ ];
32539
+ /** Abortable sleep — resolves early (never rejects) on abort. */
32540
+ function sleep$1(ms, signal) {
32541
+ return new Promise((resolve) => {
32542
+ if (signal.aborted) {
32543
+ resolve();
32544
+ return;
32545
+ }
32546
+ const onAbort = () => {
32547
+ clearTimeout(timer);
32548
+ resolve();
32549
+ };
32550
+ const timer = setTimeout(() => {
32551
+ signal.removeEventListener("abort", onAbort);
32552
+ resolve();
32553
+ }, ms);
32554
+ timer.unref?.();
32555
+ signal.addEventListener("abort", onAbort, { once: true });
32556
+ });
32557
+ }
32558
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
32559
+ * not reject (callers wrap their own try/catch). */
32560
+ async function runWithConcurrency(items, width, fn) {
32561
+ const queue = [...items];
32562
+ const laneCount = Math.max(1, Math.min(width, queue.length));
32563
+ const lane = async () => {
32564
+ for (;;) {
32565
+ const item = queue.shift();
32566
+ if (item === void 0) return;
32567
+ await fn(item);
32568
+ }
32569
+ };
32570
+ await Promise.all(Array.from({ length: laneCount }, lane));
32571
+ }
32572
+ var DeviceRestoreRetryScheduler = class {
32573
+ #logger;
32574
+ #attempt;
32575
+ #onPermanentFailure;
32576
+ #delaysMs;
32577
+ #concurrency;
32578
+ #now;
32579
+ #abort = new AbortController();
32580
+ constructor(options) {
32581
+ this.#logger = options.logger;
32582
+ this.#attempt = options.attempt;
32583
+ this.#onPermanentFailure = options.onPermanentFailure;
32584
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
32585
+ this.#concurrency = options.concurrency ?? 4;
32586
+ this.#now = options.now ?? Date.now;
32587
+ }
32588
+ /** Stop retrying (shutdown). Pending entries are NOT marked
32589
+ * permanently failed — the next boot restores them from disk. */
32590
+ cancel() {
32591
+ this.#abort.abort();
32592
+ }
32593
+ /**
32594
+ * Run the bounded retry rounds. Resolves when every entry has either
32595
+ * restored, been marked permanently failed, or the scheduler was
32596
+ * cancelled. Never rejects.
32597
+ */
32598
+ async run(initialFailures) {
32599
+ let pending = initialFailures.map((failure) => ({
32600
+ saved: failure.saved,
32601
+ lastError: failure.error,
32602
+ attempts: 1
32603
+ }));
32604
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
32605
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
32606
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
32607
+ if (this.#abort.signal.aborted) break;
32608
+ pending = await this.#runRound(pending, round);
32609
+ }
32610
+ if (this.#abort.signal.aborted) return [];
32611
+ const terminal = pending.map((entry) => ({
32612
+ deviceId: entry.saved.id,
32613
+ stableId: entry.saved.stableId,
32614
+ type: String(entry.saved.type),
32615
+ attempts: entry.attempts,
32616
+ lastError: entry.lastError,
32617
+ failedAt: this.#now()
32618
+ }));
32619
+ for (const failure of terminal) this.#onPermanentFailure(failure);
32620
+ return terminal;
32621
+ }
32622
+ /** One retry round: parents first (phase 0), then hub-adopted
32623
+ * children (phase 1) — a child's attempt depends on its parent
32624
+ * having landed, exactly like the initial two-pass restore. */
32625
+ async #runRound(pending, round) {
32626
+ const next = [];
32627
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
32628
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
32629
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
32630
+ if (this.#abort.signal.aborted) {
32631
+ next.push(entry);
32632
+ return;
32633
+ }
32634
+ const attemptNo = entry.attempts + 1;
32635
+ try {
32636
+ await this.#attempt(entry.saved);
32637
+ this.#logger.info("Device restored on retry", {
32638
+ tags: {
32639
+ deviceId: entry.saved.id,
32640
+ stableId: entry.saved.stableId
32641
+ },
32642
+ meta: { attempt: attemptNo }
32643
+ });
32644
+ } catch (err) {
32645
+ const lastError = err instanceof Error ? err.message : String(err);
32646
+ const remainingRetries = this.#delaysMs.length - (round + 1);
32647
+ this.#logger.warn("Device restore retry failed", {
32648
+ tags: {
32649
+ deviceId: entry.saved.id,
32650
+ stableId: entry.saved.stableId
32651
+ },
32652
+ meta: {
32653
+ attempt: attemptNo,
32654
+ remainingRetries,
32655
+ error: lastError
32656
+ }
32657
+ });
32658
+ next.push({
32659
+ saved: entry.saved,
32660
+ lastError,
32661
+ attempts: attemptNo
32662
+ });
32663
+ }
32664
+ });
32665
+ return next;
32666
+ }
32667
+ };
32668
+ /**
32498
32669
  * Convert an IDevice to the flat DeviceSummary shape expected by the
32499
32670
  * device-provider cap router. Shared across all providers.
32500
32671
  */
@@ -32543,6 +32714,7 @@ var BaseDeviceProvider = class extends BaseAddon {
32543
32714
  }];
32544
32715
  }
32545
32716
  async onShutdown() {
32717
+ this.cancelRestoreRetries();
32546
32718
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
32547
32719
  for (const device of devices) try {
32548
32720
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -32560,9 +32732,16 @@ var BaseDeviceProvider = class extends BaseAddon {
32560
32732
  async start() {}
32561
32733
  async stop() {}
32562
32734
  async getStatus() {
32735
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
32736
+ const summary = this.restoreFailureSummary();
32737
+ if (summary === null) return {
32738
+ connected: true,
32739
+ deviceCount: all.length
32740
+ };
32563
32741
  return {
32564
32742
  connected: true,
32565
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
32743
+ deviceCount: all.length,
32744
+ error: summary
32566
32745
  };
32567
32746
  }
32568
32747
  async getDevices() {
@@ -32652,8 +32831,137 @@ var BaseDeviceProvider = class extends BaseAddon {
32652
32831
  };
32653
32832
  }
32654
32833
  async restoreDevices(savedDevices) {
32655
- await this.onRestoreDevices(savedDevices);
32656
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
32834
+ const report = await this.onRestoreDevices(savedDevices);
32835
+ if (savedDevices.length === 0) return;
32836
+ if (report && report.failedCount > 0) {
32837
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
32838
+ return;
32839
+ }
32840
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
32841
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
32842
+ }
32843
+ /** Retry schedule. Overridable (tests use millisecond delays). */
32844
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
32845
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
32846
+ * never re-stampede full-width while the initial pass does (D167). */
32847
+ restoreRetryConcurrency = 4;
32848
+ _restoreRetryScheduler = null;
32849
+ _restoreRetryCompletion = null;
32850
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
32851
+ /** Settles when the background retry rounds finish (or `null` when
32852
+ * nothing failed). Exposed for tests and subclass diagnostics —
32853
+ * boot NEVER awaits this: the runner's post-init handshake goes out
32854
+ * with the devices that restored, and a late success is announced
32855
+ * through the `native-cap-change` → `updateCaps` path. */
32856
+ get restoreRetryCompletion() {
32857
+ return this._restoreRetryCompletion;
32858
+ }
32859
+ /** Devices that exhausted the retry bound this process lifetime. */
32860
+ get permanentRestoreFailures() {
32861
+ return [...this._permanentRestoreFailures.values()];
32862
+ }
32863
+ /** One-line operator-facing summary for `getStatus().error`, or
32864
+ * `null` when every device restored. */
32865
+ restoreFailureSummary() {
32866
+ if (this._permanentRestoreFailures.size === 0) return null;
32867
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
32868
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
32869
+ }
32870
+ cancelRestoreRetries() {
32871
+ this._restoreRetryScheduler?.cancel();
32872
+ this._restoreRetryScheduler = null;
32873
+ }
32874
+ recordPermanentRestoreFailure(failure) {
32875
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
32876
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
32877
+ tags: {
32878
+ deviceId: failure.deviceId,
32879
+ stableId: failure.stableId
32880
+ },
32881
+ meta: {
32882
+ type: failure.type,
32883
+ attempts: failure.attempts,
32884
+ error: failure.lastError
32885
+ }
32886
+ });
32887
+ }
32888
+ scheduleRestoreRetries(failures, attempt) {
32889
+ const scheduler = new DeviceRestoreRetryScheduler({
32890
+ logger: this.ctx.logger,
32891
+ delaysMs: this.restoreRetryDelaysMs,
32892
+ concurrency: this.restoreRetryConcurrency,
32893
+ attempt,
32894
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
32895
+ });
32896
+ this._restoreRetryScheduler = scheduler;
32897
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
32898
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
32899
+ });
32900
+ }
32901
+ /**
32902
+ * Tear down and reconstruct ONE device from its persisted rows — the
32903
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
32904
+ * and no other device this provider owns is disturbed.
32905
+ *
32906
+ * Keyed by `stableId` because the caller's whole reason to be here is that
32907
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
32908
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
32909
+ * whatever number the row carries NOW. The teardown is `decommission` —
32910
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
32911
+ * unregisters native caps, drops the registry entry) — and the rebuild is
32912
+ * the boot restore's own `create()` path, including its pass 2: first-class
32913
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
32914
+ * parent by the cascade and must be re-created explicitly, because only
32915
+ * accessory children come back through `getAccessoryChildren()`.
32916
+ *
32917
+ * Reloading an accessory child directly is refused (no device class) —
32918
+ * reload its parent instead.
32919
+ */
32920
+ async reloadDevice(input) {
32921
+ const { stableId } = input;
32922
+ const devices = this.ctx.kernel.devices;
32923
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
32924
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
32925
+ if (live) await devices.decommission(live.id);
32926
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
32927
+ addonId: this.addonId,
32928
+ stableId
32929
+ });
32930
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
32931
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
32932
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
32933
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
32934
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
32935
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
32936
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
32937
+ for (const row of rows) {
32938
+ if (row.parentDeviceId !== id) continue;
32939
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
32940
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
32941
+ if (!ChildClass) continue;
32942
+ try {
32943
+ await devices.create(row.stableId, ChildClass, {}, id);
32944
+ } catch (err) {
32945
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
32946
+ tags: {
32947
+ deviceId: row.id,
32948
+ stableId: row.stableId
32949
+ },
32950
+ meta: {
32951
+ parentDeviceId: id,
32952
+ error: err instanceof Error ? err.message : String(err)
32953
+ }
32954
+ });
32955
+ }
32956
+ }
32957
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
32958
+ tags: { deviceId: id },
32959
+ meta: {
32960
+ stableId,
32961
+ type: meta.type
32962
+ }
32963
+ });
32964
+ return { deviceId: id };
32657
32965
  }
32658
32966
  /**
32659
32967
  * Restore devices from persisted state. Two-pass:
@@ -32679,55 +32987,108 @@ var BaseDeviceProvider = class extends BaseAddon {
32679
32987
  * accessory-spawn flow handles via the parent's
32680
32988
  * `getAccessoryChildren()`. Override only when the default doesn't
32681
32989
  * fit.
32990
+ *
32991
+ * A row that fails either pass is NOT terminal (D347): it is handed
32992
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
32993
+ * Only after the bound is exhausted is the device marked permanently
32994
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
32995
+ * `getStatus().error`.
32682
32996
  */
32683
32997
  async onRestoreDevices(savedDevices) {
32684
32998
  const restored = /* @__PURE__ */ new Set();
32999
+ const failures = [];
33000
+ const attemptRestore = async (saved) => {
33001
+ if (restored.has(saved.id)) return;
33002
+ const Class = this.deviceClasses[saved.type];
33003
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
33004
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
33005
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33006
+ restored.add(saved.id);
33007
+ };
32685
33008
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
32686
33009
  const restoreOne = async (saved) => {
32687
- const Class = this.deviceClasses[saved.type];
32688
- if (!Class) {
33010
+ if (!this.deviceClasses[saved.type]) {
32689
33011
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
32690
- tags: { stableId: saved.stableId },
33012
+ tags: {
33013
+ deviceId: saved.id,
33014
+ stableId: saved.stableId
33015
+ },
32691
33016
  meta: { type: saved.type }
32692
33017
  });
32693
33018
  return;
32694
33019
  }
32695
33020
  try {
32696
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
32697
- restored.add(saved.id);
33021
+ await attemptRestore(saved);
32698
33022
  } catch (err) {
32699
- this.ctx.logger.warn("Failed to restore device", {
32700
- tags: { stableId: saved.stableId },
33023
+ const error = err instanceof Error ? err.message : String(err);
33024
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
33025
+ tags: {
33026
+ deviceId: saved.id,
33027
+ stableId: saved.stableId
33028
+ },
32701
33029
  meta: {
32702
33030
  type: saved.type,
32703
- error: err instanceof Error ? err.message : String(err)
33031
+ attempt: 1,
33032
+ error
32704
33033
  }
32705
33034
  });
33035
+ failures.push({
33036
+ saved,
33037
+ error
33038
+ });
32706
33039
  }
32707
33040
  };
32708
33041
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
33042
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
32709
33043
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
32710
33044
  for (const saved of childRows) {
32711
- const Class = this.deviceClasses[saved.type];
32712
- if (!Class) continue;
33045
+ if (!this.deviceClasses[saved.type]) continue;
32713
33046
  if (saved.parentDeviceId === null) continue;
32714
- if (!restored.has(saved.parentDeviceId)) continue;
32715
- try {
32716
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
32717
- restored.add(saved.id);
32718
- } catch (err) {
32719
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
33047
+ if (restored.has(saved.parentDeviceId)) {
33048
+ try {
33049
+ await attemptRestore(saved);
33050
+ } catch (err) {
33051
+ const error = err instanceof Error ? err.message : String(err);
33052
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
33053
+ tags: {
33054
+ deviceId: saved.id,
33055
+ stableId: saved.stableId,
33056
+ parentDeviceId: saved.parentDeviceId
33057
+ },
33058
+ meta: {
33059
+ type: saved.type,
33060
+ attempt: 1,
33061
+ error
33062
+ }
33063
+ });
33064
+ failures.push({
33065
+ saved,
33066
+ error
33067
+ });
33068
+ }
33069
+ continue;
33070
+ }
33071
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
33072
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
32720
33073
  tags: {
33074
+ deviceId: saved.id,
32721
33075
  stableId: saved.stableId,
32722
33076
  parentDeviceId: saved.parentDeviceId
32723
33077
  },
32724
- meta: {
32725
- type: saved.type,
32726
- error: err instanceof Error ? err.message : String(err)
32727
- }
33078
+ meta: { type: saved.type }
33079
+ });
33080
+ failures.push({
33081
+ saved,
33082
+ error: `parent device ${saved.parentDeviceId} not restored`
32728
33083
  });
33084
+ continue;
32729
33085
  }
32730
33086
  }
33087
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
33088
+ return {
33089
+ restoredCount: restored.size,
33090
+ failedCount: failures.length
33091
+ };
32731
33092
  }
32732
33093
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
32733
33094
  toSummary(device) {
@@ -34490,6 +34851,12 @@ Object.freeze({
34490
34851
  addonId: null,
34491
34852
  access: "view"
34492
34853
  },
34854
+ "deviceProvider.reloadDevice": {
34855
+ capName: "device-provider",
34856
+ capScope: "system",
34857
+ addonId: null,
34858
+ access: "create"
34859
+ },
34493
34860
  "deviceProvider.start": {
34494
34861
  capName: "device-provider",
34495
34862
  capScope: "system",
package/dist/addon.mjs CHANGED
@@ -12811,6 +12811,35 @@ var deviceProviderCapability = {
12811
12811
  name: string(),
12812
12812
  type: string()
12813
12813
  }))),
12814
+ /**
12815
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12816
+ * touching no other device this provider owns.
12817
+ *
12818
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12819
+ * migrated numbers: after `swapIds` the runner's live instance still
12820
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12821
+ * registrations and its log tags), and a live object cannot be renumbered.
12822
+ * Before this method the only flush was restarting the whole owning addon
12823
+ * — which took every camera the provider owns down with it (28 devices
12824
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12825
+ * same day ~27 devices' native caps did not come back on their own).
12826
+ *
12827
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12828
+ * that changes. The reply carries the id the device answers on NOW.
12829
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12830
+ * instance (if any), then re-create from the persisted row: the same
12831
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12832
+ * An RPC, never an event: a dropped event would leave the runner writing
12833
+ * against the wrong camera (D8).
12834
+ *
12835
+ * Construction can dial hardware, and the migrated source is
12836
+ * characteristically dead — the timeout covers a full activate window
12837
+ * rather than the 60 s default.
12838
+ */
12839
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12840
+ kind: "mutation",
12841
+ timeoutMs: 3 * 6e4
12842
+ }),
12814
12843
  supportsDiscovery: method(object({}), boolean()),
12815
12844
  /**
12816
12845
  * Run a network scan. `params` carries optional provider-specific scan
@@ -13138,7 +13167,8 @@ method(object({
13138
13167
  targetId: number()
13139
13168
  }), MigrateDeviceResultSchema, {
13140
13169
  kind: "mutation",
13141
- auth: "admin"
13170
+ auth: "admin",
13171
+ timeoutMs: 12 * 6e4
13142
13172
  }), 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({
13143
13173
  deviceId: number(),
13144
13174
  name: string()
@@ -32494,6 +32524,147 @@ var BaseDevice = class {
32494
32524
  }
32495
32525
  };
32496
32526
  /**
32527
+ * Delays before retry rounds 1..N — the round count IS the bound.
32528
+ * 10 s catches "the hub was busy for a moment"; the full schedule
32529
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
32530
+ * per attempt) covers a device-manager lock held for minutes — the
32531
+ * 2026-09-04 outage's migration hold was ~3.5 min.
32532
+ */
32533
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
32534
+ 1e4,
32535
+ 3e4,
32536
+ 9e4
32537
+ ];
32538
+ /** Abortable sleep — resolves early (never rejects) on abort. */
32539
+ function sleep$1(ms, signal) {
32540
+ return new Promise((resolve) => {
32541
+ if (signal.aborted) {
32542
+ resolve();
32543
+ return;
32544
+ }
32545
+ const onAbort = () => {
32546
+ clearTimeout(timer);
32547
+ resolve();
32548
+ };
32549
+ const timer = setTimeout(() => {
32550
+ signal.removeEventListener("abort", onAbort);
32551
+ resolve();
32552
+ }, ms);
32553
+ timer.unref?.();
32554
+ signal.addEventListener("abort", onAbort, { once: true });
32555
+ });
32556
+ }
32557
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
32558
+ * not reject (callers wrap their own try/catch). */
32559
+ async function runWithConcurrency(items, width, fn) {
32560
+ const queue = [...items];
32561
+ const laneCount = Math.max(1, Math.min(width, queue.length));
32562
+ const lane = async () => {
32563
+ for (;;) {
32564
+ const item = queue.shift();
32565
+ if (item === void 0) return;
32566
+ await fn(item);
32567
+ }
32568
+ };
32569
+ await Promise.all(Array.from({ length: laneCount }, lane));
32570
+ }
32571
+ var DeviceRestoreRetryScheduler = class {
32572
+ #logger;
32573
+ #attempt;
32574
+ #onPermanentFailure;
32575
+ #delaysMs;
32576
+ #concurrency;
32577
+ #now;
32578
+ #abort = new AbortController();
32579
+ constructor(options) {
32580
+ this.#logger = options.logger;
32581
+ this.#attempt = options.attempt;
32582
+ this.#onPermanentFailure = options.onPermanentFailure;
32583
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
32584
+ this.#concurrency = options.concurrency ?? 4;
32585
+ this.#now = options.now ?? Date.now;
32586
+ }
32587
+ /** Stop retrying (shutdown). Pending entries are NOT marked
32588
+ * permanently failed — the next boot restores them from disk. */
32589
+ cancel() {
32590
+ this.#abort.abort();
32591
+ }
32592
+ /**
32593
+ * Run the bounded retry rounds. Resolves when every entry has either
32594
+ * restored, been marked permanently failed, or the scheduler was
32595
+ * cancelled. Never rejects.
32596
+ */
32597
+ async run(initialFailures) {
32598
+ let pending = initialFailures.map((failure) => ({
32599
+ saved: failure.saved,
32600
+ lastError: failure.error,
32601
+ attempts: 1
32602
+ }));
32603
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
32604
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
32605
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
32606
+ if (this.#abort.signal.aborted) break;
32607
+ pending = await this.#runRound(pending, round);
32608
+ }
32609
+ if (this.#abort.signal.aborted) return [];
32610
+ const terminal = pending.map((entry) => ({
32611
+ deviceId: entry.saved.id,
32612
+ stableId: entry.saved.stableId,
32613
+ type: String(entry.saved.type),
32614
+ attempts: entry.attempts,
32615
+ lastError: entry.lastError,
32616
+ failedAt: this.#now()
32617
+ }));
32618
+ for (const failure of terminal) this.#onPermanentFailure(failure);
32619
+ return terminal;
32620
+ }
32621
+ /** One retry round: parents first (phase 0), then hub-adopted
32622
+ * children (phase 1) — a child's attempt depends on its parent
32623
+ * having landed, exactly like the initial two-pass restore. */
32624
+ async #runRound(pending, round) {
32625
+ const next = [];
32626
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
32627
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
32628
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
32629
+ if (this.#abort.signal.aborted) {
32630
+ next.push(entry);
32631
+ return;
32632
+ }
32633
+ const attemptNo = entry.attempts + 1;
32634
+ try {
32635
+ await this.#attempt(entry.saved);
32636
+ this.#logger.info("Device restored on retry", {
32637
+ tags: {
32638
+ deviceId: entry.saved.id,
32639
+ stableId: entry.saved.stableId
32640
+ },
32641
+ meta: { attempt: attemptNo }
32642
+ });
32643
+ } catch (err) {
32644
+ const lastError = err instanceof Error ? err.message : String(err);
32645
+ const remainingRetries = this.#delaysMs.length - (round + 1);
32646
+ this.#logger.warn("Device restore retry failed", {
32647
+ tags: {
32648
+ deviceId: entry.saved.id,
32649
+ stableId: entry.saved.stableId
32650
+ },
32651
+ meta: {
32652
+ attempt: attemptNo,
32653
+ remainingRetries,
32654
+ error: lastError
32655
+ }
32656
+ });
32657
+ next.push({
32658
+ saved: entry.saved,
32659
+ lastError,
32660
+ attempts: attemptNo
32661
+ });
32662
+ }
32663
+ });
32664
+ return next;
32665
+ }
32666
+ };
32667
+ /**
32497
32668
  * Convert an IDevice to the flat DeviceSummary shape expected by the
32498
32669
  * device-provider cap router. Shared across all providers.
32499
32670
  */
@@ -32542,6 +32713,7 @@ var BaseDeviceProvider = class extends BaseAddon {
32542
32713
  }];
32543
32714
  }
32544
32715
  async onShutdown() {
32716
+ this.cancelRestoreRetries();
32545
32717
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
32546
32718
  for (const device of devices) try {
32547
32719
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -32559,9 +32731,16 @@ var BaseDeviceProvider = class extends BaseAddon {
32559
32731
  async start() {}
32560
32732
  async stop() {}
32561
32733
  async getStatus() {
32734
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
32735
+ const summary = this.restoreFailureSummary();
32736
+ if (summary === null) return {
32737
+ connected: true,
32738
+ deviceCount: all.length
32739
+ };
32562
32740
  return {
32563
32741
  connected: true,
32564
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
32742
+ deviceCount: all.length,
32743
+ error: summary
32565
32744
  };
32566
32745
  }
32567
32746
  async getDevices() {
@@ -32651,8 +32830,137 @@ var BaseDeviceProvider = class extends BaseAddon {
32651
32830
  };
32652
32831
  }
32653
32832
  async restoreDevices(savedDevices) {
32654
- await this.onRestoreDevices(savedDevices);
32655
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
32833
+ const report = await this.onRestoreDevices(savedDevices);
32834
+ if (savedDevices.length === 0) return;
32835
+ if (report && report.failedCount > 0) {
32836
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
32837
+ return;
32838
+ }
32839
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
32840
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
32841
+ }
32842
+ /** Retry schedule. Overridable (tests use millisecond delays). */
32843
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
32844
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
32845
+ * never re-stampede full-width while the initial pass does (D167). */
32846
+ restoreRetryConcurrency = 4;
32847
+ _restoreRetryScheduler = null;
32848
+ _restoreRetryCompletion = null;
32849
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
32850
+ /** Settles when the background retry rounds finish (or `null` when
32851
+ * nothing failed). Exposed for tests and subclass diagnostics —
32852
+ * boot NEVER awaits this: the runner's post-init handshake goes out
32853
+ * with the devices that restored, and a late success is announced
32854
+ * through the `native-cap-change` → `updateCaps` path. */
32855
+ get restoreRetryCompletion() {
32856
+ return this._restoreRetryCompletion;
32857
+ }
32858
+ /** Devices that exhausted the retry bound this process lifetime. */
32859
+ get permanentRestoreFailures() {
32860
+ return [...this._permanentRestoreFailures.values()];
32861
+ }
32862
+ /** One-line operator-facing summary for `getStatus().error`, or
32863
+ * `null` when every device restored. */
32864
+ restoreFailureSummary() {
32865
+ if (this._permanentRestoreFailures.size === 0) return null;
32866
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
32867
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
32868
+ }
32869
+ cancelRestoreRetries() {
32870
+ this._restoreRetryScheduler?.cancel();
32871
+ this._restoreRetryScheduler = null;
32872
+ }
32873
+ recordPermanentRestoreFailure(failure) {
32874
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
32875
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
32876
+ tags: {
32877
+ deviceId: failure.deviceId,
32878
+ stableId: failure.stableId
32879
+ },
32880
+ meta: {
32881
+ type: failure.type,
32882
+ attempts: failure.attempts,
32883
+ error: failure.lastError
32884
+ }
32885
+ });
32886
+ }
32887
+ scheduleRestoreRetries(failures, attempt) {
32888
+ const scheduler = new DeviceRestoreRetryScheduler({
32889
+ logger: this.ctx.logger,
32890
+ delaysMs: this.restoreRetryDelaysMs,
32891
+ concurrency: this.restoreRetryConcurrency,
32892
+ attempt,
32893
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
32894
+ });
32895
+ this._restoreRetryScheduler = scheduler;
32896
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
32897
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
32898
+ });
32899
+ }
32900
+ /**
32901
+ * Tear down and reconstruct ONE device from its persisted rows — the
32902
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
32903
+ * and no other device this provider owns is disturbed.
32904
+ *
32905
+ * Keyed by `stableId` because the caller's whole reason to be here is that
32906
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
32907
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
32908
+ * whatever number the row carries NOW. The teardown is `decommission` —
32909
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
32910
+ * unregisters native caps, drops the registry entry) — and the rebuild is
32911
+ * the boot restore's own `create()` path, including its pass 2: first-class
32912
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
32913
+ * parent by the cascade and must be re-created explicitly, because only
32914
+ * accessory children come back through `getAccessoryChildren()`.
32915
+ *
32916
+ * Reloading an accessory child directly is refused (no device class) —
32917
+ * reload its parent instead.
32918
+ */
32919
+ async reloadDevice(input) {
32920
+ const { stableId } = input;
32921
+ const devices = this.ctx.kernel.devices;
32922
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
32923
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
32924
+ if (live) await devices.decommission(live.id);
32925
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
32926
+ addonId: this.addonId,
32927
+ stableId
32928
+ });
32929
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
32930
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
32931
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
32932
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
32933
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
32934
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
32935
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
32936
+ for (const row of rows) {
32937
+ if (row.parentDeviceId !== id) continue;
32938
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
32939
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
32940
+ if (!ChildClass) continue;
32941
+ try {
32942
+ await devices.create(row.stableId, ChildClass, {}, id);
32943
+ } catch (err) {
32944
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
32945
+ tags: {
32946
+ deviceId: row.id,
32947
+ stableId: row.stableId
32948
+ },
32949
+ meta: {
32950
+ parentDeviceId: id,
32951
+ error: err instanceof Error ? err.message : String(err)
32952
+ }
32953
+ });
32954
+ }
32955
+ }
32956
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
32957
+ tags: { deviceId: id },
32958
+ meta: {
32959
+ stableId,
32960
+ type: meta.type
32961
+ }
32962
+ });
32963
+ return { deviceId: id };
32656
32964
  }
32657
32965
  /**
32658
32966
  * Restore devices from persisted state. Two-pass:
@@ -32678,55 +32986,108 @@ var BaseDeviceProvider = class extends BaseAddon {
32678
32986
  * accessory-spawn flow handles via the parent's
32679
32987
  * `getAccessoryChildren()`. Override only when the default doesn't
32680
32988
  * fit.
32989
+ *
32990
+ * A row that fails either pass is NOT terminal (D347): it is handed
32991
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
32992
+ * Only after the bound is exhausted is the device marked permanently
32993
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
32994
+ * `getStatus().error`.
32681
32995
  */
32682
32996
  async onRestoreDevices(savedDevices) {
32683
32997
  const restored = /* @__PURE__ */ new Set();
32998
+ const failures = [];
32999
+ const attemptRestore = async (saved) => {
33000
+ if (restored.has(saved.id)) return;
33001
+ const Class = this.deviceClasses[saved.type];
33002
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
33003
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
33004
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33005
+ restored.add(saved.id);
33006
+ };
32684
33007
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
32685
33008
  const restoreOne = async (saved) => {
32686
- const Class = this.deviceClasses[saved.type];
32687
- if (!Class) {
33009
+ if (!this.deviceClasses[saved.type]) {
32688
33010
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
32689
- tags: { stableId: saved.stableId },
33011
+ tags: {
33012
+ deviceId: saved.id,
33013
+ stableId: saved.stableId
33014
+ },
32690
33015
  meta: { type: saved.type }
32691
33016
  });
32692
33017
  return;
32693
33018
  }
32694
33019
  try {
32695
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
32696
- restored.add(saved.id);
33020
+ await attemptRestore(saved);
32697
33021
  } catch (err) {
32698
- this.ctx.logger.warn("Failed to restore device", {
32699
- tags: { stableId: saved.stableId },
33022
+ const error = err instanceof Error ? err.message : String(err);
33023
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
33024
+ tags: {
33025
+ deviceId: saved.id,
33026
+ stableId: saved.stableId
33027
+ },
32700
33028
  meta: {
32701
33029
  type: saved.type,
32702
- error: err instanceof Error ? err.message : String(err)
33030
+ attempt: 1,
33031
+ error
32703
33032
  }
32704
33033
  });
33034
+ failures.push({
33035
+ saved,
33036
+ error
33037
+ });
32705
33038
  }
32706
33039
  };
32707
33040
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
33041
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
32708
33042
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
32709
33043
  for (const saved of childRows) {
32710
- const Class = this.deviceClasses[saved.type];
32711
- if (!Class) continue;
33044
+ if (!this.deviceClasses[saved.type]) continue;
32712
33045
  if (saved.parentDeviceId === null) continue;
32713
- if (!restored.has(saved.parentDeviceId)) continue;
32714
- try {
32715
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
32716
- restored.add(saved.id);
32717
- } catch (err) {
32718
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
33046
+ if (restored.has(saved.parentDeviceId)) {
33047
+ try {
33048
+ await attemptRestore(saved);
33049
+ } catch (err) {
33050
+ const error = err instanceof Error ? err.message : String(err);
33051
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
33052
+ tags: {
33053
+ deviceId: saved.id,
33054
+ stableId: saved.stableId,
33055
+ parentDeviceId: saved.parentDeviceId
33056
+ },
33057
+ meta: {
33058
+ type: saved.type,
33059
+ attempt: 1,
33060
+ error
33061
+ }
33062
+ });
33063
+ failures.push({
33064
+ saved,
33065
+ error
33066
+ });
33067
+ }
33068
+ continue;
33069
+ }
33070
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
33071
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
32719
33072
  tags: {
33073
+ deviceId: saved.id,
32720
33074
  stableId: saved.stableId,
32721
33075
  parentDeviceId: saved.parentDeviceId
32722
33076
  },
32723
- meta: {
32724
- type: saved.type,
32725
- error: err instanceof Error ? err.message : String(err)
32726
- }
33077
+ meta: { type: saved.type }
33078
+ });
33079
+ failures.push({
33080
+ saved,
33081
+ error: `parent device ${saved.parentDeviceId} not restored`
32727
33082
  });
33083
+ continue;
32728
33084
  }
32729
33085
  }
33086
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
33087
+ return {
33088
+ restoredCount: restored.size,
33089
+ failedCount: failures.length
33090
+ };
32730
33091
  }
32731
33092
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
32732
33093
  toSummary(device) {
@@ -34489,6 +34850,12 @@ Object.freeze({
34489
34850
  addonId: null,
34490
34851
  access: "view"
34491
34852
  },
34853
+ "deviceProvider.reloadDevice": {
34854
+ capName: "device-provider",
34855
+ capScope: "system",
34856
+ addonId: null,
34857
+ access: "create"
34858
+ },
34492
34859
  "deviceProvider.start": {
34493
34860
  capName: "device-provider",
34494
34861
  capScope: "system",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-provider-velux",
3
- "version": "0.2.59",
3
+ "version": "0.2.60",
4
4
  "description": "Velux KLF-200 device-provider addon for CamStack — local-gateway window + shutter covers (positional, no tilt)",
5
5
  "keywords": [
6
6
  "camstack",