diff --git a/CLAUDE.md b/CLAUDE.md index 6080f7f..7e17c14 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -228,9 +228,9 @@ Read this first when picking the project back up. audit `sent: null` (#15), and page polish. **Batch 1 fixed (2026-10-29):** #1 (`b320a53`, settings read after unescaping: mode −7, rate −6, interval −4, record time +6), #9 (`ce441a0`, one reconnect for - the read-back), #8 (`9492e49`, downloads stored through `save_imported_bw` with - the unit's mic overlaid, its peaks/vector sum too unless Normal range is confirmed - -- since 2026-10-31, after the checker's histogram-PVS fix -- and its range passed). #2 came with the + the read-back), #8 (`9492e49`, downloads stored through `save_imported_bw`; since + 2026-11-02 everything comes from the file -- peaks, vector sum, mic and the range + it was recorded at -- and the unit's 0C summary is only the download check). #2 came with the `dev` merge. **#15 retracted** -- `sent` NULL is "unknown", by design. Final review fixes: `dbd8a91` (wake before the reconnect's identity read), `3a1d34a` ("already stored" by Blastware name too), `a72d3a9` (the 1A write diff --git a/docs/superpowers/specs/2026-10-20-sfm-series4-parity-design.md b/docs/superpowers/specs/2026-10-20-sfm-series4-parity-design.md index 5f2a769..c72d3a1 100644 --- a/docs/superpowers/specs/2026-10-20-sfm-series4-parity-design.md +++ b/docs/superpowers/specs/2026-10-20-sfm-series4-parity-design.md @@ -685,12 +685,15 @@ a short plan; #14 waits for the line-hold decision above. 7. `/db/events` 500 "database is locked" on the first page load against a new DB. 8. ✅ **Fixed `9492e49`**, plus `3a1d34a` ("already stored" also matches the Blastware name: a histogram's row sits a second off its 0C time). The download carries the assembled Blastware file and is - stored through `save_imported_bw` (`source.kind` "sfm-live"), and its range scales - the samples. The unit's 0C mic is overlaid (the file reads it as 0). Its channel - peaks and vector sum were overlaid too until 2026-10-31; since the checker's - histogram-PVS fix (625bc39, merged `6945009`) the file's are taken when the unit - confirms Normal range, and the unit's stand for Sensitive or an unread range (the - file reader assumes Normal: 8x off). The label came with #41 (`a1d2b61`). + stored through `save_imported_bw` (`source.kind` "sfm-live"). Since 2026-11-02 + (after the checker's fixes for the histogram vector sum 625bc39, the mic psi + 5644dbd and the file's geophone range 4844135) the row and samples are the + file's: peaks, vector sum, mic, and the range the event was recorded at (the live + 1A is the unit's setting *now*, a fallback only when the file's range does not + read). The unit's 0C summary is the download check, and fills in peaks only when + the file's range does not read and the unit is not confirmed Normal. Until then + the unit's 0C values were overlaid (`9492e49`, narrowed `0a87d85`). The label + came with #41 (`a1d2b61`). **S3 downloads are stored from the live waveform decoder** (`store.save`), not the file reader that Series 4 uses (`save_imported_idf`): a histogram gets no samples and is labelled Waveform. Fix: store S3 downloads through the @@ -716,12 +719,13 @@ a short plan; #14 waits for the line-hold decision above. ring instead of its ack is exactly unknown, and the row's `error` says it was sent. 16. Page: a 504 on a unit THOR holds should say "connected, but no reply -- another program may be holding this unit's line". -17. **Found by the Batch 1 review (open):** a MiniMate downloaded *while monitoring* - answers 1A with its device-info block, so no range is read and the stored - `.h5` samples assume Normal -- 8x off on a sensitive-range unit (the row's - peaks are the unit's, overlaid). Pre-existing on the old path too. The same - reply decodes a setup name of `:S338.17`. Live S3 downloads also no longer - keep the `.a5.pkl` wire record. +17. ✅ **Range part fixed 2026-11-02:** a MiniMate downloaded *while monitoring* + answers 1A with its device-info block, so no range was read and the stored + `.h5` samples assumed Normal -- 8x off on a sensitive-range unit. The range now + comes from the event file itself (measured: K557J1KB.RQ0W stores 0.019 / 0.055 / + 0.019, Blastware's report, with the settings unread). Still open: the same reply + decodes a setup name of `:S338.17`, and live S3 downloads no longer keep the + `.a5.pkl` wire record. - **Same-second collisions (SDM).** Two events of one unit in the same second collide on (serial, timestamp) — a DB-wide dedup property, not 1b's. - **423 without `Retry-After` (1c).** Add `Retry-After` to the 423 a request gets while a download holds the unit. - **A lost `0C` during a Series-3 list (1c).** The strict walk raises on a lost `0A` diff --git a/sfm/event_ingest.py b/sfm/event_ingest.py index 64a2bf9..128ebce 100644 --- a/sfm/event_ingest.py +++ b/sfm/event_ingest.py @@ -66,36 +66,41 @@ def _already_stored(dl: DownloadedEvent, row: dict) -> EventDownload: ) -def _overlay_s3_record(ev: Any, unit_peaks: Any, geo_range: Any) -> None: - """The unit's own 0C values over the file's, where the file's are not yet right. +def _file_geo_range(bw_bytes: bytes) -> Any: + """The geophone range a Blastware file was recorded at (0 Normal, 1 Sensitive), + or None when it does not read -- or the file does not decode at all.""" + from minimateplus import event_file_io + try: + return event_file_io.decode_blastware_bytes(bw_bytes).geo_range + except Exception: # noqa: BLE001 -- the store reports it + return None - * Mic: always the unit's -- the file reads it as 0 (its psi conversion is an - open checker finding). - * Channel peaks and vector sum: the file's when the unit's settings confirm - Normal range (`geo_range` 0). The file reads them as the unit stored them - -- K441LTON.900: 0.200 / 0.385 / 0.255 and 0.385, as Blastware's report, - since the decode checker's fix (625bc39, 2026-10-31) -- while the 0C summary - is read at fixed offsets and can shift (BE9441 read 0 and -0.0 for vector - sums, bench #4). But the file reader computes every peak as if the range - were Normal, so a Sensitive unit's come out 8x off (the checker's 33 - waveforms): for Sensitive, or a range not read (a monitoring unit's 1A - carries no settings), the unit's own peaks stand, as before -- a vector - sum below the largest channel peak is impossible and is not kept. - The 0C peaks remain the download *check* (verify_s3_download) either way. +def _overlay_s3_record(ev: Any, unit_peaks: Any, unit_range: Any) -> None: + """The unit's own 0C peaks over the file's -- only where the file cannot be trusted. + + The file reads everything Blastware reports (checked on 70,042 events): the + channel peaks and the vector sum, scaled by the geophone range in its own + setup block (4844135), and the mic (5644dbd). The 0C summary is read at + fixed offsets and can shift -- BE9441's read 0 and -0.0 for vector sums + (bench #4) -- so the file's values stand. + + Except when the file's range does not read (`ev.geo_range` None; never seen + in DL2): the decoder then assumes Normal, 8x off for a Sensitive unit. Then + the unit's settings decide: Normal confirmed (`unit_range` 0) keeps the + file's peaks; Sensitive or unread keeps the unit's own, as before -- a vector + sum below the largest channel peak is impossible and is not kept. The mic + is the file's either way: its scale does not depend on the geophone range. """ + if unit_peaks is None or getattr(ev, "geo_range", None) is not None: + return + if unit_range in (0, "normal"): + return from minimateplus.models import PeakValues - if unit_peaks is None: - return if ev.peak_values is None: ev.peak_values = PeakValues() pv = ev.peak_values - micl = getattr(unit_peaks, "micl", None) - if micl is not None: - pv.micl = micl - if geo_range in (0, "normal"): - return for name in ("tran", "vert", "long"): value = getattr(unit_peaks, name, None) if value is not None: @@ -149,18 +154,23 @@ def persist_download(dl: DownloadedEvent, *, store: Any, db: Any) -> EventDownlo family = "series4" else: # The same path production's watcher uses for Blastware's own files: the - # event is decoded from the file (samples, histogram, setup-block sample - # rate and record time), not from the live 5A decode. The unit's 0C mic - # goes over the file's, and its peaks too unless Normal range is - # confirmed (_overlay_s3_record). + # event is decoded from the file -- samples, histogram, peaks, vector sum, + # mic, sample rate, record time and the geophone range it was recorded + # at -- not from the live 5A decode. The unit's 0C summary is the + # download check (verify_s3_download); it fills in only when the file's + # range does not read (_overlay_s3_record). if not dl.bw_bytes: raise IngestError(f"{dl.filename}: the download carried no event file; nothing was stored") - geo_range = getattr(dl.compliance, "geo_range", None) if dl.compliance else None + unit_range = getattr(dl.compliance, "geo_range", None) if dl.compliance else None + # The file's range is the setting when it recorded; the live 1A is the + # unit's setting now, which may have changed since -- the fallback only. + # None lets the store scale by the file's own range. ev, rec = store.save_imported_bw(dl.bw_bytes, source_path=Path(dl.filename), serial_hint=dl.serial, source_kind="sfm-live", - geo_range=geo_range if geo_range is not None else "normal") + geo_range=None if _file_geo_range(dl.bw_bytes) is not None + else unit_range) if ev is not None and dl.event is not None: - _overlay_s3_record(ev, getattr(dl.event, "peak_values", None), geo_range) + _overlay_s3_record(ev, getattr(dl.event, "peak_values", None), unit_range) family = "series3" if ev is None or ev._waveform_key is None: raise IngestError(f"{dl.filename}: stored the file but could not build a database row") diff --git a/tests/test_event_ingest.py b/tests/test_event_ingest.py index 0d7021b..6e23fa0 100644 --- a/tests/test_event_ingest.py +++ b/tests/test_event_ingest.py @@ -296,9 +296,11 @@ def test_backfill_without_settings_changes_nothing(): assert ev.sample_rate is None -# The file path does not yet read a histogram's vector sum the way Blastware does -# (the checker's open finding), the mic peak, or a sensitive range, so a live -# download keeps the unit's own 0C values for those -- the Series-4 pattern. +# Since 2026-11-02 a live download stores what its own file says -- peaks, vector +# sum, mic, and the geophone range the event was recorded at -- and the unit's 0C +# summary is only the download check. The file's range is the setting when it +# recorded; the live 1A is the unit's setting now, which may have changed since. +# Only when the file's range does not read (never seen in DL2) does the 1A decide. def _with_unit_peaks(dl, *, tran, vert, long, micl, pvs): from minimateplus.models import PeakValues @@ -313,24 +315,99 @@ def _range(geo_range): return cc -def test_a_series3_download_takes_its_peaks_from_the_file_and_its_mic_from_the_unit(sdm): - # 2026-10-31: the file now reads a histogram's vector sum as the unit stored it - # (0.385 here, as Blastware's report), so the row takes the file's peaks and - # vector sum. The unit's 0C summary is read at fixed offsets and can shift - # (bench #4 read 0 / -0.0); these believable-but-wrong values must not win. - # The mic still comes from the unit: the file reads it as 0. +GEO_FIX = Path(__file__).parent / "fixtures" / "geo-range-11-01-26" +WRONG = dict(tran=0.21, vert=0.36, long=0.27, micl=0.0009, pvs=0.40) # believable, wrong; clears the old guard + + +def _download_from(path, serial): + from minimateplus.blastware_file import blastware_filename_time + when = blastware_filename_time(path.name) + return DownloadedEvent(event_id=f"{serial}:01110000:{when:%Y%m%d%H%M%S}", serial=serial, series=3, + filename=path.name, verified=None, bw_bytes=path.read_bytes(), timestamp=when) + + +def _h5_peaks(store, serial, name): + from sfm import event_hdf5 + plot = event_hdf5.plot_json_from_hdf5(store.hdf5_path_for(serial, name)) + return tuple(max(abs(v) for v in plot["channels"][c]["values"]) for c in ("Tran", "Vert", "Long")) + + +def _file_range_unread(monkeypatch): + # A file whose range does not read: the decoder then assumes Normal. + # K441LTON.900 is a Normal file, so its peaks are what such a decode gives. + import minimateplus.event_file_io as efi + real = efi.decode_blastware_bytes + + def unread(raw, **kw): + ev = real(raw, **kw) + ev.geo_range = None + return ev + monkeypatch.setattr(efi, "decode_blastware_bytes", unread) + + +@pytest.mark.parametrize("compliance", [_range(0), _range(1), None], + ids=["unit-says-normal", "unit-says-sensitive", "settings-unread"]) +def test_a_series3_download_stores_what_its_file_says(sdm, compliance): + # K441LTON.900 was recorded in Normal range, whatever the unit is set to now. store, db = sdm dl = _with_unit_peaks(_hist_download("K441LTON.900", "0111f210", datetime.datetime(2026, 10, 26, 17, 0, 36)), - tran=0.21, vert=0.36, long=0.27, micl=0.00058, pvs=0.40) # clears the old guard - dl.compliance = _range(0) # Normal, confirmed by the unit + **WRONG) + dl.compliance = compliance row = db.get_event(persist_download(dl, store=store, db=db).db_event_id) - assert row["peak_vector_sum"] == pytest.approx(0.3852, abs=1e-4) # Blastware: 0.385 assert (row["tran_ppv"], row["vert_ppv"], row["long_ppv"]) == pytest.approx((0.2, 0.385, 0.255), abs=1e-4) - assert row["mic_ppv"] == pytest.approx(0.00058, abs=1e-6) + assert row["peak_vector_sum"] == pytest.approx(0.3852, abs=1e-4) # Blastware: 0.385 + assert row["mic_ppv"] == pytest.approx(5.80e-4, abs=2e-6) # Blastware: 106.0 dB(L) + assert _h5_peaks(store, "BE9441", "K441LTON.900")[1] == pytest.approx(0.385, abs=0.006) -def test_an_impossible_unit_vector_sum_is_not_kept(sdm): - # Bench finding #4: BE9441's 0C read -0.0 for a vector sum whose channels reach 1.08. +def test_a_sensitive_waveform_downloaded_with_its_settings_unread_is_blastwares(sdm): + # e.g. downloaded while monitoring (the 1A carries no settings). The samples + # were scaled as Normal before -- 8x: Vert read 0.44 in the .h5. + store, db = sdm + dl = _download_from(GEO_FIX / "K557J1KB.RQ0W", "BE9557") + row = db.get_event(persist_download(dl, store=store, db=db).db_event_id) + assert (row["tran_ppv"], row["vert_ppv"], row["long_ppv"]) == pytest.approx((0.019, 0.055, 0.019), abs=6e-4) + assert row["peak_vector_sum"] == pytest.approx(0.061, abs=6e-4) # Blastware's report + assert _h5_peaks(store, "BE9557", "K557J1KB.RQ0W") == pytest.approx((0.019, 0.055, 0.019), abs=7e-4) + + +def test_a_sensitive_histogram_downloaded_with_its_settings_unread_keeps_its_scale(sdm): + from minimateplus.event_file_io import decode_blastware_bytes + store, db = sdm + path = GEO_FIX / "M010HR6K.OA0H" + pv = decode_blastware_bytes(path.read_bytes()).peak_values + persist_download(_download_from(path, "BE11010"), store=store, db=db) + assert _h5_peaks(store, "BE11010", path.name) == pytest.approx((pv.tran, pv.vert, pv.long), abs=7e-4) + assert max(_h5_peaks(store, "BE11010", path.name)) < 0.02 # not 8x (Vert would read ~0.07) + + +def test_when_the_files_range_does_not_read_a_sensitive_units_own_peaks_stand(sdm, monkeypatch): + _file_range_unread(monkeypatch) + store, db = sdm + dl = _with_unit_peaks(_hist_download("K441LTON.900", "0111f210", datetime.datetime(2026, 10, 26, 17, 0, 36)), + tran=0.025, vert=0.048, long=0.032, micl=0.0009, pvs=0.0481) + dl.compliance = _range(1) + row = db.get_event(persist_download(dl, store=store, db=db).db_event_id) + assert (row["tran_ppv"], row["vert_ppv"], row["long_ppv"]) == pytest.approx((0.025, 0.048, 0.032), abs=1e-6) + assert row["peak_vector_sum"] == pytest.approx(0.0481, abs=1e-6) + assert row["mic_ppv"] == pytest.approx(5.80e-4, abs=2e-6) # the mic's scale is not the geophones' + assert _h5_peaks(store, "BE9441", "K441LTON.900")[1] == pytest.approx(0.385 / 8, abs=7e-4) + + +def test_when_the_files_range_does_not_read_and_the_unit_says_normal_the_files_peaks_stand(sdm, monkeypatch): + _file_range_unread(monkeypatch) + store, db = sdm + dl = _with_unit_peaks(_hist_download("K441LTON.900", "0111f210", datetime.datetime(2026, 10, 26, 17, 0, 36)), + **WRONG) + dl.compliance = _range(0) + row = db.get_event(persist_download(dl, store=store, db=db).db_event_id) + assert (row["vert_ppv"], row["peak_vector_sum"]) == pytest.approx((0.385, 0.3852), abs=1e-4) + + +def test_an_impossible_unit_vector_sum_is_not_kept(sdm, monkeypatch): + # Bench finding #4: BE9441's 0C read -0.0 for a vector sum whose channels reach + # 1.08. The unit's values are used only when neither range reads. + _file_range_unread(monkeypatch) store, db = sdm dl = _with_unit_peaks(_hist_download("K441LTOK.R10", "011106a0", datetime.datetime(2026, 10, 26, 16, 6, 37)), tran=0.25, vert=1.08, long=0.195, micl=0.0001, pvs=-0.0) @@ -421,21 +498,3 @@ def test_a_letterless_stored_name_still_matches_a_lettered_download(sdm): store, db = sdm _stored_as(store, db, "K441LTOK.QZ0") assert db.find_event_by_blastware_name("BE9441", "K441LTOK.QZ0H")["blastware_filename"] == "K441LTOK.QZ0" - - - -# The file reader computes every peak as if the unit were in Normal range (10 in/s); -# a Sensitive (1.25 in/s) unit's file peaks come out 8x off (the checker's 33 -# waveforms). So the file's peaks are taken only when the unit's settings confirm -# Normal -- the download check's rule; Sensitive or unread keeps the unit's own. - -@pytest.mark.parametrize("compliance", [_range(1), None], ids=["sensitive", "range-unread"]) -def test_without_a_confirmed_normal_range_the_units_peaks_stand(sdm, compliance): - store, db = sdm - dl = _with_unit_peaks(_hist_download("K441LTON.900", "0111f210", datetime.datetime(2026, 10, 26, 17, 0, 36)), - tran=0.025, vert=0.048, long=0.032, micl=0.00058, pvs=0.0481) - dl.compliance = compliance - row = db.get_event(persist_download(dl, store=store, db=db).db_event_id) - assert (row["tran_ppv"], row["vert_ppv"], row["long_ppv"]) == pytest.approx((0.025, 0.048, 0.032), abs=1e-6) - assert row["peak_vector_sum"] == pytest.approx(0.0481, abs=1e-6) - assert row["mic_ppv"] == pytest.approx(0.00058, abs=1e-6)