@camstack/addon-provider-petkit 0.2.58 → 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 +509 -25
  2. package/dist/addon.mjs +509 -25
  3. package/package.json +1 -1
package/dist/addon.js CHANGED
@@ -11629,6 +11629,89 @@ var LocationStatSchema = object({
11629
11629
  fileCount: number(),
11630
11630
  present: boolean()
11631
11631
  });
11632
+ /** Lifecycle of a backup run. Terminal states: succeeded / failed / cancelled. */
11633
+ var BackupRunStateSchema = _enum([
11634
+ "queued",
11635
+ "running",
11636
+ "succeeded",
11637
+ "failed",
11638
+ "cancelled"
11639
+ ]);
11640
+ /**
11641
+ * Where a running backup currently is. `queued` before it starts,
11642
+ * `building` while the tar.gz is being staged, `uploading` during the
11643
+ * per-destination fan-out, `done` once terminal.
11644
+ */
11645
+ var BackupRunPhaseSchema = _enum([
11646
+ "queued",
11647
+ "building",
11648
+ "uploading",
11649
+ "done"
11650
+ ]);
11651
+ /**
11652
+ * Observable state of one backup run — readable WHILE it runs via
11653
+ * `backup.listRuns`. This is what makes the execution queue and
11654
+ * `backup.cancel` usable: the 2026-09-04 incident (two concurrent
11655
+ * multi-GB builds, staging 5.1 GB → 18 GB, load 62) was only
11656
+ * diagnosable with `du` because nothing reported that runs existed or
11657
+ * how large the staged archive had grown.
11658
+ */
11659
+ var BackupRunSchema = object({
11660
+ /** Stable run id — the handle `backup.cancel` takes. */
11661
+ id: string(),
11662
+ state: BackupRunStateSchema,
11663
+ phase: BackupRunPhaseSchema,
11664
+ /**
11665
+ * Resolved destination location ids. Empty while queued (targets are
11666
+ * resolved when the run starts, against the then-current policies).
11667
+ */
11668
+ destinationIds: array(string()).readonly(),
11669
+ label: string().optional(),
11670
+ /** ms-epoch when the run was submitted (trigger call / schedule fire). */
11671
+ requestedAt: number(),
11672
+ /** ms-epoch when the run left the queue and started building. */
11673
+ startedAt: number().optional(),
11674
+ /** ms-epoch when the run reached a terminal state. */
11675
+ finishedAt: number().optional(),
11676
+ /** Compressed bytes of the staging archive written so far. */
11677
+ stagedBytes: number(),
11678
+ /** Final staged archive size, once the build phase completes. */
11679
+ archiveSizeBytes: number().optional(),
11680
+ /** Bytes pushed to the destination currently uploading. */
11681
+ uploadedBytes: number(),
11682
+ /** Destinations where the archive fully landed (uploaded + indexed). */
11683
+ completedDestinationIds: array(string()).readonly(),
11684
+ /** Destinations that failed during the fan-out. */
11685
+ failedDestinationIds: array(string()).readonly(),
11686
+ /** Failure message when `state === 'failed'`. */
11687
+ error: string().optional(),
11688
+ /**
11689
+ * 1-based place in the execution queue — 1 = runs next. Present only
11690
+ * while `state === 'queued'`. Stamped by the orchestrator from the
11691
+ * queue's OWN pending order, never derived from timestamps, so the
11692
+ * UI cannot show an order the executor will not honour.
11693
+ */
11694
+ queuePosition: number().int().min(1).optional()
11695
+ });
11696
+ /**
11697
+ * Result of `backup.trigger`. The call still resolves when the run
11698
+ * terminates (compat with schedule-driven runs and the admin UI), but
11699
+ * it now names the run and says whether it had to WAIT: a trigger that
11700
+ * arrives while another run is in flight is enqueued (or joined onto
11701
+ * an identical already-queued run), never started concurrently.
11702
+ */
11703
+ var BackupTriggerResultSchema = object({
11704
+ /** The run this trigger mapped to — poll it via `listRuns`, stop it via `cancel`. */
11705
+ runId: string(),
11706
+ /** True when the run waited behind an in-flight run instead of starting immediately. */
11707
+ queued: boolean(),
11708
+ /** True when this trigger was coalesced onto an identical already-queued run. */
11709
+ joined: boolean(),
11710
+ /** True when the run was cancelled before completing every destination. */
11711
+ cancelled: boolean(),
11712
+ /** One entry per destination the archive landed at (partial on cancel). */
11713
+ entries: array(BackupEntrySchema).readonly()
11714
+ });
11632
11715
  /**
11633
11716
  * A backup schedule — the N:M "entry" that binds one cron cadence to a
11634
11717
  * SET of destination locations. Supersedes the per-location cron on
@@ -11676,7 +11759,10 @@ method(_void(), array(BackupDestinationInfoSchema).readonly(), { auth: "admin" }
11676
11759
  * retention (manual runs).
11677
11760
  */
11678
11761
  retentionCount: number().int().min(1).max(1e3).optional()
