import { mkdirSync, writeFileSync, appendFileSync, mkdtempSync, realpathSync } from "node:fs"; import { join } from "node:path"; import { tmpdir } from "node:os"; import { createReader } from "../../../../packages/conversation/src/reader.mjs"; const proj = realpathSync(mkdtempSync(join(tmpdir(), "fb-proj-"))); const dir = join(proj, ".pi", "state", "x", "sessions"); mkdirSync(dir, { recursive: true }); const f = join(dir, "s.jsonl"); const L = (o) => JSON.stringify(o) + "\n"; const msg = (id, parentId, role, text) => L({ type: "message", id, parentId, timestamp: "2026-09-26T00:00:00Z", message: role === "assistant" ? { role, content: [{ type: "text", text }], stopReason: "stop" } : { role, content: text } }); writeFileSync(f, L({ type: "session", id: "sess", cwd: proj, timestamp: "2026-09-26T00:00:00Z" }) + msg("a", null, "user", "hi") + msg("b", "a", "assistant", "hello")); const reader = createReader({ roots: [{ seat: "x", project: "p", projectRoot: proj, dir, harness: "pi", unsupportedReason: null, engineStartedAt: null }] }); const conv = reader.catalogue().conversations[0].conversation; const p1 = reader.open({ conversation: conv }); console.log("open branch", p1.page.branch, "entries", p1.page.entries.map(e => e.id + "@" + e.branch)); appendFileSync(f, msg("c", "b", "user", "more") + msg("d", "c", "assistant", "ok")); const p2 = reader.next({ cursor: p1.follow.id, conversation: conv, branch: p1.page.branch }); console.log("follow ok", p2.ok, "page branch", p2.page?.branch, "entries", p2.page?.entries.map(e => e.id + "@" + e.branch), "view", JSON.stringify(p2.view?.branches)); const p3 = reader.open({ conversation: conv, branch: p1.page.branch }); console.log("reopen with first branch id:", JSON.stringify(p3.refusal ?? p3.page.branch));