session_cold_review.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """``/review`` and messaging ``/refine`` for a session whose cached agent is gone.
  2. The gateway drops a session's cached agent after an idle hour (``agent.agent_cache.idle_ttl_secs``),
  3. under LRU or memory pressure, and around ``/moa``. The conversation outlives it in the session's
  4. stored transcript, but ``/review`` read only the cached agent, so a session left alone for an hour
  5. answered "Nothing to review yet" until the next message rebuilt one. A cold ``/review`` reviews the
  6. stored transcript and dispatches the reviewer from a fresh agent on the route, toolsets and session
  7. id the session's next turn would use, so its completion comes back exactly as a resident agent's.
  8. """
  9. import asyncio
  10. import concurrent.futures
  11. import contextlib
  12. import logging
  13. logger = logging.getLogger(__name__)
  14. async def cold_review_history(runner, source):
  15. """``(session_id, transcript)`` of the session ``source`` belongs to: a local session's current
  16. physical transcript (``gateway/session_refine.py`` reads the same one), else the messaging entry's."""
  17. from gateway.session_refine import _local_session
  18. authority, live = _local_session(runner, source)
  19. if live is not None:
  20. from gateway.session_contract import SessionRef
  21. from gateway.session_local_recovery import transcript_target
  22. def load():
  23. target = transcript_target(authority, SessionRef(authority.profile_id, source.chat_id))
  24. return target, authority.db.get_messages_as_conversation(target, include_ancestors=True)
  25. return await asyncio.to_thread(load)
  26. entry = await runner.async_session_store.get_or_create_session(source)
  27. return entry.session_id, await runner.async_session_store.load_transcript(entry.session_id)
  28. async def stored_transcript(runner, session_key):
  29. """``(session_id, transcript)`` of a messaging session; the transcript is empty when the chat has no
  30. session or no user/assistant turn. Read-only: ``/refine`` in a new chat must not create a session."""
  31. session_id = await runner.async_session_store.peek_session_id(session_key)
  32. history = await runner.async_session_store.load_transcript(session_id) if session_id else []
  33. if not any(m.get("role") in {"user", "assistant"} for m in history):
  34. return session_id, []
  35. return session_id, list(history)
  36. @contextlib.contextmanager
  37. def cold_review_parent(runner, source, session_key, session_id):
  38. """A fresh agent on the session's next-turn route (built as ``session_execution._build_fresh_agent``
  39. builds one, minus the turn-only prompt pieces), inside the session's policy scope. On exit only its
  40. own LLM clients are released: ``close()`` would tear down the session's terminal and browser, and the
  41. reviewer it dispatched is detached and builds its own clients."""
  42. from gateway.run import _load_gateway_config, _platform_config_key
  43. from gateway.session_api_turn import api_runtime_locked
  44. from gateway.session_authorities import active_authority
  45. from gateway.session_policy import policy_for_source, policy_scope, policy_turn_toolsets
  46. from gateway.session_worker_construct import session_prefill, session_routing
  47. from run_agent import AIAgent
  48. policy = policy_for_source(runner, source)
  49. authority = active_authority(runner) if policy else None
  50. user_config = policy.config(authority, secrets=False) if policy else _load_gateway_config()
  51. platform_key = policy.platform if policy else _platform_config_key(source.platform)
  52. enabled, disabled = runner._resolve_turn_toolsets(user_config, source, platform_key)
  53. if policy:
  54. enabled = policy_turn_toolsets(authority, policy, user_config, source.chat_id)
  55. # Each input below is the one the turn runner reads for this session (``run_turn_runner``).
  56. pr = session_routing(authority, policy) if policy else runner._provider_routing
  57. prefill = session_prefill(authority, policy, source.chat_id) if policy else runner._prefill_messages
  58. tier = (policy.current_service_tier(runner, session_key) if policy else
  59. runner._resolve_session_service_tier(source=source, session_key=session_key))
  60. with policy_scope(policy, authority=authority):
  61. model, runtime = runner._resolve_session_agent_runtime(
  62. source=source, session_key=session_key, user_config=user_config)
  63. route = runner._resolve_turn_agent_config("", model, runtime, tier=tier) # /fast request options
  64. agent = AIAgent(
  65. model=route["model"], **route["runtime"], quiet_mode=True, verbose_logging=False,
  66. enabled_toolsets=enabled, disabled_toolsets=disabled,
  67. # Launch-preloaded skills (``hermes -s``): the reviewer is told to load them too.
  68. ephemeral_system_prompt=(policy.skills_prompt if policy else None) or None,
  69. prefill_messages=prefill or None,
  70. reasoning_config=runner._resolve_session_reasoning_config(source=source, session_key=session_key,
  71. model=model),
  72. service_tier=tier, request_overrides=route.get("request_overrides"),
  73. providers_allowed=pr.get("only"), providers_ignored=pr.get("ignore"), providers_order=pr.get("order"),
  74. provider_sort=pr.get("sort"), provider_require_parameters=pr.get("require_parameters", False),
  75. provider_data_collection=pr.get("data_collection"),
  76. session_id=session_id, platform=platform_key, gateway_session_key=session_key,
  77. session_db=getattr(runner._session_db, "_db", runner._session_db),
  78. # The session's backup providers, as its next turn would carry them: the reviewer inherits the chain.
  79. fallback_model=None if api_runtime_locked() else runner._refresh_fallback_model(),
  80. # The reviewer reads the transcript excerpt; this agent never runs a turn of its own.
  81. skip_memory=True)
  82. agent._end_session_on_close = False
  83. try:
  84. yield agent
  85. finally:
  86. with contextlib.suppress(Exception):
  87. agent.release_clients()
  88. def start_cold_refine(runner, source, session_key, session_id, history, focus):
  89. """``/refine`` for a messaging session whose cached agent is gone: the resident path's own
  90. ``_spawn_background_review`` call, made from the cold parent. A resident agent sends its review
  91. summary into the chat through its last turn's callback (``run_turn_runner._make_bg_review_callbacks``);
  92. this one sends it to the same chat and thread directly. Returns a future that resolves once the review
  93. is spawned (or raises its start error); the parent's clients are released when the review's request ends,
  94. on the thread that built it."""
  95. from agent.async_utils import safe_schedule_threadsafe
  96. from agent.memory_provider import spawn_context_thread
  97. from gateway.run import _interim_metadata, _non_conversational_metadata
  98. loop = asyncio.get_running_loop()
  99. adapter = runner._delivery_adapter_for(source)
  100. metadata = _interim_metadata(_non_conversational_metadata(
  101. runner._thread_metadata_for_source(source), platform=source.platform))
  102. started = concurrent.futures.Future()
  103. def deliver(message):
  104. if adapter is not None:
  105. safe_schedule_threadsafe(adapter.send(source.chat_id, str(message), metadata=metadata), loop,
  106. logger=logger, log_message="cold /refine summary delivery scheduling failed",
  107. log_level=logging.WARNING)
  108. def body():
  109. try:
  110. with cold_review_parent(runner, source, session_key, session_id) as parent:
  111. parent.background_review_callback = deliver
  112. parent._spawn_background_review(
  113. messages_snapshot=history, review_memory=True,
  114. review_skills="skill_manage" in getattr(parent, "valid_tool_names", set()), focus=focus or None,
  115. explicit=True)
  116. started.set_result(None)
  117. run = getattr(parent, "_background_review_run", None)
  118. if run is not None:
  119. run.request_done.wait()
  120. except Exception as exc: # health: allow BLE001 -- the start error becomes the /refine reply (start_failed)
  121. if started.done():
  122. logger.warning("cold /refine failed after it started", exc_info=True)
  123. else:
  124. started.set_exception(exc)
  125. spawn_context_thread(body, name="cold-refine").start()
  126. return asyncio.wrap_future(started)