@camstack/addon-provider-onvif 1.2.57 → 1.2.59

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
@@ -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
@@ -12508,6 +12602,35 @@ var deviceProviderCapability = {
12508
12602
  name: string(),
12509
12603
  type: string()
12510
12604
  }))),
12605
+ /**
12606
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12607
+ * touching no other device this provider owns.
12608
+ *
12609
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12610
+ * migrated numbers: after `swapIds` the runner's live instance still
12611
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12612
+ * registrations and its log tags), and a live object cannot be renumbered.
12613
+ * Before this method the only flush was restarting the whole owning addon
12614
+ * — which took every camera the provider owns down with it (28 devices
12615
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12616
+ * same day ~27 devices' native caps did not come back on their own).
12617
+ *
12618
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12619
+ * that changes. The reply carries the id the device answers on NOW.
12620
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12621
+ * instance (if any), then re-create from the persisted row: the same
12622
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12623
+ * An RPC, never an event: a dropped event would leave the runner writing
12624
+ * against the wrong camera (D8).
12625
+ *
12626
+ * Construction can dial hardware, and the migrated source is
12627
+ * characteristically dead — the timeout covers a full activate window
12628
+ * rather than the 60 s default.
12629
+ */
12630
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12631
+ kind: "mutation",
12632
+ timeoutMs: 3 * 6e4
12633
+ }),
12511
12634
  supportsDiscovery: method(object({}), boolean()),
12512
12635
  /**
12513
12636
  * Run a network scan. `params` carries optional provider-specific scan
@@ -12835,7 +12958,8 @@ method(object({
12835
12958
  targetId: number()
12836
12959
  }), MigrateDeviceResultSchema, {
12837
12960
  kind: "mutation",
12838
- auth: "admin"
12961
+ auth: "admin",
12962
+ timeoutMs: 12 * 6e4
12839
12963
  }), 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({
12840
12964
  deviceId: number(),
12841
12965
  name: string()
@@ -29189,6 +29313,147 @@ var DeviceConfig = class DeviceConfig {
29189
29313
  }
29190
29314
  };
29191
29315
  /**
29316
+ * Delays before retry rounds 1..N — the round count IS the bound.
29317
+ * 10 s catches "the hub was busy for a moment"; the full schedule
29318
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
29319
+ * per attempt) covers a device-manager lock held for minutes — the
29320
+ * 2026-09-04 outage's migration hold was ~3.5 min.
29321
+ */
29322
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
29323
+ 1e4,
29324
+ 3e4,
29325
+ 9e4
29326
+ ];
29327
+ /** Abortable sleep — resolves early (never rejects) on abort. */
29328
+ function sleep$1(ms, signal) {
29329
+ return new Promise((resolve) => {
29330
+ if (signal.aborted) {
29331
+ resolve();
29332
+ return;
29333
+ }
29334
+ const onAbort = () => {
29335
+ clearTimeout(timer);
29336
+ resolve();
29337
+ };
29338
+ const timer = setTimeout(() => {
29339
+ signal.removeEventListener("abort", onAbort);
29340
+ resolve();
29341
+ }, ms);
29342
+ timer.unref?.();
29343
+ signal.addEventListener("abort", onAbort, { once: true });
29344
+ });
29345
+ }
29346
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
29347
+ * not reject (callers wrap their own try/catch). */
29348
+ async function runWithConcurrency(items, width, fn) {
29349
+ const queue = [...items];
29350
+ const laneCount = Math.max(1, Math.min(width, queue.length));
29351
+ const lane = async () => {
29352
+ for (;;) {
29353
+ const item = queue.shift();
29354
+ if (item === void 0) return;
29355
+ await fn(item);
29356
+ }
29357
+ };
29358
+ await Promise.all(Array.from({ length: laneCount }, lane));
29359
+ }
29360
+ var DeviceRestoreRetryScheduler = class {
29361
+ #logger;
29362
+ #attempt;
29363
+ #onPermanentFailure;
29364
+ #delaysMs;
29365
+ #concurrency;
29366
+ #now;
29367
+ #abort = new AbortController();
29368
+ constructor(options) {
29369
+ this.#logger = options.logger;
29370
+ this.#attempt = options.attempt;
29371
+ this.#onPermanentFailure = options.onPermanentFailure;
29372
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
29373
+ this.#concurrency = options.concurrency ?? 4;
29374
+ this.#now = options.now ?? Date.now;
29375
+ }
29376
+ /** Stop retrying (shutdown). Pending entries are NOT marked
29377
+ * permanently failed — the next boot restores them from disk. */
29378
+ cancel() {
29379
+ this.#abort.abort();
29380
+ }
29381
+ /**
29382
+ * Run the bounded retry rounds. Resolves when every entry has either
29383
+ * restored, been marked permanently failed, or the scheduler was
29384
+ * cancelled. Never rejects.
29385
+ */
29386
+ async run(initialFailures) {
29387
+ let pending = initialFailures.map((failure) => ({
29388
+ saved: failure.saved,
29389
+ lastError: failure.error,
29390
+ attempts: 1
29391
+ }));
29392
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
29393
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
29394
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
29395
+ if (this.#abort.signal.aborted) break;
29396
+ pending = await this.#runRound(pending, round);
29397
+ }
29398
+ if (this.#abort.signal.aborted) return [];
29399
+ const terminal = pending.map((entry) => ({
29400
+ deviceId: entry.saved.id,
29401
+ stableId: entry.saved.stableId,
29402
+ type: String(entry.saved.type),
29403
+ attempts: entry.attempts,
29404
+ lastError: entry.lastError,
29405
+ failedAt: this.#now()
29406
+ }));
29407
+ for (const failure of terminal) this.#onPermanentFailure(failure);
29408
+ return terminal;
29409
+ }
29410
+ /** One retry round: parents first (phase 0), then hub-adopted
29411
+ * children (phase 1) — a child's attempt depends on its parent
29412
+ * having landed, exactly like the initial two-pass restore. */
29413
+ async #runRound(pending, round) {
29414
+ const next = [];
29415
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
29416
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
29417
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
29418
+ if (this.#abort.signal.aborted) {
29419
+ next.push(entry);
29420
+ return;
29421
+ }
29422
+ const attemptNo = entry.attempts + 1;
29423
+ try {
29424
+ await this.#attempt(entry.saved);
29425
+ this.#logger.info("Device restored on retry", {
29426
+ tags: {
29427
+ deviceId: entry.saved.id,
29428
+ stableId: entry.saved.stableId
29429
+ },
29430
+ meta: { attempt: attemptNo }
29431
+ });
29432
+ } catch (err) {
29433
+ const lastError = err instanceof Error ? err.message : String(err);
29434
+ const remainingRetries = this.#delaysMs.length - (round + 1);
29435
+ this.#logger.warn("Device restore retry failed", {
29436
+ tags: {
29437
+ deviceId: entry.saved.id,
29438
+ stableId: entry.saved.stableId
29439
+ },
29440
+ meta: {
29441
+ attempt: attemptNo,
29442
+ remainingRetries,
29443
+ error: lastError
29444
+ }
29445
+ });
29446
+ next.push({
29447
+ saved: entry.saved,
29448
+ lastError,
29449
+ attempts: attemptNo
29450
+ });
29451
+ }
29452
+ });
29453
+ return next;
29454
+ }
29455
+ };
29456
+ /**
29192
29457
  * Convert an IDevice to the flat DeviceSummary shape expected by the
29193
29458
  * device-provider cap router. Shared across all providers.
29194
29459
  */
