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
28 changes: 21 additions & 7 deletions bin/berth
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
# serve <yard>
# stop <yard>
# yard
# yards
# id <id>
# open <independent|worker|tender> <apart|beside|tab> <focus|no-focus> <cwd> <label> <opener> <command>...
# name <id> <label>
# list <yard>
# list <yard> <berths|all>
# send-text <id> <text>
# send-key <id> <key>
# wait-text <id> <text> <seconds>
Expand All @@ -22,7 +23,9 @@
#
# serve execs the backend, so that a service manager supervises the real
# process. wait-text matches its text as a literal within one line of recent
# output, and read prints the visible screen as plain text.
# output, and read prints the visible screen as plain text. yards prints
# each running yard on a line of its own. list with all also lists each pane
# berth did not open that runs an agent.
#
# <opener> is BERTH_ID, and so is <id> when the caller gave none. Either is
# empty when BERTH_ID is unset too, and the adapter then resolves the calling
Expand All @@ -39,11 +42,12 @@ print_usage() {
usage: berth serve <yard>
berth stop <yard>
berth yard
berth yards
berth id
berth open --as independent|worker|tender --cwd <dir> [--label <text>] [--focus]
[--beside | --tab] -- <command>...
berth name [<id>] <label>
berth list [<yard>]
berth list [<yard>] [--all]
berth send-text [<id>] <text>
berth send-key [<id>] <key>
berth wait-text [<id>] <text> --timeout <seconds>
Expand Down Expand Up @@ -126,17 +130,27 @@ case "$verb" in
*) usage ;;
esac
;;
yard)
yard | yards)
[ $# = 0 ] || usage
exec "$adapter" yard
exec "$adapter" "$verb"
;;
id)
[ $# = 0 ] || usage
exec "$adapter" id "${BERTH_ID:-}"
;;
list)
[ $# -le 1 ] || usage
exec "$adapter" list "${1:-}"
yard="" scope=berths
for arg in "$@"; do
case "$arg" in
--all) scope=all ;;
-*) usage ;;
*)
[ -z "$yard" ] || usage
yard=$arg
;;
esac
done
exec "$adapter" list "$yard" "$scope"
;;
read | state | agent-state | close)
case $# in
Expand Down
29 changes: 20 additions & 9 deletions bin/berth-herdr
Original file line number Diff line number Diff line change
Expand Up @@ -375,27 +375,38 @@ read_screen() {
on_berth "$id" "it cannot be read" herdr_call pane read "$id" --source visible --format text
}

