Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/**
* Real-Postgres proof that a messenger thread a fixed spam rule files raises
* nothing, like cold email filed the same way: the team gets no email or
* bell, no created or message event fires (so no webhook, workflow or SLA
* clock), the inbox is not told about it and routing does not assign it. The
* rule is decided before the thread exists and the thread is filed in its
* insert. The burst rule (one sender opening threads faster than a person
* types) is driven through the real rate buckets; the threads before the
* burst are the control that a real thread does raise its signals.
*/
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createId, type ConversationId, type PrincipalId, type UserId } from '@quackback/ids'
import type { Actor } from '@/lib/server/policy/types'

vi.mock('@/lib/server/db', async (importOriginal) => ({
...(await importOriginal<typeof import('@/lib/server/db')>()),
db: (await import('@/lib/server/__tests__/db-test-fixture')).testDb,
}))

vi.mock('../conversation.webhooks', () => ({
emitConversationCreated: vi.fn(async () => {}),
emitMessageCreated: vi.fn(async () => {}),
emitMessageNoteCreated: vi.fn(),
emitMessageDeleted: vi.fn(),
emitConversationStatusChanged: vi.fn(async () => {}),
emitConversationAssigned: vi.fn(),
emitConversationPriorityChanged: vi.fn(),
emitConversationCsatSubmitted: vi.fn(),
emitConversationCsatCommentAdded: vi.fn(),
}))

vi.mock('../conversation.notify', () => ({
notifyVisitorMessage: vi.fn(async () => {}),
notifyAgentReply: vi.fn(async () => {}),
notifyConversationStarted: vi.fn(async () => {}),
}))

vi.mock('@/lib/server/realtime/conversation-channels', () => ({
publishConversationEvent: vi.fn(),
publishConversationMessage: vi.fn(),
publishAgentConversationEvent: vi.fn(),
publishConversationUpdate: vi.fn(),
}))

vi.mock('../conversation.query', async (importOriginal) => ({
...(await importOriginal<typeof import('../conversation.query')>()),
conversationToDTO: vi.fn(async (row: { id: string; status: string }) => ({
id: row.id,
status: row.status,
})),
}))

// The AI classifier is off in this workspace's settings; a call would be a bug.
const mockChat = vi.fn()
vi.mock('@tanstack/ai', () => ({ chat: (...args: unknown[]) => mockChat(...args) }))

import { createDbTestFixture, testDb } from '@/lib/server/__tests__/db-test-fixture'
import { conversations, principal, settings, user, eq, sql } from '@/lib/server/db'
import { sendVisitorMessage } from '../conversation.service'
import { emitConversationCreated, emitMessageCreated } from '../conversation.webhooks'
import { notifyVisitorMessage } from '../conversation.notify'
import {
publishConversationMessage,
publishConversationUpdate,
} from '@/lib/server/realtime/conversation-channels'

const fixture = await createDbTestFixture({
probe: async (db) => {
await db.select({ spamReason: conversations.spamReason }).from(conversations).limit(0)
await db.execute(sql`select workspace_key, key, count from rate_bucket limit 0`)
},
})

const SENDER = 'burst-visitor@example.com'
let visitorPrincipalId: PrincipalId

function visitorActor(): Actor {
return {
principalId: visitorPrincipalId,
role: 'user',
principalType: 'anonymous',
segmentIds: new Set(),
}
}

async function seed(): Promise<void> {
await testDb.delete(settings)
await testDb.insert(settings).values({
name: 'Test',
slug: `t-${Date.now().toString(36)}`,
createdAt: new Date(),
spamFilterConfig: JSON.stringify({ aiClassifier: false, trustedSenders: [] }),
})
const visitorUserId = createId('user') as UserId
visitorPrincipalId = createId('principal') as PrincipalId
await testDb.insert(user).values({ id: visitorUserId, name: 'Visitor' })
await testDb.insert(principal).values({
id: visitorPrincipalId,
userId: visitorUserId,
role: 'user',
type: 'anonymous',
createdAt: new Date(),
})
await testDb.execute(sql`delete from rate_bucket where key like 'conversation:cold:burst:%'`)
}

/** A new messenger thread, with the email the visitor gave before the chat. */
async function startThread(content: string): Promise<ConversationId> {
const res = await sendVisitorMessage(
{ content, visitorEmail: SENDER },
{ principalId: visitorPrincipalId, displayName: 'Visitor' },
visitorActor()
)
return res.conversation.id as ConversationId
}

/** Calls of a spy whose first argument (or its `conversation`) is this thread. */
function callsFor(spy: unknown, id: ConversationId): number {
return (spy as { mock: { calls: unknown[][] } }).mock.calls.filter((args) =>
JSON.stringify(args).includes(id)
).length
}

