Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/server/test/git-disk-clone.test.ts?version=287&format=json
249 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 183, 3 versions changed it

You're reading version 287. The newest is 318, and no version since then changed this file. Go to the newestCompare with 318

1import assert from "node:assert/strict";
2import { randomBytes } from "node:crypto";
3import { test } from "node:test";
4import git, { type HttpClient } from "isomorphic-git";
5import { GitDiskIntake } from "../src/git-disk-intake.ts";
6import { GitFileContents } from "../src/git-file-contents.ts";
7import { GitIntakeTrees } from "../src/git-intake-trees.ts";
8import { pushedSince } from "../src/ingest.ts";
9import { MemoryFS } from "../src/memory-fs.ts";
10import { commitShared } from "../src/push.ts";
11import { memoryStorage } from "./helpers.ts";
13const packet = (text: string | Uint8Array) => {
14 const body = Buffer.from(text);
15 return Buffer.concat([Buffer.from((body.length + 4).toString(16).padStart(4, "0")), body]);
16};
18for (const mode of ["success", "limit", "fatal", "server-intake", "text-intake", "binary-limit"])
19 test(`actual disk clone handles ${mode}`, async (t) => {
20 const rejectPack = mode === "limit";
21 const source = new MemoryFS(),
22 dir = "/repo";
23 await git.init({ fs: source, dir, defaultBranch: "main" });
24 const blob = mode === "text-intake" ? Buffer.from("x".repeat(16383) + "🚀é中".repeat(20000)) : randomBytes(400000);
25 if (mode === "binary-limit") blob[0] = 255;
26 const blobOid = await git.writeBlob({ fs: source, dir, blob });
27 const tree = await git.writeTree({
28 fs: source,
29 dir,
30 tree: [{ mode: "100644", path: "large.bin", oid: blobOid, type: "blob" }],
31 });
32 const author = {
33 name: "Capacity fixture",
34 email: "fixture@example.invalid",
35 timestamp: 1,
36 timezoneOffset: 0,
37 };
38 const base = await git.writeCommit({
39 fs: source,
40 dir,
41 commit: {
42 tree,
43 parent: [],
44 author,
45 committer: author,
46 message: "Base\n\nabso-version: 0\n",
47 },
48 });
49 const head = await git.writeCommit({
50 fs: source,
51 dir,
52 commit: {
53 tree,
54 parent: [base],
55 author,
56 committer: author,
57 message: "Fixture\n",
58 },
59 });
60 const packed = (
61 await git.packObjects({
62 fs: source,
63 dir,
64 oids: [head, base, tree, blobOid],
65 write: false,
66 })
67 ).packfile!;
68 const advertisement = Buffer.concat([
69 packet("# service=git-upload-pack\n"),
70 Buffer.from("0000"),
71 packet(`${head} HEAD\0symref=HEAD:refs/heads/main side-band-64k shallow\n`),
72 packet(`${head} refs/heads/main\n`),
73 Buffer.from("0000"),
74 ]);
75 let uploads = 0;
76 let uploadChunksRead = 0;
77 let uploadClosed = false;
78 const uploadParts = [packet("NAK\n")];
79 for (let at = 0; at < packed.length; at += 16000) {
80 uploadParts.push(packet(Buffer.concat([Buffer.from([1]), packed.subarray(at, at + 16000)])));
81 uploadParts.push(
82 packet(Buffer.concat([Buffer.from([2]), Buffer.from("Counting objects: 50% (1/2)\n".repeat(200))])),
83 );
84 if (mode === "fatal" && at === 16000) {
85 uploadParts.push(packet(Buffer.concat([Buffer.from([3]), Buffer.from("fixture remote failure")])));
86 break;
87 }
88 }
89 uploadParts.push(Buffer.from("0000"));
90 const http: HttpClient = {
91 request: async ({ url, method }) => {
92 const body = method === "POST" ? Buffer.concat(uploadParts) : advertisement;
93 if (method === "POST") uploads++;
94 return {
95 url,
96 method: method ?? "GET",
97 statusCode: 200,
98 statusMessage: "OK",
99 headers: {},
100 body: (async function* () {
101 try {
102 for (let at = 0; at < body.length; at += 997) {
103 if (method === "POST") uploadChunksRead++;
104 yield body.subarray(at, at + 997);
105 }
106 } finally {
107 if (method === "POST") uploadClosed = true;
108 }
109 })(),
110 };
111 },
112 };
113 const storage = memoryStorage();
114 const intake = new GitDiskIntake(storage, rejectPack ? 70000 : 2000000, 1000000);
115 const ingest = intake.ingest.bind(intake);
116 intake.ingest = async (...args) => {
117 await new Promise((resolve) => setTimeout(resolve, 25));
118 assert.ok(uploadChunksRead < 64, `pack source read ahead ${uploadChunksRead} chunks before disk consumption`);
119 return ingest(...args);
120 };
121 const cache = {};
122 intake.attach(cache);
123 const contents = new GitFileContents(storage, 1000000, 2000000);
124 const fs = new MemoryFS(contents);
125 const write = fs.promises.writeFile;
126 fs.promises.writeFile = async (path, ...args) => {
127 assert.ok(!/\.(pack|idx)$/.test(path), "clone must not persist a collected pack or index");
128 return write(path, ...args);
129 };
130 try {
131 if (mode === "server-intake" || mode === "text-intake" || mode === "binary-limit") {
132 const trees = new GitIntakeTrees(storage);
133 if (mode === "text-intake" || mode === "binary-limit") {
134 const original = GitDiskIntake.prototype.read;
135 t.mock.method(GitDiskIntake.prototype, "read", function (this: GitDiskIntake, oid: string) {
136 if (oid === blobOid) throw Error("whole blob read forbidden");
137 return original.call(this, oid);
138 });
139 }
140 const artifacts = {
141 get: async () => ({
142 info: async () => ({ remote: "https://fixture.invalid/repo" }),
143 createToken: async () => ({ plaintext: "invented-test-token" }),
144 [Symbol.dispose]() {},
145 }),
146 };
147 const found = await pushedSince(
148 artifacts as never,
149 "fixture",
150 undefined,
151 async (bytes) => {
152 assert.notEqual(mode, "binary-limit", "oversized binary must not reach the store");
153 assert.deepEqual(Buffer.from(bytes), blob);
154 return "fixture-blob";
155 },
156 mode === "binary-limit" ? () => "previous-blob" : undefined,
157 undefined,
158 {
159 storage,
160 maxPackBytes: 2000000,
161 maxObjectBytes: 1000000,
162 maxBinaryBytes: mode === "binary-limit" ? 100000 : undefined,
163 http,
164 retainFileChunks: (files, path, chunks, oid) => trees.retainChunks(files, path, chunks, oid),
165 },
166 );
167 assert.equal(found!.base, 0);
168 assert.equal(found!.commits.length, 1);
169 if (mode === "text-intake") assert.equal(found!.commits[0]!.files["large.bin"], blob.toString("utf8"));
170 else if (mode === "binary-limit") {
171 assert.equal(found!.commits[0]!.blobs["large.bin"], "previous-blob");
172 assert.deepEqual(found!.tooBig, ["large.bin"]);
173 } else assert.equal(found!.commits[0]!.blobs["large.bin"], "fixture-blob");
174 trees.dispose();
175 for (const table of ["git_pack_spool", "git_pack_resolve", "git_object_meta", "git_object_bytes"])
176 assert.equal(storage.db.prepare(`SELECT COUNT(*) AS n FROM ${table}`).get()!.n, 0);
177 return;
178 }
179 const clone = git.clone({
180 fs,
181 dir,
182 http,
183 cache,
184 url: "https://fixture.invalid/repo",
185 ref: "main",
186 singleBranch: true,
187 noCheckout: true,
188 });
189 if (mode !== "success") {
190 await assert.rejects(clone, rejectPack ? /pack exceeds byte limit/ : /fixture remote failure/);
191 assert.equal(uploadClosed, true);
192 for (const table of ["git_pack_spool", "git_pack_resolve", "git_object_meta", "git_object_bytes"])
193 assert.equal(storage.db.prepare(`SELECT COUNT(*) AS n FROM ${table}`).get()!.n, 0);
194 return;
195 }
196 await clone;
197 intake.assertReady();
198 assert.equal((await git.log({ fs, dir, cache, ref: "main" })).length, 2);
199 let walked = 0;
200 await git.walk({
201 fs,
202 dir,
203 cache,
204 trees: [git.TREE({ ref: head })],
205 map: async (path, [entry]) => {
206 if (path === "large.bin") {
207 assert.deepEqual(Buffer.from((await entry!.content())!), blob);
208 walked++;
209 }
210 return true;
211 },
212 });
213 assert.equal(walked, 1);
214 assert.equal(uploads, 1);
215 assert.equal(await git.resolveRef({ fs, dir, ref: "main" }), head);
216 assert.equal((await git.readCommit({ fs, dir, cache, oid: head })).commit.tree, tree);
217 assert.deepEqual(Buffer.from((await git.readBlob({ fs, dir, cache, oid: blobOid })).blob), blob);
218 const exported = await commitShared(
219 fs,
220 dir,
221 head,
222 {
223 version: 1,
224 files: { "new.txt": "Disk export fixture" },
225 blobs: { "large.bin": "fixture-blob" },
226 },
227 () => blob,
228 undefined,
229 cache,
230 );
231 const next = await git.readCommit({ fs, dir, cache, oid: exported.commit });
232 assert.deepEqual(next.commit.parent, [head]);
233 assert.deepEqual(
234 Buffer.from((await git.readBlob({ fs, dir, cache, oid: exported.commit, filepath: "large.bin" })).blob),
235 blob,
236 );
237 assert.equal(
238 Buffer.from(
239 (await git.readBlob({ fs, dir, cache, oid: exported.commit, filepath: "new.txt" })).blob,
240 ).toString(),
241 "Disk export fixture",
242 );
243 for (const entry of fs.entries.values()) if (entry.kind === "file") assert.equal(entry.data.byteLength, 0);
244 } finally {
245 contents.dispose();
246 intake.dispose();
247 storage.db.close();
248 }
249 });