upload: correlate late uploads on the call's own clock; make /upload idempotent on call_id (server-26#301, server-26#302)
The edge node now spools and retries uploads for up to 72h. #301: when a call's started_at is more than late_upload_anchor_minutes behind the real clock, extraction+correlation run under clock.pin(started_at) (the replay mechanism), so recency gates and every timestamp written are on the call's timeline. On-time uploads are unchanged. Also applies to /calls/{id}/reprocess, which shares _run_intelligence_pipeline. #302: an upload for a call_id whose audio is already stored returns the stored URL without rewriting GCS or re-running Whisper/extraction/ correlation. Not atomic: two concurrent retries can both pass the check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
3246fc97b0
commit
ec35d80580
@@ -147,6 +147,10 @@ class Settings(BaseSettings):
|
||||
# location_proximity_km the slow path accepts). The LLM tier had no
|
||||
# distance check at all, which is how one incident drew a 37 km path.
|
||||
incident_max_spread_km: float = 2.0
|
||||
# server-26#301: an upload arriving more than this long after the call
|
||||
# started is correlated as of its started_at (clock pin), not now. Well
|
||||
# above normal upload + STT lag so on-time calls are never back-dated.
|
||||
late_upload_anchor_minutes: int = 10
|
||||
location_proximity_km: float = 0.5 # radius for location-proximity matching
|
||||
geocode_max_km: float = 40.0 # reject geocode results farther than this from the node
|
||||
incident_auto_resolve_minutes: int = 90 # auto-resolve after N minutes with no new calls (major / unknown severity)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import secrets
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, BackgroundTasks, UploadFile, File, Form, HTTPException, Security
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
@@ -66,6 +67,24 @@ async def upload_call_audio(
|
||||
if len(data) > settings.upload_max_bytes:
|
||||
raise HTTPException(413, f"File too large (max {settings.upload_max_bytes // (1024*1024)} MB).")
|
||||
|
||||
# server-26#302: the edge node retries uploads (spooled for up to 72h), and
|
||||
# a retry arrives whenever the node lost the response to an upload C2 had
|
||||
# already stored. call_id is the idempotency key: audio already stored
|
||||
# means this upload was already processed, so
|
||||
# re-running Whisper/extraction/correlation would double the AI spend and
|
||||
# double-link the call (the server-26#131 shape). Not atomic: two retries
|
||||
# racing inside one request's lifetime can both pass this check.
|
||||
# Keyed on stored audio only: the pipeline marker alone would refuse a
|
||||
# retry forever (and hand the node a null URL to keep retrying) after a
|
||||
# pipeline crash; such a call is recovered by the sweep or a reprocess.
|
||||
existing = await fstore.doc_get("calls", call_id) or {}
|
||||
if existing.get("audio_gcs_uri"):
|
||||
logger.info(
|
||||
f"Upload for call {call_id} from {node_id} is a retry of one already "
|
||||
f"processed — returning the stored audio, pipeline not re-run"
|
||||
)
|
||||
return {"url": existing.get("audio_gcs_uri"), "already_uploaded": True}
|
||||
|
||||
gcs_uri = await upload_audio(data, file.filename or "", call_id=call_id)
|
||||
|
||||
if gcs_uri:
|
||||
@@ -456,6 +475,33 @@ async def _extract_and_correlate(
|
||||
return incident_ids, all_tags, scenes
|
||||
|
||||
|
||||
def _late_upload_anchor(call_doc: Optional[dict]) -> Optional[datetime]:
|
||||
"""
|
||||
The call's own started_at when its upload is late (server-26#301), else
|
||||
None. "Late" is `late_upload_anchor_minutes` behind the real clock: an
|
||||
on-time call keeps being judged against now exactly as before, so the live
|
||||
path's recency windows are unchanged; only a spooled/retried upload is
|
||||
back-dated. A started_at in the future or unparseable is ignored.
|
||||
"""
|
||||
raw = (call_doc or {}).get("started_at")
|
||||
if not raw:
|
||||
return None
|
||||
if isinstance(raw, datetime): # mqtt_handler stores a native timestamp
|
||||
started = raw
|
||||
else:
|
||||
try:
|
||||
started = datetime.fromisoformat(str(raw).replace("Z", "+00:00"))
|
||||
except ValueError:
|
||||
return None
|
||||
if started.tzinfo is None:
|
||||
started = started.replace(tzinfo=timezone.utc)
|
||||
lag = clock.now() - started
|
||||
if lag > timedelta(minutes=settings.late_upload_anchor_minutes):
|
||||
logger.info(f"Late upload: call started {lag} ago — correlating as of {started.isoformat()}")
|
||||
return started
|
||||
return None
|
||||
|
||||
|
||||
async def _run_intelligence_pipeline(
|
||||
call_id: str,
|
||||
node_id: str,
|
||||
@@ -530,15 +576,27 @@ async def _run_intelligence_pipeline(
|
||||
incident_ids: list[str] = []
|
||||
all_tags: list[str] = []
|
||||
if _flag("correlation_enabled"):
|
||||
incident_ids, all_tags, _ = await _extract_and_correlate(
|
||||
call_id=call_id,
|
||||
node_id=node_id,
|
||||
system_id=system_id,
|
||||
talkgroup_id=talkgroup_id,
|
||||
talkgroup_name=talkgroup_name,
|
||||
transcript=transcript,
|
||||
segments=segments,
|
||||
)
|
||||
# server-26#301: a late upload (the node spools and retries for up to
|
||||
# 72h) is correlated as of when the call happened, not as of now — the
|
||||
# same clock pin replay uses, so every recency gate and every
|
||||
# started_at/updated_at/resolved_at it writes is on the call's own
|
||||
# timeline. Without it a call hours late linked into whatever incident
|
||||
# was live on its talkgroup now, or opened one at the wrong time.
|
||||
anchor = _late_upload_anchor(_call_doc)
|
||||
tok = clock.pin(anchor) if anchor else None
|
||||
try:
|
||||
incident_ids, all_tags, _ = await _extract_and_correlate(
|
||||
call_id=call_id,
|
||||
node_id=node_id,
|
||||
system_id=system_id,
|
||||
talkgroup_id=talkgroup_id,
|
||||
talkgroup_name=talkgroup_name,
|
||||
transcript=transcript,
|
||||
segments=segments,
|
||||
)
|
||||
finally:
|
||||
if tok is not None:
|
||||
clock.unpin(tok)
|
||||
else:
|
||||
scope = "globally" if not flags["correlation_enabled"] else f"system {system_id}"
|
||||
logger.info(f"Correlation disabled ({scope}) — skipping scene extraction and correlation for call {call_id}")
|
||||
|
||||
Reference in New Issue
Block a user