Skip to content

fix(imap-trigger): process new emails after empty mailbox poll - #1093

Open
roonakyadav wants to merge 2 commits into
heymrun:mainfrom
roonakyadav:fix/imap-trigger-empty-mailbox
Open

roonakyadav wants to merge 2 commits into
heymrun:mainfrom
roonakyadav:fix/imap-trigger-empty-mailbox

Conversation

@roonakyadav

Copy link
Copy Markdown
Contributor

Description

Fixes #1091.

Previously, fetch_imap_messages interpreted cursor.last_uid == 0 as a "first poll" / baseline check and returned next_cursor, []. Because baseline polls were coerced to ImapCursor() in _poll_workflow_node, an initial poll on an empty mailbox stored a cursor with version = 0. When subsequent emails arrived, cursor.last_uid == 0 caused those emails to be treated as baseline and skipped.

Additionally, when a mailbox was emptied between polls, latest_uid = uid_list[-1] if uid_list else 0 evaluated to 0, moving the stored cursor backwards from the last seen UID to 0. Subsequent emails were then skipped again.

Changes

  1. Explicit baseline handling: Pass claim.cursor (None on first poll) directly to _fetch_node_messages and fetch_imap_messages. When cursor is None, fetch_imap_messages records the baseline without downloading messages.
  2. Treat cursor with last_uid == 0 as 'nothing seen yet': An existing stored cursor with last_uid == 0 is no longer treated as a baseline, allowing new emails (uid > 0) to be fetched and executed.
  3. Prevent cursor backward movement within same UIDVALIDITY: Within the same UIDVALIDITY, set next_last_uid = max(cursor.last_uid, latest_uid), ensuring that an empty mailbox or one where older messages remain preserves the highest UID seen. A UIDVALIDITY change continues to reset the cursor.
  4. Resilience & unit tests:
    • Updated _mailbox in test_imap_trigger_cursor_postgres.py to match the baseline and cursor retention semantics.
    • Added test_a_baseline_on_an_empty_mailbox_runs_subsequent_new_emails in test_imap_trigger_cursor_postgres.py.
    • Added test_an_emptied_mailbox_keeps_its_cursor_and_runs_future_emails in test_imap_trigger_cursor_postgres.py.
    • Added test_poll_workflow_node_with_cursor_zero_executes_new_emails in test_imap_trigger_service.py.
    • Added unit test cases for empty mailbox baseline, cursor zero fetching, emptied mailbox cursor retention, and UIDVALIDITY change in test_imap_trigger_node.py.

Verification

  • ./check.sh: all checks passed (frontend lint/typecheck, backend Ruff check/format, migrations, and all 9,856 backend tests passed).
  • Frontend Vitest suite: 155 test files, 1,281 tests passed.

- Explicitly pass claim.cursor (None on first poll) to distinguish baseline from stored cursor.
- Treat stored cursor with last_uid == 0 as 'nothing seen yet' rather than a baseline poll.
- Prevent cursor from moving backwards within the same UIDVALIDITY when mailbox is emptied or older messages remain.
- Add focused postgres resilience tests for baseline on empty mailbox followed by new emails, and mailbox emptied between polls.
- Add unit tests for fetch_imap_messages cursor retention and polling edge cases.

Closes heymrun#1091
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T14:22:38.711332Z 689bb02 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ckakgun ckakgun added the next label Nov 22, 2026
@ckakgun

ckakgun commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the quick fix 🙏 Both cases from #1091 work now.

Two changes before we can merge:

  1. A node whose cursor is 0 and is then pointed at another mailbox now runs every email already in that mailbox. Mailboxes on one server can share a UIDVALIDITY (Dovecot and GreenMail derive it from the creation time), so the reset does not catch the switch, while main took a new baseline there. Please store which mailbox the cursor belongs to (credential id, host, username and folder) and take a new baseline when it changes. Rows saved before this change should adopt the current mailbox on their first poll without a reset, so an upgrade does not skip mail. Please add tests for a switch with a zero and a non-zero cursor, and a line in the Trigger Semantics section of imap-trigger-node.md.
  2. Please drop the = None default on fetch_imap_messages, so every caller says whether it wants a baseline, and mention None in its docstring.

…d require explicit cursor

- Store mailbox fingerprint (credential id, host, username, and folder) in trigger poll state key_id.
- Establish a new baseline when a trigger node is repointed to a different mailbox to prevent replaying existing messages.
- Allow legacy cursor rows saved before this change to adopt current mailbox fingerprint on their first poll without reset.
- Drop default '= None' on fetch_imap_messages cursor argument and document 'cursor=None' baseline behavior in docstring.
- Add tests for switching mailboxes with zero and non-zero cursors, and legacy cursor adoption.
- Document mailbox switch baseline semantics in imap-trigger-node.md.
@roonakyadav

Copy link
Copy Markdown
Contributor Author

Updated in 0186a7f:

  1. Mailbox Identity & Fingerprinting:

    • Stored the mailbox fingerprint (SHA-256 digest of credential_id:host:username:folder truncated to 32 characters) in TriggerPollState.key_id.
    • When a node is pointed to a different mailbox (stored_hash != current_mailbox_hash), _poll_workflow_node establishes a new baseline (cursor=None) and advances the stored cursor to the new baseline without running any existing messages.
    • Rows saved before this change (key_id in (None, "", "imap")) adopt the current mailbox fingerprint on their first poll without taking a reset, so upgrades do not skip mail.
    • Added tests in test_imap_trigger_cursor_postgres.py for switching mailboxes with both a zero cursor and a non-zero cursor, and for legacy cursor adoption.
    • Added a note to the Trigger Semantics section of imap-trigger-node.md.
  2. Explicit Baseline on fetch_imap_messages:

    • Dropped the = None default on the cursor argument of fetch_imap_messages.
    • Updated its docstring to explicitly state: Pass ``cursor=None`` to establish a baseline without downloading any messages.

All frontend checks (lint, strict typecheck, and 1,281 Vitest unit tests) and backend checks (Ruff format/lint and all 9,861 backend tests) pass.

@ckakgun

ckakgun commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks, both are in and every mailbox switch case works now 🙏

Two small things before we merge:

  1. mailbox_fingerprint keeps the case of the username and folder, so changing only INBOX to inbox on the credential takes a new baseline and skips the mail that arrived since the last poll. INBOX is case-insensitive (RFC 3501), and the credential id already tells accounts apart, so please compare the username and INBOX without case, with a test.
  2. reference/triggers.md (line 70) still describes the cursor as UIDVALIDITY plus the last UID. Please add that it also records the mailbox, and that pointing the node at another mailbox takes a new baseline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

IMAP Trigger skips the first emails after a poll finds the mailbox empty

2 participants