Skip to content
pi-c Artifact [2e0b51f41f]
Login

Artifact [2e0b51f41f]

Artifact 2e0b51f41fa8c66654d43be33b16a0471304b7c4becf243c3b3b1a851a9fa0e9:


#!/bin/sh
# Ctrl-C (SIGINT) cancels the running request and nothing else: a running
# command is aborted with its output kept, a provider request stops waiting,
# the request ends cancelled with the journal intact, other requests stay
# queued; a second Ctrl-C exits at once and the next run recovers.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
S=$(cd "$(dirname "$0")/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-interrupt.XXXXXX")
# pic keeps user state under HOME: never the real one.
export HOME="$T/home"
mkdir -p "$HOME"
spid=
pid=
cleanup() {
    [ -n "$spid" ] && kill "$spid" 2>/dev/null
    [ -n "$pid" ] && kill -9 "$pid" 2>/dev/null
    rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
export ANTHROPIC_API_KEY=pic-interrupt-test-key
WS="$T/ws"
mkdir "$WS"
DB="$T/j.sqlite"
pic() { "$PIC" --db "$DB" --workspace "$WS" "$@"; }
await() { # FILE PATTERN: wait up to 10s for PATTERN in FILE
    i=0
    until grep -q "$2" "$1" 2>/dev/null; do
        i=$((i + 1))
        [ "$i" -lt 200 ] || fail "timed out waiting for $2 in $1: $(cat "$1" 2>/dev/null)"
        sleep 0.05
    done
}
gone() { # PID: wait up to 10s for it to exit, then its status
    i=0
    while kill -0 "$1" 2>/dev/null; do
        i=$((i + 1))
        [ "$i" -lt 200 ] || fail "pic did not stop"
        sleep 0.05
    done
    rc=0
    wait "$1" || rc=$?
    pid=
}

# A running command: aborted, its output kept, the request cancelled.
printf 'tool bash echo started; sleep 30\ntool write never.txt x\ntext done\n' > "$T/script"
pic submit k1 'run it' > /dev/null
pic submit k2 'queued' --session latest > /dev/null 2>&1 || pic submit k2 'queued' > /dev/null
"$PIC" --db "$DB" --workspace "$WS" --stream run --script "$T/script" > "$T/out" 2> "$T/err" &
pid=$!
await "$T/err" '^-> bash'
sleep 0.3
kill -INT "$pid"
gone "$pid"
[ "$rc" != 0 ] || fail "command: exit status 0 after a cancel"
grep -q 'cancelled' "$T/err" || fail "command: $(cat "$T/err")"
pic dump | grep -q '^submission 1 k1 failed | run it error=cancelled' || fail "command: $(pic dump | grep '^submission')"
pic dump | grep -q 'started.*Command aborted' || pic dump | grep -A2 'bash done' | grep -q 'Command aborted' || fail "command: result: $(pic dump | grep -A3 '^effect')"
pic dump | grep -q '^effect .* write skipped' && fail "command: a later call was planned"
[ ! -e "$WS/never.txt" ] || fail "command: a later call ran"
pic dump | grep -q '^submission 2 k2 admitted' || fail "command: the queued request was touched"
pic check > /dev/null || fail "command: integrity"
pic log | grep -q '(request cancelled)' || fail "command: log does not show the cancel: $(pic log | tail -3)"
printf 'PASS: Ctrl-C aborts the running command and cancels only that request\n'

# A provider request that hangs: Ctrl-C stops waiting and cancels.
"$SERVER" "$T/port" "$T/log" 60 "hang:$S/text.sse:200" &
spid=$!
mkdir -p "$T/log"
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
URL="http://127.0.0.1:$(cat "$T/port")/v1/messages"
"$PIC" --db "$DB" --workspace "$WS" --json run --model anthropic/m --api-url "$URL" > "$T/ev" 2> "$T/err" &
pid=$!
await "$T/ev" '"turn_start"'
sleep 0.5
kill -INT "$pid"
gone "$pid"
grep -q '"stopReason":"aborted","errorMessage":"interrupted"' "$T/ev" || fail "provider: $(tail -3 "$T/ev")"
grep -q '"type":"agent_end","messages":\[\],"request":"k2","status":"failed","errorMessage":"cancelled"' "$T/ev" ||
    fail "provider: agent_end: $(tail -1 "$T/ev")"
pic check > /dev/null || fail "provider: integrity"
printf 'PASS: Ctrl-C stops a provider request and cancels\n'

# A second Ctrl-C exits at once; the next run recovers the request.
pic submit k3 'again' > /dev/null
printf 'tool bash sleep 30\ntext done\n' > "$T/script2"
"$PIC" --db "$DB" --workspace "$WS" run --script "$T/script2" > /dev/null 2> "$T/err" &
pid=$!
sleep 1
kill -INT "$pid"
kill -INT "$pid" 2>/dev/null || :
gone "$pid"
pic check > /dev/null || fail "twice: integrity"
printf 'PASS: a second Ctrl-C exits at once and the journal holds\n'

kill "$spid" 2> /dev/null || :

# A prompt cancelled before its reply stays in the conversation marked as interrupted, so the
# model does not take it for the question to answer with the next one.
DB="$T/j2.sqlite"
mkdir -p "$T/log2"
"$SERVER" "$T/port2" "$T/log2" 60 "hang:$S/text.sse:200" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port2" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server 2"; sleep 0.05; done
URL="http://127.0.0.1:$(cat "$T/port2")/v1/messages"
pic submit c1 'write an essay on tides' > /dev/null
"$PIC" --db "$DB" --workspace "$WS" --json run --model anthropic/m --api-url "$URL" > "$T/ev2" 2> "$T/err2" &
pid=$!
await "$T/ev2" '"turn_start"'
sleep 0.5
kill -INT "$pid"
gone "$pid"
grep -q '"errorMessage":"cancelled"' "$T/ev2" || fail "marked: not cancelled: $(tail -1 "$T/ev2")"
pic submit c2 'say hi' --session latest > /dev/null
pic run --model anthropic/m --api-url "$URL" > /dev/null 2> "$T/err2" || fail "marked: run: $(cat "$T/err2")"
grep -q 'write an essay on tides\\n\\n\[Request interrupted by user\]' "$T/log2/2.req" ||
    fail "marked: the next request: $(tail -c 600 "$T/log2/2.req")"
grep -q 'say hi' "$T/log2/2.req" || fail "marked: the new prompt"
[ "$(grep -c 'interrupted by user' "$T/log2/2.req")" = 1 ] || fail "marked: twice"
pic log | grep -q 'interrupted by user' && fail "marked: the journal's text changed"
printf 'PASS: a prompt cancelled before its reply goes to the model marked as interrupted\n'