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 149 to 150

Compare

+174 −0 in 1 place
new file, 174 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 from "isomorphic-git";
6+import http from "isomorphic-git/http/web";
7+import type { FileModes } from "../../worker/project/project-api.ts";
8+import { MemoryFS } from "./memory-fs.ts";
9+import { repoName } from "./push.ts";
11+const EXEC = 0o100755;
12+const LINK = 0o120000;
14+/** How far back to look for the Worker's last commit. */
15+const DEPTH = 50;
16+const text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: false });
18+export interface PushedCommits {
19+ /** main's head when read. */
20+ head: string;
21+ author: string;
22+ base: number;
23+ /** Oldest first, each with its whole tree as text and every file's mode. Binary files are left out. */
24+ commits: {
25+ sha: string;
26+ message: string;
27+ files: Record<string, string>;
28+ modes: FileModes;
29+ blobs: Record<string, string>;
30+ }[];
31+ /** Binary files over the blob limit, left out of the commits. */
32+ tooBig: string[];
33+}
35+export function absoVersion(message: string): number | undefined {
36+ const m = /^(?:abso|avcs)-version: (\d+)\s*$/m.exec(message);
37+ return m ? Number(m[1]) : undefined;
38+}
40+async function readAccess(
41+ artifacts: Artifacts,
42+ project: string,
43+): Promise<{ url: string; onAuth: () => { username: string; password: string } } | null> {
44+ let repo: Awaited<ReturnType<Artifacts["get"]>>;
45+ try {
46+ repo = await artifacts.get(repoName(project));
47+ } catch {
48+ return null;
49+ }
50+ using r = repo;
51+ const [info, token] = await Promise.all([r.info(), r.createToken("read", 600)]);
52+ const password = token.plaintext.split("?expires=")[0]!;
53+ return { url: info!.remote, onAuth: () => ({ username: "x", password }) };
54+}
56+/** main's head on the project's repo, or null when there's no repo or no main yet. Cheap: one ref listing. */
57+export async function remoteHead(artifacts: Artifacts, project: string): Promise<string | null> {
58+ const access = await readAccess(artifacts, project);
59+ if (!access) return null;
60+ const refs = await git.listServerRefs({ http, url: access.url, onAuth: access.onAuth, prefix: "refs/heads/main" });
61+ return refs.find((r) => r.ref === "refs/heads/main")?.oid ?? null;
62+}
64+/**
65+ * The commits pushed since `seen` (the last head this project handled), or
66+ * none when every commit above the Worker's last one has been seen.
67+ */
68+/** Stores a binary file's bytes and answers their SHA-256, or null when they're over the limit. */
69+export type StoreBlob = (bytes: Uint8Array) => Promise<string | null>;
71+/**
72+ * `oldBlob` gives shared's blob for a path: a binary file over the limit
73+ * keeps it, since a path missing from a commit would mean the file was deleted.
74+ */
75+export async function pushedSince(
76+ artifacts: Artifacts,
77+ project: string,
78+ seen: string | undefined,
79+ storeBlob?: StoreBlob,
80+ oldBlob: (path: string) => string | undefined = () => undefined,
81+): Promise<PushedCommits | null> {
82+ const access = await readAccess(artifacts, project);
83+ if (!access) return null;
84+ const fs = new MemoryFS();
85+ const dir = "/repo";
86+ await git.clone({
87+ fs,
88+ http,
89+ dir,
90+ url: access.url,
91+ onAuth: access.onAuth,
92+ ref: "main",
93+ singleBranch: true,
94+ depth: DEPTH,
95+ noCheckout: true,
96+ });
97+ const log = await git.log({ fs, dir, ref: "main", depth: DEPTH });
98+ if (log.length === 0) return null;
99+ const head = log[0]!.oid;
100+ const fresh: typeof log = [];
101+ let base: number | undefined;
102+ let past = false;
103+ for (const c of log) {
104+ if (c.oid === seen) past = true;
105+ base = absoVersion(c.commit.message);
106+ if (base !== undefined) break;
107+ if (!past) fresh.unshift(c);
108+ }
109+ if (base === undefined) throw new Error(`no abso-version commit in main's last ${DEPTH} commits`);
110+ const commits = [];
111+ const tooBig = new Set<string>();
112+ for (const c of fresh)
113+ commits.push({
114+ sha: c.oid,
115+ message: c.commit.message.trim(),
116+ ...(await treeText(fs, dir, c.oid, storeBlob, tooBig, oldBlob)),
117+ });
118+ return { head, author: fresh.at(-1)?.commit.author.name ?? "git", base, commits, tooBig: [...tooBig] };
119+}
121+/**
122+ * A commit's whole tree as text, and every file's mode: 100755 is exec, and
123+ * a link (120000) is "" with its target in modes. Files that aren't UTF-8
124+ * are left out.
125+ */
126+async function treeText(
127+ fs: MemoryFS,
128+ dir: string,
129+ oid: string,
130+ storeBlob: StoreBlob | undefined,
131+ tooBig: Set<string>,
132+ oldBlob: (path: string) => string | undefined,
133+): Promise<{ files: Record<string, string>; modes: FileModes; blobs: Record<string, string> }> {
134+ const files: Record<string, string> = {};
135+ const modes: FileModes = {};
136+ const blobs: Record<string, string> = {};
137+ await git.walk({
138+ fs,
139+ dir,
140+ trees: [git.TREE({ ref: oid })],
141+ map: async (path, [entry]) => {
142+ if (!entry || path === ".") return true;
143+ if ((await entry.type()) !== "blob") return true;
144+ const bytes = await entry.content();
145+ if (!bytes) return true;
146+ let body: string;
147+ try {
148+ body = text.decode(bytes);
149+ } catch {
150+ // Binary (#27): the bytes go to the blob store, and the file is a pointer to them.
151+ const sha = storeBlob ? await storeBlob(bytes) : null;
152+ if (sha) {
153+ blobs[path] = sha;
154+ modes[path] = { exec: (await entry.mode()) === EXEC, link: null };
155+ } else if (storeBlob) {
156+ tooBig.add(path);
157+ const kept = oldBlob(path);
158+ if (kept) blobs[path] = kept;
159+ }
160+ return true;
161+ }
162+ const mode = await entry.mode();
163+ if (mode === LINK) {
164+ files[path] = "";
165+ modes[path] = { exec: false, link: body };
166+ } else {
167+ files[path] = body;
168+ modes[path] = { exec: mode === EXEC, link: null };
169+ }
170+ return true;
171+ },
172+ });
173+ return { files, modes, blobs };
174+}
MCP
compare on this site's /mcp
CLI
abso diff 149 150 server/src/ingest.ts --json
JSON
/christopher/abso-core/compare/149..150?file=server%2Fsrc%2Fingest.ts&format=json