π OpenHands: AI-Driven Development
Fix tab-scoped active backend selection (#1217)
Graham Neubig3a8e3a54510d
diff --git a/__tests__/api/backend-registry/active-store.test.ts b/__tests__/api/backend-registry/active-store.test.tsindex 1afda6cc93..ef3888bcc6 100644--- a/__tests__/api/backend-registry/active-store.test.ts+++ b/__tests__/api/backend-registry/active-store.test.ts@@ -13,11 +13,13 @@ import type { Backend } from "#/api/backend-registry/types";beforeEach(() => {window.localStorage.clear();+ window.sessionStorage.clear();__resetActiveStoreForTests();});afterEach(() => {window.localStorage.clear();+ window.sessionStorage.clear();vi.unstubAllEnvs();__resetActiveStoreForTests();});@@ -41,6 +43,7 @@ const localBackend: Backend = {describe("active-store", () => {it("uses the no-backend sentinel when no backend details are available", () => {window.localStorage.clear();+ window.sessionStorage.clear();vi.stubEnv("VITE_SESSION_API_KEY", "");__resetActiveStoreForTests();diff --git a/__tests__/api/backend-registry/storage.test.ts b/__tests__/api/backend-registry/storage.test.tsindex b742b7f27a..f794d34f11 100644--- a/__tests__/api/backend-registry/storage.test.ts+++ b/__tests__/api/backend-registry/storage.test.ts@@ -11,6 +11,7 @@ import type { Backend } from "#/api/backend-registry/types";afterEach(() => {window.localStorage.clear();+ window.sessionStorage.clear();vi.unstubAllEnvs();});@@ -253,10 +254,41 @@ describe("backend-registry storage", () => {});});+ it("prefers the tab-scoped active selection over the global fallback", () => {+ window.localStorage.setItem(+ ACTIVE_BACKEND_STORAGE_KEY,+ JSON.stringify({ backendId: "global-backend", orgId: null }),+ );+ window.sessionStorage.setItem(+ ACTIVE_BACKEND_STORAGE_KEY,+ JSON.stringify({ backendId: "tab-backend", orgId: "org-1" }),+ );++ expect(readStoredActiveBackend()).toEqual({+ backendId: "tab-backend",+ orgId: "org-1",+ });+ });++ it("falls back to the global active selection for new tabs", () => {+ window.localStorage.setItem(+ ACTIVE_BACKEND_STORAGE_KEY,+ JSON.stringify({ backendId: "global-backend", orgId: null }),+ );++ expect(readStoredActiveBackend()).toEqual({+ backendId: "global-backend",+ orgId: null,+ });+ });+it("clears storage when active selection is set to null", () => {writeStoredActiveBackend({ backendId: "xyz", orgId: "o" });writeStoredActiveBackend(null);+ expect(+ window.sessionStorage.getItem(ACTIVE_BACKEND_STORAGE_KEY),+ ).toBeNull();expect(window.localStorage.getItem(ACTIVE_BACKEND_STORAGE_KEY)).toBeNull();expect(readStoredActiveBackend()).toBeNull();});diff --git a/src/api/backend-registry/storage.ts b/src/api/backend-registry/storage.tsindex a8278f6876..a29e90f105 100644--- a/src/api/backend-registry/storage.ts+++ b/src/api/backend-registry/storage.ts@@ -175,11 +175,10 @@ export function readStoredBackends(): Backend[] {}}-export function readStoredActiveBackend(): BackendSelection | null {- if (typeof window === "undefined") return null;+function parseBackendSelection(raw: string | null): BackendSelection | null {+ if (!raw) return null;+try {- const raw = window.localStorage.getItem(ACTIVE_BACKEND_STORAGE_KEY);- if (!raw) return null;const parsed = JSON.parse(raw);if (typeof parsed !== "object" ||@@ -199,23 +198,74 @@ export function readStoredActiveBackend(): BackendSelection | null {}}-export function writeStoredActiveBackend(- selection: BackendSelection | null,+function readStorageItem(+ storage: Storage | undefined,+ key: string,+): string | null {+ try {+ return storage?.getItem(key) ?? null;+ } catch {+ return null;+ }+}++function writeStorageItem(+ storage: Storage | undefined,+ key: string,+ value: string,): void {- if (typeof window === "undefined") return;try {- if (!selection) {- window.localStorage.removeItem(ACTIVE_BACKEND_STORAGE_KEY);- return;- }- window.localStorage.setItem(- ACTIVE_BACKEND_STORAGE_KEY,- JSON.stringify({- backendId: selection.backendId,- orgId: selection.orgId ?? null,- }),- );+ storage?.setItem(key, value);+ } catch {+ /* ignore */+ }+}++function removeStorageItem(storage: Storage | undefined, key: string): void {+ try {+ storage?.removeItem(key);} catch {/* ignore */}}++export function readStoredActiveBackend(): BackendSelection | null {+ if (typeof window === "undefined") return null;++ // Active backend is tab-scoped so reloading tab A does not adopt tab B's+ // backend. localStorage remains a last-used fallback for fresh tabs and old+ // persisted state.+ const sessionSelection = parseBackendSelection(+ readStorageItem(window.sessionStorage, ACTIVE_BACKEND_STORAGE_KEY),+ );+ if (sessionSelection) return sessionSelection;++ return parseBackendSelection(+ readStorageItem(window.localStorage, ACTIVE_BACKEND_STORAGE_KEY),+ );+}++export function writeStoredActiveBackend(+ selection: BackendSelection | null,+): void {+ if (typeof window === "undefined") return;++ if (!selection) {+ removeStorageItem(window.sessionStorage, ACTIVE_BACKEND_STORAGE_KEY);+ removeStorageItem(window.localStorage, ACTIVE_BACKEND_STORAGE_KEY);+ return;+ }++ const serialized = JSON.stringify({+ backendId: selection.backendId,+ orgId: selection.orgId ?? null,+ });+ // Mirror to localStorage only as the default for new tabs/backward+ // compatibility; reads in existing tabs prefer sessionStorage.+ writeStorageItem(+ window.sessionStorage,+ ACTIVE_BACKEND_STORAGE_KEY,+ serialized,+ );+ writeStorageItem(window.localStorage, ACTIVE_BACKEND_STORAGE_KEY, serialized);+}diff --git a/vitest.setup.ts b/vitest.setup.tsindex b7d23ed6ad..4413a388c5 100644--- a/vitest.setup.ts+++ b/vitest.setup.ts@@ -112,6 +112,7 @@ beforeEach(() => {afterEach(async () => {server.resetHandlers();+ window.sessionStorage?.removeItem("openhands-active-backend");// Cleanup the document body after each testcleanup();// Drain any queued microtasks before jsdom is torn down between test files.