@camstack/addon-pipeline 1.2.256 → 1.2.257

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 (31) hide show
  1. package/dist/audio-analyzer/index.js +2 -2
  2. package/dist/audio-analyzer/index.mjs +2 -2
  3. package/dist/{default-detection-model-A5DRjEr3.mjs → default-detection-model-BTzxxm-z.mjs} +1 -1
  4. package/dist/{default-detection-model-Bbw4nC11.js → default-detection-model-C8CHpxQl.js} +1 -1
  5. package/dist/detection-pipeline/index.js +4 -4
  6. package/dist/detection-pipeline/index.mjs +4 -4
  7. package/dist/{dist-lqqUXDfs.js → dist-BMARLLlX.js} +27 -6
  8. package/dist/{dist-MJ4ji7um.mjs → dist-xsaGDCOu.mjs} +27 -6
  9. package/dist/motion-wasm/index.js +1 -1
  10. package/dist/motion-wasm/index.mjs +1 -1
  11. package/dist/{node-80-uOMr8.js → node-Ca-vE0xX.js} +1 -1
  12. package/dist/{node-DdXmHLK4.mjs → node-Fv4ByT36.mjs} +1 -1
  13. package/dist/pipeline-runner/index.js +3 -3
  14. package/dist/pipeline-runner/index.mjs +3 -3
  15. package/dist/{process-memory-Cl3ubMxY.mjs → process-memory-BgtQADAY.mjs} +1 -1
  16. package/dist/{process-memory-Dac9TXlX.js → process-memory-C3Wjmvm0.js} +1 -1
  17. package/dist/recorder/index.js +778 -998
  18. package/dist/recorder/index.mjs +778 -998
  19. package/dist/{segment-demux-js-gIj82O9k.mjs → segment-demux-js-C7C6c-g2.mjs} +1 -1
  20. package/dist/{segment-demux-js-CWwlKQ3M.js → segment-demux-js-DHV7EOQf.js} +1 -1
  21. package/dist/stream-broker/_stub.js +2 -2
  22. package/dist/stream-broker/{_virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-pOLAb3B1.mjs → _virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-CJWLoj3Z.mjs} +3 -3
  23. package/dist/stream-broker/{_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-CPFjd94C.mjs → _virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-ReCGmeOG.mjs} +1 -1
  24. package/dist/stream-broker/{_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-BXK7jbJm.mjs → _virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-BbnIk-9T.mjs} +1 -1
  25. package/dist/stream-broker/demux-worker-child.js +1 -1
  26. package/dist/stream-broker/demux-worker-child.mjs +1 -1
  27. package/dist/stream-broker/{hostInit-BRnsXyTM.mjs → hostInit-BzBcvawL.mjs} +3 -3
  28. package/dist/stream-broker/index.js +3 -3
  29. package/dist/stream-broker/index.mjs +3 -3
  30. package/dist/stream-broker/remoteEntry.js +1 -1
  31. package/package.json +1 -1
