@camstack/addon-provider-dreo 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
@@ -10563,6 +10563,89 @@ var LocationStatSchema = object({
10563
10563
  fileCount: number(),
10564
10564
  present: boolean()
10565
10565
  });
10566
+ /** Lifecycle of a backup run. Terminal states: succeeded / failed / cancelled. */
10567
+ var BackupRunStateSchema = _enum([
10568
+ "queued",
10569
+ "running",
10570
+ "succeeded",
10571
+ "failed",
10572
+ "cancelled"
10573
+ ]);
10574
+ /**
10575
+ * Where a running backup currently is. `queued` before it starts,
10576
+ * `building` while the tar.gz is being staged, `uploading` during the
10577
+ * per-destination fan-out, `done` once terminal.
10578
+ */
10579
+ var BackupRunPhaseSchema = _enum([
10580
+ "queued",
10581
+ "building",
10582
+ "uploading",
10583
+ "done"
10584
+ ]);
10585
+ /**
10586
+ * Observable state of one backup run — readable WHILE it runs via
10587
+ * `backup.listRuns`. This is what makes the execution queue and
10588
+ * `backup.cancel` usable: the 2026-09-04 incident (two concurrent
10589
+ * multi-GB builds, staging 5.1 GB → 18 GB, load 62) was only
10590
+ * diagnosable with `du` because nothing reported that runs existed or
10591
+ * how large the staged archive had grown.
10592
+ */
10593
+ var BackupRunSchema = object({
10594
+ /** Stable run id — the handle `backup.cancel` takes. */
10595
+ id: string(),
10596
+ state: BackupRunStateSchema,
10597
+ phase: BackupRunPhaseSchema,
10598
+ /**
10599
+ * Resolved destination location ids. Empty while queued (targets are
10600
+ * resolved when the run starts, against the then-current policies).
10601
+ */
10602
+ destinationIds: array(string()).readonly(),
10603
+ label: string().optional(),
10604
+ /** ms-epoch when the run was submitted (trigger call / schedule fire). */
10605
+ requestedAt: number(),
10606
+ /** ms-epoch when the run left the queue and started building. */
10607
+ startedAt: number().optional(),
10608
+ /** ms-epoch when the run reached a terminal state. */
10609
+ finishedAt: number().optional(),
10610
+ /** Compressed bytes of the staging archive written so far. */
10611
+ stagedBytes: number(),
10612
+ /** Final staged archive size, once the build phase completes. */
10613
+ archiveSizeBytes: number().optional(),
10614
+ /** Bytes pushed to the destination currently uploading. */
10615
+ uploadedBytes: number(),
10616
+ /** Destinations where the archive fully landed (uploaded + indexed). */
10617
+ completedDestinationIds: array(string()).readonly(),
10618
+ /** Destinations that failed during the fan-out. */
10619
+ failedDestinationIds: array(string()).readonly(),
10620
+ /** Failure message when `state === 'failed'`. */
10621
+ error: string().optional(),
10622
+ /**
10623
+ * 1-based place in the execution queue — 1 = runs next. Present only
10624
+ * while `state === 'queued'`. Stamped by the orchestrator from the
10625
+ * queue's OWN pending order, never derived from timestamps, so the
10626
+ * UI cannot show an order the executor will not honour.
10627
+ */
10628
+ queuePosition: number().int().min(1).optional()
10629
+ });
10630
+ /**
10631
+ * Result of `backup.trigger`. The call still resolves when the run
10632
+ * terminates (compat with schedule-driven runs and the admin UI), but
10633
+ * it now names the run and says whether it had to WAIT: a trigger that
10634
+ * arrives while another run is in flight is enqueued (or joined onto
10635
+ * an identical already-queued run), never started concurrently.
10636
+ */
10637
+ var BackupTriggerResultSchema = object({
10638
+ /** The run this trigger mapped to — poll it via `listRuns`, stop it via `cancel`. */
10639
+ runId: string(),
10640
+ /** True when the run waited behind an in-flight run instead of starting immediately. */
10641
+ queued: boolean(),
10642
+ /** True when this trigger was coalesced onto an identical already-queued run. */
10643
+ joined: boolean(),
10644
+ /** True when the run was cancelled before completing every destination. */
10645
+ cancelled: boolean(),
10646
+ /** One entry per destination the archive landed at (partial on cancel). */
10647
+ entries: array(BackupEntrySchema).readonly()
10648
+ });
10566
10649
  /**
10567
10650
  * A backup schedule — the N:M "entry" that binds one cron cadence to a
10568
10651
  * SET of destination locations. Supersedes the per-location cron on
@@ -10610,7 +10693,10 @@ method(_void(), array(BackupDestinationInfoSchema).readonly(), { auth: "admin" }
10610
10693
  * retention (manual runs).
10611
10694
  */
10612
10695
  retentionCount: number().int().min(1).max(1e3).optional()
