session_bot_outcome.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """What a teammate's finished turn tells the ``message_agent`` sender that asked it something.
  2. Forgotten reply: the single-reply protocol makes the teammate's final response the reply. A model that ends its
  3. turn with a silence marker or nothing, after it wrote its answer beside a tool call or tried to
  4. send it back through ``message_agent`` (refused: the final response is the reply), would leave the
  5. sender with an empty receipt. Its last visible words that turn are relayed instead, once, marked
  6. so the sender knows they were not a final answer. A notify-only request (``[no reply needed]``)
  7. never gets one: silence is its expected outcome.
  8. """
  9. import json
  10. FORGOTTEN_REPLY_PREFIX = '[no final reply; their last message this turn:] '
  11. # A turn its own gateway's stop or restart interrupted settles ``interrupted`` and is never re-run: for
  12. # the sender that is a dead request, not a cancel it asked for.
  13. OWNER_STOPPED_ERROR = ('its gateway stopped or restarted mid-turn, so the turn was interrupted and is not '
  14. 'run again (some steps may already have run)')
  15. def owner_stopped(result):
  16. """True for a turn result its own gateway's stop or restart interrupted (``gateway/run.py`` reasons)."""
  17. from gateway.run import _INTERRUPT_REASON_GATEWAY_RESTART, _INTERRUPT_REASON_GATEWAY_SHUTDOWN
  18. return result.get('interrupt_message') in (_INTERRUPT_REASON_GATEWAY_RESTART, _INTERRUPT_REASON_GATEWAY_SHUTDOWN)
  19. def forgotten_reply(authority, admission_id, record):
  20. """The marked fallback reply for ``record``'s settled-but-empty admission, or None."""
  21. from tools.bot_mode_probe import NO_REPLY_MARKER
  22. author = record.get('author') or {}
  23. if not author.get('is_bot') or NO_REPLY_MARKER in (record.get('message') or ''):
  24. return None
  25. from gateway.session_results import admission_result
  26. saved = (admission_result(authority.db, admission_id) or {}).get('result') or {}
  27. text = _last_words(saved.get('messages') or [], str(author.get('name') or '').lower())
  28. return FORGOTTEN_REPLY_PREFIX + text if text else None
  29. def _last_words(messages, sender):
  30. from gateway.response_filters import is_intentional_silence_response
  31. for message in reversed(messages):
  32. if not isinstance(message, dict) or message.get('role') != 'assistant':
  33. continue
  34. for call in reversed(message.get('tool_calls') or []):
  35. text = _reply_attempt(call, sender)
  36. if text:
  37. return text
  38. content = message.get('content')
  39. if isinstance(content, str) and content.strip() and not is_intentional_silence_response(content):
  40. return content.strip()
  41. return None
  42. def _reply_attempt(call, sender):
  43. """The body of a ``message_agent`` call this turn made back to its own sender."""
  44. function = (call or {}).get('function') or {} if isinstance(call, dict) else {}
  45. if function.get('name') != 'message_agent':
  46. return None
  47. try:
  48. args = json.loads(function.get('arguments') or '{}')
  49. except ValueError:
  50. return None
  51. if not isinstance(args, dict) or str(args.get('target') or '').strip().lstrip('@').lower() != sender:
  52. return None
  53. return str(args.get('message') or '').strip() or None