Skip to content

fix(messages): keep failed sends only for inbox messages, log the rest - #1749

Merged
sung17 merged 2 commits into
mainfrom
fix/failed-send-keep-only-inbox-messages
Nov 18, 2026
Merged

sung17 merged 2 commits into
mainfrom
fix/failed-send-keep-only-inbox-messages

Conversation

@sung17

@sung17 sung17 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • A failed outgoing message stayed in the conversation with Failed to send no matter what sent it, so automated sends (flows, triggers, AI, keyword replies, comment automation, broadcasts) filled the inbox with bot errors.
  • New rule: only a message a member sends from the builder Inbox is kept with its error. Every other send is removed on its final failure and its error goes to the Error Log. Broadcast and sequence failures are removed and stay out of the Error Log, as they are today; they show in those features' own stats.
  • Existing failed rows are left as they are (no migration, no cleanup).

Behaviour by send source

Source On final failure
Inbox: composer (text, files, products, comment / private reply), flow, WhatsApp template, booking link, call-permission request, Resend Kept with sendError + messageFailed (unchanged)
Public API / CLI / MCP (member or workspace token), order notifications Removed; Error Log. "Could not queue" also removes the row, and the API caller still gets the error
Flows, triggers, keyword/default replies, AI agent, questionnaires, public comment replies, minigame, templates from flows Removed; Error Log
Broadcasts, sequences (including templates) Removed; no Error Log
Bot message quota skip Removed; Error Log row "Bot message limit reached" (not for bulk)
Late "failed" delivery status Same keep/remove rule
AI comment private reply, SMTP email step (no row) Now also write an Error Log row

Only the final failure settles a row. Non-Inbox sends no longer write sendError on every retry attempt.

How

  • sendFrom is now "inbox" | "api" (packages/utils/src/send-from.ts). Builder actions and the session-only messages/api/private.ts route pass "inbox"; the public APIs and order/service.ts pass "api". createOutgoing, resendOutgoing, sendInboxBookingLink, requestWhatsappCallPermission and the inbox template sender require it. The six existing sendFrom comparisons (messaging policy, inbox send confirmation, quick replies) use isManualSend(), which treats both values alike, so how messages are sent does not change.
  • Row marker: rows created with sendFrom: "inbox" get contentAttributes.sentFromInbox (isSentFromInbox in partials/message.ts), so retries and late delivery-status failures apply the same rule.
  • One settle function: settleFailedOutgoingMessage in send-message.ts, used by sendMessageToChannel, flow steps, both template senders, message-status.ts, record-quota-skipped-send.ts and createOutgoing's queueing failure.
  • Hard delete: hardDeleteById in the sharded repository and messageService.hardDelete remove the message and its attachment rows. The existing delete is soft and renders as "The message has been deleted".
  • Realtime: new messageRemoved event. The chat store's removeMessages drops the rows and repoints the open conversation's preview.
  • Templates: an attempt that will be retried removes its own row. Retries used to leave one row per attempt, and a failed template used to look delivered.
  • Docs: docs/failed-outgoing-messages.md (linked from AGENTS.md), docs/comment-automation.md, and the public API descriptions for messages.create, sendWhatsappTemplate, resend and contacts.sendMessage.

Known limits

  • Attachment files in object storage are not deleted, only the DB rows.
  • A conversation that isn't open keeps its list preview until the list reloads. The webchat guest widget does not get messageRemoved.
  • If a retry never runs (e.g. the owner is blocked), the row stays without an error.
  • An Inbox template can briefly disappear and reappear between retry attempts.
  • API behaviour change: a failed API-sent message is deleted, so reading it back by id returns not found, and it can no longer be resent.

Test plan

  • pnpm lint
  • pnpm check-types (67/67)
  • pnpm --filter worker test: 393 files, 5283 tests
  • business, builder, database, realtime-protocol, sdk, utils, worker-config, messenger, instagram suites pass
  • Production: fail a flow message and an API message → both disappear, Error Log has a row each; fail a composer message → kept with "Failed to send"