10613
- }).optional(), array(BackupEntrySchema).readonly(), {
10696
+ }).optional(), BackupTriggerResultSchema, {
10697
+ kind: "mutation",
10698
+ auth: "admin"
10699
+ }), method(_void(), array(BackupRunSchema).readonly(), { auth: "admin" }), method(object({ runId: string() }), object({ cancelled: boolean() }), {
10614
10700
  kind: "mutation",
10615
10701
  auth: "admin"
10616
10702
  }), method(_void(), array(BackupEntrySchema).readonly(), { auth: "admin" }), method(_void(), array(LocationStatSchema).readonly(), { auth: "admin" }), method(object({
@@ -11327,6 +11413,14 @@ method(object({
11327
11413
  }), object({ success: literal(true) }), {
11328
11414
  kind: "mutation",
11329
11415
  auth: "admin"
11416
+ }), method(object({ deviceId: number().int().nonnegative() }), object({
11417
+ derivedStreamsDeleted: array(string()).readonly(),
11418
+ assignmentsPurged: boolean(),
11419
+ probeSnapshotsDropped: number().int().nonnegative(),
11420
+ rtspTokenRowsDeleted: number().int().nonnegative()
11421
+ }), {
11422
+ kind: "mutation",
11423
+ auth: "admin"
11330
11424
  }), method(object({
11331
11425
  deviceId: number(),
11332
11426
  /** Absent = the LOWEST assigned profile — a notification attachment is
@@ -12771,6 +12865,35 @@ var deviceProviderCapability = {
12771
12865
  name: string(),
12772
12866
  type: string()
12773
12867
  }))),
12868
+ /**
12869
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12870
+ * touching no other device this provider owns.
12871
+ *
12872
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12873
+ * migrated numbers: after `swapIds` the runner's live instance still
12874
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12875
+ * registrations and its log tags), and a live object cannot be renumbered.
12876
+ * Before this method the only flush was restarting the whole owning addon
12877
+ * — which took every camera the provider owns down with it (28 devices
12878
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12879
+ * same day ~27 devices' native caps did not come back on their own).
12880
+ *
12881
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12882
+ * that changes. The reply carries the id the device answers on NOW.
12883
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12884
+ * instance (if any), then re-create from the persisted row: the same
12885
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12886
+ * An RPC, never an event: a dropped event would leave the runner writing
12887
+ * against the wrong camera (D8).
12888
+ *
12889
+ * Construction can dial hardware, and the migrated source is
12890
+ * characteristically dead — the timeout covers a full activate window
12891
+ * rather than the 60 s default.
12892
+ */
12893
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12894
+ kind: "mutation",
12895
+ timeoutMs: 3 * 6e4
12896
+ }),
12774
12897
  supportsDiscovery: method(object({}), boolean()),
12775
12898
  /**
12776
12899
  * Run a network scan. `params` carries optional provider-specific scan
@@ -13098,7 +13221,8 @@ method(object({
13098
13221
  targetId: number()
13099
13222
  }), MigrateDeviceResultSchema, {
13100
13223
  kind: "mutation",
13101
- auth: "admin"
13224
+ auth: "admin",
13225
+ timeoutMs: 12 * 6e4
13102
13226
  }), method(DeviceRegisterPayloadSchema, _void(), { kind: "mutation" }), method(DeviceRemovePayloadSchema, _void(), { kind: "mutation" }), method(DevicePersistConfigPayloadSchema, _void(), { kind: "mutation" }), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), DeviceMetaSchema.nullable()), method(object({
13103
13227
  deviceId: number(),
13104
13228
  name: string()
@@ -32462,6 +32586,147 @@ var BaseDevice$1 = class {
32462
32586
  }
32463
32587
  };
32464
32588
  /**
32589
+ * Delays before retry rounds 1..N — the round count IS the bound.
32590
+ * 10 s catches "the hub was busy for a moment"; the full schedule
32591
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
32592
+ * per attempt) covers a device-manager lock held for minutes — the
32593
+ * 2026-09-04 outage's migration hold was ~3.5 min.
32594
+ */
32595
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
32596
+ 1e4,
32597
+ 3e4,
32598
+ 9e4
32599
+ ];
32600
+ /** Abortable sleep — resolves early (never rejects) on abort. */
32601
+ function sleep$1(ms, signal) {
32602
+ return new Promise((resolve) => {
32603
+ if (signal.aborted) {
32604
+ resolve();
32605
+ return;
32606
+ }
32607
+ const onAbort = () => {
32608
+ clearTimeout(timer);
32609
+ resolve();
32610
+ };
32611
+ const timer = setTimeout(() => {
32612
+ signal.removeEventListener("abort", onAbort);
32613
+ resolve();
32614
+ }, ms);
32615
+ timer.unref?.();
32616
+ signal.addEventListener("abort", onAbort, { once: true });
32617
+ });
32618
+ }
32619
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
32620
+ * not reject (callers wrap their own try/catch). */
32621
+ async function runWithConcurrency(items, width, fn) {
32622
+ const queue = [...items];
32623
+ const laneCount = Math.max(1, Math.min(width, queue.length));
32624
+ const lane = async () => {
32625
+ for (;;) {
32626
+ const item = queue.shift();
32627
+ if (item === void 0) return;
32628
+ await fn(item);
32629
+ }
32630
+ };
32631
+ await Promise.all(Array.from({ length: laneCount }, lane));
32632
+ }
32633
+ var DeviceRestoreRetryScheduler = class {
32634
+ #logger;
32635
+ #attempt;
32636
+ #onPermanentFailure;
32637
+ #delaysMs;
32638
+ #concurrency;
32639
+ #now;
32640
+ #abort = new AbortController();
32641
+ constructor(options) {
32642
+ this.#logger = options.logger;
32643
+ this.#attempt = options.attempt;
32644
+ this.#onPermanentFailure = options.onPermanentFailure;
32645
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
32646
+ this.#concurrency = options.concurrency ?? 4;
32647
+ this.#now = options.now ?? Date.now;
32648
+ }
32649
+ /** Stop retrying (shutdown). Pending entries are NOT marked
32650
+ * permanently failed — the next boot restores them from disk. */
32651
+ cancel() {
32652
+ this.#abort.abort();
32653
+ }
32654
+ /**
32655
+ * Run the bounded retry rounds. Resolves when every entry has either
32656
+ * restored, been marked permanently failed, or the scheduler was
32657
+ * cancelled. Never rejects.
32658
+ */
32659
+ async run(initialFailures) {
32660
+ let pending = initialFailures.map((failure) => ({
32661
+ saved: failure.saved,
32662
+ lastError: failure.error,
32663
+ attempts: 1
32664
+ }));
32665
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
32666
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
32667
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
32668
+ if (this.#abort.signal.aborted) break;
32669
+ pending = await this.#runRound(pending, round);
32670
+ }
32671
+ if (this.#abort.signal.aborted) return [];
32672
+ const terminal = pending.map((entry) => ({
32673
+ deviceId: entry.saved.id,
32674
+ stableId: entry.saved.stableId,
32675
+ type: String(entry.saved.type),
32676
+ attempts: entry.attempts,
32677
+ lastError: entry.lastError,
32678
+ failedAt: this.#now()
32679
+ }));
32680
+ for (const failure of terminal) this.#onPermanentFailure(failure);
32681
+ return terminal;
32682
+ }
32683
+ /** One retry round: parents first (phase 0), then hub-adopted
32684
+ * children (phase 1) — a child's attempt depends on its parent
32685
+ * having landed, exactly like the initial two-pass restore. */
32686
+ async #runRound(pending, round) {
32687
+ const next = [];
32688
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
32689
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
32690
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
32691
+ if (this.#abort.signal.aborted) {
32692
+ next.push(entry);
32693
+ return;
32694
+ }
32695
+ const attemptNo = entry.attempts + 1;
32696
+ try {
32697
+ await this.#attempt(entry.saved);
32698
+ this.#logger.info("Device restored on retry", {
32699
+ tags: {
32700
+ deviceId: entry.saved.id,
32701
+ stableId: entry.saved.stableId
32702
+ },
32703
+ meta: { attempt: attemptNo }
32704
+ });
32705
+ } catch (err) {
32706
+ const lastError = err instanceof Error ? err.message : String(err);
32707
+ const remainingRetries = this.#delaysMs.length - (round + 1);
32708
+ this.#logger.warn("Device restore retry failed", {
32709
+ tags: {
32710
+ deviceId: entry.saved.id,
32711
+ stableId: entry.saved.stableId
32712
+ },
32713
+ meta: {
32714
+ attempt: attemptNo,
32715
+ remainingRetries,
32716
+ error: lastError
32717
+ }
32718
+ });
32719
+ next.push({
32720
+ saved: entry.saved,
32721
+ lastError,
32722
+ attempts: attemptNo
32723
+ });
32724
+ }
32725
+ });
32726
+ return next;
32727
+ }
32728
+ };
32729
+ /**
32465
32730
  * Convert an IDevice to the flat DeviceSummary shape expected by the
32466
32731
  * device-provider cap router. Shared across all providers.
32467
32732
  */
