mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-22 11:54:35 +01:00
fix(chat): continue a Slack question that a person answered in Paperclip (#22524)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Agents can work on tasks that start in Slack. They can ask the person a question, and Paperclip posts the question card to Slack. > - The person can answer in Slack or on the Paperclip task page. Paperclip then starts a new run to continue the chat, and that run must prove the answer before it can start. > - If the Slack card had a native Respond button and the person answered on the task page, the proof refused the answer. The run failed setup with `reviewed_chat_execution_binding_not_authorized` (`question_response_missing`), and the task stayed blocked. The agent did not receive the answer. > - This pull request accepts that task-page answer when the stored Slack tokens show that the task-page answer replaced the Slack controls. > - The benefit is that a person can answer an agent's Slack question on the task page, and the agent continues the work. ## Linked Issues or Issue Description **What happened?** A task started in Slack. The agent asked a text question. Slack showed the card with a native "Respond" button. The person answered on the Paperclip task page. The continuation run failed during setup with `reviewed_chat_execution_binding_not_authorized`, denial reason `question_response_missing`. Recovery then set the task to blocked. **Steps to reproduce** 1. Connect a Slack bot that has actions and modals. 2. Start a task from a Slack message. 3. Let the agent ask an `ask_user_questions` text question. Slack shows a "Respond" button. 4. Answer the question on the Paperclip task page, not in Slack. 5. The continuation run fails setup with `question_response_missing`. **Expected behavior** The answer reaches the agent, and the continuation run starts. **Cause** The task-page answer expires all Slack tokens for the question with `interaction_resolved_elsewhere`. The answer proof in `resolveQuestionResponseChain` accepted a Slack task-page answer only if no Slack answer tokens existed at all and every card action was a link. A card with a native Respond button always failed both checks. Related: #17255 added the Slack native question forms and the task-page answer proof. ## What Changed - `external-chat-question-response.ts`: the task-page answer query now ignores Slack answer tokens only when their status is `expired` with result code `interaction_resolved_elsewhere`. Any other token state (processed, issued, or expired for another reason) still makes the proof use the Slack action path, as before. - `external-chat-question-response.ts`: a Slack card can now have callback actions, but only when each callback `actionId` matches a superseded token (`question_answer` or `question_form_open`) of the same interaction, publication, endpoint and conversation. GitHub cards still must have only links. - `external-chat-wait.integration.test.ts`: the Board-answer fixture can now publish a Slack text question with a native form. A new regression test answers it on the task page and attests the continuation. The Board-answer refusal matrix now runs for this card too, with two new cases: a Slack token that the answer did not supersede, and an unknown callback button. ## Verification - Red before the fix: the new test `attests a Slack question answered in Paperclip after its native form was published` failed with denial `["question_response_missing"]`. This is the same denial reason as the failed run. - Green after the fix: the same test passes. The continuation input contains the answer. - `pnpm vitest run src/services/native-runtime/external-chat-wait.integration.test.ts` (in `server`): all 301 tests pass. This includes the 14 refusal cases for the native-form card (other user, viewer, revoked membership, revoked identity, wrong target, wrong wake, unpublished card, processed Slack action, deleted source message, paused endpoint, revoked resource, changed answer hash, token not superseded, unknown callback). - Mutation check: when I let any callback through, the `foreign_callback` case fails. When I ignore token state, the `unsuperseded_token` case fails. - `photon/channel.integration.test.ts` and `agent-action-audit-routes.test.ts` pass. - `heartbeat-process-recovery.test.ts`: 4 sandbox-lease tests fail in my sandbox with `isRetryableEnvironmentHttpStatus is not a function`. They fail the same way on unmodified master, so this change does not cause them. - The full server `tsc` was killed by the sandbox memory limit. CI is the type gate. ## Risks - This change makes the answer proof accept more answers, so I kept it narrow. A Slack task-page answer is accepted only when the stored tokens prove that the task-page answer replaced every Slack answer token. All other checks do not change: the same person must answer, membership and identity link must be current, the endpoint must be active, the publication must be published, and the answer hash must match. - If a Slack answer and a task-page answer race, any token that the task-page answer did not expire still refuses the proof. This fails closed, as before. - No migration. No change to GitHub, Speko, Photon or Slack native-answer paths. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), with tool use and code execution. ## 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 - [ ] 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 - [ ] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: paperclipbugbot <356415228+paperclipbugbot@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2 files changed
+97
-10
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { photonAnswersMatch } from "../photon/interactions.js";
|
||||
import { and, or, eq, lte, inArray, notExists, sql } from "drizzle-orm";
|
||||
import { and, or, eq, lte, inArray, not, notExists, sql } from "drizzle-orm";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import {
|
||||
agentWakeupRequests,
|
||||
@@ -714,6 +714,10 @@ async function resolveQuestionResponseChain(
|
||||
"question_answer",
|
||||
"question_form_submit",
|
||||
]),
|
||||
not(and(
|
||||
eq(chatActions.status, "expired"),
|
||||
sql`${chatActions.result}->>'code' = 'interaction_resolved_elsewhere'`,
|
||||
)!),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -787,13 +791,42 @@ async function resolveQuestionResponseChain(
|
||||
if (!action) {
|
||||
const card = record(record(publication.payload).card);
|
||||
const links = Array.isArray(card.actions) ? card.actions.map(record) : [];
|
||||
const supersededActionIds = new Set(
|
||||
provider === "slack" && links.some((link) => link.type === "callback")
|
||||
? (
|
||||
await tx
|
||||
.select({ providerActionId: chatActions.providerActionId })
|
||||
.from(chatActions)
|
||||
.where(
|
||||
and(
|
||||
eq(chatActions.companyId, binding.companyId),
|
||||
eq(chatActions.endpointId, endpoint.id),
|
||||
eq(chatActions.conversationId, conversation.id),
|
||||
inArray(chatActions.kind, ["question_answer", "question_form_open"]),
|
||||
eq(chatActions.status, "expired"),
|
||||
sql`${chatActions.result}->>'code' = 'interaction_resolved_elsewhere'`,
|
||||
sql`${chatActions.payload}->>'interactionId' = ${interaction.id}`,
|
||||
sql`${chatActions.payload}->>'publicationId' = ${publication.id}::text`,
|
||||
),
|
||||
)
|
||||
).map((row) => row.providerActionId)
|
||||
: [],
|
||||
);
|
||||
if (
|
||||
!boardAnswer ||
|
||||
interaction.resolvedByRunId !== null ||
|
||||
publication.payload.progressState !== "waiting_for_input" ||
|
||||
card.kind !== "question" ||
|
||||
(card.actions !== undefined && !Array.isArray(card.actions)) ||
|
||||
links.some((link) => link.type !== "link") ||
|
||||
links.some(
|
||||
(link) =>
|
||||
link.type !== "link" &&
|
||||
!(
|
||||
link.type === "callback" &&
|
||||
typeof link.actionId === "string" &&
|
||||
supersededActionIds.has(link.actionId)
|
||||
),
|
||||
) ||
|
||||
!publication.publishedAt ||
|
||||
publication.publishedAt > interaction.resolvedAt ||
|
||||
(provider === "github" && (
|
||||
|
||||
@@ -731,12 +731,18 @@ describe("native external-chat response wait", () => {
|
||||
};
|
||||
}
|
||||
|
||||
async function seedBoardAnsweredTurn(withLink = true, provider: "github" | "slack" = "github", oversized = false) {
|
||||
async function seedBoardAnsweredTurn(withLink = true, provider: "github" | "slack" = "github", oversized = false, nativeSlackForm = false) {
|
||||
const fixture = await seedWaitTurn(provider);
|
||||
if (provider === "slack") {
|
||||
await db.update(chatEndpointResources).set({ type: "channel" }).where(eq(chatEndpointResources.id, fixture.resourceId));
|
||||
await db.update(chatConversations).set({ isDirectMessage: false }).where(eq(chatConversations.id, fixture.conversationId));
|
||||
}
|
||||
if (nativeSlackForm) {
|
||||
const [endpoint] = await db.select({ capabilities: chatEndpoints.capabilities }).from(chatEndpoints)
|
||||
.where(eq(chatEndpoints.id, fixture.endpointId));
|
||||
await db.update(chatEndpoints).set({ capabilities: { ...endpoint!.capabilities, cards: true, actions: true, modals: true } })
|
||||
.where(eq(chatEndpoints.id, fixture.endpointId));
|
||||
}
|
||||
const gate = await seedPriorCompletionReview(fixture);
|
||||
const [current] = await db
|
||||
.select()
|
||||
@@ -785,7 +791,13 @@ describe("native external-chat response wait", () => {
|
||||
kind: "ask_user_questions",
|
||||
sourceRunId,
|
||||
continuationPolicy: "wake_assignee",
|
||||
payload: {
|
||||
payload: nativeSlackForm ? {
|
||||
version: 1,
|
||||
questionSet: {
|
||||
schema: "paperclip.question_set.v1",
|
||||
questions: [{ id: "color", prompt: "Name a color", required: true, answerMode: "text" }],
|
||||
},
|
||||
} : {
|
||||
version: 1,
|
||||
questions: [
|
||||
{
|
||||
@@ -827,7 +839,7 @@ describe("native external-chat response wait", () => {
|
||||
await interactionSvc.answerQuestions(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
interaction.id,
|
||||
{ answers: [{ questionId: "color", optionIds: ["cobalt"] }] },
|
||||
{ answers: [nativeSlackForm ? { questionId: "color", optionIds: [], otherText: "cobalt" } : { questionId: "color", optionIds: ["cobalt"] }] },
|
||||
{ userId: fixture.userId },
|
||||
);
|
||||
const [delivery] = await db
|
||||
@@ -955,6 +967,26 @@ describe("native external-chat response wait", () => {
|
||||
expect(JSON.stringify(responses)).toContain("cobalt");
|
||||
});
|
||||
|
||||
it("attests a Slack question answered in Paperclip after its native form was published", async () => {
|
||||
const fixture = await seedBoardAnsweredTurn(true, "slack", false, true);
|
||||
const [publication] = await db.select().from(chatPublications).where(eq(chatPublications.id, fixture.publicationId));
|
||||
expect(publication!.payload.card?.actions).toEqual(expect.arrayContaining([
|
||||
expect.objectContaining({ type: "callback" }),
|
||||
]));
|
||||
const tokens = await db.select().from(chatActions).where(eq(chatActions.companyId, fixture.companyId));
|
||||
expect(tokens.map(token => token.kind)).toEqual(expect.arrayContaining(["question_form_submit"]));
|
||||
expect(tokens.some(token => token.status === "processed")).toBe(false);
|
||||
const denials: string[] = [];
|
||||
await attestReviewedExternalChatRun({ db, ...fixture, contextSnapshot: { ...fixture.context }, onDenied: reason => denials.push(reason) });
|
||||
expect(denials).toEqual([]);
|
||||
expect(await resolveExternalChatQuestionResponse(db, fixture, fixture.context, "read", true)).not.toBeNull();
|
||||
await attestAnswer(fixture);
|
||||
await db.update(heartbeatRuns).set({ contextSnapshot: fixture.context }).where(eq(heartbeatRuns.id, fixture.runId));
|
||||
const responses = await materializeExternalChatQuestionResponseInput({ db, binding: fixture, contextSnapshot: fixture.context });
|
||||
expect(responses).toHaveLength(1);
|
||||
expect(JSON.stringify(responses)).toContain("cobalt");
|
||||
});
|
||||
|
||||
it.each((["github", "slack"] as const).flatMap(provider => [true, false].map(withLink => ({ provider, withLink }))))(
|
||||
"attests a native $provider question answered in Board without inventing a provider action (link: $withLink)",
|
||||
async ({ provider, withLink }) => {
|
||||
@@ -1020,7 +1052,7 @@ describe("native external-chat response wait", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it.each((["github", "slack"] as const).flatMap(provider => ([
|
||||
it.each((["github", "slack", "slack_native_form"] as const).flatMap(variant => ([
|
||||
"different_user",
|
||||
"viewer",
|
||||
"membership_revoked",
|
||||
@@ -1035,10 +1067,32 @@ describe("native external-chat response wait", () => {
|
||||
"endpoint_paused",
|
||||
"resource_revoked",
|
||||
"answer_hash",
|
||||
] as const).filter(change => provider === "github" || (change !== "generation" && change !== "missing_runtime")).map(change => ({ provider, change }))))(
|
||||
"rejects $provider Board answer authority after $change",
|
||||
async ({ provider, change }) => {
|
||||
const fixture = await seedBoardAnsweredTurn(true, provider);
|
||||
"unsuperseded_token",
|
||||
"foreign_callback",
|
||||
] as const).filter(change => variant === "github"
|
||||
? change !== "unsuperseded_token" && change !== "foreign_callback"
|
||||
: change !== "generation" && change !== "missing_runtime"
|
||||
&& (variant === "slack_native_form" || (change !== "unsuperseded_token" && change !== "foreign_callback")))
|
||||
.map(change => ({ variant, provider: variant === "github" ? "github" as const : "slack" as const, change }))))(
|
||||
"rejects $variant Board answer authority after $change",
|
||||
async ({ variant, provider, change }) => {
|
||||
const fixture = await seedBoardAnsweredTurn(true, provider, false, variant === "slack_native_form");
|
||||
if (change === "unsuperseded_token")
|
||||
await db
|
||||
.update(chatActions)
|
||||
.set({ status: "issued", result: null })
|
||||
.where(and(eq(chatActions.companyId, fixture.companyId), eq(chatActions.kind, "question_form_submit")));
|
||||
if (change === "foreign_callback") {
|
||||
const [publication] = await db.select().from(chatPublications).where(eq(chatPublications.id, fixture.publicationId));
|
||||
const card = publication!.payload.card!;
|
||||
await db
|
||||
.update(chatPublications)
|
||||
.set({ payload: { ...publication!.payload, card: { ...card, actions: [
|
||||
...(card.actions ?? []),
|
||||
{ type: "callback", actionId: `pcf:${randomUUID()}`, label: "Other" },
|
||||
] } } })
|
||||
.where(eq(chatPublications.id, fixture.publicationId));
|
||||
}
|
||||
if (change === "different_user") {
|
||||
const otherUser = `other-${randomUUID()}`;
|
||||
await db
|
||||
|
||||
Reference in new issue
Block a user