fix(sfm): a download never downgrades a stored event -- an existing row is returned, nothing written

THOR's watcher (and Blastware's ACH) store every event through
/db/import/idf_file and /db/import/blastware_file, with the paired report.
Storing a download of the same event afterwards overwrote that richer copy:
on Series 4 the file, .h5 and sidecar were rewritten (extensions dropped);
on Series 3 the row lost PVS, project strings and ZC frequencies and was
repointed at a second, review-less file set.

persist_download now looks up (serial, timestamp) first.  A row there is
returned as it stands -- inserted false, detail.already_stored true -- with
no store call, no insert_events and no sidecar write.  Never merge.

- DownloadedEvent gains `timestamp` (the unit's own event time) and
  `record` (Series 4's 0x0C fields); both adapters set them.
- The Series-3 `event is None` guard now runs before backfill/save, with a
  message that is true there ("nothing was stored").
- Tests build the watcher's state exactly as the import endpoints do and
  prove the row dict, the sidecar bytes and the file mtimes are unchanged.

No schema change, no migration, no codec change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-10-21 17:35:23 -04:00
1 parent ddb22d9624
commit ef096e3082
9 files changed
+178 -22

No files matched your search

+12 -4
View File
@@ -2,6 +2,7 @@
series; anything only one series has goes under `detail`."""
from __future__ import annotations
import datetime
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
@@ -102,9 +103,9 @@ class EventDownload:
series: int
db_event_id: Optional[str]
stored_filename: str
filesize: int
sha256: Optional[str]
inserted: bool # False: already stored; the row was refreshed
filesize: Optional[int] # None only for a stored row with no file recorded
sha256: Optional[str] # None when the event was already stored
inserted: bool # False: already stored; nothing was written (detail.already_stored)
verified: Optional[bool] # True: decode self-check passed; None: no check exists
detail: Dict[str, Any] = field(default_factory=dict)
@@ -113,7 +114,12 @@ class EventDownload:
class DownloadedEvent:
"""The device side's result, before SFM stores it. Not an HTTP shape: it
carries the payload -- a Series-3 Event + its 5A frames + the compliance
config, or a Series-4 file's bytes -- to sfm.event_ingest."""
config, or a Series-4 file's bytes -- to sfm.event_ingest.
`timestamp` is the event's time as the unit reported it: the store looks up
an existing row by (serial, timestamp) before writing anything. `record` is
what the unit's own event record said (Series 4's 0x0C), for the fields the
file alone does not give."""
event_id: str
serial: str
series: int
@@ -123,4 +129,6 @@ class DownloadedEvent:
event: Any = field(default=None, repr=False)
a5_frames: Optional[list] = field(default=None, repr=False)
compliance: Any = field(default=None, repr=False)
timestamp: Optional[datetime.datetime] = None
record: Dict[str, Any] = field(default_factory=dict)
detail: Dict[str, Any] = field(default_factory=dict)
+2 -1
View File
@@ -204,6 +204,7 @@ class Series3Adapter:
return DownloadedEvent(
event_id=make_event_id(serial, key, found), serial=serial, series=3,
filename=filename, verified=None, event=ev, a5_frames=ev._a5_frames,
compliance=info.compliance_config, detail={"a5_frames": len(ev._a5_frames)},
compliance=info.compliance_config, timestamp=_s3_dt(ev),
detail={"a5_frames": len(ev._a5_frames)},
)
return self._run(address, op, timeout=S3_EVENT_TIMEOUT)
+4 -1
View File
@@ -287,6 +287,9 @@ def s4_download_event(client: Any, serial: str, key: bytes, stamp: Optional[str]
return DownloadedEvent(
event_id=make_event_id(serial, key, found), serial=serial, series=4,
filename=ref.filename, verified=True if err is not None else None,
idf_bytes=blob, detail={"size_bytes": ref.size, "pvs_error": err},
idf_bytes=blob, timestamp=ref.timestamp,
record={"peak_vector_sum_ips": ref.peak_vector_sum_ips,
"sensor_location": ref.sensor_location, "setup": ref.setup},
detail={"size_bytes": ref.size, "pvs_error": err},
)
raise EventNotFound(f"no event {key.hex()} on {serial} -- list the events again")