Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(data): enforce result budgets in UTF-8 bytes
Afloatd9dfb19384e9
diff --git a/server/data/result-byte-budgets.test.ts b/server/data/result-byte-budgets.test.tsnew file mode 100644index 0000000000..aa3c9a2f07--- /dev/null+++ b/server/data/result-byte-budgets.test.ts@@ -0,0 +1,38 @@+import { describe, expect, it } from "vitest";++import { boundColumns, boundRows } from "./tools.ts";++const bytes = (value: unknown) => Buffer.byteLength(JSON.stringify(value));++describe("Data tool UTF-8 budgets", () => {+ it.each(["ζ±", "π"])("bounds rows containing %s by their encoded bytes", (character) => {+ const rows = Array.from({ length: 20 }, () => [character.repeat(100)]);+ const bounded = boundRows(rows, 20, 2_000);+ expect(bytes(bounded.rows)).toBeLessThanOrEqual(2_000);+ expect(bounded.truncated).toBe(true);+ expect(bounded.rows.length).toBeGreaterThan(0);+ expect(bounded.rows).toEqual(rows.slice(0, bounded.rows.length));+ });++ it("bounds Unicode column names even after statistics have been removed", () => {+ const columns = Array.from({ length: 4 }, (_, index) => ({ name: `${"ε".repeat(40)}${index}`, type: "VARCHAR" }));+ const bounded = boundColumns(columns, 400);+ expect(bytes(bounded.columns)).toBeLessThanOrEqual(400);+ expect(bounded.truncated).toBe(true);+ expect(bounded.columns.length).toBeGreaterThan(0);+ expect(bounded.columns).toEqual(columns.slice(0, bounded.columns.length));+ });++ it("drops a Unicode sample that exceeds the byte budget", () => {+ const bounded = boundColumns([{ name: "value", type: "VARCHAR", sample: ["ζ±".repeat(40)] }], 110);+ expect(bytes(bounded.columns)).toBeLessThanOrEqual(110);+ expect(bounded).toEqual({ columns: [{ name: "value", type: "VARCHAR" }], truncated: true });+ });++ it("keeps ASCII and escaped content when it fits the exact budget", () => {+ const rows = [["plain", "\"quoted\"\n\\", 7, null]];+ expect(boundRows(rows, 1, bytes(rows))).toEqual({ rows, truncated: false });+ const columns = [{ name: "value", type: "INTEGER", min: "1", max: "7" }];+ expect(boundColumns(columns, bytes(columns))).toEqual({ columns, truncated: false });+ });+});diff --git a/server/data/tools.ts b/server/data/tools.tsindex a9ee90a35e..ffb75ceea2 100644--- a/server/data/tools.ts+++ b/server/data/tools.ts@@ -327,7 +327,7 @@ const CELL_CHARS_MAX = 2000;export function boundRows(rows: RunResult["rows"], maxRows: number, maxBytes: number): { rows: RunResult["rows"]; truncated: boolean } {let truncated = rows.length > maxRows;let kept = rows.slice(0, maxRows).map((row) => row.map((cell) => (typeof cell === "string" && cell.length > CELL_CHARS_MAX ? `${cell.slice(0, CELL_CHARS_MAX)}β¦` : cell)));- while (kept.length && JSON.stringify(kept).length > maxBytes) {+ while (kept.length && Buffer.byteLength(JSON.stringify(kept)) > maxBytes) {kept = kept.slice(0, Math.max(0, kept.length - Math.max(1, Math.ceil(kept.length / 10))));truncated = true;}@@ -342,7 +342,7 @@ export function boundColumns(columns: DataColumn[], maxBytes: number, columnsMaxlet kept: DataColumn[] = truncated? columns.map(({ name, type }) => ({ name, type })): columns.map((column) => ({ ...column, ...(column.sample ? { sample: trimSample(column.sample) } : {}) }));- const size = () => JSON.stringify(kept).length;+ const size = () => Buffer.byteLength(JSON.stringify(kept));if (size() > maxBytes) { kept = kept.map(({ sample: _sample, ...rest }) => rest); truncated = true; }if (size() > maxBytes) kept = kept.map(({ min: _min, max: _max, ...rest }) => rest);if (size() > maxBytes) kept = kept.map(({ name, type }) => ({ name, type }));