#!/bin/sh
# pic rewind: back to an earlier user message, restoring the files write and
# edit changed after it (their pre-images, journaled with each call's start),
# the conversation (a fork before the message), or both. A file changed
# since pic last wrote it is refused, and nothing is done without
# --force-partial; files that did not exist are deleted; rewindMaxBytes caps
# the kept bytes; --undo puts a rewind's files back. A property run rewinds
# random write and edit sequences to random points and compares the tree;
# crashes at each new fault point (and at a write's own) are finished by the
# next pic. The RPC command too.
# --no-faults skips the crash cases (the release build has no crash hooks).
set -eu
PIC=${PIC:?set PIC to the pic binary}
SQL=${SQLITE3:-sqlite3}
FAULTS=yes
[ "${1:-}" = --no-faults ] && FAULTS=no
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-rewind.XXXXXX")
T=$(cd "$T" && pwd -P)
# pic keeps user state under HOME: never the real one.
export HOME="$T/home"
mkdir -p "$HOME"
cleanup() { [ -n "${KEEP:-}" ] && { echo "kept $T" >&2; return; }; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
n=0
fresh() { # a new journal, workspace and conversation
n=$((n + 1))
DB="$T/j$n.sqlite"
WS="$T/ws$n"
S=
K=0
mkdir "$WS"
}
pic() { "$PIC" --db "$DB" --workspace "$WS" "$@"; }
# crash POINT:N ARGS...: pic with a crash injected (not through pic(): an
# assignment before a function call outlives it in some shells).
crash() { c=$1; shift; PIC_CRASH_AT=$c "$PIC" --db "$DB" --workspace "$WS" "$@"; }
# req TEXT LINE...: one request in conversation S (a new one when unset),
# its script's turns the LINEs; the request's user entry in E.
req() {
text=$1
shift
K=$((K + 1))
printf '%s\n' "$@" > "$T/script"
if [ -z "$S" ]; then
pic submit "k$K" "$text" > /dev/null
else
pic submit "k$K" "$text" --session "$S" > /dev/null
fi
pic run --script "$T/script" > /dev/null 2> "$T/run.err" || fail "request $K: $(cat "$T/run.err")"
[ -n "$S" ] || S=$("$SQL" "$DB" "SELECT session_id FROM pic_submission WHERE request_key = 'k1'")
E=$("$SQL" "$DB" "SELECT min(e.seq) FROM pic_entry e JOIN pic_submission u ON u.id = e.submission_id WHERE u.request_key = 'k$K'")
}
# The workspace's files and their bytes, as one checksum.
tree() { (cd "$WS" && find . -type f | LC_ALL=C sort | while IFS= read -r f; do printf '%s ' "$f"; cksum < "$f"; done) | cksum; }
edit() { printf 'raw edit {"path":"%s","edits":[{"oldText":"%s","newText":"%s"}]}' "$1" "$2" "$3"; }
TAB=$(printf '\t')
# ---- files, conversation, both ----------------------------------------------
fresh
printf 'v0' > "$WS/a.txt"
req 'one' "tool write a.txt v1" "text done"
e1=$E
before2=$(tree)
req 'two' "tool $(edit a.txt v1 v2)${TAB}write sub/new.txt n" "text done"
e2=$E
req 'three' "tool write a.txt v3" "text done"
head=$("$SQL" "$DB" "SELECT head_seq FROM pic_session WHERE id = '$S'")
after=$(tree)
pic rewind "$S" --list > "$T/list"
[ "$(head -1 "$T/list" | sed 's/.*"text":"\([a-z]*\)".*/\1/')" = three ] || fail "list: newest first: $(cat "$T/list")"
grep -q "\"entry\":$e2,\"text\":\"two\",\"files\":2" "$T/list" || fail "list: files after two: $(cat "$T/list")"
grep -q "\"entry\":$e1,\"text\":\"one\",\"files\":2" "$T/list" || fail "list: files after one: $(cat "$T/list")"
out=$(pic rewind "$S" --to "$e2" --files) || fail "files: $out"
[ "$(cat "$WS/a.txt")" = v1 ] || fail "files: a.txt is $(cat "$WS/a.txt")"
[ ! -e "$WS/sub/new.txt" ] || fail "files: a file that did not exist was not deleted"
[ "$(tree)" = "$before2" ] || fail "files: tree differs from before request two"
printf '%s\n' "$out" | grep -qx 'restored a.txt' || fail "files: report: $out"
printf '%s\n' "$out" | grep -qx 'deleted sub/new.txt' || fail "files: report: $out"
[ "$("$SQL" "$DB" "SELECT head_seq FROM pic_session WHERE id = '$S'")" = "$head" ] || fail "files: the conversation moved"
[ "$("$SQL" "$DB" "SELECT count(*) FROM pic_session")" = 1 ] || fail "files: a conversation was made"
pic check > /dev/null || fail "files: integrity check"
printf 'PASS: --files restores edited files and deletes new ones, conversation untouched\n'
out=$(pic rewind "$S" --undo) || fail "undo: $out"
[ "$(tree)" = "$after" ] || fail "undo: tree differs from before the rewind"
out=$(pic rewind "$S" --undo) || fail "redo: $out"
[ "$(tree)" = "$before2" ] || fail "redo: an undo of the undo should rewind again"
pic rewind "$S" --undo > /dev/null || fail "undo again"
[ "$(tree)" = "$after" ] || fail "undo again: tree"
printf 'PASS: --undo puts a rewind back, and an undo of it rewinds again\n'
out=$(pic rewind "$S" --to "$e2" --conversation --json) || fail "conversation: $out"
f=$(printf '%s' "$out" | sed 's/.*"conversation":"\([0-9a-f]*\)".*/\1/')
[ -n "$f" ] && [ "$f" != "$S" ] || fail "conversation: no fork in $out"
printf '%s' "$out" | grep -q '"text":"two"' || fail "conversation: the message text: $out"
[ "$(tree)" = "$after" ] || fail "conversation: files changed"
fhead=$("$SQL" "$DB" "SELECT head_seq FROM pic_session WHERE id = '$f'")
parent=$("$SQL" "$DB" "SELECT parent_seq FROM pic_entry WHERE seq = $e2")
[ "$fhead" = "$parent" ] || fail "conversation: fork head $fhead, expected $parent"
[ "$("$SQL" "$DB" "SELECT head_seq FROM pic_session WHERE id = '$S'")" = "$head" ] || fail "conversation: history lost"
printf 'PASS: --conversation forks before the message, files and history untouched\n'
out=$(pic rewind "$S" --to "$e2" --json) || fail "both: $out"
g=$(printf '%s' "$out" | sed 's/.*"conversation":"\([0-9a-f]*\)".*/\1/')
[ -n "$g" ] && [ "$g" != "$S" ] && [ "$g" != "$f" ] || fail "both: no new fork in $out"
[ "$(tree)" = "$before2" ] || fail "both: files not restored"
[ "$("$SQL" "$DB" "SELECT forked FROM pic_restore ORDER BY id DESC LIMIT 1")" = "$g" ] || fail "both: restore does not name its fork"
printf 'PASS: both restores the files and forks, in one journaled restore\n'
# Further back in the fork: the files the rewind restored are its own.
out=$(pic rewind "$g" --to "$e1" --files) || fail "back in the fork: $out"
[ "$(cat "$WS/a.txt")" = v0 ] || fail "back in the fork: a.txt is $(cat "$WS/a.txt")"
pic rewind "$g" --undo > /dev/null || fail "back in the fork: undo"
[ "$(tree)" = "$before2" ] || fail "back in the fork: undo tree"
printf 'PASS: a fork made by a rewind rewinds further back over the files it restored\n'
# The first message: a new, empty conversation.
pic rewind "$S" --undo > /dev/null
out=$(pic rewind "$S" --to "$e1" --json) || fail "first: $out"
printf '%s' "$out" | grep -q '"conversation":""' || fail "first: expected a new conversation: $out"
[ "$(cat "$WS/a.txt")" = v0 ] || fail "first: a.txt is $(cat "$WS/a.txt")"
printf 'PASS: rewinding to the first message starts a new conversation and restores the original files\n'
# ---- refusals ---------------------------------------------------------------
fresh
printf 'v0' > "$WS/a.txt"
req 'one' "text ok"
e1=$E
req 'two' "tool write a.txt v1${TAB}write b.txt b1" "text done"
printf 'mine' > "$WS/a.txt"
if out=$(pic rewind "$S" --to "$e1" --files); then fail "foreign: not refused"; else rc=$?; fi
[ "$rc" = 3 ] || fail "foreign: exit $rc, expected 3"
printf '%s\n' "$out" | grep -q 'a.txt: it changed since pic last wrote it' || fail "foreign: report: $out"
[ "$(cat "$WS/b.txt")" = b1 ] || fail "foreign: something was restored without --force-partial"
[ "$("$SQL" "$DB" "SELECT count(*) FROM pic_restore")" = 0 ] || fail "foreign: a restore was journaled"
out=$(pic rewind "$S" --to "$e1" --files --json) && fail "foreign json: not refused" || :
printf '%s' "$out" | grep -q '"applied":false' || fail "foreign json: $out"
printf '%s' "$out" | grep -q '"refused":\[{"path":"a.txt","why":"it changed since pic last wrote it"}\]' || fail "foreign json: $out"
out=$(pic rewind "$S" --to "$e1" --files --force-partial) || fail "force: $out"
[ "$(cat "$WS/a.txt")" = mine ] || fail "force: the changed file was overwritten"
[ ! -e "$WS/b.txt" ] || fail "force: the rest was not restored"
printf '%s\n' "$out" | grep -q 'not restored a.txt: it changed since pic last wrote it' || fail "force: report: $out"
printf 'PASS: a file changed outside pic is refused; --force-partial restores the rest\n'
# A file another conversation wrote since is refused too.
fresh
req 'one' "text ok"
e1=$E
req 'two' "tool write c.txt mine" "text done"
mine=$S
S=
req 'other' "tool write c.txt theirs" "text done"
if pic rewind "$mine" --to "$e1" --files > "$T/out"; then fail "other conversation: not refused"; fi
grep -q 'c.txt: it changed since pic last wrote it' "$T/out" || fail "other conversation: $(cat "$T/out")"
[ "$(cat "$WS/c.txt")" = theirs ] || fail "other conversation: overwritten"
printf 'PASS: a file another conversation wrote since is refused\n'
# rewindMaxBytes: bytes past the cap are not kept, and the rewind says so.
fresh
"$PIC" settings rewindMaxBytes 8 > /dev/null
printf 'a long original line' > "$WS/big.txt"
printf 'short' > "$WS/small.txt"
req 'one' "text ok"
e1=$E
req 'two' "tool $(edit big.txt original changed)${TAB}write small.txt x" "text done"
[ "$("$SQL" "$DB" "SELECT pre_kept FROM pic_file_change WHERE path LIKE '%/big.txt'")" = 0 ] || fail "cap: big pre-image kept"
[ "$("$SQL" "$DB" "SELECT pre_kept FROM pic_file_change WHERE path LIKE '%/small.txt'")" = 1 ] || fail "cap: small pre-image not kept"
[ "$("$SQL" "$DB" "SELECT count(*) FROM pic_blob WHERE sz > 8")" = 0 ] || fail "cap: bytes past the cap stored"
[ "$("$SQL" "$DB" "SELECT length(pre_hash) FROM pic_file_change WHERE path LIKE '%/big.txt'")" = 64 ] ||
fail "cap: a file past the cap was not hashed"
pic rewind "$S" --to "$e1" --files > "$T/out" && fail "cap: not refused" || :
grep -q 'big.txt: its earlier contents were larger than rewindMaxBytes and were not kept' "$T/out" || fail "cap: $(cat "$T/out")"
pic rewind "$S" --to "$e1" --files --force-partial > /dev/null || fail "cap: force"
[ "$(cat "$WS/small.txt")" = short ] || fail "cap: small not restored"
[ "$(cat "$WS/big.txt")" = 'a long changed line' ] || fail "cap: big touched"
"$PIC" settings --unset rewindMaxBytes > /dev/null
printf 'PASS: rewindMaxBytes caps the kept bytes and the rewind names what it cannot restore\n'
# Errors: not a message, a missing conversation, bad usage.
pic rewind "$S" --to 99999 2> "$T/err" > /dev/null && fail "not a message: accepted" || :
grep -q 'is not a message of conversation' "$T/err" || fail "not a message: $(cat "$T/err")"
pic rewind "$S" --files 2> /dev/null > /dev/null && fail "usage: --files without --to accepted" || :
printf 'PASS: a bad entry or usage is refused\n'
# ---- review regressions -----------------------------------------------------
# A hand edit between two of pic's edits of one file: restoring the oldest
# "before" would lose it, so the file is refused.
fresh
printf 'line1 AAA\nline2 BBB\n' > "$WS/a.txt"
req one "text ok"
e1=$E
req two "tool $(edit a.txt AAA aaa)" "text done"
printf 'line1 aaa\nline2 BBB\nUSER WORK\n' > "$WS/a.txt"
req three "tool $(edit a.txt BBB bbb)" "text done"
kept=$(cat "$WS/a.txt")
if pic rewind "$S" --to "$e1" --files > "$T/out"; then fail "interleaved: restored over a hand edit: $(cat "$T/out")"; else rc=$?; fi
[ "$rc" = 3 ] || fail "interleaved: exit $rc, expected 3"
grep -q 'a.txt: it was changed outside pic between its changes' "$T/out" || fail "interleaved: $(cat "$T/out")"
[ "$(cat "$WS/a.txt")" = "$kept" ] || fail "interleaved: the file was touched"
printf 'PASS: a hand edit between two of pic'"'"'s changes refuses the file\n'
# The same hand edit, seen from a fork a rewind made: the edit that found it
# is not on the fork's path, so the restore that brought it over must link
# to what came before it there.
out=$(pic rewind "$S" --to "$E" --json) || fail "fork hole: both: $out"
C=$(printf '%s' "$out" | sed 's/.*"conversation":"\([0-9a-f]*\)".*/\1/')
grep -q 'USER WORK' "$WS/a.txt" || fail "fork hole: the both rewind lost the hand edit"
if pic rewind "$C" --to "$e1" --files > "$T/out"; then fail "fork hole: restored over a hand edit: $(cat "$T/out")"; else rc=$?; fi
[ "$rc" = 3 ] || fail "fork hole: exit $rc, expected 3"
grep -q 'a.txt: it was changed outside pic between its changes' "$T/out" || fail "fork hole: $(cat "$T/out")"
grep -q 'USER WORK' "$WS/a.txt" || fail "fork hole: the hand edit was lost"
printf 'PASS: a hand edit stays refused in a fork a rewind made\n'
# The current bytes are kept for an undo whatever their size.
fresh
"$PIC" settings rewindMaxBytes 30 > /dev/null
req four "text ok"
e4=$E
req five "tool write b.txt small" "text done"
req six "tool write b.txt THIS-IS-A-LONG-CONTENT-OVER-THIRTY-BYTES-USER-CARES" "text done"
pic rewind "$S" --to "$e4" --files > "$T/out" || fail "undo past cap: rewind: $(cat "$T/out")"
[ ! -e "$WS/b.txt" ] || fail "undo past cap: not deleted"
pic rewind "$S" --undo > "$T/out" || fail "undo past cap: undo refused: $(cat "$T/out")"
[ "$(cat "$WS/b.txt")" = THIS-IS-A-LONG-CONTENT-OVER-THIRTY-BYTES-USER-CARES ] || fail "undo past cap: content lost"
"$PIC" settings --unset rewindMaxBytes > /dev/null
printf 'PASS: a rewind keeps what it replaces whatever its size, so it can be undone\n'
# An undo skips rewinds that restored nothing (a conversation-and-files
# rewind with no file to put back).
fresh
printf 'v0' > "$WS/a.txt"
req one "text ok"
e1=$E
req two "tool write a.txt v1" "text done"
req three "text nothing written"
e3=$E
pic rewind "$S" --to "$e1" --files > /dev/null || fail "empty both: files"
pic rewind "$S" --to "$e3" > /dev/null || fail "empty both: both"
pic rewind "$S" --undo > "$T/out" || fail "empty both: undo: $(cat "$T/out")"
[ "$(cat "$WS/a.txt")" = v1 ] || fail "empty both: the files rewind was not undone"
printf 'PASS: an undo skips a rewind that restored no file\n'
# Two spellings of one file on a case-insensitive filesystem (macOS's
# default): the path is kept as the filesystem spells it, so both are one
# file with one record, and the rewind puts back the state before either.
fresh
printf 'v0' > "$WS/a.txt"
if [ -e "$WS/A.TXT" ]; then
req one "text ok"
e1=$E
req two "tool write a.txt v1" "text done"
req three "tool write A.txt v2" "text done"
[ "$("$SQL" "$DB" "SELECT count(DISTINCT path) FROM pic_file_change")" = 1 ] || fail "case: two records for one file"
pic rewind "$S" --to "$e1" --files > "$T/out" || fail "case: $(cat "$T/out")"
[ "$(cat "$WS/a.txt")" = v0 ] || fail "case: a.txt is $(cat "$WS/a.txt")"
printf 'mine' > "$WS/A.txt"
pic rewind "$S" --undo > "$T/out" && fail "case: undo over a change made through the other spelling" || :
[ "$(cat "$WS/a.txt")" = mine ] || fail "case: the change made through the other spelling was lost"
printf 'PASS: two spellings of one file on a case-insensitive filesystem are one file\n'
else
printf 'PASS: two spellings of one file (skipped: the filesystem is case-sensitive)\n'
fi
# ---- the property: random sequences, rewound to random points ---------------
seed=${REWIND_SEED:-$(date +%s)}
printf 'property seed %s (REWIND_SEED repeats it)\n' "$seed"
fresh
files="p.txt q.txt d/r.txt"
for f in $files; do eval "c_$(echo "$f" | tr -c 'a-z\n' _)=''"; done
printf 'base' > "$WS/p.txt"
c_p_txt=base
steps=12
i=0
while [ "$i" -lt "$steps" ]; do
i=$((i + 1))
eval "tree_$i=\$(tree)"
# 1 to 3 calls, each a write or (where the file has content) an edit.
calls=$(awk -v s="$seed" -v i="$i" 'BEGIN { srand(s * 100 + i); print int(rand() * 3) + 1 }')
line=
used=
j=0
while [ "$j" -lt "$calls" ]; do
j=$((j + 1))
pick=$(awk -v s="$seed" -v i="$i" -v j="$j" 'BEGIN { srand(s * 1000 + i * 10 + j); print int(rand() * 3) + 1, int(rand() * 2) }')
f=$(echo "$files" | cut -d' ' -f"${pick% *}")
case " $used " in *" $f "*) continue ;; esac # one call per file per turn
used="$used $f"
var=c_$(echo "$f" | tr -c 'a-z\n' _)
eval "cur=\$$var"
new="s${i}c${j}x$seed"
if [ "${pick#* }" = 1 ] && [ -n "$cur" ]; then
call=$(edit "$f" "$cur" "$new")
else
call="write $f $new"
fi
eval "$var=\$new"
line="${line:+$line$TAB}$call"
done
req "step $i" "tool $line" "text done"
eval "entry_$i=\$E"
done
final=$(tree)
for k in $(awk -v s="$seed" 'BEGIN { srand(s); for (x = 0; x < 6; x++) print int(rand() * 12) + 1 }'); do
eval "at=\$entry_$k want=\$tree_$k"
pic rewind "$S" --to "$at" --files > "$T/out" || fail "property (seed $seed): rewind to step $k: $(cat "$T/out")"
[ "$(tree)" = "$want" ] || fail "property (seed $seed): the tree after rewinding to step $k differs"
pic rewind "$S" --undo > "$T/out" || fail "property (seed $seed): undo: $(cat "$T/out")"
[ "$(tree)" = "$final" ] || fail "property (seed $seed): the tree after undoing step $k differs"
done
pic check > /dev/null || fail "property: integrity check"
printf 'PASS: random write and edit sequences rewind to the tree at each point, and undo back\n'
# ---- RPC --------------------------------------------------------------------
fresh
printf 'v0' > "$WS/a.txt"
req 'one' "text ok"
e1=$E
req 'two' "tool write a.txt v1" "text done"
printf '{"id":"r1","type":"rewind","entryId":"%s","mode":"files"}\n{"id":"r2","type":"get_state"}\n' "$e1" |
pic --mode rpc --session "$S" > "$T/rpc.out" 2> "$T/rpc.err" || fail "rpc: $(cat "$T/rpc.err")"
grep -q '"id":"r1","type":"response","command":"rewind","success":true' "$T/rpc.out" || fail "rpc: $(cat "$T/rpc.out")"
grep -q '"restored":\["a.txt"\]\|"restored":\["[^"]*/a.txt"\]' "$T/rpc.out" || fail "rpc: report: $(cat "$T/rpc.out")"
[ "$(cat "$WS/a.txt")" = v0 ] || fail "rpc: not restored"
printf '{"id":"r3","type":"rewind","entryId":"%s","mode":"sideways"}\n' "$e1" | pic --mode rpc --session "$S" > "$T/rpc.out" 2>&1
grep -q '"success":false,"error":"mode must be both, conversation or files"' "$T/rpc.out" || fail "rpc: bad mode: $(cat "$T/rpc.out")"
printf 'PASS: the RPC command rewind\n'
[ "$FAULTS" = yes ] || { printf 'PASS: rewind (crash cases skipped: --no-faults)\n'; exit 0; }
# ---- crashes ----------------------------------------------------------------
# A write's own points: the pre-image is on disk before the file is touched,
# a replay keeps it, and a rewind afterwards gives back the tree before.
for point in after-started after-exec after-result; do
fresh
printf 'v0' > "$WS/a.txt"
req 'one' "text ok"
before=$(tree)
K=$((K + 1))
printf 'tool write a.txt v1\ntool %s\ntext done\n' "$(edit a.txt v1 v2)" > "$T/script"
pic submit "k$K" two --session "$S" > /dev/null
set +e
crash "$point:1" run --script "$T/script" > /dev/null 2>&1
rc=$?
set -e
[ "$rc" = 77 ] || fail "$point: expected the injected crash, got $rc"
pic run --script "$T/script" > /dev/null 2> "$T/run.err" || fail "$point: finishing: $(cat "$T/run.err")"
[ "$(cat "$WS/a.txt")" = v2 ] || fail "$point: a.txt is $(cat "$WS/a.txt")"
pic check > /dev/null || fail "$point: integrity check"
e2=$("$SQL" "$DB" "SELECT min(e.seq) FROM pic_entry e JOIN pic_submission u ON u.id = e.submission_id WHERE u.request_key = 'k$K'")
pic rewind "$S" --to "$e2" --files > "$T/out" || fail "$point: rewind: $(cat "$T/out")"
[ "$(tree)" = "$before" ] || fail "$point: the tree after the rewind differs from before the request"
printf 'PASS: crash at %s, replay, then rewind gives back the tree\n' "$point"
done
# The restore's points: the next pic finishes it, the fork made once.
for point in restore-planned restore-wrote restore-done; do
fresh
printf 'v0' > "$WS/a.txt"
req 'one' "text ok"
req 'two' "tool write a.txt v1${TAB}write b.txt b1" "text done"
e2=$E
set +e
crash "$point:1" rewind "$S" --to "$e2" > /dev/null 2>&1
rc=$?
set -e
[ "$rc" = 77 ] || fail "$point: expected the injected crash, got $rc"
pic check > /dev/null || fail "$point: recovery"
[ "$(cat "$WS/a.txt")" = v0 ] || fail "$point: a.txt is $(cat "$WS/a.txt") after recovery"
[ ! -e "$WS/b.txt" ] || fail "$point: b.txt left after recovery"
[ "$("$SQL" "$DB" "SELECT state FROM pic_restore")" = done ] || fail "$point: restore not finished"
[ "$("$SQL" "$DB" "SELECT count(*) FROM pic_session")" = 2 ] || fail "$point: expected exactly one fork"
[ "$("$SQL" "$DB" "SELECT forked FROM pic_restore")" = "$("$SQL" "$DB" "SELECT id FROM pic_session WHERE id != '$S'")" ] ||
fail "$point: the restore does not name the fork"
pic rewind "$S" --undo > /dev/null || fail "$point: undo after recovery"
[ "$(cat "$WS/a.txt")" = v1 ] && [ "$(cat "$WS/b.txt")" = b1 ] || fail "$point: undo after recovery"
printf 'PASS: crash at %s, the next pic finishes the rewind and its fork, once\n' "$point"
done
# A file that changes between the check and the journaled restore is left,
# and so is one changed before the next pic finishes a crashed rewind.
fresh
printf 'v0' > "$WS/a.txt"
req one "text ok"
e1=$E
req two "tool write a.txt v1${TAB}write b.txt b1" "text done"
PIC_RUN_AT="restore-checked:printf theirs > '$WS/a.txt'" "$PIC" --db "$DB" --workspace "$WS" rewind "$S" --to "$e1" --files > "$T/out" ||
fail "check gap: $(cat "$T/out")"
[ "$(cat "$WS/a.txt")" = theirs ] || fail "check gap: overwrote a file changed after the check"
[ ! -e "$WS/b.txt" ] || fail "check gap: the rest was not restored"
grep -q 'not restored a.txt: it changed while the rewind ran' "$T/out" || fail "check gap: $(cat "$T/out")"
[ "$("$SQL" "$DB" "SELECT outcome FROM pic_file_change WHERE restore_id IS NOT NULL AND path LIKE '%/a.txt'")" = changed ] ||
fail "check gap: not journaled as changed"
fresh
req seven "text ok"
e7=$E
req eight "tool write c.txt pic" "text done"
set +e
crash restore-planned:1 rewind "$S" --to "$e7" --files > /dev/null 2>&1
set -e
echo mine > "$WS/c.txt"
pic check > /dev/null || fail "crash then edit: recovery"
[ "$(cat "$WS/c.txt")" = mine ] || fail "crash then edit: the next pic overwrote the user's file"
printf 'PASS: a file changed during or after an interrupted rewind is left alone\n'
printf 'PASS: rewind\n'