diff --git a/server/src/__tests__/chat-run-milestone-candidates.test.ts b/server/src/__tests__/chat-run-milestone-candidates.test.ts index b50ebc0230d..116e4cbf8fd 100644 --- a/server/src/__tests__/chat-run-milestone-candidates.test.ts +++ b/server/src/__tests__/chat-run-milestone-candidates.test.ts @@ -2,12 +2,13 @@ import { randomUUID } from "node:crypto"; import { and, asc, gte, sql } from "drizzle-orm"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { - agents, chatConversations, chatEndpoints, companies, createDb, heartbeatRuns, + agents, chatConversations, chatEndpoints, chatPublications, companies, createDb, heartbeatRuns, issues, toolApplications, toolConnections, } from "@paperclipai/db"; import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; import { chatRunMilestoneSource } from "../services/chat-run-milestone-candidates.js"; import { boundRunIssueIdSql } from "../services/run-task-binding.js"; +import { enqueueChatRunMilestones } from "../services/chat-run-publications.js"; const support = await getEmbeddedPostgresTestSupport(); (support.supported ? describe : describe.skip)("chat milestone candidate prefilter", () => { @@ -22,6 +23,39 @@ const support = await getEmbeddedPostgresTestSupport(); await database?.cleanup(); }); + it("does not fetch run result TOAST blocks for already published milestones", async () => { + const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID(); + const applicationId = randomUUID(), connectionId = randomUUID(), endpointId = randomUUID(); + await db.insert(companies).values({ id: companyId, name: "Settled milestones", issuePrefix: "SETT" }); + await db.insert(agents).values({ id: agentId, companyId, name: "Chat agent" }); + await db.insert(issues).values({ id: issueId, companyId, title: "Chat task" }); + await db.insert(toolApplications).values({ id: applicationId, companyId, name: "Slack", type: "chat" }); + await db.insert(toolConnections).values({ id: connectionId, companyId, applicationId, name: "Slack", uid: connectionId, transport: "chat_sdk", connectionPurpose: "channel" }); + await db.insert(chatEndpoints).values({ id: endpointId, companyId, connectionId, provider: "slack", publicId: endpointId, assignedAgentId: agentId }); + const [conversation] = await db.insert(chatConversations).values({ companyId, endpointId, issueId, externalConversationId: "settled", externalLabel: "Settled", state: "active" }).returning(); + const runIds = Array.from({ length: 6 }, () => randomUUID()); + for (const runId of runIds) { + await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, status: "succeeded", contextSnapshot: { issueId, source: "chat:slack" }, resultJson: { presentationDecision: {}, output: Array.from({ length: 4000 }, () => randomUUID()).join("") } }); + await db.insert(chatPublications).values({ companyId, endpointId, conversationId: conversation.id, issueId, idempotencyKey: `run:${runId}:completed:${endpointId}`, state: "published", payload: { text: "Completed", progressState: "completed" } }); + } + try { + await db.transaction(async (tx) => { + const fetched = async () => { + const rows = await tx.execute(sql`select pg_stat_get_xact_blocks_fetched(reltoastrelid)::int as blocks from pg_class where oid = 'heartbeat_runs'::regclass`); + return Number(rows[0].blocks); + }; + const before = await fetched(); + expect(await enqueueChatRunMilestones(tx as unknown as typeof db)).toBe(0); + const after = await fetched(); + await tx.execute(sql`select result_json -> 'presentationDecision' from heartbeat_runs where id = ${runIds[0]}`); + expect(await fetched()).toBeGreaterThan(after); + expect(after).toBe(before); + }); + } finally { + await db.update(chatConversations).set({ state: "completed" }).where(sql`${chatConversations.id} = ${conversation.id}`); + } + }); + it("preserves complete rows, binding precedence, company scope and the recovery window", async () => { const companyId = randomUUID(), foreignCompanyId = randomUUID(), agentId = randomUUID(); const issueId = randomUUID(), foreignIssueId = randomUUID(), closedIssueId = randomUUID(); diff --git a/server/src/__tests__/chat-run-milestone-toast.test.ts b/server/src/__tests__/chat-run-milestone-toast.test.ts new file mode 100644 index 00000000000..0c39c090435 --- /dev/null +++ b/server/src/__tests__/chat-run-milestone-toast.test.ts @@ -0,0 +1,74 @@ +import { randomBytes, randomUUID } from "node:crypto"; +import { eq, sql } from "drizzle-orm"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { + agents, chatConversations, chatEndpoints, chatPublications, companies, createDb, + heartbeatRuns, issues, toolApplications, toolConnections, +} from "@paperclipai/db"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; +import { enqueueChatRunMilestones, loadChatRunLifecycleMilestoneScopes } from "../services/chat-run-publications.js"; + +const support = await getEmbeddedPostgresTestSupport(); +(support.supported ? describe : describe.skip)("chat milestone result TOAST reads", () => { + let database: Awaited>; + let db: ReturnType; + beforeAll(async () => { + database = await startEmbeddedPostgresTestDatabase("chat-milestone-toast-"); + db = createDb(database.connectionString); + }, 90_000); + afterAll(async () => { + await db?.$client.end({ timeout: 0 }); + await database?.cleanup(); + }); + + it.each([false, true])("does not read published runs' results (decision present: %s)", async (hasDecision) => { + const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID(); + const applicationId = randomUUID(), connectionId = randomUUID(), endpointId = randomUUID(); + const conversationId = randomUUID(); + await db.insert(companies).values({ id: companyId, name: "Toast", issuePrefix: hasDecision ? "TYES" : "TNO" }); + await db.insert(agents).values({ id: agentId, companyId, name: "Chat agent" }); + await db.insert(issues).values({ id: issueId, companyId, title: "Active chat" }); + await db.insert(toolApplications).values({ id: applicationId, companyId, name: "Slack", type: "chat" }); + await db.insert(toolConnections).values({ id: connectionId, companyId, applicationId, name: "Slack", uid: connectionId, transport: "chat_sdk", connectionPurpose: "channel" }); + await db.insert(chatEndpoints).values({ id: endpointId, companyId, connectionId, provider: "slack", publicId: endpointId, assignedAgentId: agentId, publicationMode: "automatic" }); + await db.insert(chatConversations).values({ id: conversationId, companyId, endpointId, issueId, externalConversationId: conversationId, externalLabel: "Active", state: "active" }); + const runIds = Array.from({ length: 6 }, () => randomUUID()); + for (const runId of runIds) { + await db.insert(heartbeatRuns).values({ + id: runId, companyId, agentId, status: "succeeded", + contextSnapshot: { issueId, source: "chat:slack" }, + resultJson: { + output: randomBytes(256_000).toString("hex"), + ...(hasDecision ? { presentationDecision: { commentId: null } } : {}), + nativeResult: { reportedWorkDisposition: "completed" }, + }, + }); + await db.insert(chatPublications).values({ + companyId, endpointId, conversationId, issueId, state: "published", + idempotencyKey: `run:${runId}:completed:${endpointId}`, + payload: { text: "Completed", progressState: "completed" }, + }); + } + await db.transaction(async (tx) => { + const fetched = async () => { + const [row] = await tx.execute<{ blocks: number }>( + sql`select pg_stat_get_xact_blocks_fetched(reltoastrelid)::int as blocks + from pg_class where oid = 'heartbeat_runs'::regclass`, + ); + return row.blocks; + }; + const before = await fetched(); + expect(await enqueueChatRunMilestones(tx as unknown as typeof db)).toBe(0); + expect(await fetched()).toBe(before); + expect(await loadChatRunLifecycleMilestoneScopes(tx as unknown as typeof db, { companyId })).toEqual([]); + expect(await fetched()).toBe(before); + await tx.execute(sql`select result_json -> 'presentationDecision' + from heartbeat_runs where id = ${runIds[0]}`); + expect(await fetched()).toBeGreaterThan(before); + await tx.delete(chatPublications).where(eq(chatPublications.companyId, companyId)); + const withoutLineage = await fetched(); + expect(await enqueueChatRunMilestones(tx as unknown as typeof db)).toBe(0); + expect(await fetched()).toBe(withoutLineage); + }); + }); +}); diff --git a/server/src/services/chat-run-publications.ts b/server/src/services/chat-run-publications.ts index 84ea1783dd7..9dfae17c724 100644 --- a/server/src/services/chat-run-publications.ts +++ b/server/src/services/chat-run-publications.ts @@ -473,15 +473,15 @@ function lifecycleMilestonePages(db: Db, since: Date, scope?: ChatRunMilestoneSc // Heartbeat marks a run succeeded before the presentation resolver // finishes. Waiting for its durable decision prevents a generic // completion from racing an explicitly authored final comment. - sql`( + sql`case when ${chatPublications.id} is null then ( ${heartbeatRuns.status} <> 'succeeded' or ${heartbeatRuns.resultJson} -> 'presentationDecision' is not null - )`, - sql`(${heartbeatRuns.status} <> 'succeeded' or coalesce( + ) else false end`, + sql`case when ${chatPublications.id} is null then (${heartbeatRuns.status} <> 'succeeded' or coalesce( ${heartbeatRuns.resultJson}->'nativeResult'->>'reportedWorkDisposition', ${heartbeatRuns.resultJson}->'acceptedResult'->>'reportedWorkDisposition', ${heartbeatRuns.resultJson}->'semanticResult'->'result'->>'reportedWorkDisposition', '' - ) <> 'yielded')`, + ) <> 'yielded') else false end`, // A successful or interrupted run's selected final remains its // authoritative response. Filter it before LIMIT so a page of settled // runs cannot starve later milestones.