From 270ba5c703ba1cc5e936343e407ec0aa2aa4a4fb Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Sat, 15 Aug 2026 04:05:51 +0000 Subject: [PATCH 1/3] feat(cloud-environment): add canonical cloud environment setup script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Admit the shared Claude Code cloud environment baseline as a component so the claude.ai/code setup script lands here by reviewed PR instead of by hand-editing an account-scoped UI field. The environment field keeps only a three-line bootstrap that curls this script from raw.githubusercontent.com (default allowlist; public repo, no credentials). Folds in the 2026-08-14 live verification findings (melodic-software/claude-code-plugins#2654): - Blocker 1: document Custom network access (+ dot.net, aka.ms, builds.dotnet.microsoft.com, download.visualstudio.microsoft.com) as a hard prerequisite — Trusted 403-blocks the .NET installer redirect. - Blocker 2: timestamped step logging to /var/log/melodic-env-setup.log and a completion stamp (/opt/melodic-env-setup.done) written strictly last, so an interrupted cache build is detectable instead of silent. Admission evidence: live consumer is the Melodic environment plus claude-code-plugins docs/CLOUD-FLEET-SETUP.md (bootstrap + verification checklist reference this path); acceptance is the stamp check in that checklist; rollback is reverting and forcing a cache rebuild (README). Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01AXaqhrFZeGTzbCJun12Ngx --- components/cloud-environment/README.md | 72 ++++++++++++ components/cloud-environment/setup.sh | 125 +++++++++++++++++++++ components/cloud-environment/setup.test.sh | 47 ++++++++ 3 files changed, 244 insertions(+) create mode 100644 components/cloud-environment/README.md create mode 100755 components/cloud-environment/setup.sh create mode 100755 components/cloud-environment/setup.test.sh diff --git a/components/cloud-environment/README.md b/components/cloud-environment/README.md new file mode 100644 index 00000000..91517691 --- /dev/null +++ b/components/cloud-environment/README.md @@ -0,0 +1,72 @@ +# 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 dependency +bootstrap in a committed, idempotent, `CLAUDE_CODE_REMOTE`-guarded +SessionStart hook. 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. + +## 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 belong to +each repo's committed SessionStart hook (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..f634fb64 --- /dev/null +++ b/components/cloud-environment/setup.sh @@ -0,0 +1,125 @@ +#!/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_CODE_REMOTE-guarded SessionStart hook, 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.1' +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 SessionStart hook also installs its exact SDK repo-locally, so the +# env copy is a warm cache and the hook 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 own bootstrap into the cached snapshot (no-op +# for repos without the hook; hooks are idempotent, so any repo/cache pairing +# is safe — sessions re-run the hook at startup either way). +if [[ -f .claude/hooks/session-start.sh ]]; then + if CLAUDE_CODE_REMOTE=true bash .claude/hooks/session-start.sh >>"$LOG" 2>&1; then + log 'repo SessionStart hook baked' + else + log 'WARN repo SessionStart hook failed (see log; sessions re-run it)' + fi +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 From 4135da052a711b995ebadad3c07f8c0b84fb3a60 Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Sat, 15 Aug 2026 05:21:16 +0000 Subject: [PATCH 2/3] fix(cloud-environment): run the contract test in CI and log the hook-skip case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses both review findings on this PR: - Codex P2: the contract test was not executed by CI — every run-tests.sh invocation in ci.yml names explicit files, so a stamp-ordering or bootstrap-URL regression could merge green. Adds a cloud-environment lane that runs it, wired into the ci-status aggregate gate (both the needs list and the results expression, per actionlint). - Claude review nit: the hook-baking step was the one step with no else branch, so a missing "baked" line could not be told apart from the CWD not being the repo checkout. Logs the skip explicitly, matching the script's every-step-logs contract. Verified: shellcheck -x -P SCRIPTDIR clean, run-tests.sh 1 passed, actionlint clean on ci.yml. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01AXaqhrFZeGTzbCJun12Ngx --- .github/workflows/ci.yml | 15 +++++++++++++++ components/cloud-environment/setup.sh | 5 +++++ 2 files changed, 20 insertions(+) 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/setup.sh b/components/cloud-environment/setup.sh index f634fb64..cee974c0 100755 --- a/components/cloud-environment/setup.sh +++ b/components/cloud-environment/setup.sh @@ -118,6 +118,11 @@ if [[ -f .claude/hooks/session-start.sh ]]; then else log 'WARN repo SessionStart hook failed (see log; sessions re-run it)' fi +else + # Logged rather than silent: a missing "baked" line would otherwise be + # ambiguous between the expected no-op (repo has no hook) and the CWD not + # being the repo checkout, which is a real problem. + log 'no repo SessionStart hook at .claude/hooks/session-start.sh (no-op, or CWD is not the repo checkout)' fi log "done version=$SCRIPT_VERSION" From add10e42d3269d6440798085a918ec55aea95f28 Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Sat, 15 Aug 2026 01:54:40 -0400 Subject: [PATCH 3/3] feat(cloud-environment): bake cloud-bootstrap and install declared plugins (#391) Run the checked-out repo's committed .claude/cloud-bootstrap.sh as the setup script's final repo step (CLAUDE_CODE_REMOTE=true, CLAUDE_PROJECT_DIR at the checkout root; one name, no fallback paths), then a generic data-driven plugin stage: register extraKnownMarketplaces entries and install enabledPlugins entries from the checkout's .claude/settings.json, best-effort, skipping cleanly when claude/jq or plugin keys are absent. Claude Code builds its plugin registry at process start and never re-reads it, so installs must be snapshot-baked to load at a session's first turn. The README documents the frozen calling contract: exact entry-point path and env vars, best-effort and exit-0 semantics, and the warm-cache vs correctness-guarantee division. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01UHbdXDm8B4yj81uQBy8M3M --- components/cloud-environment/README.md | 62 ++++++++++++++++-- components/cloud-environment/setup.sh | 87 +++++++++++++++++++++----- 2 files changed, 127 insertions(+), 22 deletions(-) diff --git a/components/cloud-environment/README.md b/components/cloud-environment/README.md index 91517691..730cfa5a 100644 --- a/components/cloud-environment/README.md +++ b/components/cloud-environment/README.md @@ -11,9 +11,9 @@ 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 dependency -bootstrap in a committed, idempotent, `CLAUDE_CODE_REMOTE`-guarded -SessionStart hook. The full fleet plan, per-repo templates, and verification +~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). @@ -30,6 +30,56 @@ exit 0 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 @@ -67,6 +117,6 @@ rebuild by making any edit to the environment's script field. instead of `main`. The scope boundary holds as elsewhere in this repository: this component owns -the shared environment baseline only. Repo-specific dependencies belong to -each repo's committed SessionStart hook (templates in the fleet guide above), -never to this script. +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 index cee974c0..9dfa64f4 100755 --- a/components/cloud-environment/setup.sh +++ b/components/cloud-environment/setup.sh @@ -8,9 +8,9 @@ # 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_CODE_REMOTE-guarded SessionStart hook, which this script bakes -# into the snapshot as its final step when the checked-out repo has one. +# - 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 @@ -26,7 +26,7 @@ # checked plus those four hosts added. set -u -SCRIPT_VERSION='2026-08-15.1' +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 @@ -69,8 +69,8 @@ log "start version=$SCRIPT_VERSION" # 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 SessionStart hook also installs its exact SDK repo-locally, so the -# env copy is a warm cache and the hook is the correctness guarantee. +# 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 @@ -109,20 +109,75 @@ log "start version=$SCRIPT_VERSION" wait -# Bake the checked-out repo's own bootstrap into the cached snapshot (no-op -# for repos without the hook; hooks are idempotent, so any repo/cache pairing -# is safe — sessions re-run the hook at startup either way). -if [[ -f .claude/hooks/session-start.sh ]]; then - if CLAUDE_CODE_REMOTE=true bash .claude/hooks/session-start.sh >>"$LOG" 2>&1; then - log 'repo SessionStart hook baked' +# 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 SessionStart hook failed (see log; sessions re-run it)' + 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 hook) and the CWD not - # being the repo checkout, which is a real problem. - log 'no repo SessionStart hook at .claude/hooks/session-start.sh (no-op, or CWD is not the repo checkout)' + # 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"