Skip to content

Commit a6ebec4

Browse files
committed
Merge #370: refactor(app-restart, test-scratch): share the mxtest guard and mxcli-run pid check via _common.sh
Review: independent review MERGE at bbc67b0 — moved helpers byte-identical to both originals (76 lines); stricter Linux pid match verified against ./mxcli run --local, -p X.mpr run --local, env prefixes, exec wrappers, absolute paths; older-_common.sh guard cannot fire on a synced project; extract-by-function fixtures unaffected. Fixtures: app-restart 69/0, test-scratch 51/0, shared-lock-takeover 6/0, stack-up-local 60/0, common-windows 10/0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VJgWP5vEoAsNsJYqCDMGNw
2 parents 3b6ca76 + bbc67b0 commit a6ebec4

6 files changed

Lines changed: 161 additions & 187 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Sections dated before 2026-09-19 predate the cycle and stay as they are. Named m
2323
- log(bug-logs): **upstream draft for `mxcli run --local --watch`: a theme-only rebuild deletes `dist/pages` and still reports a successful reload** ([#284](https://github.com/MendixMau/mxcli-project-toolkit/issues/284)). `bug-logs/pending-github-issues/watch-theme-rebuild-drops-dist-pages.md`, status DRAFT — NOT FILED: the repro (one SCSS edit, every page 404s on `dist/pages/<Page>.js`, a restart restores all 135 bundles; twice in one app on mxcli v0.26.0 / Mendix 11.15.0), the other change classes watch serves stale, the timing table (reload 35 builds median 3.8 s, restart 44 builds median 19.2 s, max 55.2 s, 928 s total) and the upstream questions: why a restart is about 5× a reload, and whether a recreated microflow keeps its old compiled version by name. The toolkit side is already on master (exec.sh restart verdicts, `app-restart.sh`, ui-loop.md "What watch picks up"). Field evidence: the 2026-10-12 three-app UX run and its 2026-10-14 recurrence, as recorded in #284. — field retro #284, MendixMau
2424
- fix(close-task): **`close-task.sh` resolves `checkpoint.sh` instead of printing it bare.** The script ships at `claude-hooks/bin/checkpoint.sh` and reaches `~/.claude/bin` only via the opt-in `bin/install-claude-hooks.sh --basic`, so a project without the hooks got a `checkpoint.sh --write` step it could not run. `close-task.sh` now looks in PATH, then `~/.claude/bin`, then `$MXTK_ROOT/claude-hooks/bin`, and prints the full path it found, single-quoted so a path with spaces survives copy-paste. If none is found it prints `checkpoint.sh not installed — run <toolkit>/bin/install-claude-hooks.sh --basic, or skip: RESUME.md is the resume source` and carries on; the exit code still means only "writes owed". `close-the-loop.md` step 4 says the same. The `~/.claude/bin` copy (`claude-hooks/bin/close-task.sh`) now prints its sibling `checkpoint.sh` by full quoted path, because `~/.claude/bin` is usually not on PATH. Fixture `tests/wave2/test-close-task-checkpoint.sh` (14 cases, including a space and `'` in HOME and the toolkit path; 11 fail on master). Field evidence: the reporter's existing-app run, where the closing agent had to skip the step with no pointer. — joshuamoesa (#358)
2525
- new(mdl-scan): **`bin/mdl-scan.sh` flags a script that carries a scannable STOP-row pattern, inside `exec.sh` before the lock, the snapshot and the apply — warn-only until a field run** ([#349](https://github.com/MendixMau/mxcli-project-toolkit/issues/349)). Four of the fourteen live STOP rows are text patterns with a captured failure, and each is one row of `bin/lib/mdl-scan-rules.tsv`: 1 (conditional Visible/Editable inside a customContent column, BUG-18 StorageLoadException), 12 (an expression in `ContentParams`, BUG-142 CE1613), 16 (a path inside `[%CurrentUser%]`, CE0161), 26 (a quoted association path in `Attribute:`, CE1613). Output is `STOP-<n> <file>:<line> <message> → <fix> [warn|block]`; exit 0 clean, 1 a `block` hit, 3 `warn` hits only, 2 usage. Every rule ships as `warn`: exec.sh prints the hits, notes them in the BUILD-LOG row and goes on. A rule moves to `block` after one field run where it caught a real failure and flagged nothing valid (candidates 12, 16, 26; STOP-1 stays `warn`, its evidence predates v0.24.0). The `block` path is kept and tested: it refuses before anything is touched, `FORCE_EXEC`/`SKIP_CHECK` do not skip it, and the only override is the user's `MXTK_SCAN_OVERRIDE="<reason>"`, printed and logged (#348 hardens it). The other rows need model state or judgement and stay prose; 1b, 4, 18 and 24 are scannable but have no captured failure in `bug-logs/`, so they are proposed, not shipped. `learned-mdl-preflight.md` marks the enforced rows with 🔎 (+6 baseline words), and row 16's example now uses generic entity names (`Decision_Partner`, `Account_Partner`) in place of project-specific ones; the verbatim form stays in the bug log. Fixture `tests/wave2/test-mdl-scan.sh` (52 checks: each rule fires on the bad MDL captured in its bug entry and is silent on the corrected form, keys and CE text asserted verbatim in the ledger, the independent review's three false-hit forms clean, warn and block exec paths). `tests/wave2/bench-mdl-scan.sh`: 4/9 captured failures flagged, 0/19 clean files flagged, 88–260 s of applies avoidable over the corpus. Field evidence: captured bug-log output only, no live run. — external reviewer feedback (2026-10-14), independent review of #355
26+
- process(app-restart, test-scratch): **the mxcli-test-injection guard and the `mxcli run` pid check are one copy in `_common.sh`, not two drifting ones.** `mxtest_after_startup`, `mxtest_left` and `mxtest_refusal` were duplicated in `project-bin/app-restart.sh` and `project-bin/test-scratch.sh` (the copies differed only in a refusal branch test-scratch never takes; the tool-name argument still selects it, so each script prints exactly what it printed). `pid_is_mxcli_run` becomes `mxtk_pid_is_mxcli_run` on `mxtk_pid_args`: on Linux `run` must be a whole argument (an argument that merely contains " run " no longer counts), macOS keeps the joined `ps` line, Windows keeps the `tasklist` image check. Both scripts stop with exit 2 and the `sync-project.sh --upgrade-bin _common.sh` remedy when the installed `_common.sh` predates the move; one fixture case each. — engineering backlog review 2026-10-14 (item 6)
2627
- process(build): **Stage 5 drafts the ready set in parallel and applies serially.** The runbook dispatch row said "sequential within a module", which serialised drafting even for independent scripts; it now points to `iterative-build-loop.md` → Gate cost, the single statement of the rule. The unit is the build-plan row: every `BUILD` row whose "Depends on" rows are all applied, minus overlapping writers (two rows that replace, alter or drop+create the same document never share a ready set, and no row is drafted while it overlaps a queued unapplied one; `brd-to-build-plan.md` has Produces name every document a row writes), runs at once, its scripts in order, one `mdl-agent` per script. Applies stay one at a time through `bin/exec.sh`, and the ready set is recomputed after each. A queued draft made before a sibling apply landed is re-checked with `mxcli diff --format struct` (preflight STOP rule 23); any removal line (checked against `describe` per BUG-146) or STOP rule 21 target means re-draft, never patch, because a stale restatement of a shared document is exactly rule 21's silent seed-call loss and passes `check --references`. Report `ready set N, drafted N, applied k`; the `mdl-agent` template says a drafter never applies and never depends on an unapplied draft. Field evidence: none yet. — MendixMau
2728
- fix(platform-link): **CLI commits now show their changed documents in Studio Pro History.** Studio Pro's History → Model changes pane reads a `ModelChanges` array from the commit's `refs/notes/mx_metadata` note. Trap 6c's hand-written template set it to `[]`, so every mxcli/agent commit showed "Model changes 0" (field project: 270 of 270 notes empty). New `project-bin/mx-metadata-note.py` (stdlib only) computes the array from the commit's changed `.mxunit` files, in Studio Pro's entry shape: Status, UnitID, UnitType, UnitName and Module (walked up the `.mpr` Unit tree). It has `changes` / `write` / `backfill` / `sync <remote>` subcommands. `install-project-hooks.sh` now also installs post-commit and post-rewrite hooks that write the note and never block, and `--check` covers them, so `sync-project.sh` rolls them out to wired projects. Notes are published with `mx-metadata-note.py sync <remote>` after the branch push. Push config is never touched. With mxcli v0.26+ in the project root the hooks call `mxcli git note --write` instead (mendixlabs/mxcli#1337); the Python script is the fallback for older pins, and on 160 field commits both wrote the same 3,594 entries. Trap 6c rewritten. Measured against Studio Pro's own notes, 51 commits in 5 apps: 3387/3389 entries exact on all five fields. Field run on a 357-commit Mendix 11.15.0 app: 158 notes backfilled and pushed, and Studio Pro History lists the documents per revision. Review fixes: an earlier revision added `HEAD` + the notes ref to `remote.<r>.push`. It was dropped because any push refspec replaces `push.default`. A branch tracking a differently named upstream got a new same-named branch on Team Server where `simple` refuses, a missing local notes ref failed the whole push, and nobody knows whether Studio Pro's own push reads that config. The installer now only fetches the remote's notes (non-forced). It removes that exact `HEAD` + notes pair if present, and leaves and reports any other entry. `sync` handles a remote with no notes ref (it used to exit 1) and retries a rejected push once after re-merging. A note that already lists changes (Studio Pro's) is never overwritten by `write`/`backfill` without `--force`, and `sync` keeps a colleague's non-empty note over ours. The mxcli probe greps `git note --help` for `--write`. Hooks and docs call the script through `resolve_py`. Fixture `tests/wave2/test-mx-metadata-note.sh` (40 cases, over real captured template units; single- and two-tree layouts, amend, backfill and `--force`, push exit codes identical to a no-hooks clone, leftover-refspec cleanup, sync to an empty remote and its retry, notes fetch at install, a diverged notes merge; green under `init.defaultBranch` master and main). — field project (Mendix 11.15 app), MendixMau
2829
- learn(fixture-seeding): **explore the data structure first, choose rows by use case, scan actions for resets before any run.** Two seeding runs on public template apps: seeding raised journey passes 33 -> 58 and 20 -> 51 of the same sets (FAIL 93 -> 68, 163 -> 132; INVALID unchanged), and a "Reset all demodata" journey wiped accounts so 104 of 127 journeys reported `The username or password you entered is incorrect`, identical on empty and seeded databases. Skill gains three questions a seed must answer, a use-case checklist, a destructive-action scan, a generic traps table with symptoms, measured expectations and a denominator checklist; routing trigger now fires on seed/test/demo data and empty pages. Independent review fixes: object ids follow the Mendix bigint pattern with a business-key guard (no uuids), associations are seeded where the page's path stores them, four new traps (enum names, inheritance chain, calculated attributes, view/external entities), and the port-offset and container-label traps moved to `harness-architecture.md`. — MendixMau

‎project-bin/_common.sh‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,3 +915,121 @@ EOF_ARGS
915915
done
916916
return 1
917917
}
918+
919+
# ---------------------------------------------------------------------------
920+
# mxcli run / test identity — shared by app-restart.sh and test-scratch.sh (one copy each until
921+
# 2026-10-14; they had drifted only in a branch test-scratch never took). A caller whose installed
922+
# _common.sh predates this section stops with the sync remedy (`command -v mxtest_left`).
923+
# ---------------------------------------------------------------------------
924+
925+
# mxtk_pid_is_mxcli_run <pid> — 0 when the process IS an `mxcli run` (#304 review). A SIGKILLed
926+
# mxcli leaves its run --local record behind and the OS hands its pid to an unrelated process;
927+
# `kill -0` cannot tell the difference. Linux with /proc: argv one per line (mxtk_pid_args), so
928+
# `run` must be a whole argument after one naming mxcli. macOS (and Linux without /proc): the
929+
# joined `ps -o args=` line, " run " as a word after mxcli. Windows: the image name is mxcli.exe
930+
# (tasklist — Git Bash's ps cannot show a Windows process's arguments, and PowerShell per call is
931+
# slow). Unreadable → 1.
932+
mxtk_pid_is_mxcli_run() {
933+
local a="" nl='
934+
'
935+
if [ "$(mxtk_platform)" = windows ]; then
936+
a="$(tasklist //FI "PID eq $1" //FO CSV //NH 2>/dev/null | tr -d '\r' | head -1 | cut -d'"' -f2)"
937+
case "$(printf '%s' "$a" | tr '[:upper:]' '[:lower:]')" in mxcli.exe|mxcli) return 0 ;; esac
938+
return 1
939+
fi
940+
a="$(mxtk_pid_args "$1")" || return 1
941+
if [ "$(mxtk_platform)" = linux ] && [ -r "/proc/$1/cmdline" ]; then
942+
case "$nl$a$nl" in *mxcli*"${nl}run${nl}"*) return 0 ;; esac
943+
return 1
944+
fi
945+
case " $a " in *mxcli*" run "*) return 0 ;; esac
946+
return 1
947+
}
948+
949+
# ── After-startup left on an mxcli test injection (#244, BUG-157) ───────────
950+
# `mxcli test` and `mxcli run --test-endpoint` point the model's after-startup microflow at
951+
# MxTest.RegisterEndpoint (MxTest.TestRunner for a plain test run) and chain the original; only a
952+
# clean exit puts it back. A run stopped by a signal leaves it, and the next injection then chains
953+
# RegisterEndpoint to itself: StackOverflowError in the after-startup, the runtime never starts.
954+
# Read straight from the model bytes — no mxcli, no running app: the setting is the BSON string
955+
# element 0x02 "AfterStartupMicroflow" 0x00 of the project-settings unit (mprcontents/**/*.mxunit;
956+
# inside the .mpr itself on a v1 model). Layout checked against real settings units (mxcli's own
957+
# test models). A unit or .mpr without the bytes "MxTest." anywhere is not scanned at all (an 8 MB
958+
# v1 .mpr costs a grep, not an 8 s od). MXTK_SKIP_MXTEST_GUARD=1 skips the check, out loud.
959+
mxtest_after_startup() ( # $1 = file, or - for stdin. Prints the value ('' = none); 1 = no such element
960+
# A subshell with pipefail OFF: awk exits at the first match and od then dies of SIGPIPE; under
961+
# the caller's pipefail that 141 would hide the answer and switch the guard off on a large .mpr.
962+
set +o pipefail
963+
{ if [ "$1" = - ]; then od -An -v -tx1; else od -An -v -tx1 "$1"; fi; } 2>/dev/null | awk '
964+
BEGIN { for (i = 0; i < 256; i++) hv[sprintf("%02x", i)] = i
965+
for (i = 32; i < 127; i++) { c = sprintf("%c", i); ch[sprintf("%02x", i)] = c; hx[c] = sprintf("%02x", i) }
966+
s = "AfterStartupMicroflow"; T = length(s) + 2; t[1] = "02"; t[T] = "00"
967+
for (i = 1; i <= length(s); i++) t[i + 1] = hx[substr(s, i, 1)]
968+
k = 0; st = 0; found = 0 }
969+
{ for (f = 1; f <= NF; f++) { b = $f
970+
if (st == 0) { if (b == t[k + 1]) k++; else k = (b == t[1]) ? 1 : 0
971+
if (k == T) { st = 1; L = 0; m = 1; n = 0; v = "" } }
972+
else if (st == 1) { L += hv[b] * m; m *= 256
973+
if (++n == 4) { if (L < 1 || L > 1024) { st = 0; k = 0 } else if (L == 1) { found = 1; exit } else { st = 2; n = 0 } } }
974+
else { if (++n < L) v = v ((b in ch) ? ch[b] : "?"); else { found = 1; exit } } } }
975+
END { if (!found) exit 1; print v }'
976+
)
977+
MXTEST_LEFT=""; MXTEST_UNIT=""; MXTEST_HEAD=""
978+
mxtest_left() { # $1 = model dir, $2 = .mpr. 0 = after-startup is an mxcli test injection (sets MXTEST_*)
979+
local unit val cands
980+
MXTEST_LEFT=""; MXTEST_UNIT=""; MXTEST_HEAD=""
981+
if [ "${MXTK_SKIP_MXTEST_GUARD:-0}" = 1 ]; then
982+
echo " ! MXTK_SKIP_MXTEST_GUARD=1 — NOT checking for a leftover mxcli test after-startup (#244). If the app" >&2
983+
echo " then dies with StackOverflowError in MxTest.RegisterEndpoint, that is what this check was for." >&2
984+
return 1
985+
fi
986+
if [ -d "$1/mprcontents" ]; then
987+
cands="$(LC_ALL=C grep -rla --include='*.mxunit' 'AfterStartupMicroflow' "$1/mprcontents" 2>/dev/null)"
988+
else cands="$2"; fi
989+
# Every unit holding the text, not the first: a doc string elsewhere can mention the setting by
990+
# name. Only the BSON element counts, and only a unit that contains "MxTest." can hold the value.
991+
while IFS= read -r unit; do
992+
[ -n "$unit" ] && [ -f "$unit" ] || continue
993+
LC_ALL=C grep -qa 'MxTest\.' "$unit" 2>/dev/null || continue
994+
val="$(mxtest_after_startup "$unit")" || continue
995+
case "$val" in MxTest.RegisterEndpoint|MxTest.TestRunner) ;; *) continue ;; esac
996+
MXTEST_LEFT="$val"; MXTEST_UNIT="$unit"
997+
case "$unit" in *.mxunit) # what HEAD has, so the repair can name it; "?" = not tracked / no git
998+
MXTEST_HEAD="$(git -C "$(dirname "$unit")" show "HEAD:./$(basename "$unit")" 2>/dev/null | mxtest_after_startup -)" || MXTEST_HEAD="?" ;;
999+
*) MXTEST_HEAD="?" ;; esac
1000+
return 0
1001+
done <<CANDS
1002+
$cands
1003+
CANDS
1004+
return 1
1005+
}
1006+
mxtest_refusal() { # $1 = tool name. Prints the refusal and the repair to stderr. test-scratch gets the
1007+
# test-a-copy advice; any other caller (app-restart) the stop-the-live-app advice.
1008+
local was="$MXTEST_HEAD" rel="${MXTEST_UNIT#"$PROJECT_ROOT"/}"
1009+
echo "✗ $1: this model's after-startup microflow is $MXTEST_LEFT — left behind by an mxcli test or" >&2
1010+
echo " run --test-endpoint that did not exit cleanly (stopped by a signal or killed; BUG-157)," >&2
1011+
echo " or held right now by a live run --test-endpoint of this project." >&2
1012+
echo " Starting on it recurses (the next injection chains $MXTEST_LEFT to itself: StackOverflowError)," >&2
1013+
echo " or fails the build with CE1613 if the MxTest module is already gone. Either way the app never starts." >&2
1014+
echo " set in: $rel" >&2
1015+
case "$was" in '?') was="<your own after-startup microflow, or '' for none>"
1016+
echo " HEAD: not readable (no git, or the unit is untracked)" >&2 ;;
1017+
'') echo " HEAD: no after-startup microflow" >&2 ;;
1018+
*) echo " HEAD: $was" >&2 ;; esac
1019+
if [ "$1" = test-scratch ]; then
1020+
echo " → If this project's app runs with --test-endpoint right now (skills/cloud-dev-environment.md), the" >&2
1021+
echo " live model is fine as it is — test a copy whose settings unit comes from HEAD instead (#244's" >&2
1022+
echo " workaround): git worktree add <dir> HEAD, copy the units under test into it, run mxcli test there" >&2
1023+
echo " on free ports. Or stop the live app with Ctrl+C in its own terminal (a clean stop restores it)." >&2
1024+
else
1025+
echo " → If this project's app is running with --test-endpoint right now, this is its live injection:" >&2
1026+
echo " stop it with Ctrl+C in its own terminal (a clean stop restores the setting), then retry." >&2
1027+
fi
1028+
echo " → Otherwise restore it, then retry:" >&2
1029+
echo " git checkout -- '$rel' (then git status: the run may have left an MxTest module too)" >&2
1030+
echo " or through ./bin/exec.sh (an ALTER SETTINGS — say so in chat; it is what mxcli's own cleanup runs):" >&2
1031+
echo " mdl 1;" >&2
1032+
echo " alter settings runtime (AfterStartupMicroflow: '$was');" >&2
1033+
echo " or Studio Pro: App Settings → Runtime → After startup." >&2
1034+
echo " A false refusal? MXTK_SKIP_MXTEST_GUARD=1 skips this check (and says so)." >&2
1035+
}

0 commit comments

Comments
 (0)