11679
- }).optional(), array(BackupEntrySchema).readonly(), {
11762
+ }).optional(), BackupTriggerResultSchema, {
11763
+ kind: "mutation",
11764
+ auth: "admin"
11765
+ }), method(_void(), array(BackupRunSchema).readonly(), { auth: "admin" }), method(object({ runId: string() }), object({ cancelled: boolean() }), {
11680
11766
  kind: "mutation",
11681
11767
  auth: "admin"
11682
11768
  }), method(_void(), array(BackupEntrySchema).readonly(), { auth: "admin" }), method(_void(), array(LocationStatSchema).readonly(), { auth: "admin" }), method(object({
@@ -12393,6 +12479,14 @@ method(object({
12393
12479
  }), object({ success: literal(true) }), {
12394
12480
  kind: "mutation",
12395
12481
  auth: "admin"
12482
+ }), method(object({ deviceId: number().int().nonnegative() }), object({
12483
+ derivedStreamsDeleted: array(string()).readonly(),
12484
+ assignmentsPurged: boolean(),
12485
+ probeSnapshotsDropped: number().int().nonnegative(),
12486
+ rtspTokenRowsDeleted: number().int().nonnegative()
12487
+ }), {
12488
+ kind: "mutation",
12489
+ auth: "admin"
12396
12490
  }), method(object({
12397
12491
  deviceId: number(),
12398
12492
  /** Absent = the LOWEST assigned profile — a notification attachment is
@@ -13837,6 +13931,35 @@ var deviceProviderCapability = {
13837
13931
  name: string(),
13838
13932
  type: string()
13839
13933
  }))),
13934
+ /**
13935
+ * Tear down and reconstruct ONE device in place from its persisted rows —
13936
+ * touching no other device this provider owns.
13937
+ *
13938
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
13939
+ * migrated numbers: after `swapIds` the runner's live instance still
13940
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
13941
+ * registrations and its log tags), and a live object cannot be renumbered.
13942
+ * Before this method the only flush was restarting the whole owning addon
13943
+ * — which took every camera the provider owns down with it (28 devices
13944
+ * for one migrated camera, measured 2026-09-04, and the morning of the
13945
+ * same day ~27 devices' native caps did not come back on their own).
13946
+ *
13947
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
13948
+ * that changes. The reply carries the id the device answers on NOW.
13949
+ * Implemented once in `BaseDeviceProvider` — decommission the live
13950
+ * instance (if any), then re-create from the persisted row: the same
13951
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
13952
+ * An RPC, never an event: a dropped event would leave the runner writing
13953
+ * against the wrong camera (D8).
13954
+ *
13955
+ * Construction can dial hardware, and the migrated source is
13956
+ * characteristically dead — the timeout covers a full activate window
13957
+ * rather than the 60 s default.
13958
+ */
13959
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
13960
+ kind: "mutation",
13961
+ timeoutMs: 3 * 6e4
13962
+ }),
13840
13963
  supportsDiscovery: method(object({}), boolean()),
13841
13964
  /**
13842
13965
  * Run a network scan. `params` carries optional provider-specific scan
@@ -14164,7 +14287,8 @@ method(object({
14164
14287
  targetId: number()
14165
14288
  }), MigrateDeviceResultSchema, {
14166
14289
  kind: "mutation",
14167
- auth: "admin"
14290
+ auth: "admin",
14291
+ timeoutMs: 12 * 6e4
14168
14292
  }), 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({
14169
14293
  deviceId: number(),
14170
14294
  name: string()
@@ -33528,6 +33652,147 @@ var BaseDevice = class {
33528
33652
  }
33529
33653
  };
33530
33654
  /**
33655
+ * Delays before retry rounds 1..N — the round count IS the bound.
33656
+ * 10 s catches "the hub was busy for a moment"; the full schedule
33657
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
33658
+ * per attempt) covers a device-manager lock held for minutes — the
33659
+ * 2026-09-04 outage's migration hold was ~3.5 min.
33660
+ */
33661
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
33662
+ 1e4,
33663
+ 3e4,
33664
+ 9e4
33665
+ ];
33666
+ /** Abortable sleep — resolves early (never rejects) on abort. */
33667
+ function sleep$1(ms, signal) {
33668
+ return new Promise((resolve) => {
33669
+ if (signal.aborted) {
33670
+ resolve();
33671
+ return;
33672
+ }
33673
+ const onAbort = () => {
33674
+ clearTimeout(timer);
33675
+ resolve();
33676
+ };
33677
+ const timer = setTimeout(() => {
33678
+ signal.removeEventListener("abort", onAbort);
33679
+ resolve();
33680
+ }, ms);
33681
+ timer.unref?.();
33682
+ signal.addEventListener("abort", onAbort, { once: true });
33683
+ });
33684
+ }
33685
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
33686
+ * not reject (callers wrap their own try/catch). */
33687
+ async function runWithConcurrency(items, width, fn) {
33688
+ const queue = [...items];
33689
+ const laneCount = Math.max(1, Math.min(width, queue.length));
33690
+ const lane = async () => {
33691
+ for (;;) {
33692
+ const item = queue.shift();
33693
+ if (item === void 0) return;
33694
+ await fn(item);
33695
+ }
33696
+ };
33697
+ await Promise.all(Array.from({ length: laneCount }, lane));
33698
+ }
33699
+ var DeviceRestoreRetryScheduler = class {
33700
+ #logger;
33701
+ #attempt;
33702
+ #onPermanentFailure;
33703
+ #delaysMs;
33704
+ #concurrency;
33705
+ #now;
33706
+ #abort = new AbortController();
33707
+ constructor(options) {
33708
+ this.#logger = options.logger;
33709
+ this.#attempt = options.attempt;
33710
+ this.#onPermanentFailure = options.onPermanentFailure;
33711
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
33712
+ this.#concurrency = options.concurrency ?? 4;
33713
+ this.#now = options.now ?? Date.now;
33714
+ }
33715
+ /** Stop retrying (shutdown). Pending entries are NOT marked
33716
+ * permanently failed — the next boot restores them from disk. */
33717
+ cancel() {
33718
+ this.#abort.abort();
33719
+ }
33720
+ /**
33721
+ * Run the bounded retry rounds. Resolves when every entry has either
33722
+ * restored, been marked permanently failed, or the scheduler was
33723
+ * cancelled. Never rejects.
33724
+ */
33725
+ async run(initialFailures) {
33726
+ let pending = initialFailures.map((failure) => ({
33727
+ saved: failure.saved,
33728
+ lastError: failure.error,
33729
+ attempts: 1
33730
+ }));
33731
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
33732
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
33733
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
33734
+ if (this.#abort.signal.aborted) break;
33735
+ pending = await this.#runRound(pending, round);
33736
+ }
33737
+ if (this.#abort.signal.aborted) return [];
33738
+ const terminal = pending.map((entry) => ({
33739
+ deviceId: entry.saved.id,
33740
+ stableId: entry.saved.stableId,
33741
+ type: String(entry.saved.type),
33742
+ attempts: entry.attempts,
33743
+ lastError: entry.lastError,
33744
+ failedAt: this.#now()
33745
+ }));
33746
+ for (const failure of terminal) this.#onPermanentFailure(failure);
33747
+ return terminal;
33748
+ }
33749
+ /** One retry round: parents first (phase 0), then hub-adopted
33750
+ * children (phase 1) — a child's attempt depends on its parent
33751
+ * having landed, exactly like the initial two-pass restore. */
33752
+ async #runRound(pending, round) {
33753
+ const next = [];
33754
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
33755
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
33756
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
33757
+ if (this.#abort.signal.aborted) {
33758
+ next.push(entry);
33759
+ return;
33760
+ }
33761
+ const attemptNo = entry.attempts + 1;
33762
+ try {
33763
+ await this.#attempt(entry.saved);
33764
+ this.#logger.info("Device restored on retry", {
33765
+ tags: {
33766
+ deviceId: entry.saved.id,
33767
+ stableId: entry.saved.stableId
33768
+ },
33769
+ meta: { attempt: attemptNo }
33770
+ });
33771
+ } catch (err) {
33772
+ const lastError = err instanceof Error ? err.message : String(err);
33773
+ const remainingRetries = this.#delaysMs.length - (round + 1);
33774
+ this.#logger.warn("Device restore retry failed", {
33775
+ tags: {
33776
+ deviceId: entry.saved.id,
33777
+ stableId: entry.saved.stableId
33778
+ },
33779
+ meta: {
33780
+ attempt: attemptNo,
33781
+ remainingRetries,
33782
+ error: lastError
33783
+ }
33784
+ });
33785
+ next.push({
33786
+ saved: entry.saved,
33787
+ lastError,
33788
+ attempts: attemptNo
33789
+ });
33790
+ }
33791
+ });
33792
+ return next;
33793
+ }
33794
+ };
33795
+ /**
33531
33796
  * Convert an IDevice to the flat DeviceSummary shape expected by the
33532
33797
  * device-provider cap router. Shared across all providers.
33533
33798
  */
