@camstack/addon-provider-petkit 0.2.59 → 0.2.61

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 +428 -25
  2. package/dist/addon.mjs +428 -25
  3. package/package.json +1 -1
package/dist/addon.js CHANGED
@@ -13875,7 +13875,26 @@ var DiscoveryCandidateSchema = object({
13875
13875
  * identity ahead of adoption. Rendering metadata (unit, precision)
13876
13876
  * flows live through the cap STATUS SLICE after adoption.
13877
13877
  */
13878
- sourceInfo: SourceInfoSchema.optional()
13878
+ sourceInfo: SourceInfoSchema.optional(),
13879
+ /**
13880
+ * Set when this candidate is a device the provider ALREADY owns.
13881
+ *
13882
+ * A scan cannot generally produce the identity a device was onboarded under
13883
+ * (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
13884
+ * comparison never matches and an owned device looks addable. Re-adopting one
13885
+ * overwrites its config with scan-derived values — that is how a Home Hub's
13886
+ * Baichuan port was overwritten with its ONVIF port, taking the hub and its
13887
+ * three child cameras offline for four hours.
13888
+ *
13889
+ * A provider that can recognise its own devices says so here. Absent means
13890
+ * "not recognised", which is not the same as "known to be new" — a provider
13891
+ * that cannot tell simply never sets it.
13892
+ */
13893
+ alreadyOnboarded: boolean().optional(),
13894
+ /** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
13895
+ onboardedDeviceId: number().optional(),
13896
+ /** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
13897
+ onboardedName: string().optional()
13879
13898
  });
13880
13899
  /**
13881
13900
  * Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
@@ -13931,6 +13950,35 @@ var deviceProviderCapability = {
13931
13950
  name: string(),
13932
13951
  type: string()
13933
13952
  }))),
13953
+ /**
13954
+ * Tear down and reconstruct ONE device in place from its persisted rows —
13955
+ * touching no other device this provider owns.
13956
+ *
13957
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
13958
+ * migrated numbers: after `swapIds` the runner's live instance still
13959
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
13960
+ * registrations and its log tags), and a live object cannot be renumbered.
13961
+ * Before this method the only flush was restarting the whole owning addon
13962
+ * — which took every camera the provider owns down with it (28 devices
13963
+ * for one migrated camera, measured 2026-09-04, and the morning of the
13964
+ * same day ~27 devices' native caps did not come back on their own).
13965
+ *
13966
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
13967
+ * that changes. The reply carries the id the device answers on NOW.
13968
+ * Implemented once in `BaseDeviceProvider` — decommission the live
13969
+ * instance (if any), then re-create from the persisted row: the same
13970
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
13971
+ * An RPC, never an event: a dropped event would leave the runner writing
13972
+ * against the wrong camera (D8).
13973
+ *
13974
+ * Construction can dial hardware, and the migrated source is
13975
+ * characteristically dead — the timeout covers a full activate window
13976
+ * rather than the 60 s default.
13977
+ */
13978
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
13979
+ kind: "mutation",
13980
+ timeoutMs: 3 * 6e4
13981
+ }),
13934
13982
  supportsDiscovery: method(object({}), boolean()),
