Skip to content

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

fix: persist automatic query result names before use

Afloateea76851473c
diff --git a/server/data/engine.test.ts b/server/data/engine.test.ts
index 36297b6feb..2e95c7550c 100644
--- a/server/data/engine.test.ts
+++ b/server/data/engine.test.ts
@@ -10,6 +10,7 @@ import { afterAll, describe, expect, it, vi } from "vitest";
import { DATA_LIMITS, DATA_RESULTS_SCHEMA } from "../../shared/data-surface.ts";
import { CLOSE_DRAIN_MS, EXTENSION_INSTALL_TIMEOUT_MS, Lane, closeNames, createDataEngine, dataEngine, drainLanes, duckdbDirectory, instanceSettings, levenshtein, likePattern, loadExtensions, type DataEngineOptions } from "./engine.ts";
import { DataFailure, type BotDatabase, type DataEngine } from "./types.ts";
+import { DataSheetStore } from "./sheet.ts";
const root = mkdtempSync(join(tmpdir(), "omb-data-engine-"));
const outOfTree = join(root, "duckdb-resources");
@@ -279,6 +280,17 @@ describe("a bot's database", () => {
expect((await failure(db.run(`SELECT * FROM ${DATA_RESULTS_SCHEMA}.gone`, bot))).error.code).toBe("table_not_found");
});
+ it("keeps an earlier query result when the sheet reopens before the next query", async () => {
+ const database = await engine().forBot("query-names");
+ const options = { botId: "query-names", dir: join(root, "bots", "query-names") };
+ const first = new DataSheetStore(options).nextResultName();
+ await database.materialise("SELECT 11 AS value", first, bot);
+ const second = new DataSheetStore(options).nextResultName();
+ await database.materialise("SELECT 22 AS value", second, bot);
+ expect((await database.run(`SELECT * FROM ${DATA_RESULTS_SCHEMA}.${first}`, panel)).rows).toEqual([[11]]);
+ expect((await database.run(`SELECT * FROM ${DATA_RESULTS_SCHEMA}.${second}`, panel)).rows).toEqual([[22]]);
+ });
+
it("drops a result on the panel's connection without waiting behind the bot's statement", async () => {
await ready;
await db.materialise("SELECT 1 AS one", "aside", bot);
diff --git a/server/data/sheet.test.ts b/server/data/sheet.test.ts
index a8b647d66a..56437ff98e 100644
--- a/server/data/sheet.test.ts
+++ b/server/data/sheet.test.ts
@@ -68,6 +68,27 @@ describe("DataSheetStore", () => {
expect(next.id).toBe("c_3");
});
+ it("persists query-only allocations without changing or broadcasting the visible sheet", () => {
+ const dir = freshDir();
+ const { sheet, frames } = store(dir);
+ const before = sheet.sheet();
+ expect(sheet.nextResultName()).toBe("q_1");
+ expect(sheet.nextResultName()).toBe("q_2");
+ expect(sheet.sheet()).toEqual(before);
+ expect(frames).toEqual([]);
+ expect(store(dir).sheet.nextResultName()).toBe("q_3");
+ });
+
+ it("keeps result allocations made after the last visible card change", async () => {
+ const dir = freshDir();
+ const { sheet } = store(dir);
+ await sheet.addCard({ kind: "text", title: "Note", text: "hi", by: "person", status: "ready" });
+ expect(sheet.nextResultName()).toBe("q_2");
+ const reopened = store(dir).sheet;
+ expect(reopened.nextResultName()).toBe("q_3");
+ expect((await reopened.addCard({ kind: "text", title: "Next", by: "person" })).id).toBe("c_4");
+ });
+
it("prunes the oldest unpinned card first and drops its result table", async () => {
const dir = freshDir();
const { sheet, dropped } = store(dir, { cardsMax: 3 });
diff --git a/server/data/sheet.ts b/server/data/sheet.ts
index 8aaf342810..ef8457248e 100644
--- a/server/data/sheet.ts
+++ b/server/data/sheet.ts
@@ -84,11 +84,15 @@ export class DataSheetStore {
private save(): void {
const stored = this.load();
stored.updatedAt = this.now();
- mkdirSync(this.dir, { recursive: true, mode: 0o700 });
- writeFileAtomic(join(this.dir, SHEET_FILE), JSON.stringify(stored, null, 2), { mode: 0o600 });
+ this.persist();
this.deps.broadcast?.({ kind: "data", botId: this.botId, sheet: this.sheet() });
}
+ private persist(): void {
+ mkdirSync(this.dir, { recursive: true, mode: 0o700 });
+ writeFileAtomic(join(this.dir, SHEET_FILE), JSON.stringify(this.load(), null, 2), { mode: 0o600 });
+ }
+
sheet(): DataSheet {
const { version, botId, cards, sources, updatedAt } = this.load();
return { version, botId, cards, sources, updatedAt };
@@ -109,6 +113,9 @@ export class DataSheetStore {
nextResultName(prefix = "q"): string {
const stored = this.load();
stored.seq += 1;
+ // data_sql can materialise a persistent table without ever saving a card.
+ // Reserve its name on disk before the query can replace an older result.
+ this.persist();
return `${prefix}_${stored.seq}`;
}