@camstack/addon-pipeline 1.2.121 → 1.2.123

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 (35) hide show
  1. package/dist/{addon-utils-DpyYShEF.js → addon-utils-DjiHGIRF.js} +1 -1
  2. package/dist/audio-analyzer/index.js +3 -3
  3. package/dist/audio-analyzer/index.mjs +2 -2
  4. package/dist/detection-pipeline/index.js +5 -5
  5. package/dist/detection-pipeline/index.mjs +3 -3
  6. package/dist/{dist-BJFxSye4.mjs → dist-B7rIp5qQ.mjs} +65 -21
  7. package/dist/{dist-CDNpiyq6.js → dist-DomdkRKL.js} +65 -21
  8. package/dist/{event-loop-stall-monitor-BnqO_H2Z.js → event-loop-stall-monitor-ChA4AQ1D.js} +1 -1
  9. package/dist/{event-loop-stall-monitor-CcHh1ovi.mjs → event-loop-stall-monitor-mvxKgrNV.mjs} +1 -1
  10. package/dist/{lazy-sharp-B39XqJD6.js → lazy-sharp-BSeW3qY5.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/{process-memory-QlU7BsBQ.js → process-memory-BtpLNnXq.js} +1 -1
  16. package/dist/{process-memory-BeCXh3iT.mjs → process-memory-XHs-umae.mjs} +1 -1
  17. package/dist/recorder/index.js +1203 -882
  18. package/dist/recorder/index.mjs +1202 -881
  19. package/dist/{segment-demux-js-DZ6bRNvA.js → segment-demux-js-CFIIJsfc.js} +1 -1
  20. package/dist/{segment-demux-js-BuaascIe.mjs → segment-demux-js-Cc5Jubdu.mjs} +1 -1
  21. package/dist/session-decode/decode-worker-child.js +2 -2
  22. package/dist/session-decode/decode-worker-child.mjs +1 -1
  23. package/dist/stream-broker/_stub.js +1 -1
  24. package/dist/stream-broker/{_virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-DN_gNdAs.mjs → _virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-P-WP2KSi.mjs} +3 -3
  25. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-Do2DEtRt.mjs +26 -0
  26. package/dist/stream-broker/demux-worker-child.js +1 -1
  27. package/dist/stream-broker/demux-worker-child.mjs +1 -1
  28. package/dist/stream-broker/{hostInit-F493PnEJ.mjs → hostInit-D6O20fld.mjs} +3 -3
  29. package/dist/stream-broker/index.js +85 -14
  30. package/dist/stream-broker/index.mjs +85 -14
  31. package/dist/stream-broker/remoteEntry.js +1 -1
  32. package/dist/{worker-protocol-Djzf2BTN.mjs → worker-protocol-CrHJXGHV.mjs} +1 -1
  33. package/dist/{worker-protocol-BPx6jK4-.js → worker-protocol-DQ5DPOFD.js} +1 -1
  34. package/package.json +7 -12
  35. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-D-NFPV_g.mjs +0 -26
@@ -1,7 +1,7 @@
1
- const require_dist = require("../dist-CDNpiyq6.js");
1
+ const require_dist = require("../dist-DomdkRKL.js");
2
2
  const require_hub_hostname = require("../hub-hostname-DAJXlOgV.js");
3
3
  const require_restream_intent = require("../restream-intent-Cv9x3jmu.js");
4
- const require_addon_utils = require("../addon-utils-DpyYShEF.js");
4
+ const require_addon_utils = require("../addon-utils-DjiHGIRF.js");
5
5
  const require_retire_root_keys = require("../retire-root-keys-KE6D6Xh_.js");
6
6
  let node_crypto = require("node:crypto");
7
7
  let node_child_process = require("node:child_process");
@@ -5288,6 +5288,338 @@ function runFfmpeg(renderDeps, args, ext) {
5288
5288
  });
5289
5289
  });
5290
5290
  }
5291
+ Object.freeze({
5292
+ "addons": { "getLogs": { "limit": 100 } },
5293
+ "audio-analyzer": { "analyseChunk": { "settings": { "__nested__": {
5294
+ "minConfidence": .3,
5295
+ "allowedClasses": []
5296
+ } } } },
5297
+ "audio-codec": {
5298
+ "pullEncoded": { "maxCount": 8 },
5299
+ "pullPcm": { "maxCount": 8 }
5300
+ },
5301
+ "battery": { "wakeForStream": { "timeoutMs": 8e3 } },
5302
+ "decoder": {
5303
+ "createSession": {
5304
+ "maxFps": 0,
5305
+ "outputFormat": "jpeg",
5306
+ "scale": 1,
5307
+ "frameSink": "callback"
5308
+ },
5309
+ "pullFrames": { "maxCount": 1 },
5310
+ "pullHandles": { "maxCount": 1 }
5311
+ },
5312
+ "device-adoption": { "listCandidates": {
5313
+ "page": 1,
5314
+ "pageSize": 50
5315
+ } },
5316
+ "device-manager": { "adoptionListCandidates": {
5317
+ "page": 1,
5318
+ "pageSize": 50
5319
+ } },
5320
+ "llm": { "upsertProfile": { "profile": { "__nested__": {
5321
+ "timeoutMs": 6e4,
5322
+ "connectTimeoutMs": 1e4,
5323
+ "firstTokenTimeoutMs": 12e4,
5324
+ "idleTimeoutMs": 6e4,
5325
+ "retry": {
5326
+ "enabled": false,
5327
+ "maxAttempts": 1
5328
+ },
5329
+ "toolsEnabled": false
5330
+ } } } },
5331
+ "llm-runtime": {
5332
+ "complete": { "runtime": { "__nested__": {
5333
+ "contextSize": 4096,
5334
+ "gpuLayers": 0,
5335
+ "parallel": 1,
5336
+ "flashAttention": false,
5337
+ "mlock": false,
5338
+ "noMmap": false,
5339
+ "extraArgs": [],
5340
+ "autoStart": false,
5341
+ "idleStopMinutes": 30
5342
+ } } },
5343
+ "ensureStarted": { "runtime": { "__nested__": {
5344
+ "contextSize": 4096,
5345
+ "gpuLayers": 0,
5346
+ "parallel": 1,
5347
+ "flashAttention": false,
5348
+ "mlock": false,
5349
+ "noMmap": false,
5350
+ "extraArgs": [],
5351
+ "autoStart": false,
5352
+ "idleStopMinutes": 30
5353
+ } } }
5354
+ },
5355
+ "mqtt-broker": { "startEmbeddedBroker": {
5356
+ "port": 1883,
5357
+ "allowAnonymous": false
5358
+ } },
5359
+ "nodes": { "getCapUsageGraph": { "windowSeconds": 60 } },
5360
+ "notification-output": { "send": { "notification": { "__nested__": {
5361
+ "format": "text",
5362
+ "priority": 3
5363
+ } } } },
5364
+ "notification-rules": {
5365
+ "createRule": { "rule": { "__nested__": {
5366
+ "enabled": true,
5367
+ "conditions": {},
5368
+ "media": { "attach": "best" },
5369
+ "throttle": {
5370
+ "cooldownSec": 60,
5371
+ "scope": "rule-device"
5372
+ },
5373
+ "priority": 3
5374
+ } } },
5375
+ "getHistory": { "filter": { "limit": 100 } },
5376
+ "testRule": {
5377
+ "rule": { "__nested__": {
5378
+ "enabled": true,
5379
+ "conditions": {},
5380
+ "media": { "attach": "best" },
5381
+ "throttle": {
5382
+ "cooldownSec": 60,
5383
+ "scope": "rule-device"
5384
+ },
5385
+ "priority": 3
5386
+ } },
5387
+ "lookbackMinutes": 60
5388
+ }
5389
+ },
5390
+ "osd-manager": { "setSlotBinding": { "binding": { "__nested__": {
5391
+ "enabled": true,
5392
+ "template": "${value}",
5393
+ "maxDecimals": 1,
5394
+ "fallbackText": ""
5395
+ } } } },
5396
+ "pipeline-analytics": {
5397
+ "getAudioEvents": { "limit": 1e3 },
5398
+ "getKeyEvents": { "limit": 50 },
5399
+ "getMotionEvents": { "limit": 1e3 },
5400
+ "getObjectEvents": { "limit": 1e3 },
5401
+ "getSensorEvents": { "limit": 1e3 },
5402
+ "listGroups": { "limit": 40 },
5403
+ "listRecentTracks": { "limit": 200 },
5404
+ "searchObjectEvents": {
5405
+ "limit": 50,
5406
+ "minScore": .2
5407
+ }
5408
+ },
5409
+ "pipeline-runner": {
5410
+ "attachCamera": {
5411
+ "detectionMode": "on-motion",
5412
+ "audioMode": "always-on",
5413
+ "motionCooldownMs": 3e4,
5414
+ "motionFps": 4,
5415
+ "detectionFps": 10,
5416
+ "motionSources": ["analyzer"],
5417
+ "pipelineEnabled": true,
5418
+ "zones": [],
5419
+ "onboardMotionDrivesAnalyzer": true,
5420
+ "occupancyRecheckEnabled": true,
5421
+ "occupancyRecheckSec": 300,
5422
+ "occupancyRecheckFrames": 4,
5423
+ "frameSource": { "kind": "local-broker" }
5424
+ },
5425
+ "reportMotion": { "source": "analyzer" }
5426
+ },
5427
+ "recording": {
5428
+ "renderClip": {
5429
+ "preRollSec": 3,
5430
+ "postRollSec": 7,
5431
+ "maxWidth": 640
5432
+ },
5433
+ "renderGif": {
5434
+ "preRollSec": 2,
5435
+ "postRollSec": 5,
5436
+ "maxWidth": 480,
5437
+ "fps": 5
5438
+ },
5439
+ "setDeviceConfig": { "config": { "__nested__": { "bands": [] } } }
5440
+ },
5441
+ "storage": { "upsertLocation": {
5442
+ "isDefault": false,
5443
+ "isSystem": false
5444
+ } },
5445
+ "storage-provider": {
5446
+ "beginDownload": { "location": { "__nested__": {
5447
+ "isDefault": false,
5448
+ "isSystem": false
5449
+ } } },
5450
+ "beginUpload": { "location": { "__nested__": {
5451
+ "isDefault": false,
5452
+ "isSystem": false
5453
+ } } },
5454
+ "delete": { "location": { "__nested__": {
5455
+ "isDefault": false,
5456
+ "isSystem": false
5457
+ } } },
5458
+ "exists": { "location": { "__nested__": {
5459
+ "isDefault": false,
5460
+ "isSystem": false
5461
+ } } },
5462
+ "getAvailableSpace": { "location": { "__nested__": {
5463
+ "isDefault": false,
5464
+ "isSystem": false
5465
+ } } },
5466
+ "list": { "location": { "__nested__": {
5467
+ "isDefault": false,
5468
+ "isSystem": false
5469
+ } } },
5470
+ "read": { "location": { "__nested__": {
5471
+ "isDefault": false,
5472
+ "isSystem": false
5473
+ } } },
5474
+ "resolve": { "location": { "__nested__": {
5475
+ "isDefault": false,
5476
+ "isSystem": false
5477
+ } } },
5478
+ "write": { "location": { "__nested__": {
5479
+ "isDefault": false,
5480
+ "isSystem": false
5481
+ } } }
5482
+ },
5483
+ "stream-broker": {
5484
+ "produceEventMedia": {
5485
+ "preSeconds": 4,
5486
+ "postSeconds": 6,
5487
+ "kinds": ["mp4"],
5488
+ "gifMaxWidth": 640,
5489
+ "gifFps": 12,
5490
+ "gifSpeed": 4,
5491
+ "speed": 4
5492
+ },
5493
+ "pullAudioChunks": { "maxCount": 8 },
5494
+ "pullFrameHandles": { "maxCount": 4 },
5495
+ "renderPreBufferClip": {
5496
+ "preRollSec": 3,
5497
+ "postRollSec": 5,
5498
+ "format": "gif",
5499
+ "maxWidth": 480,
5500
+ "fps": 5,
5501
+ "speed": 2
5502
+ }
5503
+ },
5504
+ "terminal-session": { "pullOutput": { "waitMs": 0 } },
5505
+ "user-management": {
5506
+ "createApiKey": { "isAdmin": false },
5507
+ "createUser": { "isAdmin": false }
5508
+ },
5509
+ "vector-store": { "declareIndex": { "metric": "cosine" } },
5510
+ "zones": {
5511
+ "addZone": { "zone": { "__nested__": {
5512
+ "kind": "polygon",
5513
+ "color": "#3b82f6"
5514
+ } } },
5515
+ "updateZone": { "zone": { "__nested__": {
5516
+ "kind": "polygon",
5517
+ "color": "#3b82f6"
5518
+ } } }
5519
+ }
5520
+ });
5521
+ Object.freeze({
5522
+ "addon-pages-source": ["listPages"],
5523
+ "addon-routes": ["getRoutes"],
5524
+ "addon-widgets-source": ["listWidgets"],
5525
+ "broker": ["list", "listProviders"],
5526
+ "custom-model-registry": ["listModels"],
5527
+ "data-store-provider": ["histogram", "query"],
5528
+ "device-export": ["listExposedDevices", "listSupportedDeviceKinds"],
5529
+ "device-provider": ["discoverDevices", "getDevices"],
5530
+ "llm": [
5531
+ "getDefaults",
5532
+ "getUsage",
5533
+ "listModelCatalog",
5534
+ "listModels",
5535
+ "listNodeModels",
5536
+ "listProfileKinds",
5537
+ "listProfiles",
5538
+ "listRuntimeNodes"
5539
+ ],
5540
+ "log-destination": ["query"],
5541
+ "login-method": ["getLoginMethods"],
5542
+ "mqtt-broker": ["listBrokers"],
5543
+ "network-access": ["listEndpoints"],
5544
+ "notification-output": [
5545
+ "discoverTargets",
5546
+ "listTargetKinds",
5547
+ "listTargets"
5548
+ ],
5549
+ "storage-provider": ["list"],
5550
+ "turn-provider": ["getTurnServers"],
5551
+ "user-passkeys": ["listPasskeys"]
5552
+ });
5553
+ //#endregion
5554
+ //#region src/recorder/addon/last-known-config.ts
5555
+ /**
5556
+ * Wrap `load` so a failed read serves the last value that succeeded.
5557
+ *
5558
+ * Returns a drop-in replacement for {@link LoadDeviceConfigFn}: every existing
5559
+ * call site gains the fallback without changing, which is the point — the
5560
+ * trigger post-buffer was the path that got CAUGHT writing a wrong number, but
5561
+ * it is one of five reads and the other four fail the same way.
5562
+ */
5563
+ function withLastKnownConfig(deps) {
5564
+ const now = deps.now ?? (() => Date.now());
5565
+ const cache = /* @__PURE__ */ new Map();
5566
+ return async (deviceId) => {
5567
+ try {
5568
+ const config = await deps.load(deviceId);
5569
+ cache.set(deviceId, {
5570
+ config,
5571
+ atMs: now()
5572
+ });
5573
+ return config;
5574
+ } catch (err) {
5575
+ const cached = cache.get(deviceId);
5576
+ if (cached === void 0) {
5577
+ deps.logger.warn("recorder: config read failed and NOTHING is cached for this camera — no last-known-good to serve", {
5578
+ tags: { deviceId },
5579
+ meta: { error: require_dist.errMsg(err) }
5580
+ });
5581
+ throw err;
5582
+ }
5583
+ const ageMs = now() - cached.atMs;
5584
+ deps.logger.warn("recorder: config read failed — serving the LAST-KNOWN-GOOD config (not the shipped default)", {
5585
+ tags: { deviceId },
5586
+ meta: {
5587
+ ageMs,
5588
+ error: require_dist.errMsg(err)
5589
+ }
5590
+ });
5591
+ return cached.config;
5592
+ }
5593
+ };
5594
+ }
5595
+ //#endregion
5596
+ //#region src/recorder/addon/location-capacity.ts
5597
+ /** Human-readable failure text for a thrown value of unknown shape. */
5598
+ function reasonOf(err) {
5599
+ return err instanceof Error ? err.message : String(err);
5600
+ }
5601
+ /**
5602
+ * Free + total bytes on the volume backing `root`.
5603
+ *
5604
+ * Never throws: an unreachable volume is one degraded ROW, not a failed
5605
+ * `getStorageUsage` — a NAS that stops answering must not blank the capacity of
5606
+ * every other disk on the node.
5607
+ */
5608
+ async function readLocationCapacity(root, statfs) {
5609
+ try {
5610
+ const st = await statfs(root);
5611
+ return {
5612
+ availableBytes: st.bavail * st.bsize,
5613
+ totalBytes: st.blocks * st.bsize
5614
+ };
5615
+ } catch (err) {
5616
+ return {
5617
+ availableBytes: null,
5618
+ totalBytes: null,
5619
+ error: reasonOf(err)
5620
+ };
5621
+ }
5622
+ }
5291
5623
  //#endregion
