diff --git a/agents/plugin-advisor.md b/agents/plugin-advisor.md index 3957150..053a9df 100644 --- a/agents/plugin-advisor.md +++ b/agents/plugin-advisor.md @@ -280,8 +280,8 @@ scope. Emit the exact command; never write files directly. |---|---|---| | public website, no SEO focus | `web` | `bash $HOME/.claude/lib/profile.sh set web` | | audit-only: SEO + GEO + W3C + WCAG | `seo` | `… set seo` | -| public website end-to-end | `web-full` | `… set web-full` | -| backend / API / library | `backend` | `… set backend` | +| public website end-to-end | `full` | `… set full` | +| backend / API / library | `dev` | `… set dev` | | heavy UI, no dev | `design` | `… set design` | | typical dev session | `dev` | `… set dev` | | browser QA only | `qa` | `… set qa` | diff --git a/docs/superpowers/plans/2026-11-01-phase5-profiles-packs.md b/docs/superpowers/plans/2026-11-01-phase5-profiles-packs.md index a96f73a..31d7f66 100644 --- a/docs/superpowers/plans/2026-11-01-phase5-profiles-packs.md +++ b/docs/superpowers/plans/2026-11-01-phase5-profiles-packs.md @@ -6,7 +6,7 @@ r2 2026-11-02 after three lenses (correctness FATAL(11), robustness FATAL(10), s **Goal:** Replace the global symlink profile system with skills-dir packs switched per project (`.claude/settings.local.json`) and per conversation (`claude --settings lib/profiles/.json`), and delete the old mechanism. -**Architecture:** Nine packs `skills/pack-/` (manifest + tracked relative symlinks into `skills-external/`, four real skill dirs moved in). Eleven profiles `lib/profiles/.json` are literal settings fragments. `lib/profile.sh` (bash) merges a fragment into the project's local settings; `lib/profile-state.jq` computes the effective state in ONE jq pass; `lib/cc.sh` launches a conversation with a fragment as `--settings`; gates, hooks and doctor read `profile.sh current --plain`. +**Architecture:** Nine packs `skills/pack-/` (manifest + tracked relative symlinks into `skills-external/`, four real skill dirs moved in). Nine profiles `lib/profiles/.json` are literal settings fragments. `lib/profile.sh` (bash) merges a fragment into the project's local settings; `lib/profile-state.jq` computes the effective state in ONE jq pass; `lib/cc.sh` launches a conversation with a fragment as `--settings`; gates, hooks and doctor read `profile.sh current --plain`. **Tech Stack:** bash 5, jq 1.8, python3 (existing census only), Claude Code CLI 2.1.316, `make test` suites `lib/tests/*.test.sh`. @@ -60,7 +60,7 @@ r2 2026-11-02 after three lenses (correctness FATAL(11), robustness FATAL(10), s |---|---| | `skills/pack-/.claude-plugin/plugin.json` (9) | pack identity + `defaultEnabled` | | `skills/pack-/skills/` | tracked relative symlink, or the moved real dir (seo, geo, web-validate, site-motion) | -| `lib/profiles/.json` (11) + `README.md` | fragments + descriptions; `*.profile` deleted | +| `lib/profiles/.json` (9) + `README.md` | fragments + descriptions; `*.profile` deleted | | `lib/profile.sh` (rewrite) + `lib/profile-state.jq` (new) | CLI verbs; one jq program for the effective state | | `lib/cc.sh` (new) | `cc [profile]` | | `lib/pack-census.sh` (new) | pack layout + fragment invariants + old-layout citers; `make test` and doctor | diff --git a/lib/cc.sh b/lib/cc.sh index f78686b..3ce9770 100644 --- a/lib/cc.sh +++ b/lib/cc.sh @@ -18,10 +18,16 @@ # TMPDIR goes to ~/.cache/claude-tmp either way (BLK-021). # ============================================================ +_cc_name_ok() { [[ "$1" =~ ^[a-z0-9][a-z0-9-]*$ ]]; } + cc() { local name="${1:-claude-$(date +%H%M%S)}" profile="${2:-}" local lib="$HOME/.claude/lib" settings="" + _cc_name_ok "$name" \ + || { echo "cc: invalid name '$name' (a-z 0-9 -)" >&2; return 2; } if [ -n "$profile" ]; then + _cc_name_ok "$profile" \ + || { echo "cc: invalid profile '$profile' (a-z 0-9 -)" >&2; return 2; } settings="$lib/profiles/$profile.json" if [ ! -f "$settings" ]; then echo "cc: unknown profile '$profile'. Known:" >&2 diff --git a/lib/gstack-removed.sh b/lib/gstack-removed.sh deleted file mode 100644 index 33c8e18..0000000 --- a/lib/gstack-removed.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env bash -# ============================================================ -# lib/gstack-removed.sh — the gstack skills this config never exposes -# -# The denylist itself lives in gstack-removed.list (one name per line), -# read here into GSTACK_REMOVED and by lib/pack-census.sh. A name listed is -# in no pack and no profile, `max` included. Pruned 2026-09-28 (BDR-105), -# helpers housed here per BDR-117, retired with the symlink toggling. -# Why each name went: -# -# ship base = origin/HEAD = main on Gitea, skips develop -# land-and-deploy `gh pr merge --squash --delete-branch` then deploys -# setup-deploy companion of land-and-deploy (Claude never deploys) -# autoplan reads ~/.claude/skills/gstack/plan-*/ paths that do -# not exist in this install -# context-save its pair context-restore is not linked; never used -# learn parallel JSONL store outside .claude/memory, unused -# careful, guard hooks exit 127 (missing bin path) — vacuous; the -# house permissions.deny is stricter (BDR-095) -# design-shotgun mockups need OPENAI_API_KEY, absent -# -# No `set -euo pipefail` here (sourced lib). -# ============================================================ - -mapfile -t GSTACK_REMOVED < "$(dirname "${BASH_SOURCE[0]}")/gstack-removed.list" diff --git a/lib/profile-state.jq b/lib/profile-state.jq index 3a670ae..cac974c 100644 --- a/lib/profile-state.jq +++ b/lib/profile-state.jq @@ -22,6 +22,7 @@ def listing($plugins; $on): | ($u | parse) as $U | ($p | parse) as $P | ($l | parse) as $L | ($f | parse) as $F | ([$profiles[].frag | ep | keys[]] | unique) as $ids +| ([$profiles[].frag | dn[]] | unique) as $managed | ($m | map({key: .id, value: .on}) | from_entries) as $mdef | ($ids | map({key: ., value: ($mdef[.] // (if endswith("@skills-dir") and (is_pack | not) then true @@ -40,12 +41,13 @@ def listing($plugins; $on): | (try ($cj | fromjson | .projects[$proj].disabledMcpServers // [] | if length == 0 then "-" else join(",") end) catch (if $cj == "" then "-" else "unknown" end)) as $off +| ([$denied[] | select(. as $x | $managed | index($x))]) as $mdenied | ([$profiles[] | select((.frag | ep) == $plugins - and ((.frag | dn | sort) == ($denied | sort)) and ($all | not)) + and ((.frag | dn | sort) == ($mdenied | sort)) and ($all | not)) | .name] | .[0] // "custom") as $name | {name: $name, source: $src, flag: $flagname, plugins: $plugins, - denied: $denied, all_denied: $all, model: $model, model_source: $msrc, - mcp_off: $off} as $s + denied: $denied, managed_denied: $mdenied, all_denied: $all, + model: $model, model_source: $msrc, mcp_off: $off} as $s | if $mode == "json" then $s elif $mode == "plain" then ( ["profile=\($s.name)", "source=\($s.source)", "flag=\($s.flag)"] diff --git a/lib/profile.sh b/lib/profile.sh index 089d981..1835096 100755 --- a/lib/profile.sh +++ b/lib/profile.sh @@ -20,8 +20,12 @@ # profile.sh [-C ] enable|disable # profile.sh [-C ] diff # -# (default: cwd) resolves to the nearest ancestor holding .claude/, -# CLAUDE.md or .git, so a subdirectory reads its project's settings. +# (default: cwd) is the session dir. The project root, where the +# local file lives, follows Claude Code: inside a git repo it is the parent +# of the git common dir (a worktree resolves to the main checkout), unless +# that is $HOME or above; otherwise the nearest ancestor holding .claude/, +# CLAUDE.md or .git, below $HOME. The shared .claude/settings.json is read +# from itself. # `current` = lib/profile-state.jq, ONE jq pass over: $CLAUDE_PROFILE_SETTINGS # (the --settings file lib/cc.sh exports), /.claude/settings.local.json, # /.claude/settings.json, ~/.claude/settings.json; a pack absent from @@ -46,7 +50,8 @@ STATE_JQ="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)/profile-state.jq" USER_SETTINGS="${PROFILE_USER_SETTINGS:-$HOME/.claude/settings.json}" CLAUDE_JSON="${PROFILE_CLAUDE_JSON:-$HOME/.claude.json}" LOCAL_REL=".claude/settings.local.json" -PROJECT="$PWD" +PROJECT="$PWD" # the project root: local settings, ~/.claude.json key +SESSION_DIR="$PWD" # the -C dir: shared project settings die() { echo "profile: $*" >&2; exit 2; } usage() { @@ -55,8 +60,28 @@ usage() { } # ---- project root ---------------------------------------------------------- -find_root() { # $1 dir → nearest ancestor with .claude/, CLAUDE.md or .git - local d="$1" # the walk stops below $HOME: ~/.claude is not a project +NAME_RE='^[a-z0-9][a-z0-9-]*$' +PACK_RE='^[A-Za-z0-9][A-Za-z0-9@._:-]*$' +require_name() { # $1 value, $2 regex, $3 label: refuse before any path use + [[ "$1" =~ $2 ]] || die "invalid $3 '$1'" +} +home_or_above() { # $1 dir + [ "$1" = / ] && return 0 + case "$HOME/" in "$1"/*) return 0 ;; esac + return 1 +} +git_root() { # $1 dir → parent of the git common dir, unless $HOME or above + local common root + common="$(git -C "$1" rev-parse --path-format=absolute --git-common-dir \ + 2>/dev/null)" || return 1 + [ "$(basename "$common")" = .git ] || return 1 + root="$(dirname "$common")" + home_or_above "$root" && return 1 + echo "$root" +} +find_root() { # $1 dir → the project root (see header) + local d="$1" + git_root "$d" && return 0 while [ "$d" != / ] && [ "$d" != "$HOME" ]; do if [ -d "$d/.claude" ] || [ -f "$d/CLAUDE.md" ] || [ -e "$d/.git" ]; then echo "$d"; return 0 @@ -94,7 +119,7 @@ json_or_empty() { # $1 file → its JSON, {} when absent; rc 2 when invalid } read_local() { json_or_empty "$PROJECT/$LOCAL_REL" \ - || die "$LOCAL_REL is not valid JSON — nothing written" + || die "$LOCAL_REL is not valid JSON, nothing written" } pack_default() { # $1 id → true | false | missing (manifest defaultEnabled) local m="$SKILLS_DIR/${1%@skills-dir}/.claude-plugin/plugin.json" @@ -119,7 +144,7 @@ state() { # $1 mode: plain | human | json jq -rn --arg mode "$1" --arg proj "$PROJECT" --arg hasflag "$hasflag" \ --arg flagname "${CLAUDE_PROFILE:--}" \ --rawfile u "$(raw_or_null "$USER_SETTINGS")" \ - --rawfile p "$(raw_or_null "$PROJECT/.claude/settings.json")" \ + --rawfile p "$(raw_or_null "$SESSION_DIR/.claude/settings.json")" \ --rawfile l "$(raw_or_null "$PROJECT/$LOCAL_REL")" \ --rawfile f "$(raw_or_null "${CLAUDE_PROFILE_SETTINGS:-}")" \ --rawfile cj "$(raw_or_null "$CLAUDE_JSON")" \ @@ -139,6 +164,7 @@ cmd_list() { } cmd_show() { local f id v tag + require_name "$1" "$NAME_RE" "profile name" f="$(profile_file "$1")"; [ -f "$f" ] || unknown_profile "$1" echo "profile $1: $(profile_desc "$1")" for id in $(jq -r '.enabledPlugins | keys[]' "$f"); do @@ -169,11 +195,13 @@ after_write_hint() { "connectors and model apply to the next session" if have_flag; then echo "note: this conversation runs --settings ${CLAUDE_PROFILE:-?}:" \ - "a pack it turns off stays off here — relaunch: cc " + "the flag fragment fixes every managed key, so this write applies to" \ + "other sessions only; relaunch: cc " fi } cmd_set() { local f cur new + require_name "$1" "$NAME_RE" "profile name" f="$(profile_file "$1")"; [ -f "$f" ] || unknown_profile "$1" require_project_root; cur="$(read_local)" new="$(jq --argjson cur "$cur" \ @@ -206,6 +234,9 @@ cmd_unset() { | if (.deniedMcpServers | length) == 0 then del(.deniedMcpServers) else . end | if $pm != "-" and .model == $pm then del(.model) else . end' <<<"$cur")" + if [ "$(jq -c . <<<"$new")" = "$cur" ]; then + echo "nothing to unset in $LOCAL_REL (was: $name)"; return 0 + fi write_local "$new" echo "profile keys removed from $LOCAL_REL (was: $name)"; after_write_hint } @@ -218,6 +249,7 @@ resolve_id() { # design | pack-design | pack-design@skills-dir | } cmd_toggle() { # $1 true|false, $2 pack or plugin spelling local id cur new + require_name "$2" "$PACK_RE" "pack or plugin name" id="$(resolve_id "$2")" managed_ids | grep -qx -- "$id" \ || die "unknown pack or plugin '$2' (known: $(managed_ids | tr '\n' ' '))" @@ -229,17 +261,18 @@ cmd_toggle() { # $1 true|false, $2 pack or plugin spelling } cmd_diff() { local f out + require_name "$1" "$NAME_RE" "profile name" f="$(profile_file "$1")"; [ -f "$f" ] || unknown_profile "$1" out="$(jq -r --argjson e "$(state json)" ' def onoff: if . then "on" else "off" end; (.enabledPlugins | to_entries[] | select($e.plugins[.key] != .value) | .key + ": " + ($e.plugins[.key] | onoff) + " → " + (.value | onoff)), (([(.deniedMcpServers // [])[] | .serverName] | sort) as $want - | ($e.denied | sort) as $have + | ($e.managed_denied | sort) as $have | if $want == $have then empty else "connectors denied: " + ($have | join(", ")) + " → " + ($want | join(", ")) end), - (if (.model // "-") != $e.model + (if .model != null and .model != $e.model then "model: " + $e.model + " → " + (.model // "-") else empty end)' "$f")" if [ -n "$out" ]; then echo "$out"; else echo "no difference from $1"; fi @@ -255,6 +288,7 @@ while [ $# -gt 0 ]; do *) break ;; esac done +SESSION_DIR="$PROJECT" PROJECT="$(find_root "$PROJECT")" case "${1:-}" in list) cmd_list ;; diff --git a/lib/profiles/README.md b/lib/profiles/README.md index 1accedb..62d69b4 100644 --- a/lib/profiles/README.md +++ b/lib/profiles/README.md @@ -18,10 +18,8 @@ denied; `set web` in the project brings them back next session. - **max**: everything on, pr-review-toolkit included; for a one-off session, not a project. - **minimal**: no pack, no design plugin, connectors denied; the smallest listing. - **dev**: gstack-dev only; backend or tooling work without UI. -- **backend**: same packs as dev; kept as a name for backend projects. - **design**: design, scroll3d, gstack-design, gstack-browser, ui-ux-pro-max; connectors present. - **web**: design + scroll3d + web-validate + the three gstack packs + ui-ux-pro-max; public site work. -- **web-full**: web + seo; a site with SEO/GEO on top. - **seo**: seo, web-validate, gstack-browser, gstack-dev; audit and fixes on a live site. - **qa**: gstack-browser + web-validate; dogfooding and conformance only. - **audit**: seo, web-validate, gstack-dev, gstack-browser, pr-review-toolkit; read-mostly audits. @@ -30,5 +28,5 @@ Invariants (checked by `lib/pack-census.sh`): the same key set in every fragment; `model-router@skills-dir` and `security-guidance@claude-code-plugins` true everywhere; `example-skills@anthropic-agent-skills` and `brightdata-plugin@synced` false everywhere; the connectors denied on exactly -minimal, dev, backend, seo, qa, audit; `full` equal to the manifest defaults. +minimal, dev, seo, qa, audit; `full` equal to the manifest defaults. Add your own profile as another `.json` with the same key set. diff --git a/lib/profiles/backend.json b/lib/profiles/backend.json deleted file mode 100644 index 829bef0..0000000 --- a/lib/profiles/backend.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "enabledPlugins": { - "model-router@skills-dir": true, - "pack-design@skills-dir": false, - "pack-scroll3d@skills-dir": false, - "pack-seo@skills-dir": false, - "pack-web-validate@skills-dir": false, - "pack-gstack-dev@skills-dir": true, - "pack-gstack-browser@skills-dir": false, - "pack-gstack-design@skills-dir": false, - "pack-higgsfield@skills-dir": false, - "pack-extras@skills-dir": false, - "ui-ux-pro-max@ui-ux-pro-max-skill": false, - "security-guidance@claude-code-plugins": true, - "pr-review-toolkit@claude-code-plugins": false, - "example-skills@anthropic-agent-skills": false, - "brightdata-plugin@synced": false - }, - "deniedMcpServers": [ - { - "serverName": "claude.ai higgsfield" - }, - { - "serverName": "claude.ai Higgsfield AI" - } - ] -} diff --git a/lib/profiles/web-full.json b/lib/profiles/web-full.json deleted file mode 100644 index 0aec36a..0000000 --- a/lib/profiles/web-full.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "enabledPlugins": { - "model-router@skills-dir": true, - "pack-design@skills-dir": true, - "pack-scroll3d@skills-dir": true, - "pack-seo@skills-dir": true, - "pack-web-validate@skills-dir": true, - "pack-gstack-dev@skills-dir": true, - "pack-gstack-browser@skills-dir": true, - "pack-gstack-design@skills-dir": true, - "pack-higgsfield@skills-dir": false, - "pack-extras@skills-dir": false, - "ui-ux-pro-max@ui-ux-pro-max-skill": true, - "security-guidance@claude-code-plugins": true, - "pr-review-toolkit@claude-code-plugins": false, - "example-skills@anthropic-agent-skills": false, - "brightdata-plugin@synced": false - } -} diff --git a/lib/tests/cc.test.sh b/lib/tests/cc.test.sh index 301f688..e1d8d9f 100644 --- a/lib/tests/cc.test.sh +++ b/lib/tests/cc.test.sh @@ -56,6 +56,15 @@ check T3-rc "$rc" 0; check_has T3-argv "$log" "claude argv: "$'\n' check_has T3-profile-empty "$log" "CLAUDE_PROFILE="$'\n' check_has T3-tmpdir "$log" "TMPDIR=$FX/home/.cache/claude-tmp" +# name allowlist: a path-like name or profile starts no session +for args in "x ../y" "../x webx" "x web/x" "-x webx"; do + : > "$CC_LOG" + # shellcheck disable=SC2086 + out="$(cc $args 2>&1)"; rc=$? + check "T6-rc-$args" "$rc" 2; check_has "T6-msg-$args" "$out" "cc: invalid" + check "T6-no-session-$args" "$(cat "$CC_LOG")" "" +done + printf 'CC_ENV_CANARY=1\n' > "$FX/home/.claude/.env" : > "$CC_LOG"; cc s4 webx; check_has T4-env "$(cat "$CC_LOG")" "canary=1" check T5-shellcheck "$(shellcheck "$ROOT/lib/cc.sh" >/dev/null 2>&1 && echo ok)" ok diff --git a/lib/tests/profile.test.sh b/lib/tests/profile.test.sh index 0a45b50..83669f2 100755 --- a/lib/tests/profile.test.sh +++ b/lib/tests/profile.test.sh @@ -129,6 +129,55 @@ out="$(PROFILE_REPO_OVERRIDE="$FX" PROFILE_USER_SETTINGS="$FX/home/settings.json bash "$ROOT/lib/profile.sh" -C "$FX/proj/sub/deeper" current --plain | tr '\n' ' ')" check_has T19-walkup "$out" "profile=devx source=local " +# managed-connector match: a user-scope deny of an unmanaged connector keeps +# the profile name, shows in the plain lines, and never appears in diff +printf '{"enabledPlugins": {"ui@mk": true}, "deniedMcpServers": [{"serverName": "claude.ai Gmail"}]}\n' > "$FX/home/settings.json" +P set devx >/dev/null; out="$(plain)" +check_has T21-managed-name "$out" "profile=devx source=local " +check_has T21-plain-all "$out" "connector:claude.ai Gmail=denied" +check T21-diff "$(P diff devx)" "no difference from devx" +printf '{"enabledPlugins": {"ui@mk": true}}\n' > "$FX/home/settings.json" + +# diff skips the model line when the fragment has none +check_has T22-diff-nomodel "$(P diff devx; jq -r .model "$L")" "no difference from devx" + +# name allowlist: a refused name writes nothing and reads no path +cp "$L" "$FX/before-names.json" +for v in "set ../../settings" "show ../x" "diff a/b" "enable ../x" "disable y/z"; do + # shellcheck disable=SC2086 + out="$(P $v 2>&1)"; rc=$? + check "T23-name-rc-$v" "$rc" 2; check_has "T23-name-msg-$v" "$out" "invalid" +done +check T23-untouched "$(cmp -s "$FX/before-names.json" "$L" && echo same)" same +check T23-no-escape "$([ -e "$FX/lib/settings.json" ] && echo leaked)" "" + +# unset with nothing managed left: no write; no local file: none created +P unset >/dev/null; cp "$L" "$FX/before-unset.json" +out="$(P unset)"; check_has T24-nothing "$out" "nothing to unset" +check T24-untouched "$(cmp -s "$FX/before-unset.json" "$L" && echo same)" same +rm "$L"; P unset >/dev/null; check T24-no-file "$([ -e "$L" ] && echo created)" "" + +# project root = parent of the git common dir; a worktree and a nested dir +# holding its own CLAUDE.md both resolve to the main checkout +G="$FX/gmain"; mkdir -p "$G/pkg/inner"; printf '# pkg\n' > "$G/pkg/inner/CLAUDE.md" +git init -q "$G" +git -C "$G" -c core.hooksPath=/dev/null -c user.email=t@t -c user.name=t commit -q --allow-empty -m x +git -C "$G" -c core.hooksPath=/dev/null worktree add -q "$FX/wt" -b wt 2>/dev/null +PX() { local d="$1"; shift; PROFILE_REPO_OVERRIDE="$FX" PROFILE_USER_SETTINGS="$FX/home/settings.json" \ + PROFILE_CLAUDE_JSON="$FX/home/claude.json" bash "$ROOT/lib/profile.sh" -C "$d" "$@"; } +PX "$FX/wt" set devx >/dev/null; rc=$? +check T25-worktree-rc "$rc" 0 +check T25-worktree-main "$(jq -r '.enabledPlugins["pack-a@skills-dir"]' "$G/.claude/settings.local.json")" true +check T25-worktree-none "$([ -e "$FX/wt/.claude/settings.local.json" ] && echo local)" "" +check_has T25-nested "$(PX "$G/pkg/inner" current --plain | tr '\n' ' ')" "profile=devx source=local " + +# the walk stops below $HOME: a repo AT $HOME is not the project root +H2="$FX/home2"; mkdir -p "$H2/work"; git init -q "$H2"; printf '# w\n' > "$H2/work/CLAUDE.md" +HOME="$H2" PX "$H2/work" set devx >/dev/null; rc=$? +check T26-home-rc "$rc" 0 +check T26-home-local "$([ -f "$H2/work/.claude/settings.local.json" ] && echo yes)" yes +check T26-home-not-root "$([ -e "$H2/.claude" ] && echo leaked)" "" + # timing: two jq calls, under 100 ms here (spec target 50 ms, measured in Task 12) t0=$(date +%s%N); P current --plain >/dev/null; t1=$(date +%s%N) ms=$(( (t1 - t0) / 1000000 )); echo "current --plain: ${ms} ms"