feat: add scan mode so conversations stay in sync without sending

Stopping the bot previously halted conversation syncing too. On the
multi-account path the run-flag check sat above both the reply-sync and
response-check branches, so a stop left only MediaSyncService's coarse
30-minute chat refresh. The legacy single-account path did the opposite
and kept syncing while stopped — one boolean meaning two things.

Replace the boolean with an explicit three-way mode:

  running — discover, screen, send, automate replies
  scan    — refresh chats, detect replies, check responses; send nothing
             and discover nothing; actionable replies surface as backlog
  stopped — everything halts, including background media sync

scan differs from browser.dryRun, which still discovers and pays for AI
screening before suppressing the send.

The mode persists in runtime_state.bot_mode; a migration backfills it
from bot_running, and rows written by an older build still read
correctly. Both loops now gate on the mode, the account runtime loop
reconciles background media sync on transitions only, and the dashboard
and agent API expose all three states.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Jamie BruntonandClaude Opus 5 committed 2026-08-11 10:16:24 +01:00
1 parent 3a40651930
commit 14d92e14ee
38 files changed
+724 -140

No files matched your search

@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { dashboardQueryKeys, useSetRunningMutation, useStatsQuery, useTimersQuery } from '../../api/queries.js';
import type { BotStats, Timers } from '../../api/contracts.js';
import { dashboardQueryKeys, useSetModeMutation, useStatsQuery, useTimersQuery } from '../../api/queries.js';
import type { BotMode, BotStats, Timers } from '../../api/contracts.js';
import type { AccountStatus } from '../../../../../packages/api-contracts/src/index.js';
import { ConfirmationDialog } from '../../components/confirmation-dialog.js';
import { RedditUserText } from '../../components/reddit-user-text.js';
@@ -9,6 +9,14 @@ import { OriginPostButton } from '../../components/origin-post.js';
import { RecentMessagesPanel } from './recent-messages.js';
import { AccountOwnerBadge } from '../../components/account-owner-badge.js';
const MODE_ORDER: readonly BotMode[] = ['running', 'scan', 'stopped'];
const modeCopy: Record<BotMode, { label: string; action: string; dot: string; buttonTone: string }> = {
running: { label: 'Running', action: 'Start bot', dot: 'running', buttonTone: 'button primary' },
scan: { label: 'Scanning', action: 'Scan mode', dot: 'scanning', buttonTone: 'button secondary' },
stopped: { label: 'Stopped', action: 'Stop bot', dot: 'stopped', buttonTone: 'button danger' },
};
function errorMessage(error: Error | null): string {
return error?.message ?? 'Something went wrong.';
}
@@ -83,6 +91,7 @@ function TimersPanel({ timers }: { timers: Timers }) {
const sendingStateCopy: Record<AccountStatus['sendingState'], { label: string; tone: 'success' | 'warning' | 'neutral'; detail: string }> = {
ready: { label: 'Ready', tone: 'success', detail: 'Available for the next outbound candidate.' },
'bot-stopped': { label: 'Bot stopped', tone: 'neutral', detail: 'Global sending is stopped.' },
'scan-only': { label: 'Scan only', tone: 'neutral', detail: 'Conversations stay in sync, but nothing is sent.' },
'runtime-stopped': { label: 'Runtime stopped', tone: 'warning', detail: 'This account runtime is not running.' },
'runtime-failed': { label: 'Runtime failed', tone: 'warning', detail: 'This account runtime needs attention.' },
'login-required': { label: 'Login required', tone: 'warning', detail: 'Refresh this account session before it can send.' },
@@ -174,8 +183,8 @@ export function OverviewPage() {
const queryClient = useQueryClient();
const statsQuery = useStatsQuery();
const timersQuery = useTimersQuery();
const controlMutation = useSetRunningMutation();
const [confirmationAction, setConfirmationAction] = useState<'start' | 'stop' | null>(null);
const controlMutation = useSetModeMutation();
const [confirmationAction, setConfirmationAction] = useState<BotMode | null>(null);
const isRefreshing = (statsQuery.isFetching || timersQuery.isFetching) && !statsQuery.isPending;
async function refreshOverview() {
@@ -187,7 +196,7 @@ export function OverviewPage() {
function confirmControl() {
if (!confirmationAction) return;
controlMutation.mutate(confirmationAction === 'start', {
controlMutation.mutate(confirmationAction, {
onSuccess: () => setConfirmationAction(null),
});
}
@@ -216,7 +225,8 @@ export function OverviewPage() {
}
const stats = statsQuery.data;
const action = stats.isRunning ? 'stop' : 'start';
// Offer the two modes the bot is not currently in, in a stable order.
const offeredModes = MODE_ORDER.filter((mode) => mode !== stats.mode);
return (
<div className="page">
@@ -237,20 +247,23 @@ export function OverviewPage() {
>
{isRefreshing ? 'Refreshing…' : 'Refresh'}
</button>
<button
className={action === 'start' ? 'button primary' : 'button danger'}
onClick={() => setConfirmationAction(action)}
type="button"
>
{action === 'start' ? 'Start bot' : 'Stop bot'}
</button>
{offeredModes.map((mode) => (
<button
className={modeCopy[mode].buttonTone}
key={mode}
onClick={() => setConfirmationAction(mode)}
type="button"
>
{modeCopy[mode].action}
</button>
))}
</div>
</header>
<section aria-label="Bot state" className="status-strip">
<div>
<span className={`status-dot ${stats.isRunning ? 'running' : 'stopped'}`} aria-hidden="true" />
<div><small>Bot status</small><strong data-bot-status>{stats.isRunning ? 'Running' : 'Stopped'}</strong></div>
<span className={`status-dot ${modeCopy[stats.mode].dot}`} aria-hidden="true" />
<div><small>Bot status</small><strong data-bot-status>{modeCopy[stats.mode].label}</strong></div>
</div>
{stats.dryRunMode ? <span className="pill neutral">Dry run</span> : null}
<span className="status-note">{stats.messagesThisHour} of {stats.maxMessagesPerHour} messages used this hour</span>