Task 8: fluxer-core store skeleton and session domain (#8) #39

Merged
charles merged 2 commits from task/8-core-store-session into foundation 2026-11-11 18:16:13 +00:00
Owner

What this builds

The fluxer-core store skeleton and the session domain (Foundation plan Task 8, spec §4).

  • App / CoreConfig / App::apply(CoreEvent) -> Vec<Effect>; the other domains (Guilds, Channels, Messages, PresenceState, ReadStates, UiState) are empty structs until Tasks 9-11.
  • CoreEvent, ApiResult, UserAction, SettingsTab, PersistResult, GatewayState (event.rs); Effect, ApiRequest, PersistOp, Notification (effect.rs); EffectId, TimerId (ids.rs); CoreError, AuthError (error.rs).
  • Session / Auth reducer: SubmitLogin -> Discover (skipped once endpoints are known) -> Login -> Persist(SaveToken) + ConnectGateway; MFA -> AwaitingTotp -> LoginTotp; a 401 on any call while logged in, or GatewayState::AuthFailed, -> LoggedOut { SessionExpired } + DisconnectGateway + Persist(ClearToken); Logout; READY / USER_UPDATE / USER_SETTINGS_UPDATE / Me refresh the user and settings.
  • UserAction::OpenUrl emits Effect::OpenUrl for http/https links only.

How it was tested

crates/fluxer-core/tests/session.rs (16 tests, written first and committed failing): the five the plan names plus second-login-skips-discovery, SetInstance, wrong TOTP code, failure-to-AuthError mapping, discovery failure, unusable gateway URL from the instance, gateway AuthFailed, non-401 failures while logged in, logout, READY replacing the user, stale login answer after cancel. Two unit tests for the OpenUrl scheme filter. cargo xtask ci is green locally (Linux).

Deviations from the plan

  • CoreError instead of ApiError. The plan maps AuthError "from ApiError", but fluxer-core may not depend on fluxer-api (reqwest). CoreError mirrors fluxer_api::ApiError variant for variant (Transport -> Network(String), Decode(String), plus Other(String)); the runtime (Task 14) converts. The same applies to GatewayState, redefined here with the variants of fluxer_gateway::GatewayState.
  • ApiResult::Sent { message: Box<Message> } and ApiResult::Edited(Box<Message>) are boxed (clippy large_enum_variant), as the merged GatewayEvent variants are. Auth::LoggedIn keeps the plan's unboxed fields with an #[expect] and a reason.
  • PersistOp has only SaveToken / ClearToken, PersistResult only Ok, Notification is a minimal struct, EditMessage / DeleteMessage fields are my reading of the plan's {..} - Tasks 10, 12 and 23 extend them.
  • Additions the plan does not list: Session carries instance, endpoints, gateway, user_settings, totp_error and request_in_flight(); right after login Auth::LoggedIn.user is built from the login answer's partial user (no e-mail) until READY replaces it; wrong credentials are recognised as 401 or HTTP 400 on the login calls.
  • REST answers carry no effect id (per the plan's ApiResult), so they are matched by state; an answer arriving after the user cancelled is dropped.
  • The test commit's READY test parsed the fixture as a bare payload; the fixture is a full frame, so the implementation commit fixes the test to read its d.

Closes #8

🤖 Generated with Claude Code

## What this builds The `fluxer-core` store skeleton and the session domain (Foundation plan Task 8, spec §4). - `App` / `CoreConfig` / `App::apply(CoreEvent) -> Vec<Effect>`; the other domains (`Guilds`, `Channels`, `Messages`, `PresenceState`, `ReadStates`, `UiState`) are empty structs until Tasks 9-11. - `CoreEvent`, `ApiResult`, `UserAction`, `SettingsTab`, `PersistResult`, `GatewayState` (`event.rs`); `Effect`, `ApiRequest`, `PersistOp`, `Notification` (`effect.rs`); `EffectId`, `TimerId` (`ids.rs`); `CoreError`, `AuthError` (`error.rs`). - `Session` / `Auth` reducer: `SubmitLogin` -> `Discover` (skipped once endpoints are known) -> `Login` -> `Persist(SaveToken)` + `ConnectGateway`; MFA -> `AwaitingTotp` -> `LoginTotp`; a 401 on any call while logged in, or `GatewayState::AuthFailed`, -> `LoggedOut { SessionExpired }` + `DisconnectGateway` + `Persist(ClearToken)`; `Logout`; `READY` / `USER_UPDATE` / `USER_SETTINGS_UPDATE` / `Me` refresh the user and settings. - `UserAction::OpenUrl` emits `Effect::OpenUrl` for `http`/`https` links only. ## How it was tested `crates/fluxer-core/tests/session.rs` (16 tests, written first and committed failing): the five the plan names plus second-login-skips-discovery, `SetInstance`, wrong TOTP code, failure-to-`AuthError` mapping, discovery failure, unusable gateway URL from the instance, gateway `AuthFailed`, non-401 failures while logged in, logout, `READY` replacing the user, stale login answer after cancel. Two unit tests for the `OpenUrl` scheme filter. `cargo xtask ci` is green locally (Linux). ## Deviations from the plan - **`CoreError` instead of `ApiError`.** The plan maps `AuthError` "from `ApiError`", but `fluxer-core` may not depend on `fluxer-api` (reqwest). `CoreError` mirrors `fluxer_api::ApiError` variant for variant (`Transport` -> `Network(String)`, `Decode(String)`, plus `Other(String)`); the runtime (Task 14) converts. The same applies to `GatewayState`, redefined here with the variants of `fluxer_gateway::GatewayState`. - **`ApiResult::Sent { message: Box<Message> }` and `ApiResult::Edited(Box<Message>)`** are boxed (clippy `large_enum_variant`), as the merged `GatewayEvent` variants are. `Auth::LoggedIn` keeps the plan's unboxed fields with an `#[expect]` and a reason. - **`PersistOp` has only `SaveToken` / `ClearToken`, `PersistResult` only `Ok`, `Notification` is a minimal struct, `EditMessage` / `DeleteMessage` fields are my reading of the plan's `{..}`** - Tasks 10, 12 and 23 extend them. - **Additions the plan does not list:** `Session` carries `instance`, `endpoints`, `gateway`, `user_settings`, `totp_error` and `request_in_flight()`; right after login `Auth::LoggedIn.user` is built from the login answer's partial user (no e-mail) until `READY` replaces it; wrong credentials are recognised as 401 or HTTP 400 on the login calls. - REST answers carry no effect id (per the plan's `ApiResult`), so they are matched by state; an answer arriving after the user cancelled is dropped. - The test commit's `READY` test parsed the fixture as a bare payload; the fixture is a full frame, so the implementation commit fixes the test to read its `d`. Closes #8 🤖 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>
Author
Owner

Independent review — Task 8 (core store skeleton + session), head cacedcb

Reviewed git diff 2750bbb..origin/task/8-core-store-session cold (11 files, only crates/fluxer-core + Cargo.lock).

1. Tests named by the plan — all five exist in crates/fluxer-core/tests/session.rs and assert what the plan says:

  • login_happy_path_emits_discover_login_persist_connect: Api Discover → Api Login → [Persist(SaveToken), ConnectGateway], fixture token, wss://fluxer.forked.io/gateway, ends LoggedIn.
  • login_mfa_moves_to_awaiting_totp_then_connects: AwaitingTotp { ticket } with the fixture ticket, LoginTotp { ticket, code }, then save + connect.
  • captcha_error_shows_message_and_stays_logged_out: LoggedOut { error: Some(CaptchaRequired) }, no effects.
  • unauthorized_while_logged_in_logs_out_and_disconnects: [DisconnectGateway, Persist(ClearToken)] + SessionExpired; GatewayState::AuthFailed has its own test with the same assertions.
  • effects_get_unique_ids: 5 Api effects, 5 distinct ids.

2. Interfaces — App (fields, private next_effect_id), App::new(CoreConfig), apply, CoreEvent, Effect, AuthError, ApiRequest, ApiResult, UserAction (all 20 variants), Auth match the plan. Deviations, none blocking:

  • ApiResult::Sent.message and ApiResult::Edited are Box<Message> (clippy large_enum_variant); Task 10 builds against merged code.
  • GatewayState and CoreError are defined in fluxer-core, because the crate cannot depend on fluxer-gateway/fluxer-api. Variants equal the Task 7 list. Task 14 has to convert both; the README says so.
  • PersistOp/PersistResult/Notification are the subset this task needs; Tasks 12 and 23 extend them as the plan already states.

3. Invariants — fluxer-core deps: fluxer-proto, secrecy, thiserror, tracing, url (all existing workspace deps); no banned crates, no I/O, no unwrap/expect/panic!/indexing outside tests. An unparsable or non-ws gateway URL from discovery ends in AuthError::Other, tested. Tokens and passwords are SecretString; the only token literal is the scrubbed fixture value. Commits carry Refs #8 and the co-author line; README updated.

4. Review Focus — none of the five items is assigned to Task 8 (captcha is Tasks 6/17); the core half of it is covered by the captcha test above.

5. cargo xtask ci in the review worktree: green (fmt, clippy, deny, tests; 16 session tests + 2 unit tests pass; screenshot step skipped because FLUXER_SCREENSHOT_TESTS is not 1). Remote runners not checked.

Findings

  • minor — crates/fluxer-core/src/session.rs (on_failed, on_api): answers and 401s are not tied to a login attempt or session.
    • Any EffectFailed { Unauthorized } whose id is not the in-flight one expires the current session. A late 401 from a request sent with the previous session's token (after Logout + quick re-login), or from a cancelled login attempt, logs the new session out and clears its token.
    • Success answers are matched by state only: cancel a login and submit again, and the first attempt's answer is taken for the second. With SetInstance in between, the old instance's discovery document is stored for the new instance.
    • Both need a narrow race. A fix is a session/attempt generation, or ids on ApiResult (an interface change, so a follow-up issue rather than this PR).
  • minor — crates/fluxer-core/src/error.rs (from_login_failure): every 400 on Login/LoginTotp becomes InvalidCredentials, including validation errors that are not about credentials (e.g. a malformed e-mail). CoreError::Http.code is available to narrow it.
  • minor — crates/fluxer-core/src/effect.rs, session.rs: the MFA ticket and TOTP code are plain Strings in types that derive Debug (ApiRequest::LoginTotp, Auth::AwaitingTotp); nothing logs them today, but a runtime that debug-logs effects would. Short-lived values, the plan leaves the types open.

Verdict: APPROVE — 0 blocking, 3 minor; local ci green.

## Independent review — Task 8 (core store skeleton + session), head `cacedcb` Reviewed `git diff 2750bbb..origin/task/8-core-store-session` cold (11 files, only `crates/fluxer-core` + `Cargo.lock`). **1. Tests named by the plan** — all five exist in `crates/fluxer-core/tests/session.rs` and assert what the plan says: - `login_happy_path_emits_discover_login_persist_connect`: `Api Discover` → `Api Login` → `[Persist(SaveToken), ConnectGateway]`, fixture token, `wss://fluxer.forked.io/gateway`, ends `LoggedIn`. - `login_mfa_moves_to_awaiting_totp_then_connects`: `AwaitingTotp { ticket }` with the fixture ticket, `LoginTotp { ticket, code }`, then save + connect. - `captcha_error_shows_message_and_stays_logged_out`: `LoggedOut { error: Some(CaptchaRequired) }`, no effects. - `unauthorized_while_logged_in_logs_out_and_disconnects`: `[DisconnectGateway, Persist(ClearToken)]` + `SessionExpired`; `GatewayState::AuthFailed` has its own test with the same assertions. - `effects_get_unique_ids`: 5 Api effects, 5 distinct ids. **2. Interfaces** — `App` (fields, private `next_effect_id`), `App::new(CoreConfig)`, `apply`, `CoreEvent`, `Effect`, `AuthError`, `ApiRequest`, `ApiResult`, `UserAction` (all 20 variants), `Auth` match the plan. Deviations, none blocking: - `ApiResult::Sent.message` and `ApiResult::Edited` are `Box<Message>` (clippy `large_enum_variant`); Task 10 builds against merged code. - `GatewayState` and `CoreError` are defined in `fluxer-core`, because the crate cannot depend on `fluxer-gateway`/`fluxer-api`. Variants equal the Task 7 list. Task 14 has to convert both; the README says so. - `PersistOp`/`PersistResult`/`Notification` are the subset this task needs; Tasks 12 and 23 extend them as the plan already states. **3. Invariants** — `fluxer-core` deps: `fluxer-proto`, `secrecy`, `thiserror`, `tracing`, `url` (all existing workspace deps); no banned crates, no I/O, no `unwrap`/`expect`/`panic!`/indexing outside tests. An unparsable or non-ws gateway URL from discovery ends in `AuthError::Other`, tested. Tokens and passwords are `SecretString`; the only token literal is the scrubbed fixture value. Commits carry `Refs #8` and the co-author line; README updated. **4. Review Focus** — none of the five items is assigned to Task 8 (captcha is Tasks 6/17); the core half of it is covered by the captcha test above. **5. `cargo xtask ci`** in the review worktree: green (fmt, clippy, deny, tests; 16 session tests + 2 unit tests pass; screenshot step skipped because `FLUXER_SCREENSHOT_TESTS` is not 1). Remote runners not checked. ### Findings - **minor** — `crates/fluxer-core/src/session.rs` (`on_failed`, `on_api`): answers and 401s are not tied to a login attempt or session. - Any `EffectFailed { Unauthorized }` whose id is not the in-flight one expires the *current* session. A late 401 from a request sent with the previous session's token (after Logout + quick re-login), or from a cancelled login attempt, logs the new session out and clears its token. - Success answers are matched by state only: cancel a login and submit again, and the first attempt's answer is taken for the second. With `SetInstance` in between, the old instance's discovery document is stored for the new instance. - Both need a narrow race. A fix is a session/attempt generation, or ids on `ApiResult` (an interface change, so a follow-up issue rather than this PR). - **minor** — `crates/fluxer-core/src/error.rs` (`from_login_failure`): every 400 on Login/LoginTotp becomes `InvalidCredentials`, including validation errors that are not about credentials (e.g. a malformed e-mail). `CoreError::Http.code` is available to narrow it. - **minor** — `crates/fluxer-core/src/effect.rs`, `session.rs`: the MFA `ticket` and TOTP `code` are plain `String`s in types that derive `Debug` (`ApiRequest::LoginTotp`, `Auth::AwaitingTotp`); nothing logs them today, but a runtime that debug-logs effects would. Short-lived values, the plan leaves the types open. **Verdict: APPROVE** — 0 blocking, 3 minor; local ci green.
charles merged commit 7406e98c03 into foundation 2026-11-11 18:15:59 +00:00
Sign in to join this conversation.
No description provided.