feat: application-level read state and inbox triage for conversations

Conversations were hard to manage because "unread" came straight from
Matrix's notification_count, and nothing in the app ever sends a read
receipt — so opening a conversation in the dashboard could never clear it
and the unread set only ever grew. The list also collapsed three
orthogonal facts into one word, labelling "they spoke last and we have
not replied" as "Up to date".

Read state is now ours:

- conversation_read_state table (migration 19) plus
  ConversationReadStateRepository, kept separate from conversation_states
  so a read write can never be lost to mergeConversationState. The
  watermark is monotonic, so a stale poll cannot resurrect an unread
  badge.
- hasUnreadInbound and needsReply are derived server-side and returned on
  every conversation. Matrix's `unread` stays on the contract but the
  dashboard no longer reads it.
- POST /conversations/{id}/read|unread|status and
  POST /conversations/bulk-read, with optimistic cache updates.

Inbox rework:

- List rows are a real grid (checkbox, open button, chip strip) instead of
  a full-bleed absolute overlay button, which previously forced a
  pointer-events escape hatch for anything interactive in a row. Rows use
  aria-current rather than aria-pressed. Not a listbox: options must not
  contain interactive descendants.
- Counted filter pills replace the select, independent state chips replace
  the collapsed status word, plus a sort control (including oldest
  unanswered), a per-account filter, age-coloured timestamps, and an
  explicit count of rows hidden by the seller filter.
- Keyboard: j/k/arrows, Enter, u, e, / and Escape. Shortcuts yield to any
  open dialog so Escape dismisses the modal, not the conversation behind
  it. Multi-select with bulk mark-read and mark-done.
- Detail panel: auto-mark-read on open, a "New messages" divider pinned at
  mount (deriving it from the live watermark made it vanish a second after
  opening), triage actions in the header, the automation banner moved out
  of the composer, day dividers with time-only stamps, and a flex column
  so history scrolls in its own region instead of painting over the
  composer.
- Row order freezes while a conversation is open, with a "N new — refresh"
  pill, so a background poll cannot reorder rows mid-triage.

Two database tests rewound user_version by an offset from the latest
version, which silently re-targets a different migration whenever one is
appended. They now pin absolute versions.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Jamie BruntonandClaude Opus 5 committed 2026-08-10 10:06:03 +01:00
1 parent c8689af04d
commit 725b5fc4e6
26 files changed
+2625 -203

No files matched your search