5292
5624
  //#region src/recorder/addon/ops-log-store.ts
5293
5625
  /**
@@ -9178,223 +9510,494 @@ function makeBrokerCall(handle) {
9178
9510
  return (fn) => handle.call(fn, BROKER_READY_TIMEOUT_MS);
9179
9511
  }
9180
9512
  //#endregion
9181
- //#region src/recorder/addon/location-capacity.ts
9182
- /** Human-readable failure text for a thrown value of unknown shape. */
9183
- function reasonOf(err) {
9184
- return err instanceof Error ? err.message : String(err);
9185
- }
9513
+ //#region src/recorder/addon/recording-export-provider.ts
9514
+ /** Content type of every rendered export the engine only ever writes MP4. */
9515
+ var EXPORT_CONTENT_TYPE = "video/mp4";
9516
+ /** Filename bound, extension included. Long enough to stay recognisable. */
9517
+ var EXPORT_NAME_MAX = 64;
9186
9518
  /**
9187
- * Free + total bytes on the volume backing `root`.
9519
+ * The filename a RECIPIENT sees on the attachment.
9188
9520
  *
9189
- * Never throws: an unreachable volume is one degraded ROW, not a failed
9190
- * `getStorageUsage` a NAS that stops answering must not blank the capacity of
9191
- * every other disk on the node.
9521
+ * Derived from the operator's own export title so a delivered video is
9522
+ * identifiable in a chat thread, but slugified and BOUNDED: the title field
9523
+ * accepts 200 characters and several notifier backends silently mangle a
9524
+ * filename that long. No usable title degrades to the export id, never to an
9525
+ * empty name.
9192
9526
  */
9193
- async function readLocationCapacity(root, statfs) {
9194
- try {
9195
- const st = await statfs(root);
9196
- return {
9197
- availableBytes: st.bavail * st.bsize,
9198
- totalBytes: st.blocks * st.bsize
9199
- };
9200
- } catch (err) {
9201
- return {
9202
- availableBytes: null,
9203
- totalBytes: null,
9204
- error: reasonOf(err)
9205
- };
9206
- }
9527
+ function exportFileName(rec) {
9528
+ const slug = (rec.title ?? "").replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-+|-+$/g, "").slice(0, EXPORT_NAME_MAX - 4).replace(/-+$/g, "");
9529
+ return slug.length > 0 ? `${slug}.mp4` : `export-${rec.id}.mp4`;
9207
9530
  }