@@ -29237,6 +29502,7 @@ var BaseDeviceProvider = class extends BaseAddon {
29237
29502
  }];
29238
29503
  }
29239
29504
  async onShutdown() {
29505
+ this.cancelRestoreRetries();
29240
29506
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
29241
29507
  for (const device of devices) try {
29242
29508
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -29254,9 +29520,16 @@ var BaseDeviceProvider = class extends BaseAddon {
29254
29520
  async start() {}
29255
29521
  async stop() {}
29256
29522
  async getStatus() {
29523
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
29524
+ const summary = this.restoreFailureSummary();
29525
+ if (summary === null) return {
29526
+ connected: true,
29527
+ deviceCount: all.length
29528
+ };
29257
29529
  return {
29258
29530
  connected: true,
29259
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
29531
+ deviceCount: all.length,
29532
+ error: summary
29260
29533
  };
29261
29534
  }
29262
29535
  async getDevices() {
@@ -29346,8 +29619,137 @@ var BaseDeviceProvider = class extends BaseAddon {
29346
29619
  };
29347
29620
  }
29348
29621
  async restoreDevices(savedDevices) {
29349
- await this.onRestoreDevices(savedDevices);
29350
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
29622
+ const report = await this.onRestoreDevices(savedDevices);
29623
+ if (savedDevices.length === 0) return;
29624
+ if (report && report.failedCount > 0) {
29625
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
29626
+ return;
29627
+ }
29628
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
29629
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
29630
+ }
29631
+ /** Retry schedule. Overridable (tests use millisecond delays). */
29632
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
29633
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
29634
+ * never re-stampede full-width while the initial pass does (D167). */
29635
+ restoreRetryConcurrency = 4;
29636
+ _restoreRetryScheduler = null;
29637
+ _restoreRetryCompletion = null;
29638
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
29639
+ /** Settles when the background retry rounds finish (or `null` when
29640
+ * nothing failed). Exposed for tests and subclass diagnostics —
29641
+ * boot NEVER awaits this: the runner's post-init handshake goes out
29642
+ * with the devices that restored, and a late success is announced
29643
+ * through the `native-cap-change` → `updateCaps` path. */
29644
+ get restoreRetryCompletion() {
29645
+ return this._restoreRetryCompletion;
29646
+ }
29647
+ /** Devices that exhausted the retry bound this process lifetime. */
29648
+ get permanentRestoreFailures() {
29649
+ return [...this._permanentRestoreFailures.values()];
29650
+ }
29651
+ /** One-line operator-facing summary for `getStatus().error`, or
29652
+ * `null` when every device restored. */
29653
+ restoreFailureSummary() {
29654
+ if (this._permanentRestoreFailures.size === 0) return null;
29655
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
29656
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
29657
+ }
29658
+ cancelRestoreRetries() {
29659
+ this._restoreRetryScheduler?.cancel();
29660
+ this._restoreRetryScheduler = null;
29661
+ }
29662
+ recordPermanentRestoreFailure(failure) {
29663
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
29664
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
29665
+ tags: {
29666
+ deviceId: failure.deviceId,
29667
+ stableId: failure.stableId
29668
+ },
29669
+ meta: {
29670
+ type: failure.type,
29671
+ attempts: failure.attempts,
29672
+ error: failure.lastError
29673
+ }
29674
+ });
29675
+ }
29676
+ scheduleRestoreRetries(failures, attempt) {
29677
+ const scheduler = new DeviceRestoreRetryScheduler({
29678
+ logger: this.ctx.logger,
29679
+ delaysMs: this.restoreRetryDelaysMs,
29680
+ concurrency: this.restoreRetryConcurrency,
29681
+ attempt,
29682
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
29683
+ });
29684
+ this._restoreRetryScheduler = scheduler;
29685
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
29686
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
29687
+ });
29688
+ }
29689
+ /**
29690
+ * Tear down and reconstruct ONE device from its persisted rows — the
29691
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
29692
+ * and no other device this provider owns is disturbed.
29693
+ *
29694
+ * Keyed by `stableId` because the caller's whole reason to be here is that
29695
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
29696
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
29697
+ * whatever number the row carries NOW. The teardown is `decommission` —
29698
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
29699
+ * unregisters native caps, drops the registry entry) — and the rebuild is
29700
+ * the boot restore's own `create()` path, including its pass 2: first-class
29701
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
29702
+ * parent by the cascade and must be re-created explicitly, because only
29703
+ * accessory children come back through `getAccessoryChildren()`.
29704
+ *
29705
+ * Reloading an accessory child directly is refused (no device class) —
29706
+ * reload its parent instead.
29707
+ */
29708
+ async reloadDevice(input) {
29709
+ const { stableId } = input;
29710
+ const devices = this.ctx.kernel.devices;
29711
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
29712
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
29713
+ if (live) await devices.decommission(live.id);
29714
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
29715
+ addonId: this.addonId,
29716
+ stableId
29717
+ });
29718
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
29719
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
29720
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
29721
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
29722
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
29723
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
29724
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
29725
+ for (const row of rows) {
29726
+ if (row.parentDeviceId !== id) continue;
29727
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
29728
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
29729
+ if (!ChildClass) continue;
29730
+ try {
29731
+ await devices.create(row.stableId, ChildClass, {}, id);
29732
+ } catch (err) {
29733
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
29734
+ tags: {
29735
+ deviceId: row.id,
29736
+ stableId: row.stableId
29737
+ },
29738
+ meta: {
29739
+ parentDeviceId: id,
29740
+ error: err instanceof Error ? err.message : String(err)
29741
+ }
29742
+ });
29743
+ }
29744
+ }
29745
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
29746
+ tags: { deviceId: id },
29747
+ meta: {
29748
+ stableId,
29749
+ type: meta.type
29750
+ }
29751
+ });
29752
+ return { deviceId: id };
29351
29753
  }
