Skip to content
pi-c Artifact [d8db4a0e1c]
Login

Artifact [d8db4a0e1c]

Artifact d8db4a0e1c29d18541fd88b58b282ee69ccf66a398bb241d9fe3422c33d0f0ec:


#!/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")
# pic keeps user state under HOME: never the real one (the rows part below
# starts a fresh one).
export HOME="$T/home0"
mkdir -p "$HOME"
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":"script","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

# Rows: pic export --pi with no DIR writes the conversation to
# picu_pi_session_line in ~/.pic/pic.sqlite (compressed when smaller), the
# same lines as the file, once. The ~/.pic/sessions files are written only
# with piSessionFiles=1.
export HOME="$T/home"
mkdir -p "$HOME"
DB="$T/r.sqlite"
pic submit r1 'make notes' > /dev/null
pic run --script "$T/s1"
pic export --pi
[ ! -e "$HOME/.pic/sessions" ] || fail "files written by default: $(ls -R "$HOME/.pic/sessions")"
pic settings piSessionFiles 1
pic export --pi
f=$(ls "$HOME"/.pic/sessions/*/*.jsonl)
pic export --pi - > "$T/stdout.jsonl"
cmp -s "$f" "$T/stdout.jsonl" || fail "stdout: differs from the file"
rows() {
    sqlite3 "$HOME/.pic/pic.sqlite" "SELECT count(*) FROM picu_pi_session_line"
}
[ "$(rows)" = "$(wc -l < "$f" | tr -d ' ')" ] || fail "rows: $(rows) rows for $(wc -l < "$f") lines"
sqlite3 "$HOME/.pic/pic.sqlite" "SELECT count(*) FROM picu_pi_session_line WHERE sz > length(line)" | grep -qv '^0$' ||
    fail "rows: nothing compressed"
pic export --pi
[ "$(rows)" = "$(wc -l < "$f" | tr -d ' ')" ] || fail "rows: exporting again added rows"
pic settings piSessionFiles 0
pic submit r2 'again' --session latest > /dev/null
printf 'text Again.\n' > "$T/s4"
pic run --script "$T/s4"
before=$(wc -l < "$f" | tr -d ' ')
pic export --pi
[ "$(wc -l < "$f" | tr -d ' ')" = "$before" ] || fail "piSessionFiles=0: the file still grew"
[ "$(sqlite3 "$HOME/.pic/pic.sqlite" "SELECT count(*) FROM picu_pi_session_line")" -gt "$before" ] || fail "rows: new entries missing"
printf 'PASS: conversations are rows in pic.sqlite, compressed, once; files only when asked for\n'