@@ -33576,6 +33841,7 @@ var BaseDeviceProvider = class extends BaseAddon {
33576
33841
  }];
33577
33842
  }
33578
33843
  async onShutdown() {
33844
+ this.cancelRestoreRetries();
33579
33845
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
33580
33846
  for (const device of devices) try {
33581
33847
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -33593,9 +33859,16 @@ var BaseDeviceProvider = class extends BaseAddon {
33593
33859
  async start() {}
33594
33860
  async stop() {}
33595
33861
  async getStatus() {
33862
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
33863
+ const summary = this.restoreFailureSummary();
33864
+ if (summary === null) return {
33865
+ connected: true,
33866
+ deviceCount: all.length
33867
+ };
33596
33868
  return {
33597
33869
  connected: true,
33598
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
33870
+ deviceCount: all.length,
33871
+ error: summary
33599
33872
  };
33600
33873
  }
33601
33874
  async getDevices() {
@@ -33685,8 +33958,137 @@ var BaseDeviceProvider = class extends BaseAddon {
33685
33958
  };
33686
33959
  }
33687
33960
  async restoreDevices(savedDevices) {
33688
- await this.onRestoreDevices(savedDevices);
33689
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
33961
+ const report = await this.onRestoreDevices(savedDevices);
33962
+ if (savedDevices.length === 0) return;
33963
+ if (report && report.failedCount > 0) {
33964
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
33965
+ return;
33966
+ }
33967
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
33968
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
33969
+ }
33970
+ /** Retry schedule. Overridable (tests use millisecond delays). */
33971
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
33972
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
33973
+ * never re-stampede full-width while the initial pass does (D167). */
33974
+ restoreRetryConcurrency = 4;
33975
+ _restoreRetryScheduler = null;
33976
+ _restoreRetryCompletion = null;
33977
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
33978
+ /** Settles when the background retry rounds finish (or `null` when
33979
+ * nothing failed). Exposed for tests and subclass diagnostics —
33980
+ * boot NEVER awaits this: the runner's post-init handshake goes out
33981
+ * with the devices that restored, and a late success is announced
33982
+ * through the `native-cap-change` → `updateCaps` path. */
33983
+ get restoreRetryCompletion() {
33984
+ return this._restoreRetryCompletion;
33985
+ }
33986
+ /** Devices that exhausted the retry bound this process lifetime. */
33987
+ get permanentRestoreFailures() {
33988
+ return [...this._permanentRestoreFailures.values()];
33989
+ }
33990
+ /** One-line operator-facing summary for `getStatus().error`, or
33991
+ * `null` when every device restored. */
33992
+ restoreFailureSummary() {
33993
+ if (this._permanentRestoreFailures.size === 0) return null;
33994
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
33995
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
33996
+ }
33997
+ cancelRestoreRetries() {
33998
+ this._restoreRetryScheduler?.cancel();
33999
+ this._restoreRetryScheduler = null;
34000
+ }
34001
+ recordPermanentRestoreFailure(failure) {
34002
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
34003
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
34004
+ tags: {
34005
+ deviceId: failure.deviceId,
34006
+ stableId: failure.stableId
34007
+ },
34008
+ meta: {
34009
+ type: failure.type,
34010
+ attempts: failure.attempts,
34011
+ error: failure.lastError
34012
+ }
34013
+ });
34014
+ }
34015
+ scheduleRestoreRetries(failures, attempt) {
34016
+ const scheduler = new DeviceRestoreRetryScheduler({
34017
+ logger: this.ctx.logger,
34018
+ delaysMs: this.restoreRetryDelaysMs,
34019
+ concurrency: this.restoreRetryConcurrency,
34020
+ attempt,
34021
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
34022
+ });
34023
+ this._restoreRetryScheduler = scheduler;
34024
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
34025
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
34026
+ });
34027
+ }
34028
+ /**
34029
+ * Tear down and reconstruct ONE device from its persisted rows — the
34030
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
34031
+ * and no other device this provider owns is disturbed.
34032
+ *
34033
+ * Keyed by `stableId` because the caller's whole reason to be here is that
34034
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
34035
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
34036
+ * whatever number the row carries NOW. The teardown is `decommission` —
34037
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
34038
+ * unregisters native caps, drops the registry entry) — and the rebuild is
34039
+ * the boot restore's own `create()` path, including its pass 2: first-class
34040
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
34041
+ * parent by the cascade and must be re-created explicitly, because only
34042
+ * accessory children come back through `getAccessoryChildren()`.
34043
+ *
34044
+ * Reloading an accessory child directly is refused (no device class) —
34045
+ * reload its parent instead.
34046
+ */
34047
+ async reloadDevice(input) {
34048
+ const { stableId } = input;
34049
+ const devices = this.ctx.kernel.devices;
34050
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
34051
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
34052
+ if (live) await devices.decommission(live.id);
34053
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
34054
+ addonId: this.addonId,
34055
+ stableId
34056
+ });
34057
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
34058
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
34059
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
34060
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
34061
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
34062
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
34063
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
34064
+ for (const row of rows) {
34065
+ if (row.parentDeviceId !== id) continue;
34066
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
34067
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
34068
+ if (!ChildClass) continue;
34069
+ try {
34070
+ await devices.create(row.stableId, ChildClass, {}, id);
34071
+ } catch (err) {
34072
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
34073
+ tags: {
34074
+ deviceId: row.id,
34075
+ stableId: row.stableId
34076
+ },
34077
+ meta: {
34078
+ parentDeviceId: id,
34079
+ error: err instanceof Error ? err.message : String(err)
34080
+ }
34081
+ });
34082
+ }
34083
+ }
34084
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
34085
+ tags: { deviceId: id },
34086
+ meta: {
34087
+ stableId,
34088
+ type: meta.type
34089
+ }
34090
+ });
34091
+ return { deviceId: id };
33690
34092
  }
