@camstack/addon-provider-dreo 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
@@ -12865,6 +12865,35 @@ var deviceProviderCapability = {
12865
12865
  name: string(),
12866
12866
  type: string()
12867
12867
  }))),
12868
+ /**
12869
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12870
+ * touching no other device this provider owns.
12871
+ *
12872
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12873
+ * migrated numbers: after `swapIds` the runner's live instance still
12874
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12875
+ * registrations and its log tags), and a live object cannot be renumbered.
12876
+ * Before this method the only flush was restarting the whole owning addon
12877
+ * — which took every camera the provider owns down with it (28 devices
12878
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12879
+ * same day ~27 devices' native caps did not come back on their own).
12880
+ *
12881
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12882
+ * that changes. The reply carries the id the device answers on NOW.
12883
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12884
+ * instance (if any), then re-create from the persisted row: the same
12885
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12886
+ * An RPC, never an event: a dropped event would leave the runner writing
12887
+ * against the wrong camera (D8).
12888
+ *
12889
+ * Construction can dial hardware, and the migrated source is
12890
+ * characteristically dead — the timeout covers a full activate window
12891
+ * rather than the 60 s default.
12892
+ */
12893
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12894
+ kind: "mutation",
12895
+ timeoutMs: 3 * 6e4
12896
+ }),
12868
12897
  supportsDiscovery: method(object({}), boolean()),
12869
12898
  /**
12870
12899
  * Run a network scan. `params` carries optional provider-specific scan
@@ -13192,7 +13221,8 @@ method(object({
13192
13221
  targetId: number()
13193
13222
  }), MigrateDeviceResultSchema, {
13194
13223
  kind: "mutation",
13195
- auth: "admin"
13224
+ auth: "admin",
13225
+ timeoutMs: 12 * 6e4
13196
13226
  }), 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({
13197
13227
  deviceId: number(),
13198
13228
  name: string()
@@ -32556,6 +32586,147 @@ var BaseDevice$1 = class {
32556
32586
  }
32557
32587
  };
32558
32588
  /**
32589
+ * Delays before retry rounds 1..N — the round count IS the bound.
32590
+ * 10 s catches "the hub was busy for a moment"; the full schedule
32591
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
32592
+ * per attempt) covers a device-manager lock held for minutes — the
32593
+ * 2026-09-04 outage's migration hold was ~3.5 min.
32594
+ */
32595
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
32596
+ 1e4,
32597
+ 3e4,
32598
+ 9e4
32599
+ ];
32600
+ /** Abortable sleep — resolves early (never rejects) on abort. */
32601
+ function sleep$1(ms, signal) {
32602
+ return new Promise((resolve) => {
32603
+ if (signal.aborted) {
32604
+ resolve();
32605
+ return;
32606
+ }
32607
+ const onAbort = () => {
32608
+ clearTimeout(timer);
32609
+ resolve();
32610
+ };
32611
+ const timer = setTimeout(() => {
32612
+ signal.removeEventListener("abort", onAbort);
32613
+ resolve();
32614
+ }, ms);
32615
+ timer.unref?.();
32616
+ signal.addEventListener("abort", onAbort, { once: true });
32617
+ });
32618
+ }
32619
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
32620
+ * not reject (callers wrap their own try/catch). */
32621
+ async function runWithConcurrency(items, width, fn) {
32622
+ const queue = [...items];
32623
+ const laneCount = Math.max(1, Math.min(width, queue.length));
32624
+ const lane = async () => {
32625
+ for (;;) {
32626
+ const item = queue.shift();
32627
+ if (item === void 0) return;
32628
+ await fn(item);
32629
+ }
32630
+ };
32631
+ await Promise.all(Array.from({ length: laneCount }, lane));
32632
+ }
32633
+ var DeviceRestoreRetryScheduler = class {
32634
+ #logger;
32635
+ #attempt;
32636
+ #onPermanentFailure;
32637
+ #delaysMs;
32638
+ #concurrency;
32639
+ #now;
32640
+ #abort = new AbortController();
32641
+ constructor(options) {
32642
+ this.#logger = options.logger;
32643
+ this.#attempt = options.attempt;
32644
+ this.#onPermanentFailure = options.onPermanentFailure;
32645
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
32646
+ this.#concurrency = options.concurrency ?? 4;
32647
+ this.#now = options.now ?? Date.now;
32648
+ }
32649
+ /** Stop retrying (shutdown). Pending entries are NOT marked
32650
+ * permanently failed — the next boot restores them from disk. */
32651
+ cancel() {
32652
+ this.#abort.abort();
32653
+ }
32654
+ /**
32655
+ * Run the bounded retry rounds. Resolves when every entry has either
32656
+ * restored, been marked permanently failed, or the scheduler was
32657
+ * cancelled. Never rejects.
32658
+ */
32659
+ async run(initialFailures) {
32660
+ let pending = initialFailures.map((failure) => ({
32661
+ saved: failure.saved,
32662
+ lastError: failure.error,
32663
+ attempts: 1
32664
+ }));
32665
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
32666
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
32667
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
32668
+ if (this.#abort.signal.aborted) break;
32669
+ pending = await this.#runRound(pending, round);
32670
+ }
32671
+ if (this.#abort.signal.aborted) return [];
32672
+ const terminal = pending.map((entry) => ({
32673
+ deviceId: entry.saved.id,
32674
+ stableId: entry.saved.stableId,
32675
+ type: String(entry.saved.type),
32676
+ attempts: entry.attempts,
32677
+ lastError: entry.lastError,
32678
+ failedAt: this.#now()
32679
+ }));
32680
+ for (const failure of terminal) this.#onPermanentFailure(failure);
32681
+ return terminal;
32682
+ }
32683
+ /** One retry round: parents first (phase 0), then hub-adopted
32684
+ * children (phase 1) — a child's attempt depends on its parent
32685
+ * having landed, exactly like the initial two-pass restore. */
32686
+ async #runRound(pending, round) {
32687
+ const next = [];
32688
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
32689
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
32690
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
32691
+ if (this.#abort.signal.aborted) {
32692
+ next.push(entry);
32693
+ return;
32694
+ }
32695
+ const attemptNo = entry.attempts + 1;
32696
+ try {
32697
+ await this.#attempt(entry.saved);
32698
+ this.#logger.info("Device restored on retry", {
32699
+ tags: {
32700
+ deviceId: entry.saved.id,
32701
+ stableId: entry.saved.stableId
32702
+ },
32703
+ meta: { attempt: attemptNo }
32704
+ });
32705
+ } catch (err) {
32706
+ const lastError = err instanceof Error ? err.message : String(err);
32707
+ const remainingRetries = this.#delaysMs.length - (round + 1);
32708
+ this.#logger.warn("Device restore retry failed", {
32709
+ tags: {
32710
+ deviceId: entry.saved.id,
32711
+ stableId: entry.saved.stableId
32712
+ },
32713
+ meta: {
32714
+ attempt: attemptNo,
32715
+ remainingRetries,
32716
+ error: lastError
32717
+ }
32718
+ });
32719
+ next.push({
32720
+ saved: entry.saved,
32721
+ lastError,
32722
+ attempts: attemptNo
32723
+ });
32724
+ }
32725
+ });
32726
+ return next;
32727
+ }
32728
+ };
32729
+ /**
32559
32730
  * Convert an IDevice to the flat DeviceSummary shape expected by the
32560
32731
  * device-provider cap router. Shared across all providers.
32561
32732
  */