@@ -32510,6 +32775,7 @@ var BaseDeviceProvider = class extends BaseAddon {
32510
32775
  }];
32511
32776
  }
32512
32777
  async onShutdown() {
32778
+ this.cancelRestoreRetries();
32513
32779
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
32514
32780
  for (const device of devices) try {
32515
32781
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -32527,9 +32793,16 @@ var BaseDeviceProvider = class extends BaseAddon {
32527
32793
  async start() {}
32528
32794
  async stop() {}
32529
32795
  async getStatus() {
32796
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
32797
+ const summary = this.restoreFailureSummary();
32798
+ if (summary === null) return {
32799
+ connected: true,
32800
+ deviceCount: all.length
32801
+ };
32530
32802
  return {
32531
32803
  connected: true,
32532
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
32804
+ deviceCount: all.length,
32805
+ error: summary
32533
32806
  };
32534
32807
  }
32535
32808
  async getDevices() {
@@ -32619,8 +32892,137 @@ var BaseDeviceProvider = class extends BaseAddon {
32619
32892
  };
32620
32893
  }
32621
32894
  async restoreDevices(savedDevices) {
32622
- await this.onRestoreDevices(savedDevices);
32623
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
32895
+ const report = await this.onRestoreDevices(savedDevices);
32896
+ if (savedDevices.length === 0) return;
32897
+ if (report && report.failedCount > 0) {
32898
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
32899
+ return;
32900
+ }
32901
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
32902
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
32903
+ }
32904
+ /** Retry schedule. Overridable (tests use millisecond delays). */
32905
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
32906
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
32907
+ * never re-stampede full-width while the initial pass does (D167). */
32908
+ restoreRetryConcurrency = 4;
32909
+ _restoreRetryScheduler = null;
32910
+ _restoreRetryCompletion = null;
32911
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
32912
+ /** Settles when the background retry rounds finish (or `null` when
32913
+ * nothing failed). Exposed for tests and subclass diagnostics —
32914
+ * boot NEVER awaits this: the runner's post-init handshake goes out
32915
+ * with the devices that restored, and a late success is announced
32916
+ * through the `native-cap-change` → `updateCaps` path. */
32917
+ get restoreRetryCompletion() {
32918
+ return this._restoreRetryCompletion;
32919
+ }
32920
+ /** Devices that exhausted the retry bound this process lifetime. */
32921
+ get permanentRestoreFailures() {
32922
+ return [...this._permanentRestoreFailures.values()];
32923
+ }
32924
+ /** One-line operator-facing summary for `getStatus().error`, or
32925
+ * `null` when every device restored. */
32926
+ restoreFailureSummary() {
32927
+ if (this._permanentRestoreFailures.size === 0) return null;
32928
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
32929
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
32930
+ }
32931
+ cancelRestoreRetries() {
32932
+ this._restoreRetryScheduler?.cancel();
32933
+ this._restoreRetryScheduler = null;
32934
+ }
32935
+ recordPermanentRestoreFailure(failure) {
32936
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
32937
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
32938
+ tags: {
32939
+ deviceId: failure.deviceId,
32940
+ stableId: failure.stableId
32941
+ },
32942
+ meta: {
32943
+ type: failure.type,
32944
+ attempts: failure.attempts,
32945
+ error: failure.lastError
32946
+ }
32947
+ });
32948
+ }
32949
+ scheduleRestoreRetries(failures, attempt) {
32950
+ const scheduler = new DeviceRestoreRetryScheduler({
32951
+ logger: this.ctx.logger,
32952
+ delaysMs: this.restoreRetryDelaysMs,
32953
+ concurrency: this.restoreRetryConcurrency,
32954
+ attempt,
32955
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
32956
+ });
32957
+ this._restoreRetryScheduler = scheduler;
32958
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
32959
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
32960
+ });
32961
+ }
32962
+ /**
32963
+ * Tear down and reconstruct ONE device from its persisted rows — the
32964
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
32965
+ * and no other device this provider owns is disturbed.
32966
+ *
32967
+ * Keyed by `stableId` because the caller's whole reason to be here is that
32968
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
32969
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
32970
+ * whatever number the row carries NOW. The teardown is `decommission` —
32971
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
32972
+ * unregisters native caps, drops the registry entry) — and the rebuild is
32973
+ * the boot restore's own `create()` path, including its pass 2: first-class
32974
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
32975
+ * parent by the cascade and must be re-created explicitly, because only
32976
+ * accessory children come back through `getAccessoryChildren()`.
32977
+ *
32978
+ * Reloading an accessory child directly is refused (no device class) —
32979
+ * reload its parent instead.
32980
+ */
32981
+ async reloadDevice(input) {
32982
+ const { stableId } = input;
32983
+ const devices = this.ctx.kernel.devices;
32984
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
32985
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
32986
+ if (live) await devices.decommission(live.id);
32987
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
32988
+ addonId: this.addonId,
32989
+ stableId
32990
+ });
32991
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
32992
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
32993
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
32994
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
32995
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
32996
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
32997
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
32998
+ for (const row of rows) {
32999
+ if (row.parentDeviceId !== id) continue;
33000
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
33001
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
33002
+ if (!ChildClass) continue;
33003
+ try {
33004
+ await devices.create(row.stableId, ChildClass, {}, id);
33005
+ } catch (err) {
33006
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
33007
+ tags: {
33008
+ deviceId: row.id,
33009
+ stableId: row.stableId
33010
+ },
33011
+ meta: {
33012
+ parentDeviceId: id,
33013
+ error: err instanceof Error ? err.message : String(err)
33014
+ }
33015
+ });
33016
+ }
33017
+ }
33018
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
33019
+ tags: { deviceId: id },
33020
+ meta: {
33021
+ stableId,
33022
+ type: meta.type
33023
+ }
33024
+ });
33025
+ return { deviceId: id };
32624
33026
  }