33691
34093
  /**
33692
34094
  * Restore devices from persisted state. Two-pass:
@@ -33712,55 +34114,108 @@ var BaseDeviceProvider = class extends BaseAddon {
33712
34114
  * accessory-spawn flow handles via the parent's
33713
34115
  * `getAccessoryChildren()`. Override only when the default doesn't
33714
34116
  * fit.
34117
+ *
34118
+ * A row that fails either pass is NOT terminal (D347): it is handed
34119
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
34120
+ * Only after the bound is exhausted is the device marked permanently
34121
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
34122
+ * `getStatus().error`.
33715
34123
  */
33716
34124
  async onRestoreDevices(savedDevices) {
33717
34125
  const restored = /* @__PURE__ */ new Set();
34126
+ const failures = [];
34127
+ const attemptRestore = async (saved) => {
34128
+ if (restored.has(saved.id)) return;
34129
+ const Class = this.deviceClasses[saved.type];
34130
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
34131
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
34132
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
34133
+ restored.add(saved.id);
34134
+ };
33718
34135
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
33719
34136
  const restoreOne = async (saved) => {
33720
- const Class = this.deviceClasses[saved.type];
33721
- if (!Class) {
34137
+ if (!this.deviceClasses[saved.type]) {
33722
34138
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
33723
- tags: { stableId: saved.stableId },
34139
+ tags: {
34140
+ deviceId: saved.id,
34141
+ stableId: saved.stableId
34142
+ },
33724
34143
  meta: { type: saved.type }
33725
34144
  });
33726
34145
  return;
33727
34146
  }
33728
34147
  try {
33729
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
33730
- restored.add(saved.id);
34148
+ await attemptRestore(saved);
33731
34149
  } catch (err) {
33732
- this.ctx.logger.warn("Failed to restore device", {
33733
- tags: { stableId: saved.stableId },
34150
+ const error = err instanceof Error ? err.message : String(err);
34151
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
34152
+ tags: {
34153
+ deviceId: saved.id,
34154
+ stableId: saved.stableId
34155
+ },
33734
34156
  meta: {
33735
34157
  type: saved.type,
33736
- error: err instanceof Error ? err.message : String(err)
34158
+ attempt: 1,
34159
+ error
33737
34160
  }
33738
34161
  });
34162
+ failures.push({
34163
+ saved,
34164
+ error
34165
+ });
33739
34166
  }
33740
34167
  };
33741
34168
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
34169
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
33742
34170
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
33743
34171
  for (const saved of childRows) {
33744
- const Class = this.deviceClasses[saved.type];
33745
- if (!Class) continue;
34172
+ if (!this.deviceClasses[saved.type]) continue;
33746
34173
  if (saved.parentDeviceId === null) continue;
33747
- if (!restored.has(saved.parentDeviceId)) continue;
33748
- try {
33749
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33750
- restored.add(saved.id);
33751
- } catch (err) {
33752
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
34174
+ if (restored.has(saved.parentDeviceId)) {
34175
+ try {
34176
+ await attemptRestore(saved);
34177
+ } catch (err) {
34178
+ const error = err instanceof Error ? err.message : String(err);
34179
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
34180
+ tags: {
34181
+ deviceId: saved.id,
34182
+ stableId: saved.stableId,
34183
+ parentDeviceId: saved.parentDeviceId
34184
+ },
34185
+ meta: {
34186
+ type: saved.type,
34187
+ attempt: 1,
34188
+ error
34189
+ }
34190
+ });
34191
+ failures.push({
34192
+ saved,
34193
+ error
34194
+ });
34195
+ }
34196
+ continue;
34197
+ }
34198
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
34199
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
33753
34200
  tags: {
34201
+ deviceId: saved.id,
33754
34202
  stableId: saved.stableId,
33755
34203
  parentDeviceId: saved.parentDeviceId
33756
34204
  },
33757
- meta: {
33758
- type: saved.type,
33759
- error: err instanceof Error ? err.message : String(err)
33760
- }
34205
+ meta: { type: saved.type }
33761
34206
  });
34207
+ failures.push({
34208
+ saved,
34209
+ error: `parent device ${saved.parentDeviceId} not restored`
34210
+ });
34211
+ continue;
33762
34212
  }
33763
34213
  }
34214
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
34215
+ return {
34216
+ restoredCount: restored.size,
34217
+ failedCount: failures.length
34218
+ };
33764
34219
  }