@@ -32604,6 +32775,7 @@ var BaseDeviceProvider = class extends BaseAddon {
32604
32775
  }];
32605
32776
  }
32606
32777
  async onShutdown() {
32778
+ this.cancelRestoreRetries();
32607
32779
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
32608
32780
  for (const device of devices) try {
32609
32781
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -32621,9 +32793,16 @@ var BaseDeviceProvider = class extends BaseAddon {
32621
32793
  async start() {}
32622
32794
  async stop() {}
32623
32795
  async getStatus() {
32796
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
32797
+ const summary = this.restoreFailureSummary();
32798
+ if (summary === null) return {
32799
+ connected: true,
32800
+ deviceCount: all.length
32801
+ };
32624
32802
  return {
32625
32803
  connected: true,
32626
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
32804
+ deviceCount: all.length,
32805
+ error: summary
32627
32806
  };
32628
32807
  }
32629
32808
  async getDevices() {
@@ -32713,8 +32892,137 @@ var BaseDeviceProvider = class extends BaseAddon {
32713
32892
  };
32714
32893
  }
32715
32894
  async restoreDevices(savedDevices) {
32716
- await this.onRestoreDevices(savedDevices);
32717
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
32895
+ const report = await this.onRestoreDevices(savedDevices);
32896
+ if (savedDevices.length === 0) return;
32897
+ if (report && report.failedCount > 0) {
32898
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
32899
+ return;
32900
+ }
32901
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
32902
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
32903
+ }
32904
+ /** Retry schedule. Overridable (tests use millisecond delays). */
32905
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
32906
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
32907
+ * never re-stampede full-width while the initial pass does (D167). */
32908
+ restoreRetryConcurrency = 4;
32909
+ _restoreRetryScheduler = null;
32910
+ _restoreRetryCompletion = null;
32911
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
32912
+ /** Settles when the background retry rounds finish (or `null` when
32913
+ * nothing failed). Exposed for tests and subclass diagnostics —
32914
+ * boot NEVER awaits this: the runner's post-init handshake goes out
32915
+ * with the devices that restored, and a late success is announced
32916
+ * through the `native-cap-change` → `updateCaps` path. */
32917
+ get restoreRetryCompletion() {
32918
+ return this._restoreRetryCompletion;
32919
+ }
32920
+ /** Devices that exhausted the retry bound this process lifetime. */
32921
+ get permanentRestoreFailures() {
32922
+ return [...this._permanentRestoreFailures.values()];
32923
+ }
32924
+ /** One-line operator-facing summary for `getStatus().error`, or
32925
+ * `null` when every device restored. */
32926
+ restoreFailureSummary() {
32927
+ if (this._permanentRestoreFailures.size === 0) return null;
32928
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
32929
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
32930
+ }
32931
+ cancelRestoreRetries() {
32932
+ this._restoreRetryScheduler?.cancel();
32933
+ this._restoreRetryScheduler = null;
32934
+ }
32935
+ recordPermanentRestoreFailure(failure) {
32936
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
32937
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
32938
+ tags: {
32939
+ deviceId: failure.deviceId,
32940
+ stableId: failure.stableId
32941
+ },
32942
+ meta: {
32943
+ type: failure.type,
32944
+ attempts: failure.attempts,
32945
+ error: failure.lastError
32946
+ }
32947
+ });
32948
+ }
32949
+ scheduleRestoreRetries(failures, attempt) {
32950
+ const scheduler = new DeviceRestoreRetryScheduler({
32951
+ logger: this.ctx.logger,
32952
+ delaysMs: this.restoreRetryDelaysMs,
32953
+ concurrency: this.restoreRetryConcurrency,
32954
+ attempt,
32955
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
32956
+ });
32957
+ this._restoreRetryScheduler = scheduler;
32958
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
32959
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
32960
+ });
32961
+ }
32962
+ /**
32963
+ * Tear down and reconstruct ONE device from its persisted rows — the
32964
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
32965
+ * and no other device this provider owns is disturbed.
32966
+ *
32967
+ * Keyed by `stableId` because the caller's whole reason to be here is that
32968
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
32969
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
32970
+ * whatever number the row carries NOW. The teardown is `decommission` —
32971
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
32972
+ * unregisters native caps, drops the registry entry) — and the rebuild is
32973
+ * the boot restore's own `create()` path, including its pass 2: first-class
32974
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
32975
+ * parent by the cascade and must be re-created explicitly, because only
32976
+ * accessory children come back through `getAccessoryChildren()`.
32977
+ *
32978
+ * Reloading an accessory child directly is refused (no device class) —
32979
+ * reload its parent instead.
32980
+ */
32981
+ async reloadDevice(input) {
32982
+ const { stableId } = input;
32983
+ const devices = this.ctx.kernel.devices;
32984
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
32985
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
32986
+ if (live) await devices.decommission(live.id);
32987
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
32988
+ addonId: this.addonId,
32989
+ stableId
32990
+ });
32991
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
32992
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
32993
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
32994
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
32995
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
32996
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
32997
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
32998
+ for (const row of rows) {
32999
+ if (row.parentDeviceId !== id) continue;
33000
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
33001
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
33002
+ if (!ChildClass) continue;
33003
+ try {
33004
+ await devices.create(row.stableId, ChildClass, {}, id);
33005
+ } catch (err) {
33006
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
33007
+ tags: {
33008
+ deviceId: row.id,
33009
+ stableId: row.stableId
33010
+ },
33011
+ meta: {
33012
+ parentDeviceId: id,
33013
+ error: err instanceof Error ? err.message : String(err)
33014
+ }
33015
+ });
33016
+ }
33017
+ }
33018
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
33019
+ tags: { deviceId: id },
33020
+ meta: {
33021
+ stableId,
33022
+ type: meta.type
33023
+ }
33024
+ });
33025
+ return { deviceId: id };
32718
33026
  }
