From fb271c00e5feaf939a2d656f7fa1e87eacbf8c8b Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Sun, 15 Nov 2026 18:01:12 +0100 Subject: [PATCH] :paperclip: Keep the frontend replay's library bookkeeping as the model's The frontend interpreter recorded three library steps unlike the pure runner, so three replayed programs differed from the model with no difference in production: - A detach leaves a nested copy's child plain when the loaded library no longer has its main; the interpreter now records those children, as the detach node does, and the oracle reads each as its main's child. - A duplicate takes the reference inputs of the shapes it copies, so a duplicate of a copy back on an older library holds that version. - A library sync remembers its copies against the synced library as loaded and the other libraries as accepted. The detach node and the interpreter share `n/record-unlinked-children`. Frontend generated tests replay the three programs. Harness only; the benchmark is not run. AI-assisted-by: claude-opus-5-5 --- .../test_helpers/composable/comp/nodes.cljc | 20 ++++++++++++------- .../composable_tests/generated_test.cljs | 19 ++++++++++++++++++ .../composable_tests/interpreter.cljs | 20 +++++++++++++------ 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/common/src/app/common/test_helpers/composable/comp/nodes.cljc b/common/src/app/common/test_helpers/composable/comp/nodes.cljc index 836baa9c42..3d447ba93f 100644 --- a/common/src/app/common/test_helpers/composable/comp/nodes.cljc +++ b/common/src/app/common/test_helpers/composable/comp/nodes.cljc @@ -1097,6 +1097,17 @@ ;; one nesting level up (`mem:common/component-sync-contract`). ;; --------------------------------------------------------------------------- +(defn record-unlinked-children + "Record the copy children under the detached head `shape-id` that the + detach left as plain shapes: their parent stays linked, but the loaded + library no longer has their main." + [situation shape-id] + (let [objects (:objects (thf/current-page (tm/file situation))) + unlinked (filter #(and (nil? (get-in objects [% :shape-ref])) + (some? (get-in objects [(get-in objects [% :parent-id]) :shape-ref]))) + (cfh/get-children-ids objects shape-id))] + (update situation ::unlinked-children (fnil into #{}) unlinked))) + (defrecord Detach [target] IComponentOperation (op-kind [_] :user) @@ -1111,15 +1122,10 @@ (:data the-file) (:id page) (libraries situation)) - file' (thf/apply-changes the-file changes :validate? false) - objects (:objects (thf/current-page file')) - ;; copy children whose main the loaded library no longer has - unlinked (filter #(and (nil? (get-in objects [% :shape-ref])) - (some? (get-in objects [(get-in objects [% :parent-id]) :shape-ref]))) - (cfh/get-children-ids objects shape-id))] + file' (thf/apply-changes the-file changes :validate? false)] (-> situation (tm/with-file file') - (update ::unlinked-children (fnil into #{}) unlinked) + (record-unlinked-children shape-id) (record-changes changes) (tm/record-application this {:target target :detached shape-id}))))) diff --git a/frontend/test/frontend_tests/composable_tests/generated_test.cljs b/frontend/test/frontend_tests/composable_tests/generated_test.cljs index f3ffa3945a..adf5f5eaa4 100644 --- a/frontend/test/frontend_tests/composable_tests/generated_test.cljs +++ b/frontend/test/frontend_tests/composable_tests/generated_test.cljs @@ -1118,6 +1118,25 @@ :ops [[:rotate 16 0 7 0] [:grid-direction 0 0 0 1] [:grid-cell 0 10 0 1] [:add-child 0] [:structure 3 6 15] [:grid-direction 0 0 0 1] [:grid-cell 0 4 2 1]]}))) +(t/deftest ^:async generated-a-detach-under-an-unaccepted-child-removal-leaves-it-plain + ;; the loaded library no longer has the nested copy's child main, so the + ;; detach leaves that child plain until the consumer accepts the library + (await (replay {:world [2 0 {:variants true :stale-libraries true :library true :rich-variants true}] + :ops [[:library-delete 0 0 0 0] [:library-restore 0 0 0 0] [:library-remove-child 3 0 0 0] + [:detach 11 0 0 0] [:library-undo] [:library-redo]]}))) + +(t/deftest ^:async generated-a-duplicate-of-a-copy-back-on-its-older-library-keeps-it + ;; after an undone library sync, a duplicate of a copy holds the values of + ;; the library version the copy is back on + (await (replay {:world [2 0 {:variants true :library-variants true :stale-libraries true :library true}] + :ops [[:library-edit 2 0 0] [:library-sync] [:undo 0 0 0 0] [:duplicate 15 0 0 0]]}))) + +(t/deftest ^:async generated-a-library-sync-keeps-another-librarys-pending-turn-out + ;; a sync from one library takes it as loaded and keeps the version of the + ;; other library the consumer had accepted + (await (replay {:world [2 1 {:library true :stale-libraries true :library-dependency true :library-text true}] + :ops [[:swap 4 0 0 0] [:swap 9 0 0 0] [:library-transform 5 3 4 0] [:library-sync 0 0 0 0]]}))) + (t/deftest ^:async generated-a-token-value-edit-and-its-propagation-undo-together ;; the token form commits the edit and its propagation as one undo step (await (replay {:world [1 0 {:tokens true :variants true :rich-variants false :text false diff --git a/frontend/test/frontend_tests/composable_tests/interpreter.cljs b/frontend/test/frontend_tests/composable_tests/interpreter.cljs index 173e57bf4e..912011f4b2 100644 --- a/frontend/test/frontend_tests/composable_tests/interpreter.cljs +++ b/frontend/test/frontend_tests/composable_tests/interpreter.cljs @@ -967,7 +967,9 @@ [situation op] (let [index-before (undo-index) items-before (get-in @st/state [:workspace-undo :items]) - after (await (run-ops situation [op]))] + after (cond-> (await (run-ops situation [op])) + (instance? n/Detach op) + (some-> (n/record-unlinked-children (tm/target-shape-id situation (:target op)))))] (if-not (and after (:accepted-libraries situation)) after (let [index-after (undo-index) @@ -1005,11 +1007,13 @@ (cf/remember-copies file-id file-id dependents authored))) (instance? n/SyncFromLibrary op) - (let [source (or (:source op) (n/library-id situation))] - (-> after - (assoc-in [:accepted-libraries file-id source] (get inputs source)) - (cf/remember-copies file-id source - (set (keys (get-in inputs [source :data :components]))) inputs))) + ;; the copies take this library as loaded and keep the + ;; versions of the others they had accepted + (let [source (or (:source op) (n/library-id situation)) + after (assoc-in after [:accepted-libraries file-id source] (get inputs source))] + (cf/remember-copies after file-id source + (set (keys (get-in inputs [source :data :components]))) + (merge inputs (get-in after [:accepted-libraries file-id])))) (or (instance? n/ResetOverrides op) (instance? n/SwapShape op) @@ -1020,6 +1024,10 @@ (n/switch-value situation op)))) (cf/remember-subtree after file-id (tm/target-shape-id situation (:target op)) inputs) + (instance? n/Duplicate op) + (cf/inherit-references after file-id (tm/target-shape-id situation (:target op)) + (:new-head (n/applied-result op (tm/file situation) (tm/file after)))) + :else after) patches (get-reference-patches (get-generated-references situation) (get-generated-references after)) item (get items index-after)