Skip to content

The open-source app everyone uses to manage agents at work

fix: preserve chat message bindings in review recovery (#13818)

Devin Foley110d176fc970
diff --git a/doc/execution-semantics.md b/doc/execution-semantics.md
index 2c53d8024a..ab60fffa37 100644
--- a/doc/execution-semantics.md
+++ b/doc/execution-semantics.md
@@ -353,6 +353,12 @@ The valid action-path primitives are:
- a first-class blocker chain whose unresolved leaf issues are themselves healthy
- an open explicit recovery action that names the owner and action needed to restore liveness
+A bounded review-path recovery for a task from a supported external-chat
+provider retains the source run's admitted message IDs. It does not inherit checkout or authorization
+markers. Before dispatch, Paperclip verifies the recovery run's task ownership
+and current conversation, endpoint, and principal access for every message.
+Missing message references or revoked access still prevent execution.
+
### Durable external waits and heartbeat finalization
An external wait counts as a live or waiting path only when the next move survives the current heartbeat and is represented in Paperclip's durable control-plane state. Valid external-wait shapes are:
diff --git a/server/src/__tests__/heartbeat-reviewed-chat-binding.integration.test.ts b/server/src/__tests__/heartbeat-reviewed-chat-binding.integration.test.ts
index 88e4192bf1..bdb3c06c70 100644
--- a/server/src/__tests__/heartbeat-reviewed-chat-binding.integration.test.ts
+++ b/server/src/__tests__/heartbeat-reviewed-chat-binding.integration.test.ts
@@ -30,14 +30,16 @@ import {
listAuthorizedChatAttachments,
resolveExternalChatResponseWaitAuthorization,
} from "../services/native-runtime/chat-attachment-reuse.js";
+import { decideIssueReviewPathRecovery } from "../services/recovery/review-path-recovery.js";
import { resolveCurrentWakeCommentsBinding } from "../services/native-runtime/current-wake-comments.js";
-describe("reviewed external-chat execution binding", () => {
+describe.each(["slack", "discord"] as const)("reviewed %s execution binding", (provider) => {
let temporary: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
let db: ReturnType<typeof createDb>;
const companyId = randomUUID(),
agentId = randomUUID(),
issueId = randomUUID(),
+ otherIssueId = randomUUID(),
runId = randomUUID();
const endpointId = randomUUID(),
resourceId = randomUUID(),
@@ -49,7 +51,7 @@ describe("reviewed external-chat execution binding", () => {
userId = "reviewed-chat-user";
const context = {
issueId,
- source: "chat:discord",
+ source: `chat:${provider}`,
wakeReason: "External chat message received",
wakeCommentIds: [commentId],
commentId,
@@ -93,6 +95,15 @@ describe("reviewed external-chat execution binding", () => {
workMode: "standard",
assigneeAgentId: agentId,
});
+ await db.insert(issues).values({
+ id: otherIssueId,
+ companyId,
+ title: "Unrelated conversation",
+ issueNumber: 2,
+ identifier: "RCB-2",
+ status: "in_review",
+ assigneeAgentId: agentId,
+ });
await db
.insert(heartbeatRuns)
.values({
@@ -132,7 +143,7 @@ describe("reviewed external-chat execution binding", () => {
.values({
id: applicationId,
companyId,
- applicationKey: `chat:discord:${endpointId}`,
+ applicationKey: `chat:${provider}:${endpointId}`,
name: "Discord",
type: "chat",
status: "active",
@@ -144,7 +155,7 @@ describe("reviewed external-chat execution binding", () => {
companyId,
applicationId,
name: "Discord",
- uid: `chat-discord-${endpointId}`,
+ uid: `chat-${provider}-${endpointId}`,
connectionPurpose: "channel",
transport: "chat_sdk",
status: "active",
@@ -156,7 +167,7 @@ describe("reviewed external-chat execution binding", () => {
id: endpointId,
companyId,
connectionId,
- provider: "discord",
+ provider,
publicId: randomUUID(),
assignedAgentId: agentId,
status: "active",
@@ -193,7 +204,7 @@ describe("reviewed external-chat execution binding", () => {
.values({
id: principalId,
companyId,
- provider: "discord",
+ provider,
providerAccountId: "guild-1",
externalId: "user-1",
kind: "user",
@@ -296,7 +307,7 @@ describe("reviewed external-chat execution binding", () => {
expect(wake).toMatchObject({
checkedOutByHarness: false,
externalChatExecutionBound: true,
- externalChatProvider: "discord",
+ externalChatProvider: provider,
});
const prompt = renderPaperclipWakePrompt(wake);
expect(prompt).toContain("not a checkout, approval");
@@ -304,6 +315,84 @@ describe("reviewed external-chat execution binding", () => {
expect(prompt).not.toContain("checked out the issue for this run");
});
+ async function withReviewRecovery(
+ check: (recovery: Record<string, unknown>) => Promise<void>,
+ ) {
+ const decision = decideIssueReviewPathRecovery({
+ issueId,
+ sourceRunId: randomUUID(),
+ assigneeAgentId: agentId,
+ contextSnapshot: {
+ ...context,
+ paperclipHarnessCheckedOut: true,
+ paperclipExternalChatExecutionBound: true,
+ },
+ reviewAttention: { state: "stalled", paths: [], reason: "Review path consumed" },
+ existingWake: false,
+ });
+ expect(decision.kind).toBe("enqueue");
+ if (decision.kind !== "enqueue") return;
+ await db.update(heartbeatRuns)
+ .set({ contextSnapshot: decision.contextSnapshot })
+ .where(eq(heartbeatRuns.id, runId));
+ try {
+ await check(decision.contextSnapshot);
+ } finally {
+ await db.update(heartbeatRuns)
+ .set({ contextSnapshot: context })
+ .where(eq(heartbeatRuns.id, runId));
+ }
+ }
+
+ it("reauthorizes a review recovery from its retained message batch without approving the task", async () => {
+ const [before] = await db.select().from(issues).where(eq(issues.id, issueId));
+ await withReviewRecovery(async (recovery) => {
+ expect(recovery).not.toHaveProperty("paperclipHarnessCheckedOut");
+ expect(recovery).not.toHaveProperty("paperclipExternalChatExecutionBound");
+ await expect(attest(recovery)).resolves.toBe(true);
+ expect((await db.select().from(issues).where(eq(issues.id, issueId)))[0]).toEqual(before);
+ expect((await db.select().from(issueThreadInteractions)
+ .where(eq(issueThreadInteractions.id, interactionId)))[0].status).toBe("pending");
+ });
+ });
+
+ it("denies recovery after access revocation or a changed company, task, or message binding", async () => {
+ await withReviewRecovery(async (recovery) => {
+ await expect(attest(recovery)).resolves.toBe(true);
+ await expect(attestReviewedExternalChatRun({
+ db, ...binding, companyId: randomUUID(), contextSnapshot: recovery,
+ })).resolves.toBe(false);
+ await expect(attestReviewedExternalChatRun({
+ db, ...binding, issueId: otherIssueId, contextSnapshot: recovery,
+ })).resolves.toBe(false);
+ await expect(attest({ ...recovery, wakeCommentIds: [randomUUID()] })).resolves.toBe(false);
+ await db.update(companyMemberships).set({ status: "suspended" })
+ .where(eq(companyMemberships.principalId, userId));
+ try {
+ await expect(attest(recovery)).resolves.toBe(false);
+ } finally {
+ await db.update(companyMemberships).set({ status: "active" })
+ .where(eq(companyMemberships.principalId, userId));
+ }
+ await db.update(chatEndpointResources).set({ enabled: false })
+ .where(eq(chatEndpointResources.id, resourceId));
+ try {
+ await expect(attest(recovery)).resolves.toBe(false);
+ } finally {
+ await db.update(chatEndpointResources).set({ enabled: true })
+ .where(eq(chatEndpointResources.id, resourceId));
+ }
+ await db.update(chatConversations).set({ issueId: otherIssueId })
+ .where(eq(chatConversations.id, conversationId));
+ try {
+ await expect(attest(recovery)).resolves.toBe(false);
+ } finally {
+ await db.update(chatConversations).set({ issueId })
+ .where(eq(chatConversations.id, conversationId));
+ }
+ });
+ });
+
it("does not trust a supplied marker, owner mismatch, different wake batch, or wrong provider", async () => {
await db
.update(issues)
@@ -338,7 +427,7 @@ describe("reviewed external-chat execution binding", () => {
} finally {
await db
.update(chatEndpoints)
- .set({ provider: "discord" })
+ .set({ provider })
.where(eq(chatEndpoints.id, endpointId));
}
});
@@ -493,7 +582,7 @@ describe("reviewed external-chat execution binding", () => {
await expect(
resolveCurrentWakeCommentsBinding(db, binding),
).resolves.toMatchObject({
- provider: "discord",
+ provider,
commentIds: [commentId],
});
await db
diff --git a/server/src/services/native-runtime/chat-attachment-reuse.ts b/server/src/services/native-runtime/chat-attachment-reuse.ts
index 61b7f1b727..5afbd96080 100644
--- a/server/src/services/native-runtime/chat-attachment-reuse.ts
+++ b/server/src/services/native-runtime/chat-attachment-reuse.ts
@@ -29,6 +29,7 @@ import {
import { getStorageService } from "../../storage/index.js";
import type { StorageService } from "../../storage/types.js";
import { issueService } from "../issues.js";
+import { boundExternalChatProvider } from "./external-chat-provider.js";
import { resolveExternalChatQuestionResponse } from "./external-chat-question-response.js";
export const LIST_CHAT_ATTACHMENTS_TOOL_NAME = "list_chat_attachments";
@@ -418,17 +419,7 @@ export async function authorizeChatConversationForBoundRun(
context = answer.authorizationContext;
}
const source = typeof context.source === "string" ? context.source : "";
- const provider = [
- "slack",
- "github",
- "discord",
- "microsoft-teams",
- "telegram",
- "imessage-photon",
- ].find(
- (candidate) =>
- source === `chat:${candidate}` || source === `chat:${candidate}:recovery`,
- );
+ const provider = boundExternalChatProvider(source);
const commentIds = wakeCommentIds(context);
if (
!provider ||
@@ -549,17 +540,7 @@ function externalChatWaitCandidate(
): { provider: string; commentIds: string[] } | null {
const context = record(contextSnapshot);
const source = typeof context.source === "string" ? context.source : "";
- const provider = [
- "slack",
- "github",
- "discord",
- "microsoft-teams",
- "telegram",
- "imessage-photon",
- ].find(
- (candidate) =>
- source === `chat:${candidate}` || source === `chat:${candidate}:recovery`,
- );
+ const provider = boundExternalChatProvider(source);
const commentIds = wakeCommentIds(context);
const wake = record(context.paperclipWake);
const wakeIssue = record(wake.issue);
diff --git a/server/src/services/native-runtime/external-chat-provider.ts b/server/src/services/native-runtime/external-chat-provider.ts
new file mode 100644
index 0000000000..e16a310836
--- /dev/null
+++ b/server/src/services/native-runtime/external-chat-provider.ts
@@ -0,0 +1,16 @@
+// Providers supported by the run-bound external-chat authorization path.
+// AgentMail uses the email inbox path and is not admitted by this boundary.
+const BOUND_EXTERNAL_CHAT_PROVIDERS = [
+ "slack",
+ "github",
+ "discord",
+ "microsoft-teams",
+ "telegram",
+ "imessage-photon",
+] as const;
+
+export function boundExternalChatProvider(source: unknown) {
+ return BOUND_EXTERNAL_CHAT_PROVIDERS.find(
+ (provider) => source === `chat:${provider}` || source === `chat:${provider}:recovery`,
+ ) ?? null;
+}
diff --git a/server/src/services/recovery/review-path-recovery.test.ts b/server/src/services/recovery/review-path-recovery.test.ts
index 16f27b5f99..5e651d4594 100644
--- a/server/src/services/recovery/review-path-recovery.test.ts
+++ b/server/src/services/recovery/review-path-recovery.test.ts
@@ -57,6 +57,88 @@ describe("review-path recovery", () => {
expect(duplicate).toEqual({ kind: "skip", reason: "review-path recovery wake already exists" });
});
+ it.each(["chat:slack", "chat:slack:recovery", "chat:discord"])(
+ "retains the admitted message batch for %s without inheriting authorization",
+ (source) => {
+ const decision = decideIssueReviewPathRecovery({
+ issueId: "issue-1",
+ sourceRunId: "run-1",
+ assigneeAgentId: "agent-1",
+ contextSnapshot: {
+ issueId: "issue-1",
+ source,
+ wakeCommentIds: ["comment-1", "comment-2", "comment-1"],
+ wakeCommentId: "stale-comment",
+ commentId: "stale-comment",
+ paperclipHarnessCheckedOut: true,
+ paperclipExternalChatExecutionBound: true,
+ paperclipWake: { checkedOutByHarness: true },
+ sessionId: "old-session",
+ instruction: "old instruction",
+ },
+ reviewAttention: stalled,
+ existingWake: false,
+ });
+ expect(decision.kind).toBe("enqueue");
+ if (decision.kind !== "enqueue") return;
+ expect(decision.contextSnapshot).toMatchObject({
+ issueId: "issue-1",
+ source,
+ wakeReason: ISSUE_REVIEW_PATH_LOST_WAKE_REASON,
+ wakeCommentIds: ["comment-1", "comment-2"],
+ wakeCommentId: "comment-2",
+ commentId: "comment-2",
+ });
+ for (const key of [
+ "paperclipHarnessCheckedOut",
+ "paperclipExternalChatExecutionBound",
+ "paperclipWake",
+ "sessionId",
+ ]) expect(decision.contextSnapshot).not.toHaveProperty(key);
+ expect(decision.contextSnapshot.instruction).not.toBe("old instruction");
+ },
+ );
+
+ it.each([undefined, null, "comment-1", [], [null, 1, " "]])(
+ "does not invent an admitted message batch from invalid input %j",
+ (wakeCommentIds) => {
+ const decision = decideIssueReviewPathRecovery({
+ issueId: "issue-1",
+ sourceRunId: "run-1",
+ assigneeAgentId: "agent-1",
+ contextSnapshot: {
+ source: "chat:slack",
+ wakeCommentIds,
+ wakeCommentId: "unproven-comment",
+ },
+ reviewAttention: stalled,
+ existingWake: false,
+ });
+ expect(decision.kind).toBe("enqueue");
+ if (decision.kind !== "enqueue") return;
+ expect(decision.contextSnapshot.source).toBe("chat:slack");
+ expect(decision.contextSnapshot).not.toHaveProperty("wakeCommentIds");
+ expect(decision.contextSnapshot).not.toHaveProperty("wakeCommentId");
+ },
+ );
+
+ it.each(["issue.comment", "chat:agentmail", "chat:agentmail:recovery"])(
+ "does not carry a chat batch for unsupported source %s",
+ (source) => {
+ const decision = decideIssueReviewPathRecovery({
+ issueId: "issue-1",
+ sourceRunId: "run-1",
+ assigneeAgentId: "agent-1",
+ contextSnapshot: { source, wakeCommentIds: ["comment-1"] },
+ reviewAttention: stalled,
+ existingWake: false,
+ });
+ expect(decision.kind).toBe("enqueue");
+ if (decision.kind !== "enqueue") return;
+ expect(decision.contextSnapshot).not.toHaveProperty("wakeCommentIds");
+ },
+ );
+
it("does not requeue when the bounded recovery run also ends pathless", () => {
const decision = decideIssueReviewPathRecovery({
issueId: "issue-1",
diff --git a/server/src/services/recovery/review-path-recovery.ts b/server/src/services/recovery/review-path-recovery.ts
index 27ef24234f..afcd315647 100644
--- a/server/src/services/recovery/review-path-recovery.ts
+++ b/server/src/services/recovery/review-path-recovery.ts
@@ -1,5 +1,7 @@
import { createHash } from "node:crypto";
import type { IssueReviewAttention } from "@paperclipai/shared";
+import { boundExternalChatProvider } from "../native-runtime/external-chat-provider.js";
+import { extractWakeCommentIds } from "../../modules/run-dispatch/index.js";
import { withRecoveryContext } from "./status-only-context.js";
export const ISSUE_REVIEW_PATH_LOST_WAKE_REASON = "issue_review_path_lost";
@@ -100,6 +102,9 @@ export function decideIssueReviewPathRecovery(input: {
});
if (input.existingWake) return { kind: "skip", reason: "review-path recovery wake already exists" };
+ const source = readNonEmptyString(context.source) ?? "heartbeat.review_path_disposition";
+ const chatCommentIds = boundExternalChatProvider(source) ? extractWakeCommentIds(context) : [];
+
const payload = withRecoveryContext({
issueId: input.issueId,
taskId: input.issueId,
@@ -119,7 +124,15 @@ export function decideIssueReviewPathRecovery(input: {
contextSnapshot: withRecoveryContext({
...payload,
wakeReason: ISSUE_REVIEW_PATH_LOST_WAKE_REASON,
- source: readNonEmptyString(context.source) ?? "heartbeat.review_path_disposition",
+ source,
+ // Keep the admitted message references, not the source run's authority.
+ // Dispatch must prove the new run owns this task and recheck current
+ // endpoint, conversation, and principal access for the entire batch.
+ ...(chatCommentIds.length > 0 ? {
+ wakeCommentIds: chatCommentIds,
+ wakeCommentId: chatCommentIds.at(-1),
+ commentId: chatCommentIds.at(-1),
+ } : {}),
}, "normal_model"),
};
}