32719
33027
  /**
32720
33028
  * Restore devices from persisted state. Two-pass:
@@ -32740,55 +33048,108 @@ var BaseDeviceProvider = class extends BaseAddon {
32740
33048
  * accessory-spawn flow handles via the parent's
32741
33049
  * `getAccessoryChildren()`. Override only when the default doesn't
32742
33050
  * fit.
33051
+ *
33052
+ * A row that fails either pass is NOT terminal (D347): it is handed
33053
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
33054
+ * Only after the bound is exhausted is the device marked permanently
33055
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
33056
+ * `getStatus().error`.
32743
33057
  */
32744
33058
  async onRestoreDevices(savedDevices) {
32745
33059
  const restored = /* @__PURE__ */ new Set();
33060
+ const failures = [];
33061
+ const attemptRestore = async (saved) => {
33062
+ if (restored.has(saved.id)) return;
33063
+ const Class = this.deviceClasses[saved.type];
33064
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
33065
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
33066
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33067
+ restored.add(saved.id);
33068
+ };
32746
33069
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
32747
33070
  const restoreOne = async (saved) => {
32748
- const Class = this.deviceClasses[saved.type];
32749
- if (!Class) {
33071
+ if (!this.deviceClasses[saved.type]) {
32750
33072
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
32751
- tags: { stableId: saved.stableId },
33073
+ tags: {
33074
+ deviceId: saved.id,
33075
+ stableId: saved.stableId
33076
+ },
32752
33077
  meta: { type: saved.type }
32753
33078
  });
32754
33079
  return;
32755
33080
  }
32756
33081
  try {
32757
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
32758
- restored.add(saved.id);
33082
+ await attemptRestore(saved);
32759
33083
  } catch (err) {
32760
- this.ctx.logger.warn("Failed to restore device", {
32761
- tags: { stableId: saved.stableId },
33084
+ const error = err instanceof Error ? err.message : String(err);
33085
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
33086
+ tags: {
33087
+ deviceId: saved.id,
33088
+ stableId: saved.stableId
33089
+ },
32762
33090
  meta: {
32763
33091
  type: saved.type,
32764
- error: err instanceof Error ? err.message : String(err)
33092
+ attempt: 1,
33093
+ error
32765
33094
  }
32766
33095
  });
33096
+ failures.push({
33097
+ saved,
33098
+ error
33099
+ });
32767
33100
  }
32768
33101
  };
