mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-21 08:22:20 +02:00
## Thinking Path > - Paperclip manages agent work and the tools each agent can use. > - GitHub bots need their own App identity and restricted repository access. > - Manual App configuration and repeated setup gates make that connection hard to finish. > - GitHub can create an App from a manifest, but people must approve creation and repository access. > - Each Paperclip stack should own configuration, credentials, signature verification, and task admission. > - This change uses Cloud as an encrypted callback and event gateway, then completes setup in the stack. > - GitHub review bots have their own experimental setting. Chat connector opt-in does not control them. ## Linked Issues or Issue Description **What existing behavior does this improve?** Dedicated GitHub App setup, bot settings, and task-scoped GitHub replies. **Current behavior** Users must configure an App, copy credentials, repeat repository selection, and complete manual verification gates. Review settings are hard to scan. Mentions in descriptions can be missed, and automatic completion text can produce an extra GitHub comment. **Proposed behavior** Choose an agent and connect GitHub under a personal account or organization. GitHub asks for creation and installation consent. Paperclip resumes the same draft, stores secrets locally, imports access, verifies delivery, and opens Settings. Each accepted request has one editable App-owned comment for progress and results. Review assessments produce deterministic checks for the reviewed commit. **Breaking changes** GitHub bots require their own default-off experimental opt-in. The setup-method and delegated-agent screens are removed. Per-person automatic-run flags remain readable but are replaced by author filters. Existing connections, direct webhooks, saved restrictions, and manual existing-App recovery remain supported. Companion gateway: https://github.com/paperclipai/paperclip-cloud/pull/688 (merged). The roadmap and related PRs were checked; no duplicate dedicated-App wizard was found. ## What Changed - Add an independent **GitHub review bots** setting across the catalog, routes, identity admission, and task controls. Existing bots continue running when the UI setting is off. - Replace setup with Choose agent and Connect GitHub. Support personal and organization owners, inline low-trust conversion, saved drafts, known organization suggestions, automatic completion, and same-App recovery. - Keep manifests, OAuth PKCE, credential exchange, vault storage, installation verification, and original webhook-signature validation in the stack. Use the Cloud sealed gateway for public callbacks and outbound local delivery. - Add resumable owner metadata in migration 0323. Retry a failed registration renewal through the same durable replacement rather than creating another App route. - Support authorized mentions in issue and PR bodies, comments, inline comments, and review summaries. Preserve attachments, edited-source identity, duplicate protection, and lifecycle invalidation. Filter requests explicitly addressed to another connected bot. - Give agents task-scoped review, comment, and update tools. Update one working comment through progress and final output. Close coalesced requests and recover delayed acknowledgements, including deleted earlier working comments. Preserve automatic replies for legacy connections without saved review configuration. Preserve partial publication evidence and quarantine uncertain automatic fallbacks without blocking Board messages or questions. - Reorganize Settings and Access with rich slash-skill instructions, numeric passing scores, clear automation controls, formal-review tooltips, member and contributor sections, and verified App branding links. Preserve invalid score drafts and automation choices across tabs. - Paginate repositories in 20-row batches with search and a true connection-wide toggle. Batch verification at GitHub's 500-ID token limit. Keep saved repository overrides while hiding their editor. - Give each review a detail URL that remains usable beyond the newest 100 records. Synchronize the manager OpenAPI contracts and add recovery, authorization, UI, and browser regressions. - Reconcile the current master branch and retain its agent avatar, feature catalog, and extracted heartbeat preparation code. Preserve a saved test-drive port and recover from an occupied high port. ## Verification - Focused UI, independent experimental-setting, and CLI tests: 199 passed. - `pnpm -r typecheck`, `pnpm build`, and `pnpm check:token-gates` passed. The broad local Vitest attempt was interrupted after resource timeouts and stale expectations; the affected regressions passed focused reruns. The latest-head full CI matrix passed, including all browser shards, server/chat suites, builds, typechecking, canary packaging, and Docker context integrity. A Docker Hub 429 required a CI retry; it passed without changing the code. - Final review repairs: 127 attachment/event tests and server typecheck passed. All 13 final integration regressions passed under the normal CI timeout, covering legacy reply compatibility, deleted coalesced comments, inline-only review completion, progress edits, and native final recovery. Restart-safe description/review attachments passed the attachment suite. - Final regressions: four reply/lifecycle tests, both native GitHub wait tests, 171 feature-gate/reply-contract tests, the routed review preview, and five scheduler/reply regressions passed. The final fixture repair also passed all six setup-continuation cases and all three complete chat-test shards in CI, covering terminal receipts and retries under the normal CI timeout. One loaded-host receipt run exceeded its local timeout; the exact case passed in an isolated rerun and the complete CI shard. - GitHub integration tests cover personal and organization setup, retry recovery, edited mentions, attachment provenance, coalesced requests, delayed acknowledgements, partial review writes, old review URLs, and company boundaries. - Browser qualification: both GitHub journeys, all five provider setup/management journeys, and the GitHub uncertain-send recovery journey passed. GitHub setup, reconnect, and task replies run with chat disabled; chat enabled alone cannot expose GitHub bots. The disposable qualification database was removed after testing. - Earlier live qualification verified App-owned issue replies, native slash-skill invocation, progress comment edits, and a 5/5 PR review with a passing current-head check in the dedicated permissions-smoke repository. These live checks did not use personal credentials as bot execution authority. ## Risks - Deploy the merged Cloud gateway protocol before the stack update. Each callback and encrypted event must retain the original instance and tenant binding. - Migration 0323 uses idempotent column additions for test installations that already applied the earlier draft migration number. - A verified connection does not prove runtime isolation or a completed review. Runtime checks remain separate. - Required merge checks must be configured in GitHub. A passing numeric score does not formally approve a PR; approval and request-changes tools remain opt-in. - Existing-App credentials cannot silently renew an uncertain manifest exchange. Recovery preserves the existing App, draft, connection, and agent. - Repository changes save immediately. Global toggles affect all currently available repositories, including unloaded rows, without granting new installation access. - A failed or uncertain tool write suppresses automatic fallback text; the governed outbox retains its evidence. Deleted or replaced response comments are not recreated through a personal credential fallback. - GitHub logo upload remains an optional human action in GitHub App settings. Runtime provisioning, delegated setup, screenshot publication, and production push delivery remain deferred. ## Model Used OpenAI Codex, based on GPT-6, with tool use and code execution. The exact runtime model identifier and context size are not exposed in this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub references) - [x] My branch name describes the change and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass (focused suites; complete test matrix is checked in CI) - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
297 lines
21 KiB
TypeScript
297 lines
21 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { and, asc, desc, eq, inArray, or, sql } from "drizzle-orm";
|
|
import {
|
|
chatActions, chatConversations, chatDeliveries, chatEndpoints,
|
|
chatEndpointResources, chatGitHubConfigurations, chatMessageLinks, heartbeatRuns, issues,
|
|
type Db,
|
|
} from "@paperclipai/db";
|
|
import { conflict, forbidden, HttpError } from "../errors.js";
|
|
import { githubChatPrincipalAccess } from "./chat-github-access.js";
|
|
import { githubAutomaticAdmission } from "./chat-github-events.js";
|
|
import { githubBotRepositoryToken, githubBotRequest } from "./chat-github-client.js";
|
|
import { withGitHubPublicationLease } from "./chat-github-publication-lease.js";
|
|
import { projectSafeChatPublicationText } from "./chat-publication-projection.js";
|
|
import type { GitHubAutomaticEventContext } from "@paperclipai/shared";
|
|
import { githubRunReplyState } from "./chat-run-publications.js";
|
|
import { logActivity } from "./activity-log.js";
|
|
|
|
type Transaction = Parameters<Parameters<Db["transaction"]>[0]>[0];
|
|
type Api = { request<T>(path: string, options?: { method?: string; body?: unknown }): Promise<T> };
|
|
type Lease = { commit<T>(write: (tx: Transaction) => Promise<T>): Promise<T> };
|
|
type Source = {
|
|
endpoint: typeof chatEndpoints.$inferSelect;
|
|
conversation: typeof chatConversations.$inferSelect;
|
|
delivery: typeof chatDeliveries.$inferSelect;
|
|
number: number;
|
|
replyId: string | null;
|
|
};
|
|
type Comment = { id: number; body?: string; html_url: string; user?: { login?: string } };
|
|
const generation = (endpoint: Source["endpoint"]) => (endpoint.setup as { runtimeGeneration?: number }).runtimeGeneration ?? 0;
|
|
const actionKey = (deliveryId: string) => `github_response:${deliveryId}`;
|
|
const responseMarker = (source: Source) => `<!-- paperclip-response:${createHash("sha256")
|
|
.update(`${source.endpoint.companyId}:${source.endpoint.id}:${source.delivery.id}`).digest("hex")} -->`;
|
|
|
|
async function findResponseComment(source: Source, api: Api): Promise<Comment | null> {
|
|
const list = source.replyId ? `/pulls/${source.number}/comments` : `/issues/${source.number}/comments`;
|
|
for (let page = 1; page <= 100; page++) {
|
|
const rows = await api.request<Comment[]>(`${list}?per_page=100&page=${page}`);
|
|
const found = rows.find(row => row.body?.includes(responseMarker(source)) && row.user?.login === source.endpoint.botUsername);
|
|
if (found) return found;
|
|
if (rows.length < 100) return null;
|
|
}
|
|
throw conflict("GitHub history is too large to safely recover the response");
|
|
}
|
|
|
|
/** One durable, App-owned comment per accepted request. Callers hold the same
|
|
* repository publication lease used by agent replies and review summaries. */
|
|
async function stageResponse(db: Db, source: Source) {
|
|
const [inserted] = await db.insert(chatActions).values({
|
|
companyId: source.endpoint.companyId, endpointId: source.endpoint.id,
|
|
conversationId: source.conversation.id, deliveryId: source.delivery.id,
|
|
principalId: source.delivery.principalId, kind: "github_response_comment",
|
|
providerActionId: actionKey(source.delivery.id),
|
|
payload: { runtimeGeneration: generation(source.endpoint), agentId: source.endpoint.assignedAgentId },
|
|
status: "received",
|
|
}).onConflictDoNothing().returning();
|
|
const response = inserted ?? (await db.select().from(chatActions).where(and(
|
|
eq(chatActions.companyId, source.endpoint.companyId), eq(chatActions.endpointId, source.endpoint.id),
|
|
eq(chatActions.providerActionId, actionKey(source.delivery.id)),
|
|
)))[0];
|
|
if (!response || response.conversationId !== source.conversation.id ||
|
|
response.payload.runtimeGeneration !== generation(source.endpoint) ||
|
|
response.payload.agentId !== source.endpoint.assignedAgentId)
|
|
throw forbidden("GitHub response binding changed");
|
|
return response;
|
|
}
|
|
|
|
export async function writeGitHubResponseComment(
|
|
db: Db, source: Source, api: Api, lease: Lease,
|
|
input: { body: string; final: boolean; versionAt?: Date; acknowledgement?: boolean },
|
|
assertCurrent: () => Promise<void>,
|
|
) {
|
|
const response = await stageResponse(db, source);
|
|
// A delayed acknowledgement or progress retry must never replace a result.
|
|
if (response.result?.id && (input.acknowledgement || (response.result.final === true && !input.final) ||
|
|
(input.versionAt && Date.parse(String(response.result.versionAt)) > input.versionAt.getTime()))) {
|
|
if (!input.acknowledgement && response.result.final === true && !input.final)
|
|
throw conflict("This GitHub response is already final");
|
|
return { id: String(response.result.id), url: String(response.result.url) };
|
|
}
|
|
const marker = responseMarker(source);
|
|
const list = source.replyId ? `/pulls/${source.number}/comments` : `/issues/${source.number}/comments`;
|
|
const item = source.replyId ? "/pulls/comments" : "/issues/comments";
|
|
await assertCurrent();
|
|
let previous: Comment | null = null;
|
|
if (response.result?.id) {
|
|
// Never recreate a deleted comment or edit an arbitrary caller-selected ID.
|
|
if (!/^[1-9][0-9]*$/.test(String(response.result.id))) throw forbidden("Invalid GitHub response receipt");
|
|
previous = await api.request<Comment>(`${item}/${response.result.id}`);
|
|
if (!previous.body?.includes(marker) || previous.user?.login !== source.endpoint.botUsername)
|
|
throw forbidden("GitHub response is no longer owned by this App");
|
|
} else {
|
|
previous = await findResponseComment(source, api);
|
|
}
|
|
await assertCurrent();
|
|
const alreadyFinal = previous?.body?.includes("<!-- paperclip-response-state:final -->") === true;
|
|
const body = `${projectSafeChatPublicationText(input.body)}\n\n${marker}${input.final ? "\n<!-- paperclip-response-state:final -->" : ""}`;
|
|
const posted = previous && (input.acknowledgement || (alreadyFinal && !input.final)) ? previous : await api.request<Comment>(
|
|
previous ? `${item}/${previous.id}` : source.replyId
|
|
? `/pulls/${source.number}/comments/${source.replyId}/replies` : list,
|
|
{ method: previous ? "PATCH" : "POST", body: { body } },
|
|
);
|
|
if (!Number.isSafeInteger(posted.id) || posted.id <= 0 || !posted.html_url)
|
|
throw conflict("GitHub response delivery was not confirmed");
|
|
await assertCurrent();
|
|
const receipt = { id: String(posted.id), url: posted.html_url };
|
|
await lease.commit(async tx => {
|
|
await tx.update(chatActions).set({ status: "processed", result: {
|
|
...receipt, final: input.final || alreadyFinal, versionAt: (input.versionAt ?? response.createdAt).toISOString(),
|
|
}, updatedAt: new Date() }).where(eq(chatActions.id, response.id));
|
|
await logActivity(tx as unknown as Db, { companyId: source.endpoint.companyId,
|
|
actorType: "system", actorId: "github-response", action: "chat_github.response_updated",
|
|
entityType: "chat_endpoint", entityId: source.endpoint.id,
|
|
details: { deliveryId: source.delivery.id, final: input.final, ...receipt } });
|
|
});
|
|
return receipt;
|
|
}
|
|
|
|
export function githubResponseCommentService(db: Db, fetchImpl = fetch) {
|
|
async function context(deliveryId: string) {
|
|
const [row] = await db.select({ endpoint: chatEndpoints, conversation: chatConversations,
|
|
delivery: chatDeliveries, resource: chatEndpointResources, config: chatGitHubConfigurations,
|
|
assignee: issues.assigneeAgentId }).from(chatMessageLinks)
|
|
.innerJoin(chatDeliveries, and(eq(chatDeliveries.id, chatMessageLinks.deliveryId), eq(chatDeliveries.companyId, chatMessageLinks.companyId)))
|
|
.innerJoin(chatConversations, and(eq(chatConversations.id, chatMessageLinks.conversationId), eq(chatConversations.companyId, chatMessageLinks.companyId)))
|
|
.innerJoin(chatEndpoints, and(eq(chatEndpoints.id, chatConversations.endpointId), eq(chatEndpoints.companyId, chatConversations.companyId)))
|
|
.innerJoin(chatEndpointResources, eq(chatEndpointResources.id, chatConversations.resourceId))
|
|
.innerJoin(chatGitHubConfigurations, eq(chatGitHubConfigurations.endpointId, chatEndpoints.id))
|
|
.innerJoin(issues, and(eq(issues.id, chatConversations.issueId), eq(issues.companyId, chatEndpoints.companyId)))
|
|
.where(and(eq(chatMessageLinks.deliveryId, deliveryId), eq(chatMessageLinks.direction, "inbound"), eq(chatEndpoints.provider, "github")));
|
|
if (!row || !row.delivery.principalId || !["processing", "processed"].includes(row.delivery.state) ||
|
|
!["active", "verifying"].includes(row.endpoint.status) || !row.config.configuration.toolsEnabled ||
|
|
!row.resource.enabled || row.resource.availability !== "available" || row.assignee !== row.endpoint.assignedAgentId)
|
|
throw forbidden("GitHub request is no longer authorized");
|
|
const access = await githubChatPrincipalAccess(db, row.endpoint, row.delivery.principalId);
|
|
if (!access?.allowed) throw forbidden("The initiating GitHub person is no longer authorized");
|
|
const automatic = (row.delivery.normalizedEvent.githubAutomatic ?? row.delivery.normalizedEvent.githubIssue) as
|
|
{ context: GitHubAutomaticEventContext } | undefined;
|
|
if (automatic && !(await githubAutomaticAdmission(db, row.endpoint, automatic.context))?.allowed)
|
|
throw forbidden("Automatic GitHub task authority changed");
|
|
const match = /^github:([^:]+):(?:issue:)?([1-9][0-9]*)(?::rc:([1-9][0-9]*))?$/.exec(row.conversation.externalThreadId);
|
|
const repositoryId = String(row.resource.metadata.providerRepositoryId ?? "");
|
|
if (!match || match[1]!.toLowerCase() !== row.resource.providerResourceId.toLowerCase() || !/^[1-9][0-9]*$/.test(repositoryId))
|
|
throw forbidden("Invalid GitHub request repository");
|
|
return { ...row, repositoryId, repository: match[1]!, number: Number(match[2]), replyId: match[3] ?? null };
|
|
}
|
|
async function acknowledge(deliveryId: string, finalBody?: string, assertRun?: () => Promise<void>) {
|
|
const source = await context(deliveryId);
|
|
// Persist before competing for the publication lane. A busy lane leaves
|
|
// durable acknowledgement work for maintenance instead of losing it.
|
|
await stageResponse(db, source);
|
|
return withGitHubPublicationLease(db, { companyId: source.endpoint.companyId, endpointId: source.endpoint.id,
|
|
repositoryId: source.repositoryId, number: source.number }, fetchImpl, async lease => {
|
|
const assertCurrent = async () => {
|
|
await assertRun?.();
|
|
const current = await context(deliveryId);
|
|
if (generation(current.endpoint) !== generation(source.endpoint) || current.endpoint.assignedAgentId !== source.endpoint.assignedAgentId)
|
|
throw forbidden("GitHub response binding changed");
|
|
};
|
|
await assertCurrent();
|
|
let onlyExisting = false;
|
|
if (finalBody === undefined) {
|
|
const existing = await stageResponse(db, source);
|
|
const [run] = await db.select().from(heartbeatRuns).innerJoin(chatMessageLinks, and(
|
|
eq(chatMessageLinks.companyId, source.endpoint.companyId), eq(chatMessageLinks.endpointId, source.endpoint.id),
|
|
eq(chatMessageLinks.deliveryId, deliveryId), eq(chatMessageLinks.direction, "inbound"),
|
|
or(sql`${chatMessageLinks.commentId}::text = ${heartbeatRuns.contextSnapshot}->>'wakeCommentId'`,
|
|
sql`coalesce(${heartbeatRuns.contextSnapshot}->'wakeCommentIds', '[]'::jsonb) ? ${chatMessageLinks.commentId}::text`),
|
|
)).where(and(eq(heartbeatRuns.companyId, source.endpoint.companyId), eq(heartbeatRuns.agentId, source.endpoint.assignedAgentId),
|
|
sql`coalesce(${heartbeatRuns.nativeIssueId}::text, ${heartbeatRuns.contextSnapshot}->>'issueId') = ${source.conversation.issueId}`))
|
|
.orderBy(desc(heartbeatRuns.createdAt)).limit(1);
|
|
if (run && ["succeeded", "failed", "interrupted", "timed_out", "cancelled"].includes(run.heartbeat_runs.status)) {
|
|
const reply = await githubRunReplyState(db, { companyId: source.endpoint.companyId, endpointId: source.endpoint.id,
|
|
issueId: source.conversation.issueId!, runId: run.heartbeat_runs.id });
|
|
if (reply === "unsettled") throw conflict("GitHub reply is still being resolved");
|
|
onlyExisting = reply === "confirmed" && !existing.result?.id;
|
|
finalBody = reply === "confirmed" ? "This request has been handled. See the response in this conversation."
|
|
: run.heartbeat_runs.status === "succeeded" ? "This turn ended without publishing a final response. See the Paperclip task for details."
|
|
: "This turn stopped before completing. See the Paperclip task for details.";
|
|
} else if (existing.result?.id) return { id: String(existing.result.id), url: String(existing.result.url) };
|
|
}
|
|
const token = await githubBotRepositoryToken(db, source.endpoint.companyId, source.endpoint.id, source.repositoryId, lease.fetch);
|
|
const prefix = `/repos/${source.repository.split("/").map(encodeURIComponent).join("/")}`;
|
|
const api: Api = { request: (path, options) => githubBotRequest(lease.fetch, token, `${prefix}${path}`, options) };
|
|
if (onlyExisting && !(await findResponseComment(source, api))) {
|
|
const existing = await stageResponse(db, source);
|
|
await lease.commit(async tx => { await tx.update(chatActions).set({ status: "cancelled", result: { final: true, code: "run_already_answered" }, updatedAt: new Date() }).where(eq(chatActions.id, existing.id)); });
|
|
return;
|
|
}
|
|
return writeGitHubResponseComment(db, source, api, lease,
|
|
{ body: finalBody ?? "Working on this…", final: finalBody !== undefined,
|
|
acknowledgement: finalBody === undefined, ...(finalBody !== undefined ? { versionAt: new Date() } : {}) }, assertCurrent);
|
|
});
|
|
}
|
|
async function requestsForRun(input: { companyId: string; endpointId: string; issueId: string; runId: string }) {
|
|
const [run] = await db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.id, input.runId), eq(heartbeatRuns.companyId, input.companyId)));
|
|
if (!run || (run.nativeIssueId ?? run.contextSnapshot?.issueId) !== input.issueId) throw forbidden("GitHub run has no request binding");
|
|
const ids = [...new Set([run.contextSnapshot?.wakeCommentId, ...(Array.isArray(run.contextSnapshot?.wakeCommentIds) ? run.contextSnapshot.wakeCommentIds : [])].filter((id): id is string => typeof id === "string"))];
|
|
if (!ids.length) throw forbidden("GitHub run has no request binding");
|
|
const links = await db.select({ deliveryId: chatMessageLinks.deliveryId }).from(chatMessageLinks)
|
|
.innerJoin(chatConversations, and(eq(chatConversations.id, chatMessageLinks.conversationId), eq(chatConversations.companyId, input.companyId),
|
|
eq(chatConversations.endpointId, input.endpointId), eq(chatConversations.issueId, input.issueId)))
|
|
.where(and(eq(chatMessageLinks.companyId, input.companyId), eq(chatMessageLinks.endpointId, input.endpointId),
|
|
inArray(chatMessageLinks.commentId, ids), eq(chatMessageLinks.direction, "inbound")))
|
|
.orderBy(asc(chatMessageLinks.createdAt));
|
|
return { run, links };
|
|
}
|
|
async function finishRun(input: { companyId: string; endpointId: string; issueId: string; runId: string; closePrimary?: boolean },
|
|
primary: Source, receipt: { id: string; url: string }, api: Api, lease: Lease, assertCurrent: () => Promise<void>) {
|
|
const { run, links } = await requestsForRun(input);
|
|
if (run.agentId !== primary.endpoint.assignedAgentId) throw forbidden("GitHub response authority changed");
|
|
for (const link of links) {
|
|
if (!link.deliveryId || (link.deliveryId === primary.delivery.id && !input.closePrimary)) continue;
|
|
const sibling = await context(link.deliveryId);
|
|
if (sibling.endpoint.id !== primary.endpoint.id || sibling.number !== primary.number || sibling.replyId !== primary.replyId || sibling.conversation.id !== primary.conversation.id)
|
|
throw forbidden("Coalesced GitHub response belongs to another conversation");
|
|
const [response] = await db.select().from(chatActions).where(and(eq(chatActions.deliveryId, link.deliveryId), eq(chatActions.kind, "github_response_comment")));
|
|
if (!response || response.result?.final === true) continue;
|
|
// Never create a sibling placeholder just to close it. Mark a failed
|
|
// acknowledgement settled so its maintenance retry cannot post later.
|
|
if (!response.result?.id && !(await findResponseComment(sibling, api))) {
|
|
await lease.commit(async tx => { await tx.update(chatActions).set({ status: "cancelled", result: { final: true, code: "run_already_answered" }, updatedAt: new Date() }).where(eq(chatActions.id, response.id)); });
|
|
continue;
|
|
}
|
|
try {
|
|
await writeGitHubResponseComment(db, sibling, api, lease, {
|
|
body: link.deliveryId === primary.delivery.id
|
|
? `Review complete. See the [review finding](${receipt.url}).`
|
|
: `Handled with the [response to this request](${receipt.url}).`, final: true, versionAt: new Date(),
|
|
}, async () => { await assertCurrent(); await context(link.deliveryId!); });
|
|
} catch (error) {
|
|
const details = error instanceof HttpError ? error.details as { code?: string; providerStatus?: number } | undefined : undefined;
|
|
if (!response.result?.id || details?.code !== "github_bot_operation_failed" || details.providerStatus !== 404) throw error;
|
|
// A human can delete an earlier working comment while the coalesced
|
|
// review runs. Settle that exact receipt without recreating the comment
|
|
// or blocking the current response and check publication.
|
|
await assertCurrent();
|
|
await context(link.deliveryId);
|
|
await lease.commit(async tx => { await tx.update(chatActions).set({ status: "cancelled",
|
|
result: { ...response.result, final: true, code: "response_deleted" }, updatedAt: new Date(),
|
|
}).where(and(eq(chatActions.id, response.id), eq(chatActions.companyId, input.companyId),
|
|
eq(chatActions.endpointId, input.endpointId))); });
|
|
}
|
|
}
|
|
}
|
|
async function failRun(input: { companyId: string; endpointId: string; issueId: string; runId: string; body: string }) {
|
|
const { run, links } = await requestsForRun(input);
|
|
let receipt: { id: string; url: string } | undefined;
|
|
for (const link of links) {
|
|
if (!link.deliveryId) continue;
|
|
const assertRun = async () => {
|
|
const source = await context(link.deliveryId!);
|
|
const [current] = await db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.id, run.id), eq(heartbeatRuns.companyId, input.companyId)));
|
|
if (!current || current.agentId !== source.endpoint.assignedAgentId ||
|
|
!["succeeded", "failed", "interrupted", "timed_out", "cancelled"].includes(current.status) ||
|
|
source.endpoint.id !== input.endpointId || source.conversation.issueId !== input.issueId ||
|
|
(current.nativeIssueId ?? current.contextSnapshot?.issueId) !== input.issueId)
|
|
throw forbidden("GitHub failure authority changed");
|
|
if (await githubRunReplyState(db, input) !== "none") throw conflict("GitHub reply is still being resolved");
|
|
};
|
|
receipt = await acknowledge(link.deliveryId, input.body, assertRun);
|
|
}
|
|
if (!receipt) throw forbidden("GitHub failure has no request binding");
|
|
return receipt;
|
|
}
|
|
async function hasWorkingResponseForRun(input: { companyId: string; endpointId: string; issueId: string; runId: string }) {
|
|
const { links } = await requestsForRun(input);
|
|
const ids = links.map(link => link.deliveryId).filter((id): id is string => !!id);
|
|
if (!ids.length) return false;
|
|
const responses = await db.select({ result: chatActions.result }).from(chatActions).where(and(
|
|
eq(chatActions.companyId, input.companyId), eq(chatActions.endpointId, input.endpointId),
|
|
inArray(chatActions.deliveryId, ids), eq(chatActions.kind, "github_response_comment")));
|
|
return responses.some(response => response.result?.final !== true);
|
|
}
|
|
async function processPending(limit = 10) {
|
|
const rows = await db.select().from(chatActions).where(and(eq(chatActions.kind, "github_response_comment"),
|
|
or(eq(chatActions.status, "received"), and(eq(chatActions.status, "failed"), sql`${chatActions.result}->>'retryable' = 'true'`,
|
|
sql`(${chatActions.result}->>'retryAt')::timestamptz <= now()`)))).orderBy(asc(chatActions.createdAt)).limit(limit);
|
|
for (const row of rows) if (row.deliveryId) await tryAcknowledge(row.deliveryId);
|
|
return rows.length;
|
|
}
|
|
async function tryAcknowledge(deliveryId: string) {
|
|
try { return await acknowledge(deliveryId); }
|
|
catch (error) {
|
|
const [row] = await db.select().from(chatActions).where(and(eq(chatActions.kind, "github_response_comment"), eq(chatActions.deliveryId, deliveryId)));
|
|
if (!row || row.result?.id) return;
|
|
const attempts = Number(row.result?.attempts ?? 0) + 1;
|
|
const denied = error instanceof Error && "status" in error && error.status === 403;
|
|
await db.update(chatActions).set({ status: denied ? "cancelled" : "failed", result: {
|
|
attempts, retryable: !denied && attempts < 8,
|
|
retryAt: new Date(Date.now() + Math.min(300000, 1000 * 2 ** attempts)).toISOString(),
|
|
code: denied ? "authorization_changed" : "acknowledgement_unconfirmed",
|
|
}, updatedAt: new Date() }).where(and(eq(chatActions.id, row.id), sql`${chatActions.result}->>'id' is null`));
|
|
}
|
|
}
|
|
return { tryAcknowledge, processPending, failRun, finishRun, hasWorkingResponseForRun };
|
|
}
|