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.

14.4 KB
Raw Blame
import { isHeterogeneousAgentModelId } from '@lobechat/const';
import type { VerifyEvidenceType } from '@lobechat/types';
import debug from 'debug';

import { AgentOperationModel } from '@/database/models/agentOperation';
import { DocumentModel } from '@/database/models/document';
import { TaskModel } from '@/database/models/task';
import { VerifyCheckResultModel } from '@/database/models/verifyCheckResult';
import { VerifyEvidenceModel } from '@/database/models/verifyEvidence';
import { VerifyRunModel } from '@/database/models/verifyRun';
import type { LobeChatDatabase } from '@/database/type';

import { createVerifierAgentRunner } from './agentVerifier';
import { settledCarriedItemIds } from './carryForward';
import { runEvidenceGaps } from './evidenceCoverage';
import {
  recordHeterogeneousDeliverableEvidence,
  startEvidenceSubmission,
} from './evidenceSubmission';
import { VerifyExecutorService } from './executor';
import { resolveVerifyModelConfig } from './modelConfig';
import { finalizeVerifyRun } from './settle';
import { VERIFY_ABANDONED_MS } from './staleness';
import { VerifyStatusService } from './statusService';
import { attachTaskRunToAcceptance, resolveTaskAcceptance } from './taskAcceptance';

const log = debug('lobe-server:verify-lifecycle');
const MAX_TASK_DOCUMENT_CHARS = 80_000;

export const resolveVerificationDeliverable = async (
  db: LobeChatDatabase,
  userId: string,
  deliverable: string,
  taskId?: string | null,
  workspaceId?: string,
): Promise<string> => {
  if (!taskId) return deliverable;

  const pinnedDocuments = await new TaskModel(db, userId, workspaceId).getPinnedDocuments(taskId);
  if (pinnedDocuments.length === 0) return deliverable;

  const documentModel = new DocumentModel(db, userId, workspaceId);
  const documents = await Promise.all(
    pinnedDocuments.map(({ documentId }) => documentModel.findById(documentId)),
  );
  const readableDocuments = documents.filter((document): document is NonNullable<typeof document> =>
    Boolean(document?.content),
  );
  if (readableDocuments.length === 0) return deliverable;

  const taskDocumentContent = readableDocuments
    .map(
      (document) =>
        `## Task document: ${document.title ?? document.id}\n\n${document.content ?? ''}`,
    )
    .join('\n\n');

  return [
    deliverable,
    '# Associated task deliverables',
    taskDocumentContent.slice(0, MAX_TASK_DOCUMENT_CHARS),
  ]
    .filter(Boolean)
    .join('\n\n');
};

type VerifiedOperation = NonNullable<Awaited<ReturnType<AgentOperationModel['findById']>>>;

/**
 * Judge a run whose `verifying` lease the caller already holds, then settle it.
 * Shared by the completion-time gate and the stuck-run sweep's re-drive, so both
 * spawn verifiers and drive the task the same way.
 */
const judgeClaimedRun = async (
  db: LobeChatDatabase,
  userId: string,
  params: {
    deliverable: string;
    goal: string;
    operation: VerifiedOperation;
    verifierAgentId?: string;
  },
  workspaceId?: string,
): Promise<void> => {
  const { goal, operation: op, verifierAgentId } = params;

  const modelConfig = await resolveVerifyModelConfig(
    db,
    userId,
    {
      parentModel: op.model,
      parentProvider: op.provider,
      verifierAgentId,
    },
    workspaceId,
  );
  const resolvedDeliverable = await resolveVerificationDeliverable(
    db,
    userId,
    params.deliverable,
    op.taskId,
    workspaceId,
  );

  const executor = new VerifyExecutorService(db, userId, workspaceId);
  await executor.execute({
    deliverable: resolvedDeliverable,
    goal,
    modelConfig,
    operationId: op.id,
    // `agent`-type checks run as the task-pinned verify agent (or the builtin
    // one), which writes its verdict back via the submitVerifyResult tool.
    runVerifierAgent: createVerifierAgentRunner({
      db,
      deliverable: resolvedDeliverable,
      model: modelConfig.model,
      provider: modelConfig.provider,
      taskId: op.taskId,
      topicId: op.topicId,
      userId,
      verifierAgentId,
      workspaceId,
    }),
  });

  // Settle the run: repair-aware tail, then (on terminal settle) report + drive
  // the bound task. For inline (LLM/program) checks everything is resolved now;
  // runs with async agent checks no-op here and re-enter the same finalizer from
  // the verifier's writeback path (verifyResult runtime) β€” so the task is driven
  // from exactly one place regardless of which path finished last.
  await finalizeVerifyRun(
    db,
    userId,
    op.id,
    { report: { deliverable: resolvedDeliverable, goal, modelConfig } },
    workspaceId,
  );
};

