{"draft":null,"from":156,"to":157,"files":[{"path":"server/src/engine-project.ts","added":38,"removed":3,"diff":"--- a/server/src/engine-project.ts\n+++ b/server/src/engine-project.ts\n@@ -17,14 +17,14 @@\n \n // Characters per row. SQLite's 2 MB cap is in bytes, and a character is at\n // most 3 bytes of UTF-8 (an emoji is 2 characters and 4 bytes), so 600,000 stays under it.\n-const PART = 600_000;\n+export const LOG_PART_CHARACTERS = 600_000;\n \n-/** An entry's JSON in rows of at most PART characters, never cutting an emoji's two halves apart. */\n+/** An entry's JSON in rows of at most LOG_PART_CHARACTERS characters, never cutting an emoji's two halves apart. */\n export function logParts(text: string): string[] {\n \tconst parts: string[] = [];\n \tlet at = 0;\n \tdo {\n-\t\tlet end = Math.min(at + PART, text.length);\n+\t\tlet end = Math.min(at + LOG_PART_CHARACTERS, text.length);\n \t\tconst c = text.charCodeAt(end - 1);\n \t\tif (end < text.length && c >= 0xd800 && c <= 0xdbff) end--;\n \t\tparts.push(text.slice(at, end));\n@@ -95,6 +95,41 @@\n \t\treturn this.next;\n \t}\n \n+\t/** Reject an oversized canonical frame before loading its text into JavaScript. */\n+\tboundedFrame(seq: number, maxBytes: number): string {\n+\t\tif (\n+\t\t\t!Number.isSafeInteger(seq) ||\n+\t\t\tseq < 0 ||\n+\t\t\t!Number.isSafeInteger(maxBytes) ||\n+\t\t\tmaxBytes < 1 ||\n+\t\t\tmaxBytes > 8_000_000\n+\t\t)\n+\t\t\tthrow new Error(\"invalid canonical frame bound\");\n+\t\tconst size = this.storage.sql\n+\t\t\t.exec(\n+\t\t\t\t\"SELECT COUNT(*) AS parts, COALESCE(SUM(LENGTH(CAST(text AS BLOB))), 0) AS bytes FROM log WHERE seq = ?\",\n+\t\t\t\tseq,\n+\t\t\t)\n+\t\t\t.toArray()[0] as { parts: number; bytes: number };\n+\t\tif (!size || size.parts < 1 || size.parts > 256 || size.bytes > maxBytes)\n+\t\t\tthrow new Error(\"canonical frame exceeds adoption bound or is missing\");\n+\t\tconst rows = this.storage.sql.exec(\"SELECT part, text FROM log WHERE seq = ? ORDER BY part\", seq).toArray() as {\n+\t\t\tpart: number;\n+\t\t\ttext: string;\n+\t\t}[];\n+\t\tif (rows.length !== size.parts || rows.some((row, part) => row.part !== part))\n+\t\t\tthrow new Error(\"canonical frame parts are incomplete\");\n+\t\treturn rows.map((row) => row.text).join(\"\");\n+\t}\n+\n+\t/** Refreshes the in-memory cursor after an enclosing transaction rolls back. */\n+\trefreshSequence(): void {\n+\t\tconst rows = this.storage.sql.exec(\"SELECT COALESCE(MAX(seq), -1) AS last FROM log\").toArray() as {\n+\t\t\tlast: number;\n+\t\t}[];\n+\t\tthis.next = (rows[0]?.last ?? -1) + 1;\n+\t}\n+\n \t/** Monotonic cache invalidation epoch for every canonical log mutation. */\n \tgeneration(): number {\n \t\tconst rows = this.storage.sql.exec(\"SELECT value FROM marks WHERE name = ?\", \"generation\").toArray() as {"}],"unmatched":[]}