- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/operation-protocol.ts?format=json
280 linescodex-capacity-policy 100%Last change codex-capacity-policy in version 106, 1 version changed it
1import type { Entry } from "./project-types.ts";
3export const OPERATION_CHUNK_BYTES = 64 * 1024;
4export const OPERATION_CHUNK_ENTRIES = 32;
5export const OPERATION_MAX_BYTES = 4 * 1024 * 1024;
6export const OPERATION_MAX_CHUNKS = 64;
8export interface OperationHeads {
9 readonly log: number;
10 readonly shared: number;
11 readonly actorMine: number;
12 readonly actorBase: number;
13 readonly queueRevision: number;
14}
16export interface OperationQueueEffect {
17 readonly kind: "enqueue";
18 readonly queueId: number;
19 readonly actor: string;
20 readonly entryIndex: number;
21 readonly position: number;
22}
24export interface OperationLogSummary {
25 readonly lines: number;
26 readonly keys: number;
27 readonly chars: number;
28 readonly sharedVersion?: number;
29 readonly lastPublish?: { readonly agent: string; readonly time: number };
30}
32export interface ProjectOperation {
33 readonly id: string;
34 readonly tenant: string;
35 readonly project: string;
36 readonly incarnation: string;
37 readonly actor: string;
38 readonly build: string;
39 readonly executorIdentity: string;
40 readonly rootDigest: string;
41 readonly authorizationRevision: string;
42 readonly policyRevision: string;
43 readonly operationTime: number;
44 readonly queuePolicy: { readonly policyRevision: string; readonly checkerReady: boolean };
45 readonly generation: number;
46 readonly heads: OperationHeads;
47 readonly message: string;
48 readonly files: Readonly<Record<string, string | null>>;
49}
51export interface OperationFence {
52 readonly tenant: string;
53 readonly project: string;
54 readonly incarnation: string;
55 readonly actor: string;
56 readonly build: string;
57 readonly executorIdentity: string;
58 readonly rootDigest: string;
59 readonly authorizationRevision: string;
60 readonly policyRevision: string;
61 readonly generation: number;
62 readonly heads: OperationHeads;
63}
65export interface OperationProjection {
66 readonly sharedHead: number;
67 readonly actorMineHead: number;
68 readonly actorBaseHead: number;
69 readonly queueRevision: number;
70 readonly queueEffects: readonly OperationQueueEffect[];
71 readonly logSummary: OperationLogSummary;
72}
74export interface OperationReceipt {
75 readonly id: string;
76 readonly tenant: string;
77 readonly project: string;
78 readonly incarnation: string;
79 readonly actor: string;
80 readonly build: string;
81 readonly executorIdentity: string;
82 readonly authorizationRevision: string;
83 readonly policyRevision: string;
84 readonly generation: number;
85 readonly previousRootDigest: string;
86 readonly nextRootDigest: string;
87 readonly previousHeads: OperationHeads;
88 readonly nextHeads: OperationHeads;
89 readonly requestDigest: string;
90 readonly outputDigest: string;
91 readonly entryCount: number;
92 readonly queueRevision: number;
93 readonly queueEffects: readonly OperationQueueEffect[];
94 readonly status: "committed";
95}
97export interface OperationChunk {
98 readonly index: number;
99 readonly firstEntry: number;
100 readonly count: number;
101 readonly bytes: number;
102 readonly entries: readonly Entry[];
103 readonly text: string;
104 readonly digest: string;
105 readonly chain: string;
106}
108export function canonicalJson(value: unknown): string {
109 if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
110 if (value && typeof value === "object") {
111 const record = value as Record<string, unknown>;
112 return `{${Object.keys(record)
113 .sort()
114 .map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`)
115 .join(",")}}`;
116 }
117 return JSON.stringify(value);
118}
120export function encodeEntries(entries: readonly Entry[]): string {
121 return canonicalJson(entries);
122}
124export async function digestText(text: string): Promise<string> {
125 const bytes = new TextEncoder().encode(text);
126 const digest = await crypto.subtle.digest("SHA-256", bytes);
127 return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
128}
130export async function chainDigest(
131 previous: string,
132 index: number,
133 count: number,
134 bytes: number,
135 digest: string,
136): Promise<string> {
137 return digestText(canonicalJson({ previous, index, count, bytes, digest }));
138}
140export async function digestOperation(operation: ProjectOperation): Promise<string> {
141 return digestText(canonicalJson(operation));
142}
144function* jsonTokens(value: unknown, ancestors = new Set<object>()): Generator<string> {
145 if (value === null || typeof value === "boolean") {
146 yield String(value);
147 return;
148 }
149 if (typeof value === "number") {
150 yield Number.isFinite(value) ? JSON.stringify(value) : "null";
151 return;
152 }
153 if (typeof value === "string") {
154 yield '"';
155 for (const point of value) {
156 const code = point.charCodeAt(0);
157 if (point === '"') yield '\\"';
158 else if (point === "\\") yield "\\\\";
159 else if (point === "\b") yield "\\b";
160 else if (point === "\f") yield "\\f";
161 else if (point === "\n") yield "\\n";
162 else if (point === "\r") yield "\\r";
163 else if (point === "\t") yield "\\t";
164 else if (code < 0x20 || (point.length === 1 && code >= 0xd800 && code <= 0xdfff))
165 yield `\\u${code.toString(16).padStart(4, "0")}`;
166 else yield point;
167 }
168 yield '"';
169 return;
170 }
171 if (Array.isArray(value)) {
172 if (ancestors.has(value)) throw new TypeError("cannot serialize a cyclic entry");
173 ancestors.add(value);
174 yield "[";
175 for (let index = 0; index < value.length; index++) {
176 if (index) yield ",";
177 yield* jsonTokens(value[index] ?? null, ancestors);
178 }
179 yield "]";
180 ancestors.delete(value);
181 return;
182 }
183 if (value && typeof value === "object") {
184 const record = value as Record<string, unknown>;
185 if (ancestors.has(record)) throw new TypeError("cannot serialize a cyclic entry");
186 ancestors.add(record);
187 yield "{";
188 let first = true;
189 for (const key in record) {
190 if (!Object.hasOwn(record, key)) continue;
191 const child = record[key];
192 if (child === undefined || typeof child === "function" || typeof child === "symbol") continue;
193 if (!first) yield ",";
194 first = false;
195 yield* jsonTokens(key, ancestors);
196 yield ":";
197 yield* jsonTokens(child, ancestors);
198 }
199 yield "}";
200 ancestors.delete(record);
201 return;
202 }
203 if (value === undefined || typeof value === "function" || typeof value === "symbol") {
204 yield "null";
205 return;
206 }
207 throw new TypeError("entry contains an unsupported JSON value");
208}
210export interface Utf8Frame {
211 readonly text: string;
212 readonly bytes: number;
213}
215export function* utf8JsonFrames(value: unknown): Generator<Utf8Frame> {
216 let frame = "";
217 let bytes = 0;
218 for (const token of jsonTokens(value)) {
219 for (const point of token) {
220 const size = new TextEncoder().encode(point).byteLength;
221 if (bytes + size > OPERATION_CHUNK_BYTES) {
222 yield { text: frame, bytes };
223 frame = "";
224 bytes = 0;
225 }
226 frame += point;
227 bytes += size;
228 }
229 }
230 if (frame || bytes === 0) yield { text: frame, bytes };
231}
233export function measureUtf8Json(value: unknown): { bytes: number; frames: number } {
234 let bytes = 0;
235 let frames = 0;
236 for (const frame of utf8JsonFrames(value)) {
237 bytes += frame.bytes;
238 frames++;
239 }
240 return { bytes, frames };
241}
243/**
244 * Emits bounded JSON chunks for small entries. The store separately frames a
245 * single large entry; this helper intentionally keeps its array contract.
246 */
247export async function* chunkEntries(entries: Iterable<Entry>): AsyncGenerator<OperationChunk> {
248 let index = 0;
249 let firstEntry = 0;
250 let previous = "";
251 let batch: Entry[] = [];
252 let batchBytes = 2;
253 for (const entry of entries) {
254 const text = canonicalJson(entry);
255 const size = new TextEncoder().encode(text).byteLength;
256 if (size + 2 > OPERATION_CHUNK_BYTES) throw new RangeError("one operation entry requires split UTF-8 framing");
257 const withComma = size + (batch.length ? 1 : 0);
258 if (batch.length && (batch.length >= OPERATION_CHUNK_ENTRIES || batchBytes + withComma > OPERATION_CHUNK_BYTES)) {
259 const chunkText = encodeEntries(batch);
260 const bytes = new TextEncoder().encode(chunkText).byteLength;
261 const digest = await digestText(chunkText);
262 const chain = await chainDigest(previous, index, batch.length, bytes, digest);
263 yield { index, firstEntry, count: batch.length, bytes, entries: batch, text: chunkText, digest, chain };
264 previous = chain;
265 firstEntry += batch.length;
266 index++;
267 batch = [];
268 batchBytes = 2;
269 }
270 batch.push(entry);
271 batchBytes += size + (batch.length > 1 ? 1 : 0);
272 }
273 if (batch.length) {
274 const text = encodeEntries(batch);
275 const bytes = new TextEncoder().encode(text).byteLength;
276 const digest = await digestText(text);
277 const chain = await chainDigest(previous, index, batch.length, bytes, digest);
278 yield { index, firstEntry, count: batch.length, bytes, entries: batch, text, digest, chain };
279 }
280}