primary_runtime_snapshot.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """The ``_primary_runtime`` snapshot: the route a fallback turn restores on the next turn.
  2. Both builders (agent construction, and a deliberate model switch) record the custom entry that owns
  3. the route's credential, resolved against the config the route runs on. Restoring scopes the
  4. credential pool by that record, never by a live ``config.yaml`` lookup, so an entry edited or
  5. removed mid-session can't hand the session another account's key.
  6. """
  7. from __future__ import annotations
  8. from typing import Any
  9. from agent.custom_entry_pool import SNAPSHOT_OWNER_KEY, runtime_entry_owner
  10. def _snapshot_primary_runtime(agent):
  11. # Per-turn restoration snapshot: after a fallback, the next turn restores these so the
  12. # preferred model gets a fresh attempt.
  13. _cc = agent.context_compressor
  14. agent._primary_runtime = {
  15. "model": agent.model,
  16. "provider": agent.provider,
  17. "requested_provider": agent.requested_provider,
  18. SNAPSHOT_OWNER_KEY: runtime_entry_owner(agent.provider, agent.requested_provider, agent.base_url),
  19. "base_url": agent.base_url,
  20. "api_mode": agent.api_mode,
  21. "api_key": getattr(agent, "api_key", ""),
  22. "request_overrides": dict(getattr(agent, "request_overrides", {}) or {}),
  23. "client_kwargs": dict(agent._client_kwargs),
  24. "use_prompt_caching": agent._use_prompt_caching,
  25. "use_native_cache_layout": agent._use_native_cache_layout,
  26. "reasoning_echo_flag": getattr(agent, "_reasoning_echo_flag", False),
  27. # Engine state _try_activate_fallback() overwrites (getattr: plugin engines may lack them).
  28. "compressor_model": getattr(_cc, "model", agent.model),
  29. "compressor_base_url": getattr(_cc, "base_url", agent.base_url),
  30. "compressor_api_key": getattr(_cc, "api_key", ""),
  31. "compressor_provider": getattr(_cc, "provider", agent.provider),
  32. "compressor_context_length": _cc.context_length,
  33. "compressor_threshold_tokens": _cc.threshold_tokens,
  34. }
  35. if agent.api_mode == "anthropic_messages":
  36. agent._primary_runtime.update({
  37. "anthropic_api_key": agent._anthropic_api_key,
  38. "anthropic_base_url": agent._anthropic_base_url,
  39. "is_anthropic_oauth": agent._is_anthropic_oauth,
  40. })
  41. def _build_primary_runtime_snapshot(agent, api_mode) -> dict[str, Any]:
  42. """The ``_primary_runtime`` record that persists a switch across turns."""
  43. cc = getattr(agent, "context_compressor", None) or None
  44. rt = {
  45. "model": agent.model,
  46. "provider": agent.provider,
  47. "requested_provider": agent.requested_provider,
  48. SNAPSHOT_OWNER_KEY: runtime_entry_owner(agent.provider, agent.requested_provider, agent.base_url),
  49. "base_url": agent.base_url,
  50. "api_mode": agent.api_mode,
  51. "api_key": getattr(agent, "api_key", ""),
  52. "client_kwargs": dict(agent._client_kwargs),
  53. "use_prompt_caching": agent._use_prompt_caching,
  54. "use_native_cache_layout": agent._use_native_cache_layout,
  55. "reasoning_config": dict(agent.reasoning_config) if getattr(agent, "reasoning_config", None) else None,
  56. "reasoning_echo_flag": getattr(agent, "_reasoning_echo_flag", False),
  57. # Overrides must travel with the switched-to identity or a later recovery/restore resurrects
  58. # PRE-switch overrides from the stale init snapshot.
  59. # See #75091.
  60. "request_overrides": dict(getattr(agent, "request_overrides", {}) or {}),
  61. "runtime_capabilities": dict(getattr(agent, "runtime_capabilities", {}) or {}),
  62. "compressor_model": getattr(cc, "model", agent.model),
  63. "compressor_base_url": getattr(cc, "base_url", agent.base_url),
  64. "compressor_api_key": getattr(cc, "api_key", ""),
  65. "compressor_provider": getattr(cc, "provider", agent.provider),
  66. "compressor_context_length": cc.context_length if cc else 0,
  67. "compressor_api_mode": getattr(cc, "api_mode", agent.api_mode),
  68. "compressor_threshold_tokens": cc.threshold_tokens if cc else 0,
  69. }
  70. if api_mode == "anthropic_messages":
  71. rt.update({
  72. "anthropic_api_key": agent._anthropic_api_key,
  73. "anthropic_base_url": agent._anthropic_base_url,
  74. "is_anthropic_oauth": agent._is_anthropic_oauth,
  75. })
  76. return rt