29352
29754
  /**
29353
29755
  * Restore devices from persisted state. Two-pass:
@@ -29373,55 +29775,108 @@ var BaseDeviceProvider = class extends BaseAddon {
29373
29775
  * accessory-spawn flow handles via the parent's
29374
29776
  * `getAccessoryChildren()`. Override only when the default doesn't
29375
29777
  * fit.
29778
+ *
29779
+ * A row that fails either pass is NOT terminal (D347): it is handed
29780
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
29781
+ * Only after the bound is exhausted is the device marked permanently
29782
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
29783
+ * `getStatus().error`.
29376
29784
  */
29377
29785
  async onRestoreDevices(savedDevices) {
29378
29786
  const restored = /* @__PURE__ */ new Set();
29787
+ const failures = [];
29788
+ const attemptRestore = async (saved) => {
29789
+ if (restored.has(saved.id)) return;
29790
+ const Class = this.deviceClasses[saved.type];
29791
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
29792
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
29793
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
29794
+ restored.add(saved.id);
29795
+ };
29379
29796
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
29380
29797
  const restoreOne = async (saved) => {
29381
- const Class = this.deviceClasses[saved.type];
29382
- if (!Class) {
29798
+ if (!this.deviceClasses[saved.type]) {
29383
29799
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
29384
- tags: { stableId: saved.stableId },
29800
+ tags: {
29801
+ deviceId: saved.id,
29802
+ stableId: saved.stableId
29803
+ },
29385
29804
  meta: { type: saved.type }
29386
29805
  });
29387
29806
  return;
29388
29807
  }
29389
29808
  try {
29390
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
29391
- restored.add(saved.id);
29809
+ await attemptRestore(saved);
29392
29810
  } catch (err) {
29393
- this.ctx.logger.warn("Failed to restore device", {
29394
- tags: { stableId: saved.stableId },
29811
+ const error = err instanceof Error ? err.message : String(err);
29812
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
29813
+ tags: {
29814
+ deviceId: saved.id,
29815
+ stableId: saved.stableId
29816
+ },
29395
29817
  meta: {
29396
29818
  type: saved.type,
29397
- error: err instanceof Error ? err.message : String(err)
29819
+ attempt: 1,
29820
+ error
29398
29821
  }
29399
29822
  });
29823
+ failures.push({
29824
+ saved,
29825
+ error
29826
+ });
29400
29827
  }
29401
29828
  };
