diff --git a/doc/run-log-events.md b/doc/run-log-events.md index 0c80422eff0..5615b6851a4 100644 --- a/doc/run-log-events.md +++ b/doc/run-log-events.md @@ -1017,7 +1017,11 @@ wake then neither reopens the finished task nor starts another turn. An edit after the read, or a read by a run that did not succeed, leaves the comment owed, so it is delivered once. The check reads only the assignee's succeeded runs of that task that finished at or after the comment's current revision, -through the existing run-binding and `(company_id, run_id)` event indexes. A failure to write the event leaves the comment +through the existing run-binding and `(company_id, run_id)` event indexes. +A saved follow-up resumed after a remote Stop uses the same rule. A read by a +run that did not succeed never counts. `taskHistoryReadsForRun` lists the +comments one run read, with each read's time and the comment revision it saw, +whatever the run's outcome. A failure to write the event leaves the comment owed in the same way. ## Managed Agent File Save Receipts diff --git a/server/src/services/explicit-native-continuation.ts b/server/src/services/explicit-native-continuation.ts index c02780a1b6c..f3f4bc110f8 100644 --- a/server/src/services/explicit-native-continuation.ts +++ b/server/src/services/explicit-native-continuation.ts @@ -35,7 +35,7 @@ import { activeIssueInteractionCondition } from "./issue-question-context.js"; import { historicalAdapterType, isConversationAdapter } from "./conversation-continuation.js"; import { queuedCommentIdsFromWakePayload } from "./issue-queued-comment-queue.js"; -import { firstTurnDeliveredCommentIds } from "./queued-comment-delivery.js"; +import { firstTurnDeliveredCommentIds, historyReadDeliveredCommentIds } from "./queued-comment-delivery.js"; import { canResumePreservedResultFinalization } from "./preserved-result-finalization.js"; import { isServerReplacementInterruption } from "./server-replacement-interruption.js"; import { normalizeAgentNameKey } from "./agent-name-key.js"; @@ -129,7 +129,9 @@ export async function undeliveredLegacyUserCommentIds( } // An accepted first turn that took a queued message consumed it too. const firstTurn = await firstTurnDeliveredCommentIds(db, { companyId, issueId, agentId, commentIds: [...valid] }); - return commentIds.filter(id => valid.has(id) && !firstTurn.has(id)); + const historyRead = await historyReadDeliveredCommentIds(db, { companyId, issueId, agentId, + commentIds: [...valid].filter(id => !firstTurn.has(id)) }); + return commentIds.filter(id => valid.has(id) && !firstTurn.has(id) && !historyRead.has(id)); } /** Called under the issue lock, in the transaction that creates the new turn. diff --git a/server/src/services/queued-comment-delivery.ts b/server/src/services/queued-comment-delivery.ts index 69d97ee4d41..98664b6ef78 100644 --- a/server/src/services/queued-comment-delivery.ts +++ b/server/src/services/queued-comment-delivery.ts @@ -59,6 +59,35 @@ export async function recordTaskHistoryRead(db: Db, input: { payload: { comments: Object.fromEntries(input.comments.map(comment => [comment.id, comment.updatedAt.toISOString()])) } }); } +export async function taskHistoryReadsForRun(db: Pick, input: { companyId: string; runId: string }): Promise> { + const rows = await db.select({ readAt: heartbeatRunEvents.createdAt, payload: heartbeatRunEvents.payload }) + .from(heartbeatRunEvents).where(and(eq(heartbeatRunEvents.companyId, input.companyId), + eq(heartbeatRunEvents.runId, input.runId), eq(heartbeatRunEvents.eventType, TASK_HISTORY_READ_EVENT))) + .orderBy(heartbeatRunEvents.seq); + const latest = new Map(); + for (const row of rows) { + const comments = row.payload?.comments; + if (!comments || typeof comments !== "object" || Array.isArray(comments)) continue; + for (const [commentId, value] of Object.entries(comments as Record)) { + const revision = typeof value === "string" ? new Date(value) : null; + if (revision && !Number.isNaN(revision.getTime())) latest.set(commentId, { commentId, readAt: row.readAt, revision }); + } + } + return [...latest.values()]; +} + +export async function historyReadDeliveredCommentIds(tx: Pick, input: { + companyId: string; issueId: string; agentId: string; commentIds: string[]; +}): Promise> { + if (input.commentIds.length === 0) return new Set(); + const rows = await tx.select({ id: issueComments.id }).from(issueComments).where(and( + eq(issueComments.companyId, input.companyId), eq(issueComments.issueId, input.issueId), + inArray(issueComments.id, input.commentIds), historyReadDeliveredSql(input.companyId, input.issueId, input.agentId))); + return new Set(rows.map(row => row.id)); +} + function historyReadDeliveredSql(companyId: string, issueId: string, agentId: string) { const readBy = (binding: SQL) => sql`exists (select 1 from ${heartbeatRuns} reading_run cross join lateral (select 1 from ${heartbeatRunEvents} history_read diff --git a/server/src/services/queued-comment-history-read.test.ts b/server/src/services/queued-comment-history-read.test.ts new file mode 100644 index 00000000000..8f04500a9bc --- /dev/null +++ b/server/src/services/queued-comment-history-read.test.ts @@ -0,0 +1,73 @@ +import { randomUUID } from "node:crypto"; +import { eq } from "drizzle-orm"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { agents, companies, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, heartbeatRuns, issueComments, issues } from "@paperclipai/db"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../__tests__/helpers/embedded-postgres.js"; +import { undeliveredLegacyUserCommentIds } from "./explicit-native-continuation.js"; +import { PaperclipRunnerToolAuthority } from "./native-runtime/paperclip-runner-tool-authority.js"; +import { taskHistoryReadsForRun } from "./queued-comment-delivery.js"; + +const support = await getEmbeddedPostgresTestSupport(); +const USER = "history-read-user"; + +describe.skipIf(!support.supported)("task history reads", () => { + let database: Awaited>; + let db: ReturnType; + beforeAll(async () => { + database = await startEmbeddedPostgresTestDatabase("paperclip-history-read-"); + db = createDb(database.connectionString); + }, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS); + afterAll(async () => { await db?.$client.end(); await database?.cleanup(); }); + + async function seed() { + const [company] = await db.insert(companies).values({ name: "History read", issuePrefix: `H${randomUUID().slice(0, 4)}` }).returning(); + const [agent] = await db.insert(agents).values({ companyId: company!.id, name: "Codie", adapterType: "paperclip_runner" }).returning(); + const issueId = randomUUID(); + const [run] = await db.insert(heartbeatRuns).values({ companyId: company!.id, agentId: agent!.id, status: "running", + runtimeMode: "native", runtimeModeResolvedAt: new Date(), nativeIssueId: issueId, + startedAt: new Date(Date.now() - 60_000), contextSnapshot: { issueId } }).returning(); + await db.insert(issues).values({ id: issueId, companyId: company!.id, title: "Fix the reports", status: "in_progress", + assigneeAgentId: agent!.id, executionRunId: run!.id }); + return { companyId: company!.id, agentId: agent!.id, issueId, runId: run!.id }; + } + + const comment = async (f: Awaited>, body: string) => (await db.insert(issueComments).values({ + companyId: f.companyId, issueId: f.issueId, authorType: "user", authorUserId: USER, body }).returning())[0]!; + + async function readHistory(f: Awaited>) { + const reader = new PaperclipRunnerToolAuthority(db, { companyId: f.companyId, agentId: f.agentId, issueId: f.issueId, runId: f.runId }); + try { await reader.execute({ tool: "get_task_history", callId: randomUUID(), arguments: {} }); } finally { reader.close(); } + } + + const finish = (f: { runId: string }, status: "succeeded" | "failed") => db.update(heartbeatRuns) + .set({ status, finishedAt: new Date(Date.now() + 1_000) }).where(eq(heartbeatRuns.id, f.runId)); + + it("lists the comments a failed run read, with the read time and the revision it saw", async () => { + const f = await seed(); + const first = await comment(f, "Also include the FOLLOWUP line."); + const second = await comment(f, "And keep the ledger sorted."); + const before = new Date(); + await readHistory(f); + await finish(f, "failed"); + const reads = await taskHistoryReadsForRun(db, { companyId: f.companyId, runId: f.runId }); + expect(reads.map(read => read.commentId).sort()).toEqual([first.id, second.id].sort()); + for (const read of reads) { + const seen = read.commentId === first.id ? first : second; + expect(read.revision.toISOString()).toBe(seen.updatedAt.toISOString()); + expect(read.readAt.getTime()).toBeGreaterThanOrEqual(before.getTime() - 1_000); + } + expect(await taskHistoryReadsForRun(db, { companyId: randomUUID(), runId: f.runId })).toEqual([]); + }); + + it.each([ + { status: "succeeded" as const, owed: false }, + { status: "failed" as const, owed: true }, + ])("treats a remote-stop follow-up that a $status run read mid-turn as owed: $owed", async ({ status, owed }) => { + const f = await seed(); + const followUp = await comment(f, "Additional requirement: include the FOLLOWUP line."); + await readHistory(f); + await finish(f, status); + expect(await undeliveredLegacyUserCommentIds(db, f.companyId, f.issueId, f.agentId, [followUp.id])) + .toEqual(owed ? [followUp.id] : []); + }); +});