Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

10.2 KB
Raw Blame
import type {
  GoalGraphSnapshot,
  GoalReleaseGateDossier,
  GoalReleaseGateUnit,
  GoalRolloutState,
  VerifyCheckItem,
} from '@lobechat/types';
import debug from 'debug';

import { VerifyCheckResultModel } from '@/database/models/verifyCheckResult';
import { VerifyReportModel } from '@/database/models/verifyReport';
import { VerifyRunModel } from '@/database/models/verifyRun';
import { WorkModel } from '@/database/models/work';
import type { LobeChatDatabase } from '@/database/type';

import { AcceptanceService } from '../verify/acceptanceService';
import { carriedUnitBrief } from './rolloutBatch';
import type { RolloutGateResult } from './rolloutGate';

const log = debug('lobe-server:goal-release-gate');

/** Goal graph as `GoalService.graph()` returns it: the snapshot plus spend and acceptances. */
type FetchedGraph = GoalGraphSnapshot;

const PLAN_ITEM_ID = 'plan';
const NEXT_WAVE_ITEM_ID = 'next-wave';
const GATE_CHECKS_ITEM_ID = 'gate-checks';
const unitItemId = (nodeId: string) => `unit:${nodeId}`;

/**
 * The units a release review reports on: the probes while the round is on its
 * canary, the last released wave after that.
 */
export const reviewedUnitIds = (state: GoalRolloutState, waveSize: number): string[] => {
  if (state.waveIndex === 0) return state.probeNodeIds;
  const lastRelease = state.gateLog?.findLast((entry) => entry.outcome === 'released');
  return (state.massNodeIds ?? []).slice(-(lastRelease?.releasedCount ?? waveSize));
};

/**
 * Build the dossier a release gate is decided on, from the fetched graph.
 *
 * Probes on the first move to batch execution; the last released wave when a
 * later release is held. Frozen into the gate's round, so the review page and
 * any later audit read what the owner actually saw.
 */
export const buildReleaseGateDossier = (
  graph: FetchedGraph,
  input: {
    diagnosis?: string;
    gate?: Pick<RolloutGateResult, 'checks'>;
    kind: GoalReleaseGateDossier['kind'];
    waveSize: number;
  },
): GoalReleaseGateDossier | undefined => {
  const policy = graph.goal.config?.rollout;
  const state = graph.goal.config?.rolloutState;
  if (!policy || !state) return;

  const unitIds = reviewedUnitIds(state, input.waveSize);
  const spendByTask = new Map((graph.spend?.byTask ?? []).map((row) => [row.taskId, row]));

  const units: GoalReleaseGateUnit[] = unitIds.flatMap((nodeId) => {
    const node = graph.nodes.find((candidate) => candidate.id === nodeId);
    if (!node) return [];
    const spend = node.taskId ? spendByTask.get(node.taskId) : undefined;
    const acceptance = graph.acceptances?.[nodeId];
    const brief = carriedUnitBrief(node);
    return [
      {
        ...(acceptance && {
          acceptance: {
            id: acceptance.id,
            ...(acceptance.checks && {
              checks: {
                failed: acceptance.checks.failed,
                passed: acceptance.checks.passed,
                total: acceptance.checks.total,
              },
            }),
          },
        }),
        artifacts: graph.workVersions
          .filter((link) => link.nodeId === nodeId && link.work && link.work.type !== 'task')
          .map((link) => ({
            ...(link.work!.type === 'document' &&
              link.work!.resourceId && { documentId: link.work!.resourceId }),
            ...(link.work!.identifier && { identifier: link.work!.identifier }),
            ...(link.work!.resourceType && { resourceType: link.work!.resourceType }),
            ...(link.work!.status && { status: link.work!.status }),
            title: link.work!.title ?? link.work!.identifier ?? '',
            type: link.work!.type,
            ...(link.work!.url && { url: link.work!.url }),
            workId: link.work!.workId,
          })),
        // The unit's own note, not the plan-skill instruction every unit
        // carries: the plan itself is the dossier's first section.
        ...(brief && { brief }),
        nodeId,
        runs: spend?.runs ?? 0,
        status: node.status,
        ...(node.taskId && { taskId: node.taskId }),
        title: node.title,
        tokens: spend?.totalTokens ?? 0,
      },
    ];
  });

  const roster = state.unitTitles ?? [];
  const released = state.releasedCount ?? state.probeNodeIds.length;
  const nextWave = roster.slice(released, released + input.waveSize).map((title) => ({
    ...(policy.unitBriefs?.[title] && { brief: policy.unitBriefs[title] }),
    title,
  }));

  return {
    ...(graph.goal.agentId && { agentId: graph.goal.agentId }),
    checks: input.gate?.checks ?? state.gateLog?.at(-1)?.checks ?? [],
    ...(input.diagnosis && { diagnosis: input.diagnosis }),
    goalId: graph.goal.id,
    goalTitle: graph.goal.title,
    kind: input.kind,
    nextWave,
    plan: {
      content: policy.spec?.recipeOutline ?? '',
      revision: state.planVersions?.[String(state.templateRevision)] ?? state.templateRevision,
      ...(state.planSkill && { skillId: state.planSkill.id, skillName: state.planSkill.name }),
    },
    remainingAfter: Math.max(0, roster.length - released - nextWave.length),
    units,
    wave: (state.wavesReleased ?? state.waveIndex) + 1,
  };
};

