@camstack/addon-provider-velux 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
@@ -10527,6 +10527,89 @@ var LocationStatSchema = object({
10527
10527
  fileCount: number(),
10528
10528
  present: boolean()
10529
10529
  });
10530
+ /** Lifecycle of a backup run. Terminal states: succeeded / failed / cancelled. */
10531
+ var BackupRunStateSchema = _enum([
10532
+ "queued",
10533
+ "running",
10534
+ "succeeded",
10535
+ "failed",
10536
+ "cancelled"
10537
+ ]);
10538
+ /**
10539
+ * Where a running backup currently is. `queued` before it starts,
10540
+ * `building` while the tar.gz is being staged, `uploading` during the
10541
+ * per-destination fan-out, `done` once terminal.
10542
+ */
10543
+ var BackupRunPhaseSchema = _enum([
10544
+ "queued",
10545
+ "building",
10546
+ "uploading",
10547
+ "done"
10548
+ ]);
10549
+ /**
10550
+ * Observable state of one backup run — readable WHILE it runs via
10551
+ * `backup.listRuns`. This is what makes the execution queue and
10552
+ * `backup.cancel` usable: the 2026-09-04 incident (two concurrent
10553
+ * multi-GB builds, staging 5.1 GB → 18 GB, load 62) was only
10554
+ * diagnosable with `du` because nothing reported that runs existed or
10555
+ * how large the staged archive had grown.
10556
+ */
10557
+ var BackupRunSchema = object({
10558
+ /** Stable run id — the handle `backup.cancel` takes. */
10559
+ id: string(),
10560
+ state: BackupRunStateSchema,
10561
+ phase: BackupRunPhaseSchema,
10562
+ /**
10563
+ * Resolved destination location ids. Empty while queued (targets are
10564
+ * resolved when the run starts, against the then-current policies).
10565
+ */
10566
+ destinationIds: array(string()).readonly(),
10567
+ label: string().optional(),
10568
+ /** ms-epoch when the run was submitted (trigger call / schedule fire). */
10569
+ requestedAt: number(),
10570
+ /** ms-epoch when the run left the queue and started building. */
10571
+ startedAt: number().optional(),
10572
+ /** ms-epoch when the run reached a terminal state. */
10573
+ finishedAt: number().optional(),
10574
+ /** Compressed bytes of the staging archive written so far. */
10575
+ stagedBytes: number(),
10576
+ /** Final staged archive size, once the build phase completes. */
10577
+ archiveSizeBytes: number().optional(),
10578
+ /** Bytes pushed to the destination currently uploading. */
10579
+ uploadedBytes: number(),
10580
+ /** Destinations where the archive fully landed (uploaded + indexed). */
10581
+ completedDestinationIds: array(string()).readonly(),
10582
+ /** Destinations that failed during the fan-out. */
10583
+ failedDestinationIds: array(string()).readonly(),
10584
+ /** Failure message when `state === 'failed'`. */
10585
+ error: string().optional(),
10586
+ /**
10587
+ * 1-based place in the execution queue — 1 = runs next. Present only
10588
+ * while `state === 'queued'`. Stamped by the orchestrator from the
10589
+ * queue's OWN pending order, never derived from timestamps, so the
10590
+ * UI cannot show an order the executor will not honour.
10591
+ */
10592
+ queuePosition: number().int().min(1).optional()
10593
+ });
10594
+ /**
10595
+ * Result of `backup.trigger`. The call still resolves when the run
10596
+ * terminates (compat with schedule-driven runs and the admin UI), but
10597
+ * it now names the run and says whether it had to WAIT: a trigger that
10598
+ * arrives while another run is in flight is enqueued (or joined onto
10599
+ * an identical already-queued run), never started concurrently.
10600
+ */
10601
+ var BackupTriggerResultSchema = object({
10602
+ /** The run this trigger mapped to — poll it via `listRuns`, stop it via `cancel`. */
10603
+ runId: string(),
10604
+ /** True when the run waited behind an in-flight run instead of starting immediately. */
10605
+ queued: boolean(),
10606
+ /** True when this trigger was coalesced onto an identical already-queued run. */
10607
+ joined: boolean(),
10608
+ /** True when the run was cancelled before completing every destination. */
10609
+ cancelled: boolean(),
10610
+ /** One entry per destination the archive landed at (partial on cancel). */
10611
+ entries: array(BackupEntrySchema).readonly()
10612
+ });
10530
10613
  /**
10531
10614
  * A backup schedule — the N:M "entry" that binds one cron cadence to a
10532
10615
  * SET of destination locations. Supersedes the per-location cron on
@@ -10574,7 +10657,10 @@ method(_void(), array(BackupDestinationInfoSchema).readonly(), { auth: "admin" }
10574
10657
  * retention (manual runs).
10575
10658
  */
10576
10659
  retentionCount: number().int().min(1).max(1e3).optional()
