Skip to content
pi-c Artifact [20699506f5]
Login

Artifact [20699506f5]

Artifact 20699506f5021c88c16e718b652de86c3992d8e464c8bd712b99238ecd5a590a:


#!/bin/sh
# /permissions in the terminal interface (docs/manual/permissions.md): before the first message
# it goes with that message; the footer names the mode while it is not allow; a call that asks is
# put to the person as a question with Allow, Allow for this conversation and Deny; /permissions
# alone says the mode, and /permissions allow takes the marker away. --permission-mode does the
# same from the command line.
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}
S=$(cd "$(dirname "$0")/../pic/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tui-perm.XXXXXX")
T=$(cd "$T" && pwd -P)
spid=
cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
unset PIC_TEST_PASSPHRASE
mkdir "$T/ws" "$T/log" "$T/home"
export HOME="$T/home" PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 T
export PIC_NO_NATIVE_CLIPBOARD=1 WS="$T/ws"
esc=$(printf '{"command":"echo ran >> %s/ledger"}' "$T" | sed 's/\\/\\\\/g; s/"/\\"/g')
{
    printf 'event: message_start\ndata: {"type":"message_start","message":{"id":"msg_01","type":"message","role":"assistant","model":"m","content":[],"stop_reason":null,"usage":{"input_tokens":25,"output_tokens":1}}}\n\n'
    printf 'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_b","name":"bash","input":{}}}\n\n'
    printf 'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"%s"}}\n\n' "$esc"
    printf 'event: content_block_stop\ndata: {"type":"content_block_stop","index":0}\n\n'
    printf 'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":40}}\n\n'
    printf 'event: message_stop\ndata: {"type":"message_stop"}\n\n'
} > "$T/bash.sse"
"$SERVER" "$T/port" "$T/log" 120 "at:/v1/models=status:404:-" "sse:$T/bash.sse" "sse:$S/text.sse" "sse:$T/bash.sse" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
port=$(cat "$T/port")
cat > "$T/models.json" <<EOM
{"providers":{"fake":{"baseUrl":"http://127.0.0.1:$port","api":"anthropic-messages","apiKey":"\$FAKE_API_KEY","models":[
  {"id":"m","contextWindow":200000}]}}}
EOM
export FAKE_API_KEY=pic-perm-fake
"$PIC" settings defaultModel fake/m > /dev/null
"$PIC" settings cacheRetention none > /dev/null
screen() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$1" | grep -v '^\s*$' | tail -12; }
drive() { # NAME ARGS...: pic on a pseudo-terminal, the script on stdin
    name=$1
    shift
    "$DRIVE" "$T/$name.tx" 120 30 "$PIC" --tui-mode regular --db "$T/j" --workspace "$T/ws" "$@" || fail "$name: $(screen "$T/$name.tx")"
}
q() { sqlite3 -cmd ".timeout 10000" "$T/j" "$1"; }

drive perm <<'EOF2'
expect pic fake/m
send /permissions ask-all\r
expect permission mode ask-all, from this conversation's first message
expect ask-all
send run it\r
expect Allow bash: echo ran >>
expect type to filter
send \r
expect answered
waitfile $T/ledger
expect Hello
send /permissions\r
expect permission mode ask-all: asks before every call that changes something
send /permissions allow\r
expect permission mode allow: asks nothing
send \x04\x04
EOF2
[ "$(q "SELECT mode FROM pic_permission")" = allow ] || fail "perm: the mode: $(q 'SELECT * FROM pic_permission')"
[ "$(q "SELECT state, answer, answered_by FROM pic_ask")" = 'answered|"Allow"|terminal' ] || fail "perm: the ask: $(q 'SELECT * FROM pic_ask')"
printf 'PASS: /permissions sets the mode, the footer names it, and a call that asks is answered in the interface\n'

rm -f "$T/ledger"
drive flag -c --permission-mode read-only <<'EOF2'
expect read-only
send again\r
expect read-only (its permission mode)
expect Hello
send \x04\x04
EOF2
[ ! -f "$T/ledger" ] || fail "flag: the command ran"
[ "$(q "SELECT mode FROM pic_permission")" = read-only ] || fail "flag: the mode: $(q 'SELECT * FROM pic_permission')"
printf 'PASS: --permission-mode gives the conversation its mode at the next message\n'