mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 15:52:36 +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>
83 lines
4.9 KiB
TypeScript
83 lines
4.9 KiB
TypeScript
import type { Db } from "@paperclipai/db";
|
|
import { githubLatestTaskResponseUrl, githubRunReplyState } from "../../chat-run-publications.js";
|
|
import { baseThreadId, isMainPullThread } from "./response-target.js";
|
|
import { FINISHED_RUN_STATUSES, readGitHubResponseRun, type GitHubResponseRunRead, type GitHubResponseRunState } from "./response-run-state.js";
|
|
export { responseRunColumns as githubResponseRunColumns } from "./response-run-state.js";
|
|
|
|
export type GitHubResponseExecutionInput = {
|
|
companyId: string;
|
|
endpointId: string;
|
|
agentId: string;
|
|
issueId: string;
|
|
deliveryId: string;
|
|
sourceMessageId: string | null;
|
|
issueStatus: string;
|
|
deliveryCreatedAt: Date;
|
|
/** The request's conversation thread, such as `github:owner/repo:12` or `github:owner/repo:12:rc:34`. */
|
|
threadId: string;
|
|
};
|
|
|
|
const STATUS_TEXT = {
|
|
handled: "This request has been handled. See the response in this conversation.",
|
|
paused: "Work is paused. Your request is saved. See the Paperclip task for details.",
|
|
publish_failed: "This turn's reply could not be published. See the Paperclip task for details.",
|
|
ended: "This turn ended without publishing a final response. See the Paperclip task for details.",
|
|
stopped: "This turn stopped before completing. See the Paperclip task for details.",
|
|
} as const;
|
|
|
|
const finished = (status: string | undefined) => (FINISHED_RUN_STATUSES as readonly string[]).includes(status ?? "");
|
|
|
|
/** Whether the task is idle and this request settles from the earlier run that
|
|
* woke for it, because the latest run belongs to another request. */
|
|
export function settlesFromRequestRun(state: GitHubResponseRunState, issueStatus: string) {
|
|
return !!state.latestRun && finished(state.latestRun.status) && !state.ownsLatestRequest &&
|
|
issueStatus !== "blocked" && !!state.finishedRequestRun;
|
|
}
|
|
|
|
/** `read` is this request's state from a batched page read, if the caller has it. */
|
|
export async function githubResponseExecution(db: Db, input: GitHubResponseExecutionInput, read?: GitHubResponseRunRead) {
|
|
const state = read ?? await readGitHubResponseRun(db, input);
|
|
if ("forbidden" in state) throw state.forbidden;
|
|
const { latestRun, ownsLatestRequest, finishedRequestRun } = state;
|
|
const requestRunning = ownsLatestRequest && latestRun?.status === "running";
|
|
const terminal = latestRun && finished(latestRun.status);
|
|
// Once the task is idle, a request whose own run finished earlier settles
|
|
// from that run; otherwise its placeholder would wait for a run that never comes.
|
|
const requestRun = settlesFromRequestRun(state, input.issueStatus) ? finishedRequestRun : null;
|
|
const run = requestRun ?? (terminal && (ownsLatestRequest || input.issueStatus === "blocked") ? latestRun : null);
|
|
const answersRequest = ownsLatestRequest || !!requestRun;
|
|
|
|
const reply = run && answersRequest ? await githubRunReplyState(db, {
|
|
companyId: input.companyId, endpointId: input.endpointId, issueId: input.issueId, runId: run.id,
|
|
}) : "none";
|
|
const paused = !!run ? input.issueStatus === "blocked" && !answersRequest
|
|
: input.issueStatus === "blocked" && latestRun?.status !== "running";
|
|
const presentationPending = !!run && answersRequest && run.nativePresentationPending && reply === "none";
|
|
const passiveResponseWait = !!run && answersRequest && run.passiveResponseWait && reply === "none";
|
|
const unanswered = !!run && !presentationPending && reply === "none" && !paused;
|
|
const latestResponse = unanswered ? await githubLatestTaskResponseUrl(db, { companyId: input.companyId,
|
|
endpointId: input.endpointId, issueId: input.issueId, since: input.deliveryCreatedAt, baseThreadId: baseThreadId(input.threadId) })
|
|
: null;
|
|
const taskFinished = ["done", "cancelled"].includes(input.issueStatus);
|
|
// Precedence: a later published response is more useful to the reader than
|
|
// any notice about this request's own run, including a refused reply.
|
|
const outcome: keyof typeof STATUS_TEXT | "link" | "waiting" | null =
|
|
presentationPending ? null
|
|
: !run ? paused ? "paused" : null
|
|
: reply === "confirmed" ? "handled"
|
|
: paused ? "paused"
|
|
: latestResponse ? "link"
|
|
: run.passiveResponseWait && !isMainPullThread(input.threadId) && !taskFinished ? "waiting"
|
|
: run.responsePublicationFailed ? "publish_failed"
|
|
: run.status === "succeeded" ? "ended" : "stopped";
|
|
const body = outcome === null || outcome === "waiting" ? undefined
|
|
: outcome === "link" ? `See the [latest response](${latestResponse}).` : STATUS_TEXT[outcome];
|
|
return {
|
|
body, outcome, paused, reply, running: requestRunning, presentationPending, passiveResponseWait,
|
|
versionAt: run ? new Date(Math.max(run.updatedAt.getTime(), input.deliveryCreatedAt.getTime()))
|
|
: paused ? input.deliveryCreatedAt : undefined,
|
|
version: JSON.stringify([latestRun?.id ?? null, latestRun?.status ?? null,
|
|
latestRun?.updatedAt.toISOString() ?? null, ownsLatestRequest, input.issueStatus === "blocked", reply, requestRun?.id ?? null]),
|
|
};
|
|
}
|