Artifact 78689fb19ee6162f614b1574ec834900575bf4f009c7e9518b00b1e4cc6b034e:
- File
test/pic/crash.sh
— part of check-in
[c5ab59d7d7]
at
2026-11-18 19:37:32
on branch stage-3
— Adopt Pi's default tools: read, write, edit and bash
Tools take Pi's inputs and return Pi's truncation notices and messages. Edit computes the new file first and journals it with started, so a replay after a crash writes the same bytes and edit joins read and write as replay-safe. Bash keeps the tail of its output, saves the full output when it has to cut, and reports Pi's status lines. Results carry a journaled error flag, cancelled calls are skipped with a result, and the schema moves to version 3. (user: dmestas, size: 10307) [annotate] [blame] [check-ins using] [more...]
#!/bin/sh # Crash at every commit and effect boundary, reopen, finish, and check that the # journal and the outside world agree. --no-faults runs only the paths that need # no crash hooks (the release build has none). set -eu PIC=${PIC:?set PIC to the pic binary} FAULTS=yes [ "${1:-}" = --no-faults ] && FAULTS=no T=$(mktemp -d "${TMPDIR:-/tmp}/pic-crash.XXXXXX") trap 'rm -rf -- "$T"' EXIT HUP INT TERM fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; } # The exec appends one line to a ledger OUTSIDE the workspace: the external # effect whose count proves whether a replay-unsafe tool ran twice. cat > "$T/script" <<EOF tool write notes.txt hello from pic tool exec printf 'ran\n' >> "$T/ledger" text done EOF fresh() { # fresh NAME: a new, empty database, workspace, and ledger DB="$T/$1.sqlite" WS="$T/$1-ws" mkdir "$WS" : > "$T/ledger" } pic() { "$PIC" --db "$DB" --workspace "$WS" "$@"; } lines() { wc -l < "$T/ledger" | tr -d ' '; } verify() { # verify CASE EXPECTED_LEDGER_LINES EXPECTED_EXEC_STATE [FAILED_GENERATIONS] pic check > /dev/null || fail "$1: integrity check" out=$(pic dump) [ "$(printf '%s\n' "$out" | grep -c '^submission ')" = 1 ] || fail "$1: submission count" printf '%s\n' "$out" | grep -q '^submission 1 job-1 answered ' || fail "$1: not answered" [ "$(printf '%s\n' "$out" | grep -c ' answer | \[{"type":"text","text":"done"}\]$')" = 1 ] || fail "$1: answer count" [ "$(cat "$WS/notes.txt")" = "hello from pic" ] || fail "$1: written file" printf '%s\n' "$out" | grep -q "^effect 2 .* bash $3 safe=0 " || fail "$1: exec effect not $3" [ "$(lines)" = "$2" ] || fail "$1: ledger has $(lines) lines, expected $2" failed=$(printf '%s\n' "$out" | grep -c '^generation .* failed' || true) [ "$failed" = "${4:-0}" ] || fail "$1: $failed failed generations, expected ${4:-0}" [ "$(pic submit job-1 'make notes')" = "duplicate: already admitted" ] || fail "$1: resubmit" if pic submit job-1 'different text' 2>/dev/null; then fail "$1: key reuse accepted"; fi printf 'PASS: %s (ledger=%s exec=%s failed-generations=%s)\n' "$1" "$2" "$3" "${4:-0}" } # Clean run. fresh clean pic submit job-1 'make notes' > /dev/null pic run --script "$T/script" verify clean 1 done # A concurrently running pic must be refused immediately, not queued. fresh fence pic submit job-1 'make notes' > /dev/null printf 'tool exec sleep 3\ntext done\n' > "$T/slow" pic run --script "$T/slow" & holder=$! sleep 1 if pic run --script "$T/slow" 2> "$T/fence.err"; then fail "second writer was admitted"; fi grep -q 'owned by another pic process' "$T/fence.err" || fail "second writer error: $(cat "$T/fence.err")" wait "$holder" printf 'PASS: second writer refused while the first runs\n' effect_result() { pic dump | grep '^effect 1 ' | sed 's/^[^|]*| //'; } run_one() { # run_one NAME SCRIPT_LINE [pic options]: fresh db, run one exec, then answer script="$T/$1.script" fresh "$1" printf '%s\ntext done\n' "$2" > "$script" shift 2 pic submit job-1 'make notes' > /dev/null "$PIC" --db "$DB" --workspace "$WS" "$@" run --script "$script" } # The deadline is enforced even though the command keeps running. start=$(date +%s) run_one deadline 'tool exec sleep 30' --exec-timeout 1 [ $(( $(date +%s) - start )) -lt 10 ] || fail "deadline: exec ran past its timeout" pic dump | grep -q '^Command timed out after 1 seconds$' || fail "deadline: result $(effect_result)" printf 'PASS: exec deadline enforced\n' # Closing or redirecting stdout must not end (or kill) the command early. run_one redirect 'tool exec exec >/dev/null 2>&1; sleep 1; touch finished' [ -f "$WS/finished" ] || fail "redirect: command was cut short" [ "$(effect_result)" = '(no output)' ] || fail "redirect: result $(effect_result)" printf 'PASS: exec waits for the command, not its output\n' # A background child holding the output pipe neither hangs pic nor outlives it. start=$(date +%s) run_one background 'tool exec (sleep 2; touch bgdone) & echo started' [ $(( $(date +%s) - start )) -lt 2 ] || fail "background: pic waited for a background child" sleep 3 [ ! -f "$WS/bgdone" ] || fail "background: descendant outlived the command" printf 'PASS: background descendants are killed with the command\n' # If pic dies mid-command, the command dies too, before any restarted pic # records its outcome as unknown. fresh orphan printf 'tool exec sleep 2; touch late\ntext done\n' > "$T/orphan.script" pic submit job-1 'make notes' > /dev/null # The binary itself, not the pic() function: $! of a backgrounded function is a # subshell, and killing that would leave pic running. "$PIC" --db "$DB" --workspace "$WS" run --script "$T/orphan.script" & victim=$! sleep 1 kill -9 "$victim" wait "$victim" 2>/dev/null || true pic run --script "$T/orphan.script" pic dump | grep -q '^effect 1 .* bash unknown safe=0 ' || fail "orphan: effect not unknown" sleep 3 [ ! -f "$WS/late" ] || fail "orphan: command outlived a crashed pic" printf 'PASS: crashed pic takes its running command with it\n' # A Ctrl-C (SIGINT to pic's whole process group, as a terminal sends it) must # not orphan the command either. perl starts pic as its own group leader so the # signal does not also hit this script, and restores SIGINT: a non-interactive # shell starts background jobs with SIGINT ignored. fresh interrupt printf 'tool exec sleep 2; touch late\ntext done\n' > "$T/interrupt.script" pic submit job-1 'make notes' > /dev/null perl -e '$SIG{INT} = "DEFAULT"; setpgrp(0, 0); exec @ARGV or die' "$PIC" --db "$DB" --workspace "$WS" \ run --script "$T/interrupt.script" 2>/dev/null & leader=$! sleep 1 kill -INT -"$leader" wait "$leader" 2>/dev/null || true pic run --script "$T/interrupt.script" pic dump | grep -q '^effect 1 .* bash unknown safe=0 ' || fail "interrupt: effect not unknown" sleep 3 [ ! -f "$WS/late" ] || fail "interrupt: command outlived a Ctrl-C" printf 'PASS: Ctrl-C to the group takes the running command with it\n' # A symlink to a running pic's database is the same database: refused at once. printf 'tool exec sleep 5\ntext done\n' > "$T/slower" fresh alias pic submit job-1 'make notes' > /dev/null ln -s "$DB" "$T/alias-link.sqlite" pic run --script "$T/slower" & holder=$! sleep 1 if "$PIC" --db "$T/alias-link.sqlite" --workspace "$WS" run --script "$T/slower" 2> "$T/alias.err"; then fail "alias: second writer through a symlink was admitted" fi grep -q 'owned by another pic process' "$T/alias.err" || fail "alias error: $(cat "$T/alias.err")" wait "$holder" printf 'PASS: second writer refused through a symlinked path\n' # Provider failures: transient ones are retried as new journaled attempts, up # to three per turn; others fail the submission at once with the error kept. outcome() { # outcome NAME SCRIPT_LINE WANT: run one turn; check status and failed generations fresh "$1" printf '%s\n' "$2" > "$T/$1.script" pic submit job-1 'make notes' > /dev/null pic run --script "$T/$1.script" 2> "$T/$1.err" || true pic check > /dev/null || fail "$1: integrity check" got="$(pic dump | sed -n 's/^submission 1 job-1 \([a-z]*\) .*/\1/p') $(pic dump | grep -c '^generation .* failed' || true)" [ "$got" = "$3" ] || fail "$1: got $got, expected $3" } outcome flaky 'flaky 2 text done' 'answered 2' outcome exhausted 'flaky 9 text done' 'failed 3' grep -q 'gave up after 3 provider attempts' "$T/exhausted.err" || fail "exhausted: $(cat "$T/exhausted.err")" outcome reject 'reject bad request' 'failed 1' pic dump | grep -q '^submission 1 job-1 failed .* error=scripted rejection: bad request$' || fail "reject: error lost" outcome refuse 'refuse' 'failed 0' pic dump | grep -q '^entry 2 1 assistant | \[\]$' || fail "refuse: refusal not journaled" printf 'PASS: provider failures are retried, capped and recorded\n' # Several tool calls in one assistant turn run in call order, once each. printf 'tool write notes.txt hello from pic\texec printf '"'"'ran\\n'"'"' >> "%s/ledger"\ntext done\n' "$T" \ > "$T/parallel.script" fresh parallel pic submit job-1 'make notes' > /dev/null pic run --script "$T/parallel.script" pic dump | grep -q '^effect 1 2\.0 write done' || fail "parallel: first call" pic dump | grep -q '^effect 2 2\.1 bash done' || fail "parallel: second call" verify parallel 1 done [ "$FAULTS" = yes ] || exit 0 # point:n expected-ledger-lines expected-exec-state failed-generations # A crash after the exec is marked started but before its result commits leaves # its outcome unknown: it is never run again (ledger 0 or 1, never 2). A crash # while a generation is outstanding fails that attempt; the turn is asked again. crash_matrix() { # crash_matrix SCRIPT LABEL < cases while read -r point ledger state generations; do name=$(printf '%s-%s' "$2" "$point" | tr ':' '-') fresh "$name" if [ "$point" = after-admit:1 ]; then # env, not a prefix assignment: POSIX lets an assignment before a shell # function persist, which would crash every later command. if env PIC_CRASH_AT="$point" "$PIC" --db "$DB" --workspace "$WS" submit job-1 'make notes' \ > /dev/null 2>&1; then fail "$point: no crash"; fi else pic submit job-1 'make notes' > /dev/null if env PIC_CRASH_AT="$point" "$PIC" --db "$DB" --workspace "$WS" run --script "$1" \ 2>/dev/null; then fail "$name: no crash"; fi fi pic submit job-1 'make notes' > /dev/null pic run --script "$1" verify "$name" "$ledger" "$state" "$generations" done } crash_matrix "$T/script" serial <<'EOF' after-admit:1 1 done 0 after-generation-started:1 1 done 1 after-generation-started:2 1 done 1 after-generation-started:3 1 done 1 after-reply:1 1 done 1 after-reply:2 1 done 1 after-reply:3 1 done 1 after-assistant:1 1 done 0 after-assistant:2 1 done 0 after-started:1 1 done 0 after-started:2 0 unknown 0 after-exec:1 1 done 0 after-exec:2 1 unknown 0 after-result:1 1 done 0 after-result:2 1 done 0 after-answer:1 1 done 0 EOF crash_matrix "$T/parallel.script" parallel <<'EOF' after-reply:1 1 done 1 after-assistant:1 1 done 0 after-started:1 1 done 0 after-started:2 0 unknown 0 after-exec:2 1 unknown 0 after-result:1 1 done 0 after-result:2 1 done 0 EOF