8000 Gmail conversations: one message per thread, and one fetch for its bodies by typedev · Pull Request #45 · hyprlab/hylki · GitHub
[go: up one dir, main page]

Skip to content

Gmail conversations: one message per thread, and one fetch for its bodies - #45

Closed
typedev wants to merge 4 commits into
hyprlab:mainfrom
typedev:fix/gmail-conversations
Closed

typedev wants to merge 4 commits into
hyprlab:mainfrom
typedev:fix/gmail-conversations

Conversation

@typedev
@typedev typedev commented Aug 25, 2026
Copy link
Copy Markdown
Contributor

Where this came from

#42 was filed because conversation grouping stopped short: after updating, every
older thread showed as loose messages and the preference looked dead. That turned
out to be the cutoff 1.13.4 stamps into state.toml — working as designed, not a
bug.

This PR does not change that decision. But to see what threading an archive
actually costs, threading_since was set to 0 by hand and used that way for a
while. For the record, on an 8300-message cache going back to 2008 it produced
356 conversations covering 1828 messages, the largest of them 49 — inside
THREAD_MEMBER_LIMIT. Whether that changes the case for a settable cutoff is
for #42, not here.

What it did do is put a lot of conversations on screen, and three defects fell
out of using them. The first two meet any Gmail user under the default cutoff
too; the third is a cost the reader pays on every conversation.

One message, three copies

Opening a conversation on a Gmail account shows every message three times, and
pays a network round trip for each copy. Both come from the same place: Gmail
exposes labels as IMAP folders, so one message is stored in INBOX, All Mail and
Important at once — three folder/UID pairs carrying a single Message-ID — while
the conversation code keys on those pairs.

Measured on that same cache: 1210 of one account's 1212 messages sit under more
than one label
, and 1131 of the other's 2562. This is the ordinary case for
Gmail, not an edge one.

A six-message thread renders as eighteen

merge_related skips a related message only when the conversation already holds
its (folder_id, uid):

if conv.iter().any(|m| m.folder_id == r.folder_id && m.uid == r.uid) {
    continue; // already in the conversation, from this folder's own index
}

The three label copies differ in both, so all three join. Replaying that rule
over the cache above reproduces it exactly — one thread of 6 distinct
Message-IDs comes back as 18 rows, 6 per label; another of 8 comes back as 24.
The thread 8000 count in the message list is right, because it groups by Message-ID;
only the reader is wrong.

Lowering the cutoff is not what exposes this — it only made it frequent enough
to count. Of the 38 messages in that cache that arrived after the date 1.13.4
stamped on its own, 16 already carry several labels, so a conversation forming in
new mail duplicates just the same. A fresh install hits it within days.

Fix: the related lookup's results are reduced to one copy per message before
merging. The copy from the folder the reader is showing wins, so the "from
folder X" label and the actions on a message refer to the mail actually on
screen.

Cached data belongs to the copy, not the message

Bodies, sender checks and attachments are keyed by (folder, uid) too. A body
read while the user was in All Mail is invisible to the INBOX copy of the same
mail, and its downloaded attachments come back empty — so the reader offers a
"Load attachments" button for files already on disk. On the same cache, 412
copies have no body of their own while a sibling copy does, and 49 are missing
attachments a sibling has; attachments land almost anywhere but INBOX.

Showing all three copies had been hiding this, since one of them usually had the
data. Collapsing them exposed it.

Fix: load_body, load_sender_check and load_attachments fall back to the
message's other copies before reporting a miss. The exact copy is still tried
first, so the common path is unchanged.

Identity is not Message-ID alone

Both fixes above match messages by Message-ID. It is written by whoever sent the
mail, and spam and some list software reuse one across genuinely different
messages — where serving one message's body for another would be worse than the
cache miss being avoided. Sender and timestamp have to agree as well.

Subject deliberately does not take part: redecode_encoded_subjects can rewrite
it under one label and not another.

This costs nothing where the match was meant to fire — the strict comparison
finds the same 412 borrowable bodies as the loose one, because no Message-ID in
that cache covers two different senders or timestamps.

One fetch per conversation, and no blinking

