@camstack/server 1.2.941 → 1.2.942

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 (154) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/api/trpc/generated-cap-mounts.js +19 -1
  3. package/dist/api/trpc/generated-cap-routers.js +676 -596
  4. package/package.json +27 -11
  5. package/system/dist/action-index-B1fgpQBt.mjs +9925 -0
  6. package/system/dist/action-index-B_cLRwXJ.js +9972 -0
  7. package/system/dist/alert-episode-Be--PngO.mjs +64 -0
  8. package/system/dist/alert-episode-Bqktmt-i.js +93 -0
  9. package/system/dist/{base-addon-Md14i7pe.mjs → base-addon-BcS6r1cT.mjs} +20 -7
  10. package/system/dist/{base-addon-DD5dIfcZ.js → base-addon-CRsXtYFG.js} +20 -7
  11. package/system/dist/{base-device-CH8rfD9j.js → base-device-DAdWzDOl.js} +138 -34
  12. package/system/dist/{base-device-BcGO6LTT.mjs → base-device-DJXF5eDN.mjs} +138 -34
  13. package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.js +1 -1
  14. package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.mjs +1 -1
  15. package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.js +1 -1
  16. package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.mjs +1 -1
  17. package/system/dist/builtins/alerts/alert-episode-policy.d.ts +9 -5
  18. package/system/dist/builtins/alerts/alerts.addon.js +30 -80
  19. package/system/dist/builtins/alerts/alerts.addon.mjs +18 -68
  20. package/system/dist/builtins/autotrack/index.js +2 -2
  21. package/system/dist/builtins/autotrack/index.mjs +2 -2
  22. package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.js +1 -1
  23. package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.mjs +1 -1
  24. package/system/dist/builtins/calendars/calendars.addon.js +1 -1
  25. package/system/dist/builtins/calendars/calendars.addon.mjs +1 -1
  26. package/system/dist/builtins/camera-grid/index.js +9 -9
  27. package/system/dist/builtins/camera-grid/index.mjs +6 -6
  28. package/system/dist/builtins/composer/composer.addon.js +99 -99
  29. package/system/dist/builtins/composer/composer.addon.mjs +11 -11
  30. package/system/dist/builtins/console-logging/index.js +1 -1
  31. package/system/dist/builtins/console-logging/index.mjs +1 -1
  32. package/system/dist/builtins/core-blocks/automation-template-instances.d.ts +14 -1
  33. package/system/dist/builtins/core-blocks/core-blocks.addon.d.ts +6 -0
  34. package/system/dist/builtins/core-blocks/core-blocks.addon.js +296 -193
  35. package/system/dist/builtins/core-blocks/core-blocks.addon.mjs +237 -134
  36. package/system/dist/builtins/core-blocks/link-contributions.d.ts +2 -0
  37. package/system/dist/builtins/core-notifier/core-notifier.addon.js +91 -18
  38. package/system/dist/builtins/core-notifier/core-notifier.addon.mjs +91 -18
  39. package/system/dist/builtins/core-notifier/group-notification-output.d.ts +8 -16
  40. package/system/dist/builtins/device-history/device-history.addon.d.ts +8 -0
  41. package/system/dist/builtins/device-history/device-history.addon.js +580 -60
  42. package/system/dist/builtins/device-history/device-history.addon.mjs +569 -49
  43. package/system/dist/builtins/device-history/retention/device-raw-overrides.d.ts +16 -0
  44. package/system/dist/builtins/device-history/retention/retention-knobs.d.ts +14 -0
  45. package/system/dist/builtins/device-history/retention/retention.d.ts +21 -1
  46. package/system/dist/builtins/device-history/transitions/change-coalescing.d.ts +48 -0
  47. package/system/dist/builtins/device-history/transitions/derived-changes.d.ts +49 -0
  48. package/system/dist/builtins/device-history/transitions/history-change-port.d.ts +20 -0
  49. package/system/dist/builtins/device-history/transitions/transitions-service.d.ts +18 -3
  50. package/system/dist/builtins/device-manager/device-identity-index.d.ts +2 -2
  51. package/system/dist/builtins/device-manager/device-manager.addon.js +177 -479
  52. package/system/dist/builtins/device-manager/device-manager.addon.mjs +169 -471
  53. package/system/dist/builtins/device-manager/device-state-mirror.d.ts +0 -5
  54. package/system/dist/builtins/device-manager/identity-link-reconciler.d.ts +5 -0
  55. package/system/dist/builtins/device-manager/transitions/command-recorder.d.ts +38 -0
  56. package/system/dist/builtins/device-manager/transitions/transitions-wiring.d.ts +8 -9
  57. package/system/dist/builtins/hub-forwarder/index.js +1 -1
  58. package/system/dist/builtins/hub-forwarder/index.mjs +1 -1
  59. package/system/dist/builtins/keychain/index.js +2 -2
  60. package/system/dist/builtins/keychain/index.mjs +2 -2
  61. package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.js +2 -2
  62. package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.mjs +2 -2
  63. package/system/dist/builtins/local-auth/local-auth.addon.js +2 -2
  64. package/system/dist/builtins/local-auth/local-auth.addon.mjs +2 -2
  65. package/system/dist/builtins/local-network/address-book.d.ts +3 -1
  66. package/system/dist/builtins/local-network/local-network.addon.d.ts +26 -1
  67. package/system/dist/builtins/local-network/local-network.addon.js +66 -11
  68. package/system/dist/builtins/local-network/local-network.addon.mjs +66 -11
  69. package/system/dist/builtins/loki-logging/index.js +1 -1
  70. package/system/dist/builtins/loki-logging/index.mjs +1 -1
  71. package/system/dist/builtins/native-metrics/native-metrics.addon.js +1 -1
  72. package/system/dist/builtins/native-metrics/native-metrics.addon.mjs +1 -1
  73. package/system/dist/builtins/platform-probe/index.js +1 -1
  74. package/system/dist/builtins/platform-probe/index.mjs +1 -1
  75. package/system/dist/builtins/record-entries/index.js +1 -1
  76. package/system/dist/builtins/record-entries/index.mjs +1 -1
  77. package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.js +1 -1
  78. package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.mjs +1 -1
  79. package/system/dist/builtins/snapshot/index.js +4 -4
  80. package/system/dist/builtins/snapshot/index.mjs +4 -4
  81. package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.js +1 -1
  82. package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.mjs +1 -1
  83. package/system/dist/builtins/sqlite-storage/index.js +1 -1
  84. package/system/dist/builtins/sqlite-storage/index.mjs +1 -1
  85. package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.js +3 -3
  86. package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.mjs +3 -3
  87. package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.js +1 -1
  88. package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.mjs +1 -1
  89. package/system/dist/builtins/system-config/system-config.addon.js +1 -1
  90. package/system/dist/builtins/system-config/system-config.addon.mjs +1 -1
  91. package/system/dist/builtins/winston-logging/index.js +1 -1
  92. package/system/dist/builtins/winston-logging/index.mjs +1 -1
  93. package/system/dist/{capture-engine-CUZ3W0oZ.mjs → capture-engine-B2MmoNND.mjs} +1 -1
  94. package/system/dist/{capture-engine-mtDXEBAq.js → capture-engine-D_1IJmMs.js} +1 -1
  95. package/system/dist/command-catalog-C29hVOOg.js +210 -0
  96. package/system/dist/command-catalog-xAeqxbZk.mjs +169 -0
  97. package/system/dist/{error.cap-DCwbIGEF.mjs → compile-OBMHlqD-.mjs} +97 -100
  98. package/system/dist/{error.cap-BxLEHP6z.js → compile-SpeAhaZ6.js} +107 -116
  99. package/system/dist/{composition-sources-B56CU8hi.mjs → composition-sources-C327LGFn.mjs} +2 -2
  100. package/system/dist/{composition-sources-D_Bfwji0.js → composition-sources-Do5qM6EB.js} +17 -17
  101. package/system/dist/{core-blocks.cap-xt5_fK5Z.js → core-blocks.cap-CdTGUKtr.js} +194 -10110
  102. package/system/dist/{core-blocks.cap-DDECcMeI.mjs → core-blocks.cap-DLkEfV5b.mjs} +54 -9910
  103. package/system/dist/{device-kind-BJ2pfsjw.mjs → device-kind-C37Ra2wd.mjs} +4 -2
  104. package/system/dist/{device-kind-CsSiv_YF.js → device-kind-FMhJg8uC.js} +4 -2
  105. package/system/dist/{device-manager.cap-DyHOIh5T.js → device-manager.cap-DJs8izI6.js} +13 -4
  106. package/system/dist/{device-manager.cap-DIBK62-r.mjs → device-manager.cap-eYrj1Ezp.mjs} +13 -4
  107. package/system/dist/{device-provider.cap-BKUGJSo0.js → device-provider.cap-CNlYBQ5D.js} +1 -1
  108. package/system/dist/{device-provider.cap-DU6HMUXH.mjs → device-provider.cap-D8mJllVM.mjs} +1 -1
  109. package/system/dist/{device-row-store-O4mNbp5q.js → device-row-store-C0P2H18h.js} +2 -2
  110. package/system/dist/{device-row-store-C-_R-h1Z.mjs → device-row-store-CTocYKqC.mjs} +2 -2
  111. package/system/dist/{device-type-CbQsnFdC.js → device-type-Cnc0NiSX.js} +13 -0
  112. package/system/dist/{device-type-DgEvK3ch.mjs → device-type-TUGqGmaI.mjs} +13 -0
  113. package/system/dist/error.cap-BuKbL3Ac.js +125 -0
  114. package/system/dist/error.cap-CU1FB6uE.mjs +102 -0
  115. package/system/dist/file-host-worker.js +1 -1
  116. package/system/dist/file-host-worker.mjs +1 -1
  117. package/system/dist/{history-catalog-BwTeQ4Mr.mjs → history-catalog-A_dzXNpv.mjs} +98 -3
  118. package/system/dist/{history-catalog-BKRarIL_.js → history-catalog-CiTMP3-E.js} +98 -3
  119. package/system/dist/index.js +28 -6
  120. package/system/dist/index.mjs +28 -6
  121. package/system/dist/kernel/addon-version-policy.d.ts +10 -0
  122. package/system/dist/{legacy-integration-rows-door-B1nzxxr5.js → legacy-integration-rows-door-CdqYAj-o.js} +1 -1
  123. package/system/dist/{legacy-integration-rows-door-nTvo_6jQ.mjs → legacy-integration-rows-door-LSgpClpX.mjs} +1 -1
  124. package/system/dist/{low-power-sleep.cap-B6mz6ojP.js → low-power-sleep.cap-BJWyAuUQ.js} +1 -1
  125. package/system/dist/{low-power-sleep.cap-C3Vi8Vxj.mjs → low-power-sleep.cap-D9Orx3LS.mjs} +1 -1
  126. package/system/dist/migrations/hub/2026-11-22-device-transitions-commands-only.d.ts +10 -0
  127. package/system/dist/{network-access.cap-BEN_uRVb.js → network-access.cap-CTQ2WQoi.js} +38 -12
  128. package/system/dist/{network-access.cap-Dy8FNNVs.mjs → network-access.cap-Dx5K0I5i.mjs} +33 -13
  129. package/system/dist/{notification-rules.cap-Drwz-A4n.js → notification-rules.cap-BJKUjizS.js} +45 -9
  130. package/system/dist/{notification-rules.cap-0vVvsxQ1.mjs → notification-rules.cap-BUIuYPlQ.mjs} +45 -9
  131. package/system/dist/{notification-targets-DR-D-vIt.mjs → notification-targets-4bGYH5J7.mjs} +2 -2
  132. package/system/dist/{notification-targets-Cc_3OBH9.js → notification-targets-DMW7xgR2.js} +2 -2
  133. package/system/dist/{prefix-walk-CCO_VZVo.js → prefix-walk-BFnw8NBu.js} +5 -5
  134. package/system/dist/{prefix-walk-BhT0ZoKl.mjs → prefix-walk-Mtij0NNT.mjs} +2 -2
  135. package/system/dist/{ptz-autotrack.cap-DOz0xL0O.js → ptz-autotrack.cap-B9eyZU8w.js} +1 -1
  136. package/system/dist/{ptz-autotrack.cap-CDF6FeTK.mjs → ptz-autotrack.cap-Dw2k-po9.mjs} +1 -1
  137. package/system/dist/{recording-archive.cap-RHAN45fa.mjs → recording-archive.cap-CTlm86R8.mjs} +1 -1
  138. package/system/dist/{recording-archive.cap-BPJ7kvnd.js → recording-archive.cap-Wo-hEZhz.js} +1 -1
  139. package/system/dist/{rename-composition-block-DWUkrazx.mjs → rename-composition-block-Cdh-dRmY.mjs} +395 -20
  140. package/system/dist/{rename-composition-block-CH2vRIW7.js → rename-composition-block-D9Uc1AF7.js} +406 -31
  141. package/system/dist/{retention-CUOVI1p7.mjs → retention-CQs8QR1B.mjs} +33 -3
  142. package/system/dist/{device-transitions.cap-DCXP3zwY.mjs → retention-source.cap-550kkyp5.mjs} +98 -21
  143. package/system/dist/{device-transitions.cap-zOGvAIHz.js → retention-source.cap-C6Hy0hlM.js} +103 -20
  144. package/system/dist/{retention-BcOSL6b9.js → retention-xcjk_k1w.js} +33 -3
  145. package/system/dist/{snapshot.cap-Cis6JRwn.js → snapshot.cap-CHm29hBs.js} +1 -1
  146. package/system/dist/{snapshot.cap-BkfHpJVO.mjs → snapshot.cap-CO68YvJU.mjs} +1 -1
  147. package/system/dist/{stream-catalog.cap-D8Y-uXcy.js → stream-catalog.cap-CjEQFknc.js} +2 -2
  148. package/system/dist/{stream-catalog.cap-D1Hy19Yc.mjs → stream-catalog.cap-DVniDdRD.mjs} +2 -2
  149. package/system/dist/{user-management.cap-Ctd4c_im.mjs → user-management.cap-4IEUCSdL.mjs} +2 -2
  150. package/system/dist/{user-management.cap-DyVb68rY.js → user-management.cap-DV9Jfsj3.js} +2 -2
  151. package/system/dist/{wal-maintenance-CjpslZUL.mjs → wal-maintenance-Bey5ChBb.mjs} +0 -0
  152. package/system/dist/{wal-maintenance-DKTXQ7UF.js → wal-maintenance-BgYiAy5b.js} +0 -0
  153. package/system/package.json +4 -1
  154. package/system/dist/builtins/device-manager/transitions/transition-correlator.d.ts +0 -81
