Artifact cdeab6618224b077da43e9b4934f05fb5314ceedbfd381a60fc87a05bfa1edf7:
- File
test/pic/export_pi.sh
— part of check-in
[193e5d1a30]
at
2026-11-19 12:01:55
on branch pi-export
— Write conversations as Pi v3 session files for ctx and Pi's tools
pic export --pi [DIR] writes each conversation as an append-only Pi v3 JSONL file named as Pi names them, by default under ~/.pic/sessions/ --<cwd>--/ and never in Pi's own folder: entry ids are journal entry numbers, a fork names its source file, instruction updates are custom_message entries, names are session_info, compactions are Pi compaction entries. Exporting again appends only finished entries after the last one written, after cutting a torn last line. ask and the line mode export their conversation after each request (PIC_PI_EXPORT=0 turns that off).
Also fixes compaction file lists: they were stored as JSON text rather than arrays, so a later compaction did not carry the earlier read and modified files forward. Lists already stored that way are read either way. (user: dmestas, size: 4917) [annotate] [blame] [check-ins using] [more...]
#!/bin/sh # pic export --pi: one append-only Pi v3 session file per conversation, in # Pi's shapes; exporting again adds only what is new, a torn last line is # repaired, a name is recorded once per change, a fork points at its source, # instructions and compactions use Pi's entry types, and nothing from an # unfinished request is written. Where ctx is installed, it reads the file. set -eu PIC=${PIC:?set PIC to the pic binary} T=$(mktemp -d "${TMPDIR:-/tmp}/pic-export-pi.XXXXXX") trap 'rm -rf -- "$T"' EXIT HUP INT TERM fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; } WS="$T/ws" OUT="$T/out" DB="$T/j.sqlite" mkdir "$WS" pic() { "$PIC" --db "$DB" --workspace "$WS" "$@"; } printf 'tool write notes.txt hello\ntext Wrote it.\n' > "$T/s1" pic submit k1 'make notes' > /dev/null pic run --script "$T/s1" id=$(pic sessions | awk '{print $1}') pic name "$id" notes pic export --pi "$OUT" f=$(ls "$OUT"/*_"$id".jsonl) || fail "file name" case "$(basename "$f")" in 20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]Z_"$id".jsonl) ;; *) fail "file name: $f" ;; esac head -1 "$f" | grep -q "^{\"type\":\"session\",\"version\":3,\"id\":\"$id\",\"timestamp\":\"[^\"]*\",\"cwd\":\"/[^\"]*/ws\"}\$" || fail "header: $(head -1 "$f")" sed -n 2p "$f" | grep -q '^{"type":"message","id":"1","parentId":null,"timestamp":"[^"]*Z","message":{"role":"user","content":\[{"type":"text","text":"make notes"}\],"timestamp":[0-9]*}}$' || fail "user: $(sed -n 2p "$f")" sed -n 3p "$f" | grep -q '"message":{"role":"assistant","content":\[{"type":"toolCall","id":"toolu_fake_0_0","name":"write","arguments":{"path":"notes.txt","content":"hello"}}\],"api":"anthropic-messages","provider":"script","model":"script","usage":{"input":0,' || fail "assistant: $(sed -n 3p "$f")" sed -n 3p "$f" | grep -q '"stopReason":"toolUse"' || fail "stop reason" sed -n 4p "$f" | grep -q '"message":{"role":"toolResult","toolCallId":"toolu_fake_0_0","toolName":"write","content":\[{"type":"text","text":"Successfully wrote to notes.txt"}\],"isError":false,' || fail "tool result: $(sed -n 4p "$f")" [ "$(grep -c '"type":"session_info".*"name":"notes"' "$f")" = 1 ] || fail "name" lines=$(wc -l < "$f") pic export --pi "$OUT" [ $(($(wc -l < "$f"))) = $((lines)) ] || fail "export again added lines" printf 'PASS: a conversation becomes a Pi v3 session file; exporting again adds nothing\n' # New entries append; a torn last line is cut first; a new name is recorded. printf '{"type":"message","id":"99","par' >> "$f" printf 'text Again.\n' > "$T/s2" pic submit k2 'again' --session "$id" > /dev/null pic run --script "$T/s2" pic name "$id" renamed pic export --pi "$OUT" grep -q '"id":"99"' "$f" && fail "torn line kept" [ $(($(wc -l < "$f"))) = $((lines + 3)) ] || fail "append: $(wc -l < "$f") lines, wanted $((lines + 3))" tail -1 "$f" | grep -q '"type":"session_info".*"name":"renamed"' || fail "rename" printf 'PASS: new entries are appended after repairing a torn last line\n' # Unfinished requests are held back; a fork points at its source file. pic submit k3 'pending' --session "$id" > /dev/null pic export --pi "$OUT" grep -q '"text":"pending"' "$f" && fail "unfinished request exported" fork=$(pic fork "$id" --at 4 2>/dev/null) || fork=$(pic fork "$id") pic export --pi "$OUT" g=$(ls "$OUT"/*_"$fork".jsonl) || fail "fork file" head -1 "$g" | grep -q "\"parentSession\":\"$f\"" || fail "fork: $(head -1 "$g")" printf 'PASS: unfinished requests are held back; a fork names its source\n' # Instructions and compactions use Pi's entry types. DB="$T/k.sqlite" printf 'rule one\n' > "$WS/AGENTS.md" cat > "$T/s3" <<'S' tool bash head -c 40000 /dev/zero | tr '\0' x tool bash head -c 40000 /dev/zero | tr '\0' y text done S printf 'text ok\n' > "$T/s0" pic submit c0 'small' > /dev/null pic run --script "$T/s0" printf 'rule two\n' > "$WS/AGENTS.md" pic submit c1 'big' --session latest > /dev/null pic --context-window 30000 run --script "$T/s3" 2> /dev/null pic export --pi "$OUT/k" h=$(ls "$OUT"/k/*.jsonl) grep -q '^{"type":"custom_message","id":"[0-9]*","parentId":"[0-9]*","timestamp":"[^"]*","customType":"pic.instructions","content":".*rule two.*","display":false}$' "$h" || fail "instructions: $(grep -c custom_message "$h") custom_message lines" grep -q '^{"type":"compaction","id":"[0-9]*","parentId":"[0-9]*","timestamp":"[^"]*","summary":"[^"]*scripted summary[^"]*","firstKeptEntryId":"[0-9]*","tokensBefore":[0-9]*,"details":{"readFiles":\[\],"modifiedFiles":\[\]}}$' "$h" || fail "compaction: $(grep compaction "$h" | cut -c1-300)" printf 'PASS: instruction updates and compactions use Pi'"'"'s entry types\n' if command -v ctx > /dev/null && ctx extract "$f" > "$T/ctx.txt" 2>&1; then grep -q 'make notes' "$T/ctx.txt" && grep -q 'Session name: renamed\|renamed' "$T/ctx.txt" || fail "ctx: $(head -20 "$T/ctx.txt")" printf 'PASS: ctx reads the exported file\n' fi