📚 Separate change application and validation guidance

Split common file change and repair rules into focused memories and
link each topic directly from the common module guide.

AI-assisted-by: gpt-6.1-terra
This commit is contained in:
Andrey Antukh 2026-11-24 08:59:55 +00:00
parent 8506b916b5
commit 7a8429cc12
6 changed files with 20 additions and 35 deletions

View File

@ -0,0 +1,8 @@
# Common change application
- Start at `app.common.files.changes` and the shape-tree helpers. `process-changes` validates the change vector once by default, reduces it and then processes collected touched changes; `verify? false` is for callers that already validated. A touched shape in a main instance can mark its component modified in the second pass.
- `:set` delegates to `ctn/set-shape-attr`; `:assign` decodes attrs before applying per-attr sets. `:position-data` is derived and never touched. Geometry and content-path comparisons use approximate equality for touched purposes, but width and height are excluded from the `is-geometry?` branch.
- `:mov-objects` and `:reorder-children` protect children inside component copies unless `allow-altering-copies` is set by a sync or swap flow. New structural change types must preserve that boundary.
- `cls/generate-delete-shapes` propagates deletion from inside a component main to referencing copies transitively across the file, preventing dangling `shape-ref`s. It skips this when deleting the main root itself or when `allow-altering-copies` is set.
- `shape-tree/add-shape` falls back invalid parents or frames to root, keeps parent `:shapes` a vector without duplicate IDs, and clears a copy parent's `:remote-synced` unless `ignore-touched`. `delete-shape` removes descendants from the objects map as well as unlinking the parent; render-wasm deletion has different undo internals.
- Page-object maps can carry metadata indexes. Rebuild them with `start-page-index` / `update-page-index` as appropriate; frontend commit application calls `ctst/update-object-indices` after page changes. For change construction and undo see `mem:common/changes-architecture`; for validation and repair see `mem:common/file-validation`.

View File

@ -4,4 +4,4 @@
- Compaction must preserve redo and undo order: merge `:reg-objects` per container and put the registrations after the changes that resize the children. Never compact across commit boundaries; the backend can reject individual commits by `commit-id`. See `mem:frontend/workspace-state-persistence-subtleties` for the save order.
- Don't send a deleted component's entire object map with a metadata-only edit. Scope shape edits to `:component-id` using the builder's container context. In component metadata, omitted `:objects` preserves stored shapes; explicit nil clears them. Reserve `:objects? true` for repairs that intentionally move or clear the whole map.
- Apply fixture edits through `app.common.test-helpers.files/apply-changes` so tests exercise the production applier. Disable fixture validation only for intentionally invalid intermediate states. See `mem:common/testing`.
- For touched groups see `mem:common/component-data-model`; for the second pass and validation see `mem:common/file-change-validation-migration-subtleties`. For live inspection of emitted changes, see `mem:common/component-debugging-recipes`.
- For touched groups see `mem:common/component-data-model`; for the second pass see `mem:common/change-application` and for validation see `mem:common/file-validation`. For live inspection of emitted changes, see `mem:common/component-debugging-recipes`.

View File

@ -27,7 +27,7 @@ Masters are not normally touched through `set-shape-attr`, but touched flags can
## Swap slots and positional matching
- A swap slot (stored via `ctk/set-swap-slot`, a `:touched` group `swap-slot-<uuid>`) marks a copy sub-head that was SWAPPED to another component; `compare-children` then pairs it to the main child by slot instead of by `shape-ref`.
- Copy sub-heads without a slot are paired to main children by `shape-ref` (seek, not index). `find-near-match` (positional) is only a validator/repair heuristic; validity requires membership of the ref among the near-main parent's children, not index equality (`mem:common/file-change-validation-migration-subtleties`).
- Copy sub-heads without a slot are paired to main children by `shape-ref` (seek, not index). `find-near-match` (positional) is only a validator/repair heuristic; validity requires membership of the ref among the near-main parent's children, not index equality (`mem:common/file-validation`).
- Copy child ORDER converges to the main's via the async sync (`moved` branch of `compare-children`); local code must never reorder copy children directly (guards in `:mov-objects`/`:reorder-children`).
## Cloning paths
@ -44,4 +44,4 @@ A variant container is a frame with `:is-variant-container true`. Its children a
Predicates are broad: `ctk/is-variant?` checks `:variant-id` and applies to both variant master shapes and component rows; `ctk/is-variant-container?` checks the container shape flag.
Moving/dropping a shape into a variant container through the move-to-frame path can auto-convert it into a variant via `generate-make-shapes-variant`, which may duplicate the underlying component. Treat drag/drop into variant containers as a component/variant operation, not a plain reparent.
Moving/dropping a shape into a variant container through the move-to-frame path can auto-convert it into a variant via `generate-make-shapes-variant`, which may duplicate the underlying component. Treat drag/drop into variant containers as a component/variant operation, not a plain reparent.

View File

@ -35,7 +35,7 @@ Geometry and layout:
Change pipeline, validation, and migrations:
- Change records, undo/redo architecture, changes-builder API, and production-path mutation guidance: `mem:common/changes-architecture`.
- Change application, shape-tree edits, validation/repair, and second-pass touched behavior: `mem:common/file-change-validation-migration-subtleties`.
- Change application, shape-tree edits and second-pass touched behavior: `mem:common/change-application`. File validation and repair: `mem:common/file-validation`.
- Adding, renumbering or replacing a file migration: `mem:common/file-migrations`.
Components, variants, and debugging:

View File

