[client] Polls: a poll is a message, a vote replaces the voter's earlier one, only the asker closes and the close carries the final votes; the client keeps each conversation's polls

This commit is contained in:
2026-11-22 10:37:59 +01:00
parent 056a68de17
commit 65f56e306c
6 changed files with 1024 additions and 10 deletions
+123
View File
@@ -0,0 +1,123 @@
# Polls — three payloads, and a poll the client keeps (`PROTOCOL.md` §4)
Amicitia F28, step 3 of its full-chat plan (`Amicitia/.agents/features/full-chat.md` §2). Everything
here is **inside MLS**: the relay's view (§5, §8) does not change. No new route, no new column.
## 1. Why and what
*Where shall we eat — the Italian place or the Thai one?* A chat people move to lets them ask a
question with a few answers, lets everyone vote and change their vote, and shows who chose what. The
person who asked closes it, and from then on the result is the same on every device.
So this change adds:
1. `poll`, a message (it has a `seq`, so it can be replied to and reacted to, and a receipt covers
it): a question, 2–10 options, single or multiple choice;
2. `vote`, a member's choices in a poll, replacing their earlier vote; no choices takes it back;
3. `pollClose`, sent by the asker only: the poll is closed, and **these are the final votes**.
## 2. Design
```mermaid
sequenceDiagram
participant A as Anna's app
participant C as Client (Anna)
participant R as Relay
participant D as Client (Ben)
participant B as Ben's app
A->>C: compose_poll(ben, "Where?", ["Italian","Thai"], false) → seq 8
C->>R: {"kind":"poll","seq":8,"question":"Where?","options":["Italian","Thai"],"multiple":false}
R->>D: fetch
D->>B: Poll{seq 8, …}; D remembers (Anna, 8): 2 options, single choice, open
B->>D: vote(anna, (Anna, 8), [1])
D->>R: {"kind":"vote","poll":{"author":"…","seq":8},"choices":[1]}
R->>C: fetch
C->>A: Vote{poll (Anna, 8), choices [1]}
A->>C: close_poll(ben, (Anna, 8))
C->>R: {"kind":"pollClose","poll":{…},"ballots":[{"author":"<Ben>","choices":[1]}]}
R->>D: fetch
D->>B: PollClosed{poll, ballots}: the final result
```
### 2.1 The payloads
```json
{"kind":"poll","seq":8,"question":"Where?","options":["Italian","Thai"],"multiple":false}
{"kind":"vote","poll":{"author":"<22 chars>","seq":8},"choices":[1]}
{"kind":"vote","poll":{"author":"<22 chars>","seq":8},"choices":[]}
{"kind":"pollClose","poll":{"author":"<22 chars>","seq":8},"ballots":[{"author":"<22 chars>","choices":[1]}]}
```
- **`poll`**: `question` 1–300 characters, `options` 2–10 of 1–100 characters each, all different;
none blank, none with a control character. `multiple` says whether a vote may hold several choices.
It shares the sender's one `seq` counter with `text`.
- **`vote`**: `choices` are indexes into `options`, ascending, each once; with `multiple` false, at
most one. `[]` takes the vote back. A vote replaces the sender's earlier one in that poll. No `seq`.
- **`pollClose`**: only the poll's author may send it. `ballots` are the votes the author's client
had accepted when it closed, one per member who has a vote (an empty vote is left out). No `seq`.
### 2.2 Why the close carries the votes
Without them two devices can disagree for ever: Ben votes while Anna closes; Anna's client receives
the vote after its close and drops it, Ben's app counted it. With `ballots`, the result after a close
is *what the asker's device saw*, and every device shows that. A vote that crossed the close is not
counted anywhere, and Ben's app sees that his vote is not in the result.
### 2.3 What the client keeps
For each conversation, the polls in it: who asked and the `seq`, how many options, `multiple`,
whether it is closed, and each member's current choices (at most two in a 1:1 chat). This goes into
the snapshot (`polls`, absent in older snapshots, so `SNAPSHOT_VERSION` stays 1). With it the client
— not each app — decides whether a vote or a close makes sense, and the close's `ballots` come from
what it accepted, not from the app.
### 2.4 What the receiving client checks
| Received | Dropped when | Otherwise |
|:--|:--|:--|
| `poll` | a field over its limit, fewer than 2 or more than 10 options, a blank or repeated option, a control character | `Event::Poll`; remembered; its `seq` counts like a text's |
| `vote` | the poll is not one it remembers in this conversation, or is closed; a choice out of range, repeated, not ascending; two choices in a single-choice poll | `Event::Vote`; replaces the sender's choices |
| `pollClose` | the sender is not the poll's author; the poll is unknown or already closed; a ballot by someone not in the conversation, twice for one person, empty, or invalid as a vote | `Event::PollClosed` with the ballots; the poll is closed, its votes are the ballots |
Blocked contacts' polls, votes and closes are dropped, as their texts are. Sending checks the same
rules and refuses with `ClientError::BadPoll` (a malformed poll or vote, a poll that is not ours to
close) or `ClientError::PollClosed`.
### 2.5 The client's API (Amicitia pins it)
- `compose_poll(contact, question, options: &[String], multiple) -> Outgoing`; `sent_seq` names it.
- `vote(contact, poll: MessageRef, choices: &[u8]) -> Outgoing`.
- `close_poll(contact, poll: MessageRef) -> Outgoing`; only our own poll.
- `Event::Poll { contact, seq, question, options, multiple }`, `Event::Vote { contact, poll, choices }`,
`Event::PollClosed { contact, poll, ballots: Vec<Ballot> }`; `Ballot { author, choices }`.
- Constants `MIN_POLL_OPTIONS` 2, `MAX_POLL_OPTIONS` 10, `MAX_OPTION_CHARS` 100,
`MAX_QUESTION_CHARS` 300.
### 2.6 An older client
A client from F27 does not know these kinds: a `poll` (it has a `seq`) shows as *Update Amicitia to
see this message*; a `vote` and a `pollClose` (no `seq`) are dropped. That is F27's rule working.
## 3. Risks
- **Privacy:** the two members learn the question, the options and who voted for what — **there is
no anonymous poll**: MLS authenticates every sender, so an anonymous vote could not be honest. The
app says *Everyone in this chat sees who voted for what* on the poll. The relay sees three more
kinds of blob, sized like short texts; the close's size grows with the number of votes (§5's note
on sizes; padding is G41's later work).
- **Integrity:** only a member can vote (MLS), only for themselves (a vote has no author field: it is
the sender's), only the asker can close, and the close can only name members.
- **Snapshot growth:** a few dozen bytes per poll, kept while the conversation exists.
- **Events (Amicitia):** none — votes and closes are not check-ins; a poll is a message like a text.
- **Offline:** staged as every payload; the app delivers them in order (F27's `deliver_older`).
## 4. Gaps and designed solutions
| Gap | Design |
|:--|:--|
| A vote and a close cross | The close carries the ballots (§2.2) |
| The app could invent a close's result | The client fills `ballots` from what it accepted (§2.3) |
| A poll from before the client kept polls (none exist: nobody sent one) | — |
| Polls in groups (F32): more members, more ballots | `Ballot` already names its author; F32 checks membership against the roster |
| The close's size reveals roughly how many voted | Up to two ballots in 1:1; G41's padding later |
## 5. Verification
| Case | Test |
|:--|:--|
| A poll arrives with its seq, question, options and `multiple`; a receipt covers it | `client/tests/polls.rs` |
| A vote is set, replaced and taken back; both sides' own and their votes | same |
| A close carries the ballots the asker had, and the result is the same on both sides | same |
| A vote that crosses the close is not in the result anywhere | same |
| Only the asker closes; a vote or a second close after the close is refused and dropped | same + unit |
| Malformed polls, votes and closes are refused when sending and dropped when received | unit through `receive_payload` |
| A blocked contact's poll and vote are dropped | unit |
| The poll survives a snapshot and restore, closed or open, with its votes | `client/tests/polls.rs` |
| An F27 client shows a poll as Unsupported | F27's rule, its own test (`a_newer_apps_message_is_unsupported_and_counts_for_receipts`, now with `file` as the newer kind) |
| The relay's database holds no question and no option (canary; a choice is a small number, which no canary can find) | `real_relay.rs` |
| Planted bugs | the review |