Opening a conversation asked for each member's body separately — one
MailRequest::LoadBody each, answered one at a time with
uid_fetch(uid, "(BODY.PEEK[])"). A ten-message thread was ten sequential waits
on the server, and only 20.8% of that cache's messages have a body stored, so an
archived thread hits the network for nearly all of it.

LoadBodies takes a whole (account, folder) group and fetches the UID set at
once, in batches of ten. Ten rather than the whole thread on purpose:
BODY.PEEK[] returns whole messages, attachments included, so up to
THREAD_MEMBER_LIMIT of them in one command can be tens of megabytes in a single
response — nothing on screen until all of it lands, and nothing to show for it if
the connection drops, which one-at-a-time fetching never risked. Ten still cuts
round trips to a fifth. Members the disk cache can answer never reach the
network, and a batch that fails reconnects before the next rather than
abandoning the rest. POP3 has no set fetch, so its arm stays the loop it was.

The blinking has the same root. render() sets webview_ready = false, dropping
a themed cover over the reader until WebKit finishes loading — right for a new
message, wrong for the re-render each arriving body queues, because the
conversation is one document rebuilt in full every time. Bodies trickling in
covered and uncovered the reader once per arrival. The cover is now raised only
when the document is being replaced by a different message.

Testing

cargo test: 176 pass. Nine are new and cover each claim above — three labels
collapsing to one message with the on-screen copy preferred, a copy of a message
already shown being dropped, messages without a Message-ID never merging, a
reused Message-ID from another sender (and from another date) matching nothing, a
body and attachments cached under one label answering for another, a
single-folder thread becoming one fetch in thread order, and a thread spanning
folders and accounts splitting per folder without losing members.

cargo clippy reports the same 19 warnings as main — no new ones.

Verified by building the branch as a Flatpak and running it against those two
accounts: the thread that showed 18 messages now shows 6, attachments appear
without asking, and the reader no longer blinks while a conversation loads.

Notes

  • Both accounts used for verification are Gmail, so the behaviour of these
    changes on other providers is reasoned from the code rather than measured.
    Where a message lives in exactly one folder the sibling lookup returns nothing
    and behaviour is unchanged; the batch fetch is plain RFC 3501 sequence-set
    syntax, and it uses BODY.PEEK[] rather than a BODYSTRUCTURE fast path, so it
    keeps clear of the iCloud parsing trouble noted in load_body.
  • The sibling lookup scans messages on a cache miss. That is nothing at a few
    thousand rows, but an index on (account_id, message_id) would be worth adding
    before a fully indexed archive of hundreds of thousands.
  • The second thread-assembly path, merge_related, still requests bodies one at
    a time; it was left alone to keep this change reviewable.

typedev and others added 4 commits August 25, 2026 03:14
Opening a conversation asked for each member's body separately: one
`MailRequest::LoadBody` per message, and the worker answers those one at a
time with `uid_fetch(uid, "(BODY.PEEK[])")`. A ten-message thread was ten
sequential waits on the server. On a real cache only 20.8% of messages have
a cached body, so an archived thread hits the network for nearly all of it.
`LoadBodies` takes the whole (account, folder) group and fetches the UID set
in one round trip; members the disk cache can answer are served without a
connection, and a fully cached conversation never reaches the network at
all. POP3 has no set fetch, so its arm is the same loop it always was.

The blinking has the same root. `render()` sets `webview_ready = false`,
which drops a themed cover over the whole reader until WebKit finishes
loading — right for a new message, wrong for the re-render each arriving
body queues, because the conversation is one document rebuilt in full every
time. Bodies trickling in therefore covered and uncovered the reader once
per arrival. The cover is now raised only when the document is being
replaced by a *different* message; an incremental re-render leaves the old
one on screen until the new load swaps in.

Two tests cover the grouping: a single-folder thread becomes one fetch in
thread order (primary first), and one spanning folders and accounts splits
per folder without dropping members.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Ty7i3o9ZdQBuqn1N7Nxza
Gmail exposes labels as IMAP folders, so one message sits in INBOX, All Mail
and Important at the same time — three folder/UID pairs carrying a single
Message-ID. `merge_related` deduped a conversation on those pairs alone, so
every copy joined it: a six-message thread rendered as eighteen, each message
three times over. This is the normal case rather than an edge one — on a real
cache 1210 of an account's 1212 messages are labelled that way.

