π OpenHands: AI-Driven Development
Fix stale default-local session key hydration (#607)
Jamie Chicagobec399ca8134
diff --git a/__tests__/api/backend-registry/storage.test.ts b/__tests__/api/backend-registry/storage.test.tsindex 8fab5f0ebd..25aa643775 100644--- a/__tests__/api/backend-registry/storage.test.ts+++ b/__tests__/api/backend-registry/storage.test.ts@@ -121,6 +121,36 @@ describe("backend-registry storage", () => {});});++ it("refreshes a stale API key on the default Local backend from env defaults", () => {+ vi.stubEnv("VITE_SESSION_API_KEY", "fresh-session-key");+ window.localStorage.setItem(+ BACKENDS_STORAGE_KEY,+ JSON.stringify([+ {+ id: "default-local",+ name: "Local",+ host: window.location.origin,+ apiKey: "stale-session-key",+ kind: "local",+ },+ ]),+ );++ const result = readStoredBackends();++ expect(result[0]).toMatchObject({+ id: "default-local",+ apiKey: "fresh-session-key",+ });+ expect(+ JSON.parse(window.localStorage.getItem(BACKENDS_STORAGE_KEY) ?? "[]")[0],+ ).toMatchObject({+ id: "default-local",+ apiKey: "fresh-session-key",+ });+ });+it("does not fill the default Local backend API key after its host is edited", () => {vi.stubEnv("VITE_SESSION_API_KEY", "fresh-session-key");window.localStorage.setItem(diff --git a/__tests__/api/to-app-conversation-session-key.test.ts b/__tests__/api/to-app-conversation-session-key.test.tsnew file mode 100644index 0000000000..d52c270570--- /dev/null+++ b/__tests__/api/to-app-conversation-session-key.test.ts@@ -0,0 +1,39 @@+import { afterEach, describe, expect, it, vi } from "vitest";++import { toAppConversation } from "#/api/agent-server-adapter";+import {+ __resetActiveStoreForTests,+ setRegisteredBackends,+} from "#/api/backend-registry/active-store";+import { DEFAULT_LOCAL_BACKEND_ID } from "#/api/backend-registry/default-backend";++const directInfo = (id: string) => ({+ id,+ created_at: "2026-05-05T00:00:00Z",+ updated_at: "2026-05-05T00:00:00Z",+});++afterEach(() => {+ window.localStorage.clear();+ __resetActiveStoreForTests();+ vi.unstubAllEnvs();+});++describe("toAppConversation session_api_key hydration", () => {+ it("prefers the configured VITE_SESSION_API_KEY over a stale stored default-local apiKey", () => {+ vi.stubEnv("VITE_SESSION_API_KEY", "fresh-session-key");++ setRegisteredBackends([+ {+ id: DEFAULT_LOCAL_BACKEND_ID,+ name: "Local",+ host: window.location.origin,+ apiKey: "stale-session-key",+ kind: "local",+ },+ ]);++ const conversation = toAppConversation(directInfo("conv-1"));+ expect(conversation.session_api_key).toBe("fresh-session-key");+ });+});diff --git a/src/api/agent-server-adapter.ts b/src/api/agent-server-adapter.tsindex d3641a5dfc..a7568df222 100644--- a/src/api/agent-server-adapter.ts+++ b/src/api/agent-server-adapter.ts@@ -1,6 +1,7 @@import { DEFAULT_SETTINGS } from "#/services/settings";import { ExecutionStatus } from "#/types/agent-server/core";import { Settings, SettingsValue } from "#/types/settings";+import { getAgentServerClientOptions } from "./agent-server-client-options";import { isAgentServerToolAvailable } from "./agent-server-compatibility";import { getAgentServerWorkingDir } from "./agent-server-config";import { getEffectiveLocalBackend } from "./backend-registry/active-store";@@ -210,7 +211,8 @@ export function toConversationUrl(conversationId: string): string {// Local-format conversation URL β points at whichever local agent-server// is actually serving the conversation (the bundled one when the active// selection is cloud).- return `${getEffectiveLocalBackend().host}/api/conversations/${conversationId}`;+ const { host } = getAgentServerClientOptions();+ return `${host}/api/conversations/${conversationId}`;}// TODO(i18n): extract "Conversation" once we add CONVERSATION$DEFAULT_TITLE@@ -266,7 +268,7 @@ export function toAppConversation(ExecutionStatus.IDLE,sandbox_status: (info.sandbox_status as SandboxStatus | null) ?? null,conversation_url: toConversationUrl(info.id),- session_api_key: getEffectiveLocalBackend().apiKey || null,+ session_api_key: getAgentServerClientOptions().apiKey ?? null,sandbox_id: null,workspace: {working_dir: info.workspace?.working_dir ?? getAgentServerWorkingDir(),diff --git a/src/api/backend-registry/auth.ts b/src/api/backend-registry/auth.tsindex 6a273cbe33..3b2d47b981 100644--- a/src/api/backend-registry/auth.ts+++ b/src/api/backend-registry/auth.ts@@ -1,3 +1,5 @@+import { getAgentServerSessionApiKey } from "../agent-server-config";+import { DEFAULT_LOCAL_BACKEND_ID } from "./default-backend";import type { Backend } from "./types";/**@@ -7,6 +9,13 @@ import type { Backend } from "./types";* token in the `Authorization` header.*/export function buildAuthHeaders(backend: Backend): Record<string, string> {+ if (backend.kind === "local" && backend.id === DEFAULT_LOCAL_BACKEND_ID) {+ const configuredSessionApiKey = getAgentServerSessionApiKey();+ if (configuredSessionApiKey) {+ return { "X-Session-API-Key": configuredSessionApiKey };+ }+ }+if (!backend.apiKey) return {};if (backend.kind === "cloud") {diff --git a/src/api/backend-registry/storage.ts b/src/api/backend-registry/storage.tsindex 058e9517c3..cb89f9b545 100644--- a/src/api/backend-registry/storage.ts+++ b/src/api/backend-registry/storage.ts@@ -35,7 +35,6 @@ function syncDefaultLocalBackendAuth(backend: Backend): Backend {if (backend.id !== defaultBackend.id ||backend.kind !== "local" ||- backend.apiKey ||!defaultBackend.apiKey ||normalizeHostForComparison(backend.host) !==normalizeHostForComparison(defaultBackend.host)@@ -43,6 +42,10 @@ function syncDefaultLocalBackendAuth(backend: Backend): Backend {return backend;}+ if (backend.apiKey === defaultBackend.apiKey) {+ return backend;+ }+return {...backend,apiKey: defaultBackend.apiKey,