Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: correlate local conversation telemetry with Canvas identity (#16796)

Graham Neubig35331e795bae
diff --git a/__tests__/api/agent-server-conversation-service.test.ts b/__tests__/api/agent-server-conversation-service.test.ts
index 413f90731c..b75a6b5e84 100644
--- a/__tests__/api/agent-server-conversation-service.test.ts
+++ b/__tests__/api/agent-server-conversation-service.test.ts
@@ -32,6 +32,7 @@ const {
mockGetProfile,
mockActivateProfile,
mockListProfiles,
+ mockGetTelemetryDistinctId,
} = vi.hoisted(() => ({
mockHttpGet: vi.fn(),
mockHttpPost: vi.fn(),
@@ -46,6 +47,7 @@ const {
mockGetProfile: vi.fn(),
mockActivateProfile: vi.fn(),
mockListProfiles: vi.fn(),
+ mockGetTelemetryDistinctId: vi.fn(),
}));
const originalFetch = global.fetch;
@@ -100,6 +102,10 @@ vi.mock("#/api/settings-service/settings-service.api", () => ({
},
}));
+vi.mock("#/services/telemetry", () => ({
+ getTelemetryDistinctId: mockGetTelemetryDistinctId,
+}));
+
describe("AgentServerConversationService", () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -250,6 +256,58 @@ describe("AgentServerConversationService", () => {
});
describe("createConversation", () => {
+ it("forwards the Canvas telemetry identity to the local agent server", async () => {
+ mockGetTelemetryDistinctId.mockResolvedValue("ph-canvas-user");
+ mockGetSettings.mockResolvedValue({
+ agent_settings: { llm: { model: "gpt-4o" } },
+ conversation_settings: {},
+ });
+ mockGetSettingsForConversation.mockResolvedValue({
+ agentSettings: { llm: { model: "gpt-4o" } },
+ conversationSettings: {},
+ secretsEncrypted: true,
+ });
+ mockHttpPost.mockResolvedValue({
+ data: {
+ id: "conversation-1",
+ created_at: "2024-01-01",
+ updated_at: "2024-01-01",
+ },
+ });
+
+ await AgentServerConversationService.createConversation();
+
+ expect(mockHttpPost).toHaveBeenCalledWith(
+ "/api/conversations",
+ expect.objectContaining({ user_id: "ph-canvas-user" }),
+ );
+ });
+
+ it("omits user_id when Canvas telemetry has no consented identity", async () => {
+ mockGetTelemetryDistinctId.mockResolvedValue(null);
+ mockGetSettings.mockResolvedValue({
+ agent_settings: { llm: { model: "gpt-4o" } },
+ conversation_settings: {},
+ });
+ mockGetSettingsForConversation.mockResolvedValue({
+ agentSettings: { llm: { model: "gpt-4o" } },
+ conversationSettings: {},
+ secretsEncrypted: true,
+ });
+ mockHttpPost.mockResolvedValue({
+ data: {
+ id: "conversation-1",
+ created_at: "2024-01-01",
+ updated_at: "2024-01-01",
+ },
+ });
+
+ await AgentServerConversationService.createConversation();
+
+ const payload = mockHttpPost.mock.calls[0][1] as Record<string, unknown>;
+ expect(payload).not.toHaveProperty("user_id");
+ });
+
it("passes the selected title profile to local conversation starts", async () => {
mockGetSettings.mockResolvedValue({
title_llm_profile: "Titles",
diff --git a/__tests__/api/use-create-conversation-metadata.test.ts b/__tests__/api/use-create-conversation-metadata.test.ts
index 535af93fcb..8251c278fb 100644
--- a/__tests__/api/use-create-conversation-metadata.test.ts
+++ b/__tests__/api/use-create-conversation-metadata.test.ts
@@ -17,6 +17,7 @@ const {
mockGetSettingsForConversation,
mockUseLlmProfiles,
mockListInstalledPlugins,
+ mockGetTelemetryDistinctId,
} = vi.hoisted(() => ({
mockHttpPost: vi.fn(),
mockConversationClient: vi.fn(),
@@ -25,6 +26,7 @@ const {
mockGetSettingsForConversation: vi.fn(),
mockUseLlmProfiles: vi.fn(),
mockListInstalledPlugins: vi.fn(),
+ mockGetTelemetryDistinctId: vi.fn(),
}));
vi.mock("@openhands/typescript-client/clients", async () => {
@@ -70,6 +72,10 @@ vi.mock("#/hooks/use-tracking", () => ({
useTracking: () => ({ trackConversationCreated: vi.fn() }),
}));
+vi.mock("#/services/telemetry", () => ({
+ getTelemetryDistinctId: mockGetTelemetryDistinctId,
+}));
+
vi.mock("#/hooks/query/use-llm-profiles", () => ({
useLlmProfiles: () => mockUseLlmProfiles(),
}));
@@ -98,6 +104,7 @@ describe("useCreateConversation persists selected repository metadata", () => {
// Default: no installed plugins, so the existing repo/workspace/profile
// assertions are unaffected. Plugin-specific tests override this.
mockListInstalledPlugins.mockResolvedValue([]);
+ mockGetTelemetryDistinctId.mockReset().mockResolvedValue(null);
mockHttpPost.mockReset();
mockGetSettings.mockReset();
mockGetSettingsForConversation.mockReset();
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 ce48de7740..bb08d26fb4 100644
--- a/src/api/conversation-service/agent-server-conversation-service.api.ts
+++ b/src/api/conversation-service/agent-server-conversation-service.api.ts
@@ -50,6 +50,7 @@ import {
NoBackendAvailableError,
} from "../agent-server-client-options";
import SettingsService from "../settings-service/settings-service.api";
+import { getTelemetryDistinctId } from "../../services/telemetry";
import {
ConversationMetadata,
getStoredConversationMetadata,
@@ -486,9 +487,13 @@ class AgentServerConversationService {
titleLlmProfile,
});
+ const telemetryDistinctId = await getTelemetryDistinctId();
const data = await new ConversationClient(
getAgentServerClientOptions({ timeout: CREATE_CONVERSATION_TIMEOUT_MS }),
- ).createConversation<DirectConversationInfo>(payload);
+ ).createConversation<DirectConversationInfo>({
+ ...payload,
+ ...(telemetryDistinctId ? { user_id: telemetryDistinctId } : {}),
+ });
const localBackend = getEffectiveLocalBackend();
if (!localBackend) throw new NoBackendAvailableError();