correlator: give the LLM tier what it needs to link, stop it defaulting to "new" (#115) #116

Merged
logan merged 2 commits from fix/tiebreaker-manufactures-incidents into main 2026-09-07 16:59:46 -04:00
3 changed files with 103 additions and 10 deletions
Showing only changes of commit 3a944f35c1 - Show all commits
@@ -108,16 +108,31 @@ _ROAD_RE = re.compile(
) )
# Street-type synonyms collapsed to one token so "Mohegan Park Avenue" and
# "Mohegan Park Ave" produce the same road id (server-26#115 — that one
# difference was splitting a car-alarm incident into two).
_ROAD_SUFFIX_CANON = {
"avenue": "ave", "street": "st", "road": "rd", "drive": "dr",
"boulevard": "blvd", "lane": "ln", "court": "ct", "place": "pl",
"highway": "hwy", "parkway": "pkwy",
}
def _extract_road_ids(text: str) -> set[str]: def _extract_road_ids(text: str) -> set[str]:
""" """
Extract normalised road/route identifiers from a location string. Extract normalised road/route identifiers from a location string.
e.g. "suspect east on Route 202" → {"route 202"} e.g. "suspect east on Route 202" → {"route 202"}
"at Main Street and Oak Ave" → {"main street", "oak ave"} "at Main Street and Oak Ave" → {"main st", "oak ave"}
""" """
return { ids: set[str] = set()
re.sub(r"[\s.\-]+", " ", m.group().lower()).strip() for m in _ROAD_RE.finditer(text):
for m in _ROAD_RE.finditer(text) key = re.sub(r"[\s.\-]+", " ", m.group().lower()).strip()
} parts = key.split()
if parts and parts[-1] in _ROAD_SUFFIX_CANON:
parts[-1] = _ROAD_SUFFIX_CANON[parts[-1]]
key = " ".join(parts)
ids.add(key)
return ids
def _location_mentions_road_overlap(new_location: str, inc_mentions: list[str]) -> bool: def _location_mentions_road_overlap(new_location: str, inc_mentions: list[str]) -> bool:
+29 -5
View File
@@ -45,7 +45,18 @@ def _fmt_idle(inc: dict, now: datetime) -> str:
def _inc_summary(inc: dict, now: datetime) -> str: def _inc_summary(inc: dict, now: datetime) -> str:
# server-26#115: the model was given no title and no talkgroup, so it
# could not tell that "car alarms, Mohegan Park Ave" and "car alarms,
# Mohegan Park Avenue" on the same channel were one incident — it defaulted
# to "new". Title is the single strongest human-readable signal for "is
# this the same event"; talkgroup is what makes same-channel continuation
# obvious.
parts = [f"id:{inc['incident_id']}", f"type:{inc.get('type') or '?'}"] parts = [f"id:{inc['incident_id']}", f"type:{inc.get('type') or '?'}"]
tgs = inc.get("talkgroup_ids") or []
if tgs:
parts.append(f"tg:[{', '.join(str(t) for t in tgs[:3])}]")
if inc.get("title"):
parts.append(f"title:{inc['title']!r}")
if inc.get("location"): if inc.get("location"):
parts.append(f"loc:{inc['location']}") parts.append(f"loc:{inc['location']}")
units = inc.get("units") or [] units = inc.get("units") or []
@@ -88,11 +99,24 @@ def _call_block(ctx: dict) -> str:
_SCHEMA = '{"action": "link" | "new" | "orphan", "incident_id": "<id_string or null>", "reasoning": "<one sentence>"}' _SCHEMA = '{"action": "link" | "new" | "orphan", "incident_id": "<id_string or null>", "reasoning": "<one sentence>"}'
_RULES = """ _RULES = """
Rules: Rules (this system OVER-SPLITS — a real incident routinely gets shattered into
- "link" only with clear positive evidence: same units, same geocoded location, or semantically identical scene on the same talkgroup within the last few minutes. 5-10 duplicates. A wrong link is cheap; a duplicate incident is the failure
- A call on a DIFFERENT talkgroup than an incident requires unit overlap or geocoded location match — topic similarity alone is not enough. mode. Bias accordingly.):
- "new" only if the call has a clear incident_type AND describes a distinct, identifiable scene. - Prefer "link" when the call plausibly continues a recent incident ON THE SAME
- "orphan" when in doubt — conservative is always correct. TALKGROUP: same or overlapping units, the same or an adjacent location (treat
"Ave"/"Avenue", "St"/"Street", "Rd"/"Road" as identical; a house number plus
the same street is the same place), the same subject/vehicle/case number, or a
follow-up beat ("units clearing", "negative contact", "tow en route", "event
number 214-201", a status update) to an incident that is only a few minutes
idle. The bar for "link" on the same talkgroup is LOW.
- Reserve "new" for a call that clearly describes a DIFFERENT event from every
recent incident — a different place, different units, and a different subject,
not merely a different transmission about the same job.
- "orphan" a call that is not an incident at all: radio checks, roll call,
a unit marking on/off duty or 10-8/10-98, mileage/log entries, a bare
acknowledgement. Do not open a "new" incident for these.
- A call on a DIFFERENT talkgroup than an incident still requires unit overlap
or a geocoded/location match — topic similarity alone is not enough there.
- Do NOT link just because both calls involve police or both mention a road. - Do NOT link just because both calls involve police or both mention a road.
""" """
+54
View File
@@ -0,0 +1,54 @@
"""
server-26#115 — the tiebreaker manufactured incidents because it was blind to
what would tell it two incidents are one.
Two low-risk supports for the reframed prompt:
1. `_extract_road_ids` collapses street-type synonyms, so "Mohegan Park Ave"
and "Mohegan Park Avenue" share a road id (they were splitting one
car-alarm incident into two).
2. `_inc_summary` now carries the incident title and talkgroup, the two
signals the model needs to recognise a same-channel continuation.
"""
from datetime import datetime, timezone
from app.internal.incident_correlator import (
_extract_road_ids, _location_mentions_road_overlap,
)
from app.internal.llm_correlator import _inc_summary
NOW = datetime(2026, 9, 7, 8, 0, 0, tzinfo=timezone.utc)
def test_avenue_and_ave_are_the_same_road_id():
assert _extract_road_ids("Mohegan Park Avenue") == _extract_road_ids("Mohegan Park Ave")
assert _extract_road_ids("191 Broadway Street") == _extract_road_ids("191 Broadway St")
assert _extract_road_ids("North State Road") == _extract_road_ids("North State Rd")
def test_road_overlap_matches_across_the_synonym():
assert _location_mentions_road_overlap("multiple car alarms Mohegan Park Avenue",
["patrol to Mohegan Park Ave"]) is True
# still discriminates genuinely different streets
assert _location_mentions_road_overlap("Oak Avenue", ["Elm Avenue"]) is False
def test_inc_summary_carries_title_and_talkgroup():
s = _inc_summary({
"incident_id": "abc123",
"type": "police",
"talkgroup_ids": [9560],
"title": "Nuisance Alarm at Mohegan Park Ave",
"location": "Mohegan Park Ave",
"units": ["Headquarters"],
"tags": ["car-alarm"],
"updated_at": NOW.isoformat(),
}, NOW)
assert "title:'Nuisance Alarm at Mohegan Park Ave'" in s
assert "tg:[9560]" in s
assert "id:abc123" in s
def test_inc_summary_omits_missing_optional_fields():
s = _inc_summary({"incident_id": "x", "updated_at": NOW.isoformat()}, NOW)
assert "title:" not in s and "tg:" not in s and "loc:" not in s
assert s.startswith("id:x")