Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(metrics): fetch runtime conversation directly instead of removed cloud-proxy (#16392)

Tim O'Farrell21d5e716b2a5
diff --git a/.pr/automations-empty-pane.png b/.pr/automations-empty-pane.png
deleted file mode 100644
index 203d3fbb12..0000000000
Binary files a/.pr/automations-empty-pane.png and /dev/null differ
diff --git a/.pr/conversation-panel-tags/conversation-list-tags.png b/.pr/conversation-panel-tags/conversation-list-tags.png
deleted file mode 100644
index bd24db186e..0000000000
Binary files a/.pr/conversation-panel-tags/conversation-list-tags.png and /dev/null differ
diff --git a/__tests__/api/cloud/conversation-runtime-info.test.ts b/__tests__/api/cloud/conversation-runtime-info.test.ts
index aab59939f0..950c219818 100644
--- a/__tests__/api/cloud/conversation-runtime-info.test.ts
+++ b/__tests__/api/cloud/conversation-runtime-info.test.ts
@@ -70,7 +70,7 @@ describe("AgentServerConversationService.getRuntimeConversation", () => {
vi.stubGlobal("fetch", fetchMock);
});
- it("routes through /api/cloud-proxy targeting the conversation runtime host", async () => {
+ it("targets the conversation runtime host directly and forwards X-Session-API-Key", async () => {
// Arrange
fetchMock.mockResolvedValue(mockJsonResponse(runtimeResponse));
const conversationUrl =
@@ -84,16 +84,15 @@ describe("AgentServerConversationService.getRuntimeConversation", () => {
"session-xyz",
);
- // Assert
+ // Assert β€” fetch goes directly to the runtime host, not through
+ // /api/cloud-proxy (which was removed from the agent-server).
expect(fetchMock).toHaveBeenCalledOnce();
const [url, init] = getFetchCall(fetchMock);
- const body = getJsonBody(init);
- expect(url).toMatch(/\/api\/cloud-proxy$/);
- expect(body).toMatchObject({
- host: "http://abc123.runtime.all-hands.dev",
- method: "GET",
- path: "/api/conversations/conv-abc",
- headers: { "X-Session-API-Key": "session-xyz" },
+ expect(url).toContain("abc123.runtime.all-hands.dev");
+ expect(url).toContain("/api/conversations/conv-abc");
+ expect(url).not.toMatch(/\/api\/cloud-proxy$/);
+ expect(init.headers).toMatchObject({
+ "X-Session-API-Key": "session-xyz",
});
expect(result.stats.usage_to_metrics.agent?.accumulated_cost).toBe(1.23);
});
diff --git a/__tests__/hooks/query/use-conversation-metrics.test.tsx b/__tests__/hooks/query/use-conversation-metrics.test.tsx
index 4e7a791ffc..44cab91f80 100644
--- a/__tests__/hooks/query/use-conversation-metrics.test.tsx
+++ b/__tests__/hooks/query/use-conversation-metrics.test.tsx
@@ -1,4 +1,4 @@
-import { afterEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import React from "react";
import { renderHook, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
@@ -42,7 +42,43 @@ afterEach(() => {
vi.restoreAllMocks();
});
+beforeEach(() => {
+ vi.restoreAllMocks();
+});
+
describe("useConversationMetrics", () => {
+ it("fires the query on cloud backends, fetching directly from the runtime URL", async () => {
+ // Arrange β€” the runtime REST fetch must work on cloud backends.
+ // getRuntimeConversation now fetches directly from the runtime
+ // sandbox URL (conversationUrl) instead of tunneling through the
+ // removed /api/cloud-proxy endpoint.
+ const spy = vi
+ .spyOn(AgentServerConversationService, "getRuntimeConversation")
+ .mockResolvedValue(runtimeInfo);
+
+ // Act
+ const { result } = renderHook(
+ () =>
+ useConversationMetrics(
+ "conv-abc",
+ "https://runtime-abc.prod-runtime.all-hands.dev/api/conversations/conv-abc",
+ "session-key",
+ true,
+ ),
+ { wrapper: makeWrapper() },
+ );
+
+ // Assert β€” the query fires and resolves to the runtime data.
+ await waitFor(() => {
+ expect(result.current.data?.accumulated_cost).toBe(2.5);
+ });
+ expect(spy).toHaveBeenCalledWith(
+ "conv-abc",
+ "https://runtime-abc.prod-runtime.all-hands.dev/api/conversations/conv-abc",
+ "session-key",
+ );
+ });
+
it("fires the query when sessionApiKey is null (local backends without auth)", async () => {
// Arrange
const spy = vi
@@ -137,9 +173,9 @@ describe("useConversationMetrics", () => {
// Assert
await waitFor(() => {
- expect(
- result.current.data?.accumulated_token_usage?.per_turn_token,
- ).toBe(38826);
+ expect(result.current.data?.accumulated_token_usage?.per_turn_token).toBe(
+ 38826,
+ );
});
});
});
diff --git a/src/api/conversation-service/agent-server-conversation-service.api.ts b/src/api/conversation-service/agent-server-conversation-service.api.ts
index 999da8f1a0..a13e839282 100644
--- a/src/api/conversation-service/agent-server-conversation-service.api.ts
+++ b/src/api/conversation-service/agent-server-conversation-service.api.ts
@@ -636,32 +636,17 @@ class AgentServerConversationService {
conversationUrl: string | null | undefined,
sessionApiKey?: string | null,
): Promise<RuntimeConversationInfo> {
- const active = getActiveBackend().backend;
-
type RawRuntime = DirectConversationInfo & {
stats?: RuntimeConversationInfo["stats"];
};
- // Cloud mode: route through the cloud-proxy to the runtime sandbox at
- // the conversation's runtime URL β€” same pattern as getVSCodeUrl. Local
- // mode forwards conversationUrl so the host explicitly resolves to the
- // conversation's runtime instead of falling back to the active backend.
- const response =
- active.kind === "cloud" && conversationUrl
- ? await callCloudProxy<RawRuntime>({
- backend: active,
- method: "GET",
- hostOverride: buildHttpBaseUrl(conversationUrl),
- path: `/api/conversations/${conversationId}`,
- authMode: "session-api-key",
- sessionApiKey,
- })
- : await new ConversationClient(
- getAgentServerClientOptions({
- conversationUrl,
- sessionApiKey,
- }),
- ).getConversation<RawRuntime>(conversationId);
+ // Fetch directly from the per-conversation runtime agent-server at conversationUrl.
+ const response = await new ConversationClient(
+ getAgentServerClientOptions({
+ conversationUrl,
+ sessionApiKey,
+ }),
+ ).getConversation<RawRuntime>(conversationId);
const data = requireDirectConversationInfo(response);
const stats = isRecord(response) ? response.stats : null;