api_server_turn_boundary.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """Current-turn boundary inside an agent transcript returned to the OpenAI-compat routes.
  2. ``AIAgent.run_conversation`` hands back the FULL transcript (client history + this turn),
  3. and the routes need the index where this turn starts to build Responses ``output`` items,
  4. ``run.completed`` turn transcripts and the stored ``previous_response_id`` history.
  5. """
  6. from typing import Any, Dict, List
  7. _TRANSCRIPT_IDENTITY_KEYS = ("role", "content", "tool_calls", "tool_call_id")
  8. def _same_transcript_prefix(agent_messages: list[Any], prefix: list[Any]) -> bool:
  9. """True when ``agent_messages`` starts with ``prefix`` by what each message *says*.
  10. The API layer builds bare ``{"role", "content"}`` dicts while the agent stamps its copies
  11. with ``timestamp`` / ``_db_persisted`` / ``reasoning`` / ``finish_reason``; whole-dict
  12. equality therefore never matched and every chained turn re-appended the full prior
  13. transcript (#95137, #101644, #82513)."""
  14. if len(agent_messages) < len(prefix):
  15. return False
  16. for got, want in zip(agent_messages, prefix):
  17. if not isinstance(got, dict) or not isinstance(want, dict):
  18. if got != want:
  19. return False
  20. continue
  21. if any(got.get(k) != want.get(k) for k in _TRANSCRIPT_IDENTITY_KEYS):
  22. return False
  23. return True
  24. def response_turn_start_index(
  25. conversation_history: list[dict[str, Any]], user_message: Any, result: dict[str, Any],
  26. ) -> int:
  27. """Index in ``result["messages"]`` where this turn's assistant/tool rows begin (0 = all).
  28. Anchored on this turn's user row (the loop's canonical ``reanchor_current_turn_user_idx``),
  29. not on prefix equality with the client history: the loop repairs host-fed history before
  30. the first call (merging consecutive assistant/user rows, dropping stray tool results) and
  31. compaction rewrites it, so the returned transcript legitimately stops sharing a prefix with
  32. ``conversation_history`` and a prefix match then returned 0 — the whole transcript was
  33. treated as the current turn, replaying earlier ``function_call`` items and doubling the
  34. stored history on every chained turn (#89891).
  35. Mocked/legacy paths return only this turn's suffix (no user row): the prefix match stays as
  36. the fallback for them.
  37. """
  38. from agent.turn_context import reanchor_current_turn_user_idx
  39. agent_messages = result.get("messages") if isinstance(result, dict) else None
  40. if not isinstance(agent_messages, list) or not agent_messages:
  41. return 0
  42. if result.get('_messages_are_turn_suffix'):
  43. return 0
  44. user_idx = reanchor_current_turn_user_idx(agent_messages, user_message)
  45. if user_idx >= 0:
  46. return user_idx + 1
  47. prior = list(conversation_history)
  48. expected_prefix = prior + [{"role": "user", "content": user_message}]
  49. if _same_transcript_prefix(agent_messages, expected_prefix):
  50. return len(expected_prefix)
  51. if prior and _same_transcript_prefix(agent_messages, prior):
  52. return len(prior)
  53. return 0