|
| 1 | +""" |
| 2 | +AgentFinishedCritic implementation. |
| 3 | +
|
| 4 | +This critic evaluates whether an agent properly finished a task by checking: |
| 5 | +1. The agent's last action was a FinishAction (proper completion) |
| 6 | +2. The generated git patch is non-empty (actual changes were made) |
| 7 | +""" |
| 8 | + |
| 9 | +from collections.abc import Sequence |
| 10 | + |
| 11 | +from openhands.sdk.critic.base import CriticBase, CriticResult |
| 12 | +from openhands.sdk.event import ActionEvent, LLMConvertibleEvent |
| 13 | +from openhands.sdk.logger import get_logger |
| 14 | +from openhands.sdk.tool.builtins.finish import FinishAction |
| 15 | + |
| 16 | + |
| 17 | +logger = get_logger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +class AgentFinishedCritic(CriticBase): |
| 21 | + """ |
| 22 | + Critic that evaluates whether an agent properly finished a task. |
| 23 | +
|
| 24 | + This critic checks two main criteria: |
| 25 | + 1. The agent's last action was a FinishAction (proper completion) |
| 26 | + 2. The generated git patch is non-empty (actual changes were made) |
| 27 | + """ |
| 28 | + |
| 29 | + def evaluate( |
| 30 | + self, events: Sequence[LLMConvertibleEvent], git_patch: str | None = None |
| 31 | + ) -> CriticResult: |
| 32 | + """ |
| 33 | + Evaluate if an agent properly finished with a non-empty git patch. |
| 34 | +
|
| 35 | + Args: |
| 36 | + events: List of events from the agent's execution |
| 37 | + git_patch: Optional git patch generated by the agent |
| 38 | +
|
| 39 | + Returns: |
| 40 | + CriticResult with score 1.0 if successful, 0.0 otherwise |
| 41 | + """ |
| 42 | + reasons = [] |
| 43 | + |
| 44 | + # Check if git patch is non-empty |
| 45 | + if not git_patch or not git_patch.strip(): |
| 46 | + reasons.append("Empty git patch") |
| 47 | + logger.debug("AgentFinishedCritic: Empty git patch") |
| 48 | + return CriticResult( |
| 49 | + score=0.0, |
| 50 | + message="Agent did not produce a non-empty git patch. " |
| 51 | + + "; ".join(reasons), |
| 52 | + ) |
| 53 | + |
| 54 | + # Check if agent properly finished with FinishAction |
| 55 | + if not self._has_finish_action(events): |
| 56 | + reasons.append("No FinishAction found") |
| 57 | + logger.debug("AgentFinishedCritic: No FinishAction") |
| 58 | + return CriticResult( |
| 59 | + score=0.0, |
| 60 | + message="Agent did not finish properly. " + "; ".join(reasons), |
| 61 | + ) |
| 62 | + |
| 63 | + logger.debug("AgentFinishedCritic: Successfully completed") |
| 64 | + return CriticResult( |
| 65 | + score=1.0, |
| 66 | + message="Agent completed with FinishAction and non-empty patch", |
| 67 | + ) |
| 68 | + |
| 69 | + def _has_finish_action(self, events: Sequence[LLMConvertibleEvent]) -> bool: |
| 70 | + """Check if the last action was a FinishAction.""" |
| 71 | + if not events: |
| 72 | + return False |
| 73 | + |
| 74 | + # Look for the last ActionEvent in the history |
| 75 | + for event in reversed(events): |
| 76 | + if isinstance(event, ActionEvent): |
| 77 | + # Check if this is a FinishAction |
| 78 | + if event.action and isinstance(event.action, FinishAction): |
| 79 | + return True |
| 80 | + # If we find any other action type, the agent didn't finish |
| 81 | + return False |
| 82 | + |
| 83 | + return False |
0 commit comments