bot_mode_dm_outcome.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """One plain sentence per ``message_agent`` request outcome, for the sender's wake-up.
  2. A receipt says ``status: ambiguous`` or ``queued`` and a delivery id; models read that as "not
  3. delivered" and offer to resend, or as "still coming" and wait forever. Every runner that ends a
  4. request's wait (the live lane, the fan-out join) leads its output with ``outcome``: the teammate
  5. answered, or "<who> never answered: <why>", plus whether resending is safe. Stdlib only: the fan-out
  6. runner imports it before Hermes boots.
  7. """
  8. from __future__ import annotations
  9. _NOT_YET = ("queued", "claimed", "pending")
  10. def outcome_line(to: str, entry: dict) -> str:
  11. """The sentence for one request's final record (``status`` plus ``reply``/``error``/``detail``)."""
  12. status = entry.get("status")
  13. if status == "settled":
  14. return f"{to} answered." if entry.get("reply") else f"{to} finished its turn without replying."
  15. why = str(entry.get("error") or entry.get("detail") or "").strip().rstrip(".")
  16. if status == "cancelled":
  17. return f"{to} never answered: the request was cancelled."
  18. if status in _NOT_YET:
  19. state = entry.get("target_state")
  20. where = {"unreachable": "its gateway is not reachable",
  21. "waiting_approval": "it is waiting for its own user to approve a command",
  22. "waiting_clarify": "it is waiting for its own user to answer a question",
  23. "paused": "it is not starting new turns (restarting or updating)",
  24. "working": "its turn is still running"}.get(str(state or ""), "it has not finished")
  25. return (f"{to} never answered within the wait: {where}. The request is still on their side and its "
  26. "answer will NOT wake you; tell your user, do not resend.")
  27. if status == "ambiguous":
  28. return (f"{to} never answered: whether it received the request is unknown ({why}). It may still run "
  29. "once their gateway is back; do not resend.")
  30. safe = "NOT sent" in why or "safe to resend" in why
  31. return (f"{to} never answered: {why or 'the delivery failed'}. "
  32. + ("Nothing reached them; you may resend." if safe else "Do not resend blindly."))
  33. def combined_summary(entries: list[dict]) -> str:
  34. """The fan-out join's lead line: every teammate's sentence, answered ones first."""
  35. lines = [outcome_line(e.get("to") or "a teammate", e) for e in entries]
  36. return " ".join(sorted(lines, key=lambda line: " never answered" in line))