fix(dispatch): bucket failures by geo cell while the backend omits zone_id

Every booking.assignment_failed observed in production carries no zone_id, so
the per-zone failure counter and the once-a-day alert limit collapsed into a
single global "unknown" bucket — one alert per day for the whole country.

zone_key() prefers the backend's zone_id and falls back to a coarse lat/lon
grid cell (1 dp, ~11 km, DISPATCH_GEO_BUCKET_DP). The failure reason is now
logged too.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012AJLYcbTHCe45fyFnMfEin
This commit is contained in:
2026-09-22 16:42:15 +05:30
parent 5159c8d1a7
commit 008df49605
2 changed files with 50 additions and 2 deletions

View File

@@ -288,6 +288,29 @@ class TestRateLimitAndSinks(unittest.IsolatedAsyncioTestCase):
self.assertEqual(payload["proposed_action"], "escalate")
class TestZoneKey(unittest.IsolatedAsyncioTestCase):
"""The backend sends no zone_id today; without a fallback every failure in
the country shares one bucket and the daily alert limit becomes global."""
async def test_real_zone_id_wins(self):
from agents.dispatch_agent import zone_key
self.assertEqual(zone_key("hyderabad", 17.4, 78.4), "hyderabad")
async def test_missing_zone_falls_back_to_geo_cell(self):
from agents.dispatch_agent import zone_key
self.assertEqual(zone_key(None, 11.0182714, 76.9677744), "geo:11.0,77.0")
self.assertEqual(zone_key("unknown", 11.0182714, 76.9677744), "geo:11.0,77.0")
async def test_distant_pickups_get_different_buckets(self):
from agents.dispatch_agent import zone_key
self.assertNotEqual(zone_key(None, 11.01, 76.96), zone_key(None, 17.44, 78.39))
async def test_no_zone_and_no_coords_is_unknown(self):
from agents.dispatch_agent import zone_key
self.assertEqual(zone_key(None, None, None), "unknown")
self.assertEqual(zone_key("unknown", "bad", "coords"), "unknown")
class TestBindConsumer(unittest.IsolatedAsyncioTestCase):
async def test_binds_to_discovered_stream_without_creating(self):
js = FakeJS(stream_name="TRACKING")