10577
- }).optional(), array(BackupEntrySchema).readonly(), {
10660
+ }).optional(), BackupTriggerResultSchema, {
10661
+ kind: "mutation",
10662
+ auth: "admin"
10663
+ }), method(_void(), array(BackupRunSchema).readonly(), { auth: "admin" }), method(object({ runId: string() }), object({ cancelled: boolean() }), {
10578
10664
  kind: "mutation",
10579
10665
  auth: "admin"
10580
10666
  }), method(_void(), array(BackupEntrySchema).readonly(), { auth: "admin" }), method(_void(), array(LocationStatSchema).readonly(), { auth: "admin" }), method(object({
@@ -11291,6 +11377,14 @@ method(object({
11291
11377
  }), object({ success: literal(true) }), {
11292
11378
  kind: "mutation",
11293
11379
  auth: "admin"
11380
+ }), method(object({ deviceId: number().int().nonnegative() }), object({
11381
+ derivedStreamsDeleted: array(string()).readonly(),
11382
+ assignmentsPurged: boolean(),
11383
+ probeSnapshotsDropped: number().int().nonnegative(),
11384
+ rtspTokenRowsDeleted: number().int().nonnegative()
11385
+ }), {
11386
+ kind: "mutation",
11387
+ auth: "admin"
11294
11388
  }), method(object({
11295
11389
  deviceId: number(),
11296
11390
  /** Absent = the LOWEST assigned profile — a notification attachment is
@@ -12718,6 +12812,35 @@ var deviceProviderCapability = {
12718
12812
  name: string(),
12719
12813
  type: string()
12720
12814
  }))),
12815
+ /**
12816
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12817
+ * touching no other device this provider owns.
12818
+ *
12819
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12820
+ * migrated numbers: after `swapIds` the runner's live instance still
12821
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12822
+ * registrations and its log tags), and a live object cannot be renumbered.
12823
+ * Before this method the only flush was restarting the whole owning addon
12824
+ * — which took every camera the provider owns down with it (28 devices
12825
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12826
+ * same day ~27 devices' native caps did not come back on their own).
12827
+ *
12828
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12829
+ * that changes. The reply carries the id the device answers on NOW.
12830
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12831
+ * instance (if any), then re-create from the persisted row: the same
12832
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12833
+ * An RPC, never an event: a dropped event would leave the runner writing
12834
+ * against the wrong camera (D8).
12835
+ *
12836
+ * Construction can dial hardware, and the migrated source is
12837
+ * characteristically dead — the timeout covers a full activate window
12838
+ * rather than the 60 s default.
12839
+ */
12840
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12841
+ kind: "mutation",
12842
+ timeoutMs: 3 * 6e4
12843
+ }),
12721
12844
  supportsDiscovery: method(object({}), boolean()),
12722
12845
  /**
12723
12846
  * Run a network scan. `params` carries optional provider-specific scan
@@ -13045,7 +13168,8 @@ method(object({
13045
13168
  targetId: number()
13046
13169
  }), MigrateDeviceResultSchema, {
13047
13170
  kind: "mutation",
13048
- auth: "admin"
13171
+ auth: "admin",
13172
+ timeoutMs: 12 * 6e4
13049
13173
  }), method(DeviceRegisterPayloadSchema, _void(), { kind: "mutation" }), method(DeviceRemovePayloadSchema, _void(), { kind: "mutation" }), method(DevicePersistConfigPayloadSchema, _void(), { kind: "mutation" }), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), DeviceMetaSchema.nullable()), method(object({
13050
13174
  deviceId: number(),
13051
13175
  name: string()
@@ -32401,6 +32525,147 @@ var BaseDevice = class {
32401
32525
  }
32402
32526
  };
32403
32527
  /**
32528
+ * Delays before retry rounds 1..N — the round count IS the bound.
32529
+ * 10 s catches "the hub was busy for a moment"; the full schedule
32530
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
32531
+ * per attempt) covers a device-manager lock held for minutes — the
32532
+ * 2026-09-04 outage's migration hold was ~3.5 min.
32533
+ */
32534
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
32535
+ 1e4,
32536
+ 3e4,
32537
+ 9e4
32538
+ ];
32539
+ /** Abortable sleep — resolves early (never rejects) on abort. */
32540
+ function sleep$1(ms, signal) {
32541
+ return new Promise((resolve) => {
32542
+ if (signal.aborted) {
32543
+ resolve();
32544
+ return;
32545
+ }
32546
+ const onAbort = () => {
32547
+ clearTimeout(timer);
32548
+ resolve();
32549
+ };
32550
+ const timer = setTimeout(() => {
32551
+ signal.removeEventListener("abort", onAbort);
32552
+ resolve();
32553
+ }, ms);
32554
+ timer.unref?.();
32555
+ signal.addEventListener("abort", onAbort, { once: true });
32556
+ });
32557
+ }
32558
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
32559
+ * not reject (callers wrap their own try/catch). */
32560
+ async function runWithConcurrency(items, width, fn) {
32561
+ const queue = [...items];
32562
+ const laneCount = Math.max(1, Math.min(width, queue.length));
32563
+ const lane = async () => {
32564
+ for (;;) {
32565
+ const item = queue.shift();
32566
+ if (item === void 0) return;
32567
+ await fn(item);
32568
+ }
32569
+ };
32570
+ await Promise.all(Array.from({ length: laneCount }, lane));
32571
+ }
32572
+ var DeviceRestoreRetryScheduler = class {
32573
+ #logger;
32574
+ #attempt;
32575
+ #onPermanentFailure;
32576
+ #delaysMs;
32577
+ #concurrency;
32578
+ #now;
32579
+ #abort = new AbortController();
32580
+ constructor(options) {
32581
+ this.#logger = options.logger;
32582
+ this.#attempt = options.attempt;
32583
+ this.#onPermanentFailure = options.onPermanentFailure;
32584
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
32585
+ this.#concurrency = options.concurrency ?? 4;
32586
+ this.#now = options.now ?? Date.now;
32587
+ }
32588
+ /** Stop retrying (shutdown). Pending entries are NOT marked
32589
+ * permanently failed — the next boot restores them from disk. */
32590
+ cancel() {
32591
+ this.#abort.abort();
32592
+ }
32593
+ /**
32594
+ * Run the bounded retry rounds. Resolves when every entry has either
32595
+ * restored, been marked permanently failed, or the scheduler was
32596
+ * cancelled. Never rejects.
32597
+ */
32598
+ async run(initialFailures) {
32599
+ let pending = initialFailures.map((failure) => ({
32600
+ saved: failure.saved,
32601
+ lastError: failure.error,
32602
+ attempts: 1
32603
+ }));
32604
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
32605
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
32606
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
32607
+ if (this.#abort.signal.aborted) break;
32608
+ pending = await this.#runRound(pending, round);
32609
+ }
32610
+ if (this.#abort.signal.aborted) return [];
32611
+ const terminal = pending.map((entry) => ({
32612
+ deviceId: entry.saved.id,
32613
+ stableId: entry.saved.stableId,
32614
+ type: String(entry.saved.type),
32615
+ attempts: entry.attempts,
32616
+ lastError: entry.lastError,
32617
+ failedAt: this.#now()
32618
+ }));
32619
+ for (const failure of terminal) this.#onPermanentFailure(failure);
32620
+ return terminal;
32621
+ }
32622
+ /** One retry round: parents first (phase 0), then hub-adopted
32623
+ * children (phase 1) — a child's attempt depends on its parent
32624
+ * having landed, exactly like the initial two-pass restore. */
32625
+ async #runRound(pending, round) {
32626
+ const next = [];
32627
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
32628
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
32629
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
32630
+ if (this.#abort.signal.aborted) {
32631
+ next.push(entry);
32632
+ return;
32633
+ }
32634
+ const attemptNo = entry.attempts + 1;
32635
+ try {
32636
+ await this.#attempt(entry.saved);
32637
+ this.#logger.info("Device restored on retry", {
32638
+ tags: {
32639
+ deviceId: entry.saved.id,
32640
+ stableId: entry.saved.stableId
32641
+ },
32642
+ meta: { attempt: attemptNo }
32643
+ });
32644
+ } catch (err) {
32645
+ const lastError = err instanceof Error ? err.message : String(err);
32646
+ const remainingRetries = this.#delaysMs.length - (round + 1);
32647
+ this.#logger.warn("Device restore retry failed", {
32648
+ tags: {
32649
+ deviceId: entry.saved.id,
32650
+ stableId: entry.saved.stableId
32651
+ },
32652
+ meta: {
32653
+ attempt: attemptNo,
32654
+ remainingRetries,
32655
+ error: lastError
32656
+ }
32657
+ });
32658
+ next.push({
32659
+ saved: entry.saved,
32660
+ lastError,
32661
+ attempts: attemptNo
32662
+ });
32663
+ }
32664
+ });
32665
+ return next;
32666
+ }
32667
+ };
32668
+ /**
32404
32669
  * Convert an IDevice to the flat DeviceSummary shape expected by the
32405
32670
  * device-provider cap router. Shared across all providers.
32406
32671
  */
