summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pkgs/claude-remote.nix167
1 files changed, 132 insertions, 35 deletions
diff --git a/pkgs/claude-remote.nix b/pkgs/claude-remote.nix
index 20081cf..9429413 100644
--- a/pkgs/claude-remote.nix
+++ b/pkgs/claude-remote.nix
@@ -29,10 +29,16 @@ let
set -eu
export PATH=${lib.makeBinPath [ coreutils findutils gitMinimal gnugrep gnused jq systemd tmux util-linux ]}
# The static remotes: those multi-user.target wants, as the running
- # system has it (so changing them doesn't change this script).
- static=($(systemctl show --value -p Wants multi-user.target 2>/dev/null |
- tr ' ' '\n' | sed -n 's/^claude-remote-control@\(.*\)\.service$/\1/p'))
+ # system has it (so changing them doesn't change this script), of
+ # either kind: Remote Control servers, and interactive sessions.
+ wants=$(systemctl show --value -p Wants multi-user.target 2>/dev/null | tr ' ' '\n')
+ static=($(sed -n 's/^claude-remote-control@\(.*\)\.service$/\1/p' <<< "$wants"))
+ staticI=($(sed -n 's/^claude-interactive@\(.*\)\.service$/\1/p' <<< "$wants"))
state=${lib.escapeShellArg stateDir}
+ # interactive sessions: the dynamic ones, meant to run, and the ids of
+ # their sessions (which make a remote interactive), see configs/claude.nix
+ istate=''${state%/*}/interactive
+ sessions=''${state%/*}/interactive-sessions
home=/home/claude
remotes=$home/remotes
@@ -40,18 +46,24 @@ let
cat <<'EOF'
usage: claude-remote [COMMAND [NAME...]]
- The remotes: claude-remote-control@NAME, each a Claude Code Remote
- Control server, working in ~claude/remotes/NAME.
+ 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)
- The remotes: name, kind, state, trusted; and copies of
+ The remotes: name, type, kind, state, trusted; and copies of
~claude/.claude.json that a failed rename kept.
inspect NAME
A remote, in more detail: its unit, directory, trust, claude.ai
session, and its terminal's last lines.
- start NAME
+ start NAME [--interactive]
Start a remote; a dynamic one lasts, across reboots, until stop.
+ --interactive: a new remote, as an interactive session (which it
+ stays), with a session of its own, resumed at each start (more
+ memory and CPU; its session is archived on claude.ai while it's
+ stopped). Not for one that has had sessions, as a Remote Control
+ server: they wouldn't go on.
stop NAME
restart NAME
attach [NAME]
@@ -67,11 +79,12 @@ let
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).
+ 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]
Retire it: stop it, and move its directory and Claude Code
project to ~claude/remotes/.removed/NAME-DATE (worktrees in it
@@ -123,7 +136,7 @@ let
-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|-f|--force|-h|--help|JOURNALCTL-OPTION)($x|$)/\1$C_OPT\2$C_OFF\3/; tb" \
+ -e ":b; s/(^|$x)(--stop|--interactive|-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() {
@@ -131,8 +144,21 @@ let
printf 'claude-remote: %s\n' "$*" | paint >&2
exit 1
}
+ # interactive: static as such, or with a session id
+ is_interactive() {
+ case " ''${staticI[*]} " in *" $1 "*) return 0 ;; esac
+ test -e "$sessions/$1"
+ }
+ # NAME's unit, of its kind
unit() {
- printf 'claude-remote-control@%s.service' "$1"
+ if is_interactive "$1"; then
+ printf 'claude-interactive@%s.service' "$1"
+ else
+ printf 'claude-remote-control@%s.service' "$1"
+ fi
+ }
+ type_of() {
+ if is_interactive "$1"; then echo interactive; else echo remote; fi
}
valid() {
case $1 in "" | .* | */* | *[[:space:]]*) return 1 ;; esac
@@ -141,11 +167,11 @@ let
valid "''${1-}" || die "not a remote's name: ''${1-}"
}
is_static() {
- case " ''${static[*]} " in *" $1 "*) true ;; *) false ;; esac
+ case " ''${static[*]} ''${staticI[*]} " in *" $1 "*) true ;; *) false ;; esac
}
kind() {
if is_static "$1"; then echo static
- elif test -e "$state/$1"; then echo dynamic
+ elif test -e "$state/$1" || test -e "$istate/$1"; then echo dynamic
else echo -
fi
}
@@ -161,13 +187,13 @@ let
printf '%s/.claude/projects/%s' "$home" "$(printf %s "$remotes/$1" | tr '/.' '--')"
}
# Every remote there is: static, dynamic, with a unit loaded, or with a
- # directory.
+ # directory, of either kind.
names() {
{
- printf '%s\n' "''${static[@]}"
- find "$state" "$remotes" -mindepth 1 -maxdepth 1 -printf '%f\n' 2>/dev/null
- systemctl list-units --all --plain --no-legend 'claude-remote-control@*' 2>/dev/null |
- sed -n 's/^claude-remote-control@\([^ ]*\)\.service .*/\1/p'
+ printf '%s\n' "''${static[@]}" "''${staticI[@]}"
+ find "$state" "$istate" "$sessions" "$remotes" -mindepth 1 -maxdepth 1 -printf '%f\n' 2>/dev/null
+ systemctl list-units --all --plain --no-legend 'claude-remote-control@*' 'claude-interactive@*' 2>/dev/null |
+ sed -n 's/^claude-\(remote-control\|interactive\)@\([^ ]*\)\.service .*/\2/p'
} | while read -r n; do valid "$n" && echo "$n"; done | sort -u
}
hint_attach() {
@@ -311,9 +337,16 @@ let
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
+ # what it has: an interactive one, its session id (and maybe a
+ # link, too); a Remote Control server, the link to its session
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")
+ if is_interactive "$name"; then
+ test -s "$sessions/$name" || die "$name has no session id to forget ($sessions/$name)"
+ ids="session $(cat "$sessions/$name")"
+ else
+ test -f "$p" || die "$name has no claude.ai session to forget ($p)"
+ ids=$(jq -r '"session \(.sessionId), environment \(.environmentId)"' "$p")
+ fi
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?"
@@ -327,8 +360,17 @@ let
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 -f "$p"; then
+ undoable move_back "$p.old" "$p" as_claude
+ as_claude mv -f "$p" "$p.old"
+ fi
+ # an interactive one, a new session id (the old one kept, as a dot
+ # file, which names() passes over)
+ if is_interactive "$name" && test -s "$sessions/$name"; then
+ cp "$sessions/$name" "$sessions/.$name.old"
+ undoable mv -f "$sessions/.$name.old" "$sessions/$name"
+ cat /proc/sys/kernel/random/uuid > "$sessions/$name"
+ fi
if test -n "$active"; then
undoable systemctl stop "$(unit "$name")"
systemctl start "$(unit "$name")"
@@ -360,6 +402,7 @@ let
if test -f "$p"; then
ids=$(jq -r '"session \(.sessionId), environment \(.environmentId)"' "$p")
fi
+ if test -s "$sessions/$name"; then ids="session $(cat "$sessions/$name")"; 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=
@@ -378,6 +421,15 @@ let
fi
undoable as_claude rmdir --ignore-fail-on-non-empty "$dest" "$remotes/.removed"
as_claude mkdir -p "$dest"
+ # an interactive one's mark, and its session id, with the rest
+ if test -e "$istate/$name"; then
+ undoable touch "$istate/$name"
+ rm -f "$istate/$name"
+ fi
+ if test -e "$sessions/$name"; then
+ undoable move_back "$dest/interactive-session" "$sessions/$name"
+ mv "$sessions/$name" "$dest/interactive-session"
+ fi
wts=$(worktrees_below "$remotes/$name")
undoable move_dir_back "$rel/remote" "$name"
as_claude mv "$remotes/$name" "$dest/remote"
@@ -392,12 +444,12 @@ 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"), 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)
old=$1 new=$2 undos=() finished= backup= doing="rename $1 $2"
- local stop=$3 errs=() r n w b sha users active= wts brs
+ local stop=$3 errs=() r n w b d sha users active= wts brs
test "$(id -u)" = 0 || die "rename: as root"
valid "$old" || die "not a remote's name: $old"
valid "$new" || die "not a remote's name: $new"
@@ -413,6 +465,9 @@ let
# what it read before can bring one back)
if is_static "$new"; then errs+=("$new is static (the system's configuration)"); fi
if test -e "$state/$new"; then errs+=("$state/$new is there already"); fi
+ for d in "$istate" "$sessions"; do
+ if test -e "$d/$new"; then errs+=("$d/$new is there already"); fi
+ done
case $(active "$new") in inactive | failed | "") ;; *) errs+=("$(unit "$new") is $(active "$new")") ;; esac
while read -r r; do
n=$(as_claude git -C "$r" for-each-ref --format=x "refs/heads/$new/" | wc -l)
@@ -466,6 +521,13 @@ let
undoable move_back "$state/$new" "$state/$old"
mv "$state/$old" "$state/$new"
fi
+ # an interactive one's session id, and its mark
+ for d in "$sessions" "$istate"; do
+ if test -e "$d/$old"; then
+ undoable move_back "$d/$new" "$d/$old"
+ mv "$d/$old" "$d/$new"
+ fi
+ done
if test -e "$(project "$old")"; then
undoable move_back "$(project "$new")" "$(project "$old")" as_claude
as_claude mv "$(project "$old")" "$(project "$new")"
@@ -506,9 +568,9 @@ let
shift || :
case $cmd in
list)
- printf '%-20s %-8s %-12s %s\n' NAME KIND STATE TRUSTED
+ printf '%-20s %-12s %-8s %-12s %s\n' NAME TYPE KIND STATE TRUSTED
names | while read -r n; do
- printf '%-20s %-8s %-12s %s\n' "$n" "$(kind "$n")" "$(active "$n")" "$(trusted "$n" && echo yes || echo no)"
+ printf '%-20s %-12s %-8s %-12s %s\n' "$n" "$(type_of "$n")" "$(kind "$n")" "$(active "$n")" "$(trusted "$n" && echo yes || echo no)"
done
# not to be forgotten: copies of ~claude/.claude.json, kept by a
# rename that couldn't take back all it had done
@@ -545,10 +607,13 @@ let
inspect)
name_arg "''${1-}"
n=$1
- echo "remote: $n ($(kind "$n"))"
+ 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)')"
echo "trusted: $(trusted "$n" && echo yes || echo no)"
+ if test -s "$sessions/$n"; then
+ echo "session: $(cat "$sessions/$n") (resumed at each start)"
+ fi
p=$(project "$n")/bridge-pointer.json
if test -f "$p"; then
echo "claude.ai: $(jq -r '"session \(.sessionId), environment \(.environmentId)"' "$p")"
@@ -560,15 +625,45 @@ let
hint_attach "$n"
;;
start)
- name_arg "''${1-}"
+ interactive=
+ args=()
+ for a; do
+ case $a in
+ --interactive) interactive=1 ;;
+ -*) die "start: $a: no such option (claude-remote -h)" ;;
+ *) args+=("$a")
+ esac
+ done
+ test ''${#args[@]} = 1 || die "start NAME [--interactive] (claude-remote -h)"
+ n=''${args[0]}
+ name_arg "$n"
# 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)"
- systemctl start "$(unit "$1")"
- if ! is_static "$1"; then
- echo "$1 is dynamic: it lasts, across reboots, until claude-remote stop $1."
+ # an interactive session, from now on: a session id of its own
+ # (the kind sticks, so that start, stop and the rest find it)
+ if test -n "$interactive" && ! is_interactive "$n"; then
+ test "$(id -u)" = 0 || die "start --interactive: as root"
+ case $(active "$n") in
+ inactive | failed | "") ;;
+ *) die "$n runs as a Remote Control server: claude-remote stop $n first" ;;
+ esac
+ test ! -e "$state/$n" || die "$n is a dynamic Remote Control server ($state/$n): claude-remote stop $n first"
+ # only a new one: an interactive session wouldn't go on with the
+ # sessions it had as a Remote Control server, but start anew
+ if compgen -G "$(project "$n")/*.jsonl" >/dev/null; then
+ die "start --interactive is for a new remote: $n has had sessions already, as a Remote Control server, which an interactive session wouldn't go on with"
+ fi
+ cat /proc/sys/kernel/random/uuid > "$sessions/$n"
fi
- hint_attach "$1"
+ systemctl start "$(unit "$n")"
+ if ! is_static "$n"; then
+ 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."
+ fi
+ hint_attach "$n"
;;
stop | restart)
name_arg "''${1-}"
@@ -610,6 +705,8 @@ let
esac
elif [ "''${COMP_WORDS[1]}" = reset ] || [ "''${COMP_WORDS[1]}" = remove ]; then
COMPREPLY=($(compgen -W "-f --force" -- "$cur"))
+ elif [ "''${COMP_WORDS[1]}" = start ]; then
+ COMPREPLY=($(compgen -W "--interactive" -- "$cur"))
elif [ "''${COMP_WORDS[1]}" = rename ]; then
COMPREPLY=($(compgen -W "--stop" -- "$cur"))
fi