diff --git a/server/src/__tests__/chat-channels.integration.test.ts b/server/src/__tests__/chat-channels.integration.test.ts index fa0fea2c7f6..ff6ce27cc87 100644 --- a/server/src/__tests__/chat-channels.integration.test.ts +++ b/server/src/__tests__/chat-channels.integration.test.ts @@ -82,6 +82,7 @@ import { githubChatReviewService } from "../services/chat-github-reviews.js"; import { resolveGitHubReviewInstructions } from "../services/chat-channels/github/review-instructions.js"; import { githubResponseCommentService } from "../services/chat-github-response-comments.js"; import { GITHUB_REVIEW_CHECK_MAX_HEAD_POLL_MS, githubReviewCheckService } from "../services/chat-github-checks.js"; +import { GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS } from "../services/chat-channels/github/review-check-work.js"; import { githubAutomaticReviewEvent } from "../services/chat-github-events.js"; import { githubBotToolsForSession, syncGitHubBotTools } from "../services/chat-github-tools.js"; import { buildPaperclipRuntimeMcpServers } from "../services/heartbeat/run-preparation.js"; @@ -7774,12 +7775,13 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => { }; const window = async (seconds: number) => { const before = { statements: queries.length, pulls: pulls(), - states: queries.filter(query => query.includes('from "chat_github_reviews"')).length }; + states: queries.filter(query => query.includes('from "chat_github_reviews" where')).length }; for (let second = 0; second < seconds; second++) await pass(); return { statements: queries.length - before.statements, pulls: pulls() - before.pulls, - states: queries.filter(query => query.includes('from "chat_github_reviews"')).length - before.states }; + states: queries.filter(query => query.includes('from "chat_github_reviews" where')).length - before.states }; }; - return { ...f, pass, window, pulls, restart: () => { task = register(); }, now: () => clock }; + const states = () => queries.filter(query => query.includes('from "chat_github_reviews" where')).length; + return { ...f, pass, window, pulls, states, restart: () => { task = register(); }, now: () => clock }; } it("backs off remote head polls while an unresolved GitHub review check cannot change", async () => { @@ -7815,6 +7817,48 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => { expect((await db.select().from(chatGitHubReviews).where(eq(chatGitHubReviews.id, f.review.id)))[0].state).toBe("superseded"); }, 120_000); + it("rebuilds GitHub review checks for company-wide hints at most once per interval", async () => { + const f = await unresolvedReviewCheckPasses(); + await f.window(400); + const reads = async (hint: string) => { + const before = f.states(); + await f.pass(hint); + return f.states() - before; + }; + const company = JSON.stringify({ companyId: f.companyId }); + const repeated = []; + for (let index = 0; index < 10; index++) repeated.push(await reads(company)); + expect(repeated).toEqual([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + for (let index = 0; index < GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS / 1000 - 10; index++) await f.pass(); + expect(await reads(company)).toBe(1); + expect(await reads(JSON.stringify({ companyId: f.companyId, issueId: f.review.issueId }))).toBe(1); + expect(await reads(JSON.stringify({ companyId: f.companyId, issueId: f.review.issueId }))).toBe(1); + expect(await reads(JSON.stringify({ companyId: f.companyId, issueId: randomUUID() }))).toBe(0); + expect(await reads(JSON.stringify({ companyId: randomUUID() }))).toBe(0); + }, 120_000); + + it("publishes a run change for a GitHub review check at once from its issue milestone", async () => { + const f = await unresolvedReviewCheckPasses(); + await f.window(400); + await f.pass(JSON.stringify({ companyId: f.companyId })); + const hints: string[] = []; + const unsubscribe = subscribeDatabaseWork(db, DELIVERY_QUEUES.chatGitHubReviewChecks, (event, settled) => { + if (event === "settled") hints.push(...(settled ?? [])); + }); + onTestFinished(unsubscribe); + await db.transaction(async tx => { + await tx.update(chatGitHubReviews).set({ runId: f.run.id }).where(eq(chatGitHubReviews.id, f.review.id)); + await notifyChatRunMilestones(tx, { companyId: f.companyId, issueId: f.review.issueId, runId: f.run.id }); + }); + await expect.poll(() => hints.length, { timeout: 5000 }).toBeGreaterThan(0); + expect(hints.map(hint => JSON.parse(hint))).toContainEqual({ companyId: f.companyId, issueId: f.review.issueId }); + const writes = f.writes.length; + for (const hint of hints) await f.pass(hint); + expect(f.writes).toHaveLength(writes + 1); + expect(f.writes.at(-1)).toMatchObject({ status: "in_progress" }); + expect(await f.action()).toMatchObject({ status: "received", result: { state: "running", unchangedPolls: 0 } }); + }, 120_000); + async function reviewProjectionFixture(cancelFirst = true) { const f = await reviewBotFixture(); const deliveryId = randomUUID(); diff --git a/server/src/services/chat-channels/github/review-check-work.ts b/server/src/services/chat-channels/github/review-check-work.ts index d3e951d0aa3..49ebb3ab8f5 100644 --- a/server/src/services/chat-channels/github/review-check-work.ts +++ b/server/src/services/chat-channels/github/review-check-work.ts @@ -1,14 +1,21 @@ import { and, asc, eq, gt, or, sql } from "drizzle-orm"; -import { chatActions, chatEndpointLeases, type Db } from "@paperclipai/db"; +import { chatActions, chatEndpointLeases, chatGitHubReviews, type Db } from "@paperclipai/db"; import type { GitHubReviewEventContext } from "@paperclipai/shared"; import type { createDeliveryWorkCoordinator } from "../../delivery-work-coordinator.js"; import { DELIVERY_QUEUES, notifyDeliveryWork } from "../../delivery-work-notifications.js"; import { registerScopedDeadlineWork } from "../../scoped-deadline-work.js"; import { githubReviewCheckState } from "./review-check-state.js"; -type Scope = { companyId: string; endpointId?: string; repositoryId?: string; number?: number }; +type Scope = { companyId: string; endpointId?: string; repositoryId?: string; number?: number; issueId?: string }; type Entry = Scope & { id: string; dueAt: number }; +export const GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS = 30_000; + +export function githubReviewCheckReconstructionIntervalMs(scope: Scope | undefined) { + return scope && !scope.endpointId && !scope.repositoryId && !scope.number && !scope.issueId + ? GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS : 0; +} + export async function notifyGitHubReviewChecks(tx: object, scope: Scope) { await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubReviewChecks, JSON.stringify(scope)); } @@ -19,8 +26,14 @@ export function registerGitHubReviewCheckWork( dispatch: (id: string, canContinue: () => boolean) => Promise, canRun: () => boolean, ) { + const reviewIssueId = sql`(select r.issue_id from ${chatGitHubReviews} r + where r.endpoint_id = ${chatActions.endpointId} + and r.repository_id = ${chatActions.payload}->'event'->>'repositoryId' + and r.pull_number = (${chatActions.payload}->'event'->>'pullNumber')::integer + and r.head_sha = ${chatActions.payload}->'event'->>'headSha' + order by r.created_at desc limit 1)`; async function page(scope?: Scope, cursor?: string, actionId?: string) { - return db.select({ action: chatActions, + return db.select({ action: chatActions, reviewIssueId, leaseExpiresAt: sql`(select max(l.expires_at) from ${chatEndpointLeases} l where l.company_id = ${chatActions.companyId} and l.endpoint_id = ${chatActions.endpointId} and l.lease_key = 'github-publication:' || (${chatActions.payload}->'event'->>'repositoryId') @@ -31,6 +44,7 @@ export function registerGitHubReviewCheckWork( scope?.endpointId ? eq(chatActions.endpointId, scope.endpointId) : undefined, scope?.repositoryId ? sql`${chatActions.payload}->'event'->>'repositoryId' = ${scope.repositoryId}` : undefined, scope?.number ? sql`${chatActions.payload}->'event'->>'pullNumber' = ${String(scope.number)}` : undefined, + scope?.issueId ? sql`${reviewIssueId} = ${scope.issueId}` : undefined, cursor ? gt(chatActions.id, cursor) : undefined, actionId ? eq(chatActions.id, actionId) : undefined, )).orderBy(asc(chatActions.id)).limit(100); } @@ -45,14 +59,16 @@ export function registerGitHubReviewCheckWork( } if (row.leaseExpiresAt) dueAt = Math.max(dueAt, Date.parse(row.leaseExpiresAt)); return { id: action.id, companyId: action.companyId, endpointId: action.endpointId, - repositoryId: event.repositoryId, number: event.pullNumber, dueAt }; + repositoryId: event.repositoryId, number: event.pullNumber, issueId: row.reviewIssueId ?? undefined, dueAt }; } return registerScopedDeadlineWork(coordinator, { queue: DELIVERY_QUEUES.chatGitHubReviewChecks, decodeScope: (hint: string): Scope | undefined => hint === "*" ? undefined : JSON.parse(hint), matches: (value: Entry, scope?: Scope) => !scope || (value.companyId === scope.companyId && (!scope.endpointId || value.endpointId === scope.endpointId) && - (!scope.repositoryId || value.repositoryId === scope.repositoryId) && (!scope.number || value.number === scope.number)), + (!scope.repositoryId || value.repositoryId === scope.repositoryId) && (!scope.number || value.number === scope.number) && + (!scope.issueId || value.issueId === scope.issueId)), + reconstructionIntervalMs: githubReviewCheckReconstructionIntervalMs, load: async (scope?: Scope, signal?: AbortSignal) => { const entries: Entry[] = []; let cursor: string | undefined; diff --git a/server/src/services/chat-run-milestone-notifications.ts b/server/src/services/chat-run-milestone-notifications.ts index 524936395b5..f391ba9d1ca 100644 --- a/server/src/services/chat-run-milestone-notifications.ts +++ b/server/src/services/chat-run-milestone-notifications.ts @@ -19,7 +19,7 @@ export async function notifyChatRunMilestones(tx: object, scope: ChatRunMileston await notifyNativeResponsePresentation(tx, { companyId: scope.companyId, issueId: scope.issueId, runId: scope.issueId ? undefined : scope.runId }); } - await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubReviewChecks, JSON.stringify({ companyId: scope.companyId })); + await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubReviewChecks, JSON.stringify({ companyId: scope.companyId, issueId: scope.issueId })); await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubTerminalResponses, JSON.stringify({ companyId: scope.companyId, issueId: scope.issueId })); await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatRunMilestones, JSON.stringify({ companyId: scope.companyId, issueId: scope.issueId })); // Lifecycle and coarse native progress share lineage and authority. Extend