/** One checklist item per thing the owner judges, so comments can anchor to it. */
const gatePlan = (dossier: GoalReleaseGateDossier): VerifyCheckItem[] => {
  const item = (id: string, title: string, category: string, index: number): VerifyCheckItem => ({
    category,
    id,
    index,
    onFail: 'manual',
    required: true,
    title,
    verifierConfig: {},
    verifierType: 'program',
  });
  return [
    item(PLAN_ITEM_ID, `Plan v${dossier.plan.revision} fits every unit still owed`, 'Plan', 0),
    ...dossier.units.map((unit, index) =>
      item(unitItemId(unit.nodeId), unit.title, 'Results', index + 1),
    ),
    item(
      NEXT_WAVE_ITEM_ID,
      `Wave ${dossier.wave} (${dossier.nextWave.length} units) can start`,
      'After release',
      dossier.units.length + 1,
    ),
    item(GATE_CHECKS_ITEM_ID, 'Gate checks', 'Gate', dossier.units.length + 2),
  ];
};

/**
 * Freezes each unit's outputs the way the conversation's Work cards show them,
 * so the review lists them with the same cards. Best-effort: a failed read
 * leaves the dossier with its plain artifact list.
 */
export const withUnitWorks = async (
  db: LobeChatDatabase,
  scope: { userId: string; workspaceId?: string },
  dossier: GoalReleaseGateDossier,
): Promise<GoalReleaseGateDossier> => {
  const ids = dossier.units.flatMap((unit) =>
    unit.artifacts.flatMap((artifact) => (artifact.workId ? [artifact.workId] : [])),
  );
  if (ids.length === 0) return dossier;
  try {
    const summaries = await new WorkModel(db, scope.userId, scope.workspaceId).listSummariesByIds(
      ids,
    );
    const byId = new Map(summaries.map((summary) => [summary.id, summary]));
    return {
      ...dossier,
      units: dossier.units.map((unit) => {
        const workIds = new Set(unit.artifacts.map((artifact) => artifact.workId));
        const works = [...workIds].flatMap((id) => {
          const work = id && byId.get(id);
          return work ? [work] : [];
        });
        return works.length ? { ...unit, works } : unit;
      }),
    };
  } catch (error) {
    log('release gate: unit works not read: %O', error);
    return dossier;
  }
};

/**
 * Open the acceptance a held release is reviewed on: one aggregate per gate
 * node, one round carrying the frozen dossier, settled at once so the owner
 * can decide on it. Best-effort for the caller — a gate that could not get its
 * review page still holds the goal and stays answerable on the goal page.
 */
export const openReleaseGateAcceptance = async (
  db: LobeChatDatabase,
  scope: { userId: string; workspaceId?: string },
  gateNodeId: string,
  dossier: GoalReleaseGateDossier,
  /** The review a rewritten plan returns to; a first review opens its own. */
  existingAcceptanceId?: string,
): Promise<string> => {
  const service = new AcceptanceService(db, scope.userId, scope.workspaceId);
  const requirement =
    dossier.kind === 'probes'
      ? `Decide whether "${dossier.goalTitle}" moves from its experiments to batch execution`
      : `Decide whether "${dossier.goalTitle}" releases wave ${dossier.wave}`;
  const existing = existingAcceptanceId
    ? await service.acceptanceModel.findById(existingAcceptanceId)
    : undefined;
  const acceptance =
    existing ?? (await service.ensureForSubject('goal_gate', gateNodeId, { requirement }));
  if (!existing)
    await service.acceptanceModel.patchMetadata(acceptance.id, { title: dossier.goalTitle });

  const plan = gatePlan(dossier);
  const run = await new VerifyRunModel(db, scope.userId, scope.workspaceId).create({
    goal: requirement,
    metadata: { releaseGate: await withUnitWorks(db, scope, dossier) },
    plan,
    scenario: 'generic',
    source: 'agent',
    title: requirement,
  });
  await service.attachRun(run.id, acceptance.id);

  const results = new VerifyCheckResultModel(db, scope.userId, scope.workspaceId);
  const reworked = (runs: number) => runs > 1;
  for (const item of plan) {
    const unit = dossier.units.find((candidate) => unitItemId(candidate.nodeId) === item.id);
    const gateFailed = item.id === GATE_CHECKS_ITEM_ID && dossier.checks.some((c) => !c.passed);
    // A reworked unit or an unsettled check is what the owner has to look at:
    // `uncertain`, so the review lists it as an exception rather than a pass.
    const verdict =
      (unit && (reworked(unit.runs) || unit.status !== 'resolved')) || gateFailed
        ? ('uncertain' as const)
        : ('passed' as const);
    await results.upsertByCheckItem({
      checkItemId: item.id,
      checkItemIndex: item.index,
      checkItemTitle: item.title,
      completedAt: new Date(),
      required: true,
      status: 'passed',
      verdict,
      verifierType: 'program',
      verifyRunId: run.id,
    });
  }

  await new VerifyReportModel(db, scope.userId, scope.workspaceId).upsertByRun({
    content: null,
    failedChecks: 0,
    generatedBy: 'goal-coordinator',
    overallConfidence: null,
    passedChecks: plan.length,
    summary: requirement,
    totalChecks: plan.length,
    uncertainChecks: null,
    verdict: 'passed',
    verifyRunId: run.id,
  });
  await service.recomputeStatus(acceptance.id);
  log('release gate %s → acceptance %s (round run %s)', gateNodeId, acceptance.id, run.id);
  return acceptance.id;
};