Coverage for src/robot_dispatch/messaging/messages.py: 96%

48 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-07 17:18 +0000

1from __future__ import annotations 

2 

3from datetime import datetime 

4from typing import Any, Literal 

5from uuid import UUID 

6 

7from pydantic import BaseModel, Field 

8 

9COMMANDS_EXCHANGE = "robot.commands" 

10EVENTS_EXCHANGE = "robot.events" 

11DLX_EXCHANGE = "dispatch.dlx" 

12RESULTS_QUEUE = "dispatch.results" 

13RETRY_QUEUE = "dispatch.results.retry" 

14DLQ_QUEUE = "dispatch.results.dlq" 

15 

16 

17def command_routing_key(robot_id: UUID) -> str: 

18 return f"command.{robot_id}" 

19 

20 

21def command_queue_name(robot_id: UUID) -> str: 

22 return f"cmd.{robot_id}" 

23 

24 

25def result_routing_key(robot_id: UUID) -> str: 

26 return f"result.{robot_id}" 

27 

28 

29def heartbeat_routing_key(robot_id: UUID) -> str: 

30 return f"heartbeat.{robot_id}" 

31 

32 

33class CommandMessage(BaseModel): 

34 """Server -> robot. Sent exactly once per attempt, deduplicated by command_id.""" 

35 

36 kind: Literal["command"] = "command" 

37 command_id: UUID 

38 task_id: UUID 

39 task_type: str 

40 robot_id: UUID 

41 namespace: str 

42 attempt: int 

43 timeout_seconds: int 

44 payload: dict[str, Any] = Field(default_factory=dict) 

45 issued_at: datetime 

46 

47 

48class TaskResultMessage(BaseModel): 

49 """Robot -> server. `event=progress` keeps the lease alive.""" 

50 

51 kind: Literal["result"] = "result" 

52 event: Literal["progress", "succeeded", "failed"] 

53 task_id: UUID 

54 robot_id: UUID 

55 command_id: UUID | None = None 

56 progress: float | None = None 

57 detail: str | None = None 

58 error: str | None = None 

59 result: dict[str, Any] | None = None 

60 occurred_at: datetime 

61 

62 

63class HeartbeatMessage(BaseModel): 

64 kind: Literal["heartbeat"] = "heartbeat" 

65 robot_id: UUID 

66 namespace: str | None = None 

67 status: Literal["idle", "busy", "offline"] | None = None 

68 battery: float | None = None 

69 occurred_at: datetime