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

Compare

+158 −0 in 1 place
new file, 158 lines
1+import { createHash } from "node:crypto";
2+import type { SqliteStorage } from "./engine-project.ts";
3+import type { GitByteRanges } from "./git-delta-stream.ts";
4+import { GitIntakeLimitError } from "./git-intake-limit.ts";
5+
6+export type StoredGitBytes = GitByteRanges & {
7+ id: string;
8+ oid: string;
9+ type: string;
10+};
12+/** Intake-local object bytes; callers own this store's lifetime. */
13+export class GitObjectBytes {
14+ readonly scope = crypto.randomUUID();
15+ private closed = false;
16+ private readonly storage: SqliteStorage;
17+ private readonly maxDecodedBytes: number;
18+ constructor(storage: SqliteStorage, maxDecodedBytes = Number.MAX_SAFE_INTEGER) {
19+ if (!Number.isSafeInteger(maxDecodedBytes) || maxDecodedBytes < 0) throw Error("invalid decoded byte limit");
20+ this.storage = storage;
21+ this.maxDecodedBytes = maxDecodedBytes;
22+ storage.sql.exec(
23+ "CREATE TABLE IF NOT EXISTS git_object_bytes (scope TEXT, id TEXT, part INTEGER, data BLOB NOT NULL, PRIMARY KEY(scope,id,part))",
24+ );
25+ storage.sql.exec(
26+ "CREATE TABLE IF NOT EXISTS git_object_meta (scope TEXT, id TEXT, type TEXT, bytes INTEGER, oid TEXT, PRIMARY KEY(scope,id))",
27+ );
28+ storage.sql.exec("CREATE TABLE IF NOT EXISTS git_object_budget (scope TEXT PRIMARY KEY, decoded INTEGER NOT NULL)");
29+ storage.sql.exec("INSERT INTO git_object_budget VALUES (?, 0)", this.scope);
30+ }
31+ private active(): void {
32+ if (this.closed) throw Error("Git object store is closed");
33+ }
34+ put(
35+ type: string,
36+ produce: (write: (bytes: Uint8Array) => void) => void,
37+ maxBytes: number,
38+ rawDelta = false,
39+ ): StoredGitBytes {
40+ this.active();
41+ if (!["blob", "tree", "commit", "tag"].includes(type) || !Number.isSafeInteger(maxBytes) || maxBytes < 0)
42+ throw Error("invalid Git object limits or type");
43+ const id = crypto.randomUUID();
44+ this.storage.transactionSync(() => {
45+ const budgetScope = rawDelta ? `${this.scope}:raw` : this.scope;
46+ if (rawDelta) this.storage.sql.exec("INSERT OR IGNORE INTO git_object_budget VALUES (?, 0)", budgetScope);
47+ const budget = this.storage.sql
48+ .exec("SELECT decoded FROM git_object_budget WHERE scope = ?", budgetScope)
49+ .toArray() as { decoded: number }[];
50+ if (budget.length !== 1) throw Error("missing Git decoded budget");
51+ const remaining = this.maxDecodedBytes - budget[0]!.decoded;
52+ let length = 0,
53+ part = 0,
54+ used = 0;
55+ const pending = new Uint8Array(65536);
56+ const flush = () => {
57+ this.storage.sql.exec(
58+ "INSERT INTO git_object_bytes VALUES (?, ?, ?, ?)",
59+ this.scope,
60+ id,
61+ part++,
62+ pending.slice(0, used),
63+ );
64+ used = 0;
65+ };
66+ produce((bytes) => {
67+ if (length + bytes.length > maxBytes) throw new GitIntakeLimitError("object", "Git object exceeds byte limit");
68+ if (length + bytes.length > remaining)
69+ throw new GitIntakeLimitError(
70+ rawDelta ? "delta" : "decoded",
71+ rawDelta ? "Git pack exceeds total delta byte limit" : "Git pack exceeds total decoded byte limit",
72+ );
73+ length += bytes.length;
74+ for (let at = 0; at < bytes.length; ) {
75+ const count = Math.min(pending.length - used, bytes.length - at);
76+ pending.set(bytes.subarray(at, at + count), used);
77+ at += count;
78+ used += count;
79+ if (used === pending.length) flush();
80+ }
81+ });
82+ if (used) flush();
83+ const hash = createHash("sha1").update(`${type} ${length}\0`);
84+ for (let index = 0; index < part; index++) hash.update(this.chunk(id, index));
85+ this.storage.sql.exec(
86+ "INSERT INTO git_object_meta VALUES (?, ?, ?, ?, ?)",
87+ this.scope,
88+ id,
89+ type,
90+ length,
91+ hash.digest("hex"),
92+ );
93+ this.storage.sql.exec("UPDATE git_object_budget SET decoded = decoded + ? WHERE scope = ?", length, budgetScope);
94+ });
95+ return this.get(id);
96+ }
97+ private chunk(id: string, part: number): Uint8Array {
98+ this.active();
99+ const rows = this.storage.sql
100+ .exec("SELECT data FROM git_object_bytes WHERE scope = ? AND id = ? AND part = ?", this.scope, id, part)
101+ .toArray() as { data: ArrayBuffer }[];
102+ if (rows.length !== 1) throw Error("missing Git object chunk");
103+ const bytes = new Uint8Array(rows[0]!.data);
104+ if (!bytes.length || bytes.length > 65536) throw Error("invalid Git object chunk");
105+ return bytes;
106+ }
107+ get(id: string): StoredGitBytes {
108+ this.active();
109+ const rows = this.storage.sql
110+ .exec("SELECT type, bytes, oid FROM git_object_meta WHERE scope = ? AND id = ?", this.scope, id)
111+ .toArray() as { type: string; bytes: number; oid: string }[];
112+ if (rows.length !== 1) throw Error("missing Git object");
113+ const meta = rows[0]!;
114+ return {
115+ id,
116+ oid: meta.oid,
117+ type: meta.type,
118+ byteLength: meta.bytes,
119+ read: (start, length) => {
120+ this.active();
121+ if (
122+ !Number.isSafeInteger(start) ||
123+ !Number.isSafeInteger(length) ||
124+ start < 0 ||
125+ length < 0 ||
126+ length > 65536 ||
127+ start + length > meta.bytes
128+ )
129+ throw Error("invalid Git object range");
130+ const result = new Uint8Array(length);
131+ for (let copied = 0; copied < length; ) {
132+ const position = start + copied,
133+ chunk = this.chunk(id, Math.floor(position / 65536));
134+ const offset = position % 65536,
135+ count = Math.min(length - copied, chunk.length - offset);
136+ if (count <= 0) throw Error("truncated Git object chunk");
137+ result.set(chunk.subarray(offset, offset + count), copied);
138+ copied += count;
139+ }
140+ return result;
141+ },
142+ };
143+ }
144+ /** Remove obsolete delta instructions without refunding their decoded-work budget. */
145+ remove(id: string): void {
146+ this.active();
147+ this.storage.sql.exec("DELETE FROM git_object_bytes WHERE scope = ? AND id = ?", this.scope, id);
148+ this.storage.sql.exec("DELETE FROM git_object_meta WHERE scope = ? AND id = ?", this.scope, id);
149+ }
150+ dispose(): void {
151+ this.closed = true;
152+ this.storage.transactionSync(() => {
153+ this.storage.sql.exec("DELETE FROM git_object_bytes WHERE scope = ?", this.scope);
154+ this.storage.sql.exec("DELETE FROM git_object_meta WHERE scope = ?", this.scope);
155+ this.storage.sql.exec("DELETE FROM git_object_budget WHERE scope IN (?, ?)", this.scope, `${this.scope}:raw`);
156+ });
157+ }
158+}
MCP
compare on this site's /mcp
CLI
abso diff 55 318 server/src/git-object-bytes.ts --json
JSON
/christopher/abso-core/compare/55..318?file=server%2Fsrc%2Fgit-object-bytes.ts&format=json