An open-source, faster Devin alternative
TypeScript
Work index: keep an item file over 1 MB cut, and a row that cannot be written as that row's problem
Igor Zalutski via Computer1f32dd39e6f0
diff --git a/services/managed-agents/src/computer/work/index.ts b/services/managed-agents/src/computer/work/index.tsindex 018c8de4..8ac6b0d7 100644--- a/services/managed-agents/src/computer/work/index.ts+++ b/services/managed-agents/src/computer/work/index.ts@@ -108,7 +108,13 @@ const BIND_MAX = 90;/** Stored bodies re-parsed per step after PARSER_VERSION changes. */export const REPARSE_BATCH = 100;/** The tables' shape; another stored version drops them (the repository is the record). */-const SCHEMA_VERSION = 3;+const SCHEMA_VERSION = 4;+/**+ * Bytes of an item file kept in `items.body`. Durable Object SQLite refuses a string or a row over+ * 2 MB; a larger file is kept cut (`body_note` 'cut'), and a row that cannot be written is kept+ * without its body ('unstored'), so one file never stops the build or an advance.+ */+export const BODY_STORE_BYTES = 1_000_000;/** A commit touching more item files than this is a bulk edit: no item's own activity, no owner. */export const BULK_ITEMS = 5;/** An item whose own last change is this recent is active (with a session at work, in the Worker). */@@ -124,6 +130,7 @@ CREATE TABLE IF NOT EXISTS items (body TEXT NOT NULL,parsed TEXT NOT NULL,parser INTEGER NOT NULL,+ body_note TEXT,author_name TEXT, author_email TEXT, created_at TEXT,updated_at TEXT, closed_at TEXT, closed_by TEXT,updated_name TEXT, updated_email TEXT,@@ -751,7 +758,9 @@ export class WorkIndex {// Links (the sessions that worked on an item) stay.store.exec("DELETE FROM commits");store.exec("DELETE FROM revisions");- const old = new Map(store.exec<{ number: number; path: string; blob: string; body: string; parser: number } & Row>("SELECT number, path, blob, body, parser FROM items").map((r) => [r.number, r]));+ const old = new Map(+ store.exec<{ number: number; path: string; blob: string; body: string; body_note: string | null; parser: number } & Row>("SELECT number, path, blob, body, body_note, parser FROM items").map((r) => [r.number, r]),+ );for (const [number] of old) {if (!winners.has(number)) {store.exec("DELETE FROM items WHERE number = ?", number);@@ -763,8 +772,8 @@ export class WorkIndex {const prev = old.get(number);const keep = prev && prev.blob === w.blob && prev.parser > 0;store.exec(- `INSERT INTO items (number, path, stage, blob, body, parsed, parser) VALUES (?, ?, ?, ?, ?, ?, ?)- ON CONFLICT (number) DO UPDATE SET path = excluded.path, stage = excluded.stage, blob = excluded.blob, body = excluded.body, parsed = excluded.parsed,+ `INSERT INTO items (number, path, stage, blob, body, body_note, parsed, parser) VALUES (?, ?, ?, ?, ?, ?, ?, ?)+ ON CONFLICT (number) DO UPDATE SET path = excluded.path, stage = excluded.stage, blob = excluded.blob, body = excluded.body, body_note = excluded.body_note, parsed = excluded.parsed,parser = excluded.parser, author_name = NULL, author_email = NULL, created_at = NULL, updated_at = NULL, closed_at = NULL, closed_by = NULL,updated_name = NULL, updated_email = NULL, activity_at = NULL, activity_name = NULL, activity_email = NULL`,number,@@ -772,7 +781,8 @@ export class WorkIndex {ip.stage,w.blob,keep ? prev.body : "",- JSON.stringify(keep ? safeParse(ip, prev.body) : pendingParse(ip)),+ keep ? prev.body_note : null,+ JSON.stringify(keep ? safeParse(ip, prev.body, prev.body_note) : pendingParse(ip)),keep ? PARSER_VERSION : 0,);}@@ -817,11 +827,27 @@ export class WorkIndex {private async fill(gh: WorkGitHub, row: { number: number; path: string; blob: string }): Promise<void> {const ip = itemPath(row.path);if (!ip) return;- const body = await gh.blob(row.blob);- this.store.transaction(() => {- this.store.exec("UPDATE items SET body = ?, parsed = ?, parser = ? WHERE number = ? AND blob = ? AND path = ?", body, JSON.stringify(safeParse(ip, body)), PARSER_VERSION, row.number, row.blob, row.path);- this.bump();- });+ const kept = keepBody(await gh.blob(row.blob));+ const write = (k: KeptBody) =>+ this.store.transaction(() => {+ this.store.exec(+ "UPDATE items SET body = ?, body_note = ?, parsed = ?, parser = ? WHERE number = ? AND blob = ? AND path = ?",+ k.body,+ k.note,+ JSON.stringify(safeParse(ip, k.body, k.note)),+ PARSER_VERSION,+ row.number,+ row.blob,+ row.path,+ );+ this.bump();+ });+ try {+ write(kept);+ } catch {+ // The row's problem, not the build's: kept without its body, with a warning.+ write(UNSTORED);+ }}/** Bodies asked for before they were read: read them (joining a read already running), within the quota. */@@ -902,10 +928,11 @@ export class WorkIndex {else files.set(path, { number: itemPath(path)!.number, blob });}const winners = pickWinners([...files].map(([path, f]) => ({ path, ...f })));- const old = new Map<number, { number: number; path: string; stage: string; blob: string; body: string; parser: number } & Row>();+ type OldRow = { number: number; path: string; stage: string; blob: string; body: string; body_note: string | null; parser: number } & Row;+ const old = new Map<number, OldRow>();for (const part of chunks([...numbers])) {- for (const r of this.store.exec<{ number: number; path: string; stage: string; blob: string; body: string; parser: number } & Row>(- `SELECT number, path, stage, blob, body, parser FROM items WHERE number IN (${marks(part)})`,+ for (const r of this.store.exec<OldRow>(+ `SELECT number, path, stage, blob, body, body_note, parser FROM items WHERE number IN (${marks(part)})`,...part,)) {old.set(r.number, r);@@ -918,9 +945,9 @@ export class WorkIndex {return !prev || prev.blob !== w.blob || prev.parser === 0;});const readNow = needed.filter((w) => stageOf(w.path) === "open" || needed.length <= FILL_BATCH);- const bodies = new Map<string, string>();+ const bodies = new Map<string, KeptBody>();await parallel(readNow, async (w) => {- bodies.set(w.blob, await gh.blob(w.blob));+ bodies.set(w.blob, keepBody(await gh.blob(w.blob)));});// A range of one commit with every file listed: its details are compare's.@@ -941,29 +968,36 @@ export class WorkIndex {continue;}const ip = itemPath(w.path)!;- const body = bodies.get(w.blob) ?? (prev && prev.blob === w.blob && prev.parser > 0 ? prev.body : null);- const parsed = body !== null ? safeParse(ip, body) : pendingParse(ip);+ const kept: KeptBody | null = bodies.get(w.blob) ?? (prev && prev.blob === w.blob && prev.parser > 0 ? { body: prev.body, note: keptNote(prev.body_note) } : null);const changed = !prev || prev.blob !== w.blob || prev.path !== w.path;const moved = !prev || prev.stage !== ip.stage;// What changed is not dated yet; the walk dates it from the commit that did it.- store.exec(- `INSERT INTO items (number, path, stage, blob, body, parsed, parser) VALUES (?, ?, ?, ?, ?, ?, ?)- ON CONFLICT (number) DO UPDATE SET path = excluded.path, stage = excluded.stage, blob = excluded.blob, body = excluded.body,- parsed = excluded.parsed, parser = excluded.parser,- updated_at = CASE WHEN ? THEN NULL ELSE updated_at END,- closed_at = CASE WHEN ? THEN NULL ELSE closed_at END,- closed_by = CASE WHEN ? THEN NULL ELSE closed_by END`,- n,- w.path,- ip.stage,- w.blob,- body ?? "",- JSON.stringify(parsed),- body !== null ? PARSER_VERSION : 0,- changed ? 1 : 0,- moved ? 1 : 0,- moved ? 1 : 0,- );+ const write = (k: KeptBody | null) =>+ store.exec(+ `INSERT INTO items (number, path, stage, blob, body, body_note, parsed, parser) VALUES (?, ?, ?, ?, ?, ?, ?, ?)+ ON CONFLICT (number) DO UPDATE SET path = excluded.path, stage = excluded.stage, blob = excluded.blob, body = excluded.body,+ body_note = excluded.body_note, parsed = excluded.parsed, parser = excluded.parser,+ updated_at = CASE WHEN ? THEN NULL ELSE updated_at END,+ closed_at = CASE WHEN ? THEN NULL ELSE closed_at END,+ closed_by = CASE WHEN ? THEN NULL ELSE closed_by END`,+ n,+ w.path,+ ip.stage,+ w.blob,+ k?.body ?? "",+ k?.note ?? null,+ JSON.stringify(k ? safeParse(ip, k.body, k.note) : pendingParse(ip)),+ k ? PARSER_VERSION : 0,+ changed ? 1 : 0,+ moved ? 1 : 0,+ moved ? 1 : 0,+ );+ try {+ write(kept);+ } catch {+ // The row's problem, not the advance's: kept without its body, with a warning.+ write(UNSTORED);+ }}const walk = this.walkState();if (only) {@@ -1185,15 +1219,15 @@ export class WorkIndex {*/private reparseBatch(): void {if (!this.reparsing()) return;- const rows = this.store.exec<{ number: number; path: string; body: string } & Row>(- "SELECT number, path, body FROM items WHERE parser > 0 AND parser <> ? LIMIT ?",+ const rows = this.store.exec<{ number: number; path: string; body: string; body_note: string | null } & Row>(+ "SELECT number, path, body, body_note FROM items WHERE parser > 0 AND parser <> ? LIMIT ?",PARSER_VERSION,REPARSE_BATCH,);this.store.transaction(() => {for (const r of rows) {const ip = itemPath(r.path);- if (ip) this.store.exec("UPDATE items SET parsed = ?, parser = ? WHERE number = ?", JSON.stringify(safeParse(ip, r.body)), PARSER_VERSION, r.number);+ if (ip) this.store.exec("UPDATE items SET parsed = ?, parser = ? WHERE number = ?", JSON.stringify(safeParse(ip, r.body, r.body_note)), PARSER_VERSION, r.number);}if (rows.length < REPARSE_BATCH) this.setMeta("parser_version", PARSER_VERSION);this.bump();@@ -1300,13 +1334,39 @@ function pendingParse(ip: ItemPath): ParsedItem {return { title: slugTitle(ip.slug), stateLine: null, tags: [], owner: null, left: null, boxes: 0, pullRequests: [], warnings: [] };}+type BodyNote = "cut" | "unstored";+interface KeptBody {+ body: string;+ note: BodyNote | null;+}+const UNSTORED: KeptBody = { body: "", note: "unstored" };++function keptNote(note: string | null): BodyNote | null {+ return note === "cut" || note === "unstored" ? note : null;+}++/** A file's text as it is stored: at most BODY_STORE_BYTES of UTF-8, cut at a character boundary. */+export function keepBody(text: string): KeptBody {+ // A UTF-16 unit is at most 3 bytes: shorter text needs no encoding to know it fits.+ if (text.length * 3 <= BODY_STORE_BYTES) return { body: text, note: null };+ const bytes = new TextEncoder().encode(text);+ if (bytes.length <= BODY_STORE_BYTES) return { body: text, note: null };+ let end = BODY_STORE_BYTES;+ while (end > 0 && (bytes[end]! & 0xc0) === 0x80) end--;+ return { body: new TextDecoder().decode(bytes.subarray(0, end)), note: "cut" };+}+/** The parser never throws by design; if it does, the row keeps its slug and says so. */-function safeParse(ip: ItemPath, body: string): ParsedItem {+function safeParse(ip: ItemPath, body: string, note: string | null = null): ParsedItem {+ if (note === "unstored") return { ...pendingParse(ip), warnings: ["The file could not be stored; only its name is shown."] };+ let parsed: ParsedItem;try {- return parseItem(ip, body);+ parsed = parseItem(ip, body);} catch {return { ...pendingParse(ip), warnings: ["The file could not be read."] };}+ if (note === "cut") parsed.warnings = [`The file is larger than ${BODY_STORE_BYTES / 1_000_000} MB; only the start is read.`, ...parsed.warnings];+ return parsed;}/** Who closed: the commit's author, or null when a session made the commit (it says "closed in a session"). */diff --git a/services/managed-agents/test/fixtures/work-platform.ts b/services/managed-agents/test/fixtures/work-platform.tsindex dc73a81e..03659fdc 100644--- a/services/managed-agents/test/fixtures/work-platform.ts+++ b/services/managed-agents/test/fixtures/work-platform.ts@@ -3,6 +3,8 @@ import { sqlStorage } from "./computer-sql.js";/** Durable Object SQL binds at most 100 parameters per statement; the fake refuses more, as it does. */const BIND_LIMIT = 100;+/** Durable Object SQLite refuses a string or blob over 2 MB; the fake does too. */+const STRING_LIMIT = 2_000_000;/** The Work index's store on node:sqlite (the computer modules' fake), with real transactions. */export function indexStore(sql: SqlStorage = sqlStorage()): IndexStore & { sql: SqlStorage } {@@ -11,6 +13,7 @@ export function indexStore(sql: SqlStorage = sqlStorage()): IndexStore & { sql:sql,exec<T extends Record<string, StoreValue>>(query: string, ...params: StoreValue[]): T[] {if (params.length > BIND_LIMIT) throw new Error(`too many SQL variables: ${params.length}`);+ for (const p of params) if (typeof p === "string" && p.length > STRING_LIMIT / 3 && Buffer.byteLength(p) > STRING_LIMIT) throw new Error("string or blob too big: SQLITE_TOOBIG");return sql.exec(query, ...params).toArray() as T[];},transaction<T>(fn: () => T): T {diff --git a/services/managed-agents/test/work-index.test.ts b/services/managed-agents/test/work-index.test.tsindex 4bccba06..1d89de66 100644--- a/services/managed-agents/test/work-index.test.ts+++ b/services/managed-agents/test/work-index.test.ts@@ -16,10 +16,12 @@ import {UNREADABLE_RECHECK_MS,REPARSE_BATCH,BULK_ITEMS,+ BODY_STORE_BYTES,WorkIndex,} from "../src/computer/work/index.js";import type { IndexedItem, ItemsResult } from "../src/computer/work/index-contract.js";import { PARSER_VERSION } from "../src/computer/work/item.js";+import type { IndexStore } from "../src/computer/work/platform.js";import { sqlStorage } from "./fixtures/computer-sql.js";import { FakeRepo } from "./fixtures/fake-github-repo.js";import { FakeClock, indexStore } from "./fixtures/work-platform.js";@@ -29,7 +31,7 @@ import { FakeClock, indexStore } from "./fixtures/work-platform.js";const ORG = "acct_1";const by = { member: "m_1", name: "Ana", email: "[email protected]" };-function setup(repo: FakeRepo, options: { sql?: SqlStorage; mint?: Partial<TokenMinter>; clock?: FakeClock } = {}) {+function setup(repo: FakeRepo, options: { sql?: SqlStorage; mint?: Partial<TokenMinter>; clock?: FakeClock; store?: (store: IndexStore) => IndexStore } = {}) {const clock = options.clock ?? new FakeClock();const pending: Promise<unknown>[] = [];const mints: unknown[] = [];@@ -46,7 +48,7 @@ function setup(repo: FakeRepo, options: { sql?: SqlStorage; mint?: Partial<Token};const fetches: string[] = [];const index = new WorkIndex({- store: indexStore(sql),+ store: options.store ? options.store(indexStore(sql)) : indexStore(sql),clock,scheduler: {arm: async (at) => {@@ -989,3 +991,64 @@ describe("pull request polling", () => {expect((await open(s)).items[0]!.pullRequests).toEqual([{ url: gone, state: "open", title: "Back" }]);});});++describe("large and unstorable files", () => {+ it("keeps a file over the store's limit cut by bytes, with a warning, and the build finishes", async () => {+ // Three-byte characters: 3 MB of UTF-8, over Durable Object SQLite's 2 MB string limit.+ const huge = `# 2. Huge\n\nState of Huge.\n${"€".repeat(1_000_000)}\n`;+ const repo = new FakeRepo({ "work/1-board.md": item(1, "Board"), "work/2-huge.md": huge });+ const s = await connect(repo);+ const r = await open(s);+ expect(r.building).toBe(false);+ expect(r.head).toBe(repo.head);+ const two = byNumber(r).get(2)!;+ expect(two.title).toBe("Huge");+ expect(two.warnings).toContain("The file is larger than 1 MB; only the start is read.");+ const got = await s.index.item(ORG, 2);+ expect(got.ok && Buffer.byteLength(got.body!)).toBeLessThanOrEqual(BODY_STORE_BYTES);+ // Cut at a character boundary: no replacement character at the end.+ expect(got.ok && got.body!.endsWith("€")).toBe(true);++ // An advance to a still larger file is cut the same way.+ repo.commit({ "work/2-huge.md": `${huge}${"€".repeat(10)}` });+ s.tick(LOOK_AFTER_MS + 1);+ await open(s);+ await s.settle();+ const after = await open(s);+ expect(after.head).toBe(repo.head);+ expect(byNumber(after).get(2)!.warnings).toContain("The file is larger than 1 MB; only the start is read.");+ });++ it("makes a row that cannot be written that row's problem, in a build and in an advance", async () => {+ const repo = new FakeRepo({ "work/1-board.md": item(1, "Board"), "work/2-bad.md": item(2, "Bad") });+ const refuse = (store: IndexStore): IndexStore => ({+ ...store,+ exec(query, ...params) {+ if (params.some((p) => typeof p === "string" && p.includes("State of Bad"))) throw new Error("SQLITE_TOOBIG");+ return store.exec(query, ...params);+ },+ });+ const s = setup(repo, { store: refuse });+ await s.index.setRepository(ORG, { installationId: "inst_1", owner: "acme", name: "knowledge" }, by);+ await s.settle();+ await s.drain();+ const r = await open(s);+ expect(r.building).toBe(false);+ expect(r.items.map((i) => [i.number, i.title])).toEqual([+ [1, "Board"],+ [2, "bad"],+ ]);+ expect(byNumber(r).get(2)!.warnings).toEqual(["The file could not be stored; only its name is shown."]);+ expect(byNumber(r).get(2)!.parsed).toBe(true);++ repo.commit({ "work/1-board.md": item(1, "Board 2"), "work/2-bad.md": item(2, "Bad", "more") });+ s.tick(LOOK_AFTER_MS + 1);+ await open(s);+ await s.settle();+ const after = await open(s);+ expect(after.head).toBe(repo.head);+ expect(after.problem).toBeNull();+ expect(byNumber(after).get(1)!.title).toBe("Board 2");+ expect(byNumber(after).get(2)!.warnings).toEqual(["The file could not be stored; only its name is shown."]);+ });+});