{"draft":null,"from":91,"to":318,"files":[{"path":"client/src/record-batches.ts","added":125,"removed":0,"diff":"--- /dev/null\n+++ b/client/src/record-batches.ts\n@@ -0,0 +1,125 @@\n+import { ApiError, type RecordAnswer } from \"./api.ts\";\n+import type { Mode, Modes } from \"./modes.ts\";\n+\n+/** One atomic path entry from the folder scan. */\n+export interface RecordItem {\n+\treadonly path: string;\n+\treadonly text?: string | null;\n+\treadonly mode?: Modes[string];\n+\treadonly blob?: string;\n+}\n+\n+/** A record request; a path's text, mode, and blob reference always travel together. */\n+export interface RecordBatch {\n+\treadonly files: Record<string, string | null>;\n+\treadonly modes: Modes;\n+\treadonly blobs: Record<string, string>;\n+\treadonly paths: readonly string[];\n+}\n+\n+export const RECORD_BODY_BUDGET = 8 * 1024 * 1024;\n+\n+export class RecordItemTooLargeError extends Error {\n+\treadonly path: string;\n+\treadonly bytes: number;\n+\treadonly budget: number;\n+\tconstructor(path: string, bytes: number, budget: number) {\n+\t\tsuper(`Not recorded: ${path} needs a ${bytes}-byte record request by itself, over the ${budget}-byte limit.`);\n+\t\tthis.name = \"RecordItemTooLargeError\";\n+\t\tthis.path = path;\n+\t\tthis.bytes = bytes;\n+\t\tthis.budget = budget;\n+\t}\n+}\n+\n+function bodyFor(items: readonly RecordItem[]): RecordBatch {\n+\tconst files: Record<string, string | null> = {};\n+\tconst modes: Record<string, Mode> = {};\n+\tconst blobs: Record<string, string> = {};\n+\tfor (const item of items) {\n+\t\tif (item.text !== undefined) files[item.path] = item.text;\n+\t\tif (item.mode !== undefined) modes[item.path] = item.mode;\n+\t\tif (item.blob !== undefined) blobs[item.path] = item.blob;\n+\t}\n+\treturn { files, modes, blobs, paths: items.map((item) => item.path) };\n+}\n+\n+/** Exact serialized UTF-8 size of the API record body. */\n+export function serializedRecordBytes(batch: RecordBatch): number {\n+\treturn Buffer.byteLength(\n+\t\tJSON.stringify({\n+\t\t\tfiles: batch.files,\n+\t\t\t...(Object.keys(batch.modes).length ? { modes: batch.modes } : {}),\n+\t\t\t...(Object.keys(batch.blobs).length ? { blobs: batch.blobs } : {}),\n+\t\t}),\n+\t\t\"utf8\",\n+\t);\n+}\n+\n+/** Deterministically split sorted atomic path entries under the serialized body budget. */\n+export function batchRecordItems(items: readonly RecordItem[], budget = RECORD_BODY_BUDGET): RecordBatch[] {\n+\tif (!Number.isSafeInteger(budget) || budget <= 0)\n+\t\tthrow new RangeError(\"record body budget must be a positive safe integer\");\n+\tconst sorted = [...items].sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));\n+\tconst batches: RecordBatch[] = [];\n+\tlet current: RecordItem[] = [];\n+\tfor (const item of sorted) {\n+\t\tconst candidate = bodyFor([...current, item]);\n+\t\tif (serializedRecordBytes(candidate) <= budget) {\n+\t\t\tcurrent.push(item);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (current.length === 0) throw new RecordItemTooLargeError(item.path, serializedRecordBytes(candidate), budget);\n+\t\tbatches.push(bodyFor(current));\n+\t\tcurrent = [item];\n+\t\tconst single = bodyFor(current);\n+\t\tconst bytes = serializedRecordBytes(single);\n+\t\tif (bytes > budget) throw new RecordItemTooLargeError(item.path, bytes, budget);\n+\t}\n+\tif (current.length > 0) batches.push(bodyFor(current));\n+\treturn batches;\n+}\n+\n+export interface RecordApi {\n+\trecord(\n+\t\tfiles: Record<string, string | null>,\n+\t\textra?: { session?: string; subagent?: string },\n+\t\tmodes?: Modes,\n+\t\tblobs?: Readonly<Record<string, string>>,\n+\t): Promise<RecordAnswer>;\n+}\n+\n+/** Retry only an outcome that may have been accepted; an identical record is a no-op once its state landed. */\n+export async function sendRecordBatch(api: RecordApi, batch: RecordBatch): Promise<RecordAnswer> {\n+\tconst blobMap = batch.blobs;\n+\ttry {\n+\t\treturn await api.record(batch.files, {}, batch.modes, blobMap);\n+\t} catch (error) {\n+\t\tif (\n+\t\t\t!(error instanceof ApiError) ||\n+\t\t\terror.notSent ||\n+\t\t\t(error.status !== undefined && (error.status < 200 || (error.status >= 300 && error.status < 500)))\n+\t\t)\n+\t\t\tthrow error;\n+\t\t// The server diffs whole-file state against the agent's current view. Replaying these exact maps is idempotent.\n+\t\treturn api.record(batch.files, {}, batch.modes, blobMap);\n+\t}\n+}\n+\n+/** Run batches in order and expose only confirmed batches to local bookkeeping. */\n+export async function sendRecordBatches(\n+\tapi: RecordApi,\n+\tbatches: readonly RecordBatch[],\n+\tbeforeSend: (batch: RecordBatch) => Promise<void>,\n+\tonConfirmed: (batch: RecordBatch, answer: RecordAnswer) => Promise<void>,\n+): Promise<RecordAnswer[]> {\n+\tconst answers: RecordAnswer[] = [];\n+\tfor (const batch of batches) {\n+\t\tawait beforeSend(batch);\n+\t\tconst answer = await sendRecordBatch(api, batch);\n+\t\tif (!answer.ok) throw new Error(`the server refused the record: ${answer.reason ?? \"no reason given\"}`);\n+\t\tawait onConfirmed(batch, answer);\n+\t\tanswers.push(answer);\n+\t}\n+\treturn answers;\n+}"}],"unmatched":[]}