GitHub Actions commited on
Commit
938f6c0
·
1 Parent(s): 78d1678

Deploy from GitHub main

Browse files
app/api/routes.py CHANGED
@@ -86,6 +86,11 @@ async def receive_whatsapp_webhook(
86
  background_tasks.add_task(
87
  container.conversation.handle_inbound_message, inbound
88
  )
 
 
 
 
 
89
 
90
  return WebhookAcceptedResponse(messages=len(messages))
91
 
 
86
  background_tasks.add_task(
87
  container.conversation.handle_inbound_message, inbound
88
  )
89
+ if messages:
90
+ # An inbound webhook means WhatsApp is reachable: flush any queued
91
+ # backlog so failed messages go out immediately. Scheduled last so a
92
+ # slow WhatsApp API never delays the reply.
93
+ background_tasks.add_task(container.whatsapp.flush_pending)
94
 
95
  return WebhookAcceptedResponse(messages=len(messages))
96
 
app/database/supabase.py CHANGED
@@ -1,5 +1,5 @@
1
  import logging
2
- from datetime import UTC, date, datetime, time
3
  from typing import Any
4
 
5
  from app.config import Settings
@@ -32,32 +32,39 @@ class SupabaseRepository:
32
  def __init__(self, client: Any) -> None:
33
  self.client = client
34
 
35
- async def enqueue_whatsapp_message(
36
  self,
37
  *,
38
  recipient: str,
39
- payload: dict[str, Any],
40
- ) -> dict[str, Any]:
41
- now = datetime.now(UTC).isoformat()
 
 
 
 
 
 
 
 
 
 
42
  response = await (
43
  self.client.table("whatsapp_outbox")
44
- .insert(
45
- {
46
- "recipient": recipient,
47
- "payload": payload,
48
- "status": "pending",
49
- "next_attempt_at": now,
50
- }
51
- )
52
  .select("*")
53
  .execute()
54
  )
55
- data = _response_data(response)
56
- return data[0] if isinstance(data, list) else data
 
 
 
57
 
58
  async def get_pending_whatsapp_message(
59
  self,
60
  *,
 
61
  newer_than: datetime | None,
62
  older_than: datetime | None,
63
  newest_first: bool,
@@ -66,6 +73,7 @@ class SupabaseRepository:
66
  self.client.table("whatsapp_outbox")
67
  .select("*")
68
  .eq("status", "pending")
 
69
  )
70
  if newer_than:
71
  query = query.gte("created_at", newer_than.isoformat())
@@ -80,11 +88,18 @@ class SupabaseRepository:
80
  data = _response_data(response) or []
81
  return data[0] if data else None
82
 
83
- async def has_sending_whatsapp_message(self) -> bool:
 
 
 
 
 
 
84
  response = await (
85
  self.client.table("whatsapp_outbox")
86
  .select("id")
87
- .eq("status", "sending")
 
88
  .limit(1)
89
  .execute()
90
  )
@@ -116,36 +131,13 @@ class SupabaseRepository:
116
  *,
117
  ignore_retry_time: bool = False,
118
  ) -> dict[str, Any] | None:
119
- attempts_response = await (
120
- self.client.table("whatsapp_outbox")
121
- .select("attempts")
122
- .eq("id", message_id)
123
- .eq("status", "pending")
124
- .maybe_single()
125
- .execute()
126
- )
127
- row = _response_data(attempts_response)
128
- if not row:
129
- return None
130
-
131
- now = datetime.now(UTC).isoformat()
132
- update = (
133
- self.client.table("whatsapp_outbox")
134
- .update(
135
- {
136
- "status": "sending",
137
- "attempts": int(row.get("attempts") or 0) + 1,
138
- "last_attempt_at": now,
139
- "locked_at": now,
140
- "last_error": None,
141
- }
142
- )
143
- .eq("id", message_id)
144
- .eq("status", "pending")
145
- )
146
- if not ignore_retry_time:
147
- update = update.lte("next_attempt_at", now)
148
- response = await update.select("*").execute()
149
  data = _response_data(response) or []
150
  return data[0] if data else None
151
 
@@ -1090,15 +1082,23 @@ class SupabaseRepository:
1090
  data = _response_data(response)
1091
  return data[0] if isinstance(data, list) else data
1092
 
1093
- async def count_trip_selections(self, trip_id: str) -> int:
1094
- response = await (
1095
- self.client.table("trip_selections")
1096
- .select("id", count="exact")
1097
- .eq("trip_id", trip_id)
1098
- .eq("status", "pending")
1099
- .execute()
1100
- )
1101
- return response.count if hasattr(response, "count") and response.count is not None else 0
 
 
 
 
 
 
 
 
1102
 
1103
 
1104
  def _driver_is_registered(trip: dict[str, Any]) -> bool:
 
1
  import logging
2
+ from datetime import UTC, date, datetime, time, timedelta
3
  from typing import Any
4
 
5
  from app.config import Settings
 
32
  def __init__(self, client: Any) -> None:
33
  self.client = client
34
 
35
+ async def enqueue_whatsapp_messages(
36
  self,
37
  *,
38
  recipient: str,
39
+ payloads: list[dict[str, Any]],
40
+ ) -> list[dict[str, Any]]:
41
+ base = datetime.now(UTC)
42
+ rows = [
43
+ {
44
+ "recipient": recipient,
45
+ "payload": payload,
46
+ "status": "pending",
47
+ "next_attempt_at": (base + timedelta(microseconds=index)).isoformat(),
48
+ "created_at": (base + timedelta(microseconds=index)).isoformat(),
49
+ }
50
+ for index, payload in enumerate(payloads)
51
+ ]
52
  response = await (
53
  self.client.table("whatsapp_outbox")
54
+ .insert(rows)
 
 
 
 
 
 
 
55
  .select("*")
56
  .execute()
57
  )
58
+ data = _response_data(response) or []
59
+ if not isinstance(data, list):
60
+ data = [data]
61
+ data.sort(key=lambda row: str(row.get("created_at") or ""))
62
+ return data
63
 
64
  async def get_pending_whatsapp_message(
65
  self,
66
  *,
67
+ recipient: str,
68
  newer_than: datetime | None,
69
  older_than: datetime | None,
70
  newest_first: bool,
 
73
  self.client.table("whatsapp_outbox")
74
  .select("*")
75
  .eq("status", "pending")
76
+ .eq("recipient", recipient)
77
  )
78
  if newer_than:
79
  query = query.gte("created_at", newer_than.isoformat())
 
88
  data = _response_data(response) or []
89
  return data[0] if data else None
90
 
91
+ async def list_pending_whatsapp_queue_heads(self) -> list[dict[str, Any]]:
92
+ response = await self.client.rpc(
93
+ "list_pending_whatsapp_queue_heads",
94
+ ).execute()
95
+ return _response_data(response) or []
96
+
97
+ async def has_pending_whatsapp_messages(self, recipient: str) -> bool:
98
  response = await (
99
  self.client.table("whatsapp_outbox")
100
  .select("id")
101
+ .eq("recipient", recipient)
102
+ .in_("status", ["pending", "sending"])
103
  .limit(1)
104
  .execute()
105
  )
 
131
  *,
132
  ignore_retry_time: bool = False,
133
  ) -> dict[str, Any] | None:
134
+ response = await self.client.rpc(
135
+ "claim_whatsapp_outbox_message",
136
+ {
137
+ "p_message_id": message_id,
138
+ "p_ignore_retry_time": ignore_retry_time,
139
+ },
140
+ ).execute()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  data = _response_data(response) or []
142
  return data[0] if data else None
143
 
 
1082
  data = _response_data(response)
1083
  return data[0] if isinstance(data, list) else data
1084
 
1085
+ async def count_pending_trip_selections(
1086
+ self,
1087
+ trip_ids: list[str],
1088
+ ) -> dict[str, int]:
1089
+ ids = [trip_id for trip_id in trip_ids if trip_id]
1090
+ if not ids:
1091
+ return {}
1092
+ response = await self.client.rpc(
1093
+ "count_pending_trip_selections",
1094
+ {"p_trip_ids": ids},
1095
+ ).execute()
1096
+ rows = _response_data(response) or []
1097
+ return {
1098
+ str(row["trip_id"]): int(row.get("selection_count") or 0)
1099
+ for row in rows
1100
+ if row.get("trip_id")
1101
+ }
1102
 
1103
 
1104
  def _driver_is_registered(trip: dict[str, Any]) -> bool:
app/main.py CHANGED
@@ -57,6 +57,9 @@ def create_app(
57
  await retry_worker
58
  except asyncio.CancelledError:
59
  pass
 
 
 
60
 
61
  app = FastAPI(title="FALZH API", version="0.1.0", lifespan=lifespan)
62
  app.include_router(router)
 
57
  await retry_worker
58
  except asyncio.CancelledError:
59
  pass
60
+ close_container = getattr(getattr(app.state, "container", None), "aclose", None)
61
+ if callable(close_container):
62
+ await close_container()
63
 
64
  app = FastAPI(title="FALZH API", version="0.1.0", lifespan=lifespan)
65
  app.include_router(router)
app/services/container.py CHANGED
@@ -71,3 +71,6 @@ class ServiceContainer:
71
  group_message=group_message,
72
  admin=admin,
73
  )
 
 
 
 
71
  group_message=group_message,
72
  admin=admin,
73
  )
74
+
75
+ async def aclose(self) -> None:
76
+ await self.whatsapp.aclose()
app/services/conversation_service.py CHANGED
@@ -148,7 +148,7 @@ class ConversationService:
148
  await self.repository.cancel_driver_trip(trip_id)
149
  await unindex_trip(repository=self.repository, trip_id=trip_id)
150
  reply = "تم حذف الرحلة بنجاح"
151
- await self._store_and_send_assistant_reply(
152
  customer,
153
  inbound.remoteJid,
154
  reply,
@@ -188,7 +188,7 @@ class ConversationService:
188
  registry=registry,
189
  )
190
  if reply:
