Files

25 lines
1.4 KiB
JavaScript

// A stand-in Pi turn for the compaction launch tests (row 270, #1775).
// session-adapter.mjs's turn, plus a Pi session stream in
// $MOSAIC_SESSION_DIR: a session header when the stream is new, and for a
// message "compact <id>" one completed compaction entry with that id, in
// Pi 0.85.1's shape.
import { appendFileSync, existsSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
const request = process.env.MOSAIC_REQUEST ?? "";
const body = request.match(/^Message [^\n]*:\n\n([\s\S]*)$/m)?.[1] ?? "";
const historyFile = join(process.env.MOSAIC_SESSION_DIR, "history");
const before = existsSync(historyFile) ? readFileSync(historyFile, "utf8").split("\n").filter(Boolean) : [];
const stream = join(process.env.MOSAIC_SESSION_DIR, "pi.jsonl");
const line = (e) => `${JSON.stringify(e)}\n`;
if (!existsSync(stream)) appendFileSync(stream, line({ type: "session", version: 3, id: "pi", timestamp: "2026-10-22T11:00:00.000Z", cwd: process.cwd() }));
const id = body.match(/^compact (c\d+)$/)?.[1];
if (id) {
const timestamp = new Date(Date.UTC(2026, 9, 22, 11, Number(id.slice(1)))).toISOString();
appendFileSync(stream, line({ type: "compaction", id, parentId: null, timestamp, summary: "s", firstKeptEntryId: "k", tokensBefore: 100 }));
}
appendFileSync(historyFile, `${body}\n`);
process.stdout.write(`THREAD ${JSON.stringify({ body, before })}\n`);
writeFileSync(process.env.MOSAIC_TURN_MARKER, "done\n");