- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/record-batches.ts?format=json
125 linescodex-prose-authorship-1 100%Last change codex-prose-authorship-1 in version 108, 1 version changed it
1import { ApiError, type RecordAnswer } from "./api.ts";
2import type { Mode, Modes } from "./modes.ts";
4/** One atomic path entry from the folder scan. */
5export interface RecordItem {
6 readonly path: string;
7 readonly text?: string | null;
8 readonly mode?: Modes[string];
9 readonly blob?: string;
10}
12/** A record request; a path's text, mode, and blob reference always travel together. */
13export interface RecordBatch {
14 readonly files: Record<string, string | null>;
15 readonly modes: Modes;
16 readonly blobs: Record<string, string>;
17 readonly paths: readonly string[];
18}
20export const RECORD_BODY_BUDGET = 8 * 1024 * 1024;
22export class RecordItemTooLargeError extends Error {
23 readonly path: string;
24 readonly bytes: number;
25 readonly budget: number;
26 constructor(path: string, bytes: number, budget: number) {
27 super(`Not recorded: ${path} needs a ${bytes}-byte record request by itself, over the ${budget}-byte limit.`);
28 this.name = "RecordItemTooLargeError";
29 this.path = path;
30 this.bytes = bytes;
31 this.budget = budget;
32 }
33}
35function bodyFor(items: readonly RecordItem[]): RecordBatch {
36 const files: Record<string, string | null> = {};
37 const modes: Record<string, Mode> = {};
38 const blobs: Record<string, string> = {};
39 for (const item of items) {
40 if (item.text !== undefined) files[item.path] = item.text;
41 if (item.mode !== undefined) modes[item.path] = item.mode;
42 if (item.blob !== undefined) blobs[item.path] = item.blob;
43 }
44 return { files, modes, blobs, paths: items.map((item) => item.path) };
45}
47/** Exact serialized UTF-8 size of the API record body. */
48export function serializedRecordBytes(batch: RecordBatch): number {
49 return Buffer.byteLength(
50 JSON.stringify({
51 files: batch.files,
52 ...(Object.keys(batch.modes).length ? { modes: batch.modes } : {}),
53 ...(Object.keys(batch.blobs).length ? { blobs: batch.blobs } : {}),
54 }),
55 "utf8",
56 );
57}
59/** Deterministically split sorted atomic path entries under the serialized body budget. */
60export function batchRecordItems(items: readonly RecordItem[], budget = RECORD_BODY_BUDGET): RecordBatch[] {
61 if (!Number.isSafeInteger(budget) || budget <= 0)
62 throw new RangeError("record body budget must be a positive safe integer");
63 const sorted = [...items].sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
64 const batches: RecordBatch[] = [];
65 let current: RecordItem[] = [];
66 for (const item of sorted) {
67 const candidate = bodyFor([...current, item]);
68 if (serializedRecordBytes(candidate) <= budget) {
69 current.push(item);
70 continue;
71 }
72 if (current.length === 0) throw new RecordItemTooLargeError(item.path, serializedRecordBytes(candidate), budget);
73 batches.push(bodyFor(current));
74 current = [item];
75 const single = bodyFor(current);
76 const bytes = serializedRecordBytes(single);
77 if (bytes > budget) throw new RecordItemTooLargeError(item.path, bytes, budget);
78 }
79 if (current.length > 0) batches.push(bodyFor(current));
80 return batches;
81}
83export interface RecordApi {
84 record(
85 files: Record<string, string | null>,
86 extra?: { session?: string; subagent?: string },
87 modes?: Modes,
88 blobs?: Readonly<Record<string, string>>,
89 ): Promise<RecordAnswer>;
90}
92/** Retry only an outcome that may have been accepted; an identical record is a no-op once its state landed. */
93export async function sendRecordBatch(api: RecordApi, batch: RecordBatch): Promise<RecordAnswer> {
94 const blobMap = batch.blobs;
95 try {
96 return await api.record(batch.files, {}, batch.modes, blobMap);
97 } catch (error) {
98 if (
99 !(error instanceof ApiError) ||
100 error.notSent ||
101 (error.status !== undefined && (error.status < 200 || (error.status >= 300 && error.status < 500)))
102 )
103 throw error;
104 // The server diffs whole-file state against the agent's current view. Replaying these exact maps is idempotent.
105 return api.record(batch.files, {}, batch.modes, blobMap);
106 }
107}
109/** Run batches in order and expose only confirmed batches to local bookkeeping. */
110export async function sendRecordBatches(
111 api: RecordApi,
112 batches: readonly RecordBatch[],
113 beforeSend: (batch: RecordBatch) => Promise<void>,
114 onConfirmed: (batch: RecordBatch, answer: RecordAnswer) => Promise<void>,
115): Promise<RecordAnswer[]> {
116 const answers: RecordAnswer[] = [];
117 for (const batch of batches) {
118 await beforeSend(batch);
119 const answer = await sendRecordBatch(api, batch);
120 if (!answer.ok) throw new Error(`the server refused the record: ${answer.reason ?? "no reason given"}`);
121 await onConfirmed(batch, answer);
122 answers.push(answer);
123 }
124 return answers;
125}