list() {
local panes berths id states='{}' state
yards() {
local out
out=$(env -u HERDR_SOCKET_PATH "$herdr" session list --json 2>&1) ||
die "herdr session list failed: $out"
jq -r '.sessions[] | select(.running) | .name' <<<"$out"
}

# With all, a pane that berth did not open is listed while it runs an agent.
list() { # <yard> <berths|all>
local panes listed id states='{}' state
yard=${1:-$yard}
restore_berths
panes=$(herdr_result pane list)
berths=$(jq -c '[.panes[] | select(.tokens.berth_relationship)]' <<<"$panes")
for id in $(jq -r '.[].pane_id' <<<"$berths"); do
state=$(pane_agent_state "$(jq -c --arg id "$id" '.[] | select(.pane_id == $id)' <<<"$berths")")
listed=$(jq -c --arg scope "$2" \
'[.panes[] | select($scope == "all" or .tokens.berth_relationship)]' <<<"$panes")
for id in $(jq -r '.[].pane_id' <<<"$listed"); do
state=$(pane_agent_state "$(jq -c --arg id "$id" '.[] | select(.pane_id == $id)' <<<"$listed")")
states=$(jq -c --arg id "$id" --arg state "$state" '.[$id] = $state' <<<"$states")
done
# herdr keeps an agent's session once the agent has left.
jq --argjson states "$states" '
[.[] | ($states[.pane_id] | if . == "none" then null else . end) as $agent | {
[.[] | ($states[.pane_id] | if . == "none" then null else . end) as $agent
| (.tokens.berth_relationship // null) as $relationship
| select($relationship != null or $agent != null) | {
id: .pane_id,
label: (.label // null),
relationship: .tokens.berth_relationship,
relationship: $relationship,
parent: (.tokens.berth_parent // null),
state: "open",
agent_state: $agent,
agent_session: (if $agent == null then null else .agent_session.value? // null end)
}]' <<<"$berths"
}]' <<<"$listed"
}

state() {
Expand Down Expand Up @@ -461,7 +472,7 @@ close() {
verb=$1
shift
case "$verb" in
serve | stop | open | name | list | state | close) "$verb" "$@" ;;
serve | stop | open | name | list | state | close | yards) "$verb" "$@" ;;
yard) echo "$yard" ;;
id) berth_id "$1" ;;
send-text) send_text "$@" ;;
Expand Down
7 changes: 4 additions & 3 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ Each adapter maps its backend's agent states onto these three and corrects its b
| `berth serve <yard>` | Runs the backend for a yard in the foreground, so a service manager can supervise the real process. |
| `berth stop <yard>` | Stops a yard. |
| `berth yard` | Prints the yard the caller acts in, resolved as the adapter resolves it for every other verb. |
| `berth yards` | Prints each yard that is up, one per line. |
| `berth id` | Prints the calling berth's id. A caller that is not a berth gets the id of the pane it runs in, which stands in for it as it does for every other verb. |
| `berth open --as independent\|worker\|tender --cwd <dir> [--label <text>] [--beside \| --tab] [--focus] -- <command>` | Opens a berth and prints its id. With `--beside`, an independent berth opens beside its opener rather than apart. With `--tab`, it opens in a tab near its opener. With `--focus`, the person watching the yard lands on the new berth. Without it, their focus stays where it was. A labelled tender opens once in a line of work: where its opener's line of work already holds a tender with that label, `open` makes the opener its parent and prints its id, and runs no command. |
| `berth name [<id>] <label>` | Labels a berth. On an independent berth that opened apart or in a tab, the label also names whatever the adapter groups under it. |
| `berth list [<yard>]` | Prints the berths as JSON: id, label, relationship, parent, state, agent state and agent session. A berth's agent session is the id its agent goes by, such as a Claude Code session id. The agent state and the agent session are both null for a berth with no agent in it, such as one running a bead viewer. |
| `berth list [<yard>] [--all]` | Prints the berths as JSON: id, label, relationship, parent, state, agent state and agent session. A berth's agent session is the id its agent goes by, such as a Claude Code session id. The agent state and the agent session are both null for a berth with no agent in it, such as one running a bead viewer. With `--all`, it also lists each pane that berth did not open while an agent runs in it, such as a seat a person started by hand. Such a pane has a null relationship and parent. |
| `berth send-text [<id>] <text>` | Types text into a berth. |
| `berth send-key [<id>] <key>` | Sends one key, such as `Enter`. |
| `berth wait-text [<id>] <text> --timeout <s>` | Blocks until the text appears within one line on screen, or fails at the timeout. The text is literal, because each backend has its own regular expression dialect. |
Expand Down Expand Up @@ -110,7 +111,7 @@ Where a verb needs the caller, such as a worker's opener or the opener a berth o
A product outside boatyard may depend on four things, and on nothing else of boatyard's:

- **The three variables** above, inherited by every process a berth runs.
- **`berth list`'s JSON:** an array with one object per berth, each holding exactly the fields `id`, `label`, `relationship`, `parent`, `state`, `agent_state` and `agent_session`, with the values the verbs table and *Berth and agent states* give them.
- **`berth list`'s JSON:** an array with one object per berth, each holding exactly the fields `id`, `label`, `relationship`, `parent`, `state`, `agent_state` and `agent_session`, with the values the verbs table and *Berth and agent states* give them. Only `--all` lists a null relationship.
- **The `claude.session` label:** on a herdr app terminal running a Claude Code session that loads the plugin, the hc label `claude.session` holds that session's id, as *Agent state under the herdr app* says.
- **The efforts output:** the JSON lines that `skills/tally/efforts` prints, one per open effort, each holding at least the fields `project`, `top`, `slug`, `steward`, `ready` and `restart`, with the meanings the tally skill gives them. Its other fields, `also`, `status`, `gates` and `asks`, are not part of the contract.

Expand Down Expand Up @@ -233,7 +234,7 @@ Pull requests are GitHub's. The skills read, wait on and merge them with `gh` an

## Testing

- **Port contract suite.** One suite states what every adapter must do: open in each relationship, list, name, send-text, send-key, wait-text, read, state, agent state across all three agent states and none, close, and the cascade when an independent berth closes. It runs against every adapter.
- **Port contract suite.** One suite states what every adapter must do: open in each relationship, list, list with `--all`, yards, name, send-text, send-key, wait-text, read, state, agent state across all three agent states and none, close, and the cascade when an independent berth closes. It runs against every adapter. Each adapter's run supplies a command that opens a pane as a person would, outside berth, for the `--all` cases.
- For herdr, it runs a throwaway server with `HOME` redirected and every `HERDR_*` variable unset. An inherited `HERDR_SOCKET_PATH` would otherwise drive the live session. The server runs inside the nix build sandbox, so the suite is a flake check.
- herdr treats a process named `claude` as an agent and reads its state from the screen. The suite drives the states with a stand-in process of that name, not with Claude Code.
- **Unit tests.** The mod's compaction rule, its rule for a resumed steward and its reports to the herdr app have their own tests, run by `claude plugin test` against a stand-in engine. So does each adapter's mapping from backend states to the port's agent states, including the herdr parked-on-background-work and lost-agent corrections.
Expand Down
7 changes: 6 additions & 1 deletion nix/berth/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
let
berth = pkgs.callPackage ./package.nix { };

openByHand = pkgs.writeShellScript "herdr-open-by-hand" ''
exec bash ${../../tests/herdr-open-by-hand} "$@"
'';

# Each suite runs against a herdr server of its own inside the build
# sandbox.
herdrSuite = suite: pkgs.runCommand "berth-${suite}"
{ nativeBuildInputs = [ berth pkgs.bash pkgs.coreutils pkgs.herdr pkgs.jq ]; } ''
export BERTH_OPEN_BY_HAND=${openByHand}
bash ${../../tests/with-herdr-yard} bash ${../../tests + "/${suite}"}
touch $out
'';
Expand Down Expand Up @@ -42,7 +47,7 @@ in
${../../tests/with-herdr-yard} ${../../tests/berth-contract} ${../../tests/herdr-layout} \
${../../tests/herdr-state} ${../../tests/herdr-lost-agent} ${../../tests/herdr-caller} \
${../../tests/herdr-shell-start} ${../../tests/herdr-binary} ${../../tests/herdr-session} ${../../tests/herdr-restore} \
${../../tests/berth-usage}
${../../tests/berth-usage} ${../../tests/herdr-open-by-hand}
touch $out
'';
};
Expand Down
64 changes: 57 additions & 7 deletions tests/berth-contract
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
# "The port: berth" and "Testing".
#
# Run it with berth on the path, and BERTH_BACKEND and BERTH_YARD naming a
# yard that is up and holds no berths. It reads nothing of any backend, so a
# second adapter runs it unchanged.
# yard that is up and holds no berths. BERTH_OPEN_BY_HAND names a command
# that opens a pane in that yard as a person would, outside berth, runs the
# rest of its arguments there, and prints the pane's id. The suite reads
# nothing of any backend, so a second adapter runs it unchanged, given a
# BERTH_OPEN_BY_HAND of its own.
#
# Needs: bash, coreutils, jq, berth.
set -uo pipefail

: "${BERTH_BACKEND:?}" "${BERTH_YARD:?}"
: "${BERTH_BACKEND:?}" "${BERTH_YARD:?}" "${BERTH_OPEN_BY_HAND:?}"
unset BERTH_ID

work=$(mktemp -d)
Expand All @@ -35,13 +38,13 @@ refused() { # <what> <command>...
fi
}

field() { # <id> <field>
berth list | jq -r --arg id "$1" --arg field "$2" \
field() { # <id> <field> [--all]
berth list ${3:+"$3"} | jq -r --arg id "$1" --arg field "$2" \
'.[] | select(.id == $id) | .[$field] // "null"'
}

listed() { # <id>
berth list | jq -r --arg id "$1" 'any(.[]; .id == $id)'
listed() { # <id> [--all]
berth list ${2:+"$2"} | jq -r --arg id "$1" 'any(.[]; .id == $id)'
}

# Opens a berth whose command records the environment it was given.
Expand Down Expand Up @@ -222,6 +225,44 @@ settles "a berth running something other than an agent" "$shell" none
check "a berth running something other than an agent holds no agent session" null "$(field "$shell" agent_session)"
berth close "$shell"

echo "# list --all"

by_list() { # [<list arguments>...]
berth list "$@" | jq -cS 'sort_by(.id)'
}

check "list --all lists each berth as list does" "$(by_list)" "$(by_list --all)"

by_hand=$("$BERTH_OPEN_BY_HAND" "$work/claude" --norc --noprofile "$work/stand-in")
check "BERTH_OPEN_BY_HAND prints the id of a pane that is open" open "$(berth state "$by_hand")"
for _ in $(seq 1 200); do
[ "$(field "$by_hand" agent_state --all)" = at-rest ] && break
sleep 0.1
done
check "list --all lists an agent in a pane opened by hand" at-rest "$(field "$by_hand" agent_state --all)"
check "it has no relationship" null "$(field "$by_hand" relationship --all)"
check "it has no parent" null "$(field "$by_hand" parent --all)"
check "it is listed as open" open "$(field "$by_hand" state --all)"
check "list without --all leaves it out" false "$(listed "$by_hand")"
check "list --all is JSON with the port's fields" true "$(berth list --all | jq 'all(.[]; keys == ["agent_session", "agent_state", "id", "label", "parent", "relationship", "state"])')"
check "list takes --all after a yard" true "$(berth list "$BERTH_YARD" --all | jq --arg id "$by_hand" 'any(.[]; .id == $id)')"
check "list takes --all before a yard" true "$(berth list --all "$BERTH_YARD" | jq --arg id "$by_hand" 'any(.[]; .id == $id)')"
berth name "$by_hand" "my own seat"
check "a pane opened by hand takes a label" "my own seat" "$(field "$by_hand" label --all)"

shell_by_hand=$("$BERTH_OPEN_BY_HAND" sleep 600)
check "BERTH_OPEN_BY_HAND opens a second pane" open "$(berth state "$shell_by_hand")"
check "list --all leaves out a pane opened by hand that runs no agent" false "$(listed "$shell_by_hand" --all)"

shows "$by_hand" leave
for _ in $(seq 1 200); do
[ "$(listed "$by_hand" --all)" = false ] && break
sleep 0.1
done
check "list --all leaves out a pane opened by hand once its agent has left" false "$(listed "$by_hand" --all)"
berth close "$by_hand"
berth close "$shell_by_hand"

echo "# close"

berth close "$second_worker"
Expand Down Expand Up @@ -304,7 +345,14 @@ berth close "$apart"

echo "# serve and stop"

in_yards() { # <yard>
berth yards | grep -qxF "$1" && echo true || echo false
}

served="$BERTH_YARD-served"
check "yards lists the yard the suite runs in" true "$(in_yards "$BERTH_YARD")"
check "yards leaves out a yard that is not up" false "$(in_yards "$served")"
refused "yards takes no argument" berth yards "$BERTH_YARD"
berth serve "$served" >"$work/serve.log" 2>&1 &
serving=$!
trap 'kill "$serving" 2>/dev/null' EXIT
Expand All @@ -314,6 +362,7 @@ for _ in $(seq 1 100); do
done
check "serve brings a yard up" 0 "$(berth list "$served" | jq length)"
check "serve stays in the foreground" true "$(kill -0 "$serving" 2>/dev/null && echo true || echo false)"
check "yards lists a yard once it is served" true "$(in_yards "$served")"
refused "a yard that is being served cannot be served again" berth serve "$served"

berth stop "$served"
Expand All @@ -322,6 +371,7 @@ wait "$serving"
check "serve exits 0 once its yard stops" 0 "$?"
trap - EXIT
refused "a stopped yard cannot list its berths" berth list "$served"
check "yards leaves out a yard once it stops" false "$(in_yards "$served")"
berth stop "$served"
check "stopping a stopped yard succeeds" 0 "$?"

Expand Down
18 changes: 17 additions & 1 deletion tests/berth-usage
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,28 @@ berth=(bash "$work/berth")

prints_usage "berth --help" "${berth[@]}" --help
prints_usage "berth -h" "${berth[@]}" -h
for verb in serve stop yard id open name list send-text send-key wait-text read state agent-state close; do
for verb in serve stop yard yards id open name list send-text send-key wait-text read state agent-state close; do
prints_usage "berth $verb --help" "${berth[@]}" "$verb" --help
prints_usage "berth $verb -h" "${berth[@]}" "$verb" -h
done
prints_usage "berth name --help with no backend named" env -u BERTH_BACKEND "${berth[@]}" name --help

refuses "yards takes no argument" "${berth[@]}" yards some-yard
refuses "list refuses an option it does not know" "${berth[@]}" list --every

passes() { # <what> <want> <command>...
local what=$1 want=$2
shift 2
: >"$calls"
"$@"
check "$what" "$want" "$(cat "$calls")"
}

passes "list passes the caller's yard and berths only" "list berths" "${berth[@]}" list
passes "list --all before a yard passes both" "list some-yard all" "${berth[@]}" list --all some-yard
passes "list --all after a yard passes both" "list some-yard all" "${berth[@]}" list some-yard --all
passes "yards reaches the adapter" "yards" "${berth[@]}" yards

refuses "name refuses a label that starts with a dash" "${berth[@]}" name --verbose
refuses "name refuses a dashed label for a named berth" "${berth[@]}" name some-berth -x

Expand Down
18 changes: 18 additions & 0 deletions tests/herdr-open-by-hand
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# Opens a pane in the herdr session BERTH_YARD names, as a person would and
# outside berth, runs the command in its shell, and prints the pane's id. The
# port contract suite takes it as BERTH_OPEN_BY_HAND.
#
# Needs: bash, coreutils, herdr, jq.
set -euo pipefail

: "${BERTH_YARD:?}"
[ $# -gt 0 ] || { echo "usage: herdr-open-by-hand <command>..." >&2; exit 2; }

hand() {
env -u HERDR_SOCKET_PATH herdr --session "$BERTH_YARD" "$@"
}

pane=$(hand workspace create --cwd "$PWD" --no-focus | jq -r .result.root_pane.pane_id)
hand pane run "$pane" "$(printf '%q ' "$@")" >/dev/null
echo "$pane"