Skip to content

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

19.7 KB
Raw Blame
import type { ServerText } from "../shared/server-text.ts";
import { noticeFailure } from "./chat-notices.ts";
import type { GroupGoalRunStatus } from "../shared/group-goal-run.ts";
import type { GroupDefaultResponder } from "./store.ts";

// Odd so the final bounded turn always falls to the coordinator for the completion decision.
export const GROUP_GOAL_MAX_TURNS = 13;
export const GROUP_GOAL_WAIT_MAX_MS = Math.max(1_000, Number(process.env.OMB_GOAL_WAIT_MAX_MS) || 30 * 60_000);
const GROUP_GOAL_MAX_WAIT_EXHAUSTIONS = 3;
/** How many replies in a row may miss the control envelope before the run is blocked. */
export const GROUP_GOAL_CONTROL_MISSES = 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>";

const controlMissNote = (next: string, instruction: string): string =>
  "Your last reply did not end with exactly one valid control envelope, so the goal could not take your decision. "
  + "Do not repeat the work or the assignments you already made. Write one short update, then end with the envelope alone on its own last line: "
  + `${GROUP_GOAL_CONTROL_OPEN}{"status":"continue","next":"${next}","instruction":"${instruction}","detail":"Short progress note"}${GROUP_GOAL_CONTROL_CLOSE}`;

export type GroupGoalDecision =
  | { status: "continue"; next: string; instruction: string; detail?: string }
  | { status: "completed" | "needs-input" | "blocked"; detail: string };

export interface GoalRunMember {
  id: string;
  name: string;
  hidden?: boolean;
  chiefOfStaff?: boolean;
}

export interface ParsedGroupGoalDecision {
  visibleText: string;
  decision: GroupGoalDecision | null;
}

export function groupGoalCompletionTurnId(
  eventTurnId: string | undefined,
  boundTurnId: string | undefined,
): string | undefined {
  return eventTurnId ?? boundTurnId;
}

const bounded = (value: unknown, max: number): string =>
  typeof value === "string" ? value.trim().slice(0, max) : "";

/** A reply without its private goal envelopes; a turn outside the goal's own (a teammate's result waking the lead) can write one too. */
export function visibleCoordinatorText(text: string): string {
  let visible = text;
  for (;;) {
    // 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 + closeLen)}`;
      continue;
    }
    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, "").replaceAll(ESCAPED_GOAL_CONTROL_CLOSE, "").trim();
}

/** 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;
  }
  // 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") {
    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;
}

/** 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);
  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 > openAt) {
    const payload = text.slice(openAt + GROUP_GOAL_CONTROL_OPEN.length, closeAt);
    const decision = parseDecisionPayload(payload);
    if (decision) return { visibleText, decision };
  }

  // Truncation recovery: the close tag never arrived (EOF), or the chosen envelope carried
  // 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) && isAtLineStart(text, open)) {
    const decision = parseDecisionPayload(text.slice(open + GROUP_GOAL_CONTROL_OPEN.length));
    if (decision) return { visibleText, decision };
  }
  return fail();
}

/** A goal run never recruits somebody outside the room. */
export function selectGroupGoalCoordinator<Member extends GoalRunMember>(
  members: Member[],
  responder: GroupDefaultResponder,
): Member | null {
  const active = members.filter((member) => !member.hidden);
  const lead = responder.kind === "member" ? responder.botId : responder.kind === "auto" ? responder.fallbackBotId : undefined;
  if (lead) {
    const explicit = active.find((member) => member.id === lead);
    if (explicit) return explicit;
  }
  return active.find((member) => member.chiefOfStaff) ?? active[0] ?? null;
}

export function resolveGroupGoalMember(reference: string, members: GoalRunMember[]): GoalRunMember | null {
  const wanted = reference.trim().replace(/^@/, "").toLocaleLowerCase();
  if (!wanted) return null;
  const active = members.filter((member) => !member.hidden);
  const idMatch = active.find((member) => member.id.toLocaleLowerCase() === wanted);
  if (idMatch) return idMatch;
  const nameMatches = active.filter((member) => member.name.toLocaleLowerCase() === wanted);
  return nameMatches.length === 1 ? nameMatches[0]! : null;
}

