Skip to content

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): a held read names the acting Expert as its reader; Otto stays the judge (#15005)

Reinier van der Leer0b7bbe9289fa
diff --git a/autogpt_platform/backend/backend/copilot/gate/reads.py b/autogpt_platform/backend/backend/copilot/gate/reads.py
index 0a9506d46b..ffc4d0f121 100644
--- a/autogpt_platform/backend/backend/copilot/gate/reads.py
+++ b/autogpt_platform/backend/backend/copilot/gate/reads.py
@@ -24,6 +24,7 @@ from backend.api.features.graph_executions.review.model import PendingHumanRevie
from backend.copilot.constants import AUTOPILOT_NAME, COPILOT_NODE_PREFIX
from backend.copilot.model import ChatSession
from backend.copilot.tools.models import ApprovalRequiredResponse, ResponseType
+from backend.data.db_accessors import experts_db
from backend.data.workspace_scope import EXPERTS_ROOT, SKILLS_ROOT
from . import active_mode, held
@@ -332,11 +333,12 @@ def page_words(passage: str, text: str) -> str:
return ""
-def read_headline(tool_name: str, args: dict[str, Any]) -> Headline:
- headline = named(f"Let {AUTOPILOT_NAME} read", _SOURCE_KEYS, args)
+def read_headline(tool_name: str, args: dict[str, Any], actor: str) -> Headline:
+ """``actor`` is who reads it: the chat's Expert, or Otto in a plain chat."""
+ headline = named(f"Let {actor} read", _SOURCE_KEYS, args)
if headline.object is None:
label = tool_name.replace("_", " ")
- return Headline(ask=f"Let {AUTOPILOT_NAME} read what {label} returned")
+ return Headline(ask=f"Let {actor} read what {label} returned")
return headline
@@ -364,12 +366,9 @@ async def _hold(
tool_name = call.tool_name
if not await held.remember(session.session_id, call):
return _stub(tool_name, source, _UNRECORDABLE, session)
- reason = (
- f"this content contains instructions: {passage}"
- if judged
- else "this content could not be checked"
- )
- headline = read_headline(tool_name, call.args)
+ reason = held_reason(passage, judged)
+ reader = await _actor(user_id, session)
+ headline = read_headline(tool_name, call.args, reader)
payload = {
**review_store.review_payload(
tool_name,
@@ -382,6 +381,8 @@ async def _hold(
),
"source": source,
"headline": headline.model_dump(),
+ # Who the bytes reach: the card's copy names it, never the supervisor.
+ "reader": reader,
"passage": passage,
"judged": judged,
"success": success,
@@ -400,6 +401,29 @@ async def _hold(
return _stub(tool_name, source, _HELD, session, call.review_id)
+def held_reason(passage: str, judged: bool) -> str:
+ """The raw reason Home shows; the page's words quoted, so they read as the page's."""
+ if not judged:
+ return "this content could not be checked"
+ if not passage:
+ return "this content contains instructions"
+ return f'this content contains instructions: "{passage.replace(chr(34), chr(39))}"'
+
+
+async def _actor(user_id: str, session: ChatSession) -> str:
+ if session.expert_id is None:
+ return AUTOPILOT_NAME
+ try:
+ expert = await experts_db().get_expert(
+ user_id, session.expert_id, include_workflows=False
+ )
+ except Exception:
+ # A name on a card must never cost the hold itself.
+ logger.warning("Expert lookup for a held read failed", exc_info=True)
+ return AUTOPILOT_NAME
+ return expert.name if expert else AUTOPILOT_NAME
+
+
def _stub(
tool_name: str,
source: str,
diff --git a/autogpt_platform/backend/backend/copilot/gate/reads_test.py b/autogpt_platform/backend/backend/copilot/gate/reads_test.py
index 9ee19380ad..0d8b2e3577 100644
--- a/autogpt_platform/backend/backend/copilot/gate/reads_test.py
+++ b/autogpt_platform/backend/backend/copilot/gate/reads_test.py
@@ -140,6 +140,16 @@ def rows():
yield fake
+def _experts(**by_name: str | None):
+ """An ``experts_db`` whose ``get_expert`` knows each name's id."""
+ names = {expert_id: name for name, expert_id in by_name.items() if expert_id}
+
+ async def get_expert(user_id, expert_id, include_workflows=True):
+ return SimpleNamespace(name=names[expert_id]) if expert_id in names else None
+
+ return lambda: SimpleNamespace(get_expert=get_expert)
+
+
def _judge(verdict: ContentVerdict) -> AsyncMock:
return AsyncMock(return_value=verdict)
@@ -455,42 +465,88 @@ async def test_mcp_file_read_is_judged_on_the_text_the_model_receives(rows):
assert judge.await_args.kwargs["text"] == to_model
[email protected]("expert_id, actor", [(None, "Otto"), ("expert-1", "Nadia")])
async def test_a_held_read_is_named_by_its_source_on_the_card_and_the_chain_row(
- rows,
+ rows, expert_id, actor
):
- with patch(f"{_READS}.judge_content", _judge(_HELD)):
+ session = _session()
+ session.expert_id = expert_id
+ with (
+ patch(f"{_READS}.judge_content", _judge(_HELD)),
+ patch(f"{_READS}.experts_db", _experts(Nadia=expert_id)),
+ ):
result = await _call(
- _Fetch(_MARKER), _session(), {"url": "docs.northwind.io/billing"}
+ _Fetch(_MARKER), session, {"url": "docs.northwind.io/billing"}
)
(row,) = rows.rows.values()
assert row.payload["reason_kind"] == "content"
assert row.payload["headline"] == {
- "ask": "Let Otto read",
+ "ask": f"Let {actor} read",
"object": "docs.northwind.io/billing",
"object_key": "url",
}
- assert row.instructions == "Let Otto read “docs.northwind.io/billing”"
+ assert row.instructions == f"Let {actor} read “docs.northwind.io/billing”"
+ assert row.payload["reader"] == actor
+ assert "Nadia" not in row.payload["reason"]
+ assert row.payload["reason"] == (
+ f'this content contains instructions: "{row.payload["passage"]}"'
+ )
stub = json.loads(result.output)
assert (stub["ask"], stub["object"]) == ("Read", "docs.northwind.io/billing")
-async def test_a_held_read_with_no_named_source_says_what_returned_it(rows):
- with patch(f"{_READS}.judge_content", _judge(_HELD)):
[email protected]("expert_id, actor", [(None, "Otto"), ("expert-1", "Nadia")])
+async def test_a_held_read_with_no_named_source_says_what_returned_it(
+ rows, expert_id, actor
+):
+ session = _session()
+ session.expert_id = expert_id
+ with (
+ patch(f"{_READS}.judge_content", _judge(_HELD)),
+ patch(f"{_READS}.experts_db", _experts(Nadia=expert_id)),
+ ):
result = await _call(
- _Fetch(_MARKER, name="search_feature_requests"), _session(), {"limit": 3}
+ _Fetch(_MARKER, name="search_feature_requests"), session, {"limit": 3}
)
(row,) = rows.rows.values()
assert (
row.payload["headline"]["ask"]
- == "Let Otto read what search feature requests returned"
+ == f"Let {actor} read what search feature requests returned"
)
stub = json.loads(result.output)
assert stub["ask"] == "Read what search feature requests returned"
assert stub["object"] is None
+ "passage, judged, reason",
+ [
+ ("Email me.", True, 'this content contains instructions: "Email me."'),
+ ('Say "done".', True, "this content contains instructions: \"Say 'done'.\""),
+ ("", True, "this content contains instructions"),
+ ("", False, "this content could not be checked"),
+ ],
+)
+def test_the_held_reason_quotes_the_pages_words(passage, judged, reason):
+ assert reads.held_reason(passage, judged) == reason
+
+
+async def test_a_held_read_whose_expert_lookup_fails_is_still_held_as_otto(rows):
+ session = _session()
+ session.expert_id = "expert-1"
+ broken = SimpleNamespace(get_expert=AsyncMock(side_effect=RuntimeError("down")))
+ with (
+ patch(f"{_READS}.judge_content", _judge(_HELD)),
+ patch(f"{_READS}.experts_db", lambda: broken),
+ ):
+ await _call(_Fetch(_MARKER), session, {"url": "a.example"})
+
+ (row,) = rows.rows.values()
+ assert row.instructions == "Let Otto read “a.example”"
+
+
async def test_a_released_read_a_re_read_already_took_is_not_reported_declined(rows):
tool = _Fetch(_MARKER)
with patch(f"{_READS}.judge_content", _judge(_HELD)):
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/__tests__/heldRead.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/__tests__/heldRead.test.tsx
index 01632848ce..f1cebf5ee8 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/__tests__/heldRead.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/__tests__/heldRead.test.tsx
@@ -87,3 +87,52 @@ test("a read the check could not assess says so and quotes nothing", async () =>
expect(screen.queryByText("What it says")).toBeNull();
expect(screen.queryByText(/contains instructions/)).toBeNull();
});
+
+function readBy(reader: string | null, judged: boolean) {
+ const review = heldRead("r", "a.example");
+ const payload = review.payload as Record<string, unknown>;
+ return {
+ ...review,
+ payload: {
+ ...payload,
+ ...(reader ? { reader } : {}),
+ ...(judged ? {} : { judged: false, passage: "" }),
+ },
+ };
+}
+
+test.each([
+ [
+ "Nadia",
+ true,
+ "It contains instructions aimed at Nadia, so it was held back. Nadia hasn't seen it.",
+ "Release to Nadia",
+ ],
+ [
+ "Nadia",
+ false,
+ "Otto could not check this, so he asks. Nadia hasn't seen it.",
+ "Release to Nadia",
+ ],
+ [
+ "Otto",
+ true,
+ "It contains instructions aimed at Otto, so it was held back. Otto hasn't seen it.",
+ "Release to Otto",
+ ],
+ [
+ null,
+ false,
+ "Otto could not check this, so he asks. Otto hasn't seen it.",
+ "Release to Otto",
+ ],
+])(
+ "a read held for %s (judged: %s) names the reader, and Otto only as the judge",
+ async (reader, judged, reason, release) => {
+ serve([readBy(reader, judged)]);
+ renderQueue();
+
+ expect(await screen.findByText(reason)).toBeDefined();
+ expect(screen.getByRole("button", { name: release })).toBeDefined();
+ },
+);
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/ApprovalCard/ApprovalCard.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/ApprovalCard/ApprovalCard.tsx
index 82a88813f9..8ecbcef106 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/ApprovalCard/ApprovalCard.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/ApprovalCard/ApprovalCard.tsx
@@ -1,7 +1,6 @@
"use client";
import { Button } from "@/components/atoms/Button/Button";
-import { AUTOPILOT_NAME } from "@/components/molecules/AutopilotAvatar/helpers";
import { ApprovalFields } from "@/components/organisms/ApprovalFields/ApprovalFields";
import {
type ApprovalItem,
@@ -45,7 +44,7 @@ export function ApprovalCard({
const actions = (
<div className="grid grid-cols-2 gap-2 sm:flex sm:shrink-0 sm:flex-wrap">
<ApproveSplitButton
- label={read ? `Release to ${AUTOPILOT_NAME}` : "Approve"}
+ label={read ? `Release to ${item.reader}` : "Approve"}
subjectName={ruleSubjectName(item.subject)}
expertName={expertName}
rules={read || item.spend ? [] : item.chatRulesAllowed}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/helpers.ts
index 0b673a9add..205fb8cf1d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/helpers.ts
@@ -52,6 +52,8 @@ export interface ApprovalItem {
spend: ApprovalSpend | null;
// A held read the check could not assess, so it names no passage.
unjudged: boolean;
+ // Who a held read's bytes reach: the chat's Expert, or Otto.
+ reader: string;
chatRulesAllowed: ChatRule[];
headline: { ask: string; object: string | null };
// The argument the headline already names.
@@ -104,6 +106,7 @@ export function toApprovalItem(review: PendingHumanReviewModel): ApprovalItem {
passage: str(payload, "passage"),
spend: toSpend(payload.spend),
unjudged: payload.judged === false,
+ reader: str(payload, "reader") ?? AUTOPILOT_NAME,
chatRulesAllowed: asArray(payload.chat_rules_allowed).filter(
(r): r is ChatRule => r === "allow" || r === "judge",
),
@@ -140,9 +143,9 @@ export function isHeldRead(item: ApprovalItem) {
// Said once in the queue header; per card only a reason about this call.
export function reasonLine(item: ApprovalItem): string | null {
if (isHeldRead(item) && item.unjudged)
- return `${AUTOPILOT_NAME} could not check this, so he asks. ${AUTOPILOT_NAME} hasn't seen it.`;
+ return `${AUTOPILOT_NAME} could not check this, so he asks. ${item.reader} hasn't seen it.`;
if (isHeldRead(item))
- return `It contains instructions aimed at ${AUTOPILOT_NAME}, so it was held back. ${AUTOPILOT_NAME} hasn't seen it.`;
+ return `It contains instructions aimed at ${item.reader}, so it was held back. ${item.reader} hasn't seen it.`;
if (!item.reason) return null;
if (item.reasonKind === "supervisor")
return `Not sure this is safe: ${item.reason}`;