Skip to content

fix(mail): grade outbound delivery on the whole queue, not the top three reasons - #945

Merged
Hydralerne merged 2 commits into
oblien:mainfrom
sprayberry-code:fix/mail-delivery-verdict-past-reason-cap
Sep 25, 2026
Merged

Hydralerne merged 2 commits into
oblien:mainfrom
sprayberry-code:fix/mail-delivery-verdict-past-reason-cap

Conversation

@askalf

@askalf askalf commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

checkMailDelivery graded outbound delivery on only the first three deferral reasons the queue reported, so a fatal reason ranked fourth or later was graded warn instead of fail. It now grades on the whole queue and reports the top three reasons.

Motivation

Refs #876. A queue with three benign deferrals ahead of an auth or TLS failure reported a healthy-looking warn.

Related issue

None required (bug fix). Refs #876.

Changes

  • apps/api/src/modules/mail/mail-delivery.service.ts: parseMailQueue keeps every reason; new topDeferrals returns the first three for the report; checkMailDelivery grades on all of them.
  • apps/api/test/modules/mail/mail-delivery.service.test.ts: one existing test updated and five it.each rows added, all failing on main.

Verification

bun install --frozen-lockfile
bun run --cwd apps/api test -- test/modules/mail/mail-delivery.service.test.ts   # main: 6 failed / 41 passed; this branch: 47 passed
bun run --cwd apps/api lint                                                      # rc=0
bun format                                                                       # unrelated hunks dropped before pushing

Run twice in fresh worktrees (Bun 1.3.14, Vitest 4.0.18). Six production-file mutants were rebuilt and re-run; the test rows kill each of them. Verbatim output is in the sections below.

Checklist

  • One change per PR — one bug, or one agreed feature, with nothing unrelated bundled in
  • The diff is scoped — no reformatting or lint fixes on lines I wasn't otherwise changing
  • A test fails without this change and passes with it (or I explained above why there isn't one)
  • bun run test, bun run --cwd <workspace> lint, and bun format all pass locally (the touched test file, the API lint and bun format were run; the whole bun run test suite was not)
  • I understand every line of this diff and can explain it in review

Summary

  • parseMailQueue capped its deferral list at MAX_DEFERRALS (3) before returning it, and
    checkMailDelivery graded that capped list. A display cap was deciding the health verdict.
  • The reason that condemns a relayed box (a SASL refusal from the smarthost, or a TLS/network
    failure naming it) is often the rarest line in the queue: one message has reached the relay
    and been refused while older ones defer against ordinary receivers. Sorted by count and cut to
    three, it never reached gradeDelivery.
  • Result: a box whose relay credentials are wrong, with every relayed message failing, graded
    warn ("mail is moving late") instead of fail.
  • Fix: return every distinct reason from the parse, grade that, and apply the cap in a new
    topDeferrals when building the response. The response still carries at most three rows.
  • Two commits, two files, +78/-3 at head e610b22f (production file +11/-2, the rest tests). No
    new dependencies, no reformatting of unrelated lines. The second commit is test-only (it drops
    four table rows that passed without the fix); squash on merge is fine.

Both arms run at head e610b22f. The test file holds 47 tests: 41 unchanged, 1 pre-existing test
updated (caps the reasons it reports), 5 added as one it.each table. All 6 new or updated tests
fail on base; there are no controls.

$ # BASE arm: production file reverted to 4ad53d8a, test file kept
$ git checkout 4ad53d8a -- apps/api/src/modules/mail/mail-delivery.service.ts
$ bun x vitest run test/modules/mail/mail-delivery.service.test.ts --reporter=verbose

 × parseMailQueue > caps the reasons it reports
   → expected [ { kind: 'rejected', …(2) }, …(2) ] to have a length of 6 but got 3
 × checkMailDelivery > grades an auth refusal ranked fourth
   → expected 'warn' to be 'fail' // Object.is equality
 × checkMailDelivery > grades a TLS failure at the smarthost ranked fourth
   → expected 'warn' to be 'fail' // Object.is equality
 × checkMailDelivery > grades a connection failure at the smarthost ranked fourth
   → expected 'warn' to be 'fail' // Object.is equality
 × checkMailDelivery > grades an auth refusal ranked thirteenth
   → expected 'warn' to be 'fail' // Object.is equality
 × checkMailDelivery > grades an auth refusal queued after three one-off deferrals
   → expected 'warn' to be 'fail' // Object.is equality

 Test Files  1 failed (1)
      Tests  6 failed | 41 passed (47)

$ # HEAD arm: fix restored
$ git checkout HEAD -- apps/api/src/modules/mail/mail-delivery.service.ts
$ bun x vitest run test/modules/mail/mail-delivery.service.test.ts

 Test Files  1 passed (1)
      Tests  47 passed (47)

Decisions

The change uses the project’s existing implementation path and adds a focused regression test; broader unrelated behavior remains out of scope.

Not run: no checks beyond the staged PR evidence were run during submission.

AI assistance: this bug was found and the fix and tests were drafted with AI tooling in my workflow; the tests and checks above were executed as pasted. I'm responsible for the change and will handle review feedback.

…ree reasons

parseMailQueue capped its reason list at MAX_DEFERRALS before returning,
and checkMailDelivery graded that capped list. A relay refusal ranked
below the three most common reasons never reached gradeDelivery, so a
relayed box whose SASL credentials, TLS or egress to the smarthost were
broken graded warn instead of fail.

Return every distinct reason from the parse, grade that, and apply the
cap in topDeferrals when building the response. The response still
carries at most three rows.
The last-shown-row, other-host, empty-relay-host and direct-box rows
grade the same with the cap in the parse, so they pin nothing about
this change. Read the parse result without a non-null assertion, like
the rest of the file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants