Files
PaperClipAI/server/src/services/queued-comment-delivery.ts
T
DottaandClaude Opus 5.5 3244780bd0 fix(queue): keep a message a starting run's first turn took out of the queue (#21747)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A message posted while a run is starting goes into that run's first
turn, and the task page shows it as sent
> - The page shows it as sent only while the live queue lists it as a
first-turn message
> - When that queue closes, for example after a later Steer, the page
has no lasting record of the delivery
> - It then infers that the message is queued and shows it again with a
Steer button
> - This pull request reports each run's own first-turn receipt in the
run list, and the page places those messages in that run
> - The benefit is that a sent message stays sent

## Linked Issues or Issue Description

Follow-up to #21661, found while I verified that change on staging.

**What happened?**
With a Codex runner, two messages were posted while the run was
starting. The first turn took them and the agent acted on both. Then a
third message was steered into the running turn. After the Steer, the
first two messages came back into the queue with Steer buttons. The
server queue was empty.

**Expected behavior**
Messages that a run's first turn took stay in the conversation as
delivered.

**Steps to reproduce**
1. Start a task with a Paperclip runner agent.
2. While the runner is starting, post one or two messages.
3. After the turn starts, post another message and click Steer.

## What Changed

- `activityService.runsForIssue` returns `firstTurnCommentIds` from the
run's `firstTurnQueuedInput` receipt. The field is set only when the
receipt names this run, because a retry copies its predecessor's
context.
- `IssueDetail` adds those ids to the run's start inputs, so the
messages are placed in that run and are not inferred as queued.
- `RunForIssue` type gets the new optional field.

## Verification

Red/green tests. Each failed before the fix and passes after it:
- `server/src/__tests__/activity-service.test.ts`: "reports the queued
messages a run's own first turn took, not a predecessor's". 33/33 pass.
- `ui/src/pages/IssueDetail.test.tsx`: "keeps a message the running turn
already took out of the queue after the live queue closes". The
IssueDetail, queue helper, and task-chat suites pass (69 files, 1298
tests). UI typecheck passes.

Staging, after #21661 deployed, Codex runner: two messages posted while
the run started were taken into the first turn and acted on. No
Interrupt, error banner, or composer wait was shown. A mid-turn Steer
was accepted in seconds. This PR fixes the one defect that the check
found.

## Risks

- Low. One new read-only field on the run list. A run that took messages
and then failed before its provider accepted the turn now shows those
messages in that run. The next run still delivers them, and the queue
still lists them while they wait.

## 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
- [x] 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-15 13:05:34 +00:00

207 lines
13 KiB
TypeScript

