diff --git a/bin/berth-herdr-app b/bin/berth-herdr-app index 5e01518..ae0ebb9 100755 --- a/bin/berth-herdr-app +++ b/bin/berth-herdr-app @@ -115,6 +115,108 @@ terminal_agent_state() { # berth_command=("$BASH" -c 'export BERTH_BACKEND=herdr-app BERTH_YARD=$1 BERTH_ID=$HC_TERMINAL_ID shift; "$@"; exec "${SHELL:-sh}"' berth "$yard") +state_dir() { + echo "${XDG_STATE_HOME:-$HOME/.local/state}/berth/herdr-app/$yard" +} + +# A berth's command is kept where only its user can read it, because sign-on +# passes a seat's environment, secrets included, on its command line. The file +# is named for the terminal's creation as well as its id, so a command never +# reaches a terminal it was not opened in. +kept_command() { # + jq -r --arg dir "$(state_dir)" '"\($dir)/\(.address).\(.created_ms).command"' <<<"$1" +} + +keep_command() { # ... + local file + file=$(kept_command "$(existing_terminal "$1" "its command cannot be kept")") + shift + (umask 077 && mkdir -p "${file%/*}" && printf '%s\0' "$@" >"$file") +} + +# herdr-core restarts a kept terminal with the shell alone as its program. The +# berth.restore label names the shell the adapter has already acted on. +restored='(.argv | length) == 1 and ((.labels["berth.restore"] // "") | split(" ")[0]) != (.pid | tostring)' + +# Two berth calls at once would each type into the same restored shell, and +# the second line would land in the agent the first one started. So one call +# at a time restores a yard, and it reads the terminals once it holds the lock. +restore_lock() { + mkdir -p "$(state_dir)" + exec {restoring}>"$(state_dir)/restore.lock" + flock "$restoring" +} + +restore_unlock() { + exec {restoring}>&- +} + +# The shell is alone in the foreground for a moment before its start-up runs +# anything, such as a dev shell's hook that would swallow typed text, so it +# has to stay alone for several polls in a row, the last just before typing. +shell_settle_polls=4 +shell_poll_seconds=0.25 + +shell_settled() { # + local _ + for _ in $(seq 1 "$shell_settle_polls"); do + sleep "$shell_poll_seconds" + terminal_get "$1" | jq -e '.foreground.pid == .pid' >/dev/null || return 1 + done +} + +# Types a command into a restored berth's shell, under the berth's own +# variables, and says on the terminal what was done with its shell. hc types +# nothing into a program that reports itself blocked. +type_command() { # ... + local id pid line + id=$(jq -r .address <<<"$1") + pid=$(jq -r .pid <<<"$1") + printf -v line '%q ' env BERTH_BACKEND=herdr-app "BERTH_YARD=$yard" "BERTH_ID=$id" "${@:3}" + hc_call send -- "$id" "$line" >/dev/null + hc_call label "$id" "berth.restore=$pid $2" >/dev/null +} + +not_resumed() { # + local id pid + id=$(jq -r .address <<<"$1") + pid=$(jq -r .pid <<<"$1") + echo "berth (herdr-app): the berth $id is not resumed, because $2. Its shell is left bare." >&2 + hc_call label "$id" "berth.restore=$pid not resumed: $2" >/dev/null +} + +# Resumes each seat that herdr-core restored, from the session id the plugin's +# mod left on its terminal. sign-on launches a seat with its prompt last, after +# --, so the resume takes the prompt's place. sign-on then gets the seat past +# its startup gate. +resume_seats() { + local seats terminal id file command at restoring + restore_lock + mapfile -t seats < <(terminals | jq -c ".[] | select(.labels[\"berth.relationship\"] and .labels[\"claude.session\"] and $restored)") + for terminal in "${seats[@]}"; do + id=$(jq -r .address <<<"$terminal") + shell_settled "$id" || continue + file=$(kept_command "$terminal") + [ -f "$file" ] || { not_resumed "$terminal" "its command was not kept"; continue; } + mapfile -d '' -t command <"$file" + for ((at = 0; at < ${#command[@]}; at++)); do + [ "${command[at]}" != -- ] || break + done + case $((${#command[@]} - at)) in + 0) ;; + 2) command=("${command[@]:0:at}") ;; + *) not_resumed "$terminal" "its command has more after -- than a prompt"; continue ;; + esac + type_command "$terminal" resumed "${command[@]}" --resume "$(jq -r '.labels["claude.session"]' <<<"$terminal")" + if command -v sign-on >/dev/null; then + BERTH_BACKEND=herdr-app BERTH_YARD=$yard sign-on --pass-gate "$id" /dev/null 2>&1 {restoring}>&- & + else + echo "berth (herdr-app): sign-on is not on the path, so the berth $id waits at its startup gate." >&2 + fi + done + restore_unlock +} + # The window shows a terminal labelled with herdr.space and herdr.tab in that # space and tab, and makes a new one for an id it does not know. new_place_id() { # @@ -193,8 +295,9 @@ name_place() { #