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
89 changes: 83 additions & 6 deletions bin/berth-herdr-app
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,90 @@ terminal_agent_state() { # <terminal>
berth_command=("$BASH" -c 'export BERTH_BACKEND=herdr-app BERTH_YARD=$1 BERTH_ID=$HC_TERMINAL_ID
shift; "$@"; exec "${SHELL:-sh}"' berth "$yard")

# 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() { # <sp|tb>
printf '%s%s\n' "$1" "$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"
}

# Nothing can measure the window's panes while it may be closed, so a count
# stands in for the size floors that herdr's adapter measures.
workers_per_tab=4

# The tab in the opener's space where its next worker goes: the oldest of its
# workers' tabs that has room.
worker_tab() { # <opener> <space>
local tab
tab=$(terminals | jq -r --arg opener "$1" --arg space "$2" --argjson most "$workers_per_tab" '
[.[] | select(.labels["berth.parent"] == $opener and .labels["berth.relationship"] == "worker"
and .labels["herdr.space"] == $space)] | sort_by(.created_ms) | map(.labels["herdr.tab"]) as $tabs
| first($tabs[] as $tab | select([$tabs[] | select(. == $tab)] | length < $most) | $tab) // empty')
[ -n "$tab" ] || tab=$(new_place_id tb)
printf '%s\n' "$tab"
}

# Names and focus belong to the window, so they wait on no window. A closed
# one leaves Herdr's own names. The window may not have seen a new terminal's
# labels yet, so a refusal is tried again for a while.
window_wait_seconds=5

window() { # <command> <arguments>...
local out deadline=$((SECONDS + window_wait_seconds))
command -v herdr-app >/dev/null && herdr-app status >/dev/null 2>&1 || return 0
until out=$(herdr-app "do" "$@" 2>&1); do
if [ "$SECONDS" -ge "$deadline" ]; then
echo "berth (herdr-app): the window refused $1: $out" >&2
return 0
fi
sleep 0.2
done
}

# Names whatever an independent berth's place gives it: the space and its
# first tab for one opened apart, and the tab for one opened in a tab. The
# window takes a command with no id to mean what it shows, so a berth that
# the window does not show names nothing.
name_place() { # <terminal> <label>
local space tab
jq -e '.labels["berth.relationship"] == "independent"' <<<"$1" >/dev/null || return 0
space=$(jq -r '.labels["herdr.space"] // empty' <<<"$1")
tab=$(jq -r '.labels["herdr.tab"] // empty' <<<"$1")
[ -n "$space" ] && [ -n "$tab" ] || return 0
case $(jq -r '.labels["berth.place"]' <<<"$1") in
apart) window space.rename --id "$space" --name "$2" ;&
tab) window tab.rename --id "$tab" --name "$2" ;;
esac
}

