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 32 to 318

Compare

+275 −0 in 1 place
new file, 275 lines
1+// In from plain git (#8, demo moment 5): commits a person pushed to the
2+// project's Artifacts repo, read back for project.ingest. Every commit the
3+// Worker pushes ends "abso-version: N" ("avcs-version: N" before the rename); the commits above the newest such
4+// commit that haven't been taken in yet are the person's, and N is their base.
5+import git, { type HttpClient } from "isomorphic-git";
6+import type { FileModes } from "../../worker/project/project-api.ts";
7+import type { SqliteStorage } from "./engine-project.ts";
8+import { gitBlobText, InvalidGitUtf8 } from "./git-blob-text.ts";
9+import { GitDiskIntake } from "./git-disk-intake.ts";
10+import { gitHttp } from "./git-http.ts";
11+import { MemoryFS } from "./memory-fs.ts";
12+import { repoName } from "./push.ts";
14+const http = gitHttp();
16+const EXEC = 0o100755;
17+const LINK = 0o120000;
19+/** How far back to look for the Worker's last commit. */
20+const DEPTH = 50;
21+const text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: false });
23+export interface PushedCommits {
24+ /** main's head when read. */
25+ head: string;
26+ author: string;
27+ base: number;
28+ /** Oldest first, each with its whole tree as text and every file's mode. Binary files are left out. */
29+ commits: {
30+ sha: string;
31+ message: string;
32+ files: Record<string, string>;
33+ modes: FileModes;
34+ blobs: Record<string, string>;
35+ }[];
36+ /** Binary files over the blob limit, left out of the commits. */
37+ tooBig: string[];
38+}
40+export function absoVersion(message: string): number | undefined {
41+ const m = /^(?:abso|avcs)-version: (\d+)\s*$/m.exec(message);
42+ return m ? Number(m[1]) : undefined;
43+}
45+async function readAccess(
46+ artifacts: Artifacts,
47+ project: string,
48+): Promise<{
49+ url: string;
50+ onAuth: () => { username: string; password: string };
51+} | null> {
52+ let repo: Awaited<ReturnType<Artifacts["get"]>>;
53+ try {
54+ repo = await artifacts.get(repoName(project));
55+ } catch {
56+ return null;
57+ }
58+ using r = repo;
59+ const [info, token] = await Promise.all([r.info(), r.createToken("read", 600)]);
60+ const password = token.plaintext.split("?expires=")[0]!;
61+ return { url: info!.remote, onAuth: () => ({ username: "x", password }) };
62+}
64+/** main's head on the project's repo, or null when there's no repo or no main yet. Cheap: one ref listing. */
65+export async function remoteHead(artifacts: Artifacts, project: string): Promise<string | null> {
66+ const access = await readAccess(artifacts, project);
67+ if (!access) return null;
68+ const refs = await git.listServerRefs({
69+ http,
70+ url: access.url,
71+ onAuth: access.onAuth,
72+ prefix: "refs/heads/main",
73+ });
74+ return refs.find((r) => r.ref === "refs/heads/main")?.oid ?? null;
75+}
77+/**
78+ * The commits pushed since `seen` (the last head this project handled), or
79+ * none when every commit above the Worker's last one has been seen.
80+ */
81+/** Stores a binary file's bytes and answers their SHA-256, or null when they're over the limit. */
82+export type StoreBlob = (bytes: Uint8Array) => Promise<string | null>;
83+export type RetainFileChunks = (
84+ files: Record<string, string>,
85+ path: string,
86+ chunks: Iterable<string>,
87+ oid: string,
88+) => void;
90+/**
91+ * `oldBlob` gives shared's blob for a path: a binary file over the limit
92+ * keeps it, since a path missing from a commit would mean the file was deleted.
93+ */
94+export async function pushedSince(
95+ artifacts: Artifacts,
96+ project: string,
97+ seen: string | undefined,
98+ storeBlob?: StoreBlob,
99+ oldBlob: (path: string) => string | undefined = () => undefined,
100+ retainFile: (files: Record<string, string>, path: string, body: string, oid?: string) => void = (
101+ files,
102+ path,
103+ body,
104+ ) => {
105+ files[path] = body;
106+ },
107+ disk?: {
108+ storage: SqliteStorage;
109+ maxPackBytes: number;
110+ maxObjectBytes: number;
111+ maxDecodedBytes?: number;
112+ maxBinaryBytes?: number;
113+ http?: HttpClient;
114+ retainFileChunks?: RetainFileChunks;
115+ },
116+): Promise<PushedCommits | null> {
117+ const access = await readAccess(artifacts, project);
118+ if (!access) return null;
119+ const fs = new MemoryFS();
120+ const dir = "/repo";
121+ using intake = disk
122+ ? new GitDiskIntake(disk.storage, disk.maxPackBytes, disk.maxObjectBytes, disk.maxDecodedBytes)
123+ : undefined;
124+ const cache = disk ? {} : undefined;
125+ if (intake && cache) intake.attach(cache);
126+ await git.clone({
127+ fs,
128+ cache,
129+ http: disk?.http ?? http,
130+ dir,
131+ url: access.url,
132+ onAuth: access.onAuth,
133+ ref: "main",
134+ singleBranch: true,
135+ depth: DEPTH,
136+ noCheckout: true,
137+ });
138+ intake?.assertReady();
139+ const log = await git.log({ fs, dir, cache, ref: "main", depth: DEPTH });
140+ if (log.length === 0) return null;
141+ const head = log[0]!.oid;
142+ const fresh: typeof log = [];
143+ let base: number | undefined;
144+ let past = false;
145+ for (const c of log) {
146+ if (c.oid === seen) past = true;
147+ base = absoVersion(c.commit.message);
148+ if (base !== undefined) break;
149+ if (!past) fresh.unshift(c);
150+ }
151+ if (base === undefined) throw new Error(`no abso-version commit in main's last ${DEPTH} commits`);
152+ const commits = [];
153+ const tooBig = new Set<string>();
154+ for (const c of fresh)
155+ commits.push({
156+ sha: c.oid,
157+ message: c.commit.message.trim(),
158+ ...(await treeText(
159+ fs,
160+ dir,
161+ c.oid,
162+ storeBlob,
163+ tooBig,
164+ oldBlob,
165+ retainFile,
166+ cache,
167+ intake,
168+ disk?.retainFileChunks,
169+ disk?.maxBinaryBytes,
170+ )),
171+ });
172+ return {
173+ head,
174+ author: fresh.at(-1)?.commit.author.name ?? "git",
175+ base,
176+ commits,
177+ tooBig: [...tooBig],
178+ };
179+}
181+/**
182+ * A commit's whole tree as text, and every file's mode: 100755 is exec, and
183+ * a link (120000) is "" with its target in modes. Files that aren't UTF-8
184+ * are left out.
185+ */
186+export async function treeText(
187+ fs: MemoryFS,
188+ dir: string,
189+ oid: string,
190+ storeBlob: StoreBlob | undefined,
191+ tooBig: Set<string>,
192+ oldBlob: (path: string) => string | undefined,
193+ retainFile: (files: Record<string, string>, path: string, body: string, oid?: string) => void,
194+ cache: object = {},
195+ intake?: GitDiskIntake,
196+ retainFileChunks?: RetainFileChunks,
197+ maxBinaryBytes?: number,
198+): Promise<{
199+ files: Record<string, string>;
200+ modes: FileModes;
201+ blobs: Record<string, string>;
202+}> {
203+ const files: Record<string, string> = {};
204+ const modes: FileModes = {};
205+ const blobs: Record<string, string> = {};
206+ await git.walk({
207+ fs,
208+ cache,
209+ dir,
210+ trees: [git.TREE({ ref: oid })],
211+ iterate: async (walk, children) => {
212+ for (const child of children) await walk(child);
213+ return [];
214+ },
215+ reduce: async () => undefined,
216+ map: async (path, [entry]) => {
217+ if (!entry || path === ".") return true;
218+ if ((await entry.type()) !== "blob") return true;
219+ const mode = await entry.mode();
220+ const blobOid = await entry.oid();
221+ if (intake && retainFileChunks && mode !== LINK) {
222+ const stored = intake.bytes(blobOid);
223+ if (stored?.type !== "blob") throw Error("Git intake blob is missing");
224+ let valid = true;
225+ try {
226+ // Validate before retaining: a late invalid byte must take the binary path.
227+ for (const _ of gitBlobText(stored)) {
228+ /* Retain no decoded text. */
229+ }
230+ } catch (error) {
231+ if (!(error instanceof InvalidGitUtf8)) throw error;
232+ valid = false;
233+ }
234+ if (valid) {
235+ retainFileChunks(files, path, gitBlobText(stored), blobOid);
236+ modes[path] = { exec: mode === EXEC, link: null };
237+ return true;
238+ }
239+ if (storeBlob && maxBinaryBytes !== undefined && stored.byteLength > maxBinaryBytes) {
240+ tooBig.add(path);
241+ const kept = oldBlob(path);
242+ if (kept) blobs[path] = kept;
243+ return true;
244+ }
245+ }
246+ const bytes = await entry.content();
247+ if (!bytes) return true;
248+ let body: string;
249+ try {
250+ body = text.decode(bytes);
251+ } catch {
252+ // Binary (#27): the bytes go to the blob store, and the file is a pointer to them.
253+ const sha = storeBlob ? await storeBlob(bytes) : null;
254+ if (sha) {
255+ blobs[path] = sha;
256+ modes[path] = { exec: mode === EXEC, link: null };
257+ } else if (storeBlob) {
258+ tooBig.add(path);
259+ const kept = oldBlob(path);
260+ if (kept) blobs[path] = kept;
261+ }
262+ return true;
263+ }
264+ if (mode === LINK) {
265+ retainFile(files, path, "");
266+ modes[path] = { exec: false, link: body };
267+ } else {
268+ retainFile(files, path, body, blobOid);
269+ modes[path] = { exec: mode === EXEC, link: null };
270+ }
271+ return true;
272+ },
273+ });
274+ return { files, modes, blobs };
275+}
MCP
compare on this site's /mcp
CLI
abso diff 32 318 server/src/ingest.ts --json
JSON
/christopher/abso-core/compare/32..318?file=server%2Fsrc%2Fingest.ts&format=json