29402
29829
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
29830
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
29403
29831
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
29404
29832
  for (const saved of childRows) {
29405
- const Class = this.deviceClasses[saved.type];
29406
- if (!Class) continue;
29833
+ if (!this.deviceClasses[saved.type]) continue;
29407
29834
  if (saved.parentDeviceId === null) continue;
29408
- if (!restored.has(saved.parentDeviceId)) continue;
29409
- try {
29410
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
29411
- restored.add(saved.id);
29412
- } catch (err) {
29413
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
29835
+ if (restored.has(saved.parentDeviceId)) {
29836
+ try {
29837
+ await attemptRestore(saved);
29838
+ } catch (err) {
29839
+ const error = err instanceof Error ? err.message : String(err);
29840
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
29841
+ tags: {
29842
+ deviceId: saved.id,
29843
+ stableId: saved.stableId,
29844
+ parentDeviceId: saved.parentDeviceId
29845
+ },
29846
+ meta: {
29847
+ type: saved.type,
29848
+ attempt: 1,
29849
+ error
29850
+ }
29851
+ });
29852
+ failures.push({
29853
+ saved,
29854
+ error
29855
+ });
29856
+ }
29857
+ continue;
29858
+ }
29859
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
29860
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
29414
29861
  tags: {
29862
+ deviceId: saved.id,
29415
29863
  stableId: saved.stableId,
29416
29864
  parentDeviceId: saved.parentDeviceId
29417
29865
  },
29418
- meta: {
29419
- type: saved.type,
29420
- error: err instanceof Error ? err.message : String(err)
29421
- }
29866
+ meta: { type: saved.type }
29867
+ });
29868
+ failures.push({
29869
+ saved,
29870
+ error: `parent device ${saved.parentDeviceId} not restored`
29422
29871
  });
29872
+ continue;
29423
29873
  }
29424
29874
  }
29875
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
29876
+ return {
29877
+ restoredCount: restored.size,
29878
+ failedCount: failures.length
29879
+ };
29425
29880
  }