32769
33102
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
33103
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
32770
33104
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
32771
33105
  for (const saved of childRows) {
32772
- const Class = this.deviceClasses[saved.type];
32773
- if (!Class) continue;
33106
+ if (!this.deviceClasses[saved.type]) continue;
32774
33107
  if (saved.parentDeviceId === null) continue;
32775
- if (!restored.has(saved.parentDeviceId)) continue;
32776
- try {
32777
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
32778
- restored.add(saved.id);
32779
- } catch (err) {
32780
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
33108
+ if (restored.has(saved.parentDeviceId)) {
33109
+ try {
33110
+ await attemptRestore(saved);
33111
+ } catch (err) {
33112
+ const error = err instanceof Error ? err.message : String(err);
33113
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
33114
+ tags: {
33115
+ deviceId: saved.id,
33116
+ stableId: saved.stableId,
33117
+ parentDeviceId: saved.parentDeviceId
33118
+ },
33119
+ meta: {
33120
+ type: saved.type,
33121
+ attempt: 1,
33122
+ error
33123
+ }
33124
+ });
33125
+ failures.push({
33126
+ saved,
33127
+ error
33128
+ });
33129
+ }
33130
+ continue;
33131
+ }
33132
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
33133
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
32781
33134
  tags: {
33135
+ deviceId: saved.id,
32782
33136
  stableId: saved.stableId,
32783
33137
  parentDeviceId: saved.parentDeviceId
32784
33138
  },
32785
- meta: {
32786
- type: saved.type,
32787
- error: err instanceof Error ? err.message : String(err)
32788
- }
33139
+ meta: { type: saved.type }
32789
33140
  });
33141
+ failures.push({
33142
+ saved,
33143
+ error: `parent device ${saved.parentDeviceId} not restored`
33144
+ });
33145
+ continue;
32790
33146
  }
32791
33147
  }
33148
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
33149
+ return {
33150
+ restoredCount: restored.size,
33151
+ failedCount: failures.length
33152
+ };
32792
33153
  }
32793
33154
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
32794
33155
  toSummary(device) {
@@ -34551,6 +34912,12 @@ Object.freeze({
34551
34912
  addonId: null,
34552
34913
  access: "view"
34553
34914
  },
34915
+ "deviceProvider.reloadDevice": {
34916
+ capName: "device-provider",
34917
+ capScope: "system",
34918
+ addonId: null,
34919
+ access: "create"
34920
+ },
34554
34921
  "deviceProvider.start": {
34555
34922
  capName: "device-provider",
34556
34923
  capScope: "system",
package/dist/addon.mjs CHANGED
@@ -12866,6 +12866,35 @@ var deviceProviderCapability = {
12866
12866
  name: string(),
12867
12867
  type: string()
12868
12868
  }))),
