Skip to content

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

Length refusals tell a bot how to shorten and try again

Milind Soniae34e16fe1fc
diff --git a/server/index.ts b/server/index.ts
index 2aee79bbd3..647536f56d 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -118,7 +118,7 @@ import { vpsLeftBehindNotice } from "./cloud-backend.ts";
import * as composio from "./composio.ts";
import { connectorCallFromFrame, connectorRefusalText, connectorScopeRefusalText, connectorUnrecognizedText, evaluateConnectorTools } from "./connector-verdict.ts";
import { connectorCardDescription, connectorCardText, signInCardText } from "./connector-card-text.ts";
-import { BOT_LIMIT_CODE, BOT_PAUSED_CODE } from "../shared/bot-limit.ts"; import { createBotLimit } from "./host/starter-bot-limit.ts"; import { createStarterAwake } from "./host/starter-awake.ts"; import { createStarterCaps } from "./host/starter-caps.ts"; import { createStarterAwakeRoutes } from "./routes/starter-awake.ts"; import { chiefOfStaffSystemPrompt } from "./chief-of-staff.ts"; import { chiefLevel, readTeamRolesWith } from "./chief-level.ts"; import { defaultBotRefusal, ensureDefaultBot } from "./default-bot.ts";
+import { BOT_LIMIT_CODE, BOT_PAUSED_CODE } from "../shared/bot-limit.ts"; import { createBotLimit } from "./host/starter-bot-limit.ts"; import { createStarterAwake } from "./host/starter-awake.ts"; import { createStarterCaps } from "./host/starter-caps.ts"; import { createStarterAwakeRoutes } from "./routes/starter-awake.ts"; import { chiefOfStaffSystemPrompt } from "./chief-of-staff.ts"; import { coordinateTooLong } from "./too-long.ts"; import { chiefLevel, readTeamRolesWith } from "./chief-level.ts"; import { defaultBotRefusal, ensureDefaultBot } from "./default-bot.ts";
import { buildRecall, recallRooms, recallThreadIds, withRecalled } from "./recall.ts";
import { hostTimeZone, takesTurnClock, turnClockLine, withTurnClock } from "./turn-clock.ts";
import { createMemoryUpkeep, dreamConversations, upkeepEnabled } from "./memory-upkeep.ts";
@@ -13937,7 +13937,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {
inMyFolder: z.boolean().optional(),
done_when: z.string().trim().max(1000).optional(), answers: z.string().trim().min(1).max(128).optional(),
}).safeParse(await readInternalBody());
- if (!parsed.success) return json(res, 400, { error: "Provide 1-4 distinct botIds and a message of 1-4000 characters; thread is standing, new or the id of a thread you opened on that teammate, title one line of at most 80 characters, reply is resume or none, consult is a boolean, done_when at most 1000 characters." });
+ if (!parsed.success) return json(res, 400, { error: coordinateTooLong(parsed.error.issues) ?? "Provide 1-4 distinct botIds and a message of 1-4000 characters; thread is standing, new or the id of a thread you opened on that teammate, title one line of at most 80 characters, reply is resume or none, consult is a boolean, done_when at most 1000 characters." });
const { groupId, botIds, message, reason, rework, thread, title, reply, consult, inMyFolder, done_when: doneWhen, answers } = parsed.data;
const result = await coordinateBots({ groupId, botIds, message, reason: reason || undefined, rework, thread, title: title || undefined, reply, consult, inMyFolder, doneWhen: doneWhen || undefined, ...(answers ? { answers } : {}) }); refusedHandoffs.settled(internalCapability.threadId, internalSender, botIds, result.status);
return json(res, result.status, result.body);
diff --git a/server/team-memory.test.ts b/server/team-memory.test.ts
index a4a50bdcf5..905d61f431 100644
--- a/server/team-memory.test.ts
+++ b/server/team-memory.test.ts
@@ -21,6 +21,15 @@ afterEach(async () => {
});
describe("propose", () => {
+ it("refuses an over-long field and says how to send it again, naming the limit", () => {
+ expect(() => memory.propose("", { kind: "term", name: "OMB", detail: "x".repeat(601) }, source))
+ .toThrow("detail is over its 600-character limit, so nothing was done. Keep the fact, its context and why it matters, then send it again.");
+ expect(() => memory.propose("", { kind: "term", name: "x".repeat(121), detail: "ok" }, source)).toThrow("name is over its 120-character limit");
+ expect(() => memory.propose("", { kind: "term", name: "OMB", detail: "ok", aliases: ["x".repeat(121)] }, source)).toThrow("one of the aliases is over its 120-character limit");
+ expect(() => memory.propose("", { kind: "term", name: " ", detail: "ok" }, source)).toThrow("name is required");
+ expect(memory.list("")).toEqual([]);
+ });
+
it.each(["{broken", '{"version":1,"sections":{"Team":[{"invalid":true}]}}'])("preserves an unreadable existing file instead of replacing it: %s", (bytes) => {
const file = join(dir, "team-memory.json");
writeFileSync(file, bytes);
diff --git a/server/team-memory.ts b/server/team-memory.ts
index 35dc9ae328..ea3ece8bd2 100644
--- a/server/team-memory.ts
+++ b/server/team-memory.ts
@@ -3,6 +3,7 @@ import { readFileSync } from "node:fs";
import { createHash } from "node:crypto";
import { z } from "zod";
import { redactSecretsInText } from "./redact.ts";
+import { tooLongRefusal } from "./too-long.ts";
import { USER_ERROR, type UserErrorKey } from "./user-errors.ts";
import { recallTerms } from "./message-db.ts";
import { canonicalJson } from "../shared/package-format.ts";
@@ -181,13 +182,14 @@ export class TeamMemory {
static normalize(input: TeamMemoryInput): Required<TeamMemoryInput> {
if (!TEAM_MEMORY_KINDS.includes(input.kind)) throw new Error(`kind must be one of ${TEAM_MEMORY_KINDS.join(", ")}`);
const name = clean(input.name, TEAM_MEMORY_NAME_MAX);
- if (!name || name.length > TEAM_MEMORY_NAME_MAX) throw new Error(`name is required and at most ${TEAM_MEMORY_NAME_MAX} characters`);
+ if (!name) throw new Error("name is required");
+ if (name.length > TEAM_MEMORY_NAME_MAX) throw new Error(tooLongRefusal("name", TEAM_MEMORY_NAME_MAX, "Use a short title, then send it again."));
const detail = clean(input.detail, TEAM_MEMORY_DETAIL_MAX);
- if (detail.length > TEAM_MEMORY_DETAIL_MAX) throw new Error(`detail is at most ${TEAM_MEMORY_DETAIL_MAX} characters`);
+ if (detail.length > TEAM_MEMORY_DETAIL_MAX) throw new Error(tooLongRefusal("detail", TEAM_MEMORY_DETAIL_MAX, "Keep the fact, its context and why it matters, then send it again."));
const aliases = [...new Set((input.aliases ?? []).map((alias) => clean(alias, TEAM_MEMORY_NAME_MAX)).filter((alias) => alias && !sameName(alias, name)))].slice(0, TEAM_MEMORY_ALIASES_MAX);
- if (aliases.some(alias => alias.length > TEAM_MEMORY_NAME_MAX)) throw new Error(`aliases are at most ${TEAM_MEMORY_NAME_MAX} characters`);
+ if (aliases.some(alias => alias.length > TEAM_MEMORY_NAME_MAX)) throw new Error(tooLongRefusal("one of the aliases", TEAM_MEMORY_NAME_MAX, "Use short names, then send it again."));
const key = clean(input.key, 240) || `${input.kind}:${name.toLowerCase()}`;
- if (key.length > 240) throw new Error("key is at most 240 characters");
+ if (key.length > 240) throw new Error(tooLongRefusal("key", 240, "Use a short stable key, then send it again."));
return { kind: input.kind, name, detail, aliases, key };
}
diff --git a/server/too-long.test.ts b/server/too-long.test.ts
new file mode 100644
index 0000000000..75512d79e2
--- /dev/null
+++ b/server/too-long.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from "vitest";
+import { z } from "zod";
+
+import { coordinateTooLong, tooLongRefusal } from "./too-long.ts";
+
+const shape = z.object({
+ botIds: z.array(z.string()).min(1).max(4),
+ message: z.string().trim().min(1).max(4000),
+ reason: z.string().trim().max(200).optional(),
+ title: z.string().trim().max(80).optional(),
+ done_when: z.string().trim().max(1000).optional(),
+});
+const refusal = (body: unknown) => {
+ const parsed = shape.safeParse(body);
+ if (parsed.success) throw new Error("expected a refusal");
+ return coordinateTooLong(parsed.error.issues);
+};
+
+describe("calls refused for length", () => {
+ it("names the field and limit, says nothing happened, and says how to send it again", () => {
+ expect(tooLongRefusal("detail", 600, "Keep the fact, then send it again."))
+ .toBe("detail is over its 600-character limit, so nothing was done. Keep the fact, then send it again.");
+ });
+
+ it("tells a coordinate_bots caller how to shorten each long field", () => {
+ expect(refusal({ botIds: ["a"], message: "x".repeat(4001) })).toContain("message is over its 4000-character limit");
+ expect(refusal({ botIds: ["a"], message: "x".repeat(4001) })).toContain("save the long detail to a file");
+ expect(refusal({ botIds: ["a"], message: "ok", done_when: "x".repeat(1001) })).toContain("done_when is over its 1000-character limit");
+ expect(refusal({ botIds: ["a"], message: "ok", title: "x".repeat(81) })).toContain("title is over its 80-character limit");
+ });
+
+ it("leaves any other refusal to the general words", () => {
+ expect(refusal({ botIds: ["a", "b", "c", "d", "e"], message: "ok" }), "too many teammates is no length").toBeUndefined();
+ expect(refusal({ botIds: [], message: "" })).toBeUndefined();
+ });
+});
diff --git a/server/too-long.ts b/server/too-long.ts
new file mode 100644
index 0000000000..b10d6bf9e2
--- /dev/null
+++ b/server/too-long.ts
@@ -0,0 +1,24 @@
+// What a bot hears when a call is refused for length: which field, the limit, that nothing happened, and how to send it
+// again. Without the last part a bot retries the same words or gives up.
+
+type Issue = { code: string; path: readonly PropertyKey[]; maximum?: number | bigint };
+
+export const tooLongRefusal = (field: string, max: number | bigint, how: string): string =>
+ `${field} is over its ${String(max)}-character limit, so nothing was done. ${how}`;
+
+const COORDINATE_FIELDS: Readonly<Record<string, string>> = {
+ message: "Send only what the teammate needs to start, and save the long detail to a file in this folder and name its path, then call coordinate_bots again.",
+ done_when: "Say in a sentence or two what finished looks like, then call coordinate_bots again.",
+ title: "Use a shorter title, then call coordinate_bots again.",
+ reason: "Say why in one short line, then call coordinate_bots again.",
+};
+
+/** The first over-long field a coordinate_bots call was refused for; undefined when something else was wrong with it. */
+export function coordinateTooLong(issues: readonly Issue[]): string | undefined {
+ for (const issue of issues) {
+ const field = String(issue.path[0]);
+ const how = COORDINATE_FIELDS[field];
+ if (issue.code === "too_big" && how && issue.maximum !== undefined) return tooLongRefusal(field, issue.maximum, how);
+ }
+ return undefined;
+}