#!/bin/sh
# Terminal UI core session flow, pickers, commands, settings, import/export/tree/share, and prompts.
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
[ "${1:-}" = --no-faults ] && FAULTS=no
here=$(cd "$(dirname "$0")" && pwd)
S=$(cd "$here/../pic/streams" && pwd)
V=$(cd "$here/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tui-session.XXXXXX")
export HOME="$T/home"
. "$here/tui_lib.sh"
mkdir -p "$T/ws"
start_server "$T/port" "$T/log" 300 "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"
write_models "$T/port" "$T/models.json"
export PIC_MODELS_FILE="$T/models.json"
drive() { drive_with regular 80 24 "$T/j" "$T/ws" fake/m "$@"; }
drive first <<'EOF'
expect pic fake/m
send hello\r
expect Hello, "world"
expect hello ──
expect • hello
expect ↑25 ↓12
send write it\r
expect ✓ write t.txt
expect >
send /name main\r
expect named main
expect main ──
expect • 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 wait for me
expect 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'
# Deleting in the picker: d (or Delete, Backspace) deletes the selected conversation and u undoes
# it; space marks several for one d; the last one gone closes the picker; /restore and /trash bring
# them back.
drive trash <<'EOF'
expect pic fake/m
send /resume\r
expect d: delete
expect wait for me
send d
expect Deleted "wait for me". u to undo
send u
expect Restored "wait for me".
expect wait for me
send \x7f
expect Deleted "wait for me". u to undo
send u
expect Restored "wait for me".
send \x20\x20
wait 200
send d
expect Deleted 2 conversations. /trash brings them back.
expect no conversations yet
send /restore\r
expect /resume
send /trash\r
expect Deleted conversations (up/down, enter: restore, esc):
send \r
expect /resume
send /trash\r
expect no deleted conversations
send \x04\x04
EOF
[ "$("$PIC" --db "$T/j" --workspace "$T/ws" sessions | wc -l | tr -d ' ')" = 2 ] ||
fail "trash: not both back: $("$PIC" --db "$T/j" --workspace "$T/ws" sessions)"
"$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "trash: integrity"
printf 'PASS: d deletes in /resume, u undoes it; /restore and /trash bring conversations back\n'
# Archiving in the picker: a archives the selected conversation (out of /resume), /archived lists
# the archived ones, and a there unarchives one; the last one gone closes that picker.
drive archive <<'EOF'
expect pic fake/m
send /resume\r
expect a: archive
expect wait for me
send a
expect Archived "wait for me". /archived lists it.
send \x1b
send /archived\r
expect Archived conversations (up/down, enter: resume, a: unarchive, esc):
expect wait for me
send a
expect Unarchived "wait for me".
expect no archived conversations
send \x04\x04
EOF
[ -z "$("$PIC" --db "$T/j" --workspace "$T/ws" sessions archived)" ] || fail "archive: still archived"
[ "$("$PIC" --db "$T/j" --workspace "$T/ws" sessions | wc -l | tr -d ' ')" = 2 ] || fail "archive: not both listed"
printf 'PASS: a archives in /resume; /archived lists them and a unarchives\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'
# Quitting while the cancelled request is slow to let go (a loaded disk's
# fsync): the terminal waits a short bounded time, leaves, and the request
# finishes its cancel on its own; the journal is whole afterwards (#14).
if [ "$FAULTS" = yes ]; then
mkdir "$T/wsq"
now_ms() { perl -MTime::HiRes=time -e 'printf "%d\n", time * 1000'; }
# quit_slow NAME SECONDS: quit under a request that takes SECONDS to let go; the milliseconds it took
quit_slow() {
qn=$1
QLOG="$T/logq-$qn"
export QLOG
mkdir "$QLOG"
"$SERVER" "$T/portq-$qn" "$QLOG" 60 "hang:$S/text.sse:200" &
qpid=$!
i=0
while [ ! -s "$T/portq-$qn" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server q"; 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/portq-$qn")" > "$T/modelsq-$qn.json"
t0=$(now_ms)
PIC_MODELS_FILE="$T/modelsq-$qn.json" PIC_RUN_AT="before-cancel:sleep $2" "$DRIVE" "$T/$qn.tx" 80 24 "$PIC" --tui-mode regular \
--db "$T/j$qn" --workspace "$T/wsq" --model fake/m <<'EOF2' || fail "$qn: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$qn.tx" | grep -v '^\s*$' | tail -8)"
expect pic fake/m
send stuck\r
expect Working
waitfile $QLOG/1.req
send \x03
wait 100
send \x03
expect pic: conversation
EOF2
took=$(($(now_ms) - t0))
kill "$qpid" 2> /dev/null || :
}
PIC_TEST_QUIT_GRACE_MS=60000 quit_slow quitslow 15
[ "$took" -lt 11000 ] || fail "quit slow: leaving took ${took} ms with the request still finishing (it needs 15 s)"
i=0
until "$PIC" --db "$T/jquitslow" --workspace "$T/wsq" log 2> /dev/null | grep -q '(request cancelled)'; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "quit slow: the request never finished its cancel"
sleep 0.1
done
"$PIC" --db "$T/jquitslow" --workspace "$T/wsq" check > /dev/null || fail "quit slow: integrity"
# With a SessionEnd hook, leaving is no slower: the hook runs detached once the request lets go.
hh="$T/homeh"
mkdir -p "$hh/.pic"
cat > "$hh/hook.sh" <<'SH'
in=$(cat)
printf '%s %s\n' "$1" "$(printf '%s' "$in" | sed -n 's/.*"reason":"\([^"]*\)".*/\1/p')" >> "$(dirname "$0")/hooks.log"
SH
printf '{"hooks": {"SessionEnd": [{"hooks": [{"type": "command", "command": "sh %s/hook.sh SessionEnd"}]}]}}\n' "$hh" > "$hh/.pic/hooks.json"
home0=$HOME
HOME=$hh
PIC_TEST_QUIT_GRACE_MS=60000 quit_slow quithook 15
HOME=$home0
[ "$took" -lt 11000 ] || fail "quit hook: leaving took ${took} ms with a SessionEnd hook and the request still finishing"
i=0
until grep -q '^SessionEnd prompt_input_exit' "$hh/hooks.log" 2> /dev/null; do
i=$((i + 1))
[ "$i" -lt 600 ] || fail "quit hook: no SessionEnd after the request finished: $(cat "$hh/hooks.log" 2> /dev/null)"
sleep 0.1
done
# A request that does not let go on SIGINT is stopped by a detached watcher, not left holding the conversation.
PIC_TEST_QUIT_GRACE_MS=1030 quit_slow quitign 20
i=0
while pgrep -f -- "--db $T/jquitign " > /dev/null 2>&1; do
i=$((i + 1))
[ "$i" -lt 80 ] || fail "quit ignore: the request that ignored SIGINT was still running 8 s after quit"
sleep 0.1
done
"$PIC" --db "$T/jquitign" --workspace "$T/wsq" check > /dev/null || fail "quit ignore: integrity"
printf 'PASS: quitting leaves a slow-to-cancel request to finish on its own; the journal is whole\n'
fi
# 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
expect model anthropic/claude-haiku-5-5
send \ep
expect Select a model
send fake
expect → fake/m
send \r
expect model fake/m
send \ep
expect Select a model
send fake
expect ✓ fake/m
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 type to filter
send default thinking
expect → Default thinking level medium
send \e[C
expect → Default thinking level high
send \x15pi session
expect → Pi session files false
send \r
expect → Pi session files true
send \x15hide thinking
expect → Hide thinking false
send \r
expect → Hide thinking true
send \x15tool execution
expect → Tool execution parallel
send \r
expect → Tool execution sequential
send \x15auto-compact
expect → Auto-compact true
send \r
expect → Auto-compact false
send \e
wait 150
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 \e
wait 200
send /logout\r
expect d removes the selected key or sign-in
send \e
wait 200
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
expect KB as one HTML
expect gist.github.com through the gh command
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'