Skip to content
pi-c Artifact [a4725e0283]
Login

Artifact [a4725e0283]

Artifact a4725e02831d8d09403398f4352023a2c23f2465cb88b69a22672b3b99c336a2:


#!/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 100 ] || 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":"error","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'