diff --git a/bin/berth-herdr-app b/bin/berth-herdr-app index a27e765..8222afa 100755 --- a/bin/berth-herdr-app +++ b/bin/berth-herdr-app @@ -8,7 +8,7 @@ # yard is an hc session. The relationship, the opener, the line of work and # the label live on the terminal as hc labels. # -# Needs: bash, coreutils, flock, herdr-core, jq. +# Needs: bash, coreutils, flock, grep, herdr-core, jq. set -euo pipefail yard=${BERTH_YARD:-${HC_SESSION:-default}} diff --git a/docs/design.md b/docs/design.md index 74bc088..e589b03 100644 --- a/docs/design.md +++ b/docs/design.md @@ -251,6 +251,7 @@ Pull requests are GitHub's. The skills read, wait on and merge them with `gh` an - 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. - For the herdr app, it runs a throwaway herdr-core in the same way, with `HOME` and the XDG folders redirected and every `HC_*` and `HERDR_*` variable unset. - herdr treats a process named `claude` as an agent and reads its state from the screen. herdr-core takes the state Claude Code reports over OSC 7501. The suite drives the states with a stand-in process of that name, not with Claude Code, and the stand-in both draws Claude's screens and makes its reports. +- **sign-on.** Its suite runs against a throwaway yard of each backend, set up as for the contract suite. Its stand-in for Claude Code records its session as Claude Code does in a herdr pane and the mod does in an app terminal, so the suite finds each seat it starts in `berth list` with its agent session. A seat that herdr resumed is tested under herdr alone. - **Choosing a backend.** The front end's usage test covers a host that runs both backends. A caller in an app terminal reaches the app's adapter, one in a herdr pane reaches herdr's, and one in both or in neither is refused. - **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. - **End to end.** One real effort runs on `steward` and `worker`. diff --git a/nix/berth/package.nix b/nix/berth/package.nix index 8b59def..0703d86 100644 --- a/nix/berth/package.nix +++ b/nix/berth/package.nix @@ -1,7 +1,7 @@ # herdr and herdr-core are arguments so that a host can supply its own builds # of them. A host can instead name a herdr binary at run time with # BERTH_HERDR. -{ lib, stdenvNoCC, makeWrapper, bash, coreutils, flock, jq, herdr, herdr-core }: +{ lib, stdenvNoCC, makeWrapper, bash, coreutils, flock, gnugrep, jq, herdr, herdr-core }: stdenvNoCC.mkDerivation { name = "berth"; @@ -25,7 +25,7 @@ stdenvNoCC.mkDerivation { wrapProgram $out/bin/berth-herdr --prefix PATH : ${lib.makeBinPath [ coreutils jq ]} \ --set-default BERTH_HERDR ${herdr}/bin/herdr wrapProgram $out/bin/berth-herdr-state --prefix PATH : ${lib.makeBinPath [ jq ]} - wrapProgram $out/bin/berth-herdr-app --prefix PATH : ${lib.makeBinPath [ coreutils flock jq herdr-core ]} + wrapProgram $out/bin/berth-herdr-app --prefix PATH : ${lib.makeBinPath [ coreutils flock gnugrep jq herdr-core ]} ''; meta.mainProgram = "berth"; diff --git a/nix/sign-on/default.nix b/nix/sign-on/default.nix index 5754dc6..d2ab927 100644 --- a/nix/sign-on/default.nix +++ b/nix/sign-on/default.nix @@ -3,18 +3,22 @@ let sign-on = pkgs.callPackage ./package.nix { }; berth = pkgs.callPackage ../berth/package.nix { }; + + signOnSuite = name: yard: backend: pkgs.runCommand name + { + nativeBuildInputs = [ sign-on berth pkgs.bash pkgs.coreutils pkgs.git pkgs.jq ] ++ backend; + } '' + bash ${yard} bash ${../../tests/sign-on} + touch $out + ''; in { packages.sign-on = sign-on; checks = { - sign-on-herdr = pkgs.runCommand "sign-on-herdr" - { - nativeBuildInputs = [ sign-on berth pkgs.bash pkgs.coreutils pkgs.git pkgs.herdr pkgs.jq ]; - } '' - bash ${../../tests/with-herdr-yard} bash ${../../tests/sign-on} - touch $out - ''; + sign-on-herdr = signOnSuite "sign-on-herdr" ../../tests/with-herdr-yard [ pkgs.herdr ]; + sign-on-herdr-app = signOnSuite "sign-on-herdr-app" ../../tests/with-herdr-app-yard + [ pkgs.flock pkgs.herdr-core ]; sign-on-shellcheck = pkgs.runCommand "sign-on-shellcheck" { nativeBuildInputs = [ pkgs.shellcheck ]; } '' diff --git a/tests/sign-on b/tests/sign-on index f549f6a..17f577c 100755 --- a/tests/sign-on +++ b/tests/sign-on @@ -43,12 +43,16 @@ trust_prompt="Is this a project you created or one you trust?" # A backend knows Claude Code by its process name, so a copy of bash named # claude stands in for it. It shows the gate until it has had the given -# number of Enters, then draws the given screen, as the berth contract's -# stand-in does. +# number of Enters, then draws the given screen and reports its state over +# OSC 7501, as the berth contract's stand-in does. cp "$(command -v bash)" "$work/claude" cat >"$work/stand-in" </dev/null 2>&1 +elif [ -n "\${HC_TERMINAL_ID:-}" ]; then + hc label "\$HC_TERMINAL_ID" "claude.session=\$session" >/dev/null 2>&1 +fi # A screen that buries the gate under something else, so that wait-text # still finds the gate in recent output while the screen shows another. if [ "\$screen" = mystery ]; then printf 'Loading development channels\n❯ 1. $gate\n 2. Exit\n' - for _ in \$(seq 1 200); do echo; done + scroll_away fi if [ "\$enters" -gt 0 ]; then printf 'Loading development channels\n❯ 1. $gate\n 2. Exit\n' @@ -74,15 +86,12 @@ if [ "\$enters" -gt 0 ]; then echo x >>"\$KEYS" enters=\$((enters - 1)) done - # Scrolled away rather than cleared, so the gate stays in the recent - # output that wait-text searches. - for _ in \$(seq 1 200); do echo; done fi while :; do - printf '\e[H\e[2J' + scroll_away case \$screen in - rest) printf '\e]0;✳ Claude\a'; prompt_box ;; - turn) printf '\e]0;⠂ Claude\a✻ Thinking… (esc to interrupt)\n' ;; + rest) reports idle; printf '\e]0;✳ Claude\a'; prompt_box ;; + turn) reports working; printf '\e]0;⠂ Claude\a✻ Thinking… (esc to interrupt)\n' ;; trust) printf '$trust_prompt\n❯ No, exit\n Yes, I trust this folder\n' ;; mystery) printf 'Select a theme\n❯ 1. Dark\n 2. Light\n' ;; esac @@ -101,7 +110,7 @@ printf '%s\n' "\$@" >"$work/$1/args" env >"$work/$1/env" pwd >"$work/$1/pwd" export KEYS="$work/$1/keys" -exec "$work/claude" --norc --noprofile "$work/stand-in" $2 $3 +exec "$work/claude" --norc --noprofile "$work/stand-in" $2 $3 session-$1 EOF chmod +x "$work/$1/seat" printf '%s\n' "$work/$1/seat" @@ -157,6 +166,12 @@ check "the command takes the launch's threshold for the compaction mod" 70 \ check "the seat does not carry the mark" false \ "$(grep -qx "$mark" "$work/gated/env" && echo true || echo false)" check "sign-on leaves no process of its own running" "" "$(marked)" +session_of() { # + berth list | jq -r --arg id "$1" '.[] | select(.id == $id) | .agent_session' +} +has_session() { [ "$(session_of "$1")" = "$2" ]; } +eventually has_session "$id" session-gated +check "the seat is listed with its agent session" session-gated "$(session_of "$id")" opener=$id echo "# no gate configured" @@ -332,52 +347,57 @@ check "the gate took two Enters and is gone from the screen" false \ "$(contains "$(berth read "$id")" "$gate")" check "and nothing went to the system log" false "$(test -s "$work/logger.out" && echo true || echo false)" -echo "# a seat that herdr resumed" - -# herdr restores a seat by typing its claude --resume into a fresh shell in -# the seat's pane, whose environment holds HERDR_PANE_ID and HERDR_SESSION and -# nothing that sign-on or berth set. The caller starts --pass-gate in the -# background from there, before Claude Code has drawn its gate. HERDR_SESSION -# is set only in a pane of a named session, so this runs in one. -resumed=resumed -env -u BERTH_YARD -u BERTH_ID berth serve "$resumed" >"$work/serve.log" 2>&1 & -serving=$! -trap 'kill "$serving" 2>/dev/null' EXIT -eventually berth list "$resumed" >/dev/null 2>&1 - -resume() { # - local pane - pane=$(herdr --session "$resumed" workspace create --cwd "$work/project" --no-focus | - jq -r .result.root_pane.pane_id) - herdr --session "$resumed" pane run "$pane" "(env -u BERTH_BACKEND -u BERTH_YARD -u BERTH_ID \ -PATH='$work/log:$PATH' SIGN_ON_GATE='$gate' SIGN_ON_START_TIMEOUT=$2 \ +if [ "$BERTH_BACKEND" = herdr ]; then + echo "# a seat that herdr resumed" + + # herdr restores a seat by typing its claude --resume into a fresh shell in + # the seat's pane, whose environment holds HERDR_PANE_ID and HERDR_SESSION and + # nothing that sign-on or berth set. The caller starts --pass-gate in the + # background from there, before Claude Code has drawn its gate. HERDR_SESSION + # is set only in a pane of a named session, so this runs in one. + resumed=resumed + env -u BERTH_YARD -u BERTH_ID berth serve "$resumed" >"$work/serve.log" 2>&1 & + serving=$! + trap 'kill "$serving" 2>/dev/null' EXIT + eventually berth list "$resumed" >/dev/null 2>&1 + + # The pane echoes the command typed into it, so the gate's text comes from a + # file rather than the command, or wait-text would find it there. + printf '%s' "$gate" >"$work/gate" + resume() { # + local pane + pane=$(herdr --session "$resumed" workspace create --cwd "$work/project" --no-focus | + jq -r .result.root_pane.pane_id) + herdr --session "$resumed" pane run "$pane" "(env -u BERTH_BACKEND -u BERTH_YARD -u BERTH_ID \ +PATH='$work/log:$PATH' SIGN_ON_GATE=\"\$(cat '$work/gate')\" SIGN_ON_START_TIMEOUT=$2 \ sign-on --pass-gate; echo \$? >'$work/$1.status') & $(seat "$1" rest "$3")" >/dev/null - printf '%s\n' "$pane" -} - -finished() { # - eventually test -s "$work/$1.status" - cat "$work/$1.status" -} - -in_resumed() { env -u BERTH_ID BERTH_YARD=$resumed berth "$@"; } - -pane=$(resume woken 30 2) -check "--pass-gate from a resumed seat's own pane succeeds" 0 "$(finished woken)" -check "the pane is in a named session" "$resumed" "$(recorded woken HERDR_SESSION)" -check "the gate took two Enters" 2 "$(keys woken)" -check "and is gone from the pane's screen" false "$(contains "$(in_resumed read "$pane")" "$gate")" - -pane=$(resume ungated 3 0) -check "--pass-gate on a resumed seat that shows no gate fails" 1 "$(finished ungated)" -eventually grep -q "did not show the gate" "$work/logger.out" -check "and says why in the system log" true "$(contains "$(cat "$work/logger.out")" "did not show the gate")" -check "under the tag sign-on" true "$(grep -qx -- "-t sign-on" "$work/logger.args" && echo true || echo false)" -check "and names the seat's berth" true "$(contains "$(cat "$work/logger.out")" "the berth $pane ")" -check "and does not advise closing it" false "$(contains "$(cat "$work/logger.out")" "close")" -check "and writes nothing on the seat's screen" false "$(contains "$(in_resumed read "$pane")" "sign-on")" - -kill "$serving" + printf '%s\n' "$pane" + } + + finished() { # + eventually test -s "$work/$1.status" + cat "$work/$1.status" + } + + in_resumed() { env -u BERTH_ID BERTH_YARD=$resumed berth "$@"; } + + pane=$(resume woken 30 2) + check "--pass-gate from a resumed seat's own pane succeeds" 0 "$(finished woken)" + check "the pane is in a named session" "$resumed" "$(recorded woken HERDR_SESSION)" + check "the gate took two Enters" 2 "$(keys woken)" + check "and is gone from the pane's screen" false "$(contains "$(in_resumed read "$pane")" "$gate")" + + pane=$(resume ungated 3 0) + check "--pass-gate on a resumed seat that shows no gate fails" 1 "$(finished ungated)" + eventually grep -q "did not show the gate" "$work/logger.out" + check "and says why in the system log" true "$(contains "$(cat "$work/logger.out")" "did not show the gate")" + check "under the tag sign-on" true "$(grep -qx -- "-t sign-on" "$work/logger.args" && echo true || echo false)" + check "and names the seat's berth" true "$(contains "$(cat "$work/logger.out")" "the berth $pane ")" + check "and does not advise closing it" false "$(contains "$(cat "$work/logger.out")" "close")" + check "and writes nothing on the seat's screen" false "$(contains "$(in_resumed read "$pane")" "sign-on")" + + kill "$serving" +fi echo "# failures" @@ -398,7 +418,7 @@ fails "a gate that does not clear" "$gate" \ SIGN_ON_CLAUDE="$(seat stuck rest 100)" SIGN_ON_GATE="$gate" \ sign-on --as independent --cwd "$work/project" "$prompt" -fails "a seat beside a caller in no pane" pane \ +fails "a seat beside a caller in no pane" "neither a berth" \ SIGN_ON_CLAUDE="$(seat nowhere rest 0)" sign-on --as independent --beside --cwd "$work/project" "$prompt" check "and no berth opens for it" false "$(test -f "$work/nowhere/args" && echo true || echo false)"