diff --git a/server/src/__tests__/satisfied-connection-intents.test.ts b/server/src/__tests__/satisfied-connection-intents.test.ts index d6698cba8b6..61e36fc9d80 100644 --- a/server/src/__tests__/satisfied-connection-intents.test.ts +++ b/server/src/__tests__/satisfied-connection-intents.test.ts @@ -434,6 +434,31 @@ describePostgres("already available connection requests", () => { expect(wakeup).toHaveBeenCalledWith(f.agentId, expect.objectContaining({ requestedByActorType: "system", idempotencyKey: `connection-intent:${f.intent.id}:expired`, contextSnapshot: expect.objectContaining({ refreshTools: true, responsibleUserId: "connection-owner" }) })); }); + it.each(["blocked", "todo", "backlog"] as const)("retires a satisfied card on a %s task without waking its agent", async (status) => { + const f = await seed(); const wakeup = vi.fn().mockResolvedValue(null); + await db.update(issues).set({ status }).where(eq(issues.id, f.issueId)); + const worker = connectionIntentDeliveryService(db, { wakeup }); + await worker.sweepPending(); + expect((await db.select().from(issueThreadInteractions))[0]).toMatchObject({ status: "expired", resolvedByUserId: null, + result: { outcome: "expired", connectionId: f.connection.id } }); + expect(await db.select().from(connectionIntentDeliveries)).toHaveLength(0); + expect(wakeup).not.toHaveBeenCalled(); + expect((await db.select().from(issues))[0].status).toBe(status); + expect(await db.select().from(activityLog)).toEqual([expect.objectContaining({ actorType: "system", action: "issue.thread_interaction_resolved" })]); + expect(await worker.hasPending()).toBe(false); + }); + + it("keeps a blocked task's card pending until its connection is usable", async () => { + const f = await seed(); + await db.update(issues).set({ status: "blocked" }).where(eq(issues.id, f.issueId)); + await db.update(connectionGrants).set({ status: "revoked" }); + expect(await satisfiedConnectionIntentService(db).sweepPending()).toMatchObject({ satisfied: 0 }); + expect((await db.select().from(issueThreadInteractions))[0].status).toBe("pending"); + await db.update(connectionGrants).set({ status: "active" }); + expect(await satisfiedConnectionIntentService(db).sweepPending()).toMatchObject({ satisfied: 1 }); + expect(await db.select().from(connectionIntentDeliveries)).toHaveLength(0); + }); + it("rechecks access after a restart before it delivers the saved continuation", async () => { const f = await seed(); await satisfiedConnectionIntentService(db).sweepPending(); diff --git a/server/src/services/satisfied-connection-intents.ts b/server/src/services/satisfied-connection-intents.ts index 5637ffbe5c4..6dd12964693 100644 --- a/server/src/services/satisfied-connection-intents.ts +++ b/server/src/services/satisfied-connection-intents.ts @@ -3,7 +3,7 @@ import { notifyChatConfirmationResolution } from "./chat-confirmation-notificati import { enqueueTerminalIssueInteractionChatPublications } from "./chat-interaction-publications.js"; import { connectionManagementService } from "./connection-management.js"; import { chatCredentialEntryGenerations } from "./chat-channels/credentials.js"; -import { and, asc, eq, gt, inArray, isNull, or } from "drizzle-orm"; +import { and, asc, eq, gt, inArray, isNull, notInArray, or } from "drizzle-orm"; import { approvals, chatEndpoints, chatExternalPrincipals, chatIdentityLinks, toolConnections, companyMemberships, connectionIntentDeliveries, issueApprovals, issueThreadInteractions, issues, type Db } from "@paperclipai/db"; import { connectionIntentPayloadSchema } from "@paperclipai/shared"; import { logActivity, publishActivity, type ActivityPublication } from "./activity-log.js"; @@ -128,7 +128,7 @@ export function satisfiedConnectionIntentService(db: Db) { .from(issueThreadInteractions).innerJoin(issues, and( eq(issues.id, issueThreadInteractions.issueId), eq(issues.companyId, issueThreadInteractions.companyId), )).where(and(eq(issueThreadInteractions.kind, "connection_intent"), eq(issueThreadInteractions.status, "pending"), - inArray(issues.status, ["in_progress", "in_review"]), isNull(issues.assigneeUserId), + notInArray(issues.status, ["done", "cancelled"]), isNull(issues.assigneeUserId), ...(afterId ? [gt(issueThreadInteractions.id, afterId)] : []))) .orderBy(asc(issueThreadInteractions.id)).limit(50); // Rotate past unavailable cards so they cannot starve a later ready one. @@ -154,7 +154,9 @@ export function satisfiedConnectionIntentService(db: Db) { eq(companyMemberships.principalId, intent.addresseeUserId), )).for("share"); } - const connection = await findSatisfiedToolConnection(txDb, task, intent); + const continues = ["in_progress", "in_review"].includes(task.status); + const connection = continues ? await findSatisfiedToolConnection(txDb, task, intent) + : ["done", "cancelled"].includes(task.status) ? null : await findAvailableToolConnection(txDb, task, intent); if (!connection) return false; await notifyIssueReviewState(tx, candidate); const now = new Date(); @@ -169,8 +171,10 @@ export function satisfiedConnectionIntentService(db: Db) { const interaction = await issueThreadInteractionService(txDb).getById(retired.id); if (interaction) await enqueueTerminalIssueInteractionChatPublications(txDb, interaction); await tx.update(issues).set({ updatedAt: now }).where(and(eq(issues.id, task.id), eq(issues.companyId, task.companyId))); - await tx.insert(connectionIntentDeliveries).values({ interactionId: intent.id, companyId: task.companyId }).onConflictDoNothing(); - await notifyDeliveryWork(tx, DELIVERY_QUEUES.connection); + if (continues) { + await tx.insert(connectionIntentDeliveries).values({ interactionId: intent.id, companyId: task.companyId }).onConflictDoNothing(); + await notifyDeliveryWork(tx, DELIVERY_QUEUES.connection); + } await logActivity(txDb, { companyId: task.companyId, actorType: "system", actorId: "connection-reconciliation", action: "issue.thread_interaction_resolved", entityType: "issue", entityId: task.id, details: { interactionId: intent.id, interactionKind: "connection_intent", status: "expired",