fix(pi): restore history from transcripts with unlinked metadata

The transcript reader rejected title and model metadata without tree links before it could replay saved messages.
This commit is contained in:
paseo-bot[bot]
2026-11-30 03:13:31 +00:00
parent c77a53ffdc
commit fbc80bbce5
3 changed files with 35 additions and 10 deletions
@@ -346,6 +346,23 @@ test("replays both sides of compaction with native IDs and isolates rewound bran
).toEqual(["u1"]);
});
test.each([
{ type: "session_info", name: "Automatic title", autoTitle: true },
{ type: "model_change", id: "model", provider: "test", modelId: "test-model" },
])("keeps saved messages when $type metadata has no ancestry", (metadata) => {
const userMessage = { role: "user", content: "hello" };
const rows = [
{ type: "session", version: 3, id: "session" },
{ ...metadata, timestamp: "2026-11-30T00:00:01.000Z" },
{ type: "message", id: "u1", parentId: null, message: userMessage },
{ ...metadata, timestamp: "2026-11-30T00:00:03.000Z" },
];
const history = readPiTranscript(rows.map((row) => JSON.stringify(row)).join("\n"));
expect(history.messages).toEqual([userMessage]);
expect(history.userEntries).toEqual([{ id: "u1", text: "hello" }]);
});
test("preserves native custom entries and legacy linear messages without inventing native IDs", async () => {
const custom = {
type: "custom_message",
@@ -111,22 +111,28 @@ export function readPiTranscript(contents: string): PiTranscript {
}
function activeBranch(records: z.infer<typeof record>[]): z.infer<typeof record>[] {
const byId = new Map<string, (typeof records)[number]>();
for (const entry of records) {
if (!entry.id || entry.parentId === undefined || byId.has(entry.id))
type TreeEntry = z.infer<typeof record> & { id: string; parentId: string | null };
const byId = new Map<string, TreeEntry>();
let entry: TreeEntry | undefined;
for (const row of records) {
const { id, parentId } = row;
if (!id || parentId === undefined) {
// Older Pi transcripts also store title and model metadata outside the tree.
if (row.type === "session_info" || row.type === "model_change") continue;
throw new Error("Invalid Pi transcript tree entry");
byId.set(entry.id, entry);
}
if (byId.has(id)) throw new Error("Invalid Pi transcript tree entry");
entry = { ...row, id, parentId };
byId.set(id, entry);
}
const branch: z.infer<typeof record>[] = [];
const branch: TreeEntry[] = [];
const seen = new Set<string>();
let entry = records.at(-1);
while (entry) {
const id = entry.id!;
if (seen.has(id)) throw new Error("Cycle in Pi transcript ancestry");
seen.add(id);
if (seen.has(entry.id)) throw new Error("Cycle in Pi transcript ancestry");
seen.add(entry.id);
branch.push(entry);
if (entry.parentId === null) break;
entry = byId.get(entry.parentId!);
entry = byId.get(entry.parentId);
if (!entry) throw new Error("Missing Pi transcript ancestor");
}
branch.reverse();
@@ -401,6 +401,8 @@ test("seeded compacted Pi history survives daemon reload, native rewind and Fork
});
const records = [
{ type: "session", version: 3, id: nativeId, cwd, timestamp },
{ type: "session_info", timestamp, name: "Automatic title", autoTitle: true },
{ type: "model_change", id: "old-model", timestamp, provider: "openrouter", modelId: "test" },
{
type: "message",
id: "system",