diff --git a/server/src/__tests__/connection-intents-service.test.ts b/server/src/__tests__/connection-intents-service.test.ts index 14ff2f05d64..22a67d78a28 100644 --- a/server/src/__tests__/connection-intents-service.test.ts +++ b/server/src/__tests__/connection-intents-service.test.ts @@ -1183,6 +1183,38 @@ describeEmbeddedPostgres("connectionIntentService", () => { responsibleUserId: "grant-user", serviceSlug: "notion" })).resolves.toMatchObject({ id: f.connection.id }); }); + it("names a shared connection whose sign-in must be renewed instead of a generic attention reason", async () => { + const f = await accessFixture("sentry"); + const [profile] = await db.insert(toolProfiles).values({ companyId: f.company.id, profileKey: randomUUID(), name: "Sentry", defaultAction: "allow", status: "active" }).returning(); + await db.insert(toolProfileBindings).values({ companyId: f.company.id, profileId: profile.id, targetType: "agent", targetId: f.agent.id }); + const sentry = async () => (await f.service.search(f.claims, "sentry")).results.find(item => item.service === "sentry"); + expect(await sentry()).toMatchObject({ state: "ready", connectionId: f.connection.id }); + // The rows a rejected refresh writes for an organization grant: the grant + // needs reauthorization and the connection is disabled. lastError is unchanged. + await db.update(connectionGrants).set({ status: "needs_reauthorization" }) + .where(eq(connectionGrants.connectionId, f.connection.id)); + await db.update(toolConnections).set({ authKind: "oauth", status: "draft", enabled: false }) + .where(eq(toolConnections.id, f.connection.id)); + const result = await sentry(); + expect(result).toMatchObject({ state: "needs_user_action", connectionId: null }); + expect(result?.reason).toContain("Saved sentry"); + expect(result?.reason).toMatch(/sign in again|signed in again/i); + expect(result?.reason).toMatch(/reconnect/i); + }); + + it("does not name another person's private connection that must be renewed", async () => { + const f = await accessFixture("sentry"); + await db.update(connectionGrants).set({ kind: "user", subjectUserId: "other-user", status: "needs_reauthorization" }) + .where(eq(connectionGrants.connectionId, f.connection.id)); + await db.delete(toolConnectionInstalls).where(eq(toolConnectionInstalls.connectionId, f.connection.id)); + await db.update(toolConnections).set({ + name: "Private Sentry of another person", authKind: "oauth", credentialPolicy: "per_user", status: "draft", enabled: false, + }).where(eq(toolConnections.id, f.connection.id)); + const result = (await f.service.search(f.claims, "sentry")).results.find(item => item.service === "sentry"); + expect(result).toBeDefined(); + expect(result?.reason).not.toContain("Private Sentry of another person"); + }); + it("creates a scoped access card and atomically accepts repeated clicks without granting other agents", async () => { const f = await accessFixture(); const request = await f.service.request(f.claims, "composio", { connectionId: f.connection.id, toolNames: ["COMPOSIO_SEARCH_TOOLS", "COMPOSIO_MANAGE_CONNECTIONS"] }); diff --git a/server/src/services/connection-intents.ts b/server/src/services/connection-intents.ts index f2ad49f8732..ad59e88414b 100644 --- a/server/src/services/connection-intents.ts +++ b/server/src/services/connection-intents.ts @@ -540,12 +540,16 @@ export function connectionIntentService(db: Db) { && (aiOnly ? connection.connectionPurpose === "ai" : connection.connectionPurpose !== "ai")); // Indexed descriptions can contain private workspace metadata, including // for catalog providers. Check each configured connection's audience first. + const reauthorizationRequired: ToolConnection[] = []; const catalogs = await Promise.all(matching.map(async (connection) => { const { grants } = await access.listConnectionGrants(connection.id, run.companyId); - const authorized = grants.some((grant) => grant.status === "active" && ( + const audience = grants.filter((grant) => grant.kind === "organization" || (grant.kind === "user" && grant.subjectUserId === run.responsibleUserId) - || (grant.kind === "agent" && grant.subjectAgentId === agent.id) - )); + || (grant.kind === "agent" && grant.subjectAgentId === agent.id)); + const authorized = audience.some((grant) => grant.status === "active"); + // A rejected refresh marks the grant. Name only connections in this + // caller's audience so the agent can report the exact human action. + if (!authorized && audience.some((grant) => grant.status === "needs_reauthorization")) reauthorizationRequired.push(connection); const entries = authorized ? await indexedCatalog(connection.id, run.companyId) : []; authorizedCatalogs.set(connection.id, entries); return entries; @@ -581,6 +585,7 @@ export function connectionIntentService(db: Db) { state: ready ? "ready" : (denied && !isRemoteMcpConnectorId(service)) || !app.available || !app.methods.length ? "unavailable" : matching.length ? "needs_user_action" : "available", reason: ready ? "Connection is installed and usable by this agent" : denied ? "Ask the responsible user to grant this agent access with connection_request" : !app.available ? "Connection is disabled or unavailable" + : reauthorizationRequired.length ? `"${reauthorizationRequired[0]!.name}" must be signed in again. A person who manages this connection must reconnect it on its app page` : matching.some((connection) => isToolConnectionAttentionHealth(connection.healthStatus)) ? "Connection needs attention" : matching.length ? "Review identity and access for this agent" : "Connect this service to continue", connectionId: ready?.id ?? null,