Skip to content

notify: pack Feishu digests against the serialized request body - #174

Merged
BaaRai merged 1 commit into
mainfrom
fix/feishu-digest-overflow
Nov 10, 2026
Merged

BaaRai merged 1 commit into
mainfrom
fix/feishu-digest-overflow

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 10, 2026

Copy link
Copy Markdown
Owner

What

Feishu digest packing now measures the request body as it is serialized, so a digest stays within feishuMaxCardBytes (16000).

feishuCard adds whole items while the body fits, counting:

  • each item element by its JSON encoding (wrapper and escapes included), plus its separating comma;
  • the card around the items (header, intro, platform link), encoded for the number of items kept, since the intro changes with that number;
  • what Send adds around the card (msg_type, timestamp, sign) as the new constant feishuEnvelopeBytes = 113. The new test checks this constant against the bytes on the wire.

Only notify/feishu.go and notify/pack_test.go change. The digest branch of feishuCard no longer calls packItemCount; DingTalk, WeCom and Telegram still do, and packItemCount itself is untouched.

Why

Left open in #172 ("A Feishu digest of several hundred short items can therefore still exceed 20 KB"). Every digest line is sent as its own card element, about 52 bytes of JSON around the text. The packing counted the text of each line only, against a fixed 320-byte reserve for everything else. Feishu rejects a request body over 20 KB as a whole, so such a digest would not arrive at all.

Before / after

Request body sent on the wire by feishuChannel.Send (signed), 500-item digest, measured by the new test:

Digest of 500 items main (3b6eb2f) This branch
short lines, no assets 1 message, 41846 bytes 3 messages: 15955, 15980, 10995 bytes (187 + 186 + 127 items)
short lines, one asset first message 31598 bytes 4 messages: 15910, 15912, 15911, 7270 bytes (146 + 145 + 145 + 64)
titles with <, >, &, " first message 33247 bytes 6 messages, largest 15975 bytes (99 + 98 + 98 + 98 + 98 + 9)

The largest body on this branch is 15980 bytes. The 47206-byte figure quoted for main came from a different set of lines; with the lines of this test the same case measures 41846.

Behaviour kept

  • No item is dropped. Items that do not fit stay queued for the next message, as before; the test checks that all 500 go out exactly once and in order, and that the count Send reports equals the lines in the body.
  • The first item of a digest is always sent, with its text truncated to the same budget (feishuMaxCardBytes - markdownReservedBytes). A digest whose first item alone is oversized can therefore still exceed 16000, as on main (16370 bytes in the existing TestFeishuUntruncatedItemStaysWithinBodyLimit, which asserts 20000). This change does not touch that guard.
  • Single pushes build the same card as before.

To review

  • Feishu digests now split earlier: about 100 to 190 short items per message instead of several hundred.
  • The size is counted for a signed request. Without a secret the body is 79 bytes smaller, so the cap still holds, slightly conservatively.
  • feishuEnvelopeBytes assumes a 10-digit Unix timestamp.

Verification

Run in WSL (Go 1.26.10, linux/amd64) on commit 542582e.

  • gofmt on the CR-stripped working files and on the committed blobs: clean.
  • go build ./...: pass. go vet ./...: pass.
  • go test ./notify/ -count=1 -race: ok (1.299s).
  • New test TestFeishuDigestBodiesStayWithinCardLimit (no database, three subtests): pass. The existing Feishu tests pass unchanged.
  • Mutation checks, both restored afterwards:
    • notify/feishu.go from main (plus the constant so the test compiles): the test fails in all three subtests at 41846, 31598 and 33247 bytes.
    • New loop counting the raw line text instead of the encoded element: fails at 41846, 30962 and 32462 bytes.

Not verified:

  • No message was sent to a real Feishu endpoint. The 20 KB limit comes from Feishu's documentation, not from a rejected request.
  • go test ./... was not run; the server and db tests need Postgres. Only ./notify/ was tested.
  • No end-to-end run through the delivery engine: the test replays its "send, then requeue what did not fit" loop against a local fake receiver.

Do not merge before CI is green on the head SHA.

A Feishu digest sends every item as its own card element. The packing
counted the text of each line only, not the element around it (about 52
bytes of JSON per item, plus whatever the encoding escapes) nor the rest
of the body beyond a fixed 320-byte reserve. A digest of 500 short lines
therefore left as a single request of 41846 bytes, while Feishu rejects
a request body over 20 KB as a whole.

feishuCard now adds whole items while the request body, measured as it
is serialized, stays within feishuMaxCardBytes: each item element by its
JSON encoding, the card around the items (header, intro, platform link)
by encoding it for the number of items kept, and what Send adds around
the card (msg_type, timestamp, sign) as feishuEnvelopeBytes, which the
new test pins to the bytes on the wire.

Unchanged: the first item of a digest is always kept and its text is
truncated to the same budget, single pushes are built as before, and
the other channels still pack with packItemCount.
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.

1 participant