Files
PaperClipAI/server/src/services/agent-interaction-delivery.test.ts
T
DottaandClaude Opus 5.5 9c56e5465c fix(native): keep the agent's own unanswered question open after a turn ends without a report (#20934)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - An agent can ask the person a question and wait for the answer.
Native task status comes from the server's status arbiter, not from
model prose
> - When a native turn ends without `paperclip_finish`, the Runner sends
a default report: `done`, objective satisfied, with the agent's final
message as the only evidence
> - A later comment from the person moves an open question into history
(#15392), so the question no longer gates completion
> - Together these close a task as `done` while the agent is still
waiting for its own answer and has produced nothing. No Retry appears
and nothing flags it
> - This pull request keeps such a task `in_review` on the agent's own
unanswered question when the only completion evidence is the agent's
final message
> - The benefit is that a "waiting for your answer" turn can no longer
become a silent false completion

## Linked Issues or Issue Description

Refs #15392

**What happened?**
Restart-chaos wave on master 7e8901aa60, OpenCode task t06 (W2Q), no
fault injected. Run fec9530f asks a question (interaction c2d3b3f2); the
task goes `in_review`. A board comment says "include NOTE-238ef87a in
the brief; do not write the brief yet; I will answer shortly". Run
f6cbefa8 replies "I will not write the brief until the board's answer
arrives. Ending turn to await the codename answer." and ends its turn
without `paperclip_finish`. The Runner's default report
(`provider:opencode:agent-message` as the only evidence,
`reportedWorkDisposition: done`) is accepted, and the low-risk claim
policy sets the task `done`. The question is still unanswered and the
`brief` document does not exist.

**What did you expect to happen?**
The task keeps waiting for the answer. The answer continues the task,
and the agent writes the brief.

**Steps to reproduce**
1. An agent asks a question on its task.
2. The person posts a comment before answering.
3. The agent ends its turn without a completion report, saying it waits
for the answer.
4. The task is `done`; the question stays pending.

## What Changed

- New `server/src/services/native-runtime/own-question-wait.ts`:
- `reportsOnlyProviderMessage(result)`: true when every evidence
reference in the report is `provider:<provider>:agent-message` and the
report has no artifact and no verification. This is the shape of the
Runner's default report for a turn that ended without
`paperclip_finish`.
- `pendingOwnQuestionId(db, …)`: the agent's own pending, undismissed
ordinary question (`ask_user_questions`, not a governed tool, secret or
connection request) on the task.
- `finalizeNativeRun` (`native-run-finalizer.ts`): when the proposed
decision is `done`, the task is not an Agent Chat conversation, the
report cites only the agent's final message, and the agent's own
question is still pending, the decision becomes `in_review` with reason
`own_question_unanswered` and releases the checkout. The question stays
pending, so the person's answer continues the task through the existing
answer path.
- Dismissal (`POST /issues/:id/interactions/:interactionId/dismiss`,
`dismissInteraction`): new `enqueueOwnQuestionDismissalWake` runs inside
the dismissal transaction, only when the card moves from pending to
dismissed. When the task is `in_review` on an applied
`own_question_unanswered` decision of the assignee's run, the dismissed
card is the assignee's own question, and no other pending, undismissed
question of that agent remains, it saves a durable wake intent
(`issue_question_dismissed`, idempotency key per question) in the
existing native status wake outbox (`enqueueWake`, now exported). The
outbox dispatcher delivers and retries it, so the agent continues
without the answer. Dismissal of any other request keeps the no-wake
rule.
- `assignmentDenial` (`agent-interaction-delivery.ts`): an
`issue_question_dismissed` wake counts as a person's wake when its
`dismissedInteractionId` names the recipient's own question on that task
with a `dismissedByUserId`, so a recipient that refuses agent
assignments still receives it.
- `doc/execution-semantics.md` describes the exception to the
historical-question rule.

## Verification

- New case in
`server/src/services/native-runtime/historical-task-questions.test.ts`
("keeps the agent's own unanswered question open after an unreported
turn"), run through the real `PaperclipControlPlanePort.completeRun` and
`finalizeNativeRun` with a low-risk `agent_claim_policy` contract, as on
staging:
- Red on master (finalizer hunk removed, test kept): `unreported` fails,
`expected 'done' to be 'in_review'`.
- Green: `unreported` rests `in_review` with `own_question_unanswered`
and the question still pending. `unreported-dismissed`,
`unreported-answered` and `verified` (work-product evidence) still rest
`done`, so the #15392 behavior is unchanged.
- Dismissal after finalization (same test): the release check is null
while a second own question is open and returns the agent once both are
dismissed. A repeated dismissal saves no second intent; the saved intent
is a queued `issue_question_dismissed` outbox row. Delivery test
`delivers a dismissal wake only for a person's dismissal of the
recipient's question` (30 passed). Route test `saves the dismissal wake
intent in the dismissal transaction only when the card is dismissed`
(147 passed).
- `cd server && pnpm exec vitest run
src/services/native-runtime/historical-task-questions.test.ts
src/services/native-runtime/native-run-finalizer`: all passed.
- `cd server && pnpm exec vitest run
src/services/native-runtime/paperclip-control-plane-port.test.ts
src/services/native-runtime/status-arbiter.test.ts`: 119 passed.
- `cd server && pnpm exec tsc --noEmit -p tsconfig.json`: passes.
- Chaos, Daytona OpenCode, seed 6201, W2Q K0, 4 tasks (wave
w-20261111T211421-s6201, run from master 7e8901aa60 plus this change
because the pinned runner image must match runner sources): no false
completion. t02 and t04 PASS (in_review -> in_progress -> done after the
answer). t01 failed `R3:retry_needed` through the blocked path fixed
separately in #20931 (not in this build). t03 timed out because its
rewake OpenCode turn stayed active for 20 minutes without ending
(provider turn never finished; this change acts only at finalization,
which never ran). The unreported-done path did not occur in this sample,
so the red/green proof for this change is the integration test. Before:
wave w-20261111T200718-s6201 t06 closed done with the question
unanswered.

## Risks

- Low. Only a `done` decision whose report cites nothing but the agent's
final message changes, and only while the same agent's own ordinary
question is pending and undismissed. Explicit completion reports with
their own evidence keep the current rule. A person can still dismiss the
question to release it.
- An agent that really finished without `paperclip_finish` while its own
old question is open now rests `in_review` instead of `done`. The open
question is visible on the task; answering or dismissing it settles the
task.
- No schema change. A result saved by an older release is finalized by
the same rule because it reads only the saved report and interaction
rows.
- Related observation (not changed here): in 5 Claude/OpenCode W2/W2C
cells the agent wrote "the task remains in_progress as instructed" yet
the task became `done` (Claude t06, wave s6101). The run's
`paperclip_finish` input itself sent `reportedWorkDisposition: "done"`,
`objectiveSatisfied: true`, with the deferred phase listed as
`blocksCompletion: false`. The server did not map a yielded disposition
to done. That is model behavior on a contradictory report, a different
root from this change.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), with tool use and code execution
in an agent harness.

## 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-11 16:50:28 -06:00

381 lines
28 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { eq, sql } from "drizzle-orm";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { activityLog, agents, agentWakeupRequests, companies, createDb, heartbeatRunEvents, heartbeatRuns, issueComments, issueThreadInteractions, issues, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, type Db } from "@paperclipai/db";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../__tests__/helpers/embedded-postgres.js";
import { filterRunMessageInteractionDelivery, hasAutomaticInteractionDeliveryHold, preparedRunInteractionDeliveryDenied, recheckQueuedCommentInteractionDelivery,
recheckQueuedRunInteractions, withInteractionDeliveryOrigin } from "./agent-interaction-delivery.js";
import { createHeartbeatQueue } from "./heartbeat/queue.js";
import { createHeartbeatQueueTestDependencies } from "../__tests__/helpers/heartbeat-queue.js";
import { withPluginInteractionOrigin } from "./plugin-interaction-context.js";
import { recoveryService } from "./recovery/service.js";
import { createAdmissionTransactionScope, createWakeAdmissionWriter } from "../modules/wake-queue/adapters/postgres.js";
const support = await getEmbeddedPostgresTestSupport();
describe.skipIf(!support.supported)("pending agent interaction delivery", () => {
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
let db: ReturnType<typeof createDb>;
beforeAll(async () => {
database = await startEmbeddedPostgresTestDatabase("interaction-delivery-");
db = createDb(database.connectionString);
}, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS);
afterAll(async () => { await db?.$client.end(); await database?.cleanup(); });
async function seed() {
const [company] = await db.insert(companies).values({ name: "Delivery", issuePrefix: `D${randomUUID().slice(0, 5)}` }).returning();
const [sender, recipient] = await db.insert(agents).values([
{ companyId: company!.id, name: "Sender", adapterType: "process" },
{ companyId: company!.id, name: "Recipient", adapterType: "process" },
]).returning();
const [issue] = await db.insert(issues).values({ companyId: company!.id, title: "Work", status: "todo", assigneeAgentId: recipient!.id }).returning();
const [run] = await db.insert(heartbeatRuns).values({ companyId: company!.id, agentId: recipient!.id, status: "queued",
invocationSource: "assignment", contextSnapshot: { issueId: issue!.id }, nativeIssueId: issue!.id }).returning();
return { companyId: company!.id, sender: sender!.id, recipient: recipient!.id, issueId: issue!.id, run: run! };
}
type Fixture = Awaited<ReturnType<typeof seed>>;
const restriction = (id: string, interactions: Record<string, boolean>) => db.update(agents)
.set({ permissions: { interactions } }).where(eq(agents.id, id));
const readRun = async (id: string) => (await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, id)))[0]!;
async function wake(f: Fixture, input: { commentIds?: string[]; actorType?: string | null; actorId?: string | null } = {}) {
const context = { issueId: f.issueId, ...(input.commentIds ? { wakeCommentIds: input.commentIds, wakeCommentId: input.commentIds.at(-1) } : {}) };
const [receipt] = await db.insert(agentWakeupRequests).values({ companyId: f.companyId, agentId: f.recipient,
source: "assignment", reason: input.commentIds ? "issue_commented" : "issue_assigned", runId: f.run.id,
requestedByActorType: input.actorType === undefined ? "agent" : input.actorType,
requestedByActorId: input.actorId === undefined ? f.sender : input.actorId,
payload: { issueId: f.issueId, _paperclipWakeContext: context },
}).returning();
await db.update(heartbeatRuns).set({ wakeupRequestId: receipt!.id, contextSnapshot: context }).where(eq(heartbeatRuns.id, f.run.id));
return receipt!;
}
async function comment(f: Fixture, kind: "agent" | "user") {
const [value] = await db.insert(issueComments).values({ companyId: f.companyId, issueId: f.issueId, body: `${kind} input`,
authorType: kind, ...(kind === "agent" ? { authorAgentId: f.sender } : { authorUserId: "human" }),
}).returning();
return value!.id;
}
it("keeps old queues unchanged when settings are absent", async () => {
const f = await seed(); await wake(f, { actorType: null, actorId: null });
const before = await readRun(f.run.id);
expect(await recheckQueuedRunInteractions(db, before)).toEqual({ run: before, denied: false, denials: {} });
expect(await readRun(f.run.id)).toEqual(before);
});
it("preserves an unrestricted old queue's rendered continuation and timestamps", async () => {
const f = await seed(); const message = await comment(f, "agent"); const receipt = await wake(f, { commentIds: [message] });
const run = await readRun(f.run.id);
await db.update(heartbeatRuns).set({ contextSnapshot: { ...run.contextSnapshot,
executionContinuation: { messages: [{ id: message, body: "saved input" }] }, paperclipWake: { retained: true },
} }).where(eq(heartbeatRuns.id, run.id));
const before = await readRun(run.id);
const wakeBefore = (await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, receipt.id)))[0];
expect((await recheckQueuedRunInteractions(db, before)).denied).toBe(false);
expect(await readRun(run.id)).toEqual(before);
expect((await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, receipt.id)))[0]).toEqual(wakeBefore);
});
it.each(["incoming", "outgoing"])("refuses a previously accepted assignment after %s revocation", async direction => {
const f = await seed(); const receipt = await wake(f);
await restriction(direction === "incoming" ? f.recipient : f.sender,
direction === "incoming" ? { canReceiveAssignmentsFromAgents: false } : { canAssignTasksToAgents: false });
const result = await recheckQueuedRunInteractions(db, await readRun(f.run.id));
expect(result.denied).toBe(true);
expect(result.denials.assignment).toMatchObject({ code: "agent_interaction_denied", direction });
expect((await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, receipt.id)))[0]!.status).toBe("skipped");
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]!.assigneeAgentId).toBe(f.recipient);
await restriction(f.sender, {}); await restriction(f.recipient, {});
expect((await recheckQueuedRunInteractions(db, await readRun(f.run.id))).denied).toBe(true);
});
it("checks each author's permissions inside a mixed human and agent batch", async () => {
const f = await seed(); const agentComment = await comment(f, "agent"), humanComment = await comment(f, "user");
const receipt = await wake(f, { commentIds: [agentComment, humanComment], actorType: "user", actorId: "human" });
await restriction(f.recipient, { canReceiveCommentsFromAgents: false });
const result = await recheckQueuedRunInteractions(db, await readRun(f.run.id));
expect(result.denied).toBe(false);
expect(result.run.contextSnapshot?.wakeCommentIds).toEqual([humanComment]);
expect(result.denials[agentComment]).toMatchObject({ permission: "canReceiveCommentsFromAgents" });
const savedWake = (await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, receipt.id)))[0]!;
expect(savedWake.status).toBe("queued");
expect((savedWake.payload?._paperclipWakeContext as Record<string, unknown>).wakeCommentIds).toEqual([humanComment]);
expect(await db.select().from(issueComments).where(eq(issueComments.issueId, f.issueId))).toHaveLength(2);
});
it.each(["queued", "prepared"] as const)("rechecks every assignment in a %s run with an allowed coalesced comment", async boundary => {
for (const assignmentFirst of [true, false]) for (const direction of ["outgoing", "incoming"]) {
const f = await seed();
const messageId = await comment(f, "user");
const primary = await wake(f, assignmentFirst ? {} : { commentIds: [messageId], actorType: "user", actorId: "human" });
const primaryOrigin = { actorType: primary.requestedByActorType, actorId: primary.requestedByActorId };
await db.update(agentWakeupRequests).set({ payload: { ...primary.payload, interactionOrigin: primaryOrigin } })
.where(eq(agentWakeupRequests.id, primary.id));
const reason = assignmentFirst ? "issue_commented" : "issue_assigned";
const context = { issueId: f.issueId, wakeReason: reason, ...(assignmentFirst ? { wakeCommentId: messageId, wakeCommentIds: [messageId] } : {}) };
const actorType = assignmentFirst ? "user" : "agent", actorId = assignmentFirst ? "human" : f.sender;
const payload = withInteractionDeliveryOrigin({ issueId: f.issueId, _paperclipWakeContext: context }, actorType, actorId);
await db.transaction(tx => createWakeAdmissionWriter().coalesceIntoActiveExecutionRun(
createAdmissionTransactionScope(f.companyId, tx as unknown as Db), {
companyId: f.companyId, agentId: f.recipient, activeExecutionRunId: f.run.id, activeExecutionRunStatus: "queued",
mergedContextSnapshot: { ...context, wakeCommentId: messageId, wakeCommentIds: [messageId] },
source: "assignment", triggerDetail: "system", payload, requestedByActorType: actorType,
requestedByActorId: actorId, idempotencyKey: null,
},
));
const receipts = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.runId, f.run.id));
expect(receipts).toHaveLength(2);
expect(receipts.find(row => row.id === primary.id)?.payload?.interactionOrigin).toEqual(primaryOrigin);
const assignment = receipts.find(row => row.requestedByActorType === "agent")!;
expect(assignment.payload?.interactionOrigin).toEqual({ actorType: "agent", actorId: f.sender });
if (boundary === "queued") expect((await recheckQueuedRunInteractions(db, await readRun(f.run.id))).denied).toBe(false);
else expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(false);
const permission = direction === "outgoing" ? "canAssignTasksToAgents" : "canReceiveAssignmentsFromAgents";
await restriction(direction === "outgoing" ? f.sender : f.recipient, { [permission]: false });
if (boundary === "queued") expect((await recheckQueuedRunInteractions(db, await readRun(f.run.id))).denied).toBe(true);
else expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(true);
const denied = await readRun(f.run.id);
expect(denied.resultJson?.interactionDeliveryDenied).toBe(true);
expect((denied.resultJson?.interactionPermissionDenials as Record<string, unknown>).assignment)
.toMatchObject({ actorAgentId: f.sender, permission });
expect((await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, assignment.id)))[0])
.toMatchObject({ status: "skipped", error: "agent_interaction_delivery_denied", requestedByActorId: f.sender });
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]?.assigneeAgentId).toBe(f.recipient);
expect(await db.select().from(issueComments).where(eq(issueComments.id, messageId))).toHaveLength(1);
const retained = (await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.companyId, f.companyId)))
.filter(row => row.status === "deferred_issue_execution");
expect(retained).toHaveLength(1);
expect(retained[0]).toMatchObject({ requestedByActorType: "user", requestedByActorId: "human", runId: null });
await restriction(f.sender, {}); await restriction(f.recipient, {});
if (boundary === "queued") expect((await recheckQueuedRunInteractions(db, await readRun(f.run.id))).denied).toBe(true);
else expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(true);
}
});
it("does not apply assignment restrictions to a comment-only pending run", async () => {
const f = await seed(); const message = await comment(f, "agent"); const receipt = await wake(f, { commentIds: [message] });
await restriction(f.sender, { canAssignTasksToAgents: false });
await restriction(f.recipient, { canReceiveAssignmentsFromAgents: false });
expect((await recheckQueuedRunInteractions(db, await readRun(f.run.id))).denied).toBe(false);
expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(false);
await db.update(heartbeatRuns).set({ wakeupRequestId: null, contextSnapshot: {
issueId: f.issueId, wakeReason: "issue_execution_same_name", wakeCommentId: message, wakeCommentIds: [message],
} }).where(eq(heartbeatRuns.id, f.run.id));
await db.delete(agentWakeupRequests).where(eq(agentWakeupRequests.id, receipt.id));
expect((await recheckQueuedRunInteractions(db, await readRun(f.run.id))).denied).toBe(false);
expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(false);
});
it.each(["queued", "prepared"] as const)("recovers an older %s assignment's audit origin when its wake is missing and comments were added", async boundary => {
const f = await seed(); const messageId = await comment(f, "user");
await db.insert(activityLog).values({ companyId: f.companyId, entityType: "issue", entityId: f.issueId,
actorType: "agent", actorId: f.sender, agentId: f.sender, action: "issue.updated",
createdAt: new Date(f.run.createdAt.getTime() - 1), details: { assigneeAgentId: f.recipient } });
await db.update(heartbeatRuns).set({ contextSnapshot: { issueId: f.issueId, wakeReason: "issue_assigned",
wakeCommentId: messageId, wakeCommentIds: [messageId] } }).where(eq(heartbeatRuns.id, f.run.id));
await restriction(f.sender, { canAssignTasksToAgents: false });
if (boundary === "queued") expect((await recheckQueuedRunInteractions(db, await readRun(f.run.id))).denied).toBe(true);
else expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(true);
expect(((await readRun(f.run.id)).resultJson?.interactionPermissionDenials as Record<string, unknown>).assignment)
.toMatchObject({ actorAgentId: f.sender, permission: "canAssignTasksToAgents" });
await restriction(f.sender, {});
if (boundary === "queued") expect((await recheckQueuedRunInteractions(db, await readRun(f.run.id))).denied).toBe(true);
else expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(true);
});
it("makes refused steering permanent while leaving a running task running", async () => {
const f = await seed(); const message = await comment(f, "agent"); const receipt = await wake(f, { commentIds: [message] });
await db.update(heartbeatRuns).set({ status: "running" }).where(eq(heartbeatRuns.id, f.run.id));
await restriction(f.sender, { canCommentOnOtherTasks: false });
const input = { companyId: f.companyId, issueId: f.issueId, agentId: f.recipient, commentId: message, queueId: receipt.id };
await expect(withPluginInteractionOrigin({ companyId: f.companyId, agentId: f.recipient }, () =>
recheckQueuedCommentInteractionDelivery(db, input))).rejects.toMatchObject({ status: 403 });
expect((await readRun(f.run.id)).status).toBe("running");
await restriction(f.sender, {});
await expect(recheckQueuedCommentInteractionDelivery(db, input)).rejects.toMatchObject({ status: 403 });
});
it("refuses unknown older assignment provenance only for an explicitly restricted recipient", async () => {
const f = await seed(); await wake(f, { actorType: null, actorId: null });
await restriction(f.recipient, { canReceiveAssignmentsFromAgents: false });
const result = await recheckQueuedRunInteractions(db, await readRun(f.run.id));
expect(result.denials.assignment).toMatchObject({ code: "agent_interaction_provenance_unavailable" });
const human = await seed(); await wake(human, { actorType: "user", actorId: "human" });
await restriction(human.recipient, { canReceiveAssignmentsFromAgents: false });
expect((await recheckQueuedRunInteractions(db, await readRun(human.run.id))).denied).toBe(false);
});
it.each(["wake", "run"])("recovers old assignment provenance within the same millisecond as its %s receipt", async source => {
const f = await seed();
await db.insert(activityLog).values({ companyId: f.companyId, entityType: "issue", entityId: f.issueId,
actorType: "agent", actorId: f.sender, agentId: f.sender, action: "issue.updated",
details: { assigneeAgentId: f.recipient }, createdAt: sql`'2026-11-09T12:00:00.000100Z'::timestamptz` });
if (source === "wake") {
const receipt = await wake(f, { actorType: null, actorId: null });
await db.update(agentWakeupRequests).set({ requestedAt: sql`'2026-11-09T12:00:00.000900Z'::timestamptz` })
.where(eq(agentWakeupRequests.id, receipt.id));
} else {
await db.update(heartbeatRuns).set({ createdAt: sql`'2026-11-09T12:00:00.000900Z'::timestamptz` })
.where(eq(heartbeatRuns.id, f.run.id));
}
await restriction(f.sender, { canAssignTasksToAgents: false });
const result = await recheckQueuedRunInteractions(db, await readRun(f.run.id));
expect(result.denials.assignment).toMatchObject({ actorAgentId: f.sender, permission: "canAssignTasksToAgents" });
});
it.each(["canCommentOnOtherTasks", "canMentionAgents"])("checks %s for saved parent reports through a system dispatch envelope", async permission => {
const f = await seed(); const receipt = await wake(f, { actorType: "system", actorId: "native-status-committer" });
const payload = withInteractionDeliveryOrigin(receipt.payload, "system", "native-status-wake-dispatch", {
actorType: "agent", actorId: f.sender, operation: "comment",
});
payload.childIssueSummaries = [{ summary: `Report for [@recipient](agent://${f.recipient})` }];
await db.update(agentWakeupRequests).set({ payload, reason: "issue_children_completed" }).where(eq(agentWakeupRequests.id, receipt.id));
await restriction(f.sender, { [permission]: false });
expect((await recheckQueuedRunInteractions(db, await readRun(f.run.id))).denials.assignment)
.toMatchObject({ actorAgentId: f.sender, operation: permission === "canMentionAgents" ? "mention" : "comment", permission });
});
it.each(["dismissed", "pending", "other_agent"])("delivers a dismissal wake only for a person's dismissal of the recipient's question (%s)", async mode => {
const f = await seed();
const [question] = await db.insert(issueThreadInteractions).values({ companyId: f.companyId, issueId: f.issueId,
createdByAgentId: mode === "other_agent" ? f.sender : f.recipient, kind: "ask_user_questions", status: "pending",
title: "Codename", payload: { version: 1, questions: [] },
...(mode === "pending" ? {} : { dismissedAt: new Date(), dismissedByUserId: "human" }) }).returning();
const receipt = await wake(f, { actorType: "system", actorId: "native-status-committer" });
await db.update(agentWakeupRequests).set({ reason: "issue_question_dismissed",
payload: { ...receipt.payload as Record<string, unknown>, dismissedInteractionId: question!.id } })
.where(eq(agentWakeupRequests.id, receipt.id));
await restriction(f.recipient, { canReceiveAssignmentsFromAgents: false });
const result = await recheckQueuedRunInteractions(db, await readRun(f.run.id));
expect(result.denied).toBe(mode !== "dismissed");
});
it("recovers an older automatic retry's initiating agent from its predecessor", async () => {
const f = await seed(); await wake(f);
await db.update(heartbeatRuns).set({ status: "failed", errorCode: "controller_lease_lost" }).where(eq(heartbeatRuns.id, f.run.id));
const [retry] = await db.insert(heartbeatRuns).values({ companyId: f.companyId, agentId: f.recipient, status: "queued",
retryOfRunId: f.run.id, invocationSource: "automation", contextSnapshot: { issueId: f.issueId }, nativeIssueId: f.issueId }).returning();
await restriction(f.sender, { canAssignTasksToAgents: false });
expect((await recheckQueuedRunInteractions(db, retry!)).denials.assignment)
.toMatchObject({ actorAgentId: f.sender, permission: "canAssignTasksToAgents" });
});
it("preserves a server-authorized import routine's source company at delivery", async () => {
const source = await seed(), target = await seed();
const receipt = await wake(target, { actorType: "agent", actorId: source.sender });
await db.update(agentWakeupRequests).set({ payload: withInteractionDeliveryOrigin(receipt.payload,
"agent", source.sender, undefined, source.companyId) }).where(eq(agentWakeupRequests.id, receipt.id));
expect((await recheckQueuedRunInteractions(db, await readRun(target.run.id))).denied).toBe(false);
await restriction(source.sender, { canAssignTasksToAgents: false });
expect((await recheckQueuedRunInteractions(db, await readRun(target.run.id))).denials.assignment)
.toMatchObject({ actorAgentId: source.sender, permission: "canAssignTasksToAgents" });
});
it("uses an originating agent run even when a decision effect is presented as a human comment", async () => {
const f = await seed();
const [origin] = await db.insert(heartbeatRuns).values({ companyId: f.companyId, agentId: f.sender, status: "succeeded" }).returning();
const [message] = await db.insert(issueComments).values({ companyId: f.companyId, issueId: f.issueId,
authorUserId: "human", authorType: "user", createdByRunId: origin!.id, body: "Agent-originated effect" }).returning();
await wake(f, { commentIds: [message!.id], actorType: "user", actorId: "human" });
await restriction(f.sender, { canCommentOnOtherTasks: false });
const result = await recheckQueuedRunInteractions(db, await readRun(f.run.id));
expect(result.denied).toBe(true);
expect(result.denials[message!.id]).toMatchObject({ actorAgentId: f.sender, permission: "canCommentOnOtherTasks" });
});
it("rechecks rendered pending input after preparation and persists unread restart denial", async () => {
const f = await seed(); const message = await comment(f, "agent"); await wake(f, { commentIds: [message] });
expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(false);
await restriction(f.recipient, { canReceiveCommentsFromAgents: false });
expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(true);
await restriction(f.recipient, {});
expect(await filterRunMessageInteractionDelivery(db, { companyId: f.companyId, runId: f.run.id, issueId: f.issueId, commentIds: [message] })).toEqual(new Set());
});
it("retains allowed human input once when a mixed rendered startup must be refused", async () => {
const f = await seed(); const agentComment = await comment(f, "agent"), humanComment = await comment(f, "user");
await wake(f, { commentIds: [agentComment, humanComment] });
await restriction(f.recipient, { canReceiveCommentsFromAgents: false });
expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(true);
expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(true);
const retained = (await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.companyId, f.companyId)))
.filter(row => row.status === "deferred_issue_execution");
expect(retained).toHaveLength(1);
expect(retained[0]).toMatchObject({ requestedByActorType: "user", requestedByActorId: "human", runId: null });
expect((retained[0]!.payload?._paperclipWakeContext as Record<string, unknown>).wakeCommentIds).toEqual([humanComment]);
});
it("does not revoke recovery of an already accepted provider turn", async () => {
const f = await seed(); await wake(f);
await db.update(heartbeatRuns).set({ status: "running", startedAt: new Date() }).where(eq(heartbeatRuns.id, f.run.id));
await db.insert(heartbeatRunEvents).values({ companyId: f.companyId, agentId: f.recipient, runId: f.run.id,
seq: 1, eventType: "turn.accepted", stream: "system", level: "info" });
await restriction(f.recipient, { canReceiveAssignmentsFromAgents: false });
expect(await preparedRunInteractionDeliveryDenied(db, await readRun(f.run.id))).toBe(false);
expect((await readRun(f.run.id)).status).toBe("running");
});
it("cancels refused queued runs through the public queue claim without dispatch", async () => {
const f = await seed(); await wake(f);
await restriction(f.recipient, { canReceiveAssignmentsFromAgents: false });
const callbacks = createHeartbeatQueueTestDependencies(db);
callbacks.setRunStatus.mockImplementation(async (id, status, values) =>
(await db.update(heartbeatRuns).set({ ...values, status }).where(eq(heartbeatRuns.id, id)).returning())[0] ?? null);
callbacks.setWakeupStatus.mockImplementation(async (id, status, values) => {
if (id) await db.update(agentWakeupRequests).set({ ...values, status }).where(eq(agentWakeupRequests.id, id));
});
const queue = createHeartbeatQueue(db, callbacks);
expect(await queue.claimQueuedRun(await readRun(f.run.id))).toBeNull();
expect(callbacks.setRunStatus).toHaveBeenCalledWith(f.run.id, "cancelled", expect.objectContaining({ errorCode: "agent_interaction_delivery_denied" }));
expect(callbacks.executeRun).not.toHaveBeenCalled();
expect((await readRun(f.run.id)).status).toBe("cancelled");
expect(callbacks.releaseIssueExecutionAndPromote).toHaveBeenCalledWith(expect.objectContaining({ id: f.run.id }),
expect.objectContaining({ suppressImmediateRecovery: true }));
});
it.each(["todo", "in_progress"])("leaves a refused %s assignment held across recovery and permission resets", async status => {
const f = await seed(); await wake(f);
await restriction(f.recipient, { canReceiveAssignmentsFromAgents: false });
await recheckQueuedRunInteractions(db, await readRun(f.run.id));
await db.update(heartbeatRuns).set({ status: "cancelled", errorCode: "agent_interaction_delivery_denied", finishedAt: new Date() })
.where(eq(heartbeatRuns.id, f.run.id));
await db.update(issues).set({ status }).where(eq(issues.id, f.issueId));
const [before] = await db.select().from(issues).where(eq(issues.id, f.issueId));
const enqueueWakeup = vi.fn(async () => { throw new Error("Policy denial must not be automatically retried"); });
const recovery = recoveryService(db, { enqueueWakeup });
await restriction(f.recipient, {});
for (let attempt = 0; attempt < 2; attempt++) {
expect(await recovery.reconcileStrandedAssignedIssues({ companyId: f.companyId, issueId: f.issueId }))
.toMatchObject({ skipped: 1, dispatchRequeued: 0, continuationRequeued: 0, escalated: 0 });
}
expect(enqueueWakeup).not.toHaveBeenCalled();
expect((await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]).toEqual(before);
const callbacks = createHeartbeatQueueTestDependencies(db);
callbacks.getAgent.mockImplementation(async id => (await db.select().from(agents).where(eq(agents.id, id)))[0] ?? null);
expect(await createHeartbeatQueue(db, callbacks).enqueueWakeup(f.recipient, {
source: "assignment", reason: "issue_assignment_recovery", requestedByActorType: "system",
requestedByActorId: "recovery", contextSnapshot: { issueId: f.issueId },
})).toBeNull();
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, f.companyId))).toHaveLength(1);
expect(await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.companyId, f.companyId))).toHaveLength(1);
expect(await hasAutomaticInteractionDeliveryHold(db, {
companyId: f.companyId, issueId: f.issueId, agentId: f.recipient, payload: {}, context: {}, actorType: "user", actorId: "human",
})).toBe(false);
const humanMessage = await comment(f, "user");
expect(await hasAutomaticInteractionDeliveryHold(db, {
companyId: f.companyId, issueId: f.issueId, agentId: f.recipient, payload: {}, context: { wakeCommentId: humanMessage }, actorType: "system",
})).toBe(false);
});
});
describe("wake provenance", () => {
it("replaces any payload origin with the authenticated server actor", () => {
expect(withInteractionDeliveryOrigin({ interactionOrigin: { actorType: "user", actorId: "spoof" } }, "agent", "sender"))
.toEqual({ interactionOrigin: { actorType: "agent", actorId: "sender" } });
expect(withInteractionDeliveryOrigin({ interactionOrigin: { actorType: "user" } })).toEqual({});
});
it("captures the host's invoking agent despite a plugin's user presentation", () => {
expect(withPluginInteractionOrigin({ companyId: "company", agentId: "invoker" }, () =>
withInteractionDeliveryOrigin({}, "user", "human")))
.toEqual({ interactionOrigin: { actorType: "agent", actorId: "invoker", actorCompanyId: "company" } });
});
});