33765
34220
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
33766
34221
  toSummary(device) {
@@ -34269,6 +34724,12 @@ Object.freeze({
34269
34724
  addonId: null,
34270
34725
  access: "create"
34271
34726
  },
34727
+ "backup.cancel": {
34728
+ capName: "backup",
34729
+ capScope: "system",
34730
+ addonId: null,
34731
+ access: "create"
34732
+ },
34272
34733
  "backup.delete": {
34273
34734
  capName: "backup",
34274
34735
  capScope: "system",
@@ -34311,6 +34772,12 @@ Object.freeze({
34311
34772
  addonId: null,
34312
34773
  access: "view"
34313
34774
  },
34775
+ "backup.listRuns": {
34776
+ capName: "backup",
34777
+ capScope: "system",
34778
+ addonId: null,
34779
+ access: "view"
34780
+ },
34314
34781
  "backup.listSchedules": {
34315
34782
  capName: "backup",
34316
34783
  capScope: "system",
@@ -35511,6 +35978,12 @@ Object.freeze({
35511
35978
  addonId: null,
35512
35979
  access: "view"
35513
35980
  },
35981
+ "deviceProvider.reloadDevice": {
35982
+ capName: "device-provider",
35983
+ capScope: "system",
35984
+ addonId: null,
35985
+ access: "create"
35986
+ },
35514
35987
  "deviceProvider.start": {
35515
35988
  capName: "device-provider",
35516
35989
  capScope: "system",
@@ -38877,6 +39350,12 @@ Object.freeze({
38877
39350
  addonId: null,
38878
39351
  access: "create"
38879
39352
  },
39353
+ "streamBroker.forgetDeviceHardware": {
39354
+ capName: "stream-broker",
39355
+ capScope: "system",
39356
+ addonId: null,
39357
+ access: "delete"
39358
+ },
38880
39359
  "streamBroker.getAllRtspEntries": {
38881
39360
  capName: "stream-broker",
38882
39361
  capScope: "system",
@@ -41335,6 +41814,11 @@ Object.freeze({
41335
41814
  form: "single",
41336
41815
  optional: false
41337
41816
  }],
41817
+ "streamBroker.forgetDeviceHardware": [{
41818
+ name: "deviceId",
41819
+ form: "single",
41820
+ optional: false
41821
+ }],
41338
41822
  "streamBroker.getDeviceAudioMute": [{
41339
41823
  name: "deviceId",
41340
41824
  form: "single",
package/dist/addon.mjs CHANGED
@@ -11628,6 +11628,89 @@ var LocationStatSchema = object({
11628
11628
  fileCount: number(),
11629
11629
  present: boolean()
11630
11630
  });
11631
+ /** Lifecycle of a backup run. Terminal states: succeeded / failed / cancelled. */
11632
+ var BackupRunStateSchema = _enum([
11633
+ "queued",
11634
+ "running",
11635
+ "succeeded",
11636
+ "failed",
11637
+ "cancelled"
11638
+ ]);
11639
+ /**
11640
+ * Where a running backup currently is. `queued` before it starts,
11641
+ * `building` while the tar.gz is being staged, `uploading` during the
11642
+ * per-destination fan-out, `done` once terminal.
11643
+ */
11644
+ var BackupRunPhaseSchema = _enum([
11645
+ "queued",
11646
+ "building",
11647
+ "uploading",
11648
+ "done"
11649
+ ]);
11650
+ /**
11651
+ * Observable state of one backup run — readable WHILE it runs via
11652
+ * `backup.listRuns`. This is what makes the execution queue and
11653
+ * `backup.cancel` usable: the 2026-09-04 incident (two concurrent
11654
+ * multi-GB builds, staging 5.1 GB → 18 GB, load 62) was only
11655
+ * diagnosable with `du` because nothing reported that runs existed or
11656
+ * how large the staged archive had grown.
11657
+ */
11658
+ var BackupRunSchema = object({
11659
+ /** Stable run id — the handle `backup.cancel` takes. */
11660
+ id: string(),
11661
+ state: BackupRunStateSchema,
11662
+ phase: BackupRunPhaseSchema,
11663
+ /**
11664
+ * Resolved destination location ids. Empty while queued (targets are
11665
+ * resolved when the run starts, against the then-current policies).
11666
+ */
11667
+ destinationIds: array(string()).readonly(),
11668
+ label: string().optional(),
11669
+ /** ms-epoch when the run was submitted (trigger call / schedule fire). */
11670
+ requestedAt: number(),
11671
+ /** ms-epoch when the run left the queue and started building. */
11672
+ startedAt: number().optional(),
11673
+ /** ms-epoch when the run reached a terminal state. */
11674
+ finishedAt: number().optional(),
11675
+ /** Compressed bytes of the staging archive written so far. */
11676
+ stagedBytes: number(),
11677
+ /** Final staged archive size, once the build phase completes. */
11678
+ archiveSizeBytes: number().optional(),
11679
+ /** Bytes pushed to the destination currently uploading. */
11680
+ uploadedBytes: number(),
11681
+ /** Destinations where the archive fully landed (uploaded + indexed). */
11682
+ completedDestinationIds: array(string()).readonly(),
11683
+ /** Destinations that failed during the fan-out. */
11684
+ failedDestinationIds: array(string()).readonly(),
11685
+ /** Failure message when `state === 'failed'`. */
11686
+ error: string().optional(),
11687
+ /**
11688
+ * 1-based place in the execution queue — 1 = runs next. Present only
11689
+ * while `state === 'queued'`. Stamped by the orchestrator from the
11690
+ * queue's OWN pending order, never derived from timestamps, so the
11691
+ * UI cannot show an order the executor will not honour.
11692
+ */
11693
+ queuePosition: number().int().min(1).optional()
11694
+ });
11695
+ /**
11696
+ * Result of `backup.trigger`. The call still resolves when the run
11697
+ * terminates (compat with schedule-driven runs and the admin UI), but
11698
+ * it now names the run and says whether it had to WAIT: a trigger that
11699
+ * arrives while another run is in flight is enqueued (or joined onto
11700
+ * an identical already-queued run), never started concurrently.
11701
+ */
11702
+ var BackupTriggerResultSchema = object({
11703
+ /** The run this trigger mapped to — poll it via `listRuns`, stop it via `cancel`. */
11704
+ runId: string(),
11705
+ /** True when the run waited behind an in-flight run instead of starting immediately. */
11706
+ queued: boolean(),
11707
+ /** True when this trigger was coalesced onto an identical already-queued run. */
11708
+ joined: boolean(),
11709
+ /** True when the run was cancelled before completing every destination. */
11710
+ cancelled: boolean(),
11711
+ /** One entry per destination the archive landed at (partial on cancel). */
11712
+ entries: array(BackupEntrySchema).readonly()
11713
+ });
11631
11714
  /**
11632
11715
  * A backup schedule — the N:M "entry" that binds one cron cadence to a
11633
11716
  * SET of destination locations. Supersedes the per-location cron on
@@ -11675,7 +11758,10 @@ method(_void(), array(BackupDestinationInfoSchema).readonly(), { auth: "admin" }
11675
11758
  * retention (manual runs).
11676
11759
  */
11677
11760
  retentionCount: number().int().min(1).max(1e3).optional()
11678
- }).optional(), array(BackupEntrySchema).readonly(), {
11761
+ }).optional(), BackupTriggerResultSchema, {
11762
+ kind: "mutation",
11763
+ auth: "admin"
11764
+ }), method(_void(), array(BackupRunSchema).readonly(), { auth: "admin" }), method(object({ runId: string() }), object({ cancelled: boolean() }), {
11679
11765
  kind: "mutation",
11680
11766
  auth: "admin"
11681
11767
  }), method(_void(), array(BackupEntrySchema).readonly(), { auth: "admin" }), method(_void(), array(LocationStatSchema).readonly(), { auth: "admin" }), method(object({
@@ -12392,6 +12478,14 @@ method(object({
12392
12478
  }), object({ success: literal(true) }), {
12393
12479
  kind: "mutation",
12394
12480
  auth: "admin"
12481
+ }), method(object({ deviceId: number().int().nonnegative() }), object({
12482
+ derivedStreamsDeleted: array(string()).readonly(),
12483
+ assignmentsPurged: boolean(),
12484
+ probeSnapshotsDropped: number().int().nonnegative(),
12485
+ rtspTokenRowsDeleted: number().int().nonnegative()
12486
+ }), {
12487
+ kind: "mutation",
12488
+ auth: "admin"
12395
12489
  }), method(object({
12396
12490
  deviceId: number(),
12397
12491
  /** Absent = the LOWEST assigned profile — a notification attachment is
@@ -13836,6 +13930,35 @@ var deviceProviderCapability = {
13836
13930
  name: string(),
13837
13931
  type: string()
13838
13932
  }))),
13933
+ /**
13934
+ * Tear down and reconstruct ONE device in place from its persisted rows —
13935
+ * touching no other device this provider owns.
13936
+ *
13937
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
13938
+ * migrated numbers: after `swapIds` the runner's live instance still
13939
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
13940
+ * registrations and its log tags), and a live object cannot be renumbered.
13941
+ * Before this method the only flush was restarting the whole owning addon
13942
+ * — which took every camera the provider owns down with it (28 devices
13943
+ * for one migrated camera, measured 2026-09-04, and the morning of the
13944
+ * same day ~27 devices' native caps did not come back on their own).
13945
+ *
13946
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
13947
+ * that changes. The reply carries the id the device answers on NOW.
13948
+ * Implemented once in `BaseDeviceProvider` — decommission the live
13949
+ * instance (if any), then re-create from the persisted row: the same
13950
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
13951
+ * An RPC, never an event: a dropped event would leave the runner writing
13952
+ * against the wrong camera (D8).
13953
+ *
13954
+ * Construction can dial hardware, and the migrated source is
13955
+ * characteristically dead — the timeout covers a full activate window
13956
+ * rather than the 60 s default.
13957
+ */
13958
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
13959
+ kind: "mutation",
13960
+ timeoutMs: 3 * 6e4
13961
+ }),
13839
13962
  supportsDiscovery: method(object({}), boolean()),
13840
13963
  /**
13841
13964
  * Run a network scan. `params` carries optional provider-specific scan
@@ -14163,7 +14286,8 @@ method(object({
14163
14286
  targetId: number()
14164
14287
  }), MigrateDeviceResultSchema, {
14165
14288
  kind: "mutation",
14166
- auth: "admin"
14289
+ auth: "admin",
14290
+ timeoutMs: 12 * 6e4
14167
14291
  }), 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({
14168
14292
  deviceId: number(),
14169
14293
  name: string()
@@ -33527,6 +33651,147 @@ var BaseDevice = class {
33527
33651
  }
33528
33652
  };
33529
33653
  /**
33654
+ * Delays before retry rounds 1..N — the round count IS the bound.
33655
+ * 10 s catches "the hub was busy for a moment"; the full schedule
33656
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
33657
+ * per attempt) covers a device-manager lock held for minutes — the
33658
+ * 2026-09-04 outage's migration hold was ~3.5 min.
33659
+ */
33660
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
33661
+ 1e4,
33662
+ 3e4,
33663
+ 9e4
33664
+ ];
33665
+ /** Abortable sleep — resolves early (never rejects) on abort. */
33666
+ function sleep$1(ms, signal) {
33667
+ return new Promise((resolve) => {
33668
+ if (signal.aborted) {
33669
+ resolve();
33670
+ return;
33671
+ }
33672
+ const onAbort = () => {
33673
+ clearTimeout(timer);
33674
+ resolve();
33675
+ };
33676
+ const timer = setTimeout(() => {
33677
+ signal.removeEventListener("abort", onAbort);
33678
+ resolve();
33679
+ }, ms);
33680
+ timer.unref?.();
33681
+ signal.addEventListener("abort", onAbort, { once: true });
33682
+ });
33683
+ }
33684
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
33685
+ * not reject (callers wrap their own try/catch). */
33686
+ async function runWithConcurrency(items, width, fn) {
33687
+ const queue = [...items];
33688
+ const laneCount = Math.max(1, Math.min(width, queue.length));
33689
+ const lane = async () => {
33690
+ for (;;) {
33691
+ const item = queue.shift();
33692
+ if (item === void 0) return;
33693
+ await fn(item);
33694
+ }
33695
+ };
33696
+ await Promise.all(Array.from({ length: laneCount }, lane));
33697
+ }
33698
+ var DeviceRestoreRetryScheduler = class {
33699
+ #logger;
33700
+ #attempt;
33701
+ #onPermanentFailure;
33702
+ #delaysMs;
33703
+ #concurrency;
33704
+ #now;
33705
+ #abort = new AbortController();
33706
+ constructor(options) {
33707
+ this.#logger = options.logger;
33708
+ this.#attempt = options.attempt;
33709
+ this.#onPermanentFailure = options.onPermanentFailure;
33710
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
33711
+ this.#concurrency = options.concurrency ?? 4;
33712
+ this.#now = options.now ?? Date.now;
33713
+ }
33714
+ /** Stop retrying (shutdown). Pending entries are NOT marked
33715
+ * permanently failed — the next boot restores them from disk. */
33716
+ cancel() {
33717
+ this.#abort.abort();
33718
+ }
33719
+ /**
33720
+ * Run the bounded retry rounds. Resolves when every entry has either
33721
+ * restored, been marked permanently failed, or the scheduler was
33722
+ * cancelled. Never rejects.
33723
+ */
33724
+ async run(initialFailures) {
33725
+ let pending = initialFailures.map((failure) => ({
33726
+ saved: failure.saved,
33727
+ lastError: failure.error,
33728
+ attempts: 1
33729
+ }));
33730
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
33731
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
33732
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
33733
+ if (this.#abort.signal.aborted) break;
33734
+ pending = await this.#runRound(pending, round);
33735
+ }
33736
+ if (this.#abort.signal.aborted) return [];
33737
+ const terminal = pending.map((entry) => ({
33738
+ deviceId: entry.saved.id,
33739
+ stableId: entry.saved.stableId,
33740
+ type: String(entry.saved.type),
33741
+ attempts: entry.attempts,
33742
+ lastError: entry.lastError,
33743
+ failedAt: this.#now()
33744
+ }));
33745
+ for (const failure of terminal) this.#onPermanentFailure(failure);
33746
+ return terminal;
33747
+ }
33748
+ /** One retry round: parents first (phase 0), then hub-adopted
33749
+ * children (phase 1) — a child's attempt depends on its parent
33750
+ * having landed, exactly like the initial two-pass restore. */
33751
+ async #runRound(pending, round) {
33752
+ const next = [];
33753
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
33754
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
33755
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
33756
+ if (this.#abort.signal.aborted) {
33757
+ next.push(entry);
33758
+ return;
33759
+ }
33760
+ const attemptNo = entry.attempts + 1;
33761
+ try {
33762
+ await this.#attempt(entry.saved);
33763
+ this.#logger.info("Device restored on retry", {
33764
+ tags: {
33765
+ deviceId: entry.saved.id,
33766
+ stableId: entry.saved.stableId
33767
+ },
33768
+ meta: { attempt: attemptNo }
33769
+ });
33770
+ } catch (err) {
33771
+ const lastError = err instanceof Error ? err.message : String(err);
33772
+ const remainingRetries = this.#delaysMs.length - (round + 1);
33773
+ this.#logger.warn("Device restore retry failed", {
33774
+ tags: {
33775
+ deviceId: entry.saved.id,
33776
+ stableId: entry.saved.stableId
33777
+ },
33778
+ meta: {
33779
+ attempt: attemptNo,
33780
+ remainingRetries,
33781
+ error: lastError
33782
+ }
33783
+ });
33784
+ next.push({
33785
+ saved: entry.saved,
33786
+ lastError,
33787
+ attempts: attemptNo
33788
+ });
33789
+ }
33790
+ });
33791
+ return next;
33792
+ }
33793
+ };
33794
+ /**
33530
33795
  * Convert an IDevice to the flat DeviceSummary shape expected by the
33531
33796
  * device-provider cap router. Shared across all providers.
33532
33797
  */
@@ -33575,6 +33840,7 @@ var BaseDeviceProvider = class extends BaseAddon {
33575
33840
  }];
33576
33841
  }
33577
33842
  async onShutdown() {
33843
+ this.cancelRestoreRetries();
33578
33844
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
33579
33845
  for (const device of devices) try {
33580
33846
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -33592,9 +33858,16 @@ var BaseDeviceProvider = class extends BaseAddon {
33592
33858
  async start() {}
33593
33859
  async stop() {}
33594
33860
  async getStatus() {
33861
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
33862
+ const summary = this.restoreFailureSummary();
33863
+ if (summary === null) return {
33864
+ connected: true,
33865
+ deviceCount: all.length
33866
+ };
33595
33867
  return {
33596
33868
  connected: true,
33597
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
33869
+ deviceCount: all.length,
33870
+ error: summary
33598
33871
  };
33599
33872
  }
33600
33873
  async getDevices() {
@@ -33684,8 +33957,137 @@ var BaseDeviceProvider = class extends BaseAddon {
33684
33957
  };
33685
33958
  }
33686
33959
  async restoreDevices(savedDevices) {
33687
- await this.onRestoreDevices(savedDevices);
33688
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
33960
+ const report = await this.onRestoreDevices(savedDevices);
33961
+ if (savedDevices.length === 0) return;
33962
+ if (report && report.failedCount > 0) {
33963
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
33964
+ return;
33965
+ }
33966
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
33967
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
33968
+ }
33969
+ /** Retry schedule. Overridable (tests use millisecond delays). */
33970
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
33971
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
33972
+ * never re-stampede full-width while the initial pass does (D167). */
33973
+ restoreRetryConcurrency = 4;
33974
+ _restoreRetryScheduler = null;
33975
+ _restoreRetryCompletion = null;
33976
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
33977
+ /** Settles when the background retry rounds finish (or `null` when
33978
+ * nothing failed). Exposed for tests and subclass diagnostics —
33979
+ * boot NEVER awaits this: the runner's post-init handshake goes out
33980
+ * with the devices that restored, and a late success is announced
33981
+ * through the `native-cap-change` → `updateCaps` path. */
33982
+ get restoreRetryCompletion() {
33983
+ return this._restoreRetryCompletion;
33984
+ }
33985
+ /** Devices that exhausted the retry bound this process lifetime. */
33986
+ get permanentRestoreFailures() {
33987
+ return [...this._permanentRestoreFailures.values()];
33988
+ }
33989
+ /** One-line operator-facing summary for `getStatus().error`, or
33990
+ * `null` when every device restored. */
33991
+ restoreFailureSummary() {
33992
+ if (this._permanentRestoreFailures.size === 0) return null;
33993
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
33994
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
33995
+ }
33996
+ cancelRestoreRetries() {
33997
+ this._restoreRetryScheduler?.cancel();
33998
+ this._restoreRetryScheduler = null;
33999
+ }
34000
+ recordPermanentRestoreFailure(failure) {
34001
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
34002
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
34003
+ tags: {
34004
+ deviceId: failure.deviceId,
34005
+ stableId: failure.stableId
34006
+ },
34007
+ meta: {
34008
+ type: failure.type,
34009
+ attempts: failure.attempts,
34010
+ error: failure.lastError
34011
+ }
34012
+ });
34013
+ }
34014
+ scheduleRestoreRetries(failures, attempt) {
34015
+ const scheduler = new DeviceRestoreRetryScheduler({
34016
+ logger: this.ctx.logger,
34017
+ delaysMs: this.restoreRetryDelaysMs,
34018
+ concurrency: this.restoreRetryConcurrency,
34019
+ attempt,
34020
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
34021
+ });
34022
+ this._restoreRetryScheduler = scheduler;
34023
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
34024
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
34025
+ });
34026
+ }
34027
+ /**
34028
+ * Tear down and reconstruct ONE device from its persisted rows — the
34029
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
34030
+ * and no other device this provider owns is disturbed.
34031
+ *
34032
+ * Keyed by `stableId` because the caller's whole reason to be here is that
34033
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
34034
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
34035
+ * whatever number the row carries NOW. The teardown is `decommission` —
34036
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
34037
+ * unregisters native caps, drops the registry entry) — and the rebuild is
34038
+ * the boot restore's own `create()` path, including its pass 2: first-class
34039
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
34040
+ * parent by the cascade and must be re-created explicitly, because only
34041
+ * accessory children come back through `getAccessoryChildren()`.
34042
+ *
34043
+ * Reloading an accessory child directly is refused (no device class) —
34044
+ * reload its parent instead.
34045
+ */
34046
+ async reloadDevice(input) {
34047
+ const { stableId } = input;
34048
+ const devices = this.ctx.kernel.devices;
34049
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
34050
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
34051
+ if (live) await devices.decommission(live.id);
34052
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
34053
+ addonId: this.addonId,
34054
+ stableId
34055
+ });
34056
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
34057
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
34058
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
34059
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
34060
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
34061
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
34062
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
34063
+ for (const row of rows) {
34064
+ if (row.parentDeviceId !== id) continue;
34065
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
34066
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
34067
+ if (!ChildClass) continue;
34068
+ try {
34069
+ await devices.create(row.stableId, ChildClass, {}, id);
34070
+ } catch (err) {
34071
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
34072
+ tags: {
34073
+ deviceId: row.id,
34074
+ stableId: row.stableId
34075
+ },
34076
+ meta: {
34077
+ parentDeviceId: id,
34078
+ error: err instanceof Error ? err.message : String(err)
34079
+ }
34080
+ });
34081
+ }
34082
+ }
34083
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
34084
+ tags: { deviceId: id },
34085
+ meta: {
34086
+ stableId,
34087
+ type: meta.type
34088
+ }
34089
+ });
34090
+ return { deviceId: id };
33689
34091
  }
