Skip to content

Commit 40b2ecd

Browse files
committed
fix: provision PostgreSQL as a non-root user (closes #984)
`mxcli run --local --ensure-db` could not provision PostgreSQL in a non-root devcontainer. That is the environment mxcli itself generates: `mxcli init` writes a devcontainer from mcr.microsoft.com/devcontainers/base:bookworm which installs the postgresql server and runs as `vscode`, and wires `run --local --setup --ensure-db` into the Claude Code SessionStart hook — so it fired on every fresh session in an initialized project, not only when someone typed the flag. Fixing it here rather than in the template also repairs projects that have already been scaffolded. Three independent defects sat on one path, each sufficient to block it: - The service start was never elevated. Debian's /etc/init.d/postgresql runs under `set -e` and calls create_socket_directory first, which chmods /var/run/postgresql — refused for a non-root user, so the script aborts before it looks at a single cluster. Now attempted with `sudo -n` first and unprivileged second. - The #823 user-owned-cluster fallback was inert on Debian and Ubuntu, which wrap only the client tools into /usr/bin while initdb and pg_ctl live in /usr/lib/postgresql/<major>/bin. The safety net for a failed service start could never deploy on the platform that needs it most. Both tools are now resolved from the same versioned directory, ranked numerically — a lexical sort puts "9" above "16", and a data directory made by one major cannot be started by another. - The superuser was unreachable: `sudo -u postgres` is refused by the devcontainer's own sudoers rule, `vscode ALL=(root) NOPASSWD:ALL`, which permits root as the only target. Falls back to `sudo -n -- sudo -n -u postgres`, since root may in turn target any account. Everything stays non-interactive, so an unattended run cannot block on a password prompt. Separately, the readiness timeout discarded the service manager's own output, which this package had already collected — the permission denial was in hand and reported as a generic 20s timeout. A guard that names the wrong cause costs the reader more than no guard at all, so the diagnostics now travel with the error. Measured on Ubuntu 24.04 / PostgreSQL 16 with a real non-root user and the real sudoers rule, against a harness calling EnsureDatabase directly: the shipped build fails with `exec: "initdb": executable file not found in $PATH` and, once the cluster is up, `no local PostgreSQL superuser available`; after the fix the sudo path provisions in 2.6s and the no-sudo user-cluster path in 7.2s. Each of the four fixes was reverted individually and its test re-run against the reported symptom. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dp1syGhH8yr7Hve2wwjzqg
1 parent 5071aa4 commit 40b2ecd

5 files changed

Lines changed: 393 additions & 44 deletions

File tree

‎.claude/skills/fix-issue/findings/cmd-mxcli.jsonl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,4 @@
105105
{"area": "cmd/mxcli", "date": "2026-09-11", "symptom": "Two opposite `brain plan` failures with one root cause. A requirement anchored at a bare MODULE (`@Maintenance`) reports BUILT the moment the module exists, with none of its work done. A requirement anchored at a MODULE ROLE (`@Maintenance.Coordinator`) reports PLANNED forever, even once the roles exist \u2014 `describe` refuses it ('no describable document named \u2026') and `mxcli refs` finds nothing", "cause": "`catalogResolver.Resolve` in `cmd/mxcli/cmd_brain.go`. A module has a row in the catalog's `objects` view, so it resolves immediately \u2014 fine for a decision (anchors point backward) and useless for a requirement (anchors point forward, so resolution IS the progress signal). A module role is in NEITHER the objects view NOR `FindDocumentUnit`, because it is not a document \u2014 so both lookups miss and it falls through to NotFound, which for a requirement means 'not built yet' permanently", "file": "`cmd/mxcli/cmd_brain.go` (`catalogResolver.Resolve`, `moduleRoleExists` via `GetModuleSecurity`), `cmd/mxcli/brain/entry.go` (`requirementAnchorsArePlannable`)", "insight": "**A resolver's vocabulary has to cover what people actually anchor at, and the two failure directions need opposite fixes.** The module-role gap is a LOOKUP gap \u2014 fixed by consulting `GetModuleSecurity`, case-insensitively because Mendix treats role names that way and an anchor is hand-written. The module-anchor gap is SEMANTIC and cannot be fixed by lookup: the anchor resolves correctly and is still useless, so it is refused at capture time with the alternative named (an author told only 'no' deletes the anchor, which loses the signal entirely rather than fixing it). Keep the control in BOTH directions: a module anchor stays legal on a decision and on a question, or the fix degrades to 'refuse every module anchor' and the negative test still passes. Verified end to end: `@MyFirstModule.User` \u2192 1 anchor 1 resolved; `@MyFirstModule.NoSuchRole` \u2192 NOT FOUND, exit 1, so the resolver did not simply become permissive. Reported by ako/ChipCoV1", "refs": ["ako/ChipCoV1 FINDINGS.md"]}
106106
{"area": "cmd/mxcli", "date": "2026-09-11", "symptom": "`.claude/bootstrap-mxcli.sh` (the SessionStart hook) re-downloads ~85 MB of mxcli on EVERY fresh session in an environment where mxcli is already installed on PATH", "cause": "The script gated only on `[ ! -x ./mxcli ]` and never consulted PATH, while the bootstrap skill tells you to `rm -f <AppName>/mxcli` after moving the project to the repo root \u2014 so in a session image with mxcli pre-installed the guard could never be satisfied by the binary already present, forever", "file": "`cmd/mxcli/init_hook.go` (`bootstrapScriptTemplate`)", "insight": "**A guard that asks 'is the artifact HERE' rather than 'is it AVAILABLE' pays full cost for something already on the machine.** Hardlink a PATH copy in first (what `mxcli new` itself does), falling back to symlink across filesystems then to a copy, and only download when none of those work \u2014 every path leaves ./mxcli working, which the rest of the script and the generated project CLAUDE.md both assume. Keep BOTH controls when testing: nothing on PATH must still download (not silently no-op), and an existing ./mxcli must be left untouched. Reported by ako/ChipCoV1", "refs": ["ako/ChipCoV1 FINDINGS.md"]}
107107
{"area": "cmd/mxcli", "date": "2026-09-11", "symptom": "`mxcli theme create --from <design.css>` seeds the palette and nothing else: the scaffolded brand theme still described itself in `theme list` as 'Cool slate, one teal signal colour' with Signal's six swatches, and vendored ~500 KB of IBM Plex woff2 the seeded --mxt-font never names, plus a SIL OFL licence for fonts it does not use. Separately, the primary button was never the brand colour: Atlas derives --btn-primary-bg from --brand-primary-600 = color-mix(brand, contrast 20%), so a brand blue #10069F rendered rgb(21,13,140)", "cause": "`manifest()` copied the base theme's Summary and Colorway verbatim and the walk copied every file unconditionally. The Atlas map pinned `--btn-primary-color` to `--mxt-brand-ink` \u2014 an ink each theme picks to sit on `--mxt-brand` (console pairs near-black #04211d with bright teal #2dd4bf) \u2014 while leaving the background to Atlas's derivative, so the pairing the theme designed for was never the pairing that rendered. The map's own comment already called it 'a brand-filled button'", "file": "`cmd/mxcli/theme/create_seeded.go`, `create.go` (`manifest`, the scaffold walk), `assets/*/files/theme/web/_mxcli-atlas-map.scss`", "insight": "**Inheriting a statement ABOUT the base theme into a theme whose palette is no longer the base's is a confident lie; derive it or drop it.** Two traps in the font half. (1) The decision must be made BEFORE the walk: it is taken by reading the partial and applied to files elsewhere in the tree, so doing it inline depended on WalkDir's lexical order putting `_mxcli-<name>.scss` before `mxcli-fonts/` \u2014 true only because of the leading underscore. (2) It touches two halves \u2014 the @font-face rules and the woff2 files \u2014 and getting either alone wrong is silent: a surviving rule for a deleted file 404s, a surviving file nothing loads is the dead weight being removed. Unit tests on each half cannot catch a mismatch; the guard is an integration assertion that a scaffolded theme ships exactly the fonts it loads (control: stubbing the file half fails it with 'X is shipped but no @font-face loads it'). Reported by ako/ChipCoV1", "refs": ["ako/ChipCoV1 FINDINGS.md"]}
108+
{"area": "cmd/mxcli", "date": "2026-09-13", "symptom": "`mxcli run --local --ensure-db` cannot provision PostgreSQL in a non-root devcontainer (Debian/Ubuntu base, remoteUser vscode): reported as a bare \"PostgreSQL did not become ready at 127.0.0.1:5432 within 20s\", or as `exec: \"initdb\": executable file not found in $PATH`. Fires on every fresh Claude Code session in an initialized project, since `mxcli init` wires `run --local --setup --ensure-db` into the SessionStart hook", "cause": "THREE independent defects on one path, each sufficient to block it. (1) `service postgresql start` ran unelevated: Debian's /etc/init.d/postgresql runs under `set -e` and calls create_socket_directory FIRST, which chmods /var/run/postgresql — refused for a non-root user, so the script aborts before it looks at a single cluster. (2) The #823 user-owned-cluster fallback was INERT on Debian/Ubuntu: postgresql-common wraps only the CLIENT tools (psql, pg_isready, pg_ctlcluster) into /usr/bin, while initdb/pg_ctl live in /usr/lib/postgresql/<major>/bin — so the safety net for a failed service start could never deploy on the platform that needs it most. (3) `resolveSuperuser` used `sudo -n -u postgres psql`, but mcr.microsoft.com/devcontainers/base grants its user sudo to root ONLY (`vscode ALL=(root) NOPASSWD:ALL`, confirmed in devcontainers/features main.sh) — so the target is refused even though the user is effectively an administrator. Plus a diagnostic defect: the 20s readiness timeout discarded the service-manager output the package had already collected", "file": "`cmd/mxcli/docker/ensuredb.go` (`serviceStartAttempts`, `postgresServerBinDir`/`postgresTool`, `superuser.viaRoot`, `withServiceDiag`)", "insight": "mxcli GENERATES the broken environment — `generateDockerfile` emits that exact base image, installs postgresql, and runs as vscode — so this was not user misconfiguration, and fixing it in code (not the template) also repairs projects already scaffolded. Root may target any account, so `sudo -n -- sudo -n -u postgres` reaches postgres under a root-only sudoers policy; try the direct form first and nest only on refusal. Resolve initdb and pg_ctl from the SAME bin directory and rank majors NUMERICALLY — a lexical sort puts \"9\" above \"16\", and a data directory made by one major cannot be started by another. **Measurement trap that cost the most time**: reasoning about which error the user would see is unreliable here — four plausible code paths produce four different messages, and the reported wording was reproducible by none of them on Ubuntu 24.04/PG16. What settled it was building a harness that calls `EnsureDatabase` directly and running it as a real non-root user with the real sudoers rule, then isolating each defect with a one-variable control (widen sudoers to `(ALL)` and nothing else changes → provisioning succeeds; prepend /usr/lib/postgresql/16/bin → the fallback completes). Each of the four fixes was reverted individually and its test re-run: two controls initially failed to COMPILE rather than reproducing the symptom, which proves nothing — they were redone faithfully before being believed", "refs": ["mendixlabs/mxcli#984", "#823"]}

‎.claude/skills/mendix/run-local/SKILL.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ association catalog only at startup; behavioural changes are hot-reloaded.
5757
- **`--ensure-db`** provisions it for a fresh session: starts local Postgres if the
5858
port is down and creates the role + database if missing. It uses a service
5959
manager, or a user-owned `initdb`/`pg_ctl` cluster under `~/.mxcli/postgres`
60-
when no service becomes ready (e.g. Arch) — needing no `postgres` OS account or `sudo`.
61-
Remote hosts are only checked, not provisioned.
60+
when no service becomes ready (e.g. Arch) — the latter needing no `postgres`
61+
OS account or `sudo`. Remote hosts are only checked, not provisioned.
62+
In a **non-root devcontainer** the service start is elevated with `sudo -n`
63+
(Debian's init script aborts on a permission denial before it reaches any
64+
cluster), and the superuser is reached through root where the sudoers policy
65+
permits only that target — the devcontainer default. Both are non-interactive,
66+
so a run never blocks on a password prompt; where sudo is unavailable the
67+
user-owned cluster still carries it.
6268
The user-owned cluster persists across sessions; its server log is
6369
`~/.mxcli/postgres/server.log`. Stop it with
6470
`pg_ctl -D "$HOME/.mxcli/postgres/data" stop`. To remove it, stop it first and

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
88

99
### Fixed
1010

11+
- **`--ensure-db` could not provision PostgreSQL in a non-root devcontainer** (mendixlabs/mxcli#984) — reported as a bare `PostgreSQL did not become ready at 127.0.0.1:5432 within 20s`. This is the environment **mxcli itself generates**: `mxcli init` writes a devcontainer from `mcr.microsoft.com/devcontainers/base:bookworm` that installs the postgresql server and runs as `vscode`, and wires `run --local --setup --ensure-db` into the Claude Code SessionStart hook — so it fired on every fresh session in an initialized project, not only when someone typed the flag.
12+
13+
Three independent defects sat on that one path, each sufficient to block it. **The service start was never elevated**: Debian's `/etc/init.d/postgresql` runs under `set -e` and calls `create_socket_directory` *first*, which chmods `/var/run/postgresql` — refused for a non-root user, so the script aborts before it looks at a single cluster. **The #823 user-owned-cluster fallback was inert on Debian and Ubuntu**: `postgresql-common` wraps only the *client* tools (`psql`, `pg_isready`, `pg_ctlcluster`) into `/usr/bin` while `initdb` and `pg_ctl` live in `/usr/lib/postgresql/<major>/bin`, so the safety net for a failed service start could never deploy on the platform that needs it most. **The superuser was unreachable**: `sudo -u postgres` is refused by the devcontainer's own sudoers rule, `vscode ALL=(root) NOPASSWD:ALL`, which permits root as the only target even though the user is effectively an administrator.
14+
15+
The start is now attempted with `sudo -n` first and unprivileged second; `initdb` and `pg_ctl` are resolved from the same versioned bin directory, ranked **numerically** (a lexical sort puts `9` above `16`, and a data directory made by one major cannot be started by another); and the superuser falls back to `sudo -n -- sudo -n -u postgres`, since root may target any account. Everything stays non-interactive, so an unattended run cannot block on a password prompt.
16+
17+
Separately, the readiness timeout **discarded the service manager's own output**, which this package had already collected — the permission denial was in hand and reported as a generic timeout. A guard that names the wrong cause costs the reader more than no guard at all, so the diagnostics now travel with the error.
18+
19+
Measured on Ubuntu 24.04 / PostgreSQL 16 with a real non-root user and the real sudoers rule, against a harness calling `EnsureDatabase` directly: the shipped build fails with `exec: "initdb": executable file not found in $PATH` and, once the cluster is up, `no local PostgreSQL superuser available`; after the fix both the sudo path (2.6s) and the no-sudo user-cluster path (7.2s) provision cleanly. Note the trap — **reasoning about which error a user will see is unreliable here**: four plausible code paths produce four different messages, and the reported wording reproduced under none of them on this stack. Each fix was also reverted individually and its test re-run; two controls first failed to *compile* rather than reproducing the symptom, which proves nothing, and were redone faithfully.
20+
1121
- **`ALTER PAGE` bound the replacement widget against the wrong data context** (mendixlabs/mxcli#1076) — `replace txtPreviewPeriod with { … }` inside a data view bound `datasource: selection lvVersions` re-scoped the binding to the **outer** data view's entity (`[CE1613] "The selected attribute 'Bug.Dashboard.PeriodLabel' no longer exists."`), and the same statement inside a Gallery or DataGrid 2 sourced by a **microflow/nanoflow** dropped the binding entirely (`[CE0402] "No value specified."`, `describe` renders `ContentParams: [{1} = <unbound>]`). `mxcli check --references` and `exec` both reported success; `CREATE PAGE` binds the same widget in the same position correctly, so the defect was in the ALTER walk alone.
1222

1323
A widget's scope was resolved by **two separate walks that each knew a different subset** of Mendix's ten `Forms$*Source` kinds, which is the third time that split has produced a wrong binding (association and flow sources in FINDINGS #55, pluggable widgets in ako/mxcli#935). `Forms$ListenTargetSource` carries no `EntityRef` at all — only the listen target's *name* — so the entity walk saw no source and left the context at the enclosing data view; and the flow walk read only a widget's **top-level** `DataSource` key, so a pluggable list, whose source sits in `Object.Properties[datasource]`, was invisible to it.

0 commit comments

Comments
 (0)