9208
- //#endregion
9209
- //#region src/recorder/addon/retired-root-keys.ts
9210
- var RECORDER_RETIRED_ROOT_KEYS = [
9211
- {
9212
- key: "recordingOpsLog",
9213
- successor: RECORDING_OPS_LOG_COLLECTION,
9214
- reason: "the audit ring is one row per operation since the flatten; as a blob it was 147 KB of the 201 KB root row, re-parsed by every unrelated reader of that row"
9215
- },
9216
- {
9217
- key: "recordingExports",
9218
- successor: RECORDING_EXPORTS_COLLECTION,
9219
- reason: "one row per export job since the flatten; as a blob a once-a-second progress patch rewrote 201 KB, and an overlapping createExport erased the row (2026-08-14)"
9220
- },
9221
- {
9222
- key: "recordingConfigs",
9223
- successor: RECORDING_CONFIGS_COLLECTION,
9224
- reason: "one row per camera since the flatten; as a blob, asking what camera 617 records parsed every camera in the installation"
9225
- }
9226
- ];
9227
- //#endregion
9228
- //#region src/durable/durable-ledger.ts
9229
- /** Default reseed cap every current consumer's row set is installation-bounded. */
9230
- var DEFAULT_LOAD_LIMIT = 1e5;
9231
- var DurableLedger = class DurableLedger {
9232
- mirror = /* @__PURE__ */ new Map();
9233
- spec;
9234
- store;
9235
- logger;
9236
- constructor(deps) {
9237
- this.spec = deps.spec;
9238
- this.store = deps.store;
9239
- this.logger = deps.logger;
9240
- }
9241
- /**
9242
- * Register the collection. MUST run at boot, before any read or write: the
9243
- * SQLite backend answers 412 for an undeclared collection and takes the whole
9244
- * runner down with it (the addon-ai boot-crash lesson).
9245
- */
9246
- static declare(store, spec) {
9247
- return store.declareCollection.mutate({
9248
- collection: spec.collection,
9249
- columns: [...spec.columns],
9250
- ...spec.indexes !== void 0 ? { indexes: [...spec.indexes] } : {}
9251
- });
9252
- }
9253
- declare() {
9254
- return DurableLedger.declare(this.store, this.spec);
9255
- }
9256
- /** The collection this ledger owns — for the caller's own log lines. */
9257
- get collection() {
9258
- return this.spec.collection;
9259
- }
9260
- /**
9261
- * Boot reseed. Replaces the mirror with what the store holds and returns the
9262
- * rows, so a caller that must hydrate something else (a watcher, a registry)
9263
- * gets them without a second read.
9264
- *
9265
- * **A failure returns what is already mirrored** rather than clearing it —
9266
- * see contract rule 2. The count is worth logging out loud at the call site:
9267
- * "loaded 0" after a container recreate is the one line that explains a
9268
- * silent flood.
9269
- */
9270
- async load() {
9271
- try {
9272
- const records = await this.store.query.query({
9273
- collection: this.spec.collection,
9274
- filter: { limit: this.spec.loadLimit ?? DEFAULT_LOAD_LIMIT }
9275
- });
9276
- const next = /* @__PURE__ */ new Map();
9277
- let skipped = 0;
9278
- for (const record of records) {
9279
- const row = this.spec.fromRecord(record.id, record.data);
9280
- if (row === null) {
9281
- skipped += 1;
9282
- continue;
9283
- }
9284
- next.set(this.spec.keyOf(row), row);
9531
+ /**
9532
+ * True when at least one indexed segment overlaps `[fromMs, toMs)`. Mirrors the
9533
+ * `buildPlaybackManifest` overlap idiom: a segment `[startMs, startMs+durMs)`
9534
+ * overlaps the window iff it starts before the window ends AND ends after the
9535
+ * window starts.
9536
+ */
9537
+ async function hasFootage(index, deviceId, profile, fromMs, toMs) {
9538
+ return (await index.segmentsInWindow(deviceId, profile, fromMs, toMs)).some((s) => s.startMs < toMs && s.startMs + s.durMs > fromMs);
9539
+ }
9540
+ /** Canonical `profiles[]`, falling back to the legacy singular `profile`. */
9541
+ function resolveExportProfiles(input) {
9542
+ if (input.profiles !== void 0 && input.profiles.length > 0) return [...input.profiles];
9543
+ if (typeof input.profile === "string" && input.profile.length > 0) return [input.profile];
9544
+ return [];
9545
+ }
9546
+ /** Load a record or throw a clear NOT_FOUND-style error. */
9547
+ async function requireRecord(state, exportId) {
9548
+ const rec = await getExportRecord(state, exportId);
9549
+ if (!rec) throw new Error(`recording-export: unknown export "${exportId}"`);
9550
+ return rec;
9551
+ }
9552
+ /** Build the `recordingExport` provider over the injected deps. */
9553
+ function buildRecordingExportProvider(deps) {
9554
+ return {
9555
+ createExport: async (input) => {
9556
+ const { deviceId, fromMs, toMs, options } = input;
9557
+ const profiles = resolveExportProfiles(input);
9558
+ if (profiles.length < 1) throw new Error("recording-export: profiles must contain at least one profile");
9559
+ if (!(fromMs < toMs)) throw new Error(`recording-export: invalid range ${fromMs}..${toMs}`);
9560
+ 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}`);
9561
+ const now = deps.now();
9562
+ let first = null;
9563
+ for (const profile of profiles) {
9564
+ const record = {
9565
+ id: deps.newId(),
9566
+ deviceId,
9567
+ profile,
9568
+ fromMs,
9569
+ toMs,
9570
+ options,
9571
+ state: "queued",
9572
+ progressPct: null,
9573
+ fileBytes: null,
9574
+ expiresAt: now + options.maxLifeMs,
9575
+ deleteAfterDownload: options.deleteAfterDownload,
9576
+ downloadedAt: null,
9577
+ createdAt: now,
9578
+ createdBy: deps.createdBy(),
9579
+ error: null
9580
+ };
9581
+ const saved = await upsertExport(deps.state, record);
9582
+ deps.engine.enqueue(saved.id);
9583
+ first ??= saved;
9285
9584
  }
9286
- this.mirror.clear();
9287
- for (const [key, row] of next) this.mirror.set(key, row);
9288
- if (skipped > 0) this.logger.warn("durable rows skipped as malformed — they gate NOTHING", { meta: {
9289
- collection: this.spec.collection,
9290
- skipped
9291
- } });
9292
- return [...this.mirror.values()];
9293
- } catch (err) {
9294
- this.logger.warn("durable load failed — keeping the state already in memory", { meta: {
9295
- collection: this.spec.collection,
9296
- error: String(err),
9297
- held: this.mirror.size
9298
- } });
9299
- return [...this.mirror.values()];
9585
+ return first;
9586
+ },
9587
+ listExports: ({ deviceId }) => listExportsFor(deps.state, deviceId),
9588
+ getExport: ({ exportId }) => requireRecord(deps.state, exportId),
9589
+ cancelExport: async ({ exportId }) => {
9590
+ const existing = await requireRecord(deps.state, exportId);
9591
+ await deps.engine.cancel(exportId);
9592
+ return await getExportRecord(deps.state, exportId) ?? existing;
9593
+ },
9594
+ deleteExport: async ({ exportId }) => {
9595
+ const existing = await requireRecord(deps.state, exportId);
9596
+ await deps.removeFile(deps.outPath(existing));
9597
+ return await patchExport(deps.state, exportId, {
9598
+ state: "deleted",
9599
+ fileBytes: null
9600
+ }) ?? existing;
9601
+ },
9602
+ getDownloadUrl: async ({ exportId }) => {
9603
+ await requireRecord(deps.state, exportId);
9604
+ const url = deps.downloadUrlFor(exportId);
9605
+ return {
9606
+ url,
9607
+ endpoints: [url]
9608
+ };
9609
+ },
9610
+ readExportBytes: async ({ exportId }) => {
9611
+ const rec = await requireRecord(deps.state, exportId);
9612
+ const tags = { deviceId: rec.deviceId };
9613
+ if (rec.state !== "ready") {
9614
+ deps.logger.warn("export byte read refused: export is not ready", {
9615
+ tags,
9616
+ meta: {
9617
+ exportId,
9618
+ state: rec.state
9619
+ }
9620
+ });
9621
+ throw new Error(`recording-export: export "${exportId}" is not ready (${rec.state})`);
9622
+ }
9623
+ if (deps.now() >= rec.expiresAt) {
9624
+ deps.logger.warn("export byte read refused: export has expired", {
9625
+ tags,
9626
+ meta: {
9627
+ exportId,
9628
+ expiresAt: rec.expiresAt
9629
+ }
9630
+ });
9631
+ throw new Error(`recording-export: export "${exportId}" has expired`);
9632
+ }
9633
+ const file = deps.outPath(rec);
9634
+ let size;
9635
+ try {
9636
+ size = await deps.statBytes(file);
9637
+ } catch (err) {
9638
+ deps.logger.warn("export byte read refused: file is missing", {
9639
+ tags,
9640
+ meta: {
9641
+ exportId,
9642
+ error: String(err)
9643
+ }
9644
+ });
9645
+ throw new Error(`recording-export: export "${exportId}" has no file on disk`, { cause: err });
9646
+ }
9647
+ if (size > 52428800) {
9648
+ deps.logger.warn("export byte read refused: file is too large to inline", {
9649
+ tags,
9650
+ meta: {
9651
+ exportId,
9652
+ bytes: size,
9653
+ maxBytes: require_dist.RECORDING_EXPORT_MAX_READ_BYTES
9654
+ }
9655
+ });
9656
+ throw new Error(`recording-export: export "${exportId}" is too large to read inline (${size} > ${require_dist.RECORDING_EXPORT_MAX_READ_BYTES} bytes)`);
9657
+ }
9658
+ const bytes = await deps.readFile(file);
9659
+ return {
9660
+ base64: bytes.toString("base64"),
9661
+ contentType: EXPORT_CONTENT_TYPE,
9662
+ name: exportFileName({
9663
+ id: rec.id,
9664
+ ...rec.options.title !== void 0 ? { title: rec.options.title } : {}
9665
+ }),
9666
+ bytes: bytes.byteLength
9667
+ };
9300
9668
  }
9301
- }
9302
- /** Every mirrored row, insertion-ordered. */
9303
- snapshot() {
9304
- return [...this.mirror.values()];
9305
- }
9306
- /** The row currently held for a key, if any. Pure RAM never I/O. */
9307
- get(key) {
9308
- return this.mirror.get(key);
9309
- }
9310
- has(key) {
9311
- return this.mirror.has(key);
9312
- }
9313
- get size() {
9314
- return this.mirror.size;
9669
+ };
9670
+ }
9671
+ //#endregion
9672
+ //#region src/recorder/addon/recording-hub-host.ts
9673
+ /**
9674
+ * Resolve the host a REMOTE recording node should use to dial the hub-only
9675
+ * stream-broker's restream port.
9676
+ *
9677
+ * The implementation was promoted to the package-shared
9678
+ * `src/shared/hub-hostname.ts` in P2d (cross-node Phase 2) so the
9679
+ * pipeline-runner's remote-source leg shares the exact same resolver instead
9680
+ * of copying it. This module keeps the recorder-facing name as a thin alias —
9681
+ * resolution order and semantics are unchanged (operator override first, then
9682
+ * the host extracted from `CAMSTACK_HUB_URL`).
9683
+ */
9684
+ var resolveRecordingHubHostname = require_hub_hostname.resolveHubHostname;
9685
+ //#endregion
9686
+ //#region src/recorder/addon/retention-sweep.ts
9687
+ /**
9688
+ * Periodic footage-retention sweep (recording-spec §6).
9689
+ *
9690
+ * The disk-pressure eviction path only fires when a volume is TOO FULL
9691
+ * (`shouldSaveDiskSpace` + `minFreePercent`); it never enforces a camera's
9692
+ * age/size policy. This sweeper closes that gap: on an hourly cadence it prunes
9693
+ * footage for every device whose operator set an actionable `retention` policy,
9694
+ * delegating the actual selection + delete to the `recording` cap's
9695
+ * `pruneFootage` (which owns the retention FLOOR via `selectEvictions` — this
9696
+ * sweeper only decides WHICH devices to prune and WHEN).
9697
+ *
9698
+ * Owns its own timer + single-flight guard so a slow sweep is never overlapped
9699
+ * by the next tick — the same subsystem-owns-its-timer shape as the
9700
+ * export janitor. Pure over injected deps (config read + prune + logger +
9701
+ * clock via `setInterval`), so it is unit-testable with fake timers and a mock
9702
+ * `prune`. Every pass is best-effort: a per-device prune failure is logged and
9703
+ * never aborts the rest, and the sweep never throws.
9704
+ */
9705
+ /**
9706
+ * True when the operator set an ACTIONABLE retention policy on a device — at
9707
+ * least one of `maxAgeDays` / `maxSizeGb` is present and positive. Mirrors the
9708
+ * `> 0` guards `selectEvictions` itself applies, so a `0`/absent limit (which
9709
+ * inherits the node default = "keep forever") is treated as NO policy and the
9710
+ * device is skipped. NEVER prune a device without an explicit policy — that
9711
+ * would delete footage the operator never asked to age out.
9712
+ */
9713
+ function hasRetentionPolicy(config) {
9714
+ const retention = config.retention;
9715
+ if (!retention) return false;
9716
+ const byAge = retention.maxAgeDays != null && retention.maxAgeDays > 0;
9717
+ const bySize = retention.maxSizeGb != null && retention.maxSizeGb > 0;
9718
+ return byAge || bySize;
9719
+ }
9720
+ var RetentionSweeper = class {
9721
+ deps;
9722
+ timer = null;
9723
+ /** Single-flight guard: true while a sweep is in flight. */
9724
+ sweeping = false;
9725
+ constructor(deps) {
9726
+ this.deps = deps;
9315
9727
  }
9316
9728
  /**
9317
- * Judge ONE observation against what the ledger already accepted, and advance
9318
- * it.
9319
- *
9320
- * Synchronous on purpose: the verdict is a function of the in-RAM mirror
9321
- * alone, so a decision can never be gated on an I/O that might fail (D49).
9322
- * The durable write is kicked off behind it and its failure changes no
9323
- * verdict.
9324
- *
9325
- * `no-flip` leaves the held row UNTOUCHED — including any timestamp it
9326
- * carries, which therefore means "when this key last CHANGED", not "when it
9327
- * last spoke". That is the timestamp anyone reading the table wants.
9729
+ * Arm the sweep: run immediately, then on the interval. Idempotent a second
9730
+ * `start()` while already armed is a no-op. The timer is `unref`'d so it never
9731
+ * keeps the process alive.
9328
9732
  */
9329
- observe(row) {
9330
- const equalFact = this.spec.equalFact;
9331
- if (equalFact === void 0) throw new Error(`DurableLedger(${this.spec.collection}): observe() requires the spec to declare equalFact`);
9332
- const key = this.spec.keyOf(row);
9333
- const held = this.mirror.get(key);
9334
- if (held !== void 0 && equalFact(held, row)) return "no-flip";
9335
- this.mirror.set(key, row);
9336
- this.persist(row);
9337
- return held === void 0 ? "seeded" : "flip";
9733
+ start() {
9734
+ if (this.timer !== null) return;
9735
+ const tick = () => {
9736
+ this.sweep();
9737
+ };
9738
+ tick();
9739
+ this.timer = setInterval(tick, this.deps.intervalMs);
9740
+ this.timer.unref?.();
9338
9741
  }
9339
- /**
9340
- * Upsert a row without a verdict — the write path for a ledger whose owner
9341
- * has already decided the value changed.
9342
- *
9343
- * The order of the mirror advance and the durable write is the spec's
9344
- * {@link DurableWriteMode}, not the call site's: two call sites that
9345
- * disagreed about it would be two different durability guarantees on one
9346
- * collection.
9347
- */
9348
- async put(row) {
9349
- const key = this.spec.keyOf(row);
9350
- if (this.spec.writeMode === "write-behind") {
9351
- this.mirror.set(key, row);
9352
- await this.persist(row);
9353
- return;
9742
+ /** Stop the sweep timer. Idempotent. An in-flight sweep still completes. */
9743
+ stop() {
9744
+ if (this.timer !== null) {
9745
+ clearInterval(this.timer);
9746
+ this.timer = null;
9354
9747
  }
9355
- await this.store.set.mutate({
9356
- collection: this.spec.collection,
9357
- key,
9358
- value: this.spec.toValue(row)
9359
- });
9360
- this.mirror.set(key, row);
9361
- }
9362
- /**
9363
- * Advance the mirror WITHOUT persisting, for an owner that deliberately
9364
- * coalesces its writes.
9365
- *
9366
- * The stationary registry is the reason this exists: a parked car is
9367
- * re-confirmed on every processed frame (5–30 Hz), and persisting each
9368
- * confirmation would offer thousands of commits a day to the checkpoint
9369
- * lottery (D96) to maintain a handful of rows. It stages the advance and
9370
- * flushes on its 5-minute sweep — 288 writes a day instead of ~10⁶.
9371
- *
9372
- * **The cost is stated, not hidden**: a staged value that is never flushed
9373
- * is lost on a crash. An owner may only stage a field whose staleness its
9374
- * own TTL absorbs. Anything that GATES work must go through {@link put} or
9375
- * {@link observe}.
9376
- */
9377
- stage(row) {
9378
- this.mirror.set(this.spec.keyOf(row), row);
9379
- }
9380
- /**
9381
- * Drop a key from the MIRROR only — the durable row survives.
9382
- *
9383
- * What a scope-unbind needs: this process stops holding the value, and a
9384
- * rebind reloads it from the store. Deliberately distinct from
9385
- * {@link forget}, which deletes; conflating the two is how an unbind turns
9386
- * into a wipe.
9387
- */
9388
- evict(key) {
9389
- this.mirror.delete(key);
9390
9748
  }
9391
9749
  /**
9392
- * Drop one key, mirror and row. Best-effort on the durable half: a failed
9393
- * delete leaves a row that the next load will re-mirror, which is a stale
9394
- * value rather than a lost one.
9750
+ * One retention pass. Single-flighted: a call that fires while a previous
9751
+ * sweep is still running is a no-op. Never throws a config-read failure
9752
+ * aborts THIS pass (logged); a per-device prune failure is logged and the
9753
+ * sweep continues with the remaining devices.
9395
9754
  */
9396
- async forget(key) {
9397
- this.mirror.delete(key);
9755
+ async sweep() {
9756
+ if (this.sweeping) {
9757
+ this.deps.logger.debug("recorder: retention sweep already running — skipping this tick");
9758
+ return;
9759
+ }
9760
+ this.sweeping = true;
9761
+ try {
9762
+ let configs;
9763
+ try {
9764
+ configs = await this.deps.readConfigs();
9765
+ } catch (err) {
9766
+ this.deps.logger.warn("recorder: retention sweep could not read device configs", { meta: { error: require_dist.errMsg(err) } });
9767
+ return;
9768
+ }
9769
+ const withPolicy = [...configs].filter(([, config]) => hasRetentionPolicy(config));
9770
+ const devicesSkipped = configs.size - withPolicy.length;
9771
+ this.deps.logger.info("recorder: retention sweep starting", { meta: {
9772
+ devices: withPolicy.map(([deviceId]) => deviceId),
9773
+ skipped: devicesSkipped
9774
+ } });
9775
+ let devicesSwept = 0;
9776
+ let bucketsPruned = 0;
9777
+ let bytesReclaimed = 0;
9778
+ for (const [deviceId] of withPolicy) {
9779
+ const startedMs = Date.now();
9780
+ try {
9781
+ const result = await this.deps.prune(deviceId);
9782
+ devicesSwept += 1;
9783
+ bucketsPruned += result.deletedBuckets;
9784
+ bytesReclaimed += result.reclaimedBytes;
9785
+ this.deps.logger.info("recorder: retention prune complete for device", {
9786
+ tags: { deviceId },
9787
+ meta: {
9788
+ deletedBuckets: result.deletedBuckets,
9789
+ reclaimedBytes: result.reclaimedBytes,
9790
+ ms: Date.now() - startedMs
9791
+ }
9792
+ });
9793
+ } catch (err) {
9794
+ this.deps.logger.warn("recorder: retention prune failed for device", {
9795
+ tags: { deviceId },
9796
+ meta: { error: require_dist.errMsg(err) }
9797
+ });
9798
+ }
9799
+ }
9800
+ this.deps.logger.info("recorder: retention sweep complete", { meta: {
9801
+ devicesSwept,
9802
+ devicesSkipped,
9803
+ bucketsPruned,
9804
+ bytesReclaimed
9805
+ } });
9806
+ } finally {
9807
+ this.sweeping = false;
9808
+ }
9809
+ }
9810
+ };
9811
+ //#endregion
9812
+ //#region src/recorder/addon/retired-root-keys.ts
9813
+ var RECORDER_RETIRED_ROOT_KEYS = [
9814
+ {
9815
+ key: "recordingOpsLog",
9816
+ successor: RECORDING_OPS_LOG_COLLECTION,
9817
+ reason: "the audit ring is one row per operation since the flatten; as a blob it was 147 KB of the 201 KB root row, re-parsed by every unrelated reader of that row"
9818
+ },
9819
+ {
9820
+ key: "recordingExports",
9821
+ successor: RECORDING_EXPORTS_COLLECTION,
9822
+ reason: "one row per export job since the flatten; as a blob a once-a-second progress patch rewrote 201 KB, and an overlapping createExport erased the row (2026-08-14)"
9823
+ },
9824
+ {
9825
+ key: "recordingConfigs",
9826
+ successor: RECORDING_CONFIGS_COLLECTION,
9827
+ reason: "one row per camera since the flatten; as a blob, asking what camera 617 records parsed every camera in the installation"
9828
+ }
9829
+ ];
9830
+ //#endregion
9831
+ //#region src/durable/durable-ledger.ts
9832
+ /** Default reseed cap — every current consumer's row set is installation-bounded. */
9833
+ var DEFAULT_LOAD_LIMIT = 1e5;
9834
+ var DurableLedger = class DurableLedger {
9835
+ mirror = /* @__PURE__ */ new Map();
9836
+ spec;
9837
+ store;
9838
+ logger;
9839
+ constructor(deps) {
9840
+ this.spec = deps.spec;
9841
+ this.store = deps.store;
9842
+ this.logger = deps.logger;
9843
+ }
9844
+ /**
9845
+ * Register the collection. MUST run at boot, before any read or write: the
9846
+ * SQLite backend answers 412 for an undeclared collection and takes the whole
9847
+ * runner down with it (the addon-ai boot-crash lesson).
9848
+ */
9849
+ static declare(store, spec) {
9850
+ return store.declareCollection.mutate({
9851
+ collection: spec.collection,
9852
+ columns: [...spec.columns],
9853
+ ...spec.indexes !== void 0 ? { indexes: [...spec.indexes] } : {}
9854
+ });
9855
+ }
9856
+ declare() {
9857
+ return DurableLedger.declare(this.store, this.spec);
9858
+ }
9859
+ /** The collection this ledger owns — for the caller's own log lines. */
9860
+ get collection() {
9861
+ return this.spec.collection;
9862
+ }
9863
+ /**
9864
+ * Boot reseed. Replaces the mirror with what the store holds and returns the
9865
+ * rows, so a caller that must hydrate something else (a watcher, a registry)
9866
+ * gets them without a second read.
9867
+ *
9868
+ * **A failure returns what is already mirrored** rather than clearing it —
9869
+ * see contract rule 2. The count is worth logging out loud at the call site:
9870
+ * "loaded 0" after a container recreate is the one line that explains a
9871
+ * silent flood.
9872
+ */
9873
+ async load() {
9874
+ try {
9875
+ const records = await this.store.query.query({
9876
+ collection: this.spec.collection,
9877
+ filter: { limit: this.spec.loadLimit ?? DEFAULT_LOAD_LIMIT }
9878
+ });
9879
+ const next = /* @__PURE__ */ new Map();
9880
+ let skipped = 0;
9881
+ for (const record of records) {
9882
+ const row = this.spec.fromRecord(record.id, record.data);
9883
+ if (row === null) {
9884
+ skipped += 1;
9885
+ continue;
9886
+ }
9887
+ next.set(this.spec.keyOf(row), row);
9888
+ }
9889
+ this.mirror.clear();
9890
+ for (const [key, row] of next) this.mirror.set(key, row);
9891
+ if (skipped > 0) this.logger.warn("durable rows skipped as malformed — they gate NOTHING", { meta: {
9892
+ collection: this.spec.collection,
9893
+ skipped
9894
+ } });
9895
+ return [...this.mirror.values()];
9896
+ } catch (err) {
9897
+ this.logger.warn("durable load failed — keeping the state already in memory", { meta: {
9898
+ collection: this.spec.collection,
9899
+ error: String(err),
9900
+ held: this.mirror.size
9901
+ } });
9902
+ return [...this.mirror.values()];
9903
+ }
9904
+ }
9905
+ /** Every mirrored row, insertion-ordered. */
9906
+ snapshot() {
9907
+ return [...this.mirror.values()];
9908
+ }
9909
+ /** The row currently held for a key, if any. Pure RAM — never I/O. */
9910
+ get(key) {
9911
+ return this.mirror.get(key);
9912
+ }
9913
+ has(key) {
9914
+ return this.mirror.has(key);
9915
+ }
9916
+ get size() {
9917
+ return this.mirror.size;
9918
+ }
9919
+ /**
9920
+ * Judge ONE observation against what the ledger already accepted, and advance
9921
+ * it.
9922
+ *
9923
+ * Synchronous on purpose: the verdict is a function of the in-RAM mirror
9924
+ * alone, so a decision can never be gated on an I/O that might fail (D49).
9925
+ * The durable write is kicked off behind it and its failure changes no
9926
+ * verdict.
9927
+ *
9928
+ * `no-flip` leaves the held row UNTOUCHED — including any timestamp it
9929
+ * carries, which therefore means "when this key last CHANGED", not "when it
9930
+ * last spoke". That is the timestamp anyone reading the table wants.
9931
+ */
9932
+ observe(row) {
9933
+ const equalFact = this.spec.equalFact;
9934
+ if (equalFact === void 0) throw new Error(`DurableLedger(${this.spec.collection}): observe() requires the spec to declare equalFact`);
9935
+ const key = this.spec.keyOf(row);
9936
+ const held = this.mirror.get(key);
9937
+ if (held !== void 0 && equalFact(held, row)) return "no-flip";
9938
+ this.mirror.set(key, row);
9939
+ this.persist(row);
9940
+ return held === void 0 ? "seeded" : "flip";
9941
+ }
9942
+ /**
9943
+ * Upsert a row without a verdict — the write path for a ledger whose owner
9944
+ * has already decided the value changed.
9945
+ *
9946
+ * The order of the mirror advance and the durable write is the spec's
9947
+ * {@link DurableWriteMode}, not the call site's: two call sites that
9948
+ * disagreed about it would be two different durability guarantees on one
9949
+ * collection.
9950
+ */
9951
+ async put(row) {
9952
+ const key = this.spec.keyOf(row);
9953
+ if (this.spec.writeMode === "write-behind") {
9954
+ this.mirror.set(key, row);
9955
+ await this.persist(row);
9956
+ return;
9957
+ }
9958
+ await this.store.set.mutate({
9959
+ collection: this.spec.collection,
9960
+ key,
9961
+ value: this.spec.toValue(row)
9962
+ });
9963
+ this.mirror.set(key, row);
9964
+ }
9965
+ /**
9966
+ * Advance the mirror WITHOUT persisting, for an owner that deliberately
9967
+ * coalesces its writes.
9968
+ *
9969
+ * The stationary registry is the reason this exists: a parked car is
9970
+ * re-confirmed on every processed frame (5–30 Hz), and persisting each
9971
+ * confirmation would offer thousands of commits a day to the checkpoint
9972
+ * lottery (D96) to maintain a handful of rows. It stages the advance and
9973
+ * flushes on its 5-minute sweep — 288 writes a day instead of ~10⁶.
9974
+ *
9975
+ * **The cost is stated, not hidden**: a staged value that is never flushed
9976
+ * is lost on a crash. An owner may only stage a field whose staleness its
9977
+ * own TTL absorbs. Anything that GATES work must go through {@link put} or
9978
+ * {@link observe}.
9979
+ */
9980
+ stage(row) {
9981
+ this.mirror.set(this.spec.keyOf(row), row);
9982
+ }
9983
+ /**
9984
+ * Drop a key from the MIRROR only — the durable row survives.
9985
+ *
9986
+ * What a scope-unbind needs: this process stops holding the value, and a
9987
+ * rebind reloads it from the store. Deliberately distinct from
9988
+ * {@link forget}, which deletes; conflating the two is how an unbind turns
9989
+ * into a wipe.
9990
+ */
9991
+ evict(key) {
9992
+ this.mirror.delete(key);
9993
+ }
9994
+ /**
9995
+ * Drop one key, mirror and row. Best-effort on the durable half: a failed
9996
+ * delete leaves a row that the next load will re-mirror, which is a stale
9997
+ * value rather than a lost one.
9998
+ */
9999
+ async forget(key) {
10000
+ this.mirror.delete(key);
9398
10001
  try {
9399
10002
  await this.store.delete.mutate({
9400
10003
  collection: this.spec.collection,
@@ -9416,707 +10019,408 @@ var DurableLedger = class DurableLedger {
9416
10019
  * is a method a feature opts into rather than a policy the primitive runs.
9417
10020
  * Best-effort per row: a failed delete keeps the key (retried next prune)
9418
10021
  * rather than aborting the sweep.
9419
- */
9420
- async pruneExcept(activeKeys) {
9421
- let pruned = 0;
9422
- for (const key of [...this.mirror.keys()]) {
9423
- if (activeKeys.has(key)) continue;
9424
- try {
9425
- await this.store.delete.mutate({
9426
- collection: this.spec.collection,
9427
- key
9428
- });
9429
- this.mirror.delete(key);
9430
- pruned += 1;
9431
- } catch (err) {
9432
- this.logger.debug("durable prune delete failed", { meta: {
9433
- collection: this.spec.collection,
9434
- key,
9435
- error: String(err)
9436
- } });
9437
- }
9438
- }
9439
- return pruned;
9440
- }
9441
- /**
9442
- * Write-behind durable upsert. Best-effort and logged, never thrown at the
9443
- * decision path: the mirror already holds the truth for this process, and the
9444
- * worst a lost write can do is one silent re-seed after the next restart.
9445
- */
9446
- async persist(row) {
9447
- const deviceId = this.spec.deviceIdOf?.(row);
9448
- try {
9449
- await this.store.set.mutate({
9450
- collection: this.spec.collection,
9451
- key: this.spec.keyOf(row),
9452
- value: this.spec.toValue(row)
9453
- });
9454
- } catch (err) {
9455
- this.logger.warn("durable persist failed — this key may re-seed on boot", {
9456
- ...deviceId !== void 0 ? { tags: { deviceId } } : {},
9457
- meta: {
9458
- collection: this.spec.collection,
9459
- key: this.spec.keyOf(row),
9460
- error: String(err)
9461
- }
9462
- });
9463
- }
9464
- }
9465
- };
9466
- //#endregion
9467
- //#region src/recorder/addon/segment-hour-ledger.ts
9468
- var HOUR_MS$1 = 36e5;
9469
- /**
9470
- * @durable class=mirror owner=recorder
9471
- * write="write-behind on every finalized segment (hour-row upsert); eviction drops paths; the archive walk reconciles disk into the row set"
9472
- * retention="a row goes when the walk or eviction confirms the hour is empty — never by age, never from the ledger alone (D148)"
9473
- */
9474
- var RECORDING_SEGMENT_HOURS_COLLECTION = "recorder:segment-hours";
9475
- var RECORDING_SEGMENT_HOURS_COLUMNS = [
9476
- {
9477
- name: "key",
9478
- type: "TEXT",
9479
- primaryKey: true,
9480
- notNull: true
9481
- },
9482
- {
9483
- name: "deviceId",
9484
- type: "INTEGER",
9485
- notNull: true
9486
- },
9487
- {
9488
- name: "profile",
9489
- type: "TEXT",
9490
- notNull: true
9491
- },
9492
- {
9493
- name: "locationId",
9494
- type: "TEXT",
9495
- notNull: true
9496
- },
9497
- {
9498
- name: "hourStartMs",
9499
- type: "INTEGER",
9500
- notNull: true
9501
- },
9502
- {
9503
- name: "paths",
9504
- type: "TEXT",
9505
- notNull: true
9506
- }
9507
- ];
9508
- var RECORDING_SEGMENT_HOURS_INDEXES = [{
9509
- name: "idx_recorder_segment_hours_device",
9510
- columns: ["deviceId"]
9511
- }, {
9512
- name: "idx_recorder_segment_hours_hour",
9513
- columns: ["hourStartMs"]
9514
- }];
9515
- /**
9516
- * The largest share of a device's held hours a single walk may delete.
9517
- *
9518
- * On 2026-08-19 the recorder respawned while `/recordings` was an empty tmpfs —
9519
- * the UD disk had not mounted — and one walk took the ledger from 873 hours to
9520
- * 32 (a 96% drop). The ledger is precisely what lets the NEXT boot skip a
9521
- * ~400 s recursive walk, so a single bad walk taxed every boot after it. A
9522
- * retention sweep does not delete half a camera's archive in one pass; a
9523
- * `readdir` over an unmounted root deletes all of it.
9524
- */
9525
- var LEDGER_PRUNE_MAX_SHARE = .5;
9526
- function hourStartMs(startMs) {
9527
- return Math.floor(startMs / HOUR_MS$1) * HOUR_MS$1;
9528
- }
9529
- function segmentHourKey(deviceId, profile, locationId, hour) {
9530
- return `${deviceId}|${profile}|${locationId}|${hour}`;
9531
- }
9532
- function hourOf(row) {
9533
- const hour = hourStartMs(row.startMs);
9534
- return {
9535
- hourStartMs: hour,
9536
- key: segmentHourKey(row.deviceId, row.profile, row.locationId, hour)
9537
- };
9538
- }
9539
- function uniquePaths(paths) {
9540
- const seen = /* @__PURE__ */ new Set();
9541
- const out = [];
9542
- for (const p of paths) {
9543
- if (seen.has(p)) continue;
9544
- seen.add(p);
9545
- out.push(p);
9546
- }
9547
- return out;
9548
- }
9549
- function samePaths(a, b) {
9550
- if (a.length !== b.length) return false;
9551
- const set = new Set(a);
9552
- for (const p of b) if (!set.has(p)) return false;
9553
- return true;
9554
- }
9555
- function rowToValue(row) {
9556
- return {
9557
- deviceId: row.deviceId,
9558
- profile: row.profile,
9559
- locationId: row.locationId,
9560
- hourStartMs: row.hourStartMs,
9561
- paths: JSON.stringify(row.paths)
9562
- };
9563
- }
9564
- function recordToRow(key, data) {
9565
- const deviceId = Number(data["deviceId"]);
9566
- const profile = data["profile"];
9567
- const locationId = data["locationId"];
9568
- const hour = Number(data["hourStartMs"]);
9569
- let raw = data["paths"];
9570
- if (typeof raw === "string") try {
9571
- raw = JSON.parse(raw);
9572
- } catch {
9573
- return null;
9574
- }
9575
- 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;
9576
- return {
9577
- key,
9578
- deviceId,
9579
- profile,
9580
- locationId,
9581
- hourStartMs: hour,
9582
- paths: raw
9583
- };
9584
- }
9585
- var SPEC = {
9586
- collection: RECORDING_SEGMENT_HOURS_COLLECTION,
9587
- columns: RECORDING_SEGMENT_HOURS_COLUMNS,
9588
- indexes: RECORDING_SEGMENT_HOURS_INDEXES,
9589
- writeMode: "write-behind",
9590
- loadLimit: 1e5,
9591
- keyOf: (row) => row.key,
9592
- toValue: rowToValue,
9593
- fromRecord: recordToRow,
9594
- deviceIdOf: (row) => row.deviceId
9595
- };
9596
- var SegmentHourLedger = class {
9597
- ledger;
9598
- logger;
9599
- isLocationUsable;
9600
- constructor(deps) {
9601
- this.logger = deps.logger;
9602
- this.isLocationUsable = deps.isLocationUsable ?? (() => true);
9603
- this.ledger = new DurableLedger({
9604
- spec: SPEC,
9605
- store: deps.store,
9606
- logger: deps.logger
9607
- });
9608
- }
9609
- static declare(store) {
9610
- return DurableLedger.declare(store, SPEC);
9611
- }
9612
- declare() {
9613
- return this.ledger.declare();
9614
- }
9615
- async load() {
9616
- const rows = await this.ledger.load();
9617
- this.logger.info("recorder: segment-hour ledger loaded", { meta: {
9618
- hours: rows.length,
9619
- collection: RECORDING_SEGMENT_HOURS_COLLECTION
9620
- } });
9621
- return rows;
9622
- }
9623
- snapshot() {
9624
- return this.ledger.snapshot();
9625
- }
9626
- /**
9627
- * Seed {@link RecordingIndex} from the mirrored hour rows.
9628
- *
9629
- * Hours present in the ledger become `hydrated` so a seek in those windows
9630
- * does not pay for a disk walk. Hours the ledger never held stay `unknown`
9631
- * — that is the fail-toward-walk direction.
9632
- */
9633
- hydrateIndex(index) {
9634
- for (const hour of this.ledger.snapshot()) index.hydrateHour(hour.deviceId, hour.locationId, hour.hourStartMs, hour.paths);
9635
- }
9636
- /** Write-behind: a just-finalized segment joins its hour row. */
9637
- async recordSegment(row) {
9638
- const { key, hourStartMs: hour } = hourOf(row);
9639
- const held = this.ledger.get(key);
9640
- if (held !== void 0 && held.paths.includes(row.path)) return;
9641
- await this.ledger.put({
9642
- key,
9643
- deviceId: row.deviceId,
9644
- profile: row.profile,
9645
- locationId: row.locationId,
9646
- hourStartMs: hour,
9647
- paths: held === void 0 ? [row.path] : [...held.paths, row.path]
9648
- });
9649
- }
9650
- /**
9651
- * Eviction confirmed these paths are gone from disk. Drop them; forget the
9652
- * hour when it holds nothing.
9653
- */
9654
- async dropSegments(rows) {
9655
- const doomed = /* @__PURE__ */ new Map();
9656
- for (const row of rows) {
9657
- const { key } = hourOf(row);
9658
- let set = doomed.get(key);
9659
- if (!set) {
9660
- set = /* @__PURE__ */ new Set();
9661
- doomed.set(key, set);
9662
- }
9663
- set.add(row.path);
9664
- }
9665
- for (const [key, paths] of doomed) {
9666
- const held = this.ledger.get(key);
9667
- if (held === void 0) continue;
9668
- const next = held.paths.filter((p) => !paths.has(p));
9669
- if (next.length === 0) {
9670
- await this.ledger.forget(key);
9671
- continue;
9672
- }
9673
- if (next.length === held.paths.length) continue;
9674
- await this.ledger.put({
9675
- ...held,
9676
- paths: next
9677
- });
9678
- }
9679
- }
9680
- /**
9681
- * After a disk walk of `deviceIds`, make the ledger match the index — with
9682
- * two D148 exceptions:
9683
- *
9684
- * - an hour the walk did not see is dropped ONLY if it is in the past;
9685
- * the current hour may still hold a live writer whose file the readdir
9686
- * raced;
9687
- * - the current hour UNIONs ledger paths with index paths rather than
9688
- * replacing, for the same race.
9689
- *
9690
- * Devices not in `deviceIds` are left untouched (a partial walk must not
9691
- * prune cameras it did not look at).
9692
- *
9693
- * And a walk this ledger does not TRUST prunes nothing at all — see
9694
- * {@link refusedPrunes}. What such a walk FOUND is still real, so its paths
9695
- * are unioned in; what it did not find decides nothing (D49).
9696
- */
9697
- async reconcileFromIndex(index, deviceIds, nowMs) {
9698
- const walked = new Set(deviceIds);
9699
- const currentHour = hourStartMs(nowMs);
9700
- const fromIndex = /* @__PURE__ */ new Map();
9701
- for (const deviceId of deviceIds) for (const seg of index.segments(deviceId)) {
9702
- const { key, hourStartMs: hour } = hourOf(seg);
9703
- const existing = fromIndex.get(key);
9704
- if (existing === void 0) fromIndex.set(key, {
9705
- key,
9706
- deviceId: seg.deviceId,
9707
- profile: seg.profile,
9708
- locationId: seg.locationId,
9709
- hourStartMs: hour,
9710
- paths: [seg.path]
9711
- });
9712
- else fromIndex.set(key, {
9713
- ...existing,
9714
- paths: [...existing.paths, seg.path]
9715
- });
9716
- }
9717
- const refused = this.refusedPrunes(walked, fromIndex);
9718
- for (const row of this.ledger.snapshot()) {
9719
- if (!walked.has(row.deviceId)) continue;
9720
- const disk = fromIndex.get(row.key);
9721
- const additiveOnly = row.hourStartMs === currentHour || refused.devices.has(row.deviceId) || refused.locations.has(locationKey(row.deviceId, row.locationId));
9722
- if (disk === void 0) {
9723
- if (additiveOnly) continue;
9724
- await this.ledger.forget(row.key);
9725
- continue;
9726
- }
9727
- if (additiveOnly) {
9728
- const union = uniquePaths([...disk.paths, ...row.paths]);
9729
- if (!samePaths(union, row.paths)) await this.ledger.put({
9730
- ...row,
9731
- paths: union
9732
- });
9733
- continue;
9734
- }
9735
- if (!samePaths(row.paths, disk.paths)) await this.ledger.put(disk);
9736
- }
9737
- for (const [key, disk] of fromIndex) {
9738
- if (this.ledger.has(key)) continue;
9739
- await this.ledger.put(disk);
9740
- }
9741
- }
9742
- /**
9743
- * Which of this walk's deletions are REFUSED, and why.
9744
- *
9745
- * Two independent reasons, because the live fault of 2026-08-19 would have
9746
- * escaped either one alone:
9747
- *
9748
- * - **the volume said no.** `PlacementService`'s marker probe knows the bytes
9749
- * under a root are not the bytes we wrote. A walk of such a root found
9750
- * nothing because nothing is mounted there, not because the footage is
9751
- * gone.
9752
- * - **the magnitude said no.** At BOOT the probe has not run yet — it runs at
9753
- * the first writer attach — so on the morning the recorder respawned onto an
9754
- * empty tmpfs the volume was not flagged and the prune went through: 873
9755
- * hours to 32. A walk that deletes more than
9756
- * {@link LEDGER_PRUNE_MAX_SHARE} of a device's held hours is refused on its
9757
- * size alone.
9758
- *
9759
- * A refusal is one line per device (or per device+location), carrying the
9760
- * delta, tagged with `deviceId` — a ledger that silently declined to prune and
9761
- * a ledger with nothing to prune must never read the same.
9762
- */
9763
- refusedPrunes(walked, fromIndex) {
9764
- const heldByDevice = /* @__PURE__ */ new Map();
9765
- const unusable = /* @__PURE__ */ new Map();
9766
- for (const row of this.ledger.snapshot()) {
9767
- if (!walked.has(row.deviceId)) continue;
9768
- heldByDevice.set(row.deviceId, (heldByDevice.get(row.deviceId) ?? 0) + 1);
9769
- if (this.isLocationUsable(row.locationId)) continue;
9770
- const key = locationKey(row.deviceId, row.locationId);
9771
- const seen = unusable.get(key);
9772
- unusable.set(key, seen === void 0 ? {
9773
- deviceId: row.deviceId,
9774
- locationId: row.locationId,
9775
- held: 1
9776
- } : {
9777
- ...seen,
9778
- held: seen.held + 1
9779
- });
9780
- }
9781
- const onDiskByDevice = /* @__PURE__ */ new Map();
9782
- for (const row of fromIndex.values()) onDiskByDevice.set(row.deviceId, (onDiskByDevice.get(row.deviceId) ?? 0) + 1);
9783
- const devices = /* @__PURE__ */ new Set();
9784
- for (const [deviceId, held] of heldByDevice) {
9785
- if (held < 8) continue;
9786
- const onDisk = onDiskByDevice.get(deviceId) ?? 0;
9787
- if (onDisk >= held * .5) continue;
9788
- devices.add(deviceId);
9789
- this.logger.warn("recorder: segment-hour ledger prune REFUSED — this walk would delete most of the camera archive, which is what an unmounted root looks like; keeping the previous ledger", {
9790
- tags: { deviceId },
9791
- meta: {
9792
- held,
9793
- onDisk,
9794
- dropped: held - onDisk,
9795
- maxShare: LEDGER_PRUNE_MAX_SHARE
9796
- }
9797
- });
9798
- }
9799
- const locations = /* @__PURE__ */ new Set();
9800
- for (const [key, entry] of unusable) {
9801
- if (devices.has(entry.deviceId)) continue;
9802
- locations.add(key);
9803
- this.logger.warn("recorder: segment-hour ledger prune REFUSED — the volume under this location failed its probe, so the walk found nothing because nothing is mounted there", {
9804
- tags: { deviceId: entry.deviceId },
9805
- meta: {
9806
- locationId: entry.locationId,
9807
- held: entry.held,
9808
- onDisk: onDiskByDevice.get(entry.deviceId) ?? 0
9809
- }
9810
- });
9811
- }
9812
- return {
9813
- devices,
9814
- locations
9815
- };
9816
- }
9817
- };
9818
- function locationKey(deviceId, locationId) {
9819
- return `${deviceId}|${locationId}`;
9820
- }
9821
- //#endregion
9822
- //#region src/recorder/addon/recording-export-provider.ts
9823
- /** Content type of every rendered export — the engine only ever writes MP4. */
9824
- var EXPORT_CONTENT_TYPE = "video/mp4";
9825
- /** Filename bound, extension included. Long enough to stay recognisable. */
9826
- var EXPORT_NAME_MAX = 64;
9827
- /**
9828
- * The filename a RECIPIENT sees on the attachment.
9829
- *
9830
- * Derived from the operator's own export title so a delivered video is
9831
- * identifiable in a chat thread, but slugified and BOUNDED: the title field
9832
- * accepts 200 characters and several notifier backends silently mangle a
9833
- * filename that long. No usable title degrades to the export id, never to an
9834
- * empty name.
9835
- */
9836
- function exportFileName(rec) {
9837
- const slug = (rec.title ?? "").replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-+|-+$/g, "").slice(0, EXPORT_NAME_MAX - 4).replace(/-+$/g, "");
9838
- return slug.length > 0 ? `${slug}.mp4` : `export-${rec.id}.mp4`;
9839
- }
9840
- /**
9841
- * True when at least one indexed segment overlaps `[fromMs, toMs)`. Mirrors the
9842
- * `buildPlaybackManifest` overlap idiom: a segment `[startMs, startMs+durMs)`
9843
- * overlaps the window iff it starts before the window ends AND ends after the
9844
- * window starts.
9845
- */
9846
- async function hasFootage(index, deviceId, profile, fromMs, toMs) {
9847
- return (await index.segmentsInWindow(deviceId, profile, fromMs, toMs)).some((s) => s.startMs < toMs && s.startMs + s.durMs > fromMs);
9848
- }
9849
- /** Canonical `profiles[]`, falling back to the legacy singular `profile`. */
9850
- function resolveExportProfiles(input) {
9851
- if (input.profiles !== void 0 && input.profiles.length > 0) return [...input.profiles];
9852
- if (typeof input.profile === "string" && input.profile.length > 0) return [input.profile];
9853
- return [];
9854
- }
9855
- /** Load a record or throw a clear NOT_FOUND-style error. */
9856
- async function requireRecord(state, exportId) {
9857
- const rec = await getExportRecord(state, exportId);
9858
- if (!rec) throw new Error(`recording-export: unknown export "${exportId}"`);
9859
- return rec;
9860
- }
9861
- /** Build the `recordingExport` provider over the injected deps. */
9862
- function buildRecordingExportProvider(deps) {
9863
- return {
9864
- createExport: async (input) => {
9865
- const { deviceId, fromMs, toMs, options } = input;
9866
- const profiles = resolveExportProfiles(input);
9867
- if (profiles.length < 1) throw new Error("recording-export: profiles must contain at least one profile");
9868
- if (!(fromMs < toMs)) throw new Error(`recording-export: invalid range ${fromMs}..${toMs}`);
9869
- 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}`);
9870
- const now = deps.now();
9871
- let first = null;
9872
- for (const profile of profiles) {
9873
- const record = {
9874
- id: deps.newId(),
9875
- deviceId,
9876
- profile,
9877
- fromMs,
9878
- toMs,
9879
- options,
9880
- state: "queued",
9881
- progressPct: null,
9882
- fileBytes: null,
9883
- expiresAt: now + options.maxLifeMs,
9884
- deleteAfterDownload: options.deleteAfterDownload,
9885
- downloadedAt: null,
9886
- createdAt: now,
9887
- createdBy: deps.createdBy(),
9888
- error: null
9889
- };
9890
- const saved = await upsertExport(deps.state, record);
9891
- deps.engine.enqueue(saved.id);
9892
- first ??= saved;
9893
- }
9894
- return first;
9895
- },
9896
- listExports: ({ deviceId }) => listExportsFor(deps.state, deviceId),
9897
- getExport: ({ exportId }) => requireRecord(deps.state, exportId),
9898
- cancelExport: async ({ exportId }) => {
9899
- const existing = await requireRecord(deps.state, exportId);
9900
- await deps.engine.cancel(exportId);
9901
- return await getExportRecord(deps.state, exportId) ?? existing;
9902
- },
9903
- deleteExport: async ({ exportId }) => {
9904
- const existing = await requireRecord(deps.state, exportId);
9905
- await deps.removeFile(deps.outPath(existing));
9906
- return await patchExport(deps.state, exportId, {
9907
- state: "deleted",
9908
- fileBytes: null
9909
- }) ?? existing;
9910
- },
9911
- getDownloadUrl: async ({ exportId }) => {
9912
- await requireRecord(deps.state, exportId);
9913
- const url = deps.downloadUrlFor(exportId);
9914
- return {
9915
- url,
9916
- endpoints: [url]
9917
- };
9918
- },
9919
- readExportBytes: async ({ exportId }) => {
9920
- const rec = await requireRecord(deps.state, exportId);
9921
- const tags = { deviceId: rec.deviceId };
9922
- if (rec.state !== "ready") {
9923
- deps.logger.warn("export byte read refused: export is not ready", {
9924
- tags,
9925
- meta: {
9926
- exportId,
9927
- state: rec.state
9928
- }
9929
- });
9930
- throw new Error(`recording-export: export "${exportId}" is not ready (${rec.state})`);
9931
- }
9932
- if (deps.now() >= rec.expiresAt) {
9933
- deps.logger.warn("export byte read refused: export has expired", {
9934
- tags,
9935
- meta: {
9936
- exportId,
9937
- expiresAt: rec.expiresAt
9938
- }
9939
- });
9940
- throw new Error(`recording-export: export "${exportId}" has expired`);
9941
- }
9942
- const file = deps.outPath(rec);
9943
- let size;
9944
- try {
9945
- size = await deps.statBytes(file);
9946
- } catch (err) {
9947
- deps.logger.warn("export byte read refused: file is missing", {
9948
- tags,
9949
- meta: {
9950
- exportId,
9951
- error: String(err)
9952
- }
9953
- });
9954
- throw new Error(`recording-export: export "${exportId}" has no file on disk`, { cause: err });
9955
- }
9956
- if (size > 52428800) {
9957
- deps.logger.warn("export byte read refused: file is too large to inline", {
9958
- tags,
9959
- meta: {
9960
- exportId,
9961
- bytes: size,
9962
- maxBytes: require_dist.RECORDING_EXPORT_MAX_READ_BYTES
9963
- }
10022
+ */
10023
+ async pruneExcept(activeKeys) {
10024
+ let pruned = 0;
10025
+ for (const key of [...this.mirror.keys()]) {
10026
+ if (activeKeys.has(key)) continue;
10027
+ try {
10028
+ await this.store.delete.mutate({
10029
+ collection: this.spec.collection,
10030
+ key
9964
10031
  });
9965
- throw new Error(`recording-export: export "${exportId}" is too large to read inline (${size} > ${require_dist.RECORDING_EXPORT_MAX_READ_BYTES} bytes)`);
10032
+ this.mirror.delete(key);
10033
+ pruned += 1;
10034
+ } catch (err) {
10035
+ this.logger.debug("durable prune delete failed", { meta: {
10036
+ collection: this.spec.collection,
10037
+ key,
10038
+ error: String(err)
10039
+ } });
9966
10040
  }
9967
- const bytes = await deps.readFile(file);
9968
- return {
9969
- base64: bytes.toString("base64"),
9970
- contentType: EXPORT_CONTENT_TYPE,
9971
- name: exportFileName({
9972
- id: rec.id,
9973
- ...rec.options.title !== void 0 ? { title: rec.options.title } : {}
9974
- }),
9975
- bytes: bytes.byteLength
9976
- };
9977
10041
  }
9978
- };
9979
- }
10042
+ return pruned;
10043
+ }
10044
+ /**
10045
+ * Write-behind durable upsert. Best-effort and logged, never thrown at the
10046
+ * decision path: the mirror already holds the truth for this process, and the
10047
+ * worst a lost write can do is one silent re-seed after the next restart.
10048
+ */
10049
+ async persist(row) {
10050
+ const deviceId = this.spec.deviceIdOf?.(row);
10051
+ try {
10052
+ await this.store.set.mutate({
10053
+ collection: this.spec.collection,
10054
+ key: this.spec.keyOf(row),
10055
+ value: this.spec.toValue(row)
10056
+ });
10057
+ } catch (err) {
10058
+ this.logger.warn("durable persist failed — this key may re-seed on boot", {
10059
+ ...deviceId !== void 0 ? { tags: { deviceId } } : {},
10060
+ meta: {
10061
+ collection: this.spec.collection,
10062
+ key: this.spec.keyOf(row),
10063
+ error: String(err)
10064
+ }
10065
+ });
10066
+ }
10067
+ }
10068
+ };
9980
10069
  //#endregion
9981
- //#region src/recorder/addon/recording-hub-host.ts
10070
+ //#region src/recorder/addon/segment-hour-ledger.ts
10071
+ var HOUR_MS$1 = 36e5;
9982
10072
  /**
9983
- * Resolve the host a REMOTE recording node should use to dial the hub-only
9984
- * stream-broker's restream port.
9985
- *
9986
- * The implementation was promoted to the package-shared
9987
- * `src/shared/hub-hostname.ts` in P2d (cross-node Phase 2) so the
9988
- * pipeline-runner's remote-source leg shares the exact same resolver instead
9989
- * of copying it. This module keeps the recorder-facing name as a thin alias —
9990
- * resolution order and semantics are unchanged (operator override first, then
9991
- * the host extracted from `CAMSTACK_HUB_URL`).
10073
+ * @durable class=mirror owner=recorder
10074
+ * write="write-behind on every finalized segment (hour-row upsert); eviction drops paths; the archive walk reconciles disk into the row set"
10075
+ * retention="a row goes when the walk or eviction confirms the hour is empty — never by age, never from the ledger alone (D148)"
9992
10076
  */
9993
- var resolveRecordingHubHostname = require_hub_hostname.resolveHubHostname;
9994
- //#endregion
9995
- //#region src/recorder/addon/retention-sweep.ts
10077
+ var RECORDING_SEGMENT_HOURS_COLLECTION = "recorder:segment-hours";
10078
+ var RECORDING_SEGMENT_HOURS_COLUMNS = [
10079
+ {
10080
+ name: "key",
10081
+ type: "TEXT",
10082
+ primaryKey: true,
10083
+ notNull: true
10084
+ },
10085
+ {
10086
+ name: "deviceId",
10087
+ type: "INTEGER",
10088
+ notNull: true
10089
+ },
10090
+ {
10091
+ name: "profile",
10092
+ type: "TEXT",
10093
+ notNull: true
10094
+ },
10095
+ {
10096
+ name: "locationId",
10097
+ type: "TEXT",
10098
+ notNull: true
10099
+ },
10100
+ {
10101
+ name: "hourStartMs",
10102
+ type: "INTEGER",
10103
+ notNull: true
10104
+ },
10105
+ {
10106
+ name: "paths",
10107
+ type: "TEXT",
10108
+ notNull: true
10109
+ }
10110
+ ];
10111
+ var RECORDING_SEGMENT_HOURS_INDEXES = [{
10112
+ name: "idx_recorder_segment_hours_device",
10113
+ columns: ["deviceId"]
10114
+ }, {
10115
+ name: "idx_recorder_segment_hours_hour",
10116
+ columns: ["hourStartMs"]
10117
+ }];
9996
10118
  /**
9997
- * Periodic footage-retention sweep (recording-spec §6).
9998
- *
9999
- * The disk-pressure eviction path only fires when a volume is TOO FULL
10000
- * (`shouldSaveDiskSpace` + `minFreePercent`); it never enforces a camera's
10001
- * age/size policy. This sweeper closes that gap: on an hourly cadence it prunes
10002
- * footage for every device whose operator set an actionable `retention` policy,
10003
- * delegating the actual selection + delete to the `recording` cap's
10004
- * `pruneFootage` (which owns the retention FLOOR via `selectEvictions` — this
10005
- * sweeper only decides WHICH devices to prune and WHEN).
10119
+ * The largest share of a device's held hours a single walk may delete.
10006
10120
  *
10007
- * Owns its own timer + single-flight guard so a slow sweep is never overlapped
10008
- * by the next tick — the same subsystem-owns-its-timer shape as the
10009
- * export janitor. Pure over injected deps (config read + prune + logger +
10010
- * clock via `setInterval`), so it is unit-testable with fake timers and a mock
10011
- * `prune`. Every pass is best-effort: a per-device prune failure is logged and
10012
- * never aborts the rest, and the sweep never throws.
10013
- */
10014
- /**
10015
- * True when the operator set an ACTIONABLE retention policy on a device — at
10016
- * least one of `maxAgeDays` / `maxSizeGb` is present and positive. Mirrors the
10017
- * `> 0` guards `selectEvictions` itself applies, so a `0`/absent limit (which
10018
- * inherits the node default = "keep forever") is treated as NO policy and the
10019
- * device is skipped. NEVER prune a device without an explicit policy — that
10020
- * would delete footage the operator never asked to age out.
10121
+ * On 2026-08-19 the recorder respawned while `/recordings` was an empty tmpfs
10122
+ * the UD disk had not mounted and one walk took the ledger from 873 hours to
10123
+ * 32 (a 96% drop). The ledger is precisely what lets the NEXT boot skip a
10124
+ * ~400 s recursive walk, so a single bad walk taxed every boot after it. A
10125
+ * retention sweep does not delete half a camera's archive in one pass; a
10126
+ * `readdir` over an unmounted root deletes all of it.
10021
10127
  */
10022
- function hasRetentionPolicy(config) {
10023
- const retention = config.retention;
10024
- if (!retention) return false;
10025
- const byAge = retention.maxAgeDays != null && retention.maxAgeDays > 0;
10026
- const bySize = retention.maxSizeGb != null && retention.maxSizeGb > 0;
10027
- return byAge || bySize;
10128
+ var LEDGER_PRUNE_MAX_SHARE = .5;
10129
+ function hourStartMs(startMs) {
10130
+ return Math.floor(startMs / HOUR_MS$1) * HOUR_MS$1;
10028
10131
  }
10029
- var RetentionSweeper = class {
10030
- deps;
10031
- timer = null;
10032
- /** Single-flight guard: true while a sweep is in flight. */
10033
- sweeping = false;
10132
+ function segmentHourKey(deviceId, profile, locationId, hour) {
10133
+ return `${deviceId}|${profile}|${locationId}|${hour}`;
10134
+ }
10135
+ function hourOf(row) {
10136
+ const hour = hourStartMs(row.startMs);
10137
+ return {
10138
+ hourStartMs: hour,
10139
+ key: segmentHourKey(row.deviceId, row.profile, row.locationId, hour)
10140
+ };
10141
+ }
10142
+ function uniquePaths(paths) {
10143
+ const seen = /* @__PURE__ */ new Set();
10144
+ const out = [];
10145
+ for (const p of paths) {
10146
+ if (seen.has(p)) continue;
10147
+ seen.add(p);
10148
+ out.push(p);
10149
+ }
10150
+ return out;
10151
+ }
10152
+ function samePaths(a, b) {
10153
+ if (a.length !== b.length) return false;
10154
+ const set = new Set(a);
10155
+ for (const p of b) if (!set.has(p)) return false;
10156
+ return true;
10157
+ }
10158
+ function rowToValue(row) {
10159
+ return {
10160
+ deviceId: row.deviceId,
10161
+ profile: row.profile,
10162
+ locationId: row.locationId,
10163
+ hourStartMs: row.hourStartMs,
10164
+ paths: JSON.stringify(row.paths)
10165
+ };
10166
+ }
10167
+ function recordToRow(key, data) {
10168
+ const deviceId = Number(data["deviceId"]);
10169
+ const profile = data["profile"];
10170
+ const locationId = data["locationId"];
10171
+ const hour = Number(data["hourStartMs"]);
10172
+ let raw = data["paths"];
10173
+ if (typeof raw === "string") try {
10174
+ raw = JSON.parse(raw);
10175
+ } catch {
10176
+ return null;
10177
+ }
10178
+ 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;
10179
+ return {
10180
+ key,
10181
+ deviceId,
10182
+ profile,
10183
+ locationId,
10184
+ hourStartMs: hour,
10185
+ paths: raw
10186
+ };
10187
+ }
10188
+ var SPEC = {
10189
+ collection: RECORDING_SEGMENT_HOURS_COLLECTION,
10190
+ columns: RECORDING_SEGMENT_HOURS_COLUMNS,
10191
+ indexes: RECORDING_SEGMENT_HOURS_INDEXES,
10192
+ writeMode: "write-behind",
10193
+ loadLimit: 1e5,
10194
+ keyOf: (row) => row.key,
10195
+ toValue: rowToValue,
10196
+ fromRecord: recordToRow,
10197
+ deviceIdOf: (row) => row.deviceId
10198
+ };
10199
+ var SegmentHourLedger = class {
10200
+ ledger;
10201
+ logger;
10202
+ isLocationUsable;
10034
10203
  constructor(deps) {
10035
- this.deps = deps;
10204
+ this.logger = deps.logger;
10205
+ this.isLocationUsable = deps.isLocationUsable ?? (() => true);
10206
+ this.ledger = new DurableLedger({
10207
+ spec: SPEC,
10208
+ store: deps.store,
10209
+ logger: deps.logger
10210
+ });
10211
+ }
10212
+ static declare(store) {
10213
+ return DurableLedger.declare(store, SPEC);
10214
+ }
10215
+ declare() {
10216
+ return this.ledger.declare();
10217
+ }
10218
+ async load() {
10219
+ const rows = await this.ledger.load();
10220
+ this.logger.info("recorder: segment-hour ledger loaded", { meta: {
10221
+ hours: rows.length,
10222
+ collection: RECORDING_SEGMENT_HOURS_COLLECTION
10223
+ } });
10224
+ return rows;
10225
+ }
10226
+ snapshot() {
10227
+ return this.ledger.snapshot();
10036
10228
  }
10037
10229
  /**
10038
- * Arm the sweep: run immediately, then on the interval. Idempotent — a second
10039
- * `start()` while already armed is a no-op. The timer is `unref`'d so it never
10040
- * keeps the process alive.
10230
+ * Seed {@link RecordingIndex} from the mirrored hour rows.
10231
+ *
10232
+ * Hours present in the ledger become `hydrated` so a seek in those windows
10233
+ * does not pay for a disk walk. Hours the ledger never held stay `unknown`
10234
+ * — that is the fail-toward-walk direction.
10041
10235
  */
10042
- start() {
10043
- if (this.timer !== null) return;
10044
- const tick = () => {
10045
- this.sweep();
10046
- };
10047
- tick();
10048
- this.timer = setInterval(tick, this.deps.intervalMs);
10049
- this.timer.unref?.();
10236
+ hydrateIndex(index) {
10237
+ for (const hour of this.ledger.snapshot()) index.hydrateHour(hour.deviceId, hour.locationId, hour.hourStartMs, hour.paths);
10050
10238
  }
10051
- /** Stop the sweep timer. Idempotent. An in-flight sweep still completes. */
10052
- stop() {
10053
- if (this.timer !== null) {
10054
- clearInterval(this.timer);
10055
- this.timer = null;
10239
+ /** Write-behind: a just-finalized segment joins its hour row. */
10240
+ async recordSegment(row) {
10241
+ const { key, hourStartMs: hour } = hourOf(row);
10242
+ const held = this.ledger.get(key);
10243
+ if (held !== void 0 && held.paths.includes(row.path)) return;
10244
+ await this.ledger.put({
10245
+ key,
10246
+ deviceId: row.deviceId,
10247
+ profile: row.profile,
10248
+ locationId: row.locationId,
10249
+ hourStartMs: hour,
10250
+ paths: held === void 0 ? [row.path] : [...held.paths, row.path]
10251
+ });
10252
+ }
10253
+ /**
10254
+ * Eviction confirmed these paths are gone from disk. Drop them; forget the
10255
+ * hour when it holds nothing.
10256
+ */
10257
+ async dropSegments(rows) {
10258
+ const doomed = /* @__PURE__ */ new Map();
10259
+ for (const row of rows) {
10260
+ const { key } = hourOf(row);
10261
+ let set = doomed.get(key);
10262
+ if (!set) {
10263
+ set = /* @__PURE__ */ new Set();
10264
+ doomed.set(key, set);
10265
+ }
10266
+ set.add(row.path);
10267
+ }
10268
+ for (const [key, paths] of doomed) {
10269
+ const held = this.ledger.get(key);
10270
+ if (held === void 0) continue;
10271
+ const next = held.paths.filter((p) => !paths.has(p));
10272
+ if (next.length === 0) {
10273
+ await this.ledger.forget(key);
10274
+ continue;
10275
+ }
10276
+ if (next.length === held.paths.length) continue;
10277
+ await this.ledger.put({
10278
+ ...held,
10279
+ paths: next
10280
+ });
10056
10281
  }
10057
10282
  }
10058
10283
  /**
10059
- * One retention pass. Single-flighted: a call that fires while a previous
10060
- * sweep is still running is a no-op. Never throws — a config-read failure
10061
- * aborts THIS pass (logged); a per-device prune failure is logged and the
10062
- * sweep continues with the remaining devices.
10284
+ * After a disk walk of `deviceIds`, make the ledger match the index — with
10285
+ * two D148 exceptions:
10286
+ *
10287
+ * - an hour the walk did not see is dropped ONLY if it is in the past;
10288
+ * the current hour may still hold a live writer whose file the readdir
10289
+ * raced;
10290
+ * - the current hour UNIONs ledger paths with index paths rather than
10291
+ * replacing, for the same race.
10292
+ *
10293
+ * Devices not in `deviceIds` are left untouched (a partial walk must not
10294
+ * prune cameras it did not look at).
10295
+ *
10296
+ * And a walk this ledger does not TRUST prunes nothing at all — see
10297
+ * {@link refusedPrunes}. What such a walk FOUND is still real, so its paths
10298
+ * are unioned in; what it did not find decides nothing (D49).
10063
10299
  */
10064
- async sweep() {
10065
- if (this.sweeping) {
10066
- this.deps.logger.debug("recorder: retention sweep already running — skipping this tick");
10067
- return;
10300
+ async reconcileFromIndex(index, deviceIds, nowMs) {
10301
+ const walked = new Set(deviceIds);
10302
+ const currentHour = hourStartMs(nowMs);
10303
+ const fromIndex = /* @__PURE__ */ new Map();
10304
+ for (const deviceId of deviceIds) for (const seg of index.segments(deviceId)) {
10305
+ const { key, hourStartMs: hour } = hourOf(seg);
10306
+ const existing = fromIndex.get(key);
10307
+ if (existing === void 0) fromIndex.set(key, {
10308
+ key,
10309
+ deviceId: seg.deviceId,
10310
+ profile: seg.profile,
10311
+ locationId: seg.locationId,
10312
+ hourStartMs: hour,
10313
+ paths: [seg.path]
10314
+ });
10315
+ else fromIndex.set(key, {
10316
+ ...existing,
10317
+ paths: [...existing.paths, seg.path]
10318
+ });
10068
10319
  }
10069
- this.sweeping = true;
10070
- try {
10071
- let configs;
10072
- try {
10073
- configs = await this.deps.readConfigs();
10074
- } catch (err) {
10075
- this.deps.logger.warn("recorder: retention sweep could not read device configs", { meta: { error: require_dist.errMsg(err) } });
10076
- return;
10320
+ const refused = this.refusedPrunes(walked, fromIndex);
10321
+ for (const row of this.ledger.snapshot()) {
10322
+ if (!walked.has(row.deviceId)) continue;
10323
+ const disk = fromIndex.get(row.key);
10324
+ const additiveOnly = row.hourStartMs === currentHour || refused.devices.has(row.deviceId) || refused.locations.has(locationKey(row.deviceId, row.locationId));
10325
+ if (disk === void 0) {
10326
+ if (additiveOnly) continue;
10327
+ await this.ledger.forget(row.key);
10328
+ continue;
10077
10329
  }
10078
- const withPolicy = [...configs].filter(([, config]) => hasRetentionPolicy(config));
10079
- const devicesSkipped = configs.size - withPolicy.length;
10080
- this.deps.logger.info("recorder: retention sweep starting", { meta: {
10081
- devices: withPolicy.map(([deviceId]) => deviceId),
10082
- skipped: devicesSkipped
10083
- } });
10084
- let devicesSwept = 0;
10085
- let bucketsPruned = 0;
10086
- let bytesReclaimed = 0;
10087
- for (const [deviceId] of withPolicy) {
10088
- const startedMs = Date.now();
10089
- try {
10090
- const result = await this.deps.prune(deviceId);
10091
- devicesSwept += 1;
10092
- bucketsPruned += result.deletedBuckets;
10093
- bytesReclaimed += result.reclaimedBytes;
10094
- this.deps.logger.info("recorder: retention prune complete for device", {
10095
- tags: { deviceId },
10096
- meta: {
10097
- deletedBuckets: result.deletedBuckets,
10098
- reclaimedBytes: result.reclaimedBytes,
10099
- ms: Date.now() - startedMs
10100
- }
10101
- });
10102
- } catch (err) {
10103
- this.deps.logger.warn("recorder: retention prune failed for device", {
10104
- tags: { deviceId },
10105
- meta: { error: require_dist.errMsg(err) }
10106
- });
10107
- }
10330
+ if (additiveOnly) {
10331
+ const union = uniquePaths([...disk.paths, ...row.paths]);
10332
+ if (!samePaths(union, row.paths)) await this.ledger.put({
10333
+ ...row,
10334
+ paths: union
10335
+ });
10336
+ continue;
10108
10337
  }
10109
- this.deps.logger.info("recorder: retention sweep complete", { meta: {
10110
- devicesSwept,
10111
- devicesSkipped,
10112
- bucketsPruned,
10113
- bytesReclaimed
10114
- } });
10115
- } finally {
10116
- this.sweeping = false;
10338
+ if (!samePaths(row.paths, disk.paths)) await this.ledger.put(disk);
10339
+ }
10340
+ for (const [key, disk] of fromIndex) {
10341
+ if (this.ledger.has(key)) continue;
10342
+ await this.ledger.put(disk);
10343
+ }
10344
+ }
10345
+ /**
10346
+ * Which of this walk's deletions are REFUSED, and why.
10347
+ *
10348
+ * Two independent reasons, because the live fault of 2026-08-19 would have
10349
+ * escaped either one alone:
10350
+ *
10351
+ * - **the volume said no.** `PlacementService`'s marker probe knows the bytes
10352
+ * under a root are not the bytes we wrote. A walk of such a root found
10353
+ * nothing because nothing is mounted there, not because the footage is
10354
+ * gone.
10355
+ * - **the magnitude said no.** At BOOT the probe has not run yet — it runs at
10356
+ * the first writer attach — so on the morning the recorder respawned onto an
10357
+ * empty tmpfs the volume was not flagged and the prune went through: 873
10358
+ * hours to 32. A walk that deletes more than
10359
+ * {@link LEDGER_PRUNE_MAX_SHARE} of a device's held hours is refused on its
10360
+ * size alone.
10361
+ *
10362
+ * A refusal is one line per device (or per device+location), carrying the
10363
+ * delta, tagged with `deviceId` — a ledger that silently declined to prune and
10364
+ * a ledger with nothing to prune must never read the same.
10365
+ */
10366
+ refusedPrunes(walked, fromIndex) {
10367
+ const heldByDevice = /* @__PURE__ */ new Map();
10368
+ const unusable = /* @__PURE__ */ new Map();
10369
+ for (const row of this.ledger.snapshot()) {
10370
+ if (!walked.has(row.deviceId)) continue;
10371
+ heldByDevice.set(row.deviceId, (heldByDevice.get(row.deviceId) ?? 0) + 1);
10372
+ if (this.isLocationUsable(row.locationId)) continue;
10373
+ const key = locationKey(row.deviceId, row.locationId);
10374
+ const seen = unusable.get(key);
10375
+ unusable.set(key, seen === void 0 ? {
10376
+ deviceId: row.deviceId,
10377
+ locationId: row.locationId,
10378
+ held: 1
10379
+ } : {
10380
+ ...seen,
10381
+ held: seen.held + 1
10382
+ });
10383
+ }
10384
+ const onDiskByDevice = /* @__PURE__ */ new Map();
10385
+ for (const row of fromIndex.values()) onDiskByDevice.set(row.deviceId, (onDiskByDevice.get(row.deviceId) ?? 0) + 1);
10386
+ const devices = /* @__PURE__ */ new Set();
10387
+ for (const [deviceId, held] of heldByDevice) {
10388
+ if (held < 8) continue;
10389
+ const onDisk = onDiskByDevice.get(deviceId) ?? 0;
10390
+ if (onDisk >= held * .5) continue;
10391
+ devices.add(deviceId);
10392
+ this.logger.warn("recorder: segment-hour ledger prune REFUSED — this walk would delete most of the camera archive, which is what an unmounted root looks like; keeping the previous ledger", {
10393
+ tags: { deviceId },
10394
+ meta: {
10395
+ held,
10396
+ onDisk,
10397
+ dropped: held - onDisk,
10398
+ maxShare: LEDGER_PRUNE_MAX_SHARE
10399
+ }
10400
+ });
10401
+ }
10402
+ const locations = /* @__PURE__ */ new Set();
10403
+ for (const [key, entry] of unusable) {
10404
+ if (devices.has(entry.deviceId)) continue;
10405
+ locations.add(key);
10406
+ this.logger.warn("recorder: segment-hour ledger prune REFUSED — the volume under this location failed its probe, so the walk found nothing because nothing is mounted there", {
10407
+ tags: { deviceId: entry.deviceId },
10408
+ meta: {
10409
+ locationId: entry.locationId,
10410
+ held: entry.held,
10411
+ onDisk: onDiskByDevice.get(entry.deviceId) ?? 0
10412
+ }
10413
+ });
10117
10414
  }
10415
+ return {
10416
+ devices,
10417
+ locations
10418
+ };
10118
10419
  }
10119
10420
  };
10421
+ function locationKey(deviceId, locationId) {
10422
+ return `${deviceId}|${locationId}`;
10423
+ }
10120
10424
  //#endregion
10121
10425
  //#region src/recorder/loop-pacer.ts
10122
10426
  var DEFAULT_BUSY_LAG_MS = 25;
@@ -10737,6 +11041,23 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
10737
11041
  });
10738
11042
  /** Continuous + events band write-path controller (ffmpeg writers + watchers). */
10739
11043
  controller = null;
11044
+ /**
11045
+ * The controller's config read, with last-known-good fallback.
11046
+ *
11047
+ * Built ONCE and memoized, because the cache lives INSIDE the wrapper:
11048
+ * rebuilding it per call would throw away the very value the fallback exists
11049
+ * to serve. Lazy rather than a field initializer — a field initializer runs
11050
+ * before the constructor assigns `this.ctx`, so `this.ctx.logger` would be
11051
+ * `undefined` at that point.
11052
+ */
11053
+ lastKnownGoodRead = null;
11054
+ get loadConfigLastKnownGood() {
11055
+ this.lastKnownGoodRead ??= withLastKnownConfig({
11056
+ load: (deviceId) => loadDeviceConfig(this.configStore(), deviceId),
11057
+ logger: this.ctx.logger
11058
+ });
11059
+ return this.lastKnownGoodRead;
11060
+ }
10740
11061
  /** Per-device motion/audio markers for playback (B3 events feed the controller). */
10741
11062
  eventMap = new EventMap();
10742
11063
  /** Unsubscribe handle for the EventCapture motion subscription. */
@@ -10919,7 +11240,7 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
10919
11240
  nodeId: this.nodeId,
10920
11241
  ownerNodeId: ingestOwner.ownerNodeId,
10921
11242
  segmentStore: this.segmentStore,
10922
- loadConfig: (deviceId) => loadDeviceConfig(this.configStore(), deviceId),
11243
+ loadConfig: this.loadConfigLastKnownGood,
10923
11244
  enabledDeviceIds: async () => [...(await readDeviceConfigs(this.configStore())).keys()],
10924
11245
  /**
10925
11246
  * The DURABLE half of the trigger path (D8/D11). `pipelineAnalytics` rows