29426
29881
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
29427
29882
  toSummary(device) {
@@ -29996,6 +30451,12 @@ Object.freeze({
29996
30451
  addonId: null,
29997
30452
  access: "create"
29998
30453
  },
30454
+ "backup.cancel": {
30455
+ capName: "backup",
30456
+ capScope: "system",
30457
+ addonId: null,
30458
+ access: "create"
30459
+ },
29999
30460
  "backup.delete": {
30000
30461
  capName: "backup",
30001
30462
  capScope: "system",
@@ -30038,6 +30499,12 @@ Object.freeze({
30038
30499
  addonId: null,
30039
30500
  access: "view"
30040
30501
  },
30502
+ "backup.listRuns": {
30503
+ capName: "backup",
30504
+ capScope: "system",
30505
+ addonId: null,
30506
+ access: "view"
30507
+ },
30041
30508
  "backup.listSchedules": {
30042
30509
  capName: "backup",
30043
30510
  capScope: "system",
@@ -31238,6 +31705,12 @@ Object.freeze({
31238
31705
  addonId: null,
31239
31706
  access: "view"
31240
31707
  },
31708
+ "deviceProvider.reloadDevice": {
31709
+ capName: "device-provider",
31710
+ capScope: "system",
31711
+ addonId: null,
31712
+ access: "create"
31713
+ },
31241
31714
  "deviceProvider.start": {
31242
31715
  capName: "device-provider",
31243
31716
  capScope: "system",
@@ -34604,6 +35077,12 @@ Object.freeze({
34604
35077
  addonId: null,
34605
35078
  access: "create"
34606
35079
  },
35080
+ "streamBroker.forgetDeviceHardware": {
35081
+ capName: "stream-broker",
35082
+ capScope: "system",
35083
+ addonId: null,
35084
+ access: "delete"
35085
+ },
34607
35086
  "streamBroker.getAllRtspEntries": {
34608
35087
  capName: "stream-broker",
34609
35088
  capScope: "system",
@@ -37062,6 +37541,11 @@ Object.freeze({
37062
37541
  form: "single",
37063
37542
  optional: false
37064
37543
  }],
37544
+ "streamBroker.forgetDeviceHardware": [{
37545
+ name: "deviceId",
37546
+ form: "single",
37547
+ optional: false
37548
+ }],
37065
37549
  "streamBroker.getDeviceAudioMute": [{
37066
37550
  name: "deviceId",
37067
37551
  form: "single",
package/dist/addon.mjs 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
@@ -12509,6 +12603,35 @@ var deviceProviderCapability = {
12509
12603
  name: string(),
12510
12604
  type: string()
12511
12605
  }))),
12606
+ /**
12607
+ * Tear down and reconstruct ONE device in place from its persisted rows —
12608
+ * touching no other device this provider owns.
12609
+ *
12610
+ * The primitive `deviceManager.migrateDevice` uses to flush the two
12611
+ * migrated numbers: after `swapIds` the runner's live instance still
12612
+ * carries the PRE-swap numeric id (baked into the object, its native-cap
12613
+ * registrations and its log tags), and a live object cannot be renumbered.
12614
+ * Before this method the only flush was restarting the whole owning addon
12615
+ * — which took every camera the provider owns down with it (28 devices
12616
+ * for one migrated camera, measured 2026-09-04, and the morning of the
12617
+ * same day ~27 devices' native caps did not come back on their own).
12618
+ *
12619
+ * Keyed by `stableId`, deliberately: the numeric id is exactly the thing
12620
+ * that changes. The reply carries the id the device answers on NOW.
12621
+ * Implemented once in `BaseDeviceProvider` — decommission the live
12622
+ * instance (if any), then re-create from the persisted row: the same
12623
+ * teardown/rehydrate pair every graceful shutdown + boot already uses.
12624
+ * An RPC, never an event: a dropped event would leave the runner writing
12625
+ * against the wrong camera (D8).
12626
+ *
12627
+ * Construction can dial hardware, and the migrated source is
12628
+ * characteristically dead — the timeout covers a full activate window
12629
+ * rather than the 60 s default.
12630
+ */
12631
+ reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
12632
+ kind: "mutation",
12633
+ timeoutMs: 3 * 6e4
12634
+ }),
12512
12635
  supportsDiscovery: method(object({}), boolean()),
12513
12636
  /**
12514
12637
  * Run a network scan. `params` carries optional provider-specific scan
@@ -12836,7 +12959,8 @@ method(object({
12836
12959
  targetId: number()
12837
12960
  }), MigrateDeviceResultSchema, {
12838
12961
  kind: "mutation",
12839
- auth: "admin"
12962
+ auth: "admin",
12963
+ timeoutMs: 12 * 6e4
12840
12964
  }), 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({
12841
12965
  deviceId: number(),
12842
12966
  name: string()
@@ -29190,6 +29314,147 @@ var DeviceConfig = class DeviceConfig {
29190
29314
  }
29191
29315
  };
29192
29316
  /**
29317
+ * Delays before retry rounds 1..N — the round count IS the bound.
29318
+ * 10 s catches "the hub was busy for a moment"; the full schedule
29319
+ * (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
29320
+ * per attempt) covers a device-manager lock held for minutes — the
29321
+ * 2026-09-04 outage's migration hold was ~3.5 min.
29322
+ */
29323
+ var DEVICE_RESTORE_RETRY_DELAYS_MS = [
29324
+ 1e4,
29325
+ 3e4,
29326
+ 9e4
29327
+ ];
29328
+ /** Abortable sleep — resolves early (never rejects) on abort. */
29329
+ function sleep$1(ms, signal) {
29330
+ return new Promise((resolve) => {
29331
+ if (signal.aborted) {
29332
+ resolve();
29333
+ return;
29334
+ }
29335
+ const onAbort = () => {
29336
+ clearTimeout(timer);
29337
+ resolve();
29338
+ };
29339
+ const timer = setTimeout(() => {
29340
+ signal.removeEventListener("abort", onAbort);
29341
+ resolve();
29342
+ }, ms);
29343
+ timer.unref?.();
29344
+ signal.addEventListener("abort", onAbort, { once: true });
29345
+ });
29346
+ }
29347
+ /** Drain `items` through at most `width` concurrent lanes. `fn` must
29348
+ * not reject (callers wrap their own try/catch). */
29349
+ async function runWithConcurrency(items, width, fn) {
29350
+ const queue = [...items];
29351
+ const laneCount = Math.max(1, Math.min(width, queue.length));
29352
+ const lane = async () => {
29353
+ for (;;) {
29354
+ const item = queue.shift();
29355
+ if (item === void 0) return;
29356
+ await fn(item);
29357
+ }
29358
+ };
29359
+ await Promise.all(Array.from({ length: laneCount }, lane));
29360
+ }
29361
+ var DeviceRestoreRetryScheduler = class {
29362
+ #logger;
29363
+ #attempt;
29364
+ #onPermanentFailure;
29365
+ #delaysMs;
29366
+ #concurrency;
29367
+ #now;
29368
+ #abort = new AbortController();
29369
+ constructor(options) {
29370
+ this.#logger = options.logger;
29371
+ this.#attempt = options.attempt;
29372
+ this.#onPermanentFailure = options.onPermanentFailure;
29373
+ this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
29374
+ this.#concurrency = options.concurrency ?? 4;
29375
+ this.#now = options.now ?? Date.now;
29376
+ }
29377
+ /** Stop retrying (shutdown). Pending entries are NOT marked
29378
+ * permanently failed — the next boot restores them from disk. */
29379
+ cancel() {
29380
+ this.#abort.abort();
29381
+ }
29382
+ /**
29383
+ * Run the bounded retry rounds. Resolves when every entry has either
29384
+ * restored, been marked permanently failed, or the scheduler was
29385
+ * cancelled. Never rejects.
29386
+ */
29387
+ async run(initialFailures) {
29388
+ let pending = initialFailures.map((failure) => ({
29389
+ saved: failure.saved,
29390
+ lastError: failure.error,
29391
+ attempts: 1
29392
+ }));
29393
+ for (let round = 0; round < this.#delaysMs.length; round += 1) {
29394
+ if (pending.length === 0 || this.#abort.signal.aborted) break;
29395
+ await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
29396
+ if (this.#abort.signal.aborted) break;
29397
+ pending = await this.#runRound(pending, round);
29398
+ }
29399
+ if (this.#abort.signal.aborted) return [];
29400
+ const terminal = pending.map((entry) => ({
29401
+ deviceId: entry.saved.id,
29402
+ stableId: entry.saved.stableId,
29403
+ type: String(entry.saved.type),
29404
+ attempts: entry.attempts,
29405
+ lastError: entry.lastError,
29406
+ failedAt: this.#now()
29407
+ }));
29408
+ for (const failure of terminal) this.#onPermanentFailure(failure);
29409
+ return terminal;
29410
+ }
29411
+ /** One retry round: parents first (phase 0), then hub-adopted
29412
+ * children (phase 1) — a child's attempt depends on its parent
29413
+ * having landed, exactly like the initial two-pass restore. */
29414
+ async #runRound(pending, round) {
29415
+ const next = [];
29416
+ const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
29417
+ const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
29418
+ for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
29419
+ if (this.#abort.signal.aborted) {
29420
+ next.push(entry);
29421
+ return;
29422
+ }
29423
+ const attemptNo = entry.attempts + 1;
29424
+ try {
29425
+ await this.#attempt(entry.saved);
29426
+ this.#logger.info("Device restored on retry", {
29427
+ tags: {
29428
+ deviceId: entry.saved.id,
29429
+ stableId: entry.saved.stableId
29430
+ },
29431
+ meta: { attempt: attemptNo }
29432
+ });
29433
+ } catch (err) {
29434
+ const lastError = err instanceof Error ? err.message : String(err);
29435
+ const remainingRetries = this.#delaysMs.length - (round + 1);
29436
+ this.#logger.warn("Device restore retry failed", {
29437
+ tags: {
29438
+ deviceId: entry.saved.id,
29439
+ stableId: entry.saved.stableId
29440
+ },
29441
+ meta: {
29442
+ attempt: attemptNo,
29443
+ remainingRetries,
29444
+ error: lastError
29445
+ }
29446
+ });
29447
+ next.push({
29448
+ saved: entry.saved,
29449
+ lastError,
29450
+ attempts: attemptNo
29451
+ });
29452
+ }
29453
+ });
29454
+ return next;
29455
+ }
29456
+ };
29457
+ /**
29193
29458
  * Convert an IDevice to the flat DeviceSummary shape expected by the
29194
29459
  * device-provider cap router. Shared across all providers.
29195
29460
  */
@@ -29238,6 +29503,7 @@ var BaseDeviceProvider = class extends BaseAddon {
29238
29503
  }];
29239
29504
  }
