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=178&format=json
213 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 178, 1 version changed it

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