summaryrefslogtreecommitdiffstats
path: root/pkgs
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/claude-remote.nix301
1 files changed, 168 insertions, 133 deletions
diff --git a/pkgs/claude-remote.nix b/pkgs/claude-remote.nix
index 05649ab..327a871 100644
--- a/pkgs/claude-remote.nix
+++ b/pkgs/claude-remote.nix
@@ -1,23 +1,25 @@
# claude-remote, root's tool for the remotes (claude-remote-control@NAME,
-# see configs/claude.nix): list, inspect, start, stop, restart, attach,
-# log, rename; with bash completion.
+# claude-interactive@NAME, see configs/claude.nix): --list, --inspect,
+# --start, --stop, --restart, --attach, --log, --rename, --reset, --remove,
+# with NAME and options in any order, like claude-inbox; with bash
+# completion.
#
# Renaming a remote, OLD to NEW:
-# 1. claude-remote rename OLD NEW, as root, from anywhere but OLD itself.
+# 1. claude-remote --rename OLD NEW, as root, from anywhere but OLD itself.
# It checks first (OLD there and not static, nothing of NEW's there,
# nothing but OLD's own unit using OLD), stops OLD if it runs (asking,
-# or with --stop), moves its directory, its Claude Code project (with
+# or with -f), moves its directory, its Claude Code project (with
# the link to its claude.ai session), its trust, its worktrees and its
# branches (OLD/* to NEW/*), and starts NEW if OLD ran. A static
# remote, take out of the configuration first, and deploy.
-# 2. Check NEW: claude-remote inspect NEW, and claude.ai, where its
+# 2. Check NEW: claude-remote --inspect NEW, and claude.ai, where its
# sessions go on (named OLD-..., until new ones). Update what
# mentions OLD (it says where): ~claude/CLAUDE.md, NEW's own CLAUDE.md,
# and a static one, the configuration.
-# 3. If something's wrong: claude-remote rename NEW OLD.
+# 3. If something's wrong: claude-remote --rename NEW OLD.
# A rename is all or nothing: if a step fails, it takes back the ones
# done, and ends where it began. Only if that fails, too, it keeps a
-# copy of ~claude/.claude.json, as it was, which claude-remote list
+# copy of ~claude/.claude.json, as it was, which claude-remote --list
# shows, until it's removed.
{ lib, runCommand, writeShellScript, writeText
, coreutils, findutils, gitMinimal, gnugrep, gnused, jq, systemd, tmux, util-linux
@@ -44,62 +46,64 @@ let
usage() {
cat <<'EOF'
- usage: claude-remote [COMMAND [NAME...]]
+ usage: claude-remote [NAME] [COMMAND] [OPTION...] [-- JOURNALCTL-OPTION...]
+ (in any order, but for what comes after --)
The remotes, working in ~claude/remotes/NAME, each a Claude Code Remote
Control server (claude-remote-control@NAME), or an interactive session
with Remote Control (claude-interactive@NAME), as their TYPE says.
Commands:
- list (the default)
+ --list (the default without NAME)
The remotes: name, type, kind, state, trusted; and copies of
~claude/.claude.json that a failed rename kept.
- inspect NAME
+ --inspect NAME (the default with NAME)
A remote, in more detail: its unit, directory, trust, claude.ai
session, and its terminal's last lines.
- start NAME [--type=server|interactive]
- restart NAME [--type=server|interactive]
+ --start NAME [--type=server|interactive]
+ --restart NAME [--type=server|interactive]
Start a remote (or restart it); a dynamic one lasts, across
- reboots, until stop. --type: a Remote Control server (server),
- or an interactive session with Remote Control (interactive;
- --interactive for short), with more memory and CPU, and its
- session archived on claude.ai while it's stopped. A new one is
- made so (default: server); an existing one of the other type
- changes (stopped, if it runs): to interactive, it resumes its
- latest session; to server, the session it had as one. Without
- --type, a remote starts as the type it is. Not for a static
- one, whose type is the system's configuration's.
- stop NAME
- attach [NAME]
+ reboots, until --stop. --type: a Remote Control server
+ (server), or an interactive session with Remote Control
+ (interactive; --interactive for short), with more memory and
+ CPU, and its session archived on claude.ai while it's stopped.
+ A new one is made so (default: server); an existing one of the
+ other type changes (stopped, if it runs): to interactive, it
+ resumes its latest session; to server, the session it had as
+ one. Without --type, a remote starts as the type it is. Not
+ for a static one, whose type is the system's configuration's.
+ --stop NAME
+ --attach [NAME]
Its terminal (tmux; default: main; C-b d to leave).
- log NAME [JOURNALCTL-OPTION...]
+ --log NAME [-- JOURNALCTL-OPTION...]
Its unit's journal.
- rename OLD NEW [--stop]
+ --rename OLD NEW [-f|--force]
+ (or --from=OLD, --to=NEW, for either, in any order)
Check first, and change nothing unless all's well; stop OLD if
- it runs (asking first, or with --stop); move its directory,
- Claude Code project, trust, worktrees and branches (OLD/* to
- NEW/*); start NEW if OLD ran. All or nothing: if a step
- 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). On claude.ai,
- its session keeps its name in the side panel (that of the
- environment it registered, which it goes on with); claude-remote
- reset NEW gets the new one there, with a new session.
- reset NAME [-f|--force]
+ it runs (asking first, unless -f); move its directory, Claude
+ Code project, trust, worktrees and branches (OLD/* to NEW/*);
+ start NEW if OLD ran. All or nothing: if a step 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). On claude.ai, its session keeps
+ its name in the side panel (that of the environment it
+ registered, which it goes on with); --reset NEW gets the new one
+ there, with a new session.
+ --reset NAME [-f|--force]
Forget its claude.ai session (its link to it moved aside, or an
interactive one's session id replaced), 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]
+ --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
+ --reset and --remove ask first (y/N), unless -f; as root, and
+ not from the remote itself; all or nothing, like --rename.
+ -h, --help
This.
Kinds:
@@ -138,12 +142,11 @@ 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|reset|remove|help'
+ local x='[^-A-Za-z0-9_]' c='list|inspect|start|stop|restart|attach|log|rename|reset|remove'
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|--interactive|--type|-f|--force|-h|--help|JOURNALCTL-OPTION)($x|$)/\1$C_OPT\2$C_OFF\3/; tb" \
+ -e ":a; s/(^|$x)(--($c)|COMMAND)($x|$)/\1$C_CMD\2$C_OFF\4/; ta" \
+ -e ":b; s/(^|$x)(--interactive|--type|-f|--force|--from|--to|-h|--help|OPTION|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() {
@@ -205,7 +208,7 @@ let
}
hint_attach() {
if ! trusted "$1"; then
- echo "$1 hasn't trusted ~claude/remotes/$1 yet: claude-remote attach $1, and answer Remote Control's questions there (C-b d to leave)."
+ echo "$1 hasn't trusted ~claude/remotes/$1 yet: claude-remote --attach $1, and answer Remote Control's questions there (C-b d to leave)."
fi
}
@@ -319,7 +322,7 @@ let
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)''${backup:+; ~claude/.claude.json as it was: $backup (claude-remote list shows it, until 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
if test -n "$backup"; then rm -f "$backup"; fi
echo "claude-remote: all taken back: as it was" >&2
@@ -493,7 +496,7 @@ let
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")$(test ! -e "$dest/interactive-session" || echo ", mv $dest/interactive-session $sessions/$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."
+ echo "To bring it back: mv $dest/remote $remotes/$name, mv $dest/project $(project "$name")$(test ! -e "$dest/interactive-session" || echo ", mv $dest/interactive-session $sessions/$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)
@@ -541,7 +544,7 @@ let
*)
active=1
if test -z "$stop"; then
- test -t 0 || die "rename: $old is running: stop it first, or --stop"
+ test -t 0 || die "rename: $old is running: stop it first, or -f"
read -r -p "$old is running: stop it (which ends its sessions)? [y/N] " n
case $n in y | Y | yes) ;; *) die "rename: not stopping $old" ;; esac
fi
@@ -610,14 +613,50 @@ let
echo "These mention $old still:"
printf ' %s\n' $mentions
fi
- echo "Check $new: claude-remote inspect $new, and claude.ai. To take it back: claude-remote rename $new $old"
- echo "(claude.ai's side panel keeps calling it $old: claude-remote reset $new for the new name, with a new session.)"
+ echo "Check $new: claude-remote --inspect $new, and claude.ai. To take it back: claude-remote --rename $new $old"
+ echo "(claude.ai's side panel keeps calling it $old: claude-remote --reset $new for the new name, with a new session.)"
}
- cmd=''${1-list}
- shift || :
+ # Commands and options, in any order (one set, without overlaps), and
+ # the words, NAME (or OLD and NEW, for --rename, unless given by --from
+ # and --to); after --, journalctl's options, for --log.
+ cmd= type= force= from= to= words=() jargs=()
+ while test $# -gt 0; do
+ case $1 in
+ --list | --inspect | --start | --stop | --restart | --attach | --log | --rename | --reset | --remove | --type-of | --names)
+ test -z "$cmd" || test "$cmd" = "''${1#--}" || die "--$cmd or $1, not both"
+ cmd=''${1#--} ;;
+ -h | --help) colors 1; usage | paint; exit ;;
+ --interactive) type=interactive ;;
+ --type=server | --type=interactive) type=''${1#--type=} ;;
+ --type=*) die "$1: the type is server or interactive" ;;
+ -f | --force) force=1 ;;
+ --from=?*) from=''${1#--from=} ;;
+ --to=?*) to=''${1#--to=} ;;
+ --) shift; jargs=("$@"); break ;;
+ -*) die "$1: no such option (claude-remote -h)" ;;
+ *) words+=("$1")
+ esac
+ shift
+ done
+ # without a command, --inspect NAME, or else --list
+ if test -z "$cmd"; then
+ if test ''${#words[@]} -gt 0; then cmd=inspect; else cmd=list; fi
+ fi
+ # what each command takes
+ test -z "$type" || case $cmd in start | restart) ;; *) die "--type is for --start and --restart" ;; esac
+ test -z "$force" || case $cmd in rename | reset | remove) ;; *) die "-f is for --rename, --reset and --remove" ;; esac
+ test -z "$from$to" || test "$cmd" = rename || die "--from and --to are for --rename"
+ test ''${#jargs[@]} = 0 || test "$cmd" = log || die "-- (journalctl's options) is for --log"
+ # the one NAME the command takes, as n
+ one_name() {
+ test ''${#words[@]} = 1 || die "--$cmd NAME (claude-remote -h)"
+ n=''${words[0]}
+ name_arg "$n"
+ }
case $cmd in
list)
+ test ''${#words[@]} = 0 || die "--list takes no NAME"
printf '%-20s %-12s %-8s %-12s %s\n' NAME TYPE KIND STATE TRUSTED
names | while read -r n; do
printf '%-20s %-12s %-8s %-12s %s\n' "$n" "$(type_of "$n")" "$(kind "$n")" "$(active "$n")" "$(trusted "$n" && echo yes || echo no)"
@@ -629,34 +668,19 @@ let
done
;;
rename)
- stop=
- args=()
- for a; do
- case $a in
- --stop) stop=1 ;;
- -*) die "rename: $a: no such option (claude-remote -h)" ;;
- *) args+=("$a")
- esac
- done
- test ''${#args[@]} = 2 || die "rename OLD NEW [--stop] (claude-remote -h)"
- do_rename "''${args[0]}" "''${args[1]}" "$stop"
+ # OLD and NEW: by --from and --to, or else the words, in order
+ if test -z "$from"; then from=''${words[0]-}; words=("''${words[@]:1}"); fi
+ if test -z "$to"; then to=''${words[0]-}; words=("''${words[@]:1}"); fi
+ test -n "$from" && test -n "$to" && test ''${#words[@]} = 0 ||
+ die "--rename OLD NEW, or --from=OLD --to=NEW (claude-remote -h)"
+ do_rename "$from" "$to" "$force"
;;
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]}"
+ one_name
+ "do_$cmd" "$n"
;;
inspect)
- name_arg "''${1-}"
- n=$1
+ one_name
echo "remote: $n ($(type_of "$n"), $(kind "$n"))"
echo "unit: $(unit "$n"): $(systemctl show --value -p ActiveState,SubState "$(unit "$n")" | paste -sd/), since $(systemctl show --value -p StateChangeTimestamp "$(unit "$n")")"
echo "directory: $remotes/$n$(test -d "$remotes/$n" || echo ' (not there yet)')"
@@ -669,26 +693,13 @@ let
echo "claude.ai: $(jq -r '"session \(.sessionId), environment \(.environmentId)"' "$p")"
fi
if test -S /run/claude/"$n"/tmux; then
- echo "terminal (claude-remote attach $n):"
+ echo "terminal (claude-remote --attach $n):"
tmux -S /run/claude/"$n"/tmux capture-pane -p -t claude -S -15 | sed '/^$/d; s/^/ /'
fi
hint_attach "$n"
;;
start | restart)
- type=
- args=()
- for a; do
- case $a in
- --interactive) type=interactive ;;
- --type=server | --type=interactive) type=''${a#--type=} ;;
- --type=*) die "$cmd: $a: the type is server or interactive" ;;
- -*) die "$cmd: $a: no such option (claude-remote -h)" ;;
- *) args+=("$a")
- esac
- done
- test ''${#args[@]} = 1 || die "$cmd NAME [--type=server|interactive] (claude-remote -h)"
- n=''${args[0]}
- name_arg "$n"
+ one_name
# before the login, the unit would be skipped, and not come back
test -e "$home/.claude/.credentials.json" ||
die "there's no login yet: runuser -l claude -c 'claude auth login' (then the remotes start)"
@@ -707,72 +718,96 @@ let
fi
systemctl "$cmd" "$(unit "$n")"
if ! is_static "$n"; then
- echo "$n is dynamic: it lasts, across reboots, until claude-remote stop $n."
+ echo "$n is dynamic: it lasts, across reboots, until claude-remote --stop $n."
fi
if is_interactive "$n"; then
- echo "$n is an interactive session ($(cat "$sessions/$n" 2>/dev/null)): claude-remote attach $n for its terminal, or claude.ai while it runs."
+ echo "$n is an interactive session ($(cat "$sessions/$n" 2>/dev/null)): claude-remote --attach $n for its terminal, or claude.ai while it runs."
fi
hint_attach "$n"
;;
stop)
- name_arg "''${1-}"
- systemctl stop "$(unit "$1")"
+ one_name
+ systemctl stop "$(unit "$n")"
;;
attach)
- n=''${1:-main}
+ test ''${#words[@]} -le 1 || die "--attach [NAME] (claude-remote -h)"
+ n=''${words[0]:-main}
name_arg "$n"
- test -S /run/claude/"$n"/tmux || die "$n isn't running (claude-remote start $n)"
+ test -S /run/claude/"$n"/tmux || die "$n isn't running (claude-remote --start $n)"
exec tmux -S /run/claude/"$n"/tmux attach -t claude
;;
log)
- name_arg "''${1-}"
- n=$1
- shift
- exec journalctl -u "$(unit "$n")" "$@"
+ one_name
+ exec journalctl -u "$(unit "$n")" "''${jargs[@]}"
;;
- type) # for the completion
- name_arg "''${1-}"
- type_of "$1"
+ type-of) # for the completion
+ one_name
+ type_of "$n"
;;
names) # for the completion
names
;;
- help | -h | --help)
- colors 1
- usage | paint
- ;;
- *)
- die "$cmd: no such command (claude-remote -h)"
esac
'';
completion = writeText "claude-remote-completion" ''
_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 reset remove help" -- "$cur"))
- elif [ "$COMP_CWORD" -eq 2 ]; then
- case ''${COMP_WORDS[1]} in
- inspect | start | stop | restart | attach | log | rename | reset | remove)
- COMPREPLY=($(compgen -W "$(claude-remote names 2>/dev/null)" -- "$cur")) ;;
+ local cur=''${COMP_WORDS[COMP_CWORD]} prev=''${COMP_WORDS[COMP_CWORD-1]}
+ local i w cmd= used=" " words=()
+ # what's there so far: the command, the options used (and what they
+ # exclude), the words; after --, journalctl's options, not ours
+ # (bash splits --type=VALUE into --type, =, VALUE)
+ for ((i = 1; i < COMP_CWORD; i++)); do
+ w=''${COMP_WORDS[i]}
+ case $w in
+ --) return ;;
+ --list | --inspect | --start | --stop | --restart | --attach | --log | --rename | --reset | --remove)
+ cmd=''${w#--} ;;
+ --type | --interactive) used="$used--type= --interactive " ;;
+ --from | --to) used="$used$w= " ;;
+ -f | --force) used="$used-f --force " ;;
+ -*) used="$used$w " ;;
+ =) i=$((i + 1)) ;;
+ *) words+=("$w") ;;
esac
- elif [ "''${COMP_WORDS[1]}" = reset ] || [ "''${COMP_WORDS[1]}" = remove ]; then
- COMPREPLY=($(compgen -W "-f --force" -- "$cur"))
- elif [ "''${COMP_WORDS[1]}" = start ] || [ "''${COMP_WORDS[1]}" = restart ]; then
- # --type=TYPE (which bash splits into --type, =, TYPE): only the
- # type it isn't (a new one is a server)
- local prev=''${COMP_WORDS[COMP_CWORD-1]} other=interactive
- [ "$(claude-remote type "''${COMP_WORDS[2]}" 2>/dev/null)" != interactive ] || other=server
- if [ "$cur" = = ] && [ "$prev" = --type ]; then
- COMPREPLY=($other)
- elif [ "$prev" = = ] && [ "''${COMP_WORDS[COMP_CWORD-2]}" = --type ]; then
- COMPREPLY=($(compgen -W "$other" -- "$cur"))
- else
- COMPREPLY=($(compgen -W "--type= $([ $other = server ] || echo --interactive)" -- "$cur"))
- [ "''${COMPREPLY[*]}" != --type= ] || compopt -o nospace
- fi
- elif [ "''${COMP_WORDS[1]}" = rename ]; then
- COMPREPLY=($(compgen -W "--stop" -- "$cur"))
+ done
+ # --type=TYPE: only the type it isn't (a new one is a server)
+ local other=interactive
+ if [ ''${#words[@]} -gt 0 ] && [ "$(claude-remote --type-of "''${words[0]}" 2>/dev/null)" = interactive ]; then
+ other=server
+ fi
+ local valueOf=
+ if [ "$cur" = = ]; then valueOf=$prev cur=
+ elif [ "$prev" = = ]; then valueOf=''${COMP_WORDS[COMP_CWORD-2]}
+ fi
+ case $valueOf in
+ --type) COMPREPLY=($(compgen -W "$other" -- "$cur")); return ;;
+ --from | --to) COMPREPLY=($(compgen -W "$(claude-remote --names 2>/dev/null)" -- "$cur")); return ;;
+ esac
+ # what's left: the commands (until there's one), the command's
+ # options, and as many names as it takes
+ local cmds= opts= o left= max=1 c
+ case $cmd in
+ "") cmds="--list --inspect --start --stop --restart --attach --log --rename --reset --remove" ;;
+ list) max=0 ;;
+ start | restart)
+ opts="--type="
+ [ "$other" = server ] || opts="$opts --interactive" ;;
+ rename) opts="-f --force --from= --to=" max=2 ;;
+ reset | remove) opts="-f --force" ;;
+ log) opts="--" ;;
+ esac
+ for o in $opts; do
+ case $used in *" $o "*) ;; *) left="$left $o" ;; esac
+ done
+ [ "$cmd" != rename ] || max=$((max - $(printf '%s\n' $used | grep -c -e '^--from=$' -e '^--to=$')))
+ COMPREPLY=($(compgen -W "$cmds $left" -- "$cur"))
+ if [ ''${#words[@]} -lt "$max" ]; then
+ COMPREPLY+=($(compgen -W "$(claude-remote --names 2>/dev/null)" -- "$cur"))
+ fi
+ # an option that takes a value (--type=), the only one: no space after
+ if [ ''${#COMPREPLY[@]} = 1 ] && [ "''${COMPREPLY[0]%=}" != "''${COMPREPLY[0]}" ]; then
+ compopt -o nospace
fi
}
complete -F _claude_remote claude-remote