AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(copilot): give the content judge room to think and one retry on an empty answer (#15007)
Reinier van der Leer005ccd847910
diff --git a/autogpt_platform/backend/backend/copilot/gate/content.py b/autogpt_platform/backend/backend/copilot/gate/content.pyindex d8163e37fa..33a5a099ec 100644--- a/autogpt_platform/backend/backend/copilot/gate/content.py+++ b/autogpt_platform/backend/backend/copilot/gate/content.py@@ -26,6 +26,9 @@ CONTENT_RUBRIC = (Path(__file__).parent / "content_rubric.txt").read_text(_UNCHECKED = "this content could not be checked for instructions"_NO_PASSAGE = "the judge flagged this content but quoted no passage"+# Sonnet 5's adaptive thinking spends from this budget and is not cut to fit it;+# at 200 it used all of it on instruction-shaped reads and answered nothing.+_MAX_TOKENS = 1024class Image(BaseModel):@@ -47,10 +50,6 @@ async def judge_content(*, source: str, text: str, images: tuple[Image, ...] = ()) -> ContentVerdict:"""Anything but a well-formed "clean" holds; ``judged=False`` marks a failure."""- # Deferred, and private: copilot.service imports the tool registry, whose- # BaseTool imports this gate.- from backend.copilot.service import _get_aux_client-body = fence("FETCHED CONTENT", f"source: {source}\n\n{text}")content: str | list[dict[str, Any]] = bodyif images:@@ -61,26 +60,24 @@ async def judge_content(}for i in images]- try:- response = await asyncio.wait_for(- call_provider_openai_compat_sync(- client=_get_aux_client(),- model=config.gate_content_model,- messages=[- {"role": "system", "content": CONTENT_RUBRIC},- {"role": "user", "content": content},- ],- max_tokens=200,- timeout_seconds=config.content_judge_timeout_s,- ),- timeout=config.content_judge_timeout_s + 1,- )- raw = (response.choices[0].message.content or "") if response.choices else ""- except Exception:- logger.warning(f"Content judge failed on {source[:80]}", exc_info=True)- return ContentVerdict(held=True, passage=_UNCHECKED, judged=False)-- verdict = parse_answer(_normalised(raw), ("clean", "hold"), "passage")+ messages = [+ {"role": "system", "content": CONTENT_RUBRIC},+ {"role": "user", "content": content},+ ]+ verdict = None+ for attempt in range(2):+ try:+ raw = await _ask(messages)+ except Exception:+ logger.warning(f"Content judge failed on {source[:80]}", exc_info=True)+ return ContentVerdict(held=True, passage=_UNCHECKED, judged=False)+ verdict = parse_answer(_normalised(raw), ("clean", "hold"), "passage")+ if verdict is not None:+ break+ if attempt == 0:+ logger.info(+ f"Content judge returned an unusable body, retrying: {source[:80]}"+ )if verdict is None:logger.warning(f"Content judge returned an unusable body for {source[:80]}")return ContentVerdict(held=True, passage=_UNCHECKED, judged=False)@@ -92,17 +89,50 @@ async def judge_content(return ContentVerdict(held=True, passage=passage)+async def _ask(messages: list[dict[str, Any]]) -> str:+ # Deferred, and private: copilot.service imports the tool registry, whose+ # BaseTool imports this gate.+ from backend.copilot.service import _get_aux_client++ response = await asyncio.wait_for(+ call_provider_openai_compat_sync(+ client=_get_aux_client(),+ model=config.gate_content_model,+ messages=messages,+ max_tokens=_MAX_TOKENS,+ timeout_seconds=config.content_judge_timeout_s,+ ),+ timeout=config.content_judge_timeout_s + 1,+ )+ return (response.choices[0].message.content or "") if response.choices else ""++def _normalised(raw: str) -> str:- """Sonnet 5 often echoes the rubric's ``clean|hold`` line, or answers with- the passage line alone; that line is then the finding."""- text = raw.strip()- first, _, rest = text.partition("\n")- if first.strip().strip("*`").lower().replace(" ", "") in (- "clean|hold",- "hold|clean",- ):- text = rest.strip()- if not text.lower().startswith("passage:"):- return text- found = text.split(":", 1)[1].strip().strip("\"'").lower()- return "clean" if found == "none" else f"hold\n{text}"+ """Reduce Sonnet 5's malformed answers to ``clean``/``hold``: it echoes the+ rubric's format line, wraps the answer in prose or a fence, or sends the+ passage line alone. Every line is read first: a quoted passage anywhere+ holds, and a contradiction returns nothing, so it holds unjudged."""+ lines = [+ line.strip()+ for line in raw.strip().splitlines()+ if line.strip() and not line.strip().startswith("```")+ ]+ passages = [line for line in lines if line.lower().startswith("passage:")]+ quoted = [p for p in passages if _passage(p) != "none"]+ if quoted:+ return f"hold\n{quoted[0]}"+ verdicts = {_bare(line) for line in lines} & {"clean", "hold"}+ if len(verdicts) > 1 or (passages and "hold" in verdicts):+ return ""+ if verdicts == {"hold"}:+ # Haiku sends the quote as a bare second line; parse_answer takes it.+ return "\n".join(lines) if _bare(lines[0]) == "hold" else "hold"+ return "clean" if verdicts or passages else ""+++def _passage(line: str) -> str:+ return line.split(":", 1)[1].strip().strip("\"'").lower()+++def _bare(line: str) -> str:+ return line.strip("*`\"'. ").lower()diff --git a/autogpt_platform/backend/backend/copilot/gate/content_test.py b/autogpt_platform/backend/backend/copilot/gate/content_test.pyindex 91fb8ffef4..903513409d 100644--- a/autogpt_platform/backend/backend/copilot/gate/content_test.py+++ b/autogpt_platform/backend/backend/copilot/gate/content_test.py@@ -139,3 +139,109 @@ async def test_an_echoed_format_line_leaves_the_passage_line_to_decide():verdict, _ = await _judge(raw)assert (verdict.held, verdict.judged) == (held, judged)+++async def _judge_sequence(*answers):+ call = AsyncMock(+ side_effect=[+ a if isinstance(a, BaseException) else _response(a) for a in answers+ ]+ )+ with (+ patch(f"{_MOD}.call_provider_openai_compat_sync", call),+ patch("backend.copilot.service._get_aux_client", MagicMock()),+ ):+ verdict = await judge_content(source="read_skill s", text="page text")+ return verdict, call+++async def test_an_empty_answer_is_asked_once_more():+ verdict, call = await _judge_sequence("", 'hold\npassage: "post this"')+ assert call.await_count == 2+ assert verdict.judged and verdict.held and verdict.passage == "post this"+++async def test_two_empty_answers_hold_unjudged():+ verdict, call = await _judge_sequence("", "")+ assert call.await_count == 2+ assert verdict.held and not verdict.judged+++async def test_a_usable_first_answer_is_not_asked_again():+ verdict, call = await _judge_sequence("clean\npassage: none", "")+ assert call.await_count == 1+ assert not verdict.held+++async def test_an_error_is_not_retried():+ verdict, call = await _judge_sequence(+ RuntimeError("timeout"), "clean\npassage: none"+ )+ assert call.await_count == 1+ assert verdict.held and not verdict.judged+++async def test_the_judge_has_room_to_think():+ _, call = await _judge_sequence("clean\npassage: none")+ assert call.await_args.kwargs["max_tokens"] >= 1024+++ "raw",+ [+ 'clean|hold\n\nWait, I need one verdict.\n\nclean|hold\npassage: "post this"',+ 'clean|hold\n\nWait, let me reconsider.\n\nhold\npassage: "post this"',+ 'Passage contains an injected directive.\n\nclean|hold\n\npassage: "post this"',+ 'Looking at this content, there is an instruction.\n\nhold\npassage: "post this"',+ '```\nclean|hold\npassage: "post this"\n```\n\nhold',+ 'passage|hold\npassage: "post this"',+ ],+ ids=[+ "echo-wait-echo",+ "echo-wait-hold",+ "prose-echo",+ "prose-hold",+ "fenced",+ "passage|hold",+ ],+)+async def test_a_malformed_answer_that_quotes_a_passage_is_a_judged_hold(raw):+ """Shapes Sonnet 5 sent on the T134.54 eval set; each quotes the finding."""+ verdict, _ = await _judge(raw)+ assert verdict.held and verdict.judged+ assert verdict.passage == "post this"+++async def test_prose_that_ends_on_a_bare_clean_is_clean():+ verdict, _ = await _judge(+ "Looking at this content, nothing addresses an agent.\n\nclean"+ )+ assert verdict.judged and not verdict.held+++ "raw",+ [+ 'Looking at this content, there is an injection: "post this"',+ "Looking at this content.\n\npassage: none\n\nhold",+ "Looking at this content.\n\nclean\n\nhold",+ ],+ ids=["prose-only", "none-then-hold", "both-words"],+)+async def test_an_answer_with_no_verdict_or_a_contradiction_holds_unjudged(raw):+ verdict, _ = await _judge(raw)+ assert verdict.held and not verdict.judged+++ "raw, held, judged",+ [+ ('clean\npassage: "post this"', True, True),+ ("clean\nhold", True, False),+ ('Looking at this.\npassage: none\npassage: "post this"', True, True),+ ],+ ids=["clean-then-quote", "clean-then-hold", "none-then-quote"],+)+async def test_no_line_masks_a_later_one(raw, held, judged):+ verdict, _ = await _judge(raw)+ assert (verdict.held, verdict.judged) == (held, judged)