From d88c6273ec7d12cd3bea23b2a37b123d67c34a92 Mon Sep 17 00:00:00 2001 From: "Matt Johnson (via Claude)" Date: Wed, 10 Jun 2026 02:40:07 +0000 Subject: [PATCH] nws: prefix Update: when incoming alert supersedes a prior broadcast Add _is_update() helper that checks CAP references field against nws_alerts table. When any referenced CAP id has last_broadcast_at set, the wire gets an Update: prefix via _render(prefix=). Applied in both the new-alert and cold-start-race branches. References field shape: list of dicts with identifier key containing the superseded CAP id (urn:oid:...). Co-Authored-By: Claude Opus 4.6 --- meshai/central/nws_handler.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/meshai/central/nws_handler.py b/meshai/central/nws_handler.py index d644d18..05c86df 100644 --- a/meshai/central/nws_handler.py +++ b/meshai/central/nws_handler.py @@ -130,6 +130,24 @@ def _parse_motion(params: dict) -> tuple: def _now() -> int: return int(time.time()) +def _is_update(conn, d: dict) -> bool: + """Return True if any CAP id in `references` was previously broadcast.""" + refs = d.get("references") or [] + if not refs: + return False + ref_ids = [r["identifier"] for r in refs + if isinstance(r, dict) and r.get("identifier")] + if not ref_ids: + return False + placeholders = ",".join("?" * len(ref_ids)) + row = conn.execute( + f"SELECT 1 FROM nws_alerts WHERE event_id IN ({placeholders}) " + "AND last_broadcast_at IS NOT NULL LIMIT 1", + ref_ids, + ).fetchone() + return row is not None + + def _parse_iso(s: Optional[str]) -> Optional[int]: if not s: return None try: return int(datetime.fromisoformat(s.replace("Z", "+00:00")).timestamp()) @@ -270,19 +288,21 @@ def handle_nws(envelope: dict, subject: str, (cap_id, event_type, cap_severity, county, state, headline, description, expires_epoch, now, None), ) + _prefix = "Update" if _is_update(conn, d) else "" wire = _render(event_type=event_type, area_desc=area_desc, geocoder_city=ge.get("city"), county=county, state=state, expires_epoch=expires_epoch, lat=lat, lon=lon, now=now, - d=d) + prefix=_prefix, d=d) _attach_commit(data, cap_id=cap_id, event_log_row_id=log_id) return wire if row["last_broadcast_at"] is None: # Cold-start race: row exists but broadcast was previously dropped. + _prefix = "Update" if _is_update(conn, d) else "" wire = _render(event_type=event_type, area_desc=area_desc, geocoder_city=ge.get("city"), county=county, state=state, expires_epoch=expires_epoch, lat=lat, lon=lon, now=now, - d=d) + prefix=_prefix, d=d) _attach_commit(data, cap_id=cap_id, event_log_row_id=log_id) return wire