33690
34092
  /**
33691
34093
  * Restore devices from persisted state. Two-pass:
@@ -33711,55 +34113,108 @@ var BaseDeviceProvider = class extends BaseAddon {
33711
34113
  * accessory-spawn flow handles via the parent's
33712
34114
  * `getAccessoryChildren()`. Override only when the default doesn't
33713
34115
  * fit.
34116
+ *
34117
+ * A row that fails either pass is NOT terminal (D347): it is handed
34118
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
34119
+ * Only after the bound is exhausted is the device marked permanently
34120
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
34121
+ * `getStatus().error`.
33714
34122
  */
33715
34123
  async onRestoreDevices(savedDevices) {
33716
34124
  const restored = /* @__PURE__ */ new Set();
34125
+ const failures = [];
34126
+ const attemptRestore = async (saved) => {
34127
+ if (restored.has(saved.id)) return;
34128
+ const Class = this.deviceClasses[saved.type];
34129
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
34130
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
34131
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
34132
+ restored.add(saved.id);
34133
+ };
33717
34134
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
33718
34135
  const restoreOne = async (saved) => {
33719
- const Class = this.deviceClasses[saved.type];
33720
- if (!Class) {
34136
+ if (!this.deviceClasses[saved.type]) {
33721
34137
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
33722
- tags: { stableId: saved.stableId },
34138
+ tags: {
34139
+ deviceId: saved.id,
34140
+ stableId: saved.stableId
34141
+ },
33723
34142
  meta: { type: saved.type }
33724
34143
  });
33725
34144
  return;
33726
34145
  }
33727
34146
  try {
33728
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
33729
- restored.add(saved.id);
34147
+ await attemptRestore(saved);
33730
34148
  } catch (err) {
33731
- this.ctx.logger.warn("Failed to restore device", {
33732
- tags: { stableId: saved.stableId },
34149
+ const error = err instanceof Error ? err.message : String(err);
34150
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
34151
+ tags: {
34152
+ deviceId: saved.id,
34153
+ stableId: saved.stableId
34154
+ },
33733
34155
  meta: {
33734
34156
  type: saved.type,
33735
- error: err instanceof Error ? err.message : String(err)
34157
+ attempt: 1,
34158
+ error
33736
34159
  }
33737
34160
  });
34161
+ failures.push({
34162
+ saved,
34163
+ error
34164
+ });
33738
34165
  }
33739
34166
  };
