Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ path = "lib.rs"
git-ai = { git = "https://github.com/git-ai-project/git-ai", branch = "main" }
rtk = { git = "https://github.com/rtk-ai/rtk", branch = "master" }
worktrunk = "0.33.0"
yek = "0.22.1"
32 changes: 32 additions & 0 deletions config/claude/hooks/auto-switch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
# Auto-switch Claude account on rate limit via claude-swap (cswap).
# Triggered by the StopFailure hook when Claude Code hits a rate limit.
#
# Reads hook input from stdin: { error, error_details, ... }
# Only switches if the error indicates a rate limit.

set -euo pipefail

# Require cswap
if ! command -v cswap &>/dev/null; then
exit 0
fi
Comment on lines +11 to +13

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The script relies on jq to parse the hook input. It is safer to check for its availability alongside cswap to avoid silent failures if jq is missing in the environment.

Suggested change
if ! command -v cswap &>/dev/null; then
exit 0
fi
if ! command -v cswap &>/dev/null || ! command -v jq &>/dev/null; then
exit 0
fi


# Require at least 2 managed accounts
ACCOUNT_COUNT=$(cswap --list 2>/dev/null | grep -c '^\s*[0-9]' || echo 0)

Copilot AI Apr 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grep -c '^\s*[0-9]' is not doing what it looks like: \s isn’t a portable whitespace escape in grep BRE/ERE, so it will typically be treated as a literal s. Also, grep -c prints 0 even when it exits 1 (no matches); combined with set -o pipefail, the || echo 0 branch can run and append a second 0, producing a non-numeric ACCOUNT_COUNT (e.g., 0\n0) and breaking the -lt comparison. Use a portable whitespace class (e.g. [[:space:]]) and avoid appending output on “no matches” (e.g. || true), ensuring the command substitution always evaluates to a single integer.

Suggested change
ACCOUNT_COUNT=$(cswap --list 2>/dev/null | grep -c '^\s*[0-9]' || echo 0)
ACCOUNT_COUNT=$(cswap --list 2>/dev/null | grep -c '^[[:space:]]*[0-9]' || true)

Copilot uses AI. Check for mistakes.
if [ "$ACCOUNT_COUNT" -lt 2 ]; then
exit 0
fi

# Read hook input from stdin
INPUT=$(cat)
ERROR=$(echo "$INPUT" | jq -r '.error // empty' 2>/dev/null) || true
ERROR_DETAILS=$(echo "$INPUT" | jq -r '.error_details // empty' 2>/dev/null) || true
Comment on lines +23 to +24

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using herestrings (<<< "$INPUT") is generally preferred over echo "$INPUT" | jq as it avoids a subshell and is safer if the input starts with a hyphen (which echo might interpret as a flag).

Suggested change
ERROR=$(echo "$INPUT" | jq -r '.error // empty' 2>/dev/null) || true
ERROR_DETAILS=$(echo "$INPUT" | jq -r '.error_details // empty' 2>/dev/null) || true
ERROR=$(jq -r '.error // empty' 2>/dev/null <<< "$INPUT") || true
ERROR_DETAILS=$(jq -r '.error_details // empty' 2>/dev/null <<< "$INPUT") || true


# Check if the error is rate-limit related
case "${ERROR}${ERROR_DETAILS}" in
*rate_limit*|*rate-limit*|*rate\ limit*|*overloaded*|*too_many_requests*|*429*|*quota*|*capacity*)
echo "[$(date)] Auto-switching Claude account due to rate limit" >&2
cswap --switch 2>&1 | head -5 >&2

Copilot AI Apr 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With set -euo pipefail, a failing cswap --switch (or a broken pipe from head) can cause this hook itself to exit non-zero, which is risky for a “best effort” remediation hook triggered during failures. Consider making the switch attempt explicitly non-fatal (e.g., append || true, or temporarily disable -e/pipefail around the pipeline) so the hook doesn’t introduce additional failure modes.

Suggested change
cswap --switch 2>&1 | head -5 >&2
cswap --switch 2>&1 | head -5 >&2 || true

