fix(queue): another agent's comment no longer hides a pending approval (#21303)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - When a person approves a new plan while the agent is busy, the
approval waits in the task's message queue. Its Interrupt control stops
the current attempt and starts a fresh one with the approved plan
> - The task view shows one queue at a time. A saved card response (an
approval) gives way to any later queued message, so it never hides the
person's own later messages
> - Another agent's comment (here, the CEO's direction with `resume:
true`) is also a queued message. It took over the view, and the approval
and its Interrupt control disappeared
> - The person could then not interrupt for the plan they approved
> - This pull request lets only the person's own later messages hide a
pending approval. A queue that holds only agent comments no longer hides
it
> - The benefit is that the Interrupt control for an approved plan stays
available until the person uses it or writes a newer message

## Linked Issues or Issue Description

**What happened?**
In the paid chat eval `steering.interrupt-with-direction`, the person
approved a saved simpler plan while Morgan was busy. The approval queued
with `requiresFreshSession`, and its Interrupt control showed. The CEO
then posted its own direction comment on the task at 04:03:21. That
comment queued as a separate deferred wake. `GET /queued-comments` then
returned only the CEO comment's queue. The approval's Interrupt control
(`task-chat-queued-interrupt-<approval id>`) never rendered, the click
timed out, and no interrupt request was sent. The approval was not lost:
its deferred wake was still saved. Only the view and its control were
hidden.

**Steps to reproduce**
1. Approve a plan on a busy task, so the approval waits for a fresh
session.
2. Let another agent comment on the task, so its comment queues after
the approval.
3. Read `GET /api/issues/:id/queued-comments`. The approval is gone from
the queue.

**Expected behavior**
The approval stays the visible queue with its Interrupt control. A newer
message from the person still takes the view, as before.

## What Changed