32625
33027
  /**
32626
33028
  * Restore devices from persisted state. Two-pass:
@@ -32646,55 +33048,108 @@ var BaseDeviceProvider = class extends BaseAddon {
32646
33048
  * accessory-spawn flow handles via the parent's
32647
33049
  * `getAccessoryChildren()`. Override only when the default doesn't
32648
33050
  * fit.
33051
+ *
33052
+ * A row that fails either pass is NOT terminal (D347): it is handed
33053
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
33054
+ * Only after the bound is exhausted is the device marked permanently
33055
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
33056
+ * `getStatus().error`.
32649
33057
  */
32650
33058
  async onRestoreDevices(savedDevices) {
32651
33059
  const restored = /* @__PURE__ */ new Set();
33060
+ const failures = [];
33061
+ const attemptRestore = async (saved) => {
33062
+ if (restored.has(saved.id)) return;
33063
+ const Class = this.deviceClasses[saved.type];
33064
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
33065
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
33066
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33067
+ restored.add(saved.id);
33068
+ };
32652
33069
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
32653
33070
  const restoreOne = async (saved) => {
32654
- const Class = this.deviceClasses[saved.type];
32655
- if (!Class) {
33071
+ if (!this.deviceClasses[saved.type]) {
32656
33072
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
32657
- tags: { stableId: saved.stableId },
33073
+ tags: {
33074
+ deviceId: saved.id,
33075
+ stableId: saved.stableId
33076
+ },
32658
33077
  meta: { type: saved.type }
32659
33078
  });
32660
33079
  return;
32661
33080
  }
32662
33081
  try {
32663
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
32664
- restored.add(saved.id);
33082
+ await attemptRestore(saved);
32665
33083
  } catch (err) {
32666
- this.ctx.logger.warn("Failed to restore device", {
32667
- tags: { stableId: saved.stableId },
33084
+ const error = err instanceof Error ? err.message : String(err);
33085
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
33086
+ tags: {
33087
+ deviceId: saved.id,
33088
+ stableId: saved.stableId
33089
+ },
32668
33090
  meta: {
32669
33091
  type: saved.type,
32670
- error: err instanceof Error ? err.message : String(err)
33092
+ attempt: 1,
33093
+ error
32671
33094
  }
32672
33095
  });
33096
+ failures.push({
33097
+ saved,
33098
+ error
33099
+ });
32673
33100
  }
32674
33101
  };
32675
33102
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
33103
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
32676
33104
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
32677
33105
  for (const saved of childRows) {
32678
- const Class = this.deviceClasses[saved.type];
32679
- if (!Class) continue;
33106
+ if (!this.deviceClasses[saved.type]) continue;
32680
33107
  if (saved.parentDeviceId === null) continue;
32681
- if (!restored.has(saved.parentDeviceId)) continue;
32682
- try {
32683
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
32684
- restored.add(saved.id);
32685
- } catch (err) {
32686
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
33108
+ if (restored.has(saved.parentDeviceId)) {
33109
+ try {
33110
+ await attemptRestore(saved);
33111
+ } catch (err) {
33112
+ const error = err instanceof Error ? err.message : String(err);
33113
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
33114
+ tags: {
33115
+ deviceId: saved.id,
33116
+ stableId: saved.stableId,
33117
+ parentDeviceId: saved.parentDeviceId
33118
+ },
33119
+ meta: {
33120
+ type: saved.type,
33121
+ attempt: 1,
33122
+ error
33123
+ }
33124
+ });
33125
+ failures.push({
33126
+ saved,
33127
+ error
33128
+ });
33129
+ }
33130
+ continue;
33131
+ }
33132
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
33133
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
32687
33134
  tags: {
33135
+ deviceId: saved.id,
32688
33136
  stableId: saved.stableId,
32689
33137
  parentDeviceId: saved.parentDeviceId
32690
33138
  },
32691
- meta: {
32692
- type: saved.type,
32693
- error: err instanceof Error ? err.message : String(err)
32694
- }
33139
+ meta: { type: saved.type }
33140
+ });
33141
+ failures.push({
33142
+ saved,
33143
+ error: `parent device ${saved.parentDeviceId} not restored`
32695
33144
  });
33145
+ continue;
32696
33146
  }
32697
33147
  }
33148
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
33149
+ return {
33150
+ restoredCount: restored.size,
33151
+ failedCount: failures.length
33152
+ };
32698
33153
  }
