Skip to content

fix(parser): write an email's text output in a fixed field order - #21100

Merged
yuzhichang merged 1 commit into
infiniflow:mainfrom
L4XB:fix/email-text-field-order
Oct 19, 2026
Merged

yuzhichang merged 1 commit into
infiniflow:mainfrom
L4XB:fix/email-text-field-order

Conversation

@L4XB

@L4XB L4XB commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Summary

EmailParser.parseEmail builds the text output by ranging over the parsed content map. The default email pipeline uses that output ("output_format": "text" in the email template). Go randomizes map iteration order, so the same message produced a different text on each parse. On main I parsed one plain-text .eml 50 times and got 15 distinct texts. In some of them the body came before the headers:

text:Revenue grew.             from:[email protected]
text_html:                     to:[email protected]
to:[email protected]               subject:Quarterly numbers
subject:Quarterly numbers      date:Mon, 1 Sep 2026 10:00:00 +0000
date:Mon, 1 Sep 2026 ...       text:Revenue grew.
from:[email protected]             text_html:

The chunker splits this text on \n into 512-token chunks. An unchanged message therefore gets different chunks every time it is re-parsed. In a long email the subject and sender can land after the body, in a different chunk from the opening of the message.

What changed

The text output now writes the headers first, in the order searchableEmailHeaders already uses for the JSON header item (from, to, cc, bcc, date, subject). Then come text and text_html, and after them any other key in sorted order (emailTextKeys). Which fields appear, and how each one is written, does not change. Only the order is now fixed.

Testing

TestEmailParser_EmlTextFieldOrder parses one message 20 times and requires the exact text every time. It fails on main (one run printed from, cc, subject, date, to, …). The existing text-output tests, including the three-occurrence check in TestEmailParser_EmlTextChunkText, still pass.

CGO_ENABLED=0 go test ./internal/parser/parser/ -count=1   # ok
gofmt -l internal/parser/parser/                          # nothing

go vet on the package reports only the existing context.WithDeadline warning at paddleocr.go:453.

The text output, which the default email pipeline uses, flattened the
parsed fields by ranging over the content map. Go randomizes map order, so
the same message came out differently on every parse: 15 distinct texts
over 50 parses of one plain-text email, sometimes with the body before
the headers. Chunking a different text each time changes the chunks of
an unchanged message on every re-parse.

The fields are now written headers first, in the order the JSON header
item already uses (from, to, cc, bcc, date, subject), then text and
text_html, and any other key after them in sorted order.
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c481c100-9d06-4db9-be8a-31bd696adb79
📥 Commits

Reviewing files that changed from the base of the PR and between 543e167 and eda41d1.

📒 Files selected for processing: 2
  • internal/parser/parser/email_parser.go
  • internal/parser/parser/email_parser_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📝 Walkthrough

Email text output now places standard fields in a defined order, then writes remaining fields in sorted order. A repeated parsing test checks the output sequence.

Changes: Email text field ordering

Layer / File(s) Summary
Order email text fields
internal/parser/parser/email_parser.go, internal/parser/parser/email_parser_test.go
The parser writes standard fields in a defined order and sorts any remaining keys. A repeated test checks the flattened output order.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: xugangqiang

Merge Risk: ⚪ Minimal · up to eda41

Email text output now has stable field ordering, avoiding order variation for unchanged messages. No concrete merge-blocking risk remains.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: deterministic field ordering for email text output.
Description check Passed The description includes the required Summary section and provides clear context, changed behavior, testing details, and validation results.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the fields in line
From and To come first, just fine
Cc and Date then take their place
Subject follows at a steady pace
Body text and sorted keys appear
The output order stays clear
The rabbit hops away with cheer

Comment @coderabbitai help to get the list of available commands.

@jay77721
jay77721 self-requested a review October 19, 2026 05:54
@jay77721 jay77721 added the ci Continue Integration label Oct 19, 2026
@yuzhichang
yuzhichang merged commit fbae305 into infiniflow:main Oct 19, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants