Artifact 4aec8de985612e6bbbaedfe2b6c1eeb81484666dadab920c5933f8bf6247b9a1:
- File
test/pic/sessions.sh
— part of check-in
[ffaf5a4afb]
at
2026-11-19 02:04:10
on branch stage-5
— Add follow-up conversations, forks and in-place instruction updates
Entries form a tree: a trigger links each new entry to its conversation head and moves the head, inside the inserting transaction. Requests are built from the path, so pic ask -c and an explicit id continue a conversation and pic fork branches one from a point that ends a turn. Each conversation freezes its opening prompt and tools; edited project instructions are appended once as an update at that point. New commands list, name and print conversations. Schema 5, with journals of earlier versions stepped up in place. (user: dmestas, size: 5055) [annotate] [blame] [check-ins using] [more...]
#!/bin/sh # Sessions: follow-ups continue a conversation, forks branch it, one request # at a time per session, the opening prompt is frozen and edited project # instructions are appended. Requests are checked at a fake server. # --no-faults skips the crash case (the release build has no crash hooks). set -eu PIC=${PIC:?set PIC to the pic binary} SERVER=${SERVER:?set SERVER to the fake_server binary} FAULTS=yes [ "${1:-}" = --no-faults ] && FAULTS=no STREAMS=$(cd "$(dirname "$0")/streams" && pwd) T=$(mktemp -d "${TMPDIR:-/tmp}/pic-sessions.XXXXXX") spid= cleanup() { [ -n "$spid" ] && kill "$spid" 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-sessions-test-key DB="$T/journal.sqlite" WS="$T/ws" mkdir "$WS" "$T/log" pic() { "$PIC" --db "$DB" --workspace "$WS" "$@"; } "$SERVER" "$T/port" "$T/log" 120 "sse:$STREAMS/text.sse" & spid=$! i=0 while [ ! -s "$T/port" ]; do i=$((i + 1)) [ "$i" -lt 100 ] || fail "server did not start" sleep 0.05 done URL="http://127.0.0.1:$(cat "$T/port")/v1/messages" run() { pic run --model anthropic/m --api-url "$URL" 2>> "$T/stderr" || fail "run: $(tail -2 "$T/stderr")"; } req() { ls "$T/log" | sort -n | tail -1 | sed "s|^|$T/log/|"; } A='{"role":"assistant","content":\[{"type":"text","text":"Hello, \\"world\\"\\n"}\]}' # A follow-up continues the conversation. printf 'rule one\n' > "$WS/AGENTS.md" pic submit k1 'first' > /dev/null run first=$(pic sessions | awk '{print $1}') [ -n "$first" ] || fail "no session listed" pic submit k2 'second' --session "$first" > /dev/null run grep -q "\"messages\":\[{\"role\":\"user\",\"content\":\"first\"},$A,{\"role\":\"user\",\"content\":\"second\"}\]" "$(req)" || fail "follow-up: earlier turns not sent" printf 'PASS: a follow-up carries the conversation so far\n' # One unfinished request per session. pic submit k3 'third' --session latest > /dev/null if pic submit k4 'fourth' --session "$first" 2> "$T/busy.err"; then fail "busy: second request admitted"; fi grep -q 'still working on a request' "$T/busy.err" || fail "busy: $(cat "$T/busy.err")" # Edit the project instructions mid-session: the opening prompt stays as it # was; the update is appended once, at this point of the conversation. printf 'rule two\n' > "$WS/AGENTS.md" run grep -q 'rule one' "$(req)" || fail "frozen: opening prompt changed" grep -q "{\"role\":\"user\",\"content\":\"third\"},{\"role\":\"user\",\"content\":\[{\"type\":\"text\",\"text\":\"<project_instructions_update>\\\\n<project_context>" "$(req)" || fail "instructions: update not appended after the new message" grep -q 'rule two' "$(req)" || fail "instructions: new text missing" [ "$(pic dump | grep -c '^entry .* instructions |')" = 1 ] || fail "instructions: not journaled once" pic submit k4 'fourth' --session "$first" > /dev/null run [ "$(pic dump | grep -c '^entry .* instructions |')" = 1 ] || fail "instructions: appended again without a change" printf 'PASS: one request at a time; prompt frozen, edited instructions appended once\n' # A fork continues from an earlier point; the other branch is not seen. answer1=$(pic dump | awk '/^entry [0-9]+ 1 answer /{print $2; exit}') fork=$(pic fork "$first" --at "$answer1") [ -n "$fork" ] && [ "$fork" != "$first" ] || fail "fork: no new session" pic submit k5 'alternative' --session "$fork" > /dev/null run grep -q "\"messages\":\[{\"role\":\"user\",\"content\":\"first\"},$A,{\"role\":\"user\",\"content\":\"alternative\"}\]" "$(req)" || fail "fork: branch does not continue from its point alone" if pic fork "$first" --at 1 2> "$T/fork.err"; then fail "fork: allowed at a user entry"; fi grep -q 'not a point to fork' "$T/fork.err" || fail "fork: $(cat "$T/fork.err")" printf 'PASS: a fork branches from its point; bad fork points are refused\n' # Listing, naming and the log. pic name "$first" main-line pic sessions | grep -q "^$first main-line " || fail "name: $(pic sessions)" [ "$(pic sessions | wc -l | tr -d ' ')" = 2 ] || fail "sessions: count" pic log "$fork" | grep -q 'you: alternative' || fail "log: fork" if pic log "$fork" | grep -q 'you: second'; then fail "log: fork shows the other branch"; fi pic log "$first" | grep -q 'project instructions updated' || fail "log: instructions" pic check > /dev/null || fail "integrity" printf 'PASS: sessions are listed, named and logged along their path\n' if [ "$FAULTS" = yes ]; then rc=0 env PIC_CRASH_AT=after-admit:1 "$PIC" --db "$DB" --workspace "$WS" submit k6 'after crash' --session "$first" \ > /dev/null 2>&1 || rc=$? [ "$rc" = 77 ] || fail "crash: expected a crash at after-admit, got $rc" [ "$(pic submit k6 'after crash' --session "$first")" = "duplicate: already admitted" ] || fail "crash: resubmit" run pic check > /dev/null || fail "crash: integrity" grep -q '"content":"fourth"},.*{"role":"user","content":"after crash"}' "$(req)" || fail "crash: conversation" printf 'PASS: a crash after joining a session recovers into it\n' fi