Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
train: keep message-db.ts at 1,000 lines without a ratchet entry
milind-sonid10bf8b3b34e
diff --git a/scripts/testing/file-size-ratchet.test.ts b/scripts/testing/file-size-ratchet.test.tsindex 5614135014..10a4ea7392 100644--- a/scripts/testing/file-size-ratchet.test.ts+++ b/scripts/testing/file-size-ratchet.test.ts@@ -21,7 +21,7 @@ const LINES: Record<string, number> = {"electron/main.mjs": 3124,"server/store.ts": 2254,"src/components/Sidebar.tsx": 1654,- "server/drivers/claude.ts": 1960, // the turn loop holds the MCP mounts, session reuse and recovery together+ "server/drivers/claude.ts": 1954,"server/drivers/acp/core.ts": 1538,"server/drivers/codex.ts": 1577,"server/routines.ts": 1553,@@ -43,7 +43,6 @@ const LINES: Record<string, number> = {"server/cli.ts": 1052,"src/components/AttachmentPreview.tsx": 1017,"server/meetings/meetbot.ts": 1047,- "server/message-db.ts": 1009, // the attachment-name index hooks (issue #6255): one line per message write, delete and reopen path"electron/data-dir-lease.node-test.mjs": 846, // the data-folder lease's own tests: many platform cases (Windows probes in data-dir-lease-windows.node-test.mjs)};diff --git a/server/message-db-attachment-names.ts b/server/message-db-attachment-names.tsindex 38a79c5800..640e4f98cc 100644--- a/server/message-db-attachment-names.ts+++ b/server/message-db-attachment-names.ts@@ -1,6 +1,6 @@// Which conversations' messages name a stored upload (message-db.ts threadsReferencing). Asking SQLite once per name// (instr over every row's JSON) is a full read of messages.db per name, on the main thread: a 740k-row install held its-// server for 79 s per refused hand-off file (issue #6255). This in-memory index answers the question per name without+// server for 79 s per refused hand-off file. This in-memory index answers the question per name without// reading any row.//// saveFile/saveImage/saveAudio only ever store `${uuid}${extension}`, so every name these queries ask for is one shape:diff --git a/server/message-db.ts b/server/message-db.tsindex 8d65bbc5c1..f896d3633d 100644--- a/server/message-db.ts+++ b/server/message-db.ts@@ -9,8 +9,7 @@ import type { ResolvedSender, SteerQueueReason } from "../shared/wire.ts";import type { Message } from "./store.ts";import type { UsageTrigger } from "./usage-ledger.ts";import { BOT_TEXT_MESSAGE_INDEXES_SQL } from "./message-db-bot-text-indexes.ts";-import { ensureMemoryIndex } from "./message-db-memory-index.ts";-import { createAttachmentNameIndex, STORED_UPLOAD_NAME } from "./message-db-attachment-names.ts";+import { ensureMemoryIndex } from "./message-db-memory-index.ts"; import { createAttachmentNameIndex, STORED_UPLOAD_NAME } from "./message-db-attachment-names.ts";import { cjkSearchText, cjkTokenParts, ensureSearchIndex, ftsPhrase, hasCjk, withoutSearchMarks } from "./cjk-search.ts";import { attentionSelect } from "./message-db-attention.ts";import { MessageSearchWorker } from "./message-search-worker.ts"; import { messageWritten } from "./message-writes.ts";@@ -153,8 +152,7 @@ function db(): DatabaseSync {handle?.close();} catch {}statements = new Map();- handle = open();- attachmentNameIndex.reset();+ handle = open(); attachmentNameIndex.reset();handlePath = file;handleCheckedAt = Date.now();return handle;@@ -664,9 +662,7 @@ export function deleteThreads(threadIds: readonly string[], beforeCommit?: () =>];for (const id of threadIds) for (const statement of statements) statement.run(id);beforeCommit?.();- });- // After the commit: a rolled-back batch's rows stay, and the index must not forget them.- attachmentNameIndex.threadsDeleted(threadIds);+ }); attachmentNameIndex.threadsDeleted(threadIds); // after the commit: a rolled-back batch's rows stay, and stay indexed}/** Option-card rows whose card names one of `tools`, oldest first; the caller judges each card itself. */@@ -688,15 +684,10 @@ export function liveAppRows(limit = 100): Array<{ threadId: string; messageId: sreturn db().prepare("SELECT thread_id AS threadId, id AS messageId FROM messages WHERE kind = 'app' AND json_extract(json, '$.app.live') = 1 ORDER BY rowid DESC LIMIT ?").all(limit) as Array<{ threadId: string; messageId: string }>;}-/** The attachment-name index (message-db-attachment-names.ts), which threadsReferencing answers stored upload names- * from; exported so tests can see how often it built. */-export const attachmentNameIndex = createAttachmentNameIndex({ prepare: (sql) => prepared(sql) });-+export const attachmentNameIndex = createAttachmentNameIndex({ prepare: (sql) => prepared(sql) }); // threadsReferencing's stored upload names; exported so tests can count buildsexport function threadsReferencing(fragment: string): string[] {- if (!fragment) return [];- if (STORED_UPLOAD_NAME.test(fragment)) return attachmentNameIndex.threadsFor(fragment);- const rows = db().prepare("SELECT DISTINCT thread_id FROM messages WHERE instr(json, ?) > 0").all(fragment) as Array<{ thread_id: string }>;- return rows.map((row) => row.thread_id);+ if (!fragment) return []; if (STORED_UPLOAD_NAME.test(fragment)) return attachmentNameIndex.threadsFor(fragment);+ return (db().prepare("SELECT DISTINCT thread_id FROM messages WHERE instr(json, ?) > 0").all(fragment) as Array<{ thread_id: string }>).map((row) => row.thread_id);}export function searchMessages(query: string, limit = 40, threadId?: string): SearchHit[] {