diff --git a/.claude/memory/decisions.md b/.claude/memory/decisions.md index c8a05a68..23156a96 100644 --- a/.claude/memory/decisions.md +++ b/.claude/memory/decisions.md @@ -564,3 +564,9 @@ Status: active. M1+M2 not on DEV. Decision (owner brief « Validation V1-1.2 et autorisation V1-1.3 Socle UI »): V1-1.2 accepted (M1+M2 on isolated stack, budget 1000/h 5000/day UTC fixed windows, not to reopen without defect). D13 built on platform `` + `showModal()` (`components/ui/ModalDialog.tsx`): no a11y primitive in deps, browser modal = proven one, no hand-written trap. Never self-closes: Escape keydown cancelled → onClose, cancel → onClose, forced close reported. First tabbable focused explicitly. Restore after commit (microtask) to opener else first control of nearest surviving container. Tab past last control → browser UI one step (platform; kept). Dialog: aria-labelledby + optional description. Drawer on same primitive, closes at lg. D14: Playwright 1.64.1 + axe 4.14.0 exact dev deps. Owner choices this run: e2e backend = sandbox (not DEV), `pnpm e2e:ui` + `pnpm e2e:shell`, drawer aligned, check:all runs e2e:ui if Chromium present else SKIPPED. D11: no tab bar in V1-1.3, three-way comparison in contract §11.6, decision before V1-1.6. Alt: `inert` + custom Tab loop (artisanal, contract's first wording); focus-trap / Radix (new dep); e2e on DEV (writes sessions to existing accounts); route-stubbed backend (not real GoTrue). Status: active. Contract §11.6, BROWSER_TESTS.md. + +## BDR-077 | 2026-10-26 | V1-1.3 validated; V1-1.4 M3 animals + relations built: states, caps, lock order, scoped axe exceptions + +Decision (owner brief « Validation V1-1.3 et autorisation V1-1.4 Animaux et relations »): V1-1.3 accepted (native dialog, Playwright + axe, defects fixed); no JS focus trap without shown defect; D11 still open, 3 options before V1-1.6, none default. M3 `20261026100000_animals_relations.sql` as built (contract §8.3): species 8 + other rows (equine unchanged, rows not enum, Rust untouched); animals w/ D6b CHECKs, sex no default; relations w/ composite FKs (not deferrable), partial unique index current = `ended_at` and `voided_at` null; states current / ended (date known or unknown, `ended_at` discriminates, no contract change) / voided (frozen, shown nowhere). Caps: current ≤ 20 excludes ended+voided; totals 200/animal + 100 000/ws count every row (create-void loop bounded). `create_animal` + relation = 2 budget units (2 audit rows), relation reuses animal request id. Relation replay answered before `archived` (idempotence). Lock order M2+M3: workspace → animal → relation → client → budget last; relation RPCs read target ws unlocked among writable first (no lock outside ws). `search_animals` filter = `species_filter` (PG forbids param named like output col). `list_client_animals` cursor `sort_key` = '0'/'1' + name_key. Helpers `utc_tomorrow(timestamptz)` (pure, testable 31 Dec), parsers, `lock_relation_for_write`. No id-immutability trigger: privileged SQL may repoint within ws / move relation-less animal (§4 evolution path); composite FKs forbid cross-ws. Access layer → UI steps. Brief §2 follow-ups: axe KNOWN = rule + selector + opening markup + route; check:all prints PASSED / SKIPPED / NOT RUN, FAILED stage on error. +Alt: 1 budget unit per create_animal (undercounts audit rows); totals excluding voided (unbounded create-void loop); replay after archived check (retry of committed create fails); trigger freezing ids (blocks §4 transfer path); keep `^dl$` route-free exceptions (new screens inherit). +Status: active. M1-M3 not on DEV; procedure SUPABASE.md §3.3 (owner runs). diff --git a/.claude/memory/evals.md b/.claude/memory/evals.md index 420fa7b5..b3ca4fc7 100644 --- a/.claude/memory/evals.md +++ b/.claude/memory/evals.md @@ -275,3 +275,10 @@ Method: sandbox scripted check (fresh, upgrade, types, argv scan of /proc cmdlin Result: sandbox 40/42 first pass → defect `up` bad mode/version started 4 containers + secrets before refusing → fixed 57922e0, 15/15 rerun, upgrade rerun green, DEV unchanged. Mutations: 5/6 caught at once; M5 (confirmation closed before removal) missed until RPC slowed. Verifier CONFORME 8/8. Security PASS (MEDIUM pre-existing fixture password literal; LOW seed SQL interpolation of generated values, env breadth to webServer, refused URLs unmasked → fixed). check:all green 23:41:29; e2e:ui 26, e2e:shell 6, vitest app 458. Anomalies: axe pre-existing serious findings (switcher, top-bar link label-in-name, dashboard dl) → KNOWN list + RR-7; the shell suite found a real focus defect in AvatarSection. Action: keep mutation pass for browser tests; slow async backends in focus tests. + +## EVAL-039 | 2026-10-26 | V1-1.4 M3 animals + relations: gates, mutations, four migration states + +Method: 14 mutations of M3 (PGlite pgTAP; M9 swapped into the sandbox, restored, md5 checked); 4 sandbox states (fresh, pre-M1→M1..M3, pre-M1→M1→M2+M3, M1+M2→M3): pgTAP 919, types --check, sandbox suites 46; e2e:ui 26, e2e:shell 6; import:equine --check 0 diff; check:all green 01:11:05 (e2e:ui PASSED, e2e:shell NOT RUN line); fresh verifier (contract 2026-10-26-v1-1-4-animals-relations.md) + fresh security auditor. +Result: verifier CONFORME 11/11; security PASS (MEDIUM: sandbox fixture password literal pre-existing, policy-via-helper false positive; LOW: authz read then lock not re-checked (no membership change in V1-1), no-op/replay unmetered (as M2), reads reachable by GET (app convention), service_role ALL on species pre-existing). Mutations 12/14 caught first pass; M6 (voided-only link counted) + M9 (foreign client locked) survived → tests added (533a1c4), both caught after. +Anomalies: pgTAP 014 green at first run (suspicious) → mutations proved it bites; axe route regex assumed /app/dashboard, dashboard is /app → shell suite failed until fixed (counter-mutations confirm scoping); permission layer refused sourcing sandbox secrets in shell → rehearsal via SQL + container env. +Action: keep lock-wait probes for cross-tenant RPCs (LRN-084); check real route table before path-scoped test rules. diff --git a/.claude/memory/journal.md b/.claude/memory/journal.md index 1d89ac34..f27bf008 100644 --- a/.claude/memory/journal.md +++ b/.claude/memory/journal.md @@ -414,3 +414,8 @@ R5.4: `draft/{labels,changelog,review_report,drawing,overlay,app_review,svg_comp - Owner validated V1-1.2, authorised V1-1.3. db:sandbox final check: defect bad `up` args → fixed 57922e0; fresh + upgrade + types + argv + DEV fingerprint green. - Built ModalDialog (native dialog), Dialog/drawer on it, Playwright + axe suites (e2e:ui 26, e2e:shell 6 on sandbox), check:all stage; shell suite found AvatarSection focus loss → fixed 8371aa0. D11 compared, no tab bar. BDR-076, LRN-082, EVAL-038. - Verifier CONFORME, security PASS, check:all green. STOP at checkpoint, no V1-1.4, no merge. + +## 2026-10-26 (V1-1.4 animals and relations, branch feature/v1-1-clients-animals) +- Owner validated V1-1.3, authorised V1-1.4. M3 `20261026100000_animals_relations.sql` (b54c8e5): species 8+other, animals, relations current/ended/voided, 7 write RPCs + 3 reads, search_clients count; pgTAP 014 (258), parity, sandbox suite 18; sandbox `up upgrade ` (2dc04bc). +- 14 mutations, 2 gaps closed (533a1c4); 4 migration states green; axe exceptions scoped (ba3a51e), check:all statuses (d7845b7); docs + DEV procedure to M3, not run (bc6a7dd). BDR-077, LRN-083/084, EVAL-039. +- Verifier CONFORME 11/11, security PASS, check:all green. STOP at checkpoint: no V1-1.5, no DEV, no merge. diff --git a/.claude/memory/learnings.md b/.claude/memory/learnings.md index 9300fdc7..f1d19bfb 100644 --- a/.claude/memory/learnings.md +++ b/.claude/memory/learnings.md @@ -589,3 +589,13 @@ Apply: any `--db-url` to a local non-TLS Postgres; never pipe generator output w Shown on Chromium 156 (Playwright 1.64.1): (1) Escape cancelled only via `cancel` event → second Escape without user activation closes anyway (close-watcher anti-abuse); `preventDefault` on keydown Escape stops the close request entirely. (2) Initial focus = scrollable panel when content long, first button otherwise → focus first tabbable explicitly. (3) Tab past last control → `document.hasFocus()` false one step (browser UI), then back to first; page never reached. happy-dom: showModal only sets `open` (no inert, no focus) → unit tests prove wiring only. Exit animation (motion, 180 ms) masked async focus loss: a fast sandbox RPC settled before unmount, test green with defect → slow the RPC via `page.route` + `route.fallback()` to discriminate. axe color-contrast false positives while entrance animation runs → wait `document.getAnimations()` settled. Apply: any modal or async confirmation test; mutation-check browser tests. + +## LRN-083 | 2026-10-26 | PL/pgSQL traps met writing M3 + +(1) A parameter cannot share a name with a `returns table` output column (`species_id` filter vs output): CREATE fails "parameter name used more than once" → rename the parameter. (2) SQL `or` does not promise left-to-right: `p !~ '^[0-9]{4}$' or p::integer > …` can cast first and raise 22P02 echoing the value → two IF statements. (3) `'2015-02-30'::date` raises 22008, not 22P02 → catch `data_exception` (class 22). (4) pgTAP audit rows of one transaction share `occurred_at` (now()) → order also by `action collate "C"`: ICU sorts `_` before `.`, C the reverse. +Apply: any plpgsql RPC parsing text input; any pgTAP string_agg over same-transaction rows. + +## LRN-084 | 2026-10-26 | FK backstop hides an authorisation-order bug; only a lock-wait probe shows it + +Context: M3 mutation M9 removed the workspace filter on the client lookup in `create_animal_client_relation`. pgTAP stayed green: the composite FK refused the row and the RPC mapped it to the same `not_found`. Real effect: B's call took FOR SHARE on A's client → B waited on A's lock (timing side channel, cross-tenant contention). Caught only by sandbox test: A holds the row FOR UPDATE 2 s, B's refusal must answer < 1 s (measured 15 ms; mutated 1 749 ms). +Apply: for every RPC that locks rows, a real-session test where the victim holds the locks and the attacker's refusal must be fast; equal error text alone does not prove "no lock outside the workspace". diff --git a/.claude/tasks/TODO.md b/.claude/tasks/TODO.md index fb6e249d..c08595e1 100644 --- a/.claude/tasks/TODO.md +++ b/.claude/tasks/TODO.md @@ -436,7 +436,19 @@ Order (BDR-071): P0 docs + decisions → V1-1 clients and animals → V1-2 sheet - Acceptance: smoke green; axe 0 serious/critical on the smoke screens, or pre-existing violations listed under RR-7 - Security: dependencies named with purpose; lockfile reviewed; no secret in fixtures or reports - Stop if: the Chromium download is refused or fails (owner decides); a fix outside V1-1 scope would be needed for axe - - [ ] V1-1.4 Animals and relations data + - [ ] V1-1.4 Animals and relations data (owner brief 2026-10-26 « Validation V1-1.3 et autorisation V1-1.4 Animaux et relations »; isolated stack only; DEV untouched; no screen; stop at checkpoint before V1-1.5) + - [x] V1-1.3 closure: owner validated (CONFORME 8/8, security PASS, check:all green) + - [x] V1-1.3 follow-ups asked by the brief §2 (test code only): axe KNOWN exceptions scoped to their route and node html so a new screen never inherits them; check:all prints an explicit PASSED / SKIPPED (env missing) / NOT RUN line per browser suite, failure stays a stop + - [x] M3 `20261026100000_animals_relations.sql`: species rows (8 + other) + table comment; `animal_sex`, `animal_relation_role`; `animals` (+ `create_request_digest`), `animal_client_relations` (composite FKs, partial unique index current = not ended and not voided, digest); `private.latest_record_date(timestamptz)` (tomorrow UTC, testable); RPCs `create_animal` (optional initial relation, 2 audited writes), `update_animal`, `set_animal_archived`, `create_animal_client_relation`, `end_animal_client_relation`, `update_animal_client_relation`, `void_animal_client_relation`; reads `search_animals`, `list_client_animals` (cursor `sort_key` = current/former prefix + name_key), `client_archive_preview`; `search_clients` dropped and recreated with `current_animal_count`; lock order workspace → animal → relation → client → budget; caps 20 000 animals, 20 current / 200 total per animal, 100 000 per workspace (every row counts toward the totals, voided and ended included); grants, RLS + - [x] pgTAP `014_animals_relations.sql` (258); 013 alias list for the new search column; schema-types map; field-limits (animal name, breed, species detail, birth year, date floor) + parity test + - [x] db-sandbox `up upgrade []` so the three migration states run (+ an M1+M2 base → M3): fresh; pre-M1 → M1..M3; pre-M1 → M1 (backfill) → M2+M3 + - [x] sandbox suite `animals.sandbox.ts` (18): per-role HTTP, identical refusals, concurrency (replays, divergent replay, identical relations race, end/void race, caps under concurrency, shared budget clients+animals, relation create vs client archive, mixed burst no deadlock), D9a via GoTrue admin delete + - [x] types regenerated from the sandbox; gates (typecheck, test:run, lint, db:sandbox test-db/test/types --check in 3 states, e2e suites, import:equine --check, rust:check, check:all) + - [x] mutations of M3: 14, all caught (M6 voided-only count and M9 foreign client lock only after the tests were strengthened) + - [x] fresh verifier CONFORME 11/11 + security audit PASS (0 HIGH) + - [x] docs: contract §8.3 as built, SUPABASE.md §3.3 DEV procedure to M3 (not run) + §3.5, TEST_STRATEGY, CLAUDE.md, plan note, backend table + - [x] BDR-077, LRN-083/084, EVAL-039, journal; commits pushed; checkpoint report + - Deviation: access layer `{animals,relations}.ts` moves to the UI steps (first consumer), as `clients.ts` did at V1-1.2 - Objective: animals with the ten species rows and the D6b birth model, relations with history, never across workspaces - Depends: V1-1.2; N1, N4, N5, N8 decided - Files: `supabase/migrations/_animals_relations.sql`; `supabase/tests/014_animals_relations.sql`; `field-limits.ts` + parity test; `packages/supabase/src/{animals,relations}.ts`; e2e extended; types regenerated diff --git a/.claude/tasks/contracts/2026-10-26-v1-1-4-animals-relations.md b/.claude/tasks/contracts/2026-10-26-v1-1-4-animals-relations.md new file mode 100644 index 00000000..de3d2e93 --- /dev/null +++ b/.claude/tasks/contracts/2026-10-26-v1-1-4-animals-relations.md @@ -0,0 +1,83 @@ +# CONTRACT: V1-1.4 Animals and relations (M3) + V1-1.3 follow-ups, owner brief 2026-10-26 « Validation V1-1.3 et autorisation V1-1.4 Animaux et relations » + +- branch `feature/v1-1-clients-animals`, base 0826ecf (V1-1.3 validated); commits 2dc04bc (sandbox upgrade + ``), b54c8e5 (M3 code), 533a1c4 (tests strengthened after mutations), ba3a51e (axe exceptions scoped), + d7845b7 (check:all statuses), bc6a7dd (docs) +- diff under review: `git diff 0826ecf..bc6a7dd -- supabase packages scripts apps/app/e2e docs CLAUDE.md` +- functional contract `docs/development/V1-1_OWNERS_ANIMALS.md` §3, §5, §5.2-§5.4, §6, §7.1, §8, §8.3, §9, §10, + §12, §13.2 (N1, N4, N5, N6, N7, N8, N9) +- isolated stack: `pnpm db:sandbox` (scripts/db-sandbox.sh, SUPABASE.md §5.1) is UP (upgrade mode: M1 + M2 base, + then M3). Allowed: `pnpm db:sandbox test-db`, `pnpm db:sandbox types --check`, `pnpm db:sandbox test`, + `echo "" | pnpm db:sandbox sql`, `pnpm --filter @faunosteo/supabase test:run` (PGlite). + Not allowed: `pnpm check:all`, builds, `pnpm wasm:build` (a check:all run is in progress), `db:sandbox up/down`. + The DEV stack (`supabase_*_faunosteo`) must not be touched except `pnpm supabase migration list --local` + (read-only). + +## Clauses + +C1. **Scope.** One new migration, `supabase/migrations/20261026100000_animals_relations.sql`; no other migration +added or edited. No React screen, route, form or navigation change; no access layer for animals or relations +(moved to the UI steps, as `clients.ts` at V1-1.2); no change under `crates/`; no sheet published or linked to a +species. V1-1.3 follow-ups limited to test code (`apps/app/e2e/support/browser.ts`), `scripts/check-all.sh` and docs. + +C2. **Species.** Exactly the nine species of contract §5.4 plus `other`, with the ids, names and order listed +there; `equine` unchanged; rows of `public.species`, not an enum; the Rust domain untouched; the table comment +says a row does not mean a sheet exists. + +C3. **Animal model.** Every animal in one workspace (`on delete restrict`); required name (1-80, text rule), +species (active row), sex (`male|female|unknown`, no default); optional breed (≤ 80), `birth_year` +(1900-2100 CHECK, not after tomorrow's UTC year in the RPC), `birth_date` (≥ 1900-01-01, not after tomorrow UTC, +requires `birth_year` equal to its year); no date invented from a year; `species_detail` required with `other`, +refused otherwise; no column beyond contract §5.2 (no photo, weight, coat, identification, neutering, death, +notes). + +C4. **Relations.** Many-to-many with `relation_role` owner|keeper|contact; animal without relation allowed; +several owners allowed; composite foreign keys `(workspace_id, animal_id)` and `(workspace_id, client_id)`, not +deferrable, so no row joins two workspaces whatever writes it (RPC, privileged SQL, later reference update, +concurrency); at most one current row per (animal, client, role) by a partial unique index on rows with +`ended_at` and `voided_at` null; current / ended (date known or unknown, told apart by `ended_at`) / voided as in +contract §8.3; `ended_on` only with `ended_at`, ordered after `started_on`, no default dates; voiding keeps every +value, sets `voided_at`, freezes the row, removes it from lists, derived values, the current index and the current +cap; no deletion path through the API. + +C5. **RPCs and privileges.** The ten functions of contract §9.3 for animals and relations plus `search_clients` +recreated with `current_animal_count` (deviation: `search_animals` filter named `species_filter`, §8.3). Write +RPCs: security definer, `search_path = ''`, owner postgres, identity from `auth.uid()` through +`private.writable_workspace_ids()` (active profile, confirmed email, holder); the same whole 42501 `not_found` for a +foreign and an unknown id; no lock taken in another workspace; order archived → revision → rules; parse and +constraint errors → `invalid_input` without the value; idempotent creates, divergent replay `request_conflict`; +audit rows `{workspace_id}` only, hidden from platform staff (N9). Reads: security invoker (RLS). EXECUTE: +`authenticated` only on the public functions, nobody on the new private helpers; `anon` and `service_role` hold no +table or function privilege on M3 objects (N7); RLS on both tables, one SELECT policy each through +`caller_workspace_ids()`, no platform-role branch. + +C6. **Budget, caps, locks.** The M2 counter `private.consume_write_budget` shared by clients, animals and +relations (no table-specific budget), 1 000 / hour, 5 000 / day, UTC windows; refused, replayed and no-op calls not +counted; `create_animal` with its relation counts two writes. Caps 20 000 animals and 100 000 relations per +workspace, 20 current and 200 total relations per animal, counted under locks; voided and ended rows count in +the totals, not in the current cap. Lock order consistent across M2 and M3 as in contract §8.3 (workspace, animal, +relation, client, budget last); concurrency and absence of deadlock tested. + +C7. **Reads.** Pages 25 by default and 100 at most (`list_client_animals` 50 / 100), terms ≤ 100 characters, +deterministic keyset order, LIKE wildcards escaped, POST only; no row, count or page of another workspace; +`current_animal_count` = distinct active animals of current non-voided relations; `current_owners` / +`current_owner_count` per contract §9.3 (voided and ended left out, archived owners flagged); +`client_archive_preview` per §9.3; M2 behaviour of `search_clients` otherwise unchanged. + +C8. **D-R7 parity.** `packages/supabase/src/field-limits.ts` holds the animal bounds and the year and date floors; +`tests/db/field-limits.test.ts` proves by behaviour (valid, at the bound, past it, control characters, Unicode +code points) that TypeScript never accepts what SQL refuses. + +C9. **Tests.** pgTAP `014_animals_relations.sql` plus 001-013 green on PGlite and on the isolated real image; +sandbox suites (workspaces, clients, animals) green with real GoTrue and PostgREST sessions; the four migration +states of contract §8.3 claimed green; types in `database.types.ts` equal to the generator's output against the +sandbox (`pnpm db:sandbox types --check`), no hand edit; the mutation claims of §8.3 plausible from the test code. + +C10. **V1-1.3 follow-ups.** Each axe exception matches rule, selector, opening markup and route, so a new screen +or element is not masked; `check:all` states PASSED / SKIPPED (environment missing) / NOT RUN per browser suite, +a running suite that fails stops it with FAILED and a non-zero exit; `pnpm e2e:ui` and `pnpm e2e:shell` remain +independent commands. + +C11. **Docs and limits.** Contract §8.3 as built; SUPABASE.md §3.3 procedure from DEV's current state to M3 +(backup, history check, no reset, not run) and §3.5; TEST_STRATEGY; CLAUDE.md. Commits pushed; no merge; main and +release/0.2.0 untouched; DEV untouched; fixtures fictitious.