Commit ec3720b3 authored by nimrod's avatar nimrod
Browse files

Claude setup.

Adding Claude used in SchooLinks:

- My personal CLAUDE.md with of my preferences.
- A sandbox using bubblewrap.
- Wrapper with nicer session handling.
- Shell completion for the session wrapper.
parent 9c6eeede
Loading
Loading
Loading
Loading
+30
Changes for .bash_completion.d/claude: 30 added lines, 0 removed lines.
Original line number Diff line number Diff line
# vim: ft=bash

. "${HOME}/.bashrc.d/claude"

_claude_complete() {
    local cur prev words cword opts
    _init_completion || return
    opts='--add-dir --agent --agents --allow-dangerously-skip-permissions --allowedTools --allowed-tools --append-system-prompt --autocompact --ax-screen-reader --bg --background --bare --betas --brief --chrome --cloud -c --continue --dangerously-skip-permissions -d --debug --debug-file --desktop --disable-slash-commands --disallowedTools --disallowed-tools --effort --environment --exclude-dynamic-system-prompt-sections --fallback-model --file --fork-session --forward-subagent-text --from-pr -h --help --ide --include-hook-events --include-partial-messages --input-format --input-format --json-schema --max-budget-usd --mcp-config --model -n --name --no-chrome --no-session-persistence --output-format --permission-mode --permission-prompts --plugin-dir --plugin-url -p --print --prompt-suggestions --remote-control --remote-control-session-name-prefix --restricted -r --resume --safe-mode --session-id --setting-sources --setting-sources --settings --strict-mcp-config --system-prompt --system-prompt-snapshot --teleport --tmux --tools -v --verson -w --worktree -l --list'
    commands='agents attach auth auth-mode doctor gateway import install logs mcp plugin purge respawn rm setup-token stop kill ultrareview update upgrade prune'
    if [[ $prev == rm ]] || [[ $prev == -r ]] || [[ $prev == --resume ]] || [[ $prev == -c ]] || [[ $prev == --continue ]]
    then
        COMPREPLY=($(compgen -W "$(_claude_list)" -- "$cur"))
    elif [[ $prev == --input-format ]]
    then
        COMPREPLY=($(compgen -W "text stream-json" -- "$cur"))
    elif [[ $prev == --output-format ]]
    then
        COMPREPLY=($(compgen -W "text json stream-json" -- "$cur"))
    elif [[ $prev == --permission-prompts ]]
    then
        COMPREPLY=($(compgen -W "host none" -- "$cur"))
    elif [[ $cur == -* ]]
    then
        COMPREPLY=($(compgen -W "$opts" -- "$cur"))
    else
        COMPREPLY=($(compgen -W "$(_claude_list) ${commands}" -- "$cur"))
    fi
}

complete -F _claude_complete claude
+97
Changes for .bashrc.d/claude: 97 added lines, 0 removed lines.
Original line number Diff line number Diff line
# shellcheck shell=bash
# vim: ft=sh

_claude_list() {
    if [ "$#" -eq 0 ]
    then
        find "$HOME/.claude/projects" -maxdepth 2 -name '*.jsonl' -print0 | xargs --null cat | jq --raw-output 'select(.customTitle != null) | .customTitle' | sort -u
        find "$HOME/.claude/projects" -maxdepth 2 -name '*.jsonl' -print0 | xargs --null cat | jq --raw-output 'select(.sessionId != null) | .sessionId' | sort -u
    else
        sessionId="$(find "$HOME/.claude/projects" -maxdepth 2 -name '*.jsonl' -print0 | xargs --null cat | jq --raw-output "select(.customTitle == \"$1\") | .sessionId" | sort -u)"
        if [ -z "$sessionId" ]
        then
            echo "Session '$1' not found." >&2
            return 1
        fi
        echo "$sessionId"
    fi
}

_claude_session_prune() {
    find "${HOME}/.claude/projects" -name '*.jsonl' -mtime +14 -delete || true
    find "${HOME}/.claude/projects" -mindepth 1 -type d -empty -delete || true
}

