diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a207eb20..ba425af6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -709,6 +709,19 @@ jobs: - name: Harness self-test run: bash harness/shell/run-tests.sh harness/shell/lib.test.sh + cloud-environment: + permissions: + contents: read + runs-on: ubuntu-24.04 + timeout-minutes: 15 + steps: + - name: Check out + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Cloud-environment contract test + run: bash harness/shell/run-tests.sh components/cloud-environment/setup.test.sh + distribution: permissions: contents: read @@ -924,6 +937,7 @@ jobs: - dotnet-format - dotnet-fixtures - harness + - cloud-environment - distribution - lefthook - exec-bit @@ -969,6 +983,7 @@ jobs: ${{ needs.dotnet-format.result }} ${{ needs.dotnet-fixtures.result }} ${{ needs.harness.result }} + ${{ needs.cloud-environment.result }} ${{ needs.distribution.result }} ${{ needs.lefthook.result }} ${{ needs.exec-bit.result }} diff --git a/components/cloud-environment/README.md b/components/cloud-environment/README.md new file mode 100644 index 00000000..730cfa5a --- /dev/null +++ b/components/cloud-environment/README.md @@ -0,0 +1,122 @@ +# Cloud environment + +The canonical setup script for the organization's shared Claude Code cloud +environment (the **Melodic** environment at claude.ai/code). The exported +payload is [`setup.sh`](setup.sh); the consumer is the environment's setup +script field, which holds only the three-line bootstrap below, so the real +script lands here by reviewed pull request instead of by hand-editing an +account-scoped UI field. + +Design (one paragraph): cloud environments are account-scoped and +repo-agnostic, and a setup script's result is cached as a filesystem snapshot +— the warm boot. So one shared environment installs the *union* of static +toolchains the fleet pins (.NET SDKs, Node 24, `gh`, PowerShell) inside the +~5-minute cache-build budget, while each repo owns its own setup in a +committed, idempotent `.claude/cloud-bootstrap.sh`. +The full fleet plan, per-repo templates, and verification +checklist live in +[claude-code-plugins `docs/CLOUD-FLEET-SETUP.md`](https://github.com/melodic-software/claude-code-plugins/blob/main/docs/CLOUD-FLEET-SETUP.md). + +## Bootstrap (paste into the environment's setup script field) + +```bash +#!/bin/bash +curl -fsSL https://raw.githubusercontent.com/melodic-software/standards/main/components/cloud-environment/setup.sh \ + -o /tmp/melodic-env-setup.sh && bash /tmp/melodic-env-setup.sh +exit 0 +``` + +`raw.githubusercontent.com` is on the platform's default allowlist, and this +repository is public, so the fetch works from any account's environment with +no credentials. + +## Repo bootstrap handoff + +After the parallel toolchain tracks finish, the script runs the checked-out +repo's committed `.claude/cloud-bootstrap.sh`, best-effort, with +`CLAUDE_CODE_REMOTE=true` and `CLAUDE_PROJECT_DIR` set to the checkout root. +One name, no fallbacks: every fleet repo commits its generic repository setup +at exactly that path, and a repo without the file is a logged no-op. As with +every component change, a merged edit here reaches an environment only on +its next cache rebuild (see [Update lifecycle](#update-lifecycle)). + +## Plugin install + +After the repo bootstrap, a generic, data-driven stage installs the plugins +the checkout declares — nothing more. If `.claude/settings.json` exists, its +`extraKnownMarketplaces` entries are registered (`claude plugin marketplace +add`, skipping ones already registered) and every `enabledPlugins` entry set +to `true` is installed (`claude plugin install --scope user -y`, +skipping ones already installed). Every step is best-effort with a `WARN` +line to the log; the whole stage skips cleanly when the `claude` CLI or `jq` +is unavailable or the file declares no plugin keys. A repo that declares +nothing gets nothing. + +The timing is load-bearing: Claude Code builds its plugin registry at +process start and never re-reads it, so only installs already in the +snapshot a session boots from are loaded at the session's first turn. +Consumer repos declare github-source marketplaces, whose install/update +semantics already handle versions — the commit-drift refresh logic in +claude-code-plugins' own hook is deliberately not replicated here; it is +specific to that repo's directory-source dogfooding. + +## Calling contract (frozen) + +The interface between this component and consuming repositories: + +- The component runs the checked-out repo's `.claude/cloud-bootstrap.sh` — + that exact path — with `CLAUDE_CODE_REMOTE=true` and `CLAUDE_PROJECT_DIR` + set to the checkout root, best-effort. A missing file is a clean no-op. +- Every component step is best-effort (`|| true` semantics) and the script + always exits 0 — a failed step degrades the snapshot, never the + environment build. +- This interface is **frozen**: future changes may add to the component but + never rename the entry-point path, remove or rename the environment + variables, or make any step fail-closed. +- Division of responsibility: the component's installs are a **warm cache**; + each repo's bootstrap is the **correctness guarantee**. Repos must not + assume the component installed anything. This is what keeps component + changes from ever breaking consumers. +- The component is repo-agnostic and must never contain secrets or + repo-specific logic. + +## Network prerequisite + +The environment must use **Custom** network access with **"Also include +default list of common package managers"** checked, plus these hosts: +`dot.net`, `aka.ms`, `builds.dotnet.microsoft.com`, +`download.visualstudio.microsoft.com`. Trusted access 403-blocks the .NET +installer's redirect chain — verified live 2026-08-14 +([claude-code-plugins#2654](https://github.com/melodic-software/claude-code-plugins/issues/2654), +Blocker 1). + +## Verification stamp + +The script logs every step with a timestamp to +`/var/log/melodic-env-setup.log` (falling back to `/tmp`) and writes +`/opt/melodic-env-setup.done` (version + timestamp) as its **last** action. +Verification starts there: a missing stamp means the cache build was +interrupted before completion (claude-code-plugins#2654, Blocker 2) — force a +rebuild by making any edit to the environment's script field. + +## Update lifecycle + +- Toolchain pins (the .NET version list, the Node pin) duplicate the fleet's + manifests by necessity — the script cannot read repos it is not running in. + When a repo bumps `global.json` or `.node-version`, update the pin here and + bump `SCRIPT_VERSION`. +- A merged change does **not** reach existing environments on its own: the + snapshot rebuilds only on an edit to the environment's script/network + fields or on ~7-day cache expiry. To pick up a new version immediately, + make a trivial edit to the environment's script field (a comment character + suffices) to force a rebuild, then confirm the stamp shows the new + `SCRIPT_VERSION`. +- Rollback: environments keep booting from their cached snapshot until + rebuilt, so reverting the commit and forcing a rebuild restores the prior + state; in an emergency the bootstrap can pin a commit SHA in the raw URL + instead of `main`. + +The scope boundary holds as elsewhere in this repository: this component owns +the shared environment baseline only. Repo-specific dependencies and plugin +installs belong to each repo's committed `.claude/cloud-bootstrap.sh` +(templates in the fleet guide above), never to this script. diff --git a/components/cloud-environment/setup.sh b/components/cloud-environment/setup.sh new file mode 100755 index 00000000..9dfa64f4 --- /dev/null +++ b/components/cloud-environment/setup.sh @@ -0,0 +1,185 @@ +#!/bin/bash +# Melodic shared cloud environment — canonical setup script (SSOT). +# +# Consumed by claude.ai/code environments through the three-line bootstrap +# documented in README.md: the environment's script field curls this file from +# raw.githubusercontent.com (on the default allowlist) and runs it, so edits +# land here by pull request instead of by hand-editing an account-scoped UI +# field. Contract: +# - always exit 0 — a non-zero exit fails the environment cache build; +# - stay well under the ~5-minute cache-build budget; +# - repo-agnostic: per-repo work lives in each repo's committed +# .claude/cloud-bootstrap.sh, which this script bakes into the snapshot +# as its final step when the checked-out repo has one. +# +# Diagnosability (claude-code-plugins#2654, Blocker 2 — an interrupted cache +# build left no trace): every step logs a timestamped line to $LOG, and the +# completion stamp is written only as the very last action. Verification +# starts at the stamp: a missing $STAMP means the build never finished — +# force a rebuild (any edit to the environment's script field does). +# +# Network prerequisite (claude-code-plugins#2654, Blocker 1 — verified live +# 2026-08-14): the .NET installer redirect chain (dot.net → aka.ms → +# builds.dotnet.microsoft.com / download.visualstudio.microsoft.com) is +# 403-blocked under Trusted network access. The environment must use Custom +# network access with "Also include default list of common package managers" +# checked plus those four hosts added. +set -u + +SCRIPT_VERSION='2026-08-15.3' +STAMP='/opt/melodic-env-setup.done' +LOG='/var/log/melodic-env-setup.log' +if ! touch "$LOG" 2>/dev/null; then + LOG='/tmp/melodic-env-setup.log' +fi +log() { printf '%s %s\n' "$(date -u +%FT%TZ)" "$*" >>"$LOG"; } + +export DEBIAN_FRONTEND=noninteractive +rm -f "$STAMP" 2>/dev/null +log "start version=$SCRIPT_VERSION" + +# Track A: apt tools — gh CLI + PowerShell (packages.microsoft.com is on the +# default allowlist). +( + if apt-get update -y >>"$LOG" 2>&1; then + log 'apt-get update ok' + else + log 'WARN apt-get update failed' + fi + # gh comes from Ubuntu's own archives: the official cloud-environments + # worked example is exactly `apt update && apt install -y gh`, and + # cli.github.com (the newer upstream apt repo) is NOT on the default + # allowlist. A silent miss is caught by the verification checklist. + if apt-get install -y gh >>"$LOG" 2>&1; then + log 'gh installed' + else + log 'WARN gh install failed' + fi + ubuntu_ver="$(sed -n 's/^VERSION_ID="\{0,1\}\([0-9.]*\).*/\1/p' /etc/os-release)" + if curl -fsSL "https://packages.microsoft.com/config/ubuntu/${ubuntu_ver}/packages-microsoft-prod.deb" \ + -o /tmp/msprod.deb >>"$LOG" 2>&1 && + dpkg -i /tmp/msprod.deb >>"$LOG" 2>&1 && + apt-get update -y >>"$LOG" 2>&1 && + apt-get install -y powershell >>"$LOG" 2>&1; then + log 'powershell installed' + else + log 'WARN powershell install failed' + fi +) & + +# Track B: .NET SDKs — the fleet's exact global.json pins (rollForward: +# disable). Update the version list when a repo's global.json bumps; each +# repo's cloud-bootstrap also installs its exact SDK repo-locally, so the +# env copy is a warm cache and the bootstrap is the correctness guarantee. +( + if curl -fsSL https://dot.net/v1/dotnet-install.sh -o /tmp/dotnet-install.sh >>"$LOG" 2>&1; then + for v in 10.0.302 10.0.400; do + if bash /tmp/dotnet-install.sh --version "$v" --install-dir /opt/dotnet >>"$LOG" 2>&1; then + log "dotnet $v installed" + else + log "WARN dotnet $v install failed" + fi + done + if ! ln -sf /opt/dotnet/dotnet /usr/local/bin/dotnet 2>>"$LOG"; then + log 'WARN dotnet symlink failed' + fi + else + log 'WARN dotnet-install.sh fetch failed — check the Custom allowlist (see header)' + fi +) & + +# Track C: Node 24.18.0 — the fleet .node-version pin; the VM image ships +# Node 20/21/22 only, so this is always an install. +( + export NVM_DIR="${NVM_DIR:-/opt/nvm}" + if [[ -s "$NVM_DIR/nvm.sh" ]]; then + set +u # nvm.sh reads unset vars; subshell-local, tracks A/B keep nounset + # shellcheck disable=SC1091 + if . "$NVM_DIR/nvm.sh" && + nvm install 24.18.0 >>"$LOG" 2>&1 && + nvm alias default 24.18.0 >>"$LOG" 2>&1; then + log 'node 24.18.0 installed' + else + log 'WARN node 24.18.0 install failed' + fi + else + log 'WARN nvm not found; node pin unavailable' + fi +) & + +wait + +# Bake the checked-out repo's committed bootstrap (.claude/cloud-bootstrap.sh) +# into the cached snapshot. One name, no fallbacks: every fleet repo commits +# its generic repository setup — dependencies and plugin installs — there. +# Running it here is load-bearing for plugins: the session's plugin registry +# is built at process start and never re-read, so plugin installs must land +# at cache build, not mid-session. +if [[ -f .claude/cloud-bootstrap.sh ]]; then + if CLAUDE_CODE_REMOTE=true CLAUDE_PROJECT_DIR="$PWD" \ + bash .claude/cloud-bootstrap.sh >>"$LOG" 2>&1; then + log 'repo bootstrap .claude/cloud-bootstrap.sh baked' + else + log 'WARN repo bootstrap .claude/cloud-bootstrap.sh failed (see log)' + fi +else + # Logged rather than silent: a missing "baked" line would otherwise be + # ambiguous between the expected no-op (repo has no bootstrap) and the CWD + # not being the repo checkout, which is a real problem. + log 'no repo bootstrap at .claude/cloud-bootstrap.sh (no-op, or CWD is not the repo checkout)' +fi + +# Generic plugin install — data-driven from the checkout's .claude/settings.json +# (extraKnownMarketplaces + enabledPlugins); no repo-specific logic, and a repo +# that declares nothing gets nothing. This must happen here, at cache build: +# Claude Code reads its plugin registry at process start and never re-reads it, +# so only snapshot-baked installs are loaded at a session's first turn. +# Consumer repos use github-source marketplaces, whose install/update semantics +# already handle versions — no snapshot-refresh logic here (the commit-drift +# refresh in claude-code-plugins' own hook is specific to its directory-source +# dogfooding). +settings='.claude/settings.json' +if ! command -v claude >/dev/null 2>&1; then + log 'plugins: claude CLI not on PATH; skipping' +elif ! command -v jq >/dev/null 2>&1; then + log 'plugins: jq not available; skipping' +elif [[ ! -f "$settings" ]]; then + log 'plugins: no .claude/settings.json; skipping' +elif ! jq -e '(.extraKnownMarketplaces // {}) != {} or (.enabledPlugins // {}) != {}' \ + "$settings" >/dev/null 2>&1; then + log 'plugins: settings declare no marketplaces or plugins; skipping' +else + registered="$(claude plugin marketplace list --json 2>/dev/null | + jq -r '.[].name' 2>/dev/null)" + while IFS=$'\t' read -r mp_name mp_target; do + [[ -n "$mp_name" ]] || continue + if grep -qxF "$mp_name" <<<"$registered"; then + log "plugins: marketplace $mp_name already registered" + elif [[ -z "$mp_target" ]]; then + log "WARN plugins: marketplace $mp_name declares no repo/path/url source; skipped" + elif claude plugin marketplace add "$mp_target" >>"$LOG" 2>&1; then + log "plugins: marketplace $mp_name registered ($mp_target)" + else + log "WARN plugins: marketplace add failed: $mp_name ($mp_target)" + fi + done < <(jq -r '(.extraKnownMarketplaces // {}) | to_entries[] + | [.key, (.value.source.repo // .value.source.path // .value.source.url // "")] + | @tsv' "$settings" 2>/dev/null) + + installed="$(claude plugin list --json 2>/dev/null | jq -r '.[].id' 2>/dev/null)" + while IFS= read -r plugin_id; do + [[ -n "$plugin_id" ]] || continue + if grep -qxF "$plugin_id" <<<"$installed"; then + log "plugins: $plugin_id already installed" + elif claude plugin install "$plugin_id" --scope user -y >>"$LOG" 2>&1; then + log "plugins: installed $plugin_id" + else + log "WARN plugins: install failed: $plugin_id" + fi + done < <(jq -r '(.enabledPlugins // {}) | to_entries[] + | select(.value == true) | .key' "$settings" 2>/dev/null) +fi + +log "done version=$SCRIPT_VERSION" +printf '%s %s\n' "$SCRIPT_VERSION" "$(date -u +%FT%TZ)" >"$STAMP" 2>/dev/null +exit 0 diff --git a/components/cloud-environment/setup.test.sh b/components/cloud-environment/setup.test.sh new file mode 100755 index 00000000..223697f0 --- /dev/null +++ b/components/cloud-environment/setup.test.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# Tests the cloud-environment component: the canonical setup script parses, +# honors its ordering contract (the completion stamp is written only after the +# parallel install tracks finish), and the README documents the same bootstrap +# URL and stamp path the script implements — the drift a copy-paste consumer +# would actually hit. +set -uo pipefail +root="$(git rev-parse --show-toplevel)" +# shellcheck source=harness/shell/lib.sh +source "$root/harness/shell/lib.sh" + +script="$root/components/cloud-environment/setup.sh" +readme="$root/components/cloud-environment/README.md" + +bash -n "$script" 2>/dev/null +rc=$? +assert_exit 'setup.sh parses (bash -n)' 0 "$rc" + +tail -n 1 "$script" | grep -qx 'exit 0' +rc=$? +assert_exit 'setup.sh ends with exit 0 (cache-build contract)' 0 "$rc" + +# Ordering contract: the stamp write must come after the `wait` barrier, so an +# interrupted build can never leave a stamp behind. +wait_ln="$(grep -n '^wait$' "$script" | head -n 1 | cut -d: -f1)" +stamp_ln="$(grep -n '>"[$]STAMP"' "$script" | head -n 1 | cut -d: -f1)" +if [[ -n "$wait_ln" && -n "$stamp_ln" && "$stamp_ln" -gt "$wait_ln" ]]; then + pass 'completion stamp is written only after the wait barrier' +else + fail 'completion stamp is written only after the wait barrier' \ + "wait at line '${wait_ln:-none}', stamp write at line '${stamp_ln:-none}'" +fi + +# README/script drift guards. +stamp_path="$(sed -n "s/^STAMP='\(.*\)'\$/\1/p" "$script")" +if [[ -n "$stamp_path" ]]; then + pass 'setup.sh declares a stamp path' +else + fail 'setup.sh declares a stamp path' "no STAMP='...' assignment found" +fi +assert_contains 'README documents the stamp path the script writes' \ + "$(cat "$readme")" "$stamp_path" +assert_contains 'README bootstrap URL matches the component path' \ + "$(cat "$readme")" \ + 'raw.githubusercontent.com/melodic-software/standards/main/components/cloud-environment/setup.sh' + +[[ $FAILED -eq 0 ]] || exit 1