# State Sync and UI Settings Validation Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Complete validation-backed state-sync evidence and UI settings persistence while updating DV documents.

**Architecture:** State reducer work stays in `crates/chanora_state/src/lib.rs` with Rust unit tests. UI settings persistence stays in `apps/chanora_flutter/lib/services/ui_preferences_service.dart` with Flutter service tests; app-level theme application is wired in `apps/chanora_flutter/lib/main.dart` only if needed by the persisted setting.

**Tech Stack:** Rust/cargo tests, Flutter/Dart, shared_preferences, Markdown documentation.

---

### Task 1: State Reducer Validation

**Files:**
- Modify: `crates/chanora_state/src/lib.rs`

- [x] **Step 1: Write failing regression test**

Add `channel_delete_removes_clients_in_deleted_channel` proving channel deletion removes clients assigned to that channel and emits client-removal deltas before the channel-removal delta.

- [x] **Step 2: Verify RED**

Run: `cargo test -p chanora_state channel_delete_removes_clients_in_deleted_channel --locked`

Expected: FAIL because deleted-channel clients remain in state.

- [x] **Step 3: Implement minimal reducer fix**

In `StateEvent::ChannelDeleted`, collect clients whose `client.channel == id`, remove them from `clients` and `client_order`, then emit deterministic `ClientRemoved` deltas before `ChannelRemoved`.

- [x] **Step 4: Verify GREEN**

Run: `cargo test -p chanora_state channel_delete_removes_clients_in_deleted_channel --locked`

Expected: PASS.

- [x] **Step 5: Run full state crate tests**

Run: `cargo test -p chanora_state --locked`

Expected: all state crate tests pass.

### Task 2: UI Settings Persistence

**Files:**
- Modify: `apps/chanora_flutter/lib/services/ui_preferences_service.dart`
- Modify: `apps/chanora_flutter/test/services/ui_preferences_service_test.dart`
- Modify: `apps/chanora_flutter/lib/main.dart`

- [x] **Step 1: Add failing tests for theme persistence**

Add tests for default `system` theme mode, saving `dark`, saving `light`, and invalid stored value fallback to `system`.

- [x] **Step 2: Verify RED**

Run: `flutter test test/services/ui_preferences_service_test.dart`

Expected: FAIL because `UiThemeMode`, `themeMode`, and `saveThemeMode` do not exist.

- [x] **Step 3: Implement minimal service changes**

Add `UiThemeMode`, `UiSettings.themeMode`, persisted key `ui.theme_mode`, and `saveThemeMode`.

- [x] **Step 4: Verify GREEN**

Run: `flutter test test/services/ui_preferences_service_test.dart`

Expected: PASS.

- [x] **Step 5: Wire app theme mode**

Make `ChanoraApp` load persisted theme mode and pass `themeMode` into `MaterialApp`.

- [x] **Step 6: Run focused Flutter tests**

Run: `flutter test test/services/ui_preferences_service_test.dart`

Expected: PASS.

### Task 3: Documentation Updates

**Files:**
- Modify: `docs/implementation-status-2026-05-28.md`
- Modify: `docs/release/dv-waiver-register.md`
- Modify: `docs/verification/swe4-unit-verification-plan.md`
- Modify: `docs/verification/swe6-software-verification-plan.md`
- Modify: `docs/architecture/sdd.md`

- [x] **Step 1: Update implementation status**

Mark `chanora_state` scaffold statement as superseded by reducer implementation/tests and mark UI settings persistence implemented for SharedPreferences scope.

- [x] **Step 2: Update waiver and verification docs**

Record reducer evidence and leave event replay as the remaining P1 state-sync gap.

- [x] **Step 3: Verify docs**

Run sentinel-language scan over edited docs.

---

## Self-Review

- Spec coverage: covers reducer evidence, UI settings persistence, and document updates.
- Placeholder scan: plan contains no incomplete implementation instructions.
- Type consistency: `UiThemeMode`, `themeMode`, and `saveThemeMode` names are used consistently.