@@ -32449,6 +32714,7 @@ var BaseDeviceProvider = class extends BaseAddon {
32449
32714
  }];
32450
32715
  }
32451
32716
  async onShutdown() {
32717
+ this.cancelRestoreRetries();
32452
32718
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
32453
32719
  for (const device of devices) try {
32454
32720
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -32466,9 +32732,16 @@ var BaseDeviceProvider = class extends BaseAddon {
32466
32732
  async start() {}
32467
32733
  async stop() {}
32468
32734
  async getStatus() {
32735
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
32736
+ const summary = this.restoreFailureSummary();
32737
+ if (summary === null) return {
32738
+ connected: true,
32739
+ deviceCount: all.length
32740
+ };
32469
32741
  return {
32470
32742
  connected: true,
32471
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
32743
+ deviceCount: all.length,
32744
+ error: summary
32472
32745
  };
32473
32746
  }
32474
32747
  async getDevices() {
@@ -32558,8 +32831,137 @@ var BaseDeviceProvider = class extends BaseAddon {
32558
32831
  };
32559
32832
  }
32560
32833
  async restoreDevices(savedDevices) {
32561
- await this.onRestoreDevices(savedDevices);
32562
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
32834
+ const report = await this.onRestoreDevices(savedDevices);
32835
+ if (savedDevices.length === 0) return;
32836
+ if (report && report.failedCount > 0) {
32837
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
32838
+ return;
32839
+ }
32840
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
32841
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
32842
+ }
32843
+ /** Retry schedule. Overridable (tests use millisecond delays). */
32844
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
32845
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
32846
+ * never re-stampede full-width while the initial pass does (D167). */
32847
+ restoreRetryConcurrency = 4;
32848
+ _restoreRetryScheduler = null;
32849
+ _restoreRetryCompletion = null;
32850
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
32851
+ /** Settles when the background retry rounds finish (or `null` when
32852
+ * nothing failed). Exposed for tests and subclass diagnostics —
32853
+ * boot NEVER awaits this: the runner's post-init handshake goes out
32854
+ * with the devices that restored, and a late success is announced
32855
+ * through the `native-cap-change` → `updateCaps` path. */
32856
+ get restoreRetryCompletion() {
32857
+ return this._restoreRetryCompletion;
32858
+ }
32859
+ /** Devices that exhausted the retry bound this process lifetime. */
32860
+ get permanentRestoreFailures() {
32861
+ return [...this._permanentRestoreFailures.values()];
32862
+ }
32863
+ /** One-line operator-facing summary for `getStatus().error`, or
32864
+ * `null` when every device restored. */
32865
+ restoreFailureSummary() {
32866
+ if (this._permanentRestoreFailures.size === 0) return null;
32867
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
32868
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
32869
+ }
32870
+ cancelRestoreRetries() {
32871
+ this._restoreRetryScheduler?.cancel();
32872
+ this._restoreRetryScheduler = null;
32873
+ }
32874
+ recordPermanentRestoreFailure(failure) {
32875
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
32876
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
32877
+ tags: {
32878
+ deviceId: failure.deviceId,
32879
+ stableId: failure.stableId
32880
+ },
32881
+ meta: {
32882
+ type: failure.type,
32883
+ attempts: failure.attempts,
32884
+ error: failure.lastError
32885
+ }
32886
+ });
32887
+ }
32888
+ scheduleRestoreRetries(failures, attempt) {
32889
+ const scheduler = new DeviceRestoreRetryScheduler({
32890
+ logger: this.ctx.logger,
32891
+ delaysMs: this.restoreRetryDelaysMs,
32892
+ concurrency: this.restoreRetryConcurrency,
32893
+ attempt,
32894
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
32895
+ });
32896
+ this._restoreRetryScheduler = scheduler;
32897
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
32898
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
32899
+ });
32900
+ }
32901
+ /**
32902
+ * Tear down and reconstruct ONE device from its persisted rows — the
32903
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
32904
+ * and no other device this provider owns is disturbed.
32905
+ *
32906
+ * Keyed by `stableId` because the caller's whole reason to be here is that
32907
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
32908
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
32909
+ * whatever number the row carries NOW. The teardown is `decommission` —
32910
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
32911
+ * unregisters native caps, drops the registry entry) — and the rebuild is
32912
+ * the boot restore's own `create()` path, including its pass 2: first-class
32913
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
32914
+ * parent by the cascade and must be re-created explicitly, because only
32915
+ * accessory children come back through `getAccessoryChildren()`.
32916
+ *
32917
+ * Reloading an accessory child directly is refused (no device class) —
32918
+ * reload its parent instead.
32919
+ */
32920
+ async reloadDevice(input) {
32921
+ const { stableId } = input;
32922
+ const devices = this.ctx.kernel.devices;
32923
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
32924
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
32925
+ if (live) await devices.decommission(live.id);
32926
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
32927
+ addonId: this.addonId,
32928
+ stableId
32929
+ });
32930
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
32931
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
32932
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
32933
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
32934
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
32935
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
32936
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
32937
+ for (const row of rows) {
32938
+ if (row.parentDeviceId !== id) continue;
32939
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
32940
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
32941
+ if (!ChildClass) continue;
32942
+ try {
32943
+ await devices.create(row.stableId, ChildClass, {}, id);
32944
+ } catch (err) {
32945
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
32946
+ tags: {
32947
+ deviceId: row.id,
32948
+ stableId: row.stableId
32949
+ },
32950
+ meta: {
32951
+ parentDeviceId: id,
32952
+ error: err instanceof Error ? err.message : String(err)
32953
+ }
32954
+ });
32955
+ }
32956
+ }
32957
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
32958
+ tags: { deviceId: id },
32959
+ meta: {
32960
+ stableId,
32961
+ type: meta.type
32962
+ }
32963
+ });
32964
+ return { deviceId: id };
32563
32965
  }