13935
13983
  /**
13936
13984
  * Run a network scan. `params` carries optional provider-specific scan
@@ -14258,7 +14306,8 @@ method(object({
14258
14306
  targetId: number()
14259
14307
  }), MigrateDeviceResultSchema, {
14260
14308
  kind: "mutation",
14261
- auth: "admin"
14309
+ auth: "admin",
14310
+ timeoutMs: 12 * 6e4
14262
14311
  }), 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({
14263
14312
  deviceId: number(),
14264
14313
  name: string()
@@ -33622,6 +33671,147 @@ var BaseDevice = class {
33622
33671
  }
33623
33672
  };
33624
33673
  /**
33674
+ * Delays before retry rounds 1..N — the round count IS the bound.
33675
+ * 10 s catches "the hub was busy for a moment"; the full schedule
33676
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
33677
+ * per attempt) covers a device-manager lock held for minutes — the
33678
+ * 2026-09-04 outage's migration hold was ~3.5 min.
33679
+ */
33680
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
33681
+ 1e4,
33682
+ 3e4,
33683
+ 9e4
33684
+ ];
33685
+ /** Abortable sleep — resolves early (never rejects) on abort. */
33686
+ function sleep$1(ms, signal) {
33687
+ return new Promise((resolve) => {
33688
+ if (signal.aborted) {
33689
+ resolve();
33690
+ return;
33691
+ }
33692
+ const onAbort = () => {
33693
+ clearTimeout(timer);
33694
+ resolve();
33695
+ };
33696
+ const timer = setTimeout(() => {
33697
+ signal.removeEventListener("abort", onAbort);
33698
+ resolve();
33699
+ }, ms);
33700
+ timer.unref?.();
33701
+ signal.addEventListener("abort", onAbort, { once: true });
33702
+ });
33703
+ }
33704
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
33705
+ * not reject (callers wrap their own try/catch). */
33706
+ async function runWithConcurrency(items, width, fn) {
33707
+ const queue = [...items];
33708
+ const laneCount = Math.max(1, Math.min(width, queue.length));
33709
+ const lane = async () => {
33710
+ for (;;) {
33711
+ const item = queue.shift();
33712
+ if (item === void 0) return;
33713
+ await fn(item);
33714
+ }
33715
+ };
33716
+ await Promise.all(Array.from({ length: laneCount }, lane));
33717
+ }
33718
+ var DeviceRestoreRetryScheduler = class {
33719
+ #logger;
33720
+ #attempt;
33721
+ #onPermanentFailure;
33722
+ #delaysMs;
33723
+ #concurrency;
33724
+ #now;
33725
+ #abort = new AbortController();
33726
+ constructor(options) {
33727
+ this.#logger = options.logger;
33728
+ this.#attempt = options.attempt;
33729
+ this.#onPermanentFailure = options.onPermanentFailure;
33730
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
33731
+ this.#concurrency = options.concurrency ?? 4;
33732
+ this.#now = options.now ?? Date.now;
33733
+ }
33734
+ /** Stop retrying (shutdown). Pending entries are NOT marked
33735
+ * permanently failed — the next boot restores them from disk. */
33736
+ cancel() {
33737
+ this.#abort.abort();
33738
+ }
33739
+ /**
33740
+ * Run the bounded retry rounds. Resolves when every entry has either
33741
+ * restored, been marked permanently failed, or the scheduler was
33742
+ * cancelled. Never rejects.
33743
+ */
33744
+ async run(initialFailures) {
33745
+ let pending = initialFailures.map((failure) => ({
33746
+ saved: failure.saved,
33747
+ lastError: failure.error,
33748
+ attempts: 1
33749
+ }));
33750
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
33751
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
33752
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
33753
+ if (this.#abort.signal.aborted) break;
33754
+ pending = await this.#runRound(pending, round);
33755
+ }
33756
+ if (this.#abort.signal.aborted) return [];
33757
+ const terminal = pending.map((entry) => ({
33758
+ deviceId: entry.saved.id,
33759
+ stableId: entry.saved.stableId,
33760
+ type: String(entry.saved.type),
33761
+ attempts: entry.attempts,
33762
+ lastError: entry.lastError,
33763
+ failedAt: this.#now()
33764
+ }));
33765
+ for (const failure of terminal) this.#onPermanentFailure(failure);
33766
+ return terminal;
33767
+ }
33768
+ /** One retry round: parents first (phase 0), then hub-adopted
33769
+ * children (phase 1) — a child's attempt depends on its parent
33770
+ * having landed, exactly like the initial two-pass restore. */
33771
+ async #runRound(pending, round) {
33772
+ const next = [];
33773
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
33774
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
33775
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
33776
+ if (this.#abort.signal.aborted) {
33777
+ next.push(entry);
33778
+ return;
33779
+ }
33780
+ const attemptNo = entry.attempts + 1;
33781
+ try {
33782
+ await this.#attempt(entry.saved);
33783
+ this.#logger.info("Device restored on retry", {
33784
+ tags: {
33785
+ deviceId: entry.saved.id,
33786
+ stableId: entry.saved.stableId
33787
+ },
33788
+ meta: { attempt: attemptNo }
33789
+ });
33790
+ } catch (err) {
33791
+ const lastError = err instanceof Error ? err.message : String(err);
33792
+ const remainingRetries = this.#delaysMs.length - (round + 1);
33793
+ this.#logger.warn("Device restore retry failed", {
33794
+ tags: {
33795
+ deviceId: entry.saved.id,
33796
+ stableId: entry.saved.stableId
33797
+ },
33798
+ meta: {
33799
+ attempt: attemptNo,
33800
+ remainingRetries,
33801
+ error: lastError
33802
+ }
33803
+ });
33804
+ next.push({
33805
+ saved: entry.saved,
33806
+ lastError,
33807
+ attempts: attemptNo
33808
+ });
33809
+ }
33810
+ });
33811
+ return next;
33812
+ }
33813
+ };
33814
+ /**
33625
33815
  * Convert an IDevice to the flat DeviceSummary shape expected by the
33626
33816
  * device-provider cap router. Shared across all providers.
33627
33817
  */
@@ -33670,6 +33860,7 @@ var BaseDeviceProvider = class extends BaseAddon {
33670
33860
  }];
33671
33861
  }
