@camstack/addon-post-analysis 1.1.30 → 1.1.31

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.
@@ -1,4 +1,4 @@
1
- import { S as EventCategory, _ as hydrateSchema, b as object, c as nodePin, d as videoclipsCapability, f as zoneAnalyticsCapability, g as createEvent, h as DeviceType, i as cosineSimilarity, l as pipelineAnalyticsCapability, m as BaseAddon, n as addonWidgetsSourceCapability, o as faceGalleryCapability, p as errMsg, r as audioMetricsCapability, t as EVENT_PAD_MS, u as plateGalleryCapability, v as boolean, x as string, y as number } from "../dist-yPsKFcJL.mjs";
1
+ import { C as EventCategory, S as string, _ as createEvent, a as cosineSimilarity, b as number, d as plateGalleryCapability, f as videoclipsCapability, g as DeviceType, h as BaseAddon, i as audioMetricsCapability, l as nodePin, m as errMsg, n as EVENT_PAD_MS, p as zoneAnalyticsCapability, r as addonWidgetsSourceCapability, s as faceGalleryCapability, t as EVENT_KIND_BY_CAP, u as pipelineAnalyticsCapability, v as hydrateSchema, x as object, y as boolean } from "../dist-D1cY_vlY.mjs";
2
2
  import { randomUUID } from "node:crypto";
3
3
  import sharp from "sharp";
4
4
  //#region src/pipeline-analytics/videoclips-provider.ts
@@ -160,13 +160,13 @@ var CLASS_RANK_VEHICLE = .8;
160
160
  var CLASS_RANK_ANIMAL = .5;
161
161
  var CLASS_RANK_DEFAULT = .25;
162
162
  var PERSON_CLASSES = new Set(["person", "face"]);
