fix: vision(#623): Claude identity isolation for disinto-chat (#707)
All checks were successful
ci/woodpecker/push/ci Pipeline was successful
ci/woodpecker/pr/ci Pipeline was successful
ci/woodpecker/pr/smoke-init Pipeline was successful

This commit is contained in:
Claude 2026-04-12 01:50:23 +00:00
parent cf4e9983c2
commit a15f0763b7
4 changed files with 171 additions and 34 deletions

View file

@ -1,8 +1,10 @@
#!/usr/bin/env bash
# lib/claude-config.sh — Shared Claude config directory helpers (#641)
# lib/claude-config.sh — Shared Claude config directory helpers (#641, #707)
#
# Provides setup_claude_config_dir() for creating/migrating CLAUDE_CONFIG_DIR
# and _env_set_idempotent() for writing env vars to .env files.
# Provides:
# setup_claude_dir <dir> [<auto_yes>] — Create/migrate a Claude config directory
# setup_claude_config_dir [auto_yes] — Wrapper for default CLAUDE_CONFIG_DIR
# _env_set_idempotent() — Write env vars to .env files
#
# Requires: CLAUDE_CONFIG_DIR, CLAUDE_SHARED_DIR (set by lib/env.sh)
@ -21,24 +23,33 @@ _env_set_idempotent() {
fi
}
# Create the shared CLAUDE_CONFIG_DIR, optionally migrating ~/.claude.
# Usage: setup_claude_config_dir [auto_yes]
setup_claude_config_dir() {
local auto_yes="${1:-false}"
# Create a Claude config directory, optionally migrating ~/.claude.
# This is the parameterized helper that handles any CLAUDE_CONFIG_DIR path.
# Usage: setup_claude_dir <config_dir> [auto_yes]
# setup_claude_dir /path/to/config [true]
#
# Parameters:
# $1 - Path to the Claude config directory to create
# $2 - Auto-confirm mode (true/false), defaults to false
#
# Returns: 0 on success, 1 on failure
setup_claude_dir() {
local config_dir="$1"
local auto_yes="${2:-false}"
local home_claude="${HOME}/.claude"
# Create the shared config directory (idempotent)
install -d -m 0700 -o "$USER" "$CLAUDE_CONFIG_DIR"
echo "Claude: ${CLAUDE_CONFIG_DIR} (ready)"
# Create the config directory (idempotent)
install -d -m 0700 -o "$USER" "$config_dir"
echo "Claude: ${config_dir} (ready)"
# If ~/.claude is already a symlink to CLAUDE_CONFIG_DIR, nothing to do
# If ~/.claude is already a symlink to config_dir, nothing to do
if [ -L "$home_claude" ]; then
local link_target
link_target=$(readlink -f "$home_claude")
local config_real
config_real=$(readlink -f "$CLAUDE_CONFIG_DIR")
config_real=$(readlink -f "$config_dir")
if [ "$link_target" = "$config_real" ]; then
echo "Claude: ${home_claude} -> ${CLAUDE_CONFIG_DIR} (symlink OK)"
echo "Claude: ${home_claude} -> ${config_dir} (symlink OK)"
return 0
fi
fi
@ -54,25 +65,25 @@ setup_claude_config_dir() {
fi
fi
# Check CLAUDE_CONFIG_DIR contents
if [ -n "$(ls -A "$CLAUDE_CONFIG_DIR" 2>/dev/null)" ]; then
# Check config_dir contents
if [ -n "$(ls -A "$config_dir" 2>/dev/null)" ]; then
config_nonempty=true
fi
# Case: both non-empty — abort, operator must reconcile
if [ "$home_nonempty" = true ] && [ "$config_nonempty" = true ]; then
echo "ERROR: both ${home_claude} and ${CLAUDE_CONFIG_DIR} exist and are non-empty" >&2
echo "ERROR: both ${home_claude} and ${config_dir} exist and are non-empty" >&2
echo " Reconcile manually: merge or remove one, then re-run disinto init" >&2
return 1
fi
# Case: ~/.claude exists and CLAUDE_CONFIG_DIR is empty — offer migration
# Case: ~/.claude exists and config_dir is empty — offer migration
if [ "$home_nonempty" = true ] && [ "$config_nonempty" = false ]; then
local do_migrate=false
if [ "$auto_yes" = true ]; then
do_migrate=true
elif [ -t 0 ]; then
read -rp "Migrate ${home_claude} to ${CLAUDE_CONFIG_DIR}? [Y/n] " confirm
read -rp "Migrate ${home_claude} to ${config_dir}? [Y/n] " confirm
if [[ ! "$confirm" =~ ^[Nn] ]]; then
do_migrate=true
fi
@ -83,11 +94,11 @@ setup_claude_config_dir() {
fi
if [ "$do_migrate" = true ]; then
# Move contents (not the dir itself) to preserve CLAUDE_CONFIG_DIR ownership
cp -a "$home_claude/." "$CLAUDE_CONFIG_DIR/"
# Move contents (not the dir itself) to preserve config_dir ownership
cp -a "$home_claude/." "$config_dir/"
rm -rf "$home_claude"
ln -sfn "$CLAUDE_CONFIG_DIR" "$home_claude"
echo "Claude: migrated ${home_claude} -> ${CLAUDE_CONFIG_DIR}"
ln -sfn "$config_dir" "$home_claude"
echo "Claude: migrated ${home_claude} -> ${config_dir}"
return 0
fi
fi
@ -97,7 +108,15 @@ setup_claude_config_dir() {
rmdir "$home_claude" 2>/dev/null || true
fi
if [ ! -e "$home_claude" ]; then
ln -sfn "$CLAUDE_CONFIG_DIR" "$home_claude"
echo "Claude: ${home_claude} -> ${CLAUDE_CONFIG_DIR} (symlink created)"
ln -sfn "$config_dir" "$home_claude"
echo "Claude: ${home_claude} -> ${config_dir} (symlink created)"
fi
}
# Create the shared CLAUDE_CONFIG_DIR, optionally migrating ~/.claude.
# Wrapper around setup_claude_dir for the default config directory.
# Usage: setup_claude_config_dir [auto_yes]
setup_claude_config_dir() {
local auto_yes="${1:-false}"
setup_claude_dir "$CLAUDE_CONFIG_DIR" "$auto_yes"
}