33672
33862
  async onShutdown() {
33863
+ this.cancelRestoreRetries();
33673
33864
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
33674
33865
  for (const device of devices) try {
33675
33866
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -33687,9 +33878,16 @@ var BaseDeviceProvider = class extends BaseAddon {
33687
33878
  async start() {}
33688
33879
  async stop() {}
33689
33880
  async getStatus() {
33881
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
33882
+ const summary = this.restoreFailureSummary();
33883
+ if (summary === null) return {
33884
+ connected: true,
33885
+ deviceCount: all.length
33886
+ };
33690
33887
  return {
33691
33888
  connected: true,
33692
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
33889
+ deviceCount: all.length,
33890
+ error: summary
33693
33891
  };
33694
33892
  }
33695
33893
  async getDevices() {
@@ -33779,8 +33977,137 @@ var BaseDeviceProvider = class extends BaseAddon {
33779
33977
  };
33780
33978
  }
33781
33979
  async restoreDevices(savedDevices) {
33782
- await this.onRestoreDevices(savedDevices);
33783
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
33980
+ const report = await this.onRestoreDevices(savedDevices);
33981
+ if (savedDevices.length === 0) return;
33982
+ if (report && report.failedCount > 0) {
33983
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
33984
+ return;
33985
+ }
33986
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
33987
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
33988
+ }
33989
+ /** Retry schedule. Overridable (tests use millisecond delays). */
33990
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
33991
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
33992
+ * never re-stampede full-width while the initial pass does (D167). */
33993
+ restoreRetryConcurrency = 4;
33994
+ _restoreRetryScheduler = null;
33995
+ _restoreRetryCompletion = null;
33996
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
33997
+ /** Settles when the background retry rounds finish (or `null` when
33998
+ * nothing failed). Exposed for tests and subclass diagnostics —
33999
+ * boot NEVER awaits this: the runner's post-init handshake goes out
34000
+ * with the devices that restored, and a late success is announced
34001
+ * through the `native-cap-change` → `updateCaps` path. */
34002
+ get restoreRetryCompletion() {
34003
+ return this._restoreRetryCompletion;
34004
+ }
34005
+ /** Devices that exhausted the retry bound this process lifetime. */
34006
+ get permanentRestoreFailures() {
34007
+ return [...this._permanentRestoreFailures.values()];
34008
+ }
34009
+ /** One-line operator-facing summary for `getStatus().error`, or
34010
+ * `null` when every device restored. */
34011
+ restoreFailureSummary() {
34012
+ if (this._permanentRestoreFailures.size === 0) return null;
34013
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
34014
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
34015
+ }
34016
+ cancelRestoreRetries() {
34017
+ this._restoreRetryScheduler?.cancel();
34018
+ this._restoreRetryScheduler = null;
34019
+ }
34020
+ recordPermanentRestoreFailure(failure) {
34021
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
34022
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
34023
+ tags: {
34024
+ deviceId: failure.deviceId,
34025
+ stableId: failure.stableId
34026
+ },
34027
+ meta: {
34028
+ type: failure.type,
34029
+ attempts: failure.attempts,
34030
+ error: failure.lastError
34031
+ }
34032
+ });
34033
+ }
34034
+ scheduleRestoreRetries(failures, attempt) {
34035
+ const scheduler = new DeviceRestoreRetryScheduler({
34036
+ logger: this.ctx.logger,
34037
+ delaysMs: this.restoreRetryDelaysMs,
34038
+ concurrency: this.restoreRetryConcurrency,
34039
+ attempt,
34040
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
34041
+ });
34042
+ this._restoreRetryScheduler = scheduler;
34043
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
34044
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
34045
+ });
34046
+ }
34047
+ /**
34048
+ * Tear down and reconstruct ONE device from its persisted rows — the
34049
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
34050
+ * and no other device this provider owns is disturbed.
34051
+ *
34052
+ * Keyed by `stableId` because the caller's whole reason to be here is that
34053
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
34054
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
34055
+ * whatever number the row carries NOW. The teardown is `decommission` —
34056
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
34057
+ * unregisters native caps, drops the registry entry) — and the rebuild is
34058
+ * the boot restore's own `create()` path, including its pass 2: first-class
34059
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
34060
+ * parent by the cascade and must be re-created explicitly, because only
34061
+ * accessory children come back through `getAccessoryChildren()`.
34062
+ *
34063
+ * Reloading an accessory child directly is refused (no device class) —
34064
+ * reload its parent instead.
34065
+ */
34066
+ async reloadDevice(input) {
34067
+ const { stableId } = input;
34068
+ const devices = this.ctx.kernel.devices;
34069
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
34070
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
34071
+ if (live) await devices.decommission(live.id);
34072
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
34073
+ addonId: this.addonId,
34074
+ stableId
34075
+ });
34076
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
34077
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
34078
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
34079
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
34080
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
34081
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
34082
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
34083
+ for (const row of rows) {
34084
+ if (row.parentDeviceId !== id) continue;
34085
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
34086
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
34087
+ if (!ChildClass) continue;
34088
+ try {
34089
+ await devices.create(row.stableId, ChildClass, {}, id);
34090
+ } catch (err) {
34091
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
34092
+ tags: {
34093
+ deviceId: row.id,
34094
+ stableId: row.stableId
34095
+ },
34096
+ meta: {
34097
+ parentDeviceId: id,
34098
+ error: err instanceof Error ? err.message : String(err)
34099
+ }
34100
+ });
34101
+ }
34102
+ }
34103
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
34104
+ tags: { deviceId: id },
34105
+ meta: {
34106
+ stableId,
34107
+ type: meta.type
34108
+ }
34109
+ });
34110
+ return { deviceId: id };
33784
34111
  }
33785
34112
  /**
33786
34113
  * Restore devices from persisted state. Two-pass:
@@ -33806,55 +34133,125 @@ var BaseDeviceProvider = class extends BaseAddon {
33806
34133
  * accessory-spawn flow handles via the parent's
33807
34134
  * `getAccessoryChildren()`. Override only when the default doesn't
33808
34135
  * fit.
34136
+ *
34137
+ * A row that fails either pass is NOT terminal (D347): it is handed
34138
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
34139
+ * Only after the bound is exhausted is the device marked permanently
34140
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
34141
+ * `getStatus().error`.
33809
34142
  */
