diff --git a/.woodpecker/greenfield-install.yml b/.woodpecker/greenfield-install.yml new file mode 100644 index 00000000..79756585 --- /dev/null +++ b/.woodpecker/greenfield-install.yml @@ -0,0 +1,70 @@ +# C1 expected-RED gate. The fixture must execute from zero and discriminate the +# known failed postconditions; this step is green only when the fixture itself +# returns the expected non-zero and the named evidence rows are present. +when: + - event: [pull_request, manual] + - event: push + branch: [next, main] + +steps: + greenfield-git-present: + image: node:22-bookworm-slim + commands: + - | + set +e + MOSAIC_GREENFIELD_CONTAINER=1 \ + bash tools/e2e-install-test.sh --lane next --source checkout --git present \ + > /tmp/greenfield-git-present.log 2>&1 + fixture_status=$? + set -e + cat /tmp/greenfield-git-present.log + test "$fixture_status" -eq 1 + grep -Eq '^\[fixture\] resolved lane=next .*version=[0-9]+\.[0-9]+\.[0-9]+-next\.' /tmp/greenfield-git-present.log + grep -q '^\[P1\] PASS: required tools present (including downstream git)' /tmp/greenfield-git-present.log + grep -q '^\[P3\] PASS: absolute_path=.* version=.* equals resolved lane version' /tmp/greenfield-git-present.log + grep -q '^\[P4\] FAIL: NOT-MEASURED / UNDECLARED:' /tmp/greenfield-git-present.log + grep -q '^\[P5\] FAIL:' /tmp/greenfield-git-present.log + grep -q '^\[P6\] FAIL:' /tmp/greenfield-git-present.log + grep -q '^\[P8\] FAIL:' /tmp/greenfield-git-present.log + grep -q '^\[P9\] FAIL:' /tmp/greenfield-git-present.log + + greenfield-main-git-present: + image: node:22-bookworm-slim + commands: + - | + set +e + MOSAIC_GREENFIELD_CONTAINER=1 \ + bash tools/e2e-install-test.sh --lane main --source checkout --git present \ + > /tmp/greenfield-main-git-present.log 2>&1 + fixture_status=$? + set -e + cat /tmp/greenfield-main-git-present.log + test "$fixture_status" -eq 1 + grep -Eq '^\[fixture\] resolved lane=main .*version=[0-9]+\.[0-9]+\.[0-9]+' /tmp/greenfield-main-git-present.log + grep -q '^\[P1\] PASS: required tools present (including downstream git)' /tmp/greenfield-main-git-present.log + grep -q '^\[P3\] PASS: absolute_path=.* version=.* equals resolved lane version' /tmp/greenfield-main-git-present.log + grep -q '^\[P4\] FAIL: NOT-MEASURED / UNDECLARED:' /tmp/greenfield-main-git-present.log + grep -q '^\[P5\] FAIL:' /tmp/greenfield-main-git-present.log + grep -q '^\[P6\] FAIL:' /tmp/greenfield-main-git-present.log + grep -q '^\[P8\] FAIL:' /tmp/greenfield-main-git-present.log + grep -q '^\[P9\] FAIL:' /tmp/greenfield-main-git-present.log + + greenfield-git-absent: + image: node:22-bookworm-slim + commands: + - | + set +e + MOSAIC_GREENFIELD_CONTAINER=1 \ + bash tools/e2e-install-test.sh --lane next --source checkout --git absent \ + > /tmp/greenfield-git-absent.log 2>&1 + fixture_status=$? + set -e + cat /tmp/greenfield-git-absent.log + test "$fixture_status" -eq 1 + grep -q '^\[fixture\] installer_exit=1 done_claims=0' /tmp/greenfield-git-absent.log + grep -q '^\[P1\] FAIL: undeclared/missing prerequisite(s)=git;' /tmp/greenfield-git-absent.log + grep -q '^\[P3\] FAIL: .*executable=no' /tmp/greenfield-git-absent.log + if grep -q 'Done\.' /tmp/greenfield-git-absent.log; then + echo 'git-absent state-machine run falsely certified Done' >&2 + exit 1 + fi diff --git a/README.md b/README.md index 9bccfc60..5bd090fa 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Mosaic gives you a unified launcher for Claude Code, Codex, OpenCode, and Pi — ## Quick Install ```bash -curl -fsSL https://mosaicstack.dev/install.sh | bash +bash -o pipefail -c 'curl -fsSL https://mosaicstack.dev/install.sh | bash' ``` Or use the direct URL: @@ -30,6 +30,16 @@ This installs both components: | **Framework** | Bash launcher, guides, runtime configs, tools, skills | `~/.config/mosaic/` | | **@mosaicstack/mosaic** | Unified `mosaic` CLI — TUI, gateway client, wizard, auto-updater | `~/.npm-global/bin/` | +### Install lanes + +| Lane | Command | Use when | Source | +| ------------------------ | ------------------------------------- | ----------------------------------------------------- | ------------------------------------------------------------------------------------------- | +| Stable | `bash tools/install.sh` | You want the released Mosaic CLI/framework | npm registry `@mosaicstack/mosaic@latest` + framework archive at `main` | +| Prerelease integration | `bash tools/install.sh --next` | You want the current `next` integration branch | Exact `@next` CLI/gateway versions + pinned `next` framework commit; pinned-source fallback | +| Contributor/source build | `bash tools/install.sh --dev --ref X` | You are testing a branch before release; `--ref` wins | Build-from-source at the requested ref | + +`--next` selects the prerelease integration lane. It installs the exact CLI/gateway versions resolved from the aligned `@next` tags, and pins the framework archive to the resolved `next` commit. If the registry path fails, it builds from that pinned source. An explicit `--ref` or `MOSAIC_REF` wins and selects source mode. + After install, the wizard runs automatically or you can invoke it manually: ```bash @@ -38,10 +48,14 @@ mosaic wizard # Full guided setup (gateway install → verify) ### Requirements -- Node.js ≥ 20 -- npm (for global @mosaicstack/mosaic install) +- Linux x86_64 with glibc (Debian is the greenfield CI platform; musl/Alpine, macOS, and ARM64 currently fail as unsupported) +- Node.js ≥ 20 and npm ≥ 9 +- `bash`, `curl`, `git`, `python3`, `tar`, and standard core utilities (`awk`, `df`, `find`, `flock`, `grep`, `install`, `realpath`, `sed`, `sha256sum`, `stat`, `sync`) +- At least 256 MiB free disk and 1,000 free inodes at the npm prefix - One or more runtimes: [Claude Code](https://docs.anthropic.com/en/docs/claude-code), [Codex](https://github.com/openai/codex), [OpenCode](https://opencode.ai), or [Pi](https://github.com/mariozechner/pi-coding-agent) +The installer evaluates canonical phases P0–P9 and does not print `Done.` unless every committed postcondition passes. A failed phase exits non-zero, names the phase, and points to its durable journal under `${XDG_STATE_HOME:-~/.local/state}/mosaic/install/`. See [Installer state machine and recovery](docs/guides/installer-state-machine.md). + ## Usage ### Launching Agent Sessions @@ -337,7 +351,7 @@ Each stage has a dispatch mode (`exec` for research/review, `yolo` for coding), Run the installer again — it handles upgrades automatically: ```bash -curl -fsSL https://mosaicstack.dev/install.sh | bash +bash -o pipefail -c 'curl -fsSL https://mosaicstack.dev/install.sh | bash' ``` Or use the direct URL: @@ -358,15 +372,17 @@ The CLI also performs a background update check on every invocation (cached for ### Installer Flags ```bash -bash tools/install.sh --check # Version check only +bash tools/install.sh --check # Side-effect-free P0-P8 postcondition check bash tools/install.sh --framework # Framework only (skip npm CLI) bash tools/install.sh --cli # npm CLI only (skip framework) -bash tools/install.sh --ref v1.0 # Install from a specific git ref +bash tools/install.sh --next # Prerelease lane: exact @next versions + pinned-source fallback +bash tools/install.sh --dev # Contributor lane: source build at --ref/main +bash tools/install.sh --ref v1.0 # Install from a specific git ref (--ref wins over --next) bash tools/install.sh --yes # Non-interactive, accept all defaults bash tools/install.sh --no-auto-launch # Skip auto-launch of wizard ``` -The installer rejects unrecognized flags or positional arguments before making changes and prints the supported-option usage. +The installer rejects unrecognized flags or positional arguments before making changes and prints the supported-option usage. `--check` reports one PASS/FAIL row for each P0–P8 predicate and exits non-zero if any row fails; it does not create the npm prefix, lock, journal, manifest, or runtime files. ## Contributing diff --git a/docs/PRD.md b/docs/PRD.md index e3e8c606..b92d38ac 100644 --- a/docs/PRD.md +++ b/docs/PRD.md @@ -1379,7 +1379,7 @@ A from-zero install can report success while leaving the target host unusable be ### Normative requirements -1. The installer SHALL implement the canonical P0–P9 numbering from the greenfield-install PRD v2: P0 Resolve context; P1 Preflight; P2 Acquire artifacts; P3 Install CLI; P4 Install framework + skills; P5 Identity; P6 Runtime linking / activation; P7 Services; P8 Shell discoverability; P9 Verify + commit. +1. The installer SHALL implement the canonical P0–P9 numbering from the greenfield-install PRD v2: P0 Resolve context; P1 Preflight; P2 Acquire artifacts; P3 Install CLI; P4 Install framework + skills; P5 Identity; P6 Runtime linking / activation; P7 Services; P8 Shell discoverability; P9 Verify + commit. P2 is scoped to installer-distribution artifacts and SHALL NOT foreclose credentialed downstream acquisition. P5 owns validating any credential capability required by requested downstream work; P7 may provision credential-dependent resources only after that P5 postcondition commits. 2. Every phase SHALL declare preconditions, action, committed postconditions, and rollback. An unverifiable postcondition SHALL fail the install non-zero with the named phase and a remediation line; no best-effort failure may still certify success. P1's required-tool closure includes tools invoked by later phases, including `git`; a downstream prerequisite may not remain undeclared and degrade silently. 3. A durable mutation journal SHALL open before the first mutation and commit at P9. Fallible command output needed to diagnose a phase SHALL be journaled and surfaced, never discarded. 4. `--check` SHALL run exactly the P0–P8 postcondition predicates without mutation, report each phase PASS/FAIL, and exit non-zero if any predicate fails. diff --git a/docs/SITEMAP.md b/docs/SITEMAP.md index 3f5a296c..31411227 100644 --- a/docs/SITEMAP.md +++ b/docs/SITEMAP.md @@ -9,6 +9,11 @@ - [Whole mutator-class gate](architecture/mutator-class-gate.md) — default-deny policy, revoke-first/promote-last state machine, TTL, runtime adapters, and T-B/T-C assurance boundary. - [Compaction revocation lifecycle](architecture/compaction-revocation.md) — Claude/Pi observer matrix, same-PID generation rollover, failure fencing, and the named bounded residual stale window. +## Installation and upgrades + +- [Installer state machine and recovery](guides/installer-state-machine.md) — canonical P0–P9 phases, side-effect-free checks, durable journal states, rollback/remediation, and the Debian greenfield CI gate. +- [Upgrade safety and recovery](guides/upgrade-safety-and-recovery.md) — framework ownership, durable operator snapshots, verify net, and projection regeneration. + ## CLI and skill management - [Skill registration user guide](guides/user-guide.md#claude-code-skill-registration) — register, unregister, list statuses, automatic install/update reconciliation, and Claude reload behavior. diff --git a/docs/guides/installer-state-machine.md b/docs/guides/installer-state-machine.md new file mode 100644 index 00000000..1b2eb840 --- /dev/null +++ b/docs/guides/installer-state-machine.md @@ -0,0 +1,99 @@ +# Installer State Machine and Recovery + +The unified installer uses a transactional P0–P9 model. It may report success only after P9 reasserts every applicable committed postcondition. Internal phases invoke the CLI by P3's absolute path; shell discovery is checked only at P8. + +## Canonical phases + +| Phase | Responsibility | Failure disposition | +| ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | +| P0 Resolve context | State target user, HOME, shell, privilege mode, architecture, libc, Node, and npm | Fail before mutation | +| P1 Preflight | Validate downstream tool closure (including `git` and `python3`), writable prefix, registry lane, disk/inodes, and exclusive lock | Fail before target mutation | +| P2 Acquire artifacts | Resolve exact registry versions and an immutable framework commit; record lane and SHA-256 | Discard temporary work | +| P3 Install CLI | Install at the configured absolute prefix and require exact resolved version | Restore the prior prefix/npmrc snapshot | +| P4 Install framework + skills | Sync framework and consume a checkout-free, lane/versioned shipped-skill declaration | Restore prior framework/runtime trees | +| P5 Identity | Validate SOUL/USER content, owner, and mode; establish any credential capability requested downstream | Restore generated identity/credential binding | +| P6 Runtime linking / activation | Evaluate activation honestly; never treat dead enforcement hooks as active readiness | Restore runtime activation files | +| P7 Services | Provision only requested services/resources after any required P5 credential commits | Stop and restore requested services/resources | +| P8 Shell discoverability | Require fresh login and non-login shells of the actual target shell to resolve P3's path | Restore shell profiles | +| P9 Verify + commit | Re-run P0–P8, commit the manifest, and seal the journal | Leave an honestly reported resumable failure or restore the pre-install snapshot | + +The phase numbers are a cross-workstream contract and must not be renumbered. + +## Side-effect-free check + +```bash +bash tools/install.sh --check # stable/latest lane +bash tools/install.sh --check --next # prerelease lane +``` + +`--check`: + +- emits exactly one `[P0]` through `[P8]` PASS/FAIL row; +- exits non-zero if any predicate fails; +- does not create the npm prefix, lock, journal, manifest, shell profile, or runtime file; +- uses temporary npm observation storage outside the target HOME and removes it before exit. + +P4 currently fails as `NOT-MEASURED / UNDECLARED` until the installer publishes `~/.config/mosaic/.install-shipped-skills.json`. C1 deliberately does not select among the conflicting candidate populations; C5 owns publishing and fulfilling that declaration. Once present, the P4 predicate requires the declaration's lane/version to match the resolved install and every named skill to remain contained under `skills//SKILL.md` with matching loadable frontmatter. + +## Durable journal + +Each mutating run creates a private transaction directory: + +```text +${XDG_STATE_HOME:-~/.local/state}/mosaic/install/ + active.json + / + journal.ndjson + journal.ndjson.sha256 # committed runs only + commands.log + snapshot/ +``` + +Before each mutation scope is touched, `journal.ndjson` records: + +- phase and path; +- whether prior state existed and where its snapshot lives; +- the reversal action; +- the captured command-output location and command status. + +Journal, action-status, manifest, or command-log write/sync failure is fatal. An unrecorded mutation is not allowed. Successful P9 runs append a seal event, write the SHA-256 sidecar, and make the journal and sidecar read-only. Required P4/P6 action failures are persisted in the manifest so a later `--check` cannot turn a failed action into a false pass. + +Rollback roots must be non-overlapping, non-symlinked, target-user-owned strict descendants of canonical `HOME`; unsafe custom `MOSAIC_HOME`/`MOSAIC_PREFIX` values fail at P0. The same validation runs again immediately before recursive rollback. The OS lock is concurrency authority: if a process dies while `active.json` still says `in-progress`, a retry that acquires the free lock preserves the stale projection as `prior-active.json` and proceeds from the honestly retained partial state. + +`active.json` is the current projection: + +- `in-progress`: incomplete/open transaction; +- `rolled-back`: a fault restored the snapshot; +- `rollback-failed`: restoration failed or refused a replaced/unsafe target and requires manual recovery; +- `failed-resumable`: named postconditions failed and the recorded partial state remains for remediation; +- `committed`: P9 passed and the journal is sealed. + +## Failure recovery + +1. Read the named phase and remediation line from installer stderr. +2. Inspect `active.json`, then the referenced `journal.ndjson` and `commands.log`. Command output needed to diagnose a failure is preserved and surfaced; it is not redirected away. +3. For `rolled-back`, verify the target paths match their pre-install state before retrying. +4. For `failed-resumable`, repair the named phase owner requirement, then run `install.sh --check` before retrying the installer. +5. Do not activate the #869 enforcement hooks merely to turn P6 green. A broker-less host with those hooks is a failed P6 state. + +## Greenfield CI gate + +`.woodpecker/greenfield-install.yml` runs `tools/e2e-install-test.sh` from zero in Debian/glibc as a non-root uid with `env -i`. No host HOME, npm cache, credentials, or bind mount enters the target process. Checkout mode packages the complete current checkout into an archive, pins its SHA-256 through an internal fixture seam, and copies the self-contained fixture into the container; framework-installer changes in the PR are therefore exercised rather than fetched from an older remote branch. + +The C1 gate intentionally validates an attributable RED while C2–C5 remain open: + +- `git` present: P1 and strict P3 pass; P4/P5/P6/P8 fail for their own reasons; P9 refuses success. +- `git` absent: P1 fails before target mutation and the installer emits no `Done.`. + +The fixture is lane-parametric: + +```bash +bash tools/e2e-install-test.sh --lane next --git present +bash tools/e2e-install-test.sh --lane main --git present +``` + +CI exercises both lane parameters as expected-RED structural checks. The authoritative main-lane promotion acceptance and issue closure remain owned by #1037. + +## Source trust boundary + +Remote source mode pins the resolved commit, records the archive SHA-256, limits compressed/expanded size and entry count, and rejects traversal, links, devices, and special files before extraction. This provides immutable run provenance and archive safety, not an independent authenticity root. Signed artifact metadata/provenance is explicitly deferred by the canonical greenfield PRD; C1 does not invent a signing system. The checkout CI seam does verify an expected digest supplied independently by the fixture. diff --git a/docs/guides/upgrade-safety-and-recovery.md b/docs/guides/upgrade-safety-and-recovery.md index 1f755cb7..ba6837c2 100644 --- a/docs/guides/upgrade-safety-and-recovery.md +++ b/docs/guides/upgrade-safety-and-recovery.md @@ -12,6 +12,20 @@ with no snapshot to fall back to. Protection is layered. Each layer is independent; a later layer catches what an earlier one misses. +## Layer 0 — Transaction journal (install-wide recovery) + +The unified installer opens a private journal under +`${XDG_STATE_HOME:-~/.local/state}/mosaic/install/` before the first target +mutation. Every mutation scope records its path, prior snapshot, and reversal +instructions before it is touched. Journal write/sync failure is fatal, and P9 +seals successful journals with a SHA-256 sidecar. See +[Installer state machine and recovery](./installer-state-machine.md). + +This transaction journal is distinct from the retained operator-only backup +below. The transaction journal is required for correctness and rollback; +Layer 2's durable backup remains a separately stated, fail-open recovery bonus +for a manifest bug that the normal transaction did not detect. + ## Layer 1 — Manifest-owned sync (prevention) The single source of truth for ownership is diff --git a/docs/scratchpads/1050-install-state-machine-red-fixture.md b/docs/scratchpads/1050-install-state-machine-red-fixture.md index 33f42fe9..a8a9b609 100644 --- a/docs/scratchpads/1050-install-state-machine-red-fixture.md +++ b/docs/scratchpads/1050-install-state-machine-red-fixture.md @@ -6,7 +6,7 @@ Implement C1 from the canonical greenfield-install PRD v2: a transactional P0– ## Authority and scope -- Canonical requirements: `jason.woltje/jarvis-brain` `docs/plans/2026-08-04-greenfield-install-blockers-PRD-v2.md`, read from local `origin/main` object `b2b6ed41f5aff5ea964e69b7c701cb45718742fa`; remote currency is **unestablished** because authenticated fetch returned repository-not-found. +- Canonical requirements: `jason.woltje/jarvis-brain` `docs/plans/2026-08-04-greenfield-install-blockers-PRD-v2.md`. Currency was re-derived after compaction: authenticated fetch resolved `origin/main` to `cb23e5fbc8a282fa967b93d7a134fa48d11b4bb1`; the PRD and charters are byte-identical to the previously read remote copies. - Tracking: `mosaicstack/stack#1050` on `git.mosaicstack.dev` (author read back as `be-coder-05`). - Base: `origin/next` `4df478cdd150fdf8d52ea109f02ade5d85017acd`. - Out of scope: PATH, skills, headless wizard/identity, activation remediation, #869 wiring, RM-02, main promotion. @@ -40,23 +40,29 @@ Implement C1 from the canonical greenfield-install PRD v2: a transactional P0– ## Progress -- [x] Charter, doctrine, delivery/CI/QA/docs guides read. -- [x] Canonical PRD v2 and charters read from local origin object; numbering reconciles with the TL spec. No numbering conflict found. TL additions (early durable journal and INV-C) are additive, not contradictory. +- [x] Charter, doctrine, delivery/CI/QA/docs guides read and re-anchored after compaction. +- [x] Canonical PRD v2/v3 addenda and charters read from fetched `origin/main`; numbering reconciles with the TL spec. No numbering conflict found. INV-B/C/D are binding and implemented without renumbering. - [x] Target base reachability verified with `merge-base --is-ancestor`. - [x] Issue #1050 created and provider author read back. - [x] Initial RED captured; TL rejected P4's repo-root count as a false RED. Four populations disagree (framework payload 1, repo root 13, sync store 101 in the fixture, W-jarvis observation 7), so C1 now requires a checkout-free declared shipped-set artifact and reports P4 `NOT-MEASURED / UNDECLARED` until C5 supplies it. - [x] P6 strengthens #869: the two dead enforcement hooks reproduce from zero on a clean broker-less container. C1 asserts the breach but neither wires nor unwires it. - [x] P1 false pass identified from the P4 evidence row: `git` is absent from the Debian base and was undeclared even though skill sync shells out to it. C1 adds `git` to P1; the fixture matrix preserves absent/present controls. The prior claim that web1's missing runtime skills reproduce this greenfield mechanism is withdrawn by the TL and is not carried here. -- [ ] Corrected RED transcript captured and reported. -- [ ] State machine implemented. -- [ ] Reviews complete. +- [x] Corrected RED transcript captured and reported, including the git-present/absent controls and strict P3 PASS. +- [x] State-machine implementation complete: private pre-mutation journal/snapshot, P0–P8 `--check`, P2–P8 fault seam, rollback, durable manifest/journal seal, action-status persistence, safe rollback roots, and stale-projection recovery. +- [x] Debian/glibc checkout fixture now packages the complete current checkout, verifies its digest in-container, and reaches the expected attributable RED without host inheritance. +- [ ] Reviews complete. Automated review defects around Bash conditional errexit, explicit exits, P4/P6 persisted action status, dev/offline source resolution, stale locks, checkout coverage, and rollback path safety were remediated. Remaining automated objections are the charter-mandated expected RED/C5 boundary and signed provenance, which the canonical PRD explicitly defers; independent informed review is still required. ## Risks / blockers - The deployed create wrappers do not expose `--dry-run`; identity preflight was performed through `pr-merge.sh --dry-run` on the same HOMELAB repo, which resolved `git.mosaicstack.dev` + `be-coder-05`. The issue create then fell back from tea to the API but provider read-back confirmed author `be-coder-05`. - `next` is an integration lane; `main` promotion remains #1037-owned. - #869 must remain staged and inactive. +- Late sequencing input MB-BRAIN-01 is accommodated without implementation or renumbering: P2 covers installer distribution only; P5 owns requested credential capability; P7 leaves an ordered seam for credential-dependent resource provisioning after P5. ## Verification log -(To be updated with exact commands and resulting objects.) +- `bash -n` and ShellCheck pass for all changed shell surfaces; `git diff --check` passes. +- `bash tools/install-state-machine.test.sh` passes, including exact P0–P8 rows, good/bad discrimination, persisted P4/P6 action failures, P2–P8 rollback, unsafe/overlapping/symlink roots, stale `active.json`, and fatal journal initialization. +- `bash tools/install-next-lane.test.sh` passes, including exact `@next` versions, immutable source fallback, source-build/archive-failure rollback, offline `--dev`, explicit refs, and prerelease suffix mismatch. +- `bash tools/e2e-install-test.sh --lane next --source checkout --git present` returns the required expected RED in clean Debian/glibc as uid 1001: installer P0/P1/P2/P3/P7 PASS; P4/P5/P6/P8 and P9 blocking; no `Done.` claim; checkout archive digest pinned and current framework installer exercised. +- Earlier repository gates passed: `pnpm typecheck`, `pnpm lint`, `pnpm format:check`, `pnpm test:installer`, upgrade manifest/rollback/durable-snapshot/migration suites, and focused `@mosaicstack/mosaic` tests with an isolated npm prefix. Full rerun is required after final edits. diff --git a/package.json b/package.json index f52dd9d6..82155fdc 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "clean:generated": "node scripts/clean-generated.mjs", "typecheck": "pnpm preflight && turbo run typecheck", "test:checkout": "node --test scripts/*.test.mjs", - "test": "pnpm test:checkout && turbo run test", + "test": "pnpm test:checkout && turbo run test && pnpm run test:installer", + "test:installer": "bash tools/install-state-machine.test.sh && bash tools/install-next-lane.test.sh", "format": "prettier --write \"**/*.{ts,tsx,js,jsx,json,md}\"", "format:check": "prettier --check \"**/*.{ts,tsx,js,jsx,json,md}\"", "prepare": "node scripts/install-hooks.mjs" diff --git a/packages/mosaic/framework/install.sh b/packages/mosaic/framework/install.sh index 7d262a24..f4352276 100755 --- a/packages/mosaic/framework/install.sh +++ b/packages/mosaic/framework/install.sh @@ -58,6 +58,7 @@ done # packages/mosaic/src/framework/manifest.ts — both consume framework-manifest.txt. # Sourcing does not run its CLI dispatch (guarded by BASH_SOURCE==$0). # shellcheck source=tools/_lib/manifest.sh +# shellcheck disable=SC1091 # Dynamic SOURCE_DIR; the path is validated by set -e. source "$SOURCE_DIR/tools/_lib/manifest.sh" # Which paths a keep-mode upgrade may touch is no longer a hand-maintained @@ -222,12 +223,14 @@ prune_durable_snapshots() { [[ "$keep" =~ ^[0-9]+$ ]] && (( keep >= 1 )) || keep=5 list="$(mktemp)" if ! find "$root" -maxdepth 1 -type d -name 'pre-update-*' > "$list"; then + warn "Backup pruning skipped; policy: retention cleanup is optional and a failed enumeration must preserve every existing recovery snapshot." rm -f "$list"; return 0 fi # Newest-first ordering needs `sort` (`-o` writes back in place — no `mv` # dependency); if it is somehow unavailable, leave the backups untouched rather # than risk pruning in an undefined order. if ! LC_ALL=C sort -r -o "$list" "$list" 2>/dev/null; then + warn "Backup pruning skipped; policy: ordering failure preserves all snapshots rather than risking deletion in an undefined order." rm -f "$list"; return 0 fi while IFS= read -r d; do @@ -266,7 +269,11 @@ make_durable_snapshot() { warn "Durable snapshot skipped: cannot create backup dir $root (upgrade continues; operator files remain manifest-protected)." return 0 fi - chmod 700 "$root" 2>/dev/null || true + if ! chmod 700 "$root"; then + umask "$old_umask" + warn "Durable snapshot skipped: backup root permissions could not be made private; policy: never write operator data to an insufficiently protected location." + return 0 + fi dir="$root/pre-update-$ts" if [[ -e "$dir" ]]; then # same-second re-run: disambiguate local n=1; while [[ -e "$dir-$n" ]]; do n=$((n + 1)); done; dir="$dir-$n" @@ -281,7 +288,10 @@ make_durable_snapshot() { if ! enumerate_operator_files "$list"; then umask "$old_umask" warn "Durable snapshot skipped: could not enumerate operator files (upgrade continues)." - rm -f "$list"; rmdir "$dir" 2>/dev/null || true + rm -f "$list" + if ! rmdir "$dir"; then + warn "Durable snapshot cleanup left $dir in place; policy: preserve unexpected content rather than deleting it recursively." + fi return 0 fi while IFS= read -r -d '' rel; do @@ -292,12 +302,18 @@ make_durable_snapshot() { warn "Durable snapshot: could not copy operator file '$rel' (skipped)." continue fi - chmod 600 "$dst" 2>/dev/null || true + if ! chmod 600 "$dst"; then + rm -f "$dst" + warn "Durable snapshot: copied '$rel' could not be made private and was removed; policy: do not retain an insecure recovery copy." + continue + fi count=$((count + 1)) done < "$list" rm -f "$list" - # Tighten every dir the copy created (mkdir -p honors umask, but be explicit). - find "$dir" -type d -exec chmod 700 {} + 2>/dev/null || true + # Tighten every dir the copy created (mkdir -p already honored umask 077). + if ! find "$dir" -type d -exec chmod 700 {} +; then + warn "Durable snapshot directory permission recheck failed; policy: continue because every directory was created under umask 077, while retaining the diagnostic." + fi umask "$old_umask" # UMASK-RESTORE-NORMAL — restore before the upgrade proper resumes (see above) DURABLE_SNAPSHOT_DIR="$dir" ok "Durable pre-update snapshot: $count operator file(s) saved to $dir (recover with: mosaic restore --list)" @@ -344,7 +360,9 @@ verify_operator_surface() { continue fi if cp "$snap" "$cur"; then - chmod 600 "$cur" 2>/dev/null || true + if ! chmod 600 "$cur"; then + warn "Operator file '$rel' was restored but its mode could not be tightened to 0600; policy: preserve recovered content and require manual permission repair." + fi warn "Operator file was modified by the upgrade and has been restored from the pre-update snapshot: $rel" healed=$((healed + 1)) else @@ -535,7 +553,7 @@ sync_framework_keep() { # (unreadable dir) is surfaced as a warning rather than silently swallowed; # the "directory not empty" races we tolerate are ignored via -delete's own # rc, not by hiding stderr — so a real error is still visible to the operator. - if ! find "$dst/$root" -type d -empty -delete 2>/dev/null; then + if ! find "$dst/$root" -type d -empty -delete; then warn "prune: could not fully sweep empty framework dirs under $root (left as-is)" fi done < <(manifest_subtree_roots) @@ -581,7 +599,7 @@ run_migrations() { MIGRATION_REMOVED_PATHS+=("bin" "rails") if [[ -d "$TARGET_DIR/bin" ]]; then ok "Removing legacy bin/ directory (executables now in npm CLI)" - rm -rf "$TARGET_DIR/bin" + rm -rf "${TARGET_DIR:?}/bin" fi # Remove old mosaic PATH entry from shell profiles @@ -706,13 +724,23 @@ mkdir -p "$TARGET_DIR/credentials" # by `mosaic init` from templates with user-supplied values. reconcile_framework_files -# Ensure tool scripts are executable -find "$TARGET_DIR/tools" -name "*.sh" -exec chmod +x {} + 2>/dev/null || true -find "$TARGET_DIR/tools/_scripts" -type f -exec chmod +x {} + 2>/dev/null || true +# Ensure tool scripts are executable. These are P4 postconditions, not +# best-effort cleanup: a chmod failure leaves shipped tools unloadable. +if ! find "$TARGET_DIR/tools" -name "*.sh" -exec chmod +x {} +; then + fail "Could not mark shipped shell tools executable." + exit 1 +fi +if ! find "$TARGET_DIR/tools/_scripts" -type f -exec chmod +x {} +; then + fail "Could not mark shipped runtime scripts executable." + exit 1 +fi # git-credential-mosaic (per-agent Gitea identity helper) ships without a .sh -# suffix — git resolves credential helpers by exact name/path, not extension — -# so the *.sh glob above does not cover it; chmod it explicitly. -[[ -f "$TARGET_DIR/tools/git/git-credential-mosaic" ]] && chmod +x "$TARGET_DIR/tools/git/git-credential-mosaic" 2>/dev/null || true +# suffix — git resolves credential helpers by exact name/path, not extension. +if [[ -f "$TARGET_DIR/tools/git/git-credential-mosaic" ]] \ + && ! chmod +x "$TARGET_DIR/tools/git/git-credential-mosaic"; then + fail "Could not mark git-credential-mosaic executable." + exit 1 +fi ok "Framework synced to $TARGET_DIR" @@ -739,49 +767,110 @@ step "Post-install tasks" SCRIPTS="$TARGET_DIR/tools/_scripts" +# Capture every fallible post-install command. A failure's text is surfaced and +# also appended to the parent transaction's private command log. Failure to +# write that log is fatal: continuing would recreate the false-clean diagnosis +# INV-C forbids. +record_phase_outcome() { + local phase="$1" status="$2" reason="$3" + [[ -n "${MOSAIC_INSTALL_PHASE_STATUS_FILE:-}" ]] || return 0 + if ! printf '%s\t%s\t%s\n' "$phase" "$status" "$reason" >> "$MOSAIC_INSTALL_PHASE_STATUS_FILE" \ + || ! sync "$MOSAIC_INSTALL_PHASE_STATUS_FILE"; then + fail "Could not durably record $phase action outcome for the parent transaction." + exit 1 + fi +} + +run_captured() { + local label="$1" output status=0 + shift + output="$(mktemp "${TMPDIR:-/tmp}/mosaic-post-install.XXXXXX.log")" + if "$@" >"$output" 2>&1; then status=0; else status=$?; fi + if [[ -n "${MOSAIC_INSTALL_COMMAND_LOG:-}" ]]; then + if ! { printf '\n=== %s (exit=%s) ===\n' "$label" "$status"; cat "$output"; } >> "$MOSAIC_INSTALL_COMMAND_LOG" \ + || ! sync "$MOSAIC_INSTALL_COMMAND_LOG"; then + cat "$output" >&2 + rm -f "$output" + fail "Could not durably append '$label' diagnostics to the install command log." + exit 1 + fi + fi + if [[ "$status" -ne 0 ]]; then cat "$output" >&2; fi + rm -f "$output" + return "$status" +} + if [[ -x "$SCRIPTS/mosaic-link-runtime-assets" ]]; then link_args=() [[ "$ALLOW_INACTIVE_ENFORCEMENT" == "1" ]] && link_args+=(--allow-inactive-enforcement) - # stdout is suppressed as before, but stderr is left connected: the - # install-ordering guard's FAIL LOUD message (#869 Point-1 C2) must reach - # the operator, not be swallowed silently. - if "$SCRIPTS/mosaic-link-runtime-assets" "${link_args[@]}" >/dev/null; then + if run_captured "runtime asset linking" "$SCRIPTS/mosaic-link-runtime-assets" "${link_args[@]}"; then + record_phase_outcome P6 committed "runtime asset linker exited zero" ok "Runtime assets linked" else - warn "Runtime asset linking failed (non-fatal) — see message above for details." + record_phase_outcome P6 failed "runtime asset linker exited non-zero" + warn "Runtime asset linking did not commit; policy: continue only to enumerate all phase diagnostics, while P6/P9 remain blocking." fi +else + record_phase_outcome P6 failed "required runtime asset linker is missing or not executable" + warn "Runtime asset linking was not attempted; policy: a missing required linker remains a blocking P6/P9 failure." fi if [[ -x "$SCRIPTS/mosaic-ensure-sequential-thinking" ]]; then - if "$SCRIPTS/mosaic-ensure-sequential-thinking" >/dev/null 2>&1; then + if run_captured "sequential-thinking setup" "$SCRIPTS/mosaic-ensure-sequential-thinking"; then ok "sequential-thinking MCP configured" + elif [[ "${MOSAIC_ALLOW_MISSING_SEQUENTIAL_THINKING:-0}" == "1" ]]; then + record_phase_outcome P6 failed "sequential-thinking setup failed under diagnostic-continuation compatibility mode" + warn "sequential-thinking setup did not commit; policy: the unified installer compatibility flag allows diagnostic continuation, while P6/P9 remain blocking." else - if [[ "${MOSAIC_ALLOW_MISSING_SEQUENTIAL_THINKING:-0}" == "1" ]]; then - warn "sequential-thinking MCP setup bypassed (MOSAIC_ALLOW_MISSING_SEQUENTIAL_THINKING=1)" - else - fail "sequential-thinking MCP setup failed (hard requirement)." - exit 1 - fi + fail "sequential-thinking MCP setup failed (hard requirement)." + exit 1 fi fi if [[ -x "$SCRIPTS/mosaic-ensure-excalidraw" ]]; then - "$SCRIPTS/mosaic-ensure-excalidraw" >/dev/null 2>&1 && ok "excalidraw MCP configured" || warn "excalidraw MCP setup failed (non-fatal)" + if run_captured "excalidraw setup" "$SCRIPTS/mosaic-ensure-excalidraw"; then + ok "excalidraw MCP configured" + else + warn "excalidraw setup did not commit; policy: optional integration failure is retained in the journal and does not define core install readiness." + fi fi -if [[ "${MOSAIC_SKIP_SKILLS_SYNC:-0}" != "1" ]] && [[ -x "$SCRIPTS/mosaic-sync-skills" ]]; then - "$SCRIPTS/mosaic-sync-skills" >/dev/null 2>&1 && ok "Skills synced" || warn "Skills sync failed (non-fatal)" +if [[ "${MOSAIC_SKIP_SKILLS_SYNC:-0}" == "1" ]]; then + record_phase_outcome P4 failed "required skills sync explicitly skipped" + warn "Skills sync was skipped; policy: diagnostic continuation is allowed, but P4/P9 cannot certify an incomplete requested framework install." +elif [[ -x "$SCRIPTS/mosaic-sync-skills" ]]; then + if run_captured "skills sync" "$SCRIPTS/mosaic-sync-skills"; then + record_phase_outcome P4 committed "skills sync exited zero" + ok "Skills synced" + else + record_phase_outcome P4 failed "skills sync exited non-zero" + warn "Skills sync did not commit; policy: continue to collect P4 diagnostics, but P4/P9 must not certify the install." + fi +else + record_phase_outcome P4 failed "required skills sync command is missing or not executable" + warn "Skills sync was not attempted; policy: a missing required sync command remains a blocking P4/P9 failure." fi if [[ -x "$SCRIPTS/mosaic-migrate-local-skills" ]]; then - "$SCRIPTS/mosaic-migrate-local-skills" --apply >/dev/null 2>&1 && ok "Local skills migrated" || warn "Local skill migration failed (non-fatal)" + if run_captured "local skills migration" "$SCRIPTS/mosaic-migrate-local-skills" --apply; then + ok "Local skills migrated" + else + record_phase_outcome P4 failed "local skills migration exited non-zero" + warn "Local skill migration did not commit; policy: preserve user content and continue diagnostics, while P4/P9 remain blocking." + fi fi if [[ -x "$SCRIPTS/mosaic-doctor" ]]; then - "$SCRIPTS/mosaic-doctor" >/dev/null 2>&1 && ok "Health audit passed" || warn "Health audit reported issues — run 'mosaic doctor' for details" + if run_captured "health audit" "$SCRIPTS/mosaic-doctor"; then + ok "Health audit passed" + else + warn "Health audit found unresolved state; policy: preserve its diagnostics and let P9 issue the authoritative failure." + fi fi -# Write version stamp AFTER everything succeeds +# The version stamp records the successfully committed framework file sync. +# Post-install failures are carried separately into P4/P6 and cannot be erased +# by this stamp. write_framework_version # ── Summary ────────────────────────────────────────────────── diff --git a/tools/e2e-install-test.sh b/tools/e2e-install-test.sh index 9f64eca8..241d3d40 100755 --- a/tools/e2e-install-test.sh +++ b/tools/e2e-install-test.sh @@ -14,6 +14,7 @@ SOURCE="${MOSAIC_INSTALL_SOURCE:-checkout}" IMAGE="${MOSAIC_INSTALL_IMAGE:-node:22-bookworm-slim}" GIT_MODE="${MOSAIC_INSTALL_GIT_MODE:-present}" INSTALLER_FILE="${MOSAIC_FIXTURE_INSTALLER_FILE:-$ROOT/tools/install.sh}" +IN_CLEAN_CONTAINER="${MOSAIC_GREENFIELD_CONTAINER:-0}" usage() { cat <<'EOF' @@ -39,38 +40,62 @@ case "$LANE" in next|main) ;; *) echo "[fixture] unsupported lane '$LANE' (expec case "$SOURCE" in checkout|remote) ;; *) echo "[fixture] unsupported source '$SOURCE' (expected checkout|remote)" >&2; exit 2 ;; esac case "$GIT_MODE" in present|absent) ;; *) echo "[fixture] unsupported git mode '$GIT_MODE' (expected present|absent)" >&2; exit 2 ;; esac -if ! command -v docker >/dev/null 2>&1; then - echo "[fixture] FAIL: Docker is required; greenfield validation was NOT RUN." >&2 - exit 2 -fi -if ! docker info >/dev/null 2>&1; then - echo "[fixture] FAIL: Docker daemon is unavailable; greenfield validation was NOT RUN." >&2 - exit 2 +if [[ "$IN_CLEAN_CONTAINER" != "1" ]]; then + if ! command -v docker >/dev/null 2>&1; then + echo "[fixture] FAIL: Docker is required; greenfield validation was NOT RUN." >&2 + exit 2 + fi + if ! docker info >/dev/null 2>&1; then + echo "[fixture] FAIL: Docker daemon is unavailable; greenfield validation was NOT RUN." >&2 + exit 2 + fi fi installer_b64="" framework_payload_count="NOT-MEASURED" repo_root_count="NOT-MEASURED" +checkout_archive="" +checkout_digest="" +checkout_content_id="" if [[ "$SOURCE" == "checkout" ]]; then installer_b64="$(base64 -w0 "$INSTALLER_FILE")" [[ -d "$ROOT/packages/mosaic/framework/skills" ]] \ && framework_payload_count="$(find "$ROOT/packages/mosaic/framework/skills" -mindepth 1 -maxdepth 1 -type d | wc -l | tr -d ' ')" [[ -d "$ROOT/skills" ]] \ && repo_root_count="$(find "$ROOT/skills" -mindepth 1 -maxdepth 1 -type d | wc -l | tr -d ' ')" + checkout_archive="$(mktemp "${TMPDIR:-/tmp}/mosaic-greenfield-checkout.XXXXXX.tar.gz")" + repo_parent="$(dirname "$ROOT")" + repo_name="$(basename "$ROOT")" + tar -C "$repo_parent" \ + --exclude='*/.git' --exclude='*/node_modules' --exclude='*/dist' \ + --exclude='*/coverage' --exclude='*/.turbo' --exclude='*/.mosaic-test-work' \ + --exclude='*/.env' --exclude='*/.env.*' \ + -czf "$checkout_archive" "$repo_name" + checkout_digest="$(sha256sum "$checkout_archive" | awk '{print $1}')" + checkout_content_id="${checkout_digest:0:40}" fi inner="$(mktemp "${TMPDIR:-/tmp}/mosaic-greenfield-inner.XXXXXX.sh")" -trap 'rm -f "$inner"' EXIT +trap 'rm -f "$inner" "$checkout_archive"' EXIT cat > "$inner" <<'INNER' #!/usr/bin/env bash set -euo pipefail export DEBIAN_FRONTEND=noninteractive apt-get update -qq -packages=(bash ca-certificates curl jq passwd util-linux) +packages=(bash ca-certificates curl jq passwd python3 util-linux) [[ "$FIXTURE_GIT_MODE" == "present" ]] && packages+=(git) apt-get install -y -qq "${packages[@]}" >/dev/null +if [[ "$FIXTURE_SOURCE" == "checkout" ]]; then + awk 'found { print } /^__MOSAIC_CHECKOUT_ARCHIVE__$/ { found=1; next }' "$0" | base64 -d > /tmp/source-checkout.tar.gz + actual_checkout_digest="$(sha256sum /tmp/source-checkout.tar.gz | awk '{print $1}')" + if [[ "$actual_checkout_digest" != "$FIXTURE_CHECKOUT_SHA256" ]]; then + echo "[fixture] checkout archive transport digest mismatch" >&2 + exit 1 + fi +fi + useradd --create-home --uid 1001 --shell /bin/bash mosaic install -d -o mosaic -g mosaic /home/mosaic/work @@ -130,7 +155,7 @@ fi # P1 Preflight missing_tools=() -for tool in bash curl git node npm tar; do +for tool in bash curl git node npm python3 tar; do command -v "$tool" >/dev/null 2>&1 || missing_tools+=("$tool") done if [[ "${#missing_tools[@]}" -eq 0 && -n "$resolved_version" && -w "$home" ]]; then @@ -173,18 +198,34 @@ printf '[P4-EVIDENCE] candidate_populations framework_payload=%s repo_root=%s sy "$FIXTURE_FRAMEWORK_PAYLOAD_COUNT" "$FIXTURE_REPO_ROOT_COUNT" "$sync_store_count" "$runtime_link_count" if [[ ! -s "$declared_set" ]]; then phase_fail P4 "NOT-MEASURED / UNDECLARED: installer published no checkout-free, lane/versioned shipped-set artifact at $declared_set" -elif node - "$declared_set" <<'NODE' +elif EXPECTED_LANE="$([[ "$lane" == next ]] && echo next || echo latest)" EXPECTED_VERSION="$resolved_version" \ + MOSAIC_SKILLS_ROOT="$mosaic_home/skills" node - "$declared_set" <<'NODE' const fs = require('fs'); +const path = require('path'); const data = JSON.parse(fs.readFileSync(process.argv[2], 'utf8')); -if (!data || typeof data !== 'object' || !['latest', 'next'].includes(data.lane) || - typeof data.version !== 'string' || !data.version || !Array.isArray(data.skills) || data.skills.length === 0 || - data.skills.some((name) => typeof name !== 'string' || !name)) process.exit(1); +const root = path.resolve(process.env.MOSAIC_SKILLS_ROOT); +if (!data || data.lane !== process.env.EXPECTED_LANE || data.version !== process.env.EXPECTED_VERSION || + !Array.isArray(data.skills) || data.skills.length === 0) process.exit(1); +for (const name of data.skills) { + if (typeof name !== 'string' || !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(name)) process.exit(1); + const skill = path.join(root, name, 'SKILL.md'); + let real; + try { real = fs.realpathSync(skill); } catch { process.exit(1); } + const text = fs.readFileSync(real, 'utf8'); + const declaredName = text.match(/^---\s*$[\s\S]*?^name:\s*([^\s]+)\s*$/m)?.[1]; + if (!real.startsWith(root + path.sep) || !fs.statSync(real).isFile() || !text || declaredName !== name) process.exit(1); +} NODE then declared_count="$(node -p "require('$declared_set').skills.length")" - phase_pass P4 "declared shipped-set artifact parses (declared_count=$declared_count); C5 owns containment/loadability fulfillment" + if [[ -s "$mosaic_home/.install-manifest.json" ]] \ + && [[ "$(node -p "require('$mosaic_home/.install-manifest.json').phaseOutcomes?.P4 || 'committed'")" == failed ]]; then + phase_fail P4 "declared skills are present but the required framework/skills action reported failure" + else + phase_pass P4 "declared shipped-set matches lane/version and all $declared_count skill(s) are contained and loadable" + fi else - phase_fail P4 "NOT-MEASURED / UNDECLARED: shipped-set artifact exists but is empty, malformed, or lacks lane/version" + phase_fail P4 "shipped-set artifact is malformed, wrong-lane/version, or its declared skills are not contained and loadable" fi # P5 Identity @@ -207,13 +248,20 @@ else fi # P6 Runtime linking / activation. #869 must remain unwired without its broker. +manifest="$mosaic_home/.install-manifest.json" broker_present=false [[ -S "${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/mosaic-lease/broker.sock" ]] && broker_present=true dead_hooks=0 if [[ -f "$home/.claude/settings.json" ]]; then dead_hooks="$(grep -Ec 'mutator-gate\.py|receipt-observer-client\.py' "$home/.claude/settings.json" || true)" fi -if [[ "$broker_present" == false && "$dead_hooks" -eq 0 ]]; then +p6_action_failed=false +if [[ -s "$manifest" ]]; then + p6_action_failed="$(node -p "require('$manifest').phaseOutcomes?.P6 === 'failed' ? 'true' : 'false'" 2>/dev/null || echo true)" +fi +if [[ "$p6_action_failed" == true ]]; then + phase_fail P6 "runtime linking/activation action reported a required failure" +elif [[ "$broker_present" == false && "$dead_hooks" -eq 0 ]]; then phase_pass P6 "broker absent and #869 enforcement hooks remain inactive" elif [[ "$broker_present" == true ]]; then phase_pass P6 "activation broker present; hook state is evaluable" @@ -257,19 +305,56 @@ exec runuser -u mosaic -- env -i \ FIXTURE_GIT_MODE="$FIXTURE_GIT_MODE" \ FIXTURE_FRAMEWORK_PAYLOAD_COUNT="$FIXTURE_FRAMEWORK_PAYLOAD_COUNT" \ FIXTURE_REPO_ROOT_COUNT="$FIXTURE_REPO_ROOT_COUNT" \ + MOSAIC_INSTALL_LOCAL_SOURCE_ARCHIVE="$([[ "$FIXTURE_SOURCE" == "checkout" ]] && echo /tmp/source-checkout.tar.gz)" \ + MOSAIC_INSTALL_LOCAL_SOURCE_COMMIT="$FIXTURE_CHECKOUT_CONTENT_ID" \ + MOSAIC_INSTALL_LOCAL_SOURCE_SHA256="$FIXTURE_CHECKOUT_SHA256" \ /bin/bash /tmp/run-as-target.sh INNER +if [[ "$SOURCE" == "checkout" ]]; then + { + printf '\n__MOSAIC_CHECKOUT_ARCHIVE__\n' + base64 "$checkout_archive" + } >> "$inner" +fi chmod 0755 "$inner" printf '[fixture] platform=Debian/glibc image=%s target_uid=1001 lane=%s source=%s git=%s\n' "$IMAGE" "$LANE" "$SOURCE" "$GIT_MODE" printf '[fixture] host inheritance: no bind mounts, no host HOME, no npm cache, no credentials\n' -docker run --rm -i \ - --network bridge \ - --env FIXTURE_LANE="$LANE" \ - --env FIXTURE_SOURCE="$SOURCE" \ - --env FIXTURE_GIT_MODE="$GIT_MODE" \ - --env FIXTURE_INSTALLER_B64="$installer_b64" \ - --env FIXTURE_FRAMEWORK_PAYLOAD_COUNT="$framework_payload_count" \ - --env FIXTURE_REPO_ROOT_COUNT="$repo_root_count" \ - "$IMAGE" /bin/bash -s < "$inner" +if [[ "$IN_CLEAN_CONTAINER" == "1" ]]; then + # Woodpecker already supplies the clean Debian container. The target install + # still runs through runuser + env -i, so CI variables/credentials do not + # enter the target user's process. + FIXTURE_LANE="$LANE" \ + FIXTURE_SOURCE="$SOURCE" \ + FIXTURE_GIT_MODE="$GIT_MODE" \ + FIXTURE_INSTALLER_B64="$installer_b64" \ + FIXTURE_CHECKOUT_SHA256="$checkout_digest" \ + FIXTURE_CHECKOUT_CONTENT_ID="$checkout_content_id" \ + FIXTURE_FRAMEWORK_PAYLOAD_COUNT="$framework_payload_count" \ + FIXTURE_REPO_ROOT_COUNT="$repo_root_count" \ + /bin/bash "$inner" +else + # Copy the self-contained script+archive into a stopped container instead of + # bind-mounting the checkout or passing host paths. The target runtime still + # inherits no host HOME/cache/credentials, and the multi-megabyte checkout + # payload avoids argv/environment size limits. + fixture_cid="$(docker create \ + --network bridge \ + --env FIXTURE_LANE="$LANE" \ + --env FIXTURE_SOURCE="$SOURCE" \ + --env FIXTURE_GIT_MODE="$GIT_MODE" \ + --env FIXTURE_INSTALLER_B64="$installer_b64" \ + --env FIXTURE_CHECKOUT_SHA256="$checkout_digest" \ + --env FIXTURE_CHECKOUT_CONTENT_ID="$checkout_content_id" \ + --env FIXTURE_FRAMEWORK_PAYLOAD_COUNT="$framework_payload_count" \ + --env FIXTURE_REPO_ROOT_COUNT="$repo_root_count" \ + "$IMAGE" /bin/bash /tmp/mosaic-greenfield-fixture.sh)" + docker cp "$inner" "$fixture_cid:/tmp/mosaic-greenfield-fixture.sh" + set +e + docker start -a "$fixture_cid" + fixture_status=$? + set -e + docker rm "$fixture_cid" >/dev/null + exit "$fixture_status" +fi diff --git a/tools/install-next-lane.test.sh b/tools/install-next-lane.test.sh new file mode 100755 index 00000000..a4fb1735 --- /dev/null +++ b/tools/install-next-lane.test.sh @@ -0,0 +1,359 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +TMP="$(mktemp -d "${TMPDIR:-/tmp}/mosaic-next-install-test-XXXXXX")" +trap 'rm -rf "$TMP"' EXIT + +FAKE_BIN="$TMP/bin" +HOME_DIR="$TMP/home" +PREFIX="$HOME_DIR/prefix" +MOSAIC_HOME="$HOME_DIR/mosaic" +STATE="$TMP/state" +LOG="$TMP/npm.log" +mkdir -p "$FAKE_BIN" "$HOME_DIR" "$STATE" + +cat > "$FAKE_BIN/npm" <<'FAKE_NPM' +#!/usr/bin/env bash +set -euo pipefail +LOG="${MOSAIC_TEST_NPM_LOG:?}" +STATE="${MOSAIC_TEST_STATE:?}" +echo "$*" >> "$LOG" + +if [[ "${1:-}" == "--version" ]]; then + echo "10.6.2" + exit 0 +fi + +install_cli() { + local version="$1" + echo "$version" > "$STATE/mosaic" + mkdir -p "${MOSAIC_PREFIX:?}/bin" + cat > "$MOSAIC_PREFIX/bin/mosaic" <&2 + exit 1 + fi + case "$2 $3" in + "@mosaicstack/mosaic@next version") echo "0.0.49-next.999" ;; + "@mosaicstack/gateway@next version") echo "${MOSAIC_TEST_GATEWAY_NEXT_VERSION:-0.0.7-next.999}" ;; + "@mosaicstack/mosaic version") echo "0.0.48" ;; + *) echo "unexpected npm view: $*" >&2; exit 1 ;; + esac + exit 0 +fi + +if [[ "$1" == "install" ]]; then + case "$*" in + *"@mosaicstack/mosaic@0.0.49-next.999"*) + install_cli "0.0.49-next.999" + ;; + *"@mosaicstack/gateway@0.0.7-next.999"*) + if [[ "${MOSAIC_TEST_FAIL_NEXT_GATEWAY_INSTALL:-0}" == "1" ]]; then + echo "forced gateway install failure" >&2 + exit 1 + fi + echo "0.0.7-next.999" > "$STATE/gateway" + ;; + *"mosaicstack-mosaic-0.0.0-source.tgz"*) + install_cli "0.0.0-source" + ;; + *"mosaicstack-gateway-0.0.0-source.tgz"*) + echo "0.0.0-source" > "$STATE/gateway" + ;; + *) echo "unexpected npm install: $*" >&2; exit 1 ;; + esac + exit 0 +fi + +if [[ "$1" == "ls" ]]; then + cli="$(cat "$STATE/mosaic" 2>/dev/null || true)" + gateway="$(cat "$STATE/gateway" 2>/dev/null || true)" + node -e ' + const cli = process.argv[1]; + const gateway = process.argv[2]; + const dependencies = {}; + if (cli) dependencies["@mosaicstack/mosaic"] = { version: cli }; + if (gateway) dependencies["@mosaicstack/gateway"] = { version: gateway }; + process.stdout.write(JSON.stringify({ dependencies })); + ' "$cli" "$gateway" + exit 0 +fi + +echo "unexpected npm command: $*" >&2 +exit 1 +FAKE_NPM +chmod +x "$FAKE_BIN/npm" + +cat > "$FAKE_BIN/curl" <<'FAKE_CURL' +#!/usr/bin/env bash +set -euo pipefail +headers=""; output=""; url="" +while [[ $# -gt 0 ]]; do + case "$1" in + -D) headers="$2"; shift 2 ;; + -o) output="$2"; shift 2 ;; + --max-filesize) shift 2 ;; + -*) shift ;; + *) url="$1"; shift ;; + esac +done +case "$url" in + */api/v1/repos/mosaicstack/stack/commits?sha=*) + printf 'HTTP/1.1 200 OK\r\ncontent-type: application/json; charset=utf-8\r\n\r\n' > "$headers" + printf '[{"sha":"1111111111111111111111111111111111111111"}]\n' > "$output" + ;; + */archive/*.tar.gz) + if [[ "${MOSAIC_TEST_CORRUPT_ARCHIVE:-0}" == "1" ]]; then + printf 'not-a-tarball\n' > "$output" + else + archive_root="$(mktemp -d)" + mkdir -p "$archive_root/stack" + printf 'fixture\n' > "$archive_root/stack/.fixture" + /bin/tar czf "$output" -C "$archive_root" stack + rm -rf "$archive_root" + fi + ;; +esac +FAKE_CURL +chmod +x "$FAKE_BIN/curl" + +cat > "$FAKE_BIN/tar" <<'FAKE_TAR' +#!/usr/bin/env bash +set -euo pipefail +dest=""; list=false +while [[ $# -gt 0 ]]; do + case "$1" in + -C) dest="$2"; shift 2 ;; + -*t*|t*) list=true; shift ;; + *) shift ;; + esac +done +[[ "$list" == true ]] && exit 0 +if [[ -z "$dest" ]]; then + echo "fake tar missing -C destination" >&2 + exit 1 +fi +mkdir -p "$dest/stack/packages/mosaic" "$dest/stack/apps/gateway" +FAKE_TAR +chmod +x "$FAKE_BIN/tar" + +cat > "$FAKE_BIN/pnpm" <<'FAKE_PNPM' +#!/usr/bin/env bash +set -euo pipefail +LOG="${MOSAIC_TEST_NPM_LOG:?}" +echo "pnpm $*" >> "$LOG" + +if [[ "$1" == "pack" ]]; then + out="" + while [[ $# -gt 0 ]]; do + case "$1" in + --pack-destination) out="$2"; shift 2 ;; + *) shift ;; + esac + done + if [[ -z "$out" ]]; then + echo "fake pnpm pack missing destination" >&2 + exit 1 + fi + mkdir -p "$out" + case "$PWD" in + */apps/gateway) touch "$out/mosaicstack-gateway-0.0.0-source.tgz" ;; + */packages/mosaic) touch "$out/mosaicstack-mosaic-0.0.0-source.tgz" ;; + *) echo "unexpected pnpm pack cwd: $PWD" >&2; exit 1 ;; + esac + exit 0 +fi + +if [[ "${MOSAIC_TEST_FAIL_PNPM_INSTALL:-0}" == "1" && "$1" == "install" ]]; then + echo "forced pnpm install failure" >&2 + exit 42 +fi + +# Other install/build commands are no-ops in this harness. +exit 0 +FAKE_PNPM +chmod +x "$FAKE_BIN/pnpm" + +reset_state() { + : > "$LOG" + rm -f "$STATE"/* +} + +prefix_fingerprint() { + if [[ ! -d "$PREFIX" ]]; then printf 'ABSENT\n'; return; fi + ( + cd "$PREFIX" + find . -mindepth 1 -printf '%P|%y|%m|%l\n' | LC_ALL=C sort + find . -type f -print0 | LC_ALL=C sort -z | xargs -0 -r sha256sum + ) | sha256sum | awk '{print $1}' +} + +reset_state +echo "[test] --next fast path pins resolved package versions" +OUTPUT="$( + HOME="$HOME_DIR" \ + MOSAIC_HOME="$MOSAIC_HOME" \ + MOSAIC_PREFIX="$PREFIX" \ + MOSAIC_NO_COLOR=1 \ + MOSAIC_TEST_NPM_LOG="$LOG" \ + MOSAIC_TEST_STATE="$STATE" \ + PATH="$FAKE_BIN:$PATH" \ + bash "$ROOT/tools/install.sh" --cli --next --yes --no-auto-launch +)" + +grep -qF 'Installed @next packages: CLI 0.0.49-next.999, gateway 0.0.7-next.999' <<<"$OUTPUT" +grep -qF 'install -g @mosaicstack/gateway@0.0.7-next.999' "$LOG" +grep -qF 'install -g @mosaicstack/mosaic@0.0.49-next.999' "$LOG" +if grep -qE '^install -g .+@next( |$)' "$LOG"; then + echo "expected exact-version installs, found mutable @next install" >&2 + exit 1 +fi +if grep -qF 'Downloading source ref next at pinned commit' <<<"$OUTPUT"; then + echo "fast path unexpectedly fell back to source" >&2 + exit 1 +fi + +ACTIVE="$HOME_DIR/.local/state/mosaic/install/active.json" +[[ "$(node -p "require('$ACTIVE').status")" == "committed" ]] +JOURNAL="$(node -p "require('$ACTIVE').journal")" +[[ "$(stat -c '%a' "$JOURNAL")" == "444" ]] +( cd "$(dirname "$JOURNAL")" && sha256sum -c "$(basename "$JOURNAL").sha256" >/dev/null ) +grep -q '"event":"mutation".*"phase":"P3".*path=.*prior=.*reverse=' "$JOURNAL" + +reset_state +echo "[test] fast path failure falls back to source build" +OUTPUT="$( + HOME="$HOME_DIR" \ + MOSAIC_HOME="$MOSAIC_HOME" \ + MOSAIC_PREFIX="$PREFIX" \ + MOSAIC_NO_COLOR=1 \ + MOSAIC_TEST_NPM_LOG="$LOG" \ + MOSAIC_TEST_STATE="$STATE" \ + MOSAIC_TEST_FAIL_NEXT_GATEWAY_INSTALL=1 \ + PATH="$FAKE_BIN:$PATH" \ + bash "$ROOT/tools/install.sh" --cli --next --yes --no-auto-launch +)" + +grep -qF 'Fast gateway @next install failed.' <<<"$OUTPUT" +grep -qF 'Falling back to source build at ref next; --next will not hard-fail on registry issues.' <<<"$OUTPUT" +grep -qF 'Downloading source ref next at pinned commit 1111111111111111111111111111111111111111' <<<"$OUTPUT" +grep -qF 'Installed from source: CLI 0.0.0-source' <<<"$OUTPUT" +grep -qF 'install -g @mosaicstack/mosaic@0.0.49-next.999' "$LOG" +grep -qE 'install -g .*/mosaicstack-gateway-0\.0\.0-source\.tgz' "$LOG" +grep -qE 'install -g .*/mosaicstack-mosaic-0\.0\.0-source\.tgz' "$LOG" +[[ "$(cat "$STATE/mosaic")" == "0.0.0-source" ]] +[[ "$(cat "$STATE/gateway")" == "0.0.0-source" ]] + +reset_state +echo "[test] source-build failure is fatal and restores the pre-install prefix" +before_prefix="$(prefix_fingerprint)" +set +e +OUTPUT="$( + HOME="$HOME_DIR" \ + MOSAIC_HOME="$MOSAIC_HOME" \ + MOSAIC_PREFIX="$PREFIX" \ + MOSAIC_NO_COLOR=1 \ + MOSAIC_TEST_NPM_LOG="$LOG" \ + MOSAIC_TEST_STATE="$STATE" \ + MOSAIC_TEST_FAIL_NEXT_GATEWAY_INSTALL=1 \ + MOSAIC_TEST_FAIL_PNPM_INSTALL=1 \ + PATH="$FAKE_BIN:$PATH" \ + bash "$ROOT/tools/install.sh" --cli --next --yes --no-auto-launch 2>&1 +)" +FAIL_STATUS=$? +set -e +[[ "$FAIL_STATUS" -ne 0 ]] +[[ "$(prefix_fingerprint)" == "$before_prefix" ]] +grep -qF 'forced pnpm install failure' <<<"$OUTPUT" +[[ "$(node -p "require('$ACTIVE').status")" == "rolled-back" ]] + +reset_state +echo "[test] corrupt source archive is fatal and restores the pre-install prefix" +before_prefix="$(prefix_fingerprint)" +set +e +OUTPUT="$( + HOME="$HOME_DIR" \ + MOSAIC_HOME="$MOSAIC_HOME" \ + MOSAIC_PREFIX="$PREFIX" \ + MOSAIC_NO_COLOR=1 \ + MOSAIC_TEST_NPM_LOG="$LOG" \ + MOSAIC_TEST_STATE="$STATE" \ + MOSAIC_TEST_FAIL_NEXT_GATEWAY_INSTALL=1 \ + MOSAIC_TEST_CORRUPT_ARCHIVE=1 \ + PATH="$FAKE_BIN:$PATH" \ + bash "$ROOT/tools/install.sh" --cli --next --yes --no-auto-launch 2>&1 +)" +FAIL_STATUS=$? +set -e +[[ "$FAIL_STATUS" -ne 0 ]] +[[ "$(prefix_fingerprint)" == "$before_prefix" ]] +grep -qF 'archive safety/integrity check failed' <<<"$OUTPUT" +[[ "$(node -p "require('$ACTIVE').status")" == "rolled-back" ]] + +reset_state +echo "[test] --dev source install does not require registry version resolution" +OUTPUT="$( + HOME="$HOME_DIR" \ + MOSAIC_HOME="$MOSAIC_HOME" \ + MOSAIC_PREFIX="$PREFIX" \ + MOSAIC_NO_COLOR=1 \ + MOSAIC_TEST_NPM_LOG="$LOG" \ + MOSAIC_TEST_STATE="$STATE" \ + MOSAIC_TEST_FAIL_NPM_VIEW=1 \ + PATH="$FAKE_BIN:$PATH" \ + bash "$ROOT/tools/install.sh" --cli --dev --ref feature-x --yes --no-auto-launch +)" +grep -qF 'Downloading source ref feature-x at pinned commit 1111111111111111111111111111111111111111' <<<"$OUTPUT" +grep -qF 'Installed from source: CLI 0.0.0-source' <<<"$OUTPUT" +grep -q '^\[P2\] PASS: source_ref=feature-x pinned_commit=1111111111111111111111111111111111111111 sha256=' <<<"$OUTPUT" + +reset_state +echo "[test] explicit --ref keeps source lane and avoids @next lookup" +set +e +OUTPUT="$( + HOME="$HOME_DIR" \ + MOSAIC_HOME="$MOSAIC_HOME" \ + MOSAIC_PREFIX="$PREFIX" \ + MOSAIC_NO_COLOR=1 \ + MOSAIC_TEST_NPM_LOG="$LOG" \ + MOSAIC_TEST_STATE="$STATE" \ + PATH="$FAKE_BIN:$PATH" \ + bash "$ROOT/tools/install.sh" --check --cli --next --ref feature-x +)" +CHECK_STATUS=$? +set -e +[[ "$CHECK_STATUS" -ne 0 ]] +grep -q '^\[P2\] PASS: source_ref=feature-x pinned_commit=1111111111111111111111111111111111111111 sha256=' <<<"$OUTPUT" +if grep -qF '@next version' "$LOG"; then + echo "explicit ref should not query @next dist-tags" >&2 + exit 1 +fi + +reset_state +echo "[test] --check --next rejects mismatched prerelease pipeline suffixes" +set +e +OUTPUT="$( + HOME="$HOME_DIR" \ + MOSAIC_HOME="$MOSAIC_HOME" \ + MOSAIC_PREFIX="$PREFIX" \ + MOSAIC_NO_COLOR=1 \ + MOSAIC_TEST_NPM_LOG="$LOG" \ + MOSAIC_TEST_STATE="$STATE" \ + MOSAIC_TEST_GATEWAY_NEXT_VERSION="0.0.7-next.1000" \ + PATH="$FAKE_BIN:$PATH" \ + bash "$ROOT/tools/install.sh" --check --cli --next +)" +CHECK_STATUS=$? +set -e +[[ "$CHECK_STATUS" -ne 0 ]] +grep -q '^\[P2\] FAIL: resolved_version=unavailable' <<<"$OUTPUT" + +echo "[test] installer next lane tests passed" diff --git a/tools/install-state-machine.test.sh b/tools/install-state-machine.test.sh index cd6b85b7..ae5dde26 100755 --- a/tools/install-state-machine.test.sh +++ b/tools/install-state-machine.test.sh @@ -2,6 +2,9 @@ # Red-first acceptance checks for #1050. This file is committed before the # installer implementation. Do not weaken these properties to make it green. +# pass_case always returns zero and fail_case records the aggregate failure; +# the compact A&&pass||fail assertions are intentional. +# shellcheck disable=SC2015 set -uo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" @@ -26,8 +29,9 @@ make_fake_npm() { local bin="$1" mkdir -p "$bin" cat > "$bin/npm" <<'FAKE' -#!/usr/bin/env bash +#!/bin/bash set -euo pipefail +if [[ "${1:-}" == "--version" ]]; then echo '10.6.2'; exit 0; fi case "${1:-} ${2:-} ${3:-}" in 'view @mosaicstack/mosaic@next version') echo '0.0.50-next.999' ;; 'view @mosaicstack/gateway@next version') echo '0.0.7-next.999' ;; @@ -86,6 +90,78 @@ for phase in P5 P8; do || fail_case "$phase did not report its own expected failure" done +printf '[test] case: --check discriminates a constructed good host without mutation\n' +good_home="$TMP/good-home" +good_bin="$TMP/good-bin" +good_prefix="$good_home/.npm-global" +good_mosaic="$good_home/.config/mosaic" +mkdir -p "$good_bin" "$good_prefix/bin" "$good_mosaic/skills/declared-skill" +make_fake_npm "$good_bin" +cat > "$good_prefix/bin/mosaic" <<'CLI' +#!/usr/bin/env bash +printf '0.0.50-next.999\n' +CLI +chmod 0755 "$good_prefix/bin/mosaic" +cat > "$good_bin/getent" < "$good_bin/bash" < "$good_mosaic/SOUL.md" +printf '# User\n\nConfigured.\n' > "$good_mosaic/USER.md" +chmod 0600 "$good_mosaic/SOUL.md" "$good_mosaic/USER.md" +cat > "$good_mosaic/skills/declared-skill/SKILL.md" <<'SKILL' +--- +name: declared-skill +description: Constructed loadable acceptance skill. +--- + +# Declared skill +SKILL +printf '{"lane":"next","version":"0.0.50-next.999","skills":["declared-skill"]}\n' > "$good_mosaic/.install-shipped-skills.json" +printf '{\n "lane": "next",\n "cliVersion": "0.0.50-next.999"\n}\n' > "$good_mosaic/.install-manifest.json" +before="$(fingerprint "$good_home")" +set +e +HOME="$good_home" MOSAIC_HOME="$good_mosaic" MOSAIC_PREFIX="$good_prefix" \ + MOSAIC_NO_COLOR=1 PATH="$good_bin:/usr/local/bin:/usr/bin:/bin" \ + bash "$ROOT/tools/install.sh" --check --next >"$TMP/good-check.log" 2>&1 +status=$? +set -e +after="$(fingerprint "$good_home")" +[[ "$status" -eq 0 ]] && pass_case 'good-host --check exited zero' || fail_case "good-host --check exited $status" +[[ "$before" == "$after" ]] && pass_case 'good-host --check left HOME unchanged' || fail_case 'good-host --check mutated HOME' +good_rows="$(grep -Ec '^\[P[0-8]\] PASS:' "$TMP/good-check.log" || true)" +[[ "$good_rows" -eq 9 ]] && pass_case 'good-host --check emitted nine PASS rows' \ + || { cat "$TMP/good-check.log" >&2; fail_case "good-host --check emitted $good_rows PASS rows"; } + +printf '[test] case: persisted required-action failures remain blocking\n' +for blocked_phase in P4 P6; do + node -e ' + const fs=require("fs"); const p=process.argv[1]; const phase=process.argv[2]; + const m=JSON.parse(fs.readFileSync(p,"utf8")); m.phaseOutcomes={P4:"committed",P6:"committed"}; + m.phaseOutcomes[phase]="failed"; fs.writeFileSync(p,JSON.stringify(m)+"\n"); + ' "$good_mosaic/.install-manifest.json" "$blocked_phase" + set +e + HOME="$good_home" MOSAIC_HOME="$good_mosaic" MOSAIC_PREFIX="$good_prefix" \ + MOSAIC_NO_COLOR=1 PATH="$good_bin:/usr/local/bin:/usr/bin:/bin" \ + bash "$ROOT/tools/install.sh" --check --next >"$TMP/action-$blocked_phase.log" 2>&1 + status=$? + set -e + [[ "$status" -ne 0 ]] || fail_case "$blocked_phase action failure returned zero" + grep -q "^\[$blocked_phase\] FAIL:.*action reported a required $blocked_phase failure" "$TMP/action-$blocked_phase.log" \ + && pass_case "$blocked_phase action failure remained blocking in a later --check" \ + || fail_case "$blocked_phase persisted action failure was not attributed" +done +printf '{\n "lane": "next",\n "cliVersion": "0.0.50-next.999",\n "phaseOutcomes": {"P4":"committed","P6":"committed"}\n}\n' > "$good_mosaic/.install-manifest.json" + printf '[test] case: per-phase P2-P8 fault injection restores representative host mutations\n' for phase in P2 P3 P4 P5 P6 P7 P8; do home="$TMP/fault-$phase/home" @@ -117,6 +193,86 @@ for phase in P2 P3 P4 P5 P6 P7 P8; do fi done +printf '[test] case: unsafe and overlapping rollback roots fail before mutation\n' +unsafe_home="$TMP/unsafe-home" +mkdir -p "$unsafe_home" +for case_name in root-target home-target overlap-target; do + case "$case_name" in + root-target) unsafe_mosaic=/; unsafe_prefix="$unsafe_home/.npm-global" ;; + home-target) unsafe_mosaic="$unsafe_home"; unsafe_prefix="$unsafe_home/.npm-global" ;; + overlap-target) unsafe_mosaic="$unsafe_home/.config"; unsafe_prefix="$unsafe_home/.config/mosaic/prefix" ;; + esac + before="$(fingerprint "$unsafe_home")" + set +e + HOME="$unsafe_home" MOSAIC_HOME="$unsafe_mosaic" MOSAIC_PREFIX="$unsafe_prefix" \ + MOSAIC_NO_COLOR=1 PATH="$check_bin:/usr/local/bin:/usr/bin:/bin" \ + bash "$ROOT/tools/install.sh" --check --next >"$TMP/$case_name.log" 2>&1 + status=$? + set -e + after="$(fingerprint "$unsafe_home")" + [[ "$status" -ne 0 ]] || fail_case "$case_name unsafe path returned zero" + grep -q '^\[P0\] FAIL:.*unsafe context' "$TMP/$case_name.log" \ + && pass_case "$case_name was rejected by P0" || fail_case "$case_name lacked an attributable P0 failure" + [[ "$before" == "$after" ]] || fail_case "$case_name mutated HOME" +done + +symlink_home="$TMP/symlink-home" +symlink_outside="$TMP/symlink-outside" +mkdir -p "$symlink_home" "$symlink_outside" +ln -s "$symlink_outside" "$symlink_home/.config" +set +e +HOME="$symlink_home" MOSAIC_HOME="$symlink_home/.config/mosaic" MOSAIC_PREFIX="$symlink_home/.npm-global" \ + MOSAIC_NO_COLOR=1 PATH="$check_bin:/usr/local/bin:/usr/bin:/bin" \ + bash "$ROOT/tools/install.sh" --check --next >"$TMP/symlink-target.log" 2>&1 +status=$? +set -e +[[ "$status" -ne 0 ]] || fail_case 'symlink-parent unsafe path returned zero' +grep -q '^\[P0\] FAIL:.*unsafe context' "$TMP/symlink-target.log" \ + && pass_case 'symlinked rollback parent was rejected by P0' \ + || fail_case 'symlinked rollback parent lacked an attributable P0 failure' +[[ -z "$(find "$symlink_outside" -mindepth 1 -print -quit)" ]] || fail_case 'symlink target was mutated' + +printf '[test] case: stale in-progress projection does not impersonate a live OS lock\n' +stale_home="$TMP/stale/home" +stale_state="$TMP/stale/state" +mkdir -p "$stale_home/.config/mosaic" "$stale_state" +printf '{"status":"in-progress","journal":"%s"}\n' "$stale_state/dead-run/journal.ndjson" > "$stale_state/active.json" +set +e +HOME="$stale_home" MOSAIC_HOME="$stale_home/.config/mosaic" MOSAIC_PREFIX="$stale_home/.npm-global" \ + MOSAIC_INSTALL_STATE_DIR="$stale_state" MOSAIC_INSTALL_FAULT_AFTER=P2 MOSAIC_NO_COLOR=1 \ + bash "$ROOT/tools/install.sh" --state-machine-self-test >"$TMP/stale.log" 2>&1 +status=$? +set -e +[[ "$status" -eq 97 ]] || fail_case "stale projection recovery expected injected status 97, got $status" +if find "$stale_state" -name prior-active.json -type f -print -quit | grep -q .; then + pass_case 'stale projection was preserved and superseded after the free OS lock was acquired' +else + fail_case 'stale projection was not preserved for recovery evidence' +fi +[[ "$(node -p "require('$stale_state/active.json').status")" == "rolled-back" ]] \ + || fail_case 'stale retry did not reach an honest rolled-back terminal state' + +printf '[test] case: journal initialization failure is fatal before mutation\n' +journal_home="$TMP/journal-failure/home" +mkdir -p "$journal_home/.config/mosaic" +printf 'journal-sentinel\n' > "$journal_home/.config/mosaic/operator.txt" +before="$(fingerprint "$journal_home")" +set +e +HOME="$journal_home" MOSAIC_HOME="$journal_home/.config/mosaic" MOSAIC_PREFIX="$journal_home/.npm-global" \ + MOSAIC_INSTALL_STATE_DIR="/proc/mosaic-journal-denied-$$" MOSAIC_INSTALL_FAULT_AFTER=P2 \ + MOSAIC_NO_COLOR=1 bash "$ROOT/tools/install.sh" --state-machine-self-test \ + >"$TMP/journal-failure.log" 2>&1 +status=$? +set -e +after="$(fingerprint "$journal_home")" +[[ "$status" -ne 0 ]] && pass_case 'unwritable journal directory failed non-zero' \ + || fail_case 'unwritable journal directory returned zero' +grep -q 'cannot create private journal directory' "$TMP/journal-failure.log" \ + && pass_case 'journal initialization failure was named' \ + || fail_case 'journal initialization failure lacked a named diagnostic' +[[ "$before" == "$after" ]] && pass_case 'journal failure occurred before target mutation' \ + || fail_case "journal failure mutated target HOME (before=$before after=$after)" + if [[ "$failures" -ne 0 ]]; then printf '[test] install state-machine acceptance RED: %d failed assertion(s)\n' "$failures" >&2 printf '[test] --check transcript: %s\n' "$TMP/check.log" >&2 diff --git a/tools/install.sh b/tools/install.sh index 76925174..79121edb 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -16,6 +16,10 @@ # --framework Install/upgrade framework only (skip npm CLI) # --cli Install/upgrade npm CLI only (skip framework) # --ref Git ref for framework archive (default: main) +# --next Prerelease lane: try fast npm @next install for CLI + +# gateway from the Gitea registry, then fall back to a +# source build at next if unavailable. Explicit +# --ref/MOSAIC_REF wins and uses the source path. # --dev Build CLI + gateway FROM SOURCE at --ref instead of the # registry @latest. Zero registry writes — packs local # tarballs and installs them globally. Use to test a branch @@ -31,6 +35,7 @@ # MOSAIC_PREFIX — npm global prefix (default: ~/.npm-global) # MOSAIC_NO_COLOR — disable colour (set to 1) # MOSAIC_REF — git ref for framework (default: main) +# MOSAIC_NEXT — equivalent to --next (set to 1) # MOSAIC_DEV — equivalent to --dev (set to 1) # MOSAIC_ASSUME_YES — equivalent to --yes (set to 1) # ────────────────────────────────────────────────────────────────────────────── @@ -49,7 +54,13 @@ FLAG_NO_AUTO_LAUNCH=false FLAG_YES=false FLAG_UNINSTALL=false FLAG_DEV=false +FLAG_NEXT=false +FLAG_STATE_SELF_TEST=false GIT_REF="${MOSAIC_REF:-main}" +GIT_REF_EXPLICIT=false +if [[ -n "${MOSAIC_REF:-}" ]]; then + GIT_REF_EXPLICIT=true +fi # MOSAIC_ASSUME_YES env var acts the same as --yes if [[ "${MOSAIC_ASSUME_YES:-0}" == "1" ]]; then @@ -61,8 +72,18 @@ if [[ "${MOSAIC_DEV:-0}" == "1" ]]; then FLAG_DEV=true fi +# MOSAIC_NEXT env var acts the same as --next: fast npm @next install with +# source fallback from the permanent next integration branch unless +# MOSAIC_REF/--ref explicitly wins. +if [[ "${MOSAIC_NEXT:-0}" == "1" ]]; then + FLAG_NEXT=true + if [[ "$GIT_REF_EXPLICIT" == "false" ]]; then + GIT_REF="next" + fi +fi + installer_usage() { - printf 'Usage: install.sh [--check] [--framework] [--cli] [--ref ] [--dev] [--yes|-y] [--no-auto-launch] [--uninstall]\n' >&2 + printf 'Usage: install.sh [--check] [--framework] [--cli] [--ref ] [--next] [--dev] [--yes|-y] [--no-auto-launch] [--uninstall]\n' >&2 } while [[ $# -gt 0 ]]; do @@ -82,12 +103,17 @@ while [[ $# -gt 0 ]]; do exit 2 fi GIT_REF="$2" + GIT_REF_EXPLICIT=true shift 2 ;; --dev) FLAG_DEV=true; shift ;; + --next) FLAG_NEXT=true; if [[ "$GIT_REF_EXPLICIT" == "false" ]]; then GIT_REF="next"; fi; shift ;; --yes|-y) FLAG_YES=true; shift ;; --no-auto-launch) FLAG_NO_AUTO_LAUNCH=true; shift ;; --uninstall) FLAG_UNINSTALL=true; shift ;; + # Internal acceptance seam: exercises the real journal/snapshot/rollback + # machinery against representative installer mutations. Not a user mode. + --state-machine-self-test) FLAG_STATE_SELF_TEST=true; shift ;; *) printf 'Error: Unknown argument: %s\n' "$1" >&2 installer_usage @@ -96,12 +122,24 @@ while [[ $# -gt 0 ]]; do esac done +# Explicit refs represent a request for that exact source tree. Keep --next as +# a lane selector, but do not install the registry @next package for a different +# ref than the permanent next branch. +if [[ "$FLAG_NEXT" == "true" && "$GIT_REF_EXPLICIT" == "true" ]]; then + FLAG_DEV=true +fi + +if [[ "$FLAG_YES" == "true" ]]; then + export MOSAIC_ASSUME_YES=1 +fi + # ─── constants ──────────────────────────────────────────────────────────────── MOSAIC_HOME="${MOSAIC_HOME:-$HOME/.config/mosaic}" REGISTRY="${MOSAIC_REGISTRY:-https://git.mosaicstack.dev/api/packages/mosaicstack/npm/}" SCOPE="${MOSAIC_SCOPE:-@mosaicstack}" PREFIX="${MOSAIC_PREFIX:-$HOME/.npm-global}" CLI_PKG="${SCOPE}/mosaic" +GATEWAY_PKG="${SCOPE}/gateway" REPO_BASE="https://git.mosaicstack.dev/mosaicstack/stack" ARCHIVE_URL="${REPO_BASE}/archive/${GIT_REF}.tar.gz" @@ -116,6 +154,20 @@ fi WORK_DIR="" EXTRACTED_DIR="" +newest_matching_file() { + local dir="$1" + local pattern="$2" + local matches=() + [[ -d "$dir" ]] || return 0 + shopt -s nullglob + # shellcheck disable=SC2206 # Intentional glob expansion for caller-provided file pattern. + matches=("$dir"/$pattern) + shopt -u nullglob + [[ "${#matches[@]}" -gt 0 ]] || return 0 + # shellcheck disable=SC2012 # Need portable mtime sorting across Linux/macOS. + ls -1t "${matches[@]}" 2>/dev/null | head -1 +} + # ─── uninstall path ─────────────────────────────────────────────────────────── # Shell-level uninstall for when the CLI is broken or not available. # Handles: framework directory, npm CLI package, npmrc scope line. @@ -179,7 +231,7 @@ if [[ "$FLAG_UNINSTALL" == "true" ]]; then # Find most recent backup backup="" if [[ -d "$dir" ]]; then - backup="$(ls -1t "$dir/${base}.mosaic-bak-"* 2>/dev/null | head -1 || true)" + backup="$(newest_matching_file "$dir" "${base}.mosaic-bak-*")" fi if [[ -n "$backup" ]] && [[ -f "$backup" ]]; then cp "$backup" "$dest" @@ -235,30 +287,79 @@ fail() { echo "${R}✖${RESET} $*" >&2; } dim() { echo "${DIM}$*${RESET}"; } step() { printf '\n%s%s%s\n' "$BOLD" "$*" "$RESET"; } +is_next_registry_lane() { + [[ "$FLAG_NEXT" == "true" && "$FLAG_DEV" == "false" && "$GIT_REF" == "next" && "$GIT_REF_EXPLICIT" == "false" ]] +} + +source_ref_details() { + if is_next_registry_lane; then + echo "ref: next, --next prerelease lane" + elif [[ "$FLAG_NEXT" == "true" && "$GIT_REF" == "next" ]]; then + echo "ref: next, --next prerelease lane (build-from-source)" + elif [[ "$FLAG_NEXT" == "true" ]]; then + echo "ref: ${GIT_REF}, --next requested, explicit ref wins" + else + echo "ref: ${GIT_REF}" + fi +} + # ─── helpers ────────────────────────────────────────────────────────────────── require_cmd() { if ! command -v "$1" &>/dev/null; then fail "Required command not found: $1" echo " Install it and re-run this script." - exit 1 + return 1 fi } installed_cli_version() { local json - json="$(npm ls -g --depth=0 --json --prefix="$PREFIX" 2>/dev/null)" || true + json="$(npm ls -g --depth=0 --json --prefix="$PREFIX" --cache="${STATE_NPM_CACHE:-${TMPDIR:-/tmp}/mosaic-install-npm-cache-$$}")" || true if [[ -n "$json" ]]; then node -e " const d = JSON.parse(process.argv[1]); const v = d?.dependencies?.['${CLI_PKG}']?.version ?? ''; process.stdout.write(v); - " "$json" 2>/dev/null || true + " "$json" || true + fi +} + +installed_gateway_version() { + local json + json="$(npm ls -g --depth=0 --json --prefix="$PREFIX" --cache="${STATE_NPM_CACHE:-${TMPDIR:-/tmp}/mosaic-install-npm-cache-$$}")" || true + if [[ -n "$json" ]]; then + node -e " + const d = JSON.parse(process.argv[1]); + const v = d?.dependencies?.['${GATEWAY_PKG}']?.version ?? ''; + process.stdout.write(v); + " "$json" || true fi } latest_cli_version() { - npm view "${CLI_PKG}" version --registry="$REGISTRY" 2>/dev/null || true + npm view "${CLI_PKG}" version --registry="$REGISTRY" --cache="${STATE_NPM_CACHE:-${TMPDIR:-/tmp}/mosaic-install-npm-cache-$$}" || true +} + +next_cli_version() { + npm view "${CLI_PKG}@next" version --registry="$REGISTRY" --cache="${STATE_NPM_CACHE:-${TMPDIR:-/tmp}/mosaic-install-npm-cache-$$}" || true +} + +next_gateway_version() { + npm view "${GATEWAY_PKG}@next" version --registry="$REGISTRY" --cache="${STATE_NPM_CACHE:-${TMPDIR:-/tmp}/mosaic-install-npm-cache-$$}" || true +} + +next_pipeline_suffix() { + printf '%s' "$1" | sed -n 's/.*-next\.\([0-9][0-9]*\)$/\1/p' +} + +next_versions_share_pipeline() { + local cli_next="$1" + local gateway_next="$2" + local cli_pipeline gateway_pipeline + cli_pipeline="$(next_pipeline_suffix "$cli_next")" + gateway_pipeline="$(next_pipeline_suffix "$gateway_next")" + [[ -n "$cli_pipeline" && -n "$gateway_pipeline" && "$cli_pipeline" == "$gateway_pipeline" ]] } version_lt() { @@ -283,37 +384,760 @@ framework_version() { fi } -# Download + extract the monorepo archive at $GIT_REF exactly once per run. -# Sets the script-level EXTRACTED_DIR to the repo root. Reused by both the -# framework install (Part 1) and the dev build-from-source path (Part 2). +# ─── Transactional install state (canonical P0-P9) ─────────────────────────── +# The phase numbering and names are an external contract. C2-C5 bind to these +# exact numbers, so do not renumber when filling a failed postcondition. +INSTALL_PHASES=(P0 P1 P2 P3 P4 P5 P6 P7 P8 P9) +STATE_DIR="${MOSAIC_INSTALL_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/mosaic/install}" +STATE_RUN_DIR="" +STATE_JOURNAL="" +STATE_COMMAND_LOG="" +STATE_SNAPSHOT_DIR="" +STATE_FRAMEWORK_STATUS="" +STATE_INTERRUPTED_ACTIVE="" +STATE_CURRENT_PHASE="P0" +STATE_LOCK_FD="" +STATE_FAILURES=0 +STATE_FAILED_PHASES=() +STATE_NPM_CACHE="${TMPDIR:-/tmp}/mosaic-install-npm-cache-$$" +RESOLVED_CLI_VERSION="" +RESOLVED_SOURCE_DIGEST="" +LOCAL_SOURCE_ARCHIVE="${MOSAIC_INSTALL_LOCAL_SOURCE_ARCHIVE:-}" +LOCAL_SOURCE_COMMIT="${MOSAIC_INSTALL_LOCAL_SOURCE_COMMIT:-}" +LOCAL_SOURCE_SHA256="${MOSAIC_INSTALL_LOCAL_SOURCE_SHA256:-}" + +phase_name() { + case "$1" in + P0) echo "Resolve context" ;; P1) echo "Preflight" ;; + P2) echo "Acquire artifacts" ;; P3) echo "Install CLI" ;; + P4) echo "Install framework + skills" ;; P5) echo "Identity" ;; + P6) echo "Runtime linking / activation" ;; P7) echo "Services" ;; + P8) echo "Shell discoverability" ;; P9) echo "Verify + commit" ;; + *) echo "unknown" ;; + esac +} + +phase_contract() { + case "$1" in + P0) printf 'pre=target context available; action=resolve user/HOME/shell/platform; post=context stated and supported; rollback=n/a' ;; + P1) printf 'pre=P0 supported; action=validate tools/registry/headroom and acquire lock; post=preflight complete and exclusive; rollback=release lock' ;; + P2) printf 'pre=P1 exclusive; action=fetch pinned installer-distribution artifacts with visible output; post=lane/version/digest recorded; rollback=discard temporary artifacts; seam=does not forbid credentialed downstream acquisition' ;; + P3) printf 'pre=P2 pinned CLI; action=install CLI at known prefix; post=absolute binary version equals resolved version; rollback=restore prior prefix' ;; + P4) printf 'pre=P2 framework source and P3 absolute CLI; action=sync framework and skills; post=repository-shipped skills installed and loadable; rollback=restore prior framework/runtime trees' ;; + P5) printf 'pre=P3 absolute CLI; action=establish configured identity and validate any credential capability requested downstream; post=SOUL/USER valid owner/mode and required credential usable; rollback=remove generated identity/credential binding' ;; + P6) printf 'pre=P3 absolute CLI; action=evaluate runtime activation; post=dead #869 hooks never active without broker; rollback=restore runtime assets' ;; + P7) printf 'pre=P6 activation evaluated and applicable P5 credential committed; action=provision/manage requested services and credentialed resources only; post=requested services/resources ready; rollback=stop and restore requested services/resources' ;; + P8) printf 'pre=P3 absolute CLI; action=verify fresh target-user shells; post=login and non-login resolve P3 path; rollback=restore shell profiles' ;; + P9) printf 'pre=P0-P8 evaluated; action=reassert and commit journal/manifest; post=all phases pass and journal committed; rollback=restore pre-install snapshot' ;; + esac +} + +state_json_line() { + local event="$1" phase="$2" status="$3" message="$4" + [[ -n "$STATE_JOURNAL" ]] || return 0 + if ! EVENT="$event" PHASE="$phase" STATUS="$status" MESSAGE="$message" \ + node -e ' + const row={timestamp:new Date().toISOString(),event:process.env.EVENT,phase:process.env.PHASE,status:process.env.STATUS,message:process.env.MESSAGE}; + process.stdout.write(JSON.stringify(row)+"\\n"); + ' >> "$STATE_JOURNAL"; then + fail "Journal write failed at phase ${phase}; refusing an unrecorded mutation." + return 1 + fi + if ! sync "$STATE_JOURNAL"; then + fail "Journal sync failed at phase ${phase}; refusing an unrecorded mutation." + return 1 + fi +} + +state_record_mutation() { + local phase="$1" path="$2" reverse="$3" status root key covered=false + local prior="absent" snapshot="none" + if [[ -n "$STATE_SNAPSHOT_DIR" && -s "$STATE_SNAPSHOT_DIR/paths.tsv" ]]; then + while IFS=$'\t' read -r status root key; do + if [[ "$path" == "$root" || "$path" == "$root"/* ]]; then + covered=true + [[ -e "$path" || -L "$path" ]] && prior="present" + snapshot="$STATE_SNAPSHOT_DIR/data/$key" + break + fi + done < "$STATE_SNAPSHOT_DIR/paths.tsv" + fi + if [[ "$covered" != true && ( -e "$path" || -L "$path" ) ]]; then + # A path outside the declared snapshot cannot be mutated safely. + fail "Journal cannot bind prior state for $path before $phase mutation." + return 1 + fi + state_json_line mutation "$phase" planned "path=$path prior=$prior snapshot=$snapshot reverse=$reverse" +} + +state_seal_journal() { + local digest + state_json_line seal P9 committed "journal closed after manifest commit" || return + digest="$(sha256sum "$STATE_JOURNAL" | awk '{print $1}')" || return + if ! printf '%s %s\n' "$digest" "$(basename "$STATE_JOURNAL")" > "$STATE_JOURNAL.sha256" \ + || ! sync "$STATE_JOURNAL.sha256"; then + fail "Could not durably write the P9 journal seal." + return 1 + fi + if ! chmod 0444 "$STATE_JOURNAL" "$STATE_JOURNAL.sha256"; then + fail "Could not make the committed journal and seal immutable." + return 1 + fi + printf '%s' "$digest" +} + +state_framework_action_failed() { + local phase="$1" + [[ -n "$STATE_FRAMEWORK_STATUS" && -s "$STATE_FRAMEWORK_STATUS" ]] || return 1 + grep -q "^${phase}"$'\t'"failed"$'\t' "$STATE_FRAMEWORK_STATUS" +} + +state_manifest_action_failed() { + local phase="$1" manifest="$MOSAIC_HOME/.install-manifest.json" + [[ -s "$manifest" ]] || return 1 + node -e ' + const fs=require("fs"); + const data=JSON.parse(fs.readFileSync(process.argv[1],"utf8")); + process.exit(data?.phaseOutcomes?.[process.argv[2]] === "failed" ? 0 : 1); + ' "$manifest" "$phase" 2>/dev/null +} + +state_action_failed() { + if [[ -n "$STATE_FRAMEWORK_STATUS" ]]; then + state_framework_action_failed "$1" + else + state_manifest_action_failed "$1" + fi +} + +state_run_captured() { + local label="$1" output status=0 + shift + output="$(mktemp "${TMPDIR:-/tmp}/mosaic-phase-command.XXXXXX.log")" || return + # The command is deliberately called in a conditional so its status can be + # journaled before the caller's ERR trap rolls back. Bash disables errexit in + # functions invoked this way, so every multi-command phase helper below must + # explicitly return on each required command failure. + if "$@" >"$output" 2>&1; then status=0; else status=$?; fi + cat "$output" || { rm -f "$output"; return 1; } + if ! { printf '\n=== %s (exit=%s) ===\n' "$label" "$status"; cat "$output"; } >> "$STATE_COMMAND_LOG"; then + rm -f "$output" + fail "Could not append '$label' output to $STATE_COMMAND_LOG; refusing to continue." + return 1 + fi + if ! sync "$STATE_COMMAND_LOG"; then + rm -f "$output" + fail "Could not sync '$label' output in $STATE_COMMAND_LOG; refusing to continue." + return 1 + fi + rm -f "$output" + state_json_line command "$STATE_CURRENT_PHASE" "$([[ "$status" -eq 0 ]] && echo committed || echo failed)" "label=$label output_log=$STATE_COMMAND_LOG exit=$status" + return "$status" +} + +state_write_active() { + local content="$1" + if ! printf '%s\n' "$content" > "$STATE_DIR/active.json" || ! sync "$STATE_DIR/active.json"; then + fail "Journal state write failed at $STATE_DIR/active.json; refusing to continue." + return 1 + fi +} + +state_phase_begin() { + STATE_CURRENT_PHASE="$1" + state_json_line phase "$1" started "$(phase_contract "$1")" +} + +state_phase_finish() { + state_json_line phase "$1" "$2" "$3" +} + +state_emit() { + local phase="$1" verdict="$2" reason="$3" + printf '[%s] %s: %s\n' "$phase" "$verdict" "$reason" + if [[ "$verdict" == "FAIL" ]]; then + STATE_FAILURES=$((STATE_FAILURES + 1)) + STATE_FAILED_PHASES+=("$phase") + fi +} + +state_target_shell() { + local shell="" + if command -v getent >/dev/null 2>&1; then + shell="$(getent passwd "$(id -u)" 2>/dev/null | cut -d: -f7 || true)" + fi + printf '%s' "${shell:-${SHELL:-}}" +} + +state_resolved_version() { + local cli gateway + if [[ "$FLAG_DEV" == "true" ]]; then + return 0 + fi + if is_next_registry_lane; then + cli="$(next_cli_version)" + gateway="$(next_gateway_version)" + [[ -n "$cli" && -n "$gateway" ]] && next_versions_share_pipeline "$cli" "$gateway" || return 0 + printf '%s' "$cli" + else + latest_cli_version + fi +} + +state_expected_cli_version() { + if [[ -n "$RESOLVED_CLI_VERSION" ]]; then + printf '%s' "$RESOLVED_CLI_VERSION" + elif [[ "$FLAG_DEV" == "true" && -s "$MOSAIC_HOME/.install-manifest.json" ]]; then + node -p "require('$MOSAIC_HOME/.install-manifest.json').cliVersion || ''" 2>/dev/null || true + else + state_resolved_version + fi +} + +state_predicate() { + local phase="$1" shell node_major installed expected + local missing=() login_path nonlogin_path broker=false dead_hooks=0 + local prefix_parent disk_kb inode_count min_disk_kb min_inodes npm_major privilege_mode + STATE_REASON="" + case "$phase" in + P0) + shell="$(state_target_shell)" + node_major="$(node -p 'Number(process.versions.node.split(".")[0])' 2>/dev/null || echo 0)" + npm_major="$(npm --version 2>/dev/null | cut -d. -f1 || echo 0)" + privilege_mode="$([[ "$(id -u)" -eq 0 ]] && echo root-without-explicit-target || echo user)" + if [[ -n "$HOME" && -n "$shell" && "$privilege_mode" == "user" && "$(uname -s)" == "Linux" ]] \ + && ldd --version 2>&1 | grep -qi 'glibc\|gnu libc' \ + && [[ "$(uname -m)" == "x86_64" ]] && [[ "$node_major" -ge 20 ]] && [[ "$npm_major" -ge 9 ]] \ + && state_validate_target_paths; then + STATE_REASON="target=$(id -un) uid=$(id -u) HOME=$HOME shell=$shell privilege=$privilege_mode arch=x86_64 libc=glibc node=$(node --version) npm=$(npm --version)" + return 0 + fi + STATE_REASON="unsupported, unresolved, or unsafe context (target=$(id -un 2>/dev/null || echo unknown) uid=$(id -u) HOME=${HOME:-unset} shell=${shell:-unset} privilege=$privilege_mode arch=$(uname -m 2>/dev/null || echo unknown) node_major=$node_major npm_major=$npm_major path_check=${STATE_PATH_REASON:-not-reached})" + return 1 + ;; + P1) + # Include tools invoked by downstream phases. Omitting git made P1 pass + # while P4's sync was already guaranteed to fail and be suppressed. + for tool in awk bash curl date df find flock git grep install mktemp node npm python3 realpath sed sha256sum stat sync tar; do + command -v "$tool" >/dev/null 2>&1 || missing+=("$tool") + done + if [[ "$FLAG_DEV" == "true" ]] && ! command -v corepack >/dev/null 2>&1; then + missing+=("corepack") + fi + # Concurrency authority is the OS-backed flock acquired by + # state_begin_install. active.json is a crash-recovery projection only; + # treating a stale in-progress projection as a live lock permanently + # blocked retries after SIGKILL or power loss. + if [[ -z "$STATE_LOCK_FD" && -f "$STATE_DIR/install.lock" ]]; then + local probe_lock_fd + if exec {probe_lock_fd}<>"$STATE_DIR/install.lock"; then + if ! flock -n "$probe_lock_fd"; then missing+=("concurrent-install-lock-held"); fi + exec {probe_lock_fd}>&- + else + missing+=("install-lock-unreadable") + fi + fi + prefix_parent="$(dirname "$PREFIX")" + [[ -d "$prefix_parent" && -w "$prefix_parent" ]] || missing+=("prefix-parent-not-writable") + min_disk_kb="${MOSAIC_INSTALL_MIN_DISK_KB:-262144}" + min_inodes="${MOSAIC_INSTALL_MIN_INODES:-1000}" + disk_kb="$(df -Pk "$prefix_parent" 2>&1 | awk 'NR==2 {print $4}')" + inode_count="$(df -Pi "$prefix_parent" 2>&1 | awk 'NR==2 {print $4}')" + [[ "$disk_kb" =~ ^[0-9]+$ && "$disk_kb" -ge "$min_disk_kb" ]] || missing+=("disk-headroom") + [[ "$inode_count" =~ ^[0-9]+$ && "$inode_count" -ge "$min_inodes" ]] || missing+=("inode-headroom") + if [[ "$FLAG_DEV" == "true" ]]; then + expected="source-build-at-immutable-ref" + else + expected="$(state_resolved_version)" + [[ -n "$expected" ]] || missing+=("registry-lane-unreachable-or-unauthenticated") + fi + if [[ "${#missing[@]}" -eq 0 ]]; then + STATE_REASON="downstream tool closure present; prefix parent writable; artifact lane resolvable; disk_kb=$disk_kb inodes=$inode_count; concurrency delegated to OS lock" + return 0 + fi + STATE_REASON="preflight failures: ${missing[*]}" + return 1 + ;; + P2) + if [[ "$FLAG_DEV" == "true" ]]; then + local source_commit="${RESOLVED_SOURCE_COMMIT:-}" source_digest="${RESOLVED_SOURCE_DIGEST:-}" + if [[ "$FLAG_CHECK" == "true" && -s "$MOSAIC_HOME/.install-manifest.json" ]]; then + source_commit="$(node -p "require('$MOSAIC_HOME/.install-manifest.json').sourceCommit || ''" 2>/dev/null || true)" + source_digest="$(node -p "require('$MOSAIC_HOME/.install-manifest.json').sourceSha256 || ''" 2>/dev/null || true)" + fi + if [[ "$source_commit" =~ ^[0-9a-f]{40}$ && "$source_digest" =~ ^[0-9a-f]{64}$ ]]; then + STATE_REASON="source_ref=$GIT_REF pinned_commit=$source_commit sha256=$source_digest" + return 0 + fi + STATE_REASON="source ref has no installed pinned commit/digest evidence (commit=${source_commit:-unavailable} sha256=${source_digest:-unavailable})" + return 1 + fi + expected="${RESOLVED_CLI_VERSION:-$(state_resolved_version)}" + if [[ -n "$expected" ]] && { [[ "$FLAG_CHECK" == "false" ]] || grep -qF "\"lane\": \"$([[ "$FLAG_NEXT" == true ]] && echo next || echo latest)\"" "$MOSAIC_HOME/.install-manifest.json" 2>/dev/null; }; then + STATE_REASON="lane=$([[ "$FLAG_NEXT" == true ]] && echo next || echo latest) pinned_version=$expected" + return 0 + fi + STATE_REASON="resolved_version=${expected:-unavailable}; installed manifest does not record the resolved lane" + return 1 + ;; + P3) + expected="$(state_expected_cli_version)" + installed="" + [[ -x "$PREFIX/bin/mosaic" ]] && installed="$("$PREFIX/bin/mosaic" --version 2>&1 | tail -n 1 | tr -d '\r' || true)" + if [[ -n "$expected" && -x "$PREFIX/bin/mosaic" && "$installed" == "$expected" ]]; then + STATE_REASON="absolute_path=$PREFIX/bin/mosaic version=$installed equals resolved lane version" + return 0 + fi + STATE_REASON="absolute_path=$PREFIX/bin/mosaic executable=$([[ -x "$PREFIX/bin/mosaic" ]] && echo yes || echo no) got=${installed:-missing} expected=${expected:-unresolved}" + return 1 + ;; + P4) + # C1 defines and enforces the assertion surface but does not choose among + # the four disagreeing candidate populations. C5 owns publishing and + # fulfilling the declaration. Until then P4 remains NOT-MEASURED. + local declared_set="$MOSAIC_HOME/.install-shipped-skills.json" + local expected_lane expected_version + expected_lane="$([[ "$FLAG_NEXT" == true ]] && echo next || echo latest)" + expected_version="$(state_expected_cli_version)" + if [[ ! -s "$declared_set" ]]; then + STATE_REASON="NOT-MEASURED / UNDECLARED: installer published no checkout-free, lane/versioned shipped-set artifact at $declared_set" + return 1 + fi + if ! EXPECTED_LANE="$expected_lane" EXPECTED_VERSION="$expected_version" MOSAIC_SKILLS_ROOT="$MOSAIC_HOME/skills" \ + node - "$declared_set" <<'NODE' +const fs = require('fs'); +const path = require('path'); +const data = JSON.parse(fs.readFileSync(process.argv[2], 'utf8')); +const root = path.resolve(process.env.MOSAIC_SKILLS_ROOT); +if (!data || typeof data !== 'object' || data.lane !== process.env.EXPECTED_LANE || + data.version !== process.env.EXPECTED_VERSION || !Array.isArray(data.skills) || data.skills.length === 0) process.exit(1); +for (const name of data.skills) { + if (typeof name !== 'string' || !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(name)) process.exit(1); + const skill = path.join(root, name, 'SKILL.md'); + let real; + try { real = fs.realpathSync(skill); } catch { process.exit(1); } + if (!real.startsWith(root + path.sep)) process.exit(1); + const stat = fs.statSync(real); + const text = fs.readFileSync(real, 'utf8'); + const declaredName = text.match(/^---\s*$[\s\S]*?^name:\s*([^\s]+)\s*$/m)?.[1]; + if (!stat.isFile() || stat.size === 0 || declaredName !== name) process.exit(1); +} +NODE + then + STATE_REASON="declared shipped-set artifact is malformed, wrong-lane/version, or its declared skills are not contained and loadable" + return 1 + fi + if state_action_failed P4; then + STATE_REASON="framework/skills action reported a required P4 failure; inspect the transaction command log" + return 1 + fi + STATE_REASON="declared shipped-set matches lane=$expected_lane version=$expected_version; every declared skill is contained and loadable" + return 0 + ;; + P5) + for skill in SOUL.md USER.md; do + local path="$MOSAIC_HOME/$skill" + if [[ ! -s "$path" ]] || ! grep -q '^# ' "$path" 2>/dev/null \ + || [[ "$(stat -c '%u' "$path" 2>/dev/null || echo -1)" != "$(id -u)" ]] \ + || [[ "$(stat -c '%a' "$path" 2>/dev/null || echo 777)" =~ [2367]$ ]]; then + missing+=("$skill") + fi + done + if [[ "${#missing[@]}" -eq 0 ]]; then STATE_REASON="SOUL.md and USER.md parse and have target owner/mode"; return 0; fi + STATE_REASON="identity missing, empty, malformed, wrong-owner, or unsafe-mode: ${missing[*]}" + return 1 + ;; + P6) + if state_action_failed P6; then + STATE_REASON="runtime linking/activation action reported a required P6 failure; inspect the transaction command log" + return 1 + fi + [[ -S "${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/mosaic-lease/broker.sock" ]] && broker=true + if [[ -f "$HOME/.claude/settings.json" ]]; then + dead_hooks="$(grep -Ec 'mutator-gate\.py|receipt-observer-client\.py' "$HOME/.claude/settings.json" || true)" + fi + if [[ "$broker" == true || "$dead_hooks" -eq 0 ]]; then + STATE_REASON="$([[ "$broker" == true ]] && echo 'activation broker present' || echo 'broker absent and #869 hooks inactive')" + return 0 + fi + STATE_REASON="broker absent but dead #869 enforcement hooks active (count=$dead_hooks)" + return 1 + ;; + P7) + STATE_REASON="no services requested by this installer invocation" + return 0 + ;; + P8) + shell="$(state_target_shell)" + case "${shell##*/}" in + bash|zsh) + login_path="$(env -i HOME="$HOME" USER="$(id -un)" LOGNAME="$(id -un)" SHELL="$shell" PATH=/usr/local/bin:/usr/bin:/bin "$shell" -lc 'command -v mosaic' 2>&1 || true)" + nonlogin_path="$(env -i HOME="$HOME" USER="$(id -un)" LOGNAME="$(id -un)" SHELL="$shell" PATH=/usr/local/bin:/usr/bin:/bin "$shell" -c 'command -v mosaic' 2>&1 || true)" + ;; + fish) + login_path="$(env -i HOME="$HOME" USER="$(id -un)" LOGNAME="$(id -un)" SHELL="$shell" PATH=/usr/local/bin:/usr/bin:/bin "$shell" -lc 'command -v mosaic' 2>&1 || true)" + nonlogin_path="$(env -i HOME="$HOME" USER="$(id -un)" LOGNAME="$(id -un)" SHELL="$shell" PATH=/usr/local/bin:/usr/bin:/bin "$shell" -c 'command -v mosaic' 2>&1 || true)" + ;; + *) STATE_REASON="unsupported or unresolved target shell: ${shell:-unset}"; return 1 ;; + esac + if [[ "$login_path" == "$PREFIX/bin/mosaic" && "$nonlogin_path" == "$PREFIX/bin/mosaic" ]]; then + STATE_REASON="login=$login_path nonlogin=$nonlogin_path equals P3 path" + return 0 + fi + STATE_REASON="fresh ${shell##*/} login=${login_path:-missing} nonlogin=${nonlogin_path:-missing} expected=$PREFIX/bin/mosaic" + return 1 + ;; + esac +} + +state_check_all() { + local phase + STATE_FAILURES=0 + STATE_FAILED_PHASES=() + for phase in "${INSTALL_PHASES[@]:0:9}"; do + if state_predicate "$phase"; then state_emit "$phase" PASS "$STATE_REASON"; else state_emit "$phase" FAIL "$STATE_REASON"; fi + done + [[ "$STATE_FAILURES" -eq 0 ]] +} + +# Component-only installs preserve their historical narrow contract. `--check` +# is never narrowed: it always calls state_check_all above and evaluates P0-P8. +state_check_install_scope() { + local phase + STATE_FAILURES=0 + STATE_FAILED_PHASES=() + for phase in "${INSTALL_PHASES[@]:0:9}"; do + if [[ "$FLAG_CLI" == "true" && "$FLAG_FRAMEWORK" == "false" && "$phase" =~ ^P[4-8]$ ]]; then + state_emit "$phase" PASS "not requested by --cli component-only install" + continue + fi + if [[ "$FLAG_FRAMEWORK" == "true" && "$FLAG_CLI" == "false" && "$phase" == "P3" ]]; then + state_emit "$phase" PASS "not requested by --framework component-only install" + continue + fi + if state_predicate "$phase"; then state_emit "$phase" PASS "$STATE_REASON"; else state_emit "$phase" FAIL "$STATE_REASON"; fi + done + [[ "$STATE_FAILURES" -eq 0 ]] +} + +state_path_is_safe_target() { + local raw="$1" canonical_home normalized owner + canonical_home="$(realpath -e -- "$HOME" 2>/dev/null)" || return 1 + [[ "$HOME" == "$canonical_home" && "$raw" == /* && "$raw" != *$'\n'* ]] || return 1 + normalized="$(realpath -m -- "$raw" 2>/dev/null)" || return 1 + [[ "$normalized" == "$raw" && "$raw" != "$HOME" && "$raw" == "$HOME"/* ]] || return 1 + # realpath -m follows every existing symlink component. Equality therefore + # rejects a target or parent redirected outside the rollback tree. + if [[ -e "$raw" || -L "$raw" ]]; then + [[ ! -L "$raw" ]] || return 1 + owner="$(stat -c '%u' "$raw" 2>/dev/null)" || return 1 + [[ "$owner" == "$(id -u)" ]] || return 1 + fi +} + +state_validate_target_paths() { + local left right i j + local targets=( + "$MOSAIC_HOME" "$PREFIX" "$HOME/.npmrc" "$HOME/.bashrc" "$HOME/.bash_profile" + "$HOME/.profile" "$HOME/.zshrc" "$HOME/.config/fish/config.fish" "$HOME/.claude" + "$HOME/.pi" "$HOME/.codex" "$HOME/.config/opencode" "$HOME/.config/mosaic-gateway" + "$HOME/.config/systemd" "$HOME/.local/share/systemd" "$HOME/.local/state/mosaic-gateway" + "$HOME/.local/state/mosaic/backups" + ) + STATE_PATH_REASON="" + for left in "${targets[@]}"; do + if ! state_path_is_safe_target "$left"; then + STATE_PATH_REASON="unsafe rollback target: $left (must be a non-symlinked, target-user-owned strict descendant of canonical HOME=$HOME)" + return 1 + fi + done + for ((i=0; i<${#targets[@]}; i++)); do + for ((j=i+1; j<${#targets[@]}; j++)); do + left="${targets[$i]}"; right="${targets[$j]}" + if [[ "$left" == "$right" || "$left" == "$right"/* || "$right" == "$left"/* ]]; then + STATE_PATH_REASON="overlapping rollback targets are forbidden: $left and $right" + return 1 + fi + done + done +} + +state_snapshot_create() { + local dst list path key index=0 + if ! state_validate_target_paths; then + fail "P1 Preflight refused snapshot creation: $STATE_PATH_REASON" + return 1 + fi + STATE_SNAPSHOT_DIR="$STATE_RUN_DIR/snapshot" + mkdir -p "$STATE_SNAPSHOT_DIR/data" + list="$STATE_SNAPSHOT_DIR/paths.tsv" + : > "$list" + for path in "$MOSAIC_HOME" "$PREFIX" "$HOME/.npmrc" "$HOME/.bashrc" "$HOME/.bash_profile" \ + "$HOME/.profile" "$HOME/.zshrc" "$HOME/.config/fish/config.fish" "$HOME/.claude" \ + "$HOME/.pi" "$HOME/.codex" "$HOME/.config/opencode" "$HOME/.config/mosaic-gateway" \ + "$HOME/.config/systemd" "$HOME/.local/share/systemd" "$HOME/.local/state/mosaic-gateway" \ + "$HOME/.local/state/mosaic/backups"; do + key="path-$index" + index=$((index + 1)) + if [[ -e "$path" || -L "$path" ]]; then + printf 'present\t%s\t%s\n' "$path" "$key" >> "$list" + dst="$STATE_SNAPSHOT_DIR/data/$key" + cp -a "$path" "$dst" + else + printf 'absent\t%s\t%s\n' "$path" "$key" >> "$list" + fi + done + state_json_line snapshot P1 committed "pre-install snapshot=$STATE_SNAPSHOT_DIR" +} + +state_snapshot_restore() { + local status target key saved + [[ -s "$STATE_SNAPSHOT_DIR/paths.tsv" ]] || return 1 + while IFS=$'\t' read -r status target key; do + [[ -n "$target" ]] || continue + saved="$STATE_SNAPSHOT_DIR/data/$key" + if ! state_path_is_safe_target "$target"; then + fail "Rollback refused unsafe or replaced target path: $target" + return 1 + fi + rm -rf -- "$target" || return + if [[ "$status" == "present" ]]; then + mkdir -p "$(dirname "$target")" || return + cp -a "$saved" "$target" || return + fi + done < "$STATE_SNAPSHOT_DIR/paths.tsv" +} + +state_begin_install() { + local run_id + if ! install -d -m 0700 "$STATE_DIR"; then + fail "P1 Preflight failed: cannot create private journal directory $STATE_DIR" + exit 1 + fi + exec {STATE_LOCK_FD}>"$STATE_DIR/install.lock" + if ! flock -n "$STATE_LOCK_FD"; then + fail "P1 Preflight failed: another Mosaic install holds $STATE_DIR/install.lock" + echo " Remediation: wait for the active install to finish, then rerun." >&2 + exit 1 + fi + run_id="$(date -u +%Y%m%dT%H%M%SZ)-$$" + STATE_RUN_DIR="$STATE_DIR/$run_id" + if ! install -d -m 0700 "$STATE_RUN_DIR"; then + fail "P1 Preflight failed: cannot create private journal run directory $STATE_RUN_DIR" + exit 1 + fi + if [[ -f "$STATE_DIR/active.json" ]] \ + && grep -q '"status"[[:space:]]*:[[:space:]]*"in-progress"' "$STATE_DIR/active.json"; then + STATE_INTERRUPTED_ACTIVE="$STATE_RUN_DIR/prior-active.json" + if ! cp "$STATE_DIR/active.json" "$STATE_INTERRUPTED_ACTIVE"; then + fail "P1 Preflight failed: could not preserve the interrupted transaction projection." + exit 1 + fi + fi + STATE_JOURNAL="$STATE_RUN_DIR/journal.ndjson" + STATE_COMMAND_LOG="$STATE_RUN_DIR/commands.log" + STATE_FRAMEWORK_STATUS="$STATE_RUN_DIR/framework-phase-status.tsv" + if ! install -m 0600 /dev/null "$STATE_JOURNAL" \ + || ! install -m 0600 /dev/null "$STATE_COMMAND_LOG" \ + || ! install -m 0600 /dev/null "$STATE_FRAMEWORK_STATUS"; then + fail "P1 Preflight failed: cannot initialize private journal files in $STATE_RUN_DIR" + exit 1 + fi + export MOSAIC_INSTALL_COMMAND_LOG="$STATE_COMMAND_LOG" + export MOSAIC_INSTALL_PHASE_STATUS_FILE="$STATE_FRAMEWORK_STATUS" + export NPM_CONFIG_CACHE="$STATE_RUN_DIR/npm-cache" + state_write_active "$(printf '{\"status\":\"in-progress\",\"run\":\"%s\",\"journal\":\"%s\"}' "$run_id" "$STATE_JOURNAL")" + state_json_line install P0 opened "transaction opened before target mutation" + if [[ -n "$STATE_INTERRUPTED_ACTIVE" ]]; then + state_json_line recovery P1 resumed "stale in-progress projection preserved at $STATE_INTERRUPTED_ACTIVE; OS lock was free; current run starts from the honestly retained partial state" + fi +} + +state_handle_unexpected_failure() { + local code="$1" phase="${2:-$STATE_CURRENT_PHASE}" + trap - ERR INT TERM + set +e + state_json_line install "$phase" failed "unexpected command failure exit=$code; rollback started" + if state_snapshot_restore; then + state_json_line install "$phase" rolled-back "pre-install snapshot restored" + state_write_active "$(printf '{\"status\":\"rolled-back\",\"phase\":\"%s\",\"journal\":\"%s\"}' "$phase" "$STATE_JOURNAL")" + fail "$phase $(phase_name "$phase") failed (exit $code); pre-install snapshot restored." + else + state_json_line install "$phase" rollback-failed "snapshot restoration failed or refused an unsafe target" + state_write_active "$(printf '{\"status\":\"rollback-failed\",\"phase\":\"%s\",\"journal\":\"%s\"}' "$phase" "$STATE_JOURNAL")" + fail "$phase $(phase_name "$phase") failed (exit $code); automatic rollback did not complete." + fi + echo " Remediation: inspect $STATE_COMMAND_LOG and $STATE_JOURNAL, correct the named failure, then rerun." >&2 + exit "$code" +} + +state_mark_resumable_failure() { + local failed="${STATE_FAILED_PHASES[*]}" + trap - ERR INT TERM + state_json_line install P9 failed-resumable "failed phases=$failed; mutations retained for explicit remediation" + state_write_active "$(printf '{\"status\":\"failed-resumable\",\"phases\":\"%s\",\"journal\":\"%s\"}' "$failed" "$STATE_JOURNAL")" + fail "P9 Verify + commit failed: postconditions failed in ${failed:-unknown}." + echo " Remediation: fix each named phase, then run this installer with --check; journal: $STATE_JOURNAL" >&2 +} + +state_self_test() { + local phase path + state_begin_install + state_snapshot_create + trap 'state_handle_unexpected_failure "$?" "$STATE_CURRENT_PHASE"' ERR INT TERM + for phase in P2 P3 P4 P5 P6 P7 P8; do + state_phase_begin "$phase" + case "$phase" in + P2) path="$MOSAIC_HOME/.selftest-artifact" ;; + P3) path="$PREFIX/bin/mosaic" ;; + P4) path="$MOSAIC_HOME/.selftest-framework" ;; + P5) path="$MOSAIC_HOME/SOUL.md" ;; + P6) path="$HOME/.claude/settings.json" ;; + P7) path="$MOSAIC_HOME/.selftest-service" ;; + P8) path="$HOME/.bashrc" ;; + esac + state_record_mutation "$phase" "$path" "restore representative path from $STATE_SNAPSHOT_DIR" + mkdir -p "$(dirname "$path")" + printf 'mutated-by-%s\n' "$phase" > "$path" + state_phase_finish "$phase" committed "representative mutation committed" + if [[ "${MOSAIC_INSTALL_FAULT_AFTER:-}" == "$phase" ]]; then + state_json_line fault "$phase" injected "phase=$phase" + echo "Injected installer fault: phase=$phase" >&2 + state_snapshot_restore + state_json_line install "$phase" rolled-back "fault injection restored pre-install snapshot" + state_write_active "$(printf '{\"status\":\"rolled-back\",\"phase\":\"%s\",\"journal\":\"%s\"}' "$phase" "$STATE_JOURNAL")" + exit 97 + fi + done + fail "self-test requires MOSAIC_INSTALL_FAULT_AFTER=P2..P8" + exit 2 +} + +resolve_source_commit() { + local encoded_ref body headers content_type + encoded_ref="$(node -p 'encodeURIComponent(process.argv[1])' "$GIT_REF")" + body="$(mktemp "${TMPDIR:-/tmp}/mosaic-ref.XXXXXX.json")" || return + headers="$(mktemp "${TMPDIR:-/tmp}/mosaic-ref.XXXXXX.headers")" || { rm -f "$body"; return 1; } + if ! curl -fsSL -D "$headers" -o "$body" \ + "https://git.mosaicstack.dev/api/v1/repos/mosaicstack/stack/commits?sha=${encoded_ref}&limit=1"; then + rm -f "$body" "$headers" + fail "P2 Acquire artifacts failed: could not resolve source ref '$GIT_REF'." + return 1 + fi + content_type="$(awk 'BEGIN{IGNORECASE=1} /^content-type:/{gsub(/\r/,""); sub(/^[^:]+:[[:space:]]*/,""); print; exit}' "$headers")" + if [[ "$content_type" != application/json* ]]; then + rm -f "$body" "$headers" + fail "P2 Acquire artifacts failed: ref endpoint returned content-type '${content_type:-missing}', not JSON." + return 1 + fi + RESOLVED_SOURCE_COMMIT="$(node -e ' + const fs=require("fs"); const rows=JSON.parse(fs.readFileSync(process.argv[1],"utf8")); + if (!Array.isArray(rows) || rows.length!==1 || typeof rows[0].sha!=="string" || !/^[0-9a-f]{40}$/.test(rows[0].sha)) process.exit(1); + process.stdout.write(rows[0].sha); + ' "$body")" || { + rm -f "$body" "$headers" + fail "P2 Acquire artifacts failed: ref endpoint did not return exactly one commit with a sha." + return 1 + } + rm -f "$body" "$headers" + ARCHIVE_URL="${REPO_BASE}/archive/${RESOLVED_SOURCE_COMMIT}.tar.gz" +} + +# Download + extract the monorepo archive at the resolved immutable commit +# exactly once per run. Sets EXTRACTED_DIR for both P3 source fallback and P4. ensure_monorepo() { if [[ -n "$EXTRACTED_DIR" ]] && [[ -d "$EXTRACTED_DIR" ]]; then return 0 fi - require_cmd tar + require_cmd tar || return - WORK_DIR="$(mktemp -d "${TMPDIR:-/tmp}/mosaic-install-XXXXXX")" - # shellcheck disable=SC2317 + if [[ -n "$STATE_RUN_DIR" ]]; then + WORK_DIR="$STATE_RUN_DIR/work" + mkdir -p "$WORK_DIR" || return + else + WORK_DIR="$(mktemp -d "${TMPDIR:-/tmp}/mosaic-install-XXXXXX")" || return + fi + # shellcheck disable=SC2329 # Invoked by the EXIT trap below. cleanup_work() { [[ -n "$WORK_DIR" ]] && rm -rf "$WORK_DIR"; } trap cleanup_work EXIT - info "Downloading source from ${GIT_REF}…" - if command -v curl &>/dev/null; then - curl -fsSL "$ARCHIVE_URL" | tar xz -C "$WORK_DIR" - elif command -v wget &>/dev/null; then - wget -qO- "$ARCHIVE_URL" | tar xz -C "$WORK_DIR" + local archive="$WORK_DIR/source.tar.gz" + local max_archive_bytes="${MOSAIC_INSTALL_MAX_ARCHIVE_BYTES:-268435456}" + local max_expanded_bytes="${MOSAIC_INSTALL_MAX_EXPANDED_BYTES:-1073741824}" + if [[ -n "$LOCAL_SOURCE_ARCHIVE" ]]; then + if [[ ! "$LOCAL_SOURCE_COMMIT" =~ ^[0-9a-f]{40}$ || ! "$LOCAL_SOURCE_SHA256" =~ ^[0-9a-f]{64}$ \ + || ! -f "$LOCAL_SOURCE_ARCHIVE" || -L "$LOCAL_SOURCE_ARCHIVE" ]]; then + fail "P2 Acquire artifacts failed: local checkout fixture requires a regular archive plus exact 40-hex source ID and 64-hex SHA-256." + return 1 + fi + RESOLVED_SOURCE_COMMIT="$LOCAL_SOURCE_COMMIT" + cp "$LOCAL_SOURCE_ARCHIVE" "$archive" || return + RESOLVED_SOURCE_DIGEST="$(sha256sum "$archive" | awk '{print $1}')" || return + if [[ "$RESOLVED_SOURCE_DIGEST" != "$LOCAL_SOURCE_SHA256" ]]; then + fail "P2 Acquire artifacts failed: local checkout archive digest does not match the fixture-pinned SHA-256." + return 1 + fi + info "Acquiring checkout fixture at content ID ${RESOLVED_SOURCE_COMMIT} with pinned SHA-256 ${RESOLVED_SOURCE_DIGEST}…" else - fail "curl or wget required to download source." - exit 1 + [[ -n "${RESOLVED_SOURCE_COMMIT:-}" ]] || resolve_source_commit || return + info "Downloading source ref ${GIT_REF} at pinned commit ${RESOLVED_SOURCE_COMMIT}…" + if command -v curl &>/dev/null; then + curl -fsSL --max-filesize "$max_archive_bytes" "$ARCHIVE_URL" -o "$archive" || return + elif command -v wget &>/dev/null; then + wget -O "$archive" "$ARCHIVE_URL" || return + else + fail "curl or wget required to download source." + return 1 + fi + RESOLVED_SOURCE_DIGEST="$(sha256sum "$archive" | awk '{print $1}')" || return fi + local archive_bytes + archive_bytes="$(stat -c '%s' "$archive" 2>/dev/null)" || return + if [[ ! "$archive_bytes" =~ ^[0-9]+$ || "$archive_bytes" -gt "$max_archive_bytes" ]]; then + fail "P2 Acquire artifacts failed: source archive exceeds the configured compressed-size limit." + return 1 + fi + # Reject traversal, links, devices, excessive entry counts, and expansion + # bombs before tar writes a byte. The immutable commit + digest are retained + # as provenance; authenticated release metadata remains the trust root for a + # future distribution-artifact lane. + if ! MAX_EXPANDED_BYTES="$max_expanded_bytes" python3 - "$archive" <<'PY' +import os +import pathlib +import sys +import tarfile + +archive = sys.argv[1] +limit = int(os.environ["MAX_EXPANDED_BYTES"]) +total = 0 +with tarfile.open(archive, "r:gz") as tf: + members = tf.getmembers() + if not members or len(members) > 100_000: + raise SystemExit(1) + for member in members: + pure = pathlib.PurePosixPath(member.name) + if pure.is_absolute() or ".." in pure.parts or member.issym() or member.islnk() or member.isdev(): + raise SystemExit(1) + if not (member.isfile() or member.isdir()): + raise SystemExit(1) + total += member.size + if total > limit: + raise SystemExit(1) +PY + then + fail "P2 Acquire artifacts failed: archive safety/integrity check failed (sha256=$RESOLVED_SOURCE_DIGEST)." + return 1 + fi + tar xzf "$archive" -C "$WORK_DIR" || return + state_json_line artifact P2 committed "lane=$GIT_REF source_commit=$RESOLVED_SOURCE_COMMIT sha256=$RESOLVED_SOURCE_DIGEST" || return # Gitea archives extract to / inside the work dir - EXTRACTED_DIR="$(find "$WORK_DIR" -maxdepth 1 -mindepth 1 -type d | head -1)" + EXTRACTED_DIR="$(find "$WORK_DIR" -maxdepth 1 -mindepth 1 -type d | head -1)" || return if [[ -z "$EXTRACTED_DIR" ]] || [[ ! -d "$EXTRACTED_DIR" ]]; then fail "Could not locate extracted source in archive." - ls -la "$WORK_DIR" >&2 - exit 1 + ls -la "$WORK_DIR" >&2 || true # Diagnostic only; the named P2 failure is authoritative. + return 1 fi } @@ -324,7 +1148,7 @@ ensure_monorepo() { install_cli_from_source() { local src="$EXTRACTED_DIR" local out_dir="$WORK_DIR/dist-tarballs" - mkdir -p "$out_dir" + mkdir -p "$out_dir" || return # pnpm via corepack (ships with Node >= 16.9; required by Node >= 20 preflight). # Pin to the repo's packageManager version so the build matches CI. Surface @@ -339,47 +1163,113 @@ install_cli_from_source() { if ! command -v pnpm &>/dev/null; then fail "pnpm not available after corepack activation." echo " Install pnpm manually (https://pnpm.io/installation) and re-run with --dev." - exit 1 + return 1 fi info "Installing workspace dependencies (pnpm install)…" - ( cd "$src" && pnpm install ) 2>&1 | sed 's/^/ /' + ( cd "$src" && pnpm install ) 2>&1 | sed 's/^/ /' || return info "Building CLI + gateway from source…" - ( cd "$src" && pnpm --filter "@mosaicstack/mosaic..." --filter "@mosaicstack/gateway..." run build ) 2>&1 | sed 's/^/ /' + ( cd "$src" && pnpm --filter "@mosaicstack/mosaic..." --filter "@mosaicstack/gateway..." run build ) 2>&1 | sed 's/^/ /' || return info "Packing local tarballs…" - ( cd "$src/packages/mosaic" && pnpm pack --pack-destination "$out_dir" ) 2>&1 | sed 's/^/ /' - ( cd "$src/apps/gateway" && pnpm pack --pack-destination "$out_dir" ) 2>&1 | sed 's/^/ /' + ( cd "$src/packages/mosaic" && pnpm pack --pack-destination "$out_dir" ) 2>&1 | sed 's/^/ /' || return + ( cd "$src/apps/gateway" && pnpm pack --pack-destination "$out_dir" ) 2>&1 | sed 's/^/ /' || return local cli_tgz gw_tgz - cli_tgz="$(ls -1t "$out_dir"/mosaicstack-mosaic-*.tgz 2>/dev/null | head -1)" - gw_tgz="$(ls -1t "$out_dir"/mosaicstack-gateway-*.tgz 2>/dev/null | head -1)" + cli_tgz="$(newest_matching_file "$out_dir" 'mosaicstack-mosaic-*.tgz')" + gw_tgz="$(newest_matching_file "$out_dir" 'mosaicstack-gateway-*.tgz')" if [[ ! -f "$cli_tgz" ]]; then fail "CLI tarball was not produced by pnpm pack." - exit 1 + return 1 fi if [[ ! -f "$gw_tgz" ]]; then fail "Gateway tarball was not produced by pnpm pack." - exit 1 + return 1 fi # Gateway first so it is present globally before the CLI's wizard runs (which # skips its own gateway install via MOSAIC_GATEWAY_SKIP_NPM_INSTALL=1). info "Installing gateway from source tarball (global)…" - npm install -g "$gw_tgz" --prefix="$PREFIX" 2>&1 | sed 's/^/ /' + npm install -g "$gw_tgz" --prefix="$PREFIX" 2>&1 | sed 's/^/ /' || return info "Installing CLI from source tarball (global)…" - npm install -g "$cli_tgz" --prefix="$PREFIX" 2>&1 | sed 's/^/ /' + npm install -g "$cli_tgz" --prefix="$PREFIX" 2>&1 | sed 's/^/ /' || return - ok "Installed from source: CLI $(installed_cli_version)" + # Source fallback replaces the registry candidate with the package version + # produced by the pinned source commit. P3 must compare against what P2 + # actually selected, not the failed registry candidate. + RESOLVED_CLI_VERSION="$(installed_cli_version)" || return + [[ -n "$RESOLVED_CLI_VERSION" ]] || { fail "Source install did not expose an installed CLI version."; return 1; } + state_json_line artifact P2 committed "source fallback selected cli_version=$RESOLVED_CLI_VERSION source_commit=${RESOLVED_SOURCE_COMMIT:-unknown}" || return + ok "Installed from source: CLI $RESOLVED_CLI_VERSION" } -# ─── preflight ──────────────────────────────────────────────────────────────── +install_next_cli_from_registry() { + local cli_next gateway_next + cli_next="$(next_cli_version)" + gateway_next="$(next_gateway_version)" + + if [[ -z "$cli_next" ]]; then + warn "${CLI_PKG}@next is unavailable from $REGISTRY." + return 1 + fi + if [[ -z "$gateway_next" ]]; then + warn "${GATEWAY_PKG}@next is unavailable from $REGISTRY." + return 1 + fi + + if ! next_versions_share_pipeline "$cli_next" "$gateway_next"; then + warn "@next CLI/gateway versions do not share a pipeline suffix (${cli_next}, ${gateway_next})." + return 1 + fi + + info "Installing ${CLI_PKG}@${cli_next} from registry…" + if ! npm install -g "${CLI_PKG}@${cli_next}" --prefix="$PREFIX" 2>&1 | sed 's/^/ /'; then + warn "Fast CLI @next install failed." + return 1 + fi + + info "Installing ${GATEWAY_PKG}@${gateway_next} from registry…" + if ! npm install -g "${GATEWAY_PKG}@${gateway_next}" --prefix="$PREFIX" 2>&1 | sed 's/^/ /'; then + warn "Fast gateway @next install failed." + return 1 + fi + + local installed_cli installed_gateway + installed_cli="$(installed_cli_version)" + installed_gateway="$(installed_gateway_version)" + if [[ "$installed_cli" != "$cli_next" || "$installed_gateway" != "$gateway_next" ]]; then + warn "Installed @next versions did not match resolved versions (CLI: ${installed_cli:-missing}, gateway: ${installed_gateway:-missing})." + return 1 + fi + + export MOSAIC_GATEWAY_SKIP_NPM_INSTALL=1 + ok "Installed @next packages: CLI ${installed_cli}, gateway ${installed_gateway}" +} + +# ─── preflight / state-machine dispatch ────────────────────────────────────── + +if [[ "$FLAG_STATE_SELF_TEST" == "true" ]]; then + require_cmd node + require_cmd flock + state_self_test +fi + +# `--check` exits before mkdir, npm-prefix setup, locks, snapshots, downloads, or +# any other target mutation. Temporary observation files live under TMPDIR and +# are removed in the predicate that creates them. +if [[ "$FLAG_CHECK" == "true" ]]; then + check_status=0 + state_check_all || check_status=$? + rm -rf "$STATE_NPM_CACHE" + exit "$check_status" +fi require_cmd node require_cmd npm +require_cmd flock NODE_MAJOR="$(node -e 'process.stdout.write(String(process.versions.node.split(".")[0]))')" if [[ "$NODE_MAJOR" -lt 20 ]]; then @@ -391,10 +1281,53 @@ echo "" echo "${BOLD}Mosaic Stack Installer${RESET}" echo "" +# P0/P1 are pure preconditions. Open the durable journal and snapshot only after +# they pass, but before P2 performs the first target mutation. +if state_predicate P0; then + P0_REASON="$STATE_REASON" + state_emit P0 PASS "$P0_REASON" +else + state_emit P0 FAIL "$STATE_REASON" + fail "P0 Resolve context failed." + echo " Remediation: run as a supported non-root target user with explicit HOME/shell, glibc x86_64, and Node.js >=20." >&2 + exit 1 +fi +if state_predicate P1; then + P1_REASON="$STATE_REASON" + state_emit P1 PASS "$P1_REASON" +else + state_emit P1 FAIL "$STATE_REASON" + fail "P1 Preflight failed." + echo " Remediation: install the named prerequisites, clear any active transaction, and ensure the npm prefix parent is writable." >&2 + exit 1 +fi +state_begin_install +state_phase_finish P0 committed "$P0_REASON" +state_phase_finish P1 committed "$P1_REASON; exclusive lock acquired; journal opened" +state_snapshot_create +trap 'state_handle_unexpected_failure "$?" "$STATE_CURRENT_PHASE"' ERR INT TERM + +state_phase_begin P2 +state_record_mutation P2 "$STATE_RUN_DIR/work" "discard acquired temporary artifacts" +if [[ "$FLAG_DEV" == "true" ]]; then + RESOLVED_CLI_VERSION="" +else + RESOLVED_CLI_VERSION="$(state_resolved_version)" + if [[ -z "$RESOLVED_CLI_VERSION" ]]; then + fail "P2 Acquire artifacts failed: could not resolve a pinned CLI version for the requested lane." + false + fi +fi +if [[ "$FLAG_FRAMEWORK" == "true" || "$FLAG_DEV" == "true" ]]; then + state_run_captured "P2 acquire pinned source archive" ensure_monorepo +fi +state_phase_finish P2 committed "lane=$([[ "$FLAG_NEXT" == true ]] && echo next || echo latest) cli_version=${RESOLVED_CLI_VERSION:-pending-source-package-build} source_commit=${RESOLVED_SOURCE_COMMIT:-deferred-until-source-fallback} sha256=${RESOLVED_SOURCE_DIGEST:-deferred-until-source-fallback}" + # ═══════════════════════════════════════════════════════════════════════════════ # PART 1: Framework (bash launcher + guides + runtime configs + tools) # ═══════════════════════════════════════════════════════════════════════════════ +install_phase_p4_action() { if [[ "$FLAG_FRAMEWORK" == "true" ]]; then step "Framework (~/.config/mosaic)" @@ -409,7 +1342,7 @@ if [[ "$FLAG_FRAMEWORK" == "true" ]]; then else dim " Installed: (none)" fi - dim " Source: ${REPO_BASE} (ref: ${GIT_REF})" + dim " Source: ${REPO_BASE} ($(source_ref_details))" echo "" if [[ "$FLAG_CHECK" == "true" ]]; then @@ -419,15 +1352,15 @@ if [[ "$FLAG_FRAMEWORK" == "true" ]]; then warn "Framework not installed." fi else - # Download repo archive and extract framework (shared with the dev build) - ensure_monorepo + # Download repo archive and extract framework (shared with the dev build). + ensure_monorepo || return FRAMEWORK_SRC="$EXTRACTED_DIR/packages/mosaic/framework" if [[ ! -d "$FRAMEWORK_SRC" ]]; then fail "Framework not found in archive at packages/mosaic/framework/" fail "Archive contents:" - ls -la "$WORK_DIR" >&2 - exit 1 + ls -la "$WORK_DIR" >&2 || true # Diagnostic only; missing framework remains fatal. + return 1 fi # Run the framework's own install.sh (handles keep/overwrite for SOUL.md etc.) @@ -435,7 +1368,7 @@ if [[ "$FLAG_FRAMEWORK" == "true" ]]; then MOSAIC_INSTALL_MODE="${MOSAIC_INSTALL_MODE:-keep}" \ MOSAIC_ALLOW_MISSING_SEQUENTIAL_THINKING=1 \ MOSAIC_SKIP_SKILLS_SYNC="${MOSAIC_SKIP_SKILLS_SYNC:-0}" \ - bash "$FRAMEWORK_SRC/install.sh" + bash "$FRAMEWORK_SRC/install.sh" || return ok "Framework installed" echo "" @@ -444,18 +1377,20 @@ if [[ "$FLAG_FRAMEWORK" == "true" ]]; then # to mosaic-launch directly via its absolute path. fi fi +} # ═══════════════════════════════════════════════════════════════════════════════ # PART 2: @mosaicstack/mosaic (npm — TUI, gateway client, wizard, CLI) # ═══════════════════════════════════════════════════════════════════════════════ +install_phase_p3_action() { if [[ "$FLAG_CLI" == "true" ]]; then step "@mosaicstack/mosaic (npm package)" # Ensure prefix dir if [[ ! -d "$PREFIX" ]]; then info "Creating global prefix directory: $PREFIX" - mkdir -p "$PREFIX"/{bin,lib} + mkdir -p "$PREFIX"/{bin,lib} || return fi # Ensure npmrc scope mapping @@ -464,20 +1399,24 @@ if [[ "$FLAG_CLI" == "true" ]]; then if ! grep -qF "$SCOPE_LINE" "$NPMRC" 2>/dev/null; then info "Adding ${SCOPE} registry to $NPMRC" - echo "$SCOPE_LINE" >> "$NPMRC" + echo "$SCOPE_LINE" >> "$NPMRC" || return ok "Registry configured" fi if ! grep -qF "prefix=$PREFIX" "$NPMRC" 2>/dev/null; then if ! grep -q '^prefix=' "$NPMRC" 2>/dev/null; then - echo "prefix=$PREFIX" >> "$NPMRC" + echo "prefix=$PREFIX" >> "$NPMRC" || return info "Set npm global prefix to $PREFIX" fi fi CURRENT="$(installed_cli_version)" + NEXT_GATEWAY="" if [[ "$FLAG_DEV" == "true" ]]; then LATEST="" + elif is_next_registry_lane; then + LATEST="$(next_cli_version)" + NEXT_GATEWAY="$(next_gateway_version)" else LATEST="$(latest_cli_version)" fi @@ -489,7 +1428,19 @@ if [[ "$FLAG_CLI" == "true" ]]; then fi if [[ "$FLAG_DEV" == "true" ]]; then - dim " Source: ${REPO_BASE} (ref: ${GIT_REF}, build-from-source)" + dim " Source: ${REPO_BASE} ($(source_ref_details), build-from-source)" + elif is_next_registry_lane; then + if [[ -n "$LATEST" ]]; then + dim " Next CLI: ${CLI_PKG}@${LATEST}" + else + dim " Next CLI: (registry @next unreachable)" + fi + if [[ -n "$NEXT_GATEWAY" ]]; then + dim " Next GW: ${GATEWAY_PKG}@${NEXT_GATEWAY}" + else + dim " Next GW: (registry @next unreachable)" + fi + dim " Fallback: ${REPO_BASE} (ref: next, build-from-source)" elif [[ -n "$LATEST" ]]; then dim " Latest: ${CLI_PKG}@${LATEST}" else @@ -500,6 +1451,12 @@ if [[ "$FLAG_CLI" == "true" ]]; then if [[ "$FLAG_CHECK" == "true" ]]; then if [[ "$FLAG_DEV" == "true" ]]; then info "Dev mode: installed version is ${CURRENT:-(none)} (no registry comparison)." + elif is_next_registry_lane; then + if [[ -n "$LATEST" && -n "$NEXT_GATEWAY" ]] && next_versions_share_pipeline "$LATEST" "$NEXT_GATEWAY"; then + ok "@next registry lane available: ${CLI_PKG}@${LATEST}, ${GATEWAY_PKG}@${NEXT_GATEWAY}." + else + warn "@next registry lane incomplete, mismatched, or unreachable; --next would fall back to source." + fi elif [[ -z "$LATEST" ]]; then warn "Could not reach registry." elif [[ -z "$CURRENT" ]]; then @@ -513,8 +1470,25 @@ if [[ "$FLAG_CLI" == "true" ]]; then fi elif [[ "$FLAG_DEV" == "true" ]]; then info "Dev mode — building CLI + gateway from source at ref ${GIT_REF}…" - ensure_monorepo - install_cli_from_source + ensure_monorepo || return + install_cli_from_source || return + + # PATH check for npm prefix + if [[ ":$PATH:" != *":$PREFIX/bin:"* ]]; then + warn "$PREFIX/bin is not on your PATH" + dim " Add to your shell rc: export PATH=\"$PREFIX/bin:\$PATH\"" + fi + elif is_next_registry_lane; then + info "Next mode — trying fast npm @next install from ${REGISTRY}…" + if install_next_cli_from_registry; then + : + else + warn "Falling back to source build at ref ${GIT_REF}; --next will not hard-fail on registry issues." + unset MOSAIC_GATEWAY_SKIP_NPM_INSTALL + ensure_monorepo || return + install_cli_from_source || return + export MOSAIC_GATEWAY_SKIP_NPM_INSTALL=1 + fi # PATH check for npm prefix if [[ ":$PATH:" != *":$PREFIX/bin:"* ]]; then @@ -526,13 +1500,13 @@ if [[ "$FLAG_CLI" == "true" ]]; then warn "Could not reach registry at $REGISTRY — skipping npm CLI." elif [[ -z "$CURRENT" ]]; then info "Installing ${CLI_PKG}@${LATEST}…" - npm install -g "${CLI_PKG}@${LATEST}" --prefix="$PREFIX" 2>&1 | sed 's/^/ /' + npm install -g "${CLI_PKG}@${LATEST}" --prefix="$PREFIX" 2>&1 | sed 's/^/ /' || return ok "CLI installed: $(installed_cli_version)" elif [[ "$CURRENT" == "$LATEST" ]]; then ok "Already at latest version ($LATEST)." elif version_lt "$CURRENT" "$LATEST"; then info "Upgrading ${CLI_PKG}: $CURRENT → $LATEST…" - npm install -g "${CLI_PKG}@${LATEST}" --prefix="$PREFIX" 2>&1 | sed 's/^/ /' + npm install -g "${CLI_PKG}@${LATEST}" --prefix="$PREFIX" 2>&1 | sed 's/^/ /' || return ok "CLI upgraded: $(installed_cli_version)" else ok "CLI is at or ahead of registry ($CURRENT ≥ $LATEST)." @@ -545,6 +1519,51 @@ if [[ "$FLAG_CLI" == "true" ]]; then fi fi fi +} + +# Execute actions in canonical order. The old installer ran P4 before P3, which +# made runtime-link diagnostics depend on shell discovery instead of P3's known +# absolute binary. P3 now commits before P4 begins. +state_phase_begin P3 +if [[ "$FLAG_CLI" == "true" ]]; then + state_record_mutation P3 "$PREFIX" "restore prefix from $STATE_SNAPSHOT_DIR" + state_record_mutation P3 "$HOME/.npmrc" "restore npmrc from $STATE_SNAPSHOT_DIR" +fi +state_run_captured "P3 install CLI" install_phase_p3_action +if [[ "$FLAG_CLI" == "false" ]]; then + state_phase_finish P3 not-requested "CLI component excluded by --framework" +elif state_predicate P3; then + state_phase_finish P3 committed "$STATE_REASON" +else + state_phase_finish P3 failed "$STATE_REASON" + fail "P3 Install CLI failed: $STATE_REASON" + false +fi + +state_phase_begin P4 +if [[ "$FLAG_FRAMEWORK" == "true" ]]; then + state_record_mutation P4 "$MOSAIC_HOME" "restore framework tree from $STATE_SNAPSHOT_DIR" + state_record_mutation P4 "$HOME/.pi" "restore Pi runtime assets from $STATE_SNAPSHOT_DIR" + state_record_mutation P4 "$HOME/.claude" "restore Claude runtime assets from $STATE_SNAPSHOT_DIR" + state_record_mutation P4 "$HOME/.codex" "restore Codex runtime assets from $STATE_SNAPSHOT_DIR" + state_record_mutation P4 "$HOME/.config/opencode" "restore OpenCode runtime assets from $STATE_SNAPSHOT_DIR" + state_record_mutation P4 "$HOME/.local/state/mosaic/backups" "restore framework backup state from $STATE_SNAPSHOT_DIR" + state_record_mutation P6 "$HOME/.claude/settings.json" "restore activation settings from $STATE_SNAPSHOT_DIR" +fi +state_run_captured "P4 install framework and skills; P6 evaluate activation" install_phase_p4_action +if [[ "$FLAG_FRAMEWORK" == "false" ]]; then + state_phase_finish P4 not-requested "framework component excluded by --cli" +elif state_predicate P4; then + state_phase_finish P4 committed "$STATE_REASON" +else + # C1 intentionally cannot commit P4 while the shipped-set declaration is + # absent. Keep the partial state for P5-P8 diagnostics; P9 fails non-zero. + state_phase_finish P4 failed-resumable "$STATE_REASON" +fi + +# P5/P7 actions (wizard/service requests) live in the summary flow below and +# bind their mutation records immediately before the wizard executes. P8 is +# observation-only today, so it must not fabricate planned mutation entries. # ═══════════════════════════════════════════════════════════════════════════════ # Summary @@ -580,7 +1599,13 @@ if [[ "$FLAG_CHECK" == "false" ]]; then MOSAIC_CMD="$MOSAIC_BIN" fi - if "$MOSAIC_CMD" wizard; then + state_record_mutation P5 "$MOSAIC_HOME/SOUL.md" "restore identity from $STATE_SNAPSHOT_DIR" + state_record_mutation P5 "$MOSAIC_HOME/USER.md" "restore identity from $STATE_SNAPSHOT_DIR" + state_record_mutation P7 "$HOME/.config/mosaic-gateway" "stop requested services and restore service state" + state_record_mutation P7 "$HOME/.config/systemd" "stop requested services and restore user units" + state_record_mutation P7 "$HOME/.local/share/systemd" "stop requested services and restore user units" + state_record_mutation P7 "$HOME/.local/state/mosaic-gateway" "stop requested services and restore service state" + if state_run_captured "P5 identity and P7 service wizard" "$MOSAIC_CMD" wizard; then ok "Wizard complete." else warn "Wizard exited non-zero." @@ -597,8 +1622,8 @@ if [[ "$FLAG_CHECK" == "false" ]]; then fi # ── Write install manifest ────────────────────────────────────────────────── - # Records what was mutated so that `mosaic uninstall` can precisely reverse it. - # Written last (after all mutations) so an incomplete install leaves no manifest. + # The mutation journal was opened before P2. This projection is written as + # pending-verification and becomes committed only after P9 reasserts P0-P8. MANIFEST_PATH="$MOSAIC_HOME/.install-manifest.json" MANIFEST_CLI_VERSION="$(installed_cli_version)" MANIFEST_FW_VERSION="$(framework_version)" @@ -624,7 +1649,7 @@ if [[ "$FLAG_CHECK" == "false" ]]; then local base dir backup_path backup_val base="$(basename "$dest")" dir="$(dirname "$dest")" - backup_path="$(ls -1t "$dir/${base}.mosaic-bak-"* 2>/dev/null | head -1 || true)" + backup_path="$(newest_matching_file "$dir" "${base}.mosaic-bak-*")" if [[ -n "$backup_path" ]]; then backup_val="\"$backup_path\"" else @@ -642,6 +1667,10 @@ if [[ "$FLAG_CHECK" == "false" ]]; then } RUNTIME_COPIES="$(collect_runtime_copies)" + MANIFEST_P4_OUTCOME="committed" + MANIFEST_P6_OUTCOME="committed" + state_framework_action_failed P4 && MANIFEST_P4_OUTCOME="failed" + state_framework_action_failed P6 && MANIFEST_P6_OUTCOME="failed" # Check whether the npmrc line was present (we may have added it above) NPMRC_LINES_JSON="[]" @@ -649,15 +1678,24 @@ if [[ "$FLAG_CHECK" == "false" ]]; then NPMRC_LINES_JSON="[\"$MANIFEST_SCOPE_LINE\"]" fi - node -e " + MANIFEST_TMP="$MOSAIC_HOME/.install-manifest.json.tmp-$$" + state_record_mutation P9 "$MANIFEST_PATH" "restore manifest/framework tree from $STATE_SNAPSHOT_DIR" + state_record_mutation P9 "$MANIFEST_TMP" "remove pending manifest temp or restore framework tree from $STATE_SNAPSHOT_DIR" + if node -e " const fs = require('fs'); const path = require('path'); const p = process.argv[1]; const m = { - version: 1, + version: 2, + status: 'pending-verification', installedAt: process.argv[2], cliVersion: process.argv[3] || '(unknown)', frameworkVersion: parseInt(process.argv[4] || '0', 10), + lane: process.argv[7], + sourceCommit: process.argv[8], + sourceSha256: process.argv[9], + journal: process.argv[10], + phaseOutcomes: { P4: process.argv[11], P6: process.argv[12] }, mutations: { directories: [path.dirname(p)], npmGlobalPackages: ['@mosaicstack/mosaic'], @@ -667,19 +1705,68 @@ if [[ "$FLAG_CHECK" == "false" ]]; then } }; fs.mkdirSync(path.dirname(p), { recursive: true }); - fs.writeFileSync(p, JSON.stringify(m, null, 2) + '\\n', { mode: 0o600 }); + const tmp=process.argv[13]; + const fd=fs.openSync(tmp,'wx',0o600); + try { fs.writeFileSync(fd,JSON.stringify(m,null,2)+'\n'); fs.fsyncSync(fd); } finally { fs.closeSync(fd); } + fs.renameSync(tmp,p); + const dfd=fs.openSync(path.dirname(p),'r'); + try { fs.fsyncSync(dfd); } finally { fs.closeSync(dfd); } " \ "$MANIFEST_PATH" \ "$MANIFEST_TS" \ "$MANIFEST_CLI_VERSION" \ "$MANIFEST_FW_VERSION" \ "$NPMRC_LINES_JSON" \ - "$RUNTIME_COPIES" 2>/dev/null \ - && ok "Install manifest written: $MANIFEST_PATH" \ - || warn "Could not write install manifest (non-fatal)" + "$RUNTIME_COPIES" \ + "$([[ "$FLAG_NEXT" == true ]] && echo next || echo latest)" \ + "${RESOLVED_SOURCE_COMMIT:-not-requested}" \ + "${RESOLVED_SOURCE_DIGEST:-not-requested}" \ + "$STATE_JOURNAL" \ + "$MANIFEST_P4_OUTCOME" \ + "$MANIFEST_P6_OUTCOME" \ + "$MANIFEST_TMP"; then + ok "Install manifest written pending P9 verification: $MANIFEST_PATH" + else + fail "P9 Verify + commit could not durably write the install manifest." + false + fi + + # Record each deferred phase independently before the aggregate P9 verdict. + for phase in P5 P6 P7 P8; do + state_phase_begin "$phase" + if [[ "$FLAG_CLI" == "true" && "$FLAG_FRAMEWORK" == "false" ]]; then + state_phase_finish "$phase" not-requested "not requested by --cli component-only install" + elif state_predicate "$phase"; then + state_phase_finish "$phase" committed "$STATE_REASON" + else + state_phase_finish "$phase" failed-resumable "$STATE_REASON" + fi + done echo "" - ok "Done." + state_phase_begin P9 + if state_check_install_scope && [[ -s "$MANIFEST_PATH" ]]; then + MANIFEST_COMMIT_TMP="$MOSAIC_HOME/.install-manifest.json.commit-tmp-$$" + state_record_mutation P9 "$MANIFEST_COMMIT_TMP" "remove committed manifest temp or restore framework tree from $STATE_SNAPSHOT_DIR" + node -e ' + const fs=require("fs"), path=require("path"); const p=process.argv[1]; const m=JSON.parse(fs.readFileSync(p,"utf8")); + m.status="committed"; m.committedAt=new Date().toISOString(); + const tmp=process.argv[2]; const fd=fs.openSync(tmp,"wx",0o600); + try { fs.writeFileSync(fd,JSON.stringify(m,null,2)+"\n"); fs.fsyncSync(fd); } finally { fs.closeSync(fd); } + fs.renameSync(tmp,p); const dfd=fs.openSync(path.dirname(p),"r"); + try { fs.fsyncSync(dfd); } finally { fs.closeSync(dfd); } + ' "$MANIFEST_PATH" "$MANIFEST_COMMIT_TMP" + state_json_line install P9 committed "all postconditions verified" + state_phase_finish P9 committed "P0-P8 reasserted; manifest durably committed; journal ready to seal" + state_write_active "$(printf '{\"status\":\"committed\",\"journal\":\"%s\"}' "$STATE_JOURNAL")" + state_seal_journal >/dev/null + trap - ERR INT TERM + ok "Done." + else + state_phase_finish P9 failed-resumable "failed phases=${STATE_FAILED_PHASES[*]}" + state_mark_resumable_failure + exit 1 + fi fi } # end main