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

Versions 166 to 318

Compare

+78 −29 in 6 places
Lines 3–16 at 318 (were 3–11 at 166)
33// is created on first use. isomorphic-git works in an in-memory filesystem:
44// a depth-1 clone of main with nothing checked out, then a new tree from
55// shared's files that compresses and writes only what changed (abso issue 293).
6+import { createHash } from "node:crypto";
67import git from "isomorphic-git";
78import http from "isomorphic-git/http/web";
89import type { FileModes } from "../../worker/project/project-api.ts";
10+import type { SqliteStorage } from "./engine-project.ts";
11+import { GitDiskIntake } from "./git-disk-intake.ts";
12+import { GitFileContents } from "./git-file-contents.ts";
13+import { gitHttp } from "./git-http.ts";
914import { MemoryFS } from "./memory-fs.ts";
1015
1116export interface PushResult {
Lines 38–60 at 318 (were 33–60 at 166)
3338 prefix = "",
3439 files = new Map<string, TreeFile>(),
3540 trees = new Map<string, string>(),
41+ cache?: object,
3642): Promise<{ files: Map<string, TreeFile>; trees: Map<string, string> }> {
3743 trees.set(prefix, oid);
38− const { tree } = await git.readTree({ fs, dir, oid });
44+ const { tree } = await git.readTree({ fs, dir, oid, cache });
3945 for (const e of tree) {
4046 const path = prefix === "" ? e.path : `${prefix}/${e.path}`;
41− if (e.type === "tree") await readTreeFiles(fs, dir, e.oid, path, files, trees);
47+ if (e.type === "tree") await readTreeFiles(fs, dir, e.oid, path, files, trees, cache);
4248 else if (e.type === "blob") files.set(path, { oid: e.oid, mode: e.mode });
4349 }
4450 return { files, trees };
4551}
4652
4753/**
48− * A blob's git oid, with the platform's SHA-1 (abso issue 293): native and awaited, so hashing every file of a big
49− * project neither burns the object's CPU in JavaScript nor holds its other requests back for long.
54+ * Hash the Git header and content separately so export does not allocate a second whole-file buffer.
5055 */
5156export async function blobOid(bytes: Uint8Array): Promise<string> {
52− const head = new TextEncoder().encode(`blob ${bytes.byteLength}\0`);
53− const all = new Uint8Array(head.byteLength + bytes.byteLength);
54− all.set(head);
55− all.set(bytes, head.byteLength);
56− const d = new Uint8Array(await crypto.subtle.digest("SHA-1", all));
57− return [...d].map((b) => b.toString(16).padStart(2, "0")).join("");
57+ return createHash("sha1").update(`blob ${bytes.byteLength}\0`).update(bytes).digest("hex");
5858}
5959
6060const parentOf = (path: string) => {
Lines 152–169 at 318 (were 152–160 at 166)
152152 shared: SharedFiles,
153153 readBlob: (sha256: string) => Uint8Array | null,
154154 part: (commit: string) => Promise<void> = async () => {},
155+ cache?: object,
155156): Promise<{ commit: string; changed: number }> {
156157 const old = head
157− ? await readTreeFiles(fs, dir, (await git.readCommit({ fs, dir, oid: head })).commit.tree)
158+ ? await readTreeFiles(
159+ fs,
160+ dir,
161+ (await git.readCommit({ fs, dir, oid: head, cache })).commit.tree,
162+ "",
163+ new Map(),
164+ new Map(),
165+ cache,
166+ )
158167 : { files: new Map<string, TreeFile>(), trees: new Map<string, string>() };
159168 const blobs = shared.blobs ?? {};
160169 const now = new Map<string, TreeFile>();
Lines 179–197 at 318 (were 170–192 at 166)
170179 // Each file goes into the tree as a blob with its mode (#27): 100755 for exec, 120000 for a link, whose blob is
171180 // its target.
172181 const enc = new TextEncoder();
173− for (const path of Object.keys(shared.files)) {
174− const mode = shared.modes?.[path];
175− const link = typeof mode?.link === "string" ? mode.link : null;
176− const bytes = enc.encode(link ?? shared.files[path]!);
177− await put(path, bytes, link !== null ? "120000" : mode?.exec ? "100755" : "100644", await blobOid(bytes));
178− }
179182 const agent = shared.last?.agent ?? "abso";
180183 const author = { name: agent, email: `${agent}@abso.invalid` };
181184 const committer = { name: "abso", email: "abso@abso.invalid" };
182185 const say = (p: string) => `${shared.last?.message ?? "Start the project"}${p}\n\nabso-version: ${shared.version}\n`;
183186 let parent = head;
184− // A part holds what's written so far, and the last push's entries for the binary files not reached yet.
187+ // A part holds what's written so far and preserves old entries for files not reached yet.
185188 const commitNow = async (message: string) => {
186189 const files = new Map(now);
187− for (const [path, f] of old.files) if (!files.has(path) && path in blobs) files.set(path, f);
190+ for (const [path, f] of old.files)
191+ if (!files.has(path) && (path in blobs || path in shared.files)) files.set(path, f);
188192 const tree = await writeTrees(fs, dir, files, old.trees, changed);
189193 parent = await git.commit({
194+ cache,
190195 fs,
191196 dir,
192197 message,
Lines 203–226 at 318 (were 198–209 at 166)
198203 });
199204 return parent;
200205 };
201− // Binary files (#27) aren't in files: their bytes come from the blob store.
202− // Artifacts refuses a push much over 20 MB, so when the new blobs add up
206+ // Artifacts refuses a push much over 20 MB, so when new text or binary blobs add up
203207 // to more than PUSH_BATCH, the earlier ones go in commits of their own
204208 // first. Every part carries abso-version, so intake never takes them in.
205209 let pending = 0;
206210 let parts = 0;
211+ for (const path of Object.keys(shared.files)) {
212+ const mode = shared.modes?.[path];
213+ const link = typeof mode?.link === "string" ? mode.link : null;
214+ const bytes = enc.encode(link ?? shared.files[path]!);
215+ const oid = await blobOid(bytes);
216+ const fresh = old.files.get(path)?.oid !== oid;
217+ if (fresh && pending > 0 && pending + bytes.byteLength > PUSH_BATCH) {
218+ await part(await commitNow(say(` (part ${++parts})`)));
219+ pending = 0;
220+ }
221+ if (fresh) pending += bytes.byteLength;
222+ await put(path, bytes, link !== null ? "120000" : mode?.exec ? "100755" : "100644", oid);
223+ }
207224 for (const [path, sha] of Object.entries(blobs)) {
208225 const bytes = readBlob(sha);
209226 if (!bytes) throw new Error(`${path}: blob ${sha} is missing from the project's store`);
Lines 243–301 at 318 (were 226–252 at 166)
226243 shared: SharedFiles,
227244 /** A binary file's bytes by SHA-256 (#27), from the project's blob store. */
228245 readBlob: (sha256: string) => Uint8Array | null = () => null,
246+ disk?: { storage: SqliteStorage; maxPackBytes: number; maxObjectBytes: number; maxDecodedBytes: number },
229247): Promise<PushResult> {
230248 const t0 = Date.now();
231249 const name = repoName(project);
232250 const { remote, secret } = await writeAccess(artifacts, name);
233251 const onAuth = () => ({ username: "x", password: secret });
234− let fs = new MemoryFS();
252+ using contents = disk ? new GitFileContents(disk.storage, disk.maxObjectBytes, disk.maxDecodedBytes) : undefined;
253+ using intake = disk
254+ ? new GitDiskIntake(disk.storage, disk.maxPackBytes, disk.maxObjectBytes, disk.maxDecodedBytes)
255+ : undefined;
256+ const cache = disk ? {} : undefined;
257+ if (intake && cache) intake.attach(cache);
258+ const transport = disk ? gitHttp() : http;
259+ let fs = new MemoryFS(contents);
235260 const dir = "/repo";
236261 // A new repo has no main yet. isomorphic-git "clones" an empty repo without
237262 // an error but leaves HEAD on master, so check for main and start clean.
238263 // Nothing is checked out: the new tree is built from tree objects (293).
239− const head = await git
240− .clone({ fs, http, dir, url: remote, ref: "main", singleBranch: true, depth: 1, noCheckout: true, onAuth })
241− .then(() => git.resolveRef({ fs, dir, ref: "main" }))
242− .catch(() => null);
264+ // Only an explicitly absent main is an empty repository. Transport, decode,
265+ // and size failures must never become permission to replace remote history.
266+ const refs = await git.listServerRefs({ http: transport, url: remote, onAuth, prefix: "refs/heads/main" });
267+ let head: string | null = null;
268+ if (refs.some((ref) => ref.ref === "refs/heads/main")) {
269+ await git.clone({
270+ fs,
271+ http: transport,
272+ cache,
273+ dir,
274+ url: remote,
275+ ref: "main",
276+ singleBranch: true,
277+ depth: 1,
278+ noCheckout: true,
279+ onAuth,
280+ });
281+ intake?.assertReady();
282+ head = await git.resolveRef({ fs, dir, ref: "main" });
283+ }
243284 if (head === null) {
244− fs = new MemoryFS();
285+ fs = new MemoryFS(contents);
245286 await git.init({ fs, dir, defaultBranch: "main" });
246287 }
247288 const t1 = Date.now();
248− const push = () => git.push({ fs, http, dir, url: remote, ref: "main", remoteRef: "main", onAuth });
249− const { commit, changed } = await commitShared(fs, dir, head, shared, readBlob, async () => void (await push()));
289+ const push = () => git.push({ fs, http: transport, cache, dir, url: remote, ref: "main", remoteRef: "main", onAuth });
290+ const { commit, changed } = await commitShared(
291+ fs,
292+ dir,
293+ head,
294+ shared,
295+ readBlob,
296+ async () => void (await push()),
297+ cache,
298+ );
250299 const t2 = Date.now();
251300 await push();
252301 console.log(
MCP
compare on this site's /mcp
CLI
abso diff 166 318 server/src/push.ts --json
JSON
/christopher/abso-core/compare/166..318?file=server%2Fsrc%2Fpush.ts&format=json