Artifact 2937f21d295d816463c88c01c7f70f3a05a88b9ef7a942728f7e178ecb79b1f7:
- File
test/tui/tui.sh
— part of check-in
[254c07633a]
at
2026-11-23 18:37:37
on branch rc-names
— Conversation titles everywhere, and a phone list that tells them apart
A conversation's title is its name, else the first line of its first prompt (PIC_TITLE_SQL, with pic_line in SQL). pic sessions, its --json, the terminal's /resume picker and footer use it. pic remote's sessions list carries optional title, preview (the first line of the last message along the path) and state (running, idle, attention). (user: dmestas, size: 82070) [annotate] [blame] [check-ins using]
#!/bin/sh # The terminal interface, on a pseudo-terminal against a fake server: a # reply streams and lands in the transcript, tool calls show a header and # result, the footer counts tokens, Esc cancels a request, commands work # (/name, /resume with its picker, unknown ones), pic tui -c shows the # history, a resize is survived, Ctrl-D quits. These run in regular mode (the # inline renderer, whose redraws they read); fullscreen has its own cases at # the end. set -eu PIC=${PIC:?set PIC to the pic binary} SERVER=${SERVER:?set SERVER to the fake_server binary} DRIVE=${DRIVE:?set DRIVE to the pty_drive binary} FAULTS=yes # --no-faults (the release build): the sign-in cases, which need the dev build's test seams, are skipped [ "${1:-}" = --no-faults ] && FAULTS=no S=$(cd "$(dirname "$0")/../pic/streams" && pwd) T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tui.XXXXXX") spid= opid= cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; [ -n "$opid" ] && kill "$opid" 2>/dev/null || :; rm -rf -- "$T"; } trap cleanup EXIT HUP INT TERM fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; } mkdir "$T/ws" "$T/log" "$T/home" export HOME="$T/home" ANTHROPIC_API_KEY=pic-tui-test-key PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline export T # pty_drive scripts name files as $T/... export PIC_NO_NATIVE_CLIPBOARD=1 # never the real clipboard unset PIC_TEST_PASSPHRASE OPENAI_API_KEY GROQ_API_KEY # 1 text, 2 tool call, 3 text, 4 hang (cancelled), 5 hang (quit), then text "$SERVER" "$T/port" "$T/log" 120 "sse:$S/text.sse" "sse:$S/thinking_tool.sse" "sse:$S/text.sse" "hang:$S/text.sse:200" \ "hang:$S/text.sse:200" "sse:$S/text.sse" & spid=$! i=0 while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000},{"id":"r","contextWindow":200000,"reasoning":true}]}}}' \ "$(cat "$T/port")" > "$T/models.json" drive() { # NAME: run pic with the script on stdin name=$1 shift "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode regular --db "$T/j" --workspace "$T/ws" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } drive first <<'EOF' expect pic fake/m send hello\r expect Hello, "world" expect ↑25 ↓12 send write it\r expect ✓ write t.txt expect > send /name main\r expect named main send /bogus\r expect unknown command /bogus size 60 18 wait 300 send \x04\x04 expect pic: conversation EOF [ -f "$T/ws/t.txt" ] || fail "tool: file not written" printf 'PASS: a reply streams, a tool call shows its result, commands work, Ctrl-D quits\n' drive cancel <<'EOF' expect pic fake/m send wait for me\r expect Working waitfile $T/log/4.req send \e expect Operation aborted send \x04\x04 EOF "$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "cancel: integrity" printf 'PASS: Esc cancels the running request\n' drive resume tui -c <<'EOF' expect continuing expect wait for me send /resume\r expect Resume a conversation expect 1 wait for me expect 2 main send \e[B wait 200 send \r expect continuing expect hello expect write it send \x04\x04 EOF printf 'PASS: pic tui -c shows the history; /resume picks a conversation\n' # Quitting while a request is stuck: Ctrl-C twice leaves promptly, the # request is cancelled and the journal is whole. drive quit <<'EOF2' expect pic fake/m send stuck\r expect Working waitfile $T/log/5.req send \x03 wait 100 send \x03 expect pic: conversation EOF2 "$PIC" --db "$T/j" --workspace "$T/ws" log | grep -q '(request cancelled)' || fail "quit: request not cancelled" "$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "quit: integrity" printf 'PASS: quitting during a stuck request cancels it and exits\n' # Slash menu and model picker: /mod completes with Tab; /model completes a # model name; Alt+P opens the picker (Ctrl+L redraws, as in Claude Code), typing filters, Enter picks and saves it for the next conversation, Ctrl+S # says so; /copy copies the last reply. drive models <<'EOF2' expect pic fake/m send /mod expect select a model send \t send haiku-5-5 expect claude-haiku-5-5 [anthropic] send \r wait 200 send \r expect model anthropic/claude-haiku-5-5 send \ep expect Select a model send fake expect m [fake] send \r expect model fake/m send \ep expect Select a model send fake expect ✓ m [fake] send \x13 expect already saved send /copy\r expect Error: No agent messages to copy yet. send \x04\x04 EOF2 [ "$("$PIC" --db "$T/j" --workspace "$T/ws" settings defaultModel)" = fake/m ] || fail "models: default not saved" printf 'PASS: the slash menu completes commands and models; the picker filters, picks and saves a default\n' # The commands that keep state in ~/.pic/pic.sqlite. A fake gh stands in for # GitHub: it records what it is given and prints a URL. mkdir "$T/bin" cat > "$T/bin/gh" <<'GH' #!/bin/sh printf '%s\n' "$*" >> "$GH_LOG.args" cat > "$GH_LOG.stdin" echo https://gist.example/secret123 GH chmod +x "$T/bin/gh" export GH_LOG="$T/gh" PATH="$T/bin:$PATH" pic() { "$PIC" --db "$T/j" --workspace "$T/ws" "$@"; } # /settings: rows change settings in the user database. drive settings <<'EOF' expect pic fake/m send /settings\r expect Default thinking level send \e[B wait 100 send \r expect high send \e[B wait 100 send \r expect Pi session files true send \e[B wait 100 send \r expect Hide thinking true send \e[B\e[B\e[B\e[B\e[B\e[B\e[B wait 100 send \r expect Tool execution sequential send \e[B\e[B wait 100 send \r expect Auto-compact false send \e wait 400 send \x04\x04 EOF [ "$(pic settings defaultThinkingLevel)" = high ] || fail "settings: thinking level not saved" [ "$(pic settings compaction)" = '{"enabled":false}' ] || fail "settings: Auto-compact not saved: $(pic settings compaction)" pic settings --unset compaction [ "$(pic settings toolExecution)" = sequential ] || fail "settings: toolExecution not saved" pic settings --unset toolExecution [ "$(pic settings piSessionFiles)" = 1 ] || fail "settings: piSessionFiles not saved" [ "$(pic settings hideThinking)" = true ] || fail "settings: hideThinking not saved" pic settings --unset piSessionFiles pic settings --unset hideThinking printf 'PASS: /settings changes the default thinking level, toggles, Tool execution and Auto-compact, stored in the user database\n' # /thinking and Shift+Tab: the session's level, only the model's levels. drive thinking --model fake/r <<'EOF' expect pic fake/r expect • high send /thinking low\r expect thinking level: low expect • low send \e[Z expect thinking level: medium send /thinking xhigh\r expect does not take thinking level xhigh send /thinking\r expect Thinking level send \e[B wait 100 send \r expect thinking level: high send \e[Z expect thinking level: off send /thinking low\r expect thinking level: low send what\r expect Hello, "world" send \x04\x04 EOF grep -q '"budget_tokens":2048' "$(ls "$T/log"/* | sort -V | tail -1)" || fail "thinking: the request did not carry the low level" drive thinking-none <<'EOF' expect pic fake/m send \e[Z expect does not support thinking levels send /thinking low\r expect does not support thinking levels send \x04\x04 EOF pic settings --unset defaultThinkingLevel printf 'PASS: /thinking and Shift+Tab pick the session level among the model levels, and requests carry it\n' # The prompt and tool options given to the terminal reach every request child. drive flags --system-prompt 'TUI system only' --append-system-prompt 'TUI extra words' --tools read,write --exclude-tools read <<'EOF' expect pic fake/m send flagged\r expect Hello, "world" send \x04\x04 EOF last=$(ls "$T/log"/*.req | sort -V | tail -1) grep -qF '"system":[{"type":"text","text":"TUI system only' "$last" || fail "flags: system prompt not replaced: $(grep -o '"system".*' "$last" | cut -c1-200)" grep -qF '<addendum>\nTUI extra words\n</addendum>' "$last" || fail "flags: no addendum" grep -qF '"name":"write"' "$last" || fail "flags: --tools write not sent" if grep -qF '"name":"read"' "$last" || grep -qF '"name":"bash"' "$last"; then fail "flags: unselected tools were sent"; fi drive flags-none --no-tools <<'EOF' expect pic fake/m send flagged again\r expect Hello, "world" send \x04\x04 EOF last=$(ls "$T/log"/*.req | sort -V | tail -1) if grep -qF '"name":"read"' "$last"; then fail "flags: --no-tools still sent tools"; fi printf 'PASS: --system-prompt, --append-system-prompt and the tool options reach the terminal requests\n' # /scoped-models: the models Ctrl+P cycles. drive scoped <<'EOF' expect pic fake/m send /scoped-models\r expect Scoped models for Ctrl+P send fake expect m [fake] send \r send \e[B wait 100 send \r send \x13 expect Ctrl+P cycles 2 models send \x10 expect model fake/r send \x10 expect model fake/m send \x04\x04 EOF [ "$(pic scoped-models | sort | tr '\n' ' ')" = 'fake/m fake/r ' ] || fail "scoped: stored $(pic scoped-models | tr '\n' ' ')" drive scoped-clear <<'EOF' expect pic fake/m send /scoped-models\r expect Scoped models for Ctrl+P send fake expect m [fake] send \r send \e[B wait 100 send \r send \x13 expect no scope send \x04\x04 EOF [ -z "$(pic scoped-models)" ] || fail "scoped: not cleared by toggling off" printf 'PASS: /scoped-models stores the chosen models and Ctrl+P cycles only them\n' # /trust, /bug, /changelog, /login, /reload. printf 'be kind\n' > "$T/ws/AGENTS.md" drive misc <<'EOF' expect pic fake/m send /trust\r expect Workspace trust send \e[B wait 100 send \r expect saved trust decision: untrusted send /trust yes\r expect saved trust decision: trusted send /bug the footer wraps\r expect nothing was sent send /bug\r expect /bug needs a description send /changelog\r expect What's New send /login nosuch\r expect unknown provider nosuch send /logout\r expect No stored credentials to remove send /reload\r expect project instructions found send \x04\x04 EOF rm -f "$T/ws/AGENTS.md" [ "$(pic trust)" = trusted ] || fail "misc: trust not stored" pic bug --list | grep -q 'the footer wraps' || fail "misc: bug not stored" printf 'PASS: /trust, /bug, /changelog, /login, /logout and /reload\n' # /import, /tree, /export and /share on an imported conversation. cat > "$T/pi.jsonl" <<'JSONL' {"type":"session","version":3,"id":"abc","timestamp":"2026-01-01T00:00:00.000Z","cwd":"/x"} {"type":"message","id":"m1","parentId":null,"message":{"role":"user","content":"What is 2 + 2?"}} {"type":"message","id":"m2","parentId":"m1","message":{"role":"assistant","content":[{"type":"text","text":"It is 4."}]}} {"type":"message","id":"m3","parentId":"m2","message":{"role":"user","content":"And 3 + 3?"}} {"type":"message","id":"m4","parentId":"m3","message":{"role":"assistant","content":[{"type":"text","text":"It is 6."}]}} JSONL before=$(pic sessions | wc -l | tr -d ' ') drive import <<EOF expect pic fake/m send /import $T/pi.jsonl\r expect imported expect What is 2 + 2? expect It is 6. send /export $T/out.html\r expect written to send /share\r expect Share this conversation as a secret GitHub gist wait 200 send n expect not shared send /share\r expect Share this conversation send \e wait 400 send /share\r expect Share this conversation wait 200 send y expect shared as a secret gist expect https://gist.example/secret123 send /tree\r expect Conversation tree expect And 3 + 3? send \e[A wait 100 send \r expect Summarize branch? expect Summarize with custom prompt send \r expect branched: now in expect What is 2 + 2? send /tree\r expect Conversation tree expect is the path you are on send \e[B wait 100 send \r expect Summarize branch? send \r expect It is 6. send /tree\r expect Conversation tree expect is the path you are on send \e[A wait 100 send \r expect Summarize branch? send \e expect Conversation tree expect is the path you are on wait 100 send \r expect Summarize branch? send \e[B wait 100 send \r expect branched: now in expect [branch] expect Branch summary send \x04\x04 expect pic: conversation EOF [ "$(pic sessions | wc -l | tr -d ' ')" = $((before + 3)) ] || fail "import: expected the import, its fork and the summarized one" pic dump | grep -q '^entry .* branch_summary | The user explored a different conversation branch' || fail "tree: no branch summary" grep -q 'Create a structured summary of this conversation branch' "$(ls "$T/log"/*.req | sort -V | tail -1)" || fail "tree: the summary request did not use Pi's branch prompt" grep -q 'It is 6.' "$T/out.html" || fail "export: page lacks the answer" [ "$(wc -l < "$T/gh.args" | tr -d ' ')" = 1 ] || fail "share: gh ran $(wc -l < "$T/gh.args") times, want 1 (only after the yes)" [ "$(cat "$T/gh.args")" = "gist create -" ] || fail "share: gh got $(cat "$T/gh.args")" grep -q '<!doctype html>' "$T/gh.stdin" || fail "share: gh was not given the page" printf 'PASS: /import, /export, /tree (branching, with and without a summary) and /share (only after a yes) work\n' # Prompt templates: the slash menu lists them with their hint and description, # /name ARGS sends the expansion, a built-in command is never shadowed, and # /reload picks up the project's templates once the workspace is trusted. mkdir -p "$HOME/.pic/prompts" "$T/ws/.pic/prompts" printf -- '---\ndescription: Say hello\nargument-hint: <who>\n---\nSay hello to $1.\n' > "$HOME/.pic/prompts/greet.md" printf 'shadowed body\n' > "$HOME/.pic/prompts/bug.md" printf 'Project late body\n' > "$T/ws/.pic/prompts/late.md" pic trust no > /dev/null reqs=$(ls "$T/log" | wc -l | tr -d ' ') drive prompts <<'EOF2' expect pic fake/m send /gre expect <who> — Say hello send \r send Ada\r expect /greet Ada expect Hello, "world" send /bug shadow check\r expect nothing was sent send /reload\r expect 2 prompt templates send /trust yes\r expect saved trust decision: trusted send /reload\r expect 3 prompt templates send /late\r expect /late expect Hello, "world" send /nosuch\r expect unknown command /nosuch send \x04\x04 EOF2 [ "$(ls "$T/log" | wc -l | tr -d ' ')" = $((reqs + 2)) ] || fail "prompts: $(ls "$T/log" | wc -l) requests, want $((reqs + 2))" grep -q '"text":"Say hello to Ada."' "$T/log/$((reqs + 1)).req" || fail "prompts: /greet Ada not expanded: $(grep -o '"messages".*' "$T/log/$((reqs + 1)).req" | cut -c1-200)" grep -q '"text":"Project late body"' "$T/log/$((reqs + 2)).req" || fail "prompts: /late not expanded: $(grep -o '"messages".*' "$T/log/$((reqs + 2)).req" | cut -c1-200)" pic bug --list | grep -q 'shadow check' || fail "prompts: /bug was shadowed by a template" pic trust no > /dev/null printf 'PASS: the slash menu lists prompt templates; /name ARGS sends the expansion; built-ins win; /reload re-reads them\n' # Pi parity, round 1: tool blocks, thinking, history, footer, retries, model keys. # A second fake server with its own script; the connection order is the test order. V=$(cd "$(dirname "$0")/streams" && pwd) mkdir "$T/log2" "$T/ws2" "$T/ws2/.git" printf 'ref: refs/heads/feature\n' > "$T/ws2/.git/HEAD" printf 'alpha\nbeta\ngamma\n' > "$T/ws2/e.txt" "$SERVER" "$T/port2" "$T/log2" 240 "sse:$V/edit.sse" "sse:$V/done.sse" "sse:$V/bash.sse" "hang:$V/done.sse:200" \ "sse:$V/thinking_text.sse" "sse:$V/thinking_text.sse" "sse:$V/length.sse" "cut:$V/done.sse:420" "hang:$V/done.sse:200" \ "cut:$V/done.sse:420" "cut:$V/done.sse:420" "cut:$V/done.sse:420" "hang:$V/done.sse:200" "sse:$V/done.sse" & spid2=$! trap 'kill "$spid2" 2>/dev/null || :; cleanup' EXIT HUP INT TERM i=0 while [ ! -s "$T/port2" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server 2"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000},{"id":"r","contextWindow":200000,"reasoning":true}]}}}' \ "$(cat "$T/port2")" > "$T/models2.json" export PIC_MODELS_FILE="$T/models2.json" drive2() { # NAME [pic args]: the second journal and workspace name=$1 shift "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode regular --db "$T/j2" --workspace "$T/ws2" --model fake/r "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } pic2() { "$PIC" --db "$T/j2" --workspace "$T/ws2" "$@"; } # An edit is a coloured diff; the block says it took time; the footer has the # branch, the conversation name and the token counts. drive2 edit <<'EOF2' expect pic fake/r send fix beta\r expect ✓ edit e.txt expect -2 beta expect +2 BETA expect +3 beta two expect Took expect All done. expect ↑60 ↓40 size 200 24 wait 300 send /name tester\r expect named tester expect (feature) • tester send \x04\x04 EOF2 grep -q '^BETA$' "$T/ws2/e.txt" && grep -q '^beta two$' "$T/ws2/e.txt" || fail "edit: file not edited" perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g' "$T/edit.tx" | grep -q 'ctrl+o to expand' && fail "edit: a short diff was collapsed" # Pi's toolDiffRemoved and toolDiffAdded (the red and green of its dark theme), in 256 colours or truecolor by COLORTERM grep -aqE $'\e\\[38;(5;174|2;234;127;129)m-2 beta' "$T/edit.tx" || fail "edit: removed line not Pi's toolDiffRemoved" grep -aqE $'\e\\[38;(5;72|2;104;183;141)m[+]2 BETA' "$T/edit.tx" || fail "edit: added line not Pi's toolDiffAdded" printf 'PASS: an edit shows as a coloured diff with its duration; the footer shows branch, name and tokens\n' # Long output is collapsed with a hint; Ctrl+O expands the block while it is # still in the live area, and the block is committed in that state. drive2 collapse <<'EOF2' expect pic fake/r send list\r expect 7 earlier lines, ctrl+o to expand expect Took wait 700 send \x0f expect 101 expect tools expanded send \e expect Operation aborted send \x04\x04 EOF2 printf 'PASS: tool output is collapsed with a hint, and Ctrl+O expands blocks still live\n' # Thinking stays in the transcript; Ctrl+T hides it behind a one-line marker # and saves the setting; the setting is read when a reply is committed. drive2 thinking <<'EOF2' expect pic fake/r send ponder\r expect pondering the answer carefully expect Done thinking. expect ↑30 ↓20 wait 400 send \x14 expect thinking blocks: hidden send again\r expect Thinking... expect ↑60 ↓40 wait 400 send \x14 expect thinking blocks: visible send \x04\x04 EOF2 [ -z "$(pic2 settings hideThinking)" ] || [ "$(pic2 settings hideThinking)" = false ] || fail "thinking: Ctrl+T did not save the setting" # Pi's thinking text: the thinkingText colour (256 or truecolor by COLORTERM), then italic grep -a 'pondering the answer carefully' "$T/thinking.tx" | grep -aqE $'\e\\[38;(5;[0-9]+|2;[0-9]+;[0-9]+;[0-9]+)m\e\\[3m' || fail "thinking: not thinkingText italic" printf 'PASS: thinking is kept dim italic in the transcript; Ctrl+T hides it behind a marker and saves hideThinking\n' # A reply cut at the length limit says so; a failed attempt shows the retry # status; a request that fails on every attempt says how many were made. drive2 stops <<'EOF2' expect pic fake/r send long\r expect The long answer starts expect Response was truncated before completion. send retry\r expect Retrying (2/3) expect (esc to cancel) waitfile $T/log2/9.req send \e expect Operation aborted wait 1500 send fail\r expect Retry failed after 3 attempts send \x04\x04 EOF2 printf 'PASS: a length stop is reported; retries show their status and the final failure counts attempts\n' # Alt+P and Ctrl+P work while a reply runs and apply from the next request. drive2 keys <<'EOF2' expect pic fake/r send hold\r expect Working waitfile $T/log2/13.req send \x10 expect model send \ep expect Select a model send fake/m expect m [fake] send \r expect model fake/m expect fake/m send \e expect Operation aborted send next\r expect All done. send \x04\x04 EOF2 grep -q '"model":"m"' "$(ls "$T/log2"/*.req | sort -V | tail -1)" || fail "keys: the next request did not use fake/m" printf 'PASS: Alt+P and Ctrl+P work while a reply runs and take effect from the next request\n' # Up/Down recall the messages of the conversation being continued, oldest # first, after a restart. drive2 hist1 <<'EOF2' expect pic fake/r send first question\r expect ↑30 ↓20 wait 300 send second question\r expect ↑60 ↓40 wait 300 send \x04\x04 EOF2 drive2 hist2 tui -c <<'EOF2' expect continuing expect second question wait 300 send \e[A wait 200 send \e[A expect first question wait 100 send \r expect All done. send \x04\x04 EOF2 [ "$(grep -o '"first question"' "$(ls "$T/log2"/*.req | sort -V | tail -1)" | wc -l | tr -d ' ')" = 2 ] || fail "history: the recalled message was not sent again" printf 'PASS: Up recalls the earlier messages of a continued conversation\n' # Pi parity, round 2: markdown, "!" commands, editor keys, queue, startup. A # third fake server and workspace; the connection order is the test order. mkdir "$T/log3" "$T/ws3" "$T/ws3/.pic" "$T/home/.pic/skills" "$T/home/.pic/skills/tui2demo" printf 'Be kind.\n' > "$T/ws3/AGENTS.md" printf -- '---\nname: tui2demo\ndescription: A demo skill\n---\nDo the demo.\n' > "$T/home/.pic/skills/tui2demo/SKILL.md" "$SERVER" "$T/port3" "$T/log3" 240 "sse:$V/markdown.sse" "sse:$V/done.sse" "sse:$V/done.sse" "hang:$V/done.sse:461" \ "sse:$V/markdown.sse" & spid3=$! trap 'kill "$spid2" "$spid3" 2>/dev/null || :; cleanup' EXIT HUP INT TERM i=0 while [ ! -s "$T/port3" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server 3"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000},{"id":"q","contextWindow":200000},{"id":"r","contextWindow":200000}]}}}' \ "$(cat "$T/port3")" > "$T/models3.json" export PIC_MODELS_FILE="$T/models3.json" PIC_NO_NATIVE_CLIPBOARD=1 # A terminal that is known to draw no hyperlinks (Apple's Terminal), whatever the developer's own is. unset TMUX STY TERM_PROGRAM_VERSION export TERM=xterm-256color TERM_PROGRAM=Apple_Terminal drive3() { # NAME [pic args]: the third journal and workspace name=$1 shift "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode regular --db "$T/j3" --workspace "$T/ws3" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } pic3() { "$PIC" --db "$T/j3" --workspace "$T/ws3" "$@"; } # The startup header lists the key hints and what was loaded; the terminal # title names the directory. A reply renders as markdown: headings, italics, # a link with its url, a numbered list, a quote, a table, a labelled fence; # the user's message is on a background. drive3 markdown <<'EOF2' expect pic fake/m expect esc interrupt · ctrl+c/ctrl+d clear/exit · / commands · ! bash · ctrl+o more expect [Context] expect AGENTS.md expect [Skills] expect tui2demo expect [Prompts] expect /greet send show markdown\r expect Heading expect Some italic and bold with a link (https://example.com/page). expect 1. first expect 2. second expect │ quoted words expect │ apple expect ┌ expect ```python expect print('hi') wait 200 send \x04\x04 EOF2 grep -aq "$(printf '\033]0;pic - ws3\007')" "$T/markdown.tx" || fail "markdown: terminal title not set" grep -aqE "$(printf '\033')\[48;(5;23|2;33;59;73)m" "$T/markdown.tx" || fail "markdown: the user message has no background" grep -aq "$(printf '\033')\[3mitalic" "$T/markdown.tx" || fail "markdown: italic not styled" printf 'PASS: the header lists keys and loaded resources, the title names the directory, a reply renders as markdown, the user message has a background\n' # "!!cmd" runs in the workspace and stays out of the context; "!cmd" goes to # the model in front of the next message; a failing command shows its exit # code; Esc cancels a running one; none run while a reply does. Alt+Up # restores queued messages without stopping the reply. printf 'in-workspace\n' > "$T/ws3/marker.txt" drive3 bang tui -c <<'EOF2' expect continuing send !!cat marker.txt\r expect $ cat marker.txt expect in-workspace wait 400 send ok\r expect All done. wait 400 send !printf ctx-out\r expect ctx-out wait 400 send next\r expect All done. wait 400 send !exit 3\r expect (exit 3) wait 400 send !sleep 30\r expect Running... (esc to cancel) send \e expect (cancelled) wait 400 send hold on\r expect All done. send !echo nope\r expect a reply is running send \x15 send queued one\r expect Steering: queued one expect alt+up to edit all queued messages send \e[1;3A expect Restored 1 queued message to editor expect > queued one wait 200 send \e expect Operation aborted send \x03 send \x04\x04 EOF2 grep -q 'Ran `printf ctx-out`' "$T/log3/3.req" || fail "bang: ! output did not go with the next message" ! grep -q 'Ran `printf ctx-out`' "$T/log3/2.req" || fail "bang: ! output went early" ! grep -q 'cat marker.txt' "$T/log3"/*.req || fail "bang: !! output went to the model" # The commands are journaled as Pi's bashExecution messages: in the log, the # Pi export and the model's next request; Alt+Up made the child forget the steer. pic3 log | grep -q 'bash \$ printf ctx-out' || fail "bang: the command is not in the log: $(pic3 log | tail -5)" pic3 log --json | grep -qF '"bash":{"command":"exit 3","output":"","exitCode":3,' || fail "bang: no exit code in the journal" pic3 log --json | grep -qF '"excludeFromContext":true' || fail "bang: no !! in the journal" mkdir "$T/pi3" pic3 export --pi "$T/pi3" > /dev/null || fail "bang: export --pi" cat "$T"/pi3/* | grep -qF '"role":"bashExecution","command":"printf ctx-out"' || fail "bang: the Pi export lacks the command" cat "$T"/pi3/* | grep -qF '"excludeFromContext":true' || fail "bang: the Pi export lacks the !! flag" [ "$(sqlite3 "$T/j3" "SELECT state FROM pic_queued WHERE text = 'queued one'")" = cleared ] || fail "queue: Alt+Up left the steer queued in the child" pic3 check > /dev/null || fail "bang: integrity" printf 'PASS: !cmd goes to the model with the next message, !!cmd does not; exit codes, Esc, no commands during a reply, Alt+Up restores the queue\n' # Editor keys: kill ring and yank, undo, the external editor, suspend; Esc # Esc opens /tree; Shift+Ctrl+P cycles the models backwards; Ctrl+X copies. printf 'printf "edited by script\\n" > "$1"\n' > "$T/ed.sh" export VISUAL= EDITOR="sh $T/ed.sh" pic3 scoped-models --set fake/m fake/q fake/r drive3 editor tui -c <<'EOF2' expect continuing send one two expect > one two send \x15 wait 150 send \x19 expect > one two send \x03 send alpha beta expect > alpha beta send \x1f expect > alpha send \x03 send draft expect > draft send \x07 expect Launching external editor expect > edited by script send \x03 send keep expect > keep send \x1a wait 300 send x expect > keepx send \x03 send \x10 expect model fake/q send \e[112;6u expect model fake/m send \e[112;6u expect model fake/r send \x18 expect Copied last agent message to clipboard send \e wait 100 send \e expect Rewind send \e send \x04\x04 EOF2 unset VISUAL EDITOR pic3 scoped-models --clear [ "$(grep -ac "$(printf '\033')\[?2004h" "$T/editor.tx")" -ge 2 ] || fail "editor: the terminal was not set up again after Ctrl+Z" grep -aq "$(printf '\033')\]52;c;" "$T/editor.tx" || fail "editor: no OSC 52 copy" printf 'PASS: kill ring, undo, Ctrl+G, Ctrl+Z, Shift+Ctrl+P, Ctrl+X and Esc Esc work in the terminal\n' # Links are OSC 8 hyperlinks on a terminal that draws them: the url is not printed. export TERM_PROGRAM=ghostty drive3 links <<'EOF2' expect pic fake/m send links please\r expect Some italic and bold with a wait 200 send \x04\x04 EOF2 grep -aqE "$(printf '\033')\]8;id=pic[0-9a-f]+;https://example.com/page" "$T/links.tx" || fail "links: no OSC 8 hyperlink" printf 'PASS: links are OSC 8 hyperlinks where the terminal draws them\n' # Pi parity, round 3: steering over the child's command pipe, live tool output, # the retry countdown, queued messages handed back. A fourth server; connection # order is test order: # 1-2 live output 3-4 steer 5-7 follow-up 8 Alt+Up 9 Esc with a queue # 10-11 retry countdown 12 Esc in the wait 13-15 a request that fails mkdir "$T/log4" "$T/ws4" H=$S/bash_hold.sse "$SERVER" "$T/port4" "$T/log4" 300 "sse:$V/bash_live.sse" "sse:$V/done.sse" "sse:$H" "sse:$V/done.sse" "sse:$H" "sse:$V/done.sse" \ "sse:$V/done.sse" "sse:$H" "sse:$H" "retry:503:3" "sse:$V/done.sse" "retry:503:30" "retry:503:1" & spid4=$! trap 'kill "$spid2" "$spid3" "$spid4" "$rpid" 2>/dev/null || :; cleanup' EXIT HUP INT TERM i=0 while [ ! -s "$T/port4" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server 4"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \ "$(cat "$T/port4")" > "$T/models4.json" export PIC_MODELS_FILE="$T/models4.json" drive4() { # NAME [pic args]: the fourth journal and workspace name=$1 shift "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode regular --db "$T/j4" --workspace "$T/ws4" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } pic4() { "$PIC" --db "$T/j4" --workspace "$T/ws4" "$@"; } rpid= touch_when() { # FILE PATTERN TARGET: create TARGET once PATTERN is in FILE (waits up to 30s, then gives up) ( n=0 until grep -aqF -- "$2" "$1" 2>/dev/null; do n=$((n + 1)) [ "$n" -lt 600 ] || exit 0 sleep 0.05 done : > "$3" ) & rpid=$! } # A bash call's output is shown while it runs, not only when it ends. touch_when "$T/live.tx" out-alpha "$T/ws4/live-release" drive4 live <<'EOF2' expect pic fake/m send live\r expect $ printf out-%s alpha expect out-alpha expect Elapsed expect out-beta expect Took expect All done. send \x04\x04 EOF2 wait "$rpid" 2>/dev/null || : printf 'PASS: a running bash call shows its output as it arrives\n' # Enter while a reply runs steers it: the message is shown as queued, then as a # user message where it lands; the model sees it after the tool result. rm -f "$T/ws4/release" "$T/ws4/started" touch_when "$T/steer.tx" "Steering: steer this" "$T/ws4/release" drive4 steer <<'EOF2' expect pic fake/m send go\r expect $ touch started send steer this\r expect Steering: steer this expect alt+up to edit all queued messages expect All done. send \x04\x04 EOF2 wait "$rpid" 2>/dev/null || : grep -aqE "$(printf '\033')\[48;(5;23|2;33;59;73)m[^m]*m?[^m]*msteer this" "$T/steer.tx" || fail "steer: the delivered message is not shown as a user message" grep -qF 'steer this' "$T/log4/4.req" || fail "steer: request 2 lacks the steer: $(tail -c 600 "$T/log4/4.req")" ! grep -qF 'steer this' "$T/log4/3.req" || fail "steer: sent with the first request" [ "$(pic4 log | grep -c 'you: steer this')" = 1 ] || fail "steer: not in the journal once: $(pic4 log | tail -8)" pic4 check > /dev/null || fail "steer: integrity" printf 'PASS: Enter steers a running reply: queued as Steering, then shown as a user message and sent after the tool result\n' # Alt+Enter (ESC CR) queues a follow-up that runs after the reply. rm -f "$T/ws4/release" "$T/ws4/started" touch_when "$T/follow.tx" "Follow-up: then this" "$T/ws4/release" drive4 follow <<'EOF2' expect pic fake/m send go\r expect $ touch started send then this\e\r expect Follow-up: then this expect ↑55 ↓60 expect ↑85 ↓80 send \x04\x04 EOF2 wait "$rpid" 2>/dev/null || : grep -qF 'then this' "$T/log4/7.req" || fail "follow-up: the third request lacks it: $(tail -c 500 "$T/log4/7.req")" ! grep -qF 'then this' "$T/log4/6.req" || fail "follow-up: delivered before the answer" printf 'PASS: Alt+Enter queues a follow-up that runs after the answer\n' # Alt+Up asks the child to forget its queue (clear_queue) and puts the text back; Esc aborts. rm -f "$T/ws4/release" "$T/ws4/started" drive4 dequeue <<'EOF2' expect pic fake/m send go\r expect $ touch started send to be restored\r expect Steering: to be restored send \e[1;3A expect Restored 1 queued message to editor expect > to be restored send \e expect Operation aborted send \x03 send \x04\x04 EOF2 [ "$(sqlite3 "$T/j4" "SELECT state FROM pic_queued WHERE text = 'to be restored'")" = cleared ] || fail "dequeue: the child still holds the steer" printf 'PASS: Alt+Up clears the child'"'"'s queue and restores it to the editor; Esc aborts over the pipe\n' # Esc with messages queued: they come back to the editor, as Pi's restoreQueuedMessagesToEditor with abort. rm -f "$T/ws4/release" "$T/ws4/started" drive4 escq <<'EOF2' expect pic fake/m send go\r expect $ touch started send first queued\r expect Steering: first queued send \e expect Operation aborted expect > first queued send \x03 send \x04\x04 EOF2 [ "$(sqlite3 "$T/j4" "SELECT state FROM pic_queued WHERE text = 'first queued'")" = cleared ] || fail "escq: the steer was not cleared" pic4 check > /dev/null || fail "escq: integrity" printf 'PASS: Esc on a running reply hands the queued messages back to the editor\n' # A retry with a delay counts down; Esc during the wait cancels it. drive4 retry <<'EOF2' expect pic fake/m send slow\r expect Retrying (2/4) in 3s expect (esc to cancel) expect in 2s expect All done. send \x04\x04 EOF2 drive4 retrycancel <<'EOF2' expect pic fake/m send slower\r expect Retrying (2/4) in 30s expect in 29s send \e expect Operation aborted send \x04\x04 EOF2 printf 'PASS: a retry delay counts down in the status line and Esc during it cancels the request\n' # A request that fails hands back the follow-up it still held (a steer would have been delivered to it). drive4 failq <<'EOF2' expect pic fake/m send doomed\r expect Retrying (2/4) in 1s send never ran\e\r expect Follow-up: never ran expect Retry failed after expect > never ran send \x15 send \x04\x04 EOF2 pic4 check > /dev/null || fail "failq: integrity" printf 'PASS: a request that fails returns the queued follow-up to the editor\n' # @file completion: "@" plus part of a path lists workspace files and folders; Tab accepts, a folder # continues into it, a file ends with a space; Esc dismisses the menu; .git is not offered. mkdir -p "$T/ws/docs/sub" "$T/ws/.git" : > "$T/ws/docs/guide.md" : > "$T/ws/.git/guide.cfg" drive atfile <<'EOF2' expect pic fake/m send look @do expect docs/ send \t expect > look @docs/ send gu expect guide.md send \t expect > look @docs/guide.md send \x03 send @gu expect guide.md send \e send \x03 send @docs/su expect sub/ send \r expect > @docs/sub/ send \x03 send \x04\x04 EOF2 printf 'PASS: @file completion lists workspace paths, Tab accepts, folders continue, Esc dismisses\n' # An ignored file is not offered: .fslignore hides zzh-hidden.md, zzh-shown.md still completes. : > "$T/ws/zzh-hidden.md" : > "$T/ws/zzh-shown.md" printf 'zzh-hid*\n' > "$T/ws/.fslignore" drive atignore <<'EOF2' expect pic fake/m send @zzh expect zzh-shown.md send \x03 send \x04\x04 EOF2 if grep -q 'zzh-hidden' "$T/atignore.tx"; then fail "an ignored file was offered by @file completion"; fi printf 'PASS: @file completion omits files named by .fslignore\n' # ---- fullscreen, Pi's default tuiMode ------------------------------------ # The alternate screen: a transcript viewport over a pinned editor and footer. mkdir "$T/log5" "$T/ws5" # 1 text, 2 "Hello" and then nothing (quit while it streams) "$SERVER" "$T/port5" "$T/log5" 300 "sse:$S/text.sse" "hang:$S/text.sse:492" & spid5=$! trap 'kill "$spid2" "$spid3" "$spid4" "$spid5" "$rpid" 2>/dev/null || :; cleanup' EXIT HUP INT TERM i=0 while [ ! -s "$T/port5" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server 5"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \ "$(cat "$T/port5")" > "$T/models5.json" export PIC_MODELS_FILE="$T/models5.json" drive5() { # NAME ROWS [pic args]: the fifth journal, no --tui-mode (the default) name=$1 rows=$2 shift 2 "$DRIVE" "$T/$name.tx" 80 "$rows" "$PIC" --db "$T/j5" --workspace "$T/ws5" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } pic5() { "$PIC" --db "$T/j5" --workspace "$T/ws5" "$@"; } # FILE SEQ: the byte offset of the last SEQ in FILE (-1: none) last_at() { perl -0777 -ne 'BEGIN { $s = shift; $s =~ s/\\e/\e/g } print rindex($_, $s), "\n"' "$2" "$1"; } # FILE: the text written after the alternate screen was last left, escapes removed after_leave() { perl -0777 -ne '$i = rindex($_, "\e[?1049l"); $_ = $i < 0 ? "" : substr($_, $i + 8); s/\e\][^\a]*\a//g; s/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g; print' "$1" } # Enter and leave: the alternate screen and mouse reporting go on and off; on # exit the transcript is printed to the normal screen (fullscreenExitOutput). drive5 fs 24 <<'EOF2' expect pic fake/m send hello\r expect Hello, "world" send \x04\x04 expect pic: conversation EOF2 on=$(last_at "$T/fs.tx" '\e[?1049h') off=$(last_at "$T/fs.tx" '\e[?1049l') [ "$on" -ge 0 ] || fail "fullscreen: the alternate screen was not entered" [ "$off" -gt "$on" ] || fail "fullscreen: the alternate screen was not left" [ "$(last_at "$T/fs.tx" '\e[?1006h')" -ge 0 ] || fail "fullscreen: no mouse reporting" [ "$(last_at "$T/fs.tx" '\e[?1000l')" -gt "$on" ] || fail "fullscreen: mouse reporting left on" after_leave "$T/fs.tx" > "$T/fs.out" grep -q 'pic fake/m' "$T/fs.out" && grep -q '^ hello' "$T/fs.out" && grep -q 'Hello, "world"' "$T/fs.out" && grep -q 'pic: conversation' "$T/fs.out" || fail "fullscreen: the transcript was not printed on exit: $(cat "$T/fs.out")" printf 'PASS: fullscreen enters and leaves the alternate screen; the transcript is printed on exit\n' # Scrolling: PageUp shows earlier lines and Pi's jump-to-latest label, Ctrl+End # follows again, the wheel scrolls, Ctrl+Home reaches the top; a drag selects # and copies (OSC 52; PIC_NO_NATIVE_CLIPBOARD keeps the real clipboard out). drive5 scroll 12 tui -c <<'EOF2' expect Hello, "world" send \e[5~ expect ↓ send \e[1;5F wait 300 send \e[<64;10;2M expect ↓ send \e[6~ send \e[6~ send \e[1;5H expect pic fake/m send \e[<0;1;1M send \e[<32;10;1M send \e[<0;10;1m expect Copied! send \x04\x04 expect pic: conversation EOF2 grep -aqF "$(printf '\033]52;c;cGljIGZha2UvbQ==')" "$T/scroll.tx" || fail "fullscreen: the selection was not copied" printf 'PASS: fullscreen PageUp/PageDown, Ctrl+Home/End and the wheel scroll the transcript; a drag copies\n' # A Mac keyboard has no End key (Ctrl+Fn+Right is Spaces, Shift+PageDown is the # terminal's own scrollback): Ctrl+Shift+Down jumps to the latest message; so # does a click on the button at the right of the last row, and so does typing. drive5 macjump 12 tui -c <<'EOF2' expect Hello, "world" send \e[5~ expect ↓ send \e[1;6A expect pic fake/m send \e[1;6B expect Hello, "world" send \e[1;6A expect pic fake/m send \e[<0;78;7M send \e[<0;78;7m expect Hello, "world" send \e[1;6A expect pic fake/m send x expect Hello, "world" send \x7f send \x04\x04 expect pic: conversation EOF2 ! grep -aqi "ctrl+shift+down" "$T/macjump.tx" || fail "macjump: the button names a key" printf 'PASS: Ctrl+Shift+Down, a click on the jump button and typing jump to the latest message\n' # The transcript scrolls while a reply streams: PageUp during the request # holds the view while the dock below keeps drawing; Ctrl+End follows again. drive5 stream 12 tui -c <<'EOF2' expect Hello, "world" send again\r expect Hello send \e[5~ expect ↓ expect Working send \e[1;5F expect Hello send \x04\x04 expect pic: conversation EOF2 printf 'PASS: fullscreen scrolls while a reply streams\n' # fullscreenCopyOnSelect false: a selection waits for Ctrl+X. pic5 settings fullscreenCopyOnSelect false drive5 ctrlx 12 tui -c <<'EOF2' expect Hello, "world" send \e[1;5H expect pic fake/m send \e[<0;1;1M send \e[<32;10;1M send \e[<0;10;1m wait 300 send \x18 expect Copied! send \x04\x04 expect pic: conversation EOF2 pic5 settings --unset fullscreenCopyOnSelect [ "$(grep -ac 'Copied!' "$T/ctrlx.tx")" -ge 1 ] || fail "fullscreen: Ctrl+X did not copy the selection" printf 'PASS: fullscreenCopyOnSelect false copies the selection with Ctrl+X\n' # fullscreenExitOutput resume-hint: leaving prints only the conversation line. pic5 settings fullscreenExitOutput resume-hint drive5 hint 24 <<'EOF2' expect pic fake/m send \x04\x04 EOF2 pic5 settings --unset fullscreenExitOutput after_leave "$T/hint.tx" > "$T/hint.out" grep -q 'pic fake/m' "$T/hint.out" && fail "resume-hint: the transcript was printed" printf 'PASS: fullscreenExitOutput resume-hint prints no transcript\n' # A resize redraws the frame at the new size. drive5 resize 24 tui -c <<'EOF2' expect Hello, "world" size 60 16 expect Hello, "world" send \x04\x04 expect pic: conversation EOF2 printf 'PASS: fullscreen redraws on resize\n' # SIGTERM and a crash signal put the screen back. for sig in 15 11; do drive5 "sig$sig" 24 <<EOF2 expect pic fake/m signal $sig EOF2 on=$(last_at "$T/sig$sig.tx" '\e[?1049h') [ "$on" -ge 0 ] || fail "signal $sig: fullscreen was not on" [ "$(last_at "$T/sig$sig.tx" '\e[?1049l')" -gt "$on" ] || fail "signal $sig: the alternate screen was not left" [ "$(last_at "$T/sig$sig.tx" '\e[?1000l')" -gt "$on" ] || fail "signal $sig: mouse reporting left on" [ "$(last_at "$T/sig$sig.tx" '\e[?2004l')" -gt "$on" ] || fail "signal $sig: bracketed paste left on" done printf 'PASS: SIGTERM and SIGSEGV leave the alternate screen and restore the terminal\n' # Regular mode, by flag and by setting: no alternate screen. drive5 regular 24 --tui-mode regular <<'EOF2' expect pic fake/m send \x04\x04 EOF2 [ "$(last_at "$T/regular.tx" '\e[?1049h')" -lt 0 ] || fail "--tui-mode regular entered the alternate screen" if "$PIC" --tui-mode wide tui > "$T/badmode.out" 2>&1; then fail "--tui-mode wide was accepted"; fi grep -q 'Valid values: regular, fullscreen' "$T/badmode.out" || fail "--tui-mode: $(cat "$T/badmode.out")" # /settings lists Pi's entries; Enter on TUI mode switches to regular at once and saves it. drive5 settings 24 <<'EOF2' expect pic fake/m send /settings\r expect TUI mode fullscreen expect Fullscreen exit output transcript expect Fullscreen scrollbar auto expect Fullscreen copy on select true expect Fullscreen wheel scrolling auto expect Mouse on send \e[B\e[B\e[B\e[B wait 200 send \r wait 300 send \e expect TUI mode: regular send \x04\x04 EOF2 [ "$(last_at "$T/settings.tx" '\e[?1049l')" -gt "$(last_at "$T/settings.tx" '\e[?1049h')" ] || fail "settings: switching to regular did not leave the alternate screen" [ "$(pic5 settings tuiMode)" = regular ] || fail "settings: tuiMode not saved" drive5 setreg 24 <<'EOF2' expect pic fake/m send \x04\x04 EOF2 [ "$(last_at "$T/setreg.tx" '\e[?1049h')" -lt 0 ] || fail "tuiMode regular entered the alternate screen" pic5 settings --unset tuiMode printf 'PASS: regular mode by --tui-mode and by setting; /settings has the fullscreen entries\n' # ---- hooks: the interface owns its conversations' lifecycle ------------- # In both modes: SessionStart once per conversation (the children share the # interface's PIC_HOOK_OWNER), SessionEnd on /new and on quit, and on SIGTERM. mkdir -p "$T/log6" "$T/ws6" "$HOME/.pic" "$SERVER" "$T/port6" "$T/log6" 300 "sse:$S/text.sse" & spid6=$! trap 'kill "$spid2" "$spid3" "$spid4" "$spid5" "$spid6" "$rpid" 2>/dev/null || :; cleanup' EXIT HUP INT TERM i=0 while [ ! -s "$T/port6" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server 6"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \ "$(cat "$T/port6")" > "$T/models6.json" export PIC_MODELS_FILE="$T/models6.json" # The hook appends "EVENT CONVERSATION SOURCE-OR-REASON" to $T/hooks.log. cat > "$T/hook.sh" <<'SH' in=$(cat) field() { printf '%s' "$in" | sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p"; } printf '%s %s %s%s\n' "$1" "$(field session_id)" "$(field source)" "$(field reason)" >> "$(dirname "$0")/hooks.log" SH printf '{"hooks": {"SessionStart": [{"hooks": [{"type": "command", "command": "sh %s/hook.sh SessionStart"}]}], "SessionEnd": [{"hooks": [{"type": "command", "command": "sh %s/hook.sh SessionEnd"}]}]}}\n' "$T" "$T" > "$HOME/.pic/hooks.json" for mode in regular fullscreen; do rm -f "$T/hooks.log" "$DRIVE" "$T/hooks-$mode.tx" 80 24 "$PIC" --tui-mode "$mode" --db "$T/j6" --workspace "$T/ws6" --model fake/m <<'EOF2' || expect pic fake/m send one\r expect ↑25 ↓12 send two\r expect ↑50 ↓24 send /new\r expect new conversation send three\r expect ↑25 ↓12 send \x04\x04 expect pic: conversation EOF2 fail "hooks $mode: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/hooks-$mode.tx" | grep -v '^\s*$' | tail -8)" awk '{print $1, $3}' "$T/hooks.log" | tr '\n' ',' > "$T/hooks.seq" [ "$(cat "$T/hooks.seq")" = 'SessionStart startup,SessionEnd clear,SessionStart startup,SessionEnd prompt_input_exit,' ] || fail "hooks $mode: $(cat "$T/hooks.log")" a=$(awk 'NR == 1 {print $2}' "$T/hooks.log") b=$(awk 'NR == 3 {print $2}' "$T/hooks.log") [ -n "$a" ] && [ "$a" != "$b" ] && [ "$(awk 'NR == 2 {print $2}' "$T/hooks.log")" = "$a" ] && [ "$(awk 'NR == 4 {print $2}' "$T/hooks.log")" = "$b" ] || fail "hooks $mode: conversations: $(cat "$T/hooks.log")" done rm -f "$T/hooks.log" # PIC_LAUNCH_DELAY_MS (development builds) holds the SessionEnd launcher in # the terminal's session until it has died and the pty has hung up: the # SIGHUP that brings must not kill it. PIC_LAUNCH_DELAY_MS=300 "$DRIVE" "$T/hooks-term.tx" 80 24 "$PIC" --tui-mode fullscreen --db "$T/j6" --workspace "$T/ws6" --model fake/m <<'EOF2' || expect pic fake/m send four\r expect ↑25 ↓12 signal 15 EOF2 fail "hooks SIGTERM: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/hooks-term.tx" | grep -v '^\s*$' | tail -8)" i=0 until grep -q '^SessionEnd ' "$T/hooks.log" 2>/dev/null; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "hooks SIGTERM: no SessionEnd: $(cat "$T/hooks.log" 2>/dev/null)"; sleep 0.05; done [ "$(awk '{print $1, $3}' "$T/hooks.log" | tr '\n' ',')" = 'SessionStart startup,SessionEnd other,' ] || fail "hooks SIGTERM: $(cat "$T/hooks.log")" # SIGTERM after SessionStart has fired but before the first prompt is admitted (the hook is still # running): the conversation has no row in the journal yet, and SessionEnd still runs for it. cat > "$T/slow.sh" <<'SH' sh "$(dirname "$0")/hook.sh" SessionStart : > "$(dirname "$0")/ss.started" sleep 5 SH printf '{"hooks": {"SessionStart": [{"hooks": [{"type": "command", "command": "sh %s/slow.sh"}]}], "SessionEnd": [{"hooks": [{"type": "command", "command": "sh %s/hook.sh SessionEnd"}]}]}}\n' "$T" "$T" > "$HOME/.pic/hooks.json" for mode in regular fullscreen; do rm -f "$T/hooks.log" "$T/ss.started" PIC_LAUNCH_DELAY_MS=300 "$DRIVE" "$T/hooks-early-$mode.tx" 80 24 "$PIC" --tui-mode "$mode" --db "$T/j6" --workspace "$T/ws6" --model fake/m <<EOF2 || expect pic fake/m send five\r waitfile $T/ss.started signal 15 EOF2 fail "hooks early SIGTERM $mode: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/hooks-early-$mode.tx" | grep -v '^\s*$' | tail -8)" i=0 until grep -q '^SessionEnd ' "$T/hooks.log" 2>/dev/null; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "hooks early SIGTERM $mode: no SessionEnd: $(cat "$T/hooks.log" 2>/dev/null)"; sleep 0.05; done [ "$(awk '{print $1, $3}' "$T/hooks.log" | tr '\n' ',')" = 'SessionStart startup,SessionEnd other,' ] || fail "hooks early SIGTERM $mode: $(cat "$T/hooks.log")" done rm -f "$HOME/.pic/hooks.json" printf 'PASS: with hooks, both modes start each conversation once and end it on /new, on quit and on SIGTERM, even before its first prompt\n' # /fork with no argument: Pi's "Fork from Message" picker of earlier user # messages. Enter on the newest forks the path before it into a new # conversation and puts that message in the editor; /changelog shows the # newest entry; /reload re-reads skills. mkdir -p "$T/log7" "$T/ws7" drive7() { # NAME [pic args]: the seventh journal and workspace name=$1 shift "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode regular --db "$T/j7" --workspace "$T/ws7" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } drive7 forkpick <<'EOF2' expect pic fake/m send first question\r expect ↑25 ↓12 send second question\r expect ↑25 ↓12 send /fork\r expect Fork from Message expect Message 1 of 2 expect Message 2 of 2 send \e[A expect Message 1 of 2 send \e[B send \r expect forked: now in expect first question expect > second question send \x15 send \x04\x04 EOF2 [ "$("$PIC" --db "$T/j7" sessions | wc -l | tr -d ' ')" = 2 ] || fail "fork picker: expected the conversation and its fork" fork_id=$("$PIC" --db "$T/j7" sessions --json | head -1 | sed 's/.*"id":"\([^"]*\)".*/\1/') "$PIC" --db "$T/j7" log "$fork_id" | grep -q 'first question' || fail "fork picker: the fork lacks the first message" "$PIC" --db "$T/j7" log "$fork_id" | grep -q 'second question' && fail "fork picker: the fork kept the chosen message" printf 'PASS: /fork opens the user-message picker; choosing forks before it and refills the editor\n' drive7 forkfirst tui -c <<'EOF2' expect first question send /fork\r expect Fork from Message send \e[A send \r expect forked: a new conversation expect > first question send \x15 send \x04\x04 EOF2 printf 'PASS: forking from the first message starts an empty conversation with its text in the editor\n' drive7 changelog <<'EOF2' expect pic fake/m send /changelog\r expect What's New wait 300 send \x04\x04 EOF2 # Pi's order: oldest first, the newest last, next to the editor; rendered as # markdown (no "## " or backticks left) newest=$(grep -m1 '^## ' "$(dirname "$0")/../../CHANGELOG.md" | sed 's/^## //') oldest=$(grep '^## ' "$(dirname "$0")/../../CHANGELOG.md" | tail -1 | sed 's/^## //') perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/changelog.tx" > "$T/changelog.txt" n=$(grep -n "$newest" "$T/changelog.txt" | tail -1 | cut -d: -f1) o=$(grep -n "$oldest" "$T/changelog.txt" | tail -1 | cut -d: -f1) [ -n "$n" ] && [ -n "$o" ] && [ "$o" -lt "$n" ] || fail "changelog: the newest entry ($newest, line $n) is not after the oldest ($oldest, line $o)" grep -q '^## ' "$T/changelog.txt" && fail "changelog: raw markdown headings" grep -q '`/fork`' "$T/changelog.txt" && fail "changelog: raw backticks" printf 'PASS: /changelog shows the entries as Pi does, newest last, as markdown\n' ( n=0 until grep -aqF 'reloaded:' "$T/reload.tx" 2>/dev/null; do n=$((n + 1)) [ "$n" -lt 600 ] || exit 0 sleep 0.05 done mkdir -p "$HOME/.pic/skills/reloadme" printf -- '---\nname: reloadme\ndescription: A freshly reloaded skill\n---\nDo it.\n' > "$HOME/.pic/skills/reloadme/SKILL.md" : > "$T/skill-made" ) & rpid=$! drive7 reload <<'EOF2' expect pic fake/m send /reload\r expect reloaded: waitfile $T/skill-made send /skill:reloadm wait 300 send \x15 send /reload\r expect skills; send /skill:reloadm expect A freshly reloaded skill send \x15 send \x04\x04 EOF2 wait "$rpid" 2>/dev/null || : perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g' "$T/reload.tx" | grep -c 'A freshly reloaded skill' | grep -qx 1 || fail "reload: the new skill showed before /reload" printf 'PASS: /reload picks up a new skill\n' # The skill listing's budget: a notice at startup and on /reload naming what it cut; silent when # everything fits. "$PIC" settings skillBudget 10 > /dev/null drive skillbudget <<'EOF' expect pic fake/m expect over its budget of 10 expect shown by name only send /reload\r expect reloaded: send \x04\x04 EOF [ "$(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\s+/ /g' "$T/skillbudget.tx" | grep -o 'over its budget of 10' | wc -l | tr -d ' ')" -ge 2 ] || fail "skill budget: no notice again on /reload" "$PIC" settings --unset skillBudget > /dev/null drive skillfits <<'EOF' expect pic fake/m send /reload\r expect reloaded: send \x04\x04 EOF ! perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g' "$T/skillfits.tx" | grep -q 'over its budget' || fail "skill budget: a notice while everything fits" printf 'PASS: the skill budget warns at startup and on /reload, and is silent when the listing fits\n' # ---- field-test regressions ------------------------------------------------ mkdir -p "$T/log8" "$T/ws8" # 1 a line exactly 120 columns wide; 2-3 a long bash output, then text; 4 # thinking; 5-6 bash again; 7 thinking then a long answer, slowly; 8 thinking, # then nothing; 9-10 thinking, a tool call, then text (to resume) "$SERVER" "$T/port8" "$T/log8" 300 "sse:$V/wide.sse" "sse:$V/bash.sse" "sse:$V/done.sse" "sse:$V/thinking_text.sse" \ "sse:$V/bash.sse" "sse:$V/done.sse" "slow:$V/thinking_long.sse:15" "hang:$V/thinking_long.sse:902" \ "sse:$S/thinking_tool.sse" "sse:$S/text.sse" & spid8=$! trap 'kill "$spid2" "$spid3" "$spid4" "$spid5" "$spid6" "$spid8" "$rpid" 2>/dev/null || :; cleanup' EXIT HUP INT TERM i=0 while [ ! -s "$T/port8" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server 8"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000},{"id":"r","contextWindow":200000,"reasoning":true}]}}}' \ "$(cat "$T/port8")" > "$T/models8.json" export PIC_MODELS_FILE="$T/models8.json" SCREEN="$(dirname "$0")/screen.pl" drive8() { # NAME COLS [pic args]: the eighth journal, fullscreen unless told otherwise name=$1 cols=$2 shift 2 "$DRIVE" "$T/$name.tx" "$cols" 30 "$PIC" --db "$T/j8" --workspace "$T/ws8" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } # Fullscreen draws a line as wide as the terminal whole: its last character, # and the footer's, are not erased. drive8 fullwidth 120 <<'EOF2' expect pic fake/m send hi\r expect next line wait 300 send \x04\x04 expect pic: conversation EOF2 perl "$SCREEN" --alt 120 30 < "$T/fullwidth.tx" > "$T/fullwidth.scr" grep -q 'word lastZ$' "$T/fullwidth.scr" || fail "fullwidth: the last column was lost: $(grep 'word last' "$T/fullwidth.scr")" grep -q ' fake/m$' "$T/fullwidth.scr" || fail "fullwidth: the footer lost its last column: $(tail -1 "$T/fullwidth.scr")" printf 'PASS: fullscreen keeps the last column of a full-width line and of the footer\n' # /hotkeys lists every group, not the first kilobyte of them. drive8 hotkeys 120 --tui-mode regular <<'EOF2' expect pic fake/m send /hotkeys\r expect Other: expect Ctrl+F searches it expect Rebind any action send \x04\x04 EOF2 printf 'PASS: /hotkeys shows every group\n' # Ctrl+O draws the tool blocks already shown again, expanded (Pi re-renders # them all), and Ctrl+T the thinking blocks, hidden. drive8 ctrlo 80 <<'EOF2' expect pic fake/m send list\r expect 7 earlier lines, ctrl+o to expand expect All done. wait 300 send \x0f expect tools expanded wait 300 send \x04\x04 expect pic: conversation EOF2 perl "$SCREEN" --alt 80 30 < "$T/ctrlo.tx" > "$T/ctrlo.scr" grep -q '^ 101$' "$T/ctrlo.scr" || fail "ctrl+o: the block shown was not expanded: $(cat "$T/ctrlo.scr")" grep -q 'ctrl+o to expand' "$T/ctrlo.scr" && fail "ctrl+o: a collapsed hint is still shown: $(cat "$T/ctrlo.scr")" # Pi's spacer: a blank line above each tool block grep -B1 '^✓ \$ seq 101 112' "$T/ctrlo.scr" | head -1 | grep -q '^$' || fail "ctrl+o: no blank line above the tool block: $(cat "$T/ctrlo.scr")" drive8 ctrlt 80 <<'EOF2' expect pic fake/m send ponder\r expect Done thinking. wait 300 send \x14 expect thinking blocks: hidden wait 300 send \x04\x04 expect pic: conversation EOF2 "$PIC" --db "$T/j8" --workspace "$T/ws8" settings --unset hideThinking perl "$SCREEN" --alt 80 30 < "$T/ctrlt.tx" > "$T/ctrlt.scr" grep -q 'pondering the answer carefully' "$T/ctrlt.scr" && fail "ctrl+t: the thinking shown was not hidden: $(cat "$T/ctrlt.scr")" grep -q 'Thinking\.\.\.' "$T/ctrlt.scr" || fail "ctrl+t: no Thinking... label: $(cat "$T/ctrlt.scr")" printf 'PASS: fullscreen Ctrl+O and Ctrl+T draw the blocks already shown again\n' # Regular mode: Ctrl+O redraws the whole transcript (Pi's full redraw, with # the scrollback cleared). drive8 ctrlo-reg 80 --tui-mode regular <<'EOF2' expect pic fake/m send list\r expect 7 earlier lines, ctrl+o to expand expect All done. wait 300 send \x0f expect tools expanded wait 300 send \x04\x04 expect pic: conversation EOF2 grep -aqF "$(printf '\033[3J')" "$T/ctrlo-reg.tx" || fail "ctrl+o regular: the transcript was not redrawn" perl -0777 -ne 'print substr($_, rindex($_, "\e[3J"))' "$T/ctrlo-reg.tx" | perl "$SCREEN" 80 30 > "$T/ctrlo-reg.scr" grep -q '^ 101$' "$T/ctrlo-reg.scr" || fail "ctrl+o regular: not expanded: $(cat "$T/ctrlo-reg.scr")" grep -q 'ctrl+o to expand' "$T/ctrlo-reg.scr" && fail "ctrl+o regular: still collapsed: $(cat "$T/ctrlo-reg.scr")" printf 'PASS: regular Ctrl+O redraws the transcript with the blocks expanded\n' # Thinking goes above the answer it precedes, also when the answer is long # enough to be committed while it streams. drive8 order 80 <<'EOF2' expect pic fake/m send think long\r expect Para 40 wait 500 send \x04\x04 expect pic: conversation EOF2 after_leave "$T/order.tx" > "$T/order.out" t=$(grep -n 'think one is about the plan' "$T/order.out" | head -1 | cut -d: -f1) p=$(grep -n 'Para 01' "$T/order.out" | head -1 | cut -d: -f1) [ -n "$t" ] && [ -n "$p" ] && [ "$t" -lt "$p" ] || fail "order: thinking (line $t) is not above the answer (line $p): $(cat "$T/order.out")" [ "$(grep -c 'think one is about the plan' "$T/order.out")" = 1 ] || fail "order: the thinking was committed twice" printf 'PASS: thinking is committed above a long streamed answer\n' # Thinking streams as a growing block, as Pi draws it, not one line at a time. drive8 ticker 80 <<'EOF2' expect pic fake/m send think more\r expect think three decides wait 500 send \x03 wait 100 send \x03 expect pic: conversation EOF2 # the screen as the frame that first drew the last thought left it perl -0777 -ne '$i = index($_, "think three decides"); $j = index($_, "\e[?2026l", $i); print substr($_, 0, $j)' \ "$T/ticker.tx" | perl "$SCREEN" --alt 80 30 > "$T/ticker.scr" grep -q 'think one is about the plan' "$T/ticker.scr" && grep -q 'think three decides' "$T/ticker.scr" || fail "ticker: the thinking so far is not all shown: $(cat "$T/ticker.scr")" printf 'PASS: thinking streams as a growing block\n' # A continued conversation is drawn as it looked live: thinking, the tool # block with its mark and title, and the footer's totals from the journal. "$DRIVE" "$T/resume-src.tx" 80 30 "$PIC" --tui-mode regular --db "$T/j9" --workspace "$T/ws8" --model fake/r <<'EOF2' || expect pic fake/r send write it\r expect Hello, "world" expect ↑55 ↓32 wait 300 send \x04\x04 EOF2 fail "resume-src: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/resume-src.tx" | grep -v '^\s*$' | tail -8)" "$DRIVE" "$T/resume-full.tx" 80 30 "$PIC" --tui-mode regular --db "$T/j9" --workspace "$T/ws8" --model fake/r tui -c <<'EOF2' || expect continuing expect ↑55 ↓32 wait 300 send \x04\x04 EOF2 fail "resume-full: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/resume-full.tx" | grep -v '^\s*$' | tail -8)" perl "$SCREEN" 80 30 < "$T/resume-full.tx" > "$T/resume-full.scr" grep -q '^Write it\.$' "$T/resume-full.scr" && grep -q '^✓ write t.txt (1 line, 1B)$' "$T/resume-full.scr" && grep -q '^ x$' "$T/resume-full.scr" || fail "resume-full: $(cat "$T/resume-full.scr")" grep -q 'write: Successfully' "$T/resume-full.scr" && fail "resume-full: a tool shown as a one-liner" printf 'PASS: a continued conversation is drawn with its thinking, tool blocks and token totals\n' # /export with no path writes next to the workspace, as Pi writes into its # working directory, not into the directory pic was started from; the notice # names the file in full. mkdir -p "$T/elsewhere" (cd "$T/elsewhere" && drive8 export 250 --tui-mode regular tui -c <<'EOF2' expect continuing send /export\r expect as one self-contained web page send \x04\x04 EOF2 ) ls "$T/ws8"/pic-session-*.html > /dev/null 2>&1 || fail "export: no page in the workspace" ls "$T/elsewhere"/pic-session-*.html > /dev/null 2>&1 && fail "export: the page went to the launch directory" perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g' "$T/export.tx" | grep -q 'written to /.*ws8/pic-session-' || fail "export: the notice does not name the full path" printf 'PASS: /export with no path writes into the workspace and names the file in full\n' # A long workspace path gives way from the left in the header and the footer: # the header does not wrap, and the footer keeps the directory and the name. drive8 narrow 60 --tui-mode regular <<'EOF2' expect /help for all keys and commands expect ws8 • new conversation send \x04\x04 EOF2 printf 'PASS: long paths are shortened on the left in the header and footer\n' # /settings: Esc in the theme picker goes back one level, to the settings # list (Pi's nested menus); booleans read true or false, as Pi's do. drive8 themeesc 120 --tui-mode regular <<'EOF2' expect pic fake/m send /settings\r expect Hide thinking false send \e[A wait 100 send \r expect Theme (up/down: preview send \e expect Settings (up/down send \e wait 300 send \x04\x04 EOF2 printf 'PASS: Esc in the theme picker returns to /settings; booleans read true/false\n' # ---- a crash mid-request (field test #3) --------------------------------- # kill -9 takes the interface and its request child; pic tui -c then offers # the unfinished request: Enter resumes it with the model and thinking level # it was admitted with (not the one the interface starts with), Esc drops it # (journaled as a cancel; the prompt stays in the context, as Pi's does). mkdir -p "$T/log9" "$T/ws9" # 1 hang (killed), 2 text (resumed), 3 hang (killed), 4 text (after the drop), # 5 hang (Esc), 6 text (forked after), 7 text "$SERVER" "$T/port9" "$T/log9" 300 "hang:$S/text.sse:200" "sse:$S/text.sse" "hang:$S/text.sse:200" "sse:$S/text.sse" \ "hang:$S/text.sse:200" "sse:$S/text.sse" "sse:$S/text.sse" & spid9=$! trap 'kill "$spid2" "$spid3" "$spid4" "$spid5" "$spid6" "$spid8" "$spid9" "$rpid" 2>/dev/null || :; cleanup' EXIT HUP INT TERM i=0 while [ ! -s "$T/port9" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server 9"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000},{"id":"r","contextWindow":200000,"reasoning":true}]}}}' \ "$(cat "$T/port9")" > "$T/models9.json" export PIC_MODELS_FILE="$T/models9.json" drive9() { # NAME [pic args]: the ninth journal and workspace name=$1 shift "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode regular --db "$T/j19" --workspace "$T/ws9" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } orphans9() { # the request child outlives a killed interface (its own session): kill it too pkill -9 -f -- "--db $T/j19 " 2>/dev/null || : i=0 while pgrep -f -- "--db $T/j19 " > /dev/null 2>&1; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "orphan request child"; sleep 0.05; done } drive9 kill1 --model fake/r --thinking high <<'EOF2' expect pic fake/r send first ask\r expect Working waitfile $T/log9/1.req signal 9 EOF2 orphans9 drive9 resume1 tui -c <<'EOF2' expect first ask expect the last request was interrupted: Enter to resume, Esc to drop it send \r expect Hello, "world" expect ↑25 ↓12 send \x04\x04 expect pic: conversation EOF2 grep -q '"model":"r"' "$T/log9/2.req" || fail "tui resume: not the admitted model: $(grep -o '"model":"[^"]*"' "$T/log9/2.req")" [ "$(grep -o '"thinking":{[^}]*}' "$T/log9/1.req")" = "$(grep -o '"thinking":{[^}]*}' "$T/log9/2.req")" ] || fail "tui resume: not the admitted thinking level" "$PIC" --db "$T/j19" --workspace "$T/ws9" dump | grep -q '^submission 1 .* answered | first ask' || fail "tui resume: not answered: $("$PIC" --db "$T/j19" --workspace "$T/ws9" dump | grep "^submission")" printf 'PASS: after kill -9, pic tui -c resumes the interrupted request with its own model\n' drive9 kill2 <<'EOF2' expect pic fake/m send second ask\r expect Working waitfile $T/log9/3.req signal 9 EOF2 orphans9 drive9 drop tui -c <<'EOF2' expect second ask expect the last request was interrupted: Enter to resume, Esc to drop it send \e expect interrupted request dropped send third ask\r expect Hello, "world" send \x04\x04 expect pic: conversation EOF2 "$PIC" --db "$T/j19" --workspace "$T/ws9" dump | grep -q '^submission 2 .* failed | second ask error=cancelled' || fail "tui drop: not journaled: $("$PIC" --db "$T/j19" --workspace "$T/ws9" dump | grep '^submission')" grep -q 'second ask' "$T/log9/4.req" && grep -q 'third ask' "$T/log9/4.req" || fail "tui drop: the next request's context" "$PIC" --db "$T/j19" --workspace "$T/ws9" check > /dev/null || fail "tui drop: integrity" printf 'PASS: Esc drops the interrupted request durably; the next message goes through\n' # Ctrl+P with no scoped models walks one stable list, the models with a key # in the registry's order (pic models, Pi's available models), and # Shift+Ctrl+P walks back to where it started (field test #5: it bounced # between two models). "$PIC" --db "$T/j19" --workspace "$T/ws9" scoped-models --clear > /dev/null "$PIC" --db "$T/j19" --workspace "$T/ws9" models --json | sed 's/.*"model":"\([^"]*\)".*/\1/' > "$T/cycle9" at=$(grep -nx 'fake/m' "$T/cycle9" | cut -d: -f1) n=$(wc -l < "$T/cycle9" | tr -d ' ') next1=$(sed -n "$((at % n + 1))p" "$T/cycle9") next2=$(sed -n "$(((at + 1) % n + 1))p" "$T/cycle9") next3=$(sed -n "$(((at + 2) % n + 1))p" "$T/cycle9") drive9 cycle <<EOF2 expect pic fake/m send \x10 expect model $next1 send \x10 expect model $next2 send \x10 expect model $next3 send \e[112;6u expect model $next2 send \e[112;6u expect model $next1 send \e[112;6u expect model fake/m send \x04\x04 EOF2 printf 'PASS: Ctrl+P and Shift+Ctrl+P walk one stable list of models\n' # /fork and /tree offer only points that can be continued from: after a # prompt cancelled with Esc and a "!" command, forking before the next # message and continuing from the cancelled prompt both work (field test #6). # Esc keeps the prompt in the context and asks for nothing again, as Pi's # abort does. drive9 forkpoints <<'EOF2' expect pic fake/m send to be cancelled\r expect Working waitfile $T/log9/5.req send \e expect Operation aborted wait 400 send !echo from-bang\r expect from-bang wait 400 send after both\r expect Hello, "world" send /fork\r expect Fork from Message send \r expect forked: now in expect > after both send \x15 send /tree\r expect Conversation tree send to be cancelled wait 200 send \r expect Summarize branch? send \r expect branched: now in send next one\r expect Hello, "world" send \x04\x04 expect pic: conversation EOF2 perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g' "$T/forkpoints.tx" | grep -q 'not a point to' && fail "forkpoints: a row was refused" perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g' "$T/forkpoints.tx" | grep -q 'requested again' && fail "forkpoints: Esc said the reply is asked again" grep -q 'to be cancelled' "$T/log9/6.req" || fail "forkpoints: the cancelled prompt left the context (Pi keeps it)" grep -q 'to be cancelled' "$T/log9/7.req" && ! grep -q 'after both' "$T/log9/7.req" || fail "forkpoints: /tree did not continue from the cancelled prompt" printf 'PASS: /fork and /tree rows after a cancel and a ! command can be chosen\n' # First run with only an OpenRouter key: the saved default's provider has no # key, so pic starts on Pi's default for the first provider that has one # (defaultModelPerProvider), not on a model that cannot work. "$PIC" --db "$T/j19" --workspace "$T/ws9" models --default fake/m > /dev/null env -u ANTHROPIC_API_KEY OPENROUTER_API_KEY=pic-tui-test-key "$DRIVE" "$T/firstrun.tx" 80 24 "$PIC" --tui-mode regular \ --db "$T/j19" --workspace "$T/ws9" <<'EOF2' || expect pic openrouter/deepseek/deepseek-v4.1-flash send \x04\x04 EOF2 fail "firstrun: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/firstrun.tx" | grep -v '^\s*$' | tail -8)" printf 'PASS: with only an OpenRouter key, pic starts on a model it can use\n' # /compact compacts at once, as Pi's does: too small a conversation says so; # otherwise the summary request runs then and the result is shown. drive9 compactsmall <<'EOF2' expect pic fake/m send /compact\r expect Nothing to compact (session too small) send \x04\x04 EOF2 "$PIC" settings compaction '{"keepRecentTokens":1}' > /dev/null before=$(ls "$T/log9" | wc -l | tr -d ' ') drive9 compactnow <<'EOF2' expect pic fake/m send one\r expect ↑25 ↓12 send two\r expect ↑50 ↓24 send /compact keep the names\r expect compacted send \x04\x04 expect pic: conversation EOF2 "$PIC" settings compaction '{}' > /dev/null after=$(ls "$T/log9" | wc -l | tr -d ' ') [ "$after" -ge $((before + 3)) ] || fail "compact: $((after - before)) requests, expected two replies and a summary" grep -l 'keep the names' "$T/log9/$((before + 3)).req" > /dev/null || fail "compact: the summary request lacks the focus" "$PIC" --db "$T/j19" --workspace "$T/ws9" dump | grep -q '^entry .* compaction |' || fail "compact: no compaction journaled" printf 'PASS: /compact compacts at once and says what it did\n' # A resume that cannot finish the request here (its model's key is not in # this shell) leaves it unfinished and the offer standing; Esc still drops it. mkdir -p "$T/log10" "$T/ws10" "$SERVER" "$T/port10" "$T/log10" 300 "hang:$S/text.sse:200" "sse:$S/text.sse" & spid10=$! trap 'kill "$spid2" "$spid3" "$spid4" "$spid5" "$spid6" "$spid8" "$spid9" "$spid10" "$rpid" 2>/dev/null || :; cleanup' EXIT HUP INT TERM i=0 while [ ! -s "$T/port10" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server 10"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]},"fake9":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$PIC_TUI_KEY9","models":[{"id":"k","contextWindow":200000}]}}}' \ "$(cat "$T/port10")" "$(cat "$T/port10")" > "$T/models10.json" export PIC_MODELS_FILE="$T/models10.json" drive10() { # NAME [pic args]: the tenth journal and workspace name=$1 shift "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode regular --db "$T/j20" --workspace "$T/ws10" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } PIC_TUI_KEY9=pic-tui-key9 drive10 kill9 --model fake9/k <<'EOF2' expect pic fake9/k send needs its key\r expect Working waitfile $T/log10/1.req signal 9 EOF2 pkill -9 -f -- "--db $T/j20 " 2>/dev/null || : i=0 while pgrep -f -- "--db $T/j20 " > /dev/null 2>&1; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "orphan request child 9"; sleep 0.05; done unset PIC_TUI_KEY9 # a prefix on a function call persists in POSIX sh drive10 nokey9 tui -c <<'EOF2' expect needs its key expect the last request was interrupted: Enter to resume, Esc to drop it send \r expect left unfinished: its model fake9/k needs expect the last request was interrupted: Enter to resume, Esc to drop it send \e expect interrupted request dropped send \x04\x04 expect pic: conversation EOF2 "$PIC" --db "$T/j20" --workspace "$T/ws10" dump | grep -q '^submission 1 .* failed | needs its key error=cancelled' || fail "nokey9: $("$PIC" --db "$T/j20" --workspace "$T/ws10" dump | grep '^submission')" "$PIC" --db "$T/j20" --workspace "$T/ws10" check > /dev/null || fail "nokey9: integrity" printf 'PASS: a resume that cannot have its model keeps the offer; Esc then drops it\n' # Pi's SelectList scrolls the slash menu and counts "(selected/total)" under it; # Pi's session selector shows a fork under the conversation it came from. mkdir -p "$T/log11" "$T/ws11" "$SERVER" "$T/port11" "$T/log11" 60 "sse:$S/text.sse" & spid11=$! trap 'kill "$spid2" "$spid3" "$spid4" "$spid5" "$spid6" "$spid8" "$spid9" "$spid10" "$spid11" "$rpid" 2>/dev/null || :; cleanup' EXIT HUP INT TERM i=0 while [ ! -s "$T/port11" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server 11"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \ "$(cat "$T/port11")" > "$T/models11.json" export PIC_MODELS_FILE="$T/models11.json" drive11() { # NAME [pic args]: the eleventh journal and workspace name=$1 shift "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --tui-mode regular --db "$T/j21" --workspace "$T/ws11" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } drive11 forkseed <<'EOF2' expect pic fake/m send root question\r expect Working expect Hello send \x04\x04 EOF2 root=$("$PIC" --db "$T/j21" --workspace "$T/ws11" sessions --json | perl -ne 'print $1 if /"id":"([^"]+)"/' | head -n 1) [ -n "$root" ] || fail "forks: no conversation" "$PIC" --db "$T/j21" --workspace "$T/ws11" fork "$root" > /dev/null || fail "forks: fork" "$PIC" --db "$T/j21" --workspace "$T/ws11" fork "$root" > /dev/null || fail "forks: second fork" drive11 forkpick <<'EOF2' expect pic fake/m send / expect (1/ send \e[B expect (2/ send \e send \x15 send /resume\r expect Resume a conversation expect ├─ expect └─ send \e send \x04\x04 EOF2 "$PIC" --db "$T/j21" --workspace "$T/ws11" check > /dev/null || fail "forks: integrity" printf 'PASS: the slash menu counts and scrolls; /resume shows forks under their parent\n' # /login in place: the provider picker shows what each provider uses, a provider with a sign-in asks # the method, the key is pasted into a masked field, and the new store keeps its key without asking # for a passphrase (in ~/.pic/master.key here: this HOME is not the account's own), and nothing is # printed outside the interface. The key never reaches the screen. LKEY="sk-tui-login-sentinel-$$" LPASS="tui-login-pass-$$" (GROQ_API_KEY=pic-tui-env-key && export GROQ_API_KEY && drive login1) <<EOF expect pic fake/m send /login\r expect Log in expect ✓ env: ANTHROPIC_API_KEY send groq expect env: GROQ_API_KEY send \x7f\x7f\x7f\x7f send openai expect not configured wait 300 send \r expect Log in to openai expect Sign in with ChatGPT expect Paste an API key send \e[B wait 100 send \r expect API key for openai: send \e[200~$LKEY\e[201~ expect •••• send \r expect Saved the API key for openai send /login\r expect Log in send openai expect API key (stored) send \x03 wait 200 send /login openai\r expect Log in to openai send \e wait 100 send \e wait 600 send \x04\x04 EOF ! grep -a -q 'Rewind (' "$T/login1.tx" || fail "login1: the Esc that closed the login choice began an Esc Esc (/rewind)" for secret in "$LKEY" "$LPASS"; do ! grep -a -q -- "$secret" "$T/login1.tx" || fail "login1: a secret reached the screen" done "$PIC" --db "$T/j" --workspace "$T/ws" auth status | grep -q '^openai *default\* *API key' || fail "login1: the key was not stored" ! grep -a -q 'Passphrase for' "$T/login1.tx" || fail "login1: a passphrase was asked" [ -s "$HOME/.pic/master.key" ] || fail "login1: no key file" printf 'PASS: /login: the provider picker with states, the method, a pasted masked key, no passphrase asked\n' if [ "$FAULTS" = yes ]; then # A sign-in in place: the address is shown, Esc cancels it and Ctrl-C cancels the next (each child # closes its listener: the next sign-in binds the same port), and the third, its browser scripted, # signs in; pic keeps running throughout. mkdir "$T/olog" "${OAUTH:?set OAUTH to the fake_oauth binary}" "$T/oport" "$T/olog" 120 "$S/../responses-streams/reasoning_text.sse" & opid=$! i=0 while [ ! -s "$T/oport" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "oauth server"; sleep 0.05; done # The third launch signs in. Launches are counted with mkdir, which is atomic: they can run at # once, as macOS holds every run of a new executable while it checks the file (about half a # second, on its first run). That first run is made here, so the launches start in the order # pic makes them. cat > "$T/browser" <<EOB #!/bin/sh [ \$# -eq 0 ] && exit 0 n=1 until mkdir "$T/bcount.\$n" 2>/dev/null; do n=\$((n + 1)); done [ \$n -ge 3 ] && curl -s -L --max-time 20 "\$1" > /dev/null 2>&1 exit 0 EOB chmod +x "$T/browser" "$T/browser" CBPORT=$(perl -MIO::Socket::INET -e 'print IO::Socket::INET->new(Listen => 1, LocalAddr => "127.0.0.1", LocalPort => 0)->sockport') (PIC_TEST_OPENAI_AUTH="http://127.0.0.1:$(cat "$T/oport")" PIC_TEST_OAUTH_PORT=$CBPORT PIC_TEST_BROWSER="$T/browser" && export PIC_TEST_OPENAI_AUTH PIC_TEST_OAUTH_PORT PIC_TEST_BROWSER && drive signin) <<EOF expect pic fake/m send /login openai\r expect Log in to openai send \r expect esc: cancel expect Open this URL to sign in expect api/accounts/authorize send \e expect Sign-in cancelled. send /login openai\r expect Log in to openai send \r expect Open this URL to sign in send \x03 expect Sign-in cancelled. send /login openai\r expect Log in to openai send \r expect Signed in to openai (Sign in with ChatGPT) send /login\r expect Log in send openai expect signed in (Sign in with ChatGPT) send \e send \x04\x04 EOF ! grep -a -q 'in use' "$T/signin.tx" || fail "signin: a cancelled sign-in kept its port" ! grep -a -q 'oauth-sentinel' "$T/signin.tx" || fail "signin: a token reached the screen" opened=$(ls -d "$T"/bcount.* | wc -l | tr -d ' ') [ "$opened" = 3 ] || fail "signin: the browser opened $opened times" printf 'PASS: a sign-in in place: Esc and Ctrl-C cancel it and free its port; the next signs in\n' fi # /rewind (and Esc Esc on an empty editor): the conversation's messages, # newest first, with the files changed after each; then what to restore. A # file changed outside pic is refused and the rest restored only after a # yes. doubleEscapeAction tree gives Esc Esc back to Pi's /tree. mkdir -p "$T/ws12" p12() { "$PIC" --db "$T/j22" --workspace "$T/ws12" "$@"; } printf 'v0' > "$T/ws12/a.txt" printf 'tool write a.txt v1\twrite c.txt c1\ntext done\n' > "$T/rw1" printf 'tool write a.txt v2\twrite b.txt b1\ntext done\n' > "$T/rw2" p12 submit rw1 'first edit' > /dev/null && p12 run --script "$T/rw1" > /dev/null || fail "rewind: setup 1" rsid=$(p12 sessions --json | perl -ne 'print $1 if /"id":"([^"]+)"/' | head -n 1) p12 submit rw2 'second edit' --session "$rsid" > /dev/null && p12 run --script "$T/rw2" > /dev/null || fail "rewind: setup 2" drive12() { # NAME [pic args]: the twelfth workspace name=$1 shift "$DRIVE" "$T/$name.tx" 100 24 "$PIC" --tui-mode regular --db "$T/j22" --workspace "$T/ws12" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } drive12 rewindboth tui -c <<'EOF2' expect second edit send \e wait 100 send \e expect Rewind expect second edit (2 files changed after) expect first edit (3 files changed after) send \r expect Restore conversation and files send \r expect restored a.txt expect deleted b.txt expect rewound: now in expect > second edit send \x15 send \x04\x04 EOF2 [ "$(cat "$T/ws12/a.txt")" = v1 ] || fail "rewind: a.txt is $(cat "$T/ws12/a.txt")" [ ! -e "$T/ws12/b.txt" ] || fail "rewind: b.txt was not deleted" [ "$(p12 sessions | wc -l | tr -d ' ')" = 2 ] || fail "rewind: expected the conversation and its fork" printf 'mine' > "$T/ws12/a.txt" drive12 rewindrefuse tui -c <<'EOF2' expect first edit send /rewind\r expect Rewind send \r expect Restore conversation and files send \e[B send \e[B send \r expect Some files cannot be restored expect a.txt: it changed since pic last wrote it send \r expect deleted c.txt expect rewound the files send \x04\x04 EOF2 [ "$(cat "$T/ws12/a.txt")" = mine ] || fail "rewind: the changed file was overwritten" [ ! -e "$T/ws12/c.txt" ] || fail "rewind: the rest was not restored after the yes" "$PIC" settings doubleEscapeAction tree > /dev/null drive12 rewindtree tui -c <<'EOF2' expect first edit send \e wait 100 send \e expect Conversation tree send \e send \x04\x04 EOF2 "$PIC" settings --unset doubleEscapeAction > /dev/null p12 check > /dev/null || fail "rewind: integrity" printf 'PASS: /rewind and Esc Esc pick a message and restore conversation and files; refusals ask first\n' # /remote-control ssh (and /rc ssh): the phone's ssh address, the command, the checks with their # fixes, as text; c copies the command (OSC 52 here), Esc closes. The host lookups are faked. /rc # itself, the relay's, is test/tui/rc.sh. if [ "$FAULTS" = yes ]; then export PIC_TEST_RC="-|testmac.local|0" drive40() { # NAME [pic args]: 100 by 40 "$DRIVE" "$T/$1.tx" 100 40 "$PIC" --db "$T/j8" --workspace "$T/ws8" --model fake/m || fail "$1: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$1.tx" | grep -v '^\s*$' | tail -8)" } drive40 remote <<'EOF2' expect pic fake/m send /rc ssh\r expect Remote control over ssh expect @testmac.local expect then run: cd expect attach -i -c --format text expect Remote Login is off. Turn on Remote Login: System Settings → General → Sharing expect install Tailscale to reach it from anywhere send c expect Copied the command send \x1b wait 300 send /remote-control ssh\r expect then run: send \x1b wait 300 send /rc sh\r expect /rc takes nothing, or ssh send /hotkeys\r expect Other: send \x04\x04 EOF2 unset PIC_TEST_RC ! grep -q '▀' "$T/remote.tx" || fail "remote: a QR code drawn for ssh" grep -aq "$(printf '\033]52;c;')" "$T/remote.tx" || fail "remote: c copied nothing" printf 'PASS: /remote-control ssh and /rc ssh show the ssh address, the command and the checks; c copies, Esc closes\n' fi