Skip to content

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

fix(tools): measure what_changed requests in UTF-8 bytes

Afloatde03a7f64460
diff --git a/server/drivers/agents-what-changed-tool.test.ts b/server/drivers/agents-what-changed-tool.test.ts
new file mode 100644
index 0000000000..d051eb3b06
--- /dev/null
+++ b/server/drivers/agents-what-changed-tool.test.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it, vi } from "vitest";
+
+import type { ToolCallContext } from "./agents-call.ts";
+import { callWhatChangedTool, WHAT_CHANGED_INLINE_MAX, WHAT_CHANGED_INLINE_REFUSAL } from "./agents-what-changed-tool.ts";
+
+function context() {
+ const apiResponse = vi.fn(async () => ({ ok: true, status: 200, body: { text: "comparison" } }));
+ return { apiResponse, context: { client: { apiResponse } } as unknown as ToolCallContext };
+}
+
+describe("what_changed inline request size", () => {
+ it.each([["CJK", "η•Œ".repeat(400_000)], ["emoji", "πŸ™‚".repeat(300_000)]])("refuses oversized %s text before sending a request", async (_label, text) => {
+ const args = { original_text: text, edited_text: "edited" };
+ const body = JSON.stringify(args);
+ expect(body.length).toBeLessThan(WHAT_CHANGED_INLINE_MAX);
+ expect(Buffer.byteLength(body, "utf8")).toBeGreaterThan(1_000_000);
+ const f = context();
+ expect(await callWhatChangedTool(args, f.context)).toEqual({ text: WHAT_CHANGED_INLINE_REFUSAL, isError: true });
+ expect(f.apiResponse).not.toHaveBeenCalled();
+ });
+
+ it.each(["a", "η•Œ", "πŸ™‚"])("accepts an exact byte-budget request containing %s", async (character) => {
+ const overhead = Buffer.byteLength(JSON.stringify({ original_text: "", edited_text: "" }), "utf8");
+ const available = WHAT_CHANGED_INLINE_MAX - overhead;
+ const width = Buffer.byteLength(character, "utf8");
+ const original_text = character.repeat(Math.floor(available / width)) + "x".repeat(available % width);
+ const args = { original_text, edited_text: "" };
+ const body = JSON.stringify(args);
+ expect(Buffer.byteLength(body, "utf8")).toBe(WHAT_CHANGED_INLINE_MAX);
+ const f = context();
+ expect(await callWhatChangedTool(args, f.context)).toEqual({ text: "comparison" });
+ expect(f.apiResponse).toHaveBeenCalledWith("/api/internal/what-changed", { method: "POST", body });
+ f.apiResponse.mockClear();
+ expect(await callWhatChangedTool({ ...args, edited_text: "x" }, f.context)).toEqual({ text: WHAT_CHANGED_INLINE_REFUSAL, isError: true });
+ expect(f.apiResponse).not.toHaveBeenCalled();
+ });
+
+ it("counts JSON escaping and preserves the serialized request", async () => {
+ const args = { original_text: '\"\\\nη•ŒπŸ™‚', edited_text: "updated", start: 2 };
+ const f = context();
+ expect(await callWhatChangedTool(args, f.context)).toEqual({ text: "comparison" });
+ expect(f.apiResponse).toHaveBeenCalledWith("/api/internal/what-changed", { method: "POST", body: JSON.stringify(args) });
+ });
+});
diff --git a/server/drivers/agents-what-changed-tool.ts b/server/drivers/agents-what-changed-tool.ts
index 64fc0cb1de..5b83ce6372 100644
--- a/server/drivers/agents-what-changed-tool.ts
+++ b/server/drivers/agents-what-changed-tool.ts
@@ -13,7 +13,7 @@ export const WHAT_CHANGED_INLINE_REFUSAL = "Text passed directly may be about 90
export async function callWhatChangedTool(args: Json, context: ToolCallContext): Promise<ToolCallResult> {
const body = JSON.stringify(args);
- if (body.length > WHAT_CHANGED_INLINE_MAX) return { text: WHAT_CHANGED_INLINE_REFUSAL, isError: true };
+ if (Buffer.byteLength(body, "utf8") > WHAT_CHANGED_INLINE_MAX) return { text: WHAT_CHANGED_INLINE_REFUSAL, isError: true };
const { ok, body: answer } = await context.client.apiResponse("/api/internal/what-changed", { method: "POST", body });
return ok ? { text: String(answer.text ?? "") } : { text: String(answer.error ?? "The comparison could not finish. Try again."), isError: true };
}