32699
33154
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
32700
33155
  toSummary(device) {
@@ -33203,6 +33658,12 @@ Object.freeze({
33203
33658
  addonId: null,
33204
33659
  access: "create"
33205
33660
  },
33661
+ "backup.cancel": {
33662
+ capName: "backup",
33663
+ capScope: "system",
33664
+ addonId: null,
33665
+ access: "create"
33666
+ },
33206
33667
  "backup.delete": {
33207
33668
  capName: "backup",
33208
33669
  capScope: "system",
@@ -33245,6 +33706,12 @@ Object.freeze({
33245
33706
  addonId: null,
33246
33707
  access: "view"
33247
33708
  },
33709
+ "backup.listRuns": {
33710
+ capName: "backup",
33711
+ capScope: "system",
33712
+ addonId: null,
33713
+ access: "view"
33714
+ },
33248
33715
  "backup.listSchedules": {
33249
33716
  capName: "backup",
33250
33717
  capScope: "system",
@@ -34445,6 +34912,12 @@ Object.freeze({
34445
34912
  addonId: null,
34446
34913
  access: "view"
34447
34914
  },
34915
+ "deviceProvider.reloadDevice": {
34916
+ capName: "device-provider",
34917
+ capScope: "system",
34918
+ addonId: null,
34919
+ access: "create"
34920
+ },
34448
34921
  "deviceProvider.start": {
34449
34922
  capName: "device-provider",
34450
34923
  capScope: "system",
@@ -37811,6 +38284,12 @@ Object.freeze({
37811
38284
  addonId: null,
37812
38285
  access: "create"
37813
38286
  },
38287
+ "streamBroker.forgetDeviceHardware": {
38288
+ capName: "stream-broker",
38289
+ capScope: "system",
38290
+ addonId: null,
38291
+ access: "delete"
38292
+ },
37814
38293
  "streamBroker.getAllRtspEntries": {
37815
38294
  capName: "stream-broker",
37816
38295
  capScope: "system",
@@ -40269,6 +40748,11 @@ Object.freeze({
40269
40748
  form: "single",
40270
40749
  optional: false
40271
40750
  }],
40751
+ "streamBroker.forgetDeviceHardware": [{
40752
+ name: "deviceId",
40753
+ form: "single",
40754
+ optional: false
40755
+ }],
40272
40756
  "streamBroker.getDeviceAudioMute": [{
40273
40757
  name: "deviceId",
40274
40758
  form: "single",
package/dist/addon.mjs CHANGED
@@ -10564,6 +10564,89 @@ var LocationStatSchema = object({
10564
10564
  fileCount: number(),
10565
10565
  present: boolean()
10566
10566
  });
10567
+ /** Lifecycle of a backup run. Terminal states: succeeded / failed / cancelled. */
10568
+ var BackupRunStateSchema = _enum([
10569
+ "queued",
10570
+ "running",
10571
+ "succeeded",
10572
+ "failed",
10573
+ "cancelled"
10574
+ ]);
10575
+ /**
10576
+ * Where a running backup currently is. `queued` before it starts,
10577
+ * `building` while the tar.gz is being staged, `uploading` during the
10578
+ * per-destination fan-out, `done` once terminal.
10579
+ */
10580
+ var BackupRunPhaseSchema = _enum([
10581
+ "queued",
10582
+ "building",
10583
+ "uploading",
10584
+ "done"
10585
+ ]);
10586
+ /**
10587
+ * Observable state of one backup run — readable WHILE it runs via
10588
+ * `backup.listRuns`. This is what makes the execution queue and
10589
+ * `backup.cancel` usable: the 2026-09-04 incident (two concurrent
10590
+ * multi-GB builds, staging 5.1 GB → 18 GB, load 62) was only
10591
+ * diagnosable with `du` because nothing reported that runs existed or
10592
+ * how large the staged archive had grown.
10593
+ */
10594
+ var BackupRunSchema = object({
10595
+ /** Stable run id — the handle `backup.cancel` takes. */
10596
+ id: string(),
10597
+ state: BackupRunStateSchema,
10598
+ phase: BackupRunPhaseSchema,
10599
+ /**
10600
+ * Resolved destination location ids. Empty while queued (targets are
10601
+ * resolved when the run starts, against the then-current policies).
10602
+ */
10603
+ destinationIds: array(string()).readonly(),
10604
+ label: string().optional(),
10605
+ /** ms-epoch when the run was submitted (trigger call / schedule fire). */
10606
+ requestedAt: number(),
10607
+ /** ms-epoch when the run left the queue and started building. */
10608
+ startedAt: number().optional(),
10609
+ /** ms-epoch when the run reached a terminal state. */
10610
+ finishedAt: number().optional(),
10611
+ /** Compressed bytes of the staging archive written so far. */
10612
+ stagedBytes: number(),
10613
+ /** Final staged archive size, once the build phase completes. */
10614
+ archiveSizeBytes: number().optional(),
10615
+ /** Bytes pushed to the destination currently uploading. */
10616
+ uploadedBytes: number(),
10617
+ /** Destinations where the archive fully landed (uploaded + indexed). */
10618
+ completedDestinationIds: array(string()).readonly(),
10619
+ /** Destinations that failed during the fan-out. */
10620
+ failedDestinationIds: array(string()).readonly(),
10621
+ /** Failure message when `state === 'failed'`. */
10622
+ error: string().optional(),
10623
+ /**
10624
+ * 1-based place in the execution queue — 1 = runs next. Present only
10625
+ * while `state === 'queued'`. Stamped by the orchestrator from the
10626
+ * queue's OWN pending order, never derived from timestamps, so the
10627
+ * UI cannot show an order the executor will not honour.
10628
+ */
10629
+ queuePosition: number().int().min(1).optional()
10630
+ });
10631
+ /**
10632
+ * Result of `backup.trigger`. The call still resolves when the run
10633
+ * terminates (compat with schedule-driven runs and the admin UI), but
10634
+ * it now names the run and says whether it had to WAIT: a trigger that
10635
+ * arrives while another run is in flight is enqueued (or joined onto
10636
+ * an identical already-queued run), never started concurrently.
10637
+ */
10638
+ var BackupTriggerResultSchema = object({
10639
+ /** The run this trigger mapped to — poll it via `listRuns`, stop it via `cancel`. */
10640
+ runId: string(),
10641
+ /** True when the run waited behind an in-flight run instead of starting immediately. */
10642
+ queued: boolean(),
10643
+ /** True when this trigger was coalesced onto an identical already-queued run. */
10644
+ joined: boolean(),
10645
+ /** True when the run was cancelled before completing every destination. */
10646
+ cancelled: boolean(),
10647
+ /** One entry per destination the archive landed at (partial on cancel). */
10648
+ entries: array(BackupEntrySchema).readonly()
10649
+ });
10567
10650
  /**
10568
10651
  * A backup schedule — the N:M "entry" that binds one cron cadence to a
10569
10652
  * SET of destination locations. Supersedes the per-location cron on
@@ -10611,7 +10694,10 @@ method(_void(), array(BackupDestinationInfoSchema).readonly(), { auth: "admin" }
10611
10694
  * retention (manual runs).
10612
10695
  */
10613
10696
  retentionCount: number().int().min(1).max(1e3).optional()
10614
- }).optional(), array(BackupEntrySchema).readonly(), {
10697
+ }).optional(), BackupTriggerResultSchema, {
10698
+ kind: "mutation",
10699
+ auth: "admin"
10700
+ }), method(_void(), array(BackupRunSchema).readonly(), { auth: "admin" }), method(object({ runId: string() }), object({ cancelled: boolean() }), {
10615
10701
  kind: "mutation",
10616
10702
  auth: "admin"
10617
10703
  }), method(_void(), array(BackupEntrySchema).readonly(), { auth: "admin" }), method(_void(), array(LocationStatSchema).readonly(), { auth: "admin" }), method(object({
@@ -11328,6 +11414,14 @@ method(object({
11328
11414
  }), object({ success: literal(true) }), {
11329
11415
  kind: "mutation",
11330
11416
  auth: "admin"
11417
+ }), method(object({ deviceId: number().int().nonnegative() }), object({
11418
+ derivedStreamsDeleted: array(string()).readonly(),
11419
+ assignmentsPurged: boolean(),
11420
+ probeSnapshotsDropped: number().int().nonnegative(),
11421
+ rtspTokenRowsDeleted: number().int().nonnegative()
11422
+ }), {
11423
+ kind: "mutation",
11424
+ auth: "admin"
11331
11425
  }), method(object({
11332
11426
  deviceId: number(),
11333
11427
  /** Absent = the LOWEST assigned profile — a notification attachment is
@@ -12772,6 +12866,35 @@ var deviceProviderCapability = {
12772
12866
  name: string(),
12773
12867
  type: string()
12774
12868
  }))),
12869
+ /**
12870
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12871
+ * touching no other device this provider owns.
12872
+ *
12873
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12874
+ * migrated numbers: after `swapIds` the runner's live instance still
12875
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12876
+ * registrations and its log tags), and a live object cannot be renumbered.
12877
+ * Before this method the only flush was restarting the whole owning addon
12878
+ * — which took every camera the provider owns down with it (28 devices
12879
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12880
+ * same day ~27 devices' native caps did not come back on their own).
12881
+ *
12882
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12883
+ * that changes. The reply carries the id the device answers on NOW.
12884
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12885
+ * instance (if any), then re-create from the persisted row: the same
12886
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12887
+ * An RPC, never an event: a dropped event would leave the runner writing
12888
+ * against the wrong camera (D8).
12889
+ *
12890
+ * Construction can dial hardware, and the migrated source is
12891
+ * characteristically dead — the timeout covers a full activate window
12892
+ * rather than the 60 s default.
12893
+ */
12894
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12895
+ kind: "mutation",
12896
+ timeoutMs: 3 * 6e4
12897
+ }),
12775
12898
  supportsDiscovery: method(object({}), boolean()),
12776
12899
  /**
12777
12900
  * Run a network scan. `params` carries optional provider-specific scan
@@ -13099,7 +13222,8 @@ method(object({
13099
13222
  targetId: number()
13100
13223
  }), MigrateDeviceResultSchema, {
13101
13224
  kind: "mutation",
13102
- auth: "admin"
13225
+ auth: "admin",
13226
+ timeoutMs: 12 * 6e4
13103
13227
  }), method(DeviceRegisterPayloadSchema, _void(), { kind: "mutation" }), method(DeviceRemovePayloadSchema, _void(), { kind: "mutation" }), method(DevicePersistConfigPayloadSchema, _void(), { kind: "mutation" }), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), DeviceMetaSchema.nullable()), method(object({
13104
13228
  deviceId: number(),
13105
13229
  name: string()
@@ -32463,6 +32587,147 @@ var BaseDevice$1 = class {
32463
32587
  }
32464
32588
  };
32465
32589
  /**
32590
+ * Delays before retry rounds 1..N — the round count IS the bound.
32591
+ * 10 s catches "the hub was busy for a moment"; the full schedule
32592
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
32593
+ * per attempt) covers a device-manager lock held for minutes — the
32594
+ * 2026-09-04 outage's migration hold was ~3.5 min.
32595
+ */
32596
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
32597
+ 1e4,
32598
+ 3e4,
32599
+ 9e4
32600
+ ];
32601
+ /** Abortable sleep — resolves early (never rejects) on abort. */
32602
+ function sleep$1(ms, signal) {
32603
+ return new Promise((resolve) => {
32604
+ if (signal.aborted) {
32605
+ resolve();
32606
+ return;
32607
+ }
32608
+ const onAbort = () => {
32609
+ clearTimeout(timer);
32610
+ resolve();
32611
+ };
32612
+ const timer = setTimeout(() => {
32613
+ signal.removeEventListener("abort", onAbort);
32614
+ resolve();
32615
+ }, ms);
32616
+ timer.unref?.();
32617
+ signal.addEventListener("abort", onAbort, { once: true });
32618
+ });
32619
+ }
32620
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
32621
+ * not reject (callers wrap their own try/catch). */
32622
+ async function runWithConcurrency(items, width, fn) {
32623
+ const queue = [...items];
32624
+ const laneCount = Math.max(1, Math.min(width, queue.length));
32625
+ const lane = async () => {
32626
+ for (;;) {
32627
+ const item = queue.shift();
32628
+ if (item === void 0) return;
32629
+ await fn(item);
32630
+ }
32631
+ };
32632
+ await Promise.all(Array.from({ length: laneCount }, lane));
32633
+ }
32634
+ var DeviceRestoreRetryScheduler = class {
32635
+ #logger;
32636
+ #attempt;
32637
+ #onPermanentFailure;
32638
+ #delaysMs;
32639
+ #concurrency;
32640
+ #now;
32641
+ #abort = new AbortController();
32642
+ constructor(options) {
32643
+ this.#logger = options.logger;
32644
+ this.#attempt = options.attempt;
32645
+ this.#onPermanentFailure = options.onPermanentFailure;
32646
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
32647
+ this.#concurrency = options.concurrency ?? 4;
32648
+ this.#now = options.now ?? Date.now;
32649
+ }
32650
+ /** Stop retrying (shutdown). Pending entries are NOT marked
32651
+ * permanently failed — the next boot restores them from disk. */
32652
+ cancel() {
32653
+ this.#abort.abort();
32654
+ }
32655
+ /**
32656
+ * Run the bounded retry rounds. Resolves when every entry has either
32657
+ * restored, been marked permanently failed, or the scheduler was
32658
+ * cancelled. Never rejects.
32659
+ */
32660
+ async run(initialFailures) {
32661
+ let pending = initialFailures.map((failure) => ({
32662
+ saved: failure.saved,
32663
+ lastError: failure.error,
32664
+ attempts: 1
32665
+ }));
32666
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
32667
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
32668
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
32669
+ if (this.#abort.signal.aborted) break;
32670
+ pending = await this.#runRound(pending, round);
32671
+ }
32672
+ if (this.#abort.signal.aborted) return [];
32673
+ const terminal = pending.map((entry) => ({
32674
+ deviceId: entry.saved.id,
32675
+ stableId: entry.saved.stableId,
32676
+ type: String(entry.saved.type),
32677
+ attempts: entry.attempts,
32678
+ lastError: entry.lastError,
32679
+ failedAt: this.#now()
32680
+ }));
32681
+ for (const failure of terminal) this.#onPermanentFailure(failure);
32682
+ return terminal;
32683
+ }
32684
+ /** One retry round: parents first (phase 0), then hub-adopted
32685
+ * children (phase 1) — a child's attempt depends on its parent
32686
+ * having landed, exactly like the initial two-pass restore. */
32687
+ async #runRound(pending, round) {
32688
+ const next = [];
32689
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
32690
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
32691
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
32692
+ if (this.#abort.signal.aborted) {
32693
+ next.push(entry);
32694
+ return;
32695
+ }
32696
+ const attemptNo = entry.attempts + 1;
32697
+ try {
32698
+ await this.#attempt(entry.saved);
32699
+ this.#logger.info("Device restored on retry", {
32700
+ tags: {
32701
+ deviceId: entry.saved.id,
32702
+ stableId: entry.saved.stableId
32703
+ },
32704
+ meta: { attempt: attemptNo }
32705
+ });
32706
+ } catch (err) {
32707
+ const lastError = err instanceof Error ? err.message : String(err);
32708
+ const remainingRetries = this.#delaysMs.length - (round + 1);
32709
+ this.#logger.warn("Device restore retry failed", {
32710
+ tags: {
32711
+ deviceId: entry.saved.id,
32712
+ stableId: entry.saved.stableId
32713
+ },
32714
+ meta: {
32715
+ attempt: attemptNo,
32716
+ remainingRetries,
32717
+ error: lastError
32718
+ }
32719
+ });
32720
+ next.push({
32721
+ saved: entry.saved,
32722
+ lastError,
32723
+ attempts: attemptNo
32724
+ });
32725
+ }
32726
+ });
32727
+ return next;
32728
+ }
32729
+ };
32730
+ /**
32466
32731
  * Convert an IDevice to the flat DeviceSummary shape expected by the
32467
32732
  * device-provider cap router. Shared across all providers.
32468
32733
  */
@@ -32511,6 +32776,7 @@ var BaseDeviceProvider = class extends BaseAddon {
32511
32776
  }];
32512
32777
  }
32513
32778
  async onShutdown() {
32779
+ this.cancelRestoreRetries();
32514
32780
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
32515
32781
  for (const device of devices) try {
32516
32782
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -32528,9 +32794,16 @@ var BaseDeviceProvider = class extends BaseAddon {
32528
32794
  async start() {}
32529
32795
  async stop() {}
32530
32796
  async getStatus() {
32797
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
32798
+ const summary = this.restoreFailureSummary();
32799
+ if (summary === null) return {
32800
+ connected: true,
32801
+ deviceCount: all.length
32802
+ };
32531
32803
  return {
32532
32804
  connected: true,
32533
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
32805
+ deviceCount: all.length,
32806
+ error: summary
32534
32807
  };
32535
32808
  }
32536
32809
  async getDevices() {
@@ -32620,8 +32893,137 @@ var BaseDeviceProvider = class extends BaseAddon {
32620
32893
  };
32621
32894
  }
32622
32895
  async restoreDevices(savedDevices) {
32623
- await this.onRestoreDevices(savedDevices);
32624
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
32896
+ const report = await this.onRestoreDevices(savedDevices);
32897
+ if (savedDevices.length === 0) return;
32898
+ if (report && report.failedCount > 0) {
32899
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
32900
+ return;
32901
+ }
32902
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
32903
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
32904
+ }
32905
+ /** Retry schedule. Overridable (tests use millisecond delays). */
32906
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
32907
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
32908
+ * never re-stampede full-width while the initial pass does (D167). */
32909
+ restoreRetryConcurrency = 4;
32910
+ _restoreRetryScheduler = null;
32911
+ _restoreRetryCompletion = null;
32912
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
32913
+ /** Settles when the background retry rounds finish (or `null` when
32914
+ * nothing failed). Exposed for tests and subclass diagnostics —
32915
+ * boot NEVER awaits this: the runner's post-init handshake goes out
32916
+ * with the devices that restored, and a late success is announced
32917
+ * through the `native-cap-change` → `updateCaps` path. */
32918
+ get restoreRetryCompletion() {
32919
+ return this._restoreRetryCompletion;
32920
+ }
32921
+ /** Devices that exhausted the retry bound this process lifetime. */
32922
+ get permanentRestoreFailures() {
32923
+ return [...this._permanentRestoreFailures.values()];
32924
+ }
32925
+ /** One-line operator-facing summary for `getStatus().error`, or
32926
+ * `null` when every device restored. */
32927
+ restoreFailureSummary() {
32928
+ if (this._permanentRestoreFailures.size === 0) return null;
32929
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
32930
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
32931
+ }
32932
+ cancelRestoreRetries() {
32933
+ this._restoreRetryScheduler?.cancel();
32934
+ this._restoreRetryScheduler = null;
32935
+ }
32936
+ recordPermanentRestoreFailure(failure) {
32937
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
32938
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
32939
+ tags: {
32940
+ deviceId: failure.deviceId,
32941
+ stableId: failure.stableId
32942
+ },
32943
+ meta: {
32944
+ type: failure.type,
32945
+ attempts: failure.attempts,
32946
+ error: failure.lastError
32947
+ }
32948
+ });
32949
+ }
32950
+ scheduleRestoreRetries(failures, attempt) {
32951
+ const scheduler = new DeviceRestoreRetryScheduler({
32952
+ logger: this.ctx.logger,
32953
+ delaysMs: this.restoreRetryDelaysMs,
32954
+ concurrency: this.restoreRetryConcurrency,
32955
+ attempt,
32956
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
32957
+ });
32958
+ this._restoreRetryScheduler = scheduler;
32959
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
32960
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
32961
+ });
32962
+ }
32963
+ /**
32964
+ * Tear down and reconstruct ONE device from its persisted rows — the
32965
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
32966
+ * and no other device this provider owns is disturbed.
32967
+ *
32968
+ * Keyed by `stableId` because the caller's whole reason to be here is that
32969
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
32970
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
32971
+ * whatever number the row carries NOW. The teardown is `decommission` —
32972
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
32973
+ * unregisters native caps, drops the registry entry) — and the rebuild is
32974
+ * the boot restore's own `create()` path, including its pass 2: first-class
32975
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
32976
+ * parent by the cascade and must be re-created explicitly, because only
32977
+ * accessory children come back through `getAccessoryChildren()`.
32978
+ *
32979
+ * Reloading an accessory child directly is refused (no device class) —
32980
+ * reload its parent instead.
32981
+ */
32982
+ async reloadDevice(input) {
32983
+ const { stableId } = input;
32984
+ const devices = this.ctx.kernel.devices;
32985
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
32986
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
32987
+ if (live) await devices.decommission(live.id);
32988
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
32989
+ addonId: this.addonId,
32990
+ stableId
32991
+ });
32992
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
32993
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
32994
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
32995
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
32996
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
32997
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
32998
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
32999
+ for (const row of rows) {
33000
+ if (row.parentDeviceId !== id) continue;
33001
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
33002
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
33003
+ if (!ChildClass) continue;
33004
+ try {
33005
+ await devices.create(row.stableId, ChildClass, {}, id);
33006
+ } catch (err) {
33007
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
33008
+ tags: {
33009
+ deviceId: row.id,
33010
+ stableId: row.stableId
33011
+ },
33012
+ meta: {
33013
+ parentDeviceId: id,
33014
+ error: err instanceof Error ? err.message : String(err)
33015
+ }
33016
+ });
33017
+ }
33018
+ }
33019
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
33020
+ tags: { deviceId: id },
33021
+ meta: {
33022
+ stableId,
33023
+ type: meta.type
33024
+ }
33025
+ });
33026
+ return { deviceId: id };
32625
33027
  }