@@ -3,16 +3,17 @@ Object.defineProperties(exports, {
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
5
  require("../../chunk-Cek0wNdY.js");
6
- const require_base_addon = require("../../base-addon-DD5dIfcZ.js");
6
+ const require_base_addon = require("../../base-addon-CRsXtYFG.js");
7
7
  const require_scoped_settings = require("../../scoped-settings-CylSSq_8.js");
8
8
  const require_record_kind = require("../../record-kind-DoBfYHCs.js");
9
- const require_device_manager_cap = require("../../device-manager.cap-DyHOIh5T.js");
10
- const require_device_transitions_cap = require("../../device-transitions.cap-zOGvAIHz.js");
9
+ const require_device_manager_cap = require("../../device-manager.cap-DJs8izI6.js");
10
+ const require_retention_source_cap = require("../../retention-source.cap-C6Hy0hlM.js");
11
11
  const require_quantity = require("../../quantity-lY1KiTSZ.js");
12
- const require_device_type = require("../../device-type-CbQsnFdC.js");
13
- const require_history_catalog = require("../../history-catalog-BKRarIL_.js");
14
- const require_capture_engine = require("../../capture-engine-mtDXEBAq.js");
15
- const require_retention = require("../../retention-BcOSL6b9.js");
12
+ const require_device_type = require("../../device-type-Cnc0NiSX.js");
13
+ const require_history_catalog = require("../../history-catalog-CiTMP3-E.js");
14
+ const require_command_catalog = require("../../command-catalog-C29hVOOg.js");
15
+ const require_capture_engine = require("../../capture-engine-D_1IJmMs.js");
16
+ const require_retention = require("../../retention-xcjk_k1w.js");
16
17
  let node_crypto = require("node:crypto");
17
18
  //#region ../types/dist/esm/device/device-removal-reconcile.mjs
18
19
  /** How often an implementer re-asks for tombstones after its start-up pass. */
@@ -777,7 +778,7 @@ var HistoryQuery = class {
777
778
  }
778
779
  const energy = this.energySource(subject);
779
780
  if (energy !== null) infos.push({
780
- key: require_device_transitions_cap.DEVICE_HISTORY_ENERGY_KEY,
781
+ key: require_retention_source_cap.DEVICE_HISTORY_ENERGY_KEY,
781
782
  scope: "",
782
783
  kind: "counter",
783
784
  unit: "kWh",
@@ -2899,7 +2900,7 @@ function createDeviceRunsProvider(runs) {
2899
2900
  };
2900
2901
  }
2901
2902
  var deviceRunsRecordKinds = require_record_kind.defineRecordKinds({ "device-runs": {
2902
- schema: require_device_transitions_cap.DeviceRunSchema,
2903
+ schema: require_retention_source_cap.DeviceRunSchema,
2903
2904
  class: "ledger",
2904
2905
  database: "events",
2905
2906
  writeTrigger: "a running-state flip opens (write-through) or closes a run; the boot reconcile closes or opens one the flips missed",
@@ -2941,7 +2942,7 @@ var MAX_POINTS = 5e3;
2941
2942
  async function energyKwhFromHistory(query, deviceId, from, to) {
2942
2943
  if (to - from < 6e5) return null;
2943
2944
  const result = await query({
2944
- key: require_device_transitions_cap.DEVICE_HISTORY_ENERGY_KEY,
2945
+ key: require_retention_source_cap.DEVICE_HISTORY_ENERGY_KEY,
2945
2946
  subject: {
2946
2947
  kind: "device",
2947
2948
  deviceId
@@ -3528,11 +3529,200 @@ var DeviceRunsService = class {
3528
3529
  return out;
3529
3530
  }
3530
3531
  };
3532
+ function fieldKey(capName, field) {
3533
+ return `${capName}|${field}`;
3534
+ }
3535
+ function sameValue(a, b) {
3536
+ if (Object.is(a, b)) return true;
3537
+ if (a === null || b === null || typeof a !== "object" || typeof b !== "object") return false;
3538
+ return JSON.stringify(a) === JSON.stringify(b);
3539
+ }
3540
+ function closeBurst(capName, burst, now, out) {
3541
+ if (now - burst.lastAt < 5e3 && now - burst.firstAt < 6e4) return;
3542
+ for (const [field, held] of burst.fields) {
3543
+ if (sameValue(held.from, held.to)) continue;
3544
+ out.push({
3545
+ capName,
3546
+ field,
3547
+ at: burst.lastAt,
3548
+ from: held.from,
3549
+ to: held.to
3550
+ });
3551
+ }
3552
+ }
3553
+ /** Group the points of each capability into bursts and answer the net change of each field. */
3554
+ function coalesceChanges(input) {
3555
+ const byCap = /* @__PURE__ */ new Map();
3556
+ for (const point of input.points) {
3557
+ const list = byCap.get(point.capName);
3558
+ if (list === void 0) byCap.set(point.capName, [point]);
3559
+ else list.push(point);
3560
+ }
3561
+ const out = [];
3562
+ for (const [capName, points] of byCap) {
3563
+ const held = /* @__PURE__ */ new Map();
3564
+ for (const point of points) {
3565
+ const key = fieldKey(capName, point.field);
3566
+ const known = input.before.get(key);
3567
+ if (known !== void 0) held.set(key, known);
3568
+ }
3569
+ let burst = null;
3570
+ for (const point of points.toSorted((a, b) => a.at - b.at)) {
3571
+ const key = fieldKey(capName, point.field);
3572
+ const prior = held.get(key);
3573
+ held.set(key, point.value);
3574
+ if (prior === void 0 || sameValue(prior, point.value)) continue;
3575
+ if (burst !== null && (point.at - burst.lastAt >= 5e3 || point.at - burst.firstAt >= 6e4)) {
3576
+ closeBurst(capName, burst, Number.POSITIVE_INFINITY, out);
3577
+ burst = null;
3578
+ }
3579
+ burst ??= {
3580
+ firstAt: point.at,
3581
+ lastAt: point.at,
3582
+ fields: /* @__PURE__ */ new Map()
3583
+ };
3584
+ burst.lastAt = point.at;
3585
+ const earlier = burst.fields.get(point.field);
3586
+ burst.fields.set(point.field, {
3587
+ from: earlier?.from ?? prior,
3588
+ to: point.value
3589
+ });
3590
+ }
3591
+ if (burst !== null) closeBurst(capName, burst, input.now, out);
3592
+ }
3593
+ return out.toSorted((a, b) => b.at - a.at);
3594
+ }
3595
+ //#endregion
3596
+ //#region src/builtins/device-history/transitions/derived-changes.ts
3597
+ /** The most raw points read for one series; the newest are kept past it. */
3598
+ var DERIVED_POINTS_MAX = 1e4;
3599
+ function valueOf(kind, point) {
3600
+ if (kind === "enum") return point.state ?? void 0;
3601
+ if (point.value === null) return void 0;
3602
+ if (kind === "boolean") return point.value !== 0;
3603
+ if (kind === "gauge") return point.value;
3604
+ }
3605
+ var DerivedChanges = class {
3606
+ port;
3607
+ clock;
3608
+ constructor(port, clock) {
3609
+ this.port = port;
3610
+ this.clock = clock;
3611
+ }
3612
+ /** The settled changes of the device's audited fields in `window`, newest first. */
3613
+ async read(deviceId, window) {
3614
+ const series = this.port.auditedSeries(deviceId).filter((s) => s.kind !== "counter").filter((s) => window.capName === void 0 || s.capName === window.capName);
3615
+ const points = [];
3616
+ const before = /* @__PURE__ */ new Map();
3617
+ for (const s of series) {
3618
+ const read = await this.port.readRaw(deviceId, s, window.from, window.to);
3619
+ if (read.before !== null) {
3620
+ const prior = valueOf(s.kind, read.before);
3621
+ if (prior !== void 0) before.set(fieldKey(s.capName, s.field), prior);
3622
+ }
3623
+ for (const p of read.points) {
3624
+ const value = valueOf(s.kind, p);
3625
+ if (value !== void 0) points.push({
3626
+ capName: s.capName,
3627
+ field: s.field,
3628
+ at: p.t,
3629
+ value
3630
+ });
3631
+ }
3632
+ }
3633
+ return coalesceChanges({
3634
+ points,
3635
+ before,
3636
+ now: this.clock()
3637
+ });
3638
+ }
3639
+ };
3640
+ //#endregion
3641
+ //#region src/builtins/device-history/transitions/history-change-port.ts
3642
+ /**
3643
+ * The derived-changes reader's view of device-history (D1084): which audited fields of a device
3644
+ * have a series, and their raw points. A field with no series (its cap has no `history` block)
3645
+ * has no derived row.
3646
+ */
3647
+ /** `cap:<capName>.<field>` → its parts, or `null` for a key that is not a top-level cap field. */
3648
+ function splitCapKey(key) {
3649
+ if (!key.startsWith("cap:")) return null;
3650
+ const rest = key.slice(require_capture_engine.CAP_KEY_PREFIX.length);
3651
+ const dot = rest.indexOf(".");
3652
+ if (dot <= 0 || dot === rest.length - 1) return null;
3653
+ return {
3654
+ capName: rest.slice(0, dot),
3655
+ field: rest.slice(dot + 1)
3656
+ };
3657
+ }
3658
+ var HistoryChangePort = class {
3659
+ deps;
3660
+ constructor(deps) {
3661
+ this.deps = deps;
3662
+ }
3663
+ auditedSeries(deviceId) {
3664
+ const out = [];
3665
+ for (const series of this.deps.registry.forSubject(`d:${deviceId}`)) {
3666
+ if (series.scope !== "") continue;
3667
+ const parts = splitCapKey(series.key);
3668
+ if (parts === null || !require_command_catalog.auditedFields(parts.capName).has(parts.field)) continue;
3669
+ const kind = this.deps.registry.metric(series.key)?.kind;
3670
+ if (kind === void 0) continue;
3671
+ out.push({
3672
+ capName: parts.capName,
3673
+ field: parts.field,
3674
+ kind
3675
+ });
3676
+ }
3677
+ return out;
3678
+ }
3679
+ async readRaw(deviceId, series, from, to) {
3680
+ const result = await this.deps.query.query({
3681
+ key: require_capture_engine.capSeriesKey(series.capName, series.field),
3682
+ subject: {
3683
+ kind: "device",
3684
+ deviceId
3685
+ },
3686
+ scope: "",
3687
+ from,
3688
+ to,
3689
+ resolution: "raw",
3690
+ maxPoints: DERIVED_POINTS_MAX
3691
+ });
3692
+ if (result.truncated) this.deps.logger.warn("derived device changes read cut at the raw point cap; the oldest points are left out", {
3693
+ tags: { deviceId },
3694
+ meta: {
3695
+ field: `${series.capName}.${series.field}`,
3696
+ cap: DERIVED_POINTS_MAX
3697
+ }
3698
+ });
3699
+ const first = result.series[0];
3700
+ if (first === void 0) return {
3701
+ before: null,
3702
+ points: []
3703
+ };
3704
+ return {
3705
+ before: first.before === null ? null : {
3706
+ t: first.before.t,
3707
+ value: first.before.value,
3708
+ state: first.before.state
3709
+ },
3710
+ points: first.points.map((p) => ({
3711
+ t: p.t,
3712
+ value: p.last,
3713
+ state: p.state
3714
+ }))
3715
+ };
3716
+ }
3717
+ };
3531
3718
  var deviceTransitionsRecordKinds = require_record_kind.defineRecordKinds({ "device-transitions": {
3532
- schema: require_device_transitions_cap.DeviceTransitionSchema,
3719
+ schema: require_retention_source_cap.DeviceTransitionSchema.refine((row) => row.outcome !== "device", {
3720
+ message: "a derived device change is not stored",
3721
+ path: ["outcome"]
3722
+ }),
3533
3723
  class: "audit",
3534
3724
  database: "events",
3535
- writeTrigger: "the hub kernel delivers a batch of correlated device transitions (commands matched to slice changes, or unmatched)",
3725
+ writeTrigger: "the hub kernel delivers a batch of completed device commands, one row per command with its actor and ok or failed outcome",
3536
3726
  device: "required",
3537
3727
  at: "required",
3538
3728
  indexes: [
@@ -3591,6 +3781,101 @@ function assertOneFilter(filters) {
3591
3781
  function isRejectedRow(err) {
3592
3782
  return err instanceof require_scoped_settings.InvalidRecordError || err instanceof require_scoped_settings.RecordTooLargeError;
3593
3783
  }
3784
+ var SOURCE_CHANGE = 0;
3785
+ var SOURCE_COMMAND = 1;
3786
+ function compareKeys(a, b) {
3787
+ if (a.at !== b.at) return a.at - b.at;
3788
+ if (a.source !== b.source) return a.source - b.source;
3789
+ return compareIds(a.id, b.id);
3790
+ }
3791
+ /**
3792
+ * Ids in the order the store yields one millisecond: insertion order, which for a command id
3793
+ * `<deviceId>:<at>:c<seq>` is its sequence NUMBER, so `c9` precedes `c10`. A plain string compare
3794
+ * puts `c10` first and a cursor on either side of that pair drops a row.
3795
+ */
3796
+ function compareIds(a, b) {
3797
+ const left = a.match(/\d+|\D+/g) ?? [];
3798
+ const right = b.match(/\d+|\D+/g) ?? [];
3799
+ for (let i = 0; i < Math.min(left.length, right.length); i += 1) {
3800
+ const x = left[i] ?? "";
3801
+ const y = right[i] ?? "";
3802
+ if (x === y) continue;
3803
+ if (/^\d/.test(x) && /^\d/.test(y)) return Number(x) - Number(y);
3804
+ return x < y ? -1 : 1;
3805
+ }
3806
+ return left.length - right.length;
3807
+ }
3808
+ function commandKey(row) {
3809
+ return {
3810
+ key: {
3811
+ at: row.at,
3812
+ source: SOURCE_COMMAND,
3813
+ id: row.id
3814
+ },
3815
+ row
3816
+ };
3817
+ }
3818
+ /** A derived change as a row: actor `device`, outcome `device`, an id that names what it is. */
3819
+ function changeKey(deviceId, change) {
3820
+ const id = `chg:${deviceId}:${change.at}:${change.capName}.${change.field}`;
3821
+ return {
3822
+ key: {
3823
+ at: change.at,
3824
+ source: SOURCE_CHANGE,
3825
+ id
3826
+ },
3827
+ row: {
3828
+ id,
3829
+ deviceId,
3830
+ at: change.at,
3831
+ capName: change.capName,
3832
+ field: change.field,
3833
+ from: change.from,
3834
+ to: change.to,
3835
+ outcome: "device",
3836
+ actor: { kind: "device" }
3837
+ }
3838
+ };
3839
+ }
3840
+ function cursorOf(item) {
3841
+ return `${item.key.at}.${item.key.source}.${item.key.id}`;
3842
+ }
3843
+ function parseCursor(cursor) {
3844
+ const first = cursor.indexOf(".");
3845
+ const second = cursor.indexOf(".", first + 1);
3846
+ const at = Number(cursor.slice(0, first));
3847
+ const source = cursor.slice(first + 1, second);
3848
+ if (first < 0 || second < 0 || !Number.isFinite(at) || source !== "0" && source !== "1") throw new Error("invalid cursor");
3849
+ return {
3850
+ at,
3851
+ source: source === "1" ? SOURCE_COMMAND : SOURCE_CHANGE,
3852
+ id: cursor.slice(second + 1)
3853
+ };
3854
+ }
3855
+ /** Take the first `count` rows of several newest-first sources, merged into one newest-first run. */
3856
+ async function takeMerged(sources, count) {
3857
+ const iterators = sources.map((s) => s[Symbol.asyncIterator]());
3858
+ const heads = await Promise.all(iterators.map((it) => it.next()));
3859
+ const out = [];
3860
+ try {
3861
+ while (out.length < count) {
3862
+ let best = -1;
3863
+ for (const [i, head] of heads.entries()) {
3864
+ if (head.done === true) continue;
3865
+ const current = best < 0 ? void 0 : heads[best];
3866
+ if (current === void 0 || current.done === true || compareKeys(head.value.key, current.value.key) > 0) best = i;
3867
+ }
3868
+ const winner = best < 0 ? void 0 : heads[best];
3869
+ if (winner === void 0 || winner.done === true) break;
3870
+ out.push(winner.value);
3871
+ const it = iterators[best];
3872
+ if (it !== void 0) heads[best] = await it.next();
3873
+ }
3874
+ } finally {
3875
+ await Promise.all(iterators.map((it) => it.return?.()));
3876
+ }
3877
+ return out;
3878
+ }
3594
3879
  var TransitionsService = class {
3595
3880
  deps;
3596
3881
  handle = null;
@@ -3679,56 +3964,104 @@ var TransitionsService = class {
3679
3964
  refused
3680
3965
  };
3681
3966
  }
3967
+ /**
3968
+ * One time-ordered page of a device's commands and its own changes, newest first. The cursor
3969
+ * is the sort key of the last row returned (time, source, id), so a page boundary is the same
3970
+ * whichever source the last row came from. `outcome` and every actor kind but `device` select
3971
+ * commands; `actorKind: 'device'` selects the derived changes.
3972
+ */
3682
3973
  async list(input) {
3683
3974
  assertOneFilter({
3684
3975
  capName: input.capName,
3685
3976
  actorKind: input.actorKind,
3686
3977
  outcome: input.outcome
3687
3978
  });
3688
- const paging = {
3689
- desc: true,
3690
- limit: input.limit,
3691
- ...input.cursor !== void 0 ? { cursor: input.cursor } : {}
3979
+ const after = input.cursor === void 0 ? null : parseCursor(input.cursor);
3980
+ const wantCommands = input.actorKind !== "device";
3981
+ const wantChanges = input.outcome === void 0 && (input.actorKind === void 0 || input.actorKind === "device");
3982
+ const sources = [];
3983
+ if (wantCommands) sources.push(this.commandRows(input, after));
3984
+ if (wantChanges) sources.push(await this.changeRows(input, after));
3985
+ const page = await takeMerged(sources, input.limit + 1);
3986
+ const rows = page.slice(0, input.limit);
3987
+ const last = rows[rows.length - 1];
3988
+ return {
3989
+ transitions: rows.map((r) => r.row),
3990
+ nextCursor: page.length > input.limit && last !== void 0 ? cursorOf(last) : null
3692
3991
  };
3693
- const range = rangeOf(input);
3992
+ }
3993
+ /** Stored commands, newest first, strictly after the cursor. */
3994
+ async *commandRows(input, after) {
3694
3995
  const handle = this.kind();
3695
- const page = input.capName !== void 0 ? await handle.query({
3696
- where: {
3697
- deviceId: input.deviceId,
3698
- capName: input.capName,
3699
- ...range
3700
- },
3701
- ...paging
3702
- }) : input.actorKind !== void 0 ? await handle.query({
3703
- where: {
3704
- deviceId: input.deviceId,
3705
- "actor.kind": input.actorKind,
3706
- ...range
3707
- },
3708
- ...paging
3709
- }) : input.outcome !== void 0 ? await handle.query({
3710
- where: {
3711
- deviceId: input.deviceId,
3712
- outcome: input.outcome,
3713
- ...range
3714
- },
3715
- ...paging
3716
- }) : await handle.query({
3717
- deviceId: input.deviceId,
3718
- ...input.atFrom !== void 0 ? { atFrom: input.atFrom } : {},
3719
- ...input.atTo !== void 0 ? { atTo: input.atTo } : {},
3720
- ...paging
3996
+ const atTo = Math.min(input.atTo ?? Number.POSITIVE_INFINITY, after === null ? Infinity : after.at + 1);
3997
+ const range = rangeOf({
3998
+ atFrom: input.atFrom,
3999
+ atTo: Number.isFinite(atTo) ? atTo : void 0
3721
4000
  });
3722
- return {
3723
- transitions: page.records.map((row) => row.data),
3724
- nextCursor: page.nextCursor
3725
- };
4001
+ let cursor;
4002
+ for (;;) {
4003
+ const paging = {
4004
+ desc: true,
4005
+ limit: input.limit + 1,
4006
+ ...cursor !== void 0 ? { cursor } : {}
4007
+ };
4008
+ const page = input.capName !== void 0 ? await handle.query({
4009
+ where: {
4010
+ deviceId: input.deviceId,
4011
+ capName: input.capName,
4012
+ ...range
4013
+ },
4014
+ ...paging
4015
+ }) : input.actorKind !== void 0 ? await handle.query({
4016
+ where: {
4017
+ deviceId: input.deviceId,
4018
+ "actor.kind": input.actorKind,
4019
+ ...range
4020
+ },
4021
+ ...paging
4022
+ }) : input.outcome !== void 0 ? await handle.query({
4023
+ where: {
4024
+ deviceId: input.deviceId,
4025
+ outcome: input.outcome,
4026
+ ...range
4027
+ },
4028
+ ...paging
4029
+ }) : await handle.query({
4030
+ deviceId: input.deviceId,
4031
+ ...input.atFrom !== void 0 ? { atFrom: input.atFrom } : {},
4032
+ ...atTo !== Infinity ? { atTo } : {},
4033
+ ...paging
4034
+ });
4035
+ for (const record of page.records) {
4036
+ const keyed = commandKey(record.data);
4037
+ if (after === null || compareKeys(keyed.key, after) < 0) yield keyed;
4038
+ }
4039
+ if (page.nextCursor === null) return;
4040
+ cursor = page.nextCursor;
4041
+ }
3726
4042
  }
4043
+ /** The device's derived changes, newest first, strictly after the cursor. */
4044
+ async changeRows(input, after) {
4045
+ const now = this.deps.clock();
4046
+ const rows = (await this.deps.derived.read(input.deviceId, {
4047
+ from: input.atFrom ?? 0,
4048
+ to: Math.min(input.atTo ?? now, now),
4049
+ capName: input.capName
4050
+ })).filter((c) => input.atTo === void 0 || c.at < input.atTo).filter((c) => input.atFrom === void 0 || c.at >= input.atFrom).map((c) => changeKey(input.deviceId, c)).toSorted((a, b) => compareKeys(b.key, a.key)).filter((k) => after === null || compareKeys(k.key, after) < 0);
4051
+ return (async function* () {
4052
+ for (const row of rows) yield row;
4053
+ })();
4054
+ }
4055
+ /** The fleet page: stored commands only. A device's own changes are derived per device. */
3727
4056
  async listRecent(input) {
3728
4057
  assertOneFilter({
3729
4058
  actorKind: input.actorKind,
3730
4059
  outcome: input.outcome
3731
4060
  });
4061
+ if (input.actorKind === "device") return {
4062
+ transitions: [],
4063
+ nextCursor: null
4064
+ };
3732
4065
  const paging = {
3733
4066
  desc: true,
3734
4067
  limit: input.limit,
@@ -3814,6 +4147,138 @@ var TransitionsService = class {
3814
4147
  }
3815
4148
  };
3816
4149
  //#endregion
4150
+ //#region src/builtins/device-history/retention/device-raw-overrides.ts
4151
+ function positive(value) {
4152
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : void 0;
4153
+ }
4154
+ function parseRawOverride(record) {
4155
+ const rawDays = positive(record["rawDays"]);
4156
+ const rawDiscreteDays = positive(record["rawDiscreteDays"]);
4157
+ return {
4158
+ ...rawDays === void 0 ? {} : { rawDays },
4159
+ ...rawDiscreteDays === void 0 ? {} : { rawDiscreteDays }
4160
+ };
4161
+ }
4162
+ /** Every device with an override, one read for the ids and one batch for the values. Throws when the store cannot be asked. */
4163
+ async function readRawOverrides(settings) {
4164
+ const ids = await settings.listDeviceStoreIds();
4165
+ if (ids.length === 0) return /* @__PURE__ */ new Map();
4166
+ const rows = await settings.readDeviceStoreBatch(ids);
4167
+ const out = /* @__PURE__ */ new Map();
4168
+ for (const [deviceId, record] of rows) {
4169
+ const parsed = parseRawOverride(record);
4170
+ if (parsed.rawDays !== void 0 || parsed.rawDiscreteDays !== void 0) out.set(deviceId, parsed);
4171
+ }
4172
+ return out;
4173
+ }
4174
+ //#endregion
4175
+ //#region src/builtins/device-history/retention/retention-knobs.ts
4176
+ var DEVICE_HISTORY_ADDON_ID = "device-history";
4177
+ var CAP = "device-history";
4178
+ var RAW_KEYS = ["rawDays", "rawDiscreteDays"];
4179
+ var SPECS = [
4180
+ {
4181
+ key: "rawDays",
4182
+ label: "Raw points of measurements",
4183
+ min: 1,
4184
+ max: 60,
4185
+ zeroMeansForever: false
4186
+ },
4187
+ {
4188
+ key: "rawDiscreteDays",
4189
+ label: "Raw points of states",
4190
+ min: 1,
4191
+ max: 365,
4192
+ zeroMeansForever: false
4193
+ },
4194
+ {
4195
+ key: "fiveMinuteDays",
4196
+ label: "5-minute rollup",
4197
+ min: 1,
4198
+ max: 365,
4199
+ zeroMeansForever: false
4200
+ },
4201
+ {
4202
+ key: "hourDays",
4203
+ label: "1-hour rollup",
4204
+ min: 7,
4205
+ max: 3650,
4206
+ zeroMeansForever: false
4207
+ },
4208
+ {
4209
+ key: "dayDays",
4210
+ label: "1-day rollup",
4211
+ min: 0,
4212
+ max: 36500,
4213
+ zeroMeansForever: true
4214
+ },
4215
+ {
4216
+ key: "transitionDays",
4217
+ label: "Device commands",
4218
+ min: 7,
4219
+ max: 3650,
4220
+ zeroMeansForever: false
4221
+ }
4222
+ ];
4223
+ function isRawKey(key) {
4224
+ return key === "rawDays" || key === "rawDiscreteDays";
4225
+ }
4226
+ function knobFor(spec, policy, deviceId, override) {
4227
+ const cluster = policy[spec.key];
4228
+ const base = {
4229
+ id: `${DEVICE_HISTORY_ADDON_ID}:${spec.key}`,
4230
+ owner: DEVICE_HISTORY_ADDON_ID,
4231
+ key: spec.key,
4232
+ labelKey: `retention.${DEVICE_HISTORY_ADDON_ID}.${spec.key}`,
4233
+ label: spec.label,
4234
+ group: "history",
4235
+ unit: "days",
4236
+ defaultValue: require_retention.DEFAULT_RETENTION[spec.key],
4237
+ min: spec.min,
4238
+ max: spec.max,
4239
+ zeroMeansForever: spec.zeroMeansForever
4240
+ };
4241
+ if (!isRawKey(spec.key)) return {
4242
+ ...base,
4243
+ value: cluster,
4244
+ scope: "cluster",
4245
+ write: {
4246
+ method: "updateScopedSettings",
4247
+ addonId: DEVICE_HISTORY_ADDON_ID,
4248
+ cap: CAP,
4249
+ scope: "cluster",
4250
+ key: spec.key
4251
+ }
4252
+ };
4253
+ const own = override?.[spec.key];
4254
+ return {
4255
+ ...base,
4256
+ value: own ?? cluster,
4257
+ scope: "device",
4258
+ ...deviceId === void 0 ? {} : { deviceId },
4259
+ inherited: own === void 0,
4260
+ inheritedValue: cluster,
4261
+ write: deviceId === void 0 ? {
4262
+ method: "updateScopedSettings",
4263
+ addonId: DEVICE_HISTORY_ADDON_ID,
4264
+ cap: CAP,
4265
+ scope: "cluster",
4266
+ key: spec.key
4267
+ } : {
4268
+ method: "updateDeviceSettings",
4269
+ addonId: DEVICE_HISTORY_ADDON_ID,
4270
+ key: spec.key
4271
+ }
4272
+ };
4273
+ }
4274
+ /**
4275
+ * Without a device: every knob, a raw window listed with the cluster value it defaults to.
4276
+ * With one: only the device-scoped knobs, each saying whether the device follows the cluster.
4277
+ */
4278
+ function deviceHistoryKnobs(policy, deviceId, override) {
4279
+ return (deviceId === void 0 ? SPECS : SPECS.filter((s) => RAW_KEYS.some((k) => k === s.key))).map((spec) => knobFor(spec, policy, deviceId, override));
4280
+ }
4281
+ //#endregion
3817
4282
  //#region src/builtins/device-history/device-history.addon.ts
3818
4283
  /**
3819
4284
  * device-history — the ONE mechanism for numeric history (D850). Hub-only, its
@@ -3838,6 +4303,7 @@ var TransitionsService = class {
3838
4303
  */
3839
4304
  var DEFAULT_CONFIG = {
3840
4305
  rawDays: require_retention.DEFAULT_RETENTION.rawDays,
4306
+ rawDiscreteDays: require_retention.DEFAULT_RETENTION.rawDiscreteDays,
3841
4307
  fiveMinuteDays: require_retention.DEFAULT_RETENTION.fiveMinuteDays,
3842
4308
  hourDays: require_retention.DEFAULT_RETENTION.hourDays,
3843
4309
  dayDays: 0,
@@ -3878,10 +4344,10 @@ var DeviceHistoryAddon = class extends require_base_addon.BaseAddon {
3878
4344
  const raw = ctx.kernel?.localNodeId ?? "hub";
3879
4345
  const nodeId = raw.includes("/") ? raw.split("/")[0] ?? raw : raw;
3880
4346
  for (const capName of [
3881
- require_device_transitions_cap.deviceHistoryCapability.name,
3882
- require_device_transitions_cap.deviceRunsCapability.name,
3883
- require_device_transitions_cap.deviceTransitionsCapability.name,
3884
- require_device_transitions_cap.deviceRemovalHookCapability.name
4347
+ require_retention_source_cap.deviceHistoryCapability.name,
4348
+ require_retention_source_cap.deviceRunsCapability.name,
4349
+ require_retention_source_cap.deviceTransitionsCapability.name,
4350
+ require_retention_source_cap.deviceRemovalHookCapability.name
3885
4351
  ]) try {
3886
4352
  require_base_addon.emitReadiness(ctx.eventBus, {
3887
4353
  capName,
@@ -3905,6 +4371,7 @@ var DeviceHistoryAddon = class extends require_base_addon.BaseAddon {
3905
4371
  const c = this.config;
3906
4372
  return {
3907
4373
  rawDays: positiveOr(c.rawDays, require_retention.DEFAULT_RETENTION.rawDays),
4374
+ rawDiscreteDays: positiveOr(c.rawDiscreteDays, require_retention.DEFAULT_RETENTION.rawDiscreteDays),
3908
4375
  fiveMinuteDays: positiveOr(c.fiveMinuteDays, require_retention.DEFAULT_RETENTION.fiveMinuteDays),
3909
4376
  hourDays: positiveOr(c.hourDays, require_retention.DEFAULT_RETENTION.hourDays),
3910
4377
  dayDays: positiveOr(c.dayDays, 0) > 0 ? c.dayDays : null,
@@ -3983,6 +4450,11 @@ var DeviceHistoryAddon = class extends require_base_addon.BaseAddon {
3983
4450
  record: async (input) => this.requireTransitions().record(callerOf(input), input.transitions),
3984
4451
  getStatus: async () => this.requireTransitions().status()
3985
4452
  };
4453
+ const retentionSource = { listRetentionKnobs: async (input) => {
4454
+ const deviceId = input.deviceId;
4455
+ const stored = deviceId === void 0 ? void 0 : await this.ctx.settings?.readDeviceStore(deviceId);
4456
+ return deviceHistoryKnobs(this.retention(), deviceId, stored === void 0 ? void 0 : parseRawOverride(stored));
4457
+ } };
3986
4458
  this.emitStoreReadiness("starting");
3987
4459
  this.ctx.logger.info("Initialized — device-history provider registered (store opens on hub link)");
3988
4460
  const removalHook = {
@@ -4028,20 +4500,24 @@ var DeviceHistoryAddon = class extends require_base_addon.BaseAddon {
4028
4500
  this.removalHook = removalHook;
4029
4501
  return [
4030
4502
  {
4031
- capability: require_device_transitions_cap.deviceHistoryCapability,
4503
+ capability: require_retention_source_cap.deviceHistoryCapability,
4032
4504
  provider
4033
4505
  },
4034
4506
  {
4035
- capability: require_device_transitions_cap.deviceRunsCapability,
4507
+ capability: require_retention_source_cap.deviceRunsCapability,
4036
4508
  provider: runsFacade
4037
4509
  },
4038
4510
  {
4039
- capability: require_device_transitions_cap.deviceTransitionsCapability,
4511
+ capability: require_retention_source_cap.deviceTransitionsCapability,
4040
4512
  provider: transitionsFacade
4041
4513
  },
4042
4514
  {
4043
- capability: require_device_transitions_cap.deviceRemovalHookCapability,
4515
+ capability: require_retention_source_cap.deviceRemovalHookCapability,
4044
4516
  provider: removalHook
4517
+ },
4518
+ {
4519
+ capability: require_retention_source_cap.retentionSourceCapability,
4520
+ provider: retentionSource
4045
4521
  }
4046
4522
  ];
4047
4523
  }
@@ -4086,9 +4562,11 @@ var DeviceHistoryAddon = class extends require_base_addon.BaseAddon {
4086
4562
  const runsOpened = await runs.open();
4087
4563
  this.runsProvider = runsOpened.provider;
4088
4564
  this.runs = runs;
4089
- const transitions = await this.openTransitions();
4565
+ const transitions = await this.openTransitions(runtime);
4090
4566
  this.transitions = transitions;
4091
4567
  runtime.retentionJob.attachTransitions(transitions);
4568
+ const settings = this.ctx.settings;
4569
+ if (settings !== void 0) runtime.retentionJob.attachDeviceOverrides(() => readRawOverrides(settings));
4092
4570
  this.unsubscribe = this.ctx.eventBus.subscribe({ category: require_base_addon.EventCategory.DeviceStateChanged }, (event) => {
4093
4571
  const data = require_capture_engine.parseConfirmedStateChanged(event.data);
4094
4572
  if (data === null) return;
@@ -4142,14 +4620,19 @@ var DeviceHistoryAddon = class extends require_base_addon.BaseAddon {
4142
4620
  clock: () => Date.now()
4143
4621
  });
4144
4622
  }
4145
- async openTransitions() {
4623
+ async openTransitions(runtime) {
4146
4624
  const { ctx } = this;
4147
4625
  const records = ctx.records;
4148
4626
  if (records === void 0) throw new Error("device-history needs ctx.records on the hub for the transition ledger");
4149
4627
  const transitions = new TransitionsService({
4150
4628
  records,
4151
4629
  logger: ctx.logger,
4152
- clock: () => Date.now()
4630
+ clock: () => Date.now(),
4631
+ derived: new DerivedChanges(new HistoryChangePort({
4632
+ registry: runtime.registry,
4633
+ query: runtime.query,
4634
+ logger: ctx.logger
4635
+ }), () => Date.now())
4153
4636
  });
4154
4637
  await transitions.open();
4155
4638
  return transitions;
@@ -4171,22 +4654,59 @@ var DeviceHistoryAddon = class extends require_base_addon.BaseAddon {
4171
4654
  scopedSettingsCapabilities() {
4172
4655
  return ["device-history"];
4173
4656
  }
4657
+ /**
4658
+ * D865: the two raw windows a device may override, in the standard per-device store. Unset
4659
+ * means the device follows the cluster value (shown by the retention catalog).
4660
+ */
4661
+ deviceSettingsSchema() {
4662
+ return this.schema({ sections: [{
4663
+ id: "retention",
4664
+ title: "History retention",
4665
+ description: "Leave a field empty to follow the system-wide window (Data → Retention).",
4666
+ columns: 2,
4667
+ fields: [{
4668
+ type: "number",
4669
+ key: "rawDays",
4670
+ label: "Raw points of measurements (days)",
4671
+ min: 1,
4672
+ max: 60,
4673
+ step: 1
4674
+ }, {
4675
+ type: "number",
4676
+ key: "rawDiscreteDays",
4677
+ label: "Raw points of states (days)",
4678
+ min: 1,
4679
+ max: 365,
4680
+ step: 1
4681
+ }]
4682
+ }] });
4683
+ }
4174
4684
  async clusterSettingsSchema(cap) {
4175
4685
  if (cap !== "device-history") return null;
4176
4686
  return this.schema({ sections: [{
4177
4687
  id: "retention",
4178
4688
  title: "Retention",
4179
- description: "How long each tier is kept. The raw points feed the 5-minute rollup; the 1-day tier is kept forever unless a number of days is set. Device transitions (who changed what) are kept for their own number of days.",
4689
+ description: "How long each tier is kept. The raw points feed the 5-minute rollup, and a state series keeps them longer than a measurement; the 1-day tier is kept forever unless a number of days is set. Device transitions (who changed what) are kept for their own number of days.",
4180
4690
  columns: 2,
4181
4691
  fields: [
4182
4692
  {
4183
4693
  type: "number",
4184
4694
  key: "rawDays",
4185
- label: "Raw points (days)",
4695
+ label: "Raw points of measurements (days)",
4696
+ description: "Gauges and counters: temperature, power, battery level.",
4186
4697
  min: 1,
4187
4698
  max: 60,
4188
4699
  step: 1
4189
4700
  },
4701
+ {
4702
+ type: "number",
4703
+ key: "rawDiscreteDays",
4704
+ label: "Raw points of states (days)",
4705
+ description: "Booleans and enums: on/off, open/closed, modes. These are the changes the Interactions view lists, so they are kept longer.",
4706
+ min: 1,
4707
+ max: 365,
4708
+ step: 1
4709
+ },
4190
4710
  {
4191
4711
  type: "number",
4192
4712
  key: "fiveMinuteDays",