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

+133 −0 in 1 place
new file, 133 lines
1+import type { SqliteStorage } from "./engine-project.ts";
2+import { applyGitDeltaChunks } from "./git-delta-stream.ts";
3+import { GitObjectBytes, type StoredGitBytes } from "./git-object-bytes.ts";
4+
5+/** Scratch index and decoded objects for one pack. No in-memory object map. */
6+export class GitPackResolver {
7+ readonly scope = crypto.randomUUID();
8+ readonly objects: GitObjectBytes;
9+ private readonly storage: SqliteStorage;
10+ private readonly maxObjectBytes: number;
11+ private closed = false;
12+ constructor(storage: SqliteStorage, maxObjectBytes: number, maxDecodedBytes = maxObjectBytes) {
13+ if (!Number.isSafeInteger(maxObjectBytes) || maxObjectBytes < 0) throw Error("invalid object limit");
14+ this.storage = storage;
15+ this.maxObjectBytes = maxObjectBytes;
16+ this.objects = new GitObjectBytes(storage, maxDecodedBytes);
17+ storage.sql.exec(
18+ "CREATE TABLE IF NOT EXISTS git_pack_resolve (scope TEXT, offset INTEGER, raw TEXT, base_offset INTEGER, base_oid TEXT, result TEXT, oid TEXT, depth INTEGER, PRIMARY KEY(scope,offset))",
19+ );
20+ storage.sql.exec("CREATE INDEX IF NOT EXISTS git_pack_resolve_oid ON git_pack_resolve(scope,oid)");
21+ storage.sql.exec(
22+ "CREATE INDEX IF NOT EXISTS git_pack_unresolved ON git_pack_resolve(scope,offset) WHERE result IS NULL",
23+ );
24+ }
25+ private active(): void {
26+ if (this.closed) throw Error("pack resolver is closed");
27+ }
28+ add(offset: number, type: string, data: Uint8Array, base?: number | string): void {
29+ this.addChunks(offset, type, (write) => write(data), base);
30+ }
31+ /** The inflater writes bounded output directly into the transactional object store. */
32+ addChunks(
33+ offset: number,
34+ type: string,
35+ produce: (write: (bytes: Uint8Array) => void) => void,
36+ base?: number | string,
37+ ): void {
38+ this.active();
39+ if (!Number.isSafeInteger(offset) || offset < 12) throw Error("invalid pack offset");
40+ if (type === "delta") {
41+ if (
42+ typeof base === "number"
43+ ? !Number.isSafeInteger(base) || base < 12 || base >= offset
44+ : typeof base !== "string" || !/^[0-9a-f]{40}$/.test(base)
45+ )
46+ throw Error("invalid delta base reference");
47+ } else if (base !== undefined) throw Error("ordinary object has delta base");
48+ this.storage.transactionSync(() => {
49+ const stored = this.objects.put(type === "delta" ? "blob" : type, produce, this.maxObjectBytes, type === "delta");
50+ this.storage.sql.exec(
51+ "INSERT INTO git_pack_resolve VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
52+ this.scope,
53+ offset,
54+ stored.id,
55+ typeof base === "number" ? base : null,
56+ typeof base === "string" ? base : null,
57+ type === "delta" ? null : stored.id,
58+ type === "delta" ? null : stored.oid,
59+ type === "delta" ? null : 0,
60+ );
61+ });
62+ }
63+ resolve(): void {
64+ this.active();
65+ this.storage.transactionSync(() => {
66+ for (;;) {
67+ const rows = this.storage.sql
68+ .exec(
69+ `SELECT p.offset, p.raw, COALESCE(b.result, r.result) AS base, COALESCE(b.depth, r.depth) AS depth
70+FROM git_pack_resolve p
71+LEFT JOIN git_pack_resolve b ON b.scope = p.scope AND b.offset = p.base_offset
72+LEFT JOIN git_pack_resolve r ON r.scope = p.scope AND r.oid = p.base_oid
73+WHERE p.scope = ? AND p.result IS NULL AND (b.result IS NOT NULL OR r.result IS NOT NULL)
74+ORDER BY p.offset LIMIT 1`,
75+ this.scope,
76+ )
77+ .toArray() as {
78+ offset: number;
79+ raw: string;
80+ base: string;
81+ depth: number;
82+ }[];
83+ if (!rows.length) break;
84+ const row = rows[0]!;
85+ if (row.depth >= 64) throw Error("Git delta chain exceeds depth limit");
86+ const base = this.objects.get(row.base),
87+ delta = this.objects.get(row.raw);
88+ const result = this.objects.put(
89+ base.type,
90+ (write) => {
91+ applyGitDeltaChunks(delta, base, write, this.maxObjectBytes);
92+ },
93+ this.maxObjectBytes,
94+ );
95+ this.storage.sql.exec(
96+ "UPDATE git_pack_resolve SET result = ?, oid = ?, depth = ? WHERE scope = ? AND offset = ?",
97+ result.id,
98+ result.oid,
99+ row.depth + 1,
100+ this.scope,
101+ row.offset,
102+ );
103+ this.objects.remove(row.raw);
104+ }
105+ if (
106+ this.storage.sql
107+ .exec("SELECT offset FROM git_pack_resolve WHERE scope = ? AND result IS NULL LIMIT 1", this.scope)
108+ .toArray().length
109+ )
110+ throw Error("unresolved or cyclic Git delta base");
111+ });
112+ }
113+ get(oid: string): StoredGitBytes {
114+ const value = this.find(oid);
115+ if (!value) throw Error("missing packed Git object");
116+ return value;
117+ }
118+ find(oid: string): StoredGitBytes | undefined {
119+ this.active();
120+ const rows = this.storage.sql
121+ .exec("SELECT result FROM git_pack_resolve WHERE scope = ? AND oid = ? LIMIT 1", this.scope, oid)
122+ .toArray() as { result: string }[];
123+ if (!rows.length) return undefined;
124+ return this.objects.get(rows[0]!.result);
125+ }
126+ dispose(): void {
127+ this.closed = true;
128+ this.storage.transactionSync(() => {
129+ this.storage.sql.exec("DELETE FROM git_pack_resolve WHERE scope = ?", this.scope);
130+ this.objects.dispose();
131+ });
132+ }
133+}
MCP
compare on this site's /mcp
CLI
abso diff 32 318 server/src/git-pack-resolver.ts --json
JSON
/christopher/abso-core/compare/32..318?file=server%2Fsrc%2Fgit-pack-resolver.ts&format=json