32626
33028
  /**
32627
33029
  * Restore devices from persisted state. Two-pass:
@@ -32647,55 +33049,108 @@ var BaseDeviceProvider = class extends BaseAddon {
32647
33049
  * accessory-spawn flow handles via the parent's
32648
33050
  * `getAccessoryChildren()`. Override only when the default doesn't
32649
33051
  * fit.
33052
+ *
33053
+ * A row that fails either pass is NOT terminal (D347): it is handed
33054
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
33055
+ * Only after the bound is exhausted is the device marked permanently
33056
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
33057
+ * `getStatus().error`.
32650
33058
  */
32651
33059
  async onRestoreDevices(savedDevices) {
32652
33060
  const restored = /* @__PURE__ */ new Set();
33061
+ const failures = [];
33062
+ const attemptRestore = async (saved) => {
33063
+ if (restored.has(saved.id)) return;
33064
+ const Class = this.deviceClasses[saved.type];
33065
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
33066
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
33067
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33068
+ restored.add(saved.id);
33069
+ };
32653
33070
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
32654
33071
  const restoreOne = async (saved) => {
32655
- const Class = this.deviceClasses[saved.type];
32656
- if (!Class) {
33072
+ if (!this.deviceClasses[saved.type]) {
32657
33073
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
32658
- tags: { stableId: saved.stableId },
33074
+ tags: {
33075
+ deviceId: saved.id,
33076
+ stableId: saved.stableId
33077
+ },
32659
33078
  meta: { type: saved.type }
32660
33079
  });
32661
33080
  return;
32662
33081
  }
32663
33082
  try {
32664
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
32665
- restored.add(saved.id);
33083
+ await attemptRestore(saved);
32666
33084
  } catch (err) {
32667
- this.ctx.logger.warn("Failed to restore device", {
32668
- tags: { stableId: saved.stableId },
33085
+ const error = err instanceof Error ? err.message : String(err);
33086
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
33087
+ tags: {
33088
+ deviceId: saved.id,
33089
+ stableId: saved.stableId
33090
+ },
32669
33091
  meta: {
32670
33092
  type: saved.type,
32671
- error: err instanceof Error ? err.message : String(err)
33093
+ attempt: 1,
33094
+ error
32672
33095
  }
32673
33096
  });
33097
+ failures.push({
33098
+ saved,
33099
+ error
33100
+ });
32674
33101
  }
32675
33102
  };