Copilot uses AI. Check for mistakes.
;;
esac
11 changes: 11 additions & 0 deletions config/claude/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@
}
]
}
],
"StopFailure": [
{
"hooks": [
{
"type": "command",
"command": "$HOME/.claude/hooks/auto-switch.sh",
"timeout": 10
}
]
}
]
},
"statusLine": {
Expand Down
1 change: 0 additions & 1 deletion home-manager/modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
./tailscale
./uv-globals
./xremap
./yek
]
63 changes: 0 additions & 63 deletions home-manager/modules/yek/default.nix

This file was deleted.

4 changes: 0 additions & 4 deletions home-manager/modules/yek/install-yek-shim.sh

This file was deleted.

41 changes: 0 additions & 41 deletions home-manager/modules/yek/install-yek.sh

This file was deleted.

4 changes: 0 additions & 4 deletions home-manager/modules/yek/yek-shim.sh

This file was deleted.

15 changes: 0 additions & 15 deletions home-manager/modules/yek/yek.sh

This file was deleted.

12 changes: 12 additions & 0 deletions home-manager/programs/fish/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
grco = "_grco_function";
grcr = "_grcr_function";
hme = "_hm_load_env_file";
icg = "_install_cargo_globals_function";
ing = "_install_npm_globals_function";
iug = "_install_uv_globals_function";
kyber = "_kyber_function";
kyberd = "_kyberd_function";
kyberm = "_kyberm_function";
Expand All @@ -141,6 +144,8 @@
pixelh = "_pixelh_function";
pixeh = "_pixeh_function";
sag = "_ssh_add_github";
llmu = "_llm_update_function";
slb = "_sync_local_binaries_function";
shortcuts = "_fish_shortcuts";
tdo = "_tdo_function";
tmo = "_tmo_function";
Expand All @@ -151,6 +156,7 @@
tsw = "_tsw_function";
two = "_two_function";
tzo = "_tzo_function";
ulb = "_update_local_binaries_function";
zdo = "_zdo_function";
zmo = "_zmo_function";
zpo = "_zpo_function";
Expand Down Expand Up @@ -227,9 +233,13 @@
"_fzf_preview_name"
"_fzf_shell_history"
"_gco_function"
"_install_cargo_globals_function"
"_install_npm_globals_function"
"_install_uv_globals_function"
"_grco_function"
"_grcr_function"
"_hm_load_env_file"
"_llm_update_function"
"_kyber_function"
"_kyberd_function"
"_kyberm_function"
Expand All @@ -244,6 +254,8 @@
"_pixelh_function"
"_pixeh_function"
"_ssh_add_github"
"_sync_local_binaries_function"
"_update_local_binaries_function"
"_tdo_function"
"_tmo_function"
"_tpo_function"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function _install_cargo_globals_function --description "Install global Cargo packages"
set -l script "$HOME/dotfiles/home-manager/modules/cargo-globals/install-cargo-globals.sh"
if not test -f "$script"
echo "install-cargo-globals.sh not found at $script"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Error messages should be directed to stderr (>&2) to distinguish them from regular command output. This applies to the other new fish functions as well.

        echo "install-cargo-globals.sh not found at $script" >&2

return 1
end
bash "$script" $argv
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function _install_npm_globals_function --description "Install global npm packages"
set -l script "$HOME/dotfiles/home-manager/modules/npm-globals/install-npm-globals.sh"
if not test -f "$script"
echo "install-npm-globals.sh not found at $script"
return 1
end
bash "$script" $argv
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function _install_uv_globals_function --description "Install global uv packages"
set -l script "$HOME/dotfiles/home-manager/modules/uv-globals/install-uv-globals.sh"
if not test -f "$script"
echo "install-uv-globals.sh not found at $script"
return 1
end
bash "$script" $argv
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function _llm_update_function --description "Propagate LLM model configs via template substitution"
set -l script "$HOME/dotfiles/scripts/llm-update.sh"
if not test -f "$script"
echo "llm-update.sh not found at $script"
return 1
end
bash "$script" $argv
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function _sync_local_binaries_function --description "Sync local binaries from ~/.local-binaries.txt to ~/.local/bin"
set -l script "$HOME/dotfiles/home-manager/modules/local-binaries/sync-local-binaries.sh"
if not test -f "$script"
echo "sync-local-binaries.sh not found at $script"
return 1
end
bash "$script" $argv
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function _update_local_binaries_function --description "Update and build local binaries from .local-binaries.txt"
set -l script "$HOME/dotfiles/scripts/update-local-binaries.sh"
if not test -f "$script"
echo "update-local-binaries.sh not found at $script"
return 1
end
bash "$script" $argv
end
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tools = [
"aider-chat>=0.86.2",
"marker-pdf>=1.10.2",
"mistral-vibe>=2.7.0",
"claude-swap>=1.1.5",
"ruff>=0.15.8",
]