/**
 * Judge again a round whose first attempt claimed `verifying` and then died
 * before any verifier started. The caller must already hold the `verifying`
 * lease (the sweep claims it), so this skips the completion gate's claim and
 * evidence phase: the builder's evidence is already on the round.
 */
export const redriveClaimedVerification = async (
  db: LobeChatDatabase,
  userId: string,
  params: { deliverable: string; operationId: string },
  workspaceId?: string,
): Promise<void> => {
  const op = await new AgentOperationModel(db, userId, workspaceId).findById(params.operationId);
  if (!op) throw new Error(`Operation "${params.operationId}" not found`);

  const run = await new VerifyRunModel(db, userId, workspaceId).findByOperation(op.id);
  const acceptance = op.taskId
    ? await resolveTaskAcceptance(db, userId, op.taskId, workspaceId)
    : undefined;

  await judgeClaimedRun(
    db,
    userId,
    {
      deliverable: params.deliverable,
      goal: run?.goal ?? '',
      operation: op,
      verifierAgentId: acceptance?.config.verifierAgentId ?? undefined,
    },
    workspaceId,
  );
};

export interface RunVerifyOnCompletionParams {
  /** The run's final output / artifacts, judged against the plan. */
  deliverable: string;
  /** The user's task the run had to satisfy. */
  goal: string;
  operationId: string;
}

/**
 * Completion-side entry point for the delivery checker. Called fire-and-forget
 * from the agent runtime when an operation terminates successfully. Runs the
 * confirmed check plan (LLM judge inline), then attempts auto-repair.
 *
 * Guarded so it only acts on runs that opted in (a confirmed plan exists) and
 * never throws β€” verification must not affect the run's own lifecycle.
 *
 * Note: agent-type verifiers and auto-repair spawning require full runtime
 * context (sub-operation forking); they are injected seams. Without a spawner
 * those items degrade gracefully (skipped / no repair).
 */
