Versions 100 to 318
new file, 884 lines
1+// The engine's Project (worker/project/project.ts) behind the server's calls, with its append-only log kept in the
2+// Durable Object's SQLite. An entry longer than one row allows (init holds every starting file; SQLite rows stop at
3+// 2 MB) is split across rows and joined on load. Ported from the platform's worker/src/engine-project.ts; the
5+
9+import { type Entry, type ProjectStore as LogStore, MemoryStore, Project } from "../../worker/project/project.ts";
10+import type { EditBody, ProjectCalls, ProjectHistory } from "../../worker/project/project-api.ts";
13+
14+/** The parts of a Durable Object's storage the log and blob store use, declared structurally so tests can stand in. */
19+
23+
24+/** An entry's JSON in rows of at most LOG_PART_CHARACTERS characters, never cutting an emoji's two halves apart. */
37+
53+
65+
66+/** The version of shared an entry makes, or undefined (abso issue 78: the access check reads it without loading). */
73+
76+ if (entry.type === "land" || entry.type === "drop") return { agent: entry.agent, time: entry.time };
77+ if (entry.type === "publish" && (entry.space ?? "") === "") return { agent: entry.agent, time: entry.time };
80+
81+/** An entry's claim keys and deletes (#46): with entries and lines, what the load guard estimates memory by. */
85+ (entry as { atom?: Partial<Record<"writes" | "reads" | "holds" | "deletes", unknown[]>> }).atom,
88+ if (a) n += (a.writes?.length ?? 0) + (a.reads?.length ?? 0) + (a.holds?.length ?? 0) + (a.deletes?.length ?? 0);
92+
96+
100+ "CREATE TABLE IF NOT EXISTS log (seq INTEGER, part INTEGER, text TEXT NOT NULL, PRIMARY KEY (seq, part))",
103+ "CREATE TABLE IF NOT EXISTS shapes (space TEXT NOT NULL, version INTEGER NOT NULL, value TEXT NOT NULL, PRIMARY KEY (space, version))",
105+ storage.sql.exec("CREATE TABLE IF NOT EXISTS counts (name TEXT PRIMARY KEY, value INTEGER NOT NULL)");
106+ storage.sql.exec("CREATE TABLE IF NOT EXISTS marks (name TEXT PRIMARY KEY, value TEXT NOT NULL)");
107+ const rows = storage.sql.exec("SELECT COALESCE(MAX(seq), -1) AS last FROM log").toArray() as { last: number }[];
110+
115+
121+ "SELECT COUNT(*) AS parts, COALESCE(SUM(LENGTH(CAST(text AS BLOB))), 0) AS bytes FROM log WHERE seq = ?",
125+ if (!size || size.parts < 1 || size.parts > 256 || size.bytes < 1 || !Number.isSafeInteger(size.bytes))
133+
145+ "SELECT COUNT(*) AS parts, COALESCE(SUM(LENGTH(CAST(text AS BLOB))), 0) AS bytes FROM log WHERE seq = ?",
151+ const rows = this.storage.sql.exec("SELECT part, text FROM log WHERE seq = ? ORDER BY part", seq).toArray() as {
159+
166+ .exec("SELECT part, text FROM log WHERE seq = ? AND part >= ? ORDER BY part LIMIT 1", seq, expected)
179+
182+ const rows = this.storage.sql.exec("SELECT COALESCE(MAX(seq), -1) AS last FROM log").toArray() as {
187+
190+ const rows = this.storage.sql.exec("SELECT value FROM marks WHERE name = ?", "generation").toArray() as {
194+ if (!Number.isSafeInteger(generation) || generation < 0) throw new Error("canonical log generation is invalid");
197+
205+ const rows = this.storage.sql.exec("SELECT value FROM counts WHERE name = 'lines'").toArray() as {
210+
212+ * The newest version of shared, without loading the project (abso issue 78: the website's access check asked
213+ * every project's engine for it). Kept with each append; a log from before the count is read once by streaming.
216+ const rows = this.storage.sql.exec("SELECT value FROM counts WHERE name = 'shared'").toArray() as {
223+ this.storage.sql.exec("INSERT OR REPLACE INTO counts (name, value) VALUES ('shared', ?)", version);
226+
228+ * Who published shared's newest version, and when, without loading the project (abso issue 86).
232+ const rows = this.storage.sql.exec("SELECT value FROM marks WHERE name = ?", "last").toArray() as {
235+ if (rows[0] !== undefined) return JSON.parse(rows[0].value) as { agent: string; time: number };
242+ this.storage.sql.exec("INSERT OR REPLACE INTO marks (name, value) VALUES (?, ?)", "last", JSON.stringify(last));
245+
254+ const rows = this.storage.sql.exec("SELECT value FROM counts WHERE name = 'chars'").toArray() as {
259+ const summed = this.storage.sql.exec("SELECT COALESCE(SUM(LENGTH(text)), 0) AS chars FROM log").toArray()[0] as
263+ this.storage.sql.exec("INSERT OR REPLACE INTO counts (name, value) VALUES ('chars', ?)", chars);
266+
267+ /** Claim keys and deletes ever recorded (#46), kept like the line count; undefined before they were counted. */
269+ const rows = this.storage.sql.exec("SELECT value FROM counts WHERE name = 'keys'").toArray() as { value: number }[];
272+
274+ * The lines and keys ever recorded, counting a log from before either count without loading the project
275+ * (#46): one entry parsed at a time, so a log the load guard would refuse can still be measured. Kept once counted.
290+
294+ this.storage.sql.exec("INSERT OR REPLACE INTO counts (name, value) VALUES ('lines', ?)", lines);
298+
299+ /** Every stored entry, in order. Rows are read one at a time, so loading never holds the whole log's text at once (#43). */
313+
316+ const cursor = this.storage.sql.exec("SELECT seq, text FROM log WHERE seq >= ? ORDER BY seq, part", from) as {
319+ const rows = (cursor[Symbol.iterator] ? cursor : cursor.toArray()) as Iterable<{ seq: number; text: string }>;
330+
339+ .exec("SELECT seq, text FROM log WHERE seq >= ? AND seq < ? ORDER BY seq, part", from, from + limit)
364+
376+
393+
407+ sql.exec("SELECT value FROM shapes WHERE space = ? AND version = ?", space, version).toArray() as {
412+ void sql.exec("INSERT OR REPLACE INTO shapes (space, version, value) VALUES (?, ?, ?)", space, version, value),
415+
423+ "INSERT INTO counts (name, value) VALUES ('lines', ?) ON CONFLICT(name) DO UPDATE SET value = value + ?",
427+ // The newest version of shared: appended entries are the newest, so the last one that makes a version wins.
440+ // Keys are only added to once counted: a log from before the key count is counted whole first (countSizes).
442+ if (keys > 0) this.storage.sql.exec("UPDATE counts SET value = value + ? WHERE name = 'keys'", keys);
448+ this.storage.sql.exec("INSERT INTO log (seq, part, text) VALUES (?, ?, ?)", next, part++, text);
452+ // Chars are only added to once counted: a log from before the count is summed whole first (logSize).
453+ if (chars > 0) this.storage.sql.exec("UPDATE counts SET value = value + ? WHERE name = 'chars'", chars);
464+
467+ blame(file: string, from?: number, to?: number, version?: number, draft?: string, agent?: string) {
468+ return blame(this.project, file, from, to, version, { ...(draft ? { draft } : {}), ...(agent ? { agent } : {}) });
471+ return folderBlame(this.project, folder, version, { ...(draft ? { draft } : {}), ...(agent ? { agent } : {}) });
473+ tree(folder: string, version?: number, draft?: string | null, agent?: string, authors?: boolean) {
482+ return this.project.pathLife(path, { ...(version !== undefined ? { version } : {}), draft: draft ?? null });
484+ follow(path: string, version: number, from: number, to: number, at?: number, draft?: string | null) {
485+ return this.project.follow(path, version, from, to, { ...(at !== undefined ? { at } : {}), draft: draft ?? null });
510+ return this.project.refusals({ ...(agent ? { agent } : {}), ...(limit !== undefined ? { limit } : {}) });
512+
513+ log(draft: string | null, limit?: number, path?: string, agent?: string, before?: number, follow?: boolean) {
528+ return summary ? this.project.compareSummary(from, to, where) : this.project.compare(from, to, where);
560+ pluck(agent: string, draft: string, versions: number[], message: string, withDependencies?: boolean) {
566+ return this.project.drop(agent, versions, message, Date.now(), { withDependents: withDependents === true });
571+
575+ /** A seat's earlier chat hears that another chat used the seat (277, groups); as abso's worker has it. */
579+ starting(agent: string, text: string | null, files?: string[], options: { issue?: unknown; done?: unknown } = {}) {
588+
590+
591+ /** Reads stored entries by log position, for the feed and replay (#41); absent, those answer 501. */
594+ private readonly historical: (<T>(at: number, operation: (project: Project) => T) => T) | undefined;
595+
607+
610+ if (!this.read) throw Object.assign(new Error("the feed needs the log"), { code: "not-implemented" });
631+
656+
660+
664+
668+
672+
676+
680+
684+
688+
695+ .map((a) => ({ agent: a.agent, draft: a.draft, base: a.base, draftChanges: a.draftChanges, files: a.files })),
698+ /** The last copy replay loaded: scrubbing forward from it applies only the entries after it. */
700+
703+ if (!this.read) throw Object.assign(new Error("replay needs the log"), { code: "not-implemented" });
742+
751+
752+ /** hasBlob says whether the project holds a blob's bytes (#27); without it, every blob is refused. */
753+ /** whole stores and reads files kept whole (#46); without it every text file is tracked by line. */
772+
839+ /** Capture metadata now; each text read reopens the captured version in a fresh host scope. */
853+ return { version, paths, modes, blobs, ...(by ? { last: { agent: by.agent, message: by.message } } : {}) };
859+ * Shared's newest version. Its files and modes are laid out on first read: every call reads shared().version
860+ * before and after its op, and laying out the files reads each file's text from the log, about 0.5 s on abso
- MCP
- compare on this site's /mcp
- CLI
- abso diff 100 318 server/src/engine-project.ts --json
- JSON
- /christopher/abso-core/compare/100..318?file=server%2Fsrc%2Fengine-project.ts&format=json