/** `solo` is a room of one: the lead does the work in its own turns, and "continue" names itself for its next turn. */
export function groupGoalCoordinatorInstructions(args: {
  goal: string;
  members: GoalRunMember[];
  turn: number;
  maxTurns: number;
  remainingTurns: number;
  note?: string;
  solo?: boolean;
}): string {
  const active = args.members.filter((member) => !member.hidden);
  const roster = active
    .map((member) => `${member.name} (${member.id})`)
    .join(", ");
  const self = args.solo ? active[0] : undefined;
  return [
    self ? "You are working alone toward a bounded goal." : "You are the lead coordinator for a bounded team goal run.",
    `Goal: ${args.goal}`,
    ...(self ? [] : [`Available room members: ${roster}.`]),
    `This is ${self ? "" : "team "}turn ${args.turn} of ${args.maxTurns}; ${args.remainingTurns} turn(s) remain after this one.`,
    ...(args.note ? [`Harness note: ${args.note}`] : []),
    self
      ? "Use the conversation as the progress ledger. Do the next concrete piece of work now, then decide whether the goal is genuinely complete, needs the human, is blocked, or needs another turn."
      : "Use the conversation as the progress ledger. Decide whether the goal is genuinely complete, needs the human, is blocked, or needs one named teammate next.",
    "Do not continue merely to generate discussion. Do not claim completion unless the requested deliverable or answer is present in the conversation.",
    "Write a brief human-facing update or final answer, then end with exactly one private control envelope on its own line.",
    self
      ? `${GROUP_GOAL_CONTROL_OPEN}{"status":"continue","next":"${self.id}","instruction":"Your next step","detail":"Short progress note"}${GROUP_GOAL_CONTROL_CLOSE}`
      : `${GROUP_GOAL_CONTROL_OPEN}{"status":"continue","next":"Exact member id from the roster","instruction":"Concrete next assignment","detail":"Short progress note"}${GROUP_GOAL_CONTROL_CLOSE}`,
    `Or use status "completed", "needs-input", or "blocked" with a non-empty "detail" and omit next/instruction.`,
    "Never mention, quote, or explain the control envelope in your human-facing text.",
  ].join("\n");
}

export function groupGoalWorkerInstructions(args: {
  goal: string;
  coordinatorName: string;
  assignment: string;
  turn: number;
  maxTurns: number;
}): string {
  return [
    `You are a specialist contributing to a bounded team goal run led by ${args.coordinatorName}.`,
    `Goal: ${args.goal}`,
    `Your assignment: ${args.assignment}`,
    `This is team turn ${args.turn} of ${args.maxTurns}.`,
    "Do the assigned work now and put concrete results, evidence, or a clear blocker in your reply.",
    `Address your result to ${args.coordinatorName}. Do not invent a new orchestration protocol or emit an openmaus-goal control envelope.`,
  ].join("\n");
}

export function groupGoalAssignmentKey(memberId: string, instruction: string): string {
  return `${memberId}:${instruction.trim().toLocaleLowerCase().replace(/\s+/g, " ")}`;
}

export type GoalStepOutcome =
  | "settled"
  | "provider_failed"
  | "dispatch_failed"
  | "spend_capped"
  | "stalled"
  | "timed_out"
  | "cancelled"
  | "busy"
  | "parked"
  | "unavailable";

export interface GoalStepResult {
  ran: boolean;
  replyText: string;
  outcome?: GoalStepOutcome;
  stopReason?: string | null;
  stopReasonText?: ServerText;
}

export type GoalRunEnd = Exclude<GroupGoalRunStatus, "working">;

export interface GoalRunDeps<Bot extends GoalRunMember> {
  run: { goal: string; turnCount: number; maxTurns: number; finished: boolean };
  coordinator: Bot;
  members: readonly Bot[];
  solo?: boolean;
  cancelled(): boolean;
  /** The member as the store has it now. */
  current(botId: string): Bot | undefined;
  /** One member turn; a step counts against the run's turns once its member is claimed. */
  step(bot: Bot, coordinator: boolean, instructions: string): Promise<GoalStepResult>;
  finish(status: GoalRunEnd, detail: string, text?: ServerText): void;
  progress(detail: string, text?: ServerText): void;
  /** An activity row in the conversation, from the coordinator. */
  note(text: string): void;
}

