Skip to content
pi-c Artifact [3bdd638441]
Login

Artifact [3bdd638441]

Artifact 3bdd638441ff4f5fc4f692d98782f5856e2bda741854926e9bdcae3930d6d5b4:


#!/bin/sh
# Journals written by earlier pic versions open with the current one: the
# schema-3 fixture is migrated in place, passes pic check, keeps its history,
# and takes new work. Needs the sqlite3 command to build the fixture.
set -eu
PIC=${PIC:?set PIC to the pic binary}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-migrate.XXXXXX")
# pic keeps user state under HOME: never the real one.
export HOME="$T/home"
mkdir -p "$HOME"
trap 'rm -rf -- "$T"' EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
DB="$T/journal.sqlite"
mkdir "$T/ws"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic() { "$PIC" --db "$DB" --workspace "$T/ws" "$@"; }

pic check > /dev/null || fail "migrated journal fails pic check"
out=$(pic dump)
printf '%s\n' "$out" | grep -qF 'entry 2 1 assistant | [{"type":"thinking","thinking":"Write it.","signature":"SIG-OLD"},{"type":"toolCall","id":"toolu_old","name":"write","arguments":{"path":"notes.txt","content":"hi"}}]' ||
    fail "reply not converted to the neutral form"
printf '%s\n' "$out" | grep -q '^entry 4 1 answer | \[{"type":"text","text":"done"}\]$' || fail "answer"
printf '%s\n' "$out" | grep -q '^generation 1 1.0.1 done stop=toolUse' || fail "stop reasons not renamed"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 11 ] || fail "version not 11"
# Version 8 adds the queue of steering and follow-up messages, empty.
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_queued")" = 0 ] || fail "no empty pic_queued"
# Version 9 adds images and their attachments, empty.
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_image") $(sqlite3 "$DB" "SELECT count(*) FROM pic_attachment")" = "0 0" ] ||
    fail "no empty pic_image and pic_attachment"
# Every journal has the table of commands the user ran (pic bash), empty.
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_bash")" = 0 ] || fail "no empty pic_bash"
# Times older journals never wrote come from the ask key (054 ms, leading zero kept).
[ "$(sqlite3 "$DB" "SELECT group_concat(DISTINCT created_ms) FROM pic_entry WHERE submission_id = 1")" = 1795035064054 ] ||
    fail "entry times: $(sqlite3 "$DB" "SELECT group_concat(coalesce(created_ms, 'null')) FROM pic_entry")"
[ "$(sqlite3 "$DB" "SELECT created_ms FROM pic_session")" = 1795035064054 ] || fail "session time: $(sqlite3 "$DB" "SELECT created_ms FROM pic_session")"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_session")" = 1 ] || fail "submission not given a session"
[ "$(sqlite3 "$DB" "SELECT group_concat(coalesce(parent_seq, 0), ' ') FROM (SELECT parent_seq FROM pic_entry WHERE submission_id = 1 ORDER BY seq)")" = "0 1 2 3" ] ||
    fail "entries not chained"
[ "$(sqlite3 "$DB" "SELECT provider || '/' || model || ' ' || (native LIKE '%tool_use%') FROM pic_entry WHERE seq=2")" = \
    "anthropic/unknown 1" ] || fail "origin or native form"
printf 'tool write new.txt again\ntext done again\n' > "$T/script"
pic submit job-2 'more' > /dev/null
pic run --script "$T/script"
pic check > /dev/null || fail "journal broken after new work"
[ "$(cat "$T/ws/new.txt")" = again ] || fail "new work did not run"
printf 'PASS: a schema-3 journal migrates and keeps working\n'

# A version-8 journal (the last without images) steps up alone, keeps its
# rows, and takes new work.
DB="$T/v8.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v8: setup"
sqlite3 "$DB" "DROP TABLE pic_attachment; DROP TABLE pic_image; UPDATE pic_meta SET value='8' WHERE key='schema_version'"
pic check > /dev/null || fail "v8: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 11 ] || fail "v8: version not 11"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_entry")" = 4 ] || fail "v8: entries lost"
printf 'text done\n' > "$T/script"
pic submit job-3 'more' > /dev/null
pic run --script "$T/script" > /dev/null
pic check > /dev/null || fail "v8: journal broken after new work"
printf 'PASS: a schema-8 journal migrates and keeps working\n'

# A version-9 journal (images without sizes) is rebuilt: each image's size is
# read from its header and its type sniffed; one that no longer passes is
# pruned, its row kept.
DB="$T/v9.sqlite"
"$PIC" settings imageRetentionDays 0 > /dev/null # the fixture's dates must not age its image out
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v9: setup"
PNG=89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4890000000d49444154789c63f8cfc0f01f00050001ff89993d1d0000000049454e44ae426082
sqlite3 "$DB" "DROP TABLE pic_image; CREATE TABLE pic_image(hash TEXT PRIMARY KEY, mime TEXT NOT NULL, sz INTEGER NOT NULL, data BLOB NOT NULL);
  INSERT INTO pic_image VALUES('a', 'image/jpeg', 70, X'$PNG'), ('b', 'image/png', 3, X'000102');
  INSERT INTO pic_attachment(entry_seq, ordinal, image) VALUES(1, 0, 'a'), (1, 1, 'b');
  UPDATE pic_meta SET value='9' WHERE key='schema_version'"
pic dump > /dev/null || fail "v9: migrated journal does not open"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 11 ] || fail "v9: version not 11"
[ "$(sqlite3 "$DB" "SELECT group_concat(hash || ' ' || mime || ' ' || width || 'x' || height || ' ' || (data IS NULL), ', ') FROM pic_image")" = \
    'a image/png 1x1 0, b image/png 0x0 1' ] || fail "v9: images: $(sqlite3 "$DB" 'SELECT * FROM pic_image')"
printf 'PASS: a schema-9 journal gets image sizes and sniffed types\n'

# A version-10 journal (no hooks) with queued messages and images: pic_entry
# is rebuilt for the hook kind with every row, the queue and the images kept.
DB="$T/v10.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v10: setup"
HASH=$(sqlite3 :memory: "SELECT lower(hex(sha3(X'$PNG', 256)))")
sqlite3 "$DB" "INSERT INTO pic_image VALUES('$HASH', 'image/png', 1, 1, 70, X'$PNG');
  INSERT INTO pic_attachment(entry_seq, ordinal, image) VALUES(1, 0, '$HASH');
  INSERT INTO pic_queued(session_id, submission_id, kind, text, state, created_ms)
    SELECT session_id, id, 'follow_up', 'later, please', 'cleared', 0 FROM pic_submission WHERE id = 1;
  INSERT INTO pic_attachment(queued_id, ordinal, image) VALUES(last_insert_rowid(), 0, '$HASH');
  CREATE TABLE pic_entry_v10(seq INTEGER PRIMARY KEY, submission_id INTEGER NOT NULL,
    kind TEXT NOT NULL CHECK(kind IN ('user','assistant','tool_result','answer','instructions','compaction')),
    body TEXT NOT NULL, effect_id INTEGER, native TEXT, provider TEXT, api TEXT, model TEXT,
    parent_seq INTEGER, created_ms INTEGER, first_kept_seq INTEGER, tokens_before INTEGER);
  INSERT INTO pic_entry_v10 SELECT * FROM pic_entry;
  DROP TABLE pic_entry; ALTER TABLE pic_entry_v10 RENAME TO pic_entry;
  DROP TABLE pic_hook_run;
  UPDATE pic_meta SET value='10' WHERE key='schema_version'"
before=$(sqlite3 "$DB" "SELECT group_concat(seq || kind || parent_seq || body, '|') FROM pic_entry")
pic check > /dev/null || fail "v10: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 11 ] || fail "v10: version not 11"
[ "$(sqlite3 "$DB" "SELECT group_concat(seq || kind || parent_seq || body, '|') FROM pic_entry")" = "$before" ] || fail "v10: entries changed"
[ "$(sqlite3 "$DB" "SELECT text || ' ' || state FROM pic_queued")" = 'later, please cleared' ] || fail "v10: queue lost"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_attachment a JOIN pic_image i ON i.hash = a.image")" = 2 ] || fail "v10: images lost"
sqlite3 "$DB" "SELECT sql FROM sqlite_master WHERE name = 'pic_entry'" | grep -q "'hook'" || fail "v10: no hook kind"
sqlite3 "$DB" "SELECT count(*) FROM sqlite_master WHERE name = 'pic_entry_link'" | grep -q 1 || fail "v10: link trigger lost"
pic submit job-10 'after the migration' > /dev/null
pic run --script "$T/script" > /dev/null
pic check > /dev/null || fail "v10: journal broken after new work"
printf 'PASS: a schema-10 journal with queued messages and images migrates to hooks and keeps working\n'