Files
PaperClipAI/server/src/services/chat-channels/github/terminal-response-work.test.ts
T
DottaandClaude Opus 5.5 8ce422e45d fix(github): keep published reviews and settle every status comment (#22672)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Review agents answer GitHub pull requests through one shared main PR
comment and one comment per inline request
> - Paperclip writes status notices into these comments: "Work is
paused", "This turn ended without publishing a final response", "Working
on this…"
> - On recent PRs these notices replaced finished reviews, and many
never changed again, even after the task was done
> - People then see a stale status instead of the review, and work that
waits on the review slows down
> - This pull request keeps published reviews in place and makes every
status comment settle
> - The benefit is that the PR always shows the latest real review, and
no placeholder stays open after the task is idle

## Linked Issues or Issue Description

**Describe the bug**
Review bot comments on GitHub pull requests show "Work is paused. Your
request is saved." or "This turn ended without publishing a final
response." after the review finished. The comment never updates. Inline
replies stay at "Working on this…".

**Steps to reproduce**
1. A review agent publishes a review in the main PR comment.
2. Two more review requests arrive. The task becomes blocked for a short
time, or the run for a request ends without a reply.
3. The main PR comment now shows the status notice. The review is gone.
4. Separately: request A's run ends and request B's run starts a few
seconds later. A's inline comment stays "Working on this…" forever.

**Expected behavior**
A status notice for one request never replaces a published review in the
shared main PR comment. Every request comment settles once the run that
woke for it is finished and the task is idle.

**Evidence**
GitHub edit history shows the overwrite on all six settled stuck
comments across #22528, #22591, #22611, #22626 (two bots) and #22635.
Example: on #22626 the "Ponytail Paperclip Review — 3/5" body was
replaced by "Work is paused" at 14:28 and again at 14:56; Feynman's
reply was replaced and then patched nine times in four minutes. The task
ran 15 back-to-back runs about 3 seconds apart, so the old "latest run
only" check never matched the earlier requests.

## What Changed

- `writeGitHubResponseComment` takes a `status` flag. In the shared main
PR comment, a status notice never replaces a published reply or a newer
response. That request settles as final against the existing comment
(`published_response_kept` / `status_superseded`) instead of throwing
and retrying.
- Status notices carry a `paperclip-response-kind:status` marker. Known
status texts posted before this change count as status too, so a later
status may still replace them.
- `readGitHubResponseRun` / `readGitHubResponseRuns` return
`finishedRequestRunId`: the latest finished run that woke for the
request. It is part of the same statement, so the batched sweep keeps
its statement budget.
- `githubResponseExecution` settles a request from that run once the
task's latest run is finished, the latest run belongs to another
request, and the task is not blocked.
- `processTerminalResponses` selects a request when any finished run
woke for it, not only the latest run.

## Verification

- New red/green integration tests in
`server/src/__tests__/chat-channels.integration.test.ts`:
- `keeps the published review in the main GitHub PR comment when a later
request has only a status: ended_without_reply / paused` (red before:
body became the status text)
- `settles a GitHub working comment whose run finished just before
another request's run started` (red before: body stayed "Working on
this…")
- Updated older tests that asserted the old rule (status replaces the
review) to assert the review stays and the request settles final.
- `response-run-state.test.ts` equivalence reference now also computes
the finished request run; passes over the randomized fixture.
- `terminal-response-work.test.ts` statement budget passes (the first
fallback did one query per row; it now reads in the same statement).
- Local: all GitHub tests in the integration file passed (536) before
the batching refactor; after it, the targeted tests plus
`server/src/services/chat-channels/github/` and related files pass
(438). `tsc --noEmit` for server is clean. CI covers the full suite.

## Risks

- Behavior change: when a newer request is paused or ends without a
reply, the main PR comment keeps the earlier review instead of showing
the status. The status stays visible in the Paperclip task.
- A request whose run finished without a reply now settles to "ended
without publishing a final response" once the task is idle, even if a
later continuation run covered it in another thread.
- No migration.

## 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>
2026-11-23 14:54:42 -06:00

284 lines
19 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { setTimeout as delay } from "node:timers/promises";
import { and, eq, inArray, sql } from "drizzle-orm";
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { agents, chatActions, companies, createDb, heartbeatRuns, issues, runIdentityContexts, type Db } from "@paperclipai/db";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../../../__tests__/helpers/embedded-postgres.js";
import { createDeliveryWorkCoordinator } from "../../delivery-work-coordinator.js";
import { DELIVERY_QUEUES, notifyDeliveryWork } from "../../delivery-work-notifications.js";
import { idleWorkSnapshot } from "../../task-admission.js";
import { registerGitHubAcknowledgementWork } from "./acknowledgement-work.js";
import { registerGitHubTerminalResponseWork } from "./terminal-response-work.js";
import { readGitHubResponseRuns } from "./response-run-state.js";
const support = await getEmbeddedPostgresTestSupport();
// Staging (2026-11-15) held 1,557 processed, non-final response comments whose
// result codes were null (89%), acknowledgement_unconfirmed (10%) and
// acknowledgement_superseded (1%). Each task's latest run had ended and had
// been started by a later GitHub request, so it did not own these requests.
const ISSUES = 30;
const STALE_PER_ISSUE = 4;
const STALE = ISSUES * STALE_PER_ISSUE;
const code = (index: number) => index % 50 === 1 ? "acknowledgement_superseded"
: index % 10 === 0 ? "acknowledgement_unconfirmed" : null;
describe.skipIf(!support.supported)("GitHub terminal response sweep backlog", () => {
let temporary: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
let db: Db;
let coordinator: ReturnType<typeof createDeliveryWorkCoordinator> | undefined;
const errors: unknown[] = [];
const acknowledged: string[] = [];
beforeAll(async () => {
temporary = await startEmbeddedPostgresTestDatabase("github-terminal-response-work-");
db = createDb(temporary.connectionString);
}, 60_000);
afterEach(async () => {
await coordinator?.stop(); coordinator = undefined; acknowledged.length = 0;
vi.useRealTimers();
expect(errors.splice(0)).toEqual([]);
});
afterAll(async () => { await db?.$client.end({ timeout: 1 }); await temporary?.cleanup(); });
type Request = { issueId: string; deliveryId: string; actionId: string; commentId: string };
/** `waiting`: processed, non-final responses as on staging. `received`: every
* request still owes its first acknowledgement. */
async function seed(shape: "waiting" | "received" = "waiting") {
const client = db.$client;
const companyId = randomUUID(), agentId = randomUUID();
await db.insert(companies).values({ id: companyId, name: "GitHub backlog", issuePrefix: `G${companyId.slice(0, 7)}` });
await db.insert(agents).values({ id: agentId, companyId, name: "Reviewer" });
const [{ application }] = await client.unsafe(`insert into tool_applications (company_id, application_key, name, type)
values ($1, 'github', 'GitHub', 'chat') returning id as application`, [companyId]);
const [{ connection }] = await client.unsafe(`insert into tool_connections (company_id, application_id, name, uid, transport, connection_purpose)
values ($1, $2, 'GitHub', $3, 'chat_sdk', 'channel') returning id as connection`, [companyId, application, `github-${companyId}`]);
const [{ endpoint }] = await client.unsafe(`insert into chat_endpoints (company_id, connection_id, provider, public_id, assigned_agent_id, status)
values ($1, $2, 'github', $3, $4, 'active') returning id as endpoint`, [companyId, connection, `backlog-${companyId}`, agentId]);
const stale: Request[] = [];
const runs: Array<{ issueId: string; runId: string }> = [];
const base = Date.UTC(2026, 9, 28);
for (let issueNumber = 1; issueNumber <= ISSUES; issueNumber++) {
const [{ issue }] = await client.unsafe(`insert into issues (company_id, title, status, assignee_agent_id)
values ($1, $2, 'in_progress', $3) returning id as issue`, [companyId, `Pull ${issueNumber}`, agentId]);
const [{ conversation }] = await client.unsafe(`insert into chat_conversations (company_id, endpoint_id, issue_id,
external_conversation_id, external_label, external_thread_id)
values ($1, $2, $3, $4, $4, $4) returning id as conversation`, [companyId, endpoint, issue, `github:paperclipai/paperclip:${issueNumber}`]);
const requests: Request[] = [];
for (let request = 0; request <= STALE_PER_ISSUE; request++) {
const [{ delivery }] = await client.unsafe(`insert into chat_deliveries (company_id, endpoint_id, provider_event_id,
deduplication_key, event_kind, normalized_event, conversation_id, state)
values ($1, $2, $3, $3, 'message', '{}', $4, 'processed') returning id as delivery`,
[companyId, endpoint, `event-${companyId}-${issueNumber}-${request}`, conversation]);
const [{ comment }] = await client.unsafe(`insert into issue_comments (company_id, issue_id, body)
values ($1, $2, 'Please review') returning id as comment`, [companyId, issue]);
await client.unsafe(`insert into chat_message_links (company_id, endpoint_id, conversation_id, delivery_id, comment_id,
provider_message_id, direction) values ($1, $2, $3, $4, $5, $6, 'inbound')`,
[companyId, endpoint, conversation, delivery, comment, `in-${companyId}-${issueNumber}-${request}`]);
const index = stale.length;
const latest = request === STALE_PER_ISSUE;
const [action] = await db.insert(chatActions).values({ companyId, endpointId: endpoint, conversationId: conversation,
deliveryId: delivery, kind: "github_response_comment", providerActionId: `github_response:${delivery}`,
payload: { agentId }, status: shape === "received" ? "received" : "processed",
result: shape === "received" ? null : { id: String(1_000 + index),
url: `https://github.com/paperclipai/paperclip/pull/${issueNumber}#c${index}`,
final: latest, versionAt: new Date(base).toISOString(), ...(latest ? {} : { code: code(index) }) },
updatedAt: new Date(base + index * 60_000) }).returning();
requests.push({ issueId: issue, deliveryId: delivery, actionId: action!.id, commentId: comment });
}
stale.push(...(shape === "received" ? requests : requests.slice(0, STALE_PER_ISSUE)));
// The latest run answered the newest request, through its identity context.
const runId = randomUUID(), contextId = randomUUID();
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, nativeIssueId: issue, issueId: issue,
scopeKind: "issue", status: "succeeded", responsibleUserId: "requester", activeIdentityContextId: contextId,
contextSnapshot: { issueId: issue }, createdAt: new Date(base + issueNumber * 3_600_000) });
await db.insert(runIdentityContexts).values({ id: contextId, companyId, runId, revision: 1, responsibleUserId: "requester",
messageId: requests[STALE_PER_ISSUE]!.commentId, cause: "chat_request", correlationId: runId, status: "accepted" });
runs.push({ issueId: issue, runId });
}
await db.execute(sql`analyze`);
return { companyId, agentId, endpointId: endpoint as string, stale, runs };
}
async function start(register: typeof registerGitHubTerminalResponseWork = registerGitHubTerminalResponseWork) {
coordinator = createDeliveryWorkCoordinator({ owner: db, canRun: () => true, canReconcile: () => true,
onError: error => errors.push(error) });
const worker = register(db, coordinator, async deliveryId => {
acknowledged.push(deliveryId);
// Stand in for the real acknowledgement, which records a final response.
await db.update(chatActions).set({ status: "processed", updatedAt: new Date(),
result: sql`coalesce(${chatActions.result}, '{}'::jsonb) || '{"final": true}'::jsonb` })
.where(and(eq(chatActions.deliveryId, deliveryId), eq(chatActions.kind, "github_response_comment")));
}, () => true);
await worker.ready;
}
async function advance(ms: number) {
const until = Date.now() + ms;
while (Date.now() < until) {
await vi.advanceTimersByTimeAsync(Math.min(1_000, until - Date.now())); await delay(1);
while (idleWorkSnapshot().active > 0) await delay(5);
}
}
/** Company-wide hints, as run lifecycle changes elsewhere in the company send them. */
async function companyHint(companyId: string) {
await db.transaction(async tx => {
await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubTerminalResponses, JSON.stringify({ companyId }));
});
}
async function observe(work: () => Promise<unknown>) {
const statements: string[] = [], prior = db.$client.options.debug;
db.$client.options.debug = (_connection, query) => { statements.push(query); };
try { await work(); } finally { db.$client.options.debug = prior; }
const sweep = statements.filter(query => /"chat_actions"|heartbeat_runs|run_identity_contexts|chat_message_links/.test(query));
return {
statements: sweep.length,
// Requester provenance reads, not the page read that names identity contexts in a subquery.
provenance: sweep.filter(query => /from "run_identity_contexts"/.test(query) && !/github_response_request/.test(query)).length,
};
}
const finals = async (actionIds: string[]) => (await db.select({ result: chatActions.result }).from(chatActions)
.where(inArray(chatActions.id, actionIds))).filter(row => row.result?.final === true).length;
it("stops re-reading processed, non-final responses that cannot progress on every company rebuild", async () => {
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"], shouldAdvanceTime: true });
const f = await seed();
const first = await observe(async () => { await start(); await advance(5_000); });
expect(acknowledged).toEqual([]);
const rebuilds = await observe(async () => {
for (let pass = 0; pass < 4; pass++) { await companyHint(f.companyId); await advance(31_000); }
});
console.info(`terminal response sweep: first pass ${first.statements} statements (${first.provenance} run identity reads); `
+ `4 company rebuilds ${rebuilds.statements} statements (${rebuilds.provenance} run identity reads) for ${STALE} rows`);
// Nothing these rows wait for changed, so a rebuild reads none of them.
expect(rebuilds.provenance).toBe(0);
expect(rebuilds.statements).toBeLessThanOrEqual(4 * 2);
expect(acknowledged).toEqual([]);
// No GitHub comment changed and no row was finalized.
expect(await finals(f.stale.map(request => request.actionId))).toBe(0);
}, 300_000);
it("reads a page of waiting responses in a constant number of statements", async () => {
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"], shouldAdvanceTime: true });
await seed();
const first = await observe(async () => { await start(); await advance(5_000); });
console.info(`terminal response sweep, first pass: ${first.statements} statements `
+ `(${first.provenance} run identity reads) for ${STALE} rows in 2 pages`);
expect(acknowledged).toEqual([]);
// Per page: the page, its latest runs, the batched context and link reads, and the wait keys.
expect(first.provenance).toBeLessThanOrEqual(2);
expect(first.statements).toBeLessThanOrEqual(2 * 5);
}, 300_000);
it("settles requests whose own run finished before another request's run, with a batched page read", async () => {
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"], shouldAdvanceTime: true });
const f = await seed();
const base = Date.UTC(2026, 9, 28);
// Each waiting request's own run finished just before the newest request's
// run started. Half are linked by a wake comment, half only by an accepted
// identity context for the request's comment.
for (const [index, request] of f.stale.entries()) {
const issueNumber = f.runs.findIndex(run => run.issueId === request.issueId) + 1;
const runId = randomUUID(), contextId = randomUUID(), byIdentity = index % 2 === 1;
await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId,
nativeIssueId: request.issueId, issueId: request.issueId, scopeKind: "issue", status: "succeeded",
responsibleUserId: "requester", activeIdentityContextId: byIdentity ? contextId : null,
contextSnapshot: byIdentity ? { issueId: request.issueId } : { issueId: request.issueId, wakeCommentId: request.commentId },
createdAt: new Date(base + issueNumber * 3_600_000 - (index + 1) * 1_000) });
if (byIdentity) await db.insert(runIdentityContexts).values({ id: contextId, companyId: f.companyId, runId, revision: 1,
responsibleUserId: "requester", messageId: request.commentId, cause: "chat_request", correlationId: runId, status: "accepted" });
}
const inputs = f.stale.map(request => ({ companyId: f.companyId, endpointId: f.endpointId, agentId: f.agentId,
issueId: request.issueId, deliveryId: request.deliveryId, sourceMessageId: request.commentId }));
let reads: Awaited<ReturnType<typeof readGitHubResponseRuns>> = [];
const read = await observe(async () => { reads = await readGitHubResponseRuns(db, inputs); });
// The latest runs, their requester provenance, and the earlier runs: each one statement for the page.
expect(read.statements).toBeLessThanOrEqual(4);
expect(reads.every(state => !("forbidden" in state) && !state.ownsLatestRequest && state.finishedRequestRun)).toBe(true);
// One earlier run is still publishing its reply: that request waits.
const publishing = f.stale[0]!;
const [publishingRun] = await db.select({ id: heartbeatRuns.id }).from(heartbeatRuns).where(and(
eq(heartbeatRuns.companyId, f.companyId), sql`${heartbeatRuns.contextSnapshot}->>'wakeCommentId' = ${publishing.commentId}`));
const [conversation] = await db.select({ conversationId: chatActions.conversationId }).from(chatActions)
.where(eq(chatActions.id, publishing.actionId));
await db.insert(chatActions).values({ companyId: f.companyId, endpointId: f.endpointId,
conversationId: conversation!.conversationId, kind: "github_review_publication",
providerActionId: `github_publication:${randomUUID()}`, status: "processing",
payload: { operation: "comment", session: { companyId: f.companyId, issueId: publishing.issueId,
runId: publishingRun!.id, agentId: f.agentId } } });
await start(); await advance(31_000);
const settled = f.stale.filter(request => request !== publishing);
expect(new Set(acknowledged)).toEqual(new Set(settled.map(request => request.deliveryId)));
expect(await finals(settled.map(request => request.actionId))).toBe(settled.length);
const [waiting] = await db.select().from(chatActions).where(eq(chatActions.id, publishing.actionId));
expect(waiting).toMatchObject({ status: "processed" });
expect(waiting!.result?.final).not.toBe(true);
}, 300_000);
it("acknowledges every owed request of a page when one run's requester provenance changed", async () => {
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"], shouldAdvanceTime: true });
const f = await seed("received");
// One task's latest run lost its accepted identity context, so reading its
// requester provenance is forbidden. Only that task's requests are affected.
await db.update(runIdentityContexts).set({ status: "revoked" }).where(eq(runIdentityContexts.runId, f.runs[3]!.runId));
const first = await observe(async () => { await start(registerGitHubAcknowledgementWork); await advance(30_000); });
console.info(`acknowledgement sweep: ${first.statements} statements (${first.provenance} run identity reads) `
+ `for ${f.stale.length} owed requests`);
expect(new Set(acknowledged)).toEqual(new Set(f.stale.map(request => request.deliveryId)));
expect(acknowledged).toHaveLength(f.stale.length);
}, 300_000);
it("still settles a waiting response once a run that owns it ends", async () => {
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"], shouldAdvanceTime: true });
const f = await seed();
await start(); await advance(5_000);
const target = f.stale[5]!;
await db.insert(heartbeatRuns).values({ companyId: f.companyId, agentId: f.agentId, nativeIssueId: target.issueId,
issueId: target.issueId, scopeKind: "issue", status: "failed",
contextSnapshot: { issueId: target.issueId, wakeCommentId: target.commentId }, createdAt: new Date() });
await companyHint(f.companyId);
await advance(31_000);
expect(acknowledged).toEqual([target.deliveryId]);
const rebuilds = await observe(async () => { await companyHint(f.companyId); await advance(31_000); });
expect(rebuilds.provenance).toBe(0);
expect(acknowledged).toEqual([target.deliveryId]);
}, 300_000);
it("still settles a response after a restart lost the hint of the reply publication it waited for", async () => {
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"], shouldAdvanceTime: true });
const f = await seed();
const target = f.stale[9]!;
const [response] = await db.select().from(chatActions).where(eq(chatActions.id, target.actionId));
const runId = randomUUID();
await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, nativeIssueId: target.issueId,
issueId: target.issueId, scopeKind: "issue", status: "failed",
contextSnapshot: { issueId: target.issueId, wakeCommentId: target.commentId }, createdAt: new Date() });
// The owning run's formal review is still being published.
const [publication] = await db.insert(chatActions).values({ companyId: f.companyId, endpointId: f.endpointId,
conversationId: response!.conversationId, deliveryId: target.deliveryId, kind: "github_review_publication",
providerActionId: `github_review:${runId}`, status: "processing",
payload: { operation: "formal_review", session: { companyId: f.companyId, issueId: target.issueId, runId, agentId: f.agentId } } })
.returning();
await start(); await advance(5_000);
expect(acknowledged).toEqual([]);
// The publication commits, and the server stops before its lease release
// sends the targeted hint. The next start runs only a broad load.
await db.update(chatActions).set({ status: "processed", updatedAt: new Date(),
result: { id: "77", url: "https://github.com/paperclipai/paperclip/pull/1#pullrequestreview-77" } })
.where(eq(chatActions.id, publication!.id));
await coordinator!.stop(); coordinator = undefined;
await start(); await advance(5_000);
expect(acknowledged).toEqual([target.deliveryId]);
}, 300_000);
it("still pauses waiting responses once their task is blocked", async () => {
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"], shouldAdvanceTime: true });
const f = await seed();
await start(); await advance(5_000);
const blocked = f.runs[2]!.issueId;
await db.update(issues).set({ status: "blocked", updatedAt: new Date() }).where(eq(issues.id, blocked));
await companyHint(f.companyId);
await advance(31_000);
expect(new Set(acknowledged)).toEqual(new Set(f.stale.filter(request => request.issueId === blocked).map(request => request.deliveryId)));
}, 300_000);
});