33740
34167
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
34168
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
33741
34169
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
33742
34170
  for (const saved of childRows) {
33743
- const Class = this.deviceClasses[saved.type];
33744
- if (!Class) continue;
34171
+ if (!this.deviceClasses[saved.type]) continue;
33745
34172
  if (saved.parentDeviceId === null) continue;
33746
- if (!restored.has(saved.parentDeviceId)) continue;
33747
- try {
33748
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33749
- restored.add(saved.id);
33750
- } catch (err) {
33751
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
34173
+ if (restored.has(saved.parentDeviceId)) {
34174
+ try {
34175
+ await attemptRestore(saved);
34176
+ } catch (err) {
34177
+ const error = err instanceof Error ? err.message : String(err);
34178
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
34179
+ tags: {
34180
+ deviceId: saved.id,
34181
+ stableId: saved.stableId,
34182
+ parentDeviceId: saved.parentDeviceId
34183
+ },
34184
+ meta: {
34185
+ type: saved.type,
34186
+ attempt: 1,
34187
+ error
34188
+ }
34189
+ });
34190
+ failures.push({
34191
+ saved,
34192
+ error
34193
+ });
34194
+ }
34195
+ continue;
34196
+ }
34197
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
34198
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
33752
34199
  tags: {
34200
+ deviceId: saved.id,
33753
34201
  stableId: saved.stableId,
33754
34202
  parentDeviceId: saved.parentDeviceId
33755
34203
  },
