From 02ee538be91f612c133816a9de4d51e3fd59ab87 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:47:13 -0600 Subject: [PATCH] feat(github): steer active reviews when new messages arrive (#22066) Send compatible GitHub review updates to active native turns after normal admission. Preserve durable receipt fallback and recover acknowledgements without consuming unsent edits. Keep legacy conversation batching and existing identity, source-trust, and attachment boundaries. Add PostgreSQL regressions for concurrency, restart recovery, and edited inputs. --- doc/chat-connector-development.md | 33 ++ .../__tests__/github-inbound-steering.test.ts | 542 ++++++++++++++++++ server/src/routes/issues.ts | 25 +- .../chat-channels/github/inbound-steering.ts | 235 ++++++++ .../services/chat-channels/inbound/wakeups.ts | 27 +- .../src/services/issue-assignment-wakeup.ts | 2 + .../src/services/queued-comment-steering.ts | 24 + 7 files changed, 865 insertions(+), 23 deletions(-) create mode 100644 server/src/__tests__/github-inbound-steering.test.ts create mode 100644 server/src/services/chat-channels/github/inbound-steering.ts create mode 100644 server/src/services/queued-comment-steering.ts diff --git a/doc/chat-connector-development.md b/doc/chat-connector-development.md index 6ce0d764275..8bb1b83e32d 100644 --- a/doc/chat-connector-development.md +++ b/doc/chat-connector-development.md @@ -70,6 +70,39 @@ retain their existing fingerprint. Both paths share the same authorization implementation and perform fresh checks on every call. The real PostgreSQL controls are in `server/src/__tests__/slack-tool-preparation.test.ts`. +## GitHub updates during an active review + +Accepted GitHub messages and automatic PR updates try same-turn steering by +default after normal wake admission. The active native run must belong to the +same task, agent, conversation, responsible person, and source trust level. +Messages with attachments, omitted attachments, or a fresh-session requirement keep a separate turn. +The message carries the admitted GitHub review instructions. + +The durable wake stays pending until the runner acknowledges steering. An +acknowledged message joins the active run's input and requester lineage, and +its wake becomes coalesced. Automatic review records for the new head point to +that run. A rejected or unavailable steer retains ordinary queued delivery. +Late acknowledgements and retries use the native acknowledgement receipt to +avoid sending the same update twice. Other chat providers keep their existing +delivery behavior. + +Each native GitHub receipt stays separate until steering acknowledges its exact input. +This prevents ordinary wake coalescing from merging a newer input into an +uncertain delivery. Steering dispatch does not hold task or conversation locks while it waits for +the provider. A saved receipt records the run, identity reservation, and exact +comment revision before dispatch. Deferred delivery recovers an acknowledgement +after a restart, including when it replays a batch on a closed task. Recovery +does not consume an edit that the provider did not receive or replace a newer +requester lineage. An uncertain reservation stays pending while its source run +can still acknowledge it; recovery rejects it when that run stops without an +acknowledgement. +Retries preserve the original steering revision. An edit that was not sent +keeps ordinary delivery. Overlapping inputs also keep their admitted wakes +while another steering acknowledgement is pending. After the active review +ends, unacknowledged inputs retain ordinary delivery. Legacy runs keep their +existing conversation batching when no native steering receipt is outstanding. +Admission rechecks that condition under the task and run locks. + ## Chat activity pagination and callback diagnostics The connection Activity tab loads 25 records per page. `GET /api/chat-endpoints/:id/activity?limit=25` diff --git a/server/src/__tests__/github-inbound-steering.test.ts b/server/src/__tests__/github-inbound-steering.test.ts new file mode 100644 index 00000000000..bd51b79564f --- /dev/null +++ b/server/src/__tests__/github-inbound-steering.test.ts @@ -0,0 +1,542 @@ +import { randomUUID } from "node:crypto"; +import { and, eq, sql } from "drizzle-orm"; +import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import { + agents, agentWakeupRequests, authUsers, chatActions, chatConversations, chatDeliveries, + chatEndpoints, chatExternalPrincipals, chatGitHubReviews, chatMessageLinks, companies, companyMemberships, createDb, + heartbeatRuns, heartbeatRunEvents, issueAttachments, issueComments, issues, runIdentityContexts, toolApplications, toolConnections, assets, +} from "@paperclipai/db"; +import { startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; +import { heartbeatService } from "../services/heartbeat.js"; +import { createInboundWakeups } from "../services/chat-channels/inbound/wakeups.js"; +import { initializeRunIdentity } from "../services/run-identity.js"; +import { NativeSessionSteeringError } from "../services/native-runtime/native-session-executor.js"; +import { chatWakeAuthorityDenied } from "../services/chat-channels/inbound/wakeup-authorization-error.js"; +import { defaultGitHubReviewPolicy, type GitHubReviewEventContext } from "@paperclipai/shared"; +import { resumeDeferredChatWake } from "../services/durable-chat-wakeup.js"; + +const steer = vi.hoisted(() => vi.fn()); +vi.mock("../services/native-runtime/native-session-executor.js", async importOriginal => ({ + ...await importOriginal(), + steerNativeSession: steer, +})); + +describe("GitHub inbound steering", () => { + let temporary: Awaited>; + let db: ReturnType; + let heartbeat: ReturnType; + let unregister: (() => void) | undefined; + + beforeAll(async () => { + temporary = await startEmbeddedPostgresTestDatabase("github-steering-"); + db = createDb(temporary.connectionString); + db.$client.options.onnotice = () => {}; + }); + afterEach(async () => { + unregister?.(); + await heartbeat?.drainActiveRunExecutions(); + await db.execute(sql`truncate companies cascade`); + vi.clearAllMocks(); + steer.mockReset(); + }); + afterAll(async () => { await db?.$client.end(); await temporary?.cleanup(); }); + + async function fixture(provider: "github" | "slack" = "github") { + const companyId = randomUUID(), userId = randomUUID(); + await db.insert(authUsers).values({ id: userId, name: "Reviewer", email: `${userId}@example.test`, + createdAt: new Date(), updatedAt: new Date() }); + await db.insert(companies).values({ id: companyId, name: "Review company", issuePrefix: "REV", + defaultResponsibleUserId: userId, requireBoardApprovalForNewAgents: false }); + await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: userId, + status: "active", membershipRole: "owner" }); + const [agent] = await db.insert(agents).values({ companyId, name: "Reviewer", adapterType: "paperclip_runner", + status: "idle", runtimeConfig: { heartbeat: { enabled: true, intervalSec: 0, wakeOnDemand: true } } }).returning(); + const [application] = await db.insert(toolApplications).values({ companyId, name: "GitHub", type: "mcp_http" }).returning(); + const [connection] = await db.insert(toolConnections).values({ companyId, applicationId: application.id, + uid: randomUUID(), name: "GitHub", transport: "rest_api" }).returning(); + const [endpoint] = await db.insert(chatEndpoints).values({ companyId, connectionId: connection.id, provider, + publicId: randomUUID(), assignedAgentId: agent.id, status: "active" }).returning(); + const [principal] = await db.insert(chatExternalPrincipals).values({ companyId, provider, + providerAccountId: "installation", externalId: "123", kind: "user" }).returning(); + const [issue] = await db.insert(issues).values({ companyId, title: "PR review", status: "in_progress", + assigneeAgentId: agent.id }).returning(); + const [conversation] = await db.insert(chatConversations).values({ companyId, endpointId: endpoint.id, + issueId: issue.id, externalConversationId: "paperclipai/paperclip", externalThreadId: "github:paperclipai/paperclip:1", + externalLabel: "PR #1", state: "active" }).returning(); + const [original, followup] = await db.insert(issueComments).values([ + { companyId, issueId: issue.id, authorUserId: userId, body: "Review this PR" }, + { companyId, issueId: issue.id, authorUserId: userId, body: "The PR has a new commit. Review the updated diff." }, + ]).returning(); + const [run] = await db.insert(heartbeatRuns).values({ companyId, agentId: agent.id, status: "running", + runtimeMode: "native", nativeIssueId: issue.id, startedAt: new Date(), + contextSnapshot: { issueId: issue.id, chatConversationId: conversation.id, wakeCommentId: original.id, + wakeCommentIds: [original.id] } }).returning(); + await db.update(issues).set({ executionRunId: run.id }).where(eq(issues.id, issue.id)); + await initializeRunIdentity(db, { companyId, runId: run.id, issueId: issue.id, + responsibleUserId: userId, messageIds: [original.id], cause: "instruction" }); + const [delivery] = await db.insert(chatDeliveries).values({ companyId, endpointId: endpoint.id, + conversationId: conversation.id, principalId: principal.id, providerEventId: randomUUID(), + deduplicationKey: randomUUID(), eventKind: "message", state: "processed", normalizedEvent: {} }).returning(); + const [action] = await db.insert(chatActions).values({ companyId, endpointId: endpoint.id, + conversationId: conversation.id, deliveryId: delivery.id, principalId: principal.id, + kind: "inbound_wakeup", providerActionId: `inbound_wakeup:${delivery.id}`, status: "issued", updatedAt: new Date(), + payload: { version: 1, agentId: agent.id, issueId: issue.id, commentId: followup.id, + sessionGeneration: conversation.sessionGeneration, requestedByActorType: "user", requestedByActorId: userId } }).returning(); + heartbeat = heartbeatService(db); + const errors: string[] = []; + let authorizationCount = 0; + const authorization = { denyAfterAdmission: false }; + const makeOwner = () => createInboundWakeups(db, { + authorizeInboundWakeup: async () => { throw new Error("unused"); }, + authorizeInboundWorkStart: async (tx, source) => { + authorizationCount += 1; + if (authorization.denyAfterAdmission && authorizationCount > 2) throw chatWakeAuthorityDenied("runtime_fence"); + const [current] = await tx.select().from(issues).where(eq(issues.id, issue.id)).for("update"); + const [currentDelivery] = await tx.select().from(chatDeliveries).where(eq(chatDeliveries.id, source.deliveryId!)); + return { issue: current, endpoint, conversation, delivery: currentDelivery, resource: null }; + }, + attachmentOmissionDetail: () => null, isExternalActionAuthorizationChange: error => + (error as { details?: { code?: string } }).details?.code === "chat_action_authorization_changed", + redactError: error => { errors.push(error instanceof Error ? error.stack! : String(error)); return String(error); }, + fetchImpl: async () => { throw new Error("unused"); }, heartbeat, + }); + const restart = () => { + unregister?.(); + const owner = makeOwner(); + unregister = owner.unregisterDeferredWakeOwner; + return owner; + }; + const owner = restart(); + steer.mockResolvedValue({ turnId: "review-turn" }); + return { companyId, issue, run, action, followup, owner, errors, original, authorization, delivery, endpoint, conversation, userId, restart }; + } + + it("steers a new GitHub message into the active review and consumes its saved wake", async () => { + const f = await fixture(); + expect(await f.owner.processInboundWakeup(f.action.deliveryId!), f.errors.join("\n")).toBe(true); + expect(steer).toHaveBeenCalledWith(expect.objectContaining({ runId: f.run.id, + correlationId: f.followup.id, message: expect.stringContaining(f.followup.body) })); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "coalesced", runId: f.run.id }); + const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.run.id)); + expect(run.contextSnapshot?.wakeCommentIds).toContain(f.followup.id); + const [identity] = await db.select().from(runIdentityContexts).where(eq(runIdentityContexts.id, run.activeIdentityContextId!)); + expect(identity).toMatchObject({ status: "accepted", messageId: f.followup.id }); + expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, f.companyId))).toHaveLength(1); + await f.owner.processInboundWakeup(f.action.deliveryId!); + expect(steer).toHaveBeenCalledTimes(1); + expect(await db.select().from(agentWakeupRequests).where(and(eq(agentWakeupRequests.companyId, f.companyId), + eq(agentWakeupRequests.status, "deferred_issue_execution")))).toEqual([]); + }); + + it.each(["steering_unsupported", "steering_temporarily_unavailable", "steering_stale_turn", "steering_rejected"] as const)( + "keeps the durable wake for a separate turn when steering reports %s", async code => { + const f = await fixture(); + steer.mockRejectedValueOnce(new NativeSessionSteeringError(code, code)); + expect(await f.owner.processInboundWakeup(f.action.deliveryId!)).toBe(true); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "deferred_issue_execution", runId: null }); + const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.run.id)); + const [identity] = await db.select().from(runIdentityContexts).where(eq(runIdentityContexts.id, run.activeIdentityContextId!)); + expect(identity.messageId).toBe(f.original.id); + const [reservation] = await db.select().from(runIdentityContexts).where(eq(runIdentityContexts.messageId, f.followup.id)); + expect(reservation.status).toBe("rejected"); + }, + ); + + it.each(["slack", "legacy", "different_task", "different_conversation", "different_author", "different_trust"])( + "retains separate delivery for %s", async boundary => { + const f = await fixture(boundary === "slack" ? "slack" : "github"); + if (boundary === "legacy") await db.update(heartbeatRuns).set({ runtimeMode: "legacy" }).where(eq(heartbeatRuns.id, f.run.id)); + if (boundary === "different_task") await db.update(heartbeatRuns).set({ nativeIssueId: null, issueId: null, + contextSnapshot: { ...f.run.contextSnapshot, issueId: randomUUID() } }).where(eq(heartbeatRuns.id, f.run.id)); + if (boundary === "different_conversation") await db.update(heartbeatRuns).set({ + contextSnapshot: { ...f.run.contextSnapshot, chatConversationId: randomUUID() } }).where(eq(heartbeatRuns.id, f.run.id)); + if (boundary === "different_author") await db.update(issueComments).set({ authorUserId: randomUUID() }).where(eq(issueComments.id, f.followup.id)); + if (boundary === "different_trust") await db.update(issueComments).set({ + sourceTrust: { preset: "low_trust_review", disposition: "quarantined", sourceIssueId: f.issue.id }, + }).where(eq(issueComments.id, f.followup.id)); + await f.owner.processInboundWakeup(f.action.deliveryId!); + expect(steer).not.toHaveBeenCalled(); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "deferred_issue_execution", runId: null }); + }, + ); + + it("checks current source access again before steering an admitted message", async () => { + const f = await fixture(); + f.authorization.denyAfterAdmission = true; + await f.owner.processInboundWakeup(f.action.deliveryId!); + expect(steer).not.toHaveBeenCalled(); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "deferred_issue_execution", runId: null }); + }); + + it("retries admission if a legacy run selects native execution before scheduling", async () => { + const f = await fixture(), wakeup = heartbeat.wakeup.bind(heartbeat); + await db.update(heartbeatRuns).set({ runtimeMode: "legacy" }).where(eq(heartbeatRuns.id, f.run.id)); + const schedule = vi.spyOn(heartbeat, "wakeup").mockImplementationOnce(async (agentId, options) => { + await db.update(heartbeatRuns).set({ runtimeMode: "native" }).where(eq(heartbeatRuns.id, f.run.id)); + return wakeup(agentId, options); + }); + try { + expect(await f.owner.processInboundWakeup(f.delivery.id)).toBe(false); + expect(await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id))).toEqual([]); + await db.update(chatActions).set({ result: null }).where(eq(chatActions.id, f.action.id)); + expect(await f.owner.processInboundWakeup(f.delivery.id)).toBe(true); + expect(steer).toHaveBeenCalledTimes(1); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "coalesced", runId: f.run.id }); + } finally { schedule.mockRestore(); } + }); + + async function acknowledge(f: Awaited>) { + await db.insert(heartbeatRunEvents).values({ companyId: f.companyId, agentId: f.run.agentId, runId: f.run.id, + seq: 1, sourceEventId: randomUUID(), eventType: "item.completed", stream: "system", level: "info", + payload: { prpEvent: { turnId: "review-turn", itemId: `review-turn:steer:${f.followup.id}`, + payload: { kind: "steering_acknowledgement" } } } }); + } + + it("recovers a durable acknowledgement without sending the message again", async () => { + const f = await fixture(); + await acknowledge(f); + await f.owner.processInboundWakeup(f.action.deliveryId!); + expect(steer).not.toHaveBeenCalled(); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "coalesced", runId: f.run.id }); + }); + + it("permits native event persistence while waiting for acknowledgement", async () => { + const f = await fixture(); + steer.mockImplementationOnce(async () => { + await db.transaction(async tx => { + await tx.execute(sql`set local lock_timeout = '1s'`); + await tx.update(heartbeatRuns).set({ resultJson: { concurrentNativeReceipt: true } }).where(eq(heartbeatRuns.id, f.run.id)); + await acknowledge(f); + }); + return { turnId: "review-turn" }; + }); + await f.owner.processInboundWakeup(f.action.deliveryId!); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "coalesced", runId: f.run.id }); + const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.run.id)); + expect(run.resultJson?.concurrentNativeReceipt).toBe(true); + }); + + it("consumes a timed-out steer when its late acknowledgement arrives", async () => { + const f = await fixture(); + let onAcknowledged: (() => Promise) | undefined; + steer.mockImplementationOnce(async input => { + onAcknowledged = input.onAcknowledged; + throw new NativeSessionSteeringError("steering_timeout", "Timed out"); + }); + await f.owner.processInboundWakeup(f.action.deliveryId!); + const [pending] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(pending).toMatchObject({ status: "deferred_issue_execution", runId: null }); + await acknowledge(f); + expect(onAcknowledged).toBeTypeOf("function"); + await onAcknowledged!(); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "coalesced", runId: f.run.id }); + }); + + it("recovers an acknowledged update from deferred delivery after a server restart", async () => { + const f = await fixture(); + steer.mockRejectedValueOnce(new NativeSessionSteeringError("steering_timeout", "Timed out")); + await f.owner.processInboundWakeup(f.delivery.id); + await acknowledge(f); + await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date() }).where(eq(heartbeatRuns.id, f.run.id)); + await db.update(issues).set({ executionRunId: null }).where(eq(issues.id, f.issue.id)); + f.restart(); + const schedule = vi.spyOn(heartbeat, "wakeup").mockResolvedValue(null); + try { + await resumeDeferredChatWake(db, { companyId: f.companyId, agentId: f.run.agentId, + issueId: f.issue.id, requestId: f.action.id, recoverySweep: true }); + expect(schedule).not.toHaveBeenCalled(); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "coalesced", runId: f.run.id }); + } finally { schedule.mockRestore(); } + }); + + it("allows task and conversation changes while the provider has not acknowledged steering", async () => { + const f = await fixture(); + let release: ((value: { turnId: string }) => void) | undefined; + steer.mockImplementationOnce(() => new Promise(resolve => { release = resolve; })); + const processing = f.owner.processInboundWakeup(f.delivery.id); + await vi.waitFor(() => expect(release).toBeTypeOf("function")); + try { + await db.transaction(async tx => { + await tx.execute(sql`set local lock_timeout = '200ms'`); + await tx.update(issues).set({ title: "Changed during provider I/O" }).where(eq(issues.id, f.issue.id)); + await tx.update(chatConversations).set({ externalLabel: "Changed during provider I/O" }) + .where(eq(chatConversations.id, f.conversation.id)); + }); + } finally { + release!({ turnId: "review-turn" }); + await processing; + } + }); + + it("clears an unacknowledged reservation when its source run stops", async () => { + const f = await fixture(); + steer.mockRejectedValueOnce(new NativeSessionSteeringError("steering_timeout", "Timed out")); + await f.owner.processInboundWakeup(f.delivery.id); + await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date() }).where(eq(heartbeatRuns.id, f.run.id)); + await db.update(issues).set({ executionRunId: null }).where(eq(issues.id, f.issue.id)); + f.restart(); + const schedule = vi.spyOn(heartbeat, "wakeup").mockResolvedValue(null); + try { + await resumeDeferredChatWake(db, { companyId: f.companyId, agentId: f.run.agentId, + issueId: f.issue.id, requestId: f.action.id, recoverySweep: true }); + expect(schedule).toHaveBeenCalledTimes(1); + const [identity] = await db.select().from(runIdentityContexts).where(eq(runIdentityContexts.messageId, f.followup.id)); + expect(identity.status).toBe("rejected"); + } finally { schedule.mockRestore(); } + }); + + it("retains an edited message for a new turn if it changes before acknowledgement", async () => { + const f = await fixture(); + steer.mockImplementationOnce(async () => { + await db.update(issueComments).set({ body: "Review the newer edit" }).where(eq(issueComments.id, f.followup.id)); + return { turnId: "review-turn" }; + }); + await f.owner.processInboundWakeup(f.delivery.id); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "deferred_issue_execution", runId: null }); + const [identity] = await db.select().from(runIdentityContexts).where(eq(runIdentityContexts.messageId, f.followup.id)); + expect(identity.status).toBe("accepted"); + }); + + it("does not consume an edited message with the original acknowledgement when its action is retried", async () => { + const f = await fixture(); + steer.mockImplementationOnce(async () => { + await acknowledge(f); + await db.update(issueComments).set({ body: "Review the newer edit" }).where(eq(issueComments.id, f.followup.id)); + return { turnId: "review-turn" }; + }); + await f.owner.processInboundWakeup(f.delivery.id); + const [original] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + await db.update(chatActions).set({ status: "processing", result: null, + updatedAt: new Date(Date.now() - 60 * 60_000) }).where(eq(chatActions.id, f.action.id)); + const owner = f.restart(); + expect(await owner.processInboundWakeup(f.delivery.id), f.errors.join("\n")).toBe(true); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "deferred_issue_execution", runId: null }); + expect(receipt.payload?.githubSteering).toEqual(original.payload?.githubSteering); + expect(steer).toHaveBeenCalledTimes(1); + await db.update(heartbeatRuns).set({ status: "succeeded" }).where(eq(heartbeatRuns.id, f.run.id)); + await db.update(issues).set({ executionRunId: null }).where(eq(issues.id, f.issue.id)); + const schedule = vi.spyOn(heartbeat, "wakeup").mockResolvedValue(null); + try { + await resumeDeferredChatWake(db, { companyId: f.companyId, agentId: f.run.agentId, + issueId: f.issue.id, requestId: f.action.id, recoverySweep: true }); + expect(schedule).toHaveBeenCalledTimes(1); + expect(schedule.mock.calls[0][1]?.contextSnapshot?.wakeCommentId).toBe(f.followup.id); + } finally { schedule.mockRestore(); } + }); + + it("retains a GitHub message with a file for a separate turn", async () => { + const f = await fixture(); + const [asset] = await db.insert(assets).values({ companyId: f.companyId, provider: "local_disk", objectKey: randomUUID(), + contentType: "text/plain", byteSize: 4, sha256: "a".repeat(64), originalFilename: "review.txt" }).returning(); + await db.insert(issueAttachments).values({ companyId: f.companyId, issueId: f.issue.id, assetId: asset.id, issueCommentId: f.followup.id }); + await f.owner.processInboundWakeup(f.delivery.id); + expect(steer).not.toHaveBeenCalled(); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "deferred_issue_execution", runId: null }); + }); + + it("retains a message with an omitted attachment for a separate turn", async () => { + const f = await fixture(); + await db.update(chatActions).set({ payload: { ...f.action.payload, + attachmentOmissionReasons: { unsupported_type: 1 } } }).where(eq(chatActions.id, f.action.id)); + await f.owner.processInboundWakeup(f.delivery.id); + expect(steer).not.toHaveBeenCalled(); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "deferred_issue_execution", runId: null }); + }); + + it("invalidates derived wake projections after an acknowledged update", async () => { + const f = await fixture(); + await db.update(heartbeatRuns).set({ contextSnapshot: { ...f.run.contextSnapshot, + paperclipWake: { stale: true }, paperclipTaskMarkdown: "Old task", paperclipTurnContext: { stale: true } } }) + .where(eq(heartbeatRuns.id, f.run.id)); + await f.owner.processInboundWakeup(f.delivery.id); + const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.run.id)); + expect(run.contextSnapshot?.commentId).toBe(f.followup.id); + expect(run.contextSnapshot?.paperclipWake).toBeUndefined(); + expect(run.contextSnapshot?.paperclipTaskMarkdown).toBeUndefined(); + expect(run.contextSnapshot?.paperclipTurnContext).toBeUndefined(); + }); + + it("steers messages when attachment processing reports no omissions", async () => { + const f = await fixture(); + await db.update(chatActions).set({ payload: { ...f.action.payload, attachmentOmissionReasons: {} } }) + .where(eq(chatActions.id, f.action.id)); + await f.owner.processInboundWakeup(f.delivery.id); + expect(steer).toHaveBeenCalledTimes(1); + }); + + it("retains input that requires a fresh session", async () => { + const f = await fixture(), wakeup = heartbeat.wakeup.bind(heartbeat); + const schedule = vi.spyOn(heartbeat, "wakeup").mockImplementation((agentId, options) => wakeup(agentId, + { ...options, contextSnapshot: { ...options?.contextSnapshot, forceFreshSession: true } })); + try { + await f.owner.processInboundWakeup(f.delivery.id); + expect(steer).not.toHaveBeenCalled(); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "deferred_issue_execution", runId: null }); + } finally { schedule.mockRestore(); } + }); + + async function additionalUpdate(f: Awaited>) { + const [comment] = await db.insert(issueComments).values({ companyId: f.companyId, issueId: f.issue.id, + authorUserId: f.userId, body: "A newer GitHub update" }).returning(); + const [delivery] = await db.insert(chatDeliveries).values({ ...f.delivery, id: randomUUID(), + providerEventId: randomUUID(), deduplicationKey: randomUUID(), createdAt: new Date() }).returning(); + const [action] = await db.insert(chatActions).values({ ...f.action, id: randomUUID(), deliveryId: delivery.id, + providerActionId: `inbound_wakeup:${delivery.id}`, payload: { ...f.action.payload, commentId: comment.id }, + createdAt: new Date(), updatedAt: new Date() }).returning(); + return { comment, delivery, action }; + } + + it.each(["waiting", "timed_out"])("admits a concurrent update while a prior steer is %s", async state => { + const f = await fixture(); + let release: ((value: { turnId: string }) => void) | undefined; + if (state === "waiting") steer.mockImplementationOnce(() => new Promise(resolve => { release = resolve; })); + else steer.mockRejectedValueOnce(new NativeSessionSteeringError("steering_timeout", "Timed out")); + const processing = f.owner.processInboundWakeup(f.delivery.id); + if (state === "waiting") await vi.waitFor(() => expect(release).toBeTypeOf("function")); + else await processing; + try { + const next = await additionalUpdate(f); + expect(await f.owner.processInboundWakeup(next.delivery.id), f.errors.join("\n")).toBe(true); + const [action] = await db.select().from(chatActions).where(eq(chatActions.id, next.action.id)); + expect(action).toMatchObject({ status: "processed" }); + expect(f.errors).toEqual([]); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, next.action.id)); + expect(receipt).toMatchObject({ status: "deferred_issue_execution", runId: null }); + expect(receipt.payload?.wakeCommentId).toBe(next.comment.id); + expect(steer).toHaveBeenCalledTimes(1); + } finally { + release?.({ turnId: "review-turn" }); + await processing; + } + }); + + it("keeps the latest requester source when an older acknowledgement is recovered", async () => { + const f = await fixture(); + steer.mockRejectedValueOnce(new NativeSessionSteeringError("steering_timeout", "Timed out")); + await f.owner.processInboundWakeup(f.delivery.id); + await acknowledge(f); + f.restart(); + const next = await additionalUpdate(f); + expect(await f.owner.processInboundWakeup(next.delivery.id), f.errors.join("\n")).toBe(true); + expect(steer).toHaveBeenCalledTimes(2); + const schedule = vi.spyOn(heartbeat, "wakeup").mockResolvedValue(null); + try { + await resumeDeferredChatWake(db, { companyId: f.companyId, agentId: f.run.agentId, + issueId: f.issue.id, requestId: f.action.id, recoverySweep: true }); + expect(schedule).not.toHaveBeenCalled(); + const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.run.id)); + expect(run.contextSnapshot?.wakeCommentId).toBe(next.comment.id); + expect(run.contextSnapshot?.wakeCommentIds).toEqual([f.original.id, f.followup.id, next.comment.id]); + } finally { schedule.mockRestore(); } + }); + + it.each(["running", "succeeded"] as const)("keeps a %s successor run's task lineage when an earlier acknowledgement is recovered", async status => { + const f = await fixture(); + steer.mockRejectedValueOnce(new NativeSessionSteeringError("steering_timeout", "Timed out")); + await f.owner.processInboundWakeup(f.delivery.id); + await acknowledge(f); + await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date() }).where(eq(heartbeatRuns.id, f.run.id)); + const next = await additionalUpdate(f); + const [successor] = await db.insert(heartbeatRuns).values({ companyId: f.companyId, agentId: f.run.agentId, + status: "running", runtimeMode: "native", nativeIssueId: f.issue.id, startedAt: new Date(), + contextSnapshot: { issueId: f.issue.id, chatConversationId: f.conversation.id, + wakeCommentId: next.comment.id, wakeCommentIds: [next.comment.id] } }).returning(); + const current = await initializeRunIdentity(db, { companyId: f.companyId, runId: successor.id, issueId: f.issue.id, + responsibleUserId: f.userId, messageIds: [next.comment.id], cause: "instruction" }); + await db.update(heartbeatRuns).set({ status }).where(eq(heartbeatRuns.id, successor.id)); + await db.update(issues).set({ executionRunId: status === "running" ? successor.id : null }).where(eq(issues.id, f.issue.id)); + f.restart(); + const schedule = vi.spyOn(heartbeat, "wakeup").mockResolvedValue(null); + try { + await resumeDeferredChatWake(db, { companyId: f.companyId, agentId: f.run.agentId, + issueId: f.issue.id, requestId: f.action.id, recoverySweep: true }); + expect(schedule).not.toHaveBeenCalled(); + const [task] = await db.select().from(issues).where(eq(issues.id, f.issue.id)); + expect(task.continuationIdentityContextId).toBe(current.id); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "coalesced", runId: f.run.id }); + } finally { schedule.mockRestore(); } + }); + + it("excludes acknowledged updates from a closed-task replay batch", async () => { + const f = await fixture(); + steer.mockRejectedValueOnce(new NativeSessionSteeringError("steering_timeout", "Timed out")); + await f.owner.processInboundWakeup(f.delivery.id); + await acknowledge(f); + const next = await additionalUpdate(f); + steer.mockRejectedValueOnce(new NativeSessionSteeringError("steering_unsupported", "Unsupported")); + await f.owner.processInboundWakeup(next.delivery.id); + await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date() }).where(eq(heartbeatRuns.id, f.run.id)); + await db.update(issues).set({ status: "done", executionRunId: null }).where(eq(issues.id, f.issue.id)); + f.restart(); + const schedule = vi.spyOn(heartbeat, "wakeup").mockResolvedValue(null); + try { + await resumeDeferredChatWake(db, { companyId: f.companyId, agentId: f.run.agentId, + issueId: f.issue.id, requestId: next.action.id, recoverySweep: true }); + expect(schedule).toHaveBeenCalledTimes(1); + expect(schedule.mock.calls[0][1]?.contextSnapshot?.wakeCommentIds).toBeUndefined(); + const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, f.action.id)); + expect(receipt).toMatchObject({ status: "coalesced", runId: f.run.id }); + } finally { schedule.mockRestore(); } + }); + + it("delivers a burst of GitHub updates to one active review without a wake backlog", async () => { + const f = await fixture(); + const comments = [f.followup.id]; + await f.owner.processInboundWakeup(f.action.deliveryId!); + for (let index = 0; index < 3; index++) { + const [comment] = await db.insert(issueComments).values({ companyId: f.companyId, issueId: f.issue.id, + authorUserId: f.userId, body: `Additional review comment ${index}` }).returning(); + const [delivery] = await db.insert(chatDeliveries).values({ ...f.delivery, id: randomUUID(), + providerEventId: randomUUID(), deduplicationKey: randomUUID() }).returning(); + await db.insert(chatActions).values({ ...f.action, id: randomUUID(), deliveryId: delivery.id, + providerActionId: `inbound_wakeup:${delivery.id}`, payload: { ...f.action.payload, commentId: comment.id }, + createdAt: new Date(), updatedAt: new Date() }); + comments.push(comment.id); + expect(await f.owner.processInboundWakeup(delivery.id)).toBe(true); + } + expect(steer).toHaveBeenCalledTimes(4); + expect(await db.select().from(agentWakeupRequests).where(and(eq(agentWakeupRequests.companyId, f.companyId), + eq(agentWakeupRequests.status, "deferred_issue_execution")))).toEqual([]); + expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, f.companyId))).toHaveLength(1); + const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, f.run.id)); + expect(run.contextSnapshot?.wakeCommentIds).toEqual([f.original.id, ...comments]); + }); + + it("steers an automatic push with its review policy and binds its new head to the active run", async () => { + const f = await fixture(); + const event: GitHubReviewEventContext = { event: "synchronize", deliveryId: randomUUID(), repositoryId: "123", + repository: "paperclipai/paperclip", pullNumber: 1, title: "Review this PR", body: "Updated diff", + baseSha: "a".repeat(40), headSha: "b".repeat(40), baseBranch: "master", author: { id: "123", login: "author", isBot: false }, + sender: { id: "123", login: "author" }, draft: false, labels: [] }; + const policy = { ...defaultGitHubReviewPolicy(), instructions: "Check the new commit for concurrency errors." }; + const sourceTrust = { preset: "low_trust_review", disposition: "quarantined", sourceIssueId: f.issue.id } as const; + await db.update(issueComments).set({ sourceTrust }).where(eq(issueComments.issueId, f.issue.id)); + await db.update(chatDeliveries).set({ normalizedEvent: { githubAutomatic: { context: event, revision: 2, policy } } }) + .where(eq(chatDeliveries.id, f.delivery.id)); + await db.insert(chatMessageLinks).values({ companyId: f.companyId, endpointId: f.endpoint.id, + conversationId: f.conversation.id, deliveryId: f.delivery.id, commentId: f.followup.id, + providerMessageId: "github-event", direction: "inbound" }); + const [review] = await db.insert(chatGitHubReviews).values({ companyId: f.companyId, endpointId: f.endpoint.id, + issueId: f.issue.id, repositoryId: event.repositoryId, repository: event.repository, pullNumber: event.pullNumber, + headSha: event.headSha, deliveryId: f.delivery.id, configurationRevision: 2, policySnapshot: policy, event, state: "queued" }).returning(); + await f.owner.processInboundWakeup(f.delivery.id); + expect(steer).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining(policy.instructions) })); + const [updated] = await db.select().from(chatGitHubReviews).where(eq(chatGitHubReviews.id, review.id)); + expect(updated).toMatchObject({ runId: f.run.id, state: "running", headSha: event.headSha }); + }); +}); diff --git a/server/src/routes/issues.ts b/server/src/routes/issues.ts index 41abc60ae32..386acf2b0bf 100644 --- a/server/src/routes/issues.ts +++ b/server/src/routes/issues.ts @@ -72,8 +72,6 @@ import { markExecutionReconciliation, } from "../services/execution-recovery-resolution.js"; import { - storedSteeringAcknowledgement, - reconcileSteeredIdentity, reserveSteeredIdentity, acceptSteeredIdentity, rejectSteeredIdentity, @@ -424,7 +422,6 @@ import { import { getNativeSessionSteeringState, NativeSessionSteeringError, - steerNativeSession, } from "../services/native-runtime/native-session-executor.js"; import { buildQueuedCommentQueueSnapshot, @@ -432,6 +429,7 @@ import { queuedCommentIdsFromWakePayload, withQueuedCommentIdsInWakePayload, } from "../services/issue-queued-comment-queue.js"; +import { deliverSteeredMessage, steeringDeliveryIsUncertain } from "../services/queued-comment-steering.js"; const MAX_ISSUE_COMMENT_LIMIT = 500; const updateIssueRouteSchema = updateIssueSchema.extend({ @@ -18205,18 +18203,10 @@ export function issueRoutes( commentId, lock: true, }); steeringDeliveryAttempted = true; - const acknowledgement = - (await storedSteeringAcknowledgement(tx, steeringIdentity ?? { - companyId: issue.companyId, runId: locked.activeRun.id, messageId: commentId, - })) ?? - (await steerNativeSession({ - runId: locked.activeRun.id, - message: entry.comment.body, - correlationId: commentId, - onAcknowledged: steeringIdentity - ? () => reconcileSteeredIdentity(db, steeringIdentity) - : undefined, - })); + const acknowledgement = await deliverSteeredMessage(db, { + executor: tx, companyId: issue.companyId, runId: locked.activeRun.id, + messageId: commentId, message: entry.comment.body, identity: steeringIdentity, + }); // PRP ingestion allocates event sequences on this row. Locking it // while awaiting the provider prevents its durable ACK from arriving. // Re-read under lock after ACK so concurrent run receipts are retained. @@ -18284,10 +18274,7 @@ export function issueRoutes( }); }); } catch (error) { - const uncertain = - steeringDeliveryAttempted && - (!(error instanceof NativeSessionSteeringError) || - error.code === "steering_timeout"); + const uncertain = steeringDeliveryIsUncertain(error, steeringDeliveryAttempted); if (steeringIdentity && !uncertain) await rejectSteeredIdentity(db, steeringIdentity); diff --git a/server/src/services/chat-channels/github/inbound-steering.ts b/server/src/services/chat-channels/github/inbound-steering.ts new file mode 100644 index 00000000000..c8d49576ce6 --- /dev/null +++ b/server/src/services/chat-channels/github/inbound-steering.ts @@ -0,0 +1,235 @@ +import { and, eq, inArray, isNull, sql } from "drizzle-orm"; +import { createHash } from "node:crypto"; +import { + agentWakeupRequests, chatActions, chatEndpoints, chatGitHubReviews, heartbeatRuns, + issueAttachments, issueComments, issues, runIdentityContexts, type Db, +} from "@paperclipai/db"; +import { isUuidLike } from "@paperclipai/shared"; +import { logActivity } from "../../activity-log.js"; +import { notifyChatRunMilestones } from "../../chat-run-milestone-notifications.js"; +import { assertDurableChatWakeupReceipt, createDurableChatWakeupRequest, type DurableChatWakeupRequest } from "../../durable-chat-wakeup.js"; +import { queuedCommentIdsFromWakePayload, withQueuedCommentIdsInRunContext } from "../../issue-queued-comment-queue.js"; +import { acceptSteeredIdentity, rejectSteeredIdentity, reserveSteeredIdentity, storedSteeringAcknowledgement } from "../../run-identity.js"; +import { deliverSteeredMessage, steeringDeliveryIsUncertain } from "../../queued-comment-steering.js"; +import { boundRunIssueId } from "../../run-task-binding.js"; +import { isLowTrustQuarantined } from "../../source-trust.js"; +import { NativeSessionSteeringError } from "../../native-runtime/native-session-executor.js"; +import type { createInboundWakeAuthority } from "../inbound/wakeup-authorization.js"; +import { resolveGitHubReviewInstructions } from "./review-instructions.js"; +import { notifyGitHubReviewChecks } from "./review-check-work.js"; + +type Authorize = ReturnType["authorizeInboundWorkStart"]; +type Action = typeof chatActions.$inferSelect; +type SteeringReceipt = { runId: string; identityId: string; commentUpdatedAt: string; commentDigest: string }; +const commentDigest = (body: string) => createHash("sha256").update(body).digest("hex"); + +async function hasSteeringReceipt(db: Pick, issue: { companyId: string; id: string }) { + const [wake] = await db.select({ id: agentWakeupRequests.id }).from(agentWakeupRequests).where(and( + eq(agentWakeupRequests.companyId, issue.companyId), eq(agentWakeupRequests.status, "deferred_issue_execution"), + sql`${agentWakeupRequests.payload}->>'issueId' = ${issue.id}`, + sql`${agentWakeupRequests.payload} ? 'githubSteering'`, + )).limit(1); + return Boolean(wake); +} + +export async function githubInboundWakeAdmission(db: Db, + issue: Pick) { + const legacyRun = async (executor: Db) => !issue.executionRunId ? false : Boolean(await executor + .select({ id: heartbeatRuns.id }).from(heartbeatRuns).where(and( + eq(heartbeatRuns.id, issue.executionRunId), eq(heartbeatRuns.companyId, issue.companyId), + eq(heartbeatRuns.runtimeMode, "legacy"), + )).for("no key update").then(rows => rows[0])); + const allowRunCoalescing = await legacyRun(db) && !(await hasSteeringReceipt(db, issue)); + return { + options: { allowRunCoalescing }, + authorize: async (tx: Db, current: Pick) => { + if (allowRunCoalescing && (current.executionRunId !== issue.executionRunId || + !(await legacyRun(tx)) || await hasSteeringReceipt(tx, issue))) { + throw new Error("github_inbound_admission_changed"); + } + }, + }; +} + +function steeringReceipt(payload: Record | null): SteeringReceipt | null { + const value = payload?.githubSteering as Partial | undefined; + return value && typeof value.runId === "string" && isUuidLike(value.runId) && + typeof value.identityId === "string" && isUuidLike(value.identityId) && typeof value.commentUpdatedAt === "string" && + typeof value.commentDigest === "string" && /^[a-f0-9]{64}$/.test(value.commentDigest) + ? value as SteeringReceipt : null; +} + +async function consumeSteeredWake(db: Db, action: Action, request: DurableChatWakeupRequest, + marker: SteeringReceipt, turnId: string): Promise { + return db.transaction(async tx => { + const [task] = await tx.select({ id: issues.id, executionRunId: issues.executionRunId, + continuationIdentityContextId: issues.continuationIdentityContextId }).from(issues).where(and(eq(issues.id, request.issueId), + eq(issues.companyId, request.companyId))).for("update"); + if (!task) return false; + const [wake] = await tx.select().from(agentWakeupRequests).where(and(eq(agentWakeupRequests.id, request.id), + eq(agentWakeupRequests.companyId, request.companyId))).for("update"); + if (!wake) return false; + assertDurableChatWakeupReceipt(request, wake); + if (wake.status === "coalesced" && wake.runId === marker.runId) return true; + const currentMarker = steeringReceipt(wake.payload); + if (wake.status !== "deferred_issue_execution" || wake.runId || + !queuedCommentIdsFromWakePayload(wake.payload).includes(request.commentId) || + !currentMarker || currentMarker.runId !== marker.runId || currentMarker.identityId !== marker.identityId || + currentMarker.commentUpdatedAt !== marker.commentUpdatedAt || currentMarker.commentDigest !== marker.commentDigest) return false; + const [run] = await tx.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.id, marker.runId), + eq(heartbeatRuns.companyId, request.companyId), eq(heartbeatRuns.agentId, request.agentId))).for("no key update"); + const [identity] = await tx.select().from(runIdentityContexts).where(and(eq(runIdentityContexts.id, marker.identityId), + eq(runIdentityContexts.companyId, request.companyId), eq(runIdentityContexts.runId, marker.runId), + eq(runIdentityContexts.messageId, request.commentId), eq(runIdentityContexts.cause, "steering"), + inArray(runIdentityContexts.status, ["pending", "accepted"]))); + if (!run || !identity || boundRunIssueId(run) !== request.issueId || + run.contextSnapshot?.chatConversationId !== action.conversationId) return false; + await acceptSteeredIdentity(tx, identity); + if ((task.executionRunId && task.executionRunId !== run.id) || + (task.continuationIdentityContextId !== run.activeIdentityContextId && task.continuationIdentityContextId !== identity.id)) await tx.update(issues) + .set({ continuationIdentityContextId: task.continuationIdentityContextId }).where(eq(issues.id, task.id)); + const [comment] = await tx.select({ updatedAt: issueComments.updatedAt, body: issueComments.body }).from(issueComments) + .where(and(eq(issueComments.id, request.commentId), eq(issueComments.companyId, request.companyId))); + if (!comment || comment.updatedAt.toISOString() !== marker.commentUpdatedAt || commentDigest(comment.body) !== marker.commentDigest) return false; + const [active] = await tx.select({ id: heartbeatRuns.activeIdentityContextId }).from(heartbeatRuns).where(eq(heartbeatRuns.id, run.id)); + const ids = [...new Set([...(Array.isArray(run.contextSnapshot?.wakeCommentIds) + ? run.contextSnapshot.wakeCommentIds as string[] : []), request.commentId])]; + const comments = await tx.select({ id: issueComments.id, createdAt: issueComments.createdAt }).from(issueComments).where(and( + eq(issueComments.companyId, request.companyId), eq(issueComments.issueId, request.issueId), inArray(issueComments.id, ids), + )); + const order = new Map(ids.map((id, index) => [id, index])); + comments.sort((left, right) => left.createdAt.getTime() - right.createdAt.getTime() || order.get(left.id)! - order.get(right.id)!); + const context = withQueuedCommentIdsInRunContext(run.contextSnapshot, comments.map(comment => comment.id), [request.id]); + if (active.id !== identity.id) for (const key of ["wakeCommentId", "commentId"]) { + if (run.contextSnapshot && key in run.contextSnapshot) context[key] = run.contextSnapshot[key]; + else delete context[key]; + } + const now = new Date(), result = run.resultJson ?? {}; + await tx.update(heartbeatRuns).set({ + contextSnapshot: context, + resultJson: { ...result, queuedSteeringAcknowledgements: { + ...(result.queuedSteeringAcknowledgements as Record | undefined), + [request.commentId]: { status: "acknowledged", queueId: request.id, turnId, acknowledgedAt: now.toISOString() }, + } }, updatedAt: now, + }).where(eq(heartbeatRuns.id, run.id)); + await tx.update(agentWakeupRequests).set({ status: "coalesced", runId: run.id, finishedAt: now, updatedAt: now }) + .where(eq(agentWakeupRequests.id, wake.id)); + await notifyGitHubReviewChecks(tx, { companyId: request.companyId, endpointId: action.endpointId, issueId: request.issueId }); + await tx.update(chatGitHubReviews).set({ runId: run.id, + state: run.status === "running" ? "running" : run.status === "succeeded" ? "incomplete" : "error", updatedAt: now }).where(and( + eq(chatGitHubReviews.companyId, request.companyId), eq(chatGitHubReviews.endpointId, action.endpointId), + eq(chatGitHubReviews.deliveryId, action.deliveryId!), eq(chatGitHubReviews.state, "queued"), + )); + await notifyChatRunMilestones(tx, { companyId: request.companyId, issueId: request.issueId }); + await logActivity(tx as unknown as Db, { companyId: request.companyId, actorType: "system", actorId: "github-inbound", + action: "issue.queued_comment_steered", entityType: "issue", entityId: request.issueId, + details: { commentId: request.commentId, targetRunId: run.id, turnId, automatic: true } }); + return true; + }); +} + +export async function recoverGitHubSteeredWake(db: Db, action: Action, request?: DurableChatWakeupRequest): Promise { + const [wake] = await db.select().from(agentWakeupRequests).where(and(eq(agentWakeupRequests.id, action.id), + eq(agentWakeupRequests.companyId, action.companyId), eq(agentWakeupRequests.status, "deferred_issue_execution"), isNull(agentWakeupRequests.runId))); + const marker = wake && steeringReceipt(wake.payload); + if (!marker) return false; + const [endpoint] = await db.select({ id: chatEndpoints.id }).from(chatEndpoints).where(and( + eq(chatEndpoints.companyId, action.companyId), eq(chatEndpoints.id, action.endpointId), eq(chatEndpoints.provider, "github"), + )); + if (!endpoint) return false; + const source = request ?? createDurableChatWakeupRequest({ + id: action.id, companyId: action.companyId, agentId: String(action.payload.agentId), issueId: String(action.payload.issueId), + commentId: String(action.payload.commentId), requestedByActorType: action.payload.requestedByActorType as "user" | "system", + requestedByActorId: String(action.payload.requestedByActorId), requestedAt: action.createdAt, authorize: async () => {}, + }); + assertDurableChatWakeupReceipt(source, wake!); + const [identity] = await db.select().from(runIdentityContexts).where(and(eq(runIdentityContexts.id, marker.identityId), + eq(runIdentityContexts.companyId, source.companyId), eq(runIdentityContexts.runId, marker.runId), + eq(runIdentityContexts.messageId, source.commentId), eq(runIdentityContexts.cause, "steering"), + inArray(runIdentityContexts.status, ["pending", "accepted"]))); + if (!identity) return false; + const acknowledgement = await storedSteeringAcknowledgement(db, identity); + if (acknowledgement) return consumeSteeredWake(db, action, source, marker, acknowledgement.turnId); + const [run] = await db.select({ status: heartbeatRuns.status }).from(heartbeatRuns) + .where(and(eq(heartbeatRuns.id, marker.runId), eq(heartbeatRuns.companyId, source.companyId), eq(heartbeatRuns.agentId, source.agentId))); + if (!run || !["running", "queued"].includes(run.status)) await rejectSteeredIdentity(db, identity); + return false; +} + +export async function steerGitHubInboundWake( + db: Db, action: Action, request: DurableChatWakeupRequest, authorize: Authorize, +): Promise { + const [endpoint] = await db.select({ id: chatEndpoints.id }).from(chatEndpoints).where(and( + eq(chatEndpoints.companyId, action.companyId), eq(chatEndpoints.id, action.endpointId), eq(chatEndpoints.provider, "github"), + )); + if (!endpoint || await recoverGitHubSteeredWake(db, action, request)) return; + const candidate = await db.transaction(async tx => { + const [wake] = await tx.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, request.id)); + if (!wake || wake.status !== "deferred_issue_execution" || wake.runId || wake.payload?.githubSteering) return null; + assertDurableChatWakeupReceipt(request, wake); + const context = wake.payload?._paperclipWakeContext as Record | undefined; + if (context?.forceFreshSession || wake.payload?.executionWait || !queuedCommentIdsFromWakePayload(wake.payload).includes(request.commentId)) return null; + if (Array.isArray(context?.externalAttachmentOmissions) && context.externalAttachmentOmissions.some(entry => + entry?.commentId === request.commentId && entry.reasons && typeof entry.reasons === "object" && + Object.values(entry.reasons).some(count => typeof count === "number" && count > 0))) return null; + const current = await authorize(tx, action); + if (!current.issue.executionRunId) return null; + const [run] = await tx.select({ id: heartbeatRuns.id, status: heartbeatRuns.status, runtimeMode: heartbeatRuns.runtimeMode, + responsibleUserId: heartbeatRuns.responsibleUserId, activeIdentityContextId: heartbeatRuns.activeIdentityContextId, + nativeIssueId: heartbeatRuns.nativeIssueId, issueId: heartbeatRuns.issueId, + contextSnapshot: sql>`jsonb_build_object('issueId', ${heartbeatRuns.contextSnapshot}->'issueId', + 'chatConversationId', ${heartbeatRuns.contextSnapshot}->'chatConversationId', 'wakeCommentId', ${heartbeatRuns.contextSnapshot}->'wakeCommentId')`, + }).from(heartbeatRuns).where(and(eq(heartbeatRuns.id, current.issue.executionRunId), + eq(heartbeatRuns.companyId, request.companyId), eq(heartbeatRuns.agentId, request.agentId))); + if (!run || run.status !== "running" || run.runtimeMode !== "native" || !run.activeIdentityContextId || + boundRunIssueId(run) !== request.issueId || run.contextSnapshot.chatConversationId !== current.conversation.id) return null; + const [comment] = await tx.select().from(issueComments).where(and(eq(issueComments.id, request.commentId), + eq(issueComments.companyId, request.companyId), eq(issueComments.issueId, request.issueId), isNull(issueComments.deletedAt))); + const [origin] = await tx.select({ sourceTrust: issueComments.sourceTrust }).from(issueComments).where(and( + eq(issueComments.id, String(run.contextSnapshot.wakeCommentId)), eq(issueComments.companyId, request.companyId), eq(issueComments.issueId, request.issueId), + )); + const [attachment] = await tx.select({ id: issueAttachments.id }).from(issueAttachments) + .where(and(eq(issueAttachments.companyId, request.companyId), eq(issueAttachments.issueCommentId, request.commentId))).limit(1); + if (!comment || comment.authorUserId !== run.responsibleUserId || !origin || attachment || + isLowTrustQuarantined(comment.sourceTrust) !== isLowTrustQuarantined(origin.sourceTrust)) return null; + const instructions = await resolveGitHubReviewInstructions({ db: tx as unknown as Db, + companyId: request.companyId, agentId: request.agentId, issueId: request.issueId, commentIds: [request.commentId] }); + return { run, comment, message: [...instructions.map(entry => entry.instructions), + `GitHub update (comment ${comment.id}):\n${comment.body}`].join("\n\n") }; + }); + if (!candidate) return; + const identity = await reserveSteeredIdentity(db, { companyId: request.companyId, runId: candidate.run.id, + issueId: request.issueId, messageId: request.commentId }); + if (!identity) return; + let deliveryAttempted = false; + try { + const marker = await db.transaction(async tx => { + const current = await authorize(tx, action); + if (current.issue.executionRunId !== candidate.run.id) return null; + const [wake] = await tx.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, request.id)).for("update"); + if (!wake || wake.status !== "deferred_issue_execution" || wake.runId || wake.payload?.githubSteering) return null; + assertDurableChatWakeupReceipt(request, wake); + if (!queuedCommentIdsFromWakePayload(wake.payload).includes(request.commentId)) return null; + const [comment] = await tx.select({ updatedAt: issueComments.updatedAt }).from(issueComments).where(eq(issueComments.id, request.commentId)); + const [run] = await tx.select({ status: heartbeatRuns.status, responsibleUserId: heartbeatRuns.responsibleUserId }) + .from(heartbeatRuns).where(eq(heartbeatRuns.id, candidate.run.id)); + if (comment?.updatedAt.getTime() !== candidate.comment.updatedAt.getTime() || + run?.status !== "running" || run.responsibleUserId !== candidate.comment.authorUserId) return null; + const marker: SteeringReceipt = { runId: candidate.run.id, identityId: identity.id, + commentUpdatedAt: candidate.comment.updatedAt.toISOString(), commentDigest: commentDigest(candidate.comment.body) }; + await tx.update(agentWakeupRequests).set({ payload: { ...wake.payload, githubSteering: marker }, updatedAt: new Date() }) + .where(eq(agentWakeupRequests.id, wake.id)); + return marker; + }); + if (!marker) { await rejectSteeredIdentity(db, identity); return; } + deliveryAttempted = true; + const acknowledgement = await deliverSteeredMessage(db, { companyId: request.companyId, runId: candidate.run.id, + messageId: request.commentId, message: candidate.message, identity, + onAcknowledged: async () => { await recoverGitHubSteeredWake(db, action, request); } }); + await consumeSteeredWake(db, action, request, marker, acknowledgement.turnId); + } catch (error) { + if (!steeringDeliveryIsUncertain(error, deliveryAttempted)) await rejectSteeredIdentity(db, identity); + if (error instanceof NativeSessionSteeringError) return; + throw error; + } +} diff --git a/server/src/services/chat-channels/inbound/wakeups.ts b/server/src/services/chat-channels/inbound/wakeups.ts index 7b107b8debf..e5dfe6e5a6e 100644 --- a/server/src/services/chat-channels/inbound/wakeups.ts +++ b/server/src/services/chat-channels/inbound/wakeups.ts @@ -15,6 +15,7 @@ import { forbidden } from "../../../errors.js"; import { logger } from "../../../middleware/logger.js"; import { queueIssueAssignmentWakeup } from "../../issue-assignment-wakeup.js"; import { isGitHubAutomaticDelivery, acknowledgeGitHubWakeup } from "../github/wake-authority.js"; +import { githubInboundWakeAdmission, recoverGitHubSteeredWake, steerGitHubInboundWake } from "../github/inbound-steering.js"; import { inboundWakeScope, type createInboundWakeAuthority } from "./wakeup-authorization.js"; import type { createInboundAttachments } from "../inbound-attachments.js"; import type { IssueAssignmentWakeupDeps } from "../../issue-assignment-wakeup.js"; @@ -306,6 +307,7 @@ export function createInboundWakeups(db: Db, options: WakeupOptions) { }; let request: ReturnType | null = null; + let githubAdmission: Awaited> | undefined; const receipt = async () => { const row = await db .select() @@ -344,12 +346,17 @@ export function createInboundWakeups(db: Db, options: WakeupOptions) { requestedAt: claimed.createdAt, authorize: async (tx) => { const current = await authorizeInboundWorkStart(tx, claimed); + await githubAdmission?.authorize(tx, current.issue); if (current.delivery.state !== "processed") throw new Error("chat_inbound_wakeup_acceptance_not_committed"); }, }); - const priorReceipt = await receipt(); + let priorReceipt = await receipt(); if (priorReceipt) { + if (priorReceipt.status === "deferred_issue_execution") { + await steerGitHubInboundWake(db, claimed, request, authorizeInboundWorkStart); + priorReceipt = (await receipt())!; + } await settle(receiptDeclined(priorReceipt) ? "failed" : "processed", { code: receiptDeclined(priorReceipt) ? `inbound_wakeup_${priorReceipt.status}` @@ -366,6 +373,7 @@ export function createInboundWakeups(db: Db, options: WakeupOptions) { ); if (context.delivery.state !== "processed") throw new Error("chat_inbound_wakeup_acceptance_not_committed"); + if (context.endpoint.provider === "github") githubAdmission = await githubInboundWakeAdmission(db, context.issue); await queueIssueAssignmentWakeup({ heartbeat: options.heartbeat, issue: context.issue, @@ -377,12 +385,15 @@ export function createInboundWakeups(db: Db, options: WakeupOptions) { taskKey: context.issue.identifier, wakeCommentId: request.commentId, chatConversationId: context.conversation.id, + ...githubAdmission?.options, attachmentOmissionReasons: claimed.payload.attachmentOmissionReasons as | Record | undefined, durableChatRequest: request, rethrowOnError: true, }); + if (context.endpoint.provider === "github") + await steerGitHubInboundWake(db, claimed, request, authorizeInboundWorkStart); const durable = await receipt(); if (!durable) throw new Error("chat_inbound_wakeup_receipt_missing"); await settle(receiptDeclined(durable) ? "failed" : "processed", { @@ -479,6 +490,7 @@ export function createInboundWakeups(db: Db, options: WakeupOptions) { if (["preparing", "issued", "processing"].includes(source.status)) return sweep ? { retry: "source_pending" } : undefined; if (source.status !== "processed" || source.payload.agentId !== scope.agentId || source.payload.issueId !== scope.issueId) return sweep ? { settle: "source_missing" } : undefined; + if (await recoverGitHubSteeredWake(db, source)) return; let closed = false; if (sweep) { const [task] = await db.select({ status: issues.status, assigneeAgentId: issues.assigneeAgentId }).from(issues) @@ -489,8 +501,12 @@ export function createInboundWakeups(db: Db, options: WakeupOptions) { closed = task.status === "done" || task.status === "cancelled"; if (closed && receipt.createdAt.getTime() < Date.now() - CLOSED_TASK_CHAT_WAKE_REOPEN_WINDOW_MS) return { settle: "task_closed" }; } - const candidates = closed - ? (await recentClosedTaskChatWakes(scope, source)).filter(row => row.receipt.id !== receipt.id) : []; + const candidates: Awaited> = []; + if (closed) { + for (const candidate of await recentClosedTaskChatWakes(scope, source)) { + if (candidate.receipt.id !== receipt.id && !(await recoverGitHubSteeredWake(db, candidate.action))) candidates.push(candidate); + } + } const authorizeAction = async (tx: Db, saved: typeof chatActions.$inferSelect) => { const [current] = await tx.select().from(chatActions).where(and( eq(chatActions.id, saved.id), eq(chatActions.companyId, scope.companyId), @@ -518,6 +534,7 @@ export function createInboundWakeups(db: Db, options: WakeupOptions) { left.receipt.createdAt.getTime() - right.receipt.createdAt.getTime() || left.receipt.id.localeCompare(right.receipt.id)); const { receipt: primaryReceipt, action } = batch.at(-1)!; const riders = batch.slice(0, -1); + const githubAdmission = context.endpoint.provider === "github" ? await githubInboundWakeAdmission(db, context.issue) : undefined; const request = createDurableChatWakeupRequest({ id: action.id, companyId: scope.companyId, agentId: scope.agentId, issueId: scope.issueId, commentId: String(action.payload.commentId), @@ -525,7 +542,8 @@ export function createInboundWakeups(db: Db, options: WakeupOptions) { requestedByActorId: String(action.payload.requestedByActorId), requestedAt: action.createdAt, resumeDeferred: true, authorize: async tx => { - await authorizeAction(tx, action); + const authorized = await authorizeAction(tx, action); + await githubAdmission?.authorize(tx, authorized.issue); if (!riders.length) return; const current = await tx.select({ status: agentWakeupRequests.status, runId: agentWakeupRequests.runId }) .from(agentWakeupRequests).where(and(eq(agentWakeupRequests.companyId, scope.companyId), @@ -549,6 +567,7 @@ export function createInboundWakeups(db: Db, options: WakeupOptions) { contextSource: `chat:${context.endpoint.provider}`, taskKey: context.issue.identifier, requestedByActorType: request.requestedByActorType, requestedByActorId: request.requestedByActorId, wakeCommentId: request.commentId, chatConversationId: context.conversation.id, + ...githubAdmission?.options, ...(riders.length ? { wakeCommentIds: batch.map(entry => String(entry.action.payload.commentId)) } : {}), attachmentOmissionReasons: action.payload.attachmentOmissionReasons as Record | undefined, durableChatRequest: request, rethrowOnError: true }); diff --git a/server/src/services/issue-assignment-wakeup.ts b/server/src/services/issue-assignment-wakeup.ts index f8f8e3375eb..c94aaef77bf 100644 --- a/server/src/services/issue-assignment-wakeup.ts +++ b/server/src/services/issue-assignment-wakeup.ts @@ -43,6 +43,7 @@ export function queueIssueAssignmentWakeup(input: { /** External chat conversation the wake answers. The wake queue keeps each * conversation in its own batch, because a reviewed chat run attests one. */ chatConversationId?: string | null; + allowRunCoalescing?: boolean; rethrowOnError?: boolean; durableChatRequest?: DurableChatWakeupRequest; }) { @@ -61,6 +62,7 @@ export function queueIssueAssignmentWakeup(input: { }, requestedByActorType: input.requestedByActorType, requestedByActorId: input.requestedByActorId ?? null, + ...(input.allowRunCoalescing !== undefined ? { allowRunCoalescing: input.allowRunCoalescing } : {}), ...(input.requestedByActorCompanyId ? { requestedByActorCompanyId: input.requestedByActorCompanyId } : {}), ...(input.durableChatRequest ? { durableChatRequest: input.durableChatRequest } diff --git a/server/src/services/queued-comment-steering.ts b/server/src/services/queued-comment-steering.ts new file mode 100644 index 00000000000..b8bb5374ee5 --- /dev/null +++ b/server/src/services/queued-comment-steering.ts @@ -0,0 +1,24 @@ +import type { Db } from "@paperclipai/db"; +import { reconcileSteeredIdentity, storedSteeringAcknowledgement, type RunIdentityContext } from "./run-identity.js"; +import { NativeSessionSteeringError, steerNativeSession } from "./native-runtime/native-session-executor.js"; + +export function steeringDeliveryIsUncertain(error: unknown, attempted: boolean): boolean { + return attempted && (!(error instanceof NativeSessionSteeringError) || error.code === "steering_timeout"); +} + +export async function deliverSteeredMessage(db: Db, input: { + executor?: Pick; + companyId: string; + runId: string; + messageId: string; + message: string; + identity: RunIdentityContext | null; + onAcknowledged?: () => Promise; +}) { + return await storedSteeringAcknowledgement(input.executor ?? db, input.identity ?? { + companyId: input.companyId, runId: input.runId, messageId: input.messageId, + }) ?? await steerNativeSession({ + runId: input.runId, correlationId: input.messageId, message: input.message, + onAcknowledged: input.onAcknowledged ?? (input.identity ? () => reconcileSteeredIdentity(db, input.identity!) : undefined), + }); +}