163
- var VEHICLE_CLASSES = new Set([
163
+ var VEHICLE_CLASSES$1 = new Set([
164
164
  "vehicle",
165
165
  "car",
166
166
  "truck",
167
167
  "bus"
168
168
  ]);
169
- var ANIMAL_CLASSES = new Set([
169
+ var ANIMAL_CLASSES$1 = new Set([
170
170
  "animal",
171
171
  "dog",
172
172
  "cat"
@@ -182,8 +182,8 @@ function clamp01(x) {
182
182
  function classRank(className) {
183
183
  const c = className.toLowerCase();
184
184
  if (PERSON_CLASSES.has(c)) return 1;
185
- if (VEHICLE_CLASSES.has(c)) return CLASS_RANK_VEHICLE;
186
- if (ANIMAL_CLASSES.has(c)) return CLASS_RANK_ANIMAL;
185
+ if (VEHICLE_CLASSES$1.has(c)) return CLASS_RANK_VEHICLE;
186
+ if (ANIMAL_CLASSES$1.has(c)) return CLASS_RANK_ANIMAL;
187
187
  return CLASS_RANK_DEFAULT;
188
188
  }
189
189
  /**
@@ -1081,7 +1081,7 @@ function resolveDetectionLabel(input) {
1081
1081
  //#endregion
1082
1082
  //#region src/pipeline-analytics/pipeline/zones/geometry.ts
1083
1083
  /** Ray-casting point-in-polygon test */
1084
- function pointInPolygon(point, polygon) {
1084
+ function pointInPolygon$1(point, polygon) {
1085
1085
  if (polygon.length < 3) return false;
1086
1086
  let inside = false;
1087
1087
  for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
@@ -1594,7 +1594,7 @@ function bboxPolygonOverlap(bbox, polygon) {
1594
1594
  const gridSize = 8;
1595
1595
  let inside = 0;
1596
1596
  const total = gridSize * gridSize;
1597
- for (let row = 0; row < gridSize; row++) for (let col = 0; col < gridSize; col++) if (pointInPolygon({
1597
+ for (let row = 0; row < gridSize; row++) for (let col = 0; col < gridSize; col++) if (pointInPolygon$1({
1598
1598
  x: bbox.x + (col + .5) * (bbox.w / gridSize),
1599
1599
  y: bbox.y + (row + .5) * (bbox.h / gridSize)
1600
1600
  }, polygon)) inside++;
@@ -1613,7 +1613,7 @@ function maskPolygonOverlap(mask, maskWidth, maskHeight, bbox, polygon, _frameWi
1613
1613
  for (let my = 0; my < maskHeight; my++) for (let mx = 0; mx < maskWidth; mx++) {
1614
1614
  if (mask[my * maskWidth + mx] === 0) continue;
1615
1615
  totalMaskPixels++;
1616
- if (pointInPolygon({
1616
+ if (pointInPolygon$1({
1617
1617
  x: bbox.x + mx / maskWidth * bbox.w,
1618
1618
  y: bbox.y + my / maskHeight * bbox.h
1619
1619
  }, polygon)) insidePolygon++;
@@ -2738,7 +2738,7 @@ function computeStationaryEntryZones(entry, zones) {
2738
2738
  const matched = [];
2739
2739
  for (const zone of zones) {
2740
2740
  if (zone.polygon.length < 3) continue;
2741
- if (pointInPolygon(point, zone.polygon)) matched.push(zone.id);
2741
+ if (pointInPolygon$1(point, zone.polygon)) matched.push(zone.id);
2742
2742
  }
2743
2743
  return matched;
2744
2744
  }
@@ -2955,11 +2955,196 @@ var BindingCache = class {
2955
2955
  }
2956
2956
  };
2957
2957
  //#endregion
2958
+ //#region src/pipeline-analytics/store/recent-cursor.ts
2959
+ function encodeRecentCursor(cursor) {
2960
+ return Buffer.from(JSON.stringify({
2961
+ l: cursor.lastSeen,
2962
+ i: cursor.trackId
2963
+ }), "utf8").toString("base64url");
2964
+ }
2965
+ /**
2966
+ * Decode + validate an opaque cursor. Fails fast with a clear error on any
2967
+ * malformed input (bad base64, bad JSON, wrong field types) — a garbage
2968
+ * cursor must never silently degrade into a full-history first page.
2969
+ */
2970
+ function decodeRecentCursor(raw) {
2971
+ let parsed;
2972
+ try {
2973
+ parsed = JSON.parse(Buffer.from(raw, "base64url").toString("utf8"));
2974
+ } catch {
2975
+ throw new Error("listRecentTracks: malformed cursor");
2976
+ }
2977
+ if (parsed === null || typeof parsed !== "object" || !("l" in parsed) || !("i" in parsed) || typeof parsed.l !== "number" || !Number.isFinite(parsed.l) || typeof parsed.i !== "string" || parsed.i.length === 0) throw new Error("listRecentTracks: malformed cursor");
2978
+ return {
2979
+ lastSeen: parsed.l,
2980
+ trackId: parsed.i
2981
+ };
2982
+ }
2983
+ /** Comparator for the (lastSeen DESC, id DESC) total order. */
2984
+ function compareRecentDesc(a, b) {
2985
+ if (a.lastSeen !== b.lastSeen) return b.lastSeen - a.lastSeen;
2986
+ if (a.id === b.id) return 0;
2987
+ return a.id < b.id ? 1 : -1;
2988
+ }
2989
+ /** True when `row` sits strictly AFTER the cursor position in DESC order
2990
+ * (i.e. belongs to the next page). */
2991
+ function isAfterCursor(row, cursor) {
2992
+ if (row.lastSeen < cursor.lastSeen) return true;
2993
+ return row.lastSeen === cursor.lastSeen && row.id < cursor.trackId;
2994
+ }
2995
+ //#endregion
2996
+ //#region src/pipeline-analytics/store/zone-geometry.ts
2997
+ /**
2998
+ * Normalized min/max envelope over every position's bbox. Returns `null`
2999
+ * when the frame dimensions are unknown/degenerate or there are no
3000
+ * positions — the caller persists NULL envelope columns in that case.
3001
+ */
3002
+ function computeTrackEnvelope(positions, frameWidth, frameHeight) {
3003
+ if (frameWidth === void 0 || frameHeight === void 0 || !(frameWidth > 0) || !(frameHeight > 0) || positions.length === 0) return null;
3004
+ let minX = Number.POSITIVE_INFINITY;
3005
+ let minY = Number.POSITIVE_INFINITY;
3006
+ let maxX = Number.NEGATIVE_INFINITY;
3007
+ let maxY = Number.NEGATIVE_INFINITY;
3008
+ for (const p of positions) {
3009
+ const x0 = p.bbox.x / frameWidth;
3010
+ const y0 = p.bbox.y / frameHeight;
3011
+ const x1 = (p.bbox.x + p.bbox.w) / frameWidth;
3012
+ const y1 = (p.bbox.y + p.bbox.h) / frameHeight;
3013
+ if (x0 < minX) minX = x0;
3014
+ if (y0 < minY) minY = y0;
3015
+ if (x1 > maxX) maxX = x1;
3016
+ if (y1 > maxY) maxY = y1;
3017
+ }
3018
+ return {
3019
+ minX,
3020
+ minY,
3021
+ maxX,
3022
+ maxY
3023
+ };
3024
+ }
3025
+ /**
3026
+ * Axis-aligned bounds of a zone filter (rect: itself; polygon: vertex
3027
+ * min/max). A degenerate polygon (< 3 points) yields the full frame so the
3028
+ * SQL prefilter never silently drops rows the precise test would keep.
3029
+ */
3030
+ function zoneBounds(zone) {
3031
+ if (zone.kind === "rect") return {
3032
+ minX: zone.x,
3033
+ minY: zone.y,
3034
+ maxX: zone.x + zone.width,
3035
+ maxY: zone.y + zone.height
3036
+ };
3037
+ if (zone.points.length < 3) return {
3038
+ minX: 0,
3039
+ minY: 0,
3040
+ maxX: 1,
3041
+ maxY: 1
3042
+ };
3043
+ let minX = Number.POSITIVE_INFINITY;
3044
+ let minY = Number.POSITIVE_INFINITY;
3045
+ let maxX = Number.NEGATIVE_INFINITY;
3046
+ let maxY = Number.NEGATIVE_INFINITY;
3047
+ for (const p of zone.points) {
3048
+ if (p.x < minX) minX = p.x;
3049
+ if (p.y < minY) minY = p.y;
3050
+ if (p.x > maxX) maxX = p.x;
3051
+ if (p.y > maxY) maxY = p.y;
3052
+ }
3053
+ return {
3054
+ minX,
3055
+ minY,
3056
+ maxX,
3057
+ maxY
3058
+ };
3059
+ }
3060
+ /** Whether two axis-aligned envelopes overlap (touching edges count). */
3061
+ function envelopesOverlap(a, b) {
3062
+ return a.minX <= b.maxX && a.maxX >= b.minX && a.minY <= b.maxY && a.maxY >= b.minY;
3063
+ }
3064
+ /**
3065
+ * Ray-casting point-in-polygon (even-odd rule). Points on an edge may
3066
+ * resolve either way — acceptable for zone filtering. A polygon with
3067
+ * fewer than 3 vertices contains nothing.
3068
+ */
3069
+ function pointInPolygon(point, polygon) {
3070
+ if (polygon.length < 3) return false;
3071
+ let inside = false;
3072
+ for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
3073
+ const a = polygon[i];
3074
+ const b = polygon[j];
3075
+ if (a.y > point.y !== b.y > point.y && point.x < (b.x - a.x) * (point.y - a.y) / (b.y - a.y) + a.x) inside = !inside;
3076
+ }
3077
+ return inside;
3078
+ }
3079
+ /**
3080
+ * Precise per-position zone test.
3081
+ *
3082
+ * - rect zone → any position bbox (normalized) intersects the rect.
3083
+ * - polygon zone → any position CENTER (normalized `x`/`y` — positions
3084
+ * store the bbox center) falls inside the polygon.
3085
+ *
3086
+ * Unknown/degenerate frame dims → `true` (the track cannot be tested, so it
3087
+ * PASSES — mirroring the NULL-envelope-matches rule).
3088
+ */
3089
+ function positionsIntersectZone(positions, frameWidth, frameHeight, zone) {
3090
+ if (frameWidth === void 0 || frameHeight === void 0 || !(frameWidth > 0) || !(frameHeight > 0)) return true;
3091
+ if (zone.kind === "rect") {
3092
+ const rect = zoneBounds(zone);
3093
+ for (const p of positions) if (envelopesOverlap({
3094
+ minX: p.bbox.x / frameWidth,
3095
+ minY: p.bbox.y / frameHeight,
3096
+ maxX: (p.bbox.x + p.bbox.w) / frameWidth,
3097
+ maxY: (p.bbox.y + p.bbox.h) / frameHeight
3098
+ }, rect)) return true;
3099
+ return false;
3100
+ }
3101
+ for (const p of positions) if (pointInPolygon({
3102
+ x: p.x / frameWidth,
3103
+ y: p.y / frameHeight
3104
+ }, zone.points)) return true;
3105
+ return false;
3106
+ }
3107
+ //#endregion
2958
3108
  //#region src/pipeline-analytics/store/track-store.ts
2959
3109
  var DEFAULT_CONFIG = {
2960
3110
  ttlMs: 3e4,
2961
3111
  maxPositionHistory: 300
2962
3112
  };
3113
+ /** `queryRecent` page-size defaults (mirrors the cap input's bounds). */
3114
+ var RECENT_DEFAULT_LIMIT = 200;
3115
+ var RECENT_MAX_LIMIT = 1e3;
3116
+ /** Wide inclusive bound for the envelope-overlap `BETWEEN` prefilter.
3117
+ * Envelope values are normalized ~0..1 but a bbox can spill slightly past
3118
+ * the frame edge; ±1e6 keeps every real value inside the range while the
3119
+ * opposing bound does the actual overlap cut. */
3120
+ var ENV_RANGE_SLACK = 1e6;
3121
+ function rowMatchesZone(data, zone) {
3122
+ const fw = data["frameWidth"];
3123
+ const fh = data["frameHeight"];
3124
+ if (typeof fw !== "number" || typeof fh !== "number") return true;
3125
+ const positions = data["positions"];
3126
+ if (!Array.isArray(positions)) return true;
3127
+ const positionRows = [];
3128
+ for (const p of positions) {
3129
+ if (p === null || typeof p !== "object") continue;
3130
+ if (!("x" in p) || typeof p.x !== "number" || !("y" in p) || typeof p.y !== "number") continue;
3131
+ if (!("bbox" in p) || p.bbox === null || typeof p.bbox !== "object") continue;
3132
+ const b = p.bbox;
3133
+ if (!("x" in b) || typeof b.x !== "number" || !("y" in b) || typeof b.y !== "number") continue;
3134
+ if (!("w" in b) || typeof b.w !== "number" || !("h" in b) || typeof b.h !== "number") continue;
3135
+ positionRows.push({
3136
+ x: p.x,
3137
+ y: p.y,
3138
+ bbox: {
3139
+ x: b.x,
3140
+ y: b.y,
3141
+ w: b.w,
3142
+ h: b.h
3143
+ }
3144
+ });
3145
+ }
3146
+ return positionsIntersectZone(positionRows, fw, fh, zone);
3147
+ }
2963
3148
  var TRACKS_COLLECTION = "pipeline-analytics:tracks";
2964
3149
  var TRACKS_COLUMNS = [
2965
3150
  {
@@ -3031,6 +3216,30 @@ var TRACKS_COLUMNS = [
3031
3216
  {
3032
3217
  name: "audioLabels",
3033
3218
  type: "JSON"
3219
+ },
3220
+ {
3221
+ name: "envMinX",
3222
+ type: "REAL"
3223
+ },
3224
+ {
3225
+ name: "envMinY",
3226
+ type: "REAL"
3227
+ },
3228
+ {
3229
+ name: "envMaxX",
3230
+ type: "REAL"
3231
+ },
3232
+ {
3233
+ name: "envMaxY",
3234
+ type: "REAL"
3235
+ },
3236
+ {
3237
+ name: "frameWidth",
3238
+ type: "INTEGER"
3239
+ },
3240
+ {
3241
+ name: "frameHeight",
3242
+ type: "INTEGER"
3034
3243
  }
3035
3244
  ];
3036
3245
  var TRACKS_INDEXES = [{
@@ -3086,6 +3295,7 @@ var TrackStore = class {
3086
3295
  config;
3087
3296
  logger;
3088
3297
  store;
3298
+ frameDims;
3089
3299
  constructor(deps) {
3090
3300
  this.logger = deps.logger;
3091
3301
  this.store = deps.store;
@@ -3093,6 +3303,7 @@ var TrackStore = class {
3093
3303
  ...DEFAULT_CONFIG,
3094
3304
  ...deps.config
3095
3305
  };
3306
+ this.frameDims = deps.frameDims;
3096
3307
  }
3097
3308
  /** One-time collection declaration. Call from addon onInitialize. */
3098
3309
  static async declare(store) {
@@ -3439,21 +3650,200 @@ var TrackStore = class {
3439
3650
  }
3440
3651
  return [...seenDevices];
3441
3652
  }
3442
- /** Historical query — hits the persisted collection. */
3653
+ /** Historical query — hits the persisted collection. With `zone` set,
3654
+ * candidates are SQL-prefiltered on the envelope columns (overlap test via
3655
+ * `whereBetween`), NULL-envelope rows are re-fetched separately (they must
3656
+ * still MATCH — `BETWEEN` excludes NULL), and survivors run the precise
3657
+ * per-position test against the zone. `projection: 'slim'` drops the heavy
3658
+ * `positions[]` / `snapshots[]` JSON from the returned rows (empty arrays);
3659
+ * the zone test still runs on the stored positions before the drop. */
3443
3660
  async queryHistorical(params) {
3444
- const filter = { where: { deviceId: params.deviceId } };
3445
- if (params.since !== void 0 || params.until !== void 0) filter.whereBetween = { firstSeen: [params.since ?? 0, params.until ?? Date.now()] };
3446
- return (await this.store.query.query({
3661
+ const limit = params.limit ?? 50;
3662
+ const timeBetween = params.since !== void 0 || params.until !== void 0 ? { firstSeen: [params.since ?? 0, params.until ?? Date.now()] } : {};
3663
+ if (params.zone === void 0) return (await this.store.query.query({
3664
+ collection: TRACKS_COLLECTION,
3665
+ filter: {
3666
+ where: { deviceId: params.deviceId },
3667
+ ...Object.keys(timeBetween).length > 0 ? { whereBetween: timeBetween } : {},
3668
+ orderBy: {
3669
+ field: "firstSeen",
3670
+ direction: "desc"
3671
+ },
3672
+ limit
3673
+ }
3674
+ })).map((r) => this.rowToTrack(r.id, r.data, params.projection));
3675
+ const zone = params.zone;
3676
+ const bounds = zoneBounds(zone);
3677
+ const overlapQuery = this.store.query.query({
3447
3678
  collection: TRACKS_COLLECTION,
3448
3679
  filter: {
3449
- ...filter,
3680
+ where: { deviceId: params.deviceId },
3681
+ whereBetween: {
3682
+ ...timeBetween,
3683
+ envMinX: [-1e6, bounds.maxX],
3684
+ envMaxX: [bounds.minX, ENV_RANGE_SLACK],
3685
+ envMinY: [-1e6, bounds.maxY],
3686
+ envMaxY: [bounds.minY, ENV_RANGE_SLACK]
3687
+ },
3450
3688
  orderBy: {
3451
3689
  field: "firstSeen",
3452
3690
  direction: "desc"
3453
3691
  },
3454
- limit: params.limit ?? 50
3692
+ limit
3693
+ }
3694
+ });
3695
+ const nullEnvQuery = this.store.query.query({
3696
+ collection: TRACKS_COLLECTION,
3697
+ filter: {
3698
+ where: { deviceId: params.deviceId },
3699
+ ...Object.keys(timeBetween).length > 0 ? { whereBetween: timeBetween } : {},
3700
+ orderBy: {
3701
+ field: "firstSeen",
3702
+ direction: "desc"
3703
+ },
3704
+ limit
3455
3705
  }
3456
- })).map((r) => this.rowToTrack(r.id, r.data));
3706
+ });
3707
+ const [overlapRows, windowRows] = await Promise.all([overlapQuery, nullEnvQuery]);
3708
+ const candidates = /* @__PURE__ */ new Map();
3709
+ for (const r of overlapRows) if (typeof r.data["envMinX"] === "number") candidates.set(r.id, r.data);
3710
+ for (const r of windowRows) if (r.data["envMinX"] === null || r.data["envMinX"] === void 0) candidates.set(r.id, r.data);
3711
+ const matched = [];
3712
+ for (const [id, data] of candidates) if (rowMatchesZone(data, zone)) matched.push({
3713
+ id,
3714
+ data
3715
+ });
3716
+ matched.sort((a, b) => Number(b.data["firstSeen"] ?? 0) - Number(a.data["firstSeen"] ?? 0));
3717
+ return matched.slice(0, limit).map((r) => this.rowToTrack(r.id, r.data, params.projection));
3718
+ }
3719
+ /**
3720
+ * Batched multi-device recent-tracks page (`listRecentTracks`): the
3721
+ * persisted completed tracks of every requested device, merged and ordered
3722
+ * by (`lastSeen` DESC, `trackId` DESC) with a stable opaque cursor.
3723
+ *
3724
+ * Approach — per-device indexed page + k-way merge (documented for the
3725
+ * cap): each device is fetched with ONE indexed query on
3726
+ * `idx_tracks_device_lastSeen` (`WHERE deviceId = ? AND lastSeen BETWEEN
3727
+ * ? AND ? ORDER BY lastSeen DESC LIMIT limit+1`), then the pages are
3728
+ * merged in memory and cut to `limit`. At events-page cardinalities
3729
+ * (≤ dozens of devices × ≤ 1000 rows) the in-memory merge is negligible
3730
+ * next to the row I/O, and every row fetched is a candidate (no scan
3731
+ * waste). The +1 overfetch makes `nextCursor` exact: when the merged
3732
+ * candidate set exceeds `limit` more rows are KNOWN to exist; when it
3733
+ * does not, every device returned fewer rows than asked for and is
3734
+ * therefore exhausted — so the final page always ends with
3735
+ * `nextCursor: null` (no empty trailing page).
3736
+ *
3737
+ * Cursor correctness: SQL can only bound `lastSeen`, and rows sharing the
3738
+ * cursor's exact millisecond have no defined SQL order — so a cursor page
3739
+ * runs TWO ranges per device: an exhaustive same-millisecond tie query
3740
+ * (`lastSeen = cursor.lastSeen`, id tie-break applied in memory) plus the
3741
+ * strictly-older indexed page (`lastSeen ≤ cursor.lastSeen - 1`). Tie
3742
+ * clusters are same-ms track expiries on one camera — physically tiny —
3743
+ * so the unbounded tie query stays O(1) in practice. Known accepted edge:
3744
+ * on a NON-cursor page, a same-ms tie cluster straddling a device's
3745
+ * `limit+1` SQL cut could omit a tie row (needs > limit rows sharing one
3746
+ * millisecond on one camera — unreachable at these cardinalities).
3747
+ *
3748
+ * Errors propagate (no partial merges): a failed device query fails the
3749
+ * page rather than silently returning an incomplete window.
3750
+ */
3751
+ async queryRecent(params) {
3752
+ const limit = Math.min(Math.max(params.limit ?? RECENT_DEFAULT_LIMIT, 1), RECENT_MAX_LIMIT);
3753
+ const deviceIds = [...new Set(params.deviceIds)];
3754
+ if (deviceIds.length === 0) return {
3755
+ tracks: [],
3756
+ nextCursor: null
3757
+ };
3758
+ const cursor = params.cursor !== void 0 ? decodeRecentCursor(params.cursor) : null;
3759
+ const lo = params.since ?? 0;
3760
+ const winHi = params.until ?? Number.MAX_SAFE_INTEGER;
3761
+ const hi = cursor !== null ? Math.min(cursor.lastSeen, winHi) : winHi;
3762
+ if (hi < lo) return {
3763
+ tracks: [],
3764
+ nextCursor: null
3765
+ };
3766
+ const fetchRange = async (deviceId, range, pageLimit) => this.store.query.query({
3767
+ collection: TRACKS_COLLECTION,
3768
+ filter: {
3769
+ where: { deviceId },
3770
+ whereBetween: { lastSeen: range },
3771
+ orderBy: {
3772
+ field: "lastSeen",
3773
+ direction: "desc"
3774
+ },
3775
+ ...pageLimit !== void 0 ? { limit: pageLimit } : {}
3776
+ }
3777
+ });
3778
+ const perDevice = await Promise.all(deviceIds.map(async (deviceId) => {
3779
+ if (cursor === null || cursor.lastSeen > hi) return fetchRange(deviceId, [lo, hi], limit + 1);
3780
+ const tieRange = [cursor.lastSeen, cursor.lastSeen];
3781
+ const belowHi = cursor.lastSeen - 1;
3782
+ const [ties, below] = await Promise.all([cursor.lastSeen >= lo ? fetchRange(deviceId, tieRange) : Promise.resolve([]), belowHi >= lo ? fetchRange(deviceId, [lo, belowHi], limit + 1) : Promise.resolve([])]);
3783
+ return [...ties, ...below];
3784
+ }));
3785
+ const candidates = [];
3786
+ for (const rows of perDevice) for (const r of rows) {
3787
+ const lastSeen = Number(r.data["lastSeen"] ?? 0);
3788
+ if (cursor !== null && !isAfterCursor({
3789
+ lastSeen,
3790
+ id: r.id
3791
+ }, cursor)) continue;
3792
+ candidates.push({
3793
+ id: r.id,
3794
+ lastSeen,
3795
+ data: r.data
3796
+ });
3797
+ }
3798
+ candidates.sort(compareRecentDesc);
3799
+ const page = candidates.slice(0, limit);
3800
+ const tracks = page.map((r) => this.rowToTrack(r.id, r.data, params.projection));
3801
+ const last = page[page.length - 1];
3802
+ return {
3803
+ tracks,
3804
+ nextCursor: candidates.length > limit && last !== void 0 ? encodeRecentCursor({
3805
+ lastSeen: last.lastSeen,
3806
+ trackId: last.id
3807
+ }) : null
3808
+ };
3809
+ }
3810
+ /**
3811
+ * Deduplicated detector class names observed on a device's RECENT persisted
3812
+ * tracks (one indexed page, `lastSeen` desc). Feeds `listEventKinds` — a
3813
+ * representative "what has this camera actually seen" set, not an exhaustive
3814
+ * all-time DISTINCT (the query cap has none). Unions the primary `className`
3815
+ * with the accumulated `classes` array. Best-effort: [] on error.
3816
+ */
3817
+ async observedClassNames(deviceId, limit = 500) {
3818
+ try {
3819
+ const rows = await this.store.query.query({
3820
+ collection: TRACKS_COLLECTION,
3821
+ filter: {
3822
+ where: { deviceId },
3823
+ orderBy: {
3824
+ field: "lastSeen",
3825
+ direction: "desc"
3826
+ },
3827
+ limit
3828
+ }
3829
+ });
3830
+ const names = /* @__PURE__ */ new Set();
3831
+ for (const r of rows) {
3832
+ const className = r.data["className"];
3833
+ if (typeof className === "string" && className.length > 0) names.add(className);
3834
+ const classes = r.data["classes"];
3835
+ if (Array.isArray(classes)) {
3836
+ for (const c of classes) if (typeof c === "string" && c.length > 0) names.add(c);
3837
+ }
3838
+ }
3839
+ return [...names];
3840
+ } catch (err) {
3841
+ this.logger.warn("TrackStore.observedClassNames failed", { meta: {
3842
+ deviceId,
3843
+ error: String(err)
3844
+ } });
3845
+ return [];
3846
+ }
3457
3847
  }
3458
3848
  async getPersistedByTrackId(trackId) {
3459
3849
  const records = await this.store.query.query({
@@ -3468,6 +3858,8 @@ var TrackStore = class {
3468
3858
  return this.rowToTrack(row.id, row.data);
3469
3859
  }
3470
3860
  async persistCompleted(t) {
3861
+ const dims = this.frameDims?.(t.deviceId);
3862
+ const envelope = computeTrackEnvelope(t.positions, dims?.w, dims?.h);
3471
3863
  await this.store.set.mutate({
3472
3864
  collection: TRACKS_COLLECTION,
3473
3865
  key: t.trackId,
@@ -3486,13 +3878,30 @@ var TrackStore = class {
3486
3878
  ...t.importance !== void 0 ? { importance: t.importance } : {},
3487
3879
  ...t.bestEventId !== void 0 ? { bestEventId: t.bestEventId } : {},
3488
3880
  ...t.importanceReason !== void 0 ? { importanceReason: t.importanceReason } : {},
3489
- ...t.audioLabels !== void 0 ? { audioLabels: [...t.audioLabels] } : {}
3881
+ ...t.audioLabels !== void 0 ? { audioLabels: [...t.audioLabels] } : {},
3882
+ ...envelope !== null && dims !== void 0 ? {
3883
+ envMinX: envelope.minX,
3884
+ envMinY: envelope.minY,
3885
+ envMaxX: envelope.maxX,
3886
+ envMaxY: envelope.maxY,
3887
+ frameWidth: dims.w,
3888
+ frameHeight: dims.h
3889
+ } : {}
3490
3890
  }
3491
3891
  });
3492
3892
  }
3493
- rowToTrack(id, data) {
3494
- const positions = data["positions"] ?? [];
3495
- const snapshots = data["snapshots"] ?? [];
3893
+ /**
3894
+ * Map a persisted row onto the cap `Track` shape. `projection: 'slim'`
3895
+ * drops the heavy `positions[]` / `snapshots[]` JSON (returned as empty
3896
+ * arrays — they are required on the schema) while keeping every scalar
3897
+ * the list surfaces render; `full` (default) is byte-compatible with the
3898
+ * pre-projection behaviour. The persisted envelope columns surface as the
3899
+ * optional `envelope` object in BOTH projections (four light numbers).
3900
+ */
3901
+ rowToTrack(id, data, projection) {
3902
+ const slim = projection === "slim";
3903
+ const positions = slim ? [] : data["positions"] ?? [];
3904
+ const snapshots = slim ? [] : data["snapshots"] ?? [];
3496
3905
  const zones = data["zonesVisited"] ?? [];
3497
3906
  const classes = data["classes"];
3498
3907
  const label = data["label"];
@@ -3500,6 +3909,16 @@ var TrackStore = class {
3500
3909
  const bestEventId = data["bestEventId"];
3501
3910
  const importanceReason = data["importanceReason"];
3502
3911
  const audioLabels = data["audioLabels"];
3912
+ const envMinX = data["envMinX"];
3913
+ const envMinY = data["envMinY"];
3914
+ const envMaxX = data["envMaxX"];
3915
+ const envMaxY = data["envMaxY"];
3916
+ const envelope = typeof envMinX === "number" && typeof envMinY === "number" && typeof envMaxX === "number" && typeof envMaxY === "number" ? {
3917
+ minX: envMinX,
3918
+ minY: envMinY,
3919
+ maxX: envMaxX,
3920
+ maxY: envMaxY
3921
+ } : null;
3503
3922
  return {
3504
3923
  trackId: id,
3505
3924
  deviceId: Number(data["deviceId"]),
@@ -3517,7 +3936,8 @@ var TrackStore = class {
3517
3936
  ...typeof importance === "number" ? { importance } : {},
3518
3937
  ...typeof bestEventId === "string" ? { bestEventId } : {},
3519
3938
  ...typeof importanceReason === "string" ? { importanceReason } : {},
3520
- ...Array.isArray(audioLabels) && audioLabels.length > 0 ? { audioLabels } : {}
3939
+ ...Array.isArray(audioLabels) && audioLabels.length > 0 ? { audioLabels } : {},
3940
+ ...envelope !== null ? { envelope } : {}
3521
3941
  };
3522
3942
  }
3523
3943
  };
@@ -4628,6 +5048,384 @@ function stripNulls(data) {
4628
5048
  return out;
4629
5049
  }
4630
5050
  //#endregion
5051
+ //#region src/pipeline-analytics/store/sensor-event-store.ts
5052
+ var SENSOR_EVENTS_COLLECTION = "pipeline-analytics:sensor-events";
5053
+ var SENSOR_EVENT_COLUMNS = [
5054
+ {
5055
+ name: "id",
5056
+ type: "TEXT",
5057
+ primaryKey: true,
5058
+ notNull: true
5059
+ },
5060
+ (
5061
+ /** The CAMERA the event is attributed to. */
5062
+ {
5063
+ name: "deviceId",
5064
+ type: "INTEGER",
5065
+ notNull: true
5066
+ }),
5067
+ (
5068
+ /** The linked sensor device whose state changed. */
5069
+ {
5070
+ name: "sourceDeviceId",
5071
+ type: "INTEGER",
5072
+ notNull: true
5073
+ }),
5074
+ (
5075
+ /** Event kind id (matches an `EventKindDescriptor.kind`). */
5076
+ {
5077
+ name: "kind",
5078
+ type: "TEXT",
5079
+ notNull: true
5080
+ }),
5081
+ (
5082
+ /** Snapshot of the sensor cap's runtime-state slice at the change. */
5083
+ {
5084
+ name: "value",
5085
+ type: "JSON"
5086
+ }),
5087
+ {
5088
+ name: "timestamp",
5089
+ type: "INTEGER",
5090
+ notNull: true
5091
+ }
5092
+ ];
5093
+ var SENSOR_EVENT_INDEXES = [{
5094
+ name: "idx_sensor_events_device_ts",
5095
+ columns: ["deviceId", "timestamp"]
5096
+ }];
5097
+ var DEFAULT_QUERY_LIMIT = 1e3;
5098
+ var SensorEventStore = class {
5099
+ store;
5100
+ logger;
5101
+ constructor(deps) {
5102
+ this.store = deps.store;
5103
+ this.logger = deps.logger;
5104
+ }
5105
+ /** One-time collection declaration. Call from addon onInitialize. */
5106
+ static async declare(store) {
5107
+ await store.declareCollection.mutate({
5108
+ collection: SENSOR_EVENTS_COLLECTION,
5109
+ columns: [...SENSOR_EVENT_COLUMNS],
5110
+ indexes: [...SENSOR_EVENT_INDEXES]
5111
+ });
5112
+ }
5113
+ /** Insert one attributed sensor event. Best-effort (telemetry-lossy). */
5114
+ async insert(ev) {
5115
+ try {
5116
+ await this.store.insert.mutate({
5117
+ collection: SENSOR_EVENTS_COLLECTION,
5118
+ record: {
5119
+ id: ev.id,
5120
+ data: {
5121
+ deviceId: ev.deviceId,
5122
+ sourceDeviceId: ev.sourceDeviceId,
5123
+ kind: ev.kind,
5124
+ value: ev.value,
5125
+ timestamp: ev.timestamp
5126
+ }
5127
+ }
5128
+ });
5129
+ } catch (err) {
5130
+ this.logger.warn("SensorEventStore.insert failed", {
5131
+ tags: { deviceId: ev.deviceId },
5132
+ meta: {
5133
+ eventId: ev.id,
5134
+ error: String(err)
5135
+ }
5136
+ });
5137
+ }
5138
+ }
5139
+ /** Per-camera sensor-event history, newest first. Mirrors the
5140
+ * motion/object/audio query semantics; `kinds` narrows via `whereIn`. */
5141
+ async query(q) {
5142
+ const filter = {
5143
+ where: { deviceId: q.deviceId },
5144
+ orderBy: {
5145
+ field: "timestamp",
5146
+ direction: "desc"
5147
+ },
5148
+ limit: q.limit ?? DEFAULT_QUERY_LIMIT
5149
+ };
5150
+ if (q.since !== void 0 || q.until !== void 0) filter["whereBetween"] = { timestamp: [q.since ?? 0, q.until ?? Date.now()] };
5151
+ if (q.kinds !== void 0 && q.kinds.length > 0) filter["whereIn"] = { kind: [...q.kinds] };
5152
+ return (await this.store.query.query({
5153
+ collection: SENSOR_EVENTS_COLLECTION,
5154
+ filter
5155
+ })).map((r) => rowToSensorEvent(r.id, r.data));
5156
+ }
5157
+ /**
5158
+ * Delete every row with `timestamp ≤ cutoffMs`, draining a page at a time
5159
+ * (mirrors `EventStore.evictBefore`, including the infinite-loop guard).
5160
+ * Returns the number of rows deleted. Rides the analytics retention sweep.
5161
+ */
5162
+ async evictBefore(cutoffMs) {
5163
+ let deleted = 0;
5164
+ for (;;) {
5165
+ const rows = await this.store.query.query({
5166
+ collection: SENSOR_EVENTS_COLLECTION,
5167
+ filter: {
5168
+ whereBetween: { timestamp: [0, cutoffMs] },
5169
+ limit: EVICT_PAGE_SIZE
5170
+ }
5171
+ });
5172
+ if (rows.length === 0) break;
5173
+ let deletedInPage = 0;
5174
+ for (const row of rows) {
5175
+ if (typeof row.id !== "string") continue;
5176
+ try {
5177
+ await this.store.delete.mutate({
5178
+ collection: SENSOR_EVENTS_COLLECTION,
5179
+ key: row.id
5180
+ });
5181
+ deleted++;
5182
+ deletedInPage++;
5183
+ } catch {}
5184
+ }
5185
+ if (deletedInPage === 0) break;
5186
+ }
5187
+ return deleted;
5188
+ }
5189
+ };
5190
+ /** Page size for the eviction drain loop (mirrors EventStore.PRUNE_PAGE_SIZE). */
5191
+ var EVICT_PAGE_SIZE = 500;
5192
+ function rowToSensorEvent(id, data) {
5193
+ const value = data["value"];
5194
+ return {
5195
+ id,
5196
+ deviceId: Number(data["deviceId"]),
5197
+ sourceDeviceId: Number(data["sourceDeviceId"]),
5198
+ kind: String(data["kind"]),
5199
+ value: isRecord(value) ? value : null,
5200
+ timestamp: Number(data["timestamp"])
5201
+ };
5202
+ }
5203
+ function isRecord(x) {
5204
+ return x !== null && typeof x === "object" && !Array.isArray(x);
5205
+ }
5206
+ //#endregion
5207
+ //#region src/pipeline-analytics/services/event-kinds.ts
5208
+ /**
5209
+ * Extensible per-device event kinds (Part B).
5210
+ *
5211
+ * `composeEventKinds` builds the `listEventKinds` payload for a camera:
5212
+ * (a) built-ins — motion + audio, always present;
5213
+ * (b) detection classes actually OBSERVED on the device (track history);
5214
+ * (c) sensor kinds contributed by LINKED devices (device-manager
5215
+ * `getLinkedDevices`), one descriptor per bound sensor cap present in
5216
+ * the static `EVENT_KIND_BY_CAP` map. Binding-driven per linked device
5217
+ * (`getBindings`) — never a global cap enumeration (D12).
5218
+ *
5219
+ * `LinkedCamerasCache` is the ingest-side reverse index (sensor device →
5220
+ * linked camera ids) with a TTL, so the `DeviceStateChanged` handler stays
5221
+ * cheap at bus rate.
5222
+ */
5223
+ var MOTION_COLOR = "#f59e0b";
5224
+ var AUDIO_COLOR = "#06b6d4";
5225
+ var PERSON_COLOR = "#22c55e";
5226
+ var VEHICLE_COLOR = "#3b82f6";
5227
+ var ANIMAL_COLOR = "#f97316";
5228
+ var GENERIC_DETECTION_COLOR = "#64748b";
5229
+ var VEHICLE_CLASSES = new Set([
5230
+ "vehicle",
5231
+ "car",
5232
+ "truck",
5233
+ "bus",
5234
+ "motorcycle",
5235
+ "bicycle",
5236
+ "boat",
5237
+ "train"
5238
+ ]);
5239
+ var ANIMAL_CLASSES = new Set([
5240
+ "animal",
5241
+ "dog",
5242
+ "cat",
5243
+ "bird",
5244
+ "horse",
5245
+ "cow",
5246
+ "sheep"
5247
+ ]);
5248
+ function detectionIcon(className) {
5249
+ if (className === "person") return "person";
5250
+ if (VEHICLE_CLASSES.has(className)) return "vehicle";
5251
+ if (ANIMAL_CLASSES.has(className)) return "animal";
5252
+ return "generic";
5253
+ }
5254
+ function detectionColor(className) {
5255
+ if (className === "person") return PERSON_COLOR;
5256
+ if (VEHICLE_CLASSES.has(className)) return VEHICLE_COLOR;
5257
+ if (ANIMAL_CLASSES.has(className)) return ANIMAL_COLOR;
5258
+ return GENERIC_DETECTION_COLOR;
5259
+ }
5260
+ function titleCase(s) {
5261
+ return s.length > 0 ? s.charAt(0).toUpperCase() + s.slice(1) : s;
5262
+ }
5263
+ /**
5264
+ * Full event-kind list for a camera. Sensor kinds are deduped per
5265
+ * (kind, source deviceId) — two linked contact sensors each contribute
5266
+ * their own entry, distinguishable by `source.deviceId`.
5267
+ */
5268
+ async function composeEventKinds(deps, deviceId) {
5269
+ const out = [{
5270
+ kind: "motion",
5271
+ label: "Motion",
5272
+ color: MOTION_COLOR,
5273
+ icon: "motion",
5274
+ category: "motion",
5275
+ source: {
5276
+ capName: "pipeline-analytics",
5277
+ deviceId
5278
+ }
5279
+ }, {
5280
+ kind: "audio",
5281
+ label: "Audio",
5282
+ color: AUDIO_COLOR,
5283
+ icon: "audio",
5284
+ category: "audio",
5285
+ source: {
5286
+ capName: "pipeline-analytics",
5287
+ deviceId
5288
+ }
5289
+ }];
5290
+ try {
5291
+ const classNames = await deps.observedClassNames(deviceId);
5292
+ for (const className of [...classNames].sort()) out.push({
5293
+ kind: className,
5294
+ label: titleCase(className),
5295
+ color: detectionColor(className),
5296
+ icon: detectionIcon(className),
5297
+ category: "detection",
5298
+ source: {
5299
+ capName: "pipeline-analytics",
5300
+ deviceId
5301
+ }
5302
+ });
5303
+ } catch (err) {
5304
+ deps.onError?.("observedClassNames", err);
5305
+ }
5306
+ try {
5307
+ const { devices } = await deps.linkedDevices.getLinkedDevices({ deviceId });
5308
+ const seen = /* @__PURE__ */ new Set();
5309
+ for (const linked of devices) {
5310
+ let capNames;
5311
+ try {
5312
+ const { entries } = await deps.bindings.getBindings({ deviceId: linked.deviceId });
5313
+ capNames = entries.map((e) => e.capName);
5314
+ } catch (err) {
5315
+ deps.onError?.("getBindings", err);
5316
+ continue;
5317
+ }
5318
+ for (const capName of capNames) {
5319
+ const descriptor = EVENT_KIND_BY_CAP[capName];
5320
+ if (descriptor === void 0) continue;
5321
+ const dedupeKey = `${descriptor.kind}:${linked.deviceId}`;
5322
+ if (seen.has(dedupeKey)) continue;
5323
+ seen.add(dedupeKey);
5324
+ out.push({
5325
+ kind: descriptor.kind,
5326
+ label: descriptor.label,
5327
+ color: descriptor.color,
5328
+ icon: descriptor.icon,
5329
+ category: descriptor.category,
5330
+ source: {
5331
+ capName,
5332
+ deviceId: linked.deviceId
5333
+ }
5334
+ });
5335
+ }
5336
+ }
5337
+ } catch (err) {
5338
+ deps.onError?.("getLinkedDevices", err);
5339
+ }
5340
+ return out;
5341
+ }
5342
+ var DEFAULT_CACHE_TTL_MS = 6e4;
5343
+ /**
5344
+ * TTL-cached reverse index: source deviceId → camera ids it is linked to.
5345
+ * Rebuilds lazily (single-flight) when stale, so the `DeviceStateChanged`
5346
+ * handler pays one map lookup per event in the common case.
5347
+ */
5348
+ var LinkedCamerasCache = class {
5349
+ deps;
5350
+ ttlMs;
5351
+ index = /* @__PURE__ */ new Map();
5352
+ /** Ms timestamp of the last build; null = never built / invalidated. */
5353
+ builtAt = null;
5354
+ building = null;
5355
+ constructor(deps) {
5356
+ this.deps = deps;
5357
+ this.ttlMs = deps.ttlMs ?? DEFAULT_CACHE_TTL_MS;
5358
+ }
5359
+ /** Camera ids linked to `sourceDeviceId` ([] when none). */
5360
+ async camerasFor(sourceDeviceId, nowMs = Date.now()) {
5361
+ if (this.builtAt === null || nowMs - this.builtAt >= this.ttlMs) {
5362
+ this.building ??= this.rebuild(nowMs).finally(() => {
5363
+ this.building = null;
5364
+ });
5365
+ await this.building;
5366
+ }
5367
+ return this.index.get(sourceDeviceId) ?? [];
5368
+ }
5369
+ /** Drop the cached index (e.g. on link-topology change events). */
5370
+ invalidate() {
5371
+ this.builtAt = null;
5372
+ }
5373
+ /** Test/maintenance hook: replace the index directly. */
5374
+ seed(index, builtAt) {
5375
+ this.index = new Map(index);
5376
+ this.builtAt = builtAt;
5377
+ }
5378
+ async rebuild(nowMs) {
5379
+ try {
5380
+ const cameraIds = await this.deps.cameras.listCameraIds();
5381
+ const next = /* @__PURE__ */ new Map();
5382
+ for (const cameraId of cameraIds) try {
5383
+ const { devices } = await this.deps.linkedDevices.getLinkedDevices({ deviceId: cameraId });
5384
+ for (const d of devices) {
5385
+ const list = next.get(d.deviceId);
5386
+ if (list === void 0) next.set(d.deviceId, [cameraId]);
5387
+ else if (!list.includes(cameraId)) list.push(cameraId);
5388
+ }
5389
+ } catch (err) {
5390
+ this.deps.onError?.("getLinkedDevices", err);
5391
+ }
5392
+ this.index = next;
5393
+ this.builtAt = nowMs;
5394
+ } catch (err) {
5395
+ this.deps.onError?.("listCameraIds", err);
5396
+ this.builtAt = nowMs;
5397
+ }
5398
+ }
5399
+ };
5400
+ /**
5401
+ * One `DeviceStateChanged` → N history rows (one per linked camera). The
5402
+ * EVENT_KIND_BY_CAP gate exits first so non-sensor cap churn costs one map
5403
+ * lookup. Returns the number of rows inserted (0 when unmapped/unlinked).
5404
+ * Telemetry-lossy by design (D8) — inserts are best-effort.
5405
+ */
5406
+ async function ingestSensorStateChange(deps, data, timestamp) {
5407
+ const descriptor = EVENT_KIND_BY_CAP[data.capName];
5408
+ if (descriptor === void 0) return 0;
5409
+ const cameraIds = await deps.cache.camerasFor(data.deviceId);
5410
+ if (cameraIds.length === 0) return 0;
5411
+ const slice = data.slice;
5412
+ const value = slice !== null && slice !== void 0 && typeof slice === "object" && !Array.isArray(slice) ? slice : null;
5413
+ const makeId = deps.makeId ?? (() => `pa-sensor-${randomUUID()}`);
5414
+ let inserted = 0;
5415
+ for (const cameraId of cameraIds) {
5416
+ await deps.sink.insert({
5417
+ id: makeId(),
5418
+ deviceId: cameraId,
5419
+ sourceDeviceId: data.deviceId,
5420
+ kind: descriptor.kind,
5421
+ value,
5422
+ timestamp
5423
+ });
5424
+ inserted++;
5425
+ }
5426
+ return inserted;
5427
+ }
5428
+ //#endregion
4631
5429
  //#region src/shared/frame/resolve-frame.ts
4632
5430
  /**
4633
5431
  * Resolve the pixels a `FrameHandle` refers to via the node-routed fetch.
@@ -4861,22 +5659,26 @@ var EventMediaDispatcher = class {
4861
5659
  for (const tf of trackFrames) await this.writeTrackFrame(deviceId, frameData, fw, fh, tf);
4862
5660
  const storedSnapshots = [];
4863
5661
  for (const sn of snapshots) {
4864
- const stored = await this.writeTrackSnapshot(deviceId, frameData, fw, fh, sn);
5662
+ const stored = await this.writeTrackSnapshot(deviceId, frameData, fw, fh, sn, input.cropPadding);
4865
5663
  if (stored) storedSnapshots.push(stored);
4866
5664
  }
4867
5665
  return { storedSnapshots };
4868
5666
  }
4869
5667
  /**
4870
- * Periodic per-track media (§5). Encodes the boxed frame ONCE and fans it out
4871
- * to whichever of the three destinations is requested: an appended `snapshot`
4872
- * (timeline filmstrip), the rolling `lastFrame` (overwrite), and the best
4873
- * `thumbnail` (overwrite). Returns the appended snapshot for TrackStore wiring
4874
- * (null when `appendSnapshot` is false or the encode failed).
5668
+ * Periodic per-track media (§5). The boxed FULL frame is encoded once and
5669
+ * shared by the appended `snapshot` (timeline filmstrip) and the rolling
5670
+ * `lastFrame` (overwrite). The best `thumbnail` is DIFFERENT: a clean
5671
+ * subject-centered crop (same output contract as the object-event `crop`
5672
+ * kind) it is the gallery/reel fallback for tracks that never produced an
5673
+ * object event, and a full frame there shows the scene (e.g. a foreground
5674
+ * parked car), not the track's subject. Returns the appended snapshot for
5675
+ * TrackStore wiring (null when `appendSnapshot` is false or the encode
5676
+ * failed).
4875
5677
  */
4876
- async writeTrackSnapshot(deviceId, frameData, fw, fh, sn) {
5678
+ async writeTrackSnapshot(deviceId, frameData, fw, fh, sn, cropPadding) {
4877
5679
  if (!sn.appendSnapshot && !sn.rollingLastFrame && !sn.bestThumbnail) return null;
4878
- let boxed;
4879
- try {
5680
+ let boxed = null;
5681
+ if (sn.appendSnapshot || sn.rollingLastFrame) try {
4880
5682
  boxed = await drawBoxedFrame(frameData, fw, fh, [{
4881
5683
  ...sn.bbox,
4882
5684
  ...sn.label ? { label: sn.label } : {}
@@ -4890,10 +5692,9 @@ var EventMediaDispatcher = class {
4890
5692
  error: err instanceof Error ? err.message : String(err)
4891
5693
  }
4892
5694
  });
4893
- return null;
4894
5695
  }
4895
5696
  let stored = null;
4896
- if (sn.appendSnapshot) try {
5697
+ if (sn.appendSnapshot && boxed) try {
4897
5698
  const mediaKey = await this.deps.mediaStore.put({
4898
5699
  deviceId,
4899
5700
  ownerKind: "track",
@@ -4909,10 +5710,49 @@ var EventMediaDispatcher = class {
4909
5710
  bbox: sn.bbox
4910
5711
  };
4911
5712
  } catch {}
4912
- if (sn.rollingLastFrame) await this.replaceKind(deviceId, sn.trackId, "lastFrame", sn.timestamp, boxed);
4913
- if (sn.bestThumbnail) await this.replaceKind(deviceId, sn.trackId, "thumbnail", sn.timestamp, boxed);
5713
+ if (sn.rollingLastFrame && boxed) await this.replaceKind(deviceId, sn.trackId, "lastFrame", sn.timestamp, boxed);
5714
+ if (sn.bestThumbnail) try {
5715
+ const crop = await this.cropSubjectRegion(frameData, fw, fh, sn.bbox, cropPadding);
5716
+ await this.replaceKind(deviceId, sn.trackId, "thumbnail", sn.timestamp, crop);
5717
+ } catch (err) {
5718
+ this.deps.logger.warn("event media: track thumbnail crop failed", {
5719
+ tags: { deviceId },
5720
+ meta: {
5721
+ deviceId,
5722
+ trackId: sn.trackId,
5723
+ error: err instanceof Error ? err.message : String(err)
5724
+ }
5725
+ });
5726
+ if (boxed) await this.replaceKind(deviceId, sn.trackId, "thumbnail", sn.timestamp, boxed);
5727
+ }
4914
5728
  return stored;
4915
5729
  }
5730
+ /**
5731
+ * Clean subject-centered crop of `bbox` out of the raw frame — the shared
5732
+ * output contract of the object-event `crop` kind and the track `thumbnail`:
5733
+ * square-safe 16:9 region around the bbox, extracted from the ORIGINAL frame
5734
+ * (no box drawn), resized to 640×360, JPEG q80.
5735
+ */
5736
+ async cropSubjectRegion(frameData, fw, fh, bbox, cropPadding) {
5737
+ const region = squareSafeCropRegion(bbox, {
5738
+ W: fw,
5739
+ H: fh
5740
+ }, cropPadding);
5741
+ const left = Math.max(0, Math.min(region.x, fw - 1));
5742
+ const top = Math.max(0, Math.min(region.y, fh - 1));
5743
+ const width = Math.max(1, Math.min(region.w, fw - left));
5744
+ const height = Math.max(1, Math.min(region.h, fh - top));
5745
+ return await sharp(frameData, { raw: {
5746
+ width: fw,
5747
+ height: fh,
5748
+ channels: 3
5749
+ } }).extract({
5750
+ left,
5751
+ top,
5752
+ width,
5753
+ height
5754
+ }).resize(CROP_WIDTH, CROP_HEIGHT).jpeg({ quality: CROP_QUALITY }).toBuffer();
5755
+ }
4916
5756
  async replaceKind(deviceId, trackId, kind, timestamp, data) {
4917
5757
  try {
4918
5758
  await this.deps.mediaStore.putReplacing({
@@ -4940,24 +5780,7 @@ var EventMediaDispatcher = class {
4940
5780
  label: caption(ev.className, ev.confidence, ev.label)
4941
5781
  };
4942
5782
  try {
4943
- const region = squareSafeCropRegion(ev.bbox, {
4944
- W: fw,
4945
- H: fh
4946
- }, cropPadding);
4947
- const left = Math.max(0, Math.min(region.x, fw - 1));
4948
- const top = Math.max(0, Math.min(region.y, fh - 1));
4949
- const width = Math.max(1, Math.min(region.w, fw - left));
4950
- const height = Math.max(1, Math.min(region.h, fh - top));
4951
- const crop = await sharp(frameData, { raw: {
4952
- width: fw,
4953
- height: fh,
4954
- channels: 3
4955
- } }).extract({
4956
- left,
4957
- top,
4958
- width,
4959
- height
4960
- }).resize(CROP_WIDTH, CROP_HEIGHT).jpeg({ quality: CROP_QUALITY }).toBuffer();
5783
+ const crop = await this.cropSubjectRegion(frameData, fw, fh, ev.bbox, cropPadding);
4961
5784
  await this.deps.mediaStore.put({
4962
5785
  deviceId,
4963
5786
  ownerKind: "event",
@@ -9727,6 +10550,11 @@ var PipelineAnalyticsAddon = class extends BaseAddon {
9727
10550
  stationaryRegistry = null;
9728
10551
  mediaStore = null;
9729
10552
  eventStore = null;
10553
+ /** Per-camera history of LINKED-device sensor state changes (Part B). */
10554
+ sensorEventStore = null;
10555
+ /** Ingest-side reverse index (sensor device → linked camera ids), TTL-cached
10556
+ * so the DeviceStateChanged handler stays cheap. */
10557
+ linkedCamerasCache = null;
9730
10558
  identityStore = null;
9731
10559
  faceStore = null;
9732
10560
  faceRecognizer = null;
@@ -9777,6 +10605,8 @@ var PipelineAnalyticsAddon = class extends BaseAddon {
9777
10605
  unsubNativeDetection = null;
9778
10606
  unsubBindings = null;
9779
10607
  unsubDeviceUnreg = null;
10608
+ /** DeviceStateChanged subscription feeding the sensor-event history. */
10609
+ unsubDeviceState = null;
9780
10610
  ttlSweepTimer = null;
9781
10611
  retentionSweepTimer = null;
9782
10612
  /** Handle for the event-media data-plane listener (dispose on shutdown). */
@@ -9872,6 +10702,7 @@ var PipelineAnalyticsAddon = class extends BaseAddon {
9872
10702
  await TrackStore.declare(api.settingsStore);
9873
10703
  await MediaStore.declare(api.settingsStore);
9874
10704
  await EventStore.declare(api.settingsStore);
10705
+ await SensorEventStore.declare(api.settingsStore);
9875
10706
  await IdentityStore.declare(api.settingsStore);
9876
10707
  await FaceStore.declare(api.settingsStore);
9877
10708
  await PlateStore.declare(api.settingsStore);
@@ -9889,7 +10720,8 @@ var PipelineAnalyticsAddon = class extends BaseAddon {
9889
10720
  if (!storage) throw new Error("pipeline-analytics requires ctx.kernel.storage");
9890
10721
  this.trackStore = new TrackStore({
9891
10722
  store: api.settingsStore,
9892
- logger: logger.child("TrackStore")
10723
+ logger: logger.child("TrackStore"),
10724
+ frameDims: (deviceId) => this.lastFrameDimsByDevice.get(deviceId)
9893
10725
  });
9894
10726
  this.stationaryRegistry = new StationaryObjectRegistry({
9895
10727
  store: api.settingsStore,
@@ -9925,6 +10757,20 @@ var PipelineAnalyticsAddon = class extends BaseAddon {
9925
10757
  logger: logger.child("EventStore"),
9926
10758
  media: this.mediaStore
9927
10759
  });
10760
+ this.sensorEventStore = new SensorEventStore({
10761
+ store: api.settingsStore,
10762
+ logger: logger.child("SensorEventStore")
10763
+ });
10764
+ this.linkedCamerasCache = new LinkedCamerasCache({
10765
+ cameras: { listCameraIds: async () => {
10766
+ return (await api.deviceManager.listAll.query({})).filter((d) => d.isCamera).map((d) => d.id);
10767
+ } },
10768
+ linkedDevices: { getLinkedDevices: (input) => api.deviceManager.getLinkedDevices.query(input) },
10769
+ onError: (scope, err) => logger.warn("linked-cameras cache refresh failed", { meta: {
10770
+ scope,
10771
+ error: errMsg(err)
10772
+ } })
10773
+ });
9928
10774
  this.identityStore = new IdentityStore({
9929
10775
  store: api.settingsStore,
9930
10776
  logger: logger.child("IdentityStore")
@@ -10151,6 +10997,12 @@ var PipelineAnalyticsAddon = class extends BaseAddon {
10151
10997
  const data = ev.data;
10152
10998
  this.handleNativeDetection(data);
10153
10999
  });
11000
+ this.unsubDeviceState = this.ctx.eventBus.subscribe({ category: EventCategory.DeviceStateChanged }, (ev) => {
11001
+ const data = ev.data;
11002
+ if (EVENT_KIND_BY_CAP[data.capName] === void 0) return;
11003
+ const timestamp = ev.timestamp instanceof Date ? ev.timestamp.getTime() : Date.now();
11004
+ this.handleSensorStateChanged(data, timestamp);
11005
+ });
10154
11006
  if (await this.embeddingEnabledState.get()) {
10155
11007
  const encoderClient = {
10156
11008
  encode: (crop, width, height) => this.ctx.api.embeddingEncoder.encode.query({
@@ -10516,6 +11368,8 @@ var PipelineAnalyticsAddon = class extends BaseAddon {
10516
11368
  this.unsubBindings = null;
10517
11369
  this.unsubDeviceUnreg?.();
10518
11370
  this.unsubDeviceUnreg = null;
11371
+ this.unsubDeviceState?.();
11372
+ this.unsubDeviceState = null;
10519
11373
  await this.embeddingDispatcher?.stop();
10520
11374
  this.embeddingDispatcher = null;
10521
11375
  for (const id of this.proxies.keys()) this.releaseProxy(id);
@@ -11778,6 +12632,15 @@ var PipelineAnalyticsAddon = class extends BaseAddon {
11778
12632
  } });
11779
12633
  }
11780
12634
  await this.mediaStore.evictBefore(now - 31 * day);
12635
+ if (this.sensorEventStore) try {
12636
+ const sensorDeleted = await this.sensorEventStore.evictBefore(objectCutoffMs);
12637
+ if (sensorDeleted > 0) this.ctx.logger.info("sensor-event retention prune", { meta: {
12638
+ deleted: sensorDeleted,
12639
+ cutoffMs: objectCutoffMs
12640
+ } });
12641
+ } catch (err) {
12642
+ this.ctx.logger.debug("sensor-event prune failed", { meta: { error: String(err) } });
12643
+ }
11781
12644
  if (this.faceStore) try {
11782
12645
  const faceCutoffMs = now - FACE_DEFAULTS.bufferRetentionDays * day;
11783
12646
  const deletedFaceIds = await this.faceStore.pruneAll({
@@ -12083,6 +12946,68 @@ var PipelineAnalyticsAddon = class extends BaseAddon {
12083
12946
  async listTracks(input) {
12084
12947
  return this.trackStore?.queryHistorical(input) ?? [];
12085
12948
  }
12949
+ /**
12950
+ * Batched cluster-wide track listing — one merged (`lastSeen` DESC,
12951
+ * `trackId` DESC) page across the requested devices with a stable opaque
12952
+ * cursor. Replaces the per-camera `listTracks` fan-out for the events page
12953
+ * first paint + the reel. See `TrackStore.queryRecent` for the per-device
12954
+ * indexed page + k-way merge and the cursor encoding.
12955
+ */
12956
+ async listRecentTracks(input) {
12957
+ return this.trackStore?.queryRecent(input) ?? {
12958
+ tracks: [],
12959
+ nextCursor: null
12960
+ };
12961
+ }
12962
+ /**
12963
+ * Every event kind the device can produce: built-ins (motion + audio),
12964
+ * detection classes actually observed on the device, and sensor kinds
12965
+ * from LINKED devices (device-manager `getLinkedDevices`, binding-driven
12966
+ * per linked device). Degrades to the built-ins on error.
12967
+ */
12968
+ async listEventKinds(input) {
12969
+ const api = this.ctx.api;
12970
+ return composeEventKinds({
12971
+ linkedDevices: { getLinkedDevices: (i) => api.deviceManager.getLinkedDevices.query(i) },
12972
+ bindings: { getBindings: (i) => api.deviceManager.getBindings.query(i) },
12973
+ observedClassNames: async (deviceId) => this.trackStore?.observedClassNames(deviceId) ?? [],
12974
+ onError: (scope, err) => this.ctx.logger.warn("listEventKinds: partial compose", {
12975
+ tags: { deviceId: input.deviceId },
12976
+ meta: {
12977
+ scope,
12978
+ error: errMsg(err)
12979
+ }
12980
+ })
12981
+ }, input.deviceId);
12982
+ }
12983
+ /** Per-camera sensor-event history (state changes of linked devices). */
12984
+ async getSensorEvents(input) {
12985
+ return this.sensorEventStore?.query(input) ?? [];
12986
+ }
12987
+ /**
12988
+ * Sensor-event ingest handler — `DeviceStateChanged` of a device exposing a
12989
+ * mapped sensor cap. Resolves the linked-camera set through the TTL cache
12990
+ * and inserts ONE row per linked camera. Best-effort (telemetry-lossy).
12991
+ */
12992
+ async handleSensorStateChanged(data, timestamp) {
12993
+ const store = this.sensorEventStore;
12994
+ const cache = this.linkedCamerasCache;
12995
+ if (store === null || cache === null) return;
12996
+ try {
12997
+ await ingestSensorStateChange({
12998
+ sink: store,
12999
+ cache
13000
+ }, data, timestamp);
13001
+ } catch (err) {
13002
+ this.ctx.logger.warn("sensor-event ingest failed", {
13003
+ tags: { deviceId: data.deviceId },
13004
+ meta: {
13005
+ capName: data.capName,
13006
+ error: errMsg(err)
13007
+ }
13008
+ });
13009
+ }
13010
+ }
12086
13011
  async clearTracks(input) {
12087
13012
  this.trackStore?.clearDevice(input.deviceId);
12088
13013
  this.stationaryRegistry?.clearDevice(input.deviceId);