32676
33103
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
33104
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
32677
33105
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
32678
33106
  for (const saved of childRows) {
32679
- const Class = this.deviceClasses[saved.type];
32680
- if (!Class) continue;
33107
+ if (!this.deviceClasses[saved.type]) continue;
32681
33108
  if (saved.parentDeviceId === null) continue;
32682
- if (!restored.has(saved.parentDeviceId)) continue;
32683
- try {
32684
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
32685
- restored.add(saved.id);
32686
- } catch (err) {
32687
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
33109
+ if (restored.has(saved.parentDeviceId)) {
33110
+ try {
33111
+ await attemptRestore(saved);
33112
+ } catch (err) {
33113
+ const error = err instanceof Error ? err.message : String(err);
33114
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
33115
+ tags: {
33116
+ deviceId: saved.id,
33117
+ stableId: saved.stableId,
33118
+ parentDeviceId: saved.parentDeviceId
33119
+ },
33120
+ meta: {
33121
+ type: saved.type,
33122
+ attempt: 1,
33123
+ error
33124
+ }
33125
+ });
33126
+ failures.push({
33127
+ saved,
33128
+ error
33129
+ });
33130
+ }
33131
+ continue;
33132
+ }
33133
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
33134
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
32688
33135
  tags: {
33136
+ deviceId: saved.id,
32689
33137
  stableId: saved.stableId,
32690
33138
  parentDeviceId: saved.parentDeviceId
32691
33139
  },
