From febf2b81a1265632e690dbc1854e1d150a4de8e6 Mon Sep 17 00:00:00 2001 From: Dotta Date: Mon, 23 Nov 2026 10:39:49 -0600 Subject: [PATCH] fix(telemetry): read each Composio app's outcome and keep links from partial checks Greptile review: an agent's COMPOSIO_MANAGE_CONNECTIONS add gave every requested app the same outcome, so an app that was already connected could be counted as needs_auth, and a response with no result for an app as succeeded. Read each app's own result with composioAppSetupResult instead: connected is succeeded, a sign-in link is needs_auth, and an error or a missing result is failed. The Composio list check also emitted connection.app_linked only after the whole batch. A later app that failed in the same check skipped the event for apps already saved as connected, and the next check saw them as already connected. Emit right after each committed row. Tested: connector-telemetry unit tests (mixed per-app outcomes, error and empty responses) and a composio-app-setup case where a later app fails after an earlier app is saved as connected. Co-Authored-By: Claude Opus 5.5 --- .../src/__tests__/composio-app-setup.test.ts | 8 ++++ .../src/services/connector-telemetry.test.ts | 36 +++++++++++++----- server/src/services/connector-telemetry.ts | 38 +++++++++++++------ .../catalog/aggregator-accounts.ts | 7 ++-- server/src/services/tool-gateway.ts | 2 +- 5 files changed, 64 insertions(+), 27 deletions(-) diff --git a/server/src/__tests__/composio-app-setup.test.ts b/server/src/__tests__/composio-app-setup.test.ts index 9d67b02c5aa..93e02eaa844 100644 --- a/server/src/__tests__/composio-app-setup.test.ts +++ b/server/src/__tests__/composio-app-setup.test.ts @@ -352,6 +352,14 @@ const support = await getEmbeddedPostgresTestSupport(); expect(appEvents()).toEqual([]); expect(JSON.stringify(track.mock.calls)).not.toContain("never-store-this-token"); }); + it("keeps app_linked for apps saved before a later app in the same check fails", async () => { + const f = await fixture(); + f.activate(); + f.failToolkit("notion"); + track.mockClear(); + await expect(f.access.refreshComposioApps(f.connection.id, ["circleback_mcp", "notion"], f.actor)).rejects.toThrow(); + expect(appEvents()).toEqual([["connection.app_linked", { provider: "composio", app: aggregatorAppTelemetrySlug("composio", "circleback_mcp") }]]); + }); it("reports a failed app request when Composio does not confirm the new account", async () => { const f = await fixture(); f.setMutationStatus(500); diff --git a/server/src/services/connector-telemetry.test.ts b/server/src/services/connector-telemetry.test.ts index 8ce95d4f1f4..a9458f9421a 100644 --- a/server/src/services/connector-telemetry.test.ts +++ b/server/src/services/connector-telemetry.test.ts @@ -425,22 +425,38 @@ describe("aggregator app events", () => { expect(() => emitAggregatorAppsLinked("company-1", "composio", ["github"])).not.toThrow(); }); - it("reports agent Composio add requests and ignores list-only calls and other tools", () => { - const call = { companyId: "company-1", provider: "composio", toolName: "COMPOSIO_MANAGE_CONNECTIONS", origin: "agent" as const, pendingAuthorization: true, failed: false }; - emitAggregatorAppRequestsFromToolCall({ ...call, parameters: { toolkits: [{ name: "github", action: "add" }, { name: "gmail", action: "list" }, { name: "acme-crm", action: "add" }] } }); + it("reports agent Composio add requests with each app's own outcome and ignores other calls", () => { + const response = { jsonrpc: "2.0", id: "1", result: { structuredContent: { data: { results: { + github: { toolkit: "github", status: "ACTIVE" }, + gmail: { redirect_url: "https://connect.composio.dev/link/abc" }, + slack: { toolkit: "slack", status: "INITIATED" }, + } } } } }; + const call = { companyId: "company-1", provider: "composio", toolName: "COMPOSIO_MANAGE_CONNECTIONS", origin: "agent" as const, pendingAuthorization: true, response }; + emitAggregatorAppRequestsFromToolCall({ ...call, parameters: { toolkits: [ + { name: "github", action: "add" }, { name: "gmail", action: "add" }, { name: "notion", action: "list" }, { name: "acme-crm", action: "add" }, + ] } }); + expect(track.mock.calls).toEqual([ + ["connection.app_requested", { provider: "composio", app: "github", origin: "agent", outcome: "succeeded" }], + ["connection.app_requested", { provider: "composio", app: "gmail", origin: "agent", outcome: "needs_auth" }], + ["connection.app_requested", { provider: "composio", app: "other", origin: "agent", outcome: "needs_auth" }], + ]); + track.mockClear(); emitAggregatorAppRequestsFromToolCall({ ...call, parameters: { toolkits: [{ name: "github", action: "list" }] } }); emitAggregatorAppRequestsFromToolCall({ ...call, toolName: "COMPOSIO_MULTI_EXECUTE_TOOL", parameters: { toolkits: [{ name: "github", action: "add" }] } }); emitAggregatorAppRequestsFromToolCall({ ...call, origin: null, parameters: { toolkits: [{ name: "github", action: "add" }] } }); - emitAggregatorAppRequestsFromToolCall({ ...call, pendingAuthorization: false, failed: true, parameters: { toolkits: [{ name: "slack", action: "add" }] } }); - expect(track.mock.calls).toEqual([ - ["connection.app_requested", { provider: "composio", app: "github", origin: "agent", outcome: "needs_auth" }], - ["connection.app_requested", { provider: "composio", app: "other", origin: "agent", outcome: "needs_auth" }], - ["connection.app_requested", { provider: "composio", app: "slack", origin: "agent", outcome: "failed" }], - ]); + expect(track).not.toHaveBeenCalled(); + }); + + it("reports failed when the Composio response is an error or has no result for the app", () => { + const call = { companyId: "company-1", provider: "composio", toolName: "COMPOSIO_MANAGE_CONNECTIONS", origin: "agent" as const, pendingAuthorization: false, parameters: { toolkits: [{ name: "slack", action: "add" }] } }; + emitAggregatorAppRequestsFromToolCall({ ...call, response: { jsonrpc: "2.0", id: "1", error: { code: -32000, message: "upstream failed" } } }); + emitAggregatorAppRequestsFromToolCall({ ...call, response: { jsonrpc: "2.0", id: "1", result: { isError: true, content: [] } } }); + emitAggregatorAppRequestsFromToolCall({ ...call, response: { jsonrpc: "2.0", id: "1", result: { structuredContent: { data: { results: {} } } } } }); + expect(track.mock.calls.map(([, dims]) => dims.outcome)).toEqual(["failed", "failed", "failed"]); }); it("reports an Arcade tool call that asks for authorization as a request for its toolkit", () => { - const call = { companyId: "company-1", provider: "arcade", parameters: {}, origin: "agent" as const, failed: true }; + const call = { companyId: "company-1", provider: "arcade", parameters: {}, response: {}, origin: "agent" as const }; emitAggregatorAppRequestsFromToolCall({ ...call, toolName: "Gmail_SendEmail", pendingAuthorization: true }); emitAggregatorAppRequestsFromToolCall({ ...call, toolName: "Gmail_SendEmail", pendingAuthorization: false }); expect(track.mock.calls).toEqual([ diff --git a/server/src/services/connector-telemetry.ts b/server/src/services/connector-telemetry.ts index 063c12e21ab..947b74947c7 100644 --- a/server/src/services/connector-telemetry.ts +++ b/server/src/services/connector-telemetry.ts @@ -12,6 +12,7 @@ import { trackConnectionAppRequested, } from "@paperclipai/shared/telemetry"; import { remoteEndpointValue } from "./tool-access/connection-methods.js"; +import { composioAppSetupResult } from "./composio-app-setup.js"; import { logger } from "../middleware/logger.js"; import { getTelemetryClient } from "../telemetry.js"; import { withTelemetryCompany } from "../telemetry-company-scope.js"; @@ -413,39 +414,52 @@ function addedComposioToolkits(parameters: unknown): string[] { }); } +/** Outcome of one toolkit in a `COMPOSIO_MANAGE_CONNECTIONS` add response, read from that toolkit's own result. */ +function composioAddOutcome(response: unknown, toolkit: string): AggregatorAppRequestOutcome { + try { + const result = composioAppSetupResult(response, toolkit); + return result.status === "connected" ? "succeeded" : result.status === "authorization_required" ? "needs_auth" : "failed"; + } catch { + // A provider or protocol error in the response. + return "failed"; + } +} + /** * Agent-side app requests seen by the tool gateway: a Composio * `COMPOSIO_MANAGE_CONNECTIONS` call whose arguments add one or more - * toolkits, or an Arcade tool call that answered with an authorization step - * (the agent tried to use an app its person has not connected yet; the - * toolkit is the tool name's prefix, e.g. `Gmail` from `Gmail_SendEmail`). - * Other tools and providers return without emitting. Never throws. + * toolkits, each with the outcome read from its own result in the response, + * or an Arcade tool call that answered with an authorization step (the agent + * tried to use an app its person has not connected yet; the toolkit is the + * tool name's prefix, e.g. `Gmail` from `Gmail_SendEmail`). Other tools and + * providers return without emitting. Never throws. */ export function emitAggregatorAppRequestsFromToolCall(input: { companyId: string; provider: unknown; toolName: string; parameters: unknown; + response: unknown; origin: AggregatorAppRequestOrigin | null; pendingAuthorization: boolean; - failed: boolean; }): void { try { if (!input.origin) return; - const outcome: AggregatorAppRequestOutcome = input.pendingAuthorization - ? "needs_auth" - : input.failed ? "failed" : "succeeded"; if (input.provider === "composio" && input.toolName === "COMPOSIO_MANAGE_CONNECTIONS") { - const toolkits = addedComposioToolkits(input.parameters); - if (toolkits.length) { - emitAggregatorAppsRequested(input.companyId, { provider: "composio", toolkits, origin: input.origin, outcome }); + for (const toolkit of new Set(addedComposioToolkits(input.parameters))) { + emitAggregatorAppsRequested(input.companyId, { + provider: "composio", + toolkits: [toolkit], + origin: input.origin, + outcome: composioAddOutcome(input.response, toolkit), + }); } return; } if (input.provider === "arcade" && input.pendingAuthorization) { const toolkit = input.toolName.split(/[._]/)[0]; if (toolkit) { - emitAggregatorAppsRequested(input.companyId, { provider: "arcade", toolkits: [toolkit], origin: input.origin, outcome }); + emitAggregatorAppsRequested(input.companyId, { provider: "arcade", toolkits: [toolkit], origin: input.origin, outcome: "needs_auth" }); } } } catch (err) { diff --git a/server/src/services/tool-access/catalog/aggregator-accounts.ts b/server/src/services/tool-access/catalog/aggregator-accounts.ts index fd09fc5210e..05dcaa43bc5 100644 --- a/server/src/services/tool-access/catalog/aggregator-accounts.ts +++ b/server/src/services/tool-access/catalog/aggregator-accounts.ts @@ -156,7 +156,6 @@ export function createAggregatorAccounts(db: Db, { options, secrets, audit, cred const failedToolkits: string[] = []; const listed = toolkits.filter(tool => tool.action === "list").map(tool => tool.name); const previous = await snapshotStatuses(connection.companyId, connectionId, aggregatorUserId(actor), listed); - const committed: { toolkit: string; status: string }[] = []; for (const tool of toolkits.filter(tool => tool.action === "list")) { let accounts: ComposioAppAccount[]; try { @@ -180,10 +179,10 @@ export function createAggregatorAccounts(db: Db, { options, secrets, audit, cred }).onConflictDoUpdate({ target: [toolConnectionAppSnapshots.companyId, toolConnectionAppSnapshots.connectionId, toolConnectionAppSnapshots.userId, toolConnectionAppSnapshots.toolkit], set: { accounts, status, checkedAt, credentialKey, errorAt: null }, setWhere: lte(toolConnectionAppSnapshots.checkedAt, checkedAt) }) .returning({ toolkit: toolConnectionAppSnapshots.toolkit }); - // A newer observation already won the row; this write changed nothing. - if (written.length) committed.push({ toolkit: tool.name, status }); + // Emit per committed row: a later toolkit in this batch may still throw. + // A write that lost to a newer observation changed nothing and emits nothing. + if (written.length) emitAggregatorAppsLinked(connection.companyId, "composio", newlyLinkedToolkits(previous, [{ toolkit: tool.name, status }])); } - emitAggregatorAppsLinked(connection.companyId, "composio", newlyLinkedToolkits(previous, committed)); return { payload, failedToolkits }; } catch (error) { await markFailed(); throw error; } } diff --git a/server/src/services/tool-gateway.ts b/server/src/services/tool-gateway.ts index 6b64c082a8a..89c40afd7fc 100644 --- a/server/src/services/tool-gateway.ts +++ b/server/src/services/tool-gateway.ts @@ -8789,9 +8789,9 @@ export function createToolGatewayService( provider: connection.config.sourceTemplateKey, toolName: entry.toolName, parameters, + response: payloadRecord, origin: session.agentId ? "agent" : session.actorType === "user" ? "user" : null, pendingAuthorization: upstreamPending?.kind === "authorization", - failed: payloadRecord.error !== undefined || asRecord(payloadRecord.result)?.isError === true, }); if (upstreamPending) { await retainUpstreamHandoff(invocationId, upstreamPending);