34143
+ /**
34144
+ * Repair a row's PERSISTED config blob immediately before it is restored.
34145
+ * Default: no-op — most providers have nothing to heal.
34146
+ *
34147
+ * This exists because a restored device self-hydrates from the DB: `create()`
34148
+ * passes `{}` and `BaseDevice` parses the stored blob against the device
34149
+ * schema. A blob that lost a REQUIRED field therefore fails restore forever,
34150
+ * and no later pass revisits it — a hub-adopted Reolink camera whose blob had
34151
+ * been emptied failed all four bounded attempts against fields
34152
+ * (`host`, `password`) it inherits from its parent and never dials itself.
34153
+ *
34154
+ * Implementations get every saved row, so a child can read its parent's blob.
34155
+ * A heal that throws is treated like any other restore failure: retried under
34156
+ * the bound, then reported — never swallowed.
34157
+ */
34158
+ async healSavedConfig(_saved, _allSaved) {}
33810
34159
  async onRestoreDevices(savedDevices) {
33811
34160
  const restored = /* @__PURE__ */ new Set();
34161
+ const failures = [];
34162
+ const attemptRestore = async (saved) => {
34163
+ if (restored.has(saved.id)) return;
34164
+ const Class = this.deviceClasses[saved.type];
34165
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
34166
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
34167
+ await this.healSavedConfig(saved, savedDevices);
34168
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
34169
+ restored.add(saved.id);
34170
+ };
33812
34171
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
33813
34172
  const restoreOne = async (saved) => {
33814
- const Class = this.deviceClasses[saved.type];
33815
- if (!Class) {
34173
+ if (!this.deviceClasses[saved.type]) {
33816
34174
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
33817
- tags: { stableId: saved.stableId },
34175
+ tags: {
34176
+ deviceId: saved.id,
34177
+ stableId: saved.stableId
34178
+ },
33818
34179
  meta: { type: saved.type }
33819
34180
  });
33820
34181
  return;
33821
34182
  }
33822
34183
  try {
33823
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
33824
- restored.add(saved.id);
34184
+ await attemptRestore(saved);
33825
34185
  } catch (err) {
33826
- this.ctx.logger.warn("Failed to restore device", {
33827
- tags: { stableId: saved.stableId },
34186
+ const error = err instanceof Error ? err.message : String(err);
34187
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
34188
+ tags: {
34189
+ deviceId: saved.id,
34190
+ stableId: saved.stableId
34191
+ },
33828
34192
  meta: {
33829
34193
  type: saved.type,
33830
- error: err instanceof Error ? err.message : String(err)
34194
+ attempt: 1,
34195
+ error
33831
34196
  }
33832
34197
  });
34198
+ failures.push({
34199
+ saved,
34200
+ error
34201
+ });
33833
34202
  }
33834
34203
  };
33835
34204
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
34205
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
33836
34206
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
33837
34207
  for (const saved of childRows) {
33838
- const Class = this.deviceClasses[saved.type];
33839
- if (!Class) continue;
34208
+ if (!this.deviceClasses[saved.type]) continue;
33840
34209
  if (saved.parentDeviceId === null) continue;
33841
- if (!restored.has(saved.parentDeviceId)) continue;
33842
- try {
33843
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33844
- restored.add(saved.id);
33845
- } catch (err) {
33846
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
34210
+ if (restored.has(saved.parentDeviceId)) {
34211
+ try {
34212
+ await attemptRestore(saved);
34213
+ } catch (err) {
34214
+ const error = err instanceof Error ? err.message : String(err);
34215
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
34216
+ tags: {
34217
+ deviceId: saved.id,
34218
+ stableId: saved.stableId,
34219
+ parentDeviceId: saved.parentDeviceId
34220
+ },
34221
+ meta: {
34222
+ type: saved.type,
34223
+ attempt: 1,
34224
+ error
34225
+ }
34226
+ });
34227
+ failures.push({
34228
+ saved,
34229
+ error
34230
+ });
34231
+ }
34232
+ continue;
34233
+ }
34234
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
34235
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
33847
34236
  tags: {
34237
+ deviceId: saved.id,
33848
34238
  stableId: saved.stableId,
33849
34239
  parentDeviceId: saved.parentDeviceId
33850
34240
  },
33851
- meta: {
33852
- type: saved.type,
33853
- error: err instanceof Error ? err.message : String(err)
33854
- }
34241
+ meta: { type: saved.type }
34242
+ });
34243
+ failures.push({
34244
+ saved,
34245
+ error: `parent device ${saved.parentDeviceId} not restored`
33855
34246
  });
34247
+ continue;
33856
34248
  }
33857
34249
  }
34250
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
34251
+ return {
34252
+ restoredCount: restored.size,
34253
+ failedCount: failures.length
34254
+ };
33858
34255
  }
