Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(goal): recover a valid decision when its close tag is missing, escaped, or trailed by prose
strong4793c9bce809
diff --git a/server/group-goal-run.test.ts b/server/group-goal-run.test.tsindex 1f6cea73a1..d5eefeaf37 100644--- a/server/group-goal-run.test.ts+++ b/server/group-goal-run.test.ts@@ -243,3 +243,101 @@ describe("one goal-run loop for a room and a one-on-one chat", () => {expect(goal.finished).toEqual([{ status: "completed", detail: "Draft verified." }]);});});++describe("parseGroupGoalDecision tolerant recovery (issue #5209)", () => {+ const O = GROUP_GOAL_CONTROL_OPEN;+ const C = GROUP_GOAL_CONTROL_CLOSE;++ it("recovers a complete continue decision whose close tag never arrived (EOF truncation)", () => {+ const parsed = parseGroupGoalDecision(+ `Scout is on it.\n${O}{"status":"continue","next":"Scout","instruction":"Verify the release","detail":"Draft ready"}`,+ );+ expect(parsed.decision).toEqual({+ status: "continue",+ next: "Scout",+ instruction: "Verify the release",+ detail: "Draft ready",+ });+ expect(parsed.visibleText).toBe("Scout is on it.");+ });++ it("recovers a terminal decision whose close tag is missing", () => {+ expect(parseGroupGoalDecision(`${O}{"status":"completed","detail":"All green"}`).decision)+ .toEqual({ status: "completed", detail: "All green" });+ expect(parseGroupGoalDecision(`${O}{"status":"needs-input","detail":"Which warehouse?"}`).decision)+ .toEqual({ status: "needs-input", detail: "Which warehouse?" });+ expect(parseGroupGoalDecision(`${O}{"status":"blocked","detail":"Awaiting the key"}`).decision)+ .toEqual({ status: "blocked", detail: "Awaiting the key" });+ });++ it("recovers a decision closed by an escaped close tag", () => {+ const parsed = parseGroupGoalDecision(+ `Done.\n${O}{"status":"completed","detail":"Shipped"}<\\/openmaus-goal>`,+ );+ expect(parsed.decision).toEqual({ status: "completed", detail: "Shipped" });+ });++ it("recovers a decision when trailing prose follows the JSON before a present close tag", () => {+ const parsed = parseGroupGoalDecision(+ `${O}{"status":"continue","next":"Scout","instruction":"Verify","detail":"ok"} please proceed ${C}`,+ );+ expect(parsed.decision).toEqual({ status: "continue", next: "Scout", instruction: "Verify", detail: "ok" });+ });++ it("recovers a decision whose close tag is escaped and also carries trailing prose", () => {+ const parsed = parseGroupGoalDecision(+ `${O}{"status":"blocked","detail":"No network"} trailing ${""}<\\/openmaus-goal>`,+ );+ expect(parsed.decision).toEqual({ status: "blocked", detail: "No network" });+ });++ it("still fails closed on a genuinely truncated (unbalanced) JSON object", () => {+ expect(parseGroupGoalDecision(`${O}{"status":"continue","next":"Sc`)).toEqual({+ visibleText: "",+ decision: null,+ });+ expect(parseGroupGoalDecision(`Prefix\n${O}{"status":"continue","instruction":"Verify"`).decision).toBeNull();+ });++ it("still fails closed when a continue decision is missing its required fields", () => {+ expect(parseGroupGoalDecision(`${O}{"status":"continue"}`).decision).toBeNull();+ expect(parseGroupGoalDecision(`${O}{"status":"continue","next":"Scout"}`).decision).toBeNull();+ expect(parseGroupGoalDecision(`${O}{"status":"continue","instruction":"Verify"}`).decision).toBeNull();+ expect(parseGroupGoalDecision(`${O}{"status":"completed"}`).decision).toBeNull();+ });++ it("does not recover when two open tags are present and no close tag arrived", () => {+ // Both envelopes are truncated (no close tag anywhere). Tolerant recovery requires a+ // single unambiguous open tag, so a quoted example cannot steer the run.+ const text =+ `Example: ${O}{"status":"continue","next":"X","instruction":"Y","detail":"Z"}\n`+ + `Real: ${O}{"status":"completed","detail":"Done here"`;+ expect(parseGroupGoalDecision(text).decision).toBeNull();+ });++ it("still lets the last complete envelope win when several are present", () => {+ const text =+ `${O}{"status":"continue","next":"Scout","instruction":"A","detail":"1"}${C}\n`+ + `${O}{"status":"completed","detail":"final"}${C}`;+ expect(parseGroupGoalDecision(text).decision).toEqual({ status: "completed", detail: "final" });+ });++ it("keeps the bounded lengths when recovering", () => {+ const longInstruction = "x".repeat(2_400); // no trailing whitespace; must be cut to 2000+ const parsed = parseGroupGoalDecision(+ `${O}{"status":"continue","next":"Scout","instruction":"${longInstruction}","detail":"d"}`,+ );+ expect(parsed.decision?.status).toBe("continue");+ expect((parsed.decision as { instruction: string }).instruction.length).toBe(2_000);+ });++ it("recovers a decision after leading whitespace following the open tag", () => {+ const parsed = parseGroupGoalDecision(`${O}\n {"status":"completed","detail":"ok"}`);+ expect(parsed.decision).toEqual({ status: "completed", detail: "ok" });+ });++ it("fails closed on an open tag with no JSON at all, and on ordinary text", () => {+ expect(parseGroupGoalDecision(`just prose ${O}`).decision).toBeNull();+ expect(parseGroupGoalDecision(`no envelope here`).decision).toBeNull();+ });+});diff --git a/server/group-goal-run.ts b/server/group-goal-run.tsindex 8e57aab470..a11da634ba 100644--- a/server/group-goal-run.ts+++ b/server/group-goal-run.ts@@ -55,36 +55,88 @@ export function visibleCoordinatorText(text: string): string {return visible.replaceAll(GROUP_GOAL_CONTROL_CLOSE, "").trim();}+/** A close tag the model sometimes escapes; it ends an envelope exactly like the plain one. */+const ESCAPED_GOAL_CONTROL_CLOSE = "<\\/openmaus-goal>";++/** The first balanced {...} object at the start of s (after whitespace), or null if none is complete. */+function leadingJsonObject(s: string): string | null {+ let i = 0;+ while (i < s.length && /\s/.test(s[i] as string)) i++;+ if (s[i] !== "{") return null;+ let depth = 0;+ let inString = false;+ let escaped = false;+ for (let k = i; k < s.length; k++) {+ const c = s[k] as string;+ if (inString) {+ if (escaped) escaped = false;+ else if (c === "\\") escaped = true;+ else if (c === "\"") inString = false;+ } else if (c === "\"") inString = true;+ else if (c === "{") depth++;+ else if (c === "}") {+ depth--;+ if (depth === 0) return s.slice(i, k + 1);+ }+ }+ return null;+}++/** Validate one decision object against the goal protocol; null when it is not a usable decision. */+function parseDecisionPayload(payload: string): GroupGoalDecision | null {+ const object = leadingJsonObject(payload);+ if (!object) return null;+ let raw: Record<string, unknown>;+ try {+ raw = JSON.parse(object) as Record<string, unknown>;+ } catch {+ return null;+ }+ const status = raw.status;+ const detail = bounded(raw.detail, 500);+ if (status === "continue") {+ const next = bounded(raw.next, 100);+ const instruction = bounded(raw.instruction, 2_000);+ if (!next || !instruction) return null;+ return { status, next, instruction, ...(detail ? { detail } : {}) };+ }+ if (status === "completed" || status === "needs-input" || status === "blocked") {+ if (!detail) return null;+ return { status, detail };+ }+ return null;+}+/** The last complete envelope wins, so a quoted example earlier in the reply cannot steer the run. */export function parseGroupGoalDecision(text: string): ParsedGroupGoalDecision {- const closeAt = text.lastIndexOf(GROUP_GOAL_CONTROL_CLOSE);+ const visibleText = visibleCoordinatorText(text);+ const fail = (): ParsedGroupGoalDecision => ({ visibleText, decision: null });++ // An envelope ends at the later of the plain or escaped close tag.+ const closeAt = Math.max(+ text.lastIndexOf(GROUP_GOAL_CONTROL_CLOSE),+ text.lastIndexOf(ESCAPED_GOAL_CONTROL_CLOSE),+ );const openAt = closeAt < 0 ? -1 : text.lastIndexOf(GROUP_GOAL_CONTROL_OPEN, closeAt);- if (openAt < 0 || closeAt < 0) return { visibleText: visibleCoordinatorText(text), decision: null };+ if (openAt >= 0 && closeAt > openAt) {+ const payload = text.slice(openAt + GROUP_GOAL_CONTROL_OPEN.length, closeAt);+ const decision = parseDecisionPayload(payload);+ if (decision) return { visibleText, decision };+ }- const visibleText = visibleCoordinatorText(text);- const payload = text.slice(openAt + GROUP_GOAL_CONTROL_OPEN.length, closeAt).trim();- try {- const raw = JSON.parse(payload) as Record<string, unknown>;- const status = raw.status;- const detail = bounded(raw.detail, 500);- if (status === "continue") {- const next = bounded(raw.next, 100);- const instruction = bounded(raw.instruction, 2_000);- if (!next || !instruction) return { visibleText, decision: null };- return {- visibleText,- decision: { status, next, instruction, ...(detail ? { detail } : {}) },- };- }- if (status === "completed" || status === "needs-input" || status === "blocked") {- if (!detail) return { visibleText, decision: null };- return { visibleText, decision: { status, detail } };- }- } catch {+ // Truncation recovery: the close tag never arrived (EOF), or the chosen envelope carried+ // trailing prose. Recover only from a single unambiguous open tag whose following text is+ // exactly one complete, valid decision object, so a quoted example or a second envelope+ // cannot steer the run.+ const open = text.lastIndexOf(GROUP_GOAL_CONTROL_OPEN);+ if (open >= 0 && open === text.indexOf(GROUP_GOAL_CONTROL_OPEN)) {+ const decision = parseDecisionPayload(text.slice(open + GROUP_GOAL_CONTROL_OPEN.length));+ if (decision) return { visibleText, decision };}- return { visibleText, decision: null };+ return fail();}+/** A goal run never recruits somebody outside the room. */export function selectGroupGoalCoordinator<Member extends GoalRunMember>(members: Member[],