Artifact c6fe2a6ee4c0268046db363c6fd2feda1b9dbe3323c34c90d0ebe73a57642aa5:
- File
test/pic/crash.sh
— part of check-in
[7e7550292a]
at
2026-11-18 12:30:18
on branch tracer-bullet
— Add the first durable agent loop in C
pic keeps its journal in its own libfossil-hosted SQLite database: an idempotent admission per request key, then one short transaction per state change, never held across provider or tool I/O. A replay-safe write tool reruns after a crash; a replay-unsafe exec records an unknown outcome and never reruns. exec runs under a supervisor that starts the command only after the started commit and kills its group if pic dies or is interrupted. A second pic on the same database is refused.
test/pic/crash.sh crashes at every commit and effect boundary and checks the journal against an external ledger, plus deadline, output redirection, background child, crash and interrupt orphaning, and second-writer cases. Vendored SQLite and libfossil match ctx byte for byte. (user: dmestas, size: 7617) [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 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 | 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 .* exec $3 safe=0 " || fail "$1: exec effect not $3" [ "$(lines)" = "$2" ] || fail "$1: ledger has $(lines) lines, expected $2" [ "$(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)\n' "$1" "$2" "$3" } # 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" effect_result | grep -q '^timeout after 1s' || 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 | grep -q '^exit=0' || 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 .* exec 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 .* exec 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' [ "$FAULTS" = yes ] || exit 0 # point:n expected-ledger-lines expected-exec-state # 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). while read -r point ledger state; do name=$(printf '%s' "$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 "$T/script" \ 2>/dev/null; then fail "$point: no crash"; fi fi pic submit job-1 'make notes' > /dev/null pic run --script "$T/script" verify "$point" "$ledger" "$state" done <<'EOF' after-admit:1 1 done after-assistant:1 1 done after-assistant:2 1 done after-started:1 1 done after-started:2 0 unknown after-exec:1 1 done after-exec:2 1 unknown after-result:1 1 done after-result:2 1 done after-answer:1 1 done EOF