🤖 Generated with Claude Code

A failed outgoing message stayed in the conversation with "Failed to send"
whatever sent it, so automated sends (flows, triggers, AI, keyword replies,
comment automation, broadcasts) cluttered the inbox with bot errors.

Now only a message a member sends from the builder Inbox (composer, flow,
template, booking link, call-permission request, Resend) is kept with its
error. Every other send is hard-deleted on its final failure and a new
`messageRemoved` realtime event drops it from the open timeline; the error
reaches the Error Log through the existing `message:failed` listener
(broadcast and sequence sends stay out of it, as before).

- `sendFrom` is now "inbox" | "api": builder actions pass "inbox", the
  public API and order notifications pass "api"; the six existing
  comparisons treat both alike (`isManualSend`), so send behaviour is
  unchanged. Rows sent from the Inbox carry `sentFromInbox` so retries and
  late delivery-status failures apply the same rule.
- One settle function for sendMessageToChannel, flow steps, both template
  senders, delivery-status failures, quota skips and queueing failures.
- Template retries no longer leave a duplicate row per attempt, and a
  failed template is no longer left looking delivered.
- AI comment private replies and the SMTP email step now write Error Log
  rows.
- `hardDeleteById` removes the message and its attachments (the existing
  delete is soft and renders as "The message has been deleted").

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working as expected label Nov 18, 2026
…essages

hardDeleteById swallowed every shard error and returned [], so callers
published messageRemoved for a delete that never happened and the row
reappeared after a reload. It now deletes the attachments and the message
in one transaction per shard, tries every shard, and throws if any failed;
the worker and createOutgoing already log that and skip the realtime event.

A late "failed" delivery status carries no sendFrom, so an Inbox message
sent before the sentFromInbox marker existed was removed as if automated.
createOutgoing and the template senders now stamp sentFromInbox true/false,
and a member's message with no stamp at all is kept (isLegacyMemberMessage).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@sung17
sung17 merged commit 5347d2e into main Nov 18, 2026
14 checks passed
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
…t (#1749)

* fix(messages): keep failed sends only for inbox messages, log the rest

A failed outgoing message stayed in the conversation with "Failed to send"
whatever sent it, so automated sends (flows, triggers, AI, keyword replies,
comment automation, broadcasts) cluttered the inbox with bot errors.

Now only a message a member sends from the builder Inbox (composer, flow,
template, booking link, call-permission request, Resend) is kept with its
error. Every other send is hard-deleted on its final failure and a new
`messageRemoved` realtime event drops it from the open timeline; the error
reaches the Error Log through the existing `message:failed` listener
(broadcast and sequence sends stay out of it, as before).

- `sendFrom` is now "inbox" | "api": builder actions pass "inbox", the
  public API and order notifications pass "api"; the six existing
  comparisons treat both alike (`isManualSend`), so send behaviour is
  unchanged. Rows sent from the Inbox carry `sentFromInbox` so retries and
  late delivery-status failures apply the same rule.
- One settle function for sendMessageToChannel, flow steps, both template
  senders, delivery-status failures, quota skips and queueing failures.
- Template retries no longer leave a duplicate row per attempt, and a
  failed template is no longer left looking delivered.
- AI comment private replies and the SMTP email step now write Error Log
  rows.
- `hardDeleteById` removes the message and its attachments (the existing
  delete is soft and renders as "The message has been deleted").

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(messages): fail a hard delete loudly and keep pre-marker member messages

hardDeleteById swallowed every shard error and returned [], so callers
published messageRemoved for a delete that never happened and the row
reappeared after a reload. It now deletes the attachments and the message
in one transaction per shard, tries every shard, and throws if any failed;
the worker and createOutgoing already log that and skip the realtime event.

A late "failed" delivery status carries no sendFrom, so an Inbox message
sent before the sentFromInbox marker existed was removed as if automated.
createOutgoing and the template senders now stamp sentFromInbox true/false,
and a member's message with no stamp at all is kept (isLegacyMemberMessage).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

---------

Co-authored-by: Deathgiver <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant