diff --git a/.claude/skills/fix-issue/findings/mdl-executor/2026-11-25-layout-flows-carries-stale-loop-size.json b/.claude/skills/fix-issue/findings/mdl-executor/2026-11-25-layout-flows-carries-stale-loop-size.json new file mode 100644 index 000000000..e1e6553d4 --- /dev/null +++ b/.claude/skills/fix-issue/findings/mdl-executor/2026-11-25-layout-flows-carries-stale-loop-size.json @@ -0,0 +1 @@ +{"area": "mdl/executor", "date": "2026-11-25", "symptom": "`mxcli layout flows` on a flow whose loop had been written by the pre-#1935 builder kept the stale loop Size (e.g. 710;480 where the body fits 320;160): it reported `moved 4 of 5 objects` while bson dump showed the Size unchanged, and a later --dry-run reported the same objects as needing to move again.", "cause": "Two independent carries. (1) stripFlowLayout cleared @position/@anchor/@curve/@merge/@start but not @size, so DESCRIBE re-emitted the STORED loop box and the rebuild reused it verbatim. (2) canon.CarryUnexpressed/carryLoopSize takes max(written, stored) per dimension on every write (the #2179 contract, when MDL could not state a size), so even the correct smaller box was replaced by the larger stored one before equality, eliding the write as unchanged. Measured: writing Size=320;160 directly through UpdateRawUnit read back 710;480.", "file": "mdl/executor/cmd_microflows_layout.go; modelsdk/canon/unexpressed.go; modelsdk/canon/identity.go; mdl/backend/infrastructure.go; modelsdk/mpr/writer_core.go", "fix": "stripFlowLayout now also clears ActivityAnnotations.Size so the rebuild's fitContainerSize decides. Added canon.ContentsOwnLayout() to skip the loop-size carry when the caller owns the layout, and a backend write path UpdateRawUnitOwningLayout (interface, modelsdk backend, writer, mock, regenerated unimplemented/unsupported) that layoutFlow uses. Tests: mdl/executor/issue1935_layout_flows_test.go (inject stale Size, layout must replace it, second run unchanged) and canon TestUnexpressed_LoopSizeReplacingStaleBoxWhenLayoutOwnsIt, with the #2179 default-carry tests as the control.", "insight": "Two carry rules can stack: an unexpressed-state carry (canon) and a 'describe it back' annotation each restore a value the command just fixed, and elision then makes the run look clean while the write never lands. To tell them apart, write the intended bytes directly through UpdateRawUnit and read back: reverting means the carry is in canon, surviving means the describe/strip path. A write path authoritative about geometry needs an explicit canon option, not a largest-dimension heuristic."} diff --git a/mdl/backend/infrastructure.go b/mdl/backend/infrastructure.go index be92c6f85..25ded2b94 100644 --- a/mdl/backend/infrastructure.go +++ b/mdl/backend/infrastructure.go @@ -69,6 +69,17 @@ type RawUnitBackend interface { // documents replacing it, which is what stops an update destroying that // module's data. Only a caller doing that may use this. UpdateRawUnitOwningStorageGUIDs(unitID string, contents []byte) error + + // UpdateRawUnitOwningLayout is UpdateRawUnit for a write that is + // AUTHORITATIVE about the layout MDL cannot otherwise express — today a + // loop's box Size. + // + // The ordinary path keeps a loop box drawn larger than the engine's fit, so + // a rebuild never shrinks a hand-drawn one (ako/mxcli#2179). `mxcli layout + // flows` recomputes the box as its whole purpose, so it must replace a stale + // stored size instead of carrying it back (mendixlabs/mxcli#1935). Only that + // caller may use this. + UpdateRawUnitOwningLayout(unitID string, contents []byte) error } // MetadataBackend provides project-level metadata and introspection. diff --git a/mdl/backend/mcp/unsupported_gen.go b/mdl/backend/mcp/unsupported_gen.go index 5271beebb..ca730c94a 100644 --- a/mdl/backend/mcp/unsupported_gen.go +++ b/mdl/backend/mcp/unsupported_gen.go @@ -1466,6 +1466,11 @@ func (unsupportedBackend) UpdateRawUnit(_ string, _ []uint8) (err0 error) { return } +func (unsupportedBackend) UpdateRawUnitOwningLayout(_ string, _ []uint8) (err0 error) { + err0 = errUnsupported("UpdateRawUnitOwningLayout") + return +} + func (unsupportedBackend) UpdateRawUnitOwningStorageGUIDs(_ string, _ []uint8) (err0 error) { err0 = errUnsupported("UpdateRawUnitOwningStorageGUIDs") return diff --git a/mdl/backend/mock/backend.go b/mdl/backend/mock/backend.go index e520f2620..1b3efe0ed 100644 --- a/mdl/backend/mock/backend.go +++ b/mdl/backend/mock/backend.go @@ -348,6 +348,11 @@ type MockBackend struct { // moves a GUID without meaning to is the #1119 data-loss defect. UpdateRawUnitOwningStorageGUIDsFunc func(unitID string, contents []byte) error + // UpdateRawUnitOwningLayoutFunc stubs the write path that recomputes a + // loop's box (layout flows). Falls back to UpdateRawUnitFunc when unset, + // because most tests do not care about the carry rule. + UpdateRawUnitOwningLayoutFunc func(unitID string, contents []byte) error + // MetadataBackend ListAllUnitIDsFunc func() ([]string, error) ListUnitsFunc func() ([]*types.UnitInfo, error) diff --git a/mdl/backend/mock/mock_infrastructure.go b/mdl/backend/mock/mock_infrastructure.go index 51ee491b2..40e0a5108 100644 --- a/mdl/backend/mock/mock_infrastructure.go +++ b/mdl/backend/mock/mock_infrastructure.go @@ -105,6 +105,16 @@ func (m *MockBackend) UpdateRawUnitOwningStorageGUIDs(unitID string, contents [] return fmt.Errorf("MockBackend.UpdateRawUnitOwningStorageGUIDs not configured") } +func (m *MockBackend) UpdateRawUnitOwningLayout(unitID string, contents []byte) error { + if m.UpdateRawUnitOwningLayoutFunc != nil { + return m.UpdateRawUnitOwningLayoutFunc(unitID, contents) + } + if m.UpdateRawUnitFunc != nil { + return m.UpdateRawUnitFunc(unitID, contents) + } + return fmt.Errorf("MockBackend.UpdateRawUnitOwningLayout not configured") +} + // --------------------------------------------------------------------------- // MetadataBackend // --------------------------------------------------------------------------- diff --git a/mdl/backend/modelsdk/unimplemented_gen.go b/mdl/backend/modelsdk/unimplemented_gen.go index fa6a0a96e..161442173 100644 --- a/mdl/backend/modelsdk/unimplemented_gen.go +++ b/mdl/backend/modelsdk/unimplemented_gen.go @@ -1309,6 +1309,10 @@ func (unimplemented) UpdateRawUnit(_ string, _ []uint8) error { return errUnimplemented("UpdateRawUnit") } +func (unimplemented) UpdateRawUnitOwningLayout(_ string, _ []uint8) error { + return errUnimplemented("UpdateRawUnitOwningLayout") +} + func (unimplemented) UpdateRawUnitOwningStorageGUIDs(_ string, _ []uint8) error { return errUnimplemented("UpdateRawUnitOwningStorageGUIDs") } diff --git a/mdl/backend/modelsdk/units.go b/mdl/backend/modelsdk/units.go index ae1cb91a0..0dd8aaa0e 100644 --- a/mdl/backend/modelsdk/units.go +++ b/mdl/backend/modelsdk/units.go @@ -55,6 +55,13 @@ func (b *Backend) UpdateRawUnitOwningStorageGUIDs(unitID string, contents []byte return b.writer.UpdateRawUnitOwningStorageGUIDs(unitID, contents) } +// UpdateRawUnitOwningLayout writes a unit whose geometry is authoritative: +// `mxcli layout flows` recomputes a loop's box, so a stale stored Size must be +// replaced rather than carried back at its larger value (mendixlabs/mxcli#1935). +func (b *Backend) UpdateRawUnitOwningLayout(unitID string, contents []byte) error { + return b.writer.UpdateRawUnitOwningLayout(unitID, contents) +} + // ListRawUnitsByType returns every unit whose $Type has the given prefix, with // resolved raw contents — the catalog uses this for document types that have no // dedicated typed reader (e.g. JavaScript actions, data transformers). Delegates diff --git a/mdl/executor/cmd_microflows_layout.go b/mdl/executor/cmd_microflows_layout.go index 4dbe5f7b9..32222b239 100644 --- a/mdl/executor/cmd_microflows_layout.go +++ b/mdl/executor/cmd_microflows_layout.go @@ -157,7 +157,10 @@ func layoutFlow(ctx *ExecContext, kind string, name ast.QualifiedName, dryRun bo if err != nil { return res, mdlerrors.NewBackend("encode "+kind+" "+name.String(), err) } - if err := ctx.Backend.UpdateRawUnit(string(stored.id), out); err != nil { + // OwningLayout, not UpdateRawUnit: the recomputed loop box is the point of + // the command, so a stale stored Size must be replaced rather than carried + // back at its larger value (#1935). + if err := ctx.Backend.UpdateRawUnitOwningLayout(string(stored.id), out); err != nil { return res, mdlerrors.NewBackend("write "+kind+" "+name.String(), err) } return res, nil @@ -258,9 +261,13 @@ func rebuildFlowLayout(ctx *ExecContext, kind string, name ast.QualifiedName) (* // CREATE NANOFLOW statement, so the builder places everything itself. // // What goes is exactly what DESCRIBE emits to pin geometry: @position (on -// activities, parameters and notes), @anchor in all its forms, @curve, @merge and -// @start. A note keeps its size, which is its content's box rather than its -// placement; everything else in the statement is left alone. +// activities, parameters and notes), @anchor in all its forms, @curve, @merge, +// @start and a loop's @size. A note keeps its size, which is its content's box +// rather than its placement; everything else in the statement is left alone. +// +// @size has to go with the rest: DESCRIBE emits it from the STORED loop box, so +// keeping it would rebuild that box verbatim and leave a stale size (#1935) — the +// command would report a move while writing the same wrong Size back. // // Reflective because statements nest bodies in a dozen places — branches, loops, // splits' cases, error handlers — and a hand-written walk that missed one would @@ -293,6 +300,7 @@ func clearLayoutAnnotations(v reflect.Value, seen map[uintptr]bool) { ann.Curve = nil ann.Merge = nil ann.Start = nil + ann.Size = nil } clearLayoutAnnotations(v.Elem(), seen) case reflect.Interface: diff --git a/mdl/executor/issue1935_layout_flows_test.go b/mdl/executor/issue1935_layout_flows_test.go new file mode 100644 index 000000000..878dd15e9 --- /dev/null +++ b/mdl/executor/issue1935_layout_flows_test.go @@ -0,0 +1,161 @@ +// SPDX-License-Identifier: Apache-2.0 + +package executor + +import ( + "bytes" + "testing" + + "go.mongodb.org/mongo-driver/v2/bson" + + "github.com/mendixlabs/mxcli/mdl/backend" + modelsdkbackend "github.com/mendixlabs/mxcli/mdl/backend/modelsdk" + "github.com/mendixlabs/mxcli/mdl/visitor" +) + +// A loop written by an mxcli that sized its box from the body's absolute canvas +// coordinates keeps that wrong Size in the stored unit (#1935). `layout flows` +// rebuilds the flow, so it must heal the stored box rather than report a move +// that leaves it untouched - and heal it once, not on every run. +// +// The reads go through a fresh connection: a second `mxcli layout flows` is a +// new process, and this is the bug where the first run reported `moved` while +// the stored Size stayed put. +func TestLayoutFlow_RecomputesStaleLoopBox(t *testing.T) { + path := projectFixture(t) + exec := newLayoutFlowExecutor(t, path) + run(t, exec, `create microflow MyFirstModule.LoopFlow ( + $Items: List of System.User +) +begin + @start(60, 200) + @position(600, 200) + loop $Item in $Items + begin + @position(600, 300) + log info node 'Repro' $Item/Name; + end loop; + @position(1300, 200) + return; +end;`) + + name := flowQN("MyFirstModule.LoopFlow") + g, err := storedFlow(exec.newExecContext(t.Context()), "microflow", name) + if err != nil { + t.Fatal(err) + } + raw, err := exec.backend.GetRawUnitBytes(g.id) + if err != nil { + t.Fatal(err) + } + var doc bson.D + if err := bson.Unmarshal(raw, &doc); err != nil { + t.Fatal(err) + } + loop := findLoopD(doc) + if loop == nil { + t.Fatal("the created flow has no LoopedActivity") + } + if !setDString(loop, "Size", "710;480") { + t.Fatal("the loop has no Size to make stale") + } + stale, err := bson.Marshal(doc) + if err != nil { + t.Fatal(err) + } + if err := exec.backend.UpdateRawUnit(string(g.id), stale); err != nil { + t.Fatal(err) + } + if got := loopSizeOnDisk(t, newLayoutFlowExecutor(t, path), g); got != "710;480" { + t.Fatalf("control: the stale size did not land, got %q", got) + } + + res, err := exec.LayoutFlow("microflow", name, false) + if err != nil || res.Refused != "" { + t.Fatalf("layout: %v %s", err, res.Refused) + } + if got := loopSizeOnDisk(t, newLayoutFlowExecutor(t, path), g); got == "710;480" { + t.Fatalf("layout flows reported moved %d but left the stale loop Size %q", res.Moved, got) + } + + second, err := newLayoutFlowExecutor(t, path).LayoutFlow("microflow", name, false) + if err != nil { + t.Fatal(err) + } + if second.Changed() { + t.Errorf("a second run moved %d objects and %d flows after the box was healed", second.Moved, second.Flows) + } +} + +func newLayoutFlowExecutor(t *testing.T, path string) *Executor { + t.Helper() + exec := New(&bytes.Buffer{}) + exec.SetQuiet(true) + exec.SetBackendFactory(func() backend.FullBackend { return modelsdkbackend.New() }) + t.Cleanup(func() { exec.Close() }) + run(t, exec, "CONNECT LOCAL '"+visitor.QuoteString(path)+"'") + return exec +} + +func findLoopD(node any) bson.D { + switch v := node.(type) { + case bson.D: + for _, e := range v { + if e.Key == "$Type" { + if s, ok := e.Value.(string); ok && s == "Microflows$LoopedActivity" { + return v + } + } + } + for _, e := range v { + if loop := findLoopD(e.Value); loop != nil { + return loop + } + } + case bson.A: + for _, item := range v { + if loop := findLoopD(item); loop != nil { + return loop + } + } + } + return nil +} + +func setDString(d bson.D, key, value string) bool { + for i, e := range d { + if e.Key != key { + continue + } + d[i].Value = value + return true + } + return false +} + +func getDString(d bson.D, key string) string { + for _, e := range d { + if e.Key == key { + s, _ := e.Value.(string) + return s + } + } + return "" +} + +func loopSizeOnDisk(t *testing.T, exec *Executor, g *layoutGraph) string { + t.Helper() + raw, err := exec.backend.GetRawUnitBytes(g.id) + if err != nil { + t.Fatal(err) + } + var doc bson.D + if err := bson.Unmarshal(raw, &doc); err != nil { + t.Fatal(err) + } + loop := findLoopD(doc) + if loop == nil { + t.Fatal("the flow lost its LoopedActivity") + } + return getDString(loop, "Size") +} diff --git a/modelsdk/canon/identity.go b/modelsdk/canon/identity.go index 008a93577..5549c0392 100644 --- a/modelsdk/canon/identity.go +++ b/modelsdk/canon/identity.go @@ -40,6 +40,7 @@ type reconcileOpts struct { contentsOwnTranslations bool contentsOwnStorageGUIDs bool contentsOwnElementIDs bool + contentsOwnLayout bool } // ContentsOwnElementIDs tells Reconcile that the write is a PATCH of the stored @@ -92,6 +93,21 @@ func ContentsOwnStorageGUIDs() Option { return func(o *reconcileOpts) { o.contentsOwnStorageGUIDs = true } } +// ContentsOwnLayout tells Reconcile that the write is AUTHORITATIVE about the +// geometry MDL cannot otherwise express, so a loop's stored Size must not be +// carried back over the one this write computed. +// +// The default carry keeps a loop box a person drew larger than the engine's fit +// (ako/mxcli#2179): a rebuild from MDL could not state a size, so the larger +// stored value was kept to avoid shrinking a hand-drawn box. `layout flows` is +// the opposite case — recomputing the box IS the command — and without this a +// stale box (one written by the pre-#1935 builder from absolute canvas +// coordinates) survived every run while the command reported `moved` +// (mendixlabs/mxcli#1935). +func ContentsOwnLayout() Option { + return func(o *reconcileOpts) { o.contentsOwnLayout = true } +} + // OwnsStorageGUIDs reports whether these options carry the storage-GUID // exemption. The guard itself runs at the writer's choke point, where both // documents are in hand, rather than inside Reconcile — which returns no error — @@ -167,7 +183,7 @@ func Reconcile(contents, stored []byte, opts ...Option) (out []byte, unchanged b // The rest of what MDL cannot state: an expression's surrounding whitespace // and an empty screen-reader label Studio Pro stores as a ClientTemplate // (ako/mxcli#1897). - contents = CarryUnexpressed(contents, stored) + contents = CarryUnexpressed(contents, stored, opts...) if alwaysWrite() { return contents, false @@ -235,7 +251,7 @@ func reconcileInPasses(contents, stored []byte, opts ...Option) (out []byte, unc // The rest of what MDL cannot state: an expression's surrounding whitespace // and an empty screen-reader label Studio Pro stores as a ClientTemplate // (ako/mxcli#1897). - contents = CarryUnexpressed(contents, stored) + contents = CarryUnexpressed(contents, stored, opts...) if alwaysWrite() { return contents, false diff --git a/modelsdk/canon/unexpressed.go b/modelsdk/canon/unexpressed.go index a563ad926..4b7326c0b 100644 --- a/modelsdk/canon/unexpressed.go +++ b/modelsdk/canon/unexpressed.go @@ -39,17 +39,24 @@ import ( // // and, from a drop + create of a flow (ako/mxcli#2179): // -// - A loop's Size. MDL has no way to state it, so a rebuild wrote the size -// mxcli computes and a loop drawn larger in Studio Pro overlapped its -// neighbours (carryLoopSize). +// - A loop's Size, unless the caller says it owns the layout: a rebuild wrote +// the size mxcli computes and a loop drawn larger in Studio Pro overlapped +// its neighbours (carryLoopSize). `layout flows` IS authoritative here — it +// recomputes the box — so it passes ContentsOwnLayout and a stale size is +// replaced instead of being carried at its larger stored value (mendixlabs/ +// mxcli#1935). // // Only elements whose $ID the stored document already has take anything, which // after TransplantIDs means the elements that correspond; a real change — other // expression text, a label with text — is not touched and still lands. -func CarryUnexpressed(contents, stored []byte) []byte { +func CarryUnexpressed(contents, stored []byte, opts ...Option) []byte { if len(contents) == 0 || len(stored) == 0 { return contents } + var o reconcileOpts + for _, fn := range opts { + fn(&o) + } var storedDoc, newDoc bson.D if bson.Unmarshal(stored, &storedDoc) != nil || bson.Unmarshal(contents, &newDoc) != nil { return contents @@ -75,7 +82,7 @@ func CarryUnexpressed(contents, stored []byte) []byte { if carryUnstatedFlowProperties(d, old, used) { changed = true } - if carryLoopSize(d, old) { + if !o.contentsOwnLayout && carryLoopSize(d, old) { changed = true } for i, e := range d { diff --git a/modelsdk/canon/unexpressed_loop_size_2179_test.go b/modelsdk/canon/unexpressed_loop_size_2179_test.go index af2fd8820..990749004 100644 --- a/modelsdk/canon/unexpressed_loop_size_2179_test.go +++ b/modelsdk/canon/unexpressed_loop_size_2179_test.go @@ -63,3 +63,19 @@ func TestUnexpressed_LoopSizeOnlyForLoops(t *testing.T) { t.Errorf("an activity's size was carried: %v", got) } } + +// mendixlabs/mxcli#1935: `layout flows` recomputes the box, so it says it owns +// the layout and the size it writes replaces a larger stale one. The default +// above still carries a hand-drawn box; only the authoritative caller opts out. +func TestUnexpressed_LoopSizeReplacingStaleBoxWhenLayoutOwnsIt(t *testing.T) { + w, _ := bson.Marshal(bson.D{{Key: "Objects", Value: bson.A{int32(3), loopDoc("320;160")}}}) + s, _ := bson.Marshal(bson.D{{Key: "Objects", Value: bson.A{int32(3), loopDoc("710;480")}}}) + var out bson.D + if err := bson.Unmarshal(CarryUnexpressed(w, s, ContentsOwnLayout()), &out); err != nil { + t.Fatal(err) + } + loop := docLookup(out, "Objects").(bson.A)[1].(bson.D) + if got := docLookup(loop, "Size"); got != "320;160" { + t.Errorf("a recomputed loop box was carried back as %v, want 320;160", got) + } +} diff --git a/modelsdk/mpr/writer_core.go b/modelsdk/mpr/writer_core.go index 130107d8e..80e40f8b8 100644 --- a/modelsdk/mpr/writer_core.go +++ b/modelsdk/mpr/writer_core.go @@ -942,6 +942,14 @@ func (w *Writer) UpdateRawUnitPatch(unitID string, contents []byte) error { return w.updateUnit(unitID, contents, canon.ContentsOwnElementIDs(), canon.ContentsOwnTranslations()) } +// UpdateRawUnitOwningLayout is UpdateRawUnit for `mxcli layout flows`, whose +// patch IS the geometry: a loop's Size must be replaced by the value the layout +// computed rather than carried back at its larger stored value, so a stale box +// heals instead of surviving every run (mendixlabs/mxcli#1935). +func (w *Writer) UpdateRawUnitOwningLayout(unitID string, contents []byte) error { + return w.updateUnit(unitID, contents, canon.ContentsOwnLayout()) +} + // UpdateRawUnitOwningStorageGUIDs is UpdateRawUnit for a write that deliberately // transplants storage GUIDs onto elements that keep their $ID — the marketplace // module update, which carries a module's existing GUIDs onto the documents