Coverage for app / agents / validator.py: 97%

36 statements  

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

1""" 

2ValidatorAgent — quality checking and output validation. 

3""" 

4 

5from __future__ import annotations 

6 

7from typing import Any 

8 

9from langchain_core.messages import BaseMessage 

10from pydantic import BaseModel, Field 

11 

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

13from app.agents.enums import AgentRole 

14from app import prompts 

15from app.util import vocab 

16 

17 

18class ValidationResult(BaseModel): 

19 """Structured output produced by a ValidatorAgent.""" 

20 

21 is_valid: bool = False 

22 status: str = Field( 

23 default="pending", 

24 description=f"Verdict: {vocab.listing(prompts.VALIDATOR_STATUSES)}", 

25 ) 

26 issues: list[str] = Field(default_factory=list) 

27 recommendations: list[str] = Field(default_factory=list) 

28 quality_score: float = Field(default=0.0, ge=0.0, le=10.0) 

29 raw_output: str = Field(default="") 

30 

31 

32class ValidatorAgent(WorkerAgent): 

33 """ 

34 Base agent for validation and quality-assurance tasks. 

35 

36 Returns a structured ValidationResult. Subclass for domain-specific 

37 validators, e.g. SchemaValidatorAgent, FactCheckerAgent — override 

38 ``_build_messages`` to inject domain-specific criteria. 

39 """ 

40 

41 DEFAULT_ROLE = AgentRole.VALIDATOR 

42 DEFAULT_SYSTEM_PROMPT = prompts.VALIDATOR 

43 DEFAULT_MAX_ITERATIONS = 5 

44 

45 def __init__(self, name: str = "validator", **kwargs: Any) -> None: 

46 super().__init__(name=name, **kwargs) 

47 self.add_capability( 

48 AgentCapability( 

49 name="quality_validation", 

50 description="Validate output quality and correctness", 

51 required=True, 

52 ) 

53 ) 

54 self.add_capability( 

55 AgentCapability( 

56 name="compliance_check", 

57 description="Check compliance with stated requirements", 

58 ) 

59 ) 

60 

61 async def execute(self, input_data: Any) -> ValidationResult: 

62 messages = self._build_messages(input_data) 

63 if self.tools: 

64 output, _ = await self.run_with_tools(messages) 

65 else: 

66 llm = self.resolve_llm() 

67 response = await llm.ainvoke(messages) 

68 output = reply_text(llm, response, self.name) 

69 # The verdict is the final line, by the protocol VALIDATOR states; the 

70 # issues above it say "fails" freely, so they are not read as one. No 

71 # verdict at all is not a pass: nothing was certified. 

72 status = ( 

73 prompts.final_verdict(output, prompts.VALIDATOR_STATUSES) 

74 or prompts.VALIDATOR_STATUS_FAIL 

75 ) 

76 is_valid = status != prompts.VALIDATOR_STATUS_FAIL 

77 return ValidationResult( 

78 is_valid=is_valid, 

79 status=status, 

80 raw_output=output, 

81 ) 

82 

83 def _build_messages(self, input_data: Any) -> list[BaseMessage]: 

84 # The report's shape is the system prompt's; this only hands over the 

85 # content and restates the line the verdict is read from. 

86 user_content = ( 

87 f"Content to validate:\n{input_data}\n\n" 

88 "Report as instructed, ending with the verdict line — exactly one of: " 

89 f"{vocab.listing(prompts.VALIDATOR_STATUSES)}" 

90 ) 

91 return self.build_messages(user_content)