_claude() {
    _sl="$HOME/Repositories/SchooLinks"
    case "$PWD/" in "$_sl"/*) _cd="$PWD" ;; *) _cd="$_sl" ;; esac
    env -i \
        ANSIBLE_CACHE_PLUGIN="$ANSIBLE_CACHE_PLUGIN" \
        ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED="$ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED" \
        ANSIBLE_PIPELINING="$ANSIBLE_PIPELINING" \
        ANSIBLE_PYTHON_INTERPRETER="$ANSIBLE_PYTHON_INTERPRETER" \
        BROWSER="$BROWSER" \
        CFLAGS="$CFLAGS" \
        CLOUDSDK_ACTIVE_CONFIG_NAME="$CLOUDSDK_ACTIVE_CONFIG_NAME" \
        COLORTERM="$COLORTERM" \
        COREPACK_ENABLE_AUTO_PIN="$COREPACK_ENABLE_AUTO_PIN" \
        COREPACK_HOME="$COREPACK_HOME" \
        DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
        DIRENV_LOG_FORMAT= DOCKER_HOST="$DOCKER_HOST"  \
        DISABLE_AUTOUPDATER=1 \
        HELM_HOME="$HELM_HOME" \
        HOME="$HOME" \
        LANG="$LANG" \
        MAKEFLAGS="$MAKEFLAGS" \
        NPM_CONFIG_USERCONFIG="$NPM_CONFIG_USERCONFIG" \
        PATH="$PATH" \
        PGSSLROOTCERT="$PGSSLROOTCERT" \
        SENTRY_AUTH_TOKEN="$SENTRY_AUTH_TOKEN" \
        TERM="$TERM" \
        TF_CLI="$TF_CLI" \
        TF_PLUGIN_CACHE_DIR="$TF_PLUGIN_CACHE_DIR" \
        USER="$USER" \
        XDG_DATA_DIRS="$XDG_DATA_DIRS" \
        XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR" \
    direnv exec "$_sl" \
    bwrap \
        --ro-bind / / \
        --dev /dev \
        --proc /proc \
        --tmpfs /tmp \
        --tmpfs /run/host \
        --tmpfs /home \
        --tmpfs /etc/ssh/ssh_config.d \
        --ro-bind /etc/resolv.conf "$(readlink /etc/resolv.conf)" \
        --ro-bind "${HOME}/.local/bin" "${HOME}/.local/bin" \
        --ro-bind "${HOME}/.asdf" "${HOME}/.asdf" \
        --ro-bind "${HOME}/.tool-versions" "${HOME}/.tool-versions" \
        --ro-bind "${HOME}/.aws/config" "${HOME}/.aws/config" \
        --ro-bind "${HOME}/.config/npmrc" "${HOME}/.config/npmrc" \
        --ro-bind "${HOME}/.config/git" "${HOME}/.config/git" \
        --ro-bind "${HOME}/.local/share/gh/extensions" "${HOME}/.local/share/gh/extensions" \
        --bind "${HOME}/.aws/sso/cache" "${HOME}/.aws/sso/cache" \
        --bind "${HOME}/.cache/tofu/plugins" "${HOME}/.cache/tofu/plugins" \
        --bind "${HOME}/.cache/tofu" "${HOME}/.cache/tofu" \
        --bind "${HOME}/.claude" "${HOME}/.claude" \
        --bind "${HOME}/.claude.json" "${HOME}/.claude.json" \
        --bind "${HOME}/.config/git/config.claude" "${HOME}/.config/git/config" \
        --bind "$_sl" "$_sl" \
        --chdir "$_cd" \
        --unshare-all \
        --share-net \
        --die-with-parent \
        --new-session \
        claude \
        "$@"
}

claude() {
    case "${1:-}" in
        -l|--list) _claude_list;;
        prune) _claude_session_prune;;
        ""|agents|attach|auth|auth-mode|doctor|gateway|import|install|logs|mcp|plugin|purge|respawn|rm|setup-token|stop|kill|ultrareview|update|upgrade|-*)
            [ -z "${TMUX:-}" ] || tmux rename-window claude; _claude "$@"; tmux rename-window bash;;
        *) local name="$*"; [ -z "${TMUX:-}" ] || tmux rename-window "claude (${name})"; if [ -z "$(_claude_list "$name")" ]; then _claude --name "$name"; else _claude --resume "$name"; fi; tmux rename-window bash;;
    esac
}
+34
Changes for .claude/CLAUDE.md: 34 added lines, 0 removed lines.
Original line number Diff line number Diff line
# CODING AGENT INSTRUCTIONS

- You're running inside a bubblewrap sandbox inside a container. If you're
  having access issues or if something is missing, let me know what is missing.

- The preference is _always_ for short and simple code and short and simple
  changes.

- Make the documentation short. The longer the documentation, the less useful
  it is.

- Instead of documentation, make the code more readable. Use meaningful names,
  add small function and variables that explain the process.

- Add comments in the code to note restrictions, limitation, decisions and
  everything that explains why the code is structured in a specific way.

- If you're making additional changes to complete a task, let me know what, why
  and where.

- Avoid using bashisms, use POSIX shell (accepted exceptions are ones that are
  valid on BusyBox shell, Debian Almquist Shell _and_ OpenBSD's Korn shell).

- To notify me, use `notify-send -a "Claude Code" "<title>" "<body>"` and put
  the session name in the title. A hook already notifies me after prompts that
  take over 30s (`~/.claude/hooks/notify-long-prompt.sh`). Use `/tmp` for temp
  files, `/run/user/1000` is read-only in the sandbox.

- Don't push changes or interact with GitHub without asking me directly first.

- When accessing AWS either directly or through a tool (like OpenTofu or
  Ansible), use the schoolinks-prod-readonly and schoolinks-dev-readonly AWS
  profiles (depending on which environment you on). If you need to use a
  different profile, ask directly first.
+7
Changes for Makefile: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -99,3 +99,10 @@ secure-templates: .gnupg/trustdb.gpg
	ph show --field 'Notes' 'GPG/D3B913DE36AB5565DCAC91C6A322378C61339ECD' | gpg --import
	echo 'D3B913DE36AB5565DCAC91C6A322378C61339ECD:6:' | gpg --import-ownertrust
	chmod 600 '$@'

.PHONY: claude
claude: .local/bin/claude

.local/bin/claude:
	$(curl) https://downloads.claude.ai/claude-code-releases/$$($(curl) https://downloads.claude.ai/claude-code-releases/latest)/linux-x64/claude > '$@'
	chmod +x '$@'