191
- await self._store_and_send_assistant_reply(
192
  customer,
193
  inbound.remoteJid,
194
  reply,
@@ -258,7 +258,7 @@ class ConversationService:
258
 
259
  return reply
260
 
261
- async def _store_and_send_assistant_reply(
262
  self,
263
  customer: dict[str, Any],
264
  remoteJid: str,
@@ -266,13 +266,13 @@ class ConversationService:
266
  *,
267
  user_mode: UserMode,
268
  ) -> None:
 
269
  await self.repository.create_message(
270
  customer_id=str(customer["id"]),
271
  sender_type="assistant",
272
  message=reply,
273
  metadata={"provider_flow": "trip_interactive_reply", "user_mode": user_mode},
274
  )
275
- await self.whatsapp.send_text(remoteJid, reply)
276
 
277
  def _tool_registry(
278
  self,
 
148
  await self.repository.cancel_driver_trip(trip_id)
149
  await unindex_trip(repository=self.repository, trip_id=trip_id)
150
  reply = "تم حذف الرحلة بنجاح"
151
+ await self._send_and_store_assistant_reply(
152
  customer,
153
  inbound.remoteJid,
154
  reply,
 
188
  registry=registry,
189
  )
190
  if reply:
191
+ await self._send_and_store_assistant_reply(
192
  customer,
193
  inbound.remoteJid,
194
  reply,
 
258
 
259
  return reply
260
 
261
+ async def _send_and_store_assistant_reply(
262
  self,
263
  customer: dict[str, Any],
264
  remoteJid: str,
 
266
  *,
267
  user_mode: UserMode,
268
  ) -> None:
269
+ await self.whatsapp.send_text(remoteJid, reply)
270
  await self.repository.create_message(
271
  customer_id=str(customer["id"]),
272
  sender_type="assistant",
273
  message=reply,
274
  metadata={"provider_flow": "trip_interactive_reply", "user_mode": user_mode},
275
  )
 
276
 
277
  def _tool_registry(
278
  self,
app/tools/handlers.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import logging
2
  from decimal import Decimal, InvalidOperation
3
  from typing import Any
@@ -24,7 +25,7 @@ from app.utils.departure import (
24
  trip_satisfies_departure_request,
25
  )
26
  from app.utils.emoji import count_emojis
27
- from app.whatsapp.client import WhatsAppClientError, WhatsAppSender
28
  from app.whatsapp.trip_selection import format_trip_card
29
 
30
 
@@ -61,6 +62,46 @@ class FalzhToolHandlers:
61
  original_meta = original.get("metadata") or {}
62
  return original_meta.get("trip_id")
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  async def about_falzh(self, arguments: dict[str, Any]) -> ToolResult:
65
  query = str(arguments.get("query") or "").strip()
66
  if not query:
@@ -214,46 +255,29 @@ class FalzhToolHandlers:
214
  top_trips = filtered[:5]
215
 
216
  if top_trips:
 
 
 
 
217
  for trip_summary in top_trips:
218
  trip_id = trip_summary["trip_id"]
219
- selection_count = await self.repository.count_trip_selections(trip_id)
220
  trip_summary["selection_count"] = selection_count
221
- trip = next((t for t in trips if (t.get("trip_id") or t.get("id")) == trip_id), {})
 
 
 
222
  trip["selection_count"] = selection_count
223
- card = format_trip_card(trip)
224
- try:
225
- resp = await self.whatsapp.send_text(self.remoteJid, card)
226
- wam_id = resp.get("messages", [{}])[0].get("id")
227
- if wam_id:
228
- await self.repository.create_message(
229
- customer_id=str(self.customer["id"]),
230
- sender_type="assistant",
231
- message=card,
232
- whatsapp_message_id=wam_id,
233
- metadata={"trip_id": trip_id, "type": "trip_card"},
234
- )
235
- elif resp.get("outbox_id"):
236
- await self.repository.create_message(
237
- customer_id=str(self.customer["id"]),
238
- sender_type="assistant",
239
- message=card,
240
- metadata={
241
- "trip_id": trip_id,
242
- "type": "trip_card",
243
- "outbox_id": resp["outbox_id"],
244
- },
245
- )
246
- except WhatsAppClientError:
247
- logger.warning("Failed to send trip card for trip %s", trip_id)
248
-
249
- prompt = "يرجى الرد على إحدى بطاقات الرحلات أعلاه لاختيار رحلتك"
250
- await self.whatsapp.send_text(self.remoteJid, prompt)
251
- await self.repository.create_message(
252
- customer_id=str(self.customer["id"]),
253
- sender_type="assistant",
254
- message=prompt,
255
- metadata={"type": "trip_selection_prompt"},
256
  )
 
257
 
258
  return ToolResult(
259
  ok=True,
@@ -463,44 +487,23 @@ class FalzhToolHandlers:
463
  },
464
  )
465
 
466
- for trip in trips[:5]:
 
 
 
 
 
467
  trip_id = trip.get("trip_id") or trip.get("id")
468
- selection_count = await self.repository.count_trip_selections(trip_id)
469
  trip["selection_count"] = selection_count
470
- card = format_trip_card(trip)
471
- try:
472
- resp = await self.whatsapp.send_text(self.remoteJid, card)
473
- wam_id = resp.get("messages", [{}])[0].get("id")
474
- if wam_id:
475
- await self.repository.create_message(
476
- customer_id=str(self.customer["id"]),
477
- sender_type="assistant",
478
- message=card,
479
- whatsapp_message_id=wam_id,
480
- metadata={"trip_id": trip_id, "type": "driver_trip_card"},
481
- )
482
- elif resp.get("outbox_id"):
483
- await self.repository.create_message(
484
- customer_id=str(self.customer["id"]),
485
- sender_type="assistant",
486
- message=card,
487
- metadata={
488
- "trip_id": trip_id,
489
- "type": "driver_trip_card",
490
- "outbox_id": resp["outbox_id"],
491
- },
492
- )
493
- except WhatsAppClientError:
494
- logger.warning("Failed to send trip card for trip %s", trip_id)
495
-
496
- prompt = "هذه قائمة رحلاتك المسجلة"
497
- await self.whatsapp.send_text(self.remoteJid, prompt)
498
- await self.repository.create_message(
499
- customer_id=str(self.customer["id"]),
500
- sender_type="assistant",
501
- message=prompt,
502
- metadata={"type": "driver_trip_list_prompt"},
503
- )
504
 
505
  return ToolResult(
506
  ok=True,
@@ -748,45 +751,32 @@ class FalzhToolHandlers:
748
  )
749
 
750
  action_label = "حذفها" if action_type == "DELETE" else "تعديلها"
751
- for trip in trips[:5]:
 
 
 
 
 
752
  trip_id = trip.get("trip_id") or trip.get("id")
753
- selection_count = await self.repository.count_trip_selections(trip_id)
754
  trip["selection_count"] = selection_count
755
- card = format_trip_card(trip)
756
- try:
757
- resp = await self.whatsapp.send_text(self.remoteJid, card)
758
- wam_id = resp.get("messages", [{}])[0].get("id")
759
- if wam_id:
760
- await self.repository.create_message(
761
- customer_id=str(self.customer["id"]),
762
- sender_type="assistant",
763
- message=card,
764
- whatsapp_message_id=wam_id,
765
- metadata={"trip_id": trip_id, "type": "driver_trip_card", "action": action_type},
766
- )
767
- elif resp.get("outbox_id"):
768
- await self.repository.create_message(
769
- customer_id=str(self.customer["id"]),
770
- sender_type="assistant",
771
- message=card,
772
- metadata={
773
- "trip_id": trip_id,
774
- "type": "driver_trip_card",
775
- "action": action_type,
776
- "outbox_id": resp["outbox_id"],
777
- },
778
- )
779
- except WhatsAppClientError:
780
- logger.warning("Failed to send trip card for trip %s", trip_id)
781
-
782
- prompt = f"قم بالرد على بطاقة الرحلة التي تريد {action_label}"
783
- await self.whatsapp.send_text(self.remoteJid, prompt)
784
- await self.repository.create_message(
785
- customer_id=str(self.customer["id"]),
786
- sender_type="assistant",
787
- message=prompt,
788
- metadata={"type": "driver_trip_selection_prompt"},
789
  )
 
790
 
791
  return ToolResult(
792
  ok=True,
 
1
+ import asyncio
2
  import logging
3
  from decimal import Decimal, InvalidOperation
4
  from typing import Any
 
25
  trip_satisfies_departure_request,
26
  )
27
  from app.utils.emoji import count_emojis
28
+ from app.whatsapp.client import WhatsAppSender
29
  from app.whatsapp.trip_selection import format_trip_card
30
 
31
 
 
62
  original_meta = original.get("metadata") or {}
63
  return original_meta.get("trip_id")
64
 
65
+ async def _send_and_record_batch(
66
+ self,
67
+ items: list[tuple[str, dict[str, Any]]],
68
+ ) -> None:
69
+ """Send several texts to this chat as one batch and persist each one.
70
+
71
+ Messages are sent directly when possible; on failure the remaining
72
+ batch is queued in order so delivery is never silently dropped.
73
+ """
74
+ if not items:
75
+ return
76
+ responses = await self.whatsapp.send_texts(
77
+ self.remoteJid,
78
+ [text for text, _ in items],
79
+ )
80
+ create_tasks = []
81
+ for (text, metadata), response in zip(items, responses, strict=False):
82
+ wam_id = (response.get("messages") or [{}])[0].get("id")
83
+ if wam_id:
84
+ create_tasks.append(
85
+ self.repository.create_message(
86
+ customer_id=str(self.customer["id"]),
87
+ sender_type="assistant",
88
+ message=text,
89
+ whatsapp_message_id=wam_id,
90
+ metadata=metadata,
91
+ )
92
+ )
93
+ elif response.get("outbox_id"):
94
+ create_tasks.append(
95
+ self.repository.create_message(
96
+ customer_id=str(self.customer["id"]),
97
+ sender_type="assistant",
98
+ message=text,
99
+ metadata={**metadata, "outbox_id": response["outbox_id"]},
100
+ )
101
+ )
102
+ if create_tasks:
103
+ await asyncio.gather(*create_tasks)
104
+
105
  async def about_falzh(self, arguments: dict[str, Any]) -> ToolResult:
106
  query = str(arguments.get("query") or "").strip()
107
  if not query:
 
255
  top_trips = filtered[:5]
256
 
257
  if top_trips:
258
+ counts = await self.repository.count_pending_trip_selections(
259
+ [trip_summary["trip_id"] for trip_summary in top_trips]
260
+ )
261
+ items: list[tuple[str, dict[str, Any]]] = []
262
  for trip_summary in top_trips:
263
  trip_id = trip_summary["trip_id"]
264
+ selection_count = int(counts.get(trip_id) or 0)
265
  trip_summary["selection_count"] = selection_count
266
+ trip = next(
267
+ (t for t in trips if (t.get("trip_id") or t.get("id")) == trip_id),
268
+ {},
269
+ )
270
  trip["selection_count"] = selection_count
271
+ items.append(
272
+ (format_trip_card(trip), {"trip_id": trip_id, "type": "trip_card"})
273
+ )
274
+ items.append(
275
+ (
276
+ "يرجى الرد على إحدى بطاقات الرحلات أعلاه لاختيار رحلتك",
277
+ {"type": "trip_selection_prompt"},
278
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
  )
280
+ await self._send_and_record_batch(items)
281
 
282
  return ToolResult(
283
  ok=True,
 
487
  },
488
  )
489
 
490
+ top_trips = trips[:5]
491
+ counts = await self.repository.count_pending_trip_selections(
492
+ [trip.get("trip_id") or trip.get("id") for trip in top_trips]
493
+ )
494
+ items: list[tuple[str, dict[str, Any]]] = []
495
+ for trip in top_trips:
496
  trip_id = trip.get("trip_id") or trip.get("id")
497
+ selection_count = int(counts.get(trip_id) or 0)
498
  trip["selection_count"] = selection_count
499
+ items.append(
500
+ (
501
+ format_trip_card(trip),
502
+ {"trip_id": trip_id, "type": "driver_trip_card"},
503
+ )
504
+ )
505
+ items.append(("هذه قائمة رحلاتك المسجلة", {"type": "driver_trip_list_prompt"}))
506
+ await self._send_and_record_batch(items)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
507
 
508
  return ToolResult(
509
  ok=True,
 
751
  )
752
 
753
  action_label = "حذفها" if action_type == "DELETE" else "تعديلها"
754
+ top_trips = trips[:5]
755
+ counts = await self.repository.count_pending_trip_selections(
756
+ [trip.get("trip_id") or trip.get("id") for trip in top_trips]
757
+ )
758
+ items: list[tuple[str, dict[str, Any]]] = []
759
+ for trip in top_trips:
760
  trip_id = trip.get("trip_id") or trip.get("id")
761
+ selection_count = int(counts.get(trip_id) or 0)
762
  trip["selection_count"] = selection_count
763
+ items.append(
764
+ (
765
+ format_trip_card(trip),
766
+ {
767
+ "trip_id": trip_id,
768
+ "type": "driver_trip_card",
769
+ "action": action_type,
770
+ },
771
+ )
772
+ )
773
+ items.append(
774
+ (
775
+ f"قم بالرد على بطاقة الرحلة التي تريد {action_label}",
776
+ {"type": "driver_trip_selection_prompt"},
777
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
778
  )
779
+ await self._send_and_record_batch(items)
780
 
781
  return ToolResult(
782
  ok=True,
app/whatsapp/client.py CHANGED
@@ -21,6 +21,9 @@ class WhatsAppSender(Protocol):
21
  async def send_text(self, remoteJid: str, text: str) -> dict[str, Any]:
22
  ...
23
 
 
 
 
24
  async def send_interactive_list(
25
  self,
26
  remoteJid: str,
@@ -33,6 +36,17 @@ class WhatsAppClient:
33
  def __init__(self, settings: Settings, client: httpx.AsyncClient | None = None) -> None:
34
  self._settings = settings
35
  self._client = client
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  @property
38
  def _messages_url(self) -> str:
@@ -81,15 +95,11 @@ class WhatsAppClient:
81
  "Content-Type": "application/json",
82
  }
83
 
84
- if self._client is not None:
85
- response = await self._client.post(
86
- self._messages_url,
87
- json=payload,
88
- headers=headers,
89
- )
90
- else:
91
- async with httpx.AsyncClient(timeout=self._settings.request_timeout_seconds) as client:
92
- response = await client.post(self._messages_url, json=payload, headers=headers)
93
 
94
  if response.status_code >= 400:
95
  raise WhatsAppClientError(
 
21
  async def send_text(self, remoteJid: str, text: str) -> dict[str, Any]:
22
  ...
23
 
24
+ async def send_texts(self, remoteJid: str, texts: list[str]) -> list[dict[str, Any]]:
25
+ ...
26
+
27
  async def send_interactive_list(
28
  self,
29
  remoteJid: str,
 
36
  def __init__(self, settings: Settings, client: httpx.AsyncClient | None = None) -> None:
37
  self._settings = settings
38
  self._client = client
39
+ self._owns_client = client is None
40
+
41
+ def _http(self) -> httpx.AsyncClient:
42
+ if self._client is None:
43
+ self._client = httpx.AsyncClient(timeout=self._settings.request_timeout_seconds)
44
+ return self._client
45
+
46
+ async def aclose(self) -> None:
47
+ if self._owns_client and self._client is not None:
48
+ await self._client.aclose()
49
+ self._client = None
50
 
51
  @property
52
  def _messages_url(self) -> str:
 
95
  "Content-Type": "application/json",
96
  }
97
 
98
+ response = await self._http().post(
99
+ self._messages_url,
100
+ json=payload,
101
+ headers=headers,
102
+ )
 
 
 
 
103
 
104
  if response.status_code >= 400:
105
  raise WhatsAppClientError(
app/whatsapp/outbox.py CHANGED
@@ -28,11 +28,23 @@ class WhatsAppOutbox:
28
  for level in levels
29
  if level.retry_interval_seconds is not None
30
  )
31
- self._send_lock = asyncio.Lock()
 
 
 
 
 
 
 
32
 
33
  async def send_text(self, remoteJid: str, text: str) -> dict[str, Any]:
34
- payload = self.client.build_text_payload(remoteJid, text)
35
- return await self._enqueue_and_send(remoteJid, payload)
 
 
 
 
 
36
 
37
  async def send_interactive_list(
38
  self,
@@ -40,37 +52,56 @@ class WhatsAppOutbox:
40
  interactive: dict[str, Any],
41
  ) -> dict[str, Any]:
42
  payload = self.client.build_interactive_list_payload(remoteJid, interactive)
43
- return await self._enqueue_and_send(remoteJid, payload)
44
 
45
- async def _enqueue_and_send(
46
  self,
47
  recipient: str,
48
- payload: dict[str, Any],
49
- ) -> dict[str, Any]:
50
- row = await self.repository.enqueue_whatsapp_message(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  recipient=recipient,
52
- payload=payload,
53
  )
54
- async with self._send_lock:
55
- await self._recover_and_discard_expired()
56
- if await self.repository.has_sending_whatsapp_message():
57
- return _queued_response(row)
58
-
59
- candidate, _ = await self._next_pending_message(datetime.now(UTC))
60
- if not candidate or candidate.get("id") != row.get("id"):
61
- return _queued_response(row)
62
-
63
- if _is_future(candidate.get("next_attempt_at")):
64
- return _queued_response(row)
65
-
66
- claimed = await self.repository.claim_whatsapp_message(str(row["id"]))
67
- if not claimed:
68
- return _queued_response(row)
69
-
70
- result = await self._deliver_claimed(claimed)
71
- if not result["retryable"]:
72
- await self._drain_ready_messages(force=True)
73
- return result.get("response") or _queued_response(row)
74
 
75
  async def run_retry_worker(self) -> None:
76
  while True:
@@ -81,50 +112,59 @@ class WhatsAppOutbox:
81
  logger.exception("WhatsApp outbox retry pass failed")
82
 
83
  async def retry_pending_messages(self) -> int:
84
- async with self._send_lock:
85
- await self._recover_and_discard_expired()
86
- if await self.repository.has_sending_whatsapp_message():
87
- return 0
88
-
89
- candidate, level = await self._next_pending_message(datetime.now(UTC))
90
- if not candidate or level is None:
91
- return 0
92
- await self._align_retry_schedule(candidate, level)
93
- if _is_future(candidate.get("next_attempt_at")):
94
- return 0
95
 
96
- claimed = await self.repository.claim_whatsapp_message(str(candidate["id"]))
97
- if not claimed:
98
- return 0
 
 
 
 
 
 
99
 
100
- result = await self._deliver_claimed(claimed)
101
- sent = int(result["sent"])
102
- if result["retryable"]:
103
- return sent
104
 
105
- # A successful probe means the API is reachable. Drain in priority order,
106
- # bypassing per-message retry times for this recovery pass.
107
- sent += await self._drain_ready_messages(force=True)
108
- return sent
 
 
 
 
 
 
 
109
 
110
- async def on_connection_restored(self) -> int:
111
- async with self._send_lock:
112
- await self._recover_and_discard_expired()
113
- if await self.repository.has_sending_whatsapp_message():
114
- return 0
115
- return await self._drain_ready_messages(force=True)
116
 
117
- async def _drain_ready_messages(self, *, force: bool = False) -> int:
118
- sent = 0
 
 
 
 
 
119
  while True:
120
- await self._discard_expired_messages()
121
- if await self.repository.has_sending_whatsapp_message():
122
- break
123
- candidate, _ = await self._next_pending_message(datetime.now(UTC))
124
- if not candidate:
125
- break
126
- if not force and _is_future(candidate.get("next_attempt_at")):
127
  break
 
 
 
 
128
 
129
  claimed = await self.repository.claim_whatsapp_message(
130
  str(candidate["id"]),
@@ -134,14 +174,19 @@ class WhatsAppOutbox:
134
  break
135
 
136
  result = await self._deliver_claimed(claimed)
137
- sent += int(result["sent"])
 
 
138
  if result["retryable"]:
139
  break
 
 
140
  force = True
141
- return sent
142
 
143
  async def _next_pending_message(
144
  self,
 
145
  now: datetime,
146
  ) -> tuple[dict[str, Any] | None, WhatsAppOutboxLevel | None]:
147
  previous_max_age = 0
@@ -156,6 +201,7 @@ class WhatsAppOutbox:
156
  else None
157
  )
158
  row = await self.repository.get_pending_whatsapp_message(
 
159
  newer_than=newer_than,
160
  older_than=older_than,
161
  newest_first=level.order == "newest",
@@ -165,7 +211,7 @@ class WhatsAppOutbox:
165
  previous_max_age = level.max_age_seconds or previous_max_age
166
  return None, None
167
 
168
- async def _recover_and_discard_expired(self) -> None:
169
  lease_seconds = max(
170
  self.poll_interval_seconds * 3,
171
  int(self.request_timeout_seconds) + 10,
@@ -173,9 +219,6 @@ class WhatsAppOutbox:
173
  await self.repository.recover_stale_whatsapp_messages(
174
  stale_before=datetime.now(UTC) - timedelta(seconds=lease_seconds),
175
  )
176
- await self._discard_expired_messages()
177
-
178
- async def _discard_expired_messages(self) -> None:
179
  final_retry_level = self.levels[-2]
180
  cutoff = datetime.now(UTC) - timedelta(
181
  seconds=final_retry_level.max_age_seconds or 0
@@ -253,18 +296,30 @@ class WhatsAppOutbox:
253
  return {
254
  "sent": False,
255
  "retryable": retryable,
256
- "response": _queued_response(row, error=str(exc), retryable=retryable),
 
 
 
 
257
  }
258
 
259
  await self.repository.mark_whatsapp_message_sent(
260
  str(row["id"]),
261
  provider_response=response,
262
  )
263
- return {"sent": True, "retryable": False, "response": response}
 
 
 
 
 
 
 
 
264
 
265
 
266
  def _queued_response(
267
- row: dict[str, Any],
268
  *,
269
  error: str | None = None,
270
  retryable: bool = True,
@@ -273,7 +328,7 @@ def _queued_response(
273
  "messages": [{}],
274
  "queued": retryable,
275
  "failed": not retryable,
276
- "outbox_id": row.get("id"),
277
  }
278
  if error:
279
  response["error"] = error
 
28
  for level in levels
29
  if level.retry_interval_seconds is not None
30
  )
31
+ self._recipient_locks: dict[str, asyncio.Lock] = {}
32
+
33
+ def _lock_for(self, recipient: str) -> asyncio.Lock:
34
+ lock = self._recipient_locks.get(recipient)
35
+ if lock is None:
36
+ lock = asyncio.Lock()
37
+ self._recipient_locks[recipient] = lock
38
+ return lock
39
 
40
  async def send_text(self, remoteJid: str, text: str) -> dict[str, Any]:
41
+ return (await self.send_texts(remoteJid, [text]))[0]
42
+
43
+ async def send_texts(self, remoteJid: str, texts: list[str]) -> list[dict[str, Any]]:
44
+ if not texts:
45
+ return []
46
+ payloads = [self.client.build_text_payload(remoteJid, text) for text in texts]
47
+ return await self._send_payloads(remoteJid, payloads)
48
 
49
  async def send_interactive_list(
50
  self,
 
52
  interactive: dict[str, Any],
53
  ) -> dict[str, Any]:
54
  payload = self.client.build_interactive_list_payload(remoteJid, interactive)
55
+ return (await self._send_payloads(remoteJid, [payload]))[0]
56
 
57
+ async def _send_payloads(
58
  self,
59
  recipient: str,
60
+ payloads: list[dict[str, Any]],
61
+ ) -> list[dict[str, Any]]:
62
+ # Per-recipient lock keeps messages for the same chat ordered while
63
+ # letting different chats send concurrently.
64
+ async with self._lock_for(recipient):
65
+ if await self.repository.has_pending_whatsapp_messages(recipient):
66
+ # This chat already has a backlog, so queue behind it instead of
67
+ # sending directly: a new message must never overtake an older
68
+ # one that is still waiting to be delivered.
69
+ return await self._queue_payloads(recipient, payloads)
70
+
71
+ responses: list[dict[str, Any]] = []
72
+ queue_from: int | None = None
73
+ for index, payload in enumerate(payloads):
74
+ try:
75
+ responses.append(await self.client.send_payload(payload))
76
+ except Exception as exc: # noqa: BLE001
77
+ # Queue this message and every message after it so delivery
78
+ # order is preserved. Even non-retryable rejections (e.g. 4xx)
79
+ # must be persisted rather than silently dropped.
80
+ if isinstance(exc, WhatsAppClientError) and not exc.retryable:
81
+ logger.warning(
82
+ "WhatsApp rejected a message for %s permanently: %s",
83
+ recipient,
84
+ exc,
85
+ )
86
+ queue_from = index
87
+ break
88
+
89
+ if queue_from is not None:
90
+ responses.extend(
91
+ await self._queue_payloads(recipient, payloads[queue_from:])
92
+ )
93
+ return responses
94
+
95
+ async def _queue_payloads(
96
+ self,
97
+ recipient: str,
98
+ payloads: list[dict[str, Any]],
99
+ ) -> list[dict[str, Any]]:
100
+ rows = await self.repository.enqueue_whatsapp_messages(
101
  recipient=recipient,
102
+ payloads=payloads,
103
  )
104
+ return [_queued_response(row["id"]) for row in rows]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  async def run_retry_worker(self) -> None:
107
  while True:
 
112
  logger.exception("WhatsApp outbox retry pass failed")
113
 
114
  async def retry_pending_messages(self) -> int:
115
+ await self._run_maintenance()
116
+ sent = 0
117
+ for head in await self.repository.list_pending_whatsapp_queue_heads():
118
+ recipient = str(head["recipient"])
119
+ async with self._lock_for(recipient):
120
+ delivered = await self._drain_recipient(recipient, force=False)
121
+ sent += sum(1 for response in delivered.values() if response.get("sent"))
122
+ return sent
 
 
 
123
 
124
+ async def on_connection_restored(self) -> int:
125
+ await self._run_maintenance()
126
+ sent = 0
127
+ for head in await self.repository.list_pending_whatsapp_queue_heads():
128
+ recipient = str(head["recipient"])
129
+ async with self._lock_for(recipient):
130
+ delivered = await self._drain_recipient(recipient, force=True)
131
+ sent += sum(1 for response in delivered.values() if response.get("sent"))
132
+ return sent
133
 
134
+ async def flush_pending(self) -> int:
135
+ """Force-deliver every queued message for every recipient.
 
 
136
 
137
+ Called on inbound webhooks so a freshly reached chat flushes its
138
+ pending backlog immediately. Skips maintenance: recovery belongs to
139
+ the retry worker and the connection-restored path.
140
+ """
141
+ sent = 0
142
+ for head in await self.repository.list_pending_whatsapp_queue_heads():
143
+ recipient = str(head["recipient"])
144
+ async with self._lock_for(recipient):
145
+ delivered = await self._drain_recipient(recipient, force=True)
146
+ sent += sum(1 for response in delivered.values() if response.get("sent"))
147
+ return sent
148
 
149
+ async def aclose(self) -> None:
150
+ await self.client.aclose()
 
 
 
 
151
 
152
+ async def _drain_recipient(
153
+ self,
154
+ recipient: str,
155
+ *,
156
+ force: bool,
157
+ ) -> dict[str, dict[str, Any]]:
158
+ delivered: dict[str, dict[str, Any]] = {}
159
  while True:
160
+ now = datetime.now(UTC)
161
+ candidate, level = await self._next_pending_message(recipient, now)
162
+ if not candidate or level is None:
 
 
 
 
163
  break
164
+ if not force:
165
+ await self._align_retry_schedule(candidate, level)
166
+ if _is_future(candidate.get("next_attempt_at")):
167
+ break
168
 
169
  claimed = await self.repository.claim_whatsapp_message(
170
  str(candidate["id"]),
 
174
  break
175
 
176
  result = await self._deliver_claimed(claimed)
177
+ delivered[str(claimed["id"])] = result.get("response") or _queued_response(
178
+ claimed.get("id")
179
+ )
180
  if result["retryable"]:
181
  break
182
+ # A successful delivery means the API is reachable; keep draining
183
+ # the rest of this recipient's queue without waiting out retries.
184
  force = True
185
+ return delivered
186
 
187
  async def _next_pending_message(
188
  self,
189
+ recipient: str,
190
  now: datetime,
191
  ) -> tuple[dict[str, Any] | None, WhatsAppOutboxLevel | None]:
192
  previous_max_age = 0
 
201
  else None
202
  )
203
  row = await self.repository.get_pending_whatsapp_message(
204
+ recipient=recipient,
205
  newer_than=newer_than,
206
  older_than=older_than,
207
  newest_first=level.order == "newest",
 
211
  previous_max_age = level.max_age_seconds or previous_max_age
212
  return None, None
213
 
214
+ async def _run_maintenance(self) -> None:
215
  lease_seconds = max(
216
  self.poll_interval_seconds * 3,
217
  int(self.request_timeout_seconds) + 10,
 
219
  await self.repository.recover_stale_whatsapp_messages(
220
  stale_before=datetime.now(UTC) - timedelta(seconds=lease_seconds),
221
  )
 
 
 
222
  final_retry_level = self.levels[-2]
223
  cutoff = datetime.now(UTC) - timedelta(
224
  seconds=final_retry_level.max_age_seconds or 0
 
296
  return {
297
  "sent": False,
298
  "retryable": retryable,
299
+ "response": _queued_response(
300
+ row.get("id"),
301
+ error=str(exc),
302
+ retryable=retryable,
303
+ ),
304
  }
305
 
306
  await self.repository.mark_whatsapp_message_sent(
307
  str(row["id"]),
308
  provider_response=response,
309
  )
310
+ return {
311
+ "sent": True,
312
+ "retryable": False,
313
+ "response": {
314
+ **response,
315
+ "outbox_id": row.get("id"),
316
+ "sent": True,
317
+ },
318
+ }
319
 
320
 
321
  def _queued_response(
322
+ outbox_id: Any,
323
  *,
324
  error: str | None = None,
325
  retryable: bool = True,
 
328
  "messages": [{}],
329
  "queued": retryable,
330
  "failed": not retryable,
331
+ "outbox_id": outbox_id,
332
  }
333
  if error:
334
  response["error"] = error
supabase/migrations/202610190001_per_recipient_outbox_dispatch.sql ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ create index if not exists idx_whatsapp_outbox_recipient_pending_queue
2
+ on public.whatsapp_outbox (recipient, created_at, id)
3
+ where status = 'pending';
4
+
5
+ create index if not exists idx_whatsapp_outbox_recipient_sending
6
+ on public.whatsapp_outbox (recipient)
7
+ where status = 'sending';
8
+
9
+ -- Fast check for "does this recipient have any message waiting to go out?".
10
+ create index if not exists idx_whatsapp_outbox_recipient_active
11
+ on public.whatsapp_outbox (recipient)
12
+ where status in ('pending', 'sending');
13
+
14
+ create index if not exists idx_messages_outbox_id
15
+ on public.messages ((metadata->>'outbox_id'))
16
+ where metadata->>'outbox_id' is not null;
17
+
18
+ -- Atomically claim one queued message for delivery. Serializes claims per
19
+ -- recipient across application processes with an advisory lock, and refuses
20
+ -- to claim while another message for the same recipient is in flight.
21
+ create or replace function public.claim_whatsapp_outbox_message(
22
+ p_message_id uuid,
23
+ p_ignore_retry_time boolean default false
24
+ )
25
+ returns setof public.whatsapp_outbox
26
+ language plpgsql
27
+ security invoker
28
+ set search_path = ''
29
+ as $$
30
+ declare
31
+ v_recipient text;
32
+ begin
33
+ select queued.recipient into v_recipient
34
+ from public.whatsapp_outbox as queued
35
+ where queued.id = p_message_id;
36
+
37
+ if not found then
38
+ return;
39
+ end if;
40
+
41
+ perform pg_catalog.pg_advisory_xact_lock(
42
+ pg_catalog.hashtextextended(v_recipient, 0)
43
+ );
44
+
45
+ if exists (
46
+ select 1
47
+ from public.whatsapp_outbox as queued
48
+ where queued.recipient = v_recipient
49
+ and queued.status = 'sending'
50
+ ) then
51
+ return;
52
+ end if;
53
+
54
+ return query
55
+ update public.whatsapp_outbox as queued
56
+ set status = 'sending',
57
+ attempts = queued.attempts + 1,
58
+ last_attempt_at = pg_catalog.now(),
59
+ locked_at = pg_catalog.now(),
60
+ last_error = null
61
+ where queued.id = p_message_id
62
+ and queued.status = 'pending'
63
+ and (
64
+ p_ignore_retry_time
65
+ or queued.next_attempt_at <= pg_catalog.now()
66
+ )
67
+ returning queued.*;
68
+ end;
69
+ $$;
70
+
71
+ create or replace function public.list_pending_whatsapp_queue_heads()
72
+ returns table(
73
+ recipient text,
74
+ created_at timestamptz,
75
+ next_attempt_at timestamptz
76
+ )
77
+ language sql
78
+ stable
79
+ security invoker
80
+ set search_path = ''
81
+ as $$
82
+ with queue_heads as (
83
+ select distinct on (queued.recipient)
84
+ queued.recipient,
85
+ queued.created_at,
86
+ queued.next_attempt_at
87
+ from public.whatsapp_outbox as queued
88
+ where queued.status = 'pending'
89
+ order by queued.recipient, queued.created_at, queued.id
90
+ )
91
+ select
92
+ queue_heads.recipient,
93
+ queue_heads.created_at,
94
+ queue_heads.next_attempt_at
95
+ from queue_heads
96
+ order by queue_heads.created_at, queue_heads.recipient;
97
+ $$;
98
+
99
+ -- Batched pending-selection counts for trip cards (one round trip instead
100
+ -- of one exact count query per card).
101
+ create or replace function public.count_pending_trip_selections(
102
+ p_trip_ids uuid[]
103
+ )
104
+ returns table(trip_id uuid, selection_count bigint)
105
+ language sql
106
+ stable
107
+ security invoker
108
+ set search_path = ''
109
+ as $$
110
+ select selections.trip_id, count(*)::bigint as selection_count
111
+ from public.trip_selections as selections
112
+ where selections.trip_id = any (p_trip_ids)
113
+ and selections.status = 'pending'
114
+ group by selections.trip_id;
115
+ $$;
116
+
117
+ revoke all on function public.claim_whatsapp_outbox_message(uuid, boolean)
118
+ from public, anon, authenticated;
119
+ grant execute on function public.claim_whatsapp_outbox_message(uuid, boolean)
120
+ to service_role;
121
+
122
+ revoke all on function public.list_pending_whatsapp_queue_heads()
123
+ from public, anon, authenticated;
124
+ grant execute on function public.list_pending_whatsapp_queue_heads()
125
+ to service_role;
126
+
127
+ revoke all on function public.count_pending_trip_selections(uuid[])
128
+ from public, anon, authenticated;
129
+ grant execute on function public.count_pending_trip_selections(uuid[])
130
+ to service_role;
supabase/migrations/public_schema.sql CHANGED
@@ -33,6 +33,77 @@ CREATE SCHEMA public;
33
  COMMENT ON SCHEMA public IS 'standard public schema';
34
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  --
37
  -- Name: departure_bucket_clock_time(text); Type: FUNCTION; Schema: public; Owner: -
38
  --
@@ -49,6 +120,35 @@ CREATE FUNCTION public.departure_bucket_clock_time(bucket text) RETURNS time wit
49
  $$;
50
 
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  --
53
  -- Name: match_active_trips(extensions.vector, double precision, integer, text, text, text, date, text, time without time zone, integer, text); Type: FUNCTION; Schema: public; Owner: -
54
  --
@@ -630,6 +730,13 @@ CREATE INDEX idx_falzh_info_chunks_embedding ON public.falzh_info_chunks USING h
630
  CREATE INDEX idx_messages_customer_created_at ON public.messages USING btree (customer_id, created_at DESC);
631
 
632
 
 
 
 
 
 
 
 
633
  --
634
  -- Name: idx_whatsapp_outbox_queue; Type: INDEX; Schema: public; Owner: -
635
  --
@@ -637,6 +744,27 @@ CREATE INDEX idx_messages_customer_created_at ON public.messages USING btree (cu
637
  CREATE INDEX idx_whatsapp_outbox_queue ON public.whatsapp_outbox USING btree (created_at, id) WHERE (status = ANY (ARRAY['pending'::text, 'sending'::text]));
638
 
639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
640
  --
641
  -- Name: idx_trip_selections_customer; Type: INDEX; Schema: public; Owner: -
642
  --
 
33
  COMMENT ON SCHEMA public IS 'standard public schema';
34
 
35
 
36
+ --
37
+ -- Name: claim_whatsapp_outbox_message(uuid, boolean); Type: FUNCTION; Schema: public; Owner: -
38
+ --
39
+
40
+ CREATE FUNCTION public.claim_whatsapp_outbox_message(p_message_id uuid, p_ignore_retry_time boolean DEFAULT false)
41
+ RETURNS SETOF public.whatsapp_outbox
42
+ LANGUAGE plpgsql
43
+ SECURITY INVOKER
44
+ SET search_path = ''
45
+ AS $$
46
+ declare
47
+ v_recipient text;
48
+ begin
49
+ select queued.recipient into v_recipient
50
+ from public.whatsapp_outbox as queued
51
+ where queued.id = p_message_id;
52
+
53
+ if not found then
54
+ return;
55
+ end if;
56
+
57
+ perform pg_catalog.pg_advisory_xact_lock(
58
+ pg_catalog.hashtextextended(v_recipient, 0)
59
+ );
60
+
61
+ if exists (
62
+ select 1
63
+ from public.whatsapp_outbox as queued
64
+ where queued.recipient = v_recipient
65
+ and queued.status = 'sending'
66
+ ) then
67
+ return;
68
+ end if;
69
+
70
+ return query
71
+ update public.whatsapp_outbox as queued
72
+ set status = 'sending',
73
+ attempts = queued.attempts + 1,
74
+ last_attempt_at = pg_catalog.now(),
75
+ locked_at = pg_catalog.now(),
76
+ last_error = null
77
+ where queued.id = p_message_id
78
+ and queued.status = 'pending'
79
+ and (
80
+ p_ignore_retry_time
81
+ or queued.next_attempt_at <= pg_catalog.now()
82
+ )
83
+ returning queued.*;
84
+ end;
85
+ $$;
86
+
87
+
88
+ --
89
+ -- Name: count_pending_trip_selections(uuid[]); Type: FUNCTION; Schema: public; Owner: -
90
+ --
91
+
92
+ CREATE FUNCTION public.count_pending_trip_selections(p_trip_ids uuid[])
93
+ RETURNS TABLE(trip_id uuid, selection_count bigint)
94
+ LANGUAGE sql
95
+ STABLE
96
+ SECURITY INVOKER
97
+ SET search_path = ''
98
+ AS $$
99
+ select selections.trip_id, count(*)::bigint as selection_count
100
+ from public.trip_selections as selections
101
+ where selections.trip_id = any (p_trip_ids)
102
+ and selections.status = 'pending'
103
+ group by selections.trip_id;
104
+ $$;
105
+
106
+
107
  --
108
  -- Name: departure_bucket_clock_time(text); Type: FUNCTION; Schema: public; Owner: -
109
  --
 
120
  $$;
121
 
122
 
123
+ --
124
+ -- Name: list_pending_whatsapp_queue_heads(); Type: FUNCTION; Schema: public; Owner: -
125
+ --
126
+
127
+ CREATE FUNCTION public.list_pending_whatsapp_queue_heads()
128
+ RETURNS TABLE(recipient text, created_at timestamp with time zone, next_attempt_at timestamp with time zone)
129
+ LANGUAGE sql
130
+ STABLE
131
+ SECURITY INVOKER
132
+ SET search_path = ''
133
+ AS $$
134
+ with queue_heads as (
135
+ select distinct on (queued.recipient)
136
+ queued.recipient,
137
+ queued.created_at,
138
+ queued.next_attempt_at
139
+ from public.whatsapp_outbox as queued
140
+ where queued.status = 'pending'
141
+ order by queued.recipient, queued.created_at, queued.id
142
+ )
143
+ select
144
+ queue_heads.recipient,
145
+ queue_heads.created_at,
146
+ queue_heads.next_attempt_at
147
+ from queue_heads
148
+ order by queue_heads.created_at, queue_heads.recipient;
149
+ $$;
150
+
151
+
152
  --
153
  -- Name: match_active_trips(extensions.vector, double precision, integer, text, text, text, date, text, time without time zone, integer, text); Type: FUNCTION; Schema: public; Owner: -
154
  --
 
730
  CREATE INDEX idx_messages_customer_created_at ON public.messages USING btree (customer_id, created_at DESC);
731
 
732
 
733
+ --
734
+ -- Name: idx_messages_outbox_id; Type: INDEX; Schema: public; Owner: -
735
+ --
736
+
737
+ CREATE INDEX idx_messages_outbox_id ON public.messages USING btree ((metadata ->> 'outbox_id'::text)) WHERE (metadata ->> 'outbox_id'::text) IS NOT NULL;
738
+
739
+
740
  --
741
  -- Name: idx_whatsapp_outbox_queue; Type: INDEX; Schema: public; Owner: -
742
  --
 
744
  CREATE INDEX idx_whatsapp_outbox_queue ON public.whatsapp_outbox USING btree (created_at, id) WHERE (status = ANY (ARRAY['pending'::text, 'sending'::text]));
745
 
746
 
747
+ --
748
+ -- Name: idx_whatsapp_outbox_recipient_active; Type: INDEX; Schema: public; Owner: -
749
+ --
750
+
751
+ CREATE INDEX idx_whatsapp_outbox_recipient_active ON public.whatsapp_outbox USING btree (recipient) WHERE (status = ANY (ARRAY['pending'::text, 'sending'::text]));
752
+
753
+
754
+ --
755
+ -- Name: idx_whatsapp_outbox_recipient_pending_queue; Type: INDEX; Schema: public; Owner: -
756
+ --
757
+
758
+ CREATE INDEX idx_whatsapp_outbox_recipient_pending_queue ON public.whatsapp_outbox USING btree (recipient, created_at, id) WHERE (status = 'pending'::text);
759
+
760
+
761
+ --
762
+ -- Name: idx_whatsapp_outbox_recipient_sending; Type: INDEX; Schema: public; Owner: -
763
+ --
764
+
765
+ CREATE INDEX idx_whatsapp_outbox_recipient_sending ON public.whatsapp_outbox USING btree (recipient) WHERE (status = 'sending'::text);
766
+
767
+
768
  --
769
  -- Name: idx_trip_selections_customer; Type: INDEX; Schema: public; Owner: -
770
  --
tests/conftest.py CHANGED
@@ -90,6 +90,7 @@ def test_app(settings: Settings) -> Any:
90
  settings=settings,
91
  conversation=DummyConversation(),
92
  admin=DummyAdmin(),
 
93
  )
94
  app = create_app(settings=settings, container=container)
95
  app.state.test_container = container
@@ -468,8 +469,13 @@ class FakeRepository:
468
  self.notification_updates.append(update)
469
  return update
470
 
471
- async def count_trip_selections(self, trip_id: str) -> int:
472
- return sum(1 for s in self.trip_selections if s["trip_id"] == trip_id)
 
 
 
 
 
473
 
474
  async def get_driver_by_phone(self, remote_jid: str) -> dict[str, Any] | None:
475
  return self.drivers_by_remote_jid.get(remote_jid)
@@ -648,10 +654,13 @@ class FakeEmbeddings:
648
 
649
 
650
  class FakeWhatsApp:
651
- def __init__(self, *, fail: bool = False) -> None:
652
  self.fail = fail
 
 
653
  self.sent: list[tuple[str, str]] = []
654
  self.interactive_lists: list[tuple[str, dict[str, Any]]] = []
 
655
 
656
  async def send_text(self, to_phone: str, text: str) -> dict[str, Any]:
657
  if self.fail:
@@ -659,6 +668,25 @@ class FakeWhatsApp:
659
  self.sent.append((to_phone, text))
660
  return {"messages": [{"id": "sent"}]}
661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662
  async def send_interactive_list(
663
  self,
664
  to_phone: str,
@@ -669,6 +697,10 @@ class FakeWhatsApp:
669
  self.interactive_lists.append((to_phone, interactive))
670
  return {"messages": [{"id": "sent-interactive"}]}
671
 
 
 
 
 
672
 
673
  class FakeAI:
674
  def __init__(self, reply: str = "Here is your reply") -> None:
 
90
  settings=settings,
91
  conversation=DummyConversation(),
92
  admin=DummyAdmin(),
93
+ whatsapp=FakeWhatsApp(),
94
  )
95
  app = create_app(settings=settings, container=container)
96
  app.state.test_container = container
 
469
  self.notification_updates.append(update)
470
  return update
471
 
472
+ async def count_pending_trip_selections(self, trip_ids: list[str]) -> dict[str, int]:
473
+ counts: dict[str, int] = {}
474
+ for selection in self.trip_selections:
475
+ trip_id = selection["trip_id"]
476
+ if trip_id in trip_ids:
477
+ counts[trip_id] = counts.get(trip_id, 0) + 1
478
+ return counts
479
 
480
  async def get_driver_by_phone(self, remote_jid: str) -> dict[str, Any] | None:
481
  return self.drivers_by_remote_jid.get(remote_jid)
 
654
 
655
 
656
  class FakeWhatsApp:
657
+ def __init__(self, *, fail: bool = False, queue_outbox: bool = False) -> None:
658
  self.fail = fail
659
+ self.queue_outbox = queue_outbox
660
+ self.outbox_counter = 0
661
  self.sent: list[tuple[str, str]] = []
662
  self.interactive_lists: list[tuple[str, dict[str, Any]]] = []
663
+ self.flush_pending_calls = 0
664
 
665
  async def send_text(self, to_phone: str, text: str) -> dict[str, Any]:
666
  if self.fail:
 
668
  self.sent.append((to_phone, text))
669
  return {"messages": [{"id": "sent"}]}
670
 
671
+ async def send_texts(self, to_phone: str, texts: list[str]) -> list[dict[str, Any]]:
672
+ if self.fail:
673
+ raise RuntimeError("send failed")
674
+ responses = []
675
+ for text in texts:
676
+ if self.queue_outbox:
677
+ self.outbox_counter += 1
678
+ responses.append(
679
+ {
680
+ "messages": [{}],
681
+ "queued": True,
682
+ "outbox_id": f"out-{self.outbox_counter}",
683
+ }
684
+ )
685
+ continue
686
+ self.sent.append((to_phone, text))
687
+ responses.append({"messages": [{"id": "sent"}], "outbox_id": None})
688
+ return responses
689
+
690
  async def send_interactive_list(
691
  self,
692
  to_phone: str,
 
697
  self.interactive_lists.append((to_phone, interactive))
698
  return {"messages": [{"id": "sent-interactive"}]}
699
 
700
+ async def flush_pending(self) -> int:
701
+ self.flush_pending_calls += 1
702
+ return 0
703
+
704
 
705
  class FakeAI:
706
  def __init__(self, reply: str = "Here is your reply") -> None:
tests/test_conversation_service.py CHANGED
@@ -50,6 +50,7 @@ async def test_conversation_stores_messages_uses_last_four_context_and_sends_rep
50
  "create_driver_account",
51
  "switch_to_driver",
52
  "switch_to_passenger",
 
53
  }
54
 
55
 
@@ -116,6 +117,7 @@ async def test_conversation_uses_passenger_tools_when_user_mode_is_passenger(set
116
  "select_trip",
117
  "create_driver_account",
118
  "switch_to_driver",
 
119
  }
120
  assert "travel booking assistant" in ai.calls[0]["messages"][0]["content"]
121
 
@@ -173,6 +175,7 @@ async def test_returning_driver_gets_welcome_and_upgraded_to_driver(settings):
173
  "initiate_trip_action",
174
  "update_trip_field",
175
  "switch_to_passenger",
 
176
  }
177
 
178
  # System note should be appended with returning-driver context
@@ -224,6 +227,7 @@ async def test_conversation_uses_driver_tools_when_user_mode_is_driver(settings)
224
  "initiate_trip_action",
225
  "update_trip_field",
226
  "switch_to_passenger",
 
227
  }
228
 
229
 
 
50
  "create_driver_account",
51
  "switch_to_driver",
52
  "switch_to_passenger",
53
+ "submit_group_invite",
54
  }
55
 
56
 
 
117
  "select_trip",
118
  "create_driver_account",
119
  "switch_to_driver",
120
+ "submit_group_invite",
121
  }
122
  assert "travel booking assistant" in ai.calls[0]["messages"][0]["content"]
123
 
 
175
  "initiate_trip_action",
176
  "update_trip_field",
177
  "switch_to_passenger",
178
+ "submit_group_invite",
179
  }
180
 
181
  # System note should be appended with returning-driver context
 
227
  "initiate_trip_action",
228
  "update_trip_field",
229
  "switch_to_passenger",
230
+ "submit_group_invite",
231
  }
232
 
233
 
tests/test_outbox.py ADDED
@@ -0,0 +1,492 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from datetime import UTC, datetime, timedelta
3
+ from typing import Any
4
+
5
+ from app.config import WhatsAppOutboxLevel
6
+ from app.whatsapp.client import WhatsAppClientError
7
+ from app.whatsapp.outbox import WhatsAppOutbox
8
+
9
+ LEVELS = [
10
+ WhatsAppOutboxLevel(max_age_seconds=120, retry_interval_seconds=10, order="oldest"),
11
+ WhatsAppOutboxLevel(max_age_seconds=36_000, retry_interval_seconds=60, order="newest"),
12
+ WhatsAppOutboxLevel(max_age_seconds=None, retry_interval_seconds=None, order="discard"),
13
+ ]
14
+
15
+
16
+ def _iso(value: datetime) -> str:
17
+ return value.isoformat()
18
+
19
+
20
+ def _parse(value: Any) -> datetime:
21
+ parsed = datetime.fromisoformat(str(value).replace("Z", "+00:00"))
22
+ return parsed.replace(tzinfo=UTC) if parsed.tzinfo is None else parsed
23
+
24
+
25
+ class FakeOutboxRepository:
26
+ def __init__(self) -> None:
27
+ self.rows: dict[str, dict[str, Any]] = {}
28
+ self.seq = 0
29
+ self.sent_marks: list[str] = []
30
+
31
+ async def enqueue_whatsapp_messages(
32
+ self,
33
+ *,
34
+ recipient: str,
35
+ payloads: list[dict[str, Any]],
36
+ ) -> list[dict[str, Any]]:
37
+ base = datetime.now(UTC)
38
+ created: list[dict[str, Any]] = []
39
+ for index, payload in enumerate(payloads):
40
+ self.seq += 1
41
+ row = {
42
+ "id": f"out-{self.seq:04d}",
43
+ "recipient": recipient,
44
+ "payload": payload,
45
+ "status": "pending",
46
+ "attempts": 0,
47
+ "next_attempt_at": _iso(base + timedelta(microseconds=index)),
48
+ "created_at": _iso(base + timedelta(microseconds=index)),
49
+ "last_attempt_at": None,
50
+ "locked_at": None,
51
+ "last_error": None,
52
+ }
53
+ self.rows[row["id"]] = row
54
+ created.append(dict(row))
55
+ return created
56
+
57
+ async def has_pending_whatsapp_messages(self, recipient: str) -> bool:
58
+ return any(
59
+ row["recipient"] == recipient and row["status"] in ("pending", "sending")
60
+ for row in self.rows.values()
61
+ )
62
+
63
+ async def get_pending_whatsapp_message(
64
+ self,
65
+ *,
66
+ recipient: str,
67
+ newer_than: datetime | None,
68
+ older_than: datetime | None,
69
+ newest_first: bool,
70
+ ) -> dict[str, Any] | None:
71
+ candidates = [
72
+ row
73
+ for row in self.rows.values()
74
+ if row["recipient"] == recipient and row["status"] == "pending"
75
+ ]
76
+ if newer_than is not None:
77
+ candidates = [row for row in candidates if _parse(row["created_at"]) >= newer_than]
78
+ if older_than is not None:
79
+ candidates = [row for row in candidates if _parse(row["created_at"]) < older_than]
80
+ if not candidates:
81
+ return None
82
+ candidates.sort(
83
+ key=lambda row: (_parse(row["created_at"]), row["id"]),
84
+ reverse=newest_first,
85
+ )
86
+ return dict(candidates[0])
87
+
88
+ async def list_pending_whatsapp_queue_heads(self) -> list[dict[str, Any]]:
89
+ heads: dict[str, dict[str, Any]] = {}
90
+ for row in sorted(
91
+ (row for row in self.rows.values() if row["status"] == "pending"),
92
+ key=lambda row: (_parse(row["created_at"]), row["id"]),
93
+ ):
94
+ heads.setdefault(
95
+ row["recipient"],
96
+ {
97
+ "recipient": row["recipient"],
98
+ "created_at": row["created_at"],
99
+ "next_attempt_at": row["next_attempt_at"],
100
+ },
101
+ )
102
+ return list(heads.values())
103
+
104
+ async def claim_whatsapp_message(
105
+ self,
106
+ message_id: str,
107
+ *,
108
+ ignore_retry_time: bool = False,
109
+ ) -> dict[str, Any] | None:
110
+ row = self.rows.get(message_id)
111
+ if row is None:
112
+ return None
113
+ if any(
114
+ other["recipient"] == row["recipient"] and other["status"] == "sending"
115
+ for other in self.rows.values()
116
+ ):
117
+ return None
118
+ if row["status"] != "pending":
119
+ return None
120
+ if not ignore_retry_time and _parse(row["next_attempt_at"]) > datetime.now(UTC):
121
+ return None
122
+ row.update(
123
+ status="sending",
124
+ attempts=int(row["attempts"]) + 1,
125
+ last_attempt_at=_iso(datetime.now(UTC)),
126
+ locked_at=_iso(datetime.now(UTC)),
127
+ last_error=None,
128
+ )
129
+ return dict(row)
130
+
131
+ async def mark_whatsapp_message_sent(
132
+ self,
133
+ message_id: str,
134
+ *,
135
+ provider_response: dict[str, Any],
136
+ ) -> None:
137
+ self.rows[message_id].update(
138
+ status="sent",
139
+ sent_at=_iso(datetime.now(UTC)),
140
+ locked_at=None,
141
+ )
142
+ self.sent_marks.append(message_id)
143
+
144
+ async def fail_whatsapp_message(
145
+ self,
146
+ message_id: str,
147
+ *,
148
+ error: str,
149
+ retry_at: datetime,
150
+ ) -> None:
151
+ self.rows[message_id].update(
152
+ status="pending",
153
+ next_attempt_at=_iso(retry_at),
154
+ locked_at=None,
155
+ last_error=error,
156
+ )
157
+
158
+ async def discard_whatsapp_message(
159
+ self,
160
+ message_id: str,
161
+ *,
162
+ reason: str,
163
+ error: str | None = None,
164
+ ) -> None:
165
+ self.rows[message_id].update(
166
+ status="discarded",
167
+ locked_at=None,
168
+ discard_reason=reason,
169
+ last_error=error,
170
+ )
171
+
172
+ async def recover_stale_whatsapp_messages(self, *, stale_before: datetime) -> None:
173
+ for row in self.rows.values():
174
+ if (
175
+ row["status"] == "sending"
176
+ and row["locked_at"]
177
+ and _parse(row["locked_at"]) < stale_before
178
+ ):
179
+ row.update(
180
+ status="pending",
181
+ next_attempt_at=_iso(datetime.now(UTC)),
182
+ locked_at=None,
183
+ )
184
+
185
+ async def discard_expired_whatsapp_messages(self, *, before: datetime) -> None:
186
+ for row in self.rows.values():
187
+ if row["status"] == "pending" and _parse(row["created_at"]) < before:
188
+ row.update(status="discarded", locked_at=None)
189
+
190
+ async def schedule_whatsapp_retry(self, message_id: str, *, retry_at: datetime) -> None:
191
+ row = self.rows[message_id]
192
+ if row["status"] == "pending":
193
+ row["next_attempt_at"] = _iso(retry_at)
194
+
195
+
196
+ class FakeWhatsAppClient:
197
+ def __init__(self) -> None:
198
+ self.sent_payloads: list[dict[str, Any]] = []
199
+ self.gates: dict[str, asyncio.Event] = {}
200
+ self.fail_on: set[int] = set()
201
+ self.always_fail = False
202
+ self.fail_status = 500
203
+ self.calls = 0
204
+ self.closed = False
205
+
206
+ def build_text_payload(self, remoteJid: str, text: str) -> dict[str, Any]:
207
+ return {"to": remoteJid, "type": "text", "text": {"body": text}}
208
+
209
+ def build_interactive_list_payload(
210
+ self,
211
+ remoteJid: str,
212
+ interactive: dict[str, Any],
213
+ ) -> dict[str, Any]:
214
+ return {"to": remoteJid, "type": "interactive", "interactive": interactive}
215
+
216
+ async def send_payload(self, payload: dict[str, Any]) -> dict[str, Any]:
217
+ gate = self.gates.get(payload["to"])
218
+ if gate is not None:
219
+ await gate.wait()
220
+ self.calls += 1
221
+ if self.always_fail or self.calls in self.fail_on:
222
+ raise WhatsAppClientError("boom", status_code=self.fail_status)
223
+ self.sent_payloads.append(payload)
224
+ return {"messages": [{"id": f"wamid.{len(self.sent_payloads)}"}]}
225
+
226
+ async def aclose(self) -> None:
227
+ self.closed = True
228
+
229
+
230
+ def make_outbox(
231
+ repository: FakeOutboxRepository | None = None,
232
+ client: FakeWhatsAppClient | None = None,
233
+ ) -> WhatsAppOutbox:
234
+ return WhatsAppOutbox(
235
+ repository=repository or FakeOutboxRepository(), # type: ignore[arg-type]
236
+ client=client or FakeWhatsAppClient(), # type: ignore[arg-type]
237
+ levels=LEVELS,
238
+ request_timeout_seconds=20.0,
239
+ )
240
+
241
+
242
+ def bodies(client: FakeWhatsAppClient) -> list[str]:
243
+ return [payload["text"]["body"] for payload in client.sent_payloads]
244
+
245
+
246
+ async def test_send_texts_sends_directly_without_outbox_rows() -> None:
247
+ repository = FakeOutboxRepository()
248
+ client = FakeWhatsAppClient()
249
+ outbox = make_outbox(repository, client)
250
+
251
+ responses = await outbox.send_texts("chat-a", ["card-1", "card-2", "card-3"])
252
+
253
+ assert bodies(client) == ["card-1", "card-2", "card-3"]
254
+ assert repository.rows == {}
255
+ for index, response in enumerate(responses, start=1):
256
+ assert response["messages"][0]["id"] == f"wamid.{index}"
257
+ assert response.get("queued") is not True
258
+ assert response.get("outbox_id") is None
259
+
260
+
261
+ async def test_send_text_returns_single_response() -> None:
262
+ repository = FakeOutboxRepository()
263
+ client = FakeWhatsAppClient()
264
+ outbox = make_outbox(repository, client)
265
+
266
+ response = await outbox.send_text("chat-a", "hello")
267
+
268
+ assert bodies(client) == ["hello"]
269
+ assert response["messages"][0]["id"] == "wamid.1"
270
+ assert repository.rows == {}
271
+
272
+
273
+ async def test_slow_recipient_does_not_block_another() -> None:
274
+ client = FakeWhatsAppClient()
275
+ blocked = asyncio.Event()
276
+ client.gates["chat-a"] = blocked
277
+ outbox = make_outbox(client=client)
278
+
279
+ slow_send = asyncio.create_task(outbox.send_texts("chat-a", ["a-1", "a-2"]))
280
+ await asyncio.sleep(0)
281
+ fast_responses = await outbox.send_texts("chat-b", ["b-1"])
282
+
283
+ assert bodies(client) == ["b-1"]
284
+ assert fast_responses[0]["messages"][0]["id"] == "wamid.1"
285
+
286
+ blocked.set()
287
+ await slow_send
288
+ assert bodies(client) == ["b-1", "a-1", "a-2"]
289
+
290
+
291
+ async def test_retryable_failure_is_queued_then_drained() -> None:
292
+ repository = FakeOutboxRepository()
293
+ client = FakeWhatsAppClient()
294
+ client.fail_on = {1}
295
+ outbox = make_outbox(repository, client)
296
+
297
+ response = await outbox.send_text("chat-a", "failing")
298
+
299
+ assert response.get("queued") is True
300
+ assert response["outbox_id"]
301
+ assert client.sent_payloads == []
302
+ assert repository.rows[response["outbox_id"]]["status"] == "pending"
303
+
304
+ row = repository.rows[response["outbox_id"]]
305
+ row["next_attempt_at"] = _iso(datetime.now(UTC) - timedelta(seconds=1))
306
+
307
+ sent = await outbox.retry_pending_messages()
308
+
309
+ assert sent == 1
310
+ assert bodies(client) == ["failing"]
311
+
312
+
313
+ async def test_partial_batch_keeps_order_and_queues_the_rest() -> None:
314
+ repository = FakeOutboxRepository()
315
+ client = FakeWhatsAppClient()
316
+ client.fail_on = {2}
317
+ outbox = make_outbox(repository, client)
318
+
319
+ responses = await outbox.send_texts("chat-a", ["c1", "c2", "c3"])
320
+
321
+ assert bodies(client) == ["c1"]
322
+ assert responses[0]["messages"][0]["id"] == "wamid.1"
323
+ assert responses[1].get("queued") is True
324
+ assert responses[2].get("queued") is True
325
+ queued = [row for row in repository.rows.values() if row["status"] == "pending"]
326
+ assert [row["payload"]["text"]["body"] for row in queued] == ["c2", "c3"]
327
+
328
+
329
+ async def test_permanent_failure_is_queued_not_dropped() -> None:
330
+ repository = FakeOutboxRepository()
331
+ client = FakeWhatsAppClient()
332
+ client.fail_on = {1}
333
+ client.fail_status = 400
334
+ outbox = make_outbox(repository, client)
335
+
336
+ response = await outbox.send_text("chat-a", "bad")
337
+
338
+ assert response.get("failed") is not True
339
+ assert response.get("queued") is True
340
+ assert response["outbox_id"]
341
+ assert repository.rows[response["outbox_id"]]["status"] == "pending"
342
+ assert client.sent_payloads == []
343
+
344
+
345
+ async def test_permanent_failure_is_discarded_with_a_reason_after_a_retry() -> None:
346
+ repository = FakeOutboxRepository()
347
+ client = FakeWhatsAppClient()
348
+ client.always_fail = True
349
+ client.fail_status = 400
350
+ outbox = make_outbox(repository, client)
351
+
352
+ response = await outbox.send_text("chat-a", "bad")
353
+ row = repository.rows[response["outbox_id"]]
354
+ row["next_attempt_at"] = _iso(datetime.now(UTC) - timedelta(seconds=1))
355
+
356
+ sent = await outbox.retry_pending_messages()
357
+
358
+ assert sent == 0
359
+ assert row["status"] == "discarded"
360
+ assert "rejected" in row["discard_reason"]
361
+
362
+
363
+ async def test_new_send_queues_behind_existing_backlog() -> None:
364
+ repository = FakeOutboxRepository()
365
+ client = FakeWhatsAppClient()
366
+ outbox = make_outbox(repository, client)
367
+
368
+ rows = await repository.enqueue_whatsapp_messages(
369
+ recipient="chat-a",
370
+ payloads=[client.build_text_payload("chat-a", "old")],
371
+ )
372
+ repository.rows[rows[0]["id"]]["next_attempt_at"] = _iso(
373
+ datetime.now(UTC) + timedelta(seconds=30)
374
+ )
375
+
376
+ response = await outbox.send_text("chat-a", "new")
377
+
378
+ assert response.get("queued") is True
379
+ assert response["outbox_id"] != rows[0]["id"]
380
+ assert client.sent_payloads == []
381
+ assert repository.rows[rows[0]["id"]]["status"] == "pending"
382
+
383
+ queued = sorted(repository.rows.values(), key=lambda row: row["created_at"])
384
+ assert [row["payload"]["text"]["body"] for row in queued] == ["old", "new"]
385
+
386
+
387
+ async def test_batch_queues_behind_existing_backlog_in_order() -> None:
388
+ repository = FakeOutboxRepository()
389
+ client = FakeWhatsAppClient()
390
+ outbox = make_outbox(repository, client)
391
+
392
+ rows = await repository.enqueue_whatsapp_messages(
393
+ recipient="chat-a",
394
+ payloads=[client.build_text_payload("chat-a", "old")],
395
+ )
396
+ repository.rows[rows[0]["id"]]["next_attempt_at"] = _iso(
397
+ datetime.now(UTC) + timedelta(seconds=30)
398
+ )
399
+
400
+ responses = await outbox.send_texts("chat-a", ["new-1", "new-2"])
401
+
402
+ assert [response.get("queued") for response in responses] == [True, True]
403
+ assert client.sent_payloads == []
404
+ queued = sorted(repository.rows.values(), key=lambda row: row["created_at"])
405
+ assert [row["payload"]["text"]["body"] for row in queued] == ["old", "new-1", "new-2"]
406
+
407
+
408
+ async def test_flush_pending_drains_every_recipient() -> None:
409
+ repository = FakeOutboxRepository()
410
+ client = FakeWhatsAppClient()
411
+ outbox = make_outbox(repository, client)
412
+
413
+ rows_a = await repository.enqueue_whatsapp_messages(
414
+ recipient="chat-a",
415
+ payloads=[client.build_text_payload("chat-a", "a-1")],
416
+ )
417
+ rows_b = await repository.enqueue_whatsapp_messages(
418
+ recipient="chat-b",
419
+ payloads=[client.build_text_payload("chat-b", "b-1")],
420
+ )
421
+ for row in (*rows_a, *rows_b):
422
+ repository.rows[row["id"]]["next_attempt_at"] = _iso(
423
+ datetime.now(UTC) + timedelta(seconds=30)
424
+ )
425
+
426
+ sent = await outbox.flush_pending()
427
+
428
+ assert sent == 2
429
+ assert sorted(bodies(client)) == ["a-1", "b-1"]
430
+
431
+
432
+ async def test_retry_worker_drains_backlog_across_recipients() -> None:
433
+ repository = FakeOutboxRepository()
434
+ client = FakeWhatsAppClient()
435
+ outbox = make_outbox(repository, client)
436
+
437
+ await repository.enqueue_whatsapp_messages(
438
+ recipient="chat-a",
439
+ payloads=[client.build_text_payload("chat-a", "a-1")],
440
+ )
441
+ await repository.enqueue_whatsapp_messages(
442
+ recipient="chat-b",
443
+ payloads=[client.build_text_payload("chat-b", "b-1")],
444
+ )
445
+
446
+ sent = await outbox.retry_pending_messages()
447
+
448
+ assert sent == 2
449
+ assert sorted(bodies(client)) == ["a-1", "b-1"]
450
+
451
+
452
+ async def test_connection_restored_force_drains_retry_backoff() -> None:
453
+ repository = FakeOutboxRepository()
454
+ client = FakeWhatsAppClient()
455
+ outbox = make_outbox(repository, client)
456
+
457
+ rows = await repository.enqueue_whatsapp_messages(
458
+ recipient="chat-a",
459
+ payloads=[client.build_text_payload("chat-a", "old-1")],
460
+ )
461
+ repository.rows[rows[0]["id"]]["next_attempt_at"] = _iso(
462
+ datetime.now(UTC) + timedelta(seconds=30)
463
+ )
464
+
465
+ sent = await outbox.on_connection_restored()
466
+
467
+ assert sent == 1
468
+ assert bodies(client) == ["old-1"]
469
+
470
+
471
+ async def test_stale_sending_row_recovers_without_blocking_other_recipients() -> None:
472
+ repository = FakeOutboxRepository()
473
+ client = FakeWhatsAppClient()
474
+ outbox = make_outbox(repository, client)
475
+
476
+ stuck = await repository.enqueue_whatsapp_messages(
477
+ recipient="chat-stuck",
478
+ payloads=[client.build_text_payload("chat-stuck", "stuck")],
479
+ )
480
+ repository.rows[stuck[0]["id"]].update(
481
+ status="sending",
482
+ locked_at=_iso(datetime.now(UTC) - timedelta(minutes=10)),
483
+ )
484
+
485
+ responses = await outbox.send_texts("chat-free", ["free"])
486
+
487
+ assert bodies(client) == ["free"]
488
+ assert responses[0]["messages"][0]["id"] == "wamid.1"
489
+
490
+ sent = await outbox.retry_pending_messages()
491
+ assert sent == 1
492
+ assert bodies(client) == ["free", "stuck"]
tests/test_tools.py CHANGED
@@ -912,6 +912,32 @@ async def test_initiate_trip_action_returns_no_trips_message():
912
  assert "No trips found" in result.data["message"]
913
 
914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
915
  @pytest.mark.asyncio
916
  async def test_update_trip_field_uses_active_session_and_clears_it():
917
  repository = FakeRepository()
 
912
  assert "No trips found" in result.data["message"]
913
 
914
 
915
+ @pytest.mark.asyncio
916
+ async def test_failed_trip_cards_are_persisted_with_outbox_id():
917
+ repository = FakeRepository()
918
+ whatsapp = FakeWhatsApp(queue_outbox=True)
919
+ _driver_setup(repository)
920
+ handlers = make_handlers(
921
+ repository=repository,
922
+ whatsapp=whatsapp,
923
+ sender_phone="967700000010",
924
+ )
925
+
926
+ result = await handlers.initiate_trip_action({"action_type": "DELETE"})
927
+
928
+ assert result.ok is True
929
+ assert whatsapp.sent == []
930
+ assistant_messages = [
931
+ message for message in repository.messages if message["sender_type"] == "assistant"
932
+ ]
933
+ assert [message["metadata"]["outbox_id"] for message in assistant_messages] == [
934
+ "out-1",
935
+ "out-2",
936
+ ]
937
+ assert all(message["whatsapp_message_id"] is None for message in assistant_messages)
938
+ assert assistant_messages[0]["metadata"]["type"] == "driver_trip_card"
939
+
940
+
941
  @pytest.mark.asyncio
942
  async def test_update_trip_field_uses_active_session_and_clears_it():
943
  repository = FakeRepository()
tests/test_webhooks.py CHANGED
@@ -51,10 +51,33 @@ def test_post_webhook_validates_signature_and_accepts_text(test_app):
51
  )
52
 
53
  conversation = test_app.state.test_container.conversation
 
54
  assert response.status_code == 200
55
  assert response.json() == {"status": "accepted", "messages": 1}
56
  assert len(conversation.calls) == 1
57
  assert conversation.calls[0].text == "I need Aden to Mukalla"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
 
60
  def test_post_webhook_debug_returns_reply_and_status(test_app):
 
51
  )
52
 
53
  conversation = test_app.state.test_container.conversation
54
+ whatsapp = test_app.state.test_container.whatsapp
55
  assert response.status_code == 200
56
  assert response.json() == {"status": "accepted", "messages": 1}
57
  assert len(conversation.calls) == 1
58
  assert conversation.calls[0].text == "I need Aden to Mukalla"
59
+ assert whatsapp.flush_pending_calls == 1
60
+
61
+
62
+ def test_post_webhook_without_messages_does_not_flush(test_app):
63
+ payload = whatsapp_payload(message_id="wamid.status")
64
+ messages = payload["entry"][0]["changes"][0]["value"].pop("messages")
65
+ payload["entry"][0]["changes"][0]["value"]["statuses"] = messages
66
+ body, signature = signed_body(payload, "app-secret")
67
+
68
+ with TestClient(test_app) as client:
69
+ response = client.post(
70
+ "/webhooks/whatsapp",
71
+ content=body,
72
+ headers={
73
+ "content-type": "application/json",
74
+ "x-hub-signature-256": signature,
75
+ },
76
+ )
77
+
78
+ assert response.status_code == 200
79
+ assert response.json() == {"status": "accepted", "messages": 0}
80
+ assert test_app.state.test_container.whatsapp.flush_pending_calls == 0
81
 
82
 
83
  def test_post_webhook_debug_returns_reply_and_status(test_app):