Skip to content

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

fix(goal): harden tolerant recovery against CodeRabbit review findings

StronG.D0049316e02f8
diff --git a/server/group-goal-run.test.ts b/server/group-goal-run.test.ts
index d5eefeaf37..52d4b40797 100644
--- a/server/group-goal-run.test.ts
+++ b/server/group-goal-run.test.ts
@@ -341,3 +341,55 @@ describe("parseGroupGoalDecision tolerant recovery (issue #5209)", () => {
expect(parseGroupGoalDecision(`no envelope here`).decision).toBeNull();
});
});
+
+describe("parseGroupGoalDecision CodeRabbit review fixes", () => {
+ const O = GROUP_GOAL_CONTROL_OPEN;
+ const C = GROUP_GOAL_CONTROL_CLOSE;
+
+ it("rejects a reply that carries a second decision object after the first", () => {
+ // A correction that cancels the first decision must not be silently ignored.
+ expect(
+ parseGroupGoalDecision(
+ `${O}{"status":"continue","next":"Scout","instruction":"Send"} {"status":"blocked","detail":"Do not send"}${C}`,
+ ).decision,
+ ).toBeNull();
+ });
+
+ it("rejects a reply that repeats a control marker after the first decision", () => {
+ expect(
+ parseGroupGoalDecision(`${O}{"status":"completed","detail":"ok"}${O}${C}`).decision,
+ ).toBeNull();
+ });
+
+ it("keeps a single decision with trailing prose but no second object", () => {
+ // Minor trailing prose is the intended tolerance (issue case 3); a lone object still wins.
+ expect(
+ parseGroupGoalDecision(`${O}{"status":"completed","detail":"ok"} all set${C}`).decision,
+ ).toEqual({ status: "completed", detail: "ok" });
+ });
+
+ it("keeps the visible text that follows an escaped close tag", () => {
+ const parsed = parseGroupGoalDecision(
+ `First\n${O}{"status":"completed","detail":"ok"}<\\/openmaus-goal>\nSecond`,
+ );
+ expect(parsed.decision).toEqual({ status: "completed", detail: "ok" });
+ expect(parsed.visibleText).toContain("Second");
+ expect(parsed.visibleText).toContain("First");
+ });
+
+ it("does not accept a lone quoted example written mid-line without a close tag", () => {
+ // An open tag preceded by prose on the same line is a quoted example, not a control block.
+ expect(
+ parseGroupGoalDecision(`Example: ${O}{"status":"completed","detail":"done"}`).decision,
+ ).toBeNull();
+ });
+
+ it("still recovers a genuine truncated envelope that starts its own line", () => {
+ expect(
+ parseGroupGoalDecision(`${O}{"status":"completed","detail":"done"}`).decision,
+ ).toEqual({ status: "completed", detail: "done" });
+ expect(
+ parseGroupGoalDecision(`Progress\n${O}{"status":"continue","next":"Scout","instruction":"Verify"}`).decision,
+ ).toEqual({ status: "continue", next: "Scout", instruction: "Verify" });
+ });
+});
diff --git a/server/group-goal-run.ts b/server/group-goal-run.ts
index a11da634ba..b662a79cb0 100644
--- a/server/group-goal-run.ts
+++ b/server/group-goal-run.ts
@@ -9,6 +9,8 @@ export const GROUP_GOAL_WAIT_MAX_MS = Math.max(1_000, Number(process.env.OMB_GOA
const GROUP_GOAL_MAX_WAIT_EXHAUSTIONS = 3;
export const GROUP_GOAL_CONTROL_OPEN = "<openmaus-goal>";
export const GROUP_GOAL_CONTROL_CLOSE = "</openmaus-goal>";
+/** A close tag the model sometimes escapes; it ends an envelope exactly like the plain one. */
+const ESCAPED_GOAL_CONTROL_CLOSE = "<\\/openmaus-goal>";
export type GroupGoalDecision =
| { status: "continue"; next: string; instruction: string; detail?: string }
@@ -40,24 +42,31 @@ const bounded = (value: unknown, max: number): string =>
export function visibleCoordinatorText(text: string): string {
let visible = text;
for (;;) {
- const closeAt = visible.indexOf(GROUP_GOAL_CONTROL_CLOSE);
+ // An envelope ends at the earliest plain or escaped close tag, so an escaped
+ // envelope is stripped exactly like a plain one and any visible text after it is kept.
+ let closeAt = -1;
+ let closeLen = 0;
+ for (const close of [GROUP_GOAL_CONTROL_CLOSE, ESCAPED_GOAL_CONTROL_CLOSE]) {
+ const at = visible.indexOf(close);
+ if (at >= 0 && (closeAt < 0 || at < closeAt)) {
+ closeAt = at;
+ closeLen = close.length;
+ }
+ }
if (closeAt < 0) break;
const openAt = visible.lastIndexOf(GROUP_GOAL_CONTROL_OPEN, closeAt);
if (openAt < 0) {
- visible = `${visible.slice(0, closeAt)}${visible.slice(closeAt + GROUP_GOAL_CONTROL_CLOSE.length)}`;
+ visible = `${visible.slice(0, closeAt)}${visible.slice(closeAt + closeLen)}`;
continue;
}
- visible = `${visible.slice(0, openAt)}${visible.slice(closeAt + GROUP_GOAL_CONTROL_CLOSE.length)}`;
+ visible = `${visible.slice(0, openAt)}${visible.slice(closeAt + closeLen)}`;
}
// Never expose a half-written control marker or JSON fragment.
const danglingOpen = visible.indexOf(GROUP_GOAL_CONTROL_OPEN);
if (danglingOpen >= 0) visible = visible.slice(0, danglingOpen);
- return visible.replaceAll(GROUP_GOAL_CONTROL_CLOSE, "").trim();
+ return visible.replaceAll(GROUP_GOAL_CONTROL_CLOSE, "").replaceAll(ESCAPED_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;
@@ -92,6 +101,18 @@ function parseDecisionPayload(payload: string): GroupGoalDecision | null {
} catch {
return null;
}
+ // Only minor prose may follow the first decision object. A second decision object or a
+ // control marker means the reply contradicts itself (a correction that cancels the first),
+ // so reject rather than silently take the first object.
+ const rest = payload.slice(object.length);
+ if (
+ rest.includes(GROUP_GOAL_CONTROL_OPEN) ||
+ rest.includes(GROUP_GOAL_CONTROL_CLOSE) ||
+ rest.includes(ESCAPED_GOAL_CONTROL_CLOSE) ||
+ rest.includes("{")
+ ) {
+ return null;
+ }
const status = raw.status;
const detail = bounded(raw.detail, 500);
if (status === "continue") {
@@ -107,6 +128,16 @@ function parseDecisionPayload(payload: string): GroupGoalDecision | null {
return null;
}
+/** True when the open tag at index sits at the start of its line (only whitespace precedes it). */
+function isAtLineStart(text: string, index: number): boolean {
+ for (let i = index - 1; i >= 0; i--) {
+ const c = text[i] as string;
+ if (c === "\n" || c === "\r") return true;
+ if (!/\s/.test(c)) return false;
+ }
+ return true;
+}
+
/** The last complete envelope wins, so a quoted example earlier in the reply cannot steer the run. */
export function parseGroupGoalDecision(text: string): ParsedGroupGoalDecision {
const visibleText = visibleCoordinatorText(text);
@@ -125,11 +156,12 @@ export function parseGroupGoalDecision(text: string): ParsedGroupGoalDecision {
}
// 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.
+ // trailing prose. Recover only from a single unambiguous open tag on its own line whose
+ // following text is exactly one complete, valid decision object, so a quoted example
+ // (an open tag preceded by prose on the same line) 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)) {
+ if (open >= 0 && open === text.indexOf(GROUP_GOAL_CONTROL_OPEN) && isAtLineStart(text, open)) {
const decision = parseDecisionPayload(text.slice(open + GROUP_GOAL_CONTROL_OPEN.length));
if (decision) return { visibleText, decision };
}