32692
- meta: {
32693
- type: saved.type,
32694
- error: err instanceof Error ? err.message : String(err)
32695
- }
33140
+ meta: { type: saved.type }
33141
+ });
33142
+ failures.push({
33143
+ saved,
33144
+ error: `parent device ${saved.parentDeviceId} not restored`
32696
33145
  });
33146
+ continue;
32697
33147
  }
32698
33148
  }
33149
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
33150
+ return {
33151
+ restoredCount: restored.size,
33152
+ failedCount: failures.length
33153
+ };
32699
33154
  }
32700
33155
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
32701
33156
  toSummary(device) {
@@ -33204,6 +33659,12 @@ Object.freeze({
33204
33659
  addonId: null,
33205
33660
  access: "create"
33206
33661
  },
33662
+ "backup.cancel": {
33663
+ capName: "backup",
33664
+ capScope: "system",
33665
+ addonId: null,
33666
+ access: "create"
33667
+ },
33207
33668
  "backup.delete": {
33208
33669
  capName: "backup",
33209
33670
  capScope: "system",
@@ -33246,6 +33707,12 @@ Object.freeze({
33246
33707
  addonId: null,
33247
33708
  access: "view"
33248
33709
  },
33710
+ "backup.listRuns": {
33711
+ capName: "backup",
33712
+ capScope: "system",
33713
+ addonId: null,
33714
+ access: "view"
33715
+ },
33249
33716
  "backup.listSchedules": {
33250
33717
  capName: "backup",
33251
33718
  capScope: "system",
@@ -34446,6 +34913,12 @@ Object.freeze({
34446
34913
  addonId: null,
34447
34914
  access: "view"
34448
34915
  },
34916
+ "deviceProvider.reloadDevice": {
34917
+ capName: "device-provider",
34918
+ capScope: "system",
34919
+ addonId: null,
34920
+ access: "create"
34921
+ },
34449
34922
  "deviceProvider.start": {
34450
34923
  capName: "device-provider",
34451
34924
  capScope: "system",
@@ -37812,6 +38285,12 @@ Object.freeze({
37812
38285
  addonId: null,
37813
38286
  access: "create"
37814
38287
  },
38288
+ "streamBroker.forgetDeviceHardware": {
38289
+ capName: "stream-broker",
38290
+ capScope: "system",
38291
+ addonId: null,
38292
+ access: "delete"
38293
+ },
37815
38294
  "streamBroker.getAllRtspEntries": {
37816
38295
  capName: "stream-broker",
37817
38296
  capScope: "system",
@@ -40270,6 +40749,11 @@ Object.freeze({
40270
40749
  form: "single",
40271
40750
  optional: false
40272
40751
  }],
40752
+ "streamBroker.forgetDeviceHardware": [{
40753
+ name: "deviceId",
40754
+ form: "single",
40755
+ optional: false
40756
+ }],
40273
40757
  "streamBroker.getDeviceAudioMute": [{
40274
40758
  name: "deviceId",
40275
40759
  form: "single",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-provider-dreo",
3
- "version": "0.2.58",
3
+ "version": "0.2.60",
4
4
  "description": "Dreo smart-device (fan / air-circulator / purifier / heater / humidifier) device-provider addon for CamStack — wraps the @apocaliss92/nodedreo Dreo cloud client (REST + WebSocket)",
5
5
  "keywords": [
6
6
  "camstack",