- `server/src/routes/issues.ts`: `findQueuedCommentWake` returns a later
message queue only when it holds a message from a person. A queue with
only agent comments yields to a pending saved response. With no saved
response it is still returned, as before.
- `server/src/__tests__/issue-queued-comments-routes.test.ts`: the eval
ordering (a fresh-session approval, then another agent's comment) keeps
the approval as the queue, and a later person message in that queue
still takes the view.

## Verification

- `cd server && npx vitest run
src/__tests__/issue-queued-comments-routes.test.ts -t "another agent's
comment queues after it"` — fails on master (the CEO comment's queue is
returned) and passes here.
- `cd server && npx vitest run
src/__tests__/issue-queued-comments-routes.test.ts` (all tests passed).
- `cd server && npx tsc --noEmit` passes.
- Paid E2E: see the combined-branch run in the comments.

## Risks

- While a saved response waits, queued agent-only comments are not shown
in the composer queue. They stay saved and are delivered by the normal
deferred-wake path, as they were before the person approved anything.
The person's own messages are never hidden.
- One indexed comment lookup per queued message wake during the queue
read.
- No persisted shape or API contract change.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code
execution in Claude Code.

## 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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 authored and GitHub committed 2026-11-12 23:53:54 -06:00
1 parent 99d9e608f4
commit 61ab829d5d
2 files changed
+57 -5

No files matched your search

@@ -692,6 +692,39 @@ describeEmbeddedPostgres("issue queued-comment routes", () => {
expect(steerNativeSessionMock.mock.calls).toHaveLength(calls);
});
it("keeps a fresh-session approval and its Interrupt in view when another agent's comment queues after it", async () => {
const seeded = await seedResponseQueue(true);
const [receipt] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, seeded.wakeId));
await db.update(agentWakeupRequests).set({ payload: { ...receipt.payload,
_paperclipWakeContext: { ...seeded.context, forceFreshSession: true } } }).where(eq(agentWakeupRequests.id, seeded.wakeId));
const [ceo] = await db.insert(agents).values({ companyId: seeded.companyId, name: "CEO", role: "ceo", status: "idle",
adapterType: "paperclip_runner", adapterConfig: {}, runtimeConfig: {}, permissions: {} }).returning();
const directionId = randomUUID(), later = new Date(Date.now() + 60_000);
await db.insert(issueComments).values({ id: directionId, companyId: seeded.companyId, issueId: seeded.issueId,
authorType: "agent", authorAgentId: ceo.id, body: "CEO direction: restart with the approved layout",
createdAt: later, updatedAt: later });
const [agentWake] = await db.insert(agentWakeupRequests).values({ companyId: seeded.companyId, agentId: seeded.agentId,
source: "issue_comment", reason: "issue_commented", status: "deferred_issue_execution",
requestedByActorType: "agent", requestedByActorId: ceo.id, requestedAt: later,
payload: { issueId: seeded.issueId, commentId: directionId,
_paperclipWakeContext: { commentId: directionId, wakeCommentId: directionId, wakeCommentIds: [directionId] } } }).returning();
const client = app(seeded.companyId);
const queue = await request(client).get(`/api/issues/${seeded.issueId}/queued-comments`).expect(200);
expect(queue.body.queueId).toBe(seeded.wakeId);
expect(queue.body.entries).toEqual([expect.objectContaining({
source: expect.objectContaining({ interactionId: seeded.interactionId, requiresFreshSession: true }) })]);
const personId = randomUUID(), latest = new Date(Date.now() + 120_000);
await db.insert(issueComments).values({ id: personId, companyId: seeded.companyId, issueId: seeded.issueId,
authorType: "user", authorUserId: "queue-owner", body: "Also keep the signup button", createdAt: latest, updatedAt: latest });
await db.update(agentWakeupRequests).set({ payload: { issueId: seeded.issueId, commentId: personId,
_paperclipWakeContext: { commentId: personId, wakeCommentId: personId, wakeCommentIds: [directionId, personId] } } })
.where(eq(agentWakeupRequests.id, agentWake.id));
const withPersonMessage = await request(client).get(`/api/issues/${seeded.issueId}/queued-comments`).expect(200);
expect(withPersonMessage.body.queueId).toBe(agentWake.id);
});
it("does not steer a plan approval that requires a fresh execution session", async () => {
const seeded = await seedResponseQueue(true);
const [wake] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, seeded.wakeId));
+24 -5
View File
@@ -7746,6 +7746,21 @@ export function issueRoutes(
// is a read-only receipt. It must never hide the user's own later
// messages, which are separately queued and remain editable and sendable.
let receipt: IssueQueueState | null = null;
let agentOnly: IssueQueueState | null = null;
const hasPersonMessage = async (wake: { payload: unknown }) => {
const ids = queuedCommentIdsFromWakePayload(wake.payload);
if (ids.length === 0) return false;
const [row] = await executor.select({ id: issueComments.id }).from(issueComments).where(and(
eq(issueComments.companyId, issue.companyId), eq(issueComments.issueId, issue.id),
inArray(issueComments.id, ids), eq(issueComments.authorType, "user"), isNull(issueComments.deletedAt),
)).limit(1);
return Boolean(row);
};
const pendingMessages = async (state: IssueQueueState) => {
if (await hasPersonMessage(state.wake)) return state;
agentOnly = state;
return null;
};
for (const wake of rows) {
if (
readObject(wake.payload).issueId !== issue.id ||
@@ -7754,16 +7769,17 @@ export function issueRoutes(
continue;
const isReceipt = Boolean(queuedInteractionId(wake.payload));
if (isReceipt && receipt) continue;
// A receipt a run already received is not pending; a later one may be.
if (!isReceipt && agentOnly) continue;
const readReceipt = () => readQueuedInteractionResponse(executor as Db, wake.companyId, issue.id, wake.payload);
if (wake.status === "deferred_issue_execution") {
if (isReceipt) {
const response = await readReceipt();
// A tool action's outcome is delivered, never listed (approve = run).
if (response && !response.toolActionOutcome) receipt = { wake, state: "deferred", queueRun: null, response };
continue;
}
return { wake, state: "deferred", queueRun: null };
const messages = await pendingMessages({ wake, state: "deferred", queueRun: null });
if (messages) return messages;
continue;
}
if (!wake.runId) continue;
const queueRun = await executor
@@ -7784,9 +7800,12 @@ export function issueRoutes(
const response = await readReceipt();
if (response && !response.toolActionOutcome) receipt = { wake, state: "queued", queueRun, response };
}
else if (queueRun) return { wake, state: "queued", queueRun };
else if (queueRun) {
const messages = await pendingMessages({ wake, state: "queued", queueRun });
if (messages) return messages;
}
}
return receipt;
return receipt ?? agentOnly;
}
async function queueCommentsForWake(