The best way to control Claude Code, Codex and more from any computer or phone

main
ts 300 lines 9.85 KB
Raw
1 /**
2 * The hasher wire protocol (DESIGN §22.7, §22.9) — the part of `Hasher.ts`
3 * a hasher isolate needs and nothing more: no Cloudflare provider imports,
4 * so a dynamic worker bundling it stays small.
5 *
6 * Request: the raw part bytes, coordinates in the query string. Response:
7 * `u32 len | frame` repeated — the scan first (`encodeScanResult`: `u32
8 * jsonLength | json | blob area`, entries referencing content/zdata as
9 * `[offset, length]` into the blob area), then, when a spill was
10 * requested, the uploaded part as JSON.
11 */
12 import * as Effect from "effect/Effect";
13 import * as Schema from "effect/Schema";
14 import type { Oid, ObjectType } from "../Protocol/ObjectCodec.ts";
15 import type {
16 DeltaBase,
17 DeltaJob,
18 DeltaResolved,
19 EntryBounds,
20 ScanResult,
21 } from "../Protocol/PartialScan.ts";
22
23 export class HashError extends Schema.TaggedError<HashError>()("HashError", {
24 reason: Schema.String,
25 }) {}
26
27 export const encodeBoundsRequest = (
28 payload: Uint8Array,
29 bounds: ReadonlyArray<EntryBounds>,
30 ): Uint8Array => {
31 const json = new TextEncoder().encode(JSON.stringify(bounds));
32 const out = new Uint8Array(4 + json.length + payload.length);
33 new DataView(out.buffer).setUint32(0, json.length);
34 out.set(json, 4);
35 out.set(payload, 4 + json.length);
36 return out;
37 };
38
39 export const decodeBoundsRequest = (
40 body: Uint8Array,
41 ): {
42 readonly bounds: ReadonlyArray<EntryBounds>;
43 readonly payload: Uint8Array;
44 } => {
45 const jsonLength = new DataView(body.buffer, body.byteOffset).getUint32(0);
46 const bounds = JSON.parse(
47 new TextDecoder().decode(body.subarray(4, 4 + jsonLength)),
48 ) as ReadonlyArray<EntryBounds>;
49 return { bounds, payload: body.subarray(4 + jsonLength) };
50 };
51
52 /** The internal route the self-binding layer posts to. */
53 export const HASH_ROUTE = "/_alchemy/git/hash";
54
55 // ── protocol ────────────────────────────────────────────────────────────────
56
57 interface WireEntry {
58 readonly o: string; // oid
59 readonly t: number; // type
60 readonly h: number; // header offset
61 readonly s: number; // size
62 readonly d: number; // dataOffset
63 readonly n: number; // span
64 readonly z?: [number, number]; // zdata [offset, length] in the blob area
65 readonly c?: [number, number]; // content [offset, length]
66 readonly b?: number; // baseOffset (delta-resolved)
67 readonly r?: string; // baseOid (delta-resolved)
68 }
69 interface WireResult {
70 readonly firstOffset: number;
71 readonly entries: ReadonlyArray<WireEntry>;
72 readonly unresolved: ScanResult["unresolved"];
73 readonly consumedTo: number;
74 readonly count: number;
75 }
76
77 export const encodeScanResult = (result: ScanResult): Uint8Array => {
78 const blobs: Array<Uint8Array> = [];
79 let at = 0;
80 const put = (bytes: Uint8Array): [number, number] => {
81 blobs.push(bytes);
82 const ref: [number, number] = [at, bytes.length];
83 at += bytes.length;
84 return ref;
85 };
86 const wire: WireResult = {
87 firstOffset: result.firstOffset,
88 entries: result.entries.map((e) => ({
89 o: e.oid,
90 t: e.type,
91 h: e.offset,
92 s: e.size,
93 d: e.dataOffset,
94 n: e.span,
95 ...(e.zdata === undefined ? {} : { z: put(e.zdata) }),
96 ...(e.content === undefined ? {} : { c: put(e.content) }),
97 ...(e.baseOffset === undefined ? {} : { b: e.baseOffset }),
98 ...(e.baseOid === undefined ? {} : { r: e.baseOid }),
99 })),
100 unresolved: result.unresolved,
101 consumedTo: result.consumedTo,
102 count: result.count,
103 };
104 const json = new TextEncoder().encode(JSON.stringify(wire));
105 const out = new Uint8Array(4 + json.length + at);
106 new DataView(out.buffer).setUint32(0, json.length);
107 out.set(json, 4);
108 let cursor = 4 + json.length;
109 for (const b of blobs) {
110 out.set(b, cursor);
111 cursor += b.length;
112 }
113 return out;
114 };
115
116 export const decodeScanResult = (bytes: Uint8Array): ScanResult => {
117 const jsonLength = new DataView(bytes.buffer, bytes.byteOffset).getUint32(0);
118 const wire = JSON.parse(
119 new TextDecoder().decode(bytes.subarray(4, 4 + jsonLength)),
120 ) as WireResult;
121 const blobs = bytes.subarray(4 + jsonLength);
122 const slice = (ref: [number, number] | undefined) =>
123 ref === undefined ? undefined : blobs.subarray(ref[0], ref[0] + ref[1]);
124 return {
125 firstOffset: wire.firstOffset,
126 entries: wire.entries.map((e) => ({
127 oid: e.o as Oid,
128 type: e.t as ObjectType,
129 offset: e.h,
130 size: e.s,
131 dataOffset: e.d,
132 span: e.n,
133 zdata: slice(e.z),
134 content: slice(e.c),
135 baseOffset: e.b,
136 baseOid: e.r as Oid | undefined,
137 })),
138 unresolved: wire.unresolved,
139 consumedTo: wire.consumedTo,
140 count: wire.count,
141 };
142 };
143
144 /**
145 * Response framing of the hash route: `u32 len | frame` repeated — the
146 * scan first, then (spill only) the uploaded part as JSON once its upload
147 * has finished. The client acts on the scan without waiting for the part.
148 */
149 export const frame = (bytes: Uint8Array): Uint8Array => {
150 const out = new Uint8Array(4 + bytes.length);
151 new DataView(out.buffer).setUint32(0, bytes.length);
152 out.set(bytes, 4);
153 return out;
154 };
155
156 /** Reads length-prefixed frames off a response body, one at a time. */
157 export const makeFrameReader = (body: ReadableStream<Uint8Array>) => {
158 const reader = body.getReader();
159 const pending: Array<Uint8Array> = [];
160 let pendingBytes = 0;
161 const take = (n: number): Uint8Array => {
162 const out = new Uint8Array(n);
163 let written = 0;
164 while (written < n) {
165 const head = pending[0]!;
166 const use = Math.min(head.length, n - written);
167 out.set(head.subarray(0, use), written);
168 written += use;
169 if (use === head.length) pending.shift();
170 else pending[0] = head.subarray(use);
171 }
172 pendingBytes -= n;
173 return out;
174 };
175 const peekLength = (): number | undefined => {
176 if (pendingBytes < 4) return undefined;
177 const head = take(4);
178 const len = new DataView(head.buffer).getUint32(0);
179 pending.unshift(head);
180 pendingBytes += 4;
181 return len;
182 };
183 return (): Effect.Effect<Uint8Array | undefined, HashError> =>
184 Effect.tryPromise({
185 try: async () => {
186 while (true) {
187 const len = peekLength();
188 if (len !== undefined && pendingBytes >= 4 + len) {
189 take(4);
190 return take(len);
191 }
192 const { value, done } = await reader.read();
193 if (done) return undefined;
194 pending.push(value);
195 pendingBytes += value.length;
196 }
197 },
198 catch: (error) => HashError.make({ reason: `hash part body: ${String(error)}` }),
199 });
200 };
201
202 // ── delta batches (DESIGN §22.13) ──────────────────────────────────────────
203
204 /** `u32 jsonLen | json | blobs` with bases and deltas referenced as [offset, length]. */
205 export const encodeDeltaBatch = (
206 bases: ReadonlyArray<DeltaBase>,
207 jobs: ReadonlyArray<DeltaJob>,
208 ): Uint8Array => {
209 const blobs: Array<Uint8Array> = [];
210 let at = 0;
211 const put = (bytes: Uint8Array): [number, number] => {
212 blobs.push(bytes);
213 const ref: [number, number] = [at, bytes.length];
214 at += bytes.length;
215 return ref;
216 };
217 const wire = {
218 bases: bases.map((b) => ({ r: put(b.bytes), c: b.isContent ? 1 : 0 })),
219 jobs: jobs.map((j) => ({ i: j.id, t: j.type, b: j.base, d: put(j.delta) })),
220 };
221 const json = new TextEncoder().encode(JSON.stringify(wire));
222 const out = new Uint8Array(4 + json.length + at);
223 new DataView(out.buffer).setUint32(0, json.length);
224 out.set(json, 4);
225 let cursor = 4 + json.length;
226 for (const b of blobs) {
227 out.set(b, cursor);
228 cursor += b.length;
229 }
230 return out;
231 };
232
233 export const decodeDeltaBatch = (
234 bytes: Uint8Array,
235 ): { readonly bases: Array<DeltaBase>; readonly jobs: Array<DeltaJob> } => {
236 const jsonLength = new DataView(bytes.buffer, bytes.byteOffset).getUint32(0);
237 const wire = JSON.parse(new TextDecoder().decode(bytes.subarray(4, 4 + jsonLength))) as {
238 bases: Array<{ r: [number, number]; c: number }>;
239 jobs: Array<{ i: number; t: ObjectType; b: number; d: [number, number] }>;
240 };
241 const blobs = bytes.subarray(4 + jsonLength);
242 const slice = (ref: [number, number]) => blobs.subarray(ref[0], ref[0] + ref[1]);
243 return {
244 bases: wire.bases.map((b) => ({ bytes: slice(b.r), isContent: b.c === 1 })),
245 jobs: wire.jobs.map((j) => ({
246 id: j.i,
247 type: j.t,
248 base: j.b,
249 delta: slice(j.d),
250 })),
251 };
252 };
253
254 export const encodeDeltaResults = (results: ReadonlyArray<DeltaResolved>): Uint8Array => {
255 const blobs: Array<Uint8Array> = [];
256 let at = 0;
257 const put = (bytes: Uint8Array): [number, number] => {
258 blobs.push(bytes);
259 const ref: [number, number] = [at, bytes.length];
260 at += bytes.length;
261 return ref;
262 };
263 const wire = results.map((r) => ({
264 i: r.id,
265 o: r.oid,
266 s: r.size,
267 z: put(r.zdata),
268 ...(r.content === undefined ? {} : { c: put(r.content) }),
269 }));
270 const json = new TextEncoder().encode(JSON.stringify(wire));
271 const out = new Uint8Array(4 + json.length + at);
272 new DataView(out.buffer).setUint32(0, json.length);
273 out.set(json, 4);
274 let cursor = 4 + json.length;
275 for (const b of blobs) {
276 out.set(b, cursor);
277 cursor += b.length;
278 }
279 return out;
280 };
281
282 export const decodeDeltaResults = (bytes: Uint8Array): Array<DeltaResolved> => {
283 const jsonLength = new DataView(bytes.buffer, bytes.byteOffset).getUint32(0);
284 const wire = JSON.parse(new TextDecoder().decode(bytes.subarray(4, 4 + jsonLength))) as Array<{
285 i: number;
286 o: string;
287 s: number;
288 z: [number, number];
289 c?: [number, number];
290 }>;
291 const blobs = bytes.subarray(4 + jsonLength);
292 const slice = (ref: [number, number]) => blobs.subarray(ref[0], ref[0] + ref[1]);
293 return wire.map((r) => ({
294 id: r.i,
295 oid: r.o as Oid,
296 size: r.s,
297 zdata: slice(r.z),
298 content: r.c === undefined ? undefined : slice(r.c),
299 }));
300 };