Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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."}
11 changes: 11 additions & 0 deletions mdl/backend/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions mdl/backend/mcp/unsupported_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions mdl/backend/mock/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions mdl/backend/mock/mock_infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ---------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions mdl/backend/modelsdk/unimplemented_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions mdl/backend/modelsdk/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions mdl/executor/cmd_microflows_layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
161 changes: 161 additions & 0 deletions mdl/executor/issue1935_layout_flows_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
20 changes: 18 additions & 2 deletions modelsdk/canon/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 —
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions modelsdk/canon/unexpressed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions modelsdk/canon/unexpressed_loop_size_2179_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading
Loading