Files
PaperClipAI/server/src/services/issue-queued-comment-queue.ts
T
DottaandPaperclip 5c7a025032 fix(ui): clear consumed messages from the pending queue (#16317)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Task conversations keep follow-up messages pending while an agent
works.
> - The screen combines optimistic sends, queue snapshots, and run input
records.
> - A late send response can restore a local queue flag after a run
takes the message.
> - A stale queue snapshot can then keep the same message in pending.
> - This pull request matches the input record to its source queue and
clears stale pending flags.
> - The benefit is that consumed messages leave pending while unsent
messages stay visible.

## Linked Issues or Issue Description

**What happened?**

A follow-up can remain in the task composer's pending row after a run
takes it as input. Local queue state can outlive the send request. The
old projection cleared that state for steering, but not for ordinary
run-start input. A stale queue read can also retain the consumed entry.

**Expected behavior**

A message that a started run takes as input appears once in history and
leaves pending. Other pending messages remain available. A message
assigned to a run that has not started remains pending.

**Steps to reproduce**

1. Send a follow-up while a task has a live run.
2. Let a started run record that message as input before or after the
send response arrives.
3. Let a queue read still include the consumed message.
4. Observe the message remain pending on the base revision.

**Paperclip version or commit**

Reproduced against master at `33d24e48ad` with controlled request
ordering.

**Deployment mode**

Source checkout. Browser verification used the production task screen
with a simulated native runner and delayed API responses. It did not run
a live model or change a deployed instance.

Related: [PR
#15030](https://github.com/paperclipai/paperclip/pull/15030) addresses
acknowledgement matching in the runner console. This change addresses
the task-detail composer and does not modify that console.

## What Changed

- Clear local pending flags when a started run records the comment as
input.
- Use live-run input IDs while the run-history query catches up.
- Match stale queue entries by their primary or coalesced source wake
ID, even when the snapshot targets the previous run or has no target.
- Retain coalesced source queue IDs in run context and include receipt
IDs in compact task run reads.
- Keep unstarted run inputs and authoritative retries pending. Cover
send ordering, same-run and successor delivery, queued snapshots,
coalesced inputs, and retry consumption.
- Add the delivery-race checks to the steering feature map.

## Verification

- Before the fix, the first four request-order regressions failed with
the consumed message still in pending. Coverage now includes 16
combinations of receipt source, send-response order, and stale snapshot
kind.
- Focused UI checks: 5 files and 233 tests passed. These cover task
detail, queue projection, optimistic comments, queued-message controls,
and the task-chat adapter.
- `pnpm -r typecheck` passed.
- Server receipt checks: 5 files and 139 tests passed with
`PAPERCLIP_TEST_POSTGRES_TEMPLATE=0`. These exercise real database
reads, coalesced heartbeat input, and polling routes. The default
cached-template path timed out during setup on this host; the
fresh-cluster path passed without longer timeouts.
- Token gates and `node scripts/check-maps.mjs` passed.
- Browser: sent a follow-up with a delayed response in the production
task screen. The consumed message moved into history. A second unsent
follow-up stayed queued. Reload preserved both states. The API and
runner were simulated.
- `pnpm build` passed.
- The full local repository test run was started, then stopped to
isolate database setup timeouts. All 55 GitHub CI checks passed on
`4ec6820cf1f89880b801fd5f938d72f54591c5e2`, including the full test
matrix. One serialized server shard passed on its single rerun after a
15-second route-test timeout; the same 97-test route suite also passed
locally.
- Greptile reviewed the final head at 5/5. All review threads are
resolved.

## Risks

- Queue display depends on matching comment IDs, source queue IDs, and a
recorded start time. Rows without start evidence stay pending. Newer
queues remain visible until their own input receipt arrives.
- Run context gains additive receipt metadata. No schema migration is
required. Dispatch order, stored queue state, company access, and
steering authority stay unchanged.
- Browser evidence covers controlled response ordering. It does not
establish deployment or live-provider behavior.

## Model Used

- OpenAI Codex, GPT-6, with reasoning, tool use, and code execution. The
exact serving model ID and context-window limit 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
#` 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
- [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>
2026-10-14 16:11:29 -05:00

216 lines
8.2 KiB
TypeScript

import { createHash } from "node:crypto";
import type { IssueComment, IssueQueuedCommentQueue } from "@paperclipai/shared";
const QUEUE_CONTEXT_KEY = "_paperclipWakeContext";
const QUEUE_IDS_KEY = "wakeCommentIds";
function record(value: unknown): Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value)
? value as Record<string, unknown>
: {};
}
function uniqueIds(value: unknown): string[] {
if (!Array.isArray(value)) return [];
const seen = new Set<string>();
return value.flatMap((candidate) => {
if (typeof candidate !== "string" || !candidate || seen.has(candidate)) return [];
seen.add(candidate);
return [candidate];
});
}
export function queuedCommentIdsFromWakePayload(payloadValue: unknown): string[] {
const payload = record(payloadValue);
const context = record(payload[QUEUE_CONTEXT_KEY]);
return uniqueIds(context[QUEUE_IDS_KEY]);
}
export function queuedCommentIdsFromRunContext(contextValue: unknown): string[] {
return uniqueIds(record(contextValue)[QUEUE_IDS_KEY]);
}
export function withQueuedCommentIdsInWakePayload(
payloadValue: unknown,
ids: string[],
): Record<string, unknown> {
const payload = { ...record(payloadValue) };
const context = { ...record(payload[QUEUE_CONTEXT_KEY]) };
if (ids.length > 0) {
const latestId = ids[ids.length - 1]!;
context[QUEUE_IDS_KEY] = ids;
context.wakeCommentId = latestId;
context.commentId = latestId;
payload.commentId = latestId;
} else {
delete context[QUEUE_IDS_KEY];
delete context.wakeCommentId;
delete context.commentId;
delete payload.commentId;
}
payload[QUEUE_CONTEXT_KEY] = context;
return payload;
}
/**
* Fingerprints one queued-comment queue: the wake id plus every comment's id
* and last-updated time. A mutation that changes the queue changes this
* value, so a caller can echo it back to detect a queue it no longer holds
* the latest view of. The read path (`GET /queued-comments`) and every queue
* mutation must call this same function, so a client's fingerprint always
* compares against the same computation.
*/
export function queuedCommentQueueRevision(input: {
queueId: string | null;
comments: Array<{ id: string; updatedAt: Date }>;
}): string {
return createHash("sha256")
.update(JSON.stringify({
queueId: input.queueId,
comments: input.comments.map((comment) => [comment.id, comment.updatedAt.toISOString()]),
}))
.digest("hex")
.slice(0, 32);
}
export function withQueuedCommentIdsInRunContext(
contextValue: unknown,
ids: string[],
sourceWakeIds?: string[],
): Record<string, unknown> {
const context = { ...record(contextValue) };
if (sourceWakeIds) {
context.queuedCommentSourceWakeIds = uniqueIds([
...uniqueIds(context.queuedCommentSourceWakeIds),
...sourceWakeIds,
]);
}
if (ids.length > 0) {
const latestId = ids[ids.length - 1]!;
context[QUEUE_IDS_KEY] = ids;
context.wakeCommentId = latestId;
context.commentId = latestId;
} else {
delete context[QUEUE_IDS_KEY];
delete context.wakeCommentId;
delete context.commentId;
}
// These projections are generated immediately before dispatch. Any queue
// mutation must force them to be rebuilt from the canonical comment ids.
delete context.paperclipWake;
delete context.paperclipWakeComment;
delete context.paperclipTaskMarkdown;
delete context.paperclipTaskMarkdownCompact;
delete context.paperclipTaskMarkdownAssignment;
delete context.paperclipTaskMarkdownAssignmentCompact;
delete context.paperclipTurnContext;
return context;
}
export type QueuedCommentQueueProtocol = "paperclip_runner_v1" | "legacy";
export type QueuedCommentQueueSteeringDecision =
| { protocol: QueuedCommentQueueProtocol; kind: "unsupported" }
| { protocol: QueuedCommentQueueProtocol; kind: "temporarily_unavailable" }
/** Only the caller can probe the live runner. `steeringRunId` names the run to probe. */
| { protocol: "paperclip_runner_v1"; kind: "probe"; steeringRunId: string };
/**
* Decides the queue protocol and the steering answer for one queued-comment
* queue, from plain facts. This is the one place that resolves the
* `paperclip_runner_v1`/`legacy` protocol and the steering answer; every
* caller that builds a queue response must call this function instead of
* repeating the rule.
*
* When the decision is `"probe"`, only the caller can answer the question:
* it must ask the live runner (through a call such as
* `getNativeSessionSteeringState`) and fall back to
* `"temporarily_unavailable"` on failure. A caller that never probes the
* live runner must answer `"temporarily_unavailable"` for a `"probe"`
* decision instead.
*/
export function decideQueuedCommentQueueSteering(facts: {
state: "deferred" | "queued" | null;
/** The queued run's own runtime mode. Read only when `state` is `"queued"`. */
queueRunRuntimeMode: string | null;
/** The currently running turn, if any. Read only when `state` is `"deferred"`. */
activeRun: { id: string; runtimeMode: string | null; runtimeModeResolvedAt?: Date | null; runnerProfileJson?: Record<string, unknown> | null } | null;
assignedAgentAdapterType: string | null;
queuedCommentCount: number;
}): QueuedCommentQueueSteeringDecision {
// The default legacy value is not a selection receipt during preparation.
// Use the run's immutable dispatch choice, never the agent's mutable settings.
const preparingNative = facts.state === "deferred" && facts.activeRun?.runtimeMode === "legacy" &&
facts.activeRun.runtimeModeResolvedAt === null &&
record(facts.activeRun.runnerProfileJson?.adapterDispatch).adapterType === "paperclip_runner";
const persistedRuntimeMode = preparingNative ? null :
facts.state === "queued"
? facts.queueRunRuntimeMode
: facts.state === "deferred"
? facts.activeRun?.runtimeMode ?? null
: null;
const protocol: QueuedCommentQueueProtocol =
preparingNative || persistedRuntimeMode === "native"
|| (persistedRuntimeMode === null && facts.assignedAgentAdapterType === "paperclip_runner")
? "paperclip_runner_v1"
: "legacy";
if (protocol !== "paperclip_runner_v1") {
return { protocol, kind: "unsupported" };
}
const steeringRun = facts.state === "deferred" ? facts.activeRun : null;
if (!steeringRun || preparingNative || facts.queuedCommentCount === 0) {
return { protocol, kind: "temporarily_unavailable" };
}
return { protocol, kind: "probe", steeringRunId: steeringRun.id };
}
type QueuedCommentQueueEntryFacts = {
id: string;
updatedAt: Date;
authorUserId: string | null;
};
/**
* Assembles the shared `IssueQueuedCommentQueue` response from the already
* resolved facts: the queue identity, the already-decided protocol and
* steering answer, the live comment rows, and the actor who reads the
* queue. This is the one place that builds the response shape; every
* caller that builds a queue response must call this function instead of
* repeating the rule.
*/
export function buildQueuedCommentQueueSnapshot<TComment extends QueuedCommentQueueEntryFacts>(facts: {
issueId: string;
queueId: string | null;
state: "deferred" | "queued" | null;
/** The currently running turn's id. Read only when `state` is `"deferred"`. */
activeRunId: string | null;
protocol: QueuedCommentQueueProtocol;
steeringDisposition: IssueQueuedCommentQueue["steeringDisposition"];
comments: TComment[];
executionWait?: IssueQueuedCommentQueue["executionWait"];
actorType: "agent" | "user";
actorId: string;
}): IssueQueuedCommentQueue {
return {
issueId: facts.issueId,
queueId: facts.queueId,
state: facts.state,
targetRunId: facts.state === "deferred" ? facts.activeRunId : null,
revision: queuedCommentQueueRevision({ queueId: facts.queueId, comments: facts.comments }),
protocol: facts.protocol,
steeringDisposition: facts.steeringDisposition,
...(facts.executionWait ? { executionWait: facts.executionWait } : {}),
entries: facts.comments.map((comment, position) => ({
comment: comment as unknown as IssueComment,
position,
canEdit: facts.actorType === "user" && comment.authorUserId === facts.actorId,
canDiscard: facts.actorType === "user" && comment.authorUserId === facts.actorId,
})),
};
}