π OpenHands: AI-Driven Development
fix(files-tab): populate the Files list on cloud backends from git changes (#1628)
Lila Garcia0b0eda309674
diff --git a/__tests__/hooks/query/use-workspace-file-content.test.tsx b/__tests__/hooks/query/use-workspace-file-content.test.tsxindex de3c66a330..dcd0e1dd8b 100644--- a/__tests__/hooks/query/use-workspace-file-content.test.tsx+++ b/__tests__/hooks/query/use-workspace-file-content.test.tsx@@ -42,6 +42,12 @@ vi.mock("#/api/runtime-service/agent-server-runtime-service", () => ({},}));+const readCloudConversationFileMock = vi.fn();+vi.mock("#/api/cloud/conversation-service.api", () => ({+ readCloudConversationFile: (...args: unknown[]) =>+ readCloudConversationFileMock(...args),+}));+const fetchMock = vi.fn();function makeWrapper() {@@ -72,6 +78,7 @@ describe("useWorkspaceFileContent", () => {useRuntimeIsReadyMock.mockReset();getActiveBackendMock.mockReset();downloadFileMock.mockReset();+ readCloudConversationFileMock.mockReset();useRuntimeIsReadyMock.mockReturnValue(true);useActiveConversationMock.mockReturnValue({data: {@@ -283,8 +290,8 @@ describe("useWorkspaceFileContent", () => {});});- it("fetches text via downloadFile and exposes a data: URI staticUrl", async () => {- downloadFileMock.mockResolvedValue(arrayBufferFromString("# Hello"));+ it("fetches text via the cloud file endpoint and exposes a data: URI staticUrl", async () => {+ readCloudConversationFileMock.mockResolvedValue("# Hello");const { result } = renderHook(() => useWorkspaceFileContent("docs/readme.md"),@@ -293,11 +300,14 @@ describe("useWorkspaceFileContent", () => {await waitFor(() => expect(result.current.isSuccess).toBe(true));- expect(downloadFileMock).toHaveBeenCalledWith(- "https://agent.example.com/api/conversations/conv-1",- "session-key",- "docs/readme.md",+ // Cloud uses the first-class cloud API endpoint, never the removed+ // cloud-proxy / downloadFile path, and passes an ABSOLUTE path (the+ // runtime's /api/file/download rejects relative paths).+ expect(readCloudConversationFileMock).toHaveBeenCalledWith(+ "conv-1",+ "/workspace/project/docs/readme.md",);+ expect(downloadFileMock).not.toHaveBeenCalled();expect(fetchMock).not.toHaveBeenCalled();expect(result.current.data).toEqual({path: "docs/readme.md",@@ -308,24 +318,52 @@ describe("useWorkspaceFileContent", () => {});});- it("renders images via base64 data URI bytes from downloadFile", async () => {- const imageBytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47]).buffer;- downloadFileMock.mockResolvedValue(imageBytes);+ it("anchors the file path to the conversation's working dir", async () => {+ useActiveConversationMock.mockReturnValue({+ data: {+ id: "conv-1",+ conversation_url:+ "https://agent.example.com/api/conversations/conv-1",+ session_api_key: "session-key",+ workspace: { working_dir: "/workspace/project/my-repo" },+ },+ });+ readCloudConversationFileMock.mockResolvedValue("hi");++ const { result } = renderHook(+ () => useWorkspaceFileContent("src/index.ts"),+ { wrapper: makeWrapper() },+ );++ await waitFor(() => expect(result.current.isSuccess).toBe(true));++ expect(readCloudConversationFileMock).toHaveBeenCalledWith(+ "conv-1",+ "/workspace/project/my-repo/src/index.ts",+ );+ });++ it("renders images via base64 data URI from the cloud file endpoint", async () => {+ // The cloud endpoint returns text; use an SVG (text-based image) since+ // binary bytes can't round-trip faithfully through the string API.+ const svg = "<svg></svg>";+ readCloudConversationFileMock.mockResolvedValue(svg);const { result } = renderHook(- () => useWorkspaceFileContent("assets/logo.png"),+ () => useWorkspaceFileContent("assets/logo.svg"),{ wrapper: makeWrapper() },);await waitFor(() => expect(result.current.isSuccess).toBe(true));+ expect(downloadFileMock).not.toHaveBeenCalled();expect(fetchMock).not.toHaveBeenCalled();expect(result.current.data).toEqual({- path: "assets/logo.png",+ path: "assets/logo.svg",kind: "image",text: null,- staticUrl: `data:image/png;base64,${btoa("\x89PNG")}`,- mimeType: "image/png",+ staticUrl: `data:image/svg+xml;base64,${btoa(svg)}`,+ mimeType: "image/svg+xml",});});});diff --git a/__tests__/hooks/query/use-workspace-files.test.tsx b/__tests__/hooks/query/use-workspace-files.test.tsxnew file mode 100644index 0000000000..a25a37e7b5--- /dev/null+++ b/__tests__/hooks/query/use-workspace-files.test.tsx@@ -0,0 +1,163 @@+import React from "react";+import { renderHook, waitFor } from "@testing-library/react";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import AgentServerRuntimeService from "#/api/runtime-service/agent-server-runtime-service";+import { useWorkspaceFiles } from "#/hooks/query/use-workspace-files";+import type { GitChange } from "#/api/open-hands.types";++const useActiveBackendMock = vi.fn();+vi.mock("#/contexts/active-backend-context", () => ({+ useActiveBackend: () => useActiveBackendMock(),+}));++const useActiveConversationMock = vi.fn();+vi.mock("#/hooks/query/use-active-conversation", () => ({+ useActiveConversation: () => useActiveConversationMock(),+}));++const useRuntimeIsReadyMock = vi.fn();+vi.mock("#/hooks/use-runtime-is-ready", () => ({+ useRuntimeIsReady: () => useRuntimeIsReadyMock(),+}));++const useUnifiedGetGitChangesMock = vi.fn();+vi.mock("#/hooks/query/use-unified-get-git-changes", () => ({+ useUnifiedGetGitChanges: () => useUnifiedGetGitChangesMock(),+}));++const executeCommandSpy = vi.spyOn(AgentServerRuntimeService, "executeCommand");++function makeWrapper() {+ const client = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ return function WorkspaceFilesTestWrapper({+ children,+ }: {+ children: React.ReactNode;+ }) {+ return (+ <QueryClientProvider client={client}>{children}</QueryClientProvider>+ );+ };+}++const conversation = {+ id: "conv-1",+ conversation_url: "https://runtime.example.com/api/conversations/conv-1",+ session_api_key: "session-key",+ workspace: { working_dir: "/workspace/project" },+};++function gitChangesResult(data: GitChange[], isLoading = false) {+ return {+ data,+ isLoading,+ isFetching: false,+ isSuccess: true,+ isError: false,+ error: null,+ refetch: vi.fn(),+ };+}++beforeEach(() => {+ useActiveBackendMock.mockReset();+ useActiveConversationMock.mockReset();+ useRuntimeIsReadyMock.mockReset();+ useUnifiedGetGitChangesMock.mockReset();+ executeCommandSpy.mockReset();++ useRuntimeIsReadyMock.mockReturnValue(true);+ useActiveConversationMock.mockReturnValue({ data: conversation });+ useUnifiedGetGitChangesMock.mockReturnValue(gitChangesResult([]));+});++afterEach(() => {+ vi.clearAllMocks();+});++const makeBackend = (kind: "local" | "cloud") => ({+ backend: {+ id: "backend-id",+ name: kind === "local" ? "Local" : "Production",+ host:+ kind === "local" ? "http://127.0.0.1:8000" : "https://app.all-hands.dev",+ apiKey: "test-key",+ kind,+ },+ orgId: null,+});++describe("useWorkspaceFiles β local backend", () => {+ beforeEach(() => useActiveBackendMock.mockReturnValue(makeBackend("local")));++ it("lists files via bash find and does not touch git changes", async () => {+ executeCommandSpy.mockResolvedValue({+ exit_code: 0,+ stdout: "./hello.txt\n./src/index.ts\n",+ stderr: "",+ });++ const { result } = renderHook(() => useWorkspaceFiles(), {+ wrapper: makeWrapper(),+ });++ await waitFor(() =>+ expect(result.current.data).toEqual(["hello.txt", "src/index.ts"]),+ );+ expect(executeCommandSpy).toHaveBeenCalledTimes(1);+ });+});++describe("useWorkspaceFiles β cloud backend", () => {+ beforeEach(() => useActiveBackendMock.mockReturnValue(makeBackend("cloud")));++ it("derives the file list from git changes without running bash", async () => {+ useUnifiedGetGitChangesMock.mockReturnValue(+ gitChangesResult([+ { status: "A", path: "hello.txt" },+ { status: "M", path: "src/index.ts" },+ ]),+ );++ const { result } = renderHook(() => useWorkspaceFiles(), {+ wrapper: makeWrapper(),+ });++ await waitFor(() =>+ expect(result.current.data).toEqual(["hello.txt", "src/index.ts"]),+ );+ // Cloud must never drive the removed bash/cloud-proxy path.+ expect(executeCommandSpy).not.toHaveBeenCalled();+ });++ it("drops deleted files (they can't be opened) and de-dupes", async () => {+ useUnifiedGetGitChangesMock.mockReturnValue(+ gitChangesResult([+ { status: "A", path: "hello.txt" },+ { status: "D", path: "gone.txt" },+ { status: "M", path: "hello.txt" },+ ]),+ );++ const { result } = renderHook(() => useWorkspaceFiles(), {+ wrapper: makeWrapper(),+ });++ await waitFor(() => expect(result.current.data).toEqual(["hello.txt"]));+ });++ it("surfaces the git-changes loading state", async () => {+ useUnifiedGetGitChangesMock.mockReturnValue(gitChangesResult([], true));++ const { result } = renderHook(() => useWorkspaceFiles(), {+ wrapper: makeWrapper(),+ });++ expect(result.current.isLoading).toBe(true);+ expect(executeCommandSpy).not.toHaveBeenCalled();+ });+});diff --git a/src/hooks/query/use-workspace-file-content.ts b/src/hooks/query/use-workspace-file-content.tsindex 134805596c..9a0a3d7ccd 100644--- a/src/hooks/query/use-workspace-file-content.ts+++ b/src/hooks/query/use-workspace-file-content.ts@@ -1,7 +1,8 @@import { useQuery } from "@tanstack/react-query";+import { readCloudConversationFile } from "#/api/cloud/conversation-service.api";import { getActiveBackend } from "#/api/backend-registry/active-store";-import AgentServerRuntimeService from "#/api/runtime-service/agent-server-runtime-service";+import { getGitPath } from "#/utils/get-git-path";import { useActiveConversation } from "#/hooks/query/use-active-conversation";import { useRuntimeIsReady } from "#/hooks/use-runtime-is-ready";import {@@ -155,9 +156,22 @@ export function useWorkspaceFileContent(relativePath: string | null) {const conversationId = conversation?.id;const conversationUrl = conversation?.conversation_url;const sessionApiKey = conversation?.session_api_key;+ const selectedRepository = conversation?.selected_repository;+ const workingDir = conversation?.workspace?.working_dir?.trim();const baseUrl = workspaceSession?.baseUrl;const isCloud = getActiveBackend().backend.kind === "cloud";+ // The cloud `/file` endpoint downloads via the runtime's+ // `/api/file/download`, which rejects relative paths (400 β the cloud API+ // swallows it and returns ""). Anchor the file against the working dir the+ // same way the diff view builds its git-diff path (see use-unified-git-diff),+ // then force a leading slash since `getGitPath`'s default is relative.+ const gitPath = getGitPath(selectedRepository, workingDir);+ const workspaceRoot = gitPath.startsWith("/") ? gitPath : `/${gitPath}`;+ const absoluteFilePath = relativePath+ ? `${workspaceRoot}/${relativePath}`+ : null;+return useQuery<WorkspaceFileContent>({queryKey: ["workspace-file-content",@@ -166,6 +180,7 @@ export function useWorkspaceFileContent(relativePath: string | null) {sessionApiKey,isCloud ? "cloud" : baseUrl,relativePath,+ absoluteFilePath,workspaceMutationCount,],queryFn: async () => {@@ -175,41 +190,48 @@ export function useWorkspaceFileContent(relativePath: string | null) {const mimeType = guessMimeType(relativePath);if (isCloud) {- // Cloud: no static fileserver cookie path is reachable from the- // browser. Fetch bytes server-side through callCloudProxy and- // hand the consumer a self-contained `data:` URI for iframe /- // <img> rendering.- const buffer = await AgentServerRuntimeService.downloadFile(- conversationUrl,- sessionApiKey,- relativePath,+ // Cloud: fetch through the cloud API's first-class runtime proxy+ // (GET /api/v1/app-conversations/{id}/file), which avoids the+ // removed /api/cloud-proxy hop. The endpoint returns file content as+ // a string; binary files are detected via NUL-byte sniff on the+ // decoded result and served as base64 data URIs.+ const content = await readCloudConversationFile(+ conversationId!,+ absoluteFilePath!,);+if (kind === "text") {- if (isLikelyBinary(buffer)) {+ // NUL-byte sniff on the decoded text to catch binary files that+ // the cloud endpoint decoded as UTF-8 (fallible but sufficient).+ const buf = new TextEncoder().encode(content);+ if (isLikelyBinary(buf.buffer)) {return {path: relativePath,kind: "binary",text: null,- staticUrl: `data:application/octet-stream;base64,${arrayBufferToBase64(buffer)}`,+ staticUrl: `data:application/octet-stream;base64,${arrayBufferToBase64(buf.buffer)}`,mimeType: "application/octet-stream",};}- const text = new TextDecoder("utf-8", { fatal: false }).decode(- buffer,- );return {path: relativePath,kind: "text",- text,- staticUrl: `data:${mimeType};charset=utf-8;base64,${arrayBufferToBase64(buffer)}`,+ text: content,+ staticUrl: `data:${mimeType};charset=utf-8;base64,${arrayBufferToBase64(buf.buffer)}`,mimeType,};}+ // Image / PDF via cloud API: the endpoint returns text, so binary+ // bytes are decoded as UTF-8 server-side and can't round-trip+ // faithfully. Best-effort base64 of the returned string β a proper+ // binary path needs a cloud download endpoint (the old byte-accurate+ // downloadFile route went through the removed /api/cloud-proxy).+ const buf = new TextEncoder().encode(content);return {path: relativePath,kind,text: null,- staticUrl: `data:${mimeType};base64,${arrayBufferToBase64(buffer)}`,+ staticUrl: `data:${mimeType};base64,${arrayBufferToBase64(buf.buffer)}`,mimeType,};}diff --git a/src/hooks/query/use-workspace-files.ts b/src/hooks/query/use-workspace-files.tsindex 92dc200ac0..47ec1ccd43 100644--- a/src/hooks/query/use-workspace-files.ts+++ b/src/hooks/query/use-workspace-files.ts@@ -1,12 +1,20 @@+import { useMemo } from "react";import { useQuery } from "@tanstack/react-query";import AgentServerRuntimeService from "#/api/runtime-service/agent-server-runtime-service";+import { useActiveBackend } from "#/contexts/active-backend-context";import { useActiveConversation } from "#/hooks/query/use-active-conversation";import { useRuntimeIsReady } from "#/hooks/use-runtime-is-ready";+import { useUnifiedGetGitChanges } from "#/hooks/query/use-unified-get-git-changes";// Cap the number of files we render so a giant repo doesn't freeze the UI.const MAX_FILES = 2000;+export interface WorkspaceFilesResult {+ data: string[] | undefined;+ isLoading: boolean;+}+// Directory names that we never want to descend into when listing files.const EXCLUDED_DIRS = [".git",@@ -39,11 +47,16 @@ function normalizePath(path: string): string {}/**- * Lists every regular file beneath the active conversation's working- * directory, excluding common heavy/build directories. Returns paths relative- * to the working dir (e.g. `src/index.html`).+ * Local-backend listing: enumerate every regular file beneath the active+ * conversation's working directory via `find` over the agent-server's+ * `/api/bash/execute_bash_command`, excluding common heavy/build directories.+ * Returns paths relative to the working dir (e.g. `src/index.html`).+ *+ * Local only: the cloud API exposes no bash-exec / file-listing endpoint,+ * and the old cross-origin `/api/cloud-proxy` hop these calls relied on was+ * removed from the agent-server. See `useWorkspaceFiles` for the cloud path.*/-export function useWorkspaceFiles() {+function useLocalWorkspaceFiles(enabled: boolean): WorkspaceFilesResult {const { data: conversation } = useActiveConversation();const runtimeIsReady = useRuntimeIsReady();@@ -52,7 +65,7 @@ export function useWorkspaceFiles() {const sessionApiKey = conversation?.session_api_key;const workingDir = conversation?.workspace?.working_dir?.trim();- return useQuery<string[]>({+ const query = useQuery<string[]>({queryKey: ["workspace-files",conversationId,@@ -84,10 +97,58 @@ export function useWorkspaceFiles() {// Defensive: keep results unique and bounded.return Array.from(new Set(lines)).slice(0, MAX_FILES);},- enabled: runtimeIsReady && !!conversationId && !!workingDir,+ enabled: enabled && runtimeIsReady && !!conversationId && !!workingDir,retry: false,staleTime: 1000 * 30,gcTime: 1000 * 60 * 5,meta: { disableToast: true },});++ return { data: query.data, isLoading: query.isLoading };+}++/**+ * Cloud-backend listing: derive the file list from the conversation's git+ * changes β the same data source the diff view uses (and the only+ * runtime-workspace transport the cloud API proxies, alongside git diff and+ * single-file read). `git status` reports created/modified/untracked files,+ * which covers the common Agent Canvas case (a fresh or agent-authored+ * workspace). It intentionally does NOT enumerate unchanged tracked files β+ * the cloud API has no full-workspace listing endpoint β so a conversation+ * attached to a large existing repo shows changed files rather than the whole+ * tree. Deleted files are dropped since they can't be opened.+ */+function useCloudWorkspaceFiles(enabled: boolean): WorkspaceFilesResult {+ const gitChanges = useUnifiedGetGitChanges();++ const data = useMemo(() => {+ if (!enabled) return undefined;+ const paths = gitChanges.data+ .filter((change) => change.status !== "D")+ .map((change) => change.path);+ return Array.from(new Set(paths)).slice(0, MAX_FILES);+ }, [enabled, gitChanges.data]);++ return {+ data: enabled ? data : undefined,+ isLoading: enabled ? gitChanges.isLoading : false,+ };+}++/**+ * Lists the files shown in the Files tab for the active conversation.+ *+ * Local backends enumerate the full workspace tree via bash `find`. Cloud+ * backends derive the list from git changes (see `useCloudWorkspaceFiles`+ * for the rationale and its limitation), because the cloud API exposes no+ * bash-exec or file-listing endpoint.+ */+export function useWorkspaceFiles(): WorkspaceFilesResult {+ const { backend } = useActiveBackend();+ const isCloud = backend.kind === "cloud";++ const local = useLocalWorkspaceFiles(!isCloud);+ const cloud = useCloudWorkspaceFiles(isCloud);++ return isCloud ? cloud : local;}