mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-25 16:14:26 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Review agents answer GitHub pull request threads; each request's comment must end in a real reply or an honest final notice > - After #22672 and #22710, 13 of 85 new review-bot comments on staging still ended as status notices, and 10 old placeholders on merged PRs never settled > - Run data on staging shows two causes: runs that yield on purpose to wait for a response (`external_chat_response_waiting`), and runs whose final reply was refused (`responsePublication.state = failed`) > - The first marked inline threads "ended without publishing" while a later run posted the review; the second looked like presentation still pending, forever > - This pull request makes both settle: link the latest response, wait while the task is open, and use the honest notice only when nothing was published > - The benefit is that every request comment ends in a useful final state ## Linked Issues or Issue Description Refs #22672, #22710 **Describe the bug** - On #22732 the author replied on five review threads at once. Each reply woke its own run; those runs yielded with `external_chat_response_waiting`, and a later run posted the review. Four threads still ended with "This turn ended without publishing a final response." - On #22658 (and the other merged PRs with stuck "Working on this…"), the request's run finished `succeeded`/`committed` with no presentation decision because its final reply publication failed after 6 attempts (`publication_refused`). The settle logic treated that as presentation pending and never settled the thread. **Steps to reproduce** 1. An inline request's run yields with a response-wake continuation; a later run publishes a review. 2. Or: an inline request's run finishes with `responsePublication.state = "failed"` and no presentation decision. 3. The thread says "ended without publishing" (1) or stays "Working on this…" forever (2). **Expected behavior** The thread links the latest published response, waits while the task is still open, and otherwise ends with an honest notice. ## What Changed - `githubLatestTaskResponseUrl`: the latest processed GitHub publication (comment, formal review, assessment) for the task after the request arrived. - `githubResponseExecution`: when the request's run did not reply, link that latest response if any. An inline request whose run deliberately waits keeps waiting while the task is open. A run whose reply was refused says so. The main PR comment keeps its passive-wait cancellation (`mainPullResponse`). - `nativePresentationPending` is false when `responsePublication.state = failed`; new `responsePublicationFailed` column. - Terminal sweep decision version is now `v3`, so requests parked under v2 are read again once. ## Verification - New integration tests (red on master, green here): links a waited thread to a later response; settles a waited thread when the task finishes with no response; settles a thread whose run's reply could not be published. - Updated `keeps per-request fallback outside the main GitHub PR review wait`: waits while the task is open, then settles when done. - `keeps native presentation idle until its committed GitHub outcome` (main PR passive) still passes. - `server/src/services/chat-channels/github/` and response comment tests: 67 passed. GitHub integration group: 538 passed before the main-PR scoping fix; the two affected tests pass after it. - After deploy: re-measure the stuck rate on staging for comments created after the deploy. ## Risks - One extra indexed query per request that settles without its own reply (to find the latest response). Settled requests leave the backlog. - A waited inline thread stays "Working on this…" while its task is open and nothing was published yet; that is the honest state. - Separate, not fixed here: why the native finalizer refuses some final replies (`publication_refused`). ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use and code execution. ## 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 `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [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 - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [ ] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
186 lines
13 KiB
TypeScript
186 lines
13 KiB
TypeScript
import { and, asc, eq, gt, isNotNull, sql } from "drizzle-orm";
|
|
import { chatActions, chatConversations, chatDeliveries, chatEndpointLeases, chatEndpointResources,
|
|
chatEndpoints, chatMessageLinks, heartbeatRuns, issues, type Db } from "@paperclipai/db";
|
|
import type { createDeliveryWorkCoordinator } from "../../delivery-work-coordinator.js";
|
|
import { DELIVERY_QUEUES } from "../../delivery-work-notifications.js";
|
|
import { registerScopedDeadlineWork } from "../../scoped-deadline-work.js";
|
|
import { githubResponseExecution } from "./response-execution.js";
|
|
import { readGitHubResponseRuns, type GitHubResponseRunRead } from "./response-run-state.js";
|
|
import { githubResponseReconstructionIntervalMs, type GitHubResponseSweepScope as Scope } from "./response-sweep-scope.js";
|
|
import { HttpError } from "../../../errors.js";
|
|
export const GITHUB_TERMINAL_RESPONSE_DECISION_VERSION = "v3";
|
|
type Entry = { id: string; companyId: string; endpointId: string; deliveryId: string;
|
|
issueId: string; conversationId: string; repositoryId: string | null; number: number | null; dueAt: number };
|
|
|
|
/** A load of a whole company (or every company) rather than a targeted hint. */
|
|
const broad = (scope?: Scope) => !scope || (!scope.issueId && !scope.deliveryId && !scope.conversationId &&
|
|
!scope.endpointId && !scope.repositoryId && !scope.number);
|
|
|
|
export function registerGitHubTerminalResponseWork(
|
|
db: Db,
|
|
coordinator: ReturnType<typeof createDeliveryWorkCoordinator>,
|
|
acknowledge: (deliveryId: string, canContinue: () => boolean) => Promise<unknown>,
|
|
canRun: () => boolean,
|
|
) {
|
|
const repositoryId = sql<string | null>`${chatEndpointResources.metadata}->>'providerRepositoryId'`;
|
|
const number = sql<string | null>`substring(${chatConversations.externalThreadId}
|
|
from '^github:[^:]+:(?:issue:)?([1-9][0-9]*)(?::rc:[1-9][0-9]*)?$')`;
|
|
// The assigned agent's latest run bound to the issue, where a run's issue is
|
|
// coalesce(native_issue_id, context_snapshot->>'issueId'). The two
|
|
// precedence branches are disjoint and each uses an issue index, so a
|
|
// candidate does not walk (and detoast) the agent's whole run history.
|
|
const latestIssueRun = db.select({
|
|
status: sql<string>`latest.status`.as("latest_run_status"),
|
|
version: sql<string>`latest.id::text || ':' || latest.status || ':' || extract(epoch from latest.updated_at)::text`
|
|
.as("latest_run_version"),
|
|
}).from(sql`((select r.status, r.created_at, r.id, r.updated_at from ${heartbeatRuns} r
|
|
where r.company_id = ${chatActions.companyId} and r.native_issue_id = ${chatConversations.issueId}
|
|
and r.agent_id = ${chatEndpoints.assignedAgentId}
|
|
order by r.created_at desc, r.id desc limit 1)
|
|
union all
|
|
(select r.status, r.created_at, r.id, r.updated_at from ${heartbeatRuns} r
|
|
where r.company_id = ${chatActions.companyId} and r.native_issue_id is null
|
|
and r.context_snapshot->>'issueId' = ${chatConversations.issueId}::text
|
|
and r.agent_id = ${chatEndpoints.assignedAgentId}
|
|
order by r.created_at desc, r.id desc limit 1)) latest`)
|
|
.orderBy(sql`latest.created_at desc, latest.id desc`).limit(1).as("latest_issue_run");
|
|
// A request whose latest run does not own it (or whose pause is already
|
|
// shown) can progress only once the task's latest run, the task's status or
|
|
// the request's own row changes. Such a request records this key, and a broad
|
|
// rebuild skips it until one of them changes. A targeted load still reads it.
|
|
const actionVersion = sql<string>`extract(epoch from ${chatActions.updatedAt})::text`;
|
|
const latestPublication = sql<string>`coalesce((select extract(epoch from max(p.updated_at))::text from ${chatActions} p
|
|
where p.company_id = ${chatActions.companyId} and p.endpoint_id = ${chatActions.endpointId}
|
|
and p.kind = 'github_review_publication' and p.status = 'processed'
|
|
and p.payload->'session'->>'issueId' = ${issues.id}::text), 'none')`;
|
|
const waitKey = sql<string>`${GITHUB_TERMINAL_RESPONSE_DECISION_VERSION} || '|' || coalesce(${latestIssueRun.version}, 'none')
|
|
|| '|' || ${issues.status} || '|' || ${actionVersion} || '|' || ${latestPublication}`;
|
|
async function page(scope?: Scope, cursor?: string, actionId?: string) {
|
|
return db.select({
|
|
id: chatActions.id, companyId: chatActions.companyId, endpointId: chatActions.endpointId,
|
|
deliveryId: chatActions.deliveryId, conversationId: chatConversations.id, issueId: issues.id,
|
|
agentId: chatEndpoints.assignedAgentId, issueStatus: issues.status,
|
|
deliveryCreatedAt: chatDeliveries.createdAt, repositoryId, number,
|
|
threadId: chatConversations.externalThreadId, result: chatActions.result, actionVersion, waitKey,
|
|
sourceMessageId: sql<string | null>`(select l.comment_id from ${chatMessageLinks} l
|
|
where l.company_id = ${chatActions.companyId} and l.endpoint_id = ${chatActions.endpointId}
|
|
and l.delivery_id = ${chatActions.deliveryId} and l.direction = 'inbound' limit 1)`,
|
|
leaseExpiresAt: sql<string | null>`(select max(l.expires_at) from ${chatEndpointLeases} l
|
|
where l.company_id = ${chatActions.companyId} and l.endpoint_id = ${chatActions.endpointId}
|
|
and l.lease_key = 'github-publication:' || (${repositoryId}) || ':' || ${number})`,
|
|
}).from(chatActions)
|
|
.innerJoin(chatConversations, and(eq(chatConversations.id, chatActions.conversationId), eq(chatConversations.companyId, chatActions.companyId)))
|
|
.innerJoin(chatEndpoints, and(eq(chatEndpoints.id, chatActions.endpointId), eq(chatEndpoints.companyId, chatActions.companyId)))
|
|
.innerJoin(chatDeliveries, and(eq(chatDeliveries.id, chatActions.deliveryId), eq(chatDeliveries.companyId, chatActions.companyId)))
|
|
.innerJoin(issues, and(eq(issues.id, chatConversations.issueId), eq(issues.companyId, chatActions.companyId)))
|
|
.leftJoin(chatEndpointResources, and(eq(chatEndpointResources.id, chatConversations.resourceId), eq(chatEndpointResources.companyId, chatActions.companyId)))
|
|
.leftJoinLateral(latestIssueRun, sql`true`)
|
|
.where(and(eq(chatActions.kind, "github_response_comment"), eq(chatActions.status, "processed"),
|
|
isNotNull(chatActions.deliveryId), isNotNull(chatEndpoints.assignedAgentId),
|
|
sql`coalesce(${chatActions.result}->>'final', 'false') <> 'true'`,
|
|
sql`(${issues.status} = 'blocked' or ${latestIssueRun.status} in ('succeeded', 'failed', 'interrupted', 'timed_out', 'cancelled'))`,
|
|
broad(scope) && !actionId ? sql`${chatActions.result}->>'terminalWaitKey' is distinct from ${waitKey}` : undefined,
|
|
scope ? eq(chatActions.companyId, scope.companyId) : undefined,
|
|
scope?.issueId ? eq(issues.id, scope.issueId) : undefined,
|
|
scope?.conversationId ? eq(chatActions.conversationId, scope.conversationId) : undefined,
|
|
scope?.deliveryId ? eq(chatActions.deliveryId, scope.deliveryId) : undefined,
|
|
scope?.endpointId ? eq(chatActions.endpointId, scope.endpointId) : undefined,
|
|
scope?.repositoryId ? eq(repositoryId, scope.repositoryId) : undefined,
|
|
scope?.number ? eq(number, String(scope.number)) : undefined,
|
|
cursor ? gt(chatActions.id, cursor) : undefined, actionId ? eq(chatActions.id, actionId) : undefined,
|
|
)).orderBy(asc(chatActions.id)).limit(100);
|
|
}
|
|
type Row = Awaited<ReturnType<typeof page>>[number];
|
|
/** The request's entry when it is owed now, else whether its wait may be
|
|
* recorded. A wait on the owning run's reply or presentation is never
|
|
* recorded: a publication that commits it changes no wait-key input, and a
|
|
* restart can lose the hint that its lease release sends. */
|
|
async function entry(row: Row, read: GitHubResponseRunRead): Promise<Entry | { parkable: boolean }> {
|
|
try {
|
|
const execution = await githubResponseExecution(db, { ...row, agentId: row.agentId!, deliveryId: row.deliveryId! }, read);
|
|
if (execution.body === undefined || execution.reply === "unsettled" ||
|
|
(execution.paused && row.result?.pausedVersion === execution.version))
|
|
return { parkable: !execution.presentationPending && execution.reply === "none" };
|
|
} catch (error) {
|
|
if (!(error instanceof HttpError) || error.status !== 403) throw error;
|
|
// Keep this request eligible for the acknowledgement owner's authority
|
|
// check and cancellation; it must not poison discovery for other requests.
|
|
}
|
|
return pending(row);
|
|
}
|
|
/** The request's entry, before its run state is read. */
|
|
function pending(row: Row): Entry {
|
|
return { id: row.id, companyId: row.companyId, endpointId: row.endpointId, deliveryId: row.deliveryId!,
|
|
issueId: row.issueId, conversationId: row.conversationId, repositoryId: row.repositoryId,
|
|
number: row.number ? Number(row.number) : null, dueAt: row.leaseExpiresAt ? Date.parse(row.leaseExpiresAt) : 0 };
|
|
}
|
|
/** The entries of a page, by position, from one batched run-state read. */
|
|
async function entries(rows: Row[], signal?: AbortSignal) {
|
|
const reads = await readGitHubResponseRuns(db, rows.map(row => ({ companyId: row.companyId, endpointId: row.endpointId,
|
|
agentId: row.agentId!, issueId: row.issueId, deliveryId: row.deliveryId!, sourceMessageId: row.sourceMessageId })));
|
|
const values: Array<Awaited<ReturnType<typeof entry>>> = [];
|
|
for (const [index, row] of rows.entries()) {
|
|
if (!canRun() || signal?.aborted) throw signal?.reason ?? new Error("GitHub terminal-response recovery stopped");
|
|
values.push(await entry(row, reads[index]!));
|
|
}
|
|
return values;
|
|
}
|
|
/** Record the wait of requests that cannot progress yet. A request whose row
|
|
* changed after the read gets no key and is read again. The write leaves
|
|
* updated_at alone, because the key includes it. */
|
|
async function park(rows: Row[]) {
|
|
const waiting = rows.filter(row => row.result?.terminalWaitKey !== row.waitKey);
|
|
if (!waiting.length) return;
|
|
await db.execute(sql`update ${chatActions}
|
|
set result = coalesce(${chatActions.result}, '{}'::jsonb) || jsonb_build_object('terminalWaitKey', waiting.key)
|
|
from unnest(${sql.param(waiting.map(row => row.id))}::uuid[], ${sql.param(waiting.map(row => row.actionVersion))}::text[],
|
|
${sql.param(waiting.map(row => row.waitKey))}::text[]) as waiting(id, version, key)
|
|
where ${chatActions.id} = waiting.id and ${actionVersion} = waiting.version
|
|
and ${chatActions.status} = 'processed' and coalesce(${chatActions.result}->>'final', 'false') <> 'true'`);
|
|
}
|
|
async function current(actionId: string) {
|
|
const [row] = await page(undefined, undefined, actionId);
|
|
if (!row) return null;
|
|
const [value] = await entries([row]);
|
|
if (!("parkable" in value!)) return value!;
|
|
if (value.parkable) await park([row]);
|
|
return null;
|
|
}
|
|
|
|
return registerScopedDeadlineWork(coordinator, {
|
|
queue: DELIVERY_QUEUES.chatGitHubTerminalResponses,
|
|
reconstructionIntervalMs: githubResponseReconstructionIntervalMs,
|
|
decodeScope: (hint: string): Scope | undefined => hint === "*" ? undefined : JSON.parse(hint),
|
|
matches: (value: Entry, scope?: Scope) => !scope || (value.companyId === scope.companyId &&
|
|
(!scope.issueId || value.issueId === scope.issueId) && (!scope.deliveryId || value.deliveryId === scope.deliveryId) &&
|
|
(!scope.conversationId || value.conversationId === scope.conversationId) &&
|
|
(!scope.endpointId || value.endpointId === scope.endpointId) && (!scope.repositoryId || value.repositoryId === scope.repositoryId) &&
|
|
(!scope.number || value.number === scope.number)),
|
|
load: async (scope?: Scope, signal?: AbortSignal) => {
|
|
const loaded: Entry[] = [];
|
|
let cursor: string | undefined;
|
|
while (canRun() && !signal?.aborted) {
|
|
const rows = await page(scope, cursor);
|
|
const values = await entries(rows, signal);
|
|
const waiting: Row[] = [];
|
|
for (const [index, value] of values.entries()) {
|
|
if (!("parkable" in value)) loaded.push(value); else if (value.parkable) waiting.push(rows[index]!);
|
|
}
|
|
await park(waiting);
|
|
if (rows.length < 100) return loaded;
|
|
cursor = rows[rows.length - 1].id;
|
|
}
|
|
throw signal?.reason ?? new Error("GitHub terminal-response recovery stopped");
|
|
},
|
|
check: async (value: Entry, signal?: AbortSignal) => {
|
|
const due = await current(value.id);
|
|
if (!due || due.dueAt > Date.now() || !canRun() || signal?.aborted) return due;
|
|
await acknowledge(due.deliveryId, () => canRun() && !signal?.aborted);
|
|
// Keep a request that still waits for the next check, which reads its
|
|
// run state again before it acknowledges. One check reads it once.
|
|
const [remaining] = await page(undefined, undefined, value.id);
|
|
return remaining ? pending(remaining) : null;
|
|
},
|
|
}, canRun);
|
|
}
|