Files
paperclip/server/src/services/chat-channels/inbound/authorization.ts
T
DottaandPaperclip 2e52dfa864 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]>
2026-11-07 08:35:29 -06:00

431 lines
15 KiB
TypeScript

import type { EndpointRow, DbTransaction, DbOrTransaction, LifecycleRuntimeFence } from "../runtime-types.js";
import {
stableExternalPrincipalId,
} from "../message-lifecycle.js";
import { voiceSessionStore } from "../../voice/voice-session-store.js";
import { githubChatPrincipalAccess } from "../../chat-github-access.js";
import {
and,
eq,
inArray,
ne,
sql,
} from "drizzle-orm";
import type { Db } from "@paperclipai/db";
import {
chatConversations,
chatEndpointResources,
chatExternalPrincipals,
chatIdentityLinks,
companyMemberships,
} from "@paperclipai/db";
import {
HttpError,
forbidden,
} from "../../../errors.js";
import type {
Author,
Thread,
} from "chat";
import { canonicalProviderResourceId, chatSurfaceKind, providerResourceLabelFromThread, providerResourceType, nonDirectDestinationAllowed } from "./surfaces.js";
type ResourceRow = typeof chatEndpointResources.$inferSelect;
type InboundAuthorityOptions = {
voiceStore: Pick<ReturnType<typeof voiceSessionStore>, "authorizePrincipal">;
runtimeCallbackEndpoint: (tx: DbTransaction, endpointId: string, context: LifecycleRuntimeFence, statuses: EndpointRow["status"][]) => Promise<EndpointRow | null>;
};
export function createChatInboundAuthority(db: Db, options: InboundAuthorityOptions) {
const { voiceStore, runtimeCallbackEndpoint } = options;
async function findAuthorizedIdentityLink(endpoint: EndpointRow, userId: string) {
return db.select({ id: chatIdentityLinks.id }).from(chatIdentityLinks)
.innerJoin(companyMemberships, and(
eq(companyMemberships.companyId, chatIdentityLinks.companyId), eq(companyMemberships.principalType, "user"),
eq(companyMemberships.principalId, userId), eq(companyMemberships.status, "active"), ne(companyMemberships.membershipRole, "viewer"),
))
.where(and(eq(chatIdentityLinks.companyId, endpoint.companyId), eq(chatIdentityLinks.endpointId, endpoint.id),
eq(chatIdentityLinks.paperclipUserId, userId), eq(chatIdentityLinks.status, "linked")))
.limit(1).then((rows) => rows[0] ?? null);
}
async function ensurePrincipal(
endpoint: EndpointRow,
author: Author,
raw?: unknown,
) {
const providerAccountId = endpoint.providerAccountId ?? "unknown";
const externalId = stableExternalPrincipalId(
endpoint.provider,
author,
raw,
);
const [principal] = await db
.insert(chatExternalPrincipals)
.values({
companyId: endpoint.companyId,
provider: endpoint.provider,
providerAccountId,
externalId,
kind:
author.isBot === true ? "bot" : author.isSystem ? "system" : "user",
displayName: author.fullName,
handle: author.userName,
isBot: author.isBot === true || author.isMe,
lastSeenAt: new Date(),
})
.onConflictDoUpdate({
target: [
chatExternalPrincipals.companyId,
chatExternalPrincipals.provider,
chatExternalPrincipals.providerAccountId,
chatExternalPrincipals.externalId,
],
set: {
displayName: author.fullName,
handle: author.userName,
isBot: author.isBot === true || author.isMe,
lastSeenAt: new Date(),
updatedAt: new Date(),
},
})
.returning();
if (endpoint.provider === "speko") {
try {
const authorization = await db.transaction((tx) => voiceStore.authorizePrincipal(tx, endpoint.companyId, endpoint.id, externalId));
return { principal, userId: authorization.userId, linkedDenied: false };
} catch (error) {
if (!(error instanceof HttpError && [403, 404].includes(error.status))) throw error;
return { principal, userId: null, linkedDenied: true };
}
}
const link = await db
.select({
userId: chatIdentityLinks.paperclipUserId,
status: chatIdentityLinks.status,
})
.from(chatIdentityLinks)
.where(
and(
eq(chatIdentityLinks.endpointId, endpoint.id),
eq(chatIdentityLinks.principalId, principal.id),
),
)
.then((rows) => rows[0] ?? null);
if (link?.status === "linked" && link.userId) {
const membership = await db
.select({
status: companyMemberships.status,
membershipRole: companyMemberships.membershipRole,
})
.from(companyMemberships)
.where(
and(
eq(companyMemberships.companyId, endpoint.companyId),
eq(companyMemberships.principalType, "user"),
eq(companyMemberships.principalId, link.userId),
),
)
.then((rows) => rows[0] ?? null);
// External chat performs the same task mutations as the board. A linked
// viewer therefore remains read-only instead of gaining write authority
// merely by using a provider account.
if (
membership?.status !== "active" ||
membership.membershipRole === "viewer"
) {
return { principal, userId: null, linkedDenied: true };
}
return { principal, userId: link.userId, linkedDenied: false };
}
return { principal, userId: null, linkedDenied: false };
}
async function sponsorAllowsGuest(endpoint: EndpointRow): Promise<boolean> {
// A null sponsor is the local board/operator context used by self-hosted
// instances. Cloud endpoints retain the creating user and must revalidate
// that user's current company authority on every sponsored guest turn.
if (!endpoint.sponsorUserId) return true;
const membership = await db
.select({
status: companyMemberships.status,
membershipRole: companyMemberships.membershipRole,
})
.from(companyMemberships)
.where(
and(
eq(companyMemberships.companyId, endpoint.companyId),
eq(companyMemberships.principalType, "user"),
eq(companyMemberships.principalId, endpoint.sponsorUserId),
),
)
.then((rows) => rows[0] ?? null);
return (
membership?.status === "active" && membership.membershipRole !== "viewer"
);
}
async function lockCurrentPrincipalAuthorization(
tx: DbOrTransaction,
endpoint: EndpointRow,
principalId: string,
): Promise<{
allowed: boolean;
linkedDenied: boolean;
userId: string | null;
sponsorUserId?: string | null;
}> {
if (endpoint.provider === "speko") {
const principal = await tx.select({ externalId: chatExternalPrincipals.externalId }).from(chatExternalPrincipals)
.where(and(eq(chatExternalPrincipals.id, principalId), eq(chatExternalPrincipals.companyId, endpoint.companyId), eq(chatExternalPrincipals.provider, "speko"))).then((rows) => rows[0]);
if (!principal) return { allowed: false, linkedDenied: true, userId: null };
try { return await voiceStore.authorizePrincipal(tx as import("../../voice/voice-session-store.js").VoiceTransaction, endpoint.companyId, endpoint.id, principal.externalId); }
catch (error) {
if (!(error instanceof HttpError && [403, 404].includes(error.status))) throw error;
return { allowed: false, linkedDenied: true, userId: null };
}
}
const githubAccess = await githubChatPrincipalAccess(tx, endpoint, principalId);
if (githubAccess) return githubAccess;
// Link confirmation already uses this transaction-scoped identity key.
// Taking it at the final task mutation boundary prevents a newly confirmed
// identity from racing the authorization snapshot. Row locks below also
// serialize revocation and membership changes that do not use this key.
await tx.execute(
sql`select pg_advisory_xact_lock(hashtextextended(${`chat-identity:${endpoint.companyId}:${principalId}`}, 0))`,
);
const link = await tx
.select({
status: chatIdentityLinks.status,
userId: chatIdentityLinks.paperclipUserId,
})
.from(chatIdentityLinks)
.where(
and(
eq(chatIdentityLinks.companyId, endpoint.companyId),
eq(chatIdentityLinks.endpointId, endpoint.id),
eq(chatIdentityLinks.principalId, principalId),
),
)
.for("update")
.then((rows) => rows[0] ?? null);
if (link?.status === "linked") {
if (!link.userId) {
return { allowed: false, linkedDenied: true, userId: null };
}
const membership = await tx
.select({
status: companyMemberships.status,
membershipRole: companyMemberships.membershipRole,
})
.from(companyMemberships)
.where(
and(
eq(companyMemberships.companyId, endpoint.companyId),
eq(companyMemberships.principalType, "user"),
eq(companyMemberships.principalId, link.userId),
),
)
.for("update")
.then((rows) => rows[0] ?? null);
const allowed =
membership?.status === "active" &&
membership.membershipRole !== "viewer";
return {
allowed,
linkedDenied: !allowed,
userId: allowed ? link.userId : null,
};
}
if (!endpoint.allowUnlinkedPeople) {
return { allowed: false, linkedDenied: false, userId: null };
}
if (!endpoint.sponsorUserId) {
return { allowed: true, linkedDenied: false, userId: null };
}
const sponsorMembership = await tx
.select({
status: companyMemberships.status,
membershipRole: companyMemberships.membershipRole,
})
.from(companyMemberships)
.where(
and(
eq(companyMemberships.companyId, endpoint.companyId),
eq(companyMemberships.principalType, "user"),
eq(companyMemberships.principalId, endpoint.sponsorUserId),
),
)
.for("update")
.then((rows) => rows[0] ?? null);
return {
allowed:
sponsorMembership?.status === "active" &&
sponsorMembership.membershipRole !== "viewer",
linkedDenied: false,
userId: null,
};
}
async function requireCurrentExternalActionAuthorization(
tx: DbTransaction,
input: {
conversationId: string;
endpointId: string;
expectedUserId: string;
principalId: string;
runtimeContext: LifecycleRuntimeFence;
},
): Promise<void> {
const endpoint = await runtimeCallbackEndpoint(
tx,
input.endpointId,
input.runtimeContext,
["active", "verifying"],
);
// Photon setup already admits linked senders and publishes agent prompts.
// Let those prompts resolve so a clarifying question cannot deadlock the
// actual-reply qualification. Other providers retain their active-only gate.
if (!endpoint || (endpoint.status !== "active" &&
(endpoint.provider !== "imessage-photon" || endpoint.setup.step !== "test"))) {
throw forbidden("This chat action is no longer authorized", {
code: "chat_action_authorization_changed",
});
}
const conversation = await tx
.select()
.from(chatConversations)
.where(
and(
eq(chatConversations.companyId, endpoint.companyId),
eq(chatConversations.endpointId, endpoint.id),
eq(chatConversations.id, input.conversationId),
inArray(chatConversations.state, ["active", "waiting"]),
),
)
.for("update")
.then((rows) => rows[0] ?? null);
if (!conversation) {
throw forbidden("This chat action is no longer authorized", {
code: "chat_action_authorization_changed",
});
}
const resource = conversation.resourceId
? await tx
.select()
.from(chatEndpointResources)
.where(
and(
eq(chatEndpointResources.companyId, endpoint.companyId),
eq(chatEndpointResources.endpointId, endpoint.id),
eq(chatEndpointResources.id, conversation.resourceId),
),
)
.for("update")
.then((rows) => rows[0] ?? null)
: null;
const destinationAllowed = conversation.isDirectMessage
? endpoint.allowDirectMessages
: nonDirectDestinationAllowed(endpoint, resource);
const authorization = await lockCurrentPrincipalAuthorization(
tx,
endpoint,
input.principalId,
);
// Governed actions never fall back to sponsored-guest authority. If the
// provider identity was relinked while this callback waited, reject this
// stale actor snapshot and let a fresh callback use the new identity.
if (
!destinationAllowed ||
!authorization.allowed ||
authorization.userId !== input.expectedUserId
) {
throw forbidden("This chat action is no longer authorized", {
code: "chat_action_authorization_changed",
});
}
}
function isExternalActionAuthorizationChange(error: unknown): boolean {
if (!error || typeof error !== "object") return false;
const value = error as { details?: unknown };
return (
Boolean(value.details) &&
typeof value.details === "object" &&
(value.details as { code?: unknown }).code ===
"chat_action_authorization_changed"
);
}
async function ensureResource(
endpoint: EndpointRow,
thread: Thread,
enabledBySetupActivation: boolean,
database: DbOrTransaction = db,
) {
const type = providerResourceType(
endpoint.provider,
chatSurfaceKind(endpoint.provider, thread),
thread.isDM,
);
const providerResourceId = canonicalProviderResourceId(
endpoint.provider,
thread,
);
const resourceLabel = providerResourceLabelFromThread(
endpoint.provider,
thread,
);
const [resource] = await database
.insert(chatEndpointResources)
.values({
companyId: endpoint.companyId,
endpointId: endpoint.id,
type,
providerResourceId,
label: resourceLabel.label,
availability: "available",
// Slack can deliver the invitation's app_mention before the membership
// callback. Give either arrival order the same new-channel default.
enabled:
thread.isDM ||
enabledBySetupActivation ||
(endpoint.provider === "slack" && type === "channel"),
})
.onConflictDoUpdate({
target: [
chatEndpointResources.endpointId,
chatEndpointResources.type,
chatEndpointResources.providerResourceId,
],
set: {
// Provider inventory and membership lifecycle own availability.
// A delayed message must not resurrect a destination after the bot
// was removed or an installation lost access.
// Slack callbacks are not guaranteed to carry a channel name. Keep
// an inventory- or conversations.info-derived label instead of
// downgrading it to C… / slack:C… when a delayed message arrives.
...(resourceLabel.fallback
? {}
: {
label: sql<string>`case
when ${chatEndpointResources.availability} = 'available' then ${resourceLabel.label}
else ${chatEndpointResources.label}
end`,
}),
updatedAt: new Date(),
},
})
.returning();
return resource;
}
return { findAuthorizedIdentityLink, ensurePrincipal, sponsorAllowsGuest, lockCurrentPrincipalAuthorization, requireCurrentExternalActionAuthorization, isExternalActionAuthorizationChange, ensureResource };
}