Compare commits

..
Author SHA1 Message Date
paseo-bot[bot] 1cc1164f71 fix(pi): replay branches started immediately after an ID-less title
Pi can append a message as the root when it reloads an ID-less title. Keep rejecting other missing parent fields unless the row is a legacy model change.
2026-11-30 03:35:11 +00:00
paseo-bot[bot] c14373201a fix(pi): preserve ancestry rooted at an older model change
Pi can omit the model change parent after reloading an ID-less title. Later messages still link to that model change.
2026-11-30 03:29:17 +00:00
paseo-bot[bot] fbc80bbce5 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.
2026-11-30 03:13:31 +00:00
6 changed files with 84 additions and 56 deletions
@@ -308,24 +308,6 @@ describe("parseAssistantFileLink", () => {
});
});
it.each(["C:/repo", "\\\\host\\share\\repo"])(
"leaves Windows variable paths in inline code unlinked in %s",
(workspaceRoot) => {
expect(classifyAssistantFileLink("%TEMP%\\debug.md", { workspaceRoot })).toBeNull();
expect(classifyAssistantFileLink("%LOCALAPPDATA%/debug.md:3", { workspaceRoot })).toBeNull();
expect(classifyAssistantFileLink("C:\\Temp\\debug.md", { workspaceRoot })?.kind).toBe(
"directFile",
);
},
);
it("allows a literal percent-delimited directory in an authored file link", () => {
expect(
parseAssistantFileLink("%TEMP%/notes.md", { workspaceRoot: "C:/repo", explicitLink: true })
?.path,
).toBe("C:/repo/%TEMP%/notes.md");
});
it("parses absolute Windows hrefs inside the active workspace", () => {
expect(
parseAssistantFileLink("C:/repo/src/app.tsx#L12-L20", {
@@ -393,14 +393,6 @@ function parseWorkspaceRelativeFileLink(
value: string,
options: AssistantHrefParseOptions,
): InlinePathTarget | null {
// cmd.exe variables are shell expressions. The file opener only resolves literal paths.
if (
!options.explicitLink &&
/^(?:[A-Za-z]:[\\/]|[\\/]{2})/.test(options.workspaceRoot ?? "") &&
/^%[^%\\/]+%[\\/]/.test(value)
) {
return null;
}
const parsed = parseLocalPathParts(value, options);
if (!parsed || isAbsolutePath(parsed.path)) {
return null;
@@ -13,26 +13,13 @@ import { connectToDaemon } from "../../utils/client.js";
const repo = fileURLToPath(new URL("../../../../..", import.meta.url));
const cli = path.join(repo, "packages/cli/dist/index.js");
// Reserved ports come from below every OS's ephemeral range (Linux starts at
// 32768, macOS and Windows at 49152). A port from port 0 is ephemeral, and the
// OS may give it to another process between the reservation and the daemon's
// listen. A port from this range is never handed out that way.
const reservedPorts = { first: 20_000, count: 12_000 };
async function port() {
for (let attempt = 0; attempt < 50; attempt++) {
const value = reservedPorts.first + Math.floor(Math.random() * reservedPorts.count);
if (await isFree(value)) return value;
}
throw new Error("No free test port");
}
function isFree(value: number) {
return new Promise<boolean>((resolve) => {
const server = net.createServer();
server.once("error", () => resolve(false));
server.listen(value, "127.0.0.1", () => server.close(() => resolve(true)));
});
const server = net.createServer();
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
const value = (server.address() as net.AddressInfo).port;
await new Promise<void>((resolve) => server.close(() => resolve()));
if (value === 6767 || value === 6768) throw new Error("Unsafe test port");
return value;
}
// Port zero lets the daemon bind its own port, with no gap for another
@@ -346,6 +346,59 @@ test("replays both sides of compaction with native IDs and isolates rewound bran
).toEqual(["u1"]);
});
test("keeps saved messages when auto-title metadata has no ID", () => {
const userMessage = { role: "user", content: "hello" };
const rows = [
{ type: "session", version: 3, id: "session" },
{ type: "session_info", name: "Automatic title", autoTitle: true },
{ type: "message", id: "u1", parentId: null, message: userMessage },
{ type: "session_info", name: "Updated title", autoTitle: true },
];
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("replays the active branch rooted at a model change without a parent ID", () => {
const userMessage = { role: "user", content: "after reopening" };
const rows = [
{ type: "session", version: 3, id: "session" },
{
type: "message",
id: "old",
parentId: null,
message: { role: "user", content: "old branch" },
},
{ type: "session_info", name: "Automatic title", autoTitle: true },
{ type: "model_change", id: "model", provider: "test", modelId: "test-model" },
{ type: "message", id: "u1", parentId: "model", message: userMessage },
];
const history = readPiTranscript(rows.map((row) => JSON.stringify(row)).join("\n"));
expect(history.messages).toEqual([userMessage]);
expect(history.userEntries).toEqual([{ id: "u1", text: "after reopening" }]);
});
test("replays a message that Pi writes without a parent ID after an ID-less title", () => {
const userMessage = { role: "user", content: "after reopening" };
const rows = [
{ type: "session", version: 3, id: "session" },
{
type: "message",
id: "old",
parentId: null,
message: { role: "user", content: "old branch" },
},
{ type: "session_info", name: "Automatic title", autoTitle: true },
{ type: "message", id: "u1", message: userMessage },
];
const history = readPiTranscript(rows.map((row) => JSON.stringify(row)).join("\n"));
expect(history.messages).toEqual([userMessage]);
expect(history.userEntries).toEqual([{ id: "u1", text: "after reopening" }]);
});
test("preserves native custom entries and legacy linear messages without inventing native IDs", async () => {
const custom = {
type: "custom_message",
@@ -111,22 +111,34 @@ 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 [index, row] of records.entries()) {
const { id, parentId } = row;
// An automatic title without an ID has no position in the message tree.
if (!id && row.type === "session_info") continue;
const previous = records[index - 1];
const followsIdlessTitle = previous?.type === "session_info" && !previous.id;
if (
!id ||
byId.has(id) ||
(parentId === undefined && row.type !== "model_change" && !followsIdlessTitle)
) {
throw new Error("Invalid Pi transcript tree entry");
byId.set(entry.id, entry);
}
// After an ID-less title, Pi can append the next entry as a new root.
entry = { ...row, id, parentId: parentId ?? null };
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,10 +401,12 @@ 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",
parentId: null,
parentId: "old-model",
timestamp,
message: {
role: "system",