32564
32966
  /**
32565
32967
  * Restore devices from persisted state. Two-pass:
@@ -32585,55 +32987,108 @@ var BaseDeviceProvider = class extends BaseAddon {
32585
32987
  * accessory-spawn flow handles via the parent's
32586
32988
  * `getAccessoryChildren()`. Override only when the default doesn't
32587
32989
  * fit.
32990
+ *
32991
+ * A row that fails either pass is NOT terminal (D347): it is handed
32992
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
32993
+ * Only after the bound is exhausted is the device marked permanently
32994
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
32995
+ * `getStatus().error`.
32588
32996
  */
32589
32997
  async onRestoreDevices(savedDevices) {
32590
32998
  const restored = /* @__PURE__ */ new Set();
32999
+ const failures = [];
33000
+ const attemptRestore = async (saved) => {
33001
+ if (restored.has(saved.id)) return;
33002
+ const Class = this.deviceClasses[saved.type];
33003
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
33004
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
33005
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33006
+ restored.add(saved.id);
33007
+ };
32591
33008
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
32592
33009
  const restoreOne = async (saved) => {
32593
- const Class = this.deviceClasses[saved.type];
32594
- if (!Class) {
33010
+ if (!this.deviceClasses[saved.type]) {
32595
33011
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
32596
- tags: { stableId: saved.stableId },
33012
+ tags: {
33013
+ deviceId: saved.id,
33014
+ stableId: saved.stableId
33015
+ },
32597
33016
  meta: { type: saved.type }
32598
33017
  });
32599
33018
  return;
32600
33019
  }
32601
33020
  try {
32602
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
32603
- restored.add(saved.id);
33021
+ await attemptRestore(saved);
32604
33022
  } catch (err) {
32605
- this.ctx.logger.warn("Failed to restore device", {
32606
- tags: { stableId: saved.stableId },
33023
+ const error = err instanceof Error ? err.message : String(err);
33024
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
33025
+ tags: {
33026
+ deviceId: saved.id,
33027
+ stableId: saved.stableId
33028
+ },
32607
33029
  meta: {
32608
33030
  type: saved.type,
32609
- error: err instanceof Error ? err.message : String(err)
33031
+ attempt: 1,
33032
+ error
32610
33033
  }
32611
33034
  });
33035
+ failures.push({
33036
+ saved,
33037
+ error
33038
+ });
32612
33039
  }
32613
33040
  };
32614
33041
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
33042
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
32615
33043
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
32616
33044
  for (const saved of childRows) {
32617
- const Class = this.deviceClasses[saved.type];
32618
- if (!Class) continue;
33045
+ if (!this.deviceClasses[saved.type]) continue;
32619
33046
  if (saved.parentDeviceId === null) continue;
32620
- if (!restored.has(saved.parentDeviceId)) continue;
32621
- try {
32622
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
32623
- restored.add(saved.id);
32624
- } catch (err) {
32625
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
33047
+ if (restored.has(saved.parentDeviceId)) {
33048
+ try {
33049
+ await attemptRestore(saved);
33050
+ } catch (err) {
33051
+ const error = err instanceof Error ? err.message : String(err);
33052
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
33053
+ tags: {
33054
+ deviceId: saved.id,
33055
+ stableId: saved.stableId,
33056
+ parentDeviceId: saved.parentDeviceId
33057
+ },
33058
+ meta: {
33059
+ type: saved.type,
33060
+ attempt: 1,
33061
+ error
33062
+ }
33063
+ });
33064
+ failures.push({
33065
+ saved,
33066
+ error
33067
+ });
33068
+ }
33069
+ continue;
33070
+ }
33071
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
33072
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
32626
33073
  tags: {
33074
+ deviceId: saved.id,
32627
33075
  stableId: saved.stableId,
32628
33076
  parentDeviceId: saved.parentDeviceId
32629
33077
  },
32630
- meta: {
32631
- type: saved.type,
32632
- error: err instanceof Error ? err.message : String(err)
32633
- }
33078
+ meta: { type: saved.type }
33079
+ });
33080
+ failures.push({
33081
+ saved,
33082
+ error: `parent device ${saved.parentDeviceId} not restored`
32634
33083
  });
33084
+ continue;
32635
33085
  }
32636
33086
  }
33087
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
33088
+ return {
33089
+ restoredCount: restored.size,
33090
+ failedCount: failures.length
33091
+ };
32637
33092
  }
