@camstack/addon-pipeline 1.2.88 → 1.2.90

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 (28) hide show
  1. package/dist/{addon-utils-B8tPjc8F.js → addon-utils-yc-chXuC.js} +1 -1
  2. package/dist/audio-analyzer/index.js +2 -2
  3. package/dist/audio-analyzer/index.mjs +1 -1
  4. package/dist/detection-pipeline/index.js +4 -4
  5. package/dist/detection-pipeline/index.mjs +2 -2
  6. package/dist/{dist-DCnOL8bz.mjs → dist-BJYwWEZ9.mjs} +54 -11
  7. package/dist/{dist-CMHxEH6s.js → dist-n4Nbnxx5.js} +54 -11
  8. package/dist/{event-loop-stall-monitor-BLJ0S3w2.mjs → event-loop-stall-monitor-BiPffk0D.mjs} +1 -1
  9. package/dist/{event-loop-stall-monitor-asYJVMb1.js → event-loop-stall-monitor-DG7QY00k.js} +1 -1
  10. package/dist/{lazy-sharp-1s0ymn1V.js → lazy-sharp-EoSOHgoj.js} +1 -1
  11. package/dist/motion-wasm/index.js +2 -2
  12. package/dist/motion-wasm/index.mjs +1 -1
  13. package/dist/pipeline-runner/index.js +4 -4
  14. package/dist/pipeline-runner/index.mjs +3 -3
  15. package/dist/recorder/index.js +735 -67
  16. package/dist/recorder/index.mjs +734 -66
  17. package/dist/session-decode/decode-worker-child.js +2 -2
  18. package/dist/session-decode/decode-worker-child.mjs +1 -1
  19. package/dist/stream-broker/_stub.js +1 -1
  20. package/dist/stream-broker/{_virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-BeSS5V2H.mjs → _virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-CsrMZYLB.mjs} +1 -1
  21. package/dist/stream-broker/{_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-B2scqOH7.mjs → _virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-DNSELPGi.mjs} +1 -1
  22. package/dist/stream-broker/{hostInit-BC9QIyzJ.mjs → hostInit-DoFovQmc.mjs} +1 -1
  23. package/dist/stream-broker/index.js +3 -2
  24. package/dist/stream-broker/index.mjs +3 -2
  25. package/dist/stream-broker/remoteEntry.js +1 -1
  26. package/dist/{worker-protocol-BzvA7rx4.js → worker-protocol-DjuxhGo3.js} +1 -1
  27. package/dist/{worker-protocol-CBTyzyds.mjs → worker-protocol-XZ1e36rM.mjs} +1 -1
  28. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
- const require_dist = require("../dist-CMHxEH6s.js");
1
+ const require_dist = require("../dist-n4Nbnxx5.js");
2
2
  const require_hub_hostname = require("../hub-hostname-DAJXlOgV.js");
3
- const require_addon_utils = require("../addon-utils-B8tPjc8F.js");
3
+ const require_addon_utils = require("../addon-utils-yc-chXuC.js");
4
4
  let node_crypto = require("node:crypto");
5
5
  let node_child_process = require("node:child_process");
6
6
  let node_path = require("node:path");
@@ -507,14 +507,14 @@ function parseSegmentPath(relPath) {
507
507
  //#region src/recorder/recording-index.ts
508
508
  /** Hour bucket, the granularity the path convention (`.../YYYY/MM/DD/HH/`)
509
509
  * already gives us — so "have I walked this?" costs one Set lookup. */
510
- var HOUR_MS$4 = 36e5;
510
+ var HOUR_MS$5 = 36e5;
511
511
  /** A device walked end-to-end. Cheaper and clearer than inserting every hour of
512
512
  * the retention window into the set. */
513
513
  var ALL_HOURS = Symbol("all-hours");
514
514
  /** The hour buckets a half-open span touches. */
515
515
  function* hourBuckets(fromMs, toMs) {
516
- const first = Math.floor(fromMs / HOUR_MS$4) * HOUR_MS$4;
517
- for (let h = first; h < toMs; h += HOUR_MS$4) yield h;
516
+ const first = Math.floor(fromMs / HOUR_MS$5) * HOUR_MS$5;
517
+ for (let h = first; h < toMs; h += HOUR_MS$5) yield h;
518
518
  }
519
519
  var RecordingIndex = class {
520
520
  byDevice = /* @__PURE__ */ new Map();
@@ -630,7 +630,7 @@ var RecordingIndex = class {
630
630
  });
631
631
  }
632
632
  this.invalidateSorted(deviceId);
633
- this.markHydrated(deviceId, hourStartMs, hourStartMs + HOUR_MS$4);
633
+ this.markHydrated(deviceId, hourStartMs, hourStartMs + HOUR_MS$5);
634
634
  }
635
635
  /** Insert/replace one finalized segment (idempotent by path). */
