From 0ea936cb967ddf0347ba7cb9f44722f2b0798ad2 Mon Sep 17 00:00:00 2001 From: Jerry Date: Wed, 11 Nov 2026 12:20:52 -0500 Subject: [PATCH 01/10] Add match-day fixes design (Maneuver batch 1) Record identity (TBA event/match keys), duplicate block, draft recovery, next-match flow and exclude-from-stats. Co-Authored-By: Claude Opus 5.5 --- .../2026-11-11-match-day-fixes-design.md | 177 ++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 docs/superpowers/specs/2026-11-11-match-day-fixes-design.md diff --git a/docs/superpowers/specs/2026-11-11-match-day-fixes-design.md b/docs/superpowers/specs/2026-11-11-match-day-fixes-design.md new file mode 100644 index 0000000..91e717c --- /dev/null +++ b/docs/superpowers/specs/2026-11-11-match-day-fixes-design.md @@ -0,0 +1,177 @@ +# Match-day fixes (Maneuver batch 1): design + +Branch: `feature/maneuver-features` (from `redesign/ui`). Approved in conversation 2026-11-11. + +## Why + +The team compared Castle with Maneuver (frc-maneuver.com) and wants the features Castle lacks. 36 gaps were found. They split into four batches, built in this order: + +1. **Match-day fixes (this doc).** +2. Features that carry over to next season: pit, team stats, all-teams table, TBA check, picklists, predictions, scout dashboard. +3. Features tied to this season's field (2026 fuel, field map, climb detail). +4. Offline capture and transfer: save offline, then backup/import, then QR, then Wi-Fi. + +Batch 1 comes first for two reasons. First, record identity is the foundation for 8 later features: match validation, fuel scaling, export/import, QR, Wi-Fi, offline, custom events and predictions. Second, these four fixes stop bad or lost data during matches. + +Problems this batch fixes, all in today's code: + +- **Duplicates count twice.** If two scouts on one team enter the same robot in the same match, both records count in every average. `add_scouting_data` (`app/scout/scouting_utils.py:148`) inserts with no check. The client check in `app/static/js/scout/add.js:67-80` is commented out. +- **Records can't be joined to TBA.** `event_code` stores the TBA event's display name and `match_number` stores a label such as `Qual 12`. The posted `_event_key` is dropped, and the display name repeats every year. +- **A scout can lose a whole match of counts.** A locked phone, a Back press, a refresh or a failed submit wipes the form. Nothing saves form fields. +- **Scouts re-pick everything after each save.** Save redirects to the list (`app/scout/routes/records.py:72`), so the scout has to choose the event, match and robot again. That is slow, and it causes records filed under the wrong match. +- **Bad matches can't be dropped from stats without deleting them.** Only hard delete exists, and it loses the notes too. + +## Goals + +- **Store TBA codes.** Every new match record stores `event_key` (e.g. `2026nyny`) and `match_key` (e.g. `2026nyny_qm12`). +- **Block duplicates.** Saving a record is refused when that robot in that match is already recorded by the same team. The error names the scout who recorded it. +- **Recover lost forms.** The add form's contents survive a crash, refresh, Back or failed submit, and are offered back on the next visit. They clear only after the server confirms the save. +- **Jump to the next match.** After a save, the add form reopens with the same event, the next match and the same alliance station already selected. +- **Exclude bad matches.** A record can be marked "Exclude from stats". Excluded records drop out of every average but stay listed and editable. + +## Non-goals + +- No backfill or migration of existing records, and no writes to the user's MongoDB outside normal app use. Old records simply have no keys. Old duplicates can be excluded by hand. +- No new database index or unique constraint. +- No bulk delete by event, and no automatic exclusion of `Testing Regional` records. That belongs to demo data in a later batch. +- No offline saving. A draft survives an offline submit, but the record isn't queued. +- No visual redesign. Changes use the current styles. The redesign restyles them later. + +## Anti-goals (a failure even if it works) + +- A draft wiped before the server has confirmed the save. +- A newer in-progress draft cleared by reloading an old "saved" URL. +- A second scout's entry silently replacing the first. +- Records from two different teams blocking each other. Castle is multi-team, and each team sees only its own records (`access_match`). +- Excluded records still counted in a leaderboard, compare, match view or auto-path average. +- A 2027 record matched as a duplicate of a 2026 record. + +## Approaches considered + +- **Duplicate check.** Chosen: the server checks on save. Rejected: a browser-only pre-check, which is bypassable and racy. Also rejected: a unique Mongo index, which can't express "same scouting team" because the scout's team isn't on the record, and would need a live-DB migration. +- **Identity key.** Chosen: TBA keys. Rejected: event display name plus label, because names repeat yearly and would collide across seasons. +- **Draft storage.** Chosen: browser `localStorage`. Rejected: server drafts, which don't work offline and add a collection. +- **Next match.** Chosen: the redirect URL carries the event, last match and station. Rejected: device storage, because the redirect already has the data and the server can validate it. +- **Bad matches.** Chosen: an `excluded` flag on the record. Rejected: deleting, which loses notes, and a separate collection, which is needless. +- **When a duplicate is found.** The user chose **block**. Rejected: replace (Maneuver's behaviour), which silently loses the first entry. Also rejected: keep both and average, which complicates every stat. + +## Design + +### 1. Record identity + +- **Event key.** On add, take the posted `_event_key`. It is already filled by `add.js:81` from the selected event option's `data-key`. Accept it only if it fully matches `^\d{4}[a-z0-9]+$`. TBA keys and the built-in testing events (`{year}test1..3`) both match. Store it as `event_key`. +- **Match key.** Derive it on the server from the event key plus the posted `match_number` label. `app/scout/match13_fuel.py:38` `match_key()` already parses the labels the form produces (`Qual N` → `qmN`, `Semifinal S-M` → `sfSmM`, `Final N` → `f1mN`), but it is guarded to 2026 events by `EVENT_PATTERN`. + - Lift the label parsing into a year-agnostic helper, so record identity works next season. A natural home is `app/scout/TBA.py`, since it is about TBA key formats. + - Keep match13's 2026-only guard by having its `match_key` call the new helper after its own check. + - Store the result as `match_key`. +- **Missing keys.** If either key is missing or invalid, save the record without the keys and skip the duplicate check. Never reject a record because a key is missing: this branch can't fix the client, and losing a match is worse than a possible duplicate. +- **Display fields unchanged.** `event_code` (display name) and `match_number` (label) are still stored as today. Every existing page reads them. +- **Edit.** The edit page lets users free-type event, match and team. + - If an edit changes `event_code`, `$unset` both `event_key` and `match_key`. + - If it changes only `match_number`, `$unset` `match_key`. The record is then unlinked from TBA, never mislinked. + - Changing only `team_number` keeps both keys. + - The existing edit duplicate check stays as is. + +### 2. Duplicate block (add only) + +- **The check.** Before inserting, if `match_key` is set, look for records with the same `match_key` and `team_number` that are not excluded (`excluded != true`). Join each to its scout with the existing `SCOUTER_LOOKUP`, and keep those by the same scouting team: +- **Excluded records don't block.** *(Builder-check call.)* Excluding a record means it doesn't count, so re-scouting that robot in that match must be allowed. Editing over the excluded record would destroy the notes that exclusion exists to keep. + - If the current user has a `teamNumber`: records whose scout's `teamNumber` equals it. + - If the current user has no team: records whose `scouter_id` is the current user. +- **When one is found.** Refuse the save. The flash error reads like: `Team 254 in Qual 12 was already scouted by . Edit that entry from the scouting list instead.` Then redirect back to the add form (see section 4), so the draft is offered and the scout can discard it. +- **Return value.** `add_scouting_data` keeps its `(success, message)` return. A duplicate is a `False` with that message. +- **Known race.** Two saves in the same second can both pass. Accepted for this batch; mark it in code with a `shortcut:` comment. + +### 3. Draft recovery (add form, browser only) + +- **Draft id.** On add-form load, generate a fresh draft id (e.g. `crypto.randomUUID()`, falling back to a timestamp plus random string). Put it in a new hidden field `_draft_id`. +- **Storage shape.** *(Builder-check call.)* Use one `localStorage` key per logged-in user, e.g. `castle:scout-drafts:`, holding a map of draft id to draft. A per-user key means a scout on a shared tablet is never offered another user's draft. A map means starting a new entry never overwrites an older unsaved one. Put the user id in the page, e.g. a `data-` attribute on the form. +- **Saving.** Save the *current* draft id's entry on `input` and `change` (debounced, about 500 ms), and on `pagehide`. Only events with `event.isTrusted` count, so the URL preselection (section 4) and restore filling fields by code never create or overwrite a draft. Save every named form field, the drawn auto path (the hidden path field after `updatePathData()`), the event key, the match label, the selected team and `savedAt`. +- **Pruning.** On load, drop drafts older than 24 hours. +- **Offering drafts back.** On load, show a small banner for each other draft, newest first: `Unsaved entry: Team 254, Qual 12 (saved 3 min ago)` with **Restore** and **Discard**. There is usually one, rarely more. The banner stays until it is answered, and the scout can start a new entry meanwhile without endangering it. +- **Restore.** Remove the current (fresh) draft. Make the restored draft's id the current id and set `_draft_id` to it. Then re-select the event, wait for its matches to load, select the match, select the team, then fill the other fields and redraw the path. + - `Canvas` has `loadFromJSON` (`app/static/js/Canvas.js:991`). + - `initScoutFormCanvas` (`app/static/js/scout/form-canvas.js:428`) returns only `updatePathData`, so expose what restore needs. +- **Clearing.** A draft is removed only when the page loads with `?saved=` and a stored draft has that id. That one entry is removed, and nothing else. Any other `saved` value, such as an old URL reloaded later, leaves every draft alone. **Discard** removes only its own draft. +- **Storage errors.** Wrap every storage access in `try/catch`. If storage is unavailable, the form works exactly as today. + +### 4. Next-match flow + +- **Station.** On submit, `add.js` also posts a hidden `_station`, the selected robot's alliance plus position (`red1`..`red3`, `blue1`..`blue3`). Position is its order in the match's `red` or `blue` team list (`add.js` team select builder, about line 409). +- **Redirect after a save.** Redirect to `url_for("scouting.add", event=, after=, station=, saved=)`, instead of the list. + - Pass each value only if it validates: event key per the regex above, station per `^(red|blue)[1-3]$`, draft id per a short `[A-Za-z0-9-]{1,64}` pattern. + - `after` is the label, length-capped (e.g. 32 chars). It is only compared in JS, never rendered as HTML. +- **Redirect after a failed save** (duplicate or error). Redirect to the add form with `event`, `after` set to the *same* label and the same `station`, but no `saved`. The scout lands on the same match and gets the draft offered. + - The other failure, invalid auto-path JSON at `records.py:46-50`, also redirects to the add form rather than the list. + - **Builder's call:** for the failure redirect, a `match=