12869
+ /**
12870
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12871
+ * touching no other device this provider owns.
12872
+ *
12873
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12874
+ * migrated numbers: after `swapIds` the runner's live instance still
12875
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12876
+ * registrations and its log tags), and a live object cannot be renumbered.
12877
+ * Before this method the only flush was restarting the whole owning addon
12878
+ * — which took every camera the provider owns down with it (28 devices
12879
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12880
+ * same day ~27 devices' native caps did not come back on their own).
12881
+ *
12882
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12883
+ * that changes. The reply carries the id the device answers on NOW.
12884
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12885
+ * instance (if any), then re-create from the persisted row: the same
12886
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12887
+ * An RPC, never an event: a dropped event would leave the runner writing
12888
+ * against the wrong camera (D8).
12889
+ *
12890
+ * Construction can dial hardware, and the migrated source is
12891
+ * characteristically dead — the timeout covers a full activate window
12892
+ * rather than the 60 s default.
12893
+ */
12894
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12895
+ kind: "mutation",
12896
+ timeoutMs: 3 * 6e4
12897
+ }),
12869
12898
  supportsDiscovery: method(object({}), boolean()),
12870
12899
  /**
12871
12900
  * Run a network scan. `params` carries optional provider-specific scan
@@ -13193,7 +13222,8 @@ method(object({
13193
13222
  targetId: number()
13194
13223
  }), MigrateDeviceResultSchema, {
13195
13224
  kind: "mutation",
13196
- auth: "admin"
13225
+ auth: "admin",
13226
+ timeoutMs: 12 * 6e4
13197
13227
  }), 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({
13198
13228
  deviceId: number(),
13199
13229
  name: string()
@@ -32557,6 +32587,147 @@ var BaseDevice$1 = class {
32557
32587
  }
32558
32588
  };
32559
32589
  /**
32590
+ * Delays before retry rounds 1..N — the round count IS the bound.
32591
+ * 10 s catches "the hub was busy for a moment"; the full schedule
32592
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
32593
+ * per attempt) covers a device-manager lock held for minutes — the
32594
+ * 2026-09-04 outage's migration hold was ~3.5 min.
32595
+ */
32596
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
32597
+ 1e4,
32598
+ 3e4,
32599
+ 9e4
32600
+ ];
32601
+ /** Abortable sleep — resolves early (never rejects) on abort. */
32602
+ function sleep$1(ms, signal) {
32603
+ return new Promise((resolve) => {
32604
+ if (signal.aborted) {
32605
+ resolve();
32606
+ return;
32607
+ }
32608
+ const onAbort = () => {
32609
+ clearTimeout(timer);
32610
+ resolve();
32611
+ };
32612
+ const timer = setTimeout(() => {
32613
+ signal.removeEventListener("abort", onAbort);
32614
+ resolve();
32615
+ }, ms);
32616
+ timer.unref?.();
32617
+ signal.addEventListener("abort", onAbort, { once: true });
32618
+ });
32619
+ }
32620
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
32621
+ * not reject (callers wrap their own try/catch). */
32622
+ async function runWithConcurrency(items, width, fn) {
32623
+ const queue = [...items];
32624
+ const laneCount = Math.max(1, Math.min(width, queue.length));
32625
+ const lane = async () => {
32626
+ for (;;) {
32627
+ const item = queue.shift();
32628
+ if (item === void 0) return;
32629
+ await fn(item);
32630
+ }
32631
+ };
32632
+ await Promise.all(Array.from({ length: laneCount }, lane));
32633
+ }
32634
+ var DeviceRestoreRetryScheduler = class {
32635
+ #logger;
32636
+ #attempt;
32637
+ #onPermanentFailure;
32638
+ #delaysMs;
32639
+ #concurrency;
32640
+ #now;
32641
+ #abort = new AbortController();
32642
+ constructor(options) {
32643
+ this.#logger = options.logger;
32644
+ this.#attempt = options.attempt;
32645
+ this.#onPermanentFailure = options.onPermanentFailure;
32646
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
32647
+ this.#concurrency = options.concurrency ?? 4;
32648
+ this.#now = options.now ?? Date.now;
32649
+ }
32650
+ /** Stop retrying (shutdown). Pending entries are NOT marked
32651
+ * permanently failed — the next boot restores them from disk. */
32652
+ cancel() {
32653
+ this.#abort.abort();
32654
+ }
32655
+ /**
32656
+ * Run the bounded retry rounds. Resolves when every entry has either
32657
+ * restored, been marked permanently failed, or the scheduler was
32658
+ * cancelled. Never rejects.
32659
+ */
32660
+ async run(initialFailures) {
32661
+ let pending = initialFailures.map((failure) => ({
32662
+ saved: failure.saved,
32663
+ lastError: failure.error,
32664
+ attempts: 1
32665
+ }));
32666
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
32667
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
32668
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
32669
+ if (this.#abort.signal.aborted) break;
32670
+ pending = await this.#runRound(pending, round);
32671
+ }
32672
+ if (this.#abort.signal.aborted) return [];
32673
+ const terminal = pending.map((entry) => ({
32674
+ deviceId: entry.saved.id,
32675
+ stableId: entry.saved.stableId,
32676
+ type: String(entry.saved.type),
32677
+ attempts: entry.attempts,
32678
+ lastError: entry.lastError,
32679
+ failedAt: this.#now()
32680
+ }));
32681
+ for (const failure of terminal) this.#onPermanentFailure(failure);
32682
+ return terminal;
32683
+ }
32684
+ /** One retry round: parents first (phase 0), then hub-adopted
32685
+ * children (phase 1) — a child's attempt depends on its parent
32686
+ * having landed, exactly like the initial two-pass restore. */
32687
+ async #runRound(pending, round) {
32688
+ const next = [];
32689
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
32690
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
32691
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
32692
+ if (this.#abort.signal.aborted) {
32693
+ next.push(entry);
32694
+ return;
32695
+ }
32696
+ const attemptNo = entry.attempts + 1;
32697
+ try {
32698
+ await this.#attempt(entry.saved);
32699
+ this.#logger.info("Device restored on retry", {
32700
+ tags: {
32701
+ deviceId: entry.saved.id,
32702
+ stableId: entry.saved.stableId
32703
+ },
32704
+ meta: { attempt: attemptNo }
32705
+ });
32706
+ } catch (err) {
32707
+ const lastError = err instanceof Error ? err.message : String(err);
32708
+ const remainingRetries = this.#delaysMs.length - (round + 1);
32709
+ this.#logger.warn("Device restore retry failed", {
32710
+ tags: {
32711
+ deviceId: entry.saved.id,
32712
+ stableId: entry.saved.stableId
32713
+ },
32714
+ meta: {
32715
+ attempt: attemptNo,
32716
+ remainingRetries,
32717
+ error: lastError
32718
+ }
32719
+ });
32720
+ next.push({
32721
+ saved: entry.saved,
32722
+ lastError,
32723
+ attempts: attemptNo
32724
+ });
32725
+ }
32726
+ });
32727
+ return next;
32728
+ }
32729
+ };
32730
+ /**
32560
32731
  * Convert an IDevice to the flat DeviceSummary shape expected by the
32561
32732
  * device-provider cap router. Shared across all providers.
32562
32733
  */
