Files
accounted/extensions/general/whatsapp-inbox/lib/last-event.ts
T
6c64dd2312 fix(whatsapp): make every silent disposition observable, stop pure silence for linked senders (#1563)
* fix(whatsapp): make every silent disposition observable, stop pure silence for linked senders (#1552)

Silence was a legitimate outcome in seven places and none left a trace a
support question could be answered from. Now:

- Unknown-sender declines (over quota, quota RPC failure, greeting
  throttle) persist content-free trace rows: wamid, phone hash, type,
  disposition. No body, media, raw payload, or profile name; capped at
  20 rows per hash and day; deleted by the existing 30-day retention.
  The wamid dedupe also stops redelivered bad-code/greeting messages
  from earning a second reply.
- Linked-sender deliberate silences (muted, stale tap, ignorable type)
  record their reason on the skipped row.
- Non-policy silences reply: a row missing its media reference sends
  M18 through the link's reply address, a link revoked between arrival
  and processing sends the M1 unlinked copy (greeting-throttled).
- Outbound rows keep WHY a send failed (Graph error detail), and Meta
  'failed' delivery statuses store their error code and title.
- The WhatsApp settings panel shows the last inbound event (closed
  enum, server-derived) and warns when the latest reply never left.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(whatsapp): include errorDetail in typed sendText mock results

SendTextResult gained errorDetail; vi.mocked call sites must match the
widened type or they raise fresh tsc errors over the repo baseline.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(whatsapp): review fixes: fail-closed greeting throttle, cap only declined traces

From CodeRabbit's pass on #1563:
- greetingThrottled fails closed when the throttle window cannot be
  read, matching the unknown-sender quota's stance.
- The decline-trace day cap applies only to 'skipped' rows (the one
  unbounded path); 'done' traces always insert so the wamid dedupe
  keeps preventing duplicate M1/M2 replies even past the cap. Their
  volume is already bounded upstream by the greeting throttle and the
  pre-binding quota.
- company-question test mocks match the widened SendTextResult.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-13 16:02:16 +02:00

52 lines
2.1 KiB
TypeScript

/**
* Compact, content-free summary of the most recent inbound message on a
* phone link, for the settings panel (issue #1552): when we last heard from
* this number and what happened to that message. Derives a closed enum from
* processing_status + error_message so no internal error text reaches the
* client; the panel translates the enum through i18n.
*/
export type LastInboundEventKind =
| 'filed' // done with an inbox item: the receipt landed in Underlag
| 'handled' // done without an item: command/answer/link code handled
| 'processing' // received or claimed, still in flight
| 'awaiting_company' // parked behind the open company question
| 'duplicate' // same file already exists in the company's Underlag
| 'rate_limited' // company intake quota declined the file
| 'unsupported' // media type outside the chat allowlist
| 'muted' // dropped because the sender paused the channel (stopp)
| 'declined' // other deliberate silence (stale tap, ignorable type)
| 'failed' // processing error (the M18 path)
export interface LastInboundEventRow {
processing_status: string
error_message: string | null
inbox_item_id: string | null
}
// error_message markers written by index.ts / process-inbound.ts /
// conversation.ts. Matched by prefix where the writer appends detail.
import { COMPANY_CHOICE_EXPIRED, STAGED_AWAITING_COMPANY } from './conversation'
export function summarizeInboundEvent(row: LastInboundEventRow): LastInboundEventKind {
switch (row.processing_status) {
case 'done':
return row.inbox_item_id ? 'filed' : 'handled'
case 'received':
case 'processing':
return 'processing'
case 'skipped': {
const reason = row.error_message ?? ''
if (reason === STAGED_AWAITING_COMPANY) return 'awaiting_company'
if (reason === COMPANY_CHOICE_EXPIRED) return 'failed'
if (reason.startsWith('Duplicate document')) return 'duplicate'
if (reason === 'Rate limited') return 'rate_limited'
if (reason.startsWith('Unsupported media type')) return 'unsupported'
if (reason.startsWith('Muted')) return 'muted'
return 'declined'
}
default:
return 'failed'
}
}