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

Compare

+108 −0 in 1 place
new file, 108 lines
1+import type { SqliteStorage } from "./engine-project.ts";
2+import type { FileContents } from "./memory-fs.ts";
3+
4+function initialize(storage: SqliteStorage): void {
5+ storage.sql.exec(
6+ "CREATE TABLE IF NOT EXISTS git_fs_meta (scope TEXT, path TEXT, bytes INTEGER, PRIMARY KEY(scope,path))",
7+ );
8+ storage.sql.exec(
9+ "CREATE TABLE IF NOT EXISTS git_fs_chunks (scope TEXT, path TEXT, part INTEGER, data BLOB NOT NULL, PRIMARY KEY(scope,path,part))",
10+ );
11+ storage.sql.exec("CREATE TABLE IF NOT EXISTS git_fs_budget (scope TEXT PRIMARY KEY, bytes INTEGER NOT NULL)");
12+}
14+/** One Git operation's scratch files. Reads materialize one file, never the whole filesystem. */
15+export class GitFileContents implements FileContents {
16+ static recover(storage: SqliteStorage): void {
17+ initialize(storage);
18+ storage.transactionSync(() => {
19+ for (const table of ["git_fs_chunks", "git_fs_meta", "git_fs_budget"]) storage.sql.exec(`DELETE FROM ${table}`);
20+ });
21+ }
22+ readonly scope = crypto.randomUUID();
23+ private readonly storage: SqliteStorage;
24+ private readonly maxFileBytes: number;
25+ private readonly maxTotalBytes: number;
26+ private closed = false;
27+ constructor(storage: SqliteStorage, maxFileBytes: number, maxTotalBytes: number) {
28+ if (![maxFileBytes, maxTotalBytes].every((n) => Number.isSafeInteger(n) && n >= 0))
29+ throw Error("invalid Git scratch limits");
30+ this.storage = storage;
31+ this.maxFileBytes = maxFileBytes;
32+ this.maxTotalBytes = maxTotalBytes;
33+ initialize(storage);
34+ storage.sql.exec("INSERT INTO git_fs_budget VALUES (?, 0)", this.scope);
35+ }
36+ private active(): void {
37+ if (this.closed) throw Error("Git scratch files are closed");
38+ }
39+ private size(path: string): number | undefined {
40+ const rows = this.storage.sql
41+ .exec("SELECT bytes FROM git_fs_meta WHERE scope = ? AND path = ?", this.scope, path)
42+ .toArray() as { bytes: number }[];
43+ return rows[0]?.bytes;
44+ }
45+ write(path: string, bytes: Uint8Array): void {
46+ this.active();
47+ if (bytes.byteLength > this.maxFileBytes) throw Error("Git scratch file exceeds byte limit");
48+ this.storage.transactionSync(() => {
49+ const old = this.size(path) ?? 0;
50+ const rows = this.storage.sql.exec("SELECT bytes FROM git_fs_budget WHERE scope = ?", this.scope).toArray() as {
51+ bytes: number;
52+ }[];
53+ if (rows.length !== 1) throw Error("missing Git scratch budget");
54+ const next = rows[0]!.bytes - old + bytes.byteLength;
55+ if (!Number.isSafeInteger(next) || next < 0 || next > this.maxTotalBytes)
56+ throw Error("Git scratch exceeds total byte limit");
57+ this.storage.sql.exec("DELETE FROM git_fs_chunks WHERE scope = ? AND path = ?", this.scope, path);
58+ for (let at = 0; at < bytes.byteLength; at += 65536)
59+ this.storage.sql.exec(
60+ "INSERT INTO git_fs_chunks VALUES (?, ?, ?, ?)",
61+ this.scope,
62+ path,
63+ at / 65536,
64+ bytes.slice(at, at + 65536),
65+ );
66+ this.storage.sql.exec("INSERT OR REPLACE INTO git_fs_meta VALUES (?, ?, ?)", this.scope, path, bytes.byteLength);
67+ this.storage.sql.exec("UPDATE git_fs_budget SET bytes = ? WHERE scope = ?", next, this.scope);
68+ });
69+ }
70+ read(path: string): Uint8Array {
71+ this.active();
72+ const size = this.size(path);
73+ if (size === undefined || !Number.isSafeInteger(size) || size < 0 || size > this.maxFileBytes)
74+ throw Error("invalid Git scratch file");
75+ const bytes = new Uint8Array(size);
76+ for (let at = 0; at < size; at += 65536) {
77+ const rows = this.storage.sql
78+ .exec("SELECT data FROM git_fs_chunks WHERE scope = ? AND path = ? AND part = ?", this.scope, path, at / 65536)
79+ .toArray() as { data: ArrayBuffer }[];
80+ if (rows.length !== 1) throw Error("missing Git scratch chunk");
81+ const chunk = new Uint8Array(rows[0]!.data);
82+ if (chunk.byteLength !== Math.min(65536, size - at)) throw Error("invalid Git scratch chunk");
83+ bytes.set(chunk, at);
84+ }
85+ return bytes;
86+ }
87+ remove(path: string): void {
88+ this.active();
89+ this.storage.transactionSync(() => {
90+ const size = this.size(path);
91+ if (size === undefined) throw Error("missing Git scratch file");
92+ this.storage.sql.exec("DELETE FROM git_fs_chunks WHERE scope = ? AND path = ?", this.scope, path);
93+ this.storage.sql.exec("DELETE FROM git_fs_meta WHERE scope = ? AND path = ?", this.scope, path);
94+ this.storage.sql.exec("UPDATE git_fs_budget SET bytes = bytes - ? WHERE scope = ?", size, this.scope);
95+ });
96+ }
97+ [Symbol.dispose](): void {
98+ this.dispose();
99+ }
100+ dispose(): void {
101+ if (this.closed) return;
102+ this.storage.transactionSync(() => {
103+ for (const table of ["git_fs_chunks", "git_fs_meta", "git_fs_budget"])
104+ this.storage.sql.exec(`DELETE FROM ${table} WHERE scope = ?`, this.scope);
105+ });
106+ this.closed = true;
107+ }
108+}
MCP
compare on this site's /mcp
CLI
abso diff 116 318 server/src/git-file-contents.ts --json
JSON
/christopher/abso-core/compare/116..318?file=server%2Fsrc%2Fgit-file-contents.ts&format=json