The related lookup's results are now reduced to one copy per Message-ID
before they are merged, and the copy from the folder the reader is showing
wins, so the "from folder X" label and the actions on a message refer to the
mail actually on screen. A message carrying no Message-ID has nothing to
match on and is left alone, so two such messages stay two.

The bug predates the conversation work and was simply hard to see: bodies
arriving one per round trip re-rendered the reader on each arrival, so a
thread flickered its way through loading and the copies were lost in it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Ty7i3o9ZdQBuqn1N7Nxza
Collapsing a conversation's label copies exposed a second half of the same
problem. Bodies, sender checks and attachments are keyed by (folder, uid),
which for Gmail means per *label copy*: a body read while the user was in
All Mail is invisible to the INBOX copy of the same mail, and its downloaded
attachments come back empty — so the reader offers a "Load attachments"
button instead of the files it already has on disk. On this cache 176
messages hold a body under only some of their labels and 31 have their
attachments under only some, and attachments land almost anywhere but INBOX
(4 messages there against 14 in Paratype alone). Showing one copy per
message therefore lost data that showing all three had accidentally covered.

The three cache lookups now fall back to the message's other copies, matched
on Message-ID, before reporting a miss. The exact copy is still tried first,
so the common path is unchanged, and an empty Message-ID matches nothing
rather than everything.

`load_bodies` is also made as forgiving as `load_body` about how a response
arrives: `load_body` scans every fetch item for the body rather than trusting
the first, and the batch now does the same. An item with no body is not an
empty message, and treating it as one would have written "(empty message)"
over a real body in the cache.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Ty7i3o9ZdQBuqn1N7Nxza
Two hardenings on the conversation work, neither Gmail-specific.

Message-ID was treated as identity on its own, by both the label-copy
collapse and the cache's sibling lookup. It is written by whoever sent the
mail, and spam and some list software reuse one across genuinely different
messages — where showing one message's body for another is worse than the
cache miss the fallback exists to avoid. Sender and timestamp now have to
agree too. Subject deliberately does not: a re-decoded encoded-word can
rewrite it under one label and not another. This costs nothing where it was
meant to fire — on a real 8300-message cache the strict comparison finds the
same 412 copies that could borrow a sibling's body as the loose one did, and
49 that can borrow attachments, because no Message-ID there covers two
different senders or timestamps.

A conversation's bodies are also no longer asked for in a single command.
`BODY.PEEK[]` returns whole messages, attachments included, so up to
THREAD_MEMBER_LIMIT of them in one fetch is potentially tens of megabytes in
one response: nothing on screen until all of it arrives, and nothing to show
for it if the connection drops on the way — which one-at-a-time fetching, for
all its round trips, never risked. Ten per fetch keeps round trips at a fifth
of the old cost while the reader fills in as each batch lands, and a batch
that fails reconnects before the next rather than abandoning the rest.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Ty7i3o9ZdQBuqn1N7Nxza
hyprlab added a commit that referenced this pull request Aug 25, 2026
… request

Gmail exposes labels as IMAP folders, so one message is stored in INBOX,
All Mail and Important at once — three folder/UID pairs carrying a single
Message-ID. The conversation code keyed on those pairs, so a six-message
thread rendered as eighteen, and each copy cost its own round trip. On the
contributor's cache 1210 of one account's 1212 messages sit under more
than one label: the ordinary case for Gmail, not an edge one. Related
results are now reduced to one copy per message before merging, and the
copy from the folder on screen wins, so a message's folder label and its
actions refer to the mail actually being shown.

Bodies, sender checks and attachments were keyed by folder and UID too, so
a body read under All Mail was invisible to the INBOX copy of the same
mail and its downloaded attachments came back empty. Each now falls back
to the message's other copies, matching on Message-ID with sender and
timestamp required to agree — an id is written by whoever sent the mail,
and serving one message's body for another would be worse than the miss it
avoids.

A conversation's bodies are fetched as a UID set, in batches of ten rather
than the whole thread: BODY.PEEK[] returns whole messages, attachments
included, so a set that large can be tens of megabytes in one response
with nothing to show if the connection drops. Ten still cuts the round
trips to a fifth.

