atagen

macltok

public
Star0
Watch0
5a52012c9b1d7ec241e293fb63039a10884d6fdc
3
ssh://brewery@pls.lobotomise.me:33/atagen/macltok.git

Blame: docs/design-decisions.md

96fdf90aacd6atagen2026-09-08 08:24 UTC1
# macltok design decisions
96fdf90aacd6atagen2026-09-08 08:24 UTC2
31ce2453c82fatagen2026-09-24 13:07 UTC3
the contract the working tree actually implements. if the code disagrees with
31ce2453c82fatagen2026-09-24 13:07 UTC4
this, one of them is lying and it should be fixed
31ce2453c82fatagen2026-09-24 13:07 UTC5
31ce2453c82fatagen2026-09-24 13:07 UTC6
## two ways in, one way out
31ce2453c82fatagen2026-09-24 13:07 UTC7
31ce2453c82fatagen2026-09-24 13:07 UTC8
state changes are commands. `ClientHandle::send` queues one and hands back a
31ce2453c82fatagen2026-09-24 13:07 UTC9
`Pending`, `execute` queues and waits, and there's an awaited convenience method
31ce2453c82fatagen2026-09-24 13:07 UTC10
for every command. each command answers exactly once with
31ce2453c82fatagen2026-09-24 13:07 UTC11
`Result<CommandOutput, Error>`. drop the `Pending` if you don't care, the
31ce2453c82fatagen2026-09-24 13:07 UTC12
command still runs
31ce2453c82fatagen2026-09-24 13:07 UTC13
31ce2453c82fatagen2026-09-24 13:07 UTC14
pure reads skip the engine and ask the sdk directly through the handle. rooms,
31ce2453c82fatagen2026-09-24 13:07 UTC15
members, power levels, account data, devices, push notification resolution,
31ce2453c82fatagen2026-09-24 13:07 UTC16
and media bytes all live here
31ce2453c82fatagen2026-09-24 13:07 UTC17
31ce2453c82fatagen2026-09-24 13:07 UTC18
the event stream carries state and only state. timelines, history, typing,
31ce2453c82fatagen2026-09-24 13:07 UTC19
rooms, connection, encryption, verification, upload progress, session. no
31ce2453c82fatagen2026-09-24 13:07 UTC20
command results ride on it. drain it from its own task, it's bounded and the
31ce2453c82fatagen2026-09-24 13:07 UTC21
engine waits for you
31ce2453c82fatagen2026-09-24 13:07 UTC22
31ce2453c82fatagen2026-09-24 13:07 UTC23
one error type for everything, `Error { kind, message, challenge }`. match on
31ce2453c82fatagen2026-09-24 13:07 UTC24
`kind`. `Interrupted` means the engine stopped, the room closed, or the task
31ce2453c82fatagen2026-09-24 13:07 UTC25
doing the work died before it could answer. `challenge` turns up with
31ce2453c82fatagen2026-09-24 13:07 UTC26
`InteractiveAuthRequired`
31ce2453c82fatagen2026-09-24 13:07 UTC27
31ce2453c82fatagen2026-09-24 13:07 UTC28
## engine
31ce2453c82fatagen2026-09-24 13:07 UTC29
31ce2453c82fatagen2026-09-24 13:07 UTC30
the run loop routes and does nothing slow. every task it spawns is registered
31ce2453c82fatagen2026-09-24 13:07 UTC31
with a context. a command task that panics costs its caller an `Interrupted`
31ce2453c82fatagen2026-09-24 13:07 UTC32
and gets logged. a watcher or a room actor that panics takes the engine down,
31ce2453c82fatagen2026-09-24 13:07 UTC33
emitting `Failed` then `Stopped`
31ce2453c82fatagen2026-09-24 13:07 UTC34
31ce2453c82fatagen2026-09-24 13:07 UTC35
sync restarts itself with backoff after an error or an unexpected stop, and
5a52012c9b1datagen2026-09-24 23:45 UTC36
`ConnectionChanged` shows `Offline` meanwhile. a run that drops again inside
5a52012c9b1datagen2026-09-24 23:45 UTC37
thirty seconds counts as a failure too, or a server that answers `/versions`
5a52012c9b1datagen2026-09-24 23:45 UTC38
but refuses sync would bounce us between online and offline forever. every return to `Online` re-arms
31ce2453c82fatagen2026-09-24 13:07 UTC39
the send queues, since the sdk parks a room's queue on any send failure and
31ce2453c82fatagen2026-09-24 13:07 UTC40
never unparks it. a recoverable failure while online gets the room another go
31ce2453c82fatagen2026-09-24 13:07 UTC41
after a few seconds
31ce2453c82fatagen2026-09-24 13:07 UTC42
31ce2453c82fatagen2026-09-24 13:07 UTC43
logout and a rejected token both stop the engine. `Shutdown` answers once
5a52012c9b1datagen2026-09-24 23:45 UTC44
`Stopped` has gone out, every one of them if several were queued
5a52012c9b1datagen2026-09-24 23:45 UTC45
5a52012c9b1datagen2026-09-24 23:45 UTC46
`Engine::run` hands back a boxed future. the real one nests deep enough that
5a52012c9b1datagen2026-09-24 23:45 UTC47
every crate spawning it would trip the compiler's query depth
31ce2453c82fatagen2026-09-24 13:07 UTC48
31ce2453c82fatagen2026-09-24 13:07 UTC49
## rooms
31ce2453c82fatagen2026-09-24 13:07 UTC50
31ce2453c82fatagen2026-09-24 13:07 UTC51
one actor per open room. the engine holds a slot per room, `Active` with a
31ce2453c82fatagen2026-09-24 13:07 UTC52
sender or `Closing` with a fifo of opens and closes that arrived while it wound
31ce2453c82fatagen2026-09-24 13:07 UTC53
down. those replay in order once the actor exits. a room you leave, or get
5a52012c9b1datagen2026-09-24 23:45 UTC54
kicked or banned from, closes itself. it tells the engine first, and anything
5a52012c9b1datagen2026-09-24 23:45 UTC55
already queued to it comes back to be answered or replayed, so an open racing a
5a52012c9b1datagen2026-09-24 23:45 UTC56
leave still gets a fresh actor
31ce2453c82fatagen2026-09-24 13:07 UTC57
31ce2453c82fatagen2026-09-24 13:07 UTC58
opening sends `TimelineReset` on the stream before it answers. opening an
31ce2453c82fatagen2026-09-24 13:07 UTC59
already open room sends a fresh reset, so a second frontend gets a snapshot
31ce2453c82fatagen2026-09-24 13:07 UTC60
31ce2453c82fatagen2026-09-24 13:07 UTC61
sends, edits and replies run on the actor in order, so they reach the send
31ce2453c82fatagen2026-09-24 13:07 UTC62
queue in the order they were asked for. redactions, reactions and read
31ce2453c82fatagen2026-09-24 13:07 UTC63
receipts are network calls, so they run beside the actor and a slow one
31ce2453c82fatagen2026-09-24 13:07 UTC64
doesn't queue the room behind it. closing lets them finish rather than lie
31ce2453c82fatagen2026-09-24 13:07 UTC65
31ce2453c82fatagen2026-09-24 13:07 UTC66
threads belong to their room's actor. commands for a thread that's still
31ce2453c82fatagen2026-09-24 13:07 UTC67
opening wait behind the open, up to the actor's queue capacity
31ce2453c82fatagen2026-09-24 13:07 UTC68
31ce2453c82fatagen2026-09-24 13:07 UTC69
pagination runs one request at a time per timeline and emits `HistoryChanged`
31ce2453c82fatagen2026-09-24 13:07 UTC70
around each. the sdk remembers where history starts, we don't, because a gappy
31ce2453c82fatagen2026-09-24 13:07 UTC71
sync can unload it again
31ce2453c82fatagen2026-09-24 13:07 UTC72
5a52012c9b1datagen2026-09-24 23:45 UTC73
the actor's watchers are supervised, and a panic in any of them panics the
5a52012c9b1datagen2026-09-24 23:45 UTC74
actor. its command work isn't, a panicking redaction just answers `Interrupted`
5a52012c9b1datagen2026-09-24 23:45 UTC75
and gets logged
31ce2453c82fatagen2026-09-24 13:07 UTC76
31ce2453c82fatagen2026-09-24 13:07 UTC77
## timelines
31ce2453c82fatagen2026-09-24 13:07 UTC78
31ce2453c82fatagen2026-09-24 13:07 UTC79
`TimelineDiff::try_apply` refuses a diff whose indices don't fit your snapshot
31ce2453c82fatagen2026-09-24 13:07 UTC80
and leaves the snapshot alone. stop applying, `resync`, and carry on from the
31ce2453c82fatagen2026-09-24 13:07 UTC81
next `TimelineReset`. resync kills the old watcher before the new reset goes
31ce2453c82fatagen2026-09-24 13:07 UTC82
out, so no stale diff can land after it
31ce2453c82fatagen2026-09-24 13:07 UTC83
31ce2453c82fatagen2026-09-24 13:07 UTC84
`TimelineId` tells a room from a thread by its root event
31ce2453c82fatagen2026-09-24 13:07 UTC85
960ac0721079atagen2026-09-25 00:24 UTC86
every item is an event, a day divider, the read marker or the start. an event
960ac0721079atagen2026-09-25 00:24 UTC87
carries its sender with avatar and name ambiguity, and a `TimelineContent`
960ac0721079atagen2026-09-25 00:24 UTC88
saying what it actually is. messages keep their msgtype, membership and profile
960ac0721079atagen2026-09-25 00:24 UTC89
changes and the common room state changes get their own variants, and anything
960ac0721079atagen2026-09-25 00:24 UTC90
else is `OtherState` or `Other` with its type. nothing is flattened into english
960ac0721079atagen2026-09-25 00:24 UTC91
in a body field
960ac0721079atagen2026-09-25 00:24 UTC92
5a52012c9b1datagen2026-09-24 23:45 UTC93
thread receipts for a thread that isn't open go straight to the room, so
5a52012c9b1datagen2026-09-24 23:45 UTC94
marking a thread read works the same whether its room is on screen or not
5a52012c9b1datagen2026-09-24 23:45 UTC95
960ac0721079atagen2026-09-25 00:24 UTC96
replies carry the replied-to event once it's loaded. the watcher asks the sdk
960ac0721079atagen2026-09-25 00:24 UTC97
for any it hasn't got, once per event, since the sdk won't on its own
960ac0721079atagen2026-09-25 00:24 UTC98
960ac0721079atagen2026-09-25 00:24 UTC99
edit, redact and react take an `EventRef`, so your own message can be edited or
960ac0721079atagen2026-09-25 00:24 UTC100
reacted to before the server has echoed it back. redacting an unsent one just
960ac0721079atagen2026-09-25 00:24 UTC101
cancels it
960ac0721079atagen2026-09-25 00:24 UTC102
31ce2453c82fatagen2026-09-24 13:07 UTC103
## mutations
31ce2453c82fatagen2026-09-24 13:07 UTC104
31ce2453c82fatagen2026-09-24 13:07 UTC105
membership, moderation, power levels, room creation and account data writes go
31ce2453c82fatagen2026-09-24 13:07 UTC106
through one keyed fifo scheduler. same room, same alias, same dm partner or
31ce2453c82fatagen2026-09-24 13:07 UTC107
same account data type run in order, everything else runs at once.
31ce2453c82fatagen2026-09-24 13:07 UTC108
`create_direct` hands back the existing dm when there is one
31ce2453c82fatagen2026-09-24 13:07 UTC109
31ce2453c82fatagen2026-09-24 13:07 UTC110
notifications and encryption each get one serial worker. the encryption worker
31ce2453c82fatagen2026-09-24 13:07 UTC111
owns the verification controller outright
31ce2453c82fatagen2026-09-24 13:07 UTC112
31ce2453c82fatagen2026-09-24 13:07 UTC113
power level changes build one state event from the local mapping, since the
31ce2453c82fatagen2026-09-24 13:07 UTC114
sdk's own patcher is private
31ce2453c82fatagen2026-09-24 13:07 UTC115
31ce2453c82fatagen2026-09-24 13:07 UTC116
## media
31ce2453c82fatagen2026-09-24 13:07 UTC117
31ce2453c82fatagen2026-09-24 13:07 UTC118
media is a read. `ClientHandle::media` takes a `MediaId` a retained timeline
31ce2453c82fatagen2026-09-24 13:07 UTC119
handed you, `mxc_media` takes any plain mxc uri. both share one bounded
5a52012c9b1datagen2026-09-24 23:45 UTC120
download path with the byte cap the sdk's own download lacks, an hour's total
5a52012c9b1datagen2026-09-24 23:45 UTC121
deadline instead of the sdk client's thirty seconds, and a minute's patience
5a52012c9b1datagen2026-09-24 23:45 UTC122
for a stalled stream. concurrent asks for the same bytes share one flight.
5a52012c9b1datagen2026-09-24 23:45 UTC123
a thumbnail the server won't make falls back to the file itself.
5a52012c9b1datagen2026-09-24 23:45 UTC124
`MediaId` parses from a string, since a web client has to round trip it
31ce2453c82fatagen2026-09-24 13:07 UTC125
31ce2453c82fatagen2026-09-24 13:07 UTC126
the cache holds plaintext, keyed like the sdk keys it, so the sdk and macltok
5a52012c9b1datagen2026-09-24 23:45 UTC127
can read each other's entries. that's how your own encrypted uploads come back.
5a52012c9b1datagen2026-09-24 23:45 UTC128
the sdk keys plain and encrypted sources alike, so a plain event pointing at
5a52012c9b1datagen2026-09-24 23:45 UTC129
someone's encrypted file can leave ciphertext in the cache. an encrypted read
5a52012c9b1datagen2026-09-24 23:45 UTC130
checks the cached bytes against the file's hash and decrypts them if they match
31ce2453c82fatagen2026-09-24 13:07 UTC131
31ce2453c82fatagen2026-09-24 13:07 UTC132
media ids are registered as timelines render and forgotten when their timeline
31ce2453c82fatagen2026-09-24 13:07 UTC133
closes, least recently seen first once the cap is hit
31ce2453c82fatagen2026-09-24 13:07 UTC134
5a52012c9b1datagen2026-09-24 23:45 UTC135
uploads are commands that answer once the send queue has them. progress only
5a52012c9b1datagen2026-09-24 23:45 UTC136
goes out on a room's own send queue channel, so each room that uploads gets a
5a52012c9b1datagen2026-09-24 23:45 UTC137
forwarder, subscribed before the upload is queued and restarted for uploads the
5a52012c9b1datagen2026-09-24 23:45 UTC138
sdk resumes from last time. failures do reach the client-wide channel, so one
5a52012c9b1datagen2026-09-24 23:45 UTC139
watcher there reports them
31ce2453c82fatagen2026-09-24 13:07 UTC140
31ce2453c82fatagen2026-09-24 13:07 UTC141
## sessions and secrets
31ce2453c82fatagen2026-09-24 13:07 UTC142
31ce2453c82fatagen2026-09-24 13:07 UTC143
`Session::login_password`, `begin_oauth` and `restore` share one client builder
31ce2453c82fatagen2026-09-24 13:07 UTC144
with encrypted sqlite, refresh tokens and threads. secret-bearing inputs don't
31ce2453c82fatagen2026-09-24 13:07 UTC145
derive `Clone`
31ce2453c82fatagen2026-09-24 13:07 UTC146
31ce2453c82fatagen2026-09-24 13:07 UTC147
the caller owns the saved record. save `Session::record()` after login and
31ce2453c82fatagen2026-09-24 13:07 UTC148
replace it atomically with `ClientHandle::session_record()` on every
31ce2453c82fatagen2026-09-24 13:07 UTC149
`SessionTokensChanged`. logout and a hard rejection kill the record and the
31ce2453c82fatagen2026-09-24 13:07 UTC150
store. a soft logout keeps the store for a same-device login, or the e2ee keys
31ce2453c82fatagen2026-09-24 13:07 UTC151
go with it. a bare `Stopped` kills nothing. one store directory per login
31ce2453c82fatagen2026-09-24 13:07 UTC152
960ac0721079atagen2026-09-25 00:24 UTC153
## room lists, spaces and profiles
960ac0721079atagen2026-09-25 00:24 UTC154
960ac0721079atagen2026-09-25 00:24 UTC155
`RoomSummary` has what a room list row needs without a second query. topic,
960ac0721079atagen2026-09-25 00:24 UTC156
alias, member counts, heroes for nameless rooms, the inviter, where an upgraded
960ac0721079atagen2026-09-25 00:24 UTC157
room went, a latest event preview, the joined spaces listing it, and for a
960ac0721079atagen2026-09-25 00:24 UTC158
space the children it lists
960ac0721079atagen2026-09-25 00:24 UTC159
960ac0721079atagen2026-09-25 00:24 UTC160
parents come from one lazily built `SpaceService` shared by every handle.
960ac0721079atagen2026-09-25 00:24 UTC161
children come straight from the space's local state, joined or not.
960ac0721079atagen2026-09-25 00:24 UTC162
`space_hierarchy` pages through the server's view for browsing rooms you
960ac0721079atagen2026-09-25 00:24 UTC163
haven't joined, and holds its place between pages
960ac0721079atagen2026-09-25 00:24 UTC164
960ac0721079atagen2026-09-25 00:24 UTC165
`own_profile` and `user_profile` read profiles. name and avatar changes are
960ac0721079atagen2026-09-25 00:24 UTC166
commands, and uploading an avatar gets the same byte cap as any upload
960ac0721079atagen2026-09-25 00:24 UTC167
31ce2453c82fatagen2026-09-24 13:07 UTC168
## notifications and room data
31ce2453c82fatagen2026-09-24 13:07 UTC169
31ce2453c82fatagen2026-09-24 13:07 UTC170
the notification client is built lazily and shares the engine's sync service.
31ce2453c82fatagen2026-09-24 13:07 UTC171
sdk ui types stay behind the boundary
31ce2453c82fatagen2026-09-24 13:07 UTC172
31ce2453c82fatagen2026-09-24 13:07 UTC173
`rooms` normalises on demand with bounded concurrency and sorts by latest event,
31ce2453c82fatagen2026-09-24 13:07 UTC174
then name, then id. `RoomsChanged` is an invalidation, coalesced so one sync
31ce2453c82fatagen2026-09-24 13:07 UTC175
touching fifty rooms is one event
31ce2453c82fatagen2026-09-24 13:07 UTC176
31ce2453c82fatagen2026-09-24 13:07 UTC177
## verification
31ce2453c82fatagen2026-09-24 13:07 UTC178
5a52012c9b1datagen2026-09-24 23:45 UTC179
`Accept`, `Confirm`, `Reject` and `Cancel`. a request another device sends
5a52012c9b1datagen2026-09-24 23:45 UTC180
shows up as `Incoming` and waits for `Accept`, or `Cancel` to turn it down. one
5a52012c9b1datagen2026-09-24 23:45 UTC181
is only taken up while nothing else is in flight. reject tells the other side
5a52012c9b1datagen2026-09-24 23:45 UTC182
the emoji didn't match. the watchers subscribe before they look at the current
5a52012c9b1datagen2026-09-24 23:45 UTC183
state, since the sdk only reports what changes after you ask. the watcher and
5a52012c9b1datagen2026-09-24 23:45 UTC184
the action paths claim the terminal event, so a racing
31ce2453c82fatagen2026-09-24 13:07 UTC185
cancel can't turn `Done` into `Cancelled`
31ce2453c82fatagen2026-09-24 13:07 UTC186
31ce2453c82fatagen2026-09-24 13:07 UTC187
## public model
31ce2453c82fatagen2026-09-24 13:07 UTC188
31ce2453c82fatagen2026-09-24 13:07 UTC189
owned ruma ids and typed data wherever there's a stable shape, `Raw` where
31ce2453c82fatagen2026-09-24 13:07 UTC190
arbitrary content has to survive. anything expected to grow is
31ce2453c82fatagen2026-09-24 13:07 UTC191
`#[non_exhaustive]`. `Engine::from_client` stays public for people who built
31ce2453c82fatagen2026-09-24 13:07 UTC192
their own client