32638
33093
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
32639
33094
  toSummary(device) {
@@ -33142,6 +33597,12 @@ Object.freeze({
33142
33597
  addonId: null,
33143
33598
  access: "create"
33144
33599
  },
33600
+ "backup.cancel": {
33601
+ capName: "backup",
33602
+ capScope: "system",
33603
+ addonId: null,
33604
+ access: "create"
33605
+ },
33145
33606
  "backup.delete": {
33146
33607
  capName: "backup",
33147
33608
  capScope: "system",
@@ -33184,6 +33645,12 @@ Object.freeze({
33184
33645
  addonId: null,
33185
33646
  access: "view"
33186
33647
  },
33648
+ "backup.listRuns": {
33649
+ capName: "backup",
33650
+ capScope: "system",
33651
+ addonId: null,
33652
+ access: "view"
33653
+ },
33187
33654
  "backup.listSchedules": {
33188
33655
  capName: "backup",
33189
33656
  capScope: "system",
@@ -34384,6 +34851,12 @@ Object.freeze({
34384
34851
  addonId: null,
34385
34852
  access: "view"
34386
34853
  },
34854
+ "deviceProvider.reloadDevice": {
34855
+ capName: "device-provider",
34856
+ capScope: "system",
34857
+ addonId: null,
34858
+ access: "create"
34859
+ },
34387
34860
  "deviceProvider.start": {
34388
34861
  capName: "device-provider",
34389
34862
  capScope: "system",
@@ -37750,6 +38223,12 @@ Object.freeze({
37750
38223
  addonId: null,
37751
38224
  access: "create"
37752
38225
  },
38226
+ "streamBroker.forgetDeviceHardware": {
38227
+ capName: "stream-broker",
38228
+ capScope: "system",
38229
+ addonId: null,
38230
+ access: "delete"
38231
+ },
37753
38232
  "streamBroker.getAllRtspEntries": {
37754
38233
  capName: "stream-broker",
37755
38234
  capScope: "system",
@@ -40208,6 +40687,11 @@ Object.freeze({
40208
40687
  form: "single",
40209
40688
  optional: false
40210
40689
  }],
40690
+ "streamBroker.forgetDeviceHardware": [{
40691
+ name: "deviceId",
40692
+ form: "single",
40693
+ optional: false
40694
+ }],
40211
40695
  "streamBroker.getDeviceAudioMute": [{
40212
40696
  name: "deviceId",
40213
40697
  form: "single",
package/dist/addon.mjs CHANGED
@@ -10526,6 +10526,89 @@ var LocationStatSchema = object({
10526
10526
  fileCount: number(),
10527
10527
  present: boolean()
10528
10528
  });
10529
+ /** Lifecycle of a backup run. Terminal states: succeeded / failed / cancelled. */
10530
+ var BackupRunStateSchema = _enum([
10531
+ "queued",
10532
+ "running",
10533
+ "succeeded",
10534
+ "failed",
10535
+ "cancelled"
10536
+ ]);
10537
+ /**
10538
+ * Where a running backup currently is. `queued` before it starts,
10539
+ * `building` while the tar.gz is being staged, `uploading` during the
10540
+ * per-destination fan-out, `done` once terminal.
10541
+ */
10542
+ var BackupRunPhaseSchema = _enum([
10543
+ "queued",
10544
+ "building",
10545
+ "uploading",
10546
+ "done"
10547
+ ]);
10548
+ /**
10549
+ * Observable state of one backup run — readable WHILE it runs via
10550
+ * `backup.listRuns`. This is what makes the execution queue and
10551
+ * `backup.cancel` usable: the 2026-09-04 incident (two concurrent
10552
+ * multi-GB builds, staging 5.1 GB → 18 GB, load 62) was only
10553
+ * diagnosable with `du` because nothing reported that runs existed or
10554
+ * how large the staged archive had grown.
10555
+ */
10556
+ var BackupRunSchema = object({
10557
+ /** Stable run id — the handle `backup.cancel` takes. */
10558
+ id: string(),
10559
+ state: BackupRunStateSchema,
10560
+ phase: BackupRunPhaseSchema,
10561
+ /**
10562
+ * Resolved destination location ids. Empty while queued (targets are
10563
+ * resolved when the run starts, against the then-current policies).
10564
+ */
10565
+ destinationIds: array(string()).readonly(),
10566
+ label: string().optional(),
10567
+ /** ms-epoch when the run was submitted (trigger call / schedule fire). */
10568
+ requestedAt: number(),
10569
+ /** ms-epoch when the run left the queue and started building. */
10570
+ startedAt: number().optional(),
10571
+ /** ms-epoch when the run reached a terminal state. */
10572
+ finishedAt: number().optional(),
10573
+ /** Compressed bytes of the staging archive written so far. */
10574
+ stagedBytes: number(),
10575
+ /** Final staged archive size, once the build phase completes. */
10576
+ archiveSizeBytes: number().optional(),
10577
+ /** Bytes pushed to the destination currently uploading. */
10578
+ uploadedBytes: number(),
10579
+ /** Destinations where the archive fully landed (uploaded + indexed). */
10580
+ completedDestinationIds: array(string()).readonly(),
10581
+ /** Destinations that failed during the fan-out. */
10582
+ failedDestinationIds: array(string()).readonly(),
10583
+ /** Failure message when `state === 'failed'`. */
10584
+ error: string().optional(),
10585
+ /**
10586
+ * 1-based place in the execution queue — 1 = runs next. Present only
10587
+ * while `state === 'queued'`. Stamped by the orchestrator from the
10588
+ * queue's OWN pending order, never derived from timestamps, so the
10589
+ * UI cannot show an order the executor will not honour.
10590
+ */
10591
+ queuePosition: number().int().min(1).optional()
10592
+ });
10593
+ /**
10594
+ * Result of `backup.trigger`. The call still resolves when the run
10595
+ * terminates (compat with schedule-driven runs and the admin UI), but
10596
+ * it now names the run and says whether it had to WAIT: a trigger that
10597
+ * arrives while another run is in flight is enqueued (or joined onto
10598
+ * an identical already-queued run), never started concurrently.
10599
+ */
10600
+ var BackupTriggerResultSchema = object({
10601
+ /** The run this trigger mapped to — poll it via `listRuns`, stop it via `cancel`. */
10602
+ runId: string(),
10603
+ /** True when the run waited behind an in-flight run instead of starting immediately. */
10604
+ queued: boolean(),
10605
+ /** True when this trigger was coalesced onto an identical already-queued run. */
10606
+ joined: boolean(),
10607
+ /** True when the run was cancelled before completing every destination. */
10608
+ cancelled: boolean(),
10609
+ /** One entry per destination the archive landed at (partial on cancel). */
10610
+ entries: array(BackupEntrySchema).readonly()
10611
+ });
10529
10612
  /**
10530
10613
  * A backup schedule — the N:M "entry" that binds one cron cadence to a
10531
10614
  * SET of destination locations. Supersedes the per-location cron on
@@ -10573,7 +10656,10 @@ method(_void(), array(BackupDestinationInfoSchema).readonly(), { auth: "admin" }
10573
10656
  * retention (manual runs).
10574
10657
  */
10575
10658
  retentionCount: number().int().min(1).max(1e3).optional()
10576
- }).optional(), array(BackupEntrySchema).readonly(), {
10659
+ }).optional(), BackupTriggerResultSchema, {
10660
+ kind: "mutation",
10661
+ auth: "admin"
10662
+ }), method(_void(), array(BackupRunSchema).readonly(), { auth: "admin" }), method(object({ runId: string() }), object({ cancelled: boolean() }), {
10577
10663
  kind: "mutation",
10578
10664
  auth: "admin"
10579
10665
  }), method(_void(), array(BackupEntrySchema).readonly(), { auth: "admin" }), method(_void(), array(LocationStatSchema).readonly(), { auth: "admin" }), method(object({
@@ -11290,6 +11376,14 @@ method(object({
11290
11376
  }), object({ success: literal(true) }), {
11291
11377
  kind: "mutation",
11292
11378
  auth: "admin"
11379
+ }), method(object({ deviceId: number().int().nonnegative() }), object({
11380
+ derivedStreamsDeleted: array(string()).readonly(),
11381
+ assignmentsPurged: boolean(),
11382
+ probeSnapshotsDropped: number().int().nonnegative(),
11383
+ rtspTokenRowsDeleted: number().int().nonnegative()
11384
+ }), {
11385
+ kind: "mutation",
11386
+ auth: "admin"
11293
11387
  }), method(object({
11294
11388
  deviceId: number(),
11295
11389
  /** Absent = the LOWEST assigned profile — a notification attachment is
@@ -12717,6 +12811,35 @@ var deviceProviderCapability = {
12717
12811
  name: string(),
12718
12812
  type: string()
12719
12813
  }))),
12814
+ /**
12815
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12816
+ * touching no other device this provider owns.
12817
+ *
12818
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12819
+ * migrated numbers: after `swapIds` the runner's live instance still
12820
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12821
+ * registrations and its log tags), and a live object cannot be renumbered.
12822
+ * Before this method the only flush was restarting the whole owning addon
12823
+ * — which took every camera the provider owns down with it (28 devices
12824
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12825
+ * same day ~27 devices' native caps did not come back on their own).
12826
+ *
12827
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12828
+ * that changes. The reply carries the id the device answers on NOW.
12829
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12830
+ * instance (if any), then re-create from the persisted row: the same
12831
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12832
+ * An RPC, never an event: a dropped event would leave the runner writing
12833
+ * against the wrong camera (D8).
12834
+ *
12835
+ * Construction can dial hardware, and the migrated source is
12836
+ * characteristically dead — the timeout covers a full activate window
12837
+ * rather than the 60 s default.
12838
+ */
12839
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12840
+ kind: "mutation",
12841
+ timeoutMs: 3 * 6e4
12842
+ }),
12720
12843
  supportsDiscovery: method(object({}), boolean()),
12721
12844
  /**
12722
12845
  * Run a network scan. `params` carries optional provider-specific scan
@@ -13044,7 +13167,8 @@ method(object({
13044
13167
  targetId: number()
13045
13168
  }), MigrateDeviceResultSchema, {
13046
13169
  kind: "mutation",
13047
- auth: "admin"
13170
+ auth: "admin",
13171
+ timeoutMs: 12 * 6e4
13048
13172
  }), method(DeviceRegisterPayloadSchema, _void(), { kind: "mutation" }), method(DeviceRemovePayloadSchema, _void(), { kind: "mutation" }), method(DevicePersistConfigPayloadSchema, _void(), { kind: "mutation" }), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), DeviceMetaSchema.nullable()), method(object({
13049
13173
  deviceId: number(),
13050
13174
  name: string()
@@ -32400,6 +32524,147 @@ var BaseDevice = class {
32400
32524
  }
32401
32525
  };
32402
32526
  /**
32527
+ * Delays before retry rounds 1..N — the round count IS the bound.
32528
+ * 10 s catches "the hub was busy for a moment"; the full schedule
32529
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
32530
+ * per attempt) covers a device-manager lock held for minutes — the
32531
+ * 2026-09-04 outage's migration hold was ~3.5 min.
32532
+ */
32533
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
32534
+ 1e4,
32535
+ 3e4,
32536
+ 9e4
32537
+ ];
32538
+ /** Abortable sleep — resolves early (never rejects) on abort. */
32539
+ function sleep$1(ms, signal) {
32540
+ return new Promise((resolve) => {
32541
+ if (signal.aborted) {
32542
+ resolve();
32543
+ return;
32544
+ }
32545
+ const onAbort = () => {
32546
+ clearTimeout(timer);
32547
+ resolve();
32548
+ };
32549
+ const timer = setTimeout(() => {
32550
+ signal.removeEventListener("abort", onAbort);
32551
+ resolve();
32552
+ }, ms);
32553
+ timer.unref?.();
32554
+ signal.addEventListener("abort", onAbort, { once: true });
32555
+ });
32556
+ }
32557
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
32558
+ * not reject (callers wrap their own try/catch). */
32559
+ async function runWithConcurrency(items, width, fn) {
32560
+ const queue = [...items];
32561
+ const laneCount = Math.max(1, Math.min(width, queue.length));
32562
+ const lane = async () => {
32563
+ for (;;) {
32564
+ const item = queue.shift();
32565
+ if (item === void 0) return;
32566
+ await fn(item);
32567
+ }
32568
+ };
32569
+ await Promise.all(Array.from({ length: laneCount }, lane));
32570
+ }
32571
+ var DeviceRestoreRetryScheduler = class {
32572
+ #logger;
32573
+ #attempt;
32574
+ #onPermanentFailure;
32575
+ #delaysMs;
32576
+ #concurrency;
32577
+ #now;
32578
+ #abort = new AbortController();
32579
+ constructor(options) {
32580
+ this.#logger = options.logger;
32581
+ this.#attempt = options.attempt;
32582
+ this.#onPermanentFailure = options.onPermanentFailure;
32583
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
32584
+ this.#concurrency = options.concurrency ?? 4;
32585
+ this.#now = options.now ?? Date.now;
32586
+ }
32587
+ /** Stop retrying (shutdown). Pending entries are NOT marked
32588
+ * permanently failed — the next boot restores them from disk. */
32589
+ cancel() {
32590
+ this.#abort.abort();
32591
+ }
32592
+ /**
32593
+ * Run the bounded retry rounds. Resolves when every entry has either
32594
+ * restored, been marked permanently failed, or the scheduler was
32595
+ * cancelled. Never rejects.
32596
+ */
32597
+ async run(initialFailures) {
32598
+ let pending = initialFailures.map((failure) => ({
32599
+ saved: failure.saved,
32600
+ lastError: failure.error,
32601
+ attempts: 1
32602
+ }));
32603
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
32604
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
32605
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
32606
+ if (this.#abort.signal.aborted) break;
32607
+ pending = await this.#runRound(pending, round);
32608
+ }
32609
+ if (this.#abort.signal.aborted) return [];
32610
+ const terminal = pending.map((entry) => ({
32611
+ deviceId: entry.saved.id,
32612
+ stableId: entry.saved.stableId,
32613
+ type: String(entry.saved.type),
32614
+ attempts: entry.attempts,
32615
+ lastError: entry.lastError,
32616
+ failedAt: this.#now()
32617
+ }));
32618
+ for (const failure of terminal) this.#onPermanentFailure(failure);
32619
+ return terminal;
32620
+ }
32621
+ /** One retry round: parents first (phase 0), then hub-adopted
32622
+ * children (phase 1) — a child's attempt depends on its parent
32623
+ * having landed, exactly like the initial two-pass restore. */
32624
+ async #runRound(pending, round) {
32625
+ const next = [];
32626
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
32627
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
32628
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
32629
+ if (this.#abort.signal.aborted) {
32630
+ next.push(entry);
32631
+ return;
32632
+ }
32633
+ const attemptNo = entry.attempts + 1;
32634
+ try {
32635
+ await this.#attempt(entry.saved);
32636
+ this.#logger.info("Device restored on retry", {
32637
+ tags: {
32638
+ deviceId: entry.saved.id,
32639
+ stableId: entry.saved.stableId
32640
+ },
32641
+ meta: { attempt: attemptNo }
32642
+ });
32643
+ } catch (err) {
32644
+ const lastError = err instanceof Error ? err.message : String(err);
32645
+ const remainingRetries = this.#delaysMs.length - (round + 1);
32646
+ this.#logger.warn("Device restore retry failed", {
32647
+ tags: {
32648
+ deviceId: entry.saved.id,
32649
+ stableId: entry.saved.stableId
32650
+ },
32651
+ meta: {
32652
+ attempt: attemptNo,
32653
+ remainingRetries,
32654
+ error: lastError
32655
+ }
32656
+ });
32657
+ next.push({
32658
+ saved: entry.saved,
32659
+ lastError,
32660
+ attempts: attemptNo
32661
+ });
32662
+ }
32663
+ });
32664
+ return next;
32665
+ }
32666
+ };
32667
+ /**
32403
32668
  * Convert an IDevice to the flat DeviceSummary shape expected by the
32404
32669
  * device-provider cap router. Shared across all providers.
32405
32670
  */
@@ -32448,6 +32713,7 @@ var BaseDeviceProvider = class extends BaseAddon {
32448
32713
  }];
32449
32714
  }
32450
32715
  async onShutdown() {
32716
+ this.cancelRestoreRetries();
32451
32717
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
32452
32718
  for (const device of devices) try {
32453
32719
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -32465,9 +32731,16 @@ var BaseDeviceProvider = class extends BaseAddon {
32465
32731
  async start() {}
32466
32732
  async stop() {}
32467
32733
  async getStatus() {
32734
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
32735
+ const summary = this.restoreFailureSummary();
32736
+ if (summary === null) return {
32737
+ connected: true,
32738
+ deviceCount: all.length
32739
+ };
32468
32740
  return {
32469
32741
  connected: true,
32470
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
32742
+ deviceCount: all.length,
32743
+ error: summary
32471
32744
  };
32472
32745
  }
32473
32746
  async getDevices() {
@@ -32557,8 +32830,137 @@ var BaseDeviceProvider = class extends BaseAddon {
32557
32830
  };
32558
32831
  }
32559
32832
  async restoreDevices(savedDevices) {
32560
- await this.onRestoreDevices(savedDevices);
32561
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
32833
+ const report = await this.onRestoreDevices(savedDevices);
32834
+ if (savedDevices.length === 0) return;
32835
+ if (report && report.failedCount > 0) {
32836
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
32837
+ return;
32838
+ }
32839
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
32840
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
32841
+ }
32842
+ /** Retry schedule. Overridable (tests use millisecond delays). */
32843
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
32844
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
32845
+ * never re-stampede full-width while the initial pass does (D167). */
32846
+ restoreRetryConcurrency = 4;
32847
+ _restoreRetryScheduler = null;
32848
+ _restoreRetryCompletion = null;
32849
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
32850
+ /** Settles when the background retry rounds finish (or `null` when
32851
+ * nothing failed). Exposed for tests and subclass diagnostics —
32852
+ * boot NEVER awaits this: the runner's post-init handshake goes out
32853
+ * with the devices that restored, and a late success is announced
32854
+ * through the `native-cap-change` → `updateCaps` path. */
32855
+ get restoreRetryCompletion() {
32856
+ return this._restoreRetryCompletion;
32857
+ }
32858
+ /** Devices that exhausted the retry bound this process lifetime. */
32859
+ get permanentRestoreFailures() {
32860
+ return [...this._permanentRestoreFailures.values()];
32861
+ }
32862
+ /** One-line operator-facing summary for `getStatus().error`, or
32863
+ * `null` when every device restored. */
32864
+ restoreFailureSummary() {
32865
+ if (this._permanentRestoreFailures.size === 0) return null;
32866
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
32867
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
32868
+ }
32869
+ cancelRestoreRetries() {
32870
+ this._restoreRetryScheduler?.cancel();
32871
+ this._restoreRetryScheduler = null;
32872
+ }
32873
+ recordPermanentRestoreFailure(failure) {
32874
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
32875
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
32876
+ tags: {
32877
+ deviceId: failure.deviceId,
32878
+ stableId: failure.stableId
32879
+ },
32880
+ meta: {
32881
+ type: failure.type,
32882
+ attempts: failure.attempts,
32883
+ error: failure.lastError
32884
+ }
32885
+ });
32886
+ }
32887
+ scheduleRestoreRetries(failures, attempt) {
32888
+ const scheduler = new DeviceRestoreRetryScheduler({
32889
+ logger: this.ctx.logger,
32890
+ delaysMs: this.restoreRetryDelaysMs,
32891
+ concurrency: this.restoreRetryConcurrency,
32892
+ attempt,
32893
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
32894
+ });
32895
+ this._restoreRetryScheduler = scheduler;
32896
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
32897
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
32898
+ });
32899
+ }
32900
+ /**
32901
+ * Tear down and reconstruct ONE device from its persisted rows — the
32902
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
32903
+ * and no other device this provider owns is disturbed.
32904
+ *
32905
+ * Keyed by `stableId` because the caller's whole reason to be here is that
32906
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
32907
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
32908
+ * whatever number the row carries NOW. The teardown is `decommission` —
32909
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
32910
+ * unregisters native caps, drops the registry entry) — and the rebuild is
32911
+ * the boot restore's own `create()` path, including its pass 2: first-class
32912
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
32913
+ * parent by the cascade and must be re-created explicitly, because only
32914
+ * accessory children come back through `getAccessoryChildren()`.
32915
+ *
32916
+ * Reloading an accessory child directly is refused (no device class) —
32917
+ * reload its parent instead.
32918
+ */
32919
+ async reloadDevice(input) {
32920
+ const { stableId } = input;
32921
+ const devices = this.ctx.kernel.devices;
32922
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
32923
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
32924
+ if (live) await devices.decommission(live.id);
32925
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
32926
+ addonId: this.addonId,
32927
+ stableId
32928
+ });
32929
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
32930
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
32931
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
32932
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
32933
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
32934
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
32935
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
32936
+ for (const row of rows) {
32937
+ if (row.parentDeviceId !== id) continue;
32938
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
32939
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
32940
+ if (!ChildClass) continue;
32941
+ try {
32942
+ await devices.create(row.stableId, ChildClass, {}, id);
32943
+ } catch (err) {
32944
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
32945
+ tags: {
32946
+ deviceId: row.id,
32947
+ stableId: row.stableId
32948
+ },
32949
+ meta: {
32950
+ parentDeviceId: id,
32951
+ error: err instanceof Error ? err.message : String(err)
32952
+ }
32953
+ });
32954
+ }
32955
+ }
32956
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
32957
+ tags: { deviceId: id },
32958
+ meta: {
32959
+ stableId,
32960
+ type: meta.type
32961
+ }
32962
+ });
32963
+ return { deviceId: id };
32562
32964
  }
