Task 10: Messages, typing, read state (#10) #46

Manually merged
charles merged 0 commits from task/10-core-messages-readstate into foundation 2026-11-11 18:28:08 +00:00
Owner

Stacked on: task/8-core-store-session

What this builds

fluxer-core messages, typing and read state (Foundation plan, Task 10; spec §4).

  • messages.rs: Messages { by_channel }, ChannelMessages { items, oldest_loaded, reached_start, loading, pending }, MessageRecord { message, state: Sent | Pending | Failed }, PendingMessage { nonce, content, created_at }. Reducers for SelectChannel (first page of 50), ScrolledToOldest (page before oldest_loaded), ApiResult::Messages/Sent/Edited/Deleted, MESSAGE_CREATE/UPDATE/DELETE/DELETE_BULK, CHANNEL_DELETE, SendDraft, RetrySend, EffectFailed.
  • typing.rs: TypingState, typing_users(app, channel), incoming indicators that expire after 10 s and outgoing Api Typing at most every 8 s per channel.
  • read_state.rs: ReadStates { by_channel } from READY, MESSAGE_ACK, READ_STATE_UPDATE; unread(app, channel), mention_count(app, channel); MarkRead emits Api Ack and applies it locally.
  • selectors/messages.rs: timeline(app, channel) with day separators and grouping (rule read from the fork's MessageGroupingUtils.ts: same author, same day, at most 7 minutes, previous not a system message, current not a reply).

Review Focus 2: a MESSAGE_CREATE for a channel that is not in app.channels.by_id is ignored with a tracing::debug!. It creates no message entry, no channel, no read state, and unread stays false (message_create_unknown_channel_is_ignored).

How it was tested

crates/fluxer-core/tests/messages.rs, 25 tests through App::apply only, using the READY, MESSAGE_CREATE, MESSAGE_UPDATE, TYPING_START and messages_page fixtures: the 11 tests the plan names, plus unasked/stale pages, a failed first page, a create arriving while the first page loads, update/delete, REST-only confirmation, empty drafts, own messages not becoming unread, reconnect, channel delete and logout. Tests were written first and committed failing. cargo xtask ci is green locally.

Deviations from the plan

  1. No clock. The merged store "never reads a clock", so:
    • TypingState.by_channel maps user to the TimerId of its expiry timer instead of an Instant; the 8 s throttle is a timer too. The runtime (Task 14) must deliver CoreEvent::Timer.
    • The nonce is "the highest message id the store has seen, plus one": unique, and it sorts last in items. PendingMessage::created_at is the time of the newest message known at that moment, not wall-clock time. If a real send time is wanted for pending rows, the runtime has to supply the time in an event; that is a decision for Charles and is not blocking.
  2. Stubs of Tasks 9 and 11 got one field each (app.rs): Channels { by_id } and UiState { drafts }, with the names from those tasks' Interfaces. Nothing fills them here; the tests fill them by hand. Merging Tasks 9 and 11 will conflict in app.rs on these stubs and on selectors/mod.rs (both sides add it): keep their types, keep both pub mod lines.
    • SendDraft reads and removes the text from app.ui.drafts; it relies on Task 11 storing the draft on DraftChanged and not clearing it on SendDraft before the messages reducer runs.
    • MESSAGE_CREATE writes Channel::last_message_id in app.channels (Task 9's reducer list has no MessageCreate).
  3. Files outside the task's list: effect.rs (Effects::schedule, timer ids from the same counter as effect ids), app.rs (wiring, a new App::typing field, the stubs), lib.rs, selectors/mod.rs, Cargo.toml (time, already a workspace dependency, for Date), README.md.
  4. Additions the plan does not name:
    • A live message is stored only when the channel's first page is loaded or loading, so items stays one contiguous run; the first-page check is "loaded" rather than "no items", so an empty channel is not fetched again.
    • READY (a new session, not a resume) drops the history of open channels and fetches their first page again, because events may have been missed.
    • The user's own MESSAGE_CREATE moves the read marker (the Electron client's ackCurrentUserMessage).
    • These three domains are reduced only while logged in and cleared on logout.
    • timeline_with_offset(app, channel, UtcOffset) for local days; timeline uses UTC.

Closes #10

🤖 Generated with Claude Code

Stacked on: task/8-core-store-session ## What this builds `fluxer-core` messages, typing and read state (Foundation plan, Task 10; spec §4). - `messages.rs`: `Messages { by_channel }`, `ChannelMessages { items, oldest_loaded, reached_start, loading, pending }`, `MessageRecord { message, state: Sent | Pending | Failed }`, `PendingMessage { nonce, content, created_at }`. Reducers for `SelectChannel` (first page of 50), `ScrolledToOldest` (page before `oldest_loaded`), `ApiResult::Messages/Sent/Edited/Deleted`, `MESSAGE_CREATE/UPDATE/DELETE/DELETE_BULK`, `CHANNEL_DELETE`, `SendDraft`, `RetrySend`, `EffectFailed`. - `typing.rs`: `TypingState`, `typing_users(app, channel)`, incoming indicators that expire after 10 s and outgoing `Api Typing` at most every 8 s per channel. - `read_state.rs`: `ReadStates { by_channel }` from `READY`, `MESSAGE_ACK`, `READ_STATE_UPDATE`; `unread(app, channel)`, `mention_count(app, channel)`; `MarkRead` emits `Api Ack` and applies it locally. - `selectors/messages.rs`: `timeline(app, channel)` with day separators and grouping (rule read from the fork's `MessageGroupingUtils.ts`: same author, same day, at most 7 minutes, previous not a system message, current not a reply). Review Focus 2: a `MESSAGE_CREATE` for a channel that is not in `app.channels.by_id` is ignored with a `tracing::debug!`. It creates no message entry, no channel, no read state, and `unread` stays false (`message_create_unknown_channel_is_ignored`). ## How it was tested `crates/fluxer-core/tests/messages.rs`, 25 tests through `App::apply` only, using the `READY`, `MESSAGE_CREATE`, `MESSAGE_UPDATE`, `TYPING_START` and `messages_page` fixtures: the 11 tests the plan names, plus unasked/stale pages, a failed first page, a create arriving while the first page loads, update/delete, REST-only confirmation, empty drafts, own messages not becoming unread, reconnect, channel delete and logout. Tests were written first and committed failing. `cargo xtask ci` is green locally. ## Deviations from the plan 1. **No clock.** The merged store "never reads a clock", so: - `TypingState.by_channel` maps user to the `TimerId` of its expiry timer instead of an `Instant`; the 8 s throttle is a timer too. The runtime (Task 14) must deliver `CoreEvent::Timer`. - The nonce is "the highest message id the store has seen, plus one": unique, and it sorts last in `items`. `PendingMessage::created_at` is the time of the newest message known at that moment, not wall-clock time. If a real send time is wanted for pending rows, the runtime has to supply the time in an event; that is a decision for Charles and is not blocking. 2. **Stubs of Tasks 9 and 11 got one field each** (`app.rs`): `Channels { by_id }` and `UiState { drafts }`, with the names from those tasks' Interfaces. Nothing fills them here; the tests fill them by hand. Merging Tasks 9 and 11 will conflict in `app.rs` on these stubs and on `selectors/mod.rs` (both sides add it): keep their types, keep both `pub mod` lines. - `SendDraft` reads and removes the text from `app.ui.drafts`; it relies on Task 11 storing the draft on `DraftChanged` and not clearing it on `SendDraft` before the messages reducer runs. - `MESSAGE_CREATE` writes `Channel::last_message_id` in `app.channels` (Task 9's reducer list has no `MessageCreate`). 3. **Files outside the task's list**: `effect.rs` (`Effects::schedule`, timer ids from the same counter as effect ids), `app.rs` (wiring, a new `App::typing` field, the stubs), `lib.rs`, `selectors/mod.rs`, `Cargo.toml` (`time`, already a workspace dependency, for `Date`), `README.md`. 4. **Additions the plan does not name**: - A live message is stored only when the channel's first page is loaded or loading, so `items` stays one contiguous run; the first-page check is "loaded" rather than "no items", so an empty channel is not fetched again. - `READY` (a new session, not a resume) drops the history of open channels and fetches their first page again, because events may have been missed. - The user's own `MESSAGE_CREATE` moves the read marker (the Electron client's `ackCurrentUserMessage`). - These three domains are reduced only while logged in and cleared on logout. - `timeline_with_offset(app, channel, UtcOffset)` for local days; `timeline` uses UTC. Closes #10 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Refs #8

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
feat(core): store skeleton and session reducer
All checks were successful
ci / Windows (x64) (push) Successful in 42s
ci / Linux (x86_64) (push) Successful in 1m0s
ci / Windows (x64) (pull_request) Successful in 41s
ci / Linux (x86_64) (pull_request) Successful in 1m5s
cacedcbbf7
Refs #8

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Refs #10

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
feat(core): messages, typing and read state
All checks were successful
ci / Windows (x64) (push) Successful in 44s
ci / Windows (x64) (pull_request) Successful in 44s
ci / Linux (x86_64) (push) Successful in 1m4s
ci / Linux (x86_64) (pull_request) Successful in 1m3s
38a4f01a14
Refs #10

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Author
Owner

Independent review: Task 10 (core-messages-readstate) at 38a4f01

Scope: git diff cacedcb..38a4f01 only. Read cold; the server behaviour quoted below was checked against the fork checkout.

Checks

  1. Plan tests: all 11 named tests exist in crates/fluxer-core/tests/messages.rs and assert the plan's values (limit 50, before: oldest_loaded, short page => reached_start, 8 s throttle, 10 s expiry, nonce replace, failed + retry with the same nonce, unread from last_message_id, ack, grouping at exactly 7 min and day separators). 14 extra edge-case tests. Tests were committed failing first (223ac63).
  2. Interfaces: Messages, ChannelMessages (all five fields), PendingMessage { nonce, content, created_at }, MessageRecord, ReadStates, unread, mention_count, timeline, TimelineItem exist as written. One deviation: TypingState maps user -> TimerId, not Instant (finding 3).
  3. Invariants: no banned crates in fluxer-core (time is an existing workspace dep), no I/O, no unwrap/expect/indexing on server data in the new modules, no secrets in code or tests.
  4. Review Focus 2: message_create_unknown_channel_is_ignored covers it: no message entry, no channel, no read state, unread false, mention_count 0, no effects; the rest of an unknown channel's traffic is covered too.
  5. cargo xtask ci in the review worktree: green (screenshot step skipped, FLUXER_SCREENSHOT_TESTS not set).

Findings

  1. minor (wants a follow-up issue before merge) messages.rs send_draft: the nonce is deterministic ("highest id seen + 1"), and the server deduplicates on it. fork:fluxer_api/.../MessageOperationsHelpers.ts findExistingMessage keys a cache on message-nonce:{userId}:{nonce} for 5 minutes (MESSAGE_NONCE_TTL): a reused nonce in the same channel returns the earlier message and creates nothing; in another channel it throws UnknownMessageError. Within one session the nonces only grow, so this is safe. It is not safe across a restart or logout/login inside 5 minutes, because newest_seen restarts from 0: load channel Y (newest id G), send in X => nonce G+1; restart, load Y (still G), send in Y => nonce G+1 again => the send fails, and RetrySend reuses the nonce so it keeps failing until the TTL runs out. Two native clients of one account that are at the same newest id and send into the same channel get the worse case: the second message is dropped by the server and the client shows it as sent. Found by reading, not reproduced against a server. The Electron client uses SnowflakeUtils.fromTimestamp(Date.now()). The fix needs a clock or randomness from the runtime (for example a per-launch random base passed in CoreConfig), which touches Task 8/14 interfaces, so it is not for this PR; but it is a design question for Charles and CLAUDE.md asks for a question or follow-up issue linked from the PR, which is missing.
  2. minor messages.rs send_draft: the pending message's timestamp / PendingMessage.created_at is the newest known message time, not the send time. In a channel with no messages and nothing seen yet (first message of a new DM) the provisional id is 1, so the pending row is dated 2015-01-01 and timeline puts a day separator for that date above it until the echo arrives. Same root cause as finding 1 (no clock); the implementer disclosed the deviation. Also, a message from someone else that arrives while mine is pending sorts after it, since the provisional id is lower than any new real id.
  3. minor typing.rs: TypingState::by_channel holds TimerId where the plan's Interfaces block says Instant. Reasonable (the merged store never reads a clock, and typing_users is the read API), but consumers must not read the map's values, and Task 14 must deliver CoreEvent::Timer or indicators never expire and outgoing typing is announced once per channel only.
  4. minor app.rs: Channels { by_id } and UiState { drafts } are filled in here ahead of Tasks 9 and 11, and App gains a pub typing field. Field names and types match those tasks' Interfaces blocks, so this is a merge conflict, not a contract break. Two things for the integrator: SendDraft reads and removes app.ui.drafts[channel], so Task 11's reducer must not clear the draft before Messages::reduce runs; and crate::app::{Messages, ReadStates} no longer exist (they moved to their modules; the crate-root re-exports are unchanged).
  5. minor behaviour the plan does not name, all tested and sensible, listed so nobody is surprised: live messages are stored only for channels whose first page is loaded or loading; READY drops and refetches open channels' history; the user's own MESSAGE_CREATE advances the read marker; the three domains are reset on every event while logged out, which also discards Failed sends at logout.

Verdict: APPROVE (0 blocking, 5 minor; local ci green). Please open the follow-up/question issue for finding 1 and link it here before merge.

## Independent review: Task 10 (core-messages-readstate) at 38a4f01 Scope: `git diff cacedcb..38a4f01` only. Read cold; the server behaviour quoted below was checked against the fork checkout. **Checks** 1. Plan tests: all 11 named tests exist in `crates/fluxer-core/tests/messages.rs` and assert the plan's values (limit 50, `before: oldest_loaded`, short page => `reached_start`, 8 s throttle, 10 s expiry, nonce replace, failed + retry with the same nonce, unread from `last_message_id`, ack, grouping at exactly 7 min and day separators). 14 extra edge-case tests. Tests were committed failing first (223ac63). 2. Interfaces: `Messages`, `ChannelMessages` (all five fields), `PendingMessage { nonce, content, created_at }`, `MessageRecord`, `ReadStates`, `unread`, `mention_count`, `timeline`, `TimelineItem` exist as written. One deviation: `TypingState` maps user -> `TimerId`, not `Instant` (finding 3). 3. Invariants: no banned crates in `fluxer-core` (`time` is an existing workspace dep), no I/O, no `unwrap`/`expect`/indexing on server data in the new modules, no secrets in code or tests. 4. Review Focus 2: `message_create_unknown_channel_is_ignored` covers it: no message entry, no channel, no read state, `unread` false, `mention_count` 0, no effects; the rest of an unknown channel's traffic is covered too. 5. `cargo xtask ci` in the review worktree: **green** (screenshot step skipped, `FLUXER_SCREENSHOT_TESTS` not set). **Findings** 1. **minor (wants a follow-up issue before merge)** `messages.rs` `send_draft`: the nonce is deterministic ("highest id seen + 1"), and the server deduplicates on it. `fork:fluxer_api/.../MessageOperationsHelpers.ts findExistingMessage` keys a cache on `message-nonce:{userId}:{nonce}` for 5 minutes (`MESSAGE_NONCE_TTL`): a reused nonce in the same channel returns the earlier message and creates nothing; in another channel it throws `UnknownMessageError`. Within one session the nonces only grow, so this is safe. It is not safe across a restart or logout/login inside 5 minutes, because `newest_seen` restarts from 0: load channel Y (newest id G), send in X => nonce G+1; restart, load Y (still G), send in Y => nonce G+1 again => the send fails, and `RetrySend` reuses the nonce so it keeps failing until the TTL runs out. Two native clients of one account that are at the same newest id and send into the same channel get the worse case: the second message is dropped by the server and the client shows it as sent. Found by reading, not reproduced against a server. The Electron client uses `SnowflakeUtils.fromTimestamp(Date.now())`. The fix needs a clock or randomness from the runtime (for example a per-launch random base passed in `CoreConfig`), which touches Task 8/14 interfaces, so it is not for this PR; but it is a design question for Charles and CLAUDE.md asks for a `question` or `follow-up` issue linked from the PR, which is missing. 2. **minor** `messages.rs` `send_draft`: the pending message's `timestamp` / `PendingMessage.created_at` is the newest known message time, not the send time. In a channel with no messages and nothing seen yet (first message of a new DM) the provisional id is 1, so the pending row is dated 2015-01-01 and `timeline` puts a day separator for that date above it until the echo arrives. Same root cause as finding 1 (no clock); the implementer disclosed the deviation. Also, a message from someone else that arrives while mine is pending sorts after it, since the provisional id is lower than any new real id. 3. **minor** `typing.rs`: `TypingState::by_channel` holds `TimerId` where the plan's Interfaces block says `Instant`. Reasonable (the merged store never reads a clock, and `typing_users` is the read API), but consumers must not read the map's values, and Task 14 must deliver `CoreEvent::Timer` or indicators never expire and outgoing typing is announced once per channel only. 4. **minor** `app.rs`: `Channels { by_id }` and `UiState { drafts }` are filled in here ahead of Tasks 9 and 11, and `App` gains a pub `typing` field. Field names and types match those tasks' Interfaces blocks, so this is a merge conflict, not a contract break. Two things for the integrator: `SendDraft` reads and removes `app.ui.drafts[channel]`, so Task 11's reducer must not clear the draft before `Messages::reduce` runs; and `crate::app::{Messages, ReadStates}` no longer exist (they moved to their modules; the crate-root re-exports are unchanged). 5. **minor** behaviour the plan does not name, all tested and sensible, listed so nobody is surprised: live messages are stored only for channels whose first page is loaded or loading; `READY` drops and refetches open channels' history; the user's own `MESSAGE_CREATE` advances the read marker; the three domains are reset on every event while logged out, which also discards `Failed` sends at logout. **Verdict: APPROVE** (0 blocking, 5 minor; local ci green). Please open the follow-up/question issue for finding 1 and link it here before merge.
charles manually merged commit 24e7c9d137 into foundation 2026-11-11 18:28:08 +00:00
Sign in to join this conversation.
No description provided.