Keep the status card visible and the unread prefill stable

Two bugs visible in a device log.

"injector: anchor 0 not loaded, skipping insert" was a regression from the
previous commit. _newest_visible_message_id returns 0 when the offset reaches
past the loaded window, and the strict anchor check then refused to place the
card, so pressing Summarize appeared to do nothing at all. Before that commit
anchor 0 silently matched a date separator, which is why it went unnoticed.

A live card is the user's only feedback that the press registered, so it must
always appear: try the exact anchor, then id ordering, then the bottom of the
chat, and let reanchor() move it once the analyzed range is known. Restoring
saved summaries stays strict, since nothing corrects their position later.
_newest_visible_message_id also falls back to the oldest loaded message rather
than returning 0.

The unread button prefilled a smaller range on each press -- 1000, then 15,
then 75, with ~1100 unread. Viewing a chat calls
MessagesController.markDialogAsRead, so dialog.unread_count decays while the
chat is open and no longer describes the backlog the user wants summarized.

Resolve the count from the unread divider instead, which is what the app's own
button uses (onPageDownClicked branches on createUnreadMessageAfterId) and
which does not decay while the chat stays open, and keep the largest value
seen per chat so a later press never prefills less than an earlier one.

Both are covered by tests that fail against the previous behaviour: the strict
anchor path reports inserted=True while leaving no card in the list, and the
unread resolver reproduces exactly 1100, 15, 75.
This commit is contained in:
lyuksovannyy committed 2026-09-19 17:08:11 +02:00
1 parent b05d1a274c
commit 785618233b
4 files changed
+162 -13

No files matched your search

+20 -5
View File
@@ -316,14 +316,29 @@ class SummaryInjection:
if existing >= 0:
messages_list_now.set(existing, message_object)
else:
# A live card is a direct response to the user pressing
# Summarize, so it must become visible even when the
# provisional anchor is not loaded (a large offset reaches
# past the loaded window). Fall back to id ordering, which
# still keeps every newer message below the card, and let
# reanchor() move it to the exact spot once the analyzed
# range is known. Restoring saved summaries stays strict --
# there is no follow-up correction there.
index = find_index_for_anchor(messages_list_now, self.anchor_msg_id)
if index < 0:
# Anchor not loaded in this view: placing the card by id
# order alone would put it next to unrelated messages.
diagnostics.note(
f"injector: anchor {self.anchor_msg_id} not loaded, skipping insert"
index = find_index_for_anchor(
messages_list_now, self.anchor_msg_id, require_anchor=False
)
return
if index < 0:
# No anchor at all (nothing loaded yet, or the offset
# reached past everything). Show the card at the bottom,
# where the user is looking after pressing Summarize;
# reanchor() relocates it once the range is known.
diagnostics.note(
f"injector: anchor {self.anchor_msg_id} unresolved, "
"showing status card at the bottom"
)
index = 0
messages_list_now.add(index, message_object)
adapter = get_chat_adapter(self.chat_activity)
if adapter is not None: