Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Train fix (#5969): cap the parsed-assignment memo by the row text it holds, least recently read first

milind-soni79f95322a974
diff --git a/server/runtime/store.ts b/server/runtime/store.ts
index 51b7eb3695..7198a577ca 100644
--- a/server/runtime/store.ts
+++ b/server/runtime/store.ts
@@ -163,12 +163,20 @@ 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;
+/** Rows hold briefs and results of any length, and every update leaves its old text behind, so the memo is also capped
+ * by the row text it holds, least recently read first. */
+const ASSIGNMENT_MEMO_CHARS = 8_000_000;
const assignmentMemo = new Map<string, Assignment>();
+let assignmentMemoChars = 0;
function readAssignment(row: Row): Assignment {
const memoKey = String(row.json);
const memo = assignmentMemo.get(memoKey);
- if (memo !== undefined) return structuredClone(memo);
+ if (memo !== undefined) {
+ assignmentMemo.delete(memoKey);
+ assignmentMemo.set(memoKey, memo);
+ return structuredClone(memo);
+ }
let parsed: unknown;
try {
parsed = JSON.parse(memoKey);
@@ -177,11 +185,14 @@ function readAssignment(row: Row): Assignment {
}
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);
- }
+ if (memoKey.length > ASSIGNMENT_MEMO_CHARS / 64) return result.data;
assignmentMemo.set(memoKey, structuredClone(result.data));
+ assignmentMemoChars += memoKey.length;
+ for (const oldest of assignmentMemo.keys()) {
+ if (assignmentMemo.size <= ASSIGNMENT_MEMO_LIMIT && assignmentMemoChars <= ASSIGNMENT_MEMO_CHARS) break;
+ assignmentMemo.delete(oldest);
+ assignmentMemoChars -= oldest.length;
+ }
return result.data;
}