29240
29505
  async onShutdown() {
29506
+ this.cancelRestoreRetries();
29241
29507
  const devices = await this.ctx.kernel.devices?.getAll() ?? [];
29242
29508
  for (const device of devices) try {
29243
29509
  await this.ctx.kernel.devices?.decommission(device.id);
@@ -29255,9 +29521,16 @@ var BaseDeviceProvider = class extends BaseAddon {
29255
29521
  async start() {}
29256
29522
  async stop() {}
29257
29523
  async getStatus() {
29524
+ const all = await this.ctx.kernel.devices?.getAll() ?? [];
29525
+ const summary = this.restoreFailureSummary();
29526
+ if (summary === null) return {
29527
+ connected: true,
29528
+ deviceCount: all.length
29529
+ };
29258
29530
  return {
29259
29531
  connected: true,
29260
- deviceCount: (await this.ctx.kernel.devices?.getAll() ?? []).length
29532
+ deviceCount: all.length,
29533
+ error: summary
29261
29534
  };
29262
29535
  }
29263
29536
  async getDevices() {
@@ -29347,8 +29620,137 @@ var BaseDeviceProvider = class extends BaseAddon {
29347
29620
  };
29348
29621
  }
29349
29622
  async restoreDevices(savedDevices) {
29350
- await this.onRestoreDevices(savedDevices);
29351
- if (savedDevices.length > 0) this.ctx.logger.info(`Restored ${savedDevices.length} ${this.providerName} device(s)`);
29623
+ const report = await this.onRestoreDevices(savedDevices);
29624
+ if (savedDevices.length === 0) return;
29625
+ if (report && report.failedCount > 0) {
29626
+ this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
29627
+ return;
29628
+ }
29629
+ const restoredCount = report ? report.restoredCount : savedDevices.length;
29630
+ this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
29631
+ }
29632
+ /** Retry schedule. Overridable (tests use millisecond delays). */
29633
+ restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
29634
+ /** Retry lane width. See `device-restore-retry.ts` for why retries
29635
+ * never re-stampede full-width while the initial pass does (D167). */
29636
+ restoreRetryConcurrency = 4;
29637
+ _restoreRetryScheduler = null;
29638
+ _restoreRetryCompletion = null;
29639
+ _permanentRestoreFailures = /* @__PURE__ */ new Map();
29640
+ /** Settles when the background retry rounds finish (or `null` when
29641
+ * nothing failed). Exposed for tests and subclass diagnostics —
29642
+ * boot NEVER awaits this: the runner's post-init handshake goes out
29643
+ * with the devices that restored, and a late success is announced
29644
+ * through the `native-cap-change` → `updateCaps` path. */
29645
+ get restoreRetryCompletion() {
29646
+ return this._restoreRetryCompletion;
29647
+ }
29648
+ /** Devices that exhausted the retry bound this process lifetime. */
29649
+ get permanentRestoreFailures() {
29650
+ return [...this._permanentRestoreFailures.values()];
29651
+ }
29652
+ /** One-line operator-facing summary for `getStatus().error`, or
29653
+ * `null` when every device restored. */
29654
+ restoreFailureSummary() {
29655
+ if (this._permanentRestoreFailures.size === 0) return null;
29656
+ const ids = [...this._permanentRestoreFailures.keys()].join(", ");
29657
+ return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
29658
+ }
29659
+ cancelRestoreRetries() {
29660
+ this._restoreRetryScheduler?.cancel();
29661
+ this._restoreRetryScheduler = null;
29662
+ }
29663
+ recordPermanentRestoreFailure(failure) {
29664
+ this._permanentRestoreFailures.set(failure.deviceId, failure);
29665
+ this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
29666
+ tags: {
29667
+ deviceId: failure.deviceId,
29668
+ stableId: failure.stableId
29669
+ },
29670
+ meta: {
29671
+ type: failure.type,
29672
+ attempts: failure.attempts,
29673
+ error: failure.lastError
29674
+ }
29675
+ });
29676
+ }
29677
+ scheduleRestoreRetries(failures, attempt) {
29678
+ const scheduler = new DeviceRestoreRetryScheduler({
29679
+ logger: this.ctx.logger,
29680
+ delaysMs: this.restoreRetryDelaysMs,
29681
+ concurrency: this.restoreRetryConcurrency,
29682
+ attempt,
29683
+ onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
29684
+ });
29685
+ this._restoreRetryScheduler = scheduler;
29686
+ this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
29687
+ this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
29688
+ });
29689
+ }
29690
+ /**
29691
+ * Tear down and reconstruct ONE device from its persisted rows — the
29692
+ * `deviceProvider.reloadDevice` cap method. Persistence is never touched,
29693
+ * and no other device this provider owns is disturbed.
29694
+ *
29695
+ * Keyed by `stableId` because the caller's whole reason to be here is that
29696
+ * the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
29697
+ * fresh instance resolves its id through `allocateDeviceId`, which returns
29698
+ * whatever number the row carries NOW. The teardown is `decommission` —
29699
+ * exactly what a graceful shutdown runs per device (fires `removeDevice()`,
29700
+ * unregisters native caps, drops the registry entry) — and the rebuild is
29701
+ * the boot restore's own `create()` path, including its pass 2: first-class
29702
+ * children (hub-adopted cameras under an NVR) are decommissioned with the
29703
+ * parent by the cascade and must be re-created explicitly, because only
29704
+ * accessory children come back through `getAccessoryChildren()`.
29705
+ *
29706
+ * Reloading an accessory child directly is refused (no device class) —
29707
+ * reload its parent instead.
29708
+ */
29709
+ async reloadDevice(input) {
29710
+ const { stableId } = input;
29711
+ const devices = this.ctx.kernel.devices;
29712
+ if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
29713
+ const live = (await devices.getAll()).find((d) => d.stableId === stableId);
29714
+ if (live) await devices.decommission(live.id);
29715
+ const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
29716
+ addonId: this.addonId,
29717
+ stableId
29718
+ });
29719
+ const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
29720
+ if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
29721
+ const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
29722
+ const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
29723
+ if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
29724
+ await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
29725
+ const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
29726
+ for (const row of rows) {
29727
+ if (row.parentDeviceId !== id) continue;
29728
+ const childType = Object.values(DeviceType).find((t) => t === row.type);
29729
+ const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
29730
+ if (!ChildClass) continue;
29731
+ try {
29732
+ await devices.create(row.stableId, ChildClass, {}, id);
29733
+ } catch (err) {
29734
+ this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
29735
+ tags: {
29736
+ deviceId: row.id,
29737
+ stableId: row.stableId
29738
+ },
29739
+ meta: {
29740
+ parentDeviceId: id,
29741
+ error: err instanceof Error ? err.message : String(err)
29742
+ }
29743
+ });
29744
+ }
29745
+ }
29746
+ this.ctx.logger.info("device reloaded in place from persisted rows", {
29747
+ tags: { deviceId: id },
29748
+ meta: {
29749
+ stableId,
29750
+ type: meta.type
29751
+ }
29752
+ });
29753
+ return { deviceId: id };
29352
29754
  }