import { and, eq, inArray, sql, type SQL } from "drizzle-orm";
import { heartbeatRunEvents, heartbeatRuns, issueComments, type Db } from "@paperclipai/db";
import { appendHeartbeatRunEvent } from "./heartbeat-run-events.js";
/** The wake reason an inbound chat message records (see chat inbound wakeups). */
export const EXTERNAL_CHAT_MESSAGE_WAKE_REASON = "External chat message received";
/** The mutation an inbound chat message wake records. */
export const EXTERNAL_CHAT_MESSAGE_WAKE_MUTATION = "chat_message_received";
/**
* Per-comment consumption (work-intent outbox, invariant 4). A succeeded run
* of the wake's agent that started after the comment's latest revision
* consumed it when it received the comment as its own wake input, or when it
* was a retry of an interrupted turn whose task history included the comment
* (the retry re-reads the whole thread). Task history alone does not consume
* a message saved for its own turn, and a run that was already running never
* counts: its context can be refreshed after the fact, and an edit made after
* it started is still unread. Every path that replays a saved message reads
* this before delivering it.
*
* A run that took the message into its first provider turn
* (`firstTurnQueuedInput`, sealed by that run before `turn.start`) consumed
* it once the provider accepted that turn, whatever the run's later outcome.
* Before acceptance the receipt proves nothing and the message stays queued.
*/
export async function deliveredQueuedCommentIds(tx: Pick<Db, "select">, input: {
companyId: string; issueId: string; agentId: string; commentIds: string[];
}): Promise<Set<string>> {
if (input.commentIds.length === 0) return new Set();
const { companyId, issueId } = input;
const rows = await tx.select({ id: issueComments.id }).from(issueComments).where(and(
eq(issueComments.companyId, companyId), eq(issueComments.issueId, issueId),
inArray(issueComments.id, input.commentIds),
sql`(exists (select 1 from ${heartbeatRuns} delivered_run
where delivered_run.company_id = ${companyId} and delivered_run.agent_id = ${input.agentId}
and delivered_run.status = 'succeeded'
and (delivered_run.native_issue_id = ${issueId} or delivered_run.context_snapshot->>'issueId' = ${issueId})
and delivered_run.started_at > ${issueComments.createdAt}
and delivered_run.started_at >= ${issueComments.updatedAt}
and (coalesce(delivered_run.context_snapshot->'wakeCommentIds', '[]'::jsonb) ? ${issueComments.id}::text
or (delivered_run.retry_of_run_id is not null and jsonb_path_exists(delivered_run.context_snapshot, '$.executionContinuation.messages[*] ? (@.id == $id)',
jsonb_build_object('id', ${issueComments.id}::text))
or jsonb_path_exists(delivered_run.context_snapshot, '$.paperclipWake.messages[*] ? (@.id == $id)',
jsonb_build_object('id', ${issueComments.id}::text)))))
or ${firstTurnDeliveredSql(companyId, issueId, input.agentId)}
or ${historyReadDeliveredSql(companyId, issueId, input.agentId)})`,
));
return new Set(rows.map(row => row.id));
}
export const TASK_HISTORY_READ_EVENT = "task_history.read";
export async function recordTaskHistoryRead(db: Db, input: {
companyId: string; agentId: string; runId: string; comments: ReadonlyArray<{ id: string; updatedAt: Date }>;
}): Promise<void> {
if (input.comments.length === 0) return;
await appendHeartbeatRunEvent(db, { companyId: input.companyId, runId: input.runId, agentId: input.agentId,
eventType: TASK_HISTORY_READ_EVENT, stream: "system", level: "info",
payload: { comments: Object.fromEntries(input.comments.map(comment => [comment.id, comment.updatedAt.toISOString()])) } });
}
export async function taskHistoryReadsForRun(db: Pick<Db, "select">, input: { companyId: string; runId: string }): Promise<Array<{
commentId: string; readAt: Date; revision: Date;
}>> {
const rows = await db.select({ readAt: heartbeatRunEvents.createdAt, payload: heartbeatRunEvents.payload })
.from(heartbeatRunEvents).where(and(eq(heartbeatRunEvents.companyId, input.companyId),
eq(heartbeatRunEvents.runId, input.runId), eq(heartbeatRunEvents.eventType, TASK_HISTORY_READ_EVENT)))
.orderBy(heartbeatRunEvents.seq);
const latest = new Map<string, { commentId: string; readAt: Date; revision: Date }>();
for (const row of rows) {
const comments = row.payload?.comments;
if (!comments || typeof comments !== "object" || Array.isArray(comments)) continue;
for (const [commentId, value] of Object.entries(comments as Record<string, unknown>)) {
const revision = typeof value === "string" ? new Date(value) : null;
if (revision && !Number.isNaN(revision.getTime())) latest.set(commentId, { commentId, readAt: row.readAt, revision });
}
}
return [...latest.values()];
}
export async function historyReadDeliveredCommentIds(tx: Pick<Db, "select">, input: {
companyId: string; issueId: string; agentId: string; commentIds: string[];
}): Promise<Set<string>> {
if (input.commentIds.length === 0) return new Set();
const rows = await tx.select({ id: issueComments.id }).from(issueComments).where(and(
eq(issueComments.companyId, input.companyId), eq(issueComments.issueId, input.issueId),
inArray(issueComments.id, input.commentIds), historyReadDeliveredSql(input.companyId, input.issueId, input.agentId)));
return new Set(rows.map(row => row.id));
}
export const COMMENT_EVIDENCE_REF_PREFIX = "comment:";
function historyReadDeliveredSql(companyId: string, issueId: string, agentId: string) {
const handled = sql`exists (select 1 from ${heartbeatRunEvents} handled
where handled.company_id = ${companyId} and handled.run_id = reading_run.id
and handled.event_type = 'run.result.accepted'
and (jsonb_path_exists(handled.payload, '$.prpEvent.payload.result.evidence[*].ref ? (@ starts with $ref)',
jsonb_build_object('ref', ${COMMENT_EVIDENCE_REF_PREFIX} || ${issueComments.id}::text))
or jsonb_path_exists(handled.payload, '$.prpEvent.payload.result.completionClaim.criteria[*].evidenceRefs[*] ? (@ starts with $ref)',
jsonb_build_object('ref', ${COMMENT_EVIDENCE_REF_PREFIX} || ${issueComments.id}::text))))`;
return sql`(${historyReadSql(companyId, agentId, sql`reading_run.native_issue_id = ${issueId}`, sql`and reading_run.status = 'succeeded' and ${handled}`)}
or ${historyReadSql(companyId, agentId, sql`reading_run.context_snapshot->>'issueId' = ${issueId}`, sql`and reading_run.status = 'succeeded' and ${handled}`)})`;
}
function historyReadSql(companyId: string, agentId: string, binding: SQL, condition: SQL) {
return sql`exists (select 1 from ${heartbeatRuns} reading_run
cross join lateral (select 1 from ${heartbeatRunEvents} history_read
where history_read.company_id = ${companyId} and history_read.run_id = reading_run.id
and history_read.event_type = ${TASK_HISTORY_READ_EVENT}
and (history_read.payload->'comments'->>${issueComments.id}::text)::timestamptz
>= date_trunc('milliseconds', ${issueComments.updatedAt})
limit 1) read_revision
where reading_run.company_id = ${companyId} and ${binding}
and reading_run.agent_id = ${agentId} ${condition}
and reading_run.finished_at >= date_trunc('milliseconds', ${issueComments.updatedAt}) offset 0)`;
}
export async function readByEarlierTurnCommentIds(tx: Pick<Db, "select">, input: {
companyId: string; issueId: string; agentId: string; commentIds: string[]; currentRunId?: string | null;
}): Promise<Set<string>> {
if (input.commentIds.length === 0) return new Set();
const { companyId, issueId, agentId } = input;
const notCurrent = input.currentRunId ? sql`and reading_run.id <> ${input.currentRunId}` : sql``;
const rows = await tx.select({ id: issueComments.id }).from(issueComments).where(and(
eq(issueComments.companyId, companyId), eq(issueComments.issueId, issueId),
inArray(issueComments.id, input.commentIds),
sql`(${historyReadSql(companyId, agentId, sql`reading_run.native_issue_id = ${issueId}`, notCurrent)}
or ${historyReadSql(companyId, agentId, sql`reading_run.context_snapshot->>'issueId' = ${issueId}`, notCurrent)})`));
return new Set(rows.map(row => row.id));
}
export async function commentsArrivedDuringRun(db: Pick<Db, "select">, input: {
companyId: string; runId: string; agentId: string;
comments: ReadonlyArray<{ id: string; authorAgentId: string | null; createdAt: Date; updatedAt: Date }>;
}): Promise<string[]> {
if (input.comments.length === 0) return [];
const [run] = await db.select({ startedAt: heartbeatRuns.startedAt }).from(heartbeatRuns)
.where(and(eq(heartbeatRuns.companyId, input.companyId), eq(heartbeatRuns.id, input.runId)));
const startedAt = run?.startedAt;
if (!startedAt) return [];
return input.comments.filter(comment => comment.authorAgentId !== input.agentId
&& Math.max(comment.createdAt.getTime(), comment.updatedAt.getTime()) > startedAt.getTime()).map(comment => comment.id);
}
export function taskHistoryHandlingGuidance(commentIds: readonly string[]) {
return commentIds.length === 0 ? {} : {
arrivedDuringThisTurn: commentIds.map(id => `${COMMENT_EVIDENCE_REF_PREFIX}${id}`),
handling: "These comments arrived while this turn was running. For each one this turn applies, include its ref in the evidence of paperclip_finish or paperclip_block. Paperclip delivers any comment that is not cited to you again in a new turn after this one.",
};
}
/**
* The comment row was taken into an accepted first provider turn of a run of
* this agent on this task, and not edited after that run sealed its input.
*/
function firstTurnDeliveredSql(companyId: string, issueId: string, agentId: string) {
return sql`exists (select 1 from ${heartbeatRuns} first_turn_run
where first_turn_run.company_id = ${companyId} and first_turn_run.agent_id = ${agentId}
and (first_turn_run.native_issue_id = ${issueId} or first_turn_run.context_snapshot->>'issueId' = ${issueId})
and first_turn_run.context_snapshot->'firstTurnQueuedInput'->>'schema' = 'paperclip.first-turn-queued-input.v1'
and first_turn_run.context_snapshot->'firstTurnQueuedInput'->>'runId' = first_turn_run.id::text
and first_turn_run.context_snapshot->'firstTurnQueuedInput'->'commentIds' ? ${issueComments.id}::text
and (first_turn_run.context_snapshot->'firstTurnQueuedInput'->>'claimedAt')::timestamptz >= date_trunc('milliseconds', ${issueComments.updatedAt})
and exists (select 1 from ${heartbeatRunEvents} accepted
where accepted.company_id = ${companyId} and accepted.run_id = first_turn_run.id
and accepted.event_type in ('turn.accepted', 'turn.started')))`;
}
/**
* The comment ids a run's own first-turn receipt sealed, once that run's
* provider accepted the turn. A receipt alone proves nothing: the run may
* have failed before acceptance, and a retry copies its predecessor's
* receipt. A message edited after the seal is still undelivered. Same
* evidence as `firstTurnDeliveredSql`.
*/
export function acceptedFirstTurnCommentIdsSql(run: { id: typeof heartbeatRuns.id; companyId: typeof heartbeatRuns.companyId;
contextSnapshot: typeof heartbeatRuns.contextSnapshot }) {
return sql<string[] | null>`case
when ${run.contextSnapshot} #>> '{firstTurnQueuedInput,schema}' = 'paperclip.first-turn-queued-input.v1'
and ${run.contextSnapshot} #>> '{firstTurnQueuedInput,runId}' = ${run.id}::text
and ${run.contextSnapshot} #>> '{firstTurnQueuedInput,claimedAt}' is not null
and jsonb_typeof(${run.contextSnapshot} #> '{firstTurnQueuedInput,commentIds}') = 'array'
and exists (select 1 from ${heartbeatRunEvents} accepted
where accepted.company_id = ${run.companyId} and accepted.run_id = ${run.id}
and accepted.event_type in ('turn.accepted', 'turn.started'))
then (select jsonb_agg(sealed_comment.id::text order by sealed.position)
from (select case when raw.id ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
then raw.id::uuid end as id, raw.position
from jsonb_array_elements_text(${run.contextSnapshot} #> '{firstTurnQueuedInput,commentIds}')
with ordinality as raw(id, position)) sealed
join ${issueComments} sealed_comment on sealed_comment.id = sealed.id
and sealed_comment.company_id = ${run.companyId}
where (${run.contextSnapshot} #>> '{firstTurnQueuedInput,claimedAt}')::timestamptz
>= date_trunc('milliseconds', sealed_comment.updated_at)) end`;
}
/** Ids that an accepted first provider turn already received (see `deliveredQueuedCommentIds`). */
export async function firstTurnDeliveredCommentIds(tx: Pick<Db, "select">, input: {
companyId: string; issueId: string; agentId: string; commentIds: string[];
}): Promise<Set<string>> {
if (input.commentIds.length === 0) return new Set();
const rows = await tx.select({ id: issueComments.id }).from(issueComments).where(and(
eq(issueComments.companyId, input.companyId), eq(issueComments.issueId, input.issueId),
inArray(issueComments.id, input.commentIds), firstTurnDeliveredSql(input.companyId, input.issueId, input.agentId)));
return new Set(rows.map(row => row.id));
}