Skip to content

feat(exec): apply several MDL scripts in one run (all or nothing, plan first, --skip-failed) - #2020

Draft
italo-kalil-cesar wants to merge 32 commits into
mendixlabs:mainfrom
italo-kalil-cesar:pr/exec-batch
Draft

italo-kalil-cesar wants to merge 32 commits into
mendixlabs:mainfrom
italo-kalil-cesar:pr/exec-batch

Conversation

@italo-kalil-cesar

Copy link
Copy Markdown

Closes #2013

Opened as a draft, stacked on the mxcli plan PR (#2019, branch pr/plan-command): the first commits of this branch are that PR's. Review only the commits after feat(cli): add mxcli plan ... and its changelog commit.

What was wrong

mxcli exec takes one file. An agent or a pipeline that applies N scripts runs N processes, and each pays for the project write lock, opening the project, verifying the hashes and its own journal. Between two of those runs another writer can slip in, and a failure in script 3 cannot undo scripts 1 and 2, because each run committed on its own. Conflicts between the scripts (two of them set the same property, or one uses what an earlier one drops) were only discovered half-way, if at all.

What changes

mxcli exec -p app.mpr a.mdl b.mdl ... runs the scripts in the order given, in one process: one write lock (.mxcli/write.lock, taken once for the whole batch, so the lock from ako#2326 is respected, not weakened), one connection, one hash check, one write journal.

  • Default: all or nothing. A failure in any script (or a refusal by its pre-flight) rolls back every script already applied.
  • Before writing anything it runs the planner (mxcli plan) and refuses a predicted CONFLICT (exit 1, nothing written). --no-plan turns that off.
  • --continue-on-error keeps going after an error; nothing is rolled back.
  • --skip-failed undoes only the script that fails (a journal savepoint is taken before each script) and skips it, plus the scripts that need it (their own pre-flight refuses them). Exit 1, every skipped script is listed with why.
  • Each script's pre-flight runs right before it is applied, so a script can use what an earlier one created.
  • One unit-read cache scope covers the batch (scripts list the same documents).

Commits (one thing each, each builds and passes its tests):

  1. feat(mpr): layered write journal with savepoints - Savepoint() / RollbackToSavepoint(); Rollback() still undoes everything. Nothing calls the new methods yet. First tests for the journal (it had none).
  2. feat(exec): apply several scripts in one run, all or nothing + tests.
  3. perf(exec): one unit-read cache scope for the whole batch.
  4. feat(exec): run the conflict planner before a batch and refuse a conflict (+ --no-plan) + tests.
  5. feat(exec): --skip-failed undoes only the script that fails + test.
  6. docs(exec): help text, docs-site, quick reference, check-syntax skill, CHANGELOG.
  7. changelog: the issue numbers.

Not in this PR: the memoization of .mpk parsing / widget definition scans that lived in the same fork commit. It is a separate change.

Tests

  • modelsdk/mpr/journal_test.go: rollback restores unit file and index, rollback removes a unit the run created, rollback to a savepoint keeps what came before it, commit keeps the writes.
  • cmd/mxcli/cmd_exec_batch_test.go runs the real command in a child process on a copy of testdata/pedapp: batch gives the same units as the same scripts one by one; all or nothing; refusal by the plan; --no-plan; --continue-on-error; --skip-failed (4 scripts, the 2nd fails after writing, the 4th depends on it); second run writes nothing.

Proof

The tests fail when the logic is removed (run once, reverted, not committed):

  • journal.Rollback() replaced by error(nil) in the atomic path: TestExecBatchIsAllOrNothing fails ("the batch left changes behind: modified DomainModels$DomainModel MyFirstModule").
  • Plan refusal disabled (if false && res.Conflicts() > 0): TestExecBatchRefusesConflictingScripts fails (both scripts applied).
  • journal.RollbackToSavepoint() replaced by error(nil): TestExecBatchSkipFailed fails (the failing script's writes, and its dependant's, stay).

By hand on a copy of testdata/pedapp, three scripts, the second creates an entity then alters one that does not exist:

  • default: Nothing was written: all 3 scripts of the batch were rolled back ..., exit 1, none of the entities exist.
  • --skip-failed: Batch: 2 of 3 scripts applied, 1 skipped (each undone alone), exit 1, scripts 1 and 3 present, 2 absent.
  • two good scripts: exit 0.

Measurement (a fork, project of about 3000 units, 5 page alters): 5 separate exec runs 13.4 s -> one batch 6.1 s (before the cache scope); with the cache scope 10.4-16.7 s -> 6.9-9.7 s under machine load. The measurement is noisy; read it as "clearly faster", not as a number.

Limits (honest list)

  • Studio Pro validation covers two scenarios (see below); all-or-nothing on a failure was not opened in Studio Pro.
  • Not tested: a kill in the middle of a write, a flush failure.
  • --skip-failed: the dependant is skipped by its own pre-flight; the path where the plan holds a script back (planned map) is not exercised by a test. Plan + --skip-failed together is not tested.
  • Tests 2, 5 and 6 use --no-check.
  • --skip-failed needs the write journal; without it the command stops. The default path only warns.
  • Only the model (.mpr, mprcontents) is rolled back; generated .java/.js source is not (same as a single script).
  • Standard input (-) cannot be mixed with other files; several files need --project.

Run locally

make was not available on the author's machine (Windows); the equivalents were run by hand:

  • go build ./cmd/mxcli: ok
  • go vet ./cmd/mxcli ./modelsdk/mpr ./mdl/plan: clean; gofmt -l on changed files: empty
  • go test ./cmd/mxcli -run ExecBatch -count=1 -timeout 15m: ok (7 tests)
  • go test ./modelsdk/mpr -run Journal: ok (4 tests). The full package has 12 failures on Windows that are not in the journal (cache and unit store tests, files held open; see test(mpr): close the readers the test helpers open, so unit-contents.db can be removed on Windows #2016); not compared against main in this run.
  • go test ./mdl/plan: ok; go test ./mdl/conformance -count=1 -timeout 20m: ok
  • scripts/check-test-timeouts.sh: passes (Makefile unchanged)
  • make check-findings: could not run here (python3 missing)

Studio Pro validation

Tested with Mendix Studio Pro 10.24.4 (the project's own version), on a copy of an existing project of about 3 thousand units.

  • Baseline: mx check on the untouched copy: 0 errors.
  • A batch of 3 scripts (two entities and a microflow), applied 3 of 3: mx check: 0 errors.
  • A batch with --skip-failed where the middle script fails: 2 of 3 applied, the failing script undone on its own: mx check: 0 errors.
  • The resulting project opened in Studio Pro 10.24.4 and, after a refresh, the error list was empty.

Not covered by this validation: a batch that fails midway without --skip-failed (all or nothing) was only exercised by the automated tests and by running the command by hand on a copy of the test project, not opened in Studio Pro; and the checks above do not include a run of the app.

Agentic testing

Not run.

italo-kalil-cesar and others added 29 commits December 1, 2026 22:56
Add a package that reads several MDL scripts, in the order they would be
applied, and predicts from the AST alone what each one touches and where two
of them collide. It needs no project and executes nothing.

Findings are CONFLICT (same property of the same element, a document
rewritten by both, a script using what another drops or renames earlier or
needing a document created by a later one), ORDER (the order decides the
result) and DEPENDS (a script uses a document another one creates). Changes
to different elements of one document compose and are not reported.
Statement types the planner does not model are listed separately.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Wire mdl/plan into a new command: `mxcli plan a.mdl b.mdl ...` reads the
scripts in the order given and prints what each touches and where two of
them collide, without a project and without executing anything.

Exit status is 1 on a conflict, 2 on a usage or parse error, and 3 with
--strict when a statement type is not modelled and there is no conflict.
--findings-only prints only the findings.

Add a docs-site page (with the known limits) and a CHANGELOG entry.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The write journal kept one set of before-images for the whole run, so the only
undo was all of it. It now keeps them in layers: Savepoint() starts a layer and
RollbackToSavepoint() puts back only what was written since, leaving earlier
writes journaled; Rollback() still undoes the whole run, newest layer first.
Nothing calls Savepoint yet, so the behaviour of exec is unchanged.

The journal had no tests: add rollback, created-unit removal, savepoint and
commit tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
`mxcli exec -p app.mpr a.mdl b.mdl c.mdl` runs the scripts in the order given
in one process: one project write lock (the lock added for #2326, taken once
for the whole batch), one connection, one hash check and one write journal,
instead of paying each of them per script. A failure in any script rolls back
every script already applied in the run. Each script's pre-flight runs right
before it is applied, so a script can use what an earlier one created.
--continue-on-error keeps each script and statement on its own.

Tests run the real command in a child process on a copy of testdata/pedapp:
the batch leaves the same units as the same scripts run one by one, a failing
second script undoes the first, --continue-on-error, and a second run writes
nothing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Every script of a batch listed the same documents (all pages, all microflows)
and re-read them from the unit store. With one outer CacheUnitReads scope an
entry is served while its unit's hash is the one it was read under, so a
script's own writes retire what they rewrote (the cache's write-is-seen tests
are in modelsdk/mpr). Measurement is noisy; see the PR description.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…lict

Before anything is written, `mxcli exec a.mdl b.mdl ...` puts the scripts
through mdl/plan (the engine behind `mxcli plan`). A predicted CONFLICT between
two scripts refuses the batch with exit 1 and nothing written; the findings
are printed. ORDER, DEPENDS and statements the planner does not model are
listed but do not refuse. --no-plan turns the check off. A batch of scripts
that only read is not planned.

Tests: a batch of two scripts that recreate the same entity is refused and
writes nothing; with --no-plan both run and the last one wins.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
With several files, --skip-failed makes the batch not all or nothing: a script
that is refused by its pre-flight, held back by the plan (the later script of a
conflict, or the user of a document a later script creates) or fails while it
is applied is undone alone and skipped. The write journal takes a savepoint
before each script; undoing one releases the project, puts that script's
before-images back and connects again. The scripts before it stay applied, the
ones after it still run, and a script that needs what a skipped one would have
created is refused by its own pre-flight and skipped in turn. Every skipped
script is reported, with why, and the run exits 1.

Test: four scripts, the second fails after writing, the fourth depends on it;
the independent ones stay, the failing one and its dependant leave nothing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Script Files gets a section on `mxcli exec a.mdl b.mdl ...` (the lock, all or
nothing, --skip-failed, --continue-on-error, --no-plan); the plan page and the
quick reference link to it; the check-syntax skill notes that atomicity now
covers the whole batch; CHANGELOG entry under Unreleased.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Add behaviour tests for ALTER SETTINGS and ALTER PROJECT SECURITY, MOVE,
CREATE FRAGMENT, bulk statements named by something other than Name,
create or modify entity, the legacy drop condition, OnClick menu edits,
the depth limit of the reference walk and the Write output. Defensive and
contract tests cover branches the parser cannot reach. Behaviour that looks
wrong is documented with SUSPECT in the test name; plan.go is unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A drop of w1 was reported as colliding with an insert after w10, because the anchor test was strings.Contains over the anchor text. The two compose (w1 is dropped and the widget lands after w10): run in series with exec they do. An insert's Elem is already the widget it anchors on, so comparing the Elem is enough and a drop of w10 still collides with an insert after w10. The test that documented the false positive now asserts that the two compose.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
refsOf stopped walking at depth 40, but each level of nesting in a statement costs about four steps (pointer, interface, struct, slice), so a reference under roughly ten nested blocks or widgets was missed and a script using an entity another one drops went unreported. A pointer already visited stops a cycle, so the limit only needs to be a backstop; it is now 10000. Run without --no-check, exec refuses such a script anyway (nothing is written), so this closes a gap in the warning, not in the safety net.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Two ALTER PAGES ... SET LAYOUT statements over the same pages overwrite each other, the last one wins and the result depends on the order, but they were keyed by the target layout, so the planner saw different units and reported nothing. The touch is now keyed by the pages the statement covers (the module, or every module, and the WHERE LAYOUT filter), so a repeat over the same pages is a CONFLICT like the single-page SET Layout, while another module or another filter still composes. A project-wide statement and a one-module statement are not compared to each other. The fragment test now says why its CONFLICT is right: in a batch (one process) the second CREATE FRAGMENT of a name fails with 'fragment already exists' and the batch is rolled back.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A "drop template for X in lv" was recorded as a guess on the whole page,
and a guess is never destructive, so it never met a script that dropped,
replaced, moved or wrapped the list view. Applied after such a script it
fails ("widget lv not found") and the batch is rolled back, yet the plan
said the scripts compose in any order.

It is now a set of template:<specialization> on the list view: it collides
with drop/replace/wrap/move of that list view and with a second drop of the
same template, and still composes with other properties, other list views,
other templates of the same list view and inserts beside its widgets.
The catch-all branch for unknown page operations is now covered with a nil
operation, the only value the parser never produces.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…paths and branches with it

An outcome, path or branch of a workflow activity is the element act:T1/outcome:B, which is neither equal to act:T1 nor part of an anchor, so a drop or replace of the activity never met a drop of one of its outcomes (or a legacy drop condition ... on the activity). Run with the activity drop first the outcome drop fails (alter target T1 not found) and the batch is rolled back; run the other way round the outcome change is silently moot. The plan said the scripts compose in any order.

A drop, replace, wrap or move of an activity now collides with a change of anything it owns, in a workflow only (a menu caption may contain a slash). The reverse is not a collision: a drop of an outcome says nothing about its task, and a set on the task or a drop of a different outcome or another activity still composes. The case that documented the gap as SUSPECT now asserts the conflict, and drop condition and drop path get the same checks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…or the other scripts

A MOVE statement carries no document-type word in its name, so it was keyed by the bare name (:M.P) and never met an alter or a drop of the page (page:M.P). Moved into another module the page is Other.P, so an alter or drop of M.P applied after the move fails (page not found) and the batch is rolled back, yet the plan said the scripts compose in any order.

A move whose target module differs from the document's own is now keyed by the document type and name, the same unit as the alter, drop and create statements, and collides with them (an entity is keyed in its domain model and as entity:M.E, where ALTER ENTITY lives). The document type is the MOVE spelling without its spaces. A move to a folder or to the module the document is already in keeps its name and composes, as before, and a create or replace of the freed old name composes with the move in either order. The two SUSPECT cases now assert the correct behaviour for a folder move, and the cross-module cases are new.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A move to a folder was keyed by the bare name, apart from the create, drop and alter of the same document, so a move into another module followed by a move of the old name to a folder (which fails at exec: page not found) went unnoticed. Key every MOVE by document type and name. A move within the module becomes movefolder, which composes with alters, rewrites, drops after it and grants, collides with another move of the document, and fails after a move into another module.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ment

A move into another module touched only the old name, so a script that created Other.P, dropped it, moved something else into it or used it before the move went unnoticed, although the move fails at exec when the name is taken and the user fails when the name does not exist yet. The move now also creates the document under its new name (for an entity, in the domain model of the target module), so the rules of any other create apply: a clash is a CONFLICT, a user applied after is DEPENDS and one applied before is a CONFLICT.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… document type

Only five document types had been checked against the create and drop of the same type. Walk every type the MOVE accepts (the registry the parser reads, so a type added later is checked too) and assert that the move is keyed with the same kind word as the CREATE and the DROP of that type, built from the statement as the parser or the AST gives it, and that the R10 and AI model aliases read as their canonical type. No divergence was found among the 32 types; building block and icon collection have no create or drop in MDL, and entity lives in its domain model and is checked against create entity, drop entity and alter entity.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…or plan it from the project

A MOVE FOLDER into another module moves every document in the folder, which the scripts do not say, so plan treated it like the move of one folder and missed every collision with the documents inside it. Without a project it is now listed as not modelled, with the statement and the reason (so --strict exits 3). With the new --use-project flag, plan opens the project given by -p read-only (without a lock, and never unless asked for, so plan still needs no project) and plans one move per document the folder holds there now, folders below it included; a document type MOVE does not know, or a folder the project cannot list, keeps the statement not modelled. Documents that scripts applied earlier put in the folder are not known to it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
italo-kalil-cesar and others added 3 commits December 2, 2026 18:19
…it along

Dropping or replacing a widget takes the widgets inside it along, but the planner only compared touches of the very same widget name, so dropping a container did not collide with a change of the list view inside it (for example dropping its template). Read the nesting the scripts write out (create page and snippet bodies, placeholders, insert, replace, wrap and move into) into a tree per page or snippet, and make a drop or replace of a widget collide with a change of any widget shown inside it, in either order, except an inner drop applied before the outer one. Wrapping or moving the outer widget leaves the inner ones findable, as exec confirms, so they compose. A widget the replacement brings again stays. Without a script that shows the nesting nothing is known of the page as the project has it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… nesting

Document the new --use-project option, the folder move that is not modelled without it, and the limits that remain: the new name of a moved document, create or replace with a folder, associations of a moved entity, the folder contents as the project has them now, and widget nesting known only from the scripts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(exec): accept several MDL scripts in one run (one process, one lock, one journal, all-or-nothing), with an optional plan check

1 participant