π OpenHands: AI-Driven Development
fix(automations): read local run logs from the server-level bash events (#18022)
Engel Nyst353001513b21
diff --git a/__tests__/api/bash-service.test.ts b/__tests__/api/bash-service.test.tsindex f391a4c5df..88f582f71d 100644--- a/__tests__/api/bash-service.test.ts+++ b/__tests__/api/bash-service.test.ts@@ -6,6 +6,7 @@ import {setRegisteredBackends,} from "#/api/backend-registry/active-store";import BashService from "#/api/bash-service/bash-service.api";+import { getAgentServerClientOptions } from "#/api/agent-server-client-options";import { callCloudProxy } from "#/api/cloud/proxy";import type { Backend } from "#/api/backend-registry/types";@@ -77,6 +78,7 @@ beforeEach(() => {window.localStorage.clear();__resetActiveStoreForTests();vi.mocked(BashClient).mockClear();+ vi.mocked(getAgentServerClientOptions).mockClear();searchEventsMock.mockReset();vi.mocked(callCloudProxy).mockReset();});@@ -84,6 +86,7 @@ beforeEach(() => {afterEach(() => {window.localStorage.clear();__resetActiveStoreForTests();+ vi.restoreAllMocks();});describe("BashService.listOutputs β local backend", () => {@@ -120,6 +123,54 @@ describe("BashService.listOutputs β local backend", () => {expect(outputs).toEqual([OUTPUT_1, OUTPUT_2]);});+ it("searches the server-level bash events for a run that has a conversation", async () => {+ // Arrange: the real option builder and client, so the assertion is on the+ // request the browser actually sends. The automation's command runs at+ // server level, so a conversation-scoped search would come back empty.+ const actualClients = await vi.importActual<+ typeof import("@openhands/typescript-client/clients")+ >("@openhands/typescript-client/clients");+ const actualOptions = await vi.importActual<+ typeof import("#/api/agent-server-client-options")+ >("#/api/agent-server-client-options");+ vi.mocked(getAgentServerClientOptions).mockImplementationOnce(+ actualOptions.getAgentServerClientOptions,+ );+ vi.mocked(BashClient).mockImplementationOnce(+ function RealBashClient(options) {+ return new actualClients.BashClient(options);+ },+ );+ const fetchMock = vi+ .spyOn(globalThis, "fetch")+ .mockImplementation(async (input) => {+ const body = String(input).includes("/server_info")+ ? { capabilities: ["conversation_runtime_routes_v1"] }+ : { items: [OUTPUT_1], next_page_id: null };+ return new Response(JSON.stringify(body), {+ status: 200,+ headers: { "Content-Type": "application/json" },+ });+ });++ // Act+ const outputs = await BashService.listOutputs(+ `${localBackend.host}/api/conversations/conv-1`,+ "local-key",+ BASH_CMD_ID,+ );++ // Assert+ const urls = fetchMock.mock.calls.map(([input]) => new URL(String(input)));+ expect(urls.map((url) => url.pathname)).toEqual([+ "/api/bash/bash_events/search",+ ]);+ expect(urls[0].origin).toBe(localBackend.host);+ expect(urls[0].searchParams.get("command_id__eq")).toBe(BASH_CMD_ID);+ expect(urls[0].searchParams.has("cid")).toBe(false);+ expect(outputs).toEqual([OUTPUT_1]);+ });+it("works without a conversation URL (falls back to backend host)", async () => {searchEventsMock.mockResolvedValueOnce({ items: [OUTPUT_1] });@@ -149,6 +200,10 @@ describe("BashService.listOutputs β cloud backend", () => {// Cloud now hits the runtime host directly (CORS allowlisted), not// the /api/cloud-proxy envelope.expect(callCloudProxy).not.toHaveBeenCalled();+ expect(getAgentServerClientOptions).toHaveBeenCalledWith({+ conversationUrl: CONVERSATION_URL,+ sessionApiKey: SESSION_KEY,+ });expect(BashClient).toHaveBeenCalledTimes(1);expect(searchEventsMock).toHaveBeenCalledWith({kind__eq: "BashOutput",diff --git a/src/api/bash-service/bash-service.api.ts b/src/api/bash-service/bash-service.api.tsindex 932ea98422..9d3f5bc810 100644--- a/src/api/bash-service/bash-service.api.ts+++ b/src/api/bash-service/bash-service.api.ts@@ -26,9 +26,11 @@ function isBashOutput(event: BashEvent): event is BashOutput {** Bash events live on the agent-server runtime that owns the* conversation. In **local** mode we talk to the active backend's- * agent-server directly with the SDK's `BashClient` (a per-conversation- * URL is honoured when known, otherwise we fall back to the backend- * host β a single local agent-server hosts all conversations). In+ * agent-server directly with the SDK's `BashClient` at server level and+ * ignore any per-conversation URL: a single local agent-server hosts all+ * conversations, and the automation's command runs on it outside any+ * conversation, so a conversation-scoped search+ * (`/api/conversations/<id>/bash/...`) never finds its output. In* **cloud** mode we call that same per-conversation runtime host* directly from the browser: the runtime's CORS allowlist* (`OH_ALLOW_CORS_ORIGINS`, set to the Canvas origin in saas-deploy)@@ -78,7 +80,8 @@ class BashService {sessionApiKey: string | null | undefined,options: SearchOptions,): Promise<BashEventPage> {- if (getActiveBackend().backend.kind === "cloud") {+ const isCloud = getActiveBackend().backend.kind === "cloud";+ if (isCloud) {// Cloud requires the per-conversation runtime URL β there is no// shared cloud host that owns bash events. Callers must wait for// the conversation to be hydrated before invoking this method on@@ -90,9 +93,10 @@ class BashService {}}+ // Local searches stay at server level (see the class comment).return new BashClient(getAgentServerClientOptions({- ...(conversationUrl ? { conversationUrl } : {}),+ ...(isCloud && conversationUrl ? { conversationUrl } : {}),sessionApiKey,}),).searchEvents(options);diff --git a/src/hooks/query/use-bash-command-logs.ts b/src/hooks/query/use-bash-command-logs.tsindex 7ff03e49b2..d7f0102b9f 100644--- a/src/hooks/query/use-bash-command-logs.ts+++ b/src/hooks/query/use-bash-command-logs.ts@@ -124,9 +124,10 @@ function classifyFetchError(error: unknown): SandboxIssue | null {* - **Local backend**: the query fires as soon as the modal opens and* we have a `bash_command_id`. The conversation lookup runs in* parallel; if it resolves with `session_api_key`/`conversation_url`- * those are passed through, but a missing/stale conversation does not- * block the bash query (the local agent-server hosts events under a- * single root).+ * those are passed through (`BashService` still searches at server+ * level, where the automation's command runs), but a missing/stale+ * conversation does not block the bash query (the local agent-server+ * hosts events under a single root).* - **Cloud backend**: the run's agent-server is found through its* conversation when it has one, and through its sandbox otherwise* (script automations never create a conversation). Either lookup