π OpenHands: AI-Driven Development
fix(settings): key the settings cache on the backend that answered (#17430)
Lukas584d80b2efd0
diff --git a/.pr/settings-cache-backend-scoping.png b/.pr/settings-cache-backend-scoping.pngnew file mode 100644index 0000000000..0be25da05dBinary files /dev/null and b/.pr/settings-cache-backend-scoping.png differdiff --git a/.pr/settings-cache-live-after.png b/.pr/settings-cache-live-after.pngnew file mode 100644index 0000000000..677f606ce3Binary files /dev/null and b/.pr/settings-cache-live-after.png differdiff --git a/.pr/settings-cache-live-before.png b/.pr/settings-cache-live-before.pngnew file mode 100644index 0000000000..ff525c47aeBinary files /dev/null and b/.pr/settings-cache-live-before.png differdiff --git a/__tests__/api/settings-service.test.ts b/__tests__/api/settings-service.test.tsindex bf9b9663ef..4231ca7cfc 100644--- a/__tests__/api/settings-service.test.ts+++ b/__tests__/api/settings-service.test.ts@@ -1060,3 +1060,198 @@ describe("SettingsService", () => {expect(settings.language).toBe("ja");});});++describe("SettingsService cache scoping", () => {+ const backendA: Backend = {+ id: "local-a",+ name: "Local A",+ host: "http://127.0.0.1:3101",+ apiKey: "key-a",+ kind: "local",+ };+ const backendB: Backend = {+ id: "local-b",+ name: "Local B",+ host: "http://127.0.0.1:3102",+ apiKey: "key-b",+ kind: "local",+ };++ /**+ * Answers /api/settings with a model naming the host that was asked. The+ * host on `heldPort`, if given, answers only once `release()` is called.+ */+ const perHostSettings = (heldPort?: string) => {+ const asked: string[] = [];+ let release = () => {};+ const released = new Promise<void>((resolve) => {+ release = resolve;+ });+ server.use(+ http.get("*/api/settings", async ({ request }) => {+ const url = new URL(request.url);+ if (url.pathname.split("/").filter(Boolean).length > 2) {+ return undefined as never;+ }+ asked.push(url.port);+ if (url.port === heldPort) await released;+ return HttpResponse.json({+ agent_settings: {+ agent: "CodeActAgent",+ llm: { model: `model-from-${url.port}` },+ },+ conversation_settings: {},+ llm_api_key_is_set: true,+ });+ }),+ );+ return { asked, release };+ };++ beforeEach(() => {+ window.localStorage.clear();+ resetTestHandlersMockSettings();+ __resetActiveStoreForTests();+ SettingsService.invalidateCache();+ setRegisteredBackends([backendA, backendB]);+ });++ afterEach(() => {+ __resetActiveStoreForTests();+ });++ it("does not answer for one backend with settings fetched from another", async () => {+ // The cache was keyed on a timestamp alone, so within its five minutes a+ // switch to another local backend was served the first one's settings β+ // including the encrypted ones used to start conversations (#17416).+ const { asked } = perHostSettings();++ setActiveSelection({ backendId: backendA.id });+ const fromA = await SettingsService.getSettings();++ setActiveSelection({ backendId: backendB.id });+ const fromB = await SettingsService.getSettings();++ expect(fromA.llm_model).toBe("model-from-3101");+ expect(fromB.llm_model).toBe("model-from-3102");+ expect(asked).toEqual(["3101", "3102"]);+ });++ it("does not hand one backend's encrypted settings to another", async () => {+ const { asked } = perHostSettings();++ setActiveSelection({ backendId: backendA.id });+ await SettingsService.getSettingsForConversation();++ setActiveSelection({ backendId: backendB.id });+ const forB = await SettingsService.getSettingsForConversation();++ expect((forB.agentSettings.llm as { model: string }).model).toBe(+ "model-from-3102",+ );+ expect(asked).toEqual(["3101", "3102"]);+ });++ it("does not keep one backend's encrypted settings beside another's", async () => {+ // A read for B replaces only the redacted half, so the rest of A's entry+ // has to go with it, or B's next conversation starts with A's settings.+ const { asked } = perHostSettings();++ setActiveSelection({ backendId: backendA.id });+ await SettingsService.getSettingsForConversation();++ setActiveSelection({ backendId: backendB.id });+ await SettingsService.getSettings();+ const forB = await SettingsService.getSettingsForConversation();++ expect((forB.agentSettings.llm as { model: string }).model).toBe(+ "model-from-3102",+ );+ expect(asked).toEqual(["3101", "3102", "3102"]);+ });++ it("does not reuse settings after the same backend's credentials change", async () => {+ // `connectionRevision` changes whenever the connection credentials do β+ // the host stays the same while what it answers with does not, which is+ // why the query keys across the app already include it.+ const { asked } = perHostSettings();++ setActiveSelection({ backendId: backendA.id });+ await SettingsService.getSettings();++ setRegisteredBackends([+ { ...backendA, apiKey: "rotated", connectionRevision: 1 },+ backendB,+ ]);+ setActiveSelection({ backendId: backendA.id });+ await SettingsService.getSettings();++ expect(asked).toEqual(["3101", "3101"]);+ });++ it("still serves the same backend from cache", async () => {+ // The accept control: the cache exists to avoid a request per read, and a+ // fix that simply stopped caching would pass the two cells above.+ const { asked } = perHostSettings();++ setActiveSelection({ backendId: backendA.id });+ await SettingsService.getSettings();+ await SettingsService.getSettings();++ expect(asked).toEqual(["3101"]);+ });++ it("does not file a response under a backend selected while it was in flight", async () => {+ // The key used to be read after the request, so a switch from A to B+ // while A's answer was on its way cached that answer as B's, and the next+ // read for B was served A's settings without a request.+ const { asked, release } = perHostSettings("3101");++ setActiveSelection({ backendId: backendA.id });+ const pending = SettingsService.getSettings();+ await vi.waitFor(() => expect(asked).toEqual(["3101"]));++ setActiveSelection({ backendId: backendB.id });+ release();+ expect((await pending).llm_model).toBe("model-from-3101");+ const forB = await SettingsService.getSettings();++ expect(forB.llm_model).toBe("model-from-3102");+ expect(asked).toEqual(["3101", "3102"]);+ });++ it("does not file encrypted settings under a backend selected while they were in flight", async () => {+ const { asked, release } = perHostSettings("3101");++ setActiveSelection({ backendId: backendA.id });+ const pending = SettingsService.getSettingsForConversation();+ await vi.waitFor(() => expect(asked).toEqual(["3101"]));++ setActiveSelection({ backendId: backendB.id });+ release();+ await pending;+ const forB = await SettingsService.getSettingsForConversation();++ expect((forB.agentSettings.llm as { model: string }).model).toBe(+ "model-from-3102",+ );+ expect(asked).toEqual(["3101", "3102"]);+ });++ it("keeps the new backend's cache when the old one answers late", async () => {+ const { asked, release } = perHostSettings("3101");++ setActiveSelection({ backendId: backendA.id });+ const pending = SettingsService.getSettings();+ await vi.waitFor(() => expect(asked).toEqual(["3101"]));++ setActiveSelection({ backendId: backendB.id });+ await SettingsService.getSettings();+ release();+ await pending;+ const again = await SettingsService.getSettings();++ expect(again.llm_model).toBe("model-from-3102");+ expect(asked).toEqual(["3101", "3102"]);+ });+});diff --git a/src/api/settings-service/settings-service.api.ts b/src/api/settings-service/settings-service.api.tsindex 441578a23d..5634146dba 100644--- a/src/api/settings-service/settings-service.api.ts+++ b/src/api/settings-service/settings-service.api.ts@@ -173,18 +173,54 @@ let settingsCache: {encrypted: SettingsApiResponse | null;/** Timestamp when the cache was last populated */timestamp: number;+ /** Which backend answered. Settings from one are not settings from another. */+ backendKey: string | null;} = {redacted: null,encrypted: null,timestamp: 0,+ backendKey: null,};const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes-const isCacheValid = () => Date.now() - settingsCache.timestamp < CACHE_TTL_MS;+/**+ * Identity of the backend a cached response came from.+ *+ * `connectionRevision` is part of it because it changes whenever the+ * connection credentials do, which is the same reason the query keys across+ * the app already include it: the host can stay the same while what it+ * answers with does not.+ */+const activeBackendKey = (): string => {+ const { backend } = getActiveBackend();+ return `${backend.id}:${backend.connectionRevision ?? 0}`;+};++const isCacheValid = () =>+ settingsCache.backendKey === activeBackendKey() &&+ Date.now() - settingsCache.timestamp < CACHE_TTL_MS;const clearCache = () => {- settingsCache = { redacted: null, encrypted: null, timestamp: 0 };+ settingsCache = {+ redacted: null,+ encrypted: null,+ timestamp: 0,+ backendKey: null,+ };+};++/**+ * Whether a response requested from `requestedKey` may be cached: only while+ * that backend is still the active one, so a switch during the request does+ * not file the old backend's answer under the new one. An entry from another+ * backend is dropped first, so the two are never mixed.+ */+const prepareCacheFor = (requestedKey: string): boolean => {+ if (activeBackendKey() !== requestedKey) return false;+ if (settingsCache.backendKey !== requestedKey) clearCache();+ settingsCache.backendKey = requestedKey;+ return true;};const isRecord = (value: unknown): value is Record<string, unknown> =>@@ -578,10 +614,13 @@ class SettingsService {return syncDerivedSettings(transformApiResponse(settingsCache.redacted));}+ const requestedKey = activeBackendKey();try {const response = await this.fetchSettingsFromApi();- settingsCache.redacted = response;- settingsCache.timestamp = Date.now();+ if (prepareCacheFor(requestedKey)) {+ settingsCache.redacted = response;+ settingsCache.timestamp = Date.now();+ }return syncDerivedSettings(transformApiResponse(response));} catch (error) {// If API fails, return defaults@@ -616,10 +655,13 @@ class SettingsService {// Fetch encrypted settings - this MUST succeed for conversations to work.// Do not fall back to redacted settings as that would cause auth failures.+ const requestedKey = activeBackendKey();const response = await this.fetchSettingsFromApi("encrypted");- settingsCache.encrypted = response;- if (!settingsCache.timestamp) {- settingsCache.timestamp = Date.now();+ if (prepareCacheFor(requestedKey)) {+ settingsCache.encrypted = response;+ if (!settingsCache.timestamp) {+ settingsCache.timestamp = Date.now();+ }}return {agentSettings: response.agent_settings,