open() {
local as=$1 place=$2 cwd=$4 label=$5 opener=$6 id line="" labels=()
local as=$1 place=$2 focus=$3 cwd=$4 label=$5 opener=$6 id line="" opener_terminal space="" tab=""
local workers_tab="" labels=()
shift 6
# Focus belongs to the window, which places no berth yet.

if [ "$as" = independent ] && [ "$place" = apart ]; then
opener=""
space=$(new_place_id sp)
tab=$(new_place_id tb)
else
opener=$(berth_id "$opener" "there is nothing to open a berth beside")
line=$(existing_terminal "$opener" "there is nothing to open a berth beside" |
jq -r --arg id "$opener" '.labels["berth.line"] // $id')
opener_terminal=$(existing_terminal "$opener" "there is nothing to open a berth beside")
line=$(jq -r --arg id "$opener" '.labels["berth.line"] // $id' <<<"$opener_terminal")
labels+=(--label "berth.line=$line")
# A berth beside a terminal that the window does not show stays hidden too.
space=$(jq -r '.labels["herdr.space"] // empty' <<<"$opener_terminal")
if [ -n "$space" ]; then
case $as/$place in
independent/tab) tab=$(new_place_id tb) ;;
worker/*)
tab=$(worker_tab "$opener" "$space")
terminals | jq -e --arg tab "$tab" 'any(.[]; .labels["herdr.tab"] == $tab)' >/dev/null ||
workers_tab=$tab
;;
*) tab=$(jq -r '.labels["herdr.tab"] // empty' <<<"$opener_terminal") ;;
esac
fi
fi
labels+=(${space:+--label "herdr.space=$space"} ${tab:+--label "herdr.tab=$tab"})

if [ "$as" = tender ] && [ -n "$label" ]; then
id=$(terminals | jq -r --arg line "$line" --arg label "$label" '
Expand All @@ -148,14 +219,20 @@ open() {
${label:+--label "berth.label=$label"} "${labels[@]}" \
-- "${berth_command[@]}" "$@" | jq -r .address)
[ -n "$line" ] || hc_call label "$id" "berth.line=$id" >/dev/null
if [ -n "$space" ]; then
[ -z "$label" ] || name_place "$(hc_call get "$id")" "$label"
[ -z "$workers_tab" ] || window tab.rename --id "$workers_tab" --name workers
[ "$focus" != focus ] || window pane.focus --terminal "$id"
fi
printf '%s\n' "$id"
}

name() {
local id
local id terminal
id=$(berth_id "$1")
existing_terminal "$id" "it cannot be named" >/dev/null
terminal=$(existing_terminal "$id" "it cannot be named")
hc_call label "$id" "berth.label=$2" >/dev/null
[ -z "$(jq -r '.labels["herdr.space"] // empty' <<<"$terminal")" ] || name_place "$terminal" "$2"
}

serve() {
Expand Down
4 changes: 3 additions & 1 deletion docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ The herdr desktop app runs its terminals in its own backend, herdr-core, and nev
- **Agent state.** herdr-core takes a terminal's agent state only from what its program reports over OSC 7501. `working` is `active`, `blocked` is `blocked`, and `idle`, `done` and `error` are `at-rest`. A terminal with no report has no agent state. A berth's agent session is the terminal's `claude.session` label, which the plugin's mod sets, as *Agent state under the herdr app* says. The adapter lists none for a berth with no agent state.
- **The shell after the command.** hc runs a berth's command as the terminal's program, and the terminal finishes when the command exits. So the adapter runs the command under a shell, which becomes the shell that `SHELL` names in herdr-core's environment once the command exits. An agent that leaves for its shell then leaves its berth open, and herdr-core clears its agent state once the shell is back in the foreground. The command takes its `BERTH_ID` from herdr-core's own `HC_TERMINAL_ID`, because hc gives a terminal its id only once it exists.
- **Text waits for Enter.** hc can paste text and press Enter in one call, but it cannot type text without submitting it. So `send-text` holds the text in the adapter's state, under `berth/herdr-app/<yard>/` in `$XDG_STATE_HOME`, and the next `send-key Enter` submits it with one paste. Text held this way does not show on screen until then. A key other than Enter is refused while text is held, because hc could not type the text in front of it. A later hc that types without submitting closes the gap.
- **Lines of work.** The app window places no berth yet, so every berth stays hidden from the window. A line of work is a label: an independent berth that opens apart starts one, and every other berth joins its opener's. `open` looks within it for a labelled tender, and closing a berth closes what it opened.
- **Layout.** The window shows a terminal in the space and tab that its `herdr.space` and `herdr.tab` labels name, and makes a new space or tab for an id it does not know. So the adapter places a berth through those labels alone, and placement works while the window is closed. An independent berth that opens apart gets a new space. One opened in a tab gets a new tab in its opener's space. One opened beside its opener, and a tender, join the opener's tab, and the window chooses the split. Workers share a tab in their opener's space, four to a tab, and then open another. The window may be closed, so nothing can measure its panes, and the count stands in for herdr's size floors. A berth whose opener the window does not show stays hidden too.
- **Names and focus.** Only the window can name a space or a tab, or move focus, so the adapter asks it through `herdr-app` when one is open. A berth's label names its space and tab as it does under herdr, and a new workers' tab is called `workers`. With the window closed, Herdr's own names stand, and `--focus` does nothing. A pane takes no name, because the app before 0.1.17 cannot name one.
- **Lines of work.** A line of work is a label: an independent berth that opens apart starts one, and every other berth joins its opener's. `open` looks within it for a labelled tender, and closing a berth closes what it opened.
- **Package parameter.** herdr-core's package is a parameter, as herdr's is for its adapter.

## `sign-on`
Expand Down
3 changes: 2 additions & 1 deletion nix/berth/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ in
berth-contract-herdr = herdrSuite "berth-contract";
berth-contract-herdr-app = herdrAppSuite "berth-contract";
berth-herdr-app-caller = herdrAppSuite "herdr-app-caller";
berth-herdr-app-layout = herdrAppSuite "herdr-app-layout";
berth-herdr-layout = herdrSuite "herdr-layout";
berth-herdr-lost-agent = herdrSuite "herdr-lost-agent";
berth-herdr-session = herdrSuite "herdr-session";
Expand Down Expand Up @@ -66,7 +67,7 @@ in
${../../tests/herdr-shell-start} ${../../tests/herdr-binary} ${../../tests/herdr-session} ${../../tests/herdr-restore} \
${../../tests/berth-usage} ${../../tests/herdr-open-by-hand} \
${../../bin/berth-herdr-app} ${../../tests/with-herdr-app-yard} ${../../tests/herdr-app-open-by-hand} \
${../../tests/herdr-app-caller}
${../../tests/herdr-app-caller} ${../../tests/herdr-app-layout}
touch $out
'';
};
Expand Down
128 changes: 128 additions & 0 deletions tests/herdr-app-layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env bash
# The herdr app adapter's layout rules, which the port contract leaves to each
# adapter. A berth's place in the app window is the herdr.space and herdr.tab
# labels on its terminal, so these checks read the labels. They need no window.
# See docs/design.md, "The herdr app adapter".
#
# Run it under with-herdr-app-yard.
#
# Needs: bash, coreutils, herdr-core, jq, berth.
set -uo pipefail

work=$(mktemp -d)
failures=0

check() { # <what> <want> <got>
if [ "$2" = "$3" ]; then
echo "ok: $1"
else
echo "FAIL: $1: wanted '$2', got '$3'"
failures=$((failures + 1))
fi
}

differ() { # <a> <b>
[ "$1" != "$2" ] && echo true || echo false
}

hand() {
env -u HC_TERMINAL_SOCKET -u HC_TERMINAL_ID HC_SESSION="$BERTH_YARD" hc "$1" --json "${@:2}"
}

place() { # <terminal> <space|tab>
hand get "$1" | jq -r --arg key "herdr.$2" '.labels[$key] // "none"'
}

exists() { # <terminal>
[ "$(berth state "$1")" = open ] && echo true || echo false
}

echo "# an independent berth opened apart gets a space of its own"

first=$(berth open --as independent --cwd "$work" --label "first line" -- true)
second=$(berth open --as independent --cwd "$work" -- true)
space=$(place "$first" space)
check "it is in a space" true "$(differ none "$space")"
check "it is in a tab" true "$(differ none "$(place "$first" tab)")"
check "each independent berth has its own space" true "$(differ "$space" "$(place "$second" space)")"

echo "# workers share a tab in their opener's space"

workers=()
for _ in 1 2; do
workers+=("$(BERTH_ID=$first berth open --as worker --cwd "$work" -- true)")
done
tab=$(place "${workers[0]}" tab)
check "a worker opens in its opener's space" "$space" "$(place "${workers[0]}" space)"
check "a worker opens outside its opener's tab" true "$(differ "$(place "$first" tab)" "$tab")"
check "the second worker shares the first worker's tab" "$tab" "$(place "${workers[1]}" tab)"

echo "# workers fill a tab, then open another"

for _ in 1 2 3; do
workers+=("$(BERTH_ID=$first berth open --as worker --cwd "$work" -- true)")
done
check "the fourth worker shares the first worker's tab" "$tab" "$(place "${workers[3]}" tab)"
check "the fifth worker opens another tab" true "$(differ "$tab" "$(place "${workers[4]}" tab)")"
check "the fifth worker stays in its opener's space" "$space" "$(place "${workers[4]}" space)"
check "the fifth worker opens outside its opener's tab" true \
"$(differ "$(place "$first" tab)" "$(place "${workers[4]}" tab)")"
berth close "${workers[0]}"
refill=$(BERTH_ID=$first berth open --as worker --cwd "$work" -- true)
check "a worker fills the room a closed worker left" "$tab" "$(place "$refill" tab)"

echo "# a tender opens in its opener's tab"

tender=$(BERTH_ID=$first berth open --as tender --cwd "$work" -- true)
check "it opens in its opener's space" "$space" "$(place "$tender" space)"
check "it opens in its opener's tab" "$(place "$first" tab)" "$(place "$tender" tab)"

echo "# a berth opened beside its opener shares its tab"

beside=$(BERTH_ID=$first berth open --as independent --beside --cwd "$work" -- true)
check "it opens in its opener's tab" "$(place "$first" tab)" "$(place "$beside" tab)"
check "it opens in its opener's space" "$space" "$(place "$beside" space)"

echo "# a berth opened in a tab gets a tab of its own in its opener's space"

tabbed=$(BERTH_ID=$first berth open --as independent --tab --cwd "$work" --label effort -- true)
check "it opens in its opener's space" "$space" "$(place "$tabbed" space)"
check "it opens outside its opener's tab" true "$(differ "$(place "$first" tab)" "$(place "$tabbed" tab)")"
check "it opens outside the workers' tab" true "$(differ "$tab" "$(place "$tabbed" tab)")"

echo "# a seat a person opened in the window starts an effort beside itself"

# The app labels the terminals it starts with their space and tab.
seat=$(hand run --bg --no-parent --no-notify --cwd "$work" \
--label herdr.space=sp-a-persons-space --label herdr.tab=tb-a-persons-tab | jq -r .address)
steward=$(env -u BERTH_ID HC_TERMINAL_ID="$seat" HC_SESSION="$BERTH_YARD" \
berth open --as independent --beside --cwd "$work" -- true)
check "it opens in the seat's space" sp-a-persons-space "$(place "$steward" space)"
check "it opens in the seat's tab" tb-a-persons-tab "$(place "$steward" tab)"
steward_worker=$(BERTH_ID=$steward berth open --as worker --cwd "$work" -- true)
check "its workers open in the seat's space" sp-a-persons-space "$(place "$steward_worker" space)"
check "its workers open outside the seat's tab" true "$(differ tb-a-persons-tab "$(place "$steward_worker" tab)")"

echo "# a berth opened beside a terminal the window does not show stays hidden too"

hidden=$(hand run --bg --no-parent --no-notify --cwd "$work" | jq -r .address)
for as in worker tender; do
joined=$(env -u BERTH_ID HC_TERMINAL_ID="$hidden" HC_SESSION="$BERTH_YARD" \
berth open --as "$as" --cwd "$work" -- true)
check "a $as of a hidden opener has no space" none "$(place "$joined" space)"
check "a $as of a hidden opener has no tab" none "$(place "$joined" tab)"
done

echo "# closing a berth closes what it opened"

berth close "$first"
check "its workers are gone" false "$(exists "${workers[1]}")"
check "its tender is gone" false "$(exists "$tender")"
check "the other independent berth stays" true "$(exists "$second")"

echo
if [ "$failures" -gt 0 ]; then
echo "$failures failed."
exit 1
fi
echo "The herdr app layout rules hold."