const executeVerifyLifecycle = async (
  db: LobeChatDatabase,
  userId: string,
  params: RunVerifyOnCompletionParams,
  workspaceId?: string,
  evidenceSubmitted = false,
  throwOnError = false,
): Promise<void> => {
  try {
    const confirmedRun = await new VerifyRunModel(db, userId, workspaceId).findByOperation(
      params.operationId,
    );

    // Opt-in gate: only runs with a confirmed plan.
    if (!confirmedRun?.plan?.length || !confirmedRun.planConfirmedAt) return;

    const op = await new AgentOperationModel(db, userId, workspaceId).findById(params.operationId);
    if (!op) {
      log('op %s missing, cannot run verify', params.operationId);
      return;
    }

    // Resolved once here, before any of the branches below can return: it both
    // binds this round to the Task's Acceptance (a builder that planned the round
    // itself leaves it unattached) and pins which agent verifies. Non-task runs
    // keep an undefined verifier β†’ builtin fallback.
    //
    // A throw is left to the outer catch on purpose. Only a resolved "this Task has
    // no Acceptance" may fall back to the builtin verifier; swallowing a transient
    // failure here would hand a Task that pins its own verifier to a different one
    // and settle it on that verdict.
    const resolvedAcceptance = op.taskId
      ? await resolveTaskAcceptance(db, userId, op.taskId, workspaceId)
      : undefined;
    // Attaching can fold this round into a draft round of the acceptance, which
    // deletes the row we read above, so the rest of the lifecycle follows the row
    // the attach settled on.
    const run = resolvedAcceptance
      ? await attachTaskRunToAcceptance(
          db,
          userId,
          { acceptanceId: resolvedAcceptance.acceptance.id, run: confirmedRun },
          workspaceId,
        )
      : confirmedRun;
    // A fold merges both plans onto the surviving row; anything else keeps the plan
    // the opt-in gate above already accepted.
    const plan = run.plan?.length ? run.plan : confirmedRun.plan;

    // The builder now captures Acceptance evidence inside the main run. When it
    // covered the whole plan, the post-run evidence turn has nothing left to
    // collect β€” it mounts the evidence tool exclusively, so all it could add is
    // a restatement of text already in the transcript, at the cost of a second
    // agent run.
    //
    // Coverage is per required criterion, not "any row exists": a builder that
    // evidenced one of three criteria has not finished collecting, and skipping
    // on its first submission would strand the other two at the structural gate
    // with no chance to supply what they ask for.
    if (op.taskId && !evidenceSubmitted) {
      const inRunEvidence = await new VerifyEvidenceModel(db, userId, workspaceId).listByRun(
        run.id,
      );
      const byCheckItem = new Map<string, { type: VerifyEvidenceType }[]>();
      for (const row of inRunEvidence) {
        const rows = byCheckItem.get(row.checkItemId) ?? [];
        rows.push(row);
        byCheckItem.set(row.checkItemId, rows);
      }

      // A criterion that names the artifacts it needs is only covered once each
      // declared run-evidence type is present β€” the same rule the structural
      // gate applies, so the handoff never asks for what the gate ignores.
      const uncovered = plan.filter((item) => {
        if (item.required === false) return false;
        const captured = byCheckItem.get(item.id);
        if (!captured?.length) return true;
        return runEvidenceGaps(item.verifierConfig, captured).length > 0;
      });

      if (inRunEvidence.length > 0 && uncovered.length === 0) {
        log(
          'op %s covered every required criterion in-run (%d rows), skipping the evidence turn',
          params.operationId,
          inRunEvidence.length,
        );
        evidenceSubmitted = true;
      } else if (inRunEvidence.length > 0) {
        log(
          'op %s has partial in-run coverage (%d criteria still uncovered), running the evidence turn',
          params.operationId,
          uncovered.length,
        );
      }
    }

    if (op.taskId && !evidenceSubmitted) {
      const evidenceClaimed = await new VerifyRunModel(
        db,
        userId,
        workspaceId,
      ).claimEvidenceCollection(run.id);
      if (evidenceClaimed) {
        try {
          if (isHeterogeneousAgentModelId(op.model) || isHeterogeneousAgentModelId(op.provider)) {
            await recordHeterogeneousDeliverableEvidence({
              db,
              deliverable: params.deliverable,
              operation: op,
              plan,
              userId,
              workspaceId,
            });
            evidenceSubmitted = true;
          } else {
            // A repair round's carried checks already hold their evidence. Asking
            // the evidence turn for them anyway would add fresh rows, which reopen
            // and re-judge every carried check on the repair's own delta reply.
            const [runResults, runEvidence] = await Promise.all([
              new VerifyCheckResultModel(db, userId, workspaceId).listByRun(run.id),
              new VerifyEvidenceModel(db, userId, workspaceId).listByRun(run.id),
            ]);
            const { settled } = settledCarriedItemIds(runResults, runEvidence);
            await startEvidenceSubmission({
              db,
              deliverable: params.deliverable,
              goal: params.goal,
              operation: op,
              plan: plan.filter((item) => !settled.has(item.id)),
              userId,
              workspaceId,
            });
          }
        } catch (error) {
          await new VerifyRunModel(db, userId, workspaceId).updateStatus(run.id, 'planned');
          throw error;
        }
      }
      if (!evidenceSubmitted) return;
    }

    // Claim only after a task-bound builder has submitted evidence. Standalone
    // runs have no builder phase and enter verification directly.
    const claimed = await new VerifyStatusService(db, userId, workspaceId).claimVerifying(
      params.operationId,
      new Date(Date.now() - VERIFY_ABANDONED_MS),
    );
    if (!claimed) {
      // A queued evidence callback that finds an unfinished judge must remain
      // retryable. A later delivery either reclaims the stale run or observes
      // its terminal status and exits normally.
      if (evidenceSubmitted && run.status === 'verifying') {
        throw new Error(`Verification for operation "${params.operationId}" is still in progress`);
      }
      return;
    }

    await judgeClaimedRun(
      db,
      userId,
      {
        deliverable: params.deliverable,
        goal: params.goal || run.goal || '',
        operation: op,
        verifierAgentId: resolvedAcceptance?.config.verifierAgentId ?? undefined,
      },
      workspaceId,
    );
  } catch (error) {
    log('runVerifyOnCompletion failed for op %s (non-fatal): %O', params.operationId, error);
    if (throwOnError) throw error;
  }
};

export const runVerifyOnCompletion = async (
  db: LobeChatDatabase,
  userId: string,
  params: RunVerifyOnCompletionParams,
  workspaceId?: string,
): Promise<void> => executeVerifyLifecycle(db, userId, params, workspaceId, false);

/**
 * The completion gate as delivered by QStash (see `scheduleVerifyOnCompletion`).
 * Unlike the in-band form it rethrows, so a gate that failed β€” or found its
 * claim still held by an attempt that may have died β€” is redelivered instead of
 * reported as done.
 */
export const runVerifyOnCompletionFromQueue = async (
  db: LobeChatDatabase,
  userId: string,
  params: RunVerifyOnCompletionParams,
  workspaceId?: string,
): Promise<void> => executeVerifyLifecycle(db, userId, params, workspaceId, false, true);

/** The only entry point allowed to advance a task run out of evidence collection. */
export const runVerifyAfterEvidenceSubmission = async (
  db: LobeChatDatabase,
  userId: string,
  params: RunVerifyOnCompletionParams,
  workspaceId?: string,
): Promise<void> => executeVerifyLifecycle(db, userId, params, workspaceId, true, true);