Skip to content

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

Keep a continued reply one message, and cover the mask helper

erywimf97e0f741105
diff --git a/server/drivers/openai-chat-protocol.ts b/server/drivers/openai-chat-protocol.ts
index d904267bac..116c74668d 100644
--- a/server/drivers/openai-chat-protocol.ts
+++ b/server/drivers/openai-chat-protocol.ts
@@ -20,22 +20,28 @@ export const MAX_REASONING_DETAIL_CHARS = 4_000_000;
export class ChatProtocolError extends Error {}
export const CUT_REPLY_NOTE = "[OpenMausBot: your reply was cut at the output limit. Continue exactly where you stopped.]";
+/** Continuations per turn, shared by every reply the turn produces, so one turn cannot bill forever. */
const MAX_LENGTH_CONTINUATIONS = 8;
-/** True when a reply cut at the output limit has a continuation left. A local model writes tool calls as text, so a
- * cut reply there may be half a call and stays a failure. */
+/** Null when the completion is not a cut reply that can be continued; `exhausted` when the turn's budget is spent. A
+ * local model writes tool calls as text, so a cut reply there may be half a call and stays a failure. */
+function cutReplyOutcome(state: { count: number }, completion: { finishReason: string | null; text: string }, localTools: boolean): "continue" | "exhausted" | null {
+ if (completion.finishReason !== "length" || !completion.text.trim() || localTools) return null;
+ return state.count < MAX_LENGTH_CONTINUATIONS ? "continue" : "exhausted";
+}
+
+/** True when a cut reply has a continuation left in this turn's budget. */
export function cutReplyWillContinue(state: { count: number }, completion: { finishReason: string | null; text: string }, localTools: boolean): boolean {
- return completion.finishReason === "length" && Boolean(completion.text.trim()) && !localTools && state.count < MAX_LENGTH_CONTINUATIONS;
+ return cutReplyOutcome(state, completion, localTools) === "continue";
}
-/** True when a reply cut at the output limit should be continued; throws once the cap is reached. */
+/** True when a cut reply should be continued and consumes one continuation; throws once the turn's budget is spent. */
export function cutReplyContinues(state: { count: number }, completion: { finishReason: string | null; text: string }, localTools: boolean): boolean {
- if (!cutReplyWillContinue(state, completion, localTools)) {
- if (completion.finishReason === "length" && completion.text.trim() && !localTools) {
- throw new ChatProtocolError(`The reply was still cut at the output limit after ${MAX_LENGTH_CONTINUATIONS} continuations; the turn was stopped.`);
- }
- return false;
+ const outcome = cutReplyOutcome(state, completion, localTools);
+ if (outcome === "exhausted") {
+ throw new ChatProtocolError(`The reply was still cut at the output limit after ${MAX_LENGTH_CONTINUATIONS} continuations; the turn was stopped.`);
}
+ if (!outcome) return false;
state.count += 1;
return true;
}
diff --git a/server/drivers/openai-chat.test.ts b/server/drivers/openai-chat.test.ts
index 92cd2a4c53..7e34ff0312 100644
--- a/server/drivers/openai-chat.test.ts
+++ b/server/drivers/openai-chat.test.ts
@@ -1173,7 +1173,7 @@ describe("createOpenAIChatRuntime output-limit continuation", () => {
let requests = 0;
const events = await runTurn(() => ++requests === 1 ? sse(text("The report is", "length")) : sse(text(" written.", "stop")));
expect(requests).toBe(2);
- expect(textOf(events)).toEqual(["The report is", " written."]);
+ expect(textOf(events)).toEqual(["The report is written."]);
expect(events.some((event) => event.type === "runtime.error")).toBe(false);
expect(events.at(-1)).toMatchObject({ type: "turn.completed", ok: true });
@@ -1195,7 +1195,7 @@ describe("createOpenAIChatRuntime output-limit continuation", () => {
{ MINIMAX_API_KEY: secret },
);
expect(requests).toBe(2);
- expect(textOf(events)).toEqual(["before ", "[redacted] after"]);
+ expect(textOf(events)).toEqual(["before [redacted] after"]);
const shown = events.flatMap((event) => event.type === "content.delta" && event.streamKind === "assistant_text" ? [event.delta]
: event.type === "item.completed" && event.itemType === "assistant_text" ? [event.text] : []);
expect(shown.join("")).not.toContain("abcdefgh");
@@ -1222,7 +1222,9 @@ describe("createOpenAIChatRuntime output-limit continuation", () => {
let requests = 0;
const events = await runTurn(() => (requests++, sse(text(`part ${requests}`, "length"))));
expect(requests).toBe(9);
- expect(textOf(events)).toHaveLength(9);
+ expect(textOf(events)).toHaveLength(1);
+ expect(textOf(events)[0]).toContain("part 1");
+ expect(textOf(events)[0]).toContain("part 9");
expect(events.find((event) => event.type === "runtime.error")).toMatchObject({ message: expect.stringContaining("output limit") });
expect(events.at(-1)).toMatchObject({ type: "turn.completed", ok: false });
});
diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.ts
index 6ca9f95305..b3153d2d57 100644
--- a/server/drivers/openai-chat.ts
+++ b/server/drivers/openai-chat.ts
@@ -593,7 +593,7 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)
let toolFailed = false, spoke = false, unfloored = 0, blockedAfterReply = false; // unfloored: failed calls the safety floor did not block after the bot spoke
let correctiveRounds = 0;
const cutReplies = { count: 0 };
- let carried = "";
+ let carried = "", continuedReply = "";
const denials: string[] = [], failedCalls: string[] = [];
const seenCalls = new Set<string>();
let nudges = 0;
@@ -744,7 +744,8 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)
// Reasoning on a tool-call round belongs to the protocol, not a final reply.
const reply = completion.text.trim() ? completion.text : completion.toolCalls.length || unreadable ? "" : completion.reasoning;
const shownReply = prefix || willContinue ? maskSecrets(prefix + completion.text, !willContinue).visible : safeText(reply);
- if (reply.trim()) { spoke = true; emit({ ...base(turn.threadId, turnId), type: "item.completed", itemType: "assistant_text", text: shownReply }); }
+ if (reply.trim()) { spoke = true; continuedReply += shownReply; }
+ if (!willContinue && continuedReply) { emit({ ...base(turn.threadId, turnId), type: "item.completed", itemType: "assistant_text", text: continuedReply }); continuedReply = ""; }
abort.signal.throwIfAborted();
if (unreadable) {
askedAgain = true;
diff --git a/server/drivers/secret-halves.test.ts b/server/drivers/secret-halves.test.ts
new file mode 100644
index 0000000000..b43d32862d
--- /dev/null
+++ b/server/drivers/secret-halves.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from "vitest";
+
+import { maskSecretHalves } from "./secret-halves.ts";
+
+const safe = (text: string) => text;
+const feed = (text: string, secrets: string[], flush = false) => maskSecretHalves(text, secrets, safe, flush);
+
+describe("maskSecretHalves", () => {
+ const secret = "abcdefgh12345678";
+
+ it("holds a suffix that could start a credential, then masks it once the rest arrives", () => {
+ const first = feed(`before ${secret.slice(0, 8)}`, [secret]);
+ expect(first).toEqual({ visible: "before ", held: secret.slice(0, 8) });
+ expect(feed(first.held + secret.slice(8) + " after", [secret], true)).toEqual({ visible: "[redacted] after", held: "" });
+ });
+
+ it("carries a credential split over three chunks", () => {
+ const one = feed("abc", [secret]);
+ const two = feed(one.held + "defgh12", [secret]);
+ const three = feed(two.held + "345678 end", [secret], true);
+ expect(one.visible).toBe("");
+ expect(two.visible).toBe("");
+ expect(three.visible).toBe("[redacted] end");
+ });
+
+ it("releases held text unmasked when the next chunk shows it was not a credential", () => {
+ const held = feed("abcdefgh", [secret]).held;
+ expect(held).toBe("abcdefgh");
+ expect(feed(held + "x", [secret])).toEqual({ visible: "abcdefghx", held: "" });
+ });
+
+ it("masks any of several overlapping credentials", () => {
+ const other = "abcdefgh99999999";
+ expect(feed("say abcdefgh12", [secret, other])).toEqual({ visible: "say ", held: "abcdefgh12" });
+ expect(feed("abcdefgh12345678 ok abcdefgh99999999", [secret, other], true))
+ .toEqual({ visible: "[redacted] ok [redacted]", held: "" });
+ });
+
+ it("ignores an empty credential", () => {
+ expect(feed("plain text", ["", secret])).toEqual({ visible: "plain text", held: "" });
+ expect(feed("plain text", [""], true)).toEqual({ visible: "plain text", held: "" });
+ });
+});