fix(slack): retain DM thread tasks after completion (#19486)

## Thinking Path

> - Paperclip manages AI agents and their work.
> - Slack conversations store that work in tasks and comments.
> - A native thread must keep its task when work finishes.
> - The server treated all DMs as linear conversations.
> - A reply in a completed DM thread therefore started a new task
without its earlier comments.
> - This pull request uses the native thread identity for task
continuity.
> - People can continue the same work with its saved context.

## Linked Issues or Issue Description

**What happened?**

A reply in a Slack DM thread created a new task after the preceding task
was Done or Cancelled. It also created a new task when the saved
conversation was completed. The new task lacked earlier comments.

**Expected behavior**

A native Slack thread keeps one task in channels and DMs. Later replies
retain earlier user and agent comments. Top-level DMs keep their
existing task generation rules.

**Steps to reproduce**

1. Start work in a Slack DM thread.
2. Mark its task Done.
3. Reply in the same thread.
4. Before this fix, the reply creates another task.

**Paperclip version or commit**

Reproduced from master at eb5c24b61c.

**Deployment mode**

Built from source. Tests use local PostgreSQL and provider fixtures.

Related PR: #16151 covers the DM mention exemption. This fix preserves
that behavior. GitHub searches found no duplicate fix for completion
routing.

## What Changed

- Detect Slack native thread roots in the Slack module.
- Classify native DM threads as persistent threads for task routing.
- Select the resource type from the actual DM destination. Keep DM
access checks.
- Preserve DM task controls. Select the latest DM task even after
completion. Require a delivered control receipt before an explicit new
task generation.
- Add completion, duplicate, saved-history, separate-root, Idle, and
explicit-control regressions.
- Document the task boundary. Correct only the inaccurate DM-thread
statement in the existing feature recipe.

## Verification

- Before the production edit, the new PostgreSQL regression failed in
all three threaded-DM completion cases: two conversations existed
instead of one. The channel-thread and top-level-DM controls passed.
- The initial eight routing cases passed. CI then found five existing
slash-control failures. All five reproduced locally. Greptile reported
the same regression at 4/5.
- The first correction passed all 16 focused PostgreSQL cases. Secondary
review then found that slash commands skipped Done, Cancelled, and
completed DM tasks. Four new cases failed before the lookup change.
Commit 23ff4f04f passed all 20 focused cases, including status, explicit
new/close, delivered receipts, and a new task generation. The original
same-thread continuity cases still pass.
- Command: `pnpm exec vitest run
server/src/__tests__/chat-channels.integration.test.ts -t 'preserves
Slack thread history across completion|keeps separate Slack DM thread
roots|returns two answered Slack turns to Idle|binds Slack DM slash
controls|treats D-prefixed Slack status|serializes duplicate Slack
admission with an owned provider-reply settlement|suppresses a queued
provider reply when destination reach|continues to accept authenticated
Slack slash close/new'`.
- Map validation and `git diff --check` passed. Map validation proves
structure only.
- All 107 neighboring Slack adapter, SDK runtime, and receipt tests
passed across three files.
- The initial complete server typecheck passed with
`PAPERCLIP_TYPECHECK_LOW_MEMORY=1 pnpm --filter @paperclipai/server
typecheck`, including all three test-tree shards.
- Full local recursive typecheck and build stopped because this
workspace has no `cargo`. A separate default server typecheck was killed
under concurrent memory load; its lower-memory rerun passed. The broad
local test run was interrupted to free memory, so no full-local-suite
pass is claimed. The complete server typecheck passed again for
correction 367b6f9b3, including all three test-tree shards. The [full CI
pipeline](https://github.com/paperclipai/paperclip/actions/runs/42250458200)
passed for exact head 367b6f9b3f. This
includes typecheck, build, all chat and other test lanes, browser E2E,
Runner, and aggregate verify. The first CI run passed broad typecheck,
build, E2E, Runner, and other test groups, but its chat failures remain
recorded above.
- The initial regression launch could not load the unbuilt plugin SDK.
It ran after building the required workspace dependencies. A test-only
resource assertion was corrected to read persisted DM resources, which
the settings list omits. The completed-conversation case preserves an
existing review status.
- The initial correction test used a follow-up timestamp older than the
delivered control. The chronology gate correctly rejected it. A current
provider timestamp now verifies the new generation without weakening
that gate.
- Greptile's map finding was addressed by removing recipe expansions and
all agent-access map changes. Both review threads have replies and are
resolved. Greptile reviewed exact head
367b6f9b3f at [5/5 with zero new
findings](https://github.com/paperclipai/paperclip/pull/19486#issuecomment-6689117840).
The secondary Paperclip Review rated that head 4/5 and found the
completed-task slash lookup error. Commit 23ff4f04f fixes this finding.
Greptile and Paperclip Review both rate head
23ff4f04f6 at 5/5. All three review
threads are resolved. The [full CI
pipeline](https://github.com/paperclipai/paperclip/actions/runs/42255926149)
now passes on this exact head. This includes typecheck, build, all test
lanes, browser E2E, Runner checks, Canary Dry Run, and aggregate verify.
All checks are terminal: 54 success, 1 neutral, and 3 skipped. Snyk also
passes. The PR has no merge conflict.
- The complete server typecheck passed again on 23ff4f04f, including all
three test-tree shards.
- The new test first asserted the conversation-list state. That API
displays terminal tasks as completed before the control is sent. The
test now reads stored binding state to check that completion waits for
the delivered receipt.
- No live Slack send or deployment was performed.

## Risks

- Native DM threads now reopen the existing task after Done or
Cancelled. Explicit new/close controls still release the binding after
their response is delivered. Top-level DMs still start new generations
after completion.
- Existing split tasks stay separate. Future replies use the latest
saved binding. This change does not merge old tasks or copy surrounding
DM history into a newly created thread.
- No schema, credential, provider scope, or permission changes are
required.

## Model Used

OpenAI Codex, based on GPT-6, with reasoning, tool use, repository
editing, and code execution. The runtime does not expose a more precise
serving model ID or context-window size.

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

---------

Co-authored-by: Paperclip <[email protected]>
This commit is contained in:
DottaandPaperclip authored and GitHub committed 2026-11-07 08:35:29 -06:00
1 parent c740cb9ded
commit 2e52dfa864
10 files changed
+144 -23

No files matched your search

+10
View File
@@ -102,6 +102,16 @@ subscribes to the Slack thread, and starts the agent; later replies in that thre
continue the same task without another mention. Normal account and channel access
rules still apply, and repeated Slack deliveries do not create duplicate tasks.
A native Slack thread keeps the same task in channels and DMs, including after
the task is Done or Cancelled. A new reply reopens that task with its saved
comments. DM access settings and task controls still apply to threads inside DMs.
An explicit `/new` or `/close` starts the next task generation only after its
control response is delivered. Task completion alone does not release the binding.
Top-level DMs keep their linear lifecycle and can start a new task after completion.
For a thread that was split before this fix, later replies continue its latest
saved task. Earlier tasks remain separate; this change does not merge history
or copy messages from the surrounding DM into a new thread.
Agents can ask interactive questions in Slack through Paperclip's saved
`ask_user_questions` interactions. Native runners use `request_human_input`;
legacy adapters use the issue-interactions API documented in the injected
+2 -2
View File
@@ -101,9 +101,9 @@ cover the database and prompt contracts; they do not prove live provider deliver
For Slack, enable **Require at-mention** and **Allow direct messages**. Send a
plain DM and follow up without a mention: both should reach the agent, with
top-level replies to top-level DMs. Channel messages and thread replies still
top-level replies to top-level DMs. Channel messages and channel thread replies still
require a mention when that setting is enabled. Disable direct messages and
confirm a plain DM is rejected. Use a private channel for threaded conversations.
confirm a plain DM is rejected. Threads also work in DMs.
For Slack acknowledgement latency, mention the bot with and without an attachment.
Eyes should appear after authorized task/comment persistence, independently of
@@ -30541,6 +30541,82 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
await service.shutdown();
});
it.each([
{ isDM: true, threaded: true, status: "done", state: "active" },
{ isDM: true, threaded: true, status: "cancelled", state: "active" },
{ isDM: true, threaded: true, status: "in_review", state: "completed" },
{ isDM: false, threaded: true, status: "done", state: "completed" },
{ isDM: true, threaded: false, status: "done", state: "completed" },
] as const)(
"preserves Slack thread history across completion ($isDM, $threaded, $status, $state)",
async ({ isDM, threaded, status, state }) => {
const fixture = await seedCompany();
const { callbacks, endpoint, service, wakeup } = await configuredSlackEndpoint(fixture, { linkedBoardUser: true });
const channelId = isDM ? "D-HISTORY" : "C-HISTORY";
const thread = makeThread({
channelId,
id: `slack:${channelId}:${threaded ? "1000.1" : ""}`,
isDM,
}).thread;
const send = (id: string, text: string) => deliverMessage({
callbacks, endpointId: endpoint.id, thread,
message: makeMessage({ id, text, mentioned: !isDM, userId: "UBOARD" }),
trigger: isDM ? "direct_message" : "mention",
});
await send("1000.1", "Keep this context for my next request");
const [original] = await readChatConversationsByEndpointId(endpoint.id);
await db.insert(issueComments).values({
companyId: fixture.companyId, issueId: original.issueId,
authorType: "agent", authorAgentId: fixture.assignedAgentId,
body: "I kept the original request.",
});
await db.update(issues).set({ status }).where(eq(issues.id, original.issueId));
await db.update(chatConversations).set({ state }).where(eq(chatConversations.id, original.id));
await send("1000.2", "Is it done?");
await send("1000.2", "Is it done?");
const conversations = await readChatConversationsByEndpointId(endpoint.id);
expect(conversations).toHaveLength(threaded ? 1 : 2);
const generation = threaded ? original.sessionGeneration : original.sessionGeneration + 1;
const continued = conversations.find(row => row.sessionGeneration === generation)!;
expect(continued.issueId === original.issueId).toBe(threaded);
expect(continued.state).toBe("active");
expect((await issueService(db).getById(continued.issueId))?.status).toBe(status === "in_review" ? "in_review" : "todo");
const comments = await readIssueCommentsByIssueId(continued.issueId)
.orderBy(asc(issueComments.createdAt), asc(issueComments.id));
expect(comments.map(row => row.body)).toEqual(threaded
? ["Keep this context for my next request", "I kept the original request.", "Is it done?"]
: ["Is it done?"]);
expect(wakeup).toHaveBeenCalledTimes(2);
const resources = await db.select().from(chatEndpointResources)
.where(eq(chatEndpointResources.endpointId, endpoint.id));
expect(resources.find(row => row.providerResourceId === channelId)?.type).toBe(isDM ? "direct_message" : "channel");
if (isDM) {
await service.update(endpoint.id, { allowDirectMessages: false });
await send("1000.3", "This must be rejected");
expect(wakeup).toHaveBeenCalledTimes(2);
expect(await readIssueCommentsByIssueId(continued.issueId)).toHaveLength(threaded ? 3 : 1);
}
},
);
it("keeps separate Slack DM thread roots on separate tasks", async () => {
const fixture = await seedCompany();
const { callbacks, endpoint } = await configuredSlackEndpoint(fixture);
for (const root of ["1000.1", "1000.2"]) {
await deliverMessage({
callbacks, endpointId: endpoint.id,
thread: makeThread({ channelId: "D-ROOTS", id: `slack:D-ROOTS:${root}`, isDM: true }).thread,
message: makeMessage({ id: root, text: "A separate request" }),
trigger: "direct_message",
});
}
const conversations = await readChatConversationsByEndpointId(endpoint.id);
expect(conversations).toHaveLength(2);
expect(new Set(conversations.map(row => row.issueId)).size).toBe(2);
expect(conversations.map(row => row.sessionGeneration)).toEqual([1, 1]);
});
it("deduplicates inbound events, keeps one task per thread, and enables newly discovered Slack channels", async () => {
const fixture = await seedCompany();
const { callbacks, endpoint, runtime, service, wakeup } =
@@ -36300,10 +36376,11 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
await resumed.service.shutdown();
});
it("returns two answered Slack turns to Idle in the same thread", async () => {
it.each([false, true])("returns two answered Slack turns to Idle in the same thread (DM=%s)", async (isDM) => {
const fixture = await seedCompany();
const { callbacks, endpoint, service } = await configuredSlackEndpoint(fixture);
const thread = makeThread({ channelId: "C-IDLE", id: "slack:C-IDLE:4049.1", name: "idle" });
const channelId = isDM ? "D-IDLE" : "C-IDLE";
const thread = makeThread({ channelId, id: `slack:${channelId}:4049.1`, name: "idle", isDM });
let issueId: string | undefined;
let conversationId: string | undefined;
for (const [index, text] of ["@maya are you there?", "what is your name?"].entries()) {
@@ -58532,7 +58609,14 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
]);
});
it("binds Slack DM slash controls to the native root returned for the latest task", async () => {
it.each([
{ control: "new", status: "todo", state: "active" },
{ control: "close", status: "todo", state: "active" },
{ control: "new", status: "done", state: "active" },
{ control: "close", status: "cancelled", state: "active" },
{ control: "new", status: "done", state: "completed" },
{ control: "close", status: "cancelled", state: "completed" },
] as const)("binds Slack DM slash controls to the latest task ($control, $status, $state)", async ({ control, status, state }) => {
const fixture = await seedCompany();
const { callbacks, endpoint, runtime, service } =
await configuredSlackEndpoint(fixture);
@@ -58589,18 +58673,20 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
);
const [conversation] = await service.listConversations(endpoint.id);
expect(conversation.externalThreadId).toBe(starterThreadId);
await db.update(issues).set({ status }).where(eq(issues.id, conversation.issueId));
await db.update(chatConversations).set({ state }).where(eq(chatConversations.id, conversation.id));
await invoke("status");
await service.processPendingPublications();
expect(runtime.endpoints.get(endpoint.id)?.posts.at(-1)).toMatchObject({
threadId: starterThreadId,
text: expect.stringMatching(/— todo$/),
text: expect.stringMatching(new RegExp(`— ${status}$`)),
});
await invoke("close");
await expect(service.listConversations(endpoint.id)).resolves.toEqual([
await invoke(control);
await expect(readChatConversationsByEndpointId(endpoint.id)).resolves.toEqual([
expect.objectContaining({
externalThreadId: starterThreadId,
state: "active",
state,
}),
]);
await service.processPendingPublications();
@@ -58610,9 +58696,26 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
state: "completed",
}),
]);
expect(
await readIssuesByCompanyId(fixture.companyId),
).toHaveLength(1);
const [completion] = await db.select().from(chatPublications).where(and(
eq(chatPublications.conversationId, conversation.id),
like(chatPublications.idempotencyKey, `control:${control}:%`),
));
expect(completion?.state).toBe("published");
expect(await readIssuesByCompanyId(fixture.companyId)).toHaveLength(1);
const providerTime = Date.now();
const providerTimestamp = `${Math.floor(providerTime / 1_000)}.${String((providerTime % 1_000) * 1_000).padStart(6, "0")}`;
await deliverMessage({
callbacks, endpointId: endpoint.id,
thread: makeThread({ channelId: "D09SLASHTASK", id: starterThreadId, isDM: true }).thread,
message: makeMessage({ id: providerTimestamp, raw: { ts: providerTimestamp }, text: "An explicitly requested new task", userId: "U-DM-SLASH-TASK" }),
trigger: "direct_message",
});
const conversations = await service.listConversations(endpoint.id);
expect(conversations).toHaveLength(2);
expect(conversations.find(row => row.state === "active")).toMatchObject({
externalThreadId: starterThreadId, sessionGeneration: 2,
});
expect(await readIssuesByCompanyId(fixture.companyId)).toHaveLength(2);
});
it("acknowledges a Slack retry while one durable worker owns the unrecorded command root", async () => {
+5 -1
View File
@@ -1755,12 +1755,16 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) {
let { conversation: existingConversation, issue: existingIssue, latestConversation, isLinear } =
await resolveInboundConversationBinding(db, {
endpoint, thread,
isLinear: () => endpoint.provider === "speko" ? SPEKO_CONVERSATION_IS_LINEAR : surfaceKind !== "native_thread",
isLinear: () => endpoint.provider === "speko"
? SPEKO_CONVERSATION_IS_LINEAR
: surfaceKind !== "native_thread" || (endpoint.provider === "slack" && thread.isDM),
lookupConversation: endpoint.provider === "microsoft-teams"
? () => conversationForThread(endpoint.id, thread.id)
: undefined,
conversationIsComplete: endpoint.provider === "imessage-photon"
? (conversationId) => photonConversationBindingIsComplete(conversationId, hasCommittedTaskControlCompletion)
: endpoint.provider === "slack" && thread.isDM && surfaceKind === "native_thread"
? async (conversationId, state) => state === "completed" && hasCommittedTaskControlCompletion(conversationId)
: undefined,
});
const telegramControls = endpoint.provider === "telegram"
@@ -373,6 +373,7 @@ export function createChatInboundAuthority(db: Db, options: InboundAuthorityOpti
const type = providerResourceType(
endpoint.provider,
chatSurfaceKind(endpoint.provider, thread),
thread.isDM,
);
const providerResourceId = canonicalProviderResourceId(
endpoint.provider,
@@ -9,7 +9,7 @@ type ConversationBindingInput = {
thread: { id: string; channelId: string };
isLinear: () => boolean;
lookupConversation?: () => Promise<ConversationRow | null>;
conversationIsComplete?: (conversationId: string) => Promise<boolean>;
conversationIsComplete?: (conversationId: string, state: ConversationRow["state"]) => Promise<boolean>;
};
export async function resolveInboundConversationBinding(
@@ -54,7 +54,7 @@ export async function resolveInboundConversationBinding(
isLinear &&
existingConversation &&
(conversationIsComplete
? await conversationIsComplete(existingConversation.id)
? await conversationIsComplete(existingConversation.id, existingConversation.state)
: existingConversation.state === "completed" ||
existingIssue?.status === "done" ||
existingIssue?.status === "cancelled")
@@ -263,7 +263,7 @@ export function createInboundEnvelope(db: Db, options: InboundEnvelopeOptions) {
handle: message.author.userName,
},
resource: {
type: providerResourceType(endpoint.provider, surfaceKind),
type: providerResourceType(endpoint.provider, surfaceKind, thread.isDM),
providerResourceId: canonicalProviderResourceId(
endpoint.provider,
thread,
@@ -340,7 +340,7 @@ export function createInboundEnvelope(db: Db, options: InboundEnvelopeOptions) {
}
: {}),
resource: {
type: providerResourceType(endpoint.provider, surfaceKind),
type: providerResourceType(endpoint.provider, surfaceKind, thread.isDM),
providerResourceId: canonicalProviderResourceId(
endpoint.provider,
thread,
@@ -1,4 +1,4 @@
import { slackThreadChannelId } from "../slack/messages.js";
import { slackThreadChannelId, slackThreadRootMessageId } from "../slack/messages.js";
import type { EndpointRow } from "../runtime-types.js";
@@ -30,8 +30,9 @@ type ChatSurfaceKind = "direct_message" | "linear_group" | "native_thread";
function providerResourceType(
provider: ChatProvider,
surfaceKind: ChatSurfaceKind,
isDirectMessage: boolean,
): string {
if (surfaceKind === "direct_message") return "direct_message";
if (isDirectMessage) return "direct_message";
if (provider === "github") return "repository";
if (provider === "imessage-photon") return "group_chat";
if (provider === "discord") return "channel";
@@ -116,6 +117,7 @@ function chatSurfaceKind(
provider: ChatProvider,
thread: Thread,
): ChatSurfaceKind {
if (provider === "slack" && slackThreadRootMessageId(thread.id)) return "native_thread";
if (thread.isDM) return "direct_message";
if (provider === "imessage-photon") return "linear_group";
if (provider === "telegram") {
@@ -91,8 +91,12 @@ export function slackThreadChannelId(threadId: string): string | null {
return channelId || null;
}
export function slackThreadRootMessageId(threadId: string): string | null {
return /^slack:[^:]+:([^:]+)$/.exec(threadId)?.[1] ?? null;
}
export function slackOrphanFollowUp(threadId: string, messageId: string): boolean {
const rootMessageId = /^slack:[^:]+:(.+)$/.exec(threadId)?.[1];
const rootMessageId = slackThreadRootMessageId(threadId);
return Boolean(rootMessageId) && rootMessageId !== messageId;
}
@@ -19,7 +19,6 @@ import {
inArray,
lte,
ne,
notInArray,
or,
sql,
} from "drizzle-orm";
@@ -173,8 +172,6 @@ export function createSlackTaskStart(db: Db, options: SlackTaskStartOptions) {
eq(chatConversations.endpointId, endpointId),
inArray(chatConversations.externalConversationId, conversationIds),
eq(chatConversations.isDirectMessage, true),
inArray(chatConversations.state, ["active", "waiting"]),
notInArray(issues.status, ["done", "cancelled"]),
),
)
.orderBy(