Versions 149 to 150
new file, 724 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+
8+import { type Entry, type ProjectStore as LogStore, MemoryStore, Project } from "../../worker/project/project.ts";
9+import type { EditBody, ProjectCalls, ProjectHistory } from "../../worker/project/project-api.ts";
11+
12+/** The parts of a Durable Object's storage the log and blob store use, declared structurally so tests can stand in. */
17+
21+
22+/** An entry's JSON in rows of at most PART characters, never cutting an emoji's two halves apart. */
35+
47+
48+/** The version of shared an entry makes, or undefined (abso issue 78: the access check reads it without loading). */
55+
58+ if (entry.type === "land" || entry.type === "drop") return { agent: entry.agent, time: entry.time };
59+ if (entry.type === "publish" && (entry.space ?? "") === "") return { agent: entry.agent, time: entry.time };
62+
63+/** An entry's claim keys and deletes (#46): with entries and lines, what the load guard estimates memory by. */
67+ (entry as { atom?: Partial<Record<"writes" | "reads" | "holds" | "deletes", unknown[]>> }).atom,
70+ if (a) n += (a.writes?.length ?? 0) + (a.reads?.length ?? 0) + (a.holds?.length ?? 0) + (a.deletes?.length ?? 0);
74+
78+
82+ "CREATE TABLE IF NOT EXISTS log (seq INTEGER, part INTEGER, text TEXT NOT NULL, PRIMARY KEY (seq, part))",
85+ "CREATE TABLE IF NOT EXISTS shapes (space TEXT NOT NULL, version INTEGER NOT NULL, value TEXT NOT NULL, PRIMARY KEY (space, version))",
87+ storage.sql.exec("CREATE TABLE IF NOT EXISTS counts (name TEXT PRIMARY KEY, value INTEGER NOT NULL)");
88+ storage.sql.exec("CREATE TABLE IF NOT EXISTS marks (name TEXT PRIMARY KEY, value TEXT NOT NULL)");
89+ const rows = storage.sql.exec("SELECT COALESCE(MAX(seq), -1) AS last FROM log").toArray() as { last: number }[];
92+
97+
100+ const rows = this.storage.sql.exec("SELECT value FROM marks WHERE name = ?", "generation").toArray() as {
104+ if (!Number.isSafeInteger(generation) || generation < 0) throw new Error("canonical log generation is invalid");
107+
115+ const rows = this.storage.sql.exec("SELECT value FROM counts WHERE name = 'lines'").toArray() as {
120+
122+ * The newest version of shared, without loading the project (abso issue 78: the website's access check asked
123+ * every project's engine for it). Kept with each append; a log from before the count is read once by streaming.
126+ const rows = this.storage.sql.exec("SELECT value FROM counts WHERE name = 'shared'").toArray() as {
133+ this.storage.sql.exec("INSERT OR REPLACE INTO counts (name, value) VALUES ('shared', ?)", version);
136+
138+ * Who published shared's newest version, and when, without loading the project (abso issue 86).
142+ const rows = this.storage.sql.exec("SELECT value FROM marks WHERE name = ?", "last").toArray() as {
145+ if (rows[0] !== undefined) return JSON.parse(rows[0].value) as { agent: string; time: number };
152+ this.storage.sql.exec("INSERT OR REPLACE INTO marks (name, value) VALUES (?, ?)", "last", JSON.stringify(last));
155+
164+ const rows = this.storage.sql.exec("SELECT value FROM counts WHERE name = 'chars'").toArray() as {
169+ const summed = this.storage.sql.exec("SELECT COALESCE(SUM(LENGTH(text)), 0) AS chars FROM log").toArray()[0] as
173+ this.storage.sql.exec("INSERT OR REPLACE INTO counts (name, value) VALUES ('chars', ?)", chars);
176+
177+ /** Claim keys and deletes ever recorded (#46), kept like the line count; undefined before they were counted. */
179+ const rows = this.storage.sql.exec("SELECT value FROM counts WHERE name = 'keys'").toArray() as { value: number }[];
182+
184+ * The lines and keys ever recorded, counting a log from before either count without loading the project
185+ * (#46): one entry parsed at a time, so a log the load guard would refuse can still be measured. Kept once counted.
200+
204+ this.storage.sql.exec("INSERT OR REPLACE INTO counts (name, value) VALUES ('lines', ?)", lines);
208+
209+ /** Every stored entry, in order. Rows are read one at a time, so loading never holds the whole log's text at once (#43). */
223+
226+ const cursor = this.storage.sql.exec("SELECT seq, text FROM log WHERE seq >= ? ORDER BY seq, part", from) as {
229+ const rows = (cursor[Symbol.iterator] ? cursor : cursor.toArray()) as Iterable<{ seq: number; text: string }>;
240+
249+ .exec("SELECT seq, text FROM log WHERE seq >= ? AND seq < ? ORDER BY seq, part", from, from + limit)
274+
286+
303+
317+ sql.exec("SELECT value FROM shapes WHERE space = ? AND version = ?", space, version).toArray() as {
322+ void sql.exec("INSERT OR REPLACE INTO shapes (space, version, value) VALUES (?, ?, ?)", space, version, value),
325+
333+ "INSERT INTO counts (name, value) VALUES ('lines', ?) ON CONFLICT(name) DO UPDATE SET value = value + ?",
337+ // The newest version of shared: appended entries are the newest, so the last one that makes a version wins.
350+ // Keys are only added to once counted: a log from before the key count is counted whole first (countSizes).
352+ if (keys > 0) this.storage.sql.exec("UPDATE counts SET value = value + ? WHERE name = 'keys'", keys);
362+ // Chars are only added to once counted: a log from before the count is summed whole first (logSize).
363+ if (chars > 0) this.storage.sql.exec("UPDATE counts SET value = value + ? WHERE name = 'chars'", chars);
374+
377+ blame(file: string, from?: number, to?: number, version?: number, draft?: string, agent?: string) {
378+ return blame(this.project, file, from, to, version, { ...(draft ? { draft } : {}), ...(agent ? { agent } : {}) });
381+ return folderBlame(this.project, folder, version, { ...(draft ? { draft } : {}), ...(agent ? { agent } : {}) });
383+ tree(folder: string, version?: number, draft?: string | null, agent?: string, authors?: boolean) {
392+ return this.project.pathLife(path, { ...(version !== undefined ? { version } : {}), draft: draft ?? null });
394+ follow(path: string, version: number, from: number, to: number, at?: number, draft?: string | null) {
395+ return this.project.follow(path, version, from, to, { ...(at !== undefined ? { at } : {}), draft: draft ?? null });
410+ return why(this.project, file, line, { ...(draft ? { draft } : {}), ...(agent ? { agent } : {}) });
416+ return this.project.refusals({ ...(agent ? { agent } : {}), ...(limit !== undefined ? { limit } : {}) });
418+
419+ log(draft: string | null, limit?: number, path?: string, agent?: string, before?: number, follow?: boolean) {
462+ pluck(agent: string, draft: string, versions: number[], message: string, withDependencies?: boolean) {
468+ return this.project.drop(agent, versions, message, Date.now(), { withDependents: withDependents === true });
473+
477+ starting(agent: string, text: string | null, files?: string[], options: { issue?: unknown; done?: unknown } = {}) {
486+
488+
489+ /** Reads stored entries by log position, for the feed and replay (#41); absent, those answer 501. */
492+
493+ constructor(project: Project, read?: (from: number, limit: number) => Entry[], store?: SqliteLog) {
498+
501+ if (!this.read) throw Object.assign(new Error("the feed needs the log"), { code: "not-implemented" });
513+
538+
542+
546+
550+
554+
558+
562+
566+
570+
577+ .map((a) => ({ agent: a.agent, draft: a.draft, base: a.base, draftChanges: a.draftChanges, files: a.files })),
580+ /** The last copy replay loaded: scrubbing forward from it applies only the entries after it. */
582+
585+ if (!this.read) throw Object.assign(new Error("replay needs the log"), { code: "not-implemented" });
614+
623+
624+ /** hasBlob says whether the project holds a blob's bytes (#27); without it, every blob is refused. */
625+ /** whole stores and reads files kept whole (#46); without it every text file is tracked by line. */
644+
699+ * Shared's newest version. Its files and modes are laid out on first read: every call reads shared().version
700+ * 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 149 150 server/src/engine-project.ts --json
- JSON
- /christopher/abso-core/compare/149..150?file=server%2Fsrc%2Fengine-project.ts&format=json