summaryrefslogtreecommitdiffstats
path: root/pkgs/claude-remote.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-22 19:59:34 +0000
committertv <tv@krebsco.de>2026-11-22 23:47:20 +0000
commitbdfca5d43c188799361484876713d4229a508c14 (patch)
tree6f6b0f57325b7036a237bdfd49651a1b75f47188 /pkgs/claude-remote.nix
parent369a196232ba1477e4bf0993f42f0105cc3aef08 (diff)
pkgs/claude-remote: reset and remove, for what claude.ai deletes
Deleting a remote's session on claude.ai leaves the remote a link to it (bridge-pointer.json), which it then can't resume (404). reset NAME forgets the session: moves the link aside (bridge-pointer.json.old), and the remote starts with a new one, stopped and started again if it runs. remove NAME retires a remote: stops it, and moves its directory and Claude Code project to ~claude/remotes/.removed/NAME-DATE, worktrees in it repaired; its branches and trust stay; not for static ones. Then the old session and environment can go, on claude.ai. Both ask first (y/N), but with -f, as root, not from the remote itself, all or nothing like rename, whose take-back they share (its messages generalized). .removed is not worth a backup: in the rsync filter.
Diffstat (limited to 'pkgs/claude-remote.nix')
-rw-r--r--pkgs/claude-remote.nix151
1 files changed, 141 insertions, 10 deletions
diff --git a/pkgs/claude-remote.nix b/pkgs/claude-remote.nix
index d8975e6..20081cf 100644
--- a/pkgs/claude-remote.nix
+++ b/pkgs/claude-remote.nix
@@ -66,6 +66,19 @@ let
fails, it takes back those done. rename NEW OLD takes it back.
As root, and not from OLD itself; not for static ones (take
them out of the system's configuration first).
+ reset NAME [-f|--force]
+ Forget its claude.ai session (its link to it moved aside), so
+ that it starts with a new one: stopped, if it runs, and started
+ again. Then its old session and environment can go, on
+ claude.ai (deleting them there first leaves it a link to a
+ session that's gone, which it can't resume).
+ remove NAME [-f|--force]
+ Retire it: stop it, and move its directory and Claude Code
+ project to ~claude/remotes/.removed/NAME-DATE (worktrees in it
+ repaired); its branches and its trust stay. Then its session
+ and environment can go, on claude.ai. Not for static ones.
+ reset and remove ask first (y/N), unless -f; as root, and not
+ from the remote itself; all or nothing, like rename.
help, -h, --help
This.
@@ -105,12 +118,12 @@ let
# their colors, if any (a colored word follows an m, so each loop ends)
paint() {
if test -z "$C_OFF"; then cat; return; fi
- local x='[^-A-Za-z0-9_]' c='list|inspect|start|stop|restart|attach|log|rename|help'
+ local x='[^-A-Za-z0-9_]' c='list|inspect|start|stop|restart|attach|log|rename|reset|remove|help'
sed -E \
-e "s/^([A-Z][A-Za-z ]*:)$/$C_HEAD\1$C_OFF/" \
-e "s/^( )($c)($x|$)/\1$C_CMD\2$C_OFF\3/" \
-e ":a; s/(claude-remote |, |^usage: claude-remote \[)($c|COMMAND)($x|$)/\1$C_CMD\2$C_OFF\3/; ta" \
- -e ":b; s/(^|$x)(--stop|-h|--help|JOURNALCTL-OPTION)($x|$)/\1$C_OPT\2$C_OFF\3/; tb" \
+ -e ":b; s/(^|$x)(--stop|-f|--force|-h|--help|JOURNALCTL-OPTION)($x|$)/\1$C_OPT\2$C_OFF\3/; tb" \
-e ":c; s/(^|$x)(NAME|OLD|NEW)($x|$)/\1$C_NAME\2$C_OFF\3/; tc"
}
die() {
@@ -262,25 +275,128 @@ let
undoable() {
undos+=("$(printf '%q ' "$@")")
}
- # The steps done, taken back, last first, if the rename didn't finish.
+ # The steps done, taken back, last first, if the command (doing)
+ # didn't finish.
take_back() {
test -z "$finished" || return 0
set +e
local i failed=
- echo "claude-remote: rename $old $new failed; taking back what's done" >&2
+ echo "claude-remote: $doing failed; taking back what's done" >&2
for ((i = ''${#undos[@]} - 1; i >= 0; i--)); do
eval "''${undos[i]}" || { failed=1; echo "claude-remote: couldn't take back: ''${undos[i]}" >&2; }
done
if test -n "$failed"; then
- echo "claude-remote: not all taken back (see above); ~claude/.claude.json as it was: $backup (claude-remote list shows it, until it's removed)" >&2
+ echo "claude-remote: not all taken back (see above)''${backup:+; ~claude/.claude.json as it was: $backup (claude-remote list shows it, until removed)}" >&2
else
- rm -f "$backup"
- echo "claude-remote: all taken back: $old is as it was" >&2
+ if test -n "$backup"; then rm -f "$backup"; fi
+ echo "claude-remote: all taken back: as it was" >&2
fi
}
+ # QUESTION, answered y, on a terminal, unless with -f (force)
+ confirm() {
+ test -z "$force" || return 0
+ test -t 0 || die "$1 (asks first, on a terminal, or -f)"
+ local a
+ read -r -p "$1 [y/N] " a
+ case $a in y | Y | yes) ;; *) die "not done" ;; esac
+ }
+ # Remote NAME's claude.ai session forgotten (its link to it in its
+ # project moved aside, to bridge-pointer.json.old), so that it starts
+ # with a new one: stopped, if it runs, and started again.
+ do_reset() {
+ local name=$1 p ids active=
+ test "$(id -u)" = 0 || die "reset: as root"
+ valid "$name" || die "not a remote's name: $name"
+ test -d "$remotes/$name" || die "there's no remote $name ($remotes/$name)"
+ if grep -qs "/$(unit "$name")" /proc/self/cgroup; then
+ die "reset: this runs in $name: reset it from elsewhere (a root shell, another remote)"
+ fi
+ p=$(project "$name")/bridge-pointer.json
+ test -f "$p" || die "$name has no claude.ai session to forget ($p)"
+ ids=$(jq -r '"session \(.sessionId), environment \(.environmentId)"' "$p")
+ case $(active "$name") in inactive | failed | "") ;; *) active=1 ;; esac
+ if test -n "$active"; then
+ confirm "reset $name: stop it (which ends its sessions), forget its claude.ai $ids, and start it again, with a new one?"
+ else
+ confirm "reset $name: forget its claude.ai $ids, for a new one at its next start?"
+ fi
+ # (global, for take_back, which runs on exit)
+ doing="reset $name" undos=() finished= backup=
+ trap take_back EXIT
+ if test -n "$active"; then
+ undoable systemctl start "$(unit "$name")"
+ systemctl stop "$(unit "$name")"
+ fi
+ undoable move_back "$p.old" "$p" as_claude
+ as_claude mv -f "$p" "$p.old"
+ if test -n "$active"; then
+ undoable systemctl stop "$(unit "$name")"
+ systemctl start "$(unit "$name")"
+ fi
+ finished=1
+ echo "reset $name$(test -z "$active" || echo ", and started it again"): its old claude.ai $ids can go, on claude.ai."
+ }
+ # Remote NAME, retired: stopped, and its directory and Claude Code
+ # project moved to ~/remotes/.removed/NAME-DATE (remote, project),
+ # with the worktrees in it repaired; its branches and its trust stay.
+ do_remove() {
+ local name=$1 dest rel users active= wts p n ids=
+ test "$(id -u)" = 0 || die "remove: as root"
+ valid "$name" || die "not a remote's name: $name"
+ test -d "$remotes/$name" || die "there's no remote $name ($remotes/$name)"
+ if is_static "$name"; then
+ die "$name is static: take it out of the system's configuration first, and deploy"
+ fi
+ if grep -qs "/$(unit "$name")" /proc/self/cgroup; then
+ die "remove: this runs in $name: remove it from elsewhere (a root shell, another remote)"
+ fi
+ users=$(users_of "$name" "$remotes/$name" "$(project "$name")")
+ test -z "$users" || die "remove: $name is in use, by more than itself:$(printf '\n %s' "$users")"
+ rel=.removed/$name-$(date -u +%Y%m%dT%H%M%SZ)
+ dest=$remotes/$rel
+ test ! -e "$dest" || die "remove: $dest is there already"
+ case $(active "$name") in inactive | failed | "") ;; *) active=1 ;; esac
+ p=$(project "$name")/bridge-pointer.json
+ if test -f "$p"; then
+ ids=$(jq -r '"session \(.sessionId), environment \(.environmentId)"' "$p")
+ fi
+ confirm "remove $name: $(test -z "$active" || echo "stop it (which ends its sessions), and ")move its directory and Claude Code project to $dest?"
+ # (global, for take_back, which runs on exit)
+ doing="remove $name" undos=() finished= backup=
+ trap take_back EXIT
+ if test -n "$active"; then
+ undoable systemctl start "$(unit "$name")"
+ systemctl stop "$(unit "$name")"
+ users=$(users_of "" "$remotes/$name" "$(project "$name")")
+ test -z "$users" || die "remove: $name still in use, once stopped:$(printf '\n %s' "$users")"
+ fi
+ # its mark, if a stop didn't end well (else, at the next boot, it
+ # would start again, anew)
+ if test -e "$state/$name"; then
+ undoable touch "$state/$name"
+ rm -f "$state/$name"
+ fi
+ undoable as_claude rmdir --ignore-fail-on-non-empty "$dest" "$remotes/.removed"
+ as_claude mkdir -p "$dest"
+ wts=$(worktrees_below "$remotes/$name")
+ undoable move_dir_back "$rel/remote" "$name"
+ as_claude mv "$remotes/$name" "$dest/remote"
+ repair_worktrees "$name" "$rel/remote" <<< "$wts"
+ if test -e "$(project "$name")"; then
+ undoable move_back "$dest/project" "$(project "$name")" as_claude
+ as_claude mv "$(project "$name")" "$dest/project"
+ fi
+ finished=1
+ n=$(repos | while read -r r; do
+ as_claude git -C "$r" for-each-ref --format=x "refs/heads/$name/"
+ done | wc -l)
+ echo "removed $name: in $dest, with $(grep -c . <<< "$wts") worktrees; its $n branches $name/* stay, and so does its trust."
+ if test -n "$ids"; then echo "Its claude.ai $ids can go, on claude.ai."; fi
+ echo "To bring it back: mv $dest/remote $remotes/$name, mv $dest/project $(project "$name"), git worktree repair in each of its worktrees, claude-remote reset -f $name (its old session, deleted, can't be resumed), and claude-remote start $name."
+ }
do_rename() {
# (global, for take_back, which runs on exit)
- old=$1 new=$2 undos=() finished= backup=
+ old=$1 new=$2 undos=() finished= backup= doing="rename $1 $2"
local stop=$3 errs=() r n w b sha users active= wts brs
test "$(id -u)" = 0 || die "rename: as root"
valid "$old" || die "not a remote's name: $old"
@@ -413,6 +529,19 @@ let
test ''${#args[@]} = 2 || die "rename OLD NEW [--stop] (claude-remote -h)"
do_rename "''${args[0]}" "''${args[1]}" "$stop"
;;
+ reset | remove)
+ force=
+ args=()
+ for a; do
+ case $a in
+ -f | --force) force=1 ;;
+ -*) die "$cmd: $a: no such option (claude-remote -h)" ;;
+ *) args+=("$a")
+ esac
+ done
+ test ''${#args[@]} = 1 || die "$cmd NAME [-f|--force] (claude-remote -h)"
+ "do_$cmd" "''${args[0]}"
+ ;;
inspect)
name_arg "''${1-}"
n=$1
@@ -473,12 +602,14 @@ let
_claude_remote() {
local cur=''${COMP_WORDS[COMP_CWORD]}
if [ "$COMP_CWORD" -eq 1 ]; then
- COMPREPLY=($(compgen -W "list inspect start stop restart attach log rename help" -- "$cur"))
+ COMPREPLY=($(compgen -W "list inspect start stop restart attach log rename reset remove help" -- "$cur"))
elif [ "$COMP_CWORD" -eq 2 ]; then
case ''${COMP_WORDS[1]} in
- inspect | start | stop | restart | attach | log | rename)
+ inspect | start | stop | restart | attach | log | rename | reset | remove)
COMPREPLY=($(compgen -W "$(claude-remote names 2>/dev/null)" -- "$cur")) ;;
esac
+ elif [ "''${COMP_WORDS[1]}" = reset ] || [ "''${COMP_WORDS[1]}" = remove ]; then
+ COMPREPLY=($(compgen -W "-f --force" -- "$cur"))
elif [ "''${COMP_WORDS[1]}" = rename ]; then
COMPREPLY=($(compgen -W "--stop" -- "$cur"))
fi