32563
32965
  /**
32564
32966
  * Restore devices from persisted state. Two-pass:
@@ -32584,55 +32986,108 @@ var BaseDeviceProvider = class extends BaseAddon {
32584
32986
  * accessory-spawn flow handles via the parent's
32585
32987
  * `getAccessoryChildren()`. Override only when the default doesn't
32586
32988
  * fit.
32989
+ *
32990
+ * A row that fails either pass is NOT terminal (D347): it is handed
32991
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
32992
+ * Only after the bound is exhausted is the device marked permanently
32993
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
32994
+ * `getStatus().error`.
32587
32995
  */
32588
32996
  async onRestoreDevices(savedDevices) {
32589
32997
  const restored = /* @__PURE__ */ new Set();
32998
+ const failures = [];
32999
+ const attemptRestore = async (saved) => {
33000
+ if (restored.has(saved.id)) return;
33001
+ const Class = this.deviceClasses[saved.type];
33002
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
33003
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
33004
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
33005
+ restored.add(saved.id);
33006
+ };
32590
33007
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
32591
33008
  const restoreOne = async (saved) => {
32592
- const Class = this.deviceClasses[saved.type];
32593
- if (!Class) {
33009
+ if (!this.deviceClasses[saved.type]) {
32594
33010
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
32595
- tags: { stableId: saved.stableId },
33011
+ tags: {
33012
+ deviceId: saved.id,
33013
+ stableId: saved.stableId
33014
+ },
32596
33015
  meta: { type: saved.type }
32597
33016
  });
32598
33017
  return;
32599
33018
  }
32600
33019
  try {
32601
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
32602
- restored.add(saved.id);
33020
+ await attemptRestore(saved);
32603
33021
  } catch (err) {
32604
- this.ctx.logger.warn("Failed to restore device", {
32605
- tags: { stableId: saved.stableId },
33022
+ const error = err instanceof Error ? err.message : String(err);
33023
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
33024
+ tags: {
33025
+ deviceId: saved.id,
33026
+ stableId: saved.stableId
33027
+ },
32606
33028
  meta: {
32607
33029
  type: saved.type,
32608
- error: err instanceof Error ? err.message : String(err)
33030
+ attempt: 1,
33031
+ error
32609
33032
  }
32610
33033
  });
33034
+ failures.push({
33035
+ saved,
33036
+ error
33037
+ });
32611
33038
  }
32612
33039
  };
