00gmail-triage-bot

It went quiet. Nothing had crashed.

One Gmail OAuth grant expired. The workflow engine's answer to a failing node is to stop the execution — so a single expired token took the whole bot offline, and the only symptom was an inbox that stopped being mentioned.

gmail-triage-bot turns unread mail into Telegram cards you can act on in one tap, and drafts replies you approve before they exist in Gmail. The rewrite's first requirement was not a feature — it was that this particular silence can never happen again.

01the silence

Stopping is not error handling

A halted execution and a healthy idle bot look identical from the outside: no messages either way. That is what made it expensive — the failure was not loud enough to notice, and not quiet enough to be harmless.

BEFORE — A FAILED STEP ENDS THE RUN pollpollpoll invalid_grant execution over — and no alert indistinguishable from an empty inbox AFTER — A FAILED CYCLE IS JUST A CYCLE pollpollpoll caught pollpoll "re-auth needed" — to you, not to a log recovers
the same failure, twice — once fatal, once a Tuesday
02the guarantee

Every cycle is allowed to fail

One bad message, a Gmail hiccup, a Telegram timeout — each ends that cycle and nothing more. The process is not something a single malformed email gets to end.

Expired grantyou get a re-auth card, loop continues
Any cycle errorlogged, retried next cycle
Recoverya later cycle, once the token is back
Missing config keyrefuses to start, names the key

The last row is the opposite discipline on purpose. A transient failure should be survived; a misconfigured deploy should not be — running half-blind is how you get a bot that looks alive and does nothing.

03the race

The reply target that existed and didn't

Reply buttons carried a message id. Fetching by that id works for a standalone message and 404s the moment the message lives inside a thread — so "Draft reply" failed intermittently, depending on a shape nobody had thought to vary while testing.

fetch by messageId standalone message ok same message, in a thread 404 resolve threadId, retry once the draft, as asked "reply target gone" a card, never a stack trace THE BUG WAS NEVER THE 404 — IT WAS TREATING ONE SHAPE AS THE ONLY SHAPE
intermittent means you have not found the variable yet
04the draft

Nothing exists in Gmail until you say so

The model writes a reply; you read it in Telegram. Only when you accept does a real Gmail draft appear on the thread — and even then it is a draft, waiting in a client you already trust. The bot never sends mail.

Model writesa preview card in Telegram
Regenerateasks for a meaningfully different take
Accepta real Gmail draft on the thread
Sendyou, in Gmail, later

The signature block is fixed and non-negotiable, and the identity inside it — name, address, phone — is injected from config. That started as a rule about tone and turned out to be what made the repository publishable: nobody's contact details are in the source.

05the seam

One function stands between you and a vendor

Draft generation depends on a single contract: build_draft_builder(config) returns something with one generate(thread_context, prev_draft) method. Retiring the Anthropic key meant adding a second implementation and flipping the default.

The contractone factory, one method
Untouched by the swapprompt, cards, accept flow
Old providerstill selectable, not deleted
Model idsverified against the live API

That last line is not pedantry. A guessed model id fails at request time, in production, on a bot whose whole promise is that it does not go quiet.

06the inbox

A card per unread, three buttons

Every unread message in Primary becomes one Telegram card with Mark read, Trash and Draft reply. The point is not that it is faster than the Gmail app — it is that triage stops requiring you to open a place where everything else is also waiting.

Scopeunread, Primary only
Per messageone card, three actions
Destructive actionTrash, which Gmail can undo
Configone env file, fail-loud on a missing key
07structure

One loop, and everything else is a callback

There is exactly one thing on a timer. Every other path in the bot starts with you pressing a button, which is why nothing here can act on your mail while you are not looking.

GMAIL API category:primary is:unread ONE PROCESS · systemd poll loop every 120 s triage filter unread · Primary · cap 20 sqlite seen ids · drafts re-auth notice the loop keeps running telegram card read · trash · draft draft preview accept — you the model regenerate loops here GMAIL DRAFT on the thread, unsent THE ONLY WRITE TO GMAIL IS A DRAFT, AND ONLY AFTER YOU PRESS ACCEPT invalid_grant
one timer, one database, one irreversible step — and you own it
08build

100 tests, and none of them reach Gmail

Gmail, Telegram and the model are faked at the boundary, so the suite runs on a fresh clone with no OAuth and no keys — including the poll loop's failure paths, which are the parts that matter and the hardest to provoke against a live account.

Python · Gmail API · systemd1,968 lines
Telegram · cards and callbacks—
Draft providerOpenAI, Anthropic switch-back
Suite100 tests
Network calls under test0

Scope, honestly: this one runs for anybody, and it is the fiddliest of the set — Gmail wants its own OAuth client, and a consent screen left in testing mode expires refresh tokens in a week, which is a fine way to rediscover the bug this whole page is about.