33859
34256
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
33860
34257
  toSummary(device) {
@@ -35617,6 +36014,12 @@ Object.freeze({
35617
36014
  addonId: null,
35618
36015
  access: "view"
35619
36016
  },
36017
+ "deviceProvider.reloadDevice": {
36018
+ capName: "device-provider",
36019
+ capScope: "system",
36020
+ addonId: null,
36021
+ access: "create"
36022
+ },
35620
36023
  "deviceProvider.start": {
35621
36024
  capName: "device-provider",
35622
36025
  capScope: "system",
package/dist/addon.mjs CHANGED
@@ -13874,7 +13874,26 @@ var DiscoveryCandidateSchema = object({
13874
13874
  * identity ahead of adoption. Rendering metadata (unit, precision)
13875
13875
  * flows live through the cap STATUS SLICE after adoption.
13876
13876
  */
13877
- sourceInfo: SourceInfoSchema.optional()
13877
+ sourceInfo: SourceInfoSchema.optional(),
13878
+ /**
13879
+ * Set when this candidate is a device the provider ALREADY owns.
13880
+ *
13881
+ * A scan cannot generally produce the identity a device was onboarded under
13882
+ * (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
13883
+ * comparison never matches and an owned device looks addable. Re-adopting one
13884
+ * overwrites its config with scan-derived values — that is how a Home Hub's
13885
+ * Baichuan port was overwritten with its ONVIF port, taking the hub and its
13886
+ * three child cameras offline for four hours.
13887
+ *
13888
+ * A provider that can recognise its own devices says so here. Absent means
13889
+ * "not recognised", which is not the same as "known to be new" — a provider
13890
+ * that cannot tell simply never sets it.
13891
+ */
13892
+ alreadyOnboarded: boolean().optional(),
13893
+ /** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
13894
+ onboardedDeviceId: number().optional(),
13895
+ /** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
13896
+ onboardedName: string().optional()
13878
13897
  });
13879
13898
  /**
13880
13899
  * Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
@@ -13930,6 +13949,35 @@ var deviceProviderCapability = {
13930
13949
  name: string(),
13931
13950
  type: string()
13932
13951
  }))),
13952
+ /**
13953
+ * Tear down and reconstruct ONE device in place from its persisted rows —
13954
+ * touching no other device this provider owns.
13955
+ *
13956
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
13957
+ * migrated numbers: after `swapIds` the runner's live instance still
13958
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
13959
+ * registrations and its log tags), and a live object cannot be renumbered.
13960
+ * Before this method the only flush was restarting the whole owning addon
13961
+ * — which took every camera the provider owns down with it (28 devices
13962
+ * for one migrated camera, measured 2026-09-04, and the morning of the
13963
+ * same day ~27 devices' native caps did not come back on their own).
13964
+ *
13965
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
13966
+ * that changes. The reply carries the id the device answers on NOW.
13967
+ * Implemented once in `BaseDeviceProvider` — decommission the live
13968
+ * instance (if any), then re-create from the persisted row: the same
13969
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
13970
+ * An RPC, never an event: a dropped event would leave the runner writing
13971
+ * against the wrong camera (D8).
13972
+ *
13973
+ * Construction can dial hardware, and the migrated source is
13974
+ * characteristically dead — the timeout covers a full activate window
13975
+ * rather than the 60 s default.
13976
+ */
13977
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
13978
+ kind: "mutation",
13979
+ timeoutMs: 3 * 6e4
13980
+ }),
13933
13981
  supportsDiscovery: method(object({}), boolean()),
13934
13982
  /**
13935
13983
  * Run a network scan. `params` carries optional provider-specific scan
@@ -14257,7 +14305,8 @@ method(object({
14257
14305
  targetId: number()
14258
14306
  }), MigrateDeviceResultSchema, {
14259
14307
  kind: "mutation",
14260
- auth: "admin"
14308
+ auth: "admin",
14309
+ timeoutMs: 12 * 6e4
14261
14310
  }), 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({
14262
14311
  deviceId: number(),
14263
14312
  name: string()
@@ -33621,6 +33670,147 @@ var BaseDevice = class {
33621
33670
  }
33622
33671
  };
33623
33672
  /**
33673
+ * Delays before retry rounds 1..N — the round count IS the bound.
33674
+ * 10 s catches "the hub was busy for a moment"; the full schedule
33675
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
33676
+ * per attempt) covers a device-manager lock held for minutes — the
33677
+ * 2026-09-04 outage's migration hold was ~3.5 min.
33678
+ */
33679
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
33680
+ 1e4,
33681
+ 3e4,
33682
+ 9e4
33683
+ ];
33684
+ /** Abortable sleep — resolves early (never rejects) on abort. */
33685
+ function sleep$1(ms, signal) {
33686
+ return new Promise((resolve) => {
33687
+ if (signal.aborted) {
33688
+ resolve();
33689
+ return;
33690
+ }
33691
+ const onAbort = () => {
33692
+ clearTimeout(timer);
33693
+ resolve();
33694
+ };
33695
+ const timer = setTimeout(() => {
33696
+ signal.removeEventListener("abort", onAbort);
33697
+ resolve();
33698
+ }, ms);
33699
+ timer.unref?.();
33700
+ signal.addEventListener("abort", onAbort, { once: true });
33701
+ });
33702
+ }
33703
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
33704
+ * not reject (callers wrap their own try/catch). */
33705
+ async function runWithConcurrency(items, width, fn) {
33706
+ const queue = [...items];
33707
+ const laneCount = Math.max(1, Math.min(width, queue.length));
33708
+ const lane = async () => {
33709
+ for (;;) {
33710
+ const item = queue.shift();
33711
+ if (item === void 0) return;
33712
+ await fn(item);
33713
+ }
33714
+ };
33715
+ await Promise.all(Array.from({ length: laneCount }, lane));
33716
+ }
33717
+ var DeviceRestoreRetryScheduler = class {
33718
+ #logger;
33719
+ #attempt;
33720
+ #onPermanentFailure;
33721
+ #delaysMs;
33722
+ #concurrency;
33723
+ #now;
33724
+ #abort = new AbortController();
33725
+ constructor(options) {
33726
+ this.#logger = options.logger;
33727
+ this.#attempt = options.attempt;
33728
+ this.#onPermanentFailure = options.onPermanentFailure;
33729
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
33730
+ this.#concurrency = options.concurrency ?? 4;
33731
+ this.#now = options.now ?? Date.now;
33732
+ }
33733
+ /** Stop retrying (shutdown). Pending entries are NOT marked
33734
+ * permanently failed — the next boot restores them from disk. */
33735
+ cancel() {
33736
+ this.#abort.abort();
33737
+ }
33738
+ /**
33739
+ * Run the bounded retry rounds. Resolves when every entry has either
33740
+ * restored, been marked permanently failed, or the scheduler was
33741
+ * cancelled. Never rejects.
33742
+ */
33743
+ async run(initialFailures) {
33744
+ let pending = initialFailures.map((failure) => ({
33745
+ saved: failure.saved,
33746
+ lastError: failure.error,
33747
+ attempts: 1
33748
+ }));
33749
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
33750
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
33751
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
33752
+ if (this.#abort.signal.aborted) break;
33753
+ pending = await this.#runRound(pending, round);
33754
+ }
33755
+ if (this.#abort.signal.aborted) return [];
33756
+ const terminal = pending.map((entry) => ({
33757
+ deviceId: entry.saved.id,
33758
+ stableId: entry.saved.stableId,
33759
+ type: String(entry.saved.type),
33760
+ attempts: entry.attempts,
33761
+ lastError: entry.lastError,
33762
+ failedAt: this.#now()
33763
+ }));
33764
+ for (const failure of terminal) this.#onPermanentFailure(failure);
33765
+ return terminal;
33766
+ }
33767
+ /** One retry round: parents first (phase 0), then hub-adopted
33768
+ * children (phase 1) — a child's attempt depends on its parent
33769
+ * having landed, exactly like the initial two-pass restore. */
33770
+ async #runRound(pending, round) {
33771
+ const next = [];
33772
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
33773
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
33774
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
33775
+ if (this.#abort.signal.aborted) {
33776
+ next.push(entry);
33777
+ return;
33778
+ }
33779
+ const attemptNo = entry.attempts + 1;
33780
+ try {
33781
+ await this.#attempt(entry.saved);
33782
+ this.#logger.info("Device restored on retry", {
33783
+ tags: {
33784
+ deviceId: entry.saved.id,
33785
+ stableId: entry.saved.stableId
33786
+ },
33787
+ meta: { attempt: attemptNo }
33788
+ });
33789
+ } catch (err) {
33790
+ const lastError = err instanceof Error ? err.message : String(err);
33791
+ const remainingRetries = this.#delaysMs.length - (round + 1);
33792
+ this.#logger.warn("Device restore retry failed", {
33793
+ tags: {
33794
+ deviceId: entry.saved.id,
33795
+ stableId: entry.saved.stableId
33796
+ },
33797
+ meta: {
33798
+ attempt: attemptNo,
33799
+ remainingRetries,
33800
+ error: lastError
33801
+ }
33802
+ });
33803
+ next.push({
33804
+ saved: entry.saved,
33805
+ lastError,
33806
+ attempts: attemptNo
33807
+ });
33808
+ }
33809
+ });
33810
+ return next;
33811
+ }
33812
+ };
33813
+ /**
33624
33814
  * Convert an IDevice to the flat DeviceSummary shape expected by the
33625
33815
  * device-provider cap router. Shared across all providers.
33626
33816
  */
