Files
PaperClipAI/server/src/services/issue-queued-comment-queue.ts
T
DottaandPaperclip cc67d4e1d8 fix: preserve steering and recover stopped task conversations (#15015)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - A task conversation must let a user guide a running agent and resume
stopped work.
> - The active run owns its input protocol, even when the user changes
the next model or effort.
> - Queue delivery waits for a provider receipt, which must be able to
persist during the request.
> - A stopped startup also needs a clear user action that passes normal
task admission.
> - This pull request fixes steering delivery, makes queue actions
immediate, and restores explicit continuation.
> - The benefit is a responsive conversation that can recover without
losing saved input.

## Linked Issues or Issue Description

**What happened?**

A queued message could change from Steer to Interrupt while a native run
prepared. A steer request could wait on its own database lock and fail
to deliver. A stopped startup could then leave the conversation without
a working Retry or message continuation. Interrupt also waited for the
server and showed a toast.

**Expected behavior**

The active run keeps its input protocol. Steer delivers input to that
run. Steer and Interrupt clear the submitted queue rows and show the
input in the conversation immediately. Failed delivery restores the
latest queue with an inline error. An eligible stopped run offers Retry,
and authenticated user input can start a fresh turn through normal task
admission.

**Steps to reproduce**

1. Start a task with a native Paperclip Runner.
2. Change the selected model or effort while that run prepares.
3. Queue a message and press Steer.
4. Observe the provider receipt and queue state during the request.
5. Stop a startup before its provider process begins, then try Retry or
send a new message.
6. Repeat queued delivery with a legacy runner and press Interrupt.

**Paperclip version or commit**

Reproduced on the parent of this branch, `59c07ede7`.

**Deployment mode**

Authenticated private deployment. The fixes also cover local task
conversations.

Related work: Refs #12834, Refs #13354, Refs #13275. The open refactor
in #13160 moves the same queue route; it does not fix the receipt lock
or stopped-run continuation addressed here.

## What Changed

- Select queue behavior from the active run's immutable dispatch and
runtime resolution.
- Leave the run row unlocked during provider acknowledgement, then lock
and read it before merging the receipt.
- Retain queued input if the target run stops during that wait. Keep
inline delivery errors visible after empty queue updates.
- Permit exact Retry and authenticated continuation after verified
native startup cancellation. Preserve pause, approval, budget,
ownership, and process-stop gates.
- Carry undelivered native queue input into a fresh turn once the old
execution is confirmed stopped.
- Show Steer and Interrupt input in the conversation and clear submitted
composer rows immediately. Restore the latest queue inline on failure.
Remove delivery toasts.
- Keep optimistic delivery stable across stale polls, empty queues, and
paginated history. Preserve classic Interrupt error handling.
- Document recovery and optimistic delivery behavior. Add regression
tests across server, shared queue projection, and UI boundaries.

## Verification

- Red-green regression tests reproduced the queue protocol, receipt
lock, stopped-startup continuation, and optimistic delivery failures.
- The focused server route, continuation, queue, and runner boundary
suites passed during implementation.
- The queue-route suite passes with 78 tests. The three complete
conversation UI suites pass with 347 tests.
- UI typecheck, production build, and `pnpm check:token-gates` pass.
- Workspace `pnpm -r typecheck` and `pnpm build` pass. The local
monolithic `pnpm test:run` is still running; remote CI verifies the
complete suite on the latest commit.
- All CI gates pass on `bd9031ad56abfcde13d13a13488c1b9217c2fd3a`,
including the full test shards, runner verification, browser E2E,
typecheck, release registry, and canary dry run.
- Greptile reports 5/5 for that commit. Both review threads are
resolved.

## Risks

This changes queue display and explicit continuation admission. The UI
must restore rejected delivery without losing other-session edits. The
server must preserve concurrent provider result updates and must not
resume a process whose stop is uncertain. Focused tests cover these
boundaries. This change has no database migration.

## Model Used

OpenAI Codex, an agent based on GPT-6. The exact runtime model ID and
context window are not exposed in this session. Used reasoning,
repository tools, code execution, and browser inspection.

## 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
- [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-02 22:07:14 -05:00

209 lines
8.0 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[],
): Record<string, unknown> {
const context = { ...record(contextValue) };
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,
})),
};
}