diff --git a/.github/workflows/scripts.yml b/.github/workflows/scripts.yml index c3136fc..b690a59 100644 --- a/.github/workflows/scripts.yml +++ b/.github/workflows/scripts.yml @@ -59,5 +59,89 @@ jobs: exit 1 fi echo "$count scripts answer --help" + - name: lock.sh holds one run at a time and gives the lock back + run: | + export PLADDER_LOCK_DIR="$RUNNER_TEMP/locks" + mkdir -p "$PLADDER_LOCK_DIR" + L=scripts/lock.sh + LOCK="$PLADDER_LOCK_DIR/pladder-ane.lock" + fail() { echo "::error file=$L::$*"; exit 1; } + taken() { + for _ in $(seq 50); do + if grep -q '^command-pid=' "$LOCK/owner" 2>/dev/null; then return 0; fi + sleep 0.1 + done + fail "the lock was never taken" + } + + s=0; "$L" ane -- sh -c 'exit 7' || s=$? + [[ $s -eq 7 ]] || fail "a command's exit 7 came back as $s" + [[ ! -e "$LOCK" && "$("$L" --status ane)" == "ane: free" ]] || fail "a failed command left the lock held" + + "$L" ane -- sh -c 'echo a >>"$1"; sleep 2; echo b >>"$1"' sh "$RUNNER_TEMP/order" & first=$! + taken + status=$("$L" --status ane) + for part in "held by $PWD" "pid $first" "sh -c echo a"; do + grep -qF "$part" <<<"$status" || fail "--status says \"$status\", without \"$part\"" + done + s=0; "$L" --wait 0 ane -- true || s=$? + [[ $s -eq 75 ]] || fail "--wait 0 on a held lock exited $s, not 75" + "$L" ane -- sh -c 'echo c >>"$1"; echo d >>"$1"' sh "$RUNNER_TEMP/order" + wait "$first" + [[ "$(paste -s -d ' ' "$RUNNER_TEMP/order")" == "a b c d" ]] || fail "two runs interleaved: $(paste -s -d ' ' "$RUNNER_TEMP/order")" + + sh -c 'exit 0' & dead=$!; wait "$dead" + mkdir "$LOCK" + printf 'pid=%s\nstarted=Mon Jan 1 00:00:00 2000\n' "$dead" >"$LOCK/owner" + grep -q "left by pid $dead (gone)" <<<"$("$L" --status ane)" || fail "--status missed a gone holder" + "$L" --wait 5 ane -- true || fail "a gone holder's lock was not taken over" + [[ ! -e "$LOCK" && ! -e "$LOCK.takeover" ]] || fail "the takeover left a lock behind" + + mkdir "$LOCK" + echo "measuring by hand" >"$LOCK/owner" + s=0; "$L" --wait 2 ane -- true || s=$? + [[ $s -eq 75 && -f "$LOCK/owner" ]] || fail "a lock taken by hand was taken over (exit $s)" + grep -q "held by hand: measuring by hand" <<<"$("$L" --status ane)" || fail "--status missed a lock taken by hand" + rm -r "$LOCK" + + "$L" ane -- sleep 2 & held=$! + taken + kill -TERM "$held" + s=0; wait "$held" || s=$? + [[ $s -eq 143 && ! -e "$LOCK" ]] || fail "SIGTERM exited $s and left the lock: $(ls "$PLADDER_LOCK_DIR")" + + timeout 10 "$L" screen -- "$L" screen -- true || fail "a lock.sh inside the same lock did not just run" + [[ "$("$L" ane -- sh -c 'printf "%s\n" "$1"' sh --help)" == "--help" ]] || fail "--help after -- did not reach the command" + + mkdir "$LOCK" "$LOCK.takeover" + printf 'pid=%s\nstarted=Mon Jan 1 00:00:00 2000\n' "$dead" >"$LOCK/owner" + s=0; timeout 10 "$L" --wait 2 ane -- true || s=$? + [[ $s -eq 75 ]] || fail "a fresh takeover left by another run did not end in the wait's timeout (exit $s)" + touch -t 200001010000 "$LOCK.takeover" + "$L" --wait 5 ane -- true || fail "a takeover left over a minute ago was not cleared" + [[ ! -e "$LOCK" && ! -e "$LOCK.takeover" ]] || fail "a stale takeover left a lock behind" + echo "lock.sh: exit codes, --status, order, --wait 0, takeover, by hand, SIGTERM, nesting, a stale takeover" + - name: test-copy.sh picks a chord the copy in use never reacts to + run: | + fail() { echo "::error file=scripts/test-copy.sh::$*"; exit 1; } + home() { # + h="$RUNNER_TEMP/homes/$1" + mkdir -p "$h/Library/Application Support/Pladder" + if [[ -n "$2" ]]; then printf '%s' "$2" >"$h/Library/Application Support/Pladder/settings.json"; fi + echo "$h" + } + expect() { # + out=$(HOME=$(home "$1" "$2") scripts/test-copy.sh --dry-run) || fail "$1: exited non-zero" + grep -qF "\"hotkey\":{\"keyCodes\":[$3]}" <<<"$out" || fail "$1: wanted [$3], got $out" + } + expect none "" "17,55,58,59" + expect defaults '{"hotkey":{"keyCodes":[49,58]},"toggleHotkey":{"keyCodes":[]},"submitKey":{"keyCodes":[9]}}' "17,55,58,59" + expect fn-hold '{"hotkey":{"keyCodes":[63]},"toggleHotkey":{"keyCodes":[49,58]},"submitKey":{"keyCodes":[59]}}' "17,55,58,59" + expect right-command '{"hotkey":{"keyCodes":[54]}}' "17,56,58,59" + expect taken '{"hotkey":{"keyCodes":[17,55,58,59]}}' "5,55,58,59" + for bad in '{"hotkey":' '{"hotkey":{"kind":"modifier","keyCode":54}}' '[]'; do + if HOME=$(home bad "$bad") scripts/test-copy.sh --dry-run >/dev/null 2>&1; then fail "launched on $bad"; fi + done + echo "test-copy.sh: chords for no file, the defaults, fn, Right Command and a taken T; unreadable files refused" - name: ShellCheck run: shellcheck scripts/*.sh diff --git a/CLAUDE.md b/CLAUDE.md index e2bacaf..852f935 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -26,6 +26,8 @@ Before and after any change that touches the code between `recordingStopped` and swift build # debug build of everything swift test # unit tests, well under a second ./scripts/bundle.sh [--run] [--install] [--dev-modes] # release build → dist/Pladder.app, signed +./scripts/test-copy.sh [--stop | --dry-run] # bundle dist/Pladder.app and launch it beside the copy in use, with its own settings file and chord; --stop stops it +./scripts/lock.sh [--wait ] -- # run a command holding the screen, or the Neural Engine and GPU, one worktree at a time; --status names the holder ./scripts/release.sh # Developer ID, notarised, stapled → dist/release/Pladder.dmg; publishes nothing ./scripts/changelog.sh [] # the release notes release.yml publishes, from the commit subjects ./scripts/appcast.sh # sign the DMG for Sparkle and print the release's appcast.xml @@ -120,11 +122,11 @@ swift run -c release pladder-cli polish-set docs/polish-set.json --model s1-mini The developer dictates with a running Pladder all day, often into Claude sessions, and several worktrees may each have their own `dist/Pladder.app`. -- Never `pkill -x Pladder` or `killall Pladder`: that also kills the copy in use, mid-recording, and the text is lost. Stop only the copy you launched: `pkill -f "$PWD/dist/Pladder.app"`. SIGTERM quits the normal way, so a copy stopped like this gives back the speakers and the clipboard first. `bundle.sh --install` stops only `/Applications/Pladder.app`, and CI fails on a script that kills every copy. +- Never `pkill -x Pladder` or `killall Pladder`: that also kills the copy in use, mid-recording, and the text is lost. Stop only the copy you launched: `scripts/test-copy.sh --stop` for a test copy, `pkill -f "$PWD/dist/Pladder.app"` for one you started some other way. SIGTERM quits the normal way, so a copy stopped like this gives back the speakers and the clipboard first. `bundle.sh --install` stops only `/Applications/Pladder.app`, and CI fails on a script that kills every copy. - Do not post synthetic hotkey events unless the user has asked for a live UI test. Every running copy reacts to them, so they start, cut short, or paste the user's recordings. - To see an overlay change, run `scripts/overlay-demo.sh` and read its contact sheets. It drives the real coordinator and pill with stand-ins for the engine, microphone, paste and hotkey, so nothing reaches a running copy. - Do not edit `~/Library/Application Support/Pladder/settings.json`; it is the live configuration. -- A copy launched for testing gets its own settings file with `PLADDER_SETTINGS_PATH=/tmp//settings.json "$PWD/dist/Pladder.app/Contents/MacOS/Pladder"`, and a different chord from the copy in use, so the two never fire together. Launched as the bare binary so the environment reaches it; its command line still contains `$PWD/dist/Pladder.app`, so the `pkill -f` above stops it. +- A copy for testing comes from `scripts/test-copy.sh`: it bundles `dist/Pladder.app`, launches the bare binary with a settings file of its own, `/tmp//settings.json`, and a chord the copy in use never reacts to, chosen from the live settings file, which it only reads, and prints the PID. Stop it with `scripts/test-copy.sh --stop` as soon as you are done with it; that stops this worktree's test copy and nothing else. - To test an update, the copy needs a bundle ID of its own (Sparkle's defaults and the grants are per bundle ID), `LSEnvironment` in its Info.plist for `PLADDER_SETTINGS_PATH` (Sparkle relaunches it through LaunchServices, without your shell's environment), a feed on `http://127.0.0.1` and a throwaway key, signed with `appcast.sh` and `SPARKLE_ED_KEY`; never the real key. ## Verify @@ -136,19 +138,27 @@ Proof is what you saw in this session: a command's output, a picture, a contact | Any Swift | `swift build`, with no new warning, and `swift test` | the terminal | | Core logic: the coordinator, a tracker, the settings | a test that fails without the change | the terminal | | A processor, the dictionary | `swift run -c release pladder-cli