Skip to content

fix(drafts): preserve Gmail thread when --reply-to receives a threadId - #14

Merged
willtmc merged 1 commit into
mainfrom
fix-4-reply-to-threading
Nov 23, 2026
Merged

willtmc merged 1 commit into
mainfrom
fix-4-reply-to-threading

Conversation

@willtmc

@willtmc willtmc commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes Gmail reply threading when --reply-to receives a thread ID (or a message whose Message-Id / References headers use non-uppercase casing).

Root causes fixed

  1. Thread IDs equal root message IDs in Gmail: Previously, createDraft and sendMessage only called users.threads.get when users.messages.get(replyToId) threw. Because a Gmail thread's ID equals its first message's ID, passing --reply-to <threadId> succeeded on messages.get and replied to the stale root message instead of resolving the thread's latest message.
  2. Case-sensitive header lookups: Message-ID and References were matched with strict === instead of case-insensitive getHeaderValue, silently dropping In-Reply-To and References whenever Gmail/MTAs returned Message-Id or references.
  3. Trailing draft messages in threads: Thread resolution previously picked messages[messages.length - 1] blindly, even if that entry had the DRAFT label. selectLatestReplyMessage now prefers the latest non-draft message (falling back to the last message only if all entries are drafts).
  4. Deduplication: Extracted shared resolveReplyTarget and resolveReplyMetadata helpers used by both createDraft and sendMessage, and avoided duplicating Message-ID when already present in References.

Testing

  • npm run check (biome check src/ --write --diagnostic-level=error && tsgo --noEmit): passed
  • npm test (npm run build && node --test test/**/*.test.js): 16/16 passed, including new unit tests in test/reply-threading.test.js covering:
    • createDraft --reply-to <threadId> where threadId === rootMessageId (with trailing DRAFT message and Message-Id/references casing)
    • createDraft --reply-to <threadId> fallback when root message is missing (messages.get throws)
    • createDraft --reply-to <messageId> targeting a specific non-root message
    • sendMessage --reply-to <threadId> resolving the latest message and headers
    • selectLatestReplyMessage skipping DRAFT messages

Closes #4

@willtmc
willtmc marked this pull request as ready for review November 23, 2026 21:05
@willtmc
willtmc merged commit 660190a into main Nov 23, 2026
@willtmc
willtmc deleted the fix-4-reply-to-threading branch November 23, 2026 21:05
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.

drafts create --reply-to <threadId> may break threading (lands in new thread)

1 participant