@ -1,31 +0,0 @@
# Common File Change, Validation, and Migration Subtleties
## Change application
- `process-changes` validates the whole change vector once by default, reduces changes, then performs a second pass for collected touched changes. Callers that already validated can pass `verify? false`.
- `process-operation :set` delegates to `ctn/set-shape-attr`; `:assign` first decodes attrs with the shape-attrs JSON transformer and then emits per-attr set operations.
- `set-shape-attr` treats `:position-data` as derived and never touched. Geometry/content-path changes use approximate equality; geometry differences under about 1px can be ignored for touched purposes.
- Width/height are excluded from the `is-geometry?` branch in `set-shape-attr`; do not assume all geometry-group attrs follow identical ignore-geometry behavior.
- `process-touched-change` marks the owning component modified when a touched shape belongs to a main instance; component-data changes can come from shape ops through this second pass.
- Copy structure is guarded at change application: `:mov-objects` (`is-valid-move?`) and `:reorder-children` both refuse to alter children of shapes inside component copies unless the change carries `allow-altering-copies` (sync/swap flows set it). New structural change types must follow the same rule.
- `cls/generate-delete-shapes` propagates deletions from INSIDE a component main to the copy shapes referencing them (transitively, all pages of the file) so no dangling `shape-ref`s remain; skipped when the main root itself is deleted (copies then resolve into the deleted component) and for `allow-altering-copies` flows (swap replaces the shape; sync reconciles).
## Shape tree edits
- `shape-tree/add-shape` falls back invalid/missing parent or frame ids to root (`uuid/zero`), ensures parent `:shapes` is a vector, avoids duplicate child ids, and clears `:remote-synced` on copy parents unless `ignore-touched` is true.
- `shape-tree/delete-shape` removes the shape and all descendants from the objects map and removes the id from its parent. This is different from render-wasm deletion, which may keep deleted children for undo/redo internals.
- Page object maps can carry metadata indexes such as cached frame lists. `start-page-index` / `update-page-index` rebuild those metadata indexes; `frontend` commit application calls `ctst/update-object-indices` after page changes.
## Validation and repair
- Full referential/semantic validation currently runs only when file features contain `"components/v2"`.
- Validation starts at root plus orphan shapes, then validates component records. `validate-file!` raises `:validation :referential-integrity` with collected details.
- `update-file` validates per save with the change-scoped variants: `validate-file-affected!` (referential, whole touched pages/components) and `validate-file-schema-affected!` (file schema with `:pages-index` reduced to the touched pages; non-page data checked whole). The schema variant maps change types to pages itself (`touched-page-ids`): a new change type that writes a page must be added there, or to `file-level-change-types` if it touches no page; an unknown type makes it check the whole file. The whole-file schema check loads and decodes every page, ~75–80% of a save on multi-page files.
- `repair-file` does not mutate data directly; it reduces validation errors into redo changes using `changes-builder`. Callers must apply or persist those changes.
- `repair-file-until-valid` stops on: no errors; unchanged `:data` after apply (backend page/`PointerMap` `=` is pointer identity); stable error fingerprint `(frequencies (map #(dissoc % :shape) errors))` vs previous pass (check fingerprint before `repair-file`/`process-changes`); or `max-iterations`. Each `repair-error` builds from the original `file-data`, so N `:set`-based errors on one shape take N passes.
- `:ref-shape-not-found` repair: when the copy root's local component has a `:main-instance-id` that resolves to no shape but a `:main-instance` shape of that component sits on its `:main-instance-page`, it emits `mod-component` pointing the id back to that main (keeps the copy linked; a repair scoped to the copy's page never sees the main's `:invalid-main-instance-id`). Otherwise near-main reassign, else detach the copy subtree.
- `:missing-slot` fires only for a REAL swap: a copy sub-head whose `shape-ref` is no longer a child of the near main parent. A pure positional mismatch (ref still a sibling elsewhere) is a reorder — valid, realigned by the async component sync; do not "repair" it by assigning swap slots (a slot freezes the child out of normal sync). `fix-missing-swap-slots` (migration 0019) follows the same membership rule.
## Migrations
For migration IDs, ordering and compatibility with previously saved files, see `mem:common/file-migrations`.

View File

@ -0,0 +1,8 @@
# Common file validation and repair
- Start at `app.common.files.validate` and `app.common.files.repair`. Full referential and semantic validation runs when file features include `"components/v2"`; it visits the root and orphan shapes, then component records. `validate-file!` raises `:validation` / `:referential-integrity` with details.
- `update-file` uses change-scoped referential and schema validation. `touched-page-ids` selects pages for the schema check while non-page data is checked whole. Register a new change type as page-writing or file-level; an unknown type falls back to validating the whole file, which loads every page.
- `repair-file` produces redo changes rather than mutating data; callers must apply or persist them. `repair-file-until-valid` stops on no errors, unchanged data, a stable error fingerprint without `:shape`, or its iteration limit. Check the fingerprint before another repair pass; backend `PointerMap` equality is pointer identity. Multiple `:set` repairs on one shape can require multiple passes because each repair starts from the original file data.
- For `:ref-shape-not-found`, repair can restore a local component's `:main-instance-id` when the recorded main is missing but the matching main exists on its recorded page. Otherwise it tries a near-main reassignment or detaches the copy subtree.
- `:missing-slot` is a real swap only when a copy sub-head's `shape-ref` is no longer a child of the near main parent. A sibling in a different position is a reorder, not a swap: assigning a slot would block normal component sync. Migration 0019 follows the same membership rule; see `mem:common/file-migrations`.
- For change application see `mem:common/change-application`; for copy and slot semantics see `mem:common/component-data-model`.