Files
PaperClipAI/server/src/services/issue-queued-comment-queue.test.ts
T
DottaandPaperclip 08341ea134 fix(queue): let board users discard agent-authored queued messages (#21458)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The task composer holds messages that an agent has not consumed yet.
> - A board operator needs to remove an agent-authored pending message
before it starts more work.
> - The queue currently grants discard only to the human author, so
these messages have a disabled trash button and the API rejects the
board user.
> - This change derives the board discard permission in the queue
snapshot and uses that same permission under the existing issue lock.
> - The route checks task access before it changes the queue. The
benefit is a working discard action with the existing company, privacy,
revision, and dispatch guards.

## Linked Issues or Issue Description

Fixes #15464.

Supersedes #15465 with an independent implementation and red/green
regression evidence. Thanks to @MMTE for identifying the gap and
proposing a fix. Related: #18958 addresses deletion of comments that
have already been delivered; this change covers pending queue entries.

## What Changed

- Queue responses grant board users discard permission for
agent-authored messages. Editing and other human authors' messages
retain their existing permissions.
- The discard use case honors the actor-specific permission rebuilt
under the queue lock. Author agents keep their existing cancellation
path.
- The board discard route checks private-task access before deletion.
The denial for a non-author agent now points to the board action instead
of saying that no board action exists.
- Tests cover queue states, cancellation of an empty continuation,
persistence, queue linkage, audit attribution, private-task denial, and
cross-company denial. The API reference describes the supported action.

## Verification

- Red before the fix: the two snapshot regressions returned `canDiscard:
false`; the discard regression threw
`QueuedCommentMutationForbiddenError`. The route regressions also caught
the disabled permission, missing private-task guard, and stale agent
denial text.
- Green after the fix: 30 queue snapshot and use-case tests; all five
focused route regressions with real disposable Postgres; six Postgres
adapter tests; 42 existing queue UI tests.
- Server typecheck: `cd server && node scripts/typecheck-layers.mjs`
passes. `node scripts/check-module-boundaries.mjs` and `git diff
--check` pass.
- The [complete PR
workflow](https://github.com/paperclipai/paperclip/actions/runs/43484735280)
passes on head `164200602828e23f01a8b209ff954821c5e14e9a`. [Greptile
reports
5/5](https://github.com/paperclipai/paperclip/pull/21458#issuecomment-6861824451)
on that same head with no open review threads.
- Local scope: the 83 focused snapshot, use-case, route, adapter, and UI
checks pass. The existing queue UI suite has 42 tests. The new route
cases use real disposable Postgres and verify the resulting database
state after reload.
- Additional local full-suite attempt: 174 tests passed and 13
approval-related tests failed with cleanup timeouts, Postgres deadlocks,
and duplicate-prefix errors after cleanup failed. The first failure,
`queues request_confirmation resolution while its legacy source run is
still running`, also fails on unchanged base code with the same
30-second `afterEach` and `afterAll` timeouts. This broader local run is
not counted as a pass. No test timeout or assertion was weakened. CI
supplies the broad passing result for the committed head.
- `node scripts/local-verify.mjs --explain` returned exit 2 because the
API documentation is a global CI input; all of its required CI lanes
passed. The initial adapter attempt could not initialize local Postgres
under the sandbox; the same six checks passed with local-process access.
The initial cross-company fixture was corrected to use a distinct
company prefix.

## Risks

- This grants board operators a new action on agent-authored pending
messages. The route must pass company membership and private-task access
checks; the mutation retains its transaction, revision, and dispatch
protections.
- No schema or migration is required. The UI uses its existing discard
control and error handling.
- The staging task was inspected to confirm the cause. No staging data
was deleted, and a deployed UI retest is not claimed by these local and
CI checks.

## Model Used

- OpenAI Codex, GPT-6, with reasoning, repository inspection, tool use,
and code execution. The exact runtime model ID and context 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
#` / `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 — the focused regression,
adapter, and UI checks listed above; the additional broad local cleanup
failure is documented separately
- [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-11-13 13:03:46 -06:00

179 lines
7.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { buildQueuedCommentQueueSnapshot, decideQueuedCommentQueueSteering, withQueuedCommentIdsInRunContext } from "./issue-queued-comment-queue.js";
describe("decideQueuedCommentQueueSteering", () => {
it("answers unsupported on the legacy protocol", () => {
const decision = decideQueuedCommentQueueSteering({
state: "deferred",
queueRunRuntimeMode: null,
activeRun: { id: "run-1", runtimeMode: "legacy" },
assignedAgentAdapterType: "codex_local",
queuedCommentCount: 1,
});
expect(decision).toEqual({ protocol: "legacy", kind: "unsupported" });
});
it("answers temporarily_unavailable for a promoted native queue with no deferred run", () => {
const decision = decideQueuedCommentQueueSteering({
state: "queued",
queueRunRuntimeMode: "native",
activeRun: null,
assignedAgentAdapterType: "paperclip_runner",
queuedCommentCount: 1,
});
expect(decision).toEqual({ protocol: "paperclip_runner_v1", kind: "temporarily_unavailable" });
});
it("answers temporarily_unavailable when the queue holds no live comments", () => {
const decision = decideQueuedCommentQueueSteering({
state: "deferred",
queueRunRuntimeMode: null,
activeRun: { id: "run-1", runtimeMode: "native" },
assignedAgentAdapterType: "paperclip_runner",
queuedCommentCount: 0,
});
expect(decision).toEqual({ protocol: "paperclip_runner_v1", kind: "temporarily_unavailable" });
});
it("tells the caller it may probe a running deferred turn on the native protocol", () => {
const decision = decideQueuedCommentQueueSteering({
state: "deferred",
queueRunRuntimeMode: null,
activeRun: { id: "run-1", runtimeMode: "native" },
assignedAgentAdapterType: "paperclip_runner",
queuedCommentCount: 1,
});
expect(decision).toEqual({ protocol: "paperclip_runner_v1", kind: "probe", steeringRunId: "run-1" });
});
// Acceptance-criterion fact pattern: a deferred queue whose active run
// has not resolved a runtime mode yet, for an agent on the
// `paperclip_runner` adapter. The protocol resolves to
// `paperclip_runner_v1` through the adapter-type fallback, and the
// decision hands the run to the caller to probe live — it never answers
// the flat "unsupported" value a duplicated, unshared rule can drift to.
it("resolves the protocol through the adapter-type fallback and asks the caller to probe", () => {
const decision = decideQueuedCommentQueueSteering({
state: "deferred",
queueRunRuntimeMode: null,
activeRun: { id: "run-1", runtimeMode: null },
assignedAgentAdapterType: "paperclip_runner",
queuedCommentCount: 1,
});
expect(decision).toEqual({ protocol: "paperclip_runner_v1", kind: "probe", steeringRunId: "run-1" });
});
it("keeps a preparing Paperclip Runner on the steering protocol until runtime selection finishes", () => {
expect(decideQueuedCommentQueueSteering({ state: "deferred", queueRunRuntimeMode: null,
activeRun: { id: "preparing-run", runtimeMode: "legacy", runtimeModeResolvedAt: null,
runnerProfileJson: { adapterDispatch: { adapterType: "paperclip_runner" } } },
assignedAgentAdapterType: "paperclip_runner", queuedCommentCount: 1,
})).toEqual({ protocol: "paperclip_runner_v1", kind: "temporarily_unavailable" });
});
it("preserves the historical adapter when mutable agent settings change", () => {
expect(decideQueuedCommentQueueSteering({ state: "deferred", queueRunRuntimeMode: null,
activeRun: { id: "legacy-run", runtimeMode: "legacy", runtimeModeResolvedAt: null,
runnerProfileJson: { adapterDispatch: { adapterType: "codex_local" } } },
assignedAgentAdapterType: "paperclip_runner", queuedCommentCount: 1,
})).toEqual({ protocol: "legacy", kind: "unsupported" });
});
});
describe("buildQueuedCommentQueueSnapshot entry permissions", () => {
const baseFacts = {
issueId: "issue-1",
queueId: "queue-1",
state: "queued" as const,
activeRunId: null,
protocol: "legacy" as const,
steeringDisposition: "unsupported" as const,
};
it("grants edit and discard to the user who authored the queued comment", () => {
const queue = buildQueuedCommentQueueSnapshot({
...baseFacts,
actorType: "user",
actorId: "user-1",
comments: [{ id: "comment-1", updatedAt: new Date(), authorUserId: "user-1", authorAgentId: null }],
});
expect(queue.entries[0]?.canEdit).toBe(true);
expect(queue.entries[0]?.canDiscard).toBe(true);
});
it("denies edit and discard to a user who did not author the queued comment", () => {
const queue = buildQueuedCommentQueueSnapshot({
...baseFacts,
actorType: "user",
actorId: "user-1",
comments: [{ id: "comment-1", updatedAt: new Date(), authorUserId: "user-2", authorAgentId: null }],
});
expect(queue.entries[0]?.canEdit).toBe(false);
expect(queue.entries[0]?.canDiscard).toBe(false);
});
it("denies edit and discard to an agent actor even when the comment carries a matching author id", () => {
const queue = buildQueuedCommentQueueSnapshot({
...baseFacts,
actorType: "agent",
actorId: "user-1",
comments: [{ id: "comment-1", updatedAt: new Date(), authorUserId: "user-1", authorAgentId: null }],
});
expect(queue.entries[0]?.canEdit).toBe(false);
expect(queue.entries[0]?.canDiscard).toBe(false);
});
it.each(["deferred", "queued"] as const)("lets the board discard an agent message in a %s queue without editing it", (state) => {
const queue = buildQueuedCommentQueueSnapshot({
...baseFacts,
state,
actorType: "user",
actorId: "user-1",
comments: [{ id: "comment-1", updatedAt: new Date(), authorUserId: null, authorAgentId: "agent-1" }],
});
expect(queue.entries[0]).toMatchObject({ canEdit: false, canDiscard: true });
});
});
describe("withQueuedCommentIdsInRunContext", () => {
it("invalidates every generated task projection and ownership metadata", () => {
const result = withQueuedCommentIdsInRunContext({
issueId: "issue-1",
preserved: "keep",
paperclipWake: { comments: [{ id: "comment-1" }] },
paperclipWakeComment: { id: "comment-1" },
paperclipTaskMarkdown: "historical",
paperclipTaskMarkdownCompact: "historical compact",
paperclipTaskMarkdownAssignment: "assignment",
paperclipTaskMarkdownAssignmentCompact: "assignment compact",
paperclipTurnContext: { version: 1, events: { owner: "wake_prompt" } },
}, ["comment-2"], ["source-wake"]);
expect(result).toMatchObject({
issueId: "issue-1",
preserved: "keep",
queuedCommentSourceWakeIds: ["source-wake"],
wakeCommentIds: ["comment-2"],
wakeCommentId: "comment-2",
commentId: "comment-2",
});
expect(result).not.toHaveProperty("paperclipWake");
expect(result).not.toHaveProperty("paperclipWakeComment");
expect(result).not.toHaveProperty("paperclipTaskMarkdown");
expect(result).not.toHaveProperty("paperclipTaskMarkdownCompact");
expect(result).not.toHaveProperty("paperclipTaskMarkdownAssignment");
expect(result).not.toHaveProperty("paperclipTaskMarkdownAssignmentCompact");
expect(result).not.toHaveProperty("paperclipTurnContext");
expect(withQueuedCommentIdsInRunContext(result, ["comment-3"]).queuedCommentSourceWakeIds).toEqual(["source-wake"]);
});
});