636
636
  addSegment(s) {
@@ -1371,6 +1371,8 @@ var SegmentStore = class SegmentStore {
1371
1371
  if (removed.length > 0) {
1372
1372
  this.deps.index.removeSegments(removed);
1373
1373
  this.notifyEvicted(rows);
1374
+ const gone = new Set(removed);
1375
+ this.deps.onDeindexed?.(rows.filter((r) => gone.has(r.path)));
1374
1376
  }
1375
1377
  await this.pruneEmptyAncestors(location, [...byBucket.keys()]);
1376
1378
  this.deps.logger.info("recorder: eviction round complete", { meta: {
@@ -1936,7 +1938,7 @@ async function profileHasFootage(root, deviceId, profile) {
1936
1938
  //#endregion
1937
1939
  //#region src/recorder/addon/calendar-index.ts
1938
1940
  var DAY_MS$3 = 864e5;
1939
- var HOUR_MS$3 = 36e5;
1941
+ var HOUR_MS$4 = 36e5;
1940
1942
  /**
1941
1943
  * UTC ms → the UTC epoch of LOCAL midnight of the day it falls in.
1942
1944
  *
@@ -2109,7 +2111,7 @@ var CalendarIndex = class {
2109
2111
  }));
2110
2112
  const days = /* @__PURE__ */ new Set();
2111
2113
  for (const { day, hours } of perDay) for (const h of hours) {
2112
- const at = day + h * HOUR_MS$3;
2114
+ const at = day + h * HOUR_MS$4;
2113
2115
  if (at < fromMs || at >= toMs) continue;
2114
2116
  days.add(localDayStart(at, tzOffsetMinutes));
2115
2117
  }
@@ -2171,7 +2173,7 @@ var CalendarIndex = class {
2171
2173
  const day = utcDayOf(utcDayMs);
2172
2174
  for (let h = 0; h < 24; h++) {
2173
2175
  await this.awaitQuiet();
2174
- await this.segmentsOf(deviceId, profile, day + h * HOUR_MS$3, true);
2176
+ await this.segmentsOf(deviceId, profile, day + h * HOUR_MS$4, true);
2175
2177
  }
2176
2178
  }
2177
2179
  /**
@@ -2192,10 +2194,10 @@ var CalendarIndex = class {
2192
2194
  */
2193
2195
  async segmentsOfChecked(deviceId, profile, hourStartMs, background = false) {
2194
2196
  if (!background) this.lastForegroundAt = this.deps.now?.() ?? Date.now();
2195
- const hour = Math.floor(hourStartMs / HOUR_MS$3) * HOUR_MS$3;
2197
+ const hour = Math.floor(hourStartMs / HOUR_MS$4) * HOUR_MS$4;
2196
2198
  const key = `${deviceId}:${profile}:${hour}`;
2197
2199
  const now = this.deps.now?.() ?? Date.now();
2198
- const isCurrentHour = hour === Math.floor(now / HOUR_MS$3) * HOUR_MS$3;
2200
+ const isCurrentHour = hour === Math.floor(now / HOUR_MS$4) * HOUR_MS$4;
2199
2201
  const cached = this.hourSegs.get(key);
2200
2202
  if (cached !== void 0) {
2201
2203
  if (!isCurrentHour) return {
@@ -2259,7 +2261,7 @@ var CalendarIndex = class {
2259
2261
  */
2260
2262
  async dayRanges(deviceId, profile, utcDayMs) {
2261
2263
  const day = utcDayOf(utcDayMs);
2262
- return mergeRanges((await mapBounded(Array.from({ length: 24 }, (_, h) => day + h * HOUR_MS$3), READ_CONCURRENCY, (hour) => this.segmentsOf(deviceId, profile, hour))).flat(), RANGE_MERGE_GAP_MS$1);
2264
+ return mergeRanges((await mapBounded(Array.from({ length: 24 }, (_, h) => day + h * HOUR_MS$4), READ_CONCURRENCY, (hour) => this.segmentsOf(deviceId, profile, hour))).flat(), RANGE_MERGE_GAP_MS$1);
2263
2265
  }
2264
2266
  /**
2265
2267
  * Warm the days either side of the one being viewed, in the BACKGROUND.
@@ -2437,7 +2439,7 @@ function activeModeForConfig(config) {
2437
2439
  }
2438
2440
  //#endregion
2439
2441
  //#region src/recorder/addon/directory-handler.ts
2440
- var HOUR_MS$2 = 36e5;
2442
+ var HOUR_MS$3 = 36e5;
2441
2443
  /** A window wider than this is refused: it is a client bug, and answering it
2442
2444
  * would walk the archive on the read path — the thing this replaces. */
2443
2445
  var MAX_WINDOW_MS = 32 * 864e5;
@@ -2484,9 +2486,9 @@ function withDirectoryRoute(deps, next) {
2484
2486
  return;
2485
2487
  }
2486
2488
  const rows = [];
2487
- const firstHour = Math.floor(fromMs / HOUR_MS$2) * HOUR_MS$2;
2489
+ const firstHour = Math.floor(fromMs / HOUR_MS$3) * HOUR_MS$3;
2488
2490
  let hydrated = true;
2489
- for (let hour = firstHour; hour < toMs; hour += HOUR_MS$2) {
2491
+ for (let hour = firstHour; hour < toMs; hour += HOUR_MS$3) {
2490
2492
  const { segments, complete } = await deps.source.segmentsOfChecked(deviceId, profile, hour);
2491
2493
  if (!complete) hydrated = false;
2492
2494
  for (const s of segments) {
@@ -2634,6 +2636,28 @@ function withStreamTimeDenseRanges(input) {
2634
2636
  }
2635
2637
  //#endregion
2636
2638
  //#region src/recorder/addon/export-ffmpeg-args.ts
2639
+ /**
2640
+ * ffmpeg argv builder for recording EXPORTS (render a footage range → one MP4).
2641
+ *
2642
+ * Input is the export's OWN source playlist for the range — a per-export file
2643
+ * whose segment URIs are absolute on-disk paths (`export-source-playlist.ts`).
2644
+ * It is deliberately NOT the playback manifest's `localMasterPath`: those URIs
2645
+ * resolve only through the data-plane's multi-root search, which ffmpeg does not
2646
+ * have. Output is a single faststart MP4. Three shapes, from `ExportOptions`:
2647
+ * - 1× + audio → pure remux (`-c copy`), zero re-encode.
2648
+ * - speed ≠ 1 → `setpts=PTS/speed` video re-encode; audio kept
2649
+ * only for 0.5–2× (single `atempo`), else dropped.
2650
+ * - timelapse → sample one frame per `everyMs`, output at
2651
+ * `outputFps`, always silent.
2652
+ * Distinct from `buildPassthroughArgs` (live segmentation) — do not conflate.
2653
+ *
2654
+ * A **timelapse of a long HLS concat** is two argv sets, not one. Pass 1
2655
+ * (`buildConcatPassArgs`) remuxes the playlist to a single mpegts with `-c
2656
+ * copy`. Pass 2 (`buildExportArgs` with that file as `-i`) then selects
2657
+ * frames. Selecting WHILE walking `#EXT-X-DISCONTINUITY` (one per 10s
2658
+ * segment from `-reset_timestamps`) is what parked a 30h salone job at
2659
+ * `rendering 0%` / 0% CPU — ffmpeg never left the concat demuxer.
2660
+ */
2637
2661
  /** Widest [min,max] speed a single ffmpeg `atempo` filter accepts. Outside this
2638
2662
  * band audio is dropped rather than chained (kept simple + predictable). */
2639
2663
  var ATEMPO_MIN = .5;
@@ -2697,6 +2721,29 @@ function selectPredicate(everySec, dense) {
2697
2721
  if (dense === void 0 || dense.ranges.length === 0) return `${seed}+${elapsed(everySec)}`;
2698
2722
  return `${seed}+if(${dense.ranges.map((r) => `between(t,${sec(r.fromSec)},${sec(r.toSec)})`).join("+")},${elapsed(dense.everyMs / 1e3)},${elapsed(everySec)})`;
2699
2723
  }
2724
+ /**
2725
+ * Sidecar for timelapse pass 1: same directory as the final MP4, `.concat.ts`.
2726
+ * mpegts (not MP4) because `-c copy` across HLS discontinuities is reliable
2727
+ * in a transport stream and often is not in a fragmented MP4.
2728
+ */
2729
+ function concatPassOutPath(outPath) {
2730
+ const dir = node_path.default.dirname(outPath);
2731
+ const base = node_path.default.basename(outPath, node_path.default.extname(outPath));
2732
+ return node_path.default.join(dir, `${base}.concat.ts`);
2733
+ }
2734
+ /** Pass 1 of a timelapse: remux the HLS playlist into one continuous file. */
2735
+ function buildConcatPassArgs(input) {
2736
+ return [
2737
+ "-i",
2738
+ input.inputPlaylist,
2739
+ "-c",
2740
+ "copy",
2741
+ "-an",
2742
+ "-f",
2743
+ "mpegts",
2744
+ input.outPath
2745
+ ];
2746
+ }
2700
2747
  /** Build the ffmpeg argv for one export render. */
2701
2748
  function buildExportArgs(input) {
2702
2749
  const { inputPlaylist, outPath, options } = input;
@@ -2983,12 +3030,6 @@ var ExportEngine = class {
2983
3030
  /** Spawn ffmpeg for one render and resolve when it has exited + finalized. */
2984
3031
  async spawnRender(id, rec, playlist, options) {
2985
3032
  const outPath = this.deps.outPath(rec);
2986
- const args = buildExportArgs({
2987
- inputPlaylist: playlist,
2988
- outPath,
2989
- options
2990
- });
2991
- const totalSec = expectedOutSeconds(rec);
2992
3033
  this.deps.logger.info("export render starting", {
2993
3034
  tags: { deviceId: rec.deviceId },
2994
3035
  meta: {
@@ -2999,11 +3040,67 @@ var ExportEngine = class {
2999
3040
  outPath
3000
3041
  }
3001
3042
  });
3043
+ if (options.timelapse) {
3044
+ await this.spawnTimelapse(id, rec, playlist, outPath, options);
3045
+ return;
3046
+ }
3047
+ const args = buildExportArgs({
3048
+ inputPlaylist: playlist,
3049
+ outPath,
3050
+ options
3051
+ });
3052
+ const ran = await this.runFfmpeg(id, args, {
3053
+ totalSec: expectedOutSeconds(rec),
3054
+ floor: 0,
3055
+ ceil: 99
3056
+ });
3057
+ await this.finish(id, ran.code, outPath, ran.stderrTail);
3058
+ }
3059
+ /**
3060
+ * Concat the HLS pieces into one continuous file, THEN select frames.
3061
+ * The select filter on a discontinuity-heavy playlist is what parked a
3062
+ * 30h job at `rendering 0%` — pass 1 is `-c copy` into mpegts so pass 2
3063
+ * sees a single timestamp line.
3064
+ */
3065
+ async spawnTimelapse(id, rec, playlist, outPath, options) {
3066
+ const concatPath = concatPassOutPath(outPath);
3067
+ try {
3068
+ const concatArgs = buildConcatPassArgs({
3069
+ inputPlaylist: playlist,
3070
+ outPath: concatPath
3071
+ });
3072
+ const concatRan = await this.runFfmpeg(id, concatArgs, {
3073
+ totalSec: Math.max(.001, (rec.toMs - rec.fromMs) / 1e3),
3074
+ floor: 0,
3075
+ ceil: 49
3076
+ });
3077
+ if (this.cancelled.has(id) || concatRan.code !== 0) {
3078
+ await this.finish(id, concatRan.code, concatPath, concatRan.stderrTail);
3079
+ return;
3080
+ }
3081
+ const selectArgs = buildExportArgs({
3082
+ inputPlaylist: concatPath,
3083
+ outPath,
3084
+ options
3085
+ });
3086
+ const selectRan = await this.runFfmpeg(id, selectArgs, {
3087
+ totalSec: expectedOutSeconds(rec),
3088
+ floor: 50,
3089
+ ceil: 99
3090
+ });
3091
+ await this.finish(id, selectRan.code, outPath, selectRan.stderrTail);
3092
+ } finally {
3093
+ try {
3094
+ await this.deps.removeFile(concatPath);
3095
+ } catch {}
3096
+ }
3097
+ }
3098
+ runFfmpeg(id, args, progress) {
3002
3099
  this.deps.logger.debug("export ffmpeg argv", { meta: {
3003
3100
  exportId: id,
3004
3101
  args
3005
3102
  } });
3006
- await new Promise((resolve) => {
3103
+ return new Promise((resolve) => {
3007
3104
  const proc = this.deps.spawn(this.deps.ffmpegPath ?? "ffmpeg", args);
3008
3105
  this.activeProc = proc;
3009
3106
  const stderrTail = [];
@@ -3018,7 +3115,8 @@ var ExportEngine = class {
3018
3115
  const m = TIME_RE.exec(line);
3019
3116
  if (m) {
3020
3117
  const sec = Number(m[1]) * 3600 + Number(m[2]) * 60 + Number(m[3]);
3021
- const pct = Math.min(99, Math.round(sec / totalSec * 100));
3118
+ const span = progress.ceil - progress.floor;
3119
+ const pct = Math.min(progress.ceil, progress.floor + Math.round(sec / progress.totalSec * span));
3022
3120
  patchExport(this.deps.state, id, { progressPct: pct });
3023
3121
  this.deps.emit(require_dist.EventCategory.RecordingExportProgress, {
3024
3122
  exportId: id,
@@ -3029,7 +3127,10 @@ var ExportEngine = class {
3029
3127
  });
3030
3128
  proc.on("exit", (code) => {
3031
3129
  this.activeProc = null;
3032
- this.finish(id, typeof code === "number" ? code : null, outPath, stderrTail).then(resolve);
3130
+ resolve({
3131
+ code: typeof code === "number" ? code : null,
3132
+ stderrTail
3133
+ });
3033
3134
  });
3034
3135
  });
3035
3136
  }
@@ -3080,9 +3181,15 @@ var ExportEngine = class {
3080
3181
  outPath,
3081
3182
  bytes
3082
3183
  } });
3184
+ const rec = await getExportRecord(this.deps.state, id);
3083
3185
  this.deps.emit(require_dist.EventCategory.RecordingExportCompleted, {
3084
3186
  exportId: id,
3085
- fileBytes: bytes
3187
+ fileBytes: bytes,
3188
+ ...rec !== null ? {
3189
+ deviceId: rec.deviceId,
3190
+ ...rec.options.title !== void 0 ? { title: rec.options.title } : {},
3191
+ ...rec.options.notifyTargetIds !== void 0 && rec.options.notifyTargetIds.length > 0 ? { notifyTargetIds: rec.options.notifyTargetIds } : {}
3192
+ } : {}
3086
3193
  });
3087
3194
  }
3088
3195
  async fail(id, error) {
@@ -5081,7 +5188,7 @@ function hourDirRelPath(deviceId, profile, hourStartMs) {
5081
5188
  const p2 = (n) => String(n).padStart(2, "0");
5082
5189
  return `${deviceId}/${profile}/${d.getUTCFullYear()}/${p2(d.getUTCMonth() + 1)}/${p2(d.getUTCDate())}/${p2(d.getUTCHours())}`;
5083
5190
  }
5084
- var HOUR_MS$1 = 36e5;
5191
+ var HOUR_MS$2 = 36e5;
5085
5192
  /**
5086
5193
  * Hydrate ONLY the hours a window spans, instead of walking the archive.
5087
5194
  *
@@ -5116,8 +5223,8 @@ async function hydrateWindowFromStorage(index, deviceId, fromMs, toMs, locations
5116
5223
  });
5117
5224
  continue;
5118
5225
  }
5119
- const first = Math.floor(fromMs / HOUR_MS$1) * HOUR_MS$1;
5120
- for (let hour = first; hour < toMs; hour += HOUR_MS$1) for (const profile of profiles) {
5226
+ const first = Math.floor(fromMs / HOUR_MS$2) * HOUR_MS$2;
5227
+ for (let hour = first; hour < toMs; hour += HOUR_MS$2) for (const profile of profiles) {
5121
5228
  const rel = hourDirRelPath(deviceId, profile, hour);
5122
5229
  let files;
5123
5230
  try {
@@ -5759,8 +5866,19 @@ function startPeriodicPass(deps) {
5759
5866
  isRunning: () => startedAt !== null
5760
5867
  };
5761
5868
  }
5762
- //#endregion
5763
- //#region src/recorder/addon/readiness-restore.ts
5869
+ async function runBounded(items, concurrency, worker) {
5870
+ if (items.length === 0) return;
5871
+ const queue = [...items];
5872
+ const lanes = Math.max(1, Math.min(concurrency, queue.length));
5873
+ const lane = async () => {
5874
+ for (;;) {
5875
+ const item = queue.shift();
5876
+ if (item === void 0) return;
5877
+ await worker(item);
5878
+ }
5879
+ };
5880
+ await Promise.all(Array.from({ length: lanes }, () => lane()));
5881
+ }
5764
5882
  var ReadinessRestore = class {
5765
5883
  deps;
5766
5884
  pending = /* @__PURE__ */ new Map();
@@ -5770,6 +5888,9 @@ var ReadinessRestore = class {
5770
5888
  constructor(deps) {
5771
5889
  this.deps = deps;
5772
5890
  }
5891
+ get concurrency() {
5892
+ return Math.max(1, this.deps.concurrency ?? 8);
5893
+ }
5773
5894
  /** Seed the pending set, subscribe to readiness, and drain once immediately
5774
5895
  * (covers the already-ready case). Returns when the first drain settles. */
5775
5896
  async start(items) {
@@ -5800,7 +5921,8 @@ var ReadinessRestore = class {
5800
5921
  return this.pending.has(key);
5801
5922
  }
5802
5923
  /** Attempt every still-pending item; repeat once if a readiness signal arrived
5803
- * mid-flight. Re-entrant and self-coalescing. */
5924
+ * mid-flight. Re-entrant and self-coalescing. Overlapping up to
5925
+ * {@link ReadinessRestoreDeps.concurrency}. */
5804
5926
  async drain() {
5805
5927
  if (this.draining) {
5806
5928
  this.rerun = true;
@@ -5811,12 +5933,15 @@ var ReadinessRestore = class {
5811
5933
  try {
5812
5934
  do {
5813
5935
  this.rerun = false;
5814
- for (const [key, value] of Array.from(this.pending)) try {
5815
- await this.deps.attempt(key, value);
5816
- this.pending.delete(key);
5817
- } catch (err) {
5818
- this.deps.onDefer?.(key, value, err);
5819
- }
5936
+ await runBounded(Array.from(this.pending), this.concurrency, async ([key, value]) => {
5937
+ if (!this.pending.has(key)) return;
5938
+ try {
5939
+ await this.deps.attempt(key, value);
5940
+ this.pending.delete(key);
5941
+ } catch (err) {
5942
+ this.deps.onDefer?.(key, value, err);
5943
+ }
5944
+ });
5820
5945
  } while (this.rerun && this.pending.size > 0);
5821
5946
  } finally {
5822
5947
  this.draining = false;
@@ -6511,14 +6636,16 @@ var RecordingController = class {
6511
6636
  } catch (err) {
6512
6637
  this.deps.logger.warn("recorder controller: placement recompute failed", { meta: { error: require_dist.errMsg(err) } });
6513
6638
  }
6514
- for (const id of ids) try {
6515
- await this.evaluateDevice(id);
6516
- } catch (err) {
6517
- this.deps.logger.warn("recorder controller: convergence evaluate failed", {
6518
- tags: { deviceId: id },
6519
- meta: { error: require_dist.errMsg(err) }
6520
- });
6521
- }
6639
+ await runBounded([...ids], 8, async (id) => {
6640
+ try {
6641
+ await this.evaluateDevice(id);
6642
+ } catch (err) {
6643
+ this.deps.logger.warn("recorder controller: convergence evaluate failed", {
6644
+ tags: { deviceId: id },
6645
+ meta: { error: require_dist.errMsg(err) }
6646
+ });
6647
+ }
6648
+ });
6522
6649
  }
6523
6650
  /**
6524
6651
  * Re-evaluate one device after a config change (called from `setDeviceConfig`).
@@ -6915,6 +7042,503 @@ function makeBrokerCall(handle) {
6915
7042
  return (fn) => handle.call(fn, BROKER_READY_TIMEOUT_MS);
6916
7043
  }
6917
7044
  //#endregion
7045
+ //#region src/durable/durable-ledger.ts
7046
+ /** Default reseed cap — every current consumer's row set is installation-bounded. */
7047
+ var DEFAULT_LOAD_LIMIT = 1e5;
7048
+ var DurableLedger = class DurableLedger {
7049
+ mirror = /* @__PURE__ */ new Map();
7050
+ spec;
7051
+ store;
7052
+ logger;
7053
+ constructor(deps) {
7054
+ this.spec = deps.spec;
7055
+ this.store = deps.store;
7056
+ this.logger = deps.logger;
7057
+ }
7058
+ /**
7059
+ * Register the collection. MUST run at boot, before any read or write: the
7060
+ * SQLite backend answers 412 for an undeclared collection and takes the whole
7061
+ * runner down with it (the addon-ai boot-crash lesson).
7062
+ */
7063
+ static declare(store, spec) {
7064
+ return store.declareCollection.mutate({
7065
+ collection: spec.collection,
7066
+ columns: [...spec.columns],
7067
+ ...spec.indexes !== void 0 ? { indexes: [...spec.indexes] } : {}
7068
+ });
7069
+ }
7070
+ declare() {
7071
+ return DurableLedger.declare(this.store, this.spec);
7072
+ }
7073
+ /** The collection this ledger owns — for the caller's own log lines. */
7074
+ get collection() {
7075
+ return this.spec.collection;
7076
+ }
7077
+ /**
7078
+ * Boot reseed. Replaces the mirror with what the store holds and returns the
7079
+ * rows, so a caller that must hydrate something else (a watcher, a registry)
7080
+ * gets them without a second read.
7081
+ *
7082
+ * **A failure returns what is already mirrored** rather than clearing it —
7083
+ * see contract rule 2. The count is worth logging out loud at the call site:
7084
+ * "loaded 0" after a container recreate is the one line that explains a
7085
+ * silent flood.
7086
+ */
7087
+ async load() {
7088
+ try {
7089
+ const records = await this.store.query.query({
7090
+ collection: this.spec.collection,
7091
+ filter: { limit: this.spec.loadLimit ?? DEFAULT_LOAD_LIMIT }
7092
+ });
7093
+ const next = /* @__PURE__ */ new Map();
7094
+ let skipped = 0;
7095
+ for (const record of records) {
7096
+ const row = this.spec.fromRecord(record.id, record.data);
7097
+ if (row === null) {
7098
+ skipped += 1;
7099
+ continue;
7100
+ }
7101
+ next.set(this.spec.keyOf(row), row);
7102
+ }
7103
+ this.mirror.clear();
7104
+ for (const [key, row] of next) this.mirror.set(key, row);
7105
+ if (skipped > 0) this.logger.warn("durable rows skipped as malformed — they gate NOTHING", { meta: {
7106
+ collection: this.spec.collection,
7107
+ skipped
7108
+ } });
7109
+ return [...this.mirror.values()];
7110
+ } catch (err) {
7111
+ this.logger.warn("durable load failed — keeping the state already in memory", { meta: {
7112
+ collection: this.spec.collection,
7113
+ error: String(err),
7114
+ held: this.mirror.size
7115
+ } });
7116
+ return [...this.mirror.values()];
7117
+ }
7118
+ }
7119
+ /** Every mirrored row, insertion-ordered. */
7120
+ snapshot() {
7121
+ return [...this.mirror.values()];
7122
+ }
7123
+ /** The row currently held for a key, if any. Pure RAM — never I/O. */
7124
+ get(key) {
7125
+ return this.mirror.get(key);
7126
+ }
7127
+ has(key) {
7128
+ return this.mirror.has(key);
7129
+ }
7130
+ get size() {
7131
+ return this.mirror.size;
7132
+ }
7133
+ /**
7134
+ * Judge ONE observation against what the ledger already accepted, and advance
7135
+ * it.
7136
+ *
7137
+ * Synchronous on purpose: the verdict is a function of the in-RAM mirror
7138
+ * alone, so a decision can never be gated on an I/O that might fail (D49).
7139
+ * The durable write is kicked off behind it and its failure changes no
7140
+ * verdict.
7141
+ *
7142
+ * `no-flip` leaves the held row UNTOUCHED — including any timestamp it
7143
+ * carries, which therefore means "when this key last CHANGED", not "when it
7144
+ * last spoke". That is the timestamp anyone reading the table wants.
7145
+ */
7146
+ observe(row) {
7147
+ const equalFact = this.spec.equalFact;
7148
+ if (equalFact === void 0) throw new Error(`DurableLedger(${this.spec.collection}): observe() requires the spec to declare equalFact`);
7149
+ const key = this.spec.keyOf(row);
7150
+ const held = this.mirror.get(key);
7151
+ if (held !== void 0 && equalFact(held, row)) return "no-flip";
7152
+ this.mirror.set(key, row);
7153
+ this.persist(row);
7154
+ return held === void 0 ? "seeded" : "flip";
7155
+ }
7156
+ /**
7157
+ * Upsert a row without a verdict — the write path for a ledger whose owner
7158
+ * has already decided the value changed.
7159
+ *
7160
+ * The order of the mirror advance and the durable write is the spec's
7161
+ * {@link DurableWriteMode}, not the call site's: two call sites that
7162
+ * disagreed about it would be two different durability guarantees on one
7163
+ * collection.
7164
+ */
7165
+ async put(row) {
7166
+ const key = this.spec.keyOf(row);
7167
+ if (this.spec.writeMode === "write-behind") {
7168
+ this.mirror.set(key, row);
7169
+ await this.persist(row);
7170
+ return;
7171
+ }
7172
+ await this.store.set.mutate({
7173
+ collection: this.spec.collection,
7174
+ key,
7175
+ value: this.spec.toValue(row)
7176
+ });
7177
+ this.mirror.set(key, row);
7178
+ }
7179
+ /**
7180
+ * Advance the mirror WITHOUT persisting, for an owner that deliberately
7181
+ * coalesces its writes.
7182
+ *
7183
+ * The stationary registry is the reason this exists: a parked car is
7184
+ * re-confirmed on every processed frame (5–30 Hz), and persisting each
7185
+ * confirmation would offer thousands of commits a day to the checkpoint
7186
+ * lottery (D96) to maintain a handful of rows. It stages the advance and
7187
+ * flushes on its 5-minute sweep — 288 writes a day instead of ~10⁶.
7188
+ *
7189
+ * **The cost is stated, not hidden**: a staged value that is never flushed
7190
+ * is lost on a crash. An owner may only stage a field whose staleness its
7191
+ * own TTL absorbs. Anything that GATES work must go through {@link put} or
7192
+ * {@link observe}.
7193
+ */
7194
+ stage(row) {
7195
+ this.mirror.set(this.spec.keyOf(row), row);
7196
+ }
7197
+ /**
7198
+ * Drop a key from the MIRROR only — the durable row survives.
7199
+ *
7200
+ * What a scope-unbind needs: this process stops holding the value, and a
7201
+ * rebind reloads it from the store. Deliberately distinct from
7202
+ * {@link forget}, which deletes; conflating the two is how an unbind turns
7203
+ * into a wipe.
7204
+ */
7205
+ evict(key) {
7206
+ this.mirror.delete(key);
7207
+ }
7208
+ /**
7209
+ * Drop one key, mirror and row. Best-effort on the durable half: a failed
7210
+ * delete leaves a row that the next load will re-mirror, which is a stale
7211
+ * value rather than a lost one.
7212
+ */
7213
+ async forget(key) {
7214
+ this.mirror.delete(key);
7215
+ try {
7216
+ await this.store.delete.mutate({
7217
+ collection: this.spec.collection,
7218
+ key
7219
+ });
7220
+ } catch (err) {
7221
+ this.logger.debug("durable delete failed", { meta: {
7222
+ collection: this.spec.collection,
7223
+ key,
7224
+ error: String(err)
7225
+ } });
7226
+ }
7227
+ }
7228
+ /**
7229
+ * Drop every mirrored key NOT in `activeKeys`. Returns how many rows went.
7230
+ *
7231
+ * **The caller must hold an AUTHORITATIVE active set.** A prune driven by a
7232
+ * fallible read is work destroyed on an error (D49/D130) — that is why this
7233
+ * is a method a feature opts into rather than a policy the primitive runs.
7234
+ * Best-effort per row: a failed delete keeps the key (retried next prune)
7235
+ * rather than aborting the sweep.
7236
+ */
7237
+ async pruneExcept(activeKeys) {
7238
+ let pruned = 0;
7239
+ for (const key of [...this.mirror.keys()]) {
7240
+ if (activeKeys.has(key)) continue;
7241
+ try {
7242
+ await this.store.delete.mutate({
7243
+ collection: this.spec.collection,
7244
+ key
7245
+ });
7246
+ this.mirror.delete(key);
7247
+ pruned += 1;
7248
+ } catch (err) {
7249
+ this.logger.debug("durable prune delete failed", { meta: {
7250
+ collection: this.spec.collection,
7251
+ key,
7252
+ error: String(err)
7253
+ } });
7254
+ }
7255
+ }
7256
+ return pruned;
7257
+ }
7258
+ /**
7259
+ * Write-behind durable upsert. Best-effort and logged, never thrown at the
7260
+ * decision path: the mirror already holds the truth for this process, and the
7261
+ * worst a lost write can do is one silent re-seed after the next restart.
7262
+ */
7263
+ async persist(row) {
7264
+ const deviceId = this.spec.deviceIdOf?.(row);
7265
+ try {
7266
+ await this.store.set.mutate({
7267
+ collection: this.spec.collection,
7268
+ key: this.spec.keyOf(row),
7269
+ value: this.spec.toValue(row)
7270
+ });
7271
+ } catch (err) {
7272
+ this.logger.warn("durable persist failed — this key may re-seed on boot", {
7273
+ ...deviceId !== void 0 ? { tags: { deviceId } } : {},
7274
+ meta: {
7275
+ collection: this.spec.collection,
7276
+ key: this.spec.keyOf(row),
7277
+ error: String(err)
7278
+ }
7279
+ });
7280
+ }
7281
+ }
7282
+ };
7283
+ //#endregion
7284
+ //#region src/recorder/addon/segment-hour-ledger.ts
7285
+ var HOUR_MS$1 = 36e5;
7286
+ /**
7287
+ * @durable class=mirror owner=recorder
7288
+ * write="write-behind on every finalized segment (hour-row upsert); eviction drops paths; the archive walk reconciles disk into the row set"
7289
+ * retention="a row goes when the walk or eviction confirms the hour is empty — never by age, never from the ledger alone (D148)"
7290
+ */
7291
+ var RECORDING_SEGMENT_HOURS_COLLECTION = "recorder:segment-hours";
7292
+ var RECORDING_SEGMENT_HOURS_COLUMNS = [
7293
+ {
7294
+ name: "key",
7295
+ type: "TEXT",
7296
+ primaryKey: true,
7297
+ notNull: true
7298
+ },
7299
+ {
7300
+ name: "deviceId",
7301
+ type: "INTEGER",
7302
+ notNull: true
7303
+ },
7304
+ {
7305
+ name: "profile",
7306
+ type: "TEXT",
7307
+ notNull: true
7308
+ },
7309
+ {
7310
+ name: "locationId",
7311
+ type: "TEXT",
7312
+ notNull: true
7313
+ },
7314
+ {
7315
+ name: "hourStartMs",
7316
+ type: "INTEGER",
7317
+ notNull: true
7318
+ },
7319
+ {
7320
+ name: "paths",
7321
+ type: "TEXT",
7322
+ notNull: true
7323
+ }
7324
+ ];
7325
+ var RECORDING_SEGMENT_HOURS_INDEXES = [{
7326
+ name: "idx_recorder_segment_hours_device",
7327
+ columns: ["deviceId"]
7328
+ }, {
7329
+ name: "idx_recorder_segment_hours_hour",
7330
+ columns: ["hourStartMs"]
7331
+ }];
7332
+ function hourStartMs(startMs) {
7333
+ return Math.floor(startMs / HOUR_MS$1) * HOUR_MS$1;
7334
+ }
7335
+ function segmentHourKey(deviceId, profile, locationId, hour) {
7336
+ return `${deviceId}|${profile}|${locationId}|${hour}`;
7337
+ }
7338
+ function hourOf(row) {
7339
+ const hour = hourStartMs(row.startMs);
7340
+ return {
7341
+ hourStartMs: hour,
7342
+ key: segmentHourKey(row.deviceId, row.profile, row.locationId, hour)
7343
+ };
7344
+ }
7345
+ function uniquePaths(paths) {
7346
+ const seen = /* @__PURE__ */ new Set();
7347
+ const out = [];
7348
+ for (const p of paths) {
7349
+ if (seen.has(p)) continue;
7350
+ seen.add(p);
7351
+ out.push(p);
7352
+ }
7353
+ return out;
7354
+ }
7355
+ function samePaths(a, b) {
7356
+ if (a.length !== b.length) return false;
7357
+ const set = new Set(a);
7358
+ for (const p of b) if (!set.has(p)) return false;
7359
+ return true;
7360
+ }
7361
+ function rowToValue(row) {
7362
+ return {
7363
+ deviceId: row.deviceId,
7364
+ profile: row.profile,
7365
+ locationId: row.locationId,
7366
+ hourStartMs: row.hourStartMs,
7367
+ paths: JSON.stringify(row.paths)
7368
+ };
7369
+ }
7370
+ function recordToRow(key, data) {
7371
+ const deviceId = Number(data["deviceId"]);
7372
+ const profile = data["profile"];
7373
+ const locationId = data["locationId"];
7374
+ const hour = Number(data["hourStartMs"]);
7375
+ let raw = data["paths"];
7376
+ if (typeof raw === "string") try {
7377
+ raw = JSON.parse(raw);
7378
+ } catch {
7379
+ return null;
7380
+ }
7381
+ if (!Number.isFinite(deviceId) || deviceId <= 0 || typeof profile !== "string" || profile.length === 0 || typeof locationId !== "string" || locationId.length === 0 || !Number.isFinite(hour) || !Array.isArray(raw) || !raw.every((p) => typeof p === "string" && p.length > 0)) return null;
7382
+ return {
7383
+ key,
7384
+ deviceId,
7385
+ profile,
7386
+ locationId,
7387
+ hourStartMs: hour,
7388
+ paths: raw
7389
+ };
7390
+ }
7391
+ var SPEC = {
7392
+ collection: RECORDING_SEGMENT_HOURS_COLLECTION,
7393
+ columns: RECORDING_SEGMENT_HOURS_COLUMNS,
7394
+ indexes: RECORDING_SEGMENT_HOURS_INDEXES,
7395
+ writeMode: "write-behind",
7396
+ loadLimit: 1e5,
7397
+ keyOf: (row) => row.key,
7398
+ toValue: rowToValue,
7399
+ fromRecord: recordToRow,
7400
+ deviceIdOf: (row) => row.deviceId
7401
+ };
7402
+ var SegmentHourLedger = class {
7403
+ ledger;
7404
+ logger;
7405
+ constructor(deps) {
7406
+ this.logger = deps.logger;
7407
+ this.ledger = new DurableLedger({
7408
+ spec: SPEC,
7409
+ store: deps.store,
7410
+ logger: deps.logger
7411
+ });
7412
+ }
7413
+ static declare(store) {
7414
+ return DurableLedger.declare(store, SPEC);
7415
+ }
7416
+ declare() {
7417
+ return this.ledger.declare();
7418
+ }
7419
+ async load() {
7420
+ const rows = await this.ledger.load();
7421
+ this.logger.info("recorder: segment-hour ledger loaded", { meta: {
7422
+ hours: rows.length,
7423
+ collection: RECORDING_SEGMENT_HOURS_COLLECTION
7424
+ } });
7425
+ return rows;
7426
+ }
7427
+ snapshot() {
7428
+ return this.ledger.snapshot();
7429
+ }
7430
+ /**
7431
+ * Seed {@link RecordingIndex} from the mirrored hour rows.
7432
+ *
7433
+ * Hours present in the ledger become `hydrated` so a seek in those windows
7434
+ * does not pay for a disk walk. Hours the ledger never held stay `unknown`
7435
+ * — that is the fail-toward-walk direction.
7436
+ */
7437
+ hydrateIndex(index) {
7438
+ for (const hour of this.ledger.snapshot()) index.hydrateHour(hour.deviceId, hour.locationId, hour.hourStartMs, hour.paths);
7439
+ }
7440
+ /** Write-behind: a just-finalized segment joins its hour row. */
7441
+ async recordSegment(row) {
7442
+ const { key, hourStartMs: hour } = hourOf(row);
7443
+ const held = this.ledger.get(key);
7444
+ if (held !== void 0 && held.paths.includes(row.path)) return;
7445
+ await this.ledger.put({
7446
+ key,
7447
+ deviceId: row.deviceId,
7448
+ profile: row.profile,
7449
+ locationId: row.locationId,
7450
+ hourStartMs: hour,
7451
+ paths: held === void 0 ? [row.path] : [...held.paths, row.path]
7452
+ });
7453
+ }
7454
+ /**
7455
+ * Eviction confirmed these paths are gone from disk. Drop them; forget the
7456
+ * hour when it holds nothing.
7457
+ */
7458
+ async dropSegments(rows) {
7459
+ const doomed = /* @__PURE__ */ new Map();
7460
+ for (const row of rows) {
7461
+ const { key } = hourOf(row);
7462
+ let set = doomed.get(key);
7463
+ if (!set) {
7464
+ set = /* @__PURE__ */ new Set();
7465
+ doomed.set(key, set);
7466
+ }
7467
+ set.add(row.path);
7468
+ }
7469
+ for (const [key, paths] of doomed) {
7470
+ const held = this.ledger.get(key);
7471
+ if (held === void 0) continue;
7472
+ const next = held.paths.filter((p) => !paths.has(p));
7473
+ if (next.length === 0) {
7474
+ await this.ledger.forget(key);
7475
+ continue;
7476
+ }
7477
+ if (next.length === held.paths.length) continue;
7478
+ await this.ledger.put({
7479
+ ...held,
7480
+ paths: next
7481
+ });
7482
+ }
7483
+ }
7484
+ /**
7485
+ * After a disk walk of `deviceIds`, make the ledger match the index — with
7486
+ * two D148 exceptions:
7487
+ *
7488
+ * - an hour the walk did not see is dropped ONLY if it is in the past;
7489
+ * the current hour may still hold a live writer whose file the readdir
7490
+ * raced;
7491
+ * - the current hour UNIONs ledger paths with index paths rather than
7492
+ * replacing, for the same race.
7493
+ *
7494
+ * Devices not in `deviceIds` are left untouched (a partial walk must not
7495
+ * prune cameras it did not look at).
7496
+ */
7497
+ async reconcileFromIndex(index, deviceIds, nowMs) {
7498
+ const walked = new Set(deviceIds);
7499
+ const currentHour = hourStartMs(nowMs);
7500
+ const fromIndex = /* @__PURE__ */ new Map();
7501
+ for (const deviceId of deviceIds) for (const seg of index.segments(deviceId)) {
7502
+ const { key, hourStartMs: hour } = hourOf(seg);
7503
+ const existing = fromIndex.get(key);
7504
+ if (existing === void 0) fromIndex.set(key, {
7505
+ key,
7506
+ deviceId: seg.deviceId,
7507
+ profile: seg.profile,
7508
+ locationId: seg.locationId,
7509
+ hourStartMs: hour,
7510
+ paths: [seg.path]
7511
+ });
7512
+ else fromIndex.set(key, {
7513
+ ...existing,
7514
+ paths: [...existing.paths, seg.path]
7515
+ });
7516
+ }
7517
+ for (const row of this.ledger.snapshot()) {
7518
+ if (!walked.has(row.deviceId)) continue;
7519
+ const disk = fromIndex.get(row.key);
7520
+ if (disk === void 0) {
7521
+ if (row.hourStartMs === currentHour) continue;
7522
+ await this.ledger.forget(row.key);
7523
+ continue;
7524
+ }
7525
+ if (row.hourStartMs === currentHour) {
7526
+ const union = uniquePaths([...disk.paths, ...row.paths]);
7527
+ if (!samePaths(union, row.paths)) await this.ledger.put({
7528
+ ...row,
7529
+ paths: union
7530
+ });
7531
+ continue;
7532
+ }
7533
+ if (!samePaths(row.paths, disk.paths)) await this.ledger.put(disk);
7534
+ }
7535
+ for (const [key, disk] of fromIndex) {
7536
+ if (this.ledger.has(key)) continue;
7537
+ await this.ledger.put(disk);
7538
+ }
7539
+ }
7540
+ };
7541
+ //#endregion
6918
7542
  //#region src/recorder/addon/recording-export-provider.ts
6919
7543
  /** Content type of every rendered export — the engine only ever writes MP4. */
6920
7544
  var EXPORT_CONTENT_TYPE = "video/mp4";
@@ -6942,6 +7566,12 @@ function exportFileName(rec) {
6942
7566
  async function hasFootage(index, deviceId, profile, fromMs, toMs) {
6943
7567
  return (await index.segmentsInWindow(deviceId, profile, fromMs, toMs)).some((s) => s.startMs < toMs && s.startMs + s.durMs > fromMs);
6944
7568
  }
7569
+ /** Canonical `profiles[]`, falling back to the legacy singular `profile`. */
7570
+ function resolveExportProfiles(input) {
7571
+ if (input.profiles !== void 0 && input.profiles.length > 0) return [...input.profiles];
7572
+ if (typeof input.profile === "string" && input.profile.length > 0) return [input.profile];
7573
+ return [];
7574
+ }
6945
7575
  /** Load a record or throw a clear NOT_FOUND-style error. */
6946
7576
  async function requireRecord(state, exportId) {
6947
7577
  const rec = await getExportRecord(state, exportId);
@@ -6951,30 +7581,37 @@ async function requireRecord(state, exportId) {
6951
7581
  /** Build the `recordingExport` provider over the injected deps. */
6952
7582
  function buildRecordingExportProvider(deps) {
6953
7583
  return {
6954
- createExport: async ({ deviceId, profile, fromMs, toMs, options }) => {
7584
+ createExport: async (input) => {
7585
+ const { deviceId, fromMs, toMs, options } = input;
7586
+ const profiles = resolveExportProfiles(input);
7587
+ if (profiles.length < 1) throw new Error("recording-export: profiles must contain at least one profile");
6955
7588
  if (!(fromMs < toMs)) throw new Error(`recording-export: invalid range ${fromMs}..${toMs}`);
6956
- if (!await hasFootage(deps.index, deviceId, profile, fromMs, toMs)) throw new Error(`recording-export: no footage for ${deviceId}/${profile} in ${fromMs}..${toMs}`);
7589
+ for (const profile of profiles) if (!await hasFootage(deps.index, deviceId, profile, fromMs, toMs)) throw new Error(`recording-export: no footage for ${deviceId}/${profile} in ${fromMs}..${toMs}`);
6957
7590
  const now = deps.now();
6958
- const record = {
6959
- id: deps.newId(),
6960
- deviceId,
6961
- profile,
6962
- fromMs,
6963
- toMs,
6964
- options,
6965
- state: "queued",
6966
- progressPct: null,
6967
- fileBytes: null,
6968
- expiresAt: now + options.maxLifeMs,
6969
- deleteAfterDownload: options.deleteAfterDownload,
6970
- downloadedAt: null,
6971
- createdAt: now,
6972
- createdBy: deps.createdBy(),
6973
- error: null
6974
- };
6975
- const saved = await upsertExport(deps.state, record);
6976
- deps.engine.enqueue(saved.id);
6977
- return saved;
7591
+ let first = null;
7592
+ for (const profile of profiles) {
7593
+ const record = {
7594
+ id: deps.newId(),
7595
+ deviceId,
7596
+ profile,
7597
+ fromMs,
7598
+ toMs,
7599
+ options,
7600
+ state: "queued",
7601
+ progressPct: null,
7602
+ fileBytes: null,
7603
+ expiresAt: now + options.maxLifeMs,
7604
+ deleteAfterDownload: options.deleteAfterDownload,
7605
+ downloadedAt: null,
7606
+ createdAt: now,
7607
+ createdBy: deps.createdBy(),
7608
+ error: null
7609
+ };
7610
+ const saved = await upsertExport(deps.state, record);
7611
+ deps.engine.enqueue(saved.id);
7612
+ first ??= saved;
7613
+ }
7614
+ return first;
6978
7615
  },
6979
7616
  listExports: ({ deviceId }) => listExportsFor(deps.state, deviceId),
6980
7617
  getExport: ({ exportId }) => requireRecord(deps.state, exportId),
@@ -7723,6 +8360,12 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
7723
8360
  isRecordingNode = true;
7724
8361
  index = new RecordingIndex();
7725
8362
  /**
8363
+ * Durable hour-directory copy of {@link index}. Null on an inert
8364
+ * (non-recording) node. Boot reseeds the RAM index from this; the
8365
+ * deferred archive walk reconciles it against disk.
8366
+ */
8367
+ segmentHours = null;
8368
+ /**
7726
8369
  * The calendar rungs — which days, which hours, and a day's exact ranges.
7727
8370
  *
7728
8371
  * Separate from `RecordingIndex` on purpose: that one is the SEGMENT model
@@ -7794,6 +8437,16 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
7794
8437
  } });
7795
8438
  return { providers: [] };
7796
8439
  }
8440
+ this.segmentHours = new SegmentHourLedger({
8441
+ store: this.ctx.api.settingsStore,
8442
+ logger: this.ctx.logger
8443
+ });
8444
+ try {
8445
+ await this.segmentHours.declare();
8446
+ } catch (err) {
8447
+ this.ctx.logger.warn("recorder: segment-hour ledger declare failed — index stays RAM-only this boot", { meta: { error: require_dist.errMsg(err) } });
8448
+ this.segmentHours = null;
8449
+ }
7797
8450
  const ingestOwner = await this.resolveIngestOwner();
7798
8451
  const consumerHostname = this.nodeId === ingestOwner.ownerNodeId ? void 0 : ingestOwner.reachableHost ?? resolveRecordingHubHostname(this.config.recordingHubHostname, process.env["CAMSTACK_HUB_URL"]);
7799
8452
  if (this.nodeId !== ingestOwner.ownerNodeId) {
@@ -7830,7 +8483,11 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
7830
8483
  onEvicted: (deviceId, atMs) => {
7831
8484
  this.calendar.invalidate(deviceId, atMs);
7832
8485
  },
8486
+ onDeindexed: (rows) => {
8487
+ this.segmentHours?.dropSegments(rows);
8488
+ },
7833
8489
  onIndexed: (row, absPath) => {
8490
+ this.segmentHours?.recordSegment(row);
7834
8491
  if (!isMfraTableServable(row.startMs, Date.now())) return;
7835
8492
  readMfraTable(absPath, row.bytes).then((table) => {
7836
8493
  this.mfraTables.record(row.deviceId, row.profile, row.startMs, table);
@@ -8207,6 +8864,12 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
8207
8864
  this.controller?.onTrigger(deviceId, "motion", atMs);
8208
8865
  }
8209
8866
  });
8867
+ try {
8868
+ await this.segmentHours?.load();
8869
+ this.segmentHours?.hydrateIndex(this.index);
8870
+ } catch (err) {
8871
+ this.ctx.logger.warn("recorder: segment-hour ledger load failed — falling through to the archive walk", { meta: { error: require_dist.errMsg(err) } });
8872
+ }
8210
8873
  let configs;
8211
8874
  try {
8212
8875
  configs = await readDeviceConfigs(this.configStore());
@@ -8694,6 +9357,11 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
8694
9357
  locationCount: this.resolvedLocations.length,
8695
9358
  ms: Date.now() - hydrateStartedMs
8696
9359
  } });
9360
+ try {
9361
+ await this.segmentHours?.reconcileFromIndex(this.index, deviceIds, Date.now());
9362
+ } catch (err) {
9363
+ this.ctx.logger.warn("recorder: segment-hour ledger reconcile failed — walk remains the authority", { meta: { error: require_dist.errMsg(err) } });
9364
+ }
8697
9365
  const loggedDomains = /* @__PURE__ */ new Set();
8698
9366
  for (const loc of this.resolvedLocations) {
8699
9367
  const domain = this.locationDomain(loc.id);