diff --git a/.claude/memory/journal.md b/.claude/memory/journal.md index ab7252e2..1bb1d37c 100644 --- a/.claude/memory/journal.md +++ b/.claude/memory/journal.md @@ -277,3 +277,7 @@ Owner brief: 5 checks before R3 close. Copy round trip proven, no in-app restore ## 2026-10-17 (R4.0 plan) Owner go R4.0 (A1-A13: one branch + J1, old PDFs preserved, years 0-99 kept, Producer/Creator Faunosteo, goldens/r4, 0 px diff, import message kept + kinds split, copy via worker, Firefox/WebKit, report:compare name, TTF/scope_disclaimer/caps out, WASM stop, real PDFs). chore/rust-r4-plan: plan rev 1 → 3 challengers (19 major, 0 fatal) → rev 2. Facts: Rust lossy UTF-8 = TextDecoder (200,015 cases, 0 diff); serde_json refuses 1e400 (JS → null) → rewrite; TS embedPng holds RGB planes until save (≈ 3.8 MB/view) → session ≈ 0.37 MB/view; import already needs DecompressionStream (Firefox 113, Safari 16.4); glue module record retains instances (no throwaway instance). Firefox 157 Playwright OK, WebKit needs 2 apt libs. No code. Stop: owner validation, OD-1..OD-7. + +## 2026-10-18 (R4 prerequisites) + +Owner validated R4.0 plan with conditions (BDR-057, plan §A.6, LRN-059/060). R3 state checked: merged 8df964b, branch gone. chore/rust-r4-plan finished → develop ae17097. Hotfix bugfix/pdf-download-webkit 2b8000e: downloadBlob (anchor attached, URL revoked after 30 s) shared by rescue copy + PDF export; 3 challengers SOLID, app 405, security PASS, RUST.md 8.7 synced. Playwright WebKit 27.2 now starts (owner installed libs); desktop WebKit/Firefox/Chromium save the PDF even with immediate revoke → fix unproven until owner iPhone run. Stop: owner iPhone validation + CHANGELOG wording, then merge, then feature branch. diff --git a/.claude/tasks/TODO.md b/.claude/tasks/TODO.md index 6269cea8..ffda072d 100644 --- a/.claude/tasks/TODO.md +++ b/.claude/tasks/TODO.md @@ -205,7 +205,9 @@ feat/findings-tree-v2 merged (FindingsTreePicker tracked in HEAD); all work belo - [ ] R3 follow-ups kept by the owner (2026-10-15): worker TLS exercised against a real HTTPS endpoint before the deployment that needs it; roxmltree limits + SVG prescan (pin the iterative release when published); Firefox/WebKit check; SVG references across drawings scoped per view before community uploads (Step 13); WASM size margin (396 of 600 KB) before R4; TypeScript PDF sanitiser still present until R4 - [ ] R4 pdf core: reader (Web Worker, BLK-009 browser measurements) + writer (`pdf-writer`, Std14 AFM, draw log; lines/rects/colours golden frozen with the TS engine first, D-R2) + `report-compare` tool; `packages/pdf` deleted; N-1 scanner reads R4 PDFs - [x] R4.0 plan (owner go 2026-10-17, arbitrations A1-A13): `.claude/tasks/plans/2026-10-17-rust-r4-pdf-core.md` rev 2 after 3 challengers (correctness 6 major, robustness 10 major, simplicity 3 major; none fatal) on chore/rust-r4-plan; Playwright Firefox 157 installed + proven, WebKit 27.2 downloaded (owner: `sudo apt-get install libevent-2.1-7t64 libmanette-0.2-0`); validated by the owner 2026-10-18 with conditions (plan §A.6, BDR-057) - - [ ] after validation: finish chore/rust-r4-plan into develop (owner signal), Safari PDF download fix (separate bugfix, owner go), then cut feature/rust-r4-pdf-core + - [x] R3 closed checked 2026-10-18 (8df964b on develop, branch gone); chore/rust-r4-plan finished into develop ae17097 (owner signal 2026-10-18) + - [ ] Safari PDF download fix on bugfix/pdf-download-webkit (2b8000e + docs): downloadBlob, URL kept 30 s; 3 challengers SOLID, app 405 tests, typecheck, lint, security PASS; Playwright WebKit/Firefox/Chromium do not reproduce the cancel (desktop) → owner iPhone validation, then merge (branch kept unmerged meanwhile); CHANGELOG entry in owner wording; if the iPhone run fails: investigate async trigger + iOS user gesture first, never raise the 30 s + - [ ] then cut feature/rust-r4-pdf-core from updated develop; WebKit libs installed by owner 2026-10-18, Playwright WebKit 27.2 runs workers + wasm - [ ] R4.1 freeze (O1-O13, ledger, browser harness, TS baselines) - [ ] R4.2 reader (cargo + wasm, differential, OD-3 thresholds confirmed with owner before R4.3) - [ ] R4.3 import worker + rescue copy (B.1.5, OD-1 applied, browser proof per slice from here) diff --git a/.claude/tasks/contracts/2026-10-18-pdf-download-webkit-0930.md b/.claude/tasks/contracts/2026-10-18-pdf-download-webkit-0930.md new file mode 100644 index 00000000..1150a5bd --- /dev/null +++ b/.claude/tasks/contracts/2026-10-18-pdf-download-webkit-0930.md @@ -0,0 +1,11 @@ +# CONTRACT: PDF export download survives WebKit (hotfix) + +- REQUEST (verbatim, owner brief 2026-10-18 + synthesis 2026-10-17): « Le téléchargement PDF Safari mérite un correctif indépendant avant la recette iPhone. » The URL is revoked at once (`EditorRoute.tsx:245`), which WebKit may cancel. +- ACCEPTANCE CRITERIA: + 1. The PDF export download keeps its object URL alive `REVOKE_DELAY_MS` (30 s) and revokes it afterwards; it is never revoked synchronously. + 2. The anchor is attached to the document while clicked, then removed. + 3. File name unchanged: `bilan-${report.animal.name}-${report.date}.pdf`; Blob type `application/pdf`. + 4. `downloadJson` behaviour unchanged (name `-.json`, type `application/json`, 30 s revoke). + 5. `pnpm --filter @faunosteo/app test:run`, `pnpm typecheck`, `pnpm lint` green. +- FILE SCOPE: `apps/app/src/lib/download.ts`, `apps/app/src/app/routes/EditorRoute.tsx`, `apps/app/tests/unit/routes.EditorRoute.export.test.tsx` (the existing case asserts the old immediate revoke and must change). +- OUT OF SCOPE: any UI change, the export engine, other object URLs (rasteriser, avatar). diff --git a/.claude/tasks/plans/2026-10-18-pdf-download-webkit-0930.md b/.claude/tasks/plans/2026-10-18-pdf-download-webkit-0930.md new file mode 100644 index 00000000..10896d9d --- /dev/null +++ b/.claude/tasks/plans/2026-10-18-pdf-download-webkit-0930.md @@ -0,0 +1,13 @@ +# FIX PLAN: PDF export download revoked at once (WebKit may cancel it) + +Root cause: `apps/app/src/app/routes/EditorRoute.tsx` onExport (lines 239-245) builds a detached ``, clicks it and calls `URL.revokeObjectURL(url)` on the next line. WebKit (Safari, iOS) can cancel a download whose blob URL is revoked before the download starts reading it; Firefox historically ignored clicks on detached anchors. `apps/app/src/lib/download.ts` `downloadJson` (R3, BDR-055) already attaches the anchor while clicking and revokes after `REVOKE_DELAY_MS = 30_000`. + +Edit: +1. `lib/download.ts`: new exported `downloadBlob(blob: Blob, fileName: string): void` = create URL, anchor with `href` and `download`, `document.body.appendChild(a)`, `a.click()`, `a.remove()`, `setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS)`. `downloadJson(raw, stem)` becomes `downloadBlob(new Blob([raw], { type: 'application/json' }), `${stem}-${todayIso()}.json`)`; its doc comment keeps the lone-surrogate note, the WebKit note moves to `downloadBlob`. +2. `EditorRoute.tsx`: the seven lines become `downloadBlob(new Blob([pdf as BlobPart], { type: 'application/pdf' }), `bilan-${report.animal.name}-${report.date}.pdf`)`, import from `@/lib/download`. Warnings alert and error handling unchanged (the alert still follows the click synchronously, as today). +3. Test `routes.EditorRoute.export.test.tsx` case "downloads bilan--.pdf through a blob URL it then revokes": fake timers with `shouldAdvanceTime: true` for that case; the click spy also records `this.isConnected`; assert connected at click, not revoked right after the download, revoked with `blob:faunosteo-test` after advancing 30,000 ms. + +## Challenge summary (correctness SOLID, robustness SOLID, simplicity SOLID; minors folded) +- Test step 3, closed: keep the `downloads` array as is; add a separate `clicked: HTMLAnchorElement[]` filled by the click spy (cleared in beforeEach). In the revoke case: `vi.useFakeTimers({ shouldAdvanceTime: true })` BEFORE `mountAndExport`; after the download, assert `clicked[0].isConnected` was true at click (record it in the spy as `connectedAtClick`), `document.querySelector('a[download]')` is null, `URL.revokeObjectURL` not called; then `vi.advanceTimersByTime(30_000)` and assert it was called with `blob:faunosteo-test`. Add `vi.useRealTimers()` to the file's afterEach. +- `downloadJson` criterion 4 (name `-.json`, type, 30 s revoke) is checked by reading the code and by the existing coreStopped / heldSession suites; no scope widening. +- WebKit: unit tests prove the code shape only. The premise is checked with a Playwright WebKit reproduction (immediate vs delayed revoke) and the fix is validated only by the owner's iPhone run (BDR-057 OD-6): several views, the warnings-alert path, the download prompt left open over 30 s. If that run still fails, the next suspect is the lost user activation between the click and `a.click()` after `await exportReportToPdf`. diff --git a/CHANGELOG.md b/CHANGELOG.md index e06f5c73..6f3cec8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ Versions follow [Semantic Versioning](https://semver.org/). ## [Unreleased] +### Fixed (2026-10-18) + +- Improve Safari/iOS PDF download reliability by attaching the download link to the document during activation and delaying object URL revocation by 30 seconds. The file name and content remain unchanged. + ### Changed (Rust R3, anatomy core, 2026-10-15; validated by the owner's LAN acceptance and merged 2026-10-17) - **The editor canvas draws what the Rust core composes** (`crates/core/src/anatomy`): one SVG reader for every drawing the product shows (bounds checked by a markup scanner before any XML parser, the engine's allowlist matched the same way, a reference graph refusing cycles and expansions over 50,000 elements, a serialiser that re-reads to the same tree), the annotation layer and the view composition, reached through the new `view.canvas` operation and memoised per view. The canvas keeps its in-place selection styling, click and tooltip delegation. The browser-side denylist and the app's dependency on the TypeScript renderer are gone; the PDF engine and the curation pipeline keep the TypeScript renderer until R4 and R5. diff --git a/apps/app/src/app/routes/EditorRoute.tsx b/apps/app/src/app/routes/EditorRoute.tsx index 62dd0dc0..525f99a2 100644 --- a/apps/app/src/app/routes/EditorRoute.tsx +++ b/apps/app/src/app/routes/EditorRoute.tsx @@ -33,6 +33,7 @@ import type { SheetIndex } from '@/services/sheetIndex' import { pickLabel } from '@/i18n/pickLocaleLabel' import type { ViewStates } from '@/anatomy/drawing' import { exportReportToPdf } from '@/pdf/export' +import { downloadBlob } from '@/lib/download' import type { ExportWarning } from '@faunosteo/pdf' import type { RebaseOutcome } from '@faunosteo/core-wasm/types' import { useT } from '@/i18n/useT' @@ -236,13 +237,10 @@ export default function EditorRoute() { const { pdf, warnings } = await exportReportToPdf(report, index, { locale, }) - const blob = new Blob([pdf as BlobPart], { type: 'application/pdf' }) - const url = URL.createObjectURL(blob) - const a = document.createElement('a') - a.href = url - a.download = `bilan-${report.animal.name}-${report.date}.pdf` - a.click() - URL.revokeObjectURL(url) + downloadBlob( + new Blob([pdf as BlobPart], { type: 'application/pdf' }), + `bilan-${report.animal.name}-${report.date}.pdf`, + ) const missing = missingItemsOf(warnings) if (missing.length) { window.alert( diff --git a/apps/app/src/lib/download.ts b/apps/app/src/lib/download.ts index e19ec8e1..009fa9f4 100644 --- a/apps/app/src/lib/download.ts +++ b/apps/app/src/lib/download.ts @@ -3,18 +3,28 @@ import { todayIso } from '@/lib/dates' const REVOKE_DELAY_MS = 30_000 /** - * Saves `raw` as `-.json`. A lone surrogate in a JSON.stringify output is a - * `\uXXXX` escape, so the UTF-8 Blob is lossless. This copy may be the last one before a - * deletion: the anchor is attached while clicked and the URL revoked later, since some - * engines (WebKit) cancel a download whose URL is revoked at once. + * Saves `blob` under `fileName`. The anchor is attached while clicked and the URL revoked + * later, since some engines (WebKit) cancel a download whose URL is revoked at once. */ -export function downloadJson(raw: string, stem: string): void { - const url = URL.createObjectURL(new Blob([raw], { type: 'application/json' })) +export function downloadBlob(blob: Blob, fileName: string): void { + const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url - a.download = `${stem}-${todayIso()}.json` + a.download = fileName document.body.appendChild(a) a.click() a.remove() setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS) } + +/** + * Saves `raw` as `-.json`. A lone surrogate in a JSON.stringify output is a + * `\uXXXX` escape, so the UTF-8 Blob is lossless. This copy may be the last one before a + * deletion. + */ +export function downloadJson(raw: string, stem: string): void { + downloadBlob( + new Blob([raw], { type: 'application/json' }), + `${stem}-${todayIso()}.json`, + ) +} diff --git a/apps/app/tests/unit/routes.EditorRoute.export.test.tsx b/apps/app/tests/unit/routes.EditorRoute.export.test.tsx index c1bd4e8c..7d20d875 100644 --- a/apps/app/tests/unit/routes.EditorRoute.export.test.tsx +++ b/apps/app/tests/unit/routes.EditorRoute.export.test.tsx @@ -31,6 +31,7 @@ const PDF_BYTES = new Uint8Array([37, 80, 68, 70]) let sheet: SheetIndex const downloads: string[] = [] +const clicked: { connectedAtClick: boolean }[] = [] const realCreateObjectURL = URL.createObjectURL const realRevokeObjectURL = URL.revokeObjectURL @@ -48,17 +49,20 @@ beforeEach(() => { .hydrateImport({ report: minimalReport(sheet), archive: null }, sheet) exportMock.mockReset() downloads.length = 0 + clicked.length = 0 URL.createObjectURL = vi.fn(() => 'blob:faunosteo-test') URL.revokeObjectURL = vi.fn() vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function ( this: HTMLAnchorElement, ) { downloads.push(this.download) + clicked.push({ connectedAtClick: this.isConnected }) }) vi.stubGlobal('alert', vi.fn()) }) afterEach(() => { cleanup() + vi.useRealTimers() vi.restoreAllMocks() vi.unstubAllGlobals() URL.createObjectURL = realCreateObjectURL @@ -94,7 +98,8 @@ describe('EditorRoute export', () => { expect(exportMock.mock.calls[0]![2]).toEqual({ locale: 'en' }) }) - it('downloads bilan--.pdf through a blob URL it then revokes', async () => { + it('downloads bilan--.pdf through a blob URL it revokes after a delay', async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }) exportMock.mockResolvedValue({ pdf: PDF_BYTES, warnings: [] }) await mountAndExport(fr['editor.export']!) await waitFor(() => expect(downloads).toHaveLength(1)) @@ -102,6 +107,10 @@ describe('EditorRoute export', () => { expect(URL.createObjectURL).toHaveBeenCalledTimes(1) const blob = vi.mocked(URL.createObjectURL).mock.calls[0]![0] as Blob expect(blob.type).toBe('application/pdf') + expect(clicked[0]!.connectedAtClick).toBe(true) + expect(document.body.querySelector('a[download]')).toBeNull() + expect(URL.revokeObjectURL).not.toHaveBeenCalled() + vi.advanceTimersByTime(30_000) expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:faunosteo-test') expect(window.alert).not.toHaveBeenCalled() }) diff --git a/docs/development/RUST.md b/docs/development/RUST.md index bcb1e586..61f4df72 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -662,9 +662,9 @@ application itself builds. banner until a write succeeds; a refused archive write at a new session removes the replaced session's archive rather than leave it paired with the new draft, and shows the same banner (the tab's copy lacks it). The - rescue download keeps its object URL alive 30 s (WebKit cancels a - download whose URL is revoked at once; the PDF export still does that, - follow-up). + rescue download and the PDF export keep their object URL alive 30 s + (WebKit cancels a download whose URL is revoked at once; both go through + `downloadBlob`, `apps/app/src/lib/download.ts`). 4. A core that cannot load leaves the editor on the degraded notice (before R2 a failed sheet chunk left it on "loading"). 5. Home and `/new` no longer load the sheet into memory on mount.