Landed from #45 with two changes. The reader's cover rule is kept as it is
here — this branch already reaches the same end by rendering a
conversation once rather than once per body, and holds the spinner until
the document has painted. And the batched fetch is applied to both places
a conversation asks for bodies, the second of which arrived after the pull
request was opened.

Co-Authored-By: Alexander Lubovenko <2742734+typedev@users.noreply.github.com>
@hyprlab
hyprlab commented Aug 25, 2026
Copy link
Copy Markdown
Owner

Landed in v1.14.0, as c9dbd3c, credited to you in the commit trailer, the README and the About window's Thanks page.

Taken as written: the label-copy collapse in the related lookup with the on-screen copy preferred; the (folder, uid) fallbacks in load_body, load_sender_check and load_attachments; the Message-ID + sender + timestamp identity rule; and LoadBodies with its ten-message batching. All nine of your tests came across and pass.

Two changes, both worth explaining.

The reader's cover rule is not yours. main had moved a long way under this branch while it was open, and the blinking was chased from the other end: a conversation now renders once per open rather than once per body, held until the lookup has answered and the bodies are in, with the spinner staying up until the document has actually painted. Your rendered_for check and that arrangement solve the same problem, so keeping both would have meant two mechanisms guarding one thing; I kept the one the surrounding code now depends on and dropped the field. Your diagnosis was right, and it is the same diagnosis — render() covering the view on a re-render that is not a new message.

The batched fetch is applied twice. A second place now asks for a conversation's bodies — merge_related, when a cross-folder lookup brings members in after the first paint — which did not exist when you opened this. It would have kept fetching one at a time.

On the cutoff: you left it to #42 and that was the right call, but your measurement made the case. 1.14.0 adds "Thread older messages too", off by default. The two things the date bound was quietly holding down are bounded by count instead — the cross-folder links, and the size of one conversation, capped at 25 messages newest-first. Worth noting your largest thread of 49 now renders its newest 25.

One thing your writeup taught me beyond the fix: WorkerEvent::Body gained a path field on both sides independently — you needed it so a body cached under one label could answer for another, I needed it so a prefetched body could not overwrite a different message sharing a UID. Identical signature, arrived at separately. Git merged it as one line.

Thank you — this was unusually well evidenced, and the numbers in the description are what made it easy to trust.

@hyprlab hyprlab closed this Aug 25, 2026
@typedev
typedev deleted the fix/gmail-conversations branch September 2, 2026 18:04
somepaulo pushed a commit to somepaulo/hylki that referenced this pull request Sep 24, 2026
… request

Gmail exposes labels as IMAP folders, so one message is stored in INBOX,
All Mail and Important at once — three folder/UID pairs carrying a single
Message-ID. The conversation code keyed on those pairs, so a six-message
thread rendered as eighteen, and each copy cost its own round trip. On the
contributor's cache 1210 of one account's 1212 messages sit under more
than one label: the ordinary case for Gmail, not an edge one. Related
results are now reduced to one copy per message before merging, and the
copy from the folder on screen wins, so a message's folder label and its
actions refer to the mail actually being shown.

Bodies, sender checks and attachments were keyed by folder and UID too, so
a body read under All Mail was invisible to the INBOX copy of the same
mail and its downloaded attachments came back empty. Each now falls back
to the message's other copies, matching on Message-ID with sender and
timestamp required to agree — an id is written by whoever sent the mail,
and serving one message's body for another would be worse than the miss it
avoids.

A conversation's bodies are fetched as a UID set, in batches of ten rather
than the whole thread: BODY.PEEK[] returns whole messages, attachments
included, so a set that large can be tens of megabytes in one response
with nothing to show if the connection drops. Ten still cuts the round
trips to a fifth.

Landed from hyprlab#45 with two changes. The reader's cover rule is kept as it is
here — this branch already reaches the same end by rendering a
conversation once rather than once per body, and holds the spinner until
the document has painted. And the batched fetch is applied to both places
a conversation asks for bodies, the second of which arrived after the pull
request was opened.

Co-Authored-By: Alexander Lubovenko <2742734+typedev@users.noreply.github.com>
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

0