33756
- meta: {
33757
- type: saved.type,
33758
- error: err instanceof Error ? err.message : String(err)
33759
- }
34204
+ meta: { type: saved.type }
33760
34205
  });
34206
+ failures.push({
34207
+ saved,
34208
+ error: `parent device ${saved.parentDeviceId} not restored`
34209
+ });
34210
+ continue;
33761
34211
  }
33762
34212
  }
34213
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
34214
+ return {
34215
+ restoredCount: restored.size,
34216
+ failedCount: failures.length
34217
+ };
33763
34218
  }
33764
34219
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
33765
34220
  toSummary(device) {
@@ -34268,6 +34723,12 @@ Object.freeze({
34268
34723
  addonId: null,
34269
34724
  access: "create"
34270
34725
  },
34726
+ "backup.cancel": {
34727
+ capName: "backup",
34728
+ capScope: "system",
34729
+ addonId: null,
34730
+ access: "create"
34731
+ },
34271
34732
  "backup.delete": {
34272
34733
  capName: "backup",
34273
34734
  capScope: "system",
@@ -34310,6 +34771,12 @@ Object.freeze({
34310
34771
  addonId: null,
34311
34772
  access: "view"
34312
34773
  },
34774
+ "backup.listRuns": {
34775
+ capName: "backup",
34776
+ capScope: "system",
34777
+ addonId: null,
34778
+ access: "view"
34779
+ },
34313
34780
  "backup.listSchedules": {
34314
34781
  capName: "backup",
34315
34782
  capScope: "system",
@@ -35510,6 +35977,12 @@ Object.freeze({
35510
35977
  addonId: null,
35511
35978
  access: "view"
35512
35979
  },
35980
+ "deviceProvider.reloadDevice": {
35981
+ capName: "device-provider",
35982
+ capScope: "system",
35983
+ addonId: null,
35984
+ access: "create"
35985
+ },
35513
35986
  "deviceProvider.start": {
35514
35987
  capName: "device-provider",
35515
35988
  capScope: "system",
@@ -38876,6 +39349,12 @@ Object.freeze({
38876
39349
  addonId: null,
38877
39350
  access: "create"
38878
39351
  },
39352
+ "streamBroker.forgetDeviceHardware": {
39353
+ capName: "stream-broker",
39354
+ capScope: "system",
39355
+ addonId: null,
39356
+ access: "delete"
39357
+ },
38879
39358
  "streamBroker.getAllRtspEntries": {
38880
39359
  capName: "stream-broker",
38881
39360
  capScope: "system",
@@ -41334,6 +41813,11 @@ Object.freeze({
41334
41813
  form: "single",
41335
41814
  optional: false
41336
41815
  }],
41816
+ "streamBroker.forgetDeviceHardware": [{
41817
+ name: "deviceId",
41818
+ form: "single",
41819
+ optional: false
41820
+ }],
41337
41821
  "streamBroker.getDeviceAudioMute": [{
41338
41822
  name: "deviceId",
41339
41823
  form: "single",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-provider-petkit",
3
- "version": "0.2.58",
3
+ "version": "0.2.60",
4
4
  "description": "PetKit smart-feeder device-provider addon for CamStack — wraps the @apocaliss92/nodepetkit PetKit cloud client",
5
5
  "keywords": [
6
6
  "camstack",