feat: move Captain conversation outcomes to episode grain [CW-7792] (#15315)

Moves Captain conversation outcomes from one row per conversation to one
row per **engagement episode**: a new row each time demand for Captain
(re)starts - first eligible message, a reopen after resolution, or
(reserved) explicit assignment. Each episode has its own demand anchor,
window, and trigger, so returning customers count as new demand and
later cycles can't overwrite an earlier episode's handoff reason,
resolution, or CSAT. Also adds `conversation_outcomes` associations on
Account, Inbox, Conversation, and Captain::Assistant.

## Why this wasn't in #15233

The episode design came out of reviewing the wiring PR: per-conversation
grain couldn't answer per-cycle questions without a patch per field. The
table is unreleased with no writers, so changing the grain now is a pure
schema swap - and landing it first means the tracker gets reviewed
against the final model.

## What changed

- Adds `episode_trigger`, `started_at`, `ended_at`
- Drops `reopen_count` and `last_reopened_at` - reopens are episode rows
now
- Uniqueness moves from `(account, assistant, conversation)` to
per-boundary `(account, conversation, started_at)`
- Two partial unique indexes: one open episode per conversation, one
initial episode per stream
- Model: trigger enum, `started_at` uniqueness validation,
`chronological`/`covering` scopes
This commit is contained in:
Shivam Mishra
2026-08-04 15:58:28 +05:30
committed by GitHub
parent 1c28df49e5
commit 94e9727eb4
9 changed files with 103 additions and 14 deletions

View File

@@ -15,6 +15,13 @@ RSpec.describe ConversationOutcome, type: :model do
.backed_by_column_of_type(:string)
.with_prefix(:handoff_reason)
}
it {
expect(subject).to define_enum_for(:episode_trigger)
.with_values(described_class::EPISODE_TRIGGERS.index_by(&:itself))
.backed_by_column_of_type(:string)
.with_prefix(:trigger)
}
end
describe 'validations' do
@@ -48,18 +55,34 @@ RSpec.describe ConversationOutcome, type: :model do
expect(outcome.errors[:inbox]).to be_present
end
it 'requires unique conversations per assistant and account' do
existing = create(:conversation_outcome)
it 'allows a later episode on the same conversation' do
existing = create(:conversation_outcome, started_at: 1.hour.ago, ended_at: 30.minutes.ago)
second_episode = build(
:conversation_outcome,
account: existing.account,
assistant: existing.assistant,
conversation: existing.conversation,
inbox: existing.inbox,
episode_trigger: 'reopen',
started_at: 30.minutes.ago
)
expect(second_episode).to be_valid
end
it 'rejects a duplicate boundary on the same conversation' do
existing = create(:conversation_outcome, started_at: 1.hour.ago)
duplicate = build(
:conversation_outcome,
account: existing.account,
assistant: existing.assistant,
conversation: existing.conversation,
inbox: existing.inbox
inbox: existing.inbox,
started_at: existing.started_at
)
expect(duplicate).not_to be_valid
expect(duplicate.errors[:conversation_id]).to be_present
expect(duplicate.errors[:started_at]).to be_present
end
end