Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
perf: remember a parsed assignment by its JSON text
mattmillartech3ac0aa8ee9e1
diff --git a/server/runtime/store.test.ts b/server/runtime/store.test.tsindex b36b9b5c82..49f989b377 100644--- a/server/runtime/store.test.ts+++ b/server/runtime/store.test.ts@@ -53,6 +53,17 @@ describe("runtime store", () => {expect(() => store.update(assignment("ghost"))).toThrow(/does not exist/);});+ it("hands each reader its own copy of a row, and a changed row reads as its new text", () => {+ const store = open();+ const root = assignment("root", { status: "running" });+ store.insert(root, { kind: "enqueued", at: 1_000 });+ store.get("root")!.status = "failed";+ expect(store.get("root")?.status).toBe("running");+ root.status = "completed"; root.outcome = "done"; root.finishedAt = 5_000;+ store.update(root);+ expect(store.get("root")).toEqual(root);+ });+it("filters all() by tree, liveness and requester thread", () => {const store = open();const a = assignment("a", { status: "completed", outcome: "done", finishedAt: 5_000 });diff --git a/server/runtime/store.ts b/server/runtime/store.tsindex 46cfae0e07..51b7eb3695 100644--- a/server/runtime/store.ts+++ b/server/runtime/store.ts@@ -160,15 +160,28 @@ const SCHEMA = `type Row = Record<string, unknown>;+/** A parsed row is remembered by its JSON text: the parse is a pure function of that text, so a changed+ * row (different text) can never hit a stale entry. Callers get a clone, so none can alter another's copy. */+const ASSIGNMENT_MEMO_LIMIT = 8_192;+const assignmentMemo = new Map<string, Assignment>();+function readAssignment(row: Row): Assignment {+ const memoKey = String(row.json);+ const memo = assignmentMemo.get(memoKey);+ if (memo !== undefined) return structuredClone(memo);let parsed: unknown;try {- parsed = JSON.parse(String(row.json));+ parsed = JSON.parse(memoKey);} catch (error) {throw new Error(`runtime.db: assignment ${String(row.id)} holds invalid JSON (${String(error)})`);}const result = assignmentSchema.safeParse(parsed);if (!result.success) throw new Error(`runtime.db: assignment ${String(row.id)} does not match the record schema: ${result.error.message}`);+ if (assignmentMemo.size >= ASSIGNMENT_MEMO_LIMIT) {+ const oldest = assignmentMemo.keys().next().value;+ if (oldest !== undefined) assignmentMemo.delete(oldest);+ }+ assignmentMemo.set(memoKey, structuredClone(result.data));return result.data;}