32613
33040
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
33041
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
32614
33042
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
32615
33043
  for (const saved of childRows) {
32616
- const Class = this.deviceClasses[saved.type];
32617
- if (!Class) continue;
33044
+ if (!this.deviceClasses[saved.type]) continue;
32618
33045
  if (saved.parentDeviceId === null) continue;
32619
- if (!restored.has(saved.parentDeviceId)) continue;
32620
- try {
32621
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
32622
- restored.add(saved.id);
32623
- } catch (err) {
32624
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
33046
+ if (restored.has(saved.parentDeviceId)) {
33047
+ try {
33048
+ await attemptRestore(saved);
33049
+ } catch (err) {
33050
+ const error = err instanceof Error ? err.message : String(err);
33051
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
33052
+ tags: {
33053
+ deviceId: saved.id,
33054
+ stableId: saved.stableId,
33055
+ parentDeviceId: saved.parentDeviceId
33056
+ },
33057
+ meta: {
33058
+ type: saved.type,
33059
+ attempt: 1,
33060
+ error
33061
+ }
33062
+ });
33063
+ failures.push({
33064
+ saved,
33065
+ error
33066
+ });
33067
+ }
33068
+ continue;
33069
+ }
33070
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
33071
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
32625
33072
  tags: {
33073
+ deviceId: saved.id,
32626
33074
  stableId: saved.stableId,
32627
33075
  parentDeviceId: saved.parentDeviceId
32628
33076
  },
