Coverage for app / agents / reviewer.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1""" 

2ReviewerAgent — critically reviews prior agent output within a graph. 

3 

4Unlike ValidatorAgent (which checks correctness), ReviewerAgent provides 

5qualitative critique, identifies gaps, and decides whether the output 

6is ready to proceed or needs revision. 

7""" 

8 

9from __future__ import annotations 

10 

11from typing import Any 

12 

13from pydantic import BaseModel 

14 

15from app.agents.base import WorkerAgent, AgentCapability, reply_text 

16from app.agents.enums import AgentRole 

17from app import prompts 

18 

19 

20class ReviewOutcome(str): 

21 APPROVED = "approved" 

22 NEEDS_REVISION = "needs_revision" 

23 REJECTED = "rejected" 

24 

25 

26class ReviewResult(BaseModel): 

27 outcome: str = ReviewOutcome.APPROVED # approved | needs_revision | rejected 

28 summary: str = "" 

29 strengths: list[str] = [] 

30 issues: list[str] = [] 

31 suggestions: list[str] = [] 

32 confidence: str = "medium" # high | medium | low 

33 raw_output: str = "" 

34 

35 

36class ReviewerAgent(WorkerAgent): 

37 """ 

38 Critically reviews the output of a preceding agent node. 

39 

40 Examines work for quality, completeness, accuracy, and alignment 

41 with the original goal. Returns a structured ReviewResult including 

42 an outcome verdict (approved / needs_revision / rejected) that 

43 downstream conditional edges can route on. 

44 

45 Typical position in a graph: after an EXECUTOR or WORKER node, 

46 before deciding whether to loop back for revision or proceed. 

47 """ 

48 

49 DEFAULT_ROLE = AgentRole.REVIEWER 

50 DEFAULT_SYSTEM_PROMPT = prompts.REVIEWER 

51 DEFAULT_MAX_ITERATIONS = 5 

52 

53 def __init__(self, *args: Any, **kwargs: Any) -> None: 

54 super().__init__(*args, **kwargs) 

55 self.add_capability( 

56 AgentCapability( 

57 name="critical_review", 

58 description="Assesses prior output for quality and completeness", 

59 required=True, 

60 ) 

61 ) 

62 self.add_capability( 

63 AgentCapability( 

64 name="revision_decision", 

65 description="Decides whether to approve, request revision, or reject", 

66 required=True, 

67 ) 

68 ) 

69 

70 async def execute(self, input_data: Any) -> ReviewResult: 

71 llm = self.resolve_llm() 

72 messages = self.build_messages( 

73 f"Please review the following output:\n\n{input_data}" 

74 ) 

75 response = await llm.ainvoke(messages) 

76 content: str = ( 

77 reply_text(llm, response, self.name) 

78 if hasattr(response, "content") 

79 else str(response) 

80 ) 

81 

82 # The verdict is the final line, by the protocol REVIEWER states — the 

83 # issues above it quote the work and may say "rejected" of anything. No 

84 # verdict at all is not an approval: it asks for another pass. 

85 outcome = { 

86 prompts.REVIEWER_VERDICT_APPROVED: ReviewOutcome.APPROVED, 

87 prompts.REVIEWER_VERDICT_NEEDS_REVISION: ReviewOutcome.NEEDS_REVISION, 

88 prompts.REVIEWER_VERDICT_REJECTED: ReviewOutcome.REJECTED, 

89 }.get( 

90 prompts.final_verdict(content, prompts.REVIEWER_VERDICTS), 

91 ReviewOutcome.NEEDS_REVISION, 

92 ) 

93 

94 # Extract bullet-point sections heuristically 

95 lines = [ln.strip() for ln in content.splitlines() if ln.strip()] 

96 issues: list[str] = [] 

97 suggestions: list[str] = [] 

98 strengths: list[str] = [] 

99 in_section = None 

100 for line in lines: 

101 ll = line.lower() 

102 if "strength" in ll or "positive" in ll or "good" in ll: 

103 in_section = "strengths" 

104 elif "issue" in ll or "problem" in ll or "gap" in ll or "weakness" in ll: 

105 in_section = "issues" 

106 elif "suggestion" in ll or "recommend" in ll or "improve" in ll: 

107 in_section = "suggestions" 

108 elif line.startswith(("-", "*", "•")) or ( 

109 len(line) > 2 and line[0].isdigit() and line[1] in ".):" 

110 ): 

111 item = line.lstrip("-*•0123456789.) ").strip() 

112 if in_section == "strengths": 

113 strengths.append(item) 

114 elif in_section == "issues": 

115 issues.append(item) 

116 elif in_section == "suggestions": 

117 suggestions.append(item) 

118 

119 return ReviewResult( 

120 outcome=outcome, 

121 summary=lines[0] if lines else "", 

122 strengths=strengths, 

123 issues=issues, 

124 suggestions=suggestions, 

125 confidence="high" if len(issues) == 0 else "medium", 

126 raw_output=content, 

127 )