// spend_capped is deliberately absent: a cap refusal is deterministic.
const TRANSIENT_STEP: ReadonlySet<GoalStepOutcome | undefined> = new Set(["provider_failed", "dispatch_failed", "stalled", "timed_out"]);

async function stepOnceRetried<Bot extends GoalRunMember>(deps: GoalRunDeps<Bot>, bot: Bot, coordinator: boolean, instructions: string): Promise<GoalStepResult> {
  const first = await deps.step(bot, coordinator, instructions);
  if (!TRANSIENT_STEP.has(first.outcome) || deps.cancelled()) return first;
  const why = { provider_failed: "chat.notice.part.providerFailed", dispatch_failed: "chat.notice.part.dispatchFailed", stalled: "chat.notice.part.goalStalled", timed_out: "chat.notice.part.goalTimedOut" }[first.outcome as "provider_failed" | "dispatch_failed" | "stalled" | "timed_out"];
  const retry = noticeFailure("chat.notice.goalRetry", { name: bot.name, why });
  deps.progress(retry.message, retry.text);
  return deps.step(bot, coordinator, instructions);
}

/** The goal run loop, the same for a room and a one-on-one chat (a room of one). */
export async function runGoalRun<Bot extends GoalRunMember>(deps: GoalRunDeps<Bot>): Promise<void> {
  const { run, coordinator } = deps;
  const assignmentCounts = new Map<string, number>();
  const goalMembers: GoalRunMember[] = deps.members.map((member) => ({
    id: member.id,
    name: member.name,
    hidden: member.hidden,
    chiefOfStaff: member.chiefOfStaff,
  }));

  let coordinatorNote: string | undefined;
  let waitExhaustions = 0;
  let controlMisses = 0;
  while (!deps.cancelled() && run.turnCount < run.maxTurns) {
    const coordinatorTurn = run.turnCount + 1;
    const note = coordinatorNote;
    coordinatorNote = undefined;
    const coordinatorResult = await stepOnceRetried(deps, coordinator, true, groupGoalCoordinatorInstructions({
      goal: run.goal,
      members: goalMembers,
      turn: coordinatorTurn,
      maxTurns: run.maxTurns,
      remainingTurns: run.maxTurns - coordinatorTurn,
      note,
      solo: deps.solo,
    }));
    if (deps.cancelled()) return;
    if (coordinatorResult.outcome === "unavailable") {
      deps.finish("blocked", `${coordinator.name} is not available.`);
      return;
    }
    if (coordinatorResult.outcome === "spend_capped") {
      deps.finish("blocked", coordinatorResult.stopReason ?? `${coordinator.name} hit the workspace spend cap.`);
      return;
    }
    if (coordinatorResult.outcome === "busy") {
      const code = coordinatorResult.stopReasonText;
      const busy = code?.key === "chat.notice.goalBusyOne" || code?.key === "chat.notice.goalBusyMany"
        ? noticeFailure(code.key === "chat.notice.goalBusyOne" ? "chat.notice.goalBusyOneAgain" : "chat.notice.goalBusyManyAgain", code.params as Record<string, string>) : undefined;
      deps.finish("blocked", busy?.message ?? `${coordinatorResult.stopReason ?? `${coordinator.name} stayed busy`} — send the goal again when they are free.`, busy?.text);
      return;
    }
    if (coordinatorResult.outcome === "parked") {
      deps.finish(
        "blocked",
        `${coordinatorResult.stopReason ?? `${coordinator.name} parked waiting for the computer`} — send the goal again when the computer is free.`,
      );
      return;
    }
    if (!coordinatorResult.ran || coordinatorResult.outcome !== "settled") {
      const reason = coordinatorResult.stopReason?.trim().slice(0, 120);
      deps.finish("failed", `${coordinator.name} could not complete the coordination step${reason ? ` — ${reason}` : ""}.`);
      return;
    }

    const decision = parseGroupGoalDecision(coordinatorResult.replyText).decision;
    if (!decision) {
      controlMisses += 1;
      const limit = GROUP_GOAL_CONTROL_MISSES;
      if (controlMisses >= limit || run.turnCount >= run.maxTurns) {
        deps.finish("blocked", `${coordinator.name} did not provide a valid next-step decision after ${controlMisses} ${controlMisses === 1 ? "reply" : "replies"}.`);
        return;
      }
      deps.progress(`${coordinator.name}'s reply did not end with a valid control envelope. Asking again (${controlMisses}/${limit}).`);
      coordinatorNote = deps.solo
        ? controlMissNote(coordinator.id, "Your next step")
        : controlMissNote("Exact member id from the roster", "Concrete next assignment");
      continue;
    }
    controlMisses = 0;
    if (decision.status !== "continue") {
      deps.finish(decision.status, decision.detail);
      return;
    }
    if (run.turnCount >= run.maxTurns) break;

    const worker = resolveGroupGoalMember(decision.next, goalMembers);
    if (!worker) {
      deps.finish("blocked", `${coordinator.name} selected a teammate who is not an active member of this channel.`);
      return;
    }
    const workerBot = deps.current(worker.id);
    if (!workerBot || workerBot.hidden) {
      deps.finish("blocked", `${worker.name} is not available.`);
      return;
    }
    const assignmentKey = groupGoalAssignmentKey(worker.id, decision.instruction);
    const repeated = (assignmentCounts.get(assignmentKey) ?? 0) + 1;
    assignmentCounts.set(assignmentKey, repeated);
    if (repeated >= 3) {
      deps.finish("blocked", `The team repeated the same assignment three times without resolving the goal.`);
      return;
    }
    if (deps.solo && workerBot.id === coordinator.id) {
      coordinatorNote = `Your next step: ${decision.instruction}`;
      continue;
    }

    const workerTurn = run.turnCount + 1;
    const workerResult = await stepOnceRetried(deps, workerBot, false, groupGoalWorkerInstructions({
      goal: run.goal,
      coordinatorName: coordinator.name,
      assignment: decision.instruction,
      turn: workerTurn,
      maxTurns: run.maxTurns,
    }));
    if (deps.cancelled()) return;
    if (workerResult.outcome === "unavailable") {
      deps.finish("blocked", `${workerBot.name} is not available.`);
      return;
    }
    if (workerResult.outcome === "spend_capped") {
      deps.finish("blocked", workerResult.stopReason ?? `${workerBot.name} hit the workspace spend cap.`);
      return;
    }
    if (workerResult.outcome === "busy" || workerResult.outcome === "parked") {
      waitExhaustions += 1;
      if (waitExhaustions >= GROUP_GOAL_MAX_WAIT_EXHAUSTIONS) {
        deps.finish("blocked", `Teammates stayed busy past the wait limit ${waitExhaustions} times — try again when the team is free.`);
        return;
      }
      const reason = workerResult.stopReason?.trim().slice(0, 120) ?? `${workerBot.name} stayed busy`;
      deps.note(`${reason} — asking ${coordinator.name} to reassign`);
      deps.progress(`${reason}. ${coordinator.name} is reassigning.`);
      coordinatorNote =
        `${reason} and could not take the assignment "${decision.instruction.slice(0, 160)}". ` +
        "Reassign it to another available member, do it yourself if you can, or report blocked.";
      continue;
    }
    if (!workerResult.ran || workerResult.outcome !== "settled" || !workerResult.replyText.trim()) {
      const reason = workerResult.stopReason?.trim().slice(0, 120);
      deps.finish("failed", `${workerBot.name} could not return a result to ${coordinator.name}${reason ? ` — ${reason}` : ""}.`);
      return;
    }
  }

  if (!deps.cancelled() && !run.finished) {
    deps.finish("limit-reached", `Paused at the ${run.maxTurns}-turn safety limit. Send the goal again to continue with a fresh bounded run.`);
  }
}