Expand Down
14 changes: 14 additions & 0 deletions spec/auto_switch_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# shellcheck disable=SC2329

Describe 'auto-switch.sh'
SCRIPT="$PWD/config/claude/hooks/auto-switch.sh"

It 'exits 0 when cswap is not available'
# Mock command so cswap is not found
command() { return 1; }
Comment on lines +8 to +9

Copilot AI Apr 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command() { return 1; } definition on line 9 is not used by the When run ... invocation (which runs in a separate bash -c and redefines command again). Removing the outer definition avoids confusion and keeps the spec focused on the behavior under test.

Suggested change
# Mock command so cswap is not found
command() { return 1; }

Copilot uses AI. Check for mistakes.
When run bash -c "command() { return 1; }; source '$SCRIPT' </dev/null"
The status should equal 0
End

End
25 changes: 5 additions & 20 deletions spec/coverage_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ It 'has spec file for config/claude/hooks/atuin-history.sh'
The path "spec/atuin_history_spec.sh" should be exist
End

It 'has spec file for config/claude/hooks/auto-switch.sh'
The path "spec/auto_switch_spec.sh" should be exist
End

It 'has spec file for config/claude/hooks/security.sh'
The path "spec/security_spec.sh" should be exist
End
Expand Down Expand Up @@ -141,22 +145,6 @@ It 'has spec file for home-manager/modules/uv-globals/install-uv-globals.sh'
The path "spec/uv_globals_spec.sh" should be exist
End

It 'has spec file for home-manager/modules/yek/install-yek.sh'
The path "spec/yek_spec.sh" should be exist
End

It 'has spec file for home-manager/modules/yek/yek.sh'
The path "spec/yek_spec.sh" should be exist
End

It 'has spec file for home-manager/modules/yek/install-yek-shim.sh'
The path "spec/install_yek_shim_spec.sh" should be exist
End

It 'has spec file for home-manager/modules/yek/yek-shim.sh'
The path "spec/yek_shim_spec.sh" should be exist
End

It 'has spec file for named-hosts/matic/falcon-init.sh'
The path "spec/falcon_init_spec.sh" should be exist
End
Expand Down Expand Up @@ -237,6 +225,7 @@ covered_scripts="config/ccs/hydrate.sh
config/claude/hooks/notify.sh
config/claude/hooks/pushover.sh
config/claude/hooks/rtk-rewrite.sh
config/claude/hooks/auto-switch.sh
config/claude/hooks/security.sh
config/claude/hooks/atuin-history.sh
config/claude/hooks/statusline.sh
Expand All @@ -257,10 +246,6 @@ home-manager/modules/local-scripts/clipboard-copy.sh
home-manager/modules/local-scripts/notify-local.sh
home-manager/modules/npm-globals/install-npm-globals.sh
home-manager/modules/uv-globals/install-uv-globals.sh
home-manager/modules/yek/install-yek-shim.sh
home-manager/modules/yek/install-yek.sh
home-manager/modules/yek/yek-shim.sh
home-manager/modules/yek/yek.sh
home-manager/programs/neovim/run_tests.sh
home-manager/programs/tmux/session-logger.sh
home-manager/services/brew-upgrader/upgrade.sh
Expand Down
10 changes: 10 additions & 0 deletions spec/fish/_install_cargo_globals_function_test.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set fn (status dirname)/../../home-manager/programs/fish/functions
source $fn/_install_cargo_globals_function.fish

set tmpdir (mktemp -d)

set -x HOME $tmpdir
_install_cargo_globals_function >/dev/null 2>&1
@test "returns 1 when script missing" $status = 1

rm -rf $tmpdir
10 changes: 10 additions & 0 deletions spec/fish/_install_npm_globals_function_test.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set fn (status dirname)/../../home-manager/programs/fish/functions
source $fn/_install_npm_globals_function.fish

set tmpdir (mktemp -d)

set -x HOME $tmpdir
_install_npm_globals_function >/dev/null 2>&1
@test "returns 1 when script missing" $status = 1

rm -rf $tmpdir
Loading
Loading