@@ -32605,6 +32776,7 @@ var BaseDeviceProvider = class extends BaseAddon {
32605
32776
  }];
32606
32777
  }
32607
32778
  async onShutdown() {
32779
+ this.cancelRestoreRetries();
32608
32780
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
32609
32781
  for (const device of devices) try {
32610
32782
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -32622,9 +32794,16 @@ var BaseDeviceProvider = class extends BaseAddon {
32622
32794
  async start() {}
32623
32795
  async stop() {}
32624
32796
  async getStatus() {
32797
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
32798
+ const summary = this.restoreFailureSummary();
32799
+ if (summary === null) return {
32800
+ connected: true,
32801
+ deviceCount: all.length
32802
+ };
32625
32803
  return {
32626
32804
  connected: true,
32627
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
32805
+ deviceCount: all.length,
32806
+ error: summary
32628
32807
  };
32629
32808
  }
32630
32809
  async getDevices() {
@@ -32714,8 +32893,137 @@ var BaseDeviceProvider = class extends BaseAddon {
32714
32893
  };
32715
32894
  }
32716
32895
  async restoreDevices(savedDevices) {
32717
- await this.onRestoreDevices(savedDevices);
32718
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
32896
+ const report = await this.onRestoreDevices(savedDevices);
32897
+ if (savedDevices.length === 0) return;
32898
+ if (report && report.failedCount > 0) {
32899
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
32900
+ return;
32901
+ }
32902
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
32903
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
32904
+ }
32905
+ /** Retry schedule. Overridable (tests use millisecond delays). */
32906
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
32907
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
32908
+ * never re-stampede full-width while the initial pass does (D167). */
32909
+ restoreRetryConcurrency = 4;
32910
+ _restoreRetryScheduler = null;
32911
+ _restoreRetryCompletion = null;
32912
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
32913
+ /** Settles when the background retry rounds finish (or `null` when
32914
+ * nothing failed). Exposed for tests and subclass diagnostics —
32915
+ * boot NEVER awaits this: the runner's post-init handshake goes out
32916
+ * with the devices that restored, and a late success is announced
32917
+ * through the `native-cap-change` → `updateCaps` path. */
32918
+ get restoreRetryCompletion() {
32919
+ return this._restoreRetryCompletion;
32920
+ }
32921
+ /** Devices that exhausted the retry bound this process lifetime. */
32922
+ get permanentRestoreFailures() {
32923
+ return [...this._permanentRestoreFailures.values()];
32924
+ }
32925
+ /** One-line operator-facing summary for `getStatus().error`, or
32926
+ * `null` when every device restored. */
32927
+ restoreFailureSummary() {
32928
+ if (this._permanentRestoreFailures.size === 0) return null;
32929
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
32930
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
32931
+ }
32932
+ cancelRestoreRetries() {
32933
+ this._restoreRetryScheduler?.cancel();
32934
+ this._restoreRetryScheduler = null;
32935
+ }
32936
+ recordPermanentRestoreFailure(failure) {
32937
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
32938
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
32939
+ tags: {
32940
+ deviceId: failure.deviceId,
32941
+ stableId: failure.stableId
32942
+ },
32943
+ meta: {
32944
+ type: failure.type,
32945
+ attempts: failure.attempts,
32946
+ error: failure.lastError
32947
+ }
32948
+ });
32949
+ }
32950
+ scheduleRestoreRetries(failures, attempt) {
32951
+ const scheduler = new DeviceRestoreRetryScheduler({
32952
+ logger: this.ctx.logger,
32953
+ delaysMs: this.restoreRetryDelaysMs,
32954
+ concurrency: this.restoreRetryConcurrency,
32955
+ attempt,
32956
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
32957
+ });
32958
+ this._restoreRetryScheduler = scheduler;
32959
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
32960
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
32961
+ });
32962
+ }
32963
+ /**
32964
+ * Tear down and reconstruct ONE device from its persisted rows — the
32965
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
32966
+ * and no other device this provider owns is disturbed.
32967
+ *
32968
+ * Keyed by `stableId` because the caller's whole reason to be here is that
32969
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
32970
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
32971
+ * whatever number the row carries NOW. The teardown is `decommission` —
32972
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
32973
+ * unregisters native caps, drops the registry entry) — and the rebuild is
32974
+ * the boot restore's own `create()` path, including its pass 2: first-class
32975
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
32976
+ * parent by the cascade and must be re-created explicitly, because only
32977
+ * accessory children come back through `getAccessoryChildren()`.
32978
+ *
32979
+ * Reloading an accessory child directly is refused (no device class) —
32980
+ * reload its parent instead.
32981
+ */
32982
+ async reloadDevice(input) {
32983
+ const { stableId } = input;
32984
+ const devices = this.ctx.kernel.devices;
32985
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
32986
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
32987
+ if (live) await devices.decommission(live.id);
32988
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
32989
+ addonId: this.addonId,
32990
+ stableId
32991
+ });
32992
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
32993
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
32994
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
32995
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
32996
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
32997
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
32998
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
32999
+ for (const row of rows) {
33000
+ if (row.parentDeviceId !== id) continue;
33001
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
33002
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
33003
+ if (!ChildClass) continue;
33004
+ try {
33005
+ await devices.create(row.stableId, ChildClass, {}, id);
33006
+ } catch (err) {
33007
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
33008
+ tags: {
33009
+ deviceId: row.id,
33010
+ stableId: row.stableId
33011
+ },
33012
+ meta: {
33013
+ parentDeviceId: id,
33014
+ error: err instanceof Error ? err.message : String(err)
33015
+ }
33016
+ });
33017
+ }
33018
+ }
33019
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
33020
+ tags: { deviceId: id },
33021
+ meta: {
33022
+ stableId,
33023
+ type: meta.type
33024
+ }
33025
+ });
33026
+ return { deviceId: id };
32719
33027
  }