@@ -33669,6 +33859,7 @@ var BaseDeviceProvider = class extends BaseAddon {
33669
33859
  }];
33670
33860
  }
33671
33861
  async onShutdown() {
33862
+ this.cancelRestoreRetries();
33672
33863
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
33673
33864
  for (const device of devices) try {
33674
33865
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -33686,9 +33877,16 @@ var BaseDeviceProvider = class extends BaseAddon {
33686
33877
  async start() {}
33687
33878
  async stop() {}
33688
33879
  async getStatus() {
33880
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
33881
+ const summary = this.restoreFailureSummary();
33882
+ if (summary === null) return {
33883
+ connected: true,
33884
+ deviceCount: all.length
33885
+ };
33689
33886
  return {
33690
33887
  connected: true,
33691
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
33888
+ deviceCount: all.length,
33889
+ error: summary
33692
33890
  };
33693
33891
  }
33694
33892
  async getDevices() {
@@ -33778,8 +33976,137 @@ var BaseDeviceProvider = class extends BaseAddon {
33778
33976
  };
33779
33977
  }
33780
33978
  async restoreDevices(savedDevices) {
33781
- await this.onRestoreDevices(savedDevices);
33782
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
33979
+ const report = await this.onRestoreDevices(savedDevices);
33980
+ if (savedDevices.length === 0) return;
33981
+ if (report && report.failedCount > 0) {
33982
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
33983
+ return;
33984
+ }
33985
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
33986
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
33987
+ }
33988
+ /** Retry schedule. Overridable (tests use millisecond delays). */
33989
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
33990
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
33991
+ * never re-stampede full-width while the initial pass does (D167). */
33992
+ restoreRetryConcurrency = 4;
33993
+ _restoreRetryScheduler = null;
33994
+ _restoreRetryCompletion = null;
33995
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
33996
+ /** Settles when the background retry rounds finish (or `null` when
33997
+ * nothing failed). Exposed for tests and subclass diagnostics —
33998
+ * boot NEVER awaits this: the runner's post-init handshake goes out
33999
+ * with the devices that restored, and a late success is announced
34000
+ * through the `native-cap-change` → `updateCaps` path. */
34001
+ get restoreRetryCompletion() {
34002
+ return this._restoreRetryCompletion;
34003
+ }
34004
+ /** Devices that exhausted the retry bound this process lifetime. */
34005
+ get permanentRestoreFailures() {
34006
+ return [...this._permanentRestoreFailures.values()];
34007
+ }
34008
+ /** One-line operator-facing summary for `getStatus().error`, or
34009
+ * `null` when every device restored. */
34010
+ restoreFailureSummary() {
34011
+ if (this._permanentRestoreFailures.size === 0) return null;
34012
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
34013
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
34014
+ }
34015
+ cancelRestoreRetries() {
34016
+ this._restoreRetryScheduler?.cancel();
34017
+ this._restoreRetryScheduler = null;
34018
+ }
34019
+ recordPermanentRestoreFailure(failure) {
34020
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
34021
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
34022
+ tags: {
34023
+ deviceId: failure.deviceId,
34024
+ stableId: failure.stableId
34025
+ },
34026
+ meta: {
34027
+ type: failure.type,
34028
+ attempts: failure.attempts,
34029
+ error: failure.lastError
34030
+ }
34031
+ });
34032
+ }
34033
+ scheduleRestoreRetries(failures, attempt) {
34034
+ const scheduler = new DeviceRestoreRetryScheduler({
34035
+ logger: this.ctx.logger,
34036
+ delaysMs: this.restoreRetryDelaysMs,
34037
+ concurrency: this.restoreRetryConcurrency,
34038
+ attempt,
34039
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
34040
+ });
34041
+ this._restoreRetryScheduler = scheduler;
34042
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
34043
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
34044
+ });
34045
+ }
34046
+ /**
34047
+ * Tear down and reconstruct ONE device from its persisted rows — the
34048
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
34049
+ * and no other device this provider owns is disturbed.
34050
+ *
34051
+ * Keyed by `stableId` because the caller's whole reason to be here is that
34052
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
34053
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
34054
+ * whatever number the row carries NOW. The teardown is `decommission` —
34055
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
34056
+ * unregisters native caps, drops the registry entry) — and the rebuild is
34057
+ * the boot restore's own `create()` path, including its pass 2: first-class
34058
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
34059
+ * parent by the cascade and must be re-created explicitly, because only
34060
+ * accessory children come back through `getAccessoryChildren()`.
34061
+ *
34062
+ * Reloading an accessory child directly is refused (no device class) —
34063
+ * reload its parent instead.
34064
+ */
34065
+ async reloadDevice(input) {
34066
+ const { stableId } = input;
34067
+ const devices = this.ctx.kernel.devices;
34068
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
34069
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
34070
+ if (live) await devices.decommission(live.id);
34071
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
34072
+ addonId: this.addonId,
34073
+ stableId
34074
+ });
34075
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
34076
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
34077
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
34078
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
34079
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
34080
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
34081
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
34082
+ for (const row of rows) {
34083
+ if (row.parentDeviceId !== id) continue;
34084
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
34085
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
34086
+ if (!ChildClass) continue;
34087
+ try {
34088
+ await devices.create(row.stableId, ChildClass, {}, id);
34089
+ } catch (err) {
34090
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
34091
+ tags: {
34092
+ deviceId: row.id,
34093
+ stableId: row.stableId
34094
+ },
34095
+ meta: {
34096
+ parentDeviceId: id,
34097
+ error: err instanceof Error ? err.message : String(err)
34098
+ }
34099
+ });
34100
+ }
34101
+ }
34102
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
34103
+ tags: { deviceId: id },
34104
+ meta: {
34105
+ stableId,
34106
+ type: meta.type
34107
+ }
34108
+ });
34109
+ return { deviceId: id };
33783
34110
  }
