@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 147 lines 5.98 KB
Raw
1 /**
2 * A {@link RandomAccess} over a blob-store object, for pack ingestion
3 * (DESIGN.md §3.6 upgrade seam) — this is what removes the push size cap.
4 *
5 * v1 buffered the whole incoming pack in the isolate, which forced a hard
6 * 50 MiB limit: a bigger push could not be held in 128 MB of memory
7 * alongside delta bases. Instead the body is streamed to the blob store
8 * and parsed *from there*, so ingest memory is bounded by the read window
9 * rather than by the pack size. `PackParser` was written against
10 * `RandomAccess` from the start precisely so this swap touches one
11 * implementation and no protocol code.
12 *
13 * Reads are served from window-aligned slabs (the same technique the object
14 * store uses for compacted packs): pack parsing is overwhelmingly
15 * sequential, so one window fetch serves thousands of consecutive reads,
16 * and the occasional backwards seek for an OFS_DELTA base usually lands in
17 * a retained window too.
18 */
19 import { RuntimeContext } from "alchemy/RuntimeContext";
20 import * as Effect from "effect/Effect";
21 import type { BlobStoreShape } from "../BlobStore.ts";
22 import type { RandomAccess } from "../Protocol/PackParser.ts";
23 import { StoreError } from "../Protocol/Store.ts";
24
25 /** Bytes fetched per window. */
26 export const PACK_WINDOW_BYTES = 4 * 1024 * 1024;
27
28 /** Windows retained (insertion-ordered LRU) — bounds ingest memory. */
29 export const PACK_MAX_WINDOWS = 4;
30
31 /**
32 * Opens a blob-store object as a {@link RandomAccess}. `size` must be the
33 * object's exact byte length (the caller knows it: it just uploaded it).
34 */
35 export const blobRandomAccess = (options: {
36 readonly blobs: BlobStoreShape;
37 readonly key: string;
38 readonly size: number;
39 readonly windowBytes?: number | undefined;
40 readonly maxWindows?: number | undefined;
41 }): RandomAccess => {
42 const windowBytes = options.windowBytes ?? PACK_WINDOW_BYTES;
43 const maxWindows = options.maxWindows ?? PACK_MAX_WINDOWS;
44 const windows = new Map<number, { start: number; bytes: Uint8Array }>();
45
46 const fetchWindow = (index: number) =>
47 Effect.gen(function* () {
48 const hit = windows.get(index);
49 if (hit !== undefined) {
50 windows.delete(index);
51 windows.set(index, hit);
52 return hit;
53 }
54 const start = index * windowBytes;
55 const body = yield* options.blobs
56 .get(options.key, { offset: start, length: windowBytes })
57 .pipe(
58 Effect.mapError((error) =>
59 StoreError.make({
60 reason: `incoming pack read ${options.key}: ${error.reason}`,
61 }),
62 ),
63 Effect.provide(RuntimeContext.phantom),
64 );
65 if (body === null) {
66 return yield* StoreError.make({ reason: `incoming pack missing: ${options.key}` });
67 }
68 const bytes = yield* body.bytes.pipe(
69 Effect.mapError((error) =>
70 StoreError.make({
71 reason: `incoming pack read ${options.key}: ${error.reason}`,
72 }),
73 ),
74 );
75 const slab = { start, bytes };
76 windows.set(index, slab);
77 while (windows.size > maxWindows) {
78 const oldest = windows.keys().next().value;
79 if (oldest === undefined) break;
80 windows.delete(oldest);
81 }
82 return slab;
83 });
84
85 return {
86 size: options.size,
87 awaitEnd: Effect.succeed(options.size),
88 readSync: (offset, length) => {
89 const end = Math.min(offset + length, options.size);
90 if (end <= offset) return new Uint8Array(0);
91 const first = Math.floor(offset / windowBytes);
92 if (first !== Math.floor((end - 1) / windowBytes)) return undefined;
93 const slab = windows.get(first);
94 if (slab === undefined) return undefined;
95 const from = offset - slab.start;
96 return slab.bytes.subarray(from, from + (end - offset));
97 },
98 read: (offset, length) =>
99 Effect.gen(function* () {
100 const end = Math.min(offset + length, options.size);
101 if (end <= offset) return new Uint8Array(0);
102 const first = Math.floor(offset / windowBytes);
103 const last = Math.floor((end - 1) / windowBytes);
104 if (first === last) {
105 // A VIEW, not a copy (DESIGN §22.4). The parser probes every
106 // entry with a window-sized read and copies only what it keeps;
107 // returning a slice here copied that probe window per entry —
108 // 15k entries × the probe size — which on a spilled 40 MiB push
109 // was the difference between ~6 s and ~40 s of ingest. The slab
110 // stays alive as long as any view does, so eviction is safe.
111 const slab = yield* fetchWindow(first);
112 const from = offset - slab.start;
113 return slab.bytes.subarray(from, from + (end - offset));
114 }
115 // A read spanning window boundaries: stitch the windows it covers.
116 const out = new Uint8Array(end - offset);
117 let written = 0;
118 for (let index = first; index <= last; index++) {
119 const slab = yield* fetchWindow(index);
120 const chunkStart = Math.max(offset, slab.start);
121 const chunkEnd = Math.min(end, slab.start + slab.bytes.length);
122 if (chunkEnd <= chunkStart) continue;
123 out.set(slab.bytes.subarray(chunkStart - slab.start, chunkEnd - slab.start), written);
124 written += chunkEnd - chunkStart;
125 }
126 return written === out.length ? out : out.subarray(0, written);
127 }),
128 };
129 };
130
131 /**
132 * A view of `source` starting at `start` — used to address the pack that
133 * follows the command section inside a spilled receive-pack body.
134 */
135 export const sliceRandomAccess = (source: RandomAccess, start: number): RandomAccess => ({
136 size: source.size - start,
137 read: (offset, length) => source.read(start + offset, length),
138 readSync:
139 source.readSync === undefined
140 ? undefined
141 : (offset, length) => source.readSync!(start + offset, length),
142 awaitEnd:
143 source.awaitEnd === undefined
144 ? undefined
145 : source.awaitEnd.pipe(Effect.map((total) => total - start)),
146 release: source.release === undefined ? undefined : (offset) => source.release!(start + offset),
147 });