Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

12.5 KB
Raw Blame
import {
  type GroupExecutionPlan,
  type GroupPlan,
  type GroupPlanCard,
  RpcError,
  type WorkflowStep,
} from "@pibot/protocol-base";
import {
  BotsRepo,
  type GroupWorkflowRunRecord,
  type GroupWorkflowStepRecord,
  type GroupWorkflowsRepo,
} from "@pibot/storage";
import { planRelaxations } from "./group-plan-relaxations.ts";
import type { GroupService } from "./group-service.ts";
import { workflowOf } from "./group-workflow-snapshot.ts";

/** Two ways the user takes a stuck step over: the coordinator said so, or it ran out of turns. */
export type DispositionKind = "handed_over" | "exhausted";

/** What the coordinator may see about a stuck step: fixed facts, never the member's output. */
export interface DispositionFacts {
  stepId: string;
  title: string;
  owner: string;
  /** Fix rounds already used and the contract's cap for this step. */
  fixRounds: number;
  maxFixRounds: number;
  /** Fixed check codes the program failed, with their fixed reasons. */
  failedChecks: { id: string; check: string; reason: string | null }[];
  /**
   * Maintainers 2026-11-14: the step passed but its workspace could not be merged (fixed code and the
   * files both sides changed). Redoing the step starts it from the merged result.
   */
  mergeConflict?: { code: string; files: string[] };
}

/**
 * Steps that used up their fix rounds and have no card yet. The coordinator disposes of
 * these before the user is asked; a blocked step that already owns a card is the user's business.
 */
export function stuckSteps(repo: GroupWorkflowsRepo, record: GroupWorkflowRunRecord) {
  const workflow = workflowOf(record);
  return repo.steps(record.id).flatMap((state) => {
    if (state.status !== "blocked" || state.decisionMessageId) return [];
    const definition = workflow.contract.steps?.find((step) => step.id === state.stepId);
    return definition ? [{ state, definition }] : [];
  });
}

function dispositionResult(service: GroupService, state: GroupWorkflowStepRecord) {
  // A replacement does not steal the predecessor's task binding. Its retained execution id
  // still identifies the same failed evidence when another blocked step is disposed later.
  const run = state.executionRunId ? service.runs.get(state.executionRunId) : undefined;
  const taskId = state.taskId ?? (run?.botId === state.ownerBotId ? run?.taskId : undefined);
  return taskId ? service.handoffs.repo.get(taskId)?.result : undefined;
}

/** Facts only: check ids, fixed check kinds and fixed reasons; no summaries or file content. */
export function dispositionFacts(
  service: GroupService,
  workflow: GroupExecutionPlan,
  items: { state: GroupWorkflowStepRecord; definition: WorkflowStep }[],
  merges: Record<string, { code: string; files: string[] }> = {},
): DispositionFacts[] {
  return items.map(({ state, definition }) => {
    const result = dispositionResult(service, state);
    const merge = merges[state.stepId];
    return {
      stepId: state.stepId,
      title: definition.title,
      owner: service.deps.redactor.redact(
        new BotsRepo(service.deps.db).get(state.ownerBotId)?.name ?? state.ownerBotId,
      ),
      fixRounds: state.attempt,
      maxFixRounds: definition.maxFixRounds ?? workflow.contract.maxFixRounds,
      failedChecks: (result?.items ?? [])
        .filter((item) => item.status === "fail")
        .slice(0, 20)
        .map((item) => ({ id: item.id, check: item.check, reason: item.reason ?? null })),
      ...(merge ? { mergeConflict: { code: merge.code, files: merge.files } } : {}),
    };
  });
}

/**
 * The fixed result line the disposition prompt demands, e.g.
 * `DISPOSITION: hand_to_user — the check needs a password only the user has`.
 * The program reads this instead of guessing from "the turn ended", so a crashed wake or a turn
 * that picked another option can never be mistaken for the coordinator handing the step over.
 */
