[
    {
        "role": "user",
        "content": "when%20using%20neomutt%20%2B%20mbsync%20%2B%20gmail%2C%20i%20know%20gmail%20keeps%20track%20of%20the%20folder%20with%20a%20tag.%20where%20is%20this%20tag%20stored%20exactly%20and%20does%20neomutt%20or%20mbsync%20update%20it%20as%20the%20message%20moves%20around%20by%20mailbox%20or%20folder%3F"
    },
    {
        "role": "assistant",
        "content": "Here\u2019s a detailed explanation of how Gmail \u201ctags\u201d (labels) are stored (from Gmail\u2019s point of view), and how tools like `mbsync` + `neomutt` handle moving messages or labels. Let me break it into two parts: Gmail\u2019s internals, and then what happens in your setup (mbsync + neomutt).\n\n---\n\n## 1. How Gmail Stores Labels / Tags Internally\n\n### What are Gmail Labels\n\n- Gmail labels are metadata attached to messages. One message can have multiple labels (e.g. *Inbox*, *Work*, *To-Do*). These aren\u2019t separate physical copies of the message on the server\u2014just multiple labels referencing the same message. ([stackoverflow.com](https://stackoverflow.com/questions/4173596/gmail-imap-any-way-to-find-all-labels-folders-a-particular-message-is-in?utm_source=openai))  \n- These labels include:\n  - System labels: `INBOX`, `SENT`, `TRASH`, `SPAM`, `DRAFT`, `IMPORTANT`, etc.  \n  - Custom labels you create.  \n  - Gmail also stores all messages in a catch-all \u201carchive\u201d storage called *All Mail*.\u2217 Messages might not show in *Inbox*, but unless deleted they stay in *All Mail*. ([marcorucci.com](https://marcorucci.com/post/34416884082/backup-gmail-with-offlineimap-folders-and-labels?utm_source=openai))\n\n### Storage Location\n\n- The messages themselves (and ALL their labels) are stored on Gmail\u2019s servers.\n- Labels are stored in Gmail\u2019s internal database, not embedded in the message\u2019s raw content (like headers or body). They are not in the MIME or header of the message per se.  \n- When you access Gmail via IMAP, Gmail exposes those labels via an IMAP extension: using **X-GM-LABELS**. The `FETCH` command can request `X-GM-LABELS` to see which labels are assigned. Gmail also maps labels to IMAP folder names for browsing. ([stackoverflow.com](https://stackoverflow.com/questions/4173596/gmail-imap-any-way-to-find-all-labels-folders-a-particular-message-is-in?utm_source=openai))\n\n---\n\n## 2. What Happens with mbsync + Neomutt\n\n### mbsync / isync\n\n- `mbsync` pulls messages over IMAP (from Gmail) and stores them locally (for example in Maildir). It sees Gmail\u2019s folder/label structure via IMAP (i.e. what Gmail exposes as mailboxes). This includes Gmail\u2019s system folders and custom labels (if configured to show in IMAP). ([futurile.net](https://www.futurile.net/2025/05/18/neomutt-email-native-imap-tutorial/?utm_source=openai))\n\n- **Label change tracking**:  \n  - If a message moves from one \u201cfolder\u201d (label-as-folder) to another via IMAP operations (e.g. COPY, MOVE, DELETE), `mbsync` can reflect that locally (e.g. move the file or copy+delete).  \n  - But if a label is changed on Gmail\u2019s side without moving the message into a different folder (say adding a label \u201cWork\u201d while the message already is in \u201cInbox\u201d), that might not necessarily trigger a local fetch/redownload unless that label corresponds to an IMAP folder that you\u2019re synchronizing. If the IMAP folder is subscribed/visible, then local mailbox will see a presence of the message under that folder label. If not, changes might be invisible. \n\n- mbsync does **not** sync internal Gmail-only metadata beyond what IMAP exposes. That means labels that are not shown in IMAP will not appear or be synchronized.\n\n### Neomutt\n\n- As an IMAP client, Neomutt sees the folder structure that Gmail exposes via IMAP. It also can issue `FETCH ... X-GM-LABELS` to see what labels a message has. Neomutt has support for custom tags/labels via IMAP keywords and Gmail\u2019s labels via that extension. ([community.latenode.com](https://community.latenode.com/t/accessing-gmail-labels-through-imap-is-it-possible/13931?utm_source=openai))\n\n- When you \u201cmove\u201d a message between folders in Neomutt (e.g. save it into `[Gmail]/Work` or `All Mail`, etc.), you're actually modifying Gmail\u2019s label set via IMAP commands. For example:\n\n  1. Remove the old label by deleting from its IMAP folder.  \n  2. Add the label by copying or appending to a new IMAP folder (which corresponds to that label).  \n\n- Neomutt can use the `set record`, `trash`, etc. settings to send messages to particular \u201cfolders\u201d (labels) when you send or delete. If those folders are Gmail system folders, Gmail handles them in its label system. ([futurile.net](https://www.futurile.net/2025/05/18/neomutt-email-native-imap-tutorial/?utm_source=openai))\n\n---\n\n## 3. Putting Them Together: What Happens \u201cWhen a Message Moves Around\u201d\n\nLet\u2019s look at various scenarios (*on Gmail side via web*, *via neomutt*, *via mbsync*) and how the \u201ctag\u201d/label metadata is updated.\n\n| Action | Label stored on Gmail side? | mbsync sees the change? | neomutt sees/updates the change? |\n|---|---|---|---|\n| Add a custom label via Gmail Web | Yes, stored in Gmail DB | If the custom label is *shown in IMAP*, mbsync will see a matching IMAP folder; will fetch presence under that folder on next sync. If not, may ignore it. | neomutt will see under the folder(s) that correspond to the label. Via `FETCH X-GM-LABELS`, it may show the set of labels. |\n| Remove a label via Gmail Web | Yes | If IMAP-visible folder, mbsync will stop showing the message in that folder; message remains in \u201cAll Mail\u201d unless other labels also discard. | neomutt sees the absence when fetching labels or folder membership. |\n| Move message via neomutt (saving / copying to folder corresponding to label) | Neomutt issues IMAP commands under the hood: adds label (folder) and likely removes old label(s) | mbsync will sync those folder membership changes (move/copy/#deleted) on next sync. | neomutt itself immediately knows. |\n| \u201cLabel-only\u201d change (you don\u2019t move but you RESTORE label or add a parallel label) | Gmail updates its internal label DB; it changes X-GM-LABELS metadata. | mbsync sees it only if the corresponding label\u2019s folder is part of which folders you're syncing. If the label has no IMAP-visible folder, it may be lost. | neomutt can fetch label metadata if requested and display accordingly. |\n| Structural changes (renaming label / parent/child) | Yes in Gmail DB | mbsync may see folder rename via IMAP, but subtle edge cases may hide some subfolders or old labels. | neomutt sees folder list updated when refreshing/subscribing. |\n\n---\n\n## 4. Storage: Where *Exactly* Is the Label Data (on Gmail\u2019s Servers)?\n\n- Labels are stored *in Gmail\u2019s backend database*, not as part of each message\u2019s header. The stored message file (if ever exposed) does not include label info in the message text; labels are metadata stored separately.  \n- The IMAP interface exposes a special attribute `X-GM-LABELS` in the FETCH command so clients can request \u201cwhat labels are assigned to this message?\u201d ([stackoverflow.com](https://stackoverflow.com/questions/4173596/gmail-imap-any-way-to-find-all-labels-folders-a-particular-message-is-in?utm_source=openai))\n\n---\n\n## 5. Gotchas / Limitations You Should Know\n\n- **Show in IMAP**: In Gmail\u2019s settings under *Labels*, you can configure which labels are visible via IMAP (\u201cShow in IMAP\u201d). If a label is unchecked, clients (including mbsync and neomutt) will not see it or sync it. ([reddit.com](https://www.reddit.com/r/GMail/comments/1iuk7wv?utm_source=openai))\n\n- **mbsync doesn't sync \u201cmessage internal changes\u201d** beyond flags or folder membership. If a label that's not in IMAP is added, there may be no mechanism for mbsync to know. Also, mbsync doesn\u2019t modify message bodies or headers (Gmail stores labels separately). Some IMAP keywords (non Gmail-specific) are supported, but label metadata is handled via Gmail\u2019s own IMAP extensions. ([sourceforge.net](https://sourceforge.net/p/isync/mailman/isync-devel/thread/CAPx46aRtP1txKNT-R6q6ZPds_QB19u1DMQSY-Wqmux%3DWDjRv%3DQ%40mail.gmail.com/?utm_source=openai))\n\n- **Neomutt**: Whether it shows all labels depends on whether you ask for them (e.g. using `FETCH X-GM-LABELS`) and whether your configuration subscribes to those folders.\n\n---\n\n## In Summary\n\n- Gmail stores tags/labels entirely server-side as metadata in its database\u2014not inside the message raw content.\n- The `X-GM-LABELS` IMAP extension is how labels are exposed to IMAP clients.\n- `mbsync` and `neomutt` will synchronize/mirror those labels *only insofar as* those labels correspond to IMAP-visible folders, and only when changes happen that IMAP supports (adding/removing labels via folder membership).\n- Moves between \u201cfolders\u201d in neomutt are actually operations that change labels server-side (via IMAP), so Gmail gets updated, and mbsync will reflect them on the next sync\u2014if those labels/folders are part of the sync set.\n\n---\n\nIf you want, I can walk through **your exact config** and point out which labels are visible/dropped, so you can ensure mbsync + neomutt behaves exactly how you expect. Do you want me to check that?"
    }
]