+13
View File
@@ -412,6 +412,19 @@ export function runMigrations(db: Database.Database): void {
CREATE INDEX IF NOT EXISTS idx_media_library_sends_conversation
ON media_library_sends(account_id, conversation_id, requested_at);
`),
() => db.exec(`
CREATE TABLE IF NOT EXISTS conversation_read_state (
account_id TEXT NOT NULL,
conversation_id TEXT NOT NULL,
last_read_message_ts INTEGER NOT NULL DEFAULT 0,
last_read_at INTEGER NOT NULL,
status TEXT NOT NULL DEFAULT 'open' CHECK(status IN ('open', 'snoozed', 'done')),
snoozed_until INTEGER,
PRIMARY KEY (account_id, conversation_id)
);
CREATE INDEX IF NOT EXISTS idx_conversation_read_state_status
ON conversation_read_state(account_id, status, snoozed_until);
`),
];
let currentVersion = db.pragma('user_version', { simple: true }) as number;
+121
View File
@@ -1180,6 +1180,127 @@ export class ConversationStateRepository {
}
}
/**
* Operator-facing triage state for a conversation.
*
* This is deliberately separate from `conversation_states`: that blob is owned
* by the automation pipeline and goes through `mergeConversationState`, whereas
* read state is written by the dashboard on every open and must never lose a
* write to a merge. Matrix's own `unread_notifications` count is not usable for
* this — nothing in the app sends read receipts, so it never clears.
*/
export type ConversationTriageStatus = 'open' | 'snoozed' | 'done';
export interface ConversationReadStateRecord {
conversationId: string;
lastReadMessageTs: number;
lastReadAt: number;
status: ConversationTriageStatus;
snoozedUntil: number | null;
}
interface ConversationReadStateRow {
conversation_id: string;
last_read_message_ts: number;
last_read_at: number;
status: ConversationTriageStatus;
snoozed_until: number | null;
}
function toReadStateRecord(row: ConversationReadStateRow): ConversationReadStateRecord {
return {
conversationId: row.conversation_id,
lastReadMessageTs: row.last_read_message_ts,
lastReadAt: row.last_read_at,
status: row.status,
snoozedUntil: row.snoozed_until,
};
}
export class ConversationReadStateRepository {
constructor(
private readonly db: Database.Database,
private readonly accountId: string = LEGACY_UNASSIGNED_ACCOUNT_ID,
) {}
get(conversationId: string): ConversationReadStateRecord | undefined {
const row = this.db.prepare(`
SELECT conversation_id, last_read_message_ts, last_read_at, status, snoozed_until
FROM conversation_read_state WHERE account_id = ? AND conversation_id = ?
`).get(this.accountId, conversationId) as ConversationReadStateRow | undefined;
return row ? toReadStateRecord(row) : undefined;
}
list(): ConversationReadStateRecord[] {
const rows = this.db.prepare(`
SELECT conversation_id, last_read_message_ts, last_read_at, status, snoozed_until
FROM conversation_read_state WHERE account_id = ?
`).all(this.accountId) as ConversationReadStateRow[];
return rows.map(toReadStateRecord);
}
/**
* Advances the read watermark. Never moves backwards, so a stale poll landing
* after the operator has read further cannot resurrect an unread badge.
*/
markRead(conversationId: string, throughTimestamp: number, now = Date.now()): void {
this.db.prepare(`
INSERT INTO conversation_read_state (account_id, conversation_id, last_read_message_ts, last_read_at)
VALUES (?, ?, ?, ?)
ON CONFLICT(account_id, conversation_id) DO UPDATE SET
last_read_message_ts = MAX(last_read_message_ts, excluded.last_read_message_ts),
last_read_at = excluded.last_read_at
`).run(this.accountId, conversationId, Math.max(0, throughTimestamp), now);
}
/**
* Pushes a conversation back onto the queue by rewinding the watermark to just
* before the message the operator wants to see as new again.
*/
markUnread(conversationId: string, beforeTimestamp: number, now = Date.now()): void {
const watermark = Math.max(0, beforeTimestamp - 1);
this.db.prepare(`
INSERT INTO conversation_read_state (account_id, conversation_id, last_read_message_ts, last_read_at, status)
VALUES (?, ?, ?, ?, 'open')
ON CONFLICT(account_id, conversation_id) DO UPDATE SET
last_read_message_ts = excluded.last_read_message_ts,
last_read_at = excluded.last_read_at,
status = 'open',
snoozed_until = NULL
`).run(this.accountId, conversationId, watermark, now);
}
setStatus(
conversationId: string,
status: ConversationTriageStatus,
snoozedUntil: number | null = null,
now = Date.now(),
): void {
this.db.prepare(`
INSERT INTO conversation_read_state (account_id, conversation_id, last_read_message_ts, last_read_at, status, snoozed_until)
VALUES (?, ?, 0, ?, ?, ?)
ON CONFLICT(account_id, conversation_id) DO UPDATE SET
status = excluded.status,
snoozed_until = excluded.snoozed_until,
last_read_at = excluded.last_read_at
`).run(this.accountId, conversationId, now, status, status === 'snoozed' ? snoozedUntil : null);
}
/** Returns snoozed conversations whose snooze has expired, so they can reopen. */
expireSnoozes(now = Date.now()): string[] {
const rows = this.db.prepare(`
SELECT conversation_id FROM conversation_read_state
WHERE account_id = ? AND status = 'snoozed' AND snoozed_until IS NOT NULL AND snoozed_until <= ?
`).all(this.accountId, now) as Array<{ conversation_id: string }>;
if (rows.length === 0) return [];
this.db.prepare(`
UPDATE conversation_read_state SET status = 'open', snoozed_until = NULL, last_read_at = ?
WHERE account_id = ? AND status = 'snoozed' AND snoozed_until IS NOT NULL AND snoozed_until <= ?
`).run(now, this.accountId, now);
return rows.map((row) => row.conversation_id);
}
}
/** Durable idempotency claims for AI replies to individual inbound events. */
export interface ConversationAutomationClaimRecord {
conversationId: string;