π OpenHands: AI-Driven Development
fix: Cache automation SDK version telemetry lookup (#1919)
Rohit Malhotrae645e334807b
diff --git a/__tests__/hooks/query/use-automation-sdk-version.test.tsx b/__tests__/hooks/query/use-automation-sdk-version.test.tsxnew file mode 100644index 0000000000..7d0cb77476--- /dev/null+++ b/__tests__/hooks/query/use-automation-sdk-version.test.tsx@@ -0,0 +1,117 @@+import { renderHook, waitFor } from "@testing-library/react";+import { beforeEach, describe, expect, it, vi } from "vitest";+import AutomationService from "#/api/automation-service/automation-service.api";+import type { ResolvedActiveBackend } from "#/api/backend-registry/types";+import {+ __resetAutomationSdkVersionCacheForTests,+ useAutomationSdkVersion,+} from "#/hooks/query/use-automation-sdk-version";++vi.mock("#/api/automation-service/automation-service.api", () => ({+ default: {+ getSdkVersion: vi.fn(),+ },+}));++const activeBackendMock = vi.hoisted(() => ({+ active: {+ backend: {+ id: "local-1",+ name: "Local",+ host: "http://localhost:8000",+ apiKey: "session-key",+ kind: "local",+ },+ orgId: null,+ } as ResolvedActiveBackend,+}));++vi.mock("#/contexts/active-backend-context", () => ({+ useActiveBackend: () => activeBackendMock.active,+}));++describe("useAutomationSdkVersion", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ __resetAutomationSdkVersionCacheForTests();+ activeBackendMock.active = {+ backend: {+ id: "local-1",+ name: "Local",+ host: "http://localhost:8000",+ apiKey: "session-key",+ kind: "local",+ },+ orgId: null,+ };+ });++ it("shares one SDK version request across multiple hook consumers", async () => {+ vi.mocked(AutomationService.getSdkVersion).mockResolvedValue("1.36.3");++ const { result } = renderHook(() => ({+ first: useAutomationSdkVersion(),+ second: useAutomationSdkVersion(),+ }));++ await waitFor(() => expect(result.current.first).toBe("1.36.3"));++ expect(result.current.second).toBe("1.36.3");+ expect(AutomationService.getSdkVersion).toHaveBeenCalledTimes(1);+ });++ it("keeps the SDK version cached across hook remounts", async () => {+ vi.mocked(AutomationService.getSdkVersion).mockResolvedValue("1.36.3");++ const first = renderHook(() => useAutomationSdkVersion());+ await waitFor(() => expect(first.result.current).toBe("1.36.3"));+ first.unmount();++ const second = renderHook(() => useAutomationSdkVersion());+ expect(second.result.current).toBe("1.36.3");++ expect(AutomationService.getSdkVersion).toHaveBeenCalledTimes(1);+ });++ it("fetches a new SDK version when the active backend changes", async () => {+ vi.mocked(AutomationService.getSdkVersion)+ .mockResolvedValueOnce("1.36.3")+ .mockResolvedValueOnce("1.37.0");++ const { result, rerender } = renderHook(() => useAutomationSdkVersion());+ await waitFor(() => expect(result.current).toBe("1.36.3"));++ activeBackendMock.active = {+ backend: {+ id: "cloud-1",+ name: "Cloud",+ host: "https://app.all-hands.dev",+ apiKey: "cloud-key",+ kind: "cloud",+ },+ orgId: "org-1",+ };+ rerender();++ await waitFor(() => expect(result.current).toBe("1.37.0"));+ expect(AutomationService.getSdkVersion).toHaveBeenCalledTimes(2);+ });++ it("does not query without an available backend", () => {+ activeBackendMock.active = {+ backend: {+ id: "no-backend",+ name: "No Backend Available",+ host: "",+ apiKey: "",+ kind: "local",+ },+ orgId: null,+ };++ const { result } = renderHook(() => useAutomationSdkVersion());++ expect(result.current).toBeNull();+ expect(AutomationService.getSdkVersion).not.toHaveBeenCalled();+ });+});diff --git a/__tests__/hooks/use-tracking.test.ts b/__tests__/hooks/use-tracking.test.tsindex cb9eb2aa9c..137310fd3e 100644--- a/__tests__/hooks/use-tracking.test.ts+++ b/__tests__/hooks/use-tracking.test.ts@@ -14,11 +14,11 @@ vi.mock("#/api/agent-server-compatibility", () => ({getCachedAgentServerVersion: compatibilityMocks.getCachedAgentServerVersion,}));-const automationServiceMocks = vi.hoisted(() => ({- getSdkVersion: vi.fn(),+const automationSdkVersionHookMock = vi.hoisted(() => ({+ useAutomationSdkVersion: vi.fn(),}));-vi.mock("#/api/automation-service/automation-service.api", () => ({- default: automationServiceMocks,+vi.mock("#/hooks/query/use-automation-sdk-version", () => ({+ useAutomationSdkVersion: automationSdkVersionHookMock.useAutomationSdkVersion,}));import { useTracking } from "#/hooks/use-tracking";@@ -37,8 +37,8 @@ describe("useTracking", () => {.mockResolvedValue(undefined);setBackendContextMock = vi.spyOn(telemetry, "setTelemetryBackendContext");useSettingsMock.mockReset();- automationServiceMocks.getSdkVersion.mockReset();- automationServiceMocks.getSdkVersion.mockResolvedValue(null);+ automationSdkVersionHookMock.useAutomationSdkVersion.mockReset();+ automationSdkVersionHookMock.useAutomationSdkVersion.mockReturnValue(null);compatibilityMocks.getCachedAgentServerVersion.mockReset();compatibilityMocks.getCachedAgentServerVersion.mockReturnValue(null);@@ -149,7 +149,9 @@ describe("useTracking", () => {});it("declares backend versions to the telemetry service when known", async () => {- automationServiceMocks.getSdkVersion.mockResolvedValue("1.36.3");+ automationSdkVersionHookMock.useAutomationSdkVersion.mockReturnValue(+ "1.36.3",+ );compatibilityMocks.getCachedAgentServerVersion.mockReturnValue("1.36.2");const { result } = renderHook(() => useTracking());diff --git a/src/hooks/query/use-automation-sdk-version.ts b/src/hooks/query/use-automation-sdk-version.tsnew file mode 100644index 0000000000..3f5a64a7c3--- /dev/null+++ b/src/hooks/query/use-automation-sdk-version.ts@@ -0,0 +1,112 @@+import React from "react";+import AutomationService from "#/api/automation-service/automation-service.api";+import { isNoBackend } from "#/api/backend-registry/active-store";+import { useActiveBackend } from "#/contexts/active-backend-context";++export const AUTOMATION_SDK_VERSION_CACHE_NAMESPACE = "automation-sdk-version";++const AUTOMATION_SDK_VERSION_CACHE_TIME_MS = 60 * 60 * 1000;+const CACHE_KEY_SEPARATOR = "\u0000";++interface AutomationSdkVersionCacheEntry {+ expiresAt: number;+ version: string | null;+}++const sdkVersionCache = new Map<string, AutomationSdkVersionCacheEntry>();+const sdkVersionRequests = new Map<string, Promise<string | null>>();++export function __resetAutomationSdkVersionCacheForTests() {+ sdkVersionCache.clear();+ sdkVersionRequests.clear();+}++function readCachedSdkVersion(cacheKey: string): string | null | undefined {+ const cached = sdkVersionCache.get(cacheKey);+ if (!cached) return undefined;++ if (cached.expiresAt <= Date.now()) {+ sdkVersionCache.delete(cacheKey);+ return undefined;+ }++ return cached.version;+}++function writeCachedSdkVersion(cacheKey: string, version: string | null) {+ sdkVersionCache.set(cacheKey, {+ expiresAt: Date.now() + AUTOMATION_SDK_VERSION_CACHE_TIME_MS,+ version,+ });+}++function getCachedOrFetchSdkVersion(cacheKey: string) {+ const cached = readCachedSdkVersion(cacheKey);+ if (cached !== undefined) return Promise.resolve(cached);++ const existingRequest = sdkVersionRequests.get(cacheKey);+ if (existingRequest) return existingRequest;++ if (typeof AutomationService.getSdkVersion !== "function") {+ writeCachedSdkVersion(cacheKey, null);+ return Promise.resolve(null);+ }++ const request = AutomationService.getSdkVersion()+ .then((version) => {+ writeCachedSdkVersion(cacheKey, version);+ return version;+ })+ .catch(() => {+ writeCachedSdkVersion(cacheKey, null);+ return null;+ })+ .finally(() => {+ sdkVersionRequests.delete(cacheKey);+ });+ sdkVersionRequests.set(cacheKey, request);+ return request;+}++export function useAutomationSdkVersion() {+ const active = useActiveBackend();+ const { backend } = active;+ const cacheKey = [+ AUTOMATION_SDK_VERSION_CACHE_NAMESPACE,+ backend.id,+ backend.kind,+ backend.host,+ active.orgId ?? "",+ ].join(CACHE_KEY_SEPARATOR);++ const [automationSdkVersion, setAutomationSdkVersion] = React.useState<+ string | null+ >(() => readCachedSdkVersion(cacheKey) ?? null);++ React.useEffect(() => {+ if (isNoBackend(backend)) {+ setAutomationSdkVersion(null);+ return undefined;+ }++ const cached = readCachedSdkVersion(cacheKey);+ if (cached !== undefined) {+ setAutomationSdkVersion(cached);+ return undefined;+ }++ let isMounted = true;+ setAutomationSdkVersion(null);+ void getCachedOrFetchSdkVersion(cacheKey).then((version) => {+ if (isMounted) {+ setAutomationSdkVersion(version);+ }+ });++ return () => {+ isMounted = false;+ };+ }, [backend, cacheKey]);++ return automationSdkVersion;+}diff --git a/src/hooks/use-tracking.ts b/src/hooks/use-tracking.tsindex cffbbf54ea..16e7fe6095 100644--- a/src/hooks/use-tracking.ts+++ b/src/hooks/use-tracking.ts@@ -5,7 +5,7 @@ import type { WorkspaceMode } from "#/api/conversation-metadata-store";import type { CloudConnectionSource } from "#/services/cloud-funnel-analytics";import { getCachedAgentServerVersion } from "#/api/agent-server-compatibility";import { useActiveBackend } from "#/contexts/active-backend-context";-import AutomationService from "#/api/automation-service/automation-service.api";+import { useAutomationSdkVersion } from "#/hooks/query/use-automation-sdk-version";import { getBackendTelemetryProperties } from "#/services/telemetry-context";import { setTelemetryBackendContext, trackEvent } from "#/services/telemetry";@@ -18,35 +18,8 @@ import { setTelemetryBackendContext, trackEvent } from "#/services/telemetry";* backend settings because they can be stale while a backend changes.*/export const useTracking = () => {- const activeBackend = useActiveBackend();- const { backend } = activeBackend;- const [automationSdkVersion, setAutomationSdkVersion] = React.useState<- string | null- >(null);-- React.useEffect(() => {- if (typeof AutomationService.getSdkVersion !== "function") {- setAutomationSdkVersion(null);- return undefined;- }-- let isMounted = true;- setAutomationSdkVersion(null);- void AutomationService.getSdkVersion().then((version) => {- if (isMounted) {- setAutomationSdkVersion(version);- }- });- return () => {- isMounted = false;- };- }, [- activeBackend.orgId,- backend.apiKey,- backend.host,- backend.id,- backend.kind,- ]);+ const { backend } = useActiveBackend();+ const automationSdkVersion = useAutomationSdkVersion();const getBackendTelemetryContext = React.useCallback(() => ({