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(platform): preserve tool-output provenance in workspace artifacts (#13614)
Yiğit ERDOĞAN32f130447a36
diff --git a/autogpt_platform/backend/backend/api/features/workspace/routes_test.py b/autogpt_platform/backend/backend/api/features/workspace/routes_test.pyindex 6b0a310024..a8b373ecbe 100644--- a/autogpt_platform/backend/backend/api/features/workspace/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/workspace/routes_test.py@@ -93,6 +93,7 @@ def test_list_files_returns_all_when_no_session(mock_manager_cls, mock_get_worksfiles = [_make_file(id="f1", name="a.txt", metadata={"origin": "user-upload"}),_make_file(id="f2", name="b.csv", metadata={"origin": "agent-created"}),+ _make_file(id="f3", name="tc-123.json", metadata={"purpose": "tool-output"}),]mock_instance = AsyncMock()mock_instance.list_files.return_value = files@@ -102,7 +103,7 @@ def test_list_files_returns_all_when_no_session(mock_manager_cls, mock_get_worksassert response.status_code == 200data = response.json()- assert len(data["files"]) == 2+ assert len(data["files"]) == 3assert data["has_more"] is Falseassert data["offset"] == 0assert data["files"][0]["id"] == "f1"@@ -110,6 +111,8 @@ def test_list_files_returns_all_when_no_session(mock_manager_cls, mock_get_worksassert data["files"][0]["origin"] == "uploaded"assert data["files"][1]["id"] == "f2"assert data["files"][1]["origin"] == "generated"+ assert data["files"][2]["metadata"] == {"purpose": "tool-output"}+ assert data["files"][2]["origin"] == "generated"mock_instance.list_files.assert_called_once_with(limit=201,offset=0,diff --git a/autogpt_platform/backend/backend/copilot/tools/base.py b/autogpt_platform/backend/backend/copilot/tools/base.pyindex 0abcbaa523..8120fcb533 100644--- a/autogpt_platform/backend/backend/copilot/tools/base.py+++ b/autogpt_platform/backend/backend/copilot/tools/base.py@@ -103,6 +103,7 @@ async def _persist_and_summarize(filename=f"{tool_call_id}.json",path=file_path,mime_type="application/json",+ metadata={"purpose": "tool-output"},overwrite=True,)except Exception:diff --git a/autogpt_platform/backend/backend/copilot/tools/base_test.py b/autogpt_platform/backend/backend/copilot/tools/base_test.pyindex 4903f7d092..924dc79e3e 100644--- a/autogpt_platform/backend/backend/copilot/tools/base_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/base_test.py@@ -96,6 +96,7 @@ class TestPersistAndSummarize:call_kwargs = mock_manager.write_file.call_argsassert call_kwargs.kwargs["content"] == raw.encode("utf-8")assert call_kwargs.kwargs["path"] == "tool-outputs/tc-123.json"+ assert call_kwargs.kwargs["metadata"] == {"purpose": "tool-output"}@pytest.mark.asyncioasync def test_fallback_on_workspace_error(self):diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/ContextPanelToggle.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/ContextPanelToggle.tsxindex c724afbf2d..af6c6b0360 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/ContextPanelToggle.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/ContextPanelToggle.tsx@@ -18,13 +18,16 @@ interface Props {}function getLastGeneratedFile(generated: SessionFile[]): SessionFile | null {- if (generated.length === 0) return null;- return generated.reduce((latest, file) =>- new Date(file.item.created_at).getTime() >- new Date(latest.item.created_at).getTime()- ? file- : latest,- );+ let latest: SessionFile | null = null;+ let latestTime = Number.NEGATIVE_INFINITY;+ for (const file of generated) {+ const time = new Date(file.item.created_at).getTime();+ if (latest === null || time > latestTime) {+ latest = file;+ latestTime = time;+ }+ }+ return latest;}/** The chat's top-right controls: the Computer toggle and the artifacts@@ -54,8 +57,8 @@ export function ContextPanelToggle({ sessionId = null }: Props) {const closeComputer = useCopilotUIStore((s) => s.closeComputer);const setArtifactPanelMode = useCopilotUIStore((s) => s.setArtifactPanelMode);const isMobile = useIsMobile();- const { generated } = useSessionFiles(sessionId);- const lastGenerated = getLastGeneratedFile(generated);+ const { deliverables } = useSessionFiles(sessionId);+ const lastGenerated = getLastGeneratedFile(deliverables);const isFilesCardOpen = useAreWorkspaceFileCardsOpen();const isArtifactsOpen = isOpen && activeTab === "artifacts";// An open artifact preview and the artifacts tab are both the documentdiff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/__tests__/ContextPanelToggle.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/__tests__/ContextPanelToggle.test.tsxindex e84ad6152b..0e2fcc91d1 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/__tests__/ContextPanelToggle.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/__tests__/ContextPanelToggle.test.tsx@@ -1,12 +1,20 @@-import { fireEvent, render, screen } from "@/tests/integrations/test-utils";+import { getListWorkspaceFilesMockHandler200 } from "@/app/api/__generated__/endpoints/workspace/workspace.msw";+import type { ListFilesResponse } from "@/app/api/__generated__/models/listFilesResponse";+import { server } from "@/mocks/mock-server";+import {+ fireEvent,+ render,+ screen,+ waitFor,+} from "@/tests/integrations/test-utils";import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";import { Key, storage } from "@/services/storage/local-storage";import { useCopilotUIStore } from "../../../store";import { ContextPanelToggle } from "../ContextPanelToggle";--vi.mock("../components/FilesTab/useSessionFiles", () => ({- useSessionFiles: () => ({ generated: [], uploaded: [] }),-}));+import { useSessionFiles } from "../components/FilesTab/useSessionFiles";+import { useWorkspaceFileCards } from "../../WorkspaceFileCards/useWorkspaceFileCards";+import { WorkspaceFileCard } from "../../WorkspaceFileCards/components/WorkspaceFileCard";+import { ArtifactsTab } from "../components/ArtifactsTab/ArtifactsTab";let mobile = false;function setMobile(value: boolean) {@@ -43,6 +51,13 @@ function panelState() {}beforeEach(() => {+ server.use(+ getListWorkspaceFilesMockHandler200({+ files: [],+ offset: 0,+ has_more: false,+ }),+ );setPanel({isOpen: false,activeArtifact: null,@@ -272,3 +287,231 @@ describe("ContextPanelToggle computer button", () => {expect(screen.getByLabelText("Open artifacts")).toBeDefined();});});++const SESSION = "session-1";++// The toggle renders the same bare icon whether the files are still in+// flight or every one of them was filtered out, so a "stays unlabeled"+// assertion would pass trivially. This probe gives those tests something to+// await that proves the listing actually arrived.+interface Props {+ sessionId: string;+}++function LoadedFilesProbe({ sessionId }: Props) {+ const { generated } = useSessionFiles(sessionId);+ return <div data-testid="generated-count">{generated.length}</div>;+}++function WorkspaceFilesProbe({ sessionId }: Props) {+ const { files, handleOpen, handleDownload, setPendingDelete } =+ useWorkspaceFileCards(sessionId);+ return (+ <>+ {files.map((file) => (+ <WorkspaceFileCard+ key={file.item.id}+ file={file}+ onOpen={handleOpen}+ onDownload={handleDownload}+ onRequestDelete={setPendingDelete}+ />+ ))}+ </>+ );+}++function realFile(): ListFilesResponse["files"][number] {+ return {+ id: "aaaaaaaa-0000-0000-0000-000000000001",+ name: "result.csv",+ path: "/sessions/session-1/result.csv",+ mime_type: "text/csv",+ size_bytes: 4096,+ metadata: { origin: "agent" },+ origin: "generated",+ created_at: "2026-05-20T11:00:00Z",+ };+}++// One entry per branch of `isInternalToolOutput`, each newer than the real+// deliverable so a missing filter would surface it instead.+function toolOutputs(): ListFilesResponse["files"] {+ return [+ {+ id: "bbbbbbbb-0000-0000-0000-000000000002",+ name: "toolu_01ABCdef.json",+ path: "/sessions/session-1/tool-outputs/toolu_01ABCdef.json",+ mime_type: "application/json",+ size_bytes: 512,+ metadata: {},+ origin: "generated",+ created_at: "2026-05-20T12:00:00Z",+ },+ {+ id: "cccccccc-0000-0000-0000-000000000003",+ name: "mcp_a1b2-c3d4.json",+ path: "/sessions/session-1/tool-outputs/mcp_a1b2-c3d4.json",+ mime_type: "application/json",+ size_bytes: 256,+ metadata: {},+ origin: "generated",+ created_at: "2026-05-20T13:00:00Z",+ },+ {+ id: "dddddddd-0000-0000-0000-000000000004",+ name: "toolu_02ZYXwvu.json",+ path: "/sessions/session-1/tool-results/toolu_02ZYXwvu.json",+ mime_type: "application/json",+ size_bytes: 128,+ metadata: {},+ origin: "generated",+ created_at: "2026-05-20T14:00:00Z",+ },+ // The id an SDK-transport session actually produces: `_execute_tool_sync`+ // synthesizes `sdk-<uuid>` rather than forwarding the SDK's `toolu_*`, so+ // this is the shape the default AutoPilot path parks in the directory.+ {+ id: "ffffffff-0000-0000-0000-000000000006",+ name: "sdk-a1b2c3d4e5f6.json",+ path: "/sessions/session-1/tool-outputs/sdk-a1b2c3d4e5f6.json",+ mime_type: "application/json",+ size_bytes: 320,+ metadata: {},+ origin: "generated",+ created_at: "2026-05-20T16:00:00Z",+ },+ ];+}++function listing(files: ListFilesResponse["files"]): ListFilesResponse {+ return { files, offset: 0, has_more: false };+}++describe("ContextPanelToggle internal tool output", () => {+ test("uses provenance after an internal output is renamed, while retaining file access", async () => {+ const internal = {+ ...toolOutputs()[0],+ name: "renamed.json",+ path: "/sessions/session-1/renamed.json",+ metadata: { purpose: "tool-output" },+ };+ server.use(+ getListWorkspaceFilesMockHandler200(listing([realFile(), internal])),+ );+ render(+ <>+ <ContextPanelToggle sessionId={SESSION} />+ <ArtifactsTab sessionId={SESSION} />+ <WorkspaceFilesProbe sessionId={SESSION} />+ </>,+ );++ expect(await screen.findByLabelText("Open result.csv")).toBeDefined();+ expect(await screen.findByLabelText("Download renamed.json")).toBeDefined();+ expect(screen.getAllByText("renamed.json")).toHaveLength(1);+ fireEvent.click(screen.getByTitle("renamed.json"));+ expect(panelState().activeArtifact?.id).toBe(internal.id);+ });++ test("promotes an explicitly marked deliverable in the legacy tool-output directory", async () => {+ const deliverable = {+ ...toolOutputs()[0],+ metadata: { purpose: "deliverable" },+ };+ server.use(+ getListWorkspaceFilesMockHandler200(listing([realFile(), deliverable])),+ );+ render(<ContextPanelToggle sessionId={SESSION} />);++ expect(+ await screen.findByLabelText(`Open ${deliverable.name}`),+ ).toBeDefined();+ });+ test("wears the newest user-facing file, not a newer tool output", async () => {+ server.use(+ getListWorkspaceFilesMockHandler200(+ listing([realFile(), ...toolOutputs()]),+ ),+ );+ render(<ContextPanelToggle sessionId={SESSION} />);++ const button = await screen.findByLabelText("Open result.csv");+ fireEvent.click(button);++ expect(panelState().isOpen).toBe(true);+ expect(panelState().activeArtifact?.id).toBe(+ "aaaaaaaa-0000-0000-0000-000000000001",+ );+ });++ test("still wears a deliverable whose name looks like an SDK tool id", async () => {+ server.use(+ getListWorkspaceFilesMockHandler200(+ listing([+ realFile(),+ ...toolOutputs(),+ {+ id: "eeeeeeee-0000-0000-0000-000000000005",+ name: "mcp_config.json",+ path: "/sessions/session-1/mcp_config.json",+ mime_type: "application/json",+ size_bytes: 64,+ metadata: {},+ origin: "generated",+ created_at: "2026-05-20T17:00:00Z",+ },+ ]),+ ),+ );+ render(<ContextPanelToggle sessionId={SESSION} />);++ expect(await screen.findByLabelText("Open mcp_config.json")).toBeDefined();+ });++ test("still wears a deliverable under the user's own tool-outputs folder", async () => {+ server.use(+ getListWorkspaceFilesMockHandler200(+ listing([+ realFile(),+ ...toolOutputs(),+ {+ id: "99999999-0000-0000-0000-000000000007",+ name: "data.json",+ path: "/sessions/session-1/my-pipeline/tool-outputs/data.json",+ mime_type: "application/json",+ size_bytes: 96,+ metadata: {},+ origin: "generated",+ created_at: "2026-05-20T17:00:00Z",+ },+ ]),+ ),+ );+ render(<ContextPanelToggle sessionId={SESSION} />);++ expect(await screen.findByLabelText("Open data.json")).toBeDefined();+ });++ test("stays unlabeled when every generated file is an internal tool output", async () => {+ server.use(getListWorkspaceFilesMockHandler200(listing(toolOutputs())));+ render(+ <>+ <ContextPanelToggle sessionId={SESSION} />+ <LoadedFilesProbe sessionId={SESSION} />+ </>,+ );++ await waitFor(() =>+ expect(screen.getByTestId("generated-count").textContent).toBe(+ String(toolOutputs().length),+ ),+ );+ expect(screen.getByLabelText("Open artifacts")).toBeDefined();++ fireEvent.click(screen.getByLabelText("Open artifacts"));++ expect(panelState().activeArtifact).toBeNull();+ expect(panelState().activeTab).toBe("artifacts");+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/ArtifactsTab/ArtifactsTab.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/ArtifactsTab/ArtifactsTab.tsxindex fe9f86d9c7..8d32f0c4c8 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/ArtifactsTab/ArtifactsTab.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/ArtifactsTab/ArtifactsTab.tsx@@ -21,9 +21,9 @@ interface Props {export function ArtifactsTab({ sessionId }: Props) {const openArtifact = useCopilotUIStore((s) => s.openArtifact);- const { uploaded, generated, isLoading, isError } =+ const { uploaded, deliverables, isLoading, isError } =useSessionFiles(sessionId);- const files = [...generated, ...uploaded].slice(0, PREVIEWABLE_LIMIT);+ const files = [...deliverables, ...uploaded].slice(0, PREVIEWABLE_LIMIT);function handleDownload(file: SessionFile) {downloadArtifact(fileItemToArtifactRef(file.item)).catch(() =>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/helpers.test.tsindex e9c92b559f..ff28be2013 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/helpers.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/helpers.test.ts@@ -5,6 +5,7 @@ import {fileItemToArtifactRef,formatFileSize,formatFileTimestamp,+ isInternalToolOutput,isUploadedFile,} from "./helpers";@@ -43,6 +44,111 @@ describe("FilesTab helpers", () => {});});+describe("isInternalToolOutput", () => {+ function generated(name: string, path?: string): WorkspaceFileItem {+ return {+ ...baseItem,+ name,+ path: path ?? `/sessions/s1/${name}`,+ mime_type: "application/json",+ metadata: {},+ origin: "generated",+ };+ }++ function toolOutput(name: string): WorkspaceFileItem {+ return generated(name, `/sessions/s1/tool-outputs/${name}`);+ }++ test("recognizes explicit tool-output provenance after a file is renamed or moved", () => {+ expect(+ isInternalToolOutput({+ ...generated("renamed.json"),+ metadata: { purpose: "tool-output" },+ }),+ ).toBe(true);+ });++ test("respects explicit deliverable provenance even in a legacy scratch directory", () => {+ expect(+ isInternalToolOutput({+ ...toolOutput("report.json"),+ metadata: { purpose: "deliverable" },+ }),+ ).toBe(false);+ });++ test("matches whatever is parked in the session tool-output dir", () => {+ expect(isInternalToolOutput(toolOutput("toolu_01ABCdef.json"))).toBe(true);+ expect(isInternalToolOutput(toolOutput("mcp_a1b2-c3d4.json"))).toBe(true);+ // The ids the platform actually mints: `_execute_tool_sync` synthesizes+ // `sdk-<uuid>` for every SDK-transport call, and the baseline transport+ // passes the provider's id straight through. Neither is `toolu_`/`mcp_`.+ expect(isInternalToolOutput(toolOutput("sdk-a1b2c3d4e5f6.json"))).toBe(+ true,+ );+ expect(isInternalToolOutput(toolOutput("call_9xYzAbC.json"))).toBe(true);+ expect(isInternalToolOutput(toolOutput("tc-123.json"))).toBe(true);+ expect(+ isInternalToolOutput(+ generated(+ "toolu_01ABCdef.json",+ "/sessions/s1/tool-results/toolu_01ABCdef.json",+ ),+ ),+ ).toBe(true);+ });++ test("leaves user-facing files alone", () => {+ expect(isInternalToolOutput(generated("report.json"))).toBe(false);+ expect(isInternalToolOutput(generated("result.csv"))).toBe(false);+ expect(+ isInternalToolOutput(+ generated("notes.json", "/sessions/s1/my-tool-results-archive.json"),+ ),+ ).toBe(false);+ });++ // The directory is the entire rule, so the session root is what bounds it:+ // an SDK-looking name outside that folder is a plausible deliverable, and a+ // `tool-outputs/` folder deeper in the tree is the user's own.+ test("bounds the match to a tool-output dir at the session root", () => {+ expect(isInternalToolOutput(generated("mcp_config.json"))).toBe(false);+ expect(isInternalToolOutput(generated("toolu_01ABCdef.json"))).toBe(false);+ expect(+ isInternalToolOutput(+ generated(+ "data.json",+ "/sessions/s1/my-pipeline/tool-outputs/data.json",+ ),+ ),+ ).toBe(false);+ expect(+ isInternalToolOutput(+ generated(+ "toolu_mine.json",+ "/sessions/s1/my-pipeline/tool-outputs/toolu_mine.json",+ ),+ ),+ ).toBe(false);+ expect(+ isInternalToolOutput(+ generated("deep.json", "/sessions/s1/tool-outputs/nested/deep.json"),+ ),+ ).toBe(false);+ });++ test("never hides files the user uploaded themselves", () => {+ expect(+ isInternalToolOutput({+ ...baseItem,+ name: "toolu_01ABCdef.json",+ path: "/sessions/s1/tool-outputs/toolu_01ABCdef.json",+ }),+ ).toBe(false);+ });+});+describe("downloadFilesAsZip", () => {test("fetches each file and produces a zip blob via the sink", async () => {const fetched: string[] = [];diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/helpers.tsindex 97257a66f1..181fc48574 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/helpers.ts@@ -11,6 +11,19 @@ export function isUploadedFile(item: WorkspaceFileItem): boolean {return item.origin === "uploaded";}+// Older outputs have no purpose metadata. Only the reserved directory directly+// under the session root is a legacy fallback; nested user directories are not.+const SDK_TOOL_RESULT_PATH =+ /^\/sessions\/[^/]+\/tool-(?:results|outputs)\/[^/]+$/i;++export function isInternalToolOutput(item: WorkspaceFileItem): boolean {+ if (isUploadedFile(item)) return false;+ if (item.metadata?.purpose !== undefined) {+ return item.metadata.purpose === "tool-output";+ }+ return SDK_TOOL_RESULT_PATH.test(item.path);+}+export function fileItemToArtifactRef(item: WorkspaceFileItem): ArtifactRef {return {id: item.id,diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/useSessionFiles.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/useSessionFiles.tsindex 5b67af3bc6..4bfc2f0f18 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/useSessionFiles.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/useSessionFiles.ts@@ -6,7 +6,7 @@ import type { WorkspaceFileItem } from "@/app/api/__generated__/models/workspaceimport { useMemo } from "react";import { useCopilotStreamStore } from "../../../../copilotStreamStore";import { getMessageArtifacts } from "../../../ChatMessagesContainer/helpers";-import { isUploadedFile } from "./helpers";+import { isInternalToolOutput, isUploadedFile } from "./helpers";export interface SessionFile {item: WorkspaceFileItem;@@ -32,7 +32,7 @@ export function useSessionFiles(sessionId: string | null) {// `messageSnapshots` is rewritten with a fresh array per streamed token —// so without memoising, scanning every message part (and compiling a// RegExp per matched workspace URI) would run at token cadence.- const { uploaded, generated, files } = useMemo(() => {+ const { uploaded, generated, deliverables, files } = useMemo(() => {const fileIdToMessageId = new Map<string, string>();for (const message of messages ?? []) {for (const artifact of getMessageArtifacts(message)) {@@ -51,12 +51,16 @@ export function useSessionFiles(sessionId: string | null) {files,uploaded: files.filter((f) => isUploadedFile(f.item)),generated: files.filter((f) => !isUploadedFile(f.item)),+ deliverables: files.filter(+ (f) => !isUploadedFile(f.item) && !isInternalToolOutput(f.item),+ ),};}, [messages, query.data]);return {uploaded,generated,+ deliverables,isLoading: query.isLoading && !!sessionId,isError: query.isError,error: query.error,