32629
- meta: {
32630
- type: saved.type,
32631
- error: err instanceof Error ? err.message : String(err)
32632
- }
33077
+ meta: { type: saved.type }
33078
+ });
33079
+ failures.push({
33080
+ saved,
33081
+ error: `parent device ${saved.parentDeviceId} not restored`
32633
33082
  });
33083
+ continue;
32634
33084
  }
32635
33085
  }
33086
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
33087
+ return {
33088
+ restoredCount: restored.size,
33089
+ failedCount: failures.length
33090
+ };
32636
33091
  }
32637
33092
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
32638
33093
  toSummary(device) {
@@ -33141,6 +33596,12 @@ Object.freeze({
33141
33596
  addonId: null,
33142
33597
  access: "create"
33143
33598
  },
33599
+ "backup.cancel": {
33600
+ capName: "backup",
33601
+ capScope: "system",
33602
+ addonId: null,
33603
+ access: "create"
33604
+ },
33144
33605
  "backup.delete": {
33145
33606
  capName: "backup",
33146
33607
  capScope: "system",
@@ -33183,6 +33644,12 @@ Object.freeze({
33183
33644
  addonId: null,
33184
33645
  access: "view"
33185
33646
  },
33647
+ "backup.listRuns": {
33648
+ capName: "backup",
33649
+ capScope: "system",
33650
+ addonId: null,
33651
+ access: "view"
33652
+ },
33186
33653
  "backup.listSchedules": {
33187
33654
  capName: "backup",
33188
33655
  capScope: "system",
@@ -34383,6 +34850,12 @@ Object.freeze({
34383
34850
  addonId: null,
34384
34851
  access: "view"
34385
34852
  },
34853
+ "deviceProvider.reloadDevice": {
34854
+ capName: "device-provider",
34855
+ capScope: "system",
34856
+ addonId: null,
34857
+ access: "create"
34858
+ },
34386
34859
  "deviceProvider.start": {
34387
34860
  capName: "device-provider",
34388
34861
  capScope: "system",
@@ -37749,6 +38222,12 @@ Object.freeze({
37749
38222
  addonId: null,
37750
38223
  access: "create"
37751
38224
  },
38225
+ "streamBroker.forgetDeviceHardware": {
38226
+ capName: "stream-broker",
38227
+ capScope: "system",
38228
+ addonId: null,
38229
+ access: "delete"
38230
+ },
37752
38231
  "streamBroker.getAllRtspEntries": {
37753
38232
  capName: "stream-broker",
37754
38233
  capScope: "system",
@@ -40207,6 +40686,11 @@ Object.freeze({
40207
40686
  form: "single",
40208
40687
  optional: false
40209
40688
  }],
40689
+ "streamBroker.forgetDeviceHardware": [{
40690
+ name: "deviceId",
40691
+ form: "single",
40692
+ optional: false
40693
+ }],
40210
40694
  "streamBroker.getDeviceAudioMute": [{
40211
40695
  name: "deviceId",
40212
40696
  form: "single",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-provider-velux",
3
- "version": "0.2.58",
3
+ "version": "0.2.60",
4
4
  "description": "Velux KLF-200 device-provider addon for CamStack — local-gateway window + shutter covers (positional, no tilt)",
5
5
  "keywords": [
6
6
  "camstack",