Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions libs/deepagents/deepagents/middleware/rubric.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,22 @@ def _finalize_evaluation(
(sync `_grade` vs `await _agrade`).
"""
evaluation = self._build_evaluation(graded, grading_run_id, iteration)
if graded.result == "needs_revision" and iteration + 1 >= self.max_iterations:
# Emit and persist the terminal status rather than a misleading
# `needs_revision` event that the middleware will not actually loop on.
logger.warning(
"RubricMiddleware exhausted max_iterations=%d without 'satisfied' verdict (grading_run_id=%s)",
self.max_iterations,
evaluation["grading_run_id"],
)
evaluation["result"] = "max_iterations_reached"
self._emit(runtime, "rubric_evaluation_end", grading_run_id, iteration, evaluation)
if self._on_evaluation is not None:
try:
self._on_evaluation(evaluation)
except Exception:
logger.exception("RubricMiddleware on_evaluation callback raised")
return self._compose_update(state, evaluation, graded.result)
return self._compose_update(state, evaluation)

def _ensure_grader(self) -> Any: # noqa: ANN401
if self._grader is not None:
Expand Down Expand Up @@ -626,7 +635,6 @@ def _compose_update(
self,
state: RubricState,
evaluation: RubricEvaluation,
graded_result: GraderVerdict,
) -> dict[str, Any]:
iteration = evaluation["iteration"]
next_iteration = iteration + 1
Expand All @@ -638,24 +646,7 @@ def _compose_update(
"_rubric_status": evaluation["result"],
}

if graded_result == "satisfied":
return update

if graded_result == "failed":
update["_rubric_status"] = "failed"
return update

# needs_revision
if next_iteration >= self.max_iterations:
# Default logging level is WARNING, so this surfaces under
# the default config -- the alternative would be silent: see
# the class docstring "Observing non-satisfied terminations".
logger.warning(
"RubricMiddleware exhausted max_iterations=%d without 'satisfied' verdict (grading_run_id=%s)",
self.max_iterations,
evaluation["grading_run_id"],
)
update["_rubric_status"] = "max_iterations_reached"
if evaluation["result"] != "needs_revision":
return update

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,11 +784,16 @@ def test_logger_warning_emitted_when_cap_hits(
) -> None:
"""The cap fires a `WARNING`-level log so it's visible under default config.

The terminal `max_iterations_reached` status lives in private state,
so this log is the only out-of-band signal a caller gets without
explicitly opting into `on_evaluation` or the stream event.
The terminal `max_iterations_reached` status is visible through state,
callbacks, stream events, and a warning log.
"""
mw = RubricMiddleware(model=_STUB_MODEL, max_iterations=1)
events: list[dict[str, Any]] = []
seen: list[RubricEvaluation] = []
mw = RubricMiddleware(
model=_STUB_MODEL,
max_iterations=1,
on_evaluation=seen.append,
)
_stub_grader(
mw,
monkeypatch,
Expand All @@ -806,8 +811,12 @@ def test_logger_warning_emitted_when_cap_hits(
"_rubric_iterations": 0,
}
with caplog.at_level("WARNING", logger="deepagents.middleware.rubric"):
update = mw.after_agent(state, _runtime())
update = mw.after_agent(state, _runtime(events))
assert update is not None
assert update["_rubric_status"] == "max_iterations_reached"
assert update["_rubric_evaluations"][0]["result"] == "max_iterations_reached"
assert "jump_to" not in update
assert events[-1]["type"] == "rubric_evaluation_end"
assert events[-1]["result"] == "max_iterations_reached"
assert seen[0]["result"] == "max_iterations_reached"
assert any("exhausted max_iterations" in rec.message and "grading-cap" in rec.message for rec in caplog.records)
3 changes: 2 additions & 1 deletion libs/deepagents/tests/unit_tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -4350,7 +4350,8 @@ def test_max_iterations_reached(self) -> None:
assert state["_rubric_status"] == "max_iterations_reached"
assert state["_rubric_iterations"] == 2
assert len(state["_rubric_evaluations"]) == 2
assert all(e["result"] == "needs_revision" for e in state["_rubric_evaluations"])
results = [e["result"] for e in state["_rubric_evaluations"]]
assert results == ["needs_revision", "max_iterations_reached"]

def test_no_rubric_is_noop(self) -> None:
"""Without a rubric on invocation state the middleware does not call the grader."""
Expand Down