@@ -3,8 +3,8 @@ Object.defineProperties(exports, {
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
5
  const require_chunk = require("../chunk-emK7D4bc.js");
6
- const require_dist = require("../dist-lqqUXDfs.js");
7
- const require_node = require("../node-80-uOMr8.js");
6
+ const require_dist = require("../dist-BMARLLlX.js");
7
+ const require_node = require("../node-Ca-vE0xX.js");
8
8
  const require_hub_hostname = require("../hub-hostname-DAJXlOgV.js");
9
9
  const require_restream_intent = require("../restream-intent-VVJiUAkh.js");
10
10
  const require_retire_root_keys = require("../retire-root-keys-Csb8z2f3.js");
@@ -15,7 +15,6 @@ node_path = require_chunk.__toESM(node_path);
15
15
  let node_child_process = require("node:child_process");
16
16
  let node_fs_promises = require("node:fs/promises");
17
17
  let _camstack_system_addon_utils = require("@camstack/system/addon-utils");
18
- let node_zlib = require("node:zlib");
19
18
  //#region src/recorder/event-capture.ts
20
19
  /**
21
20
  * La sottoscrizione motion del recorder: bus → `onTrigger(deviceId, atMs)`.
@@ -1124,6 +1123,37 @@ var RecordingIndex = class {
1124
1123
  this.markHydrated(deviceId, hourStartMs, hourStartMs + HOUR_MS$9);
1125
1124
  }
1126
1125
  /**
1126
+ * Make one hour of ONE location exactly `relPaths` — merge what is there and
1127
+ * drop what is not.
1128
+ *
1129
+ * {@link hydrateHour} merges, which is right for a reader: a listing it took
1130
+ * of a live hour is a lower bound, and dropping on it would delete the index
1131
+ * of footage the readdir merely raced. This is for the one caller that has
1132
+ * TRUSTED evidence the hour is over and the volume it listed is the volume it
1133
+ * started on — the operator-armed ledger walk — where the directory is the
1134
+ * whole truth about the hour and an index row naming a file that is not in it
1135
+ * is a ghost (D523, D148's "trusted walk" arm).
1136
+ *
1137
+ * Scoped to the (device, location, hour) triple: a path filed under another
1138
+ * hour, or held for another location, is none of this call's business.
1139
+ * @residency mutation
1140
+ */
1141
+ resetHour(deviceId, locationId, hourStartMs, relPaths) {
1142
+ const m = this.mapFor(deviceId);
1143
+ const keep = new Set(relPaths);
1144
+ const dropped = [];
1145
+ for (const [path, row] of m) {
1146
+ if (row.locationId !== locationId) continue;
1147
+ if (Math.floor(row.startMs / HOUR_MS$9) * HOUR_MS$9 !== hourStartMs) continue;
1148
+ if (keep.has(path)) continue;
1149
+ dropped.push(path);
1150
+ }
1151
+ for (const path of dropped) m.delete(path);
1152
+ if (dropped.length > 0) this.invalidateSorted(deviceId);
1153
+ this.hydrateHour(deviceId, locationId, hourStartMs, relPaths);
1154
+ return dropped;
1155
+ }
1156
+ /**
1127
1157
  * Insert/replace one finalized segment (idempotent by path).
1128
1158
  *
1129
1159
  * THE hot mutation: every profile-writer lands here every `segmentSeconds`,
@@ -1772,40 +1802,6 @@ async function hydrateWindowFromStorage(index, deviceId, fromMs, toMs, locations
1772
1802
  const verify = options?.verifyClosedHours === true;
1773
1803
  if (!verify && index.hydrationOf(deviceId, fromMs, toMs) === "hydrated") return;
1774
1804
  const liveHour = Math.floor(Date.now() / HOUR_MS$8) * HOUR_MS$8;
1775
- /** Per (locationId, profile, hour): what the LEDGER claimed, for the diff. */
1776
- const claimed = /* @__PURE__ */ new Map();
1777
- const claimKey = (locationId, hour) => `${locationId}:${hour}`;
1778
- /**
1779
- * Closed hours the ledger answered for with a row nobody SEALED (D451): what
1780
- * a writer had persisted when it was last heard from, which is a legitimate
1781
- * answer and not a complete one. Each is read from the array once and the
1782
- * row is unioned and sealed, so the second reader pays a point read.
1783
- */
1784
- const unsealed = /* @__PURE__ */ new Set();
1785
- if (ledger !== void 0) {
1786
- const startedMs = Date.now();
1787
- const hours = await ledger.detailForWindow(deviceId, fromMs, toMs);
1788
- let segments = 0;
1789
- let served = 0;
1790
- for (const hour of hours) {
1791
- if (hour.hourStartMs >= liveHour) continue;
1792
- index.hydrateHour(deviceId, hour.locationId, hour.hourStartMs, hour.paths);
1793
- claimed.set(claimKey(hour.locationId, hour.hourStartMs), hour.paths.length);
1794
- if (!hour.sealed) unsealed.add(hour.hourStartMs);
1795
- segments += hour.paths.length;
1796
- served += 1;
1797
- }
1798
- if (served > 0) logger.info("recorder: hydrated a window from the hour ledger", {
1799
- tags: { deviceId },
1800
- meta: {
1801
- hours: served,
1802
- segments,
1803
- unsealedHours: unsealed.size,
1804
- ms: Date.now() - startedMs
1805
- }
1806
- });
1807
- if (!verify && unsealed.size === 0 && index.hydrationOf(deviceId, fromMs, toMs) === "hydrated") return;
1808
- }
1809
1805
  for (const [root, aliases] of aliasesByRoot(locations)) {
1810
1806
  const deviceDir = node_path.default.join(root, String(deviceId));
1811
1807
  let profiles;
@@ -1829,7 +1825,7 @@ async function hydrateWindowFromStorage(index, deviceId, fromMs, toMs, locations
1829
1825
  let forgottenHours = 0;
1830
1826
  let sealed = 0;
1831
1827
  for (let hour = first; hour < toMs; hour += HOUR_MS$8) {
1832
- if (!verify && hour < liveHour && !unsealed.has(hour) && index.hydrationOf(deviceId, hour, hour + HOUR_MS$8) === "hydrated") continue;
1828
+ if (!verify && hour < liveHour && index.hydrationOf(deviceId, hour, hour + HOUR_MS$8) === "hydrated") continue;
1833
1829
  for (const profile of profiles) {
1834
1830
  const rel = hourDirRelPath(deviceId, profile, hour);
1835
1831
  let files;
@@ -1843,11 +1839,10 @@ async function hydrateWindowFromStorage(index, deviceId, fromMs, toMs, locations
1843
1839
  const relPaths = files.filter((f) => f.endsWith(".m4s")).map((f) => `${rel}/${f}`);
1844
1840
  index.hydrateHour(deviceId, locationId, hour, relPaths);
1845
1841
  if (ledger === void 0 || relPaths.length === 0 || hour + HOUR_MS$8 > Date.now()) continue;
1846
- const claimedHere = claimed.get(claimKey(locationId, hour));
1847
1842
  const outcome = await ledger.adoptWalkedHour(deviceId, profile, locationId, hour, relPaths);
1848
1843
  if (outcome === null) continue;
1849
1844
  if (outcome.created) repaired += 1;
1850
- else if (claimedHere !== void 0 && outcome.added > 0) {
1845
+ else if (outcome.added > 0) {
1851
1846
  forgottenSegments += outcome.added;
1852
1847
  forgottenHours += 1;
1853
1848
  }
@@ -2484,10 +2479,12 @@ var RelocateEngine = class {
2484
2479
  *
2485
2480
  * The order is the whole point. Between the copy and the delete both
2486
2481
  * locations hold every byte, so the ledger re-point in the middle can only
2487
- * ever name a location that HAS the file — which is what makes a crash
2488
- * recoverable instead of a black window. A ledger that refuses (a failed
2489
- * point read: D49/D287) stops this hour dead with nothing deleted; the
2490
- * copies at the target are adopted by the next run.
2482
+ * ever account for a location that HAS the files — which is what makes a
2483
+ * crash recoverable instead of a black window.
2484
+ *
2485
+ * There is no ledger refusal to handle any more (D523): the durable half is
2486
+ * arithmetic over a row the mirror already holds, so nothing on that path can
2487
+ * fail and stop an hour dead.
2491
2488
  */
2492
2489
  async moveHour(job, rows, from, to, samePhysicalRoot, sleep, bytesPerMs) {
2493
2490
  const movable = [];
@@ -2523,17 +2520,7 @@ var RelocateEngine = class {
2523
2520
  toLocationId: to.id
2524
2521
  }
2525
2522
  });
2526
- else if (!await ledger.repointSegments(movable, to.id)) {
2527
- this.deps.logger.warn("relocate: hour NOT moved — the durable ledger refused the re-point, so the source bytes are kept", {
2528
- tags: { deviceId: movable[0].deviceId },
2529
- meta: {
2530
- jobId: job.jobId,
2531
- files: movable.length,
2532
- toLocationId: to.id
2533
- }
2534
- });
2535
- return;
2536
- }
2523
+ else await ledger.repointSegments(movable, to.id);
2537
2524
  if (!samePhysicalRoot) for (const row of movable) await node_fs.promises.rm(node_path.default.join(from.root, row.fromPath ?? row.path), { force: true });
2538
2525
  for (const row of movable) {
2539
2526
  this.deps.index.removeSegments([row.fromPath ?? row.path]);
@@ -2926,23 +2913,21 @@ var SegmentStore = class SegmentStore {
2926
2913
  * work now the per-bucket archive scan is gone. */
2927
2914
  static DEFAULT_EVICT_CHUNK = 25;
2928
2915
  /**
2929
- * True when `victims` cover EVERY segment the ARCHIVE says the bucket holds,
2930
- * AND the bucket's hour has fully elapsed.
2916
+ * True when `victims` cover EVERY segment the bucket DIRECTORY holds, AND the
2917
+ * bucket's hour has fully elapsed.
2931
2918
  *
2932
2919
  * The current-hour guard matters because `onFinalized` relocates new segments
2933
2920
  * INTO the current bucket concurrently — an rm racing that rename would
2934
2921
  * orphan a just-indexed segment.
2935
2922
  *
2936
- * The cover test is against `SegmentHourLedger`'s own hour row, never
2937
- * `RecordingIndex`. A bucketDir is `<dev>/<profile>/Y/M/D/H`, so it names
2938
- * exactly one durable hour row: "every path that row lists is a victim" is
2939
- * the same question the `rm -rf` answers, asked of the authority for what
2940
- * EXISTS (D295). Asked of the resident index it read EQUAL whenever the hour
2941
- * was partly resident — both sides short by the same rows — and the directory
2942
- * went with every file the round never named, leaving their durable rows
2943
- * behind as ghosts.
2923
+ * The cover test is a listing of the directory the `rm -rf` is about to take,
2924
+ * never `RecordingIndex` and never a durable row (D523). A bucketDir is
2925
+ * `<dev>/<profile>/Y/M/D/H`, so one listing answers exactly the question the
2926
+ * delete asks. Asked of the resident index it read EQUAL whenever the hour was
2927
+ * partly resident — both sides short by the same rows — and the directory went
2928
+ * with every file the round never named.
2944
2929
  *
2945
- * An unanswerable archive returns `false`: per-file deletes are slower and
2930
+ * An unanswerable listing returns `false`: per-file deletes are slower and
2946
2931
  * always correct, and the alternative is a recursive delete computed from a
2947
2932
  * fraction of the truth.
2948
2933
  */
@@ -2951,7 +2936,7 @@ var SegmentStore = class SegmentStore {
2951
2936
  if (!first) return false;
2952
2937
  const bucketStartMs = Math.floor(first.startMs / SegmentStore.HOUR_MS) * SegmentStore.HOUR_MS;
2953
2938
  if ((this.deps.now?.() ?? Date.now()) < bucketStartMs + SegmentStore.HOUR_MS) return false;
2954
- const ask = this.deps.archiveHourPaths;
2939
+ const ask = this.deps.hourDirectoryPaths;
2955
2940
  if (ask === void 0) return false;
2956
2941
  const held = await ask(first.deviceId, first.profile, location, bucketStartMs);
2957
2942
  if (held === null || held.length === 0) return false;
@@ -4084,8 +4069,9 @@ var CalendarIndex = class {
4084
4069
  *
4085
4070
  * Also used to warm the days ADJACENT to one being viewed — changing day is a
4086
4071
  * single click and the operator expects the summary to be there. That warm
4087
- * belongs in the background: at ~650 ms per cold read, doing it inline would
4088
- * only move the wait.
4072
+ * belongs in the background: a cold read costs whatever the array charges
4073
+ * that day (see the header — the ~650 ms figure is from the retired FUSE
4074
+ * share), and doing it inline would only move the wait onto the click.
4089
4075
  */
4090
4076
  async warm(deviceId, fromMs, toMs) {
4091
4077
  for (let day = utcDayOf(fromMs); day <= utcDayOf(toMs); day += DAY_MS$3) {
@@ -5207,22 +5193,18 @@ function createFootageDiscardJob(deps) {
5207
5193
  const forgetLedger = async (location) => {
5208
5194
  const ledger = deps.ledger;
5209
5195
  if (ledger === null) return 0;
5210
- let forgotten = 0;
5211
5196
  const profiles = profilesForType(location.type);
5212
- for await (const page of ledger.streamSegments({
5197
+ const scope = {
5213
5198
  locationId: location.id,
5214
5199
  profiles
5215
- })) {
5216
- if (cancelRequested) return forgotten;
5217
- deps.index.removeSegments(page.map((row) => row.path));
5218
- await ledger.dropSegments(page);
5219
- forgotten += page.length;
5220
- report({
5221
- dbRowsDeleted: forgotten,
5222
- detail: `forgot ${forgotten} ledger segments`
5223
- });
5224
- await sleep(0);
5225
- }
5200
+ };
5201
+ const hours = ledger.snapshot().filter((row) => row.locationId === location.id && profiles.includes(row.profile));
5202
+ const forgotten = await ledger.forgetScope(scope);
5203
+ for (const hour of hours) deps.index.resetHour(hour.deviceId, hour.locationId, hour.hourStartMs, []);
5204
+ report({
5205
+ dbRowsDeleted: forgotten,
5206
+ detail: `forgot ${forgotten} ledger segments`
5207
+ });
5226
5208
  return forgotten;
5227
5209
  };
5228
5210
  return {
@@ -8275,63 +8257,6 @@ function runFfmpeg(renderDeps, args, ext) {
8275
8257
  });
8276
8258
  }
8277
8259
  //#endregion
8278
- //#region src/recorder/addon/paths-codec.ts
8279
- /**
8280
- * How an hour row's `paths` becomes text, and back (D471).
8281
- *
8282
- * `recorder:segment-hours` is 66 MB of the settings database and **99.2 % of it
8283
- * is this one column** — 15 643 bytes per row over 4 036 rows. The content is a
8284
- * list of location-relative segment paths that nearly all share the same
8285
- * directory prefix, repeated in full on every entry. Measured per column on a
8286
- * copy of the live database, never the live file (D468).
8287
- *
8288
- * Compressing it is unusually safe here because of D287's two-tier mirror: boot
8289
- * loads the aggregate half of every row and NOT `paths`, so this column is read
8290
- * only on a point materialise, never on the hot path.
8291
- *
8292
- * A local copy of the same idea `pipeline-analytics` uses for `positions` —
8293
- * addons never import each other (CLAUDE.md), so twenty duplicated lines are
8294
- * the correct shape, not a shared dependency across an addon boundary.
8295
- */
8296
- /** Marks a value this module wrote. A plain JSON array always starts `[`. */
8297
- var ENCODED_PREFIX = "z1:";
8298
- function encodePaths(paths) {
8299
- return ENCODED_PREFIX + (0, node_zlib.gzipSync)(Buffer.from(JSON.stringify(paths), "utf8"), { level: 6 }).toString("base64");
8300
- }
8301
- /**
8302
- * Decode whichever era wrote the value: the `z1:` string this writes, the plain
8303
- * JSON string every pre-D471 row holds, or an array the store already parsed.
8304
- *
8305
- * `null` means "this row is unreadable", which the caller turns into a DISCARDED
8306
- * row — the same contract the bare `JSON.parse` had. An hour whose paths cannot
8307
- * be read must not silently become an hour with no segments: that would report
8308
- * an empty disk, which is the failure `recordToRow` was already written to
8309
- * avoid.
8310
- */
8311
- function decodePaths(value) {
8312
- if (Array.isArray(value)) return isPathList(value) ? value : null;
8313
- if (typeof value !== "string") return null;
8314
- const json = value.startsWith(ENCODED_PREFIX) ? tryGunzip(value.slice(3)) : value;
8315
- if (json === null) return null;
8316
- try {
8317
- const parsed = JSON.parse(json);
8318
- if (!Array.isArray(parsed)) return null;
8319
- return isPathList(parsed) ? parsed : null;
8320
- } catch {
8321
- return null;
8322
- }
8323
- }
8324
- function isPathList(v) {
8325
- return v.every((p) => typeof p === "string" && p.length > 0);
8326
- }
8327
- function tryGunzip(b64) {
8328
- try {
8329
- return (0, node_zlib.gunzipSync)(Buffer.from(b64, "base64")).toString("utf8");
8330
- } catch {
8331
- return null;
8332
- }
8333
- }
8334
- //#endregion
8335
8260
  //#region src/durable/durable-ledger.ts
8336
8261
  /** Default reseed cap — every current consumer's row set is installation-bounded. */
8337
8262
  var DEFAULT_LOAD_LIMIT = 1e5;
@@ -8586,19 +8511,19 @@ var macrotask$1 = () => new Promise((resolve) => {
8586
8511
  });
8587
8512
  /**
8588
8513
  * @durable class=mirror owner=recorder
8589
- * write="write-behind on every finalized segment (hour-row upsert, serialised per hour); the writer SEALS the hour it leaves (one write per hour); a disk walk of a closed hour unions what it found and seals; eviction drops paths"
8514
+ * write="write-behind on every finalized segment (hour-row upsert, serialised per hour); the writer SEALS the hour it leaves (one write per hour); a listing of a closed hour re-derives the aggregate and seals; eviction and relocation shrink it"
8590
8515
  * retention="a row goes when the walk or eviction confirms the hour is empty — never by age, never from the ledger alone (D148)"
8591
8516
  */
8592
8517
  var RECORDING_SEGMENT_HOURS_COLLECTION = "recorder:segment-hours";
8593
8518
  /**
8594
- * The four aggregate columns exist so "how much footage is on this disk" is a
8595
- * `SUM`/`MIN`/`MAX` over ~19 k hour rows instead of a walk over ~7.1 M in-RAM
8596
- * segment rows. They are DERIVED from `paths` — never a second authority — by
8597
- * {@link withSegmentPath} and nothing else: {@link hourAggregate} is that step
8598
- * over every path and {@link appendSegmentPath} is that step over one, so there
8599
- * is no second derivation to drift. A row loaded from a database that predates
8600
- * these columns is corrected in the mirror the moment it is read and repaired
8601
- * on disk by {@link SegmentHourLedger.repairAggregates}.
8519
+ * The four aggregate columns are what this table IS (D523).
8520
+ *
8521
+ * They exist so "how much footage is on this disk" is a `SUM`/`MIN`/`MAX` over
8522
+ * ~19 k hour rows instead of a walk over ~7.1 M in-RAM segment rows, and they
8523
+ * are derived from segment NAMES — by {@link withSegmentPath} and nothing else.
8524
+ * {@link hourAggregate} is that step over a whole listing and
8525
+ * {@link appendSegmentPath} is that step over the one path that just arrived,
8526
+ * so there is no second derivation to drift.
8602
8527
  */
8603
8528
  var RECORDING_SEGMENT_HOURS_COLUMNS = [
8604
8529
  {
@@ -8627,11 +8552,6 @@ var RECORDING_SEGMENT_HOURS_COLUMNS = [
8627
8552
  type: "INTEGER",
8628
8553
  notNull: true
8629
8554
  },
8630
- {
8631
- name: "paths",
8632
- type: "TEXT",
8633
- notNull: true
8634
- },
8635
8555
  {
8636
8556
  name: "bytes",
8637
8557
  type: "INTEGER",
@@ -8690,15 +8610,9 @@ var RECORDING_SEGMENT_HOURS_INDEXES = [
8690
8610
  * `MAX(maxStartMs)` over `locationId IN (…)` — one row back, and with this
8691
8611
  * index not one table row read to produce it (D447).
8692
8612
  *
8693
- * `idx_recorder_segment_hours_loc_hour` finds the same range, but the four
8694
- * aggregates live in the table row, next to `paths` — up to 360 segment
8695
- * paths of ~80 characters per hour — so the eviction index served the
8696
- * accounting read at the price of ~30 KB of table page per hour row. On the
8697
- * live hub that was `durationMs=1001…2902` once a MINUTE on
8698
- * `hub/sqlite-settings` (174 slow-call WARNs in ten hours on 2026-09-10/11),
8699
- * every one of them holding the thread every configuration read in the
8700
- * cluster waits on. The index is ~50 bytes a row over ~19 k rows — nothing —
8701
- * and the first declare after it ships builds it in well under a second.
8613
+ * Since D523 dropped `paths` the table row is ten scalars, so the covering
8614
+ * index no longer saves ~30 KB of page per row; it still saves the row
8615
+ * fetch, and the accounting read is the one this hub runs on a schedule.
8702
8616
  */
8703
8617
  {
8704
8618
  name: "idx_recorder_segment_hours_loc_accounting",
@@ -8723,13 +8637,13 @@ var RECORDING_SEGMENT_HOURS_INDEXES = [
8723
8637
  * nominal `-segment_time` (ffmpeg cuts on the next GOP, never on the clock).
8724
8638
  *
8725
8639
  * It is a guess, and this is the shape of being wrong in each direction.
8726
- * TIGHTER: a normal rollover stops sealing, every hour of the archive is walked
8727
- * once by its first reader, and D451's "at rest nothing is unsealed" is undone
8728
- * — at ~3.6 ms per hour directory cold, affordable but pointless. LOOSER: a
8729
- * runner respawn inside the window seals an hour it entered late, which is
8730
- * exactly the 2026-09-17 defect; the observed hole there was ~29 minutes, so
8731
- * anything under a few minutes would have caught it and 90 s is the smallest
8732
- * number that does not fire on a legitimate rollover.
8640
+ * TIGHTER: a normal rollover stops sealing and every hour of the archive is
8641
+ * listed once by the adoption sweep — at ~1 ms per hour directory on this hub's
8642
+ * SATA SSD, affordable but pointless. LOOSER: a runner respawn inside the
8643
+ * window seals an hour it entered late, which is exactly the 2026-09-17 defect;
8644
+ * the observed hole there was ~29 minutes, so anything under a few minutes
8645
+ * would have caught it and 90 s is the smallest number that does not fire on a
8646
+ * legitimate rollover.
8733
8647
  */
8734
8648
  var WRITER_HOUR_ENTRY_GRACE_MS = 9e4;
8735
8649
  /** The identity of the {@link hourAggregate} fold — an hour holding nothing. */
@@ -8743,12 +8657,11 @@ var EMPTY_HOUR_AGGREGATE = {
8743
8657
  * ONE path folded into an aggregate — the whole derivation, expressed as its
8744
8658
  * single step.
8745
8659
  *
8746
- * {@link hourAggregate} is this step over every path, and {@link
8660
+ * {@link hourAggregate} is this step over a whole listing, and {@link
8747
8661
  * SegmentHourLedger.recordSegment} is this step over the one path that just
8748
8662
  * arrived. That is not two implementations that happen to agree: the full
8749
8663
  * derivation is *defined* as this function repeated, so an append is the fold
8750
- * resumed from the state the held row already carries — a `SegmentHourRow`'s
8751
- * aggregate half IS the fold state over its `paths`.
8664
+ * resumed from the state the held row already carries.
8752
8665
  *
8753
8666
  * Why it had to become a step: `recordSegment` re-derived the whole hour on
8754
8667
  * every finalize. Σ1..360 ≈ 65 k path parses per camera-profile-hour at 10 s
@@ -8771,49 +8684,57 @@ function withSegmentPath(agg, path) {
8771
8684
  };
8772
8685
  }
8773
8686
  /**
8774
- * Bytes / count / oldest / newest for one hour's paths — the sole FULL
8775
- * derivation, used by rebuilds ({@link recordToRow}, `dropSegments`) and never
8776
- * by the append path.
8687
+ * Bytes / count / oldest / newest for one hour's LISTING — the sole FULL
8688
+ * derivation, and since D523 the only thing that can produce an EXACT row.
8777
8689
  *
8778
8690
  * Every field of a segment path encodes its own facts (`parseSegmentPath`), so
8779
- * this is a derivation, not a measurement: it can never disagree with the paths
8780
- * it was computed from.
8691
+ * this is a derivation, not a measurement: it can never disagree with the
8692
+ * listing it was computed from.
8781
8693
  */
8782
8694
  function hourAggregate(paths) {
8783
8695
  let agg = EMPTY_HOUR_AGGREGATE;
8784
8696
  for (const path of paths) agg = withSegmentPath(agg, path);
8785
8697
  return agg;
8786
8698
  }
8787
- /** Build the persisted row for an hour from its paths — the ONE constructor. */
8699
+ /** Build the persisted row for an hour from a LISTING of it — the ONE
8700
+ * constructor that can produce an exact aggregate. */
8788
8701
  function segmentHourRow(base) {
8702
+ const { paths, sealed, ...identity } = base;
8789
8703
  return {
8790
- ...base,
8791
- sealed: base.sealed ?? false,
8792
- pathsResident: true,
8793
- ...hourAggregate(base.paths)
8704
+ ...identity,
8705
+ sealed: sealed ?? false,
8706
+ ...hourAggregate(paths)
8707
+ };
8708
+ }
8709
+ /**
8710
+ * Build the persisted row for an hour from an aggregate somebody already folded
8711
+ * — a relocation moving one, or a listing that has already been reduced.
8712
+ *
8713
+ * `sealed` is a NAMED field and never a positional boolean, because
8714
+ * `scripts/check-seal-authority.ts` attributes every literal `sealed: true` in
8715
+ * the recorder to the method that wrote it. A seal smuggled in as a third
8716
+ * argument is a seal the guard cannot see.
8717
+ */
8718
+ function hourRowOf(identity, row) {
8719
+ return {
8720
+ ...identity,
8721
+ ...row
8794
8722
  };
8795
8723
  }
8796
8724
  /**
8797
8725
  * The row an hour becomes when ONE more path joins it.
8798
8726
  *
8799
- * Identical to `segmentHourRow({ ...held, paths: [...held.paths, path] })` and
8800
- * strictly cheaper: the aggregate half of `held` is already the fold over
8801
- * `held.paths`, so this resumes that fold instead of restarting it. The
8727
+ * Identical to `segmentHourRow({ …held, paths: [...everything, path] })` and
8728
+ * strictly cheaper: the aggregate half of `held` is already the fold over the
8729
+ * hour's names, so this resumes that fold instead of restarting it. The
8802
8730
  * equality is not an assumption — `segment-hour-append.spec.ts` pins it on
8803
8731
  * generated hours including out-of-order arrival, duplicate paths, unparseable
8804
8732
  * paths and storm-sized hours.
8805
- *
8806
- * The four fields are named rather than spread on purpose. {@link
8807
- * withSegmentPath} hands its argument straight back for a path the parser
8808
- * refuses, and `held` is a whole row — spreading that return would put the
8809
- * PREVIOUS `paths` array back over the appended one. Which is exactly what it
8810
- * did until the generated-hours test caught it.
8811
8733
  */
8812
8734
  function appendSegmentPath(held, path) {
8813
8735
  const { bytes, segments, minStartMs, maxStartMs } = withSegmentPath(held, path);
8814
8736
  return {
8815
8737
  ...held,
8816
- paths: [...held.paths, path],
8817
8738
  bytes,
8818
8739
  segments,
8819
8740
  minStartMs,
@@ -8831,6 +8752,20 @@ function appendSegmentPath(held, path) {
8831
8752
  * `readdir` over an unmounted root deletes all of it.
8832
8753
  */
8833
8754
  var LEDGER_PRUNE_MAX_SHARE = .5;
8755
+ /**
8756
+ * The largest share of ONE hour's accounted segments a single listing may
8757
+ * erase (D523).
8758
+ *
8759
+ * The directory is the authority for WHICH segments exist, so a listing of a
8760
+ * closed hour replaces that hour's aggregate — including downwards, which is
8761
+ * what makes an over-counted row self-correcting. The guard is the same
8762
+ * argument {@link LEDGER_PRUNE_MAX_SHARE} makes one level up: a root that has
8763
+ * gone away, or a bucket half-deleted by a round that died, answers SHORT, and
8764
+ * a short answer that halves an hour is indistinguishable from one that is
8765
+ * simply right. Under the bar the listing wins; over it the row stands, the
8766
+ * hour stays unsealed, and the disagreement is named.
8767
+ */
8768
+ var HOUR_LISTING_MAX_SHRINK_SHARE = .5;
8834
8769
  /** Backstop on the oldest-first probe. A bounded read must stay bounded even if
8835
8770
  * a caller asks for a target no archive can cover. */
8836
8771
  var OLDEST_MAX_PAGES = 16;
@@ -8840,9 +8775,6 @@ var OLDEST_PROBE_COLUMNS = [
8840
8775
  "bytes",
8841
8776
  "locationId"
8842
8777
  ];
8843
- /** Aggregate-repair probe: the primary key always comes back, so this asks for
8844
- * the predicate column alone. */
8845
- var REPAIR_PROBE_COLUMNS = ["segments"];
8846
8778
  /**
8847
8779
  * Backstop on {@link SegmentHourLedger.streamSegments}.
8848
8780
  *
@@ -8881,47 +8813,8 @@ var CENSUS_FIELDS = [
8881
8813
  op: "max"
8882
8814
  }
8883
8815
  ];
8884
- /** Rows repaired per boot. The live archive is ~19 k hour rows. */
8885
- var REPAIR_MAX_ROWS = 5e4;
8886
- /**
8887
- * The boot projection (D287): everything the recorder's always-resident tier
8888
- * needs, and deliberately NOT `paths`.
8889
- *
8890
- * `paths` is ~55 B per segment and the archive is the only thing that grows:
8891
- * ~16 MB of JSON today, a projected ~460 MB at 28 TB of today's bitrate mix
8892
- * and ~5.9 GB at a low-bitrate mix — one tRPC response, parsed on the main
8893
- * thread, before the index exists. The summary half is O(writers × days) and
8894
- * stays ~10–15 MB at any archive size.
8895
- */
8896
- var SEGMENT_HOUR_SUMMARY_COLUMNS = [
8897
- "deviceId",
8898
- "profile",
8899
- "locationId",
8900
- "hourStartMs",
8901
- "bytes",
8902
- "segments",
8903
- "minStartMs",
8904
- "maxStartMs",
8905
- "sealed"
8906
- ];
8907
8816
  /** Summary rows folded into the mirror between yields. */
8908
8817
  var LOAD_CHUNK = 5e3;
8909
- /** Hour rows materialised per point-read page. One page covers a day of one
8910
- * camera's three profiles with room to spare. */
8911
- var HOUR_DETAIL_PAGE = 64;
8912
- /**
8913
- * The live tail materialised at boot: the current hour and the one before it.
8914
- *
8915
- * `recordSegment` must gate from RAM (D49), and the hour it appends to at boot
8916
- * is exactly the hour the previous process was writing. Two hours × the active
8917
- * writers is ~60 rows — one point-read page — and it is what makes the write
8918
- * path's materialise a case that essentially never happens.
8919
- */
8920
- var LIVE_TAIL_HOURS = 2;
8921
- /** Rows the one-shot aggregate repair may materialise in a single boot. The
8922
- * backfill is bounded so it cannot re-become the whole-archive read D287
8923
- * removed; a partial pass finishes on the next boot. */
8924
- var REPAIR_MAX_MATERIALIZE = 2e3;
8925
8818
  /** The four scalars an accounting answer is made of, asked in one statement. */
8926
8819
  var ACCOUNTING_FIELDS = [
8927
8820
  {
@@ -8974,14 +8867,6 @@ function writerKey(deviceId, profile) {
8974
8867
  function writerHourKey(deviceId, profile, hour) {
8975
8868
  return `${deviceId}|${profile}|${hour}`;
8976
8869
  }
8977
- function detailOf(row) {
8978
- return {
8979
- locationId: row.locationId,
8980
- hourStartMs: row.hourStartMs,
8981
- paths: row.paths,
8982
- sealed: row.sealed
8983
- };
8984
- }
8985
8870
  /**
8986
8871
  * The `hourStartMs` of one probe record.
8987
8872
  *
@@ -8993,21 +8878,31 @@ function hourOfRecord(record) {
8993
8878
  const raw = record.data["hourStartMs"];
8994
8879
  return typeof raw === "number" && Number.isFinite(raw) ? raw : -1;
8995
8880
  }
8996
- function uniquePaths(paths) {
8997
- const seen = /* @__PURE__ */ new Set();
8998
- const out = [];
8999
- for (const p of paths) {
9000
- if (seen.has(p)) continue;
9001
- seen.add(p);
9002
- out.push(p);
8881
+ /** One listing turned into the segment rows a caller can act on — the only
8882
+ * place a `SegmentRow` is built out of a directory read. */
8883
+ function segmentRowsOf(locationId, paths) {
8884
+ const rows = [];
8885
+ let unparseable = 0;
8886
+ for (const path of paths) {
8887
+ const parsed = parseSegmentPath(path);
8888
+ if (parsed === null) {
8889
+ unparseable += 1;
8890
+ continue;
8891
+ }
8892
+ rows.push({
8893
+ deviceId: parsed.deviceId,
8894
+ profile: parsed.profile,
8895
+ startMs: parsed.startMs,
8896
+ durMs: parsed.durMs,
8897
+ bytes: parsed.bytes,
8898
+ path,
8899
+ locationId
8900
+ });
9003
8901
  }
9004
- return out;
9005
- }
9006
- function samePaths(a, b) {
9007
- if (a.length !== b.length) return false;
9008
- const set = new Set(a);
9009
- for (const p of b) if (!set.has(p)) return false;
9010
- return true;
8902
+ return {
8903
+ rows,
8904
+ unparseable
8905
+ };
9011
8906
  }
9012
8907
  function rowToValue(row) {
9013
8908
  return {
@@ -9015,7 +8910,6 @@ function rowToValue(row) {
9015
8910
  profile: row.profile,
9016
8911
  locationId: row.locationId,
9017
8912
  hourStartMs: row.hourStartMs,
9018
- paths: encodePaths(row.paths),
9019
8913
  bytes: row.bytes,
9020
8914
  segments: row.segments,
9021
8915
  minStartMs: row.minStartMs,
@@ -9028,44 +8922,21 @@ function rowToValue(row) {
9028
8922
  function sealedOf(raw) {
9029
8923
  return raw === 1 || raw === true;
9030
8924
  }
9031
- function recordToRow(key, data) {
9032
- const deviceId = Number(data["deviceId"]);
9033
- const profile = data["profile"];
9034
- const locationId = data["locationId"];
9035
- const hour = Number(data["hourStartMs"]);
9036
- const decodedPaths = decodePaths(data["paths"]);
9037
- if (decodedPaths === null) return null;
9038
- const raw = decodedPaths;
9039
- 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;
9040
- return segmentHourRow({
9041
- key,
9042
- deviceId,
9043
- profile,
9044
- locationId,
9045
- hourStartMs: hour,
9046
- paths: raw,
9047
- sealed: sealedOf(data["sealed"])
9048
- });
9049
- }
9050
- /** The paths of a summary row: one shared empty array, never a per-row
9051
- * allocation, and never mistakable for "this hour holds nothing". */
9052
- var NO_PATHS = Object.freeze([]);
9053
8925
  function finiteOr0(raw) {
9054
8926
  const n = Number(raw);
9055
8927
  return Number.isFinite(n) ? n : 0;
9056
8928
  }
9057
8929
  /**
9058
- * One row of the boot projection ({@link SEGMENT_HOUR_SUMMARY_COLUMNS}).
8930
+ * One persisted row.
9059
8931
  *
9060
- * The identity half is validated exactly as {@link recordToRow} validates it —
9061
- * a row that cannot say which camera, profile, location and hour it is about
9062
- * is skipped, because a summary keyed on garbage would refuse the write path
9063
- * for an hour that is perfectly fine. The aggregate half is taken as the
9064
- * database wrote it: a row that predates the aggregate columns reports zeros,
9065
- * which UNDER-reports its disk (prunes less, never more) until
9066
- * {@link SegmentHourLedger.repairAggregates} rewrites it.
8932
+ * The identity half is validated: a row that cannot say which camera, profile,
8933
+ * location and hour it is about is DISCARDED, because a row keyed on garbage
8934
+ * would refuse the write path for an hour that is perfectly fine. The aggregate
8935
+ * half is taken as the database wrote it — a row that predates the aggregate
8936
+ * columns reports zeros, which UNDER-reports its disk (prunes less, never more)
8937
+ * until a listing of that hour re-derives it.
9067
8938
  */
9068
- function summaryToRow(key, data) {
8939
+ function recordToRow(key, data) {
9069
8940
  const deviceId = Number(data["deviceId"]);
9070
8941
  const profile = data["profile"];
9071
8942
  const locationId = data["locationId"];
@@ -9077,8 +8948,6 @@ function summaryToRow(key, data) {
9077
8948
  profile,
9078
8949
  locationId,
9079
8950
  hourStartMs: hour,
9080
- paths: NO_PATHS,
9081
- pathsResident: false,
9082
8951
  sealed: sealedOf(data["sealed"]),
9083
8952
  bytes: finiteOr0(data["bytes"]),
9084
8953
  segments: finiteOr0(data["segments"]),
@@ -9100,11 +8969,12 @@ var SPEC = {
9100
8969
  var SegmentHourLedger = class SegmentHourLedger {
9101
8970
  ledger;
9102
8971
  logger;
9103
- /** Held for the two ARCHIVE reads below, which are queries rather than mirror
8972
+ /** Held for the ARCHIVE reads below, which are queries rather than mirror
9104
8973
  * lookups. Every DECISION path (`recordSegment`, `dropSegments`, the prune
9105
8974
  * refusals) still answers from the mirror alone — D49 is about gates, and an
9106
8975
  * accounting number is not a gate. */
9107
8976
  store;
8977
+ listHour;
9108
8978
  isLocationUsable;
9109
8979
  now;
9110
8980
  loadChunk;
@@ -9118,6 +8988,19 @@ var SegmentHourLedger = class SegmentHourLedger {
9118
8988
  lanes = /* @__PURE__ */ new Map();
9119
8989
  /** Per (device, profile): the hour its writer last appended to. */
9120
8990
  writerHours = /* @__PURE__ */ new Map();
8991
+ /**
8992
+ * Segment paths THIS PROCESS has already accounted for, per hour key.
8993
+ *
8994
+ * The `paths` column used to make the write path idempotent for free: an
8995
+ * append checked `held.paths.includes(path)`. With the column gone the
8996
+ * aggregate cannot say whether it already counted a name, so the dedup lives
8997
+ * here — bounded to the hours this process actually writes (two per writer:
8998
+ * the current hour and the one it just left), dropped when the writer moves
8999
+ * on. A duplicate that survives a RESTART is not caught here and is not
9000
+ * silent either: the row is unsealed, and the first listing of the closed
9001
+ * hour re-derives the aggregate exactly ({@link adoptWalkedHour}).
9002
+ */
9003
+ accounted = /* @__PURE__ */ new Map();
9121
9004
  /** Hour keys a write was REFUSED for in this process — never sealed by it. */
9122
9005
  refused = /* @__PURE__ */ new Set();
9123
9006
  /**
@@ -9130,6 +9013,7 @@ var SegmentHourLedger = class SegmentHourLedger {
9130
9013
  constructor(deps) {
9131
9014
  this.logger = deps.logger;
9132
9015
  this.store = deps.store;
9016
+ this.listHour = deps.listHour ?? null;
9133
9017
  this.isLocationUsable = deps.isLocationUsable ?? (() => true);
9134
9018
  this.now = deps.now ?? (() => Date.now());
9135
9019
  this.loadChunk = deps.loadPacing?.chunkSize ?? LOAD_CHUNK;
@@ -9148,22 +9032,17 @@ var SegmentHourLedger = class SegmentHourLedger {
9148
9032
  return this.ledger.declare();
9149
9033
  }
9150
9034
  /**
9151
- * Boot load — **T1 only** (D287).
9152
- *
9153
- * One projected query over {@link SEGMENT_HOUR_SUMMARY_COLUMNS}, folded into
9154
- * the mirror in chunks with a yield between them, then one point-read page
9155
- * that materialises the live tail so the write path can gate from RAM.
9035
+ * Boot load — every hour row, in one query.
9156
9036
  *
9157
- * It replaces `DurableLedger.load`, which fetches whole rows and therefore
9158
- * the whole archive's path strings, and it keeps that primitive's two rules:
9159
- * a failed read RETURNS WHAT IS ALREADY MIRRORED rather than clearing it,
9160
- * and a malformed row is skipped rather than repaired.
9037
+ * Since D523 the whole row is ten scalars, so there is no second tier to
9038
+ * materialise and no projection to choose: the mirror this returns is the
9039
+ * ledger, entire. It keeps `DurableLedger.load`'s two rules — a failed read
9040
+ * RETURNS WHAT IS ALREADY MIRRORED rather than clearing it, and a malformed
9041
+ * row is skipped rather than repaired.
9161
9042
  *
9162
- * It deliberately does NOT replay the archive into {@link RecordingIndex}.
9163
- * `hydrated` now means "the rows are resident", so a ledger-known hour is
9164
- * merely *hydratable from T3*: the first read of it pays one point read
9165
- * ({@link detailForWindow}), and an hour whose tail the ledger is missing is
9166
- * no longer permanently invisible to playback.
9043
+ * It deliberately does NOT replay the archive into {@link RecordingIndex}: a
9044
+ * row says how MUCH an hour holds, never which files, so only a listing can
9045
+ * seed the index (D523).
9167
9046
  */
9168
9047
  async load() {
9169
9048
  const startedMs = this.now();
@@ -9171,7 +9050,6 @@ var SegmentHourLedger = class SegmentHourLedger {
9171
9050
  try {
9172
9051
  records = await this.store.query.query({
9173
9052
  collection: RECORDING_SEGMENT_HOURS_COLLECTION,
9174
- columns: SEGMENT_HOUR_SUMMARY_COLUMNS,
9175
9053
  filter: { limit: this.rowCap }
9176
9054
  });
9177
9055
  } catch (err) {
@@ -9188,7 +9066,7 @@ var SegmentHourLedger = class SegmentHourLedger {
9188
9066
  const end = Math.min(i + this.loadChunk, records.length);
9189
9067
  for (let j = i; j < end; j += 1) {
9190
9068
  const record = records[j];
9191
- const row = summaryToRow(record.id, record.data);
9069
+ const row = recordToRow(record.id, record.data);
9192
9070
  if (row === null) {
9193
9071
  skipped += 1;
9194
9072
  continue;
@@ -9197,7 +9075,7 @@ var SegmentHourLedger = class SegmentHourLedger {
9197
9075
  }
9198
9076
  if (end < records.length) await this.yieldBetween();
9199
9077
  }
9200
- if (skipped > 0) this.logger.warn("recorder: segment-hour summaries skipped as malformed — they gate NOTHING", { meta: {
9078
+ if (skipped > 0) this.logger.warn("recorder: segment-hour rows skipped as malformed — they gate NOTHING", { meta: {
9201
9079
  collection: RECORDING_SEGMENT_HOURS_COLLECTION,
9202
9080
  skipped
9203
9081
  } });
@@ -9205,11 +9083,8 @@ var SegmentHourLedger = class SegmentHourLedger {
9205
9083
  limit: this.rowCap,
9206
9084
  loaded: records.length
9207
9085
  } });
9208
- const tail = await this.materializeLiveTail();
9209
9086
  this.logger.info("recorder: segment-hour ledger loaded", { meta: {
9210
9087
  hours: this.ledger.size,
9211
- pathsLoaded: false,
9212
- liveTailHours: tail,
9213
9088
  skipped,
9214
9089
  queryMs: queriedMs - startedMs,
9215
9090
  ms: this.now() - startedMs,
@@ -9217,8 +9092,7 @@ var SegmentHourLedger = class SegmentHourLedger {
9217
9092
  } });
9218
9093
  return this.ledger.snapshot();
9219
9094
  }
9220
- /** Every hour the ledger knows — summaries included. A row whose
9221
- * `pathsResident` is false carries real aggregates and empty `paths`. */
9095
+ /** Every hour row held. Pure RAM. */
9222
9096
  snapshot() {
9223
9097
  return this.ledger.snapshot();
9224
9098
  }
@@ -9228,10 +9102,10 @@ var SegmentHourLedger = class SegmentHourLedger {
9228
9102
  * The archive's answer to "what should placement plan for", and the reason it
9229
9103
  * is here rather than a `census` loop or an index read:
9230
9104
  *
9231
- * - `RecordingIndex.accountingByProfile` is the wrong authority. Since D287
9232
- * it holds the live tail plus whatever an operator hydrated, so a camera
9233
- * nobody has viewed since boot reports NO profiles — and the placement
9234
- * pass then never plans it (D295 §3a #9, D299).
9105
+ * - `RecordingIndex.accountingByProfile` is the wrong authority. It holds
9106
+ * the live tail plus whatever a reader hydrated, so a camera nobody has
9107
+ * viewed since boot reports NO profiles — and the placement pass then
9108
+ * never plans it (D295 §3a #9, D299).
9235
9109
  * - `census({ deviceId, profiles: [p] })` is the right authority but the
9236
9110
  * wrong shape for a 30 s tick: it is one SQL round trip per (camera,
9237
9111
  * profile), so a 16-camera fleet pays 48 SERIAL trips over the UDS door
@@ -9239,10 +9113,7 @@ var SegmentHourLedger = class SegmentHourLedger {
9239
9113
  * 240003` pass (see `PLACEMENT_STORE_TIMEOUT_MS`), and it grows with the
9240
9114
  * fleet.
9241
9115
  *
9242
- * So it is answered from T1, the hour summary D287 made permanently resident
9243
- * precisely so that O(writers × days) questions cost no I/O: ~10–15 MB and
9244
- * ~47 k rows even at 30 TB, one fold, no query. A hydrated segment row is not
9245
- * needed to know that an hour exists.
9116
+ * So it is answered from the mirror: one fold, no query, no I/O.
9246
9117
  *
9247
9118
  * An EMPTY result for a device is a real answer — "the archive knows no hour
9248
9119
  * for this camera" — and it is also what a ledger whose load failed reports,
@@ -9259,136 +9130,111 @@ var SegmentHourLedger = class SegmentHourLedger {
9259
9130
  }
9260
9131
  return out;
9261
9132
  }
9262
- /** Does the ledger hold a row for this hour at all? Pure RAM — the gate the
9263
- * storage-walk repair consults before it upserts a rebuilt hour. */
9133
+ /** Does the ledger hold a row for this hour at all? Pure RAM. */
9264
9134
  knowsHour(deviceId, profile, locationId, hour) {
9265
9135
  return this.ledger.has(segmentHourKey(deviceId, profile, locationId, hour));
9266
9136
  }
9267
- /**
9268
- * The paths of ONE held hour row, materialised — or `null` when they could
9269
- * not be read.
9270
- *
9271
- * {@link detailForWindow} answers per (device, window) and drops the profile
9272
- * on the way out, which is fine for a reader assembling a playlist and wrong
9273
- * for a caller that must act on one hour ROW: the ghost-location reconcile
9274
- * decides per `(deviceId, profile, locationId, hour)` and hands the resulting
9275
- * paths straight back to {@link dropSegments} / {@link repointSegments},
9276
- * whose bucket key is that same tuple.
9277
- *
9278
- * `null` is "known, and its detail could not be had" — never an empty array.
9279
- * The one caller FORGETS rows on this answer, and a failed point read that
9280
- * came back as `[]` would read as "this hour holds nothing", which is the
9281
- * D148 prune-on-no-evidence this ledger refuses to make.
9282
- */
9283
- async hourPaths(deviceId, profile, locationId, hour) {
9284
- const key = segmentHourKey(deviceId, profile, locationId, hour);
9285
- if (!this.ledger.has(key)) return null;
9286
- const held = await this.resident(key, deviceId);
9287
- return held === null ? null : held.paths;
9288
- }
9289
- /**
9290
- * The hours of `[fromMs, toMs)` the ledger knows, with their paths — the
9291
- * cold-hour detail read that replaces the boot replay (D287).
9292
- *
9293
- * One bounded point read against the settings store, never the media array:
9294
- * a `readdir` of one hour directory was measured at ~650 ms under load on
9295
- * the live share, and the array is the resource the scrub feeder is
9296
- * competing for. Hours the ledger does NOT know are absent from the result,
9297
- * and the caller walks those from disk exactly as it always did.
9298
- *
9299
- * **A read that fails returns what it already had** (D49). The caller sees
9300
- * fewer hours, the index stays `unknown` for the rest, and the window falls
9301
- * through to the walk — nothing is destroyed and nothing renders as an
9302
- * empty hour that was merely unread.
9303
- */
9304
- async detailForWindow(deviceId, fromMs, toMs) {
9305
- const first = hourStartMs(fromMs);
9306
- const out = [];
9307
- const wanted = [];
9308
- for (const row of this.ledger.snapshot()) {
9309
- if (row.deviceId !== deviceId) continue;
9310
- if (row.hourStartMs < first || row.hourStartMs >= toMs) continue;
9311
- if (row.pathsResident) {
9312
- out.push(detailOf(row));
9313
- continue;
9314
- }
9315
- wanted.push(row.key);
9316
- }
9317
- for (const row of await this.materialize(wanted, deviceId)) out.push(detailOf(row));
9318
- return out;
9137
+ /** One held hour row, or `undefined`. Pure RAM — since D523 a row carries
9138
+ * everything it has. */
9139
+ hourRow(deviceId, profile, locationId, hour) {
9140
+ return this.ledger.get(segmentHourKey(deviceId, profile, locationId, hour));
9319
9141
  }
9320
9142
  /**
9321
- * Union a CLOSED hour the storage walk read into its row, and SEAL it.
9322
- *
9323
- * The reader repairs the durable index it just reconstructed, so the next
9324
- * cold read of this hour is a point read instead of another `readdir`
9325
- * (`docs/design/2026-08-29-reference-nvr-recording-model.md` §4.2), and it
9326
- * seals the row because a directory read of a closed hour is the one
9327
- * complete answer there is. The caller guarantees the hour is CLOSED — a
9328
- * live hour's directory races its writer, so a walk of it is a lower bound.
9143
+ * A LISTING of a CLOSED hour is adopted as that hour's account, and SEALS it.
9329
9144
  *
9330
- * A row the ledger already holds is UNIONED, never replaced: the walk can
9331
- * only add (D148) — a path the row names and the directory lacks stays,
9332
- * because "the reader did not see it" is not evidence it is gone. This is
9333
- * the repair D445 described and did not perform: its predecessor returned
9334
- * on any known key, so a row known INCOMPLETELY — the only kind that needs
9335
- * repairing — was exactly the row it refused to touch, and the export's
9336
- * `forgottenSegments=9758` was counted again the next morning.
9145
+ * The directory is the authority for which segments exist (D523), so a
9146
+ * listing of an hour that is OVER is the one complete answer there is — it
9147
+ * replaces the row's aggregate rather than merging with it, which is what
9148
+ * makes an hour that was over-counted (a duplicate finalize across a restart)
9149
+ * or under-counted (a writer torn down mid-hour) self-correcting. The caller
9150
+ * guarantees the hour is CLOSED: a live hour's directory races its writer, so
9151
+ * a listing of it is a lower bound.
9337
9152
  *
9338
- * `null` when the row is known and its detail could not be read: nothing
9339
- * is written from a row whose paths are not resident.
9153
+ * A listing that would erase more than {@link HOUR_LISTING_MAX_SHRINK_SHARE}
9154
+ * of what the row accounts for is REFUSED and named. That is the one thing a
9155
+ * listing cannot be trusted about: a root that has gone away and a bucket a
9156
+ * dead round half-deleted both answer short, and neither is distinguishable
9157
+ * from an hour that is simply smaller than the row thought.
9340
9158
  *
9341
9159
  * @seal-authority closed-hour-listing — a directory read of an hour that is
9342
9160
  * OVER is the one complete answer there is; the caller guarantees the hour is
9343
- * closed (D451, D521).
9161
+ * closed (D451, D521, D523).
9344
9162
  */
9345
9163
  async adoptWalkedHour(deviceId, profile, locationId, hour, paths) {
9346
9164
  const key = segmentHourKey(deviceId, profile, locationId, hour);
9165
+ const identity = {
9166
+ key,
9167
+ deviceId,
9168
+ profile,
9169
+ locationId,
9170
+ hourStartMs: hour
9171
+ };
9172
+ const walked = hourAggregate(paths);
9347
9173
  return this.serialized(key, async () => {
9348
- if (!this.ledger.has(key)) {
9349
- if (paths.length === 0) return {
9174
+ const held = this.ledger.get(key);
9175
+ if (held === void 0) {
9176
+ if (walked.segments === 0) return {
9350
9177
  created: false,
9351
9178
  added: 0,
9352
9179
  sealedNow: false
9353
9180
  };
9354
- const row = segmentHourRow({
9355
- key,
9356
- deviceId,
9357
- profile,
9358
- locationId,
9359
- hourStartMs: hour,
9360
- paths: uniquePaths(paths),
9181
+ await this.ledger.put(hourRowOf(identity, {
9182
+ ...walked,
9361
9183
  sealed: true
9362
- });
9363
- await this.ledger.put(row);
9364
- this.logger.info("recorder: adopted a closed hour the disk walk rebuilt and the ledger never held", {
9184
+ }));
9185
+ this.logger.info("recorder: adopted a closed hour the disk listing rebuilt and the ledger never held", {
9365
9186
  tags: { deviceId },
9366
9187
  meta: {
9367
9188
  profile,
9368
9189
  locationId,
9369
9190
  hourStartMs: hour,
9370
- segments: row.segments,
9371
- bytes: row.bytes
9191
+ segments: walked.segments,
9192
+ bytes: walked.bytes
9372
9193
  }
9373
9194
  });
9374
9195
  return {
9375
9196
  created: true,
9376
- added: row.paths.length,
9197
+ added: walked.segments,
9377
9198
  sealedNow: true
9378
9199
  };
9379
9200
  }
9380
- const held = await this.resident(key, deviceId);
9381
- if (held === null) return null;
9382
- const union = uniquePaths([...held.paths, ...paths]);
9383
- const added = union.length - held.paths.length;
9384
- if (added === 0 && held.sealed) return {
9201
+ const shrink = held.segments - walked.segments;
9202
+ if (shrink > 0 && shrink > held.segments * .5) {
9203
+ this.logger.warn("recorder: hour listing REFUSED — it accounts for less than half of what this hour row holds, which is what an unmounted root looks like; the row stands and stays unsealed", {
9204
+ tags: { deviceId },
9205
+ meta: {
9206
+ profile,
9207
+ locationId,
9208
+ hourStartMs: hour,
9209
+ held: held.segments,
9210
+ listed: walked.segments,
9211
+ maxShare: HOUR_LISTING_MAX_SHRINK_SHARE
9212
+ }
9213
+ });
9214
+ return {
9215
+ created: false,
9216
+ added: 0,
9217
+ sealedNow: false
9218
+ };
9219
+ }
9220
+ const added = Math.max(0, walked.segments - held.segments);
9221
+ if (added === 0 && shrink === 0 && held.sealed) return {
9385
9222
  created: false,
9386
9223
  added: 0,
9387
9224
  sealedNow: false
9388
9225
  };
9389
- await this.ledger.put(segmentHourRow({
9390
- ...held,
9391
- paths: union,
9226
+ if (shrink > 0) this.logger.info("recorder: hour listing accounts for FEWER segments than the row did — the directory decides", {
9227
+ tags: { deviceId },
9228
+ meta: {
9229
+ profile,
9230
+ locationId,
9231
+ hourStartMs: hour,
9232
+ held: held.segments,
9233
+ listed: walked.segments
9234
+ }
9235
+ });
9236
+ await this.ledger.put(hourRowOf(identity, {
9237
+ ...walked,
9392
9238
  sealed: true
9393
9239
  }));
9394
9240
  return {
@@ -9418,9 +9264,9 @@ var SegmentHourLedger = class SegmentHourLedger {
9418
9264
  }
9419
9265
  /**
9420
9266
  * A finalize this ledger was NEVER handed — the fire-and-forget lane refused
9421
- * it. The hour's row is short by that path and this process is the only
9422
- * party that knows, so the hour must not be sealed by it; a reader walks it
9423
- * once instead, which is exactly what an unsealed row is for.
9267
+ * it. The hour's row is short by that segment and this process is the only
9268
+ * party that knows, so the hour must not be sealed by it; a listing corrects
9269
+ * it instead, which is exactly what an unsealed row is for.
9424
9270
  */
9425
9271
  markRefused(row) {
9426
9272
  this.refused.add(hourOf(row).key);
@@ -9448,7 +9294,7 @@ var SegmentHourLedger = class SegmentHourLedger {
9448
9294
  marked += 1;
9449
9295
  }
9450
9296
  if (marked === 0) return 0;
9451
- this.logger.warn("recorder: segments this writer produced never reached the hour ledger — these hours stay UNSEALED and a reader walks them", {
9297
+ this.logger.warn("recorder: segments this writer produced never reached the hour ledger — these hours stay UNSEALED and a listing corrects them", {
9452
9298
  tags: { deviceId: span.deviceId },
9453
9299
  meta: {
9454
9300
  profile: span.profile,
@@ -9481,10 +9327,11 @@ var SegmentHourLedger = class SegmentHourLedger {
9481
9327
  graceMs: WRITER_HOUR_ENTRY_GRACE_MS
9482
9328
  }
9483
9329
  });
9330
+ this.accounted.delete(target.key);
9484
9331
  return;
9485
9332
  }
9486
9333
  if (this.refused.has(target.key) || this.refusedWriterHours.has(writerHourKey(target.deviceId, target.profile, target.hourStartMs))) {
9487
- this.logger.warn("recorder: hour left unsealed — a ledger write for it was refused, so its row is short and a reader must walk it", {
9334
+ this.logger.warn("recorder: hour left unsealed — a ledger write for it was refused, so its row is short and a listing must correct it", {
9488
9335
  tags: { deviceId: target.deviceId },
9489
9336
  meta: {
9490
9337
  profile: target.profile,
@@ -9492,10 +9339,12 @@ var SegmentHourLedger = class SegmentHourLedger {
9492
9339
  hourStartMs: target.hourStartMs
9493
9340
  }
9494
9341
  });
9342
+ this.accounted.delete(target.key);
9495
9343
  return;
9496
9344
  }
9497
- const held = await this.resident(target.key, target.deviceId);
9498
- if (held === null || held.sealed) return;
9345
+ const held = this.ledger.get(target.key);
9346
+ this.accounted.delete(target.key);
9347
+ if (held === void 0 || held.sealed) return;
9499
9348
  await this.ledger.put({
9500
9349
  ...held,
9501
9350
  sealed: true
@@ -9523,11 +9372,25 @@ var SegmentHourLedger = class SegmentHourLedger {
9523
9372
  /**
9524
9373
  * Run `fn` after every mutation already queued on `key`.
9525
9374
  *
9526
- * A row is read, appended to and written back with awaits in between; two
9527
- * such runs on one key interleave, and the second write carries a paths
9528
- * array that never saw the first — the write-behind's own lost update. The
9529
- * lane makes "read, append, write" one step per key. A rejected step must
9530
- * not poison the lane: the next step still runs.
9375
+ * It was the lost-update guard: a row was read from the STORE, folded into
9376
+ * and written back with awaits in between, so two such runs on one key
9377
+ * interleaved and the second write carried a path set that never saw the
9378
+ * first — the sparse rows D451 found. Since D523 there is no store read on
9379
+ * any of these paths (the mirror holds the whole row) and `DurableLedger.put`
9380
+ * sets the mirror synchronously before it awaits the store, so every
9381
+ * get→fold→put here is synchronous up to the mirror and the interleaving
9382
+ * window is gone.
9383
+ *
9384
+ * What the lane still buys is the ORDER of the durable writes per hour key —
9385
+ * a rollover seal fired without an await must not land over an append that
9386
+ * was queued before it — and a handle for {@link flush} to wait on at
9387
+ * shutdown. A rejected step must not poison the lane: the next step still
9388
+ * runs.
9389
+ *
9390
+ * Note for anyone tempted to delete it: `segment-hour-seal.spec.ts` does NOT
9391
+ * catch its removal any more, and says so. Pinning durable write ORDER needs
9392
+ * a store whose latency a test can control, which `fakeSettingsStore` has no
9393
+ * way to express.
9531
9394
  */
9532
9395
  serialized(key, fn) {
9533
9396
  const run = (this.lanes.get(key) ?? Promise.resolve()).then(fn, fn);
@@ -9539,116 +9402,59 @@ var SegmentHourLedger = class SegmentHourLedger {
9539
9402
  return run;
9540
9403
  }
9541
9404
  /**
9542
- * Fetch whole rows for `keys` and promote them in the mirror.
9405
+ * List the hour directories of `rows`, paced, and expand them into segment
9406
+ * rows.
9543
9407
  *
9544
- * Paged and yielding: an export window can name thousands of hours, and one
9545
- * `whereIn` over all of them would rebuild the very whole-archive response
9546
- * D287 removed. A page that FAILS ends the materialise and returns what
9547
- * landed — partial detail is real detail, and the caller's fallback (a disk
9548
- * walk, or refusing a write) is strictly safer than a guess.
9549
- *
9550
- * A row the store returns but this cannot parse is EVICTED from the mirror
9551
- * rather than left as a summary that can never become resident: it re-seeds
9552
- * cold on the next write, which is `DurableLedger`'s rule 4 and the only
9553
- * escape from an hour permanently frozen against its own writer.
9408
+ * The one place the ledger touches the array. An hour whose listing FAILED
9409
+ * takes no part in the caller's pass and is SAID — a branch that drops work
9410
+ * silently reads as "there was nothing there", which for a drain or a prune
9411
+ * is the difference between "moved everything" and "moved what it could see".
9554
9412
  */
9555
- async materialize(keys, deviceId) {
9413
+ async listHours(rows, reason) {
9556
9414
  const out = [];
9557
- if (keys.length === 0) return out;
9558
- const tags = deviceId === void 0 ? {} : { tags: { deviceId } };
9559
- for (let i = 0; i < keys.length; i += HOUR_DETAIL_PAGE) {
9560
- const page = keys.slice(i, i + HOUR_DETAIL_PAGE);
9561
- let records;
9562
- try {
9563
- records = await this.store.query.query({
9564
- collection: RECORDING_SEGMENT_HOURS_COLLECTION,
9565
- filter: {
9566
- whereIn: { key: page },
9567
- limit: page.length
9568
- }
9569
- });
9570
- } catch (err) {
9571
- this.logger.warn("recorder: hour-detail point read failed — these hours stay summaries, and the caller walks the disk or declines the write", {
9572
- ...tags,
9415
+ if (rows.length === 0) return out;
9416
+ const lister = this.listHour;
9417
+ if (lister === null) {
9418
+ this.logger.warn("recorder: no hour lister on this ledger — nothing that needs segment NAMES can run here", { meta: {
9419
+ hours: rows.length,
9420
+ reason
9421
+ } });
9422
+ return out;
9423
+ }
9424
+ let listed = 0;
9425
+ for (const row of rows) {
9426
+ const paths = await lister(row.locationId, row.deviceId, row.profile, row.hourStartMs);
9427
+ if (paths === null) {
9428
+ this.logger.warn("recorder: hour directory could not be listed — this hour takes NO part in this pass", {
9429
+ tags: { deviceId: row.deviceId },
9573
9430
  meta: {
9574
- collection: RECORDING_SEGMENT_HOURS_COLLECTION,
9575
- asked: page.length,
9576
- materialized: out.length,
9577
- error: String(err)
9431
+ profile: row.profile,
9432
+ locationId: row.locationId,
9433
+ hourStartMs: row.hourStartMs,
9434
+ accounted: row.segments,
9435
+ reason
9578
9436
  }
9579
9437
  });
9580
- return out;
9438
+ continue;
9581
9439
  }
9582
- for (const record of records) {
9583
- const row = recordToRow(record.id, record.data);
9584
- if (row === null) {
9585
- this.ledger.evict(record.id);
9586
- this.logger.warn("recorder: hour row is unparseable — dropping the summary so the hour re-seeds cold instead of freezing its writer", {
9587
- ...tags,
9588
- meta: { key: record.id }
9589
- });
9590
- continue;
9440
+ const { rows: segments, unparseable } = segmentRowsOf(row.locationId, paths);
9441
+ if (unparseable > 0) this.logger.warn("recorder: hour directory holds names that are not segments", {
9442
+ tags: { deviceId: row.deviceId },
9443
+ meta: {
9444
+ profile: row.profile,
9445
+ locationId: row.locationId,
9446
+ hourStartMs: row.hourStartMs,
9447
+ unparseable,
9448
+ reason
9591
9449
  }
9592
- this.ledger.stage(row);
9593
- out.push(row);
9594
- }
9595
- if (i + HOUR_DETAIL_PAGE < keys.length) await this.yieldBetween();
9450
+ });
9451
+ out.push(...segments);
9452
+ listed += 1;
9453
+ if (listed % 8 === 0) await this.yieldBetween();
9596
9454
  }
9597
9455
  return out;
9598
9456
  }
9599
9457
  /**
9600
- * Materialise every hour these devices hold, and report how many stayed
9601
- * summaries.
9602
- *
9603
- * The reconcile is the one path that DELETES durable rows, so it may not run
9604
- * against a mirror that only half knows what it holds. This is a whole-device
9605
- * read and it is deliberately confined here: `reconcileFromIndex` follows an
9606
- * operator-armed rescan of that same device — a recursive `readdir` over
9607
- * >1M files — so a paged point read alongside it is noise, and it is the one
9608
- * caller whose correctness depends on completeness.
9609
- */
9610
- async materializeDevices(deviceIds) {
9611
- const keys = [];
9612
- for (const row of this.ledger.snapshot()) if (!row.pathsResident && deviceIds.has(row.deviceId)) keys.push(row.key);
9613
- if (keys.length === 0) return;
9614
- await this.materialize(keys);
9615
- const stillSummary = /* @__PURE__ */ new Map();
9616
- for (const row of this.ledger.snapshot()) {
9617
- if (row.pathsResident || !deviceIds.has(row.deviceId)) continue;
9618
- stillSummary.set(row.deviceId, (stillSummary.get(row.deviceId) ?? 0) + 1);
9619
- }
9620
- for (const [deviceId, unreadable] of stillSummary) this.logger.warn("recorder: hours without readable detail take NO part in this reconcile — they are neither pruned nor unioned", {
9621
- tags: { deviceId },
9622
- meta: {
9623
- asked: keys.length,
9624
- unreadable
9625
- }
9626
- });
9627
- }
9628
- /** The hour a write path may touch: current + previous. Materialised at boot
9629
- * so {@link recordSegment}'s dedup gate answers from RAM (D49). */
9630
- async materializeLiveTail() {
9631
- const oldest = hourStartMs(this.now()) - (LIVE_TAIL_HOURS - 1) * HOUR_MS$4;
9632
- const keys = [];
9633
- for (const row of this.ledger.snapshot()) if (row.hourStartMs >= oldest && !row.pathsResident) keys.push(row.key);
9634
- return (await this.materialize(keys)).length;
9635
- }
9636
- /**
9637
- * Promote one HELD row to paths-resident, or answer `null`.
9638
- *
9639
- * `null` means "this hour is known and its detail could not be had" — the
9640
- * only honest answer to a failed point read, and every caller turns it into
9641
- * "do nothing". It is deliberately distinct from `this.ledger.get(key) ===
9642
- * undefined`, which means "no such hour" and is what licenses a fresh row.
9643
- */
9644
- async resident(key, deviceId) {
9645
- const held = this.ledger.get(key);
9646
- if (held !== void 0 && held.pathsResident) return held;
9647
- await this.materialize([key], deviceId);
9648
- const after = this.ledger.get(key);
9649
- return after !== void 0 && after.pathsResident ? after : null;
9650
- }
9651
- /**
9652
9458
  * Bytes / count / oldest / newest across a SET of storage locations, as ONE
9653
9459
  * `SUM`/`COUNT`/`MIN`/`MAX` over the hour rows.
9654
9460
  *
@@ -9689,9 +9495,9 @@ var SegmentHourLedger = class SegmentHourLedger {
9689
9495
  * ## Why two phases
9690
9496
  *
9691
9497
  * Phase one reads only the small columns and walks oldest-first until the
9692
- * accumulated bytes cover the target — normally one page. `paths` is by far
9693
- * the fattest column in the row and phase one never touches it. Phase two
9694
- * fetches paths for exactly the hours phase one chose.
9498
+ * accumulated bytes cover the target — normally one page. Phase two LISTS the
9499
+ * hour directories phase one chose, which is the only thing that can name the
9500
+ * files eviction is about to delete (D523).
9695
9501
  *
9696
9502
  * `exhausted` means the stream ran out before the target was met — the
9697
9503
  * caller's stop condition, and the one thing a bounded read must still be
@@ -9725,11 +9531,10 @@ var SegmentHourLedger = class SegmentHourLedger {
9725
9531
  /**
9726
9532
  * Every camera the ledger holds an hour for on these locations.
9727
9533
  *
9728
- * Answered from T1 — the always-resident hour summary, which carries
9729
- * `deviceId` and `locationId` — exactly like {@link profilesWithFootage},
9730
- * and for the same reason: this is an O(writers × days) question and it must
9731
- * cost no I/O. It runs on the disk-pressure path, where an I/O that fails
9732
- * would have to be turned into a verdict.
9534
+ * Answered from the mirror, exactly like {@link profilesWithFootage}, and for
9535
+ * the same reason: this is an O(writers × days) question and it must cost no
9536
+ * I/O. It runs on the disk-pressure path, where an I/O that fails would have
9537
+ * to be turned into a verdict.
9733
9538
  *
9734
9539
  * An EMPTY answer is "the mirror knows no hour here", which is also what a
9735
9540
  * ledger whose load failed reports — so the caller must treat it as "decide
@@ -9744,7 +9549,7 @@ var SegmentHourLedger = class SegmentHourLedger {
9744
9549
  * Bytes / segments / oldest / newest per CAMERA on these locations — the
9745
9550
  * per-camera half of the storage page, answered by the ARCHIVE (D418).
9746
9551
  *
9747
- * ## Why the T1 fold and not `census({ deviceId })` per camera
9552
+ * ## Why the mirror fold and not `census({ deviceId })` per camera
9748
9553
  *
9749
9554
  * `census` is the right authority for a SCOPE, and it cannot express this
9750
9555
  * scope: {@link SegmentHourLedger.predicate} takes ONE `locationId`, so
@@ -9754,14 +9559,10 @@ var SegmentHourLedger = class SegmentHourLedger {
9754
9559
  * accounts for. Per camera per volume that is `cameras × volumes` SQL
9755
9560
  * aggregates for one operator click, and it grows with the fleet.
9756
9561
  *
9757
- * The T1 hour summary answers all of them in ONE pass with no I/O, exactly
9758
- * as {@link devicesWithFootage} and {@link profilesWithFootage} already do,
9759
- * and it is the same rows `accountingForLocations` sums in SQL — the mirror
9760
- * is write-behind, so it is never BEHIND the store. The cost is one pass
9761
- * over the hour rows the mirror holds — O(writers × profiles × hours), ~19 k
9762
- * on the live hub (see {@link REPAIR_MAX_ROWS}) against 98 445 segments,
9763
- * i.e. it does NOT scale with the segment count — and it is capped for good
9764
- * by {@link SEGMENT_HOUR_SUMMARY_LIMIT}, the mirror's own ceiling.
9562
+ * The mirror answers all of them in ONE pass with no I/O, exactly as
9563
+ * {@link devicesWithFootage} and {@link profilesWithFootage} already do, and
9564
+ * it is the same rows `accountingForLocations` sums in SQL — the mirror is
9565
+ * write-behind, so it is never BEHIND the store.
9765
9566
  *
9766
9567
  * ## What an empty answer means
9767
9568
  *
@@ -9835,44 +9636,28 @@ var SegmentHourLedger = class SegmentHourLedger {
9835
9636
  rows: [],
9836
9637
  exhausted: true
9837
9638
  };
9838
- const full = await this.store.query.query({
9839
- collection: RECORDING_SEGMENT_HOURS_COLLECTION,
9840
- filter: {
9841
- whereIn: { key: chosen },
9842
- limit: chosen.length
9843
- }
9844
- });
9845
- const rows = [];
9846
- for (const record of full) {
9847
- const hour = recordToRow(record.id, record.data);
9848
- if (hour === null) continue;
9849
- this.ledger.stage(hour);
9850
- for (const path of hour.paths) {
9851
- const parsed = parseSegmentPath(path);
9852
- if (parsed === null) continue;
9853
- rows.push({
9854
- deviceId: parsed.deviceId,
9855
- profile: parsed.profile,
9856
- startMs: parsed.startMs,
9857
- durMs: parsed.durMs,
9858
- bytes: parsed.bytes,
9859
- path,
9860
- locationId: hour.locationId
9861
- });
9862
- }
9863
- }
9864
9639
  return {
9865
- rows: rows.toSorted((a, b) => a.startMs - b.startMs),
9640
+ rows: (await this.listHours(this.rowsOfKeys(chosen), "oldest-segments")).toSorted((a, b) => a.startMs - b.startMs),
9866
9641
  exhausted
9867
9642
  };
9868
9643
  }
9644
+ /** Mirror rows for keys a probe named. A key the mirror does not hold is
9645
+ * skipped: the probe and the mirror describe the same table, and an hour the
9646
+ * mirror never loaded is one this process may not act on. */
9647
+ rowsOfKeys(keys) {
9648
+ const out = [];
9649
+ for (const key of keys) {
9650
+ const row = this.ledger.get(key);
9651
+ if (row !== void 0) out.push(row);
9652
+ }
9653
+ return out;
9654
+ }
9869
9655
  /**
9870
9656
  * The predicate half of an {@link ArchiveScope}, as one filter.
9871
9657
  *
9872
9658
  * REFUSES an empty scope rather than compiling to "the whole collection".
9873
- * There is no read here that may be unscoped: at 30 TB the unscoped form is
9874
- * the ~460 MB boot query D287 deleted, and a helper that quietly allows it is
9875
- * how that query comes back under a new name.
9659
+ * There is no read here that may be unscoped: an unscoped stream lists every
9660
+ * hour directory on the hub.
9876
9661
  */
9877
9662
  static predicate(scope) {
9878
9663
  const where = {};
@@ -9885,6 +9670,15 @@ var SegmentHourLedger = class SegmentHourLedger {
9885
9670
  ...profiles.length > 0 ? { whereIn: { profile: [...profiles] } } : {}
9886
9671
  };
9887
9672
  }
9673
+ /** Does this row fall inside `scope`? The mirror-side twin of
9674
+ * {@link SegmentHourLedger.predicate}, for the passes that fold the mirror
9675
+ * instead of querying. */
9676
+ static inScope(row, scope) {
9677
+ if (scope.locationId !== void 0 && row.locationId !== scope.locationId) return false;
9678
+ if (scope.deviceId !== void 0 && row.deviceId !== scope.deviceId) return false;
9679
+ const profiles = scope.profiles ?? [];
9680
+ return profiles.length === 0 || profiles.includes(row.profile);
9681
+ }
9888
9682
  /**
9889
9683
  * What the ARCHIVE holds in this scope — segments and bytes, as ONE
9890
9684
  * `SUM` over the hour rows.
@@ -9928,6 +9722,15 @@ var SegmentHourLedger = class SegmentHourLedger {
9928
9722
  /**
9929
9723
  * Every segment in `scope`, oldest-first, ONE PAGE at a time.
9930
9724
  *
9725
+ * ## Two phases, and which authority answers which
9726
+ *
9727
+ * Phase one asks the TABLE which hours the scope holds, oldest-first — the
9728
+ * ordered aggregate no directory read can answer. Phase two LISTS those hour
9729
+ * directories, which is the only authority for the file names the caller is
9730
+ * about to move or delete (D523). An hour whose listing fails is skipped and
9731
+ * said; the caller's own N-versus-M report is what makes the short walk
9732
+ * visible.
9733
+ *
9931
9734
  * ## Why a keyset cursor and not `offset`
9932
9735
  *
9933
9736
  * Both callers MUTATE the scope while they walk it: the drain re-points each
@@ -9944,10 +9747,9 @@ var SegmentHourLedger = class SegmentHourLedger {
9944
9747
  *
9945
9748
  * ## What a failed read does
9946
9749
  *
9947
- * It ENDS the stream (D49: a fallible read destroys nothing) — and the
9948
- * caller's own N-versus-M report is what makes the short walk visible. A
9949
- * stream that swallowed the error and returned would be indistinguishable
9950
- * from a scope that was genuinely empty.
9750
+ * It ENDS the stream (D49: a fallible read destroys nothing). A stream that
9751
+ * swallowed the error and returned would be indistinguishable from a scope
9752
+ * that was genuinely empty.
9951
9753
  */
9952
9754
  async *streamSegments(scope) {
9953
9755
  const predicate = SegmentHourLedger.predicate(scope);
@@ -10000,8 +9802,8 @@ var SegmentHourLedger = class SegmentHourLedger {
10000
9802
  if (nextCursor !== cursor) emittedAtCursor = /* @__PURE__ */ new Set();
10001
9803
  for (const record of fresh) if (hourOfRecord(record) === nextCursor) emittedAtCursor.add(record.id);
10002
9804
  cursor = nextCursor;
10003
- const rows = await this.rowsForHours(fresh.map((record) => record.id), scope);
10004
- if (rows.length > 0) yield rows;
9805
+ const rows = await this.listHours(this.rowsOfKeys(fresh.map((record) => record.id)), "archive-stream");
9806
+ if (rows.length > 0) yield rows.toSorted((a, b) => a.startMs - b.startMs);
10005
9807
  if (probe.length < 128 && fresh.length === probe.length) return;
10006
9808
  }
10007
9809
  this.logger.warn("recorder: archive stream hit its page backstop — the rest of this scope was NOT walked", {
@@ -10015,110 +9817,50 @@ var SegmentHourLedger = class SegmentHourLedger {
10015
9817
  });
10016
9818
  }
10017
9819
  /**
10018
- * Phase two of {@link streamSegments}: the paths of exactly the hours phase
10019
- * one chose, expanded into segment rows.
9820
+ * Forget every hour row in `scope` — the durable half of an operator's
9821
+ * "discard this location's footage".
9822
+ *
9823
+ * It exists because the files are already GONE by the time it runs: the
9824
+ * discard wipes the class subtrees first, so every hour directory in the
9825
+ * scope answers ENOENT and nothing that needs a listing can name a single
9826
+ * segment. The rows are forgotten by their own identity instead, and the
9827
+ * count returned is the SEGMENTS they accounted for, which is the number the
9828
+ * operator is shown.
10020
9829
  *
10021
- * Staged into the mirror on the way through, exactly as `oldestSegments`
10022
- * does and for the same reason: both consumers immediately call a write path
10023
- * (`repointSegments` / `dropSegments`) which needs the hour paths-resident,
10024
- * and which `forget`s the row as it empties — so the staging is
10025
- * self-cleaning and the alternative is two reads per hour.
9830
+ * Never reachable except from that explicitly-armed action: this is the one
9831
+ * surface here that deletes durable rows with no evidence from the array at
9832
+ * all, because the operator's own instruction is the evidence.
10026
9833
  */
10027
- async rowsForHours(keys, scope) {
10028
- if (keys.length === 0) return [];
10029
- let records;
10030
- try {
10031
- records = await this.store.query.query({
10032
- collection: RECORDING_SEGMENT_HOURS_COLLECTION,
10033
- filter: {
10034
- whereIn: { key: [...keys] },
10035
- limit: keys.length
10036
- }
10037
- });
10038
- } catch (err) {
10039
- this.logger.warn("recorder: archive page detail unavailable — these hours take NO part in this pass", {
10040
- ...scope.deviceId !== void 0 ? { tags: { deviceId: scope.deviceId } } : {},
10041
- meta: {
10042
- collection: RECORDING_SEGMENT_HOURS_COLLECTION,
10043
- hours: keys.length,
10044
- error: String(err)
10045
- }
10046
- });
10047
- return [];
10048
- }
10049
- const rows = [];
10050
- for (const record of records) {
10051
- const hour = recordToRow(record.id, record.data);
10052
- if (hour === null) continue;
10053
- this.ledger.stage(hour);
10054
- for (const path of hour.paths) {
10055
- const parsed = parseSegmentPath(path);
10056
- if (parsed === null) continue;
10057
- rows.push({
10058
- deviceId: parsed.deviceId,
10059
- profile: parsed.profile,
10060
- startMs: parsed.startMs,
10061
- durMs: parsed.durMs,
10062
- bytes: parsed.bytes,
10063
- path,
10064
- locationId: hour.locationId
10065
- });
10066
- }
10067
- }
10068
- return rows.toSorted((a, b) => a.startMs - b.startMs);
10069
- }
10070
9834
  /**
10071
- * Rewrite, once, the rows whose aggregate columns are still the schema
10072
- * default.
9835
+ * Forget ONE hour row outright, and answer the segments it accounted for.
10073
9836
  *
10074
- * `declareCollection` adds a column additively, so every row written before
10075
- * the aggregate columns existed holds `segments = 0` — and an hour row with
10076
- * no segments cannot exist any other way (`dropSegments` forgets an hour that
10077
- * empties). So `segments = 0` IS the backfill predicate, and after one pass
10078
- * it selects nothing. A partial repair finishes on the next boot; until then
10079
- * the affected disks UNDER-report, which prunes less and never more.
9837
+ * The caller must have TRUSTED evidence that the hour holds nothing — a
9838
+ * directory listing under a volume anchor that agreed twice (D49), or a
9839
+ * per-path confirmation against every root there is. This method makes no
9840
+ * such check itself and can make none: it is handed a verdict, not evidence.
10080
9841
  *
10081
- * Since D287 the boot load carries no `paths`, and `paths` is the authority
10082
- * this repair derives from — so the stale rows are MATERIALISED first, at
10083
- * most {@link REPAIR_MAX_MATERIALIZE} per boot. That bound is the point: an
10084
- * unbounded materialise here would be the whole-archive read this stage
10085
- * removed, wearing a different name.
9842
+ * It exists because {@link adoptWalkedHour} deliberately refuses an EMPTY
9843
+ * listing — "the walk found nothing" is what an unmounted root looks like —
9844
+ * so the one repair a listing cannot express is the one that empties an hour.
10086
9845
  */
10087
- async repairAggregates() {
10088
- let repaired = 0;
10089
- try {
10090
- const stale = await this.store.query.query({
10091
- collection: RECORDING_SEGMENT_HOURS_COLLECTION,
10092
- columns: REPAIR_PROBE_COLUMNS,
10093
- filter: {
10094
- where: { segments: 0 },
10095
- limit: REPAIR_MAX_ROWS
10096
- }
10097
- });
10098
- const pending = stale.map((record) => record.id).filter((key) => this.ledger.get(key)?.pathsResident === false);
10099
- if (pending.length > REPAIR_MAX_MATERIALIZE) this.logger.info("recorder: segment-hour aggregate backfill is bounded this boot — the rest is repaired on the next one", { meta: {
10100
- pending: pending.length,
10101
- perBoot: REPAIR_MAX_MATERIALIZE
10102
- } });
10103
- await this.materialize(pending.slice(0, REPAIR_MAX_MATERIALIZE));
10104
- for (const record of stale) {
10105
- const held = this.ledger.get(record.id);
10106
- if (held === void 0 || !held.pathsResident || held.segments === 0) continue;
10107
- await this.ledger.put(held);
10108
- repaired += 1;
10109
- }
10110
- } catch (err) {
10111
- this.logger.warn("recorder: segment-hour aggregate repair failed — disks under-report", { meta: {
10112
- collection: RECORDING_SEGMENT_HOURS_COLLECTION,
10113
- error: String(err)
10114
- } });
10115
- return repaired;
9846
+ async forgetHour(deviceId, profile, locationId, hour) {
9847
+ const key = segmentHourKey(deviceId, profile, locationId, hour);
9848
+ return this.serialized(key, async () => {
9849
+ const held = this.ledger.get(key);
9850
+ if (held === void 0) return 0;
9851
+ await this.ledger.forget(key);
9852
+ this.accounted.delete(key);
9853
+ return held.segments;
9854
+ });
9855
+ }
9856
+ async forgetScope(scope) {
9857
+ let forgotten = 0;
9858
+ for (const row of this.ledger.snapshot()) {
9859
+ if (!SegmentHourLedger.inScope(row, scope)) continue;
9860
+ await this.ledger.forget(row.key);
9861
+ forgotten += row.segments;
10116
9862
  }
10117
- if (repaired > 0) this.logger.info("recorder: segment-hour aggregate columns backfilled", { meta: {
10118
- rows: repaired,
10119
- collection: RECORDING_SEGMENT_HOURS_COLLECTION
10120
- } });
10121
- return repaired;
9863
+ return forgotten;
10122
9864
  }
10123
9865
  /**
10124
9866
  * Write-behind: a just-finalized segment joins its hour row.
@@ -10126,8 +9868,8 @@ var SegmentHourLedger = class SegmentHourLedger {
10126
9868
  * THE hot path — every profile-writer lands here every `segmentSeconds`,
10127
9869
  * ~3.4 times a second fleet-wide. The append is {@link appendSegmentPath}:
10128
9870
  * one `parseSegmentPath` and four scalar updates, whatever the hour already
10129
- * holds. It used to re-derive the whole hour, which made one hour
10130
- * O(N²) — 65 k parses at 360 segments, 1.46 M in a restart-storm hour.
9871
+ * holds — and since D523 it needs no read of the store at all, because the
9872
+ * mirror carries the whole row.
10131
9873
  */
10132
9874
  async recordSegment(row) {
10133
9875
  const { key, hourStartMs: hour } = hourOf(row);
@@ -10140,7 +9882,12 @@ var SegmentHourLedger = class SegmentHourLedger {
10140
9882
  entryMs: row.startMs
10141
9883
  });
10142
9884
  await this.serialized(key, async () => {
10143
- if (!this.ledger.has(key)) {
9885
+ const seen = this.accounted.get(key);
9886
+ if (seen !== void 0 && seen.has(row.path)) return;
9887
+ if (seen === void 0) this.accounted.set(key, new Set([row.path]));
9888
+ else seen.add(row.path);
9889
+ const held = this.ledger.get(key);
9890
+ if (held === void 0) {
10144
9891
  await this.ledger.put(segmentHourRow({
10145
9892
  key,
10146
9893
  deviceId: row.deviceId,
@@ -10151,25 +9898,19 @@ var SegmentHourLedger = class SegmentHourLedger {
10151
9898
  }));
10152
9899
  return;
10153
9900
  }
10154
- const held = await this.resident(key, row.deviceId);
10155
- if (held === null) {
10156
- this.refused.add(key);
10157
- this.logger.warn("recorder: hour detail unavailable — this finalized segment is NOT in the durable hour row yet", {
10158
- tags: { deviceId: row.deviceId },
10159
- meta: {
10160
- key,
10161
- path: row.path
10162
- }
10163
- });
10164
- return;
10165
- }
10166
- if (held.paths.includes(row.path)) return;
10167
9901
  await this.ledger.put(appendSegmentPath(held, row.path));
10168
9902
  });
10169
9903
  }
10170
9904
  /**
10171
- * Eviction confirmed these paths are gone from disk. Drop them; forget the
10172
- * hour when it holds nothing.
9905
+ * Eviction confirmed these segments are gone from disk. Take them out of the
9906
+ * hour's account; forget the hour when nothing is left.
9907
+ *
9908
+ * The aggregate shrinks by exactly what was removed. `minStartMs` /
9909
+ * `maxStartMs` are NOT re-derived — they cannot be, without a listing — so a
9910
+ * partly-evicted hour keeps extremes that may be older or newer than what
9911
+ * survives. That error is bounded by the hour itself (both values always lie
9912
+ * inside `[hourStartMs, hourStartMs + 1h)`), and the row is UNSEALED so the
9913
+ * next listing of it re-derives both exactly.
10173
9914
  */
10174
9915
  async dropSegments(rows) {
10175
9916
  const doomed = /* @__PURE__ */ new Map();
@@ -10179,70 +9920,50 @@ var SegmentHourLedger = class SegmentHourLedger {
10179
9920
  if (entry === void 0) {
10180
9921
  doomed.set(key, {
10181
9922
  deviceId: row.deviceId,
10182
- paths: new Set([row.path])
10183
- });
10184
- continue;
10185
- }
10186
- entry.paths.add(row.path);
10187
- }
10188
- for (const [key, { deviceId, paths }] of doomed) {
10189
- if (!this.ledger.has(key)) continue;
10190
- const held = await this.resident(key, deviceId);
10191
- if (held === null) {
10192
- this.logger.warn("recorder: hour detail unavailable — these evicted paths stay in the durable hour row until a walk or the next eviction confirms them", {
10193
- tags: { deviceId },
10194
- meta: {
10195
- key,
10196
- paths: paths.size
10197
- }
9923
+ bytes: row.bytes,
9924
+ segments: 1
10198
9925
  });
10199
9926
  continue;
10200
9927
  }
10201
- const next = held.paths.filter((p) => !paths.has(p));
10202
- if (next.length === 0) {
9928
+ entry.bytes += row.bytes;
9929
+ entry.segments += 1;
9930
+ }
9931
+ for (const [key, dropped] of doomed) await this.serialized(key, async () => {
9932
+ const held = this.ledger.get(key);
9933
+ if (held === void 0) return;
9934
+ const segments = held.segments - dropped.segments;
9935
+ if (segments <= 0) {
10203
9936
  await this.ledger.forget(key);
10204
- continue;
9937
+ this.accounted.delete(key);
9938
+ return;
10205
9939
  }
10206
- if (next.length === held.paths.length) continue;
10207
- await this.ledger.put(segmentHourRow({
9940
+ await this.ledger.put({
10208
9941
  ...held,
10209
- paths: next
10210
- }));
10211
- }
9942
+ sealed: false,
9943
+ segments,
9944
+ bytes: Math.max(0, held.bytes - dropped.bytes)
9945
+ });
9946
+ });
10212
9947
  }
10213
9948
  /**
10214
- * Move `rows` from the hour rows they are listed in today to the SAME hours
9949
+ * Move `rows` from the hour they are accounted under today to the SAME hour
10215
9950
  * at `toLocationId` — the DURABLE half of a relocation.
10216
9951
  *
10217
- * Without this the mover re-points only `RecordingIndex`, which is RAM. Since
10218
- * D287 a closed hour is hydrated from the ledger FIRST and the disk is not
10219
- * consulted at all when the ledger covered the window, so a relocated hour
10220
- * comes back at its OLD location on the next boot — paths under a root that
10221
- * no longer holds the file. Playback does not stat (that syscall per segment
10222
- * was removed deliberately), so the result is a silent black window; and once
10223
- * the drained location is deleted, `collectRangeSegments` drops the rows
10224
- * outright and the footage is invisible. That is the shape of the 2 131
10225
- * `recordings:high` ghost segments already on this hub.
9952
+ * Without this the mover re-points only `RecordingIndex`, which is RAM, and
9953
+ * the drained location keeps the bytes on every storage page for the life of
9954
+ * the hub. That is the shape of the 2 131 `recordings:high` ghost rows this
9955
+ * hub carried.
10226
9956
  *
10227
9957
  * **The ordering is the invariant, not an implementation detail.** The
10228
9958
  * destination row is written BEFORE the source row is shrunk, and the caller
10229
9959
  * has not yet deleted the source bytes — so the only state a crash can leave
10230
- * is "listed at BOTH locations, both of which hold the file", which resolves
10231
- * correctly whichever row hydrate happens to apply last. The reverse order
10232
- * has a window in which the path is in NO row, and a ledger-known hour marks
10233
- * the index `hydrated`: nothing would ever walk that hour again, and the
10234
- * segment would sit on disk permanently unreachable.
10235
- *
10236
- * **Two path sets, not one.** A path can arrive at the target under a
10237
- * DIFFERENT name than it leaves the source under: a mover that found a file
10238
- * larger than its recorded size reconciles the row, and the byte count is
10239
- * part of the name. So the source row is shrunk by what is LEAVING it and the
10240
- * target row grows by what is ARRIVING — conflating them would leave the
10241
- * source row naming a file the caller is about to delete.
9960
+ * is "accounted at BOTH locations, both of which hold the files", which a
9961
+ * listing of either hour resolves. The reverse order has a window in which
9962
+ * the segments are accounted NOWHERE.
10242
9963
  *
10243
- * Returns `false` when NOTHING was repointed and the caller must delete
10244
- * nothing. An hour whose detail could not be read decides nothing (D49) —
10245
- * the same refusal {@link dropSegments} makes for eviction.
9964
+ * Both rows come out UNSEALED: each carries an aggregate that arithmetic
9965
+ * moved rather than a listing derived, and the next walk of either hour fixes
9966
+ * whatever the arithmetic rounded.
10246
9967
  */
10247
9968
  async repointSegments(rows, toLocationId) {
10248
9969
  const byHour = /* @__PURE__ */ new Map();
@@ -10250,87 +9971,75 @@ var SegmentHourLedger = class SegmentHourLedger {
10250
9971
  if (row.locationId === toLocationId) continue;
10251
9972
  const { key } = hourOf(row);
10252
9973
  const entry = byHour.get(key);
10253
- if (entry === void 0) byHour.set(key, {
10254
- row,
10255
- leaving: new Set([row.fromPath ?? row.path]),
10256
- arriving: new Set([row.path])
10257
- });
10258
- else {
10259
- entry.leaving.add(row.fromPath ?? row.path);
10260
- entry.arriving.add(row.path);
10261
- }
10262
- }
10263
- for (const [sourceKey, { row, leaving, arriving }] of byHour) {
10264
- const held = await this.resident(sourceKey, row.deviceId);
10265
- if (held === null) {
10266
- this.logger.warn("recorder: hour detail unavailable — this relocation is REFUSED and nothing is deleted; the segments stay where they are", {
10267
- tags: { deviceId: row.deviceId },
10268
- meta: {
10269
- key: sourceKey,
10270
- toLocationId,
10271
- paths: leaving.size
10272
- }
10273
- });
10274
- return false;
10275
- }
10276
- const targetKey = segmentHourKey(row.deviceId, row.profile, toLocationId, held.hourStartMs);
10277
- const target = this.ledger.has(targetKey) ? await this.resident(targetKey, row.deviceId) : void 0;
10278
- if (target === null) {
10279
- this.logger.warn("recorder: TARGET hour detail unavailable — this relocation is REFUSED; appending to a summary would rewrite the hour as these paths alone", {
10280
- tags: { deviceId: row.deviceId },
10281
- meta: {
10282
- key: targetKey,
10283
- fromLocationId: row.locationId,
10284
- paths: arriving.size
10285
- }
9974
+ if (entry === void 0) {
9975
+ byHour.set(key, {
9976
+ row,
9977
+ bytes: row.bytes,
9978
+ segments: 1,
9979
+ minStartMs: row.startMs,
9980
+ maxStartMs: row.startMs
10286
9981
  });
10287
- return false;
9982
+ continue;
10288
9983
  }
10289
- const merged = uniquePaths([...target?.paths ?? [], ...arriving]);
10290
- await this.ledger.put(segmentHourRow({
9984
+ entry.bytes += row.bytes;
9985
+ entry.segments += 1;
9986
+ entry.minStartMs = Math.min(entry.minStartMs, row.startMs);
9987
+ entry.maxStartMs = Math.max(entry.maxStartMs, row.startMs);
9988
+ }
9989
+ for (const [sourceKey, moved] of byHour) {
9990
+ const hour = hourStartMs(moved.row.startMs);
9991
+ const targetKey = segmentHourKey(moved.row.deviceId, moved.row.profile, toLocationId, hour);
9992
+ const target = this.ledger.get(targetKey);
9993
+ const targetIdentity = {
10291
9994
  key: targetKey,
10292
- deviceId: row.deviceId,
10293
- profile: row.profile,
9995
+ deviceId: moved.row.deviceId,
9996
+ profile: moved.row.profile,
10294
9997
  locationId: toLocationId,
10295
- hourStartMs: held.hourStartMs,
10296
- paths: merged
9998
+ hourStartMs: hour
9999
+ };
10000
+ const targetEmpty = target === void 0 || target.segments === 0;
10001
+ await this.ledger.put(hourRowOf(targetIdentity, {
10002
+ sealed: false,
10003
+ bytes: (target?.bytes ?? 0) + moved.bytes,
10004
+ segments: (target?.segments ?? 0) + moved.segments,
10005
+ minStartMs: targetEmpty ? moved.minStartMs : Math.min(target.minStartMs, moved.minStartMs),
10006
+ maxStartMs: targetEmpty ? moved.maxStartMs : Math.max(target.maxStartMs, moved.maxStartMs)
10297
10007
  }));
10298
- const next = held.paths.filter((p) => !leaving.has(p));
10299
- if (next.length === 0) await this.ledger.forget(sourceKey);
10300
- else await this.ledger.put(segmentHourRow({
10008
+ const held = this.ledger.get(sourceKey);
10009
+ if (held === void 0) continue;
10010
+ const segments = held.segments - moved.segments;
10011
+ if (segments <= 0) {
10012
+ await this.ledger.forget(sourceKey);
10013
+ continue;
10014
+ }
10015
+ await this.ledger.put({
10301
10016
  ...held,
10302
- paths: next
10303
- }));
10017
+ sealed: false,
10018
+ segments,
10019
+ bytes: Math.max(0, held.bytes - moved.bytes)
10020
+ });
10304
10021
  }
10305
- return true;
10306
10022
  }
10307
10023
  /**
10308
- * After a disk walk of `deviceIds`, make the ledger match the index — with
10309
- * two D148 exceptions:
10024
+ * After a disk walk of `deviceIds`, make the ledger match what the index now
10025
+ * holds — with two D148 exceptions:
10310
10026
  *
10311
10027
  * - an hour the walk did not see is dropped ONLY if it is in the past;
10312
10028
  * the current hour may still hold a live writer whose file the readdir
10313
10029
  * raced;
10314
- * - the current hour UNIONs ledger paths with index paths rather than
10315
- * replacing, for the same race.
10030
+ * - the current hour takes the LARGER of the two accounts rather than the
10031
+ * walk's, for the same race.
10316
10032
  *
10317
10033
  * Devices not in `deviceIds` are left untouched (a partial walk must not
10318
10034
  * prune cameras it did not look at).
10319
10035
  *
10320
10036
  * And a walk this ledger does not TRUST prunes nothing at all — see
10321
- * {@link refusedPrunes}. What such a walk FOUND is still real, so its paths
10322
- * are unioned in; what it did not find decides nothing (D49).
10323
- *
10324
- * Since D287 the walked devices' hours are MATERIALISED first: every
10325
- * comparison below reads `row.paths`, and a summary's empty `paths` would
10326
- * read as "the ledger holds nothing here" — which is the union collapsing to
10327
- * the walk's own view, i.e. the prune the refusal machinery exists to
10328
- * prevent. An hour whose detail could not be read is left additive-only.
10037
+ * {@link refusedPrunes}. What such a walk FOUND is still real, so its larger
10038
+ * account is taken; what it did not find decides nothing (D49).
10329
10039
  */
10330
10040
  async reconcileFromIndex(index, deviceIds, nowMs) {
10331
10041
  const walked = new Set(deviceIds);
10332
10042
  const currentHour = hourStartMs(nowMs);
10333
- await this.materializeDevices(walked);
10334
10043
  const fromIndex = /* @__PURE__ */ new Map();
10335
10044
  for (const deviceId of deviceIds) for (const seg of index.segments(deviceId)) {
10336
10045
  const { key, hourStartMs: hour } = hourOf(seg);
@@ -10349,7 +10058,6 @@ var SegmentHourLedger = class SegmentHourLedger {
10349
10058
  const refused = this.refusedPrunes(walked, fromIndex);
10350
10059
  for (const row of this.ledger.snapshot()) {
10351
10060
  if (!walked.has(row.deviceId)) continue;
10352
- if (!row.pathsResident) continue;
10353
10061
  const disk = fromIndex.get(row.key);
10354
10062
  const additiveOnly = row.hourStartMs === currentHour || refused.devices.has(row.deviceId) || refused.locations.has(locationKey(row.deviceId, row.locationId));
10355
10063
  if (disk === void 0) {
@@ -10358,14 +10066,10 @@ var SegmentHourLedger = class SegmentHourLedger {
10358
10066
  continue;
10359
10067
  }
10360
10068
  if (additiveOnly) {
10361
- const union = uniquePaths([...disk.paths, ...row.paths]);
10362
- if (!samePaths(union, row.paths)) await this.ledger.put(segmentHourRow({
10363
- ...row,
10364
- paths: union
10365
- }));
10069
+ if (disk.segments > row.segments) await this.ledger.put(disk);
10366
10070
  continue;
10367
10071
  }
10368
- if (!samePaths(row.paths, disk.paths)) await this.ledger.put(disk);
10072
+ if (disk.segments !== row.segments || disk.bytes !== row.bytes) await this.ledger.put(disk);
10369
10073
  }
10370
10074
  for (const [key, disk] of fromIndex) {
10371
10075
  if (this.ledger.has(key)) continue;
@@ -10484,44 +10188,45 @@ function locationKey(deviceId, locationId) {
10484
10188
  * skips it forever. Under D148 as written, a ghost row can never be forgotten
10485
10189
  * by any mechanism — which is why the live ones have stood for thirteen days.
10486
10190
  *
10487
- * The resolution is that a walk is not the only trusted confirmation; it is a
10488
- * BULK one. The paths in an hour row are relative to a root
10489
- * (`590/high/2026/08/15/04/…m4s`), so each one can be resolved against every
10490
- * currently-resolvable root and asked about directly. **Per-path
10491
- * `storage.exists`, over every candidate root, for EVERY path of the hour, is
10492
- * the same confirmation a walk gives — done one syscall at a time instead of
10493
- * one `readdir` at a time.** That, and nothing weaker, is what licenses the
10191
+ * The resolution is that the ghost row's root does not resolve but its hour
10192
+ * DIRECTORY is addressable anywhere: the path convention
10193
+ * (`590/high/2026/08/15/04/`) is a function of the row's own fields, so the same
10194
+ * hour can be listed under every currently-resolvable root. **A listing of the
10195
+ * hour directory at every candidate root is exactly the walk D148 sanctions,
10196
+ * done one root at a time.** That, and nothing weaker, is what licenses the
10494
10197
  * forget. Two consequences ride with it:
10495
10198
  *
10496
- * - **Sampling is not evidence.** An hour is forgotten only when every one of
10497
- * its paths came back absent from every root. One unprobed path and the hour
10498
- * stands.
10499
- * - **An error is not an absence** (D49). A probe that THROWS ends the hour
10500
- * with nothing decided — never "not found".
10199
+ * - **Every root must answer.** An hour is forgotten only when every candidate
10200
+ * root listed its directory and every one of them came back EMPTY. One root
10201
+ * that could not answer and the hour stands.
10202
+ * - **An error is not an absence** (D49). A listing that fails, or that reports
10203
+ * "I could not decide", ends the hour with nothing decided — never "not
10204
+ * found". Whether an absent directory means "empty" or "this disk is gone" is
10205
+ * the lister's question, and it answers it by reading the ROOT.
10206
+ *
10207
+ * Until D523 this probed each of the row's `paths` at each root, one
10208
+ * `storage.exists` at a time — up to ~1 000 syscalls to prove one hour absent.
10209
+ * The column is gone and the row names no file, so the unit is the directory,
10210
+ * which is both cheaper and the same evidence.
10501
10211
  *
10502
10212
  * ## Repoint versus forget
10503
10213
  *
10504
10214
  * The asymmetry is the whole safety argument, and it is deliberately cheap in
10505
10215
  * the safe direction:
10506
10216
  *
10507
- * - **one path found anywhere ⇒ the row is MIS-POINTED, not orphaned.** It is
10508
- * repointed to that root's location and the hour is finished — no further
10509
- * probing. A renamed or re-created location produces exactly this shape, and
10510
- * deleting here would destroy the index of footage that EXISTS, which is the
10511
- * failure D291 was written to prevent.
10512
- * - **every path absent everywhere ⇒ orphaned**, and only then forgotten.
10513
- *
10514
- * So proving presence costs one probe and proving absence costs all of them,
10515
- * which is the right way round: the expensive proof is the destructive one.
10217
+ * - **a non-empty listing at any root ⇒ the row is MIS-POINTED, not orphaned.**
10218
+ * It is repointed to that root's location, with the segments that listing
10219
+ * names, and the hour is finished. A renamed or re-created location produces
10220
+ * exactly this shape, and deleting here would destroy the index of footage
10221
+ * that EXISTS, which is the failure D291 was written to prevent.
10222
+ * - **every root lists an EMPTY hour ⇒ orphaned**, and only then forgotten.
10516
10223
  *
10517
10224
  * ## Pacing
10518
10225
  *
10519
- * The recordings array is saturated and slow (a `readdir` of one hour
10520
- * directory measured ~650 ms under load, and a `find` over it hung an SSH
10521
- * session on 2026-08-29). This is a janitor: bounded hours per pass, a hard
10522
- * probe ceiling, and a caller-supplied `pace()` awaited between probe batches.
10523
- * Work it does not finish is left for the next pass and REPORTED, never
10524
- * silently dropped.
10226
+ * The recordings array is the resource every live read competes for. This is a
10227
+ * janitor: bounded hours per pass, a hard probe ceiling, and a caller-supplied
10228
+ * `pace()` awaited between probe batches. Work it does not finish is left for
10229
+ * the next pass and REPORTED, never silently dropped.
10525
10230
  */
10526
10231
  var HOUR_MS$3 = 36e5;
10527
10232
  /**
@@ -10670,22 +10375,12 @@ var GhostLocationSweeper = class {
10670
10375
  refuse(row, "live-tail", `hourStartMs=${row.hourStartMs}`);
10671
10376
  continue;
10672
10377
  }
10673
- const paths = await this.deps.ledger.hourPaths(row.deviceId, row.profile, row.locationId, row.hourStartMs);
10674
- if (paths === null) {
10675
- refuse(row, "detail-unavailable", "point read failed");
10676
- continue;
10677
- }
10678
- if (paths.length === 0) {
10679
- refuse(row, "no-paths", "the row materialised empty");
10680
- continue;
10681
- }
10682
- const rows = this.rowsOf(row, paths);
10683
- if (rows === null) {
10684
- refuse(row, "unparseable-paths", "a path does not parse into this hour");
10378
+ if (row.segments === 0) {
10379
+ refuse(row, "no-paths", "the row accounts for no segments");
10685
10380
  continue;
10686
10381
  }
10687
10382
  const before = probes;
10688
- const verdict = await this.probeHour(rows, roots, maxProbes - probes);
10383
+ const verdict = await this.probeHour(row, roots, maxProbes - probes, probes);
10689
10384
  probes += verdict.probes;
10690
10385
  tallyOf(row.deviceId).probes += probes - before;
10691
10386
  if (verdict.outcome.kind === "refused") {
@@ -10694,10 +10389,12 @@ var GhostLocationSweeper = class {
10694
10389
  }
10695
10390
  if (verdict.outcome.kind === "found") {
10696
10391
  const target = locationForHydratedProfile(verdict.outcome.root.aliases, row.profile);
10697
- if (!await this.deps.ledger.repointSegments(rows, target.id)) {
10698
- refuse(row, "durable-refused", `repoint to ${target.id} refused`);
10392
+ const rows = this.rowsOf(row, verdict.outcome.paths);
10393
+ if (rows === null) {
10394
+ refuse(row, "unparseable-paths", "a listed name does not parse into this hour");
10699
10395
  continue;
10700
10396
  }
10397
+ await this.deps.ledger.repointSegments(rows, target.id);
10701
10398
  this.deps.applyToIndex(rows, target.id);
10702
10399
  repointedHours += 1;
10703
10400
  segmentsRepointed += rows.length;
@@ -10707,19 +10404,19 @@ var GhostLocationSweeper = class {
10707
10404
  tally.targets.add(target.id);
10708
10405
  continue;
10709
10406
  }
10710
- await this.deps.ledger.dropSegments(rows);
10407
+ const forgotten = await this.deps.ledger.forgetHour(row.deviceId, row.profile, row.locationId, row.hourStartMs);
10711
10408
  if (this.deps.ledger.knowsHour(row.deviceId, row.profile, row.locationId, row.hourStartMs)) {
10712
- refuse(row, "durable-refused", "the ledger still holds the hour after the drop");
10409
+ refuse(row, "durable-refused", "the ledger still holds the hour after the forget");
10713
10410
  continue;
10714
10411
  }
10715
- this.deps.applyToIndex(rows, null);
10412
+ this.deps.forgetInIndex(row.deviceId, row.locationId, row.hourStartMs);
10716
10413
  forgottenHours += 1;
10717
- segmentsForgotten += rows.length;
10718
- bytesForgotten += rows.reduce((sum, r) => sum + r.bytes, 0);
10414
+ segmentsForgotten += forgotten;
10415
+ bytesForgotten += row.bytes;
10719
10416
  const tally = tallyOf(row.deviceId);
10720
10417
  tally.forgottenHours += 1;
10721
- tally.forgottenSegments += rows.length;
10722
- tally.forgottenBytes += rows.reduce((sum, r) => sum + r.bytes, 0);
10418
+ tally.forgottenSegments += forgotten;
10419
+ tally.forgottenBytes += row.bytes;
10723
10420
  }
10724
10421
  for (const [deviceId, tally] of byDevice) this.deps.logger.info("recorder: ghost-location reconcile — camera summary", {
10725
10422
  tags: { deviceId },
@@ -10801,60 +10498,82 @@ var GhostLocationSweeper = class {
10801
10498
  return out;
10802
10499
  }
10803
10500
  /**
10804
- * Ask every root about the hour's paths until one answers `true`.
10501
+ * List this hour's directory under EVERY candidate root.
10502
+ *
10503
+ * A non-empty listing at one root makes the hour mis-pointed. Two roots
10504
+ * holding it is a refusal, not a choice: repointing would be a guess about
10505
+ * which copy is the footage. Absence is only concluded when every root
10506
+ * answered and every answer was empty — a listing that could not decide
10507
+ * (`null`) or threw ends the hour with nothing decided (D49).
10508
+ *
10509
+ * The whole loop runs even after a hit, because "found at exactly one root"
10510
+ * is a claim about every root and cannot be short-circuited without giving up
10511
+ * the multi-root refusal.
10805
10512
  *
10806
- * Presence short-circuits the hour (one confirmed file makes it mis-pointed);
10807
- * absence does not, and cannot — the hour is orphaned only when EVERY path
10808
- * came back absent from EVERY root. A throw ends the hour with nothing
10809
- * decided.
10513
+ * `spent` is the listings the PASS has already made, not this hour's, and the
10514
+ * brake fires on that running total. Since D523 an hour costs one listing per
10515
+ * physical root — two or three on this hub — so a counter that reset per hour
10516
+ * would never reach {@link GHOST_PROBE_BATCH} and the `pace()` dep would be
10517
+ * dead on every real topology.
10810
10518
  */
10811
- async probeHour(rows, roots, budget) {
10519
+ async probeHour(row, roots, budget, spent) {
10812
10520
  let used = 0;
10813
- for (const row of rows) {
10814
- const found = [];
10815
- for (const root of roots) {
10816
- if (used >= budget) return {
10521
+ const found = [];
10522
+ for (const root of roots) {
10523
+ if (used >= budget) return {
10524
+ outcome: {
10525
+ kind: "refused",
10526
+ reason: "probe-budget",
10527
+ detail: `stopped after ${used} listings with ${roots.length} roots to probe`
10528
+ },
10529
+ probes: used
10530
+ };
10531
+ const alias = locationForHydratedProfile(root.aliases, row.profile);
10532
+ used += 1;
10533
+ if ((spent + used) % 16 === 0) await this.deps.pace();
10534
+ let listing;
10535
+ try {
10536
+ listing = await this.deps.listHour(alias.id, row.deviceId, row.profile, row.hourStartMs);
10537
+ } catch (err) {
10538
+ return {
10817
10539
  outcome: {
10818
10540
  kind: "refused",
10819
- reason: "probe-budget",
10820
- detail: `stopped after ${used} probes with ${rows.length} paths in the hour`
10541
+ reason: "probe-error",
10542
+ detail: `${alias.id}: ${require_dist.errMsg(err)}`
10821
10543
  },
10822
10544
  probes: used
10823
10545
  };
10824
- const alias = locationForHydratedProfile(root.aliases, row.profile);
10825
- used += 1;
10826
- if (used % 16 === 0) await this.deps.pace();
10827
- try {
10828
- if (await this.deps.exists(alias.id, row.path)) found.push(root);
10829
- } catch (err) {
10830
- return {
10831
- outcome: {
10832
- kind: "refused",
10833
- reason: "probe-error",
10834
- detail: `${alias.id}: ${require_dist.errMsg(err)}`
10835
- },
10836
- probes: used
10837
- };
10838
- }
10839
10546
  }
10840
- if (found.length > 1) return {
10547
+ if (listing === null) return {
10841
10548
  outcome: {
10842
10549
  kind: "refused",
10843
- reason: "found-at-multiple-roots",
10844
- detail: found.map((root) => root.key).join(", ")
10845
- },
10846
- probes: used
10847
- };
10848
- const only = found[0];
10849
- if (only !== void 0) return {
10850
- outcome: {
10851
- kind: "found",
10852
- root: only,
10853
- path: row.path
10550
+ reason: "detail-unavailable",
10551
+ detail: `${alias.id} could not say what its copy of this hour holds`
10854
10552
  },
10855
10553
  probes: used
10856
10554
  };
10555
+ if (listing.length > 0) found.push({
10556
+ root,
10557
+ paths: listing
10558
+ });
10857
10559
  }
10560
+ if (found.length > 1) return {
10561
+ outcome: {
10562
+ kind: "refused",
10563
+ reason: "found-at-multiple-roots",
10564
+ detail: found.map((hit) => hit.root.key).join(", ")
10565
+ },
10566
+ probes: used
10567
+ };
10568
+ const only = found[0];
10569
+ if (only !== void 0) return {
10570
+ outcome: {
10571
+ kind: "found",
10572
+ root: only.root,
10573
+ paths: only.paths
10574
+ },
10575
+ probes: used
10576
+ };
10858
10577
  return {
10859
10578
  outcome: { kind: "absent" },
10860
10579
  probes: used
@@ -11235,34 +10954,12 @@ function emptyTally() {
11235
10954
  orphanFiles: 0
11236
10955
  };
11237
10956
  }
11238
- /** The basename of a segment path — what a listing returns. */
11239
- function fileName(relPath) {
11240
- const cut = relPath.lastIndexOf("/");
11241
- return cut === -1 ? relPath : relPath.slice(cut + 1);
11242
- }
11243
- /** Group one archive page into whole hours. A page always carries complete hour
11244
- * rows (`streamSegments` expands them whole), so no hour is ever split. */
11245
- function claimsOf(rows) {
11246
- const byHour = /* @__PURE__ */ new Map();
11247
- for (const row of rows) {
11248
- const hour = hourStartMs(row.startMs);
11249
- const key = `${row.deviceId}|${row.profile}|${hour}`;
11250
- const held = byHour.get(key);
11251
- if (held) held.push(row);
11252
- else byHour.set(key, [row]);
11253
- }
11254
- const out = [];
11255
- for (const group of byHour.values()) {
11256
- const first = group[0];
11257
- if (first === void 0) continue;
11258
- out.push({
11259
- deviceId: first.deviceId,
11260
- profile: first.profile,
11261
- hourStartMs: hourStartMs(first.startMs),
11262
- rows: group
11263
- });
11264
- }
11265
- return out.toSorted((a, b) => a.hourStartMs - b.hourStartMs || a.deviceId - b.deviceId);
10957
+ /** Does this hour row fall inside the scope the operator asked for? */
10958
+ function inScope(row, scope) {
10959
+ if (scope.locationId !== void 0 && row.locationId !== scope.locationId) return false;
10960
+ if (scope.deviceId !== void 0 && row.deviceId !== scope.deviceId) return false;
10961
+ const profiles = scope.profiles ?? [];
10962
+ return profiles.length === 0 || profiles.includes(row.profile);
11266
10963
  }
11267
10964
  /**
11268
10965
  * One reconcile pass over one location's claimed hours.
@@ -11360,20 +11057,35 @@ var LedgerWalkReconciler = class {
11360
11057
  this.reportPass(report);
11361
11058
  return report;
11362
11059
  }
11363
- /** Walk the archive page by page, one hour at a time. */
11060
+ /**
11061
+ * Walk the hour ROWS of the scope, oldest first.
11062
+ *
11063
+ * From the MIRROR, not a query. Since D523 a row is ten scalars, so the whole
11064
+ * claim set is already in RAM and costs nothing to fold — and the pass mutates
11065
+ * what it walks, which is exactly the shape that makes `offset` paging skip
11066
+ * half a scope. A snapshot taken once cannot skip.
11067
+ */
11364
11068
  async walk(state, scope) {
11365
- for await (const page of state.ledger.streamSegments(scope)) for (const claim of claimsOf(page)) {
11069
+ const claims = state.ledger.snapshot().filter((row) => inScope(row, scope)).toSorted((a, b) => a.hourStartMs - b.hourStartMs || a.deviceId - b.deviceId);
11070
+ for (const claim of claims) {
11366
11071
  if (state.hoursClaimed >= state.maxHours) {
11367
11072
  state.bounded = true;
11368
11073
  return;
11369
11074
  }
11370
11075
  state.hoursClaimed += 1;
11371
- state.segmentsClaimed += claim.rows.length;
11076
+ state.segmentsClaimed += claim.segments;
11372
11077
  if (!await this.reconcileHour(state, claim)) return;
11373
11078
  }
11374
11079
  }
11375
11080
  /**
11376
- * Diff ONE hour, and forget what it proved absent.
11081
+ * Reconcile ONE hour: what its row ACCOUNTS FOR against what its directory
11082
+ * HOLDS, and repair the account from the directory.
11083
+ *
11084
+ * Since D523 the row names no files, so this is a comparison of counts and
11085
+ * bytes — and it does not need to name them: the repair is "make the account
11086
+ * the listing's", which is exact, and the RESIDENT index is reset to the same
11087
+ * listing so a row it holds for a file the directory does not is dropped with
11088
+ * it.
11377
11089
  *
11378
11090
  * `false` ends the whole pass — the only thing that returns it is an anchor
11379
11091
  * that stopped agreeing, and every listing taken after that point is about a
@@ -11386,10 +11098,6 @@ var LedgerWalkReconciler = class {
11386
11098
  return true;
11387
11099
  }
11388
11100
  const relDir = hourDirRelPath(claim.deviceId, claim.profile, claim.hourStartMs);
11389
- for (const row of claim.rows) if (parseSegmentPath(row.path) === null || row.path !== `${relDir}/${fileName(row.path)}`) {
11390
- this.skip(state, claim, "path-mismatch", row.path);
11391
- return true;
11392
- }
11393
11101
  let listing;
11394
11102
  try {
11395
11103
  listing = await this.deps.listHour(state.location.root, relDir);
@@ -11401,36 +11109,55 @@ var LedgerWalkReconciler = class {
11401
11109
  if (state.listings % 8 === 0) await this.deps.pace();
11402
11110
  state.hoursWalked += 1;
11403
11111
  tally.hoursWalked += 1;
11404
- const diff = this.diff(claim, listing);
11405
- if (diff.missingDirectory) {
11112
+ const missingDirectory = listing === null;
11113
+ if (missingDirectory) {
11406
11114
  state.hoursMissing += 1;
11407
11115
  tally.hoursMissing += 1;
11408
11116
  }
11409
- if (diff.orphans.length > 0) {
11410
- state.orphanFiles += diff.orphans.length;
11411
- tally.orphanFiles += diff.orphans.length;
11412
- for (const orphan of diff.orphans) {
11117
+ const present = [];
11118
+ for (const name of listing ?? []) {
11119
+ if (!name.endsWith(".m4s")) continue;
11120
+ const relPath = `${relDir}/${name}`;
11121
+ const parsed = parseSegmentPath(relPath);
11122
+ if (parsed === null || hourStartMs(parsed.startMs) !== claim.hourStartMs) {
11123
+ this.skip(state, claim, "path-mismatch", relPath);
11124
+ return true;
11125
+ }
11126
+ present.push(relPath);
11127
+ }
11128
+ const listed = hourAggregate(present);
11129
+ const orphans = Math.max(0, listed.segments - claim.segments);
11130
+ if (orphans > 0) {
11131
+ state.orphanFiles += orphans;
11132
+ tally.orphanFiles += orphans;
11133
+ for (const path of present) {
11413
11134
  if (state.orphanSample.length >= 50) break;
11414
- state.orphanSample.push(`${relDir}/${orphan}`);
11135
+ state.orphanSample.push(path);
11415
11136
  }
11416
- this.deps.logger.warn("recorder: files under a claimed hour that the durable ledger does NOT name — reported, never deleted", {
11137
+ this.deps.logger.warn("recorder: an hour directory holds more segments than its durable row accounts for", {
11417
11138
  tags: { deviceId: claim.deviceId },
11418
11139
  meta: {
11419
11140
  locationId: state.location.id,
11420
11141
  profile: claim.profile,
11421
11142
  hourStartMs: claim.hourStartMs,
11422
- orphans: diff.orphans.length,
11423
- sample: diff.orphans.slice(0, 5)
11143
+ accounted: claim.segments,
11144
+ listed: listed.segments,
11145
+ apply: state.apply
11424
11146
  }
11425
11147
  });
11426
11148
  }
11427
- if (diff.ghosts.length === 0) return true;
11428
- const ghostBytes = diff.ghosts.reduce((sum, row) => sum + row.bytes, 0);
11429
- state.ghostSegments += diff.ghosts.length;
11149
+ const ghosts = Math.max(0, claim.segments - listed.segments);
11150
+ if (ghosts === 0) {
11151
+ if (state.apply && listed.segments > 0 && listed.bytes !== claim.bytes) await state.ledger.adoptWalkedHour(claim.deviceId, claim.profile, state.location.id, claim.hourStartMs, present);
11152
+ if (state.apply) this.resetIndex(state, claim, present);
11153
+ return true;
11154
+ }
11155
+ const ghostBytes = Math.max(0, claim.bytes - listed.bytes);
11156
+ state.ghostSegments += ghosts;
11430
11157
  state.ghostBytes += ghostBytes;
11431
- tally.ghostSegments += diff.ghosts.length;
11158
+ tally.ghostSegments += ghosts;
11432
11159
  tally.ghostBytes += ghostBytes;
11433
- if (diff.ghosts.length === claim.rows.length) state.ghostHoursWhole += 1;
11160
+ if (listed.segments === 0) state.ghostHoursWhole += 1;
11434
11161
  const anchor = await this.readAnchor(state.location);
11435
11162
  if (!anchor.ok || anchor.volumeId !== state.anchor) {
11436
11163
  state.refused = anchor.ok ? "anchor-moved" : anchor.refusal;
@@ -11446,56 +11173,52 @@ var LedgerWalkReconciler = class {
11446
11173
  });
11447
11174
  return false;
11448
11175
  }
11449
- this.deps.logger.warn("recorder: durable hour row claims segments the directory does not hold", {
11176
+ this.deps.logger.warn("recorder: durable hour row accounts for segments the directory does not hold", {
11450
11177
  tags: { deviceId: claim.deviceId },
11451
11178
  meta: {
11452
11179
  locationId: state.location.id,
11453
11180
  profile: claim.profile,
11454
11181
  hourStartMs: claim.hourStartMs,
11455
- claimed: claim.rows.length,
11456
- ghosts: diff.ghosts.length,
11182
+ accounted: claim.segments,
11183
+ listed: listed.segments,
11184
+ ghosts,
11457
11185
  ghostBytes,
11458
- wholeHour: diff.ghosts.length === claim.rows.length,
11459
- missingDirectory: diff.missingDirectory,
11186
+ wholeHour: listed.segments === 0,
11187
+ missingDirectory,
11460
11188
  apply: state.apply
11461
11189
  }
11462
11190
  });
11463
11191
  if (!state.apply) return true;
11464
- await state.ledger.dropSegments(diff.ghosts);
11465
- if (!await this.forgetLanded(state, claim, diff.ghosts)) {
11466
- this.skip(state, claim, "durable-refused", "the ledger still lists a dropped path");
11192
+ const forgotten = listed.segments === 0 ? await state.ledger.forgetHour(claim.deviceId, claim.profile, state.location.id, claim.hourStartMs) : await this.adoptLanded(state, claim, present, ghosts);
11193
+ if (forgotten === null) {
11194
+ this.skip(state, claim, "durable-refused", "the ledger still accounts for the ghosts");
11467
11195
  return true;
11468
11196
  }
11469
- this.deps.applyToIndex(diff.ghosts.map((row) => row.path));
11470
- state.forgottenSegments += diff.ghosts.length;
11197
+ this.resetIndex(state, claim, present);
11198
+ state.forgottenSegments += forgotten;
11471
11199
  state.forgottenBytes += ghostBytes;
11472
- tally.forgottenSegments += diff.ghosts.length;
11200
+ tally.forgottenSegments += forgotten;
11473
11201
  return true;
11474
11202
  }
11475
- /** Did the durable drop actually land? */
11476
- async forgetLanded(state, claim, ghosts) {
11477
- const { deviceId, profile, hourStartMs: hour } = claim;
11478
- if (!state.ledger.knowsHour(deviceId, profile, state.location.id, hour)) return true;
11479
- const held = await state.ledger.hourPaths(deviceId, profile, state.location.id, hour);
11480
- if (held === null) return false;
11481
- const remaining = new Set(held);
11482
- return ghosts.every((row) => !remaining.has(row.path));
11483
- }
11484
11203
  /**
11485
- * What the hour row claims, against what the directory holds — BOTH ways.
11204
+ * Adopt the listing, then READ THE ROW BACK.
11486
11205
  *
11487
- * A missing directory is an empty listing here, which makes every claim a
11488
- * ghost. That is only admissible because the caller re-reads the volume
11489
- * anchor before acting on it.
11206
+ * `adoptWalkedHour` refuses a listing that would erase more than half of what
11207
+ * the row accounts for — the guard against a root that answered short — and it
11208
+ * refuses silently, which a caller that counted it as done would turn into
11209
+ * telling the resident index a row is gone that the archive still holds.
11210
+ * `null` is that refusal.
11490
11211
  */
11491
- diff(claim, listing) {
11492
- const present = new Set((listing ?? []).filter((name) => name.endsWith(".m4s")));
11493
- const claimed = new Set(claim.rows.map((row) => fileName(row.path)));
11494
- return {
11495
- ghosts: claim.rows.filter((row) => !present.has(fileName(row.path))),
11496
- orphans: [...present].filter((name) => !claimed.has(name)).toSorted(),
11497
- missingDirectory: listing === null
11498
- };
11212
+ async adoptLanded(state, claim, present, ghosts) {
11213
+ await state.ledger.adoptWalkedHour(claim.deviceId, claim.profile, state.location.id, claim.hourStartMs, present);
11214
+ const after = state.ledger.hourRow(claim.deviceId, claim.profile, state.location.id, claim.hourStartMs);
11215
+ if (after === void 0) return claim.segments;
11216
+ return after.segments === claim.segments - ghosts ? ghosts : null;
11217
+ }
11218
+ /** Make the resident index's view of the hour the listing's, and say how many
11219
+ * index rows that dropped. */
11220
+ resetIndex(state, claim, present) {
11221
+ return this.deps.applyToIndex(claim.deviceId, state.location.id, claim.hourStartMs, present);
11499
11222
  }
11500
11223
  /** The volume anchor, or the refusal its absence produces. */
11501
11224
  async readAnchor(location) {
@@ -17784,12 +17507,17 @@ var GHOST_LOCATION_PROBE_PAUSE_MS = 50;
17784
17507
  /**
17785
17508
  * Awaited every `WALK_HOUR_BATCH` hour listings by the ledger↔disk walk (D319).
17786
17509
  *
17787
- * Twice the ghost sweep's brake, because the syscall between brakes is far
17788
- * heavier: that pass paces `storage.exists` on one path, this one paces a
17789
- * `readdir` of a whole hour directory — measured at ~650 ms on the live share
17790
- * under load. The walk is operator-armed and one-off, so the right trade is to
17791
- * be slower and invisible rather than fast and felt: a `find` over this array
17792
- * hung an SSH session on 2026-08-29.
17510
+ * Twice the ghost sweep's brake. That used to be because the syscall between
17511
+ * brakes was far heavier — the sweep paced one `storage.exists`, this one an
17512
+ * hour `readdir` "measured at ~650 ms on the live share under load". Both
17513
+ * halves of that have changed: the sweep paces a directory listing too since
17514
+ * D523, and the 650 ms belonged to a FUSE mount that has been retired (an hour
17515
+ * of 345–349 files on `/recordings-2` lists in ~1 ms).
17516
+ *
17517
+ * It stays at twice anyway, for the reason that never depended on the number:
17518
+ * the walk is operator-armed and one-off, so the right trade is to be slower
17519
+ * and invisible rather than fast and felt — a `find` over this array hung an
17520
+ * SSH session on 2026-08-29.
17793
17521
  */
17794
17522
  var LEDGER_WALK_PAUSE_MS = 100;
17795
17523
  /**
@@ -18086,6 +17814,7 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
18086
17814
  this.segmentHours = new SegmentHourLedger({
18087
17815
  store: this.ctx.api.settingsStore,
18088
17816
  logger: this.ctx.logger,
17817
+ listHour: (locationId, deviceId, profile, hourStartMs) => this.listHourDirectory(locationId, deviceId, profile, hourStartMs),
18089
17818
  isLocationUsable: (locationId) => this.placement?.isLocationUsable(locationId) ?? true
18090
17819
  });
18091
17820
  try {
@@ -18119,7 +17848,7 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
18119
17848
  await node_fs.promises.unlink(fromAbs);
18120
17849
  }
18121
17850
  },
18122
- archiveHourPaths: async (deviceId, profile, locationId, hourStartMs) => await this.segmentHours?.hourPaths(deviceId, profile, locationId, hourStartMs) ?? null,
17851
+ hourDirectoryPaths: (deviceId, profile, locationId, hourStartMs) => this.listHourDirectory(locationId, deviceId, profile, hourStartMs),
18123
17852
  removeDir: async (locationId, relDir) => {
18124
17853
  const loc = requireReadableLocation(this.resolvedLocations, locationId, "recorder");
18125
17854
  await node_fs.promises.rm(node_path.default.join(loc.root, relDir), {
@@ -18604,8 +18333,8 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
18604
18333
  localNodeId: this.nodeId,
18605
18334
  index: this.index,
18606
18335
  ledger: this.segmentHours === null ? null : {
18607
- streamSegments: (scope) => this.segmentHours.streamSegments(scope),
18608
- dropSegments: (rows) => this.segmentHours.dropSegments(rows)
18336
+ forgetScope: (scope) => this.segmentHours.forgetScope(scope),
18337
+ snapshot: () => this.segmentHours.snapshot()
18609
18338
  },
18610
18339
  now: () => Date.now()
18611
18340
  });
@@ -18712,9 +18441,7 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
18712
18441
  }
18713
18442
  },
18714
18443
  anchorOf: async (root) => (await readLocationMarker(root))?.volumeId ?? null,
18715
- applyToIndex: (paths) => {
18716
- this.index.removeSegments(paths);
18717
- },
18444
+ applyToIndex: (deviceId, locationId, hour, paths) => this.index.resetHour(deviceId, locationId, hour, paths),
18718
18445
  logger: this.ctx.logger,
18719
18446
  now: () => Date.now(),
18720
18447
  pace: () => require_dist.sleep(LEDGER_WALK_PAUSE_MS)
@@ -18842,7 +18569,6 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
18842
18569
  });
18843
18570
  try {
18844
18571
  await this.segmentHours?.load();
18845
- this.segmentHours?.repairAggregates();
18846
18572
  } catch (err) {
18847
18573
  this.ctx.logger.warn("recorder: segment-hour ledger load failed — hours stay unknown until a read walks them", { meta: { error: require_dist.errMsg(err) } });
18848
18574
  }
@@ -18885,18 +18611,15 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
18885
18611
  const sweeper = new GhostLocationSweeper({
18886
18612
  ledger,
18887
18613
  locations: () => this.resolvedLocations,
18888
- exists: (location, relativePath) => this.ctx.api.storage.exists.query({
18889
- location,
18890
- relativePath
18891
- }),
18614
+ listHour: (locationId, deviceId, profile, hourStartMs) => this.listHourDirectory(locationId, deviceId, profile, hourStartMs),
18892
18615
  applyToIndex: (rows, toLocationId) => {
18893
18616
  this.index.removeSegments(rows.map((row) => row.path));
18894
- if (toLocationId === null) return;
18895
18617
  for (const row of rows) this.index.addSegment({
18896
18618
  ...row,
18897
18619
  locationId: toLocationId
18898
18620
  });
18899
18621
  },
18622
+ forgetInIndex: (deviceId, locationId, hourStartMs) => this.index.resetHour(deviceId, locationId, hourStartMs, []),
18900
18623
  logger: this.ctx.logger,
18901
18624
  now: () => Date.now(),
18902
18625
  pace: () => require_dist.sleep(GHOST_LOCATION_PROBE_PAUSE_MS)
@@ -19171,6 +18894,63 @@ var RecorderV2Addon = class extends require_dist.BaseAddon {
19171
18894
  return this.resolvedLocations;
19172
18895
  }
19173
18896
  /**
18897
+ * ONE hour bucket's segment files, location-relative — the recorder's single
18898
+ * implementation of {@link HourLister} (D523).
18899
+ *
18900
+ * The directory is the authority for WHICH segments exist, and the segment
18901
+ * NAME carries start, duration and bytes, so this is a complete description
18902
+ * of the hour and no file is ever opened.
18903
+ *
18904
+ * Three answers, and the difference between the last two is the whole point:
18905
+ *
18906
+ * - `null` — **undecided.** The location does not resolve, may not be read,
18907
+ * or the read failed for a reason that is not "this directory is not
18908
+ * there". Every caller turns it into "this hour takes no part in this
18909
+ * pass".
18910
+ * - `[]` — **the hour holds nothing**, and the volume confirmed it: an
18911
+ * ENOENT on the hour directory is only reported as empty once the
18912
+ * location's ROOT has answered a read. A root that has gone away answers
18913
+ * ENOENT for every hour on it, and reporting that as "empty" is how a pass
18914
+ * deletes an operator's index of real footage (D148, D393).
18915
+ * - the paths.
18916
+ */
18917
+ async listHourDirectory(locationId, deviceId, profile, hourStartMs) {
18918
+ const location = this.resolvedLocations.find((l) => l.id === locationId);
18919
+ if (location === void 0 || !locationMayBeRead(location)) return null;
18920
+ const rel = hourDirRelPath(deviceId, profile, hourStartMs);
18921
+ try {
18922
+ return (await node_fs.promises.readdir(node_path.default.join(location.root, rel))).filter((f) => f.endsWith(".m4s")).map((f) => `${rel}/${f}`);
18923
+ } catch (err) {
18924
+ if (err.code !== "ENOENT") {
18925
+ this.ctx.logger.warn("recorder: hour directory read failed — this hour decides nothing", {
18926
+ tags: { deviceId },
18927
+ meta: {
18928
+ profile,
18929
+ locationId,
18930
+ hourStartMs,
18931
+ error: require_dist.errMsg(err)
18932
+ }
18933
+ });
18934
+ return null;
18935
+ }
18936
+ try {
18937
+ await node_fs.promises.readdir(location.root);
18938
+ } catch {
18939
+ this.ctx.logger.warn("recorder: hour directory is absent AND its location root cannot be read — this hour decides nothing", {
18940
+ tags: { deviceId },
18941
+ meta: {
18942
+ profile,
18943
+ locationId,
18944
+ root: location.root,
18945
+ hourStartMs
18946
+ }
18947
+ });
18948
+ return null;
18949
+ }
18950
+ return [];
18951
+ }
18952
+ }
18953
+ /**
19174
18954
  * Attach-boundary placement for one (device, profile). Falls back to the pure
19175
18955
  * resolver when the service is not built (non-recording node / early boot),
19176
18956
  * which is byte-identical to the pre-placement behaviour.