review_engine.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. """Shared engine for the /review command — every surface calls this.
  2. /review spawns an independent, full-privilege background subagent (the same async rail as
  3. ``delegate_task(background=true)``) to review whatever the recent conversation presented;
  4. its result re-enters the spawning session as a normal async-delegation completion.
  5. Model routing: ``auxiliary.review`` when configured, else the parent agent's credentials,
  6. passed as ``credentials_cfg`` to ``delegate_task`` so native-SDK providers, api_mode
  7. detection and credential pools behave identically to ``delegation.provider`` pins.
  8. Surfaces (CLI/gateway ``/review``, TUI/Desktop) snapshot, call :func:`start_review`, print the note.
  9. """
  10. from __future__ import annotations
  11. import json
  12. import logging
  13. import re
  14. from typing import Any, Dict, List, Optional
  15. logger = logging.getLogger(__name__)
  16. # How many recent chat messages (user + assistant turns) the reviewer gets.
  17. DEFAULT_CONTEXT_MESSAGES = 10
  18. # Per-message excerpt cap: generous (a PR summary/diff excerpt is exactly what the
  19. # reviewer needs) but bounded against a pathological turn.
  20. _MESSAGE_CHAR_CAP = 12_000
  21. _REVIEW_GOAL = (
  22. "Act as an independent senior reviewer. Thoroughly review the work presented in the conversation excerpt "
  23. "provided in your context: investigate any code, pull request, branch, commit, documentation, design, or other "
  24. "artifact it references (open the PR, read the diff, run the code or tests where feasible) rather than judging "
  25. "from the excerpt alone. Produce a full, structured review: what the work does, whether it is correct and "
  26. "complete, concrete defects or risks found (with file/line references where possible), what was verified vs. "
  27. "only read, and a clear final verdict with recommended next steps."
  28. )
  29. def _message_text(message: dict[str, Any]) -> str:
  30. """Display text of a message; multimodal parts are joined, non-text parts noted."""
  31. content = message.get("content")
  32. if isinstance(content, str):
  33. return content
  34. if isinstance(content, list):
  35. parts = [str(part.get("text") or "") if part.get("type") == "text" else f"[{part.get('type', 'attachment')}]"
  36. for part in content if isinstance(part, dict)]
  37. return "\n".join(p for p in parts if p)
  38. return ""
  39. def snapshot_recent_messages(messages: list[dict[str, Any]], limit: int = DEFAULT_CONTEXT_MESSAGES) -> list[dict[str, str]]:
  40. """Last ``limit`` user/assistant messages with text as {role, text}, oldest first (system, tool and
  41. pure tool-call stubs excluded)."""
  42. out: list[dict[str, str]] = []
  43. for message in reversed(list(messages or [])):
  44. if not isinstance(message, dict):
  45. continue
  46. role = str(message.get("role") or "")
  47. text = _message_text(message).strip() if role in ("user", "assistant") else ""
  48. if not text:
  49. continue
  50. if len(text) > _MESSAGE_CHAR_CAP:
  51. text = text[:_MESSAGE_CHAR_CAP] + "\n[... truncated ...]"
  52. out.append({"role": role, "text": text})
  53. if len(out) >= limit:
  54. break
  55. out.reverse()
  56. return out
  57. def collect_parent_loaded_skills(parent_agent, messages: list[dict[str, Any]], limit: int = 8) -> list[str]:
  58. """Skills the parent was operating under: launch-preloaded (marker in ``ephemeral_system_prompt``)
  59. first, then ``skill_view`` loads from history, deduped, capped at ``limit`` (a reviewer told to load 30
  60. skills would burn its budget before working)."""
  61. names: list[str] = []
  62. prompt = str(getattr(parent_agent, "ephemeral_system_prompt", "") or "")
  63. candidates = [m.group(1) for m in re.finditer(r'with the "([^"]+)" skill\s+preloaded', prompt)]
  64. for message in messages or []:
  65. if not isinstance(message, dict) or message.get("role") != "assistant":
  66. continue
  67. for tool_call in message.get("tool_calls") or []:
  68. fn = tool_call.get("function") or {} if isinstance(tool_call, dict) else {}
  69. if fn.get("name") != "skill_view":
  70. continue
  71. try:
  72. args = json.loads(fn.get("arguments") or "{}")
  73. except Exception:
  74. continue
  75. # Only whole-skill loads seed the reviewer; a reference-file read is a detail
  76. # of the parent's task covered by loading the SKILL.md.
  77. if isinstance(args, dict) and not args.get("file_path"):
  78. candidates.append(str(args.get("name") or ""))
  79. for name in candidates:
  80. cleaned = name.strip()
  81. if cleaned and cleaned not in names:
  82. names.append(cleaned)
  83. return names[:limit]
  84. def build_review_task(snapshot: list[dict[str, str]], user_prompt: str = "", loaded_skills: Optional[list[str]] = None) -> tuple:
  85. """Compose a viewer-friendly goal and the complete reviewer briefing."""
  86. focus = " ".join(user_prompt.split())
  87. goal = f"Review: {focus}" if focus else "Review recent work"
  88. if len(goal) > 80:
  89. goal = goal[:79].rstrip() + "…"
  90. # The goal is also the live worker label; keep the full instructions in context.
  91. lines = [
  92. _REVIEW_GOAL,
  93. "",
  94. "You were spawned by the /review command. The following is an excerpt of the most recent conversation "
  95. "between the user and their primary agent. It is your starting evidence — the work to "
  96. "review is referenced in it.",
  97. "",
  98. "--- Recent conversation (oldest first) ---",
  99. ]
  100. for message in snapshot:
  101. lines += [f"[{'USER' if message['role'] == 'user' else 'PRIMARY AGENT'}]", message["text"], ""]
  102. lines.append("--- End of conversation excerpt ---")
  103. if loaded_skills:
  104. skill_list = ", ".join(loaded_skills)
  105. lines += [
  106. "",
  107. "The primary agent was operating under these loaded skills: "
  108. f"{skill_list}. Before reviewing, load each with "
  109. "skill_view(name=...) and treat their conventions, invariants, "
  110. "and review standards as binding for your assessment — the work "
  111. "was produced under them and must be judged against them.",
  112. ]
  113. if user_prompt.strip():
  114. lines += ["", "Additional review instructions from the user:", user_prompt.strip()]
  115. lines += [
  116. "",
  117. "Your review is delivered back into that conversation, addressed to "
  118. "the primary agent and its user. Be direct and specific; do not "
  119. "soften findings.",
  120. ]
  121. return goal, "\n".join(lines)
  122. def _load_review_credentials_cfg() -> Optional[dict[str, Any]]:
  123. """``auxiliary.review`` as a delegation-credentials dict, or None when unconfigured (provider auto/empty
  124. and no model/base_url) so the reviewer inherits the parent's credentials."""
  125. try:
  126. from hermes_cli.config import load_config_readonly
  127. review = (load_config_readonly().get("auxiliary") or {}).get("review") or {}
  128. except Exception:
  129. return None
  130. if not isinstance(review, dict):
  131. return None
  132. cfg = {k: str(review.get(k) or "").strip() for k in ("provider", "model", "base_url", "api_key", "api_mode")}
  133. if cfg["provider"].lower() == "auto":
  134. cfg["provider"] = ""
  135. if not (cfg["provider"] or cfg["model"] or cfg["base_url"]):
  136. return None
  137. return cfg
  138. def start_review(parent_agent, messages: list[dict[str, Any]], user_prompt: str = "") -> dict[str, Any]:
  139. """Dispatch the reviewer subagent; returns the parsed ``delegate_task`` dict (``status: "dispatched"`` +
  140. ``delegation_id``, or the synchronous result on channels without async completions). Raises ValueError
  141. when there is nothing to review or the dispatch is rejected/errored."""
  142. if parent_agent is None:
  143. raise ValueError("No active agent — send a message first.")
  144. snapshot = snapshot_recent_messages(messages)
  145. if not snapshot:
  146. raise ValueError("Nothing to review yet — the conversation is empty.")
  147. goal, context = build_review_task(snapshot, user_prompt, collect_parent_loaded_skills(parent_agent, messages))
  148. credentials_cfg = _load_review_credentials_cfg()
  149. from tools.delegate_tool import delegate_task
  150. raw = delegate_task(goal=goal, context=context, background=True, parent_agent=parent_agent, credentials_cfg=credentials_cfg)
  151. try:
  152. result = json.loads(raw)
  153. except Exception:
  154. result = None
  155. if isinstance(result, dict) and result.get("error"):
  156. raise ValueError(str(result["error"]))
  157. if not isinstance(result, dict):
  158. raise ValueError(f"Review dispatch failed: {raw!r}")
  159. result.setdefault("review_model", (credentials_cfg or {}).get("model") or "")
  160. return result
  161. def format_dispatch_note(result: dict[str, Any], user_prompt: str = "") -> str:
  162. """Human-facing one-liner for a successful dispatch. Shared by surfaces."""
  163. if result.get("status") == "dispatched":
  164. return "Review started. Results will return here."
  165. model = str(result.get("review_model") or "").strip()
  166. model_note = f" on {model}" if model else ""
  167. focus_note = f" (focus: {user_prompt.strip()})" if user_prompt.strip() else ""
  168. # Synchronous fallback (channels that cannot route async completions).
  169. return (
  170. f"⚖ Review completed synchronously{model_note}{focus_note} — "
  171. f"results:\n{json.dumps(result.get('results', result), ensure_ascii=False)[:4000]}"
  172. )