29353
29755
  /**
29354
29756
  * Restore devices from persisted state. Two-pass:
@@ -29374,55 +29776,108 @@ var BaseDeviceProvider = class extends BaseAddon {
29374
29776
  * accessory-spawn flow handles via the parent's
29375
29777
  * `getAccessoryChildren()`. Override only when the default doesn't
29376
29778
  * fit.
29779
+ *
29780
+ * A row that fails either pass is NOT terminal (D347): it is handed
29781
+ * to a bounded background retry (`DeviceRestoreRetryScheduler`).
29782
+ * Only after the bound is exhausted is the device marked permanently
29783
+ * failed — logged at ERROR with `tags.deviceId` and surfaced via
29784
+ * `getStatus().error`.
29377
29785
  */
29378
29786
  async onRestoreDevices(savedDevices) {
29379
29787
  const restored = /* @__PURE__ */ new Set();
29788
+ const failures = [];
29789
+ const attemptRestore = async (saved) => {
29790
+ if (restored.has(saved.id)) return;
29791
+ const Class = this.deviceClasses[saved.type];
29792
+ if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
29793
+ if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
29794
+ await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
29795
+ restored.add(saved.id);
29796
+ };
29380
29797
  const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
29381
29798
  const restoreOne = async (saved) => {
29382
- const Class = this.deviceClasses[saved.type];
29383
- if (!Class) {
29799
+ if (!this.deviceClasses[saved.type]) {
29384
29800
  this.ctx.logger.warn("No device class registered for restored type — skipping", {
29385
- tags: { stableId: saved.stableId },
29801
+ tags: {
29802
+ deviceId: saved.id,
29803
+ stableId: saved.stableId
29804
+ },
29386
29805
  meta: { type: saved.type }
29387
29806
  });
29388
29807
  return;
29389
29808
  }
29390
29809
  try {
29391
- await this.ctx.kernel.devices.create(saved.stableId, Class, {});
29392
- restored.add(saved.id);
29810
+ await attemptRestore(saved);
29393
29811
  } catch (err) {
29394
- this.ctx.logger.warn("Failed to restore device", {
29395
- tags: { stableId: saved.stableId },
29812
+ const error = err instanceof Error ? err.message : String(err);
29813
+ this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
29814
+ tags: {
29815
+ deviceId: saved.id,
29816
+ stableId: saved.stableId
29817
+ },
29396
29818
  meta: {
29397
29819
  type: saved.type,
29398
- error: err instanceof Error ? err.message : String(err)
29820
+ attempt: 1,
29821
+ error
29399
29822
  }
29400
29823
  });
29824
+ failures.push({
29825
+ saved,
29826
+ error
29827
+ });
29401
29828
  }
29402
29829
  };
29403
29830
  await Promise.all(topLevel.map((saved) => restoreOne(saved)));
29831
+ const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
29404
29832
  const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
29405
29833
  for (const saved of childRows) {
29406
- const Class = this.deviceClasses[saved.type];
29407
- if (!Class) continue;
29834
+ if (!this.deviceClasses[saved.type]) continue;
29408
29835
  if (saved.parentDeviceId === null) continue;
29409
- if (!restored.has(saved.parentDeviceId)) continue;
29410
- try {
29411
- await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
29412
- restored.add(saved.id);
29413
- } catch (err) {
29414
- this.ctx.logger.warn("Failed to restore hub-adopted child", {
29836
+ if (restored.has(saved.parentDeviceId)) {
29837
+ try {
29838
+ await attemptRestore(saved);
29839
+ } catch (err) {
29840
+ const error = err instanceof Error ? err.message : String(err);
29841
+ this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
29842
+ tags: {
29843
+ deviceId: saved.id,
29844
+ stableId: saved.stableId,
29845
+ parentDeviceId: saved.parentDeviceId
29846
+ },
29847
+ meta: {
29848
+ type: saved.type,
29849
+ attempt: 1,
29850
+ error
29851
+ }
29852
+ });
29853
+ failures.push({
29854
+ saved,
29855
+ error
29856
+ });
29857
+ }
29858
+ continue;
29859
+ }
29860
+ if (failedTopLevelIds.has(saved.parentDeviceId)) {
29861
+ this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
29415
29862
  tags: {
29863
+ deviceId: saved.id,
29416
29864
  stableId: saved.stableId,
29417
29865
  parentDeviceId: saved.parentDeviceId
29418
29866
  },
29419
- meta: {
29420
- type: saved.type,
29421
- error: err instanceof Error ? err.message : String(err)
29422
- }
29867
+ meta: { type: saved.type }
29868
+ });
29869
+ failures.push({
29870
+ saved,
29871
+ error: `parent device ${saved.parentDeviceId} not restored`
29423
29872
  });
29873
+ continue;
29424
29874
  }
29425
29875
  }
29876
+ if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
29877
+ return {
29878
+ restoredCount: restored.size,
29879
+ failedCount: failures.length
29880
+ };
29426
29881
  }
