diff --git a/.serena/memories/frontend/workspace-state-persistence-subtleties.md b/.serena/memories/frontend/workspace-state-persistence-subtleties.md index ef2eae493c..5e11d062b7 100644 --- a/.serena/memories/frontend/workspace-state-persistence-subtleties.md +++ b/.serena/memories/frontend/workspace-state-persistence-subtleties.md @@ -42,7 +42,7 @@ - `leave-signals` (document `visibilitychange`→hidden, window `blur`) emits `persist-buffered`, which forces a save when `[:persistence :buffer]` is not empty, so leaving the tab/window sends the 3 s batch at once; tests mock `dps/leave-signals` BEFORE `initialize-persistence` runs. pagehide/beforeunload are too late for an async save. - Exports (exporter and client/WASM PDF/SVG/PNG alike), the dashboard `dd/duplicate-files` and the `.penpot` download call `force-persist-files-and-wait file-ids timeout` first. A client export draws local data, so its warning says the export includes unsaved changes (`workspace.export.unsaved-changes-included`); exporter exports and the duplicate copy server data and warn they miss them. `duplicate-files` owns its one toast, shown once every copy exists: the plural `dashboard.duplicate-unsaved-changes` warning when the files had unsaved changes before the copy request, else the success toast; a failed copy errors the stream, so neither shows (the file menu must not emit its own success toast, the single-toast store would let it replace the warning). - Exports and the `.penpot` download call `force-persist-files-and-wait file-ids timeout` (waits only for those files, ends on `:error` or timeout, never raises) and then `unsaved-changes? state file-ids` to warn; it reads `refs/persistence`, so tests mock it with `(l/derived :persistence store)`. -- `persistence-failed` emits a `save-failed` audit event (`:cause-type :code :retrying :queued-commits`) whenever the user is notified (`notify-failure?`, same rule as the toast), including delegated auth failures. +- `save-failed` audit event (built by `save-failed-event`): always `:code :file-id :error-codes :cause-type :retrying :queued-commits`; `:error-codes` is the sorted distinct `:code` of the error's validation `:details` (nil when none). Two emitters: `persistence-failed` (`queue-failed-event`) whenever the user is notified (`notify-failure?`, same rule as the toast), including delegated auth failures, with `:file-id` from the failed commit; and `rollback-rejected` (`rejected-event`) when the server rejects commits (partial or full) and a rollback plan exists, reading `:type`/`:code`/`:details` from the `:rejected :error` map, with `:retrying false` and `:rejected-commits` (count). A stale rejected commit (`stale-commit?`, no plan) emits nothing. - `start-undo-transaction` stamps the transaction with a fresh `:commit-group` (dropped from the history entry); `commit-changes` uses `(or undo-group (:undo-group tx) (:commit-group tx) (uuid/next))`, so all commits of one transaction are one rollback unit. `` (`get-stacked-undo-group`) is the `:undo-group` of the entry at the current undo index, taken only by a `:stack-undo? true` commit that records undo (`save-undo?` and non-empty undo changes, the history watcher's test) and only when the open transaction holds no changes (else `append-undo` accumulates it into the transaction): the commits it starts (component sync after a main text resizes) inherit it, so one Ctrl+Z reverts the whole action. Heals (`:stack-undo? true :save-undo? false`) never take it, or a rejected heal would roll back the user's last action. Known limit: a late stacked commit (font-load resize) that lands while an unrelated user transaction is open but still empty (colorpicker mousedown) takes the previous entry's group; its sync reuses that transaction (`start-undo-transaction` keeps an open one) and sets the transaction's `:undo-group`, so the user's next action undoes together with the previous one. Rare and loses no data; left as is. Pre-setting `:undo-group` instead breaks entries whose group is set later (sync transactions). - Undo transactions can stay open only temporarily; timed-out pending transactions are force-committed after about 20s. Undo entries are capped at 50. - Undo/redo are ignored while a normal editor/drawing interaction is active, except grid-layout edition handles undo through this path. `undo`, `redo` and `undo-to-index` also do nothing while the save lock holds: `materialize-undo` would move the index while the commit is refused. diff --git a/frontend/src/app/main/data/persistence.cljs b/frontend/src/app/main/data/persistence.cljs index 245afddf88..00da5d325e 100644 --- a/frontend/src/app/main/data/persistence.cljs +++ b/frontend/src/app/main/data/persistence.cljs @@ -491,15 +491,41 @@ (not (and (:recovering pstate) (slow-retry-pending? pstate)))) +(defn- error-codes + "Sorted distinct codes of the validation details of `error`, or nil." + [{:keys [details]}] + (some->> (seq details) (keep :code) distinct sort vec not-empty)) + (defn- save-failed-event "Audit event for a save failure the user is told about, so failed saves - can be counted." - [{:keys [error queue] :as pstate}] - (ev/event {::ev/name "save-failed" - :cause-type (:cause-type error) - :code (:code error) - :retrying (slow-retry-pending? pstate) - :queued-commits (count queue)})) + can be counted and split by file and by validation code. `props` adds + the fields of each kind of failure." + [error file-id props] + (ev/event (assoc props + ::ev/name "save-failed" + :code (:code error) + :file-id file-id + :error-codes (error-codes error)))) + +(defn- queue-failed-event + "`save-failed` event of a queue halted by `error` in `pstate`." + [{:keys [error queue index] :as pstate}] + (save-failed-event error + (or (dm/get-in index [(:commit-id error) :file-id]) + (:file-id error)) + {:cause-type (:cause-type error) + :retrying (slow-retry-pending? pstate) + :queued-commits (count queue)})) + +(defn- rejected-event + "`save-failed` event of the commits of `file-id` that the server + `rejected`: they are undone, so they count as a failed save." + [state file-id {:keys [error commit-ids]}] + (save-failed-event error file-id + {:cause-type (:type error) + :retrying false + :rejected-commits (count commit-ids) + :queued-commits (count (dm/get-in state [:persistence :queue]))})) (defn- end-stopped-slow-cycle "Clears `:recovering` when the slow cycle no longer resends the failure: @@ -539,7 +565,7 @@ (rx/of (ptk/data-event ::error cause) (ntf/hide :tag reconnecting-tag)) (if (notify-failure? pstate) - (rx/of (save-failed-event pstate)) + (rx/of (queue-failed-event pstate)) (rx/empty)) ;; A transport failure that outlasts the backoff can still pass, so ;; the queue keeps trying at a slow pace. @@ -846,7 +872,9 @@ ptk/WatchEvent (watch [it state stream] (if-let [plan @plan] - (apply-rollback-plan it state stream plan) + (rx/concat + (rx/of (rejected-event state file-id rejected)) + (apply-rollback-plan it state stream plan)) (rx/empty))) ptk/EffectEvent diff --git a/frontend/test/frontend_tests/data/persistence_test.cljs b/frontend/test/frontend_tests/data/persistence_test.cljs index 544a41ee03..e9a482686c 100644 --- a/frontend/test/frontend_tests/data/persistence_test.cljs +++ b/frontend/test/frontend_tests/data/persistence_test.cljs @@ -1233,12 +1233,49 @@ (t/is (= 1 (count @events))) (t/is (= {:cause-type :validation :code :referential-integrity + :file-id file-id + :error-codes [:missing-slot :parent-not-found] :retrying false :queued-commits 1} (select-keys (first @events) - [:cause-type :code :retrying :queued-commits]))))) + [:cause-type :code :file-id :error-codes + :retrying :queued-commits]))))) (fn [_ _] - (rx/throw (ex-info "invalid" {:type :validation :code :referential-integrity})))))) + (rx/throw (ex-info "invalid" {:type :validation + :code :referential-integrity + :details [{:code :parent-not-found} + {:code :missing-slot} + {:code :parent-not-found}]})))))) + +;; Scenario: the server keeps the valid commits of a save and rejects one +;; as invalid data. Proves: the undone commit is counted as a failed save, +;; with its file and validation codes, while the queue keeps going. +(t/deftest ^:async a-rejected-commit-emits-a-save-failed-event + (await + (with-persistence + (^:async fn [{:keys [file-id response store]}] + (let [events (save-failed-events store) + page-id (uuid/next) + shape-id (uuid/next) + commit (thp/rename-commit file-id page-id shape-id "A" "B")] + (ptk/emit! store (thp/open-file file-id page-id (thp/file-data page-id shape-id "B"))) + (ptk/emit! store commit ::dps/force-persist) + (rx/push! response (assoc-in (thp/rejection (:id @commit)) + [:rejected :error :details] + [{:code :missing-slot}])) + (await (async/wait-for #(seq @events) "the event is emitted")) + (await (async/settle)) + (t/is (= 1 (count @events))) + (t/is (= {:cause-type :validation + :code :referential-integrity + :file-id file-id + :error-codes [:missing-slot] + :retrying false + :rejected-commits 1} + (select-keys (first @events) + [:cause-type :code :file-id :error-codes + :retrying :rejected-commits]))) + (t/is (not= :error (get-in @store [:persistence :status])))))))) ;; Buffered commits.