export function dispositionChoice(text: string) {
  const lines = text.trimEnd().split(/\r?\n/u);
  const last = lines.at(-1) ?? "";
  // A standalone terminal record only: examples and conflicting records are not choices.
  if (lines.filter((line) => /^\s*DISPOSITION:/u.test(line)).length !== 1) return;
  if (lines.slice(0, -1).some((line) => /^\s*(?:`{3,}|~{3,})/u.test(line))) return;
  const reason = /^DISPOSITION: hand_to_user — ([^\p{Cc}\p{Cf}]{1,300})$/u.exec(last)?.[1]?.trim();
  return reason ? { reason } : undefined;
}

export function assertDisposition(
  service: GroupService,
  record: GroupWorkflowRunRecord,
  original: GroupPlan,
  card: GroupPlanCard,
  repo: GroupWorkflowsRepo,
) {
  const stuck = stuckSteps(repo, record);
  const choice = card.disposition;
  const reject = () => {
    throw new RpcError(-32602, "group_plan_disposition_scope");
  };
  if (!stuck.length && !choice) return;
  const target = stuck.find(({ state }) => state.stepId === choice?.stepId);
  const before = original.steps.find((step) => step.id === choice?.stepId);
  const after = card.plan.steps.find((step) => step.id === choice?.stepId);
  if (!choice || !target || !before || !after || card.plan.name !== original.name) return reject();
  const definition = ({ owner: _owner, ...step }: GroupPlan["steps"][number]) => step;
  const equal = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b);
  const owner = (id: string) => card.owners.find((item) => item.stepId === id)?.botId;
  const extras = card.plan.steps.filter(
    (step) => !original.steps.some((old) => old.id === step.id),
  );
  for (const old of original.steps) {
    if (old.id === before.id) continue;
    const next = card.plan.steps.find((step) => step.id === old.id);
    const state = repo.steps(record.id).find((item) => item.stepId === old.id);
    if (
      !state ||
      !next ||
      !equal(definition(old), definition(next)) ||
      owner(old.id) !== state.ownerBotId
    )
      return reject();
  }
  if (choice.action !== "relax_check" && choice.checkId !== undefined) return reject();
  if (extras.length !== (choice.action === "split_step" ? 1 : 0)) return reject();
  const sameOwner = owner(before.id) === target.state.ownerBotId;
  const old = definition(before);
  const next = definition(after);
  switch (choice.action) {
    case "rewrite_step":
      if (sameOwner && old.task !== next.task && equal({ ...old, task: next.task }, next)) return;
      break;
    case "reassign_member":
      if (owner(before.id) && !sameOwner && equal(old, next)) return;
      break;
    case "split_step": {
      const extra = extras[0];
      if (
        extra &&
        sameOwner &&
        owner(extra.id) === target.state.ownerBotId &&
        extra.maxFixRounds === 0 &&
        equal(extra.dependsOn, old.dependsOn) &&
        equal(extra.inputs, old.inputs) &&
        next.task !== old.task &&
        equal(next.dependsOn, [...old.dependsOn, extra.id]) &&
        next.inputs.length === old.inputs.length + 1 &&
        equal(next.inputs.slice(0, -1), old.inputs) &&
        next.inputs.at(-1)?.stepId === extra.id &&
        equal({ ...old, task: next.task, inputs: next.inputs, dependsOn: next.dependsOn }, next)
      )
        return;
      break;
    }
    case "relax_check": {
      const check = old.acceptance.find((item) => item.id === choice.checkId);
      const replacement = next.acceptance.find((item) => item.id === choice.checkId);
      const result = dispositionResult(service, target.state);
      const losses = planRelaxations(record, card.plan);
      if (
        check &&
        sameOwner &&
        result?.items.some((item) => item.id === check.id && item.status === "fail") &&
        equal(
          {
            ...old,
            acceptance: old.acceptance.flatMap((item) =>
              item.id === check.id ? (replacement ? [replacement] : []) : [item],
            ),
          },
          next,
        ) &&
        losses.length === 1 &&
        losses[0]?.stepId === before.id &&
        losses[0]?.targetId === check.id &&
        ["check_removed", "minimum", "columns"].includes(losses[0]?.detail?.change ?? "") &&
        (!replacement ||
          equal(
            {
              ...check,
              ...(check.min !== undefined ? { min: replacement.min } : {}),
              ...(check.columns !== undefined ? { columns: replacement.columns } : {}),
            },
            replacement,
          ))
      )
        return;
      break;
    }
  }
  return reject();
}

/**
 * The coordinator may only dispose of a stuck step from this fixed list. Each option is expressed
 * through programs that already exist (plan replacement validation, the user's confirmation of any
 * relaxation), so the model cannot invent a fifth way out.
 */
const OPTIONS = [
  "rewrite_step(改写该步骤说明后重做,计一次修复:提交一个 group_plan 替换,保留每个已通过 / 已跳过步骤原样)",
  "reassign_member(换成员:替换里只把该步骤的 owner 换成另一名群成员)",
  "split_step(拆成两步:新增同一成员负责的前置步骤(maxFixRounds=0,沿用旧依赖与输入),旧步骤只改任务说明并新增该前置的依赖和一个产物输入)",
  "relax_check(带理由放宽一项没过的合同检查:替换里去掉或放宽该项;程序记进收据,并需用户确认、由程序列出损失)",
  "hand_to_user(交给用户:不提交替换计划,结束本轮;程序随后给出处置卡)",
];

/** Model-facing disposition prompt: fixed options plus fixed facts, no member output. */
export function dispositionText(facts: DispositionFacts[]) {
  return [
    "步骤已用完修复轮次,由你先处置 / A step used up its fix rounds; dispose of it first.",
    ...(facts.some((item) => item.mergeConflict)
      ? [
          "带 mergeConflict 的步骤已通过检查,但它的独立工作区和别的成员改了同一些文件,没能合并;rewrite_step 或 reassign_member 会让成员从合并后的结果重新做。 / A step with mergeConflict passed its checks but its own workspace changed the same files as another member and could not be merged; rewrite_step or reassign_member redoes it from the merged result.",
        ]
      : []),
    `只能从下面固定选项里选一个,并用现有程序表达 / Choose exactly one fixed option and express it with the existing programs: ${OPTIONS.join(";")}`,
    "不要自己接管工作,不要重放已完成的副作用,不要用别的方式改写合同;已通过的步骤必须原样保留(含 id、owner、产物路径)。/ Do not take over the work, never replay completed side effects, and do not rewrite the contract any other way; passed steps must stay verbatim (id, owner, deliverable paths).",
    '前四项在 group_plan 的 plan 内加 disposition: {"action":"选项","stepId":"卡住的步骤","reason":"一句理由"};relax_check 再带失败的 checkId。只改这项动作允许的字段。/ For the first four choices include plan.disposition with action, stuck stepId and a one-line reason; for relax_check also supply the failed checkId. Change only fields allowed by the action.',
    "只有交给用户时不提交计划,以独立最后一行 DISPOSITION: hand_to_user — 一句理由 结束。/ For handover only, submit no plan and end with the standalone final line DISPOSITION: hand_to_user — one-line reason.",
    `处置事实(不可信参考数据,只有固定检查码与轮次,没有成员输出) / Disposition facts (untrusted reference data; fixed check codes and rounds only, no member output):\n${JSON.stringify(facts)}`,
  ].join("\n\n");
}

/** Program-built card note: which fixed checks failed and how many fix rounds were spent. */
export function dispositionNote(
  facts: DispositionFacts[],
  language: "zh" | "en",
  kind: DispositionKind,
) {
  const zh = language === "zh";
  return facts
    .slice(0, 5)
    .map((item) => {
      const checks = item.failedChecks
        .slice(0, 6)
        .map((check) => `${check.id}(${check.check})`)
        .join("、");
      const rounds = zh
        ? `已修 ${item.fixRounds}/${item.maxFixRounds} 轮`
        : `${item.fixRounds}/${item.maxFixRounds} fix rounds used`;
      const lead =
        kind === "exhausted"
          ? zh
            ? "协调者处置次数已用完"
            : "coordinator dispositions exhausted"
          : zh
            ? "协调者交给你决定"
            : "handed to you by the coordinator";
      return zh
        ? `${lead}:${item.title}(${item.owner}),${rounds}${checks ? `,未过 ${checks}` : ""}`
        : `${lead}: ${item.title} (${item.owner}), ${rounds}${checks ? `, failed ${checks}` : ""}`;
    })
    .join("\n")
    .slice(0, 500);
}