32720
33028
  /**
32721
33029
  * Restore devices from persisted state. Two-pass:
@@ -32741,55 +33049,108 @@ var BaseDeviceProvider = class extends BaseAddon {
32741
33049
  * accessory-spawn flow handles via the parent's
32742
33050
  * `getAccessoryChildren()`. Override only when the default doesn't
32743
33051
  * fit.
33052
+ *
33053
+ * A row that fails either pass is NOT terminal (D347): it is handed
33054
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
33055
+ * Only after the bound is exhausted is the device marked permanently
33056
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
33057
+ * `getStatus().error`.
32744
33058
  */
32745
33059
  async onRestoreDevices(savedDevices) {
32746
33060
  const restored = /* @__PURE__ */ new Set();
33061
+ const failures = [];
33062
+ const attemptRestore = async (saved) => {
33063
+ if (restored.has(saved.id)) return;
33064
+ const Class = this.deviceClasses[saved.type];
33065
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
33066
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
33067
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33068
+ restored.add(saved.id);
33069
+ };
32747
33070
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
32748
33071
  const restoreOne = async (saved) => {
32749
- const Class = this.deviceClasses[saved.type];
32750
- if (!Class) {
33072
+ if (!this.deviceClasses[saved.type]) {
32751
33073
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
32752
- tags: { stableId: saved.stableId },
33074
+ tags: {
33075
+ deviceId: saved.id,
33076
+ stableId: saved.stableId
33077
+ },
32753
33078
  meta: { type: saved.type }
32754
33079
  });
32755
33080
  return;
32756
33081
  }
32757
33082
  try {
32758
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
32759
- restored.add(saved.id);
33083
+ await attemptRestore(saved);
32760
33084
  } catch (err) {
32761
- this.ctx.logger.warn("Failed to restore device", {
32762
- tags: { stableId: saved.stableId },
33085
+ const error = err instanceof Error ? err.message : String(err);
33086
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
33087
+ tags: {
33088
+ deviceId: saved.id,
33089
+ stableId: saved.stableId
33090
+ },
32763
33091
  meta: {
32764
33092
  type: saved.type,
32765
- error: err instanceof Error ? err.message : String(err)
33093
+ attempt: 1,
33094
+ error
32766
33095
  }
32767
33096
  });
33097
+ failures.push({
33098
+ saved,
33099
+ error
33100
+ });
32768
33101
  }
32769
33102
  };
