diff --git a/lib/cc.sh b/lib/cc.sh new file mode 100644 index 0000000..d3ba15d --- /dev/null +++ b/lib/cc.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# ============================================================ +# lib/cc.sh — `cc [profile]`: a named dtach session running claude, +# optionally under a per-conversation profile. +# +# Sourced from ~/.bashrc (user step, MIGRATION.md), guarded so a checkout +# without the file does not break the shell: +# [ -f "$HOME/.claude/lib/cc.sh" ] && . "$HOME/.claude/lib/cc.sh" +# The old `dtach_claude()` and `alias cc='dtach_claude'` must go (an alias +# wins over this function and drops the profile argument; doctor warns). +# +# With a profile, claude starts with `--settings lib/profiles/.json` +# and the session exports CLAUDE_PROFILE + CLAUDE_PROFILE_SETTINGS, which +# `profile.sh current` reads (source=flag). That conversation is frozen: +# `/reload-plugins` re-reads neither the flag file nor a local value the +# flag overrides; to change packs, start another `cc `. +# Without a profile: manifest defaults + local settings, as before. +# TMPDIR goes to ~/.cache/claude-tmp either way (BLK-021). +# ============================================================ + +cc() { + local name="${1:-claude-$(date +%H%M%S)}" profile="${2:-}" + local lib="$HOME/.claude/lib" settings="" + if [ -n "$profile" ]; then + settings="$lib/profiles/$profile.json" + if [ ! -f "$settings" ]; then + echo "cc: unknown profile '$profile'. Known:" >&2 + bash "$lib/profile.sh" list >&2 + return 2 + fi + fi + mkdir -p "$HOME/.cache/claude-tmp" "$HOME/.dtach" + # shellcheck disable=SC2016,SC1003 # the inner shell expands $HOME; '^\' is dtach's detach key + CLAUDE_PROFILE="$profile" CLAUDE_PROFILE_SETTINGS="$settings" \ + TMPDIR="$HOME/.cache/claude-tmp" \ + systemd-run --user --quiet --collect --scope --slice=dev.slice \ + --unit="cc-${name}" \ + dtach -c "$HOME/.dtach/${name}" -e '^\' \ + bash -c 'set -a; [ -f "$HOME/.claude/.env" ] && . "$HOME/.claude/.env" + set +a + if [ -n "$CLAUDE_PROFILE_SETTINGS" ]; then + exec claude --settings "$CLAUDE_PROFILE_SETTINGS" + fi + exec claude' +} diff --git a/lib/tests/cc.test.sh b/lib/tests/cc.test.sh new file mode 100644 index 0000000..301f688 --- /dev/null +++ b/lib/tests/cc.test.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +# lib/tests/cc.test.sh — lib/cc.sh `cc [profile]`: unknown profile → +# list + rc 2, no session; a profile → claude --settings with +# CLAUDE_PROFILE / CLAUDE_PROFILE_SETTINGS / TMPDIR exported through +# systemd-run + dtach; no profile → plain claude, TMPDIR still set; .env +# sourced. Hermetic: PATH shims, HOME in a fixture holding .claude/lib. +set -u +unset CLAUDE_PROFILE CLAUDE_PROFILE_SETTINGS +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +pass=0; fail=0 +check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1)); + printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; } +check_has() { case "$2" in *"$3"*) pass=$((pass+1));; *) fail=$((fail+1)); + printf 'FAIL %s: [%s] does not contain [%s]\n' "$1" "$2" "$3";; esac; } +FX="$(mktemp -d)"; trap 'rm -rf "$FX"' EXIT +mkdir -p "$FX/bin" "$FX/home/.claude/lib/profiles" +cp "$ROOT/lib/cc.sh" "$ROOT/lib/profile.sh" "$FX/home/.claude/lib/" +printf '{"enabledPlugins": {"x@skills-dir": true}}\n' > "$FX/home/.claude/lib/profiles/webx.json" +printf -- '- **webx**: web fixture\n' > "$FX/home/.claude/lib/profiles/README.md" +cat > "$FX/bin/systemd-run" <<'EOT' +#!/usr/bin/env bash +while [ $# -gt 0 ]; do case "$1" in --*) shift ;; *) break ;; esac; done +echo "systemd-run" >> "$CC_LOG"; exec "$@" +EOT +cat > "$FX/bin/dtach" <<'EOT' +#!/usr/bin/env bash +while [ $# -gt 0 ]; do case "$1" in -c|-e) shift 2 ;; *) break ;; esac; done +echo "dtach" >> "$CC_LOG"; exec "$@" +EOT +cat > "$FX/bin/claude" <<'EOT' +#!/usr/bin/env bash +{ echo "claude argv: $*"; echo "CLAUDE_PROFILE=${CLAUDE_PROFILE-unset}" + echo "CLAUDE_PROFILE_SETTINGS=${CLAUDE_PROFILE_SETTINGS-unset}" + echo "TMPDIR=${TMPDIR-unset}"; echo "canary=${CC_ENV_CANARY-unset}"; } >> "$CC_LOG" +EOT +chmod +x "$FX/bin/"* +export CC_LOG="$FX/log" HOME="$FX/home" +PATH="$FX/bin:$PATH" +# shellcheck disable=SC1091 +. "$FX/home/.claude/lib/cc.sh" + +: > "$CC_LOG"; out="$(cc s1 nope 2>&1)"; rc=$? +check T1-unknown-rc "$rc" 2; check_has T1-unknown-msg "$out" "cc: unknown profile 'nope'" +check_has T1-unknown-list "$out" "webx"; check T1-no-session "$(cat "$CC_LOG")" "" + +: > "$CC_LOG"; cc s2 webx; rc=$?; log="$(cat "$CC_LOG")" +check T2-rc "$rc" 0; check_has T2-chain "$log" "systemd-run"; check_has T2-dtach "$log" "dtach" +check_has T2-argv "$log" "claude argv: --settings $FX/home/.claude/lib/profiles/webx.json" +check_has T2-profile "$log" "CLAUDE_PROFILE=webx" +check_has T2-settings "$log" "CLAUDE_PROFILE_SETTINGS=$FX/home/.claude/lib/profiles/webx.json" +check_has T2-tmpdir "$log" "TMPDIR=$FX/home/.cache/claude-tmp" +check T2-tmpdir-exists "$([ -d "$FX/home/.cache/claude-tmp" ] && echo yes)" yes + +: > "$CC_LOG"; cc s3; rc=$?; log="$(cat "$CC_LOG")" +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" + +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 +printf 'PASS=%s FAIL=%s\n' "$pass" "$fail"; [ "$fail" -eq 0 ]