33784
34111
  /**
33785
34112
  * Restore devices from persisted state. Two-pass:
@@ -33805,55 +34132,125 @@ var BaseDeviceProvider = class extends BaseAddon {
33805
34132
  * accessory-spawn flow handles via the parent's
33806
34133
  * `getAccessoryChildren()`. Override only when the default doesn't
33807
34134
  * fit.
34135
+ *
34136
+ * A row that fails either pass is NOT terminal (D347): it is handed
34137
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
34138
+ * Only after the bound is exhausted is the device marked permanently
34139
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
34140
+ * `getStatus().error`.
33808
34141
  */
34142
+ /**
34143
+ * Repair a row's PERSISTED config blob immediately before it is restored.
34144
+ * Default: no-op — most providers have nothing to heal.
34145
+ *
34146
+ * This exists because a restored device self-hydrates from the DB: `create()`
34147
+ * passes `{}` and `BaseDevice` parses the stored blob against the device
34148
+ * schema. A blob that lost a REQUIRED field therefore fails restore forever,
34149
+ * and no later pass revisits it — a hub-adopted Reolink camera whose blob had
34150
+ * been emptied failed all four bounded attempts against fields
34151
+ * (`host`, `password`) it inherits from its parent and never dials itself.
34152
+ *
34153
+ * Implementations get every saved row, so a child can read its parent's blob.
34154
+ * A heal that throws is treated like any other restore failure: retried under
34155
+ * the bound, then reported — never swallowed.
34156
+ */
34157
+ async healSavedConfig(_saved, _allSaved) {}
33809
34158
  async onRestoreDevices(savedDevices) {
33810
34159
  const restored = /* @__PURE__ */ new Set();
34160
+ const failures = [];
34161
+ const attemptRestore = async (saved) => {
34162
+ if (restored.has(saved.id)) return;
34163
+ const Class = this.deviceClasses[saved.type];
34164
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
34165
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
34166
+ await this.healSavedConfig(saved, savedDevices);
34167
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
34168
+ restored.add(saved.id);
34169
+ };
33811
34170
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
33812
34171
  const restoreOne = async (saved) => {
33813
- const Class = this.deviceClasses[saved.type];
33814
- if (!Class) {
34172
+ if (!this.deviceClasses[saved.type]) {
33815
34173
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
33816
- tags: { stableId: saved.stableId },
34174
+ tags: {
34175
+ deviceId: saved.id,
34176
+ stableId: saved.stableId
34177
+ },
33817
34178
  meta: { type: saved.type }
33818
34179
  });
33819
34180
  return;
33820
34181
  }
33821
34182
  try {
33822
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
33823
- restored.add(saved.id);
34183
+ await attemptRestore(saved);
33824
34184
  } catch (err) {
33825
- this.ctx.logger.warn("Failed to restore device", {
33826
- tags: { stableId: saved.stableId },
34185
+ const error = err instanceof Error ? err.message : String(err);
34186
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
34187
+ tags: {
34188
+ deviceId: saved.id,
34189
+ stableId: saved.stableId
34190
+ },
33827
34191
  meta: {
33828
34192
  type: saved.type,
33829
- error: err instanceof Error ? err.message : String(err)
34193
+ attempt: 1,
34194
+ error
33830
34195
  }
33831
34196
  });
34197
+ failures.push({
34198
+ saved,
34199
+ error
34200
+ });
33832
34201
  }
33833
34202
  };