32770
33103
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
33104
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
32771
33105
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
32772
33106
  for (const saved of childRows) {
32773
- const Class = this.deviceClasses[saved.type];
32774
- if (!Class) continue;
33107
+ if (!this.deviceClasses[saved.type]) continue;
32775
33108
  if (saved.parentDeviceId === null) continue;
32776
- if (!restored.has(saved.parentDeviceId)) continue;
32777
- try {
32778
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
32779
- restored.add(saved.id);
32780
- } catch (err) {
32781
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
33109
+ if (restored.has(saved.parentDeviceId)) {
33110
+ try {
33111
+ await attemptRestore(saved);
33112
+ } catch (err) {
33113
+ const error = err instanceof Error ? err.message : String(err);
33114
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
33115
+ tags: {
33116
+ deviceId: saved.id,
33117
+ stableId: saved.stableId,
33118
+ parentDeviceId: saved.parentDeviceId
33119
+ },
33120
+ meta: {
33121
+ type: saved.type,
33122
+ attempt: 1,
33123
+ error
33124
+ }
33125
+ });
33126
+ failures.push({
33127
+ saved,
33128
+ error
33129
+ });
33130
+ }
33131
+ continue;
33132
+ }
33133
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
33134
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
32782
33135
  tags: {
33136
+ deviceId: saved.id,
32783
33137
  stableId: saved.stableId,
32784
33138
  parentDeviceId: saved.parentDeviceId
32785
33139
  },
32786
- meta: {
32787
- type: saved.type,
32788
- error: err instanceof Error ? err.message : String(err)
32789
- }
33140
+ meta: { type: saved.type }
32790
33141
  });
33142
+ failures.push({
33143
+ saved,
33144
+ error: `parent device ${saved.parentDeviceId} not restored`
33145
+ });
33146
+ continue;
32791
33147
  }
32792
33148
  }
33149
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
33150
+ return {
33151
+ restoredCount: restored.size,
33152
+ failedCount: failures.length
33153
+ };
32793
33154
  }
32794
33155
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
32795
33156
  toSummary(device) {
@@ -34552,6 +34913,12 @@ Object.freeze({
34552
34913
  addonId: null,
34553
34914
  access: "view"
34554
34915
  },
34916
+ "deviceProvider.reloadDevice": {
34917
+ capName: "device-provider",
34918
+ capScope: "system",
34919
+ addonId: null,
34920
+ access: "create"
34921
+ },
34555
34922
  "deviceProvider.start": {
34556
34923
  capName: "device-provider",
34557
34924
  capScope: "system",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-provider-dreo",
3
- "version": "0.2.59",
3
+ "version": "0.2.60",
4
4
  "description": "Dreo smart-device (fan / air-circulator / purifier / heater / humidifier) device-provider addon for CamStack — wraps the @apocaliss92/nodedreo Dreo cloud client (REST + WebSocket)",
5
5
  "keywords": [
6
6
  "camstack",