#!/usr/bin/env bash # # Set up shared agent state for agent-container. # # Moves agent state (Claude Code, Pi, OpenCode) from the real $HOME into the # container home and symlinks it back, so the containerized tools (claude, pi, # opencode) and the local variants (claude-local, pi-local, opencode-local) # share logins, settings, and history. See README.md, section "Sharing agent # state between host and container via symlinks". # # Shows a plan and asks for confirmation before changing anything: # --dry-run (-n) show the plan without changing anything # --yes (-y) apply the plan without asking set -euo pipefail XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" CONTAINER_HOME="$XDG_DATA_HOME/agent-container/container-home" # State directories to share. Created in the container home (and linked into # $HOME) even if they don't exist yet, so both sides share from the start. DIRS=( .claude .pi .agents .config/opencode .local/share/opencode ) # Single files to share. Only linked when they exist on one side, tools create # them on first use. FILES=( .claude.json ) info() { printf ' %s\n' "$*"; } warn() { printf ' warning: %s\n' "$*" >&2; } usage() { cat <&2; exit 1 ;; esac shift done printf 'Sharing agent state via %s\n\n' "$CONTAINER_HOME" # Plan everything up front; decide() changes nothing, so applying the # stored actions afterwards is exactly what the plan describes. local -a items=() actions=() local item action for item in "${DIRS[@]}"; do items+=("$item"); actions+=("$(decide "$item" no)"); done for item in "${FILES[@]}"; do items+=("$item"); actions+=("$(decide "$item" yes)"); done printf 'Plan:\n' local i for i in "${!items[@]}"; do describe "${actions[$i]}" "${items[$i]}" done if [ "$dry_run" = "yes" ]; then printf '\nDry run: nothing changed.\n' return fi if [ "$ask" = "yes" ]; then if [ -t 0 ]; then local reply printf '\nApply these changes? [y/N] ' read -r reply case "$reply" in y|Y|yes|Yes) ;; *) printf 'Aborted, nothing changed.\n'; return ;; esac else printf '\nNot running interactively; pass --yes to apply the plan.\n' >&2 exit 1 fi fi printf '\n' for i in "${!items[@]}"; do apply "${items[$i]}" "${actions[$i]}" done if [ -f "$HOME/.gitconfig" ] && [ ! -e "$CONTAINER_HOME/.gitconfig" ]; then printf '\n' info "Tip: share your git config too (see README):" info " ln $HOME/.gitconfig $CONTAINER_HOME/.gitconfig" fi } main "$@"