Repository navigation
bridge: scheduled text messages (timer + fallback, DB-backed) - #15
Open
std-external wants to merge 1 commit into
Open
std-external wants to merge 1 commit into
std-external wants to merge 1 commit into
Conversation
std-external
force-pushed
the
feat/scheduled-text-messages
branch
from
October 18, 2026 07:31
6ce6abc to
b1b68b3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Scheduled text messages (v1)
A Telegram client can now schedule a text message, list it, send it early and delete it. The bridge stores the message and its scheduling state in the database and sends it from its own timer, so delivery no longer depends on any Telegram client staying online.
This is the first slice of the design discussed with the reporter: text only, no repeat, no media. Media scheduling keeps returning
SCHEDULED_MESSAGES_UNAVAILABLEinstead of pretending to work.Why this shape
Two tables, body separate from state.
mtproto_scheduled_messageholds what to send (text, entities, reply target, conversation snapshot);mtproto_scheduled_stateholds when and how far it got (dueAt,status,attempts,nextAttemptAt, lease columns,randomId). Editing a body never touches the state machine, and retries never rewrite the body. The existingmtproto_im_messagetable is not reused: it models messages that already exist on the platform, with a platform-id uniqueness rule, and scheduled rows would pollute history, unread counts and id allocation.Write first, arm second. Every change commits to the database before the timer is touched.
armis idempotent: a new row only re-arms when it is earlier than the currently armed one, so a burst of tasks does not thrash the timer. If the process dies between commit and arm, the fallback sweeps it up.One timer for the earliest row + a periodic sweep.
cron'sCronJobwith aDatetrigger (the owner's suggestion) replaces our own delay arithmetic; a 2-minute safety sweep covers restart, a throwing callback, suspend and clock jumps. Both paths go through the same claim, so they cannot double-send. Waits are clamped to 24 days becausesetTimeoutfires immediately past 2^31-1 ms.Claim = state machine + lease. A send first moves the row to
sendingwith a 60 s lease; a crashed sender's expired lease can be stolen on the next pass. In practice the claim is serialized per database instance:@cordisjs/plugin-databaseimplementsset()as SELECT-then-UPDATE (seeplugin-database-sqlite/lib/index.js), so two concurrent claimers inside one process could both match. The lease columns still carry the crash-recovery semantics, and crossgram runs one bridge process today.Scheduled ids live in their own counter scope (
scheduled:<session>:<conversation>), so they never consume the timestamp-bucket ids used for real messages. When the message is actually sent, the ordinary send path allocates a normal id; clients receiveupdateDeleteScheduledMessages(withsent_messages) next to the usualupdateNewMessage.What is implemented
messages.sendMessagewithschedule_date(text only) → persists the row, returnsupdateNewScheduledMessage.messages.getScheduledMessages,messages.getScheduledHistory(was an always-empty stub).messages.sendScheduledMessages(send now) andmessages.deleteScheduledMessages.messages.sendMedia/sendMultiMedianow explicitly rejectschedule_date; before, they silently sent immediately.ctx.effect; sessions are rebuilt on demand, so a due message is sent even when no Telegram client is connected.Not implemented (still
METHOD_NOT_IMPLEMENTED/SCHEDULED_MESSAGES_UNAVAILABLE): scheduling media, forwarded messages, rich messages, topic subchannels, editing a scheduled message, and repeat (schedule_repeat_periodis parsed but ignored).What a library swap would and would not remove
cronalready replaced the delay/setTimeoutglue: deletingCronJobwould put ~20 lines back.app.yml), and those queues would still need a mapping layer for Telegram scheduled ids and updates.Verification
packages/bridge/src/scheduled-messages.test.ts(11 tests): row pair creation, listing, cancel, send +sentstate, restart recovery from an expired lease, two dispatchers racing for one row, cancelled rows not sent, no re-arm for a later row, retry/park after repeated failures, the 24-day clamp, and RPC round-trips (create → list → deliver → delete; send-now).packages/bridge/src/dialogs.test.tsand the rest ofpackages/bridge/srcpass (811 tests); the only failure isvoice-worker-client.test.ts, which needs acargotoolchain that this machine does not expose to the test user.tsc -p tsconfig.test.jsonreports no errors inpackages/bridge. The four remaining errors are the knownplatform-crossgrambaseline (memory #76) and are untouched by this PR.updateNewScheduledMessage, and whether scheduled ids overlapping real id ranges is harmless (the client keeps them in a separate container).Known limitations
sentis written can deliver it twice.randomIdis persisted for a future reconciliation pass.Source: relay research task from a crossgram tester in the community group; design notes in
reports/crossgram-scheduled-messages-design.md(not part of this repository).