{"draft":null,"from":161,"to":162,"files":[{"path":"server/src/engine-project.ts","added":55,"removed":1,"diff":"--- a/server/src/engine-project.ts\n+++ b/server/src/engine-project.ts\n@@ -96,6 +96,23 @@\n \t}\n \n \t/** Reject an oversized canonical frame before loading its text into JavaScript. */\n+\tframeBytes(seq: number): number {\n+\t\tif (!Number.isSafeInteger(seq) || seq < 0) throw Error(\"invalid canonical frame position\");\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 < 1 || !Number.isSafeInteger(size.bytes))\n+\t\t\tthrow Error(\"canonical frame exceeds hard decoding bound or is missing\");\n+\t\treturn size.bytes;\n+\t}\n+\t/** A larger first frame gets its own budget; later frames are deferred. */\n+\tadoptionPageBytes(from: number): number {\n+\t\treturn Math.max(1_048_576, this.frameBytes(from));\n+\t}\n+\n \tboundedFrame(seq: number, maxBytes: number): string {\n \t\tif (\n \t\t\t!Number.isSafeInteger(seq) ||\n@@ -122,6 +139,26 @@\n \t\treturn rows.map((row) => row.text).join(\"\");\n \t}\n \n+\t/** Stream one canonical frame without retaining its complete JSON text. */\n+\t*frameChunks(seq: number): IterableIterator<string> {\n+\t\tif (!Number.isSafeInteger(seq) || seq < 0) throw Error(\"invalid canonical frame position\");\n+\t\tlet expected = 0;\n+\t\tfor (;;) {\n+\t\t\tconst rows = this.storage.sql\n+\t\t\t\t.exec(\"SELECT part, text FROM log WHERE seq = ? AND part >= ? ORDER BY part LIMIT 1\", seq, expected)\n+\t\t\t\t.toArray() as { part: number; text: string }[];\n+\t\t\tif (!rows.length) {\n+\t\t\t\tif (!expected) throw Error(\"canonical frame is missing\");\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tconst row = rows[0]!;\n+\t\t\tif (row.part !== expected || row.text.length > LOG_PART_CHARACTERS)\n+\t\t\t\tthrow Error(\"canonical frame parts are incomplete or oversized\");\n+\t\t\texpected++;\n+\t\t\tyield row.text;\n+\t\t}\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@@ -524,11 +561,18 @@\n \t/** Reads stored entries by log position, for the feed and replay (#41); absent, those answer 501. */\n \tprivate readonly read: ((from: number, limit: number) => Entry[]) | undefined;\n \tprivate readonly store: SqliteLog | undefined;\n+\tprivate readonly historical: (<T>(at: number, operation: (project: Project) => T) => T) | undefined;\n \n-\tconstructor(project: Project, read?: (from: number, limit: number) => Entry[], store?: SqliteLog) {\n+\tconstructor(\n+\t\tproject: Project,\n+\t\tread?: (from: number, limit: number) => Entry[],\n+\t\tstore?: SqliteLog,\n+\t\thistorical?: <T>(at: number, operation: (project: Project) => T) => T,\n+\t) {\n \t\tthis.project = project;\n \t\tthis.read = read;\n \t\tthis.store = store;\n+\t\tthis.historical = historical;\n \t}\n \n \t/** The project's events from log position `from` (#41): reading consumes nothing. */\n@@ -638,6 +682,11 @@\n \t\treturn this.replica;\n \t}\n \tstatusAt(at: number) {\n+\t\tif (this.historical)\n+\t\t\treturn this.historical(Math.max(0, Math.min(at, this.project.logLength)), (p) => ({\n+\t\t\t\tat: Math.min(at, this.project.logLength),\n+\t\t\t\t...(p.initialized ? p.status() : { version: -1, agents: [] }),\n+\t\t\t}));\n \t\tconst p = this.at(at);\n \t\tconst answer = {\n \t\t\tat: Math.min(at, this.project.logLength),\n@@ -647,6 +696,11 @@\n \t\treturn answer;\n \t}\n \twhereAt(at: number) {\n+\t\tif (this.historical)\n+\t\t\treturn this.historical(Math.max(0, Math.min(at, this.project.logLength)), (p) => ({\n+\t\t\t\tat: Math.min(at, this.project.logLength),\n+\t\t\t\t...(p.initialized ? where(p) : { version: -1, files: {} }),\n+\t\t\t}));\n \t\tconst p = this.at(at);\n \t\tconst answer = {\n \t\t\tat: Math.min(at, this.project.logLength),"}],"unmatched":[]}