29427
29882
  /** Convert an IDevice to the flat DeviceSummary for the cap router. */
29428
29883
  toSummary(device) {
@@ -29997,6 +30452,12 @@ Object.freeze({
29997
30452
  addonId: null,
29998
30453
  access: "create"
29999
30454
  },
30455
+ "backup.cancel": {
30456
+ capName: "backup",
30457
+ capScope: "system",
30458
+ addonId: null,
30459
+ access: "create"
30460
+ },
30000
30461
  "backup.delete": {
30001
30462
  capName: "backup",
30002
30463
  capScope: "system",
@@ -30039,6 +30500,12 @@ Object.freeze({
30039
30500
  addonId: null,
30040
30501
  access: "view"
30041
30502
  },
30503
+ "backup.listRuns": {
30504
+ capName: "backup",
30505
+ capScope: "system",
30506
+ addonId: null,
30507
+ access: "view"
30508
+ },
30042
30509
  "backup.listSchedules": {
30043
30510
  capName: "backup",
30044
30511
  capScope: "system",
@@ -31239,6 +31706,12 @@ Object.freeze({
31239
31706
  addonId: null,
31240
31707
  access: "view"
31241
31708
  },
31709
+ "deviceProvider.reloadDevice": {
31710
+ capName: "device-provider",
31711
+ capScope: "system",
31712
+ addonId: null,
31713
+ access: "create"
31714
+ },
31242
31715
  "deviceProvider.start": {
31243
31716
  capName: "device-provider",
31244
31717
  capScope: "system",
@@ -34605,6 +35078,12 @@ Object.freeze({
34605
35078
  addonId: null,
34606
35079
  access: "create"
34607
35080
  },
35081
+ "streamBroker.forgetDeviceHardware": {
35082
+ capName: "stream-broker",
35083
+ capScope: "system",
35084
+ addonId: null,
35085
+ access: "delete"
35086
+ },
34608
35087
  "streamBroker.getAllRtspEntries": {
34609
35088
  capName: "stream-broker",
34610
35089
  capScope: "system",
@@ -37063,6 +37542,11 @@ Object.freeze({
37063
37542
  form: "single",
37064
37543
  optional: false
37065
37544
  }],
37545
+ "streamBroker.forgetDeviceHardware": [{
37546
+ name: "deviceId",
37547
+ form: "single",
37548
+ optional: false
37549
+ }],
37066
37550
  "streamBroker.getDeviceAudioMute": [{
37067
37551
  name: "deviceId",
37068
37552
  form: "single",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-provider-onvif",
3
- "version": "1.2.57",
3
+ "version": "1.2.59",
4
4
  "description": "ONVIF camera device provider addon for CamStack",
5
5
  "keywords": [
6
6
  "camstack",