Files
PaperClipAI/packages/db/src/schema/chat_channels.ts
T
DottaandClaude Opus 5.5 94510d7b26 fix(github): settle request comments after deliberate waits and refused replies (#22798)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Review agents answer GitHub pull request threads; each request's
comment must end in a real reply or an honest final notice
> - After #22672 and #22710, 13 of 85 new review-bot comments on staging
still ended as status notices, and 10 old placeholders on merged PRs
never settled
> - Run data on staging shows two causes: runs that yield on purpose to
wait for a response (`external_chat_response_waiting`), and runs whose
final reply was refused (`responsePublication.state = failed`)
> - The first marked inline threads "ended without publishing" while a
later run posted the review; the second looked like presentation still
pending, forever
> - This pull request makes both settle: link the latest response, wait
while the task is open, and use the honest notice only when nothing was
published
> - The benefit is that every request comment ends in a useful final
state

## Linked Issues or Issue Description

Refs #22672, #22710

**Describe the bug**
- On #22732 the author replied on five review threads at once. Each
reply woke its own run; those runs yielded with
`external_chat_response_waiting`, and a later run posted the review.
Four threads still ended with "This turn ended without publishing a
final response."
- On #22658 (and the other merged PRs with stuck "Working on this…"),
the request's run finished `succeeded`/`committed` with no presentation
decision because its final reply publication failed after 6 attempts
(`publication_refused`). The settle logic treated that as presentation
pending and never settled the thread.

**Steps to reproduce**
1. An inline request's run yields with a response-wake continuation; a
later run publishes a review.
2. Or: an inline request's run finishes with `responsePublication.state
= "failed"` and no presentation decision.
3. The thread says "ended without publishing" (1) or stays "Working on
this…" forever (2).

**Expected behavior**
The thread links the latest published response, waits while the task is
still open, and otherwise ends with an honest notice.

## What Changed

- `githubLatestTaskResponseUrl`: the latest processed GitHub publication
(comment, formal review, assessment) for the task after the request
arrived.
- `githubResponseExecution`: when the request's run did not reply, link
that latest response if any. An inline request whose run deliberately
waits keeps waiting while the task is open. A run whose reply was
refused says so. The main PR comment keeps its passive-wait cancellation
(`mainPullResponse`).
- `nativePresentationPending` is false when `responsePublication.state =
failed`; new `responsePublicationFailed` column.
- Terminal sweep decision version is now `v3`, so requests parked under
v2 are read again once.

## Verification

- New integration tests (red on master, green here): links a waited
thread to a later response; settles a waited thread when the task
finishes with no response; settles a thread whose run's reply could not
be published.
- Updated `keeps per-request fallback outside the main GitHub PR review
wait`: waits while the task is open, then settles when done.
- `keeps native presentation idle until its committed GitHub outcome`
(main PR passive) still passes.
- `server/src/services/chat-channels/github/` and response comment
tests: 67 passed. GitHub integration group: 538 passed before the
main-PR scoping fix; the two affected tests pass after it.
- After deploy: re-measure the stuck rate on staging for comments
created after the deploy.

## Risks

- One extra indexed query per request that settles without its own reply
(to find the latest response). Settled requests leave the backlog.
- A waited inline thread stays "Working on this…" while its task is open
and nothing was published yet; that is the honest state.
- Separate, not fixed here: why the native finalizer refuses some final
replies (`publication_refused`).

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, 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
- [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
- [ ] 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-24 13:36:30 -06:00

853 lines
32 KiB
TypeScript

import { sql } from "drizzle-orm";
import {
boolean,
check,
foreignKey,
index,
integer,
jsonb,
pgTable,
text,
timestamp,
unique,
uniqueIndex,
uuid,
} from "drizzle-orm/pg-core";
import type {
ChatExecutionDefaults,
ChatAdapterCapabilities,
ChatConcurrencyPolicy,
ChatDeliveryState,
ChatDeploymentMode,
ChatEndpointSetupState,
ChatEndpointStatus,
ChatEventKind,
ChatIdentityLinkStatus,
ChatPrincipalKind,
ChatProvider,
ChatPublicationState,
ChatResourceAvailability,
SafeChatPublicationPayload,
} from "@paperclipai/shared";
import { agents } from "./agents.js";
import { companies } from "./companies.js";
import { issueComments } from "./issue_comments.js";
import { issues } from "./issues.js";
import { toolConnections } from "./tool_access.js";
export const chatEndpoints = pgTable(
"chat_endpoints",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
connectionId: uuid("connection_id").notNull(),
provider: text("provider").$type<ChatProvider>().notNull(),
publicId: text("public_id").notNull(),
publicationMode: text("publication_mode").$type<"automatic" | "explicit">().notNull().default("automatic"),
externalExecutionPolicy: text("external_execution_policy").$type<"restricted" | "agent">().notNull().default("restricted"),
assignedAgentId: uuid("assigned_agent_id")
.notNull()
.references(() => agents.id, { onDelete: "restrict" }),
sponsorUserId: text("sponsor_user_id"),
status: text("status")
.$type<ChatEndpointStatus>()
.notNull()
.default("draft"),
deploymentMode: text("deployment_mode")
.$type<ChatDeploymentMode>()
.notNull()
.default("direct"),
providerAccountId: text("provider_account_id"),
providerAccountLabel: text("provider_account_label"),
botExternalId: text("bot_external_id"),
botUsername: text("bot_username"),
botDisplayName: text("bot_display_name"),
botAvatarUrl: text("bot_avatar_url"),
communicationInstructions: text("communication_instructions").notNull().default(""),
executionDefaults: jsonb("execution_defaults").$type<ChatExecutionDefaults>(),
allowDirectMessages: boolean("allow_direct_messages")
.notNull()
.default(true),
requireAtMention: boolean("require_at_mention").notNull().default(false),
allowGroupChats: boolean("allow_group_chats").notNull().default(false),
allowUnlinkedPeople: boolean("allow_unlinked_people")
.notNull()
.default(true),
concurrencyPolicy: text("concurrency_policy")
.$type<ChatConcurrencyPolicy>()
.notNull()
.default("queue"),
capabilities: jsonb("capabilities")
.$type<ChatAdapterCapabilities>()
.notNull()
.default({
threads: false,
directMessages: false,
nativeStreaming: false,
messageEdits: false,
messageDeletes: false,
reactions: false,
files: false,
cards: false,
actions: false,
modals: false,
slashCommands: false,
ephemeralMessages: false,
proactiveDirectMessages: false,
}),
setup: jsonb("setup")
.$type<ChatEndpointSetupState>()
.notNull()
.default({ step: "provider_setup" }),
healthMessage: text("health_message"),
lastEventAt: timestamp("last_event_at", { withTimezone: true }),
lastPublicationAt: timestamp("last_publication_at", { withTimezone: true }),
lastError: text("last_error"),
activatedAt: timestamp("activated_at", { withTimezone: true }),
archivedAt: timestamp("archived_at", { withTimezone: true }),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
check("chat_endpoints_publication_mode_check", sql`${table.publicationMode} in ('automatic', 'explicit')`),
check("chat_endpoints_execution_policy_check", sql`${table.externalExecutionPolicy} in ('restricted', 'agent')`),
check("chat_endpoints_email_policy_check", sql`${table.provider} <> 'agentmail' or (${table.publicationMode} = 'explicit' and ${table.externalExecutionPolicy} = 'agent')`),
check(
"chat_endpoints_provider_check",
sql`${table.provider} in ('slack', 'github', 'discord', 'microsoft-teams', 'telegram', 'speko', 'agentmail', 'imessage-photon')`,
),
check(
"chat_endpoints_status_check",
sql`${table.status} in ('draft', 'verifying', 'active', 'paused', 'attention', 'revoked', 'archived')`,
),
check(
"chat_endpoints_deployment_check",
sql`${table.deploymentMode} in ('direct', 'relay')`,
),
check(
"chat_endpoints_concurrency_check",
sql`${table.concurrencyPolicy} in ('burst', 'queue', 'debounce', 'drop', 'concurrent')`,
),
index("chat_endpoints_company_idx").on(table.companyId),
index("chat_endpoints_agent_idx").on(
table.companyId,
table.assignedAgentId,
),
index("chat_endpoints_status_idx").on(table.companyId, table.status),
uniqueIndex("chat_endpoints_public_id_uq").on(table.publicId),
uniqueIndex("chat_endpoints_agentmail_inbox_uq")
.on(table.botExternalId)
.where(sql`${table.provider} = 'agentmail' and ${table.status} != 'archived' and ${table.botExternalId} is not null`),
uniqueIndex("chat_endpoints_connection_uq").on(table.connectionId),
// A native provider identity can back only one live Paperclip endpoint.
// Historical archived/revoked endpoints retain attribution without
// preventing an operator from deliberately reusing the provider bot later.
uniqueIndex("chat_endpoints_live_bot_external_uq")
.on(table.provider, table.providerAccountId, table.botExternalId)
.where(
sql`${table.status} in ('verifying', 'active', 'paused', 'attention')
and ${table.providerAccountId} is not null
and ${table.botExternalId} is not null`,
),
// One Discord application can be installed in many guilds, but it remains
// one native bot identity. Excluding providerAccountId closes the race
// where concurrent setup in two guilds could otherwise claim that bot for
// two Paperclip agents after both application-level prechecks passed.
uniqueIndex("chat_endpoints_photon_number_uq")
.on(table.botExternalId)
.where(sql`${table.provider} = 'imessage-photon' and ${table.status} <> 'archived' and ${table.botExternalId} is not null`),
uniqueIndex("chat_endpoints_live_discord_bot_external_uq")
.on(table.provider, table.botExternalId)
.where(
sql`${table.provider} = 'discord'
and ${table.status} in ('verifying', 'active', 'paused', 'attention')
and ${table.botExternalId} is not null`,
),
// GitHub App and Microsoft Bot application ids are provider-global bot
// identities. Their mutable owner/tenant coordinate is useful metadata,
// but it cannot be part of the exclusivity key: an App transfer or a
// multi-tenant service principal must never let one native bot represent
// two Paperclip agents through two different webhook URLs.
uniqueIndex("chat_endpoints_live_global_app_bot_external_uq")
.on(table.provider, table.botExternalId)
.where(
sql`${table.provider} in ('github', 'microsoft-teams')
and ${table.status} in ('verifying', 'active', 'paused', 'attention')
and ${table.botExternalId} is not null`,
),
// GitHub App verification does not expose the bot user's numeric id, so
// retain an equivalent live-slot constraint on the provider-native name.
uniqueIndex("chat_endpoints_live_bot_username_uq")
.on(table.provider, table.providerAccountId, table.botUsername)
.where(
sql`${table.status} in ('verifying', 'active', 'paused', 'attention')
and ${table.providerAccountId} is not null
and ${table.botUsername} is not null`,
),
unique("chat_endpoints_company_id_uq").on(table.companyId, table.id),
foreignKey({
columns: [table.companyId, table.assignedAgentId],
foreignColumns: [agents.companyId, agents.id],
name: "chat_endpoints_company_agent_fk",
}),
foreignKey({
columns: [table.companyId, table.connectionId],
foreignColumns: [toolConnections.companyId, toolConnections.id],
name: "chat_endpoints_company_connection_fk",
}).onDelete("cascade"),
],
);
export const chatEndpointResources = pgTable(
"chat_endpoint_resources",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
endpointId: uuid("endpoint_id").notNull(),
type: text("type").notNull(),
providerResourceId: text("provider_resource_id").notNull(),
parentProviderResourceId: text("parent_provider_resource_id"),
label: text("label").notNull(),
detail: text("detail"),
providerUrl: text("provider_url"),
availability: text("availability")
.$type<ChatResourceAvailability>()
.notNull()
.default("available"),
enabled: boolean("enabled").notNull().default(false),
executionDefaults: jsonb("execution_defaults").$type<ChatExecutionDefaults>(),
metadata: jsonb("metadata")
.$type<Record<string, unknown>>()
.notNull()
.default({}),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
check(
"chat_endpoint_resources_availability_check",
sql`${table.availability} in ('available', 'unavailable', 'removed')`,
),
index("chat_endpoint_resources_endpoint_idx").on(
table.companyId,
table.endpointId,
),
uniqueIndex("chat_endpoint_resources_external_uq").on(
table.endpointId,
table.type,
table.providerResourceId,
),
unique("chat_endpoint_resources_company_id_uq").on(
table.companyId,
table.id,
),
foreignKey({
columns: [table.companyId, table.endpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_endpoint_resources_company_endpoint_fk",
}).onDelete("cascade"),
],
);
export const chatExternalPrincipals = pgTable(
"chat_external_principals",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
provider: text("provider").$type<ChatProvider>().notNull(),
providerAccountId: text("provider_account_id").notNull(),
externalId: text("external_id").notNull(),
kind: text("kind").$type<ChatPrincipalKind>().notNull().default("user"),
displayName: text("display_name"),
handle: text("handle"),
avatarUrl: text("avatar_url"),
isBot: boolean("is_bot").notNull().default(false),
lastSeenAt: timestamp("last_seen_at", { withTimezone: true }),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
check(
"chat_external_principals_provider_check",
sql`${table.provider} in ('slack', 'github', 'discord', 'microsoft-teams', 'telegram', 'speko', 'agentmail', 'imessage-photon')`,
),
check(
"chat_external_principals_kind_check",
sql`${table.kind} in ('user', 'bot', 'app', 'system')`,
),
index("chat_external_principals_company_idx").on(table.companyId),
uniqueIndex("chat_external_principals_external_uq").on(
table.companyId,
table.provider,
table.providerAccountId,
table.externalId,
),
unique("chat_external_principals_company_id_uq").on(
table.companyId,
table.id,
),
],
);
export const chatIdentityLinks = pgTable(
"chat_identity_links",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
endpointId: uuid("endpoint_id").notNull(),
principalId: uuid("principal_id").notNull(),
paperclipUserId: text("paperclip_user_id"),
status: text("status")
.$type<ChatIdentityLinkStatus>()
.notNull()
.default("pending"),
confirmationTokenHash: text("confirmation_token_hash"),
expiresAt: timestamp("expires_at", { withTimezone: true }),
confirmedAt: timestamp("confirmed_at", { withTimezone: true }),
revokedAt: timestamp("revoked_at", { withTimezone: true }),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
check(
"chat_identity_links_status_check",
sql`${table.status} in ('pending', 'linked', 'revoked', 'expired')`,
),
index("chat_identity_links_user_idx").on(
table.companyId,
table.paperclipUserId,
),
uniqueIndex("chat_identity_links_endpoint_principal_uq").on(
table.endpointId,
table.principalId,
),
foreignKey({
columns: [table.companyId, table.endpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_identity_links_company_endpoint_fk",
}).onDelete("cascade"),
foreignKey({
columns: [table.companyId, table.principalId],
foreignColumns: [
chatExternalPrincipals.companyId,
chatExternalPrincipals.id,
],
name: "chat_identity_links_company_principal_fk",
}).onDelete("cascade"),
],
);
export const chatConversations = pgTable(
"chat_conversations",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
endpointId: uuid("endpoint_id").notNull(),
resourceId: uuid("resource_id").references(() => chatEndpointResources.id, {
onDelete: "set null",
}),
issueId: uuid("issue_id")
.notNull()
.references(() => issues.id, { onDelete: "restrict" }),
externalConversationId: text("external_conversation_id").notNull(),
externalThreadId: text("external_thread_id").notNull().default(""),
// Providers with linear conversations (DMs, Telegram groups, Teams group
// chats) reuse one native thread id. A generation preserves the native id
// used for replies while allowing completed Paperclip tasks to roll over.
sessionGeneration: integer("session_generation").notNull().default(1),
externalLabel: text("external_label").notNull(),
providerUrl: text("provider_url"),
isDirectMessage: boolean("is_direct_message").notNull().default(false),
// Immutable initial task context, never refreshed from endpoint settings.
communicationGuidance: text("communication_guidance"),
state: text("state").notNull().default("active"),
lastActivityAt: timestamp("last_activity_at", { withTimezone: true }),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
check(
"chat_conversations_state_check",
sql`${table.state} in ('active', 'waiting', 'completed', 'unavailable', 'endpoint_removed')`,
),
index("chat_conversations_issue_idx").on(table.companyId, table.issueId),
uniqueIndex("chat_conversations_thread_uq").on(
table.endpointId,
table.externalConversationId,
table.externalThreadId,
table.sessionGeneration,
),
unique("chat_conversations_company_id_uq").on(table.companyId, table.id),
foreignKey({
columns: [table.companyId, table.issueId],
foreignColumns: [issues.companyId, issues.id],
name: "chat_conversations_company_issue_fk",
}),
foreignKey({
columns: [table.companyId, table.endpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_conversations_company_endpoint_fk",
}).onDelete("cascade"),
foreignKey({
columns: [table.companyId, table.resourceId],
foreignColumns: [
chatEndpointResources.companyId,
chatEndpointResources.id,
],
name: "chat_conversations_company_resource_fk",
}),
],
);
export const chatDeliveries = pgTable(
"chat_deliveries",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
endpointId: uuid("endpoint_id").notNull(),
conversationId: uuid("conversation_id").references(
() => chatConversations.id,
{
onDelete: "set null",
},
),
principalId: uuid("principal_id").references(
() => chatExternalPrincipals.id,
{
onDelete: "set null",
},
),
providerEventId: text("provider_event_id").notNull(),
deduplicationKey: text("deduplication_key").notNull(),
eventKind: text("event_kind").$type<ChatEventKind>().notNull(),
normalizedEvent: jsonb("normalized_event")
.$type<Record<string, unknown>>()
.notNull(),
state: text("state")
.$type<ChatDeliveryState>()
.notNull()
.default("received"),
attempts: integer("attempts").notNull().default(0),
redactedError: text("redacted_error"),
nextAttemptAt: timestamp("next_attempt_at", { withTimezone: true }),
receivedAt: timestamp("received_at", { withTimezone: true })
.notNull()
.defaultNow(),
processedAt: timestamp("processed_at", { withTimezone: true }),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
check(
"chat_deliveries_state_check",
sql`${table.state} in ('received', 'filtered', 'processing', 'processed', 'retry', 'failed')`,
),
index("chat_deliveries_work_idx").on(table.state, table.nextAttemptAt),
unique("chat_deliveries_company_id_uq").on(table.companyId, table.id),
uniqueIndex("chat_deliveries_event_uq").on(
table.endpointId,
table.providerEventId,
),
uniqueIndex("chat_deliveries_dedupe_uq").on(
table.endpointId,
table.deduplicationKey,
),
// Review checks find their delivery by the GitHub webhook delivery id.
index("chat_deliveries_github_automatic_delivery_idx")
.on(table.endpointId, sql`(${table.normalizedEvent}->'githubAutomatic'->'context'->>'deliveryId')`)
.where(sql`(${table.normalizedEvent}->'githubAutomatic'->'context'->>'deliveryId') is not null`)
.concurrently(),
foreignKey({
columns: [table.companyId, table.endpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_deliveries_company_endpoint_fk",
}).onDelete("cascade"),
foreignKey({
columns: [table.companyId, table.conversationId],
foreignColumns: [chatConversations.companyId, chatConversations.id],
name: "chat_deliveries_company_conversation_fk",
}),
foreignKey({
columns: [table.companyId, table.principalId],
foreignColumns: [
chatExternalPrincipals.companyId,
chatExternalPrincipals.id,
],
name: "chat_deliveries_company_principal_fk",
}),
],
);
export const chatPublications = pgTable(
"chat_publications",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
endpointId: uuid("endpoint_id").notNull(),
conversationId: uuid("conversation_id").notNull(),
issueId: uuid("issue_id")
.notNull()
.references(() => issues.id, { onDelete: "restrict" }),
commentId: uuid("comment_id").references(() => issueComments.id, {
onDelete: "set null",
}),
idempotencyKey: text("idempotency_key").notNull(),
payload: jsonb("payload").$type<SafeChatPublicationPayload>().notNull(),
state: text("state")
.$type<ChatPublicationState>()
.notNull()
.default("pending"),
providerMessageId: text("provider_message_id"),
providerUrl: text("provider_url"),
attempts: integer("attempts").notNull().default(0),
redactedError: text("redacted_error"),
nextAttemptAt: timestamp("next_attempt_at", { withTimezone: true }),
publishedAt: timestamp("published_at", { withTimezone: true }),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
check(
"chat_publications_state_check",
sql`${table.state} in ('pending', 'streaming', 'published', 'retry', 'delivery_unknown', 'failed', 'cancelled', 'awaiting_consent')`,
),
uniqueIndex("chat_publications_company_id_uq").on(
table.companyId,
table.id,
),
foreignKey({
columns: [table.companyId, table.issueId],
foreignColumns: [issues.companyId, issues.id],
name: "chat_publications_company_issue_fk",
}),
// Retain the single-column SET NULL action above. The additional tenant
// key uses NO ACTION so deletion clears only comment_id, never company_id.
foreignKey({
columns: [table.companyId, table.commentId],
foreignColumns: [issueComments.companyId, issueComments.id],
name: "chat_publications_company_comment_fk",
}),
index("chat_publications_work_idx").on(table.state, table.nextAttemptAt),
index("chat_publications_comment_destination_idx").on(table.companyId, table.commentId, table.endpointId, table.conversationId)
.where(sql`${table.commentId} is not null`),
uniqueIndex("chat_publications_idempotency_uq").on(
table.companyId,
table.idempotencyKey,
),
foreignKey({
columns: [table.companyId, table.endpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_publications_company_endpoint_fk",
}).onDelete("cascade"),
foreignKey({
columns: [table.companyId, table.conversationId],
foreignColumns: [chatConversations.companyId, chatConversations.id],
name: "chat_publications_company_conversation_fk",
}).onDelete("cascade"),
],
);
export const chatMessageLinks = pgTable(
"chat_message_links",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
endpointId: uuid("endpoint_id").notNull(),
conversationId: uuid("conversation_id").notNull(),
deliveryId: uuid("delivery_id").references(() => chatDeliveries.id, {
onDelete: "set null",
}),
publicationId: uuid("publication_id").references(
() => chatPublications.id,
{ onDelete: "set null" },
),
commentId: uuid("comment_id").references(() => issueComments.id, {
onDelete: "set null",
}),
providerMessageId: text("provider_message_id").notNull(),
direction: text("direction").notNull(),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
check(
"chat_message_links_direction_check",
sql`${table.direction} in ('inbound', 'outbound')`,
),
foreignKey({
columns: [table.companyId, table.endpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_message_links_company_endpoint_fk",
}).onDelete("cascade"),
foreignKey({
columns: [table.companyId, table.deliveryId],
foreignColumns: [chatDeliveries.companyId, chatDeliveries.id],
name: "chat_message_links_company_delivery_fk",
}),
foreignKey({
columns: [table.companyId, table.publicationId],
foreignColumns: [chatPublications.companyId, chatPublications.id],
name: "chat_message_links_company_publication_fk",
}),
foreignKey({
columns: [table.companyId, table.commentId],
foreignColumns: [issueComments.companyId, issueComments.id],
name: "chat_message_links_company_comment_fk",
}),
uniqueIndex("chat_message_links_provider_message_uq").on(
table.endpointId,
table.conversationId,
table.providerMessageId,
),
// Source-comment and delivery lookups (GitHub review waits and checks).
// These also serve the comment and delivery `on delete set null` actions.
index("chat_message_links_company_comment_idx")
.on(table.companyId, table.commentId)
.where(sql`${table.commentId} is not null`)
.concurrently(),
index("chat_message_links_delivery_idx")
.on(table.deliveryId)
.where(sql`${table.deliveryId} is not null`)
.concurrently(),
foreignKey({
columns: [table.companyId, table.conversationId],
foreignColumns: [chatConversations.companyId, chatConversations.id],
name: "chat_message_links_company_conversation_fk",
}).onDelete("cascade"),
],
);
export const chatActions = pgTable(
"chat_actions",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
endpointId: uuid("endpoint_id").notNull(),
deliveryId: uuid("delivery_id").references(() => chatDeliveries.id, {
onDelete: "set null",
}),
conversationId: uuid("conversation_id"),
principalId: uuid("principal_id"),
kind: text("kind").notNull(),
providerActionId: text("provider_action_id").notNull(),
payload: jsonb("payload")
.$type<Record<string, unknown>>()
.notNull()
.default({}),
status: text("status").notNull().default("received"),
result: jsonb("result").$type<Record<string, unknown>>(),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
index("chat_actions_continuation_work_idx")
.on(table.companyId, table.id)
.where(sql`${table.kind} = 'interaction_wakeup' AND ${table.status} IN ('issued', 'processing')`),
index("chat_actions_confirmation_recovery_idx")
.on(table.companyId, table.id)
.where(sql`${table.kind} = 'confirmation_response' AND ${table.status} = 'processing'`),
uniqueIndex("chat_actions_provider_action_uq").on(
table.endpointId,
table.providerActionId,
),
// Chat-admission checks read an issue's inbound input and its failed-run
// retries by company and payload issue id.
index("chat_actions_company_issue_kind_idx")
.on(table.companyId, sql`(${table.payload}->>'issueId')`, table.kind, table.status)
.where(sql`${table.kind} IN ('inbound_wakeup', 'failed_run_retry')`)
.concurrently(),
index("chat_actions_github_stale_head_idx").on(table.companyId, table.endpointId,
sql`(${table.payload}->'event'->>'deliveryId')`).where(sql`${table.kind} = 'github_review_check'
and ${table.status} = 'cancelled' and ${table.result}->>'code' = 'stale_head'`),
index("chat_actions_github_check_work_idx").on(table.id).where(sql`${table.kind} = 'github_review_check'
and (${table.status} = 'received' or (${table.status} = 'failed' and ${table.result}->>'retryable' = 'true'))`),
index("chat_actions_github_task_publications_idx")
.on(table.companyId, table.endpointId, sql`(${table.payload}->'session'->>'issueId')`, table.updatedAt)
.where(sql`${table.kind} = 'github_review_publication' and ${table.status} = 'processed'`)
.concurrently(),
index("chat_actions_github_terminal_work_idx").on(table.id).where(sql`${table.kind} = 'github_response_comment'
and ${table.status} = 'processed' and ${table.deliveryId} is not null
and coalesce(${table.result}->>'final', 'false') <> 'true'`),
index("chat_actions_github_ack_work_idx").on(table.id).where(sql`${table.kind} = 'github_response_comment'
and ${table.deliveryId} is not null and coalesce(${table.result}->>'final', 'false') <> 'true'
and (${table.status} = 'received' or (${table.status} = 'failed' and ${table.result}->>'retryable' = 'true'))`),
foreignKey({
columns: [table.companyId, table.deliveryId],
foreignColumns: [chatDeliveries.companyId, chatDeliveries.id],
name: "chat_actions_company_delivery_fk",
}),
foreignKey({
columns: [table.companyId, table.conversationId],
foreignColumns: [chatConversations.companyId, chatConversations.id],
name: "chat_actions_company_conversation_fk",
}),
foreignKey({
columns: [table.companyId, table.principalId],
foreignColumns: [
chatExternalPrincipals.companyId,
chatExternalPrincipals.id,
],
name: "chat_actions_company_principal_fk",
}),
foreignKey({
columns: [table.companyId, table.endpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_actions_company_endpoint_fk",
}).onDelete("cascade"),
],
);
export const chatAgentRoutes = pgTable(
"chat_agent_routes",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
sourceEndpointId: uuid("source_endpoint_id").notNull(),
destinationEndpointId: uuid("destination_endpoint_id").notNull(),
enabled: boolean("enabled").notNull().default(false),
triggerMode: text("trigger_mode").notNull().default("explicit_mention"),
maxHops: integer("max_hops").notNull().default(1),
createdByUserId: text("created_by_user_id"),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
check(
"chat_agent_routes_hops_check",
sql`${table.maxHops} between 1 and 8`,
),
uniqueIndex("chat_agent_routes_pair_uq").on(
table.sourceEndpointId,
table.destinationEndpointId,
),
foreignKey({
columns: [table.companyId, table.sourceEndpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_agent_routes_company_source_fk",
}).onDelete("cascade"),
foreignKey({
columns: [table.companyId, table.destinationEndpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_agent_routes_company_destination_fk",
}).onDelete("cascade"),
],
);
export const chatEndpointLeases = pgTable(
"chat_endpoint_leases",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
endpointId: uuid("endpoint_id").notNull(),
leaseKey: text("lease_key").notNull(),
token: text("token").notNull(),
expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
uniqueIndex("chat_endpoint_leases_active_uq").on(
table.endpointId,
table.leaseKey,
),
index("chat_endpoint_leases_expiry_idx").on(table.expiresAt),
foreignKey({
columns: [table.companyId, table.endpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_endpoint_leases_company_endpoint_fk",
}).onDelete("cascade"),
],
);
export const chatSdkState = pgTable(
"chat_sdk_state",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id")
.notNull()
.references(() => companies.id, { onDelete: "cascade" }),
endpointId: uuid("endpoint_id").notNull(),
stateKey: text("state_key").notNull(),
version: integer("version").notNull().default(1),
value: jsonb("value").$type<unknown>().notNull(),
expiresAt: timestamp("expires_at", { withTimezone: true }),
createdAt: timestamp("created_at", { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
uniqueIndex("chat_sdk_state_key_uq").on(table.endpointId, table.stateKey),
index("chat_sdk_state_expiry_idx").on(table.expiresAt),
foreignKey({
columns: [table.companyId, table.endpointId],
foreignColumns: [chatEndpoints.companyId, chatEndpoints.id],
name: "chat_sdk_state_company_endpoint_fk",
}).onDelete("cascade"),
],
);