beforeEach(async () => {
expect(fixture.available).toBe(true)
await fixture.begin()
vi.clearAllMocks()
await seed()
})
afterEach(fixture.rollback)
afterAll(fixture.close)

describe('a messenger thread filed by the burst rule', () => {
it('is filed in Spam and raises nothing; the threads before it raised their signals', async () => {
// Three threads in ten minutes is the most a person opens: each is real.
const real: ConversationId[] = []
for (let i = 0; i < 3; i++) real.push(await startThread(`Question ${i}`))
for (const id of real) {
expect(callsFor(notifyVisitorMessage, id)).toBe(1)
expect(callsFor(emitConversationCreated, id)).toBe(1)
expect(callsFor(emitMessageCreated, id)).toBe(1)
expect(callsFor(publishConversationUpdate, id)).toBeGreaterThan(0)
}

const burst = await startThread('Buy followers now')

// Filed, whenever the filing happens.
await vi.waitFor(async () => {
const stored = await testDb.query.conversations.findFirst({
where: eq(conversations.id, burst),
})
expect(stored?.endReason).toBe('spam')
})
// Let any fire-and-forget notification start if it is going to.
await new Promise((resolve) => setTimeout(resolve, 20))

expect(callsFor(notifyVisitorMessage, burst)).toBe(0)
expect(callsFor(emitConversationCreated, burst)).toBe(0)
expect(callsFor(emitMessageCreated, burst)).toBe(0)
expect(callsFor(publishConversationUpdate, burst)).toBe(0)
expect(callsFor(publishConversationMessage, burst)).toBe(0)
expect(mockChat).not.toHaveBeenCalled()

// Filed in the insert: nobody is waiting on a reply to spam.
const stored = await testDb.query.conversations.findFirst({
where: eq(conversations.id, burst),
})
expect(stored).toMatchObject({
status: 'closed',
endReason: 'spam',
spamReason: 'burst_rate',
waitingSince: null,
assignedAgentPrincipalId: null,
})
expect(stored?.resolvedAt).not.toBeNull()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,24 @@ export async function sendVisitorMessage(
? callerMetadata
: null

// A new thread's fixed spam rules (the burst rule: one sender opening threads
// faster than a person types) are decided BEFORE the thread exists, so a
// filed thread is filed in its insert and raises nothing, like cold email a
// fixed rule files: no team email or bell, no created or message event (so
// no webhook, workflow or SLA clock), no inbox update and no routing. The
// sender is the visitor's own real address, else one they gave before the
// chat. Only the AI classifier runs after the create.
const spamSenderEmail = input.conversationId
? null
: (normalizeEmail(realEmail(author.email) ?? undefined) ??
normalizeEmail(input.visitorEmail) ??
null)
const fixedSpamSignal = input.conversationId
? null
: await import('./conversation.spam-filter').then((m) =>
m.fixedSpamSignalFor({ senderEmail: spamSenderEmail }, author.principalId)
)

let created = false
// The conversation's status BEFORE this message, so the assistant trigger can
// tell a genuinely reopened thread from one a human deliberately closed.
Expand Down Expand Up @@ -386,9 +404,19 @@ export async function sendVisitorMessage(
.values({
visitorPrincipalId: author.principalId,
channel: 'messenger',
status: 'open',
status: fixedSpamSignal ? 'closed' : 'open',
subject: preview(content || fallbackLabel, attachments),
...(testOwner ? { assignedAgentPrincipalId: testOwner } : {}),
// The Spam-view shape autoFileConversationAsSpam writes, in the
// insert so the thread is never open.
...(fixedSpamSignal
? {
resolvedAt: new Date(),
endReason: 'spam' as const,
endNote: `Auto-filed by the spam filter (${CONVERSATION_SPAM_FILED_BY_LABELS[fixedSpamSignal]})`,
spamReason: fixedSpamSignal,
}
: {}),
})
.returning()
conversation = createdConv
Expand Down Expand Up @@ -420,13 +448,17 @@ export async function sendVisitorMessage(
// a block posted on an already-closed conversation is the intended
// post-close CSAT/button flow, not the customer reopening the thread —
// see applyVisitorReopenStatus's doc.
const visitorNextStatus = heldInSpam
? 'closed'
: applyVisitorReopenStatus(
conversation.status,
!!resolvedBlockReply,
getChannelDescriptor(conversation.channel)?.reopenOnReply ?? 'always'
)
// A thread a fixed spam rule filed in its insert stays closed (resolvedAt
// then keeps the filing time, below), and nobody is waiting on a reply.
const filedInInsert = created && fixedSpamSignal !== null
const visitorNextStatus =
heldInSpam || filedInInsert
? 'closed'
: applyVisitorReopenStatus(
conversation.status,
!!resolvedBlockReply,
getChannelDescriptor(conversation.channel)?.reopenOnReply ?? 'always'
)
const [updated] = await tx
.update(conversations)
.set({
Expand All @@ -439,10 +471,13 @@ export async function sendVisitorMessage(
snoozedUntil: null,
// The customer is now waiting on a reply: start the clock if it isn't
// already running (the oldest unanswered message wins). Nobody owes
// spam a reply, so a message held in Spam starts no clock.
waitingSince: heldInSpam
? conversation.waitingSince
: (conversation.waitingSince ?? message.createdAt),
// spam a reply, so a message held in Spam or a thread filed in its
// insert starts no clock.
waitingSince: filedInInsert
? null
: heldInSpam
? conversation.waitingSince
: (conversation.waitingSince ?? message.createdAt),
// Keep resolvedAt consistent with the new status — a reply that reopens
// a closed thread must clear the stale resolution timestamp. The one
// case that stays 'closed' (the post-close matched-blockReply carve-out
Expand Down Expand Up @@ -486,6 +521,17 @@ export async function sendVisitorMessage(
const visitorMessage = toMessageDTO(txResult.message, authored.publicAuthor)
const agentMessage = toMessageDTO(txResult.message, authored.supportAuthor)

// Filed to Spam by a fixed rule in the insert: the thread is kept and
// reviewable in the Spam view, and nothing else happens (see above).
if (created && fixedSpamSignal) {
log.info(
{ conversation_id: txResult.conversation.id, signal: fixedSpamSignal },
'spam signal matched; messenger thread filed'
)
const filedDTO = await conversationToDTO(txResult.conversation, 'visitor')
return { conversation: filedDTO, message: visitorMessage, created }
}

// A new conversation appears in the agent inbox; publish the agent-side DTO
// there (publishConversationUpdate strips agent-only fields for the visitor).
if (created) {
Expand Down Expand Up @@ -573,22 +619,17 @@ export async function sendVisitorMessage(
// A brand-new conversation is a spam-classification candidate: the filter
// files only obvious spam (workspace-trusted senders bypass it), and every
// failure leaves the thread in triage. Fire-and-forget so a slow provider
// never delays the visitor's send.
// never delays the visitor's send. The fixed rules already ran before the
// insert; only the AI classifier is left.
if (created) {
void import('./conversation.spam-filter')
.then((m) =>
m.maybeAutoFileSpam(txResult.conversation.id, {
// The visitor's own address when they have a real one (an
// anonymous visitor's is a synthetic placeholder), else one they
// gave before the chat. With neither, the burst rule counts on the
// visitor themselves.
senderEmail:
normalizeEmail(realEmail(author.email) ?? undefined) ??
txResult.conversation.visitorEmail ??
null,
senderEmail: spamSenderEmail,
senderPrincipalId: author.principalId,
subject: null,
content: content || fallbackLabel,
signalsChecked: true,
})
)
.catch((err) => log.warn({ err }, 'spam classification failed'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,30 @@ describe('new-message bell and email', () => {
)
})

it('bell: a conversation filed as spam by the time the bell resolves reaches no one', async () => {
const w = await seedWorld()
const to = { assignee: null, team: null }
const { conversation } = await seedConversation(to)
// The event predates the filing (the AI classifier files after the create).
await testDb
.update(conversations)
.set({ status: 'closed', endReason: 'spam', spamReason: 'ai_classifier' })
.where(eq(conversations.id, conversation.id))
const target = await getMessageCreatedTargets(
messageCreated(conversation.id as ConversationId, to, true)
)
expect(target).toBeNull()
// Restored, it bells again.
await testDb
.update(conversations)
.set({ status: 'open', endReason: null, spamReason: null })
.where(eq(conversations.id, conversation.id))
const restored = await getMessageCreatedTargets(
messageCreated(conversation.id as ConversationId, to, true)
)
expect(within(w, recipientsOf(restored)).length).toBeGreaterThan(0)
})

it('bell: a conversation assigned to someone else with no team skips the other scoped teammates', async () => {
const w = await seedWorld()
const to = { assignee: w.solo.id, team: null }
Expand Down
10 changes: 10 additions & 0 deletions apps/web/src/lib/server/events/targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,16 @@ export async function getMessageCreatedTargets(event: EventData): Promise<HookTa
)
if (pairTicketId) return null

// A conversation in Spam bells no one: read by id, not from the payload, so
// a thread filed after the event was raised (the AI classifier files after
// the create) is caught too.
const [current] = await db
.select({ status: conversations.status, endReason: conversations.endReason })
.from(conversations)
.where(eq(conversations.id, event.data.message.conversationId as ConversationId))
.limit(1)
if (current?.status === 'closed' && current.endReason === 'spam') return null

const team = await db
.select({ id: principal.id, type: principal.type, role: principal.role })
.from(principal)
Expand Down
Loading