fix(daemon): freeze snapshot messages at capture
1 unmodified line2345678691011121316 unmodified lines30313233343536373839404142434445464748495051100 unmodified lines1 unmodified lineimport { tmpdir } from "node:os";import { join } from "node:path";import type { AgentMessage } from "@earendil-works/pi-agent-core";import { afterEach, describe, expect, it } from "vitest";import { createSnapshotTranscriptChunks, SnapshotTranscriptCache,import { SnapshotTranscriptCache } from "../src/modes/daemon/snapshot-transcript-cache.js";} from "../src/modes/daemon/snapshot-transcript-cache.js";
const tempDirs: string[] = [];
afterEach(() => {16 unmodified lines }));}
describe("snapshot transcript cache", () => { it("captures message content when a streamed snapshot is created", () => { const message: AgentMessage = { role: "user", content: "before", timestamp: 1 }; const chunks = createSnapshotTranscriptChunks({ activeSessionId: "active-capture", snapshotId: "snapshot-capture", messages: [message], });
message.content = "after";
const decoded = [...chunks].flatMap((chunk) => JSON.parse(chunk.toString("utf8")).messages); expect(decoded).toEqual([{ role: "user", content: "before", timestamp: 1 }]); });
it("encodes transcript chunks as directly forwardable JSONL", () => { const cache = new SnapshotTranscriptCache({ activeSessionId: "active-a", snapshotId: "snapshot-a",100 unmodified lines29 unmodified lines3031323334343536373813 unmodified lines525354555656575858596061228 unmodified linesExpand all29 unmodified lines messages: readonly AgentMessage[]; targetChunkBytes?: number; signal?: AbortSignal;}): Iterable<Buffer> { const messages = [...options.messages]; const messages = options.messages.map((message) => JSON.stringify(message)); const targetChunkBytes = options.targetChunkBytes ?? SNAPSHOT_TARGET_CHUNK_BYTES; return { *[Symbol.iterator](): Iterator<Buffer> { options.signal?.throwIfAborted();13 unmodified lines index++; return line; };
for (const message of messages) { for (const serialized of messages) { options.signal?.throwIfAborted(); const serialized = JSON.stringify(message); const bytes = Buffer.byteLength(serialized) + (serializedMessages.length > 0 ? 1 : 0); if (serializedMessages.length > 0 && serializedBytes + bytes > targetChunkBytes) { const chunk = flush(); if (chunk) yield chunk;228 unmodified linesExpand all