Skip to content

fix(contextchat): use a consistent item id for indexed messages - #13443

Draft
kesselb wants to merge 1 commit into
mainfrom
fix/context-chat-item-id
Draft

fix(contextchat): use a consistent item id for indexed messages#13443
kesselb wants to merge 1 commit into
mainfrom
fix/context-chat-item-id

Conversation

@kesselb

@kesselb kesselb commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Content was indexed as "{mailboxId}:{messageId}" but deleted by bare IMAP uid, so deleting a message never removed it from the knowledge base. Key both sides off account, mailbox and uid via a shared helper.

  • Todo: Check with integration how to clear the existing data

馃 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

@kesselb

kesselb commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

/backport to stable5.10

Content was indexed as "{mailboxId}:{messageId}" but deleted by bare
IMAP uid, so deleting a message never removed it from the knowledge
base. Key both sides off account, mailbox and uid via a shared helper.

Assisted-by: ClaudeCode:claude-opus-5
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>

# Conflicts:
#	lib/ContextChat/ContextChatProvider.php
@kesselb
kesselb force-pushed the fix/context-chat-item-id branch from d190fbe to 7fadd20 Compare August 9, 2026 15:22
@ChristophWurst

Copy link
Copy Markdown
Member

/backport to stable5.11

@edward-ly
edward-ly requested a review from marcelklehr August 10, 2026 20:08

@edward-ly edward-ly left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also roping in @marcelklehr for additional insight. I'll test this later if I have time, but it looks good to me so far. Do we need to do some sort of migration as well (e.g. updating the IDs of existing items, or deleting and re-importing all messages again)?

@kesselb

kesselb commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Do we need to do some sort of migration as well (e.g. updating the IDs of existing items, or deleting and re-importing all messages again)?

@kyteinsky and I did some brainstorming about it (in helpdesk). It would be indeed good to reset the index. The best option for now seems to change the provider id. My first idea was to trigger deleteProvider with a migration, but Anupam explain it runs async and hence we cannot really know when it's done. By using a different provider id deleting and re-importing can run in parallel.

@kesselb

kesselb commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Todo: Check with integration how to clear the existing data

Follow up

Comment on lines +124 to +125
} catch (\Throwable) {
// Context chat fails the whole answer if this throws

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for pointing this out, fixed in nextcloud/context_chat#266

Comment on lines +119 to +123
// Context chat calls this when it renders the sources of an answer, so the
// message id is looked up on demand rather than baked into the item id.
try {
$mailbox = $this->mailboxMapper->findById((int)$mailboxId);
$messageId = $this->messageMapper->getIdForUid($mailbox, (int)$uid);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getItemUrl is called for every item in the sources list when the output is rendered, true
but without this change, it was still not looked up until the link was clicked by the user. Here, every link would be checked even when not clicked.

not sure what's the difference between uid and id but the with id approach, the message was only checked when the user clicked on it and the webpage from 'mail.page.thread' opened.

I'm not familiar with the mail's code so sorry if the point does not make much sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, that's a valid concern. The previous version was a cheap call.

id = oc_mail_messages.id
uid = identifier on the imap server, unique per mailbox, also stored as oc_mail_messages.uid

Better would be the id, but some of the events (e.g. MessageDeletedEvent) does not carry it.

But that might change very soon, and hence I will move this PR back to draft for now and continue once the JMAP changes are in. At best we have id everywhere, otherwise I will add a new entrypoint that accepts mailboxId/uid doing the lookup internally.

@kesselb
kesselb marked this pull request as draft August 13, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants