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
20 changes: 0 additions & 20 deletions apps/web/e2e/QUARANTINE.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,23 +238,3 @@ Each entry: owner: unassigned, review by: 2026-12-03.
there, the requester's thread then failed with "Something went wrong", so
the fix likely needs more than that line. owner: unassigned, review by:
2026-12-03.

### Support suite: inbox remainder

- `src/lib/server/domains/conversation/__tests__/conversation-spam-autofile-quiet.db.test.ts`
› a spam-filed cold email rings no team bell (vitest, marked `it.fails`
rather than tagged: remove the marker when it starts to fail). A cold
inbound email that a deterministic rule files into Spam still bells the
team. `createEmailConversation` creates the thread open and writes
`conversation.created` and `message.created` to the outbox
(`conversation.email-cold-inbound.ts:276`, `:280`) before
`maybeAutoFileSpam` files it (`conversation.email-inbound.service.ts:784`),
and the worker-time bell resolver `getMessageCreatedTargets`
(`events/targets.ts:1215`) never checks the filing, so every teammate in
scope gets a new-message bell for spam. Outbound webhooks (and workflow
triggers, when a live workflow exists) are resolved from the same two
events. The messenger path has the same order: `notifyVisitorMessage`
(team email, `conversation.service.ts:507`) runs before the filter
(`:551`), whose burst-rate rule needs only the visitor's email (traced,
not tested). owner: unassigned, review by:
2026-12-03.
22 changes: 7 additions & 15 deletions apps/web/e2e/tests/support/email-inbound.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,21 +978,13 @@ test.describe('spam, loops and floods', () => {
spam_reason: 'auto_responder',
end_note: 'Auto-filed by the spam filter (Auto-responder)',
})
const system = (await messagesIn(auto.row.id)).filter((m) => m.sender_type === 'system')
expect(system.map((m) => [m.content, m.is_internal])).toContainEqual([
'Conversation auto-filed as spam',
true,
])
// It had already been created and announced before filing.
await expect
.poll(() => eventTypesFor(auto.id))
.toEqual(
expect.arrayContaining([
'conversation.created',
'message.created',
'conversation.status_changed',
])
)
// Filed as it arrived, like quarantined mail: nothing was announced, so
// no bell, workflow or webhook reacts to it. (Settle briefly first: an
// event would be written in the same request.)
await new Promise((resolve) => setTimeout(resolve, 1_000))
const types = await eventTypesFor(auto.id)
expect(types).not.toContain('conversation.created')
expect(types).not.toContain('message.created')

// Sender-auth failure short of a reject.
const weak = await coldConversation(request, `weak-${customerEmail}`, `${marker}-weak`, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,36 +204,23 @@ describe.skipIf(!fixture.available)('spam auto-filing stays quiet (real DB, roll
expect(bell.recipients).toContain(admin)
})

it('a deterministic rule files the cold email into Spam with a team-only notice', async () => {
it('a deterministic rule files the cold email into Spam as it arrives, with its reason', async () => {
const { conversationId } = await ingestFiled()
const stored = await testDb.query.conversations.findFirst({
where: eq(conversations.id, conversationId),
})
expect(stored?.status).toBe('closed')
expect(stored?.endReason).toBe('spam')
expect(stored?.spamReason).toBe('auto_responder')
expect(stored?.endNote).toContain('Auto-filed by the spam filter')
expect(mockChat).not.toHaveBeenCalled()
const notices = await testDb.query.conversationMessages.findMany({
where: (m, { eq: eq_ }) => eq_(m.conversationId, conversationId),
})
const system = notices.filter((m) => m.senderType === 'system')
expect(system.map((m) => [m.content, m.isInternal])).toEqual([
['Conversation auto-filed as spam', true],
])
// The bell below is resolved from a real outbox event, not a missing one.
expect((await bellRecipients(conversationId)).eventFound).toBe(true)
// Filed in its insert, like quarantined mail: no message event is written,
// so nothing downstream (bells, workflows, webhooks) can react to it.
expect((await bellRecipients(conversationId)).eventFound).toBe(false)
})

// Product bug (e2e/QUARANTINE.md, "Support suite: inbox remainder"): the
// thread is created open and its message.created event is written before
// maybeAutoFileSpam files it (conversation.email-cold-inbound.ts, the emits
// after the insert; conversation.email-inbound.service.ts, the filing after
// createEmailConversation), and getMessageCreatedTargets (events/targets.ts)
// never looks at the filing, so the worker bells the team for spam. `fails`
// keeps the suite green until the fix lands; then this turns red and the
// marker comes off. Setup is proven by the test above, so the only thing
// that can fail here is the assertion.
it.fails('a spam-filed cold email rings no team bell', async () => {
// INB-62: a cold email a fixed rule files as spam rings no one.
it('a spam-filed cold email rings no team bell', async () => {
const { conversationId } = await ingestFiled()
expect((await bellRecipients(conversationId)).recipients).toEqual([])
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ vi.mock('../conversation.webhooks', async (orig) => ({
emitMessageCreated: vi.fn().mockResolvedValue(undefined),
emitConversationStatusChanged: vi.fn().mockResolvedValue(undefined),
}))
const mockSendAck = vi.fn().mockResolvedValue('sent')
vi.mock('../conversation.auto-ack', () => ({
maybeSendColdInboundAck: (...args: unknown[]) => mockSendAck(...args),
}))
vi.mock('@/lib/server/realtime/conversation-channels', () => ({
publishConversationEvent: vi.fn(),
publishConversationMessage: vi.fn(),
Expand Down Expand Up @@ -92,6 +96,16 @@ import { ingestParsedEmail } from '../conversation.email-inbound.service'
import { listConversationsForAgent } from '../conversation.query'
import { maybeAutoFileSpam } from '../conversation.spam-filter'
import { sendVisitorMessage } from '../conversation.service'
import { emitConversationCreated, emitMessageCreated } from '../conversation.webhooks'

/** Mail filed by a fixed rule raises nothing a customer or the team sees. */
async function expectNoSignals(): Promise<void> {
// The acknowledgement is sent out of band; let it start if it is going to.
await new Promise((resolve) => setTimeout(resolve, 20))
expect(emitConversationCreated).not.toHaveBeenCalled()
expect(emitMessageCreated).not.toHaveBeenCalled()
expect(mockSendAck).not.toHaveBeenCalled()
}

const fixture = await createDbTestFixture({
probe: async (db) => {
Expand Down Expand Up @@ -252,6 +266,10 @@ describe.skipIf(!fixture.available)('inbound auto-spam filter (real DB, rolled b
expect(stored?.status).toBe('open')
expect(stored?.endReason).toBeNull()
await expect(spamViewIds()).resolves.not.toContain(res.conversationId)
// Accepted mail raises its signals and is acknowledged.
expect(emitConversationCreated).toHaveBeenCalledTimes(1)
expect(emitMessageCreated).toHaveBeenCalledTimes(1)
await vi.waitFor(() => expect(mockSendAck).toHaveBeenCalledTimes(1))
})

it('bypasses classification for a workspace-trusted sender', async () => {
Expand Down Expand Up @@ -292,6 +310,7 @@ describe.skipIf(!fixture.available)('inbound auto-spam filter (real DB, rolled b
expect(stored?.status).toBe('closed')
expect(stored?.endReason).toBe('spam')
expect(stored?.spamReason).toBe('auto_responder')
await expectNoSignals()
await expect(spamViewIds()).resolves.toContain(res.conversationId)
await expect(spamViewReason(res.conversationId)).resolves.toBe('auto_responder')
// The deterministic signal filed it; the AI path never ran.
Expand Down Expand Up @@ -331,6 +350,7 @@ describe.skipIf(!fixture.available)('inbound auto-spam filter (real DB, rolled b
expect(stored?.status).toBe('closed')
expect(stored?.endReason).toBe('spam')
expect(stored?.spamReason).toBe('sender_auth_failure')
await expectNoSignals()
expect(mockChat).not.toHaveBeenCalled()
})

Expand All @@ -352,6 +372,7 @@ describe.skipIf(!fixture.available)('inbound auto-spam filter (real DB, rolled b
expect(stored?.status).toBe('closed')
expect(stored?.endReason).toBe('spam')
expect(stored?.spamReason).toBe('burst_rate')
await expectNoSignals()
expect(mockChat).not.toHaveBeenCalled()
})

Expand Down Expand Up @@ -458,6 +479,7 @@ describe.skipIf(!fixture.available)('inbound auto-spam filter (real DB, rolled b
})
expect(stored?.status).toBe('open')
expect(stored?.endReason).toBeNull()
expect(emitConversationCreated).toHaveBeenCalledTimes(1)
expect(mockChat).not.toHaveBeenCalled()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ export async function resolveColdInboundSender(
* signals a widget-started one does. Direct inserts (the visitor-message create
* path hardcodes channel='messenger'); the emit bridge is error-isolated.
*
* `quarantine` inverts both halves of that for a message we REFUSED. It files
* the thread to Spam in the same INSERT, and it fires neither emit.
* `quarantine` inverts both halves of that for a message we REFUSED, and for
* one a fixed spam rule filed. It files the thread to Spam in the same INSERT,
* and it fires neither emit.
*
* Filing in the insert rather than with a follow-up update is what makes the
* refusal hold: there is no instant at which a refused message sits in the open
Expand All @@ -183,8 +184,9 @@ export async function createEmailConversation(input: {
contentJson?: TiptapContent | null
/** Discrete files rehosted from the inbound MIME parts, or none. */
attachments?: ConversationAttachment[]
/** Refused mail: file to Spam in the insert and raise no signals. The cause
* is the enumerated one the Spam view badges the row with. */
/** Refused mail, or mail a fixed spam rule filed: file to Spam in the
* insert and raise no signals. The cause is the enumerated one the Spam
* view badges the row with. */
quarantine?: { cause: ConversationSpamFiledBy; note: string } | null
}): Promise<ConversationId> {
const { parsed, channelAccountId, principalId, unverified, content, contentJson } = input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ import {
createEmailConversation,
cleanupColdInboundLead,
} from './conversation.email-cold-inbound'
import { maybeAutoFileSpam } from './conversation.spam-filter'
import { fixedSpamSignalFor, maybeAutoFileSpam } from './conversation.spam-filter'
import { CONVERSATION_SPAM_FILED_BY_LABELS } from '@/lib/shared/conversation/types'
import { DEFAULT_LOCALE } from '@/lib/shared/i18n'
import { senderAuthSpamSignal } from './conversation.spam-signals'

Expand Down Expand Up @@ -748,6 +749,30 @@ async function ingestColdInbound(
return { status: 'no_conversation' }
}

// A fixed spam rule (auto-responder headers, weak sender authentication, a
// burst from one sender) is decided before the thread exists, so a filed
// message is filed in the insert and raises nothing, exactly like refused
// mail: no created or message events, so no webhook, bell, workflow or SLA
// clock, and no acknowledgement mail. The workspace trust list still
// outranks it. Only the AI classifier runs after the create.
const spamSignal = quarantineCause
? null
: await fixedSpamSignalFor(
{
senderEmail,
signals: { autoResponder: opts.autoResponder === true, senderAuthSignal: authSignal },
},
resolution.principalId
)
const filing = quarantineCause
? { cause: quarantineCause, note: quarantineNote }
: spamSignal
? {
cause: spamSignal,
note: `Auto-filed by the spam filter (${CONVERSATION_SPAM_FILED_BY_LABELS[spamSignal]})`,
}
: null

let conversationId: ConversationId
try {
conversationId = await createEmailConversation({
Expand All @@ -762,7 +787,7 @@ async function ingestColdInbound(
// in the open queue. The free-text note is the MTA's own wording, or ours
// for a refusal we decided ourselves, kept as a supplement to the
// enumerated cause and never as a substitute for it.
quarantine: quarantineCause ? { cause: quarantineCause, note: quarantineNote } : null,
quarantine: filing,
})
} catch (error) {
await discardMintedLead()
Expand All @@ -774,23 +799,23 @@ async function ingestColdInbound(
return { status: 'quarantined', conversationId, cause: quarantineCause }
}

// Spam filter, after the thread exists so a filing has something to land
// on. Error-isolated inside (a classification failure leaves the thread in
// triage, never breaks ingestion); the workspace trust list bypasses it.
// The deterministic signals run first: auto-responder headers (this mail
// arrived already flagged machine-generated) and whatever the sender's
// authentication earned short of the quarantine above — either files to Spam
// without spending an AI completion.
await maybeAutoFileSpam(conversationId, {
if (spamSignal) {
log.info({ conversation_id: conversationId, signal: spamSignal }, 'spam signal matched; filed')
return { status: 'ingested', conversationId }
}

// The AI classifier, after the thread exists so a filing has something to
// land on. Error-isolated inside (a classification failure leaves the thread
// in triage, never breaks ingestion); the workspace trust list bypasses it.
// The fixed rules already ran above.
const filedBySpamFilter = await maybeAutoFileSpam(conversationId, {
senderEmail,
subject: parsed.subject,
content,
signals: {
autoResponder: opts.autoResponder === true,
senderAuthSignal: authSignal,
},
signalsChecked: true,
})
if (opts.autoResponder !== true) {
// Mail filed to Spam gets no acknowledgement.
if (!filedBySpamFilter && opts.autoResponder !== true) {
// Fire-and-forget on purpose — the ack must not delay the ingest response —
// but never unhandled: a failed ack is a log line, not a process-level
// unhandled rejection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { enforceAiTokenBudget } from '@/lib/server/domains/settings/tier-enforce
import { TierLimitError } from '@/lib/server/errors/tier-limit-error'
import { logger } from '@/lib/server/logger'
import type { SpamFilterConfig } from '@/lib/server/domains/settings/settings.spam'
import type { SpamSignalHints } from './conversation.spam-signals'
import type { SpamSignal, SpamSignalHints } from './conversation.spam-signals'

const log = logger.child({ component: 'spam-filter' })

Expand Down Expand Up @@ -167,6 +167,41 @@ export async function classifyInboundAsSpam(input: ClassifyInboundSpamInput): Pr
return verdict.spam === true
}

/**
* The fixed rule (a deterministic signal, never the AI) that files a new
* conversation's first message to Spam, decided BEFORE the conversation
* exists so the caller can file it in the insert and raise nothing: no
* created/message events, no webhook, no bell, no workflow, no SLA clock.
* Null when no rule matches, and when the sender is a test customer or is on
* the workspace trust list (which outranks every filing path). Never throws:
* any failure is "no rule", leaving the message in triage.
*
* A caller that uses this runs `maybeAutoFileSpam` afterwards with
* `signalsChecked`, so the signals (the burst count above all) are evaluated
* once per message.
*/
export async function fixedSpamSignalFor(
input: Pick<ClassifyInboundSpamInput, 'senderEmail'> & {
signals?: SpamSignalHints
},
visitorPrincipalId: string
): Promise<SpamSignal | null> {
try {
const { isTestCustomer } = await import('@/lib/server/test-data')
if (await isTestCustomer(visitorPrincipalId)) return null
if (await isTrustedInboundSender(input.senderEmail)) return null
const { detectSpamSignal } = await import('./conversation.spam-signals')
return await detectSpamSignal({
senderEmail: input.senderEmail,
senderPrincipalId: visitorPrincipalId,
...input.signals,
})
} catch (err) {
log.warn({ err }, 'fixed spam rule check failed, leaving the message in triage')
return null
}
}

/**
* Classify, then file the conversation as spam on an affirmative verdict.
* The one call both ingest paths make; error-isolated so a filing failure
Expand All @@ -179,19 +214,26 @@ export async function classifyInboundAsSpam(input: ClassifyInboundSpamInput): Pr
*/
export async function maybeAutoFileSpam(
conversationId: ConversationId,
input: ClassifyInboundSpamInput & { signals?: SpamSignalHints }
input: ClassifyInboundSpamInput & {
signals?: SpamSignalHints
/** The caller already ran the fixed rules (`fixedSpamSignalFor`) and none
* matched: only the AI classifier is left. */
signalsChecked?: boolean
}
): Promise<boolean> {
try {
// A teammate's test thread is never spam, and spends no classification.
const { isTestConversation } = await import('@/lib/server/test-data')
if (await isTestConversation(conversationId)) return false
if (await isTrustedInboundSender(input.senderEmail)) return false
const { detectSpamSignal } = await import('./conversation.spam-signals')
const signal = await detectSpamSignal({
senderEmail: input.senderEmail,
senderPrincipalId: input.senderPrincipalId ?? null,
...input.signals,
})
const signal = input.signalsChecked
? null
: await detectSpamSignal({
senderEmail: input.senderEmail,
senderPrincipalId: input.senderPrincipalId ?? null,
...input.signals,
})
if (signal) {
log.info({ conversation_id: conversationId, signal }, 'spam signal matched; filing')
const { autoFileConversationAsSpam } = await import('./conversation.service')
Expand Down
Loading