33834
34203
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
34204
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
33835
34205
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
33836
34206
  for (const saved of childRows) {
33837
- const Class = this.deviceClasses[saved.type];
33838
- if (!Class) continue;
34207
+ if (!this.deviceClasses[saved.type]) continue;
33839
34208
  if (saved.parentDeviceId === null) continue;
33840
- if (!restored.has(saved.parentDeviceId)) continue;
33841
- try {
33842
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33843
- restored.add(saved.id);
33844
- } catch (err) {
33845
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
34209
+ if (restored.has(saved.parentDeviceId)) {
34210
+ try {
34211
+ await attemptRestore(saved);
34212
+ } catch (err) {
34213
+ const error = err instanceof Error ? err.message : String(err);
34214
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
34215
+ tags: {
34216
+ deviceId: saved.id,
34217
+ stableId: saved.stableId,
34218
+ parentDeviceId: saved.parentDeviceId
34219
+ },
34220
+ meta: {
34221
+ type: saved.type,
34222
+ attempt: 1,
34223
+ error
34224
+ }
34225
+ });
34226
+ failures.push({
34227
+ saved,
34228
+ error
34229
+ });
34230
+ }
34231
+ continue;
34232
+ }
34233
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
34234
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
33846
34235
  tags: {
34236
+ deviceId: saved.id,
33847
34237
  stableId: saved.stableId,
33848
34238
  parentDeviceId: saved.parentDeviceId
33849
34239
  },
33850
- meta: {
33851
- type: saved.type,
33852
- error: err instanceof Error ? err.message : String(err)
33853
- }
34240
+ meta: { type: saved.type }
34241
+ });
34242
+ failures.push({
34243
+ saved,
34244
+ error: `parent device ${saved.parentDeviceId} not restored`
33854
34245
  });
34246
+ continue;
33855
34247
  }
33856
34248
  }
34249
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
34250
+ return {
34251
+ restoredCount: restored.size,
34252
+ failedCount: failures.length
34253
+ };
33857
34254
  }
33858
34255
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
33859
34256
  toSummary(device) {
@@ -35616,6 +36013,12 @@ Object.freeze({
35616
36013
  addonId: null,
35617
36014
  access: "view"
35618
36015
  },
36016
+ "deviceProvider.reloadDevice": {
36017
+ capName: "device-provider",
36018
+ capScope: "system",
36019
+ addonId: null,
36020
+ access: "create"
36021
+ },
35619
36022
  "deviceProvider.start": {
35620
36023
  capName: "device-provider",
35621
36024
  capScope: "system",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-provider-petkit",
3
- "version": "0.2.59",
3
+ "version": "0.2.61",
4
4
  "description": "PetKit smart-feeder device-provider addon for CamStack — wraps the @apocaliss92/nodepetkit PetKit cloud client",
5
5
  "keywords": [
6
6
  "camstack",