diff --git a/packages/yugos-evals/src/yugos_evals/scorers/_outcome.py b/packages/yugos-evals/src/yugos_evals/scorers/_outcome.py index 335925a..b97cdf2 100644 --- a/packages/yugos-evals/src/yugos_evals/scorers/_outcome.py +++ b/packages/yugos-evals/src/yugos_evals/scorers/_outcome.py @@ -72,14 +72,22 @@ def empty_answer() -> Outcome: return Outcome(GradeCode.INCORRECT, details={"cause": "empty_answer"}) +def _message_limit_reached(state: TaskState) -> bool: + """Inspect ends a sample at its message limit with the conversation at that size.""" + limit = state.message_limit + return limit is not None and len(state.messages) >= limit + + def completion_gate(state: TaskState) -> Outcome | None: """The outcome for an answer that must not be graded, as Yugos grading gates it. A sample that ended at a limit before the router answered has no completion and is not attempted; a truncated answer is a graded zero; an answer the - provider blocked is graded incorrect; a completion that ended for another - reason than `stop` (a tool call) needs review. An empty answer that stopped - normally (the model may have only reasoned) is an attempt, graded incorrect. + provider blocked is graded incorrect; an agent still calling a tool when its + message limit ended the sample gave no final answer and is graded incorrect; + a completion that ended for another reason than `stop` needs review. An empty + answer that stopped normally (the model may have only reasoned) is an + attempt, graded incorrect. """ if not state.output.choices: return Outcome(GradeCode.NOT_ATTEMPTED, details={"cause": "no_response"}) @@ -90,6 +98,11 @@ def completion_gate(state: TaskState) -> Outcome | None: ) if reason in _BLOCKED: return Outcome(GradeCode.INCORRECT, details={"cause": "provider_blocked"}) + if reason == "tool_calls" and _message_limit_reached(state): + return Outcome( + GradeCode.INCORRECT, + details={"cause": "message_limit_reached", "stop_reason": reason}, + ) if reason != "stop": return Outcome( GradeCode.NEEDS_REVIEW, diff --git a/packages/yugos-evals/tests/test_completion_gate.py b/packages/yugos-evals/tests/test_completion_gate.py new file mode 100644 index 0000000..dcd1c01 --- /dev/null +++ b/packages/yugos-evals/tests/test_completion_gate.py @@ -0,0 +1,75 @@ +"""The completion gate grades an agent stopped at its message limit mid tool call.""" + +import pytest +from evals_fakes import task_state +from inspect_ai import Task +from inspect_ai import eval as inspect_eval +from inspect_ai.dataset import Sample +from inspect_ai.model import ChatMessageUser, ModelOutput, get_model +from inspect_ai.scorer import scorer +from inspect_ai.solver import generate, use_tools +from inspect_ai.tool import tool + +from yugos.evaluation.grades import GradeCode +from yugos_evals.scorers._outcome import completion_gate, to_score + + +def tool_call_state(messages, message_limit): + state = task_state("", stop_reason="tool_calls") + state.messages = [ChatMessageUser(content="q") for _ in range(messages)] + state.message_limit = message_limit + return state + + +def test_tool_call_at_the_message_limit_is_graded_incorrect(): + outcome = completion_gate(tool_call_state(20, 20)) + assert outcome.code == GradeCode.INCORRECT + assert outcome.status == "graded" + assert outcome.details == {"cause": "message_limit_reached", "stop_reason": "tool_calls"} + + +@pytest.mark.parametrize("messages,limit", [(19, 20), (5, None)]) +def test_other_tool_call_endings_still_need_review(messages, limit): + outcome = completion_gate(tool_call_state(messages, limit)) + assert outcome.code == GradeCode.NEEDS_REVIEW + assert outcome.status == "unavailable" + + +def test_an_agent_stopped_by_inspect_at_its_message_limit_is_graded(tmp_path): + @tool + def lookup(): + async def run(key: str) -> str: + """Look up a key. + + Args: + key: The key. + """ + return "value" + + return run + + @scorer(metrics=[]) + def gated(): + async def score(state, target): + return to_score(completion_gate(state)) + + return score + + calls = [ModelOutput.for_tool_call("mockllm/model", "lookup", {"key": "k"})] * 20 + task = Task( + dataset=[Sample(input="q", target="x")], + solver=[use_tools(lookup()), generate()], + scorer=gated(), + message_limit=6, + ) + [log] = inspect_eval( + task, + model=get_model("mockllm/model", custom_outputs=calls), + display="none", + log_dir=str(tmp_path), + ) + [sample] = log.samples + assert sample.limit.type == "message" + [score] = sample.scores.values() + assert score.value == 0 + assert score.metadata["code"] == GradeCode.INCORRECT