moa_alternation.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """Reactive same-role merge for the MoA aggregator request (#112358, last atom).
  2. The aggregator request deliberately ends ``user(task), user(guidance)`` on iteration 1 of a
  3. turn: the guidance is its own trailing message so every earlier message stays byte-stable and
  4. the provider prefix cache keeps growing (``moa_loop._attach_reference_guidance``). Strict-
  5. alternation chat templates (llama.cpp / vLLM Jinja templates, Mistral, some OpenRouter routes)
  6. 400 on that adjacency. Merging proactively for everyone would re-introduce the divergence the
  7. split shape removed and only move the 400, so the merge is reactive and destination-scoped:
  8. * a 400 classified ``FailoverReason.role_alternation`` → retry ONCE with adjacent same-role
  9. messages merged;
  10. * the destination (``base_url``/provider + model) is remembered on the facade for the rest of
  11. the session, so later iterations pre-merge for it and never pay the 400 again;
  12. * destinations that accepted the split shape are never touched — their prefix stays byte-stable.
  13. """
  14. from __future__ import annotations
  15. import logging
  16. from typing import Any
  17. logger = logging.getLogger(__name__)
  18. def destination_key(runtime: dict[str, Any]) -> tuple[str, str]:
  19. """``(route, model)`` identity of an aggregator destination: the base_url when the slot
  20. resolved one (two providers can share a model id), else the provider slug."""
  21. route = str(runtime.get("base_url") or runtime.get("provider") or "").strip().rstrip("/")
  22. return route, str(runtime.get("model") or "").strip()
  23. def merge_same_role_messages(messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
  24. """Return ``messages`` with adjacent user turns folded into one (the only same-role adjacency
  25. the aggregator request produces); other rows are shared, never mutated. Returns the input
  26. object itself when nothing merged so callers can detect a no-op."""
  27. from agent.agent_runtime_helpers import _UNMERGEABLE, _merge_user_content
  28. merged: list[dict[str, Any]] = []
  29. changed = False
  30. for message in messages:
  31. prev = merged[-1] if merged else None
  32. content: Any = _UNMERGEABLE
  33. if prev is not None and prev.get("role") == "user" and message.get("role") == "user":
  34. content = _merge_user_content(prev.get("content", ""), message.get("content", ""))
  35. if content is _UNMERGEABLE:
  36. merged.append(message)
  37. continue
  38. merged[-1] = {**prev, "content": content}
  39. changed = True
  40. return merged if changed else messages
  41. def is_role_alternation_rejection(exc: Exception, runtime: dict[str, Any]) -> bool:
  42. """True when the aggregator destination rejected the request for adjacent same-role messages."""
  43. from agent.error_classifier import FailoverReason, classify_api_error
  44. try:
  45. classified = classify_api_error(
  46. exc, provider=str(runtime.get("provider") or ""), model=str(runtime.get("model") or ""),
  47. base_url=str(runtime.get("base_url") or ""),
  48. )
  49. except Exception: # pragma: no cover - classification must never mask the original error
  50. return False
  51. return classified.reason is FailoverReason.role_alternation