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.

31.5 KB
Raw Blame
import { RequestTrigger } from '@lobechat/types';
import debug from 'debug';

import { releaseAgentRunSlot } from '@/business/server/agent-run/runConcurrency';
import { settleAgentRunFee } from '@/business/server/agent-run/runFee';
import {
  AgentOperationModel,
  type RecordOperationStartParams,
} from '@/database/models/agentOperation';
import { MessageModel } from '@/database/models/message';
import { VerifyRunModel } from '@/database/models/verifyRun';
import type { LobeChatDatabase } from '@/database/type';
import { formatErrorForState } from '@/server/modules/AgentRuntime/formatErrorForState';
import { recordShareRunResult } from '@/server/services/aiAgent/shareGate/runMetrics';
import {
  instantiateVerifyPlanOnStart,
  releaseUndeliveredRound,
  scheduleVerifyOnCompletion,
  settleFailedRepair,
} from '@/server/services/verify';
import { after } from '@/server/utils/scheduleAfterResponse';

import { CriticalHookDeliveryError, hookDispatcher } from '../hooks';
import {
  markInterruptedTurn,
  recoverLastAssistantContent,
  resolveErrorMessageId,
} from './assistantMessage';
import { registerFileWorks } from './fileWorks';
import {
  CriticalAgentInterventionPersistenceError,
  notifyPendingAgentIntervention,
} from './intervention';
import { buildLifecycleEvent, extractErrorMessage } from './lifecycleEvent';
import { extractOutboundAttachments, extractTextFromMessageContent } from './messageContent';
import { type CompletionTransition, persistCompletion, settleRunThread } from './persistence';
import { recallUserOnCompletion } from './recall';
import { emitSignalEvents, isAgentShareRun } from './signals';
import type {
  CompleteOperationOptions,
  CompletionLifecycleDeps,
  OperationCompletionInput,
  SignalEvent,
  TopicRunSettledHandler,
} from './types';

export { CriticalAgentInterventionPersistenceError } from './intervention';
export {
  extractTextFromMessage,
  extractTextFromMessageContent,
  findLastAssistantMessage,
  normalizeCompletionMessages,
} from './messageContent';
export { isAgentShareRun } from './signals';
export type {
  CompleteOperationOptions,
  OperationCompletionInput,
  TopicRunSettledHandler,
} from './types';

const log = debug('lobe-server:completion-lifecycle');

/**
 * Terminal reasons this lifecycle treats as a successful completion: the run
 * produced a deliverable, even when it was stopped by a step/cost cap rather
 * than finishing naturally. `persistCompletion` stores all three with
 * status='done', and success-side effects (assistant-content recovery,
 * file-Work registration) must cover the capped reasons too β€” gating them on
 * `reason === 'done'` alone silently drops capped runs' artifacts.
 */
/** Terminal reasons after which the topic's agent may be woken again. */
const isTopicRunSettledReason = (reason: string) =>
  reason !== 'interrupted' && reason !== 'waiting_for_human' && reason !== 'waiting_for_async_tool';

export const isSuccessLikeCompletionReason = (reason: string): boolean =>
  reason === 'done' ||
  reason === 'max_steps' ||
  reason === 'cost_limit' ||
  reason === 'tool_call_repeat_limit';

/**
 * Count a shared Agent run's terminal result, once: only the write that moved the row out of an
 * in-flight status counts, so a replayed completion (a retried hook delivery, a repeated device
 * finish) does not count the run again. The row's `trigger` identifies a share run on synthetic
 * terminals (a device dispatch failure, an abandonment) whose state carries no share visitor.
 *
 * Only the visitor's own top-level run counts: a sub-agent or group member it spawns is part of
 * that run, and counting it too would inflate both the total and the failure rate. A capped run
 * (`max_steps` and the like) that stopped without a reply is a failure the visitor saw, typed
 * by its cap.
 */
const recordShareRunCompletion = (
  state: any,
  reason: string,
  event: {
    attachments?: unknown[];
    errorAttribution?: string;
    errorType?: string;
    lastAssistantContent?: string;
  },
  runOrigin: ReturnType<typeof buildLifecycleEvent>['origin'],
  transition: CompletionTransition | undefined,
) => {
  if (!transition) return;
  if (!isAgentShareRun(state) && transition.trigger !== RequestTrigger.AgentShare) return;
  if (
    transition.parentOperationId ||
    runOrigin.lineage?.parentOperationId ||
    runOrigin.lineage?.isSubAgent === true ||
    runOrigin.lineage?.orchestrationRole === 'member'
  )
    return;
  if (reason === 'done' || reason === 'interrupted') {
    recordShareRunResult({ outcome: reason === 'done' ? 'success' : 'interrupted', stage: 'run' });
    return;
  }
  if (isSuccessLikeCompletionReason(reason)) {
    const replied = !!event.lastAssistantContent?.trim() || !!event.attachments?.length;
    recordShareRunResult(
      replied
        ? { outcome: 'success', stage: 'run' }
        : { errorType: reason, outcome: 'error', stage: 'run' },
    );
    return;
  }
  if (reason === 'error')
    recordShareRunResult({
      attribution: event.errorAttribution,
      errorType: event.errorType,
      outcome: 'error',
      stage: 'run',
    });
};

/**
 * Owns everything that happens once an operation reaches a terminal state:
 * building the lifecycle event payload, emitting completion AgentSignal source
 * events, dispatching `onComplete`/`onError` hooks, and writing the final
 * error back onto the assistant message row.
 *
 * Ordinary side-effect errors are logged and remain non-fatal. Critical
 * no-fallback webhook failures are rethrown after terminal persistence so a
 * queue execution can retry the control-flow handoff.
 */
/** Above the p99 of planning a task run (41 s in production). */
const VERIFY_PLAN_DISPATCH_WAIT_MS = 45_000;

export class CompletionLifecycle {
  private readonly messageModel: MessageModel;
  private readonly agentOperationModel: AgentOperationModel;
  private readonly workspaceId?: string;
  /**
   * In-flight verify-plan instantiations started in {@link recordStart}, keyed by
   * operationId. `dispatchHooks` awaits the matching one before running the
   * completion gate so a very short / no-op task run can't race past its own plan
   * (instantiation is fire-and-forget at start and may not have settled yet).
   */
  private readonly verifyPlanInstantiations = new Map<string, Promise<void>>();
  private readonly onTopicRunSettled?: TopicRunSettledHandler;

  constructor(
    private readonly serverDB: LobeChatDatabase,
    private readonly userId: string,
    workspaceId?: string,
    options?: {
      /**
       * Opt IN to agent-share visitor rows on this service's `messageModel`.
       * Reserved for share-runtime callers driving a visitor turn under the
       * creator's identity.
       */
      includeShareVisitor?: boolean;
      /** See {@link TopicRunSettledHandler}. */
      onTopicRunSettled?: TopicRunSettledHandler;
    },
  ) {
    this.workspaceId = workspaceId;
    this.includeShareVisitor = options?.includeShareVisitor ?? false;
    this.onTopicRunSettled = options?.onTopicRunSettled;
    this.messageModel = new MessageModel(serverDB, userId, workspaceId, undefined, {
      includeShareVisitor: this.includeShareVisitor,
    });
    this.agentOperationModel = new AgentOperationModel(serverDB, userId, workspaceId);
  }

  private readonly includeShareVisitor: boolean;

  /**
   * The instance state the extracted completion steps read. Built per call so
   * the steps always see the current models.
   */
  private deps(): CompletionLifecycleDeps {
    return {
      agentOperationModel: this.agentOperationModel,
      includeShareVisitor: this.includeShareVisitor,
      messageModel: this.messageModel,
      serverDB: this.serverDB,
      userId: this.userId,
      workspaceId: this.workspaceId,
    };
  }

  /**
   * Persist the initial `agent_operations` row when an operation is created.
   * Returns whether the row write succeeded so security-sensitive callers can
   * require durable state before dispatch. Other runtime paths may preserve
   * the historical non-blocking behavior by ignoring the result.
   */
  async recordStart(params: RecordOperationStartParams): Promise<boolean> {
    let persisted = true;
    try {
      await this.agentOperationModel.recordStart(params);
    } catch (error) {
      persisted = false;
      // Hook configuration may contain inline authorization headers. Never log
      // a database error that could repeat its bound metadata parameters.
      const hooks = params.metadata?._hooks;
      if (hooks && (!Array.isArray(hooks) || hooks.length > 0))
        log('[%s] Failed to record operation start (non-fatal)', params.operationId);
      else log('[%s] Failed to record operation start (non-fatal): %O', params.operationId, error);
    }

    // Auto-instantiate the task's verify plan at run start so the completion gate
    // fires. Only for a top-level task operation β€” repair / verifier sub-agents
    // (which carry a parentOperationId) get their plan from the repair path, not
    // here. Fire-and-forget; never blocks startup. We keep the promise (instead of
    // void-ing it) so `dispatchHooks` can await it before the completion gate runs
    // β€” a fast run can otherwise complete first and the gate would no-op on a plan
    // that lands moments later. (instantiateVerifyPlanOnStart never rejects.)
    if (params.taskId && !params.parentOperationId) {
      this.verifyPlanInstantiations.set(
        params.operationId,
        instantiateVerifyPlanOnStart(
          this.serverDB,
          this.userId,
          { operationId: params.operationId, taskId: params.taskId },
          this.workspaceId,
        ),
      );
    }

    return persisted;
  }

  /**
   * Wait, within a bound, for the verify plan {@link recordStart} began. A
   * dispatched run keeps working on another machine while this request ends, so
   * work left in flight here can be dropped with the function. Waiting also
   * means the builder can read its criteria from its first command.
   *
   * The bound only keeps a slow plan call from holding the dispatch: past it the
   * run starts anyway and the finish path still creates the plan.
   */
  async waitForVerifyPlan(
    operationId: string,
    timeoutMs = VERIFY_PLAN_DISPATCH_WAIT_MS,
  ): Promise<void> {
    const instantiation = this.verifyPlanInstantiations.get(operationId);
    if (!instantiation) return;

    let timer: ReturnType<typeof setTimeout> | undefined;
    await Promise.race([
      instantiation,
      new Promise<void>((resolve) => {
        timer = setTimeout(resolve, timeoutMs);
      }),
    ]);
    clearTimeout(timer);
  }

  /**
   * Persist terminal state to `agent_operations`. Fire-and-forget: a DB
   * outage must never block hook dispatch or the executor's terminal
   * cleanup path.
   */
  private async persistCompletion(
    operationId: string,
    state: any,
    reason: string,
    settledAsAbandoned?: boolean,
    terminalClaimed?: boolean,
  ) {
    return persistCompletion(
      this.deps(),
      operationId,
      state,
      reason,
      settledAsAbandoned,
      terminalClaimed,
    );
  }

  /**
   * Extract a human-readable error message from the agent state error object.
   * Handles both raw `ChatCompletionErrorPayload` (from runtime.step catch) and
   * formatted `ChatMessageError` (from executeStep catch).
   *
   * Public so callers can use the same formatting when surfacing errors
   * outside the hook dispatch path (e.g. trace snapshot finalize).
   */
  extractErrorMessage(error: any): string | undefined {
    return extractErrorMessage(error);
  }

  /**
   * Emit completion AgentSignal source events and return compact snapshot
   * events for attachment to the trace step. Fire-and-forget.
   */
  async emitSignalEvents(operationId: string, state: any, reason: string): Promise<SignalEvent[]> {
    return emitSignalEvents(this.deps(), operationId, state, reason);
  }

  /**
   * Insert a `role='verify'` message that renders the Agent Run delivery-checker
   * card (plan + results, read off `metadata.verifyOperationId`). Only created
   * when the run actually has a verify plan. Self-guarded β€” failures never affect
   * the run; the card is purely additive UI.
   */
  private async createVerifyMessage(
    operationId: string,
    assistantMessageId: string | undefined,
    userId: string,
  ): Promise<void> {
    try {
      const run = await new VerifyRunModel(this.serverDB, userId).findByOperation(operationId);
      if (!run?.plan?.length) return;

      const op = await new AgentOperationModel(this.serverDB, userId).findById(operationId);
      if (!op?.topicId) return;

      const messageModel = new MessageModel(this.serverDB, userId, undefined, undefined, {
        includeShareVisitor: this.includeShareVisitor,
      });
      // One card per run. A producer that retries its terminal call (a
      // heterogeneous `heteroFinish` the edge timed out) re-enters here for
      // the same operation, and each pass used to stack another card.
      if (await messageModel.findVerifyMessageByOperationId(operationId, op.topicId)) return;

      await messageModel.create({
        agentId: op.agentId ?? undefined,
        content: '',
        groupId: op.chatGroupId ?? undefined,
        metadata: { verifyOperationId: operationId },
        parentId: assistantMessageId,
        role: 'verify',
        threadId: op.threadId ?? undefined,
        topicId: op.topicId,
      });
    } catch (error) {
      log('createVerifyMessage failed for op %s (non-fatal): %O', operationId, error);
    }
  }

  /**
   * Expand a normalized {@link OperationCompletionInput} into the runtime `state`
   * shape `dispatchHooks` consumes. The SINGLE place that mirrors the runtime
   * state for non-in-process paths β€” goal/deliverable become the user/assistant
   * turns the gate reads, model/provider backfill the op row, hooks ride on
   * `host.hooks`. Replaces the per-caller hand-rolled synthetic state that
   * previously drifted (e.g. a verify field added here was missed by heteroFinish).
   */
  private buildStateFromInput(input: OperationCompletionInput) {
    return {
      cost: input.cost ?? { total: null },
      createdAt: input.startedAt,
      error: input.error ?? undefined,
      messages: [
        { content: input.goal ?? '', role: 'user' },
        { content: input.deliverable ?? '', role: 'assistant' },
      ],
      host: { hooks: input.serializedHooks },
      metadata: {
        assistantMessageId: input.assistantMessageId,
        ...(input.worksRegistered && { _fileWorksRegistered: true }),
      },
      ...(input.shareVisitor && { principal: { actor: { shareVisitor: input.shareVisitor } } }),
      origin: {
        agentId: input.agentId,
        lineage: { orchestrationRole: input.orchestrationRole },
        topicId: input.topicId,
        userId: input.userId ?? this.userId,
      },
      model: input.model ?? undefined,
      provider: input.provider ?? undefined,
      stepCount: input.stepCount ?? null,
      llmCalls: input.llmCalls ?? undefined,
      usage: input.usage ?? undefined,
    };
  }

  /**
   * Register the operation's Works (edited entity files plus github issue/PR
   * Works). Idempotent per state object; see `registerFileWorks` in
   * `./fileWorks` for the marker and retry contract.
   */
  async registerFileWorks(operationId: string, state: any): Promise<void> {
    return registerFileWorks(this.deps(), operationId, state);
  }

  // Feed the result back as `worksRegistered` so completeOperation's backstop skips a second scan.
  async registerOperationWorks(input: OperationCompletionInput): Promise<boolean> {
    const state = this.buildStateFromInput(input);
    await this.registerFileWorks(input.operationId, state);
    return (state.metadata as { _fileWorksRegistered?: boolean })._fileWorksRegistered === true;
  }

  /**
   * The single terminal-completion entry for every path that does NOT have the
   * in-process runtime's rich `state` in hand: heterogeneous CLI exit
   * (`heteroFinish`), remote-agent done signal (`agentNotify`), and synchronous
   * dispatch failure (`finalizeHeteroDispatchError`). Builds the synthetic state
   * once and runs the SAME pipeline the in-process runtime uses β€” persist the
   * terminal op row, fire onComplete/onError hooks, and (on `done`) run the
   * delivery-checker verify gate. This is what makes those paths true lifecycle
   * peers instead of firing a stripped-down hooks-only funnel.
   */
  async completeOperation(
    input: OperationCompletionInput,
    reason: 'done' | 'error' | 'interrupted',
    options?: CompleteOperationOptions,
  ): Promise<void> {
    await this.dispatchHooks(input.operationId, this.buildStateFromInput(input), reason, options);
  }

  /**
   * Dispatch `onComplete` (and `onError` for `reason='error'`) hooks via
   * the global `hookDispatcher`. On the error path, also writes the error
   * back onto the assistant message row so the frontend can render it.
   * Fire-and-forget; unregisters the operation after a settled lifecycle while
   * preserving registrations across retryable critical delivery failures.
   */
  async dispatchHooks(
    operationId: string,
    state: any,
    reason: string,
    options?: CompleteOperationOptions,
  ): Promise<void> {
    // `waiting_for_async_tool` parks the SAME operation: it persists the parked
    // status (the async-tool resume CAS reads it) but must NOT fire `onComplete`
    // or unregister hooks β€” the op resumes under this same id and reaches its
    // real terminal state later, which is when consumers should be notified.
    // `waiting_for_human` is different: the parked segment fires `onComplete`
    // so hook consumers can surface the approval request. A winning decision
    // schedules a fresh continuation operation and then retires this parked
    // segment; the continuation receives the serialized hooks through
    // `host.hooks`.
    // `waiting_for_client` (no client to run the next LLM call) parks the same
    // operation the same way: it resumes under this id or expires to `error`.
    const isAsyncToolPark = reason === 'waiting_for_async_tool' || reason === 'waiting_for_client';
    let shouldRetainHooksForRetry = false;

    try {
      const {
        assistantMessageId,
        event,
        metadata,
        origin: runOrigin,
      } = this.buildLifecycleEvent(operationId, state, reason);

      // Finalize the agent_operations row before user hooks fire so
      // downstream consumers see the row in its terminal shape.
      const completionAccepted = await this.persistCompletion(
        operationId,
        state,
        reason,
        options?.settledAsAbandoned,
        options?.terminalClaimed,
      );
      if (completionAccepted === false) {
        log('[%s] Skipping hooks for an operation with a conflicting terminal owner', operationId);
        return;
      }

      // At this point the parked operation state has been durably projected to
      // agent_operations. Verify the tool rows independently before creating a
      // durable Review so a partial batch can never reach notification UI.
      if (reason === 'waiting_for_human') {
        await this.notifyPendingAgentIntervention(operationId, state);
      }

      if (isAsyncToolPark) return;

      // Before the hooks, so a parent resumed by the bridge reads a closed thread.
      await settleRunThread(this.deps(), operationId, state, reason);

      // Every stop (before step 0, during a deferred init, mid-stream, during a
      // tool call) marks the turn it interrupted; a run that finished is untouched.
      if (reason === 'interrupted') {
        await markInterruptedTurn(this.deps(), operationId, state, runOrigin);
      }

      // The run tree no longer works, so its concurrency slot frees now rather
      // than when the lease lapses. A run parked on a human decision frees it
      // too; its continuation takes a slot of its own.
      await releaseAgentRunSlot(runOrigin.userId || this.userId, operationId);

      // Charge or give back the per-run fee the run held at admission. A run
      // parked on a human decision settles here too: its continuation is part
      // of the same run and holds no fee of its own. A billing failure must
      // not cost the user their completion hooks.
      await settleAgentRunFee({
        durationMs: event.duration,
        operationId,
        reason,
        userId: runOrigin.userId || this.userId,
      }).catch((error) => {
        console.error('[CompletionLifecycle] settleAgentRunFee failed', { error, operationId });
      });

      // `lastAssistantContent` comes off the Redis-backed `state.messages`,
      // while the assistant message row is persisted through a separate
      // `messageModel.update` path. When the two diverge (state entry empty
      // but the DB row holds the full reply β€” Discord bot completions
      // arrived with no content while the app showed the reply), consumers
      // like the IM bot callback silently drop the reply. Recover from the DB
      // row β€” the same source of truth the app UI renders β€” before dispatch.
      if (
        isSuccessLikeCompletionReason(reason) &&
        !event.lastAssistantContent?.trim() &&
        !event.attachments?.length
      ) {
        const recovered = await this.recoverLastAssistantContent(
          operationId,
          assistantMessageId,
          runOrigin.userId || this.userId,
          typeof runOrigin.topicId === 'string' ? runOrigin.topicId : undefined,
        );
        if (recovered) {
          event.lastAssistantContent = recovered;
          const attachments = extractOutboundAttachments([
            { content: recovered, role: 'assistant' },
          ]);
          event.attachments = attachments.length > 0 ? attachments : undefined;
        }
      }

      // After content recovery: a capped run counts as a success only when it left a reply.
      recordShareRunCompletion(state, reason, event, runOrigin, completionAccepted.transition);

      await hookDispatcher.dispatch(operationId, 'onComplete', event, state?.host?.hooks);

      // Recall the user when a run finishes with a deliverable while they may be
      // away (push / inbox). Fires on every success-like terminal β€” `done` plus
      // the step/cost caps, which still produce a reply worth recalling β€” to
      // match the desktop completion notification, which notifies on any clean
      // (non-abort, non-error) terminal. Fire-and-forget through the
      // `@/business` slot; the default implementation is a no-op. Sub-agent /
      // group-member completions are internal steps of a parent run, never a
      // user-facing recall β€” in-group members carry `orchestrationRole: 'member'`
      // WITHOUT `isSubAgent` (see execAgentMember), so guard both. The
      // remaining condition β€” only interactive chat runs recall the user β€”
      // lives in recallUserOnCompletion.
      //
      // Agent Share visitor runs execute AS the creator, so this `userId`-scoped
      // recall would otherwise reach the creator's push/inbox for every turn an
      // arbitrary link visitor completes β€” a visitor could spam the owner by
      // repeatedly running the shared agent, and the notification would deep-link
      // into a visitor topic (`topics.senderId`) that is deliberately excluded
      // from creator-facing surfaces.
      if (
        isSuccessLikeCompletionReason(reason) &&
        runOrigin.lineage?.isSubAgent !== true &&
        runOrigin.lineage?.orchestrationRole !== 'member' &&
        !isAgentShareRun(state)
      ) {
        void this.recallUserOnCompletion(operationId, event, runOrigin).catch((error) =>
          log('[%s] Completion notification failed (non-fatal): %O', operationId, error),
        );
      }

      const settledTopicId = typeof runOrigin.topicId === 'string' ? runOrigin.topicId : undefined;
      if (
        this.onTopicRunSettled &&
        settledTopicId &&
        isTopicRunSettledReason(reason) &&
        !runOrigin.lineage?.parentOperationId &&
        runOrigin.lineage?.isSubAgent !== true &&
        runOrigin.lineage?.orchestrationRole !== 'member' &&
        !runOrigin.signal &&
        !isAgentShareRun(state)
      ) {
        const onTopicRunSettled = this.onTopicRunSettled;
        after(() => onTopicRunSettled({ operationId, topicId: settledTopicId }));
      }

      // Delivery checker: on a successful completion, run the confirmed verify
      // plan against the deliverable. Fire-and-forget and self-guarded β€” a run
      // without an opted-in plan is a no-op, and failures never affect the run.
      if (reason === 'done') {
        // The task's verify plan is instantiated fire-and-forget at run start; a
        // fast or no-op run can reach completion before it settles. Await the
        // in-flight instantiation (if any) so the gate sees the confirmed plan
        // instead of racing past it and leaving a planned run with no results/card.
        await this.verifyPlanInstantiations.get(operationId);

        const messages: any[] = Array.isArray(state?.messages) ? state.messages : [];
        const firstUserMessage = messages.find((m) => m?.role === 'user');
        const goal = firstUserMessage
          ? (extractTextFromMessageContent(firstUserMessage.content) ?? '')
          : '';
        // Surface the delivery-checker card first (a role='verify' message that
        // renders the run's plan + results). Awaited before verification so
        // auto-repair can persist its failure feedback onto this card (the
        // VerifyMessageProcessor then surfaces it into the repair run's context).
        // Self-guarded β€” failures never affect the run.
        await this.createVerifyMessage(
          operationId,
          metadata?.assistantMessageId,
          runOrigin.userId || this.userId,
        );
        // Judging is minutes of LLM calls and must not ride this request:
        // entering `verifying` is a durable write, so a gate cut off with the
        // request (proxy timeout, function limit) leaves the round claimed and
        // never judged. The gate runs in its own QStash delivery instead, which
        // also collapses a producer's retried completions into one gate.
        await scheduleVerifyOnCompletion(
          this.serverDB,
          runOrigin.userId || this.userId,
          {
            deliverable: event.lastAssistantContent ?? '',
            goal,
            operationId,
          },
          this.workspaceId,
        );
      }

      if (reason === 'error' || reason === 'interrupted') {
        after(async () => {
          const userId = runOrigin.userId || this.userId;
          await settleFailedRepair(this.serverDB, userId, operationId, this.workspaceId);
          // A task run that failed before delivering leaves the round opened at
          // its start with nothing to verify. When planning is still under way,
          // `instantiateVerifyPlanOnStart` releases the round once it lands.
          await releaseUndeliveredRound(this.serverDB, userId, operationId, this.workspaceId);
        });
      }

      // Register entity files edited this round as `file` Works. On the
      // gateway/queue path this already ran BEFORE the terminal snapshot (see
      // `registerFileWorks`) and no-ops via the state marker; here it is the
      // backstop for every other terminal path (in-process runtime, hetero
      // completions, already-terminal early exits). Guarded on success-LIKE
      // reasons, not `done` alone: a run stopped by a step/cost cap still
      // produced its edits and persists as status='done'.
      //
      // `waiting_for_human` deliberately does NOT register: the park is not a
      // successful deliverable boundary. The fresh approval continuation is
      // built from the complete authoritative history and performs the real
      // terminal scan. Registering here would freeze pre-approval file content
      // as a completed Work before the user decision has run.
      if (isSuccessLikeCompletionReason(reason)) {
        await this.registerFileWorks(operationId, state);
      }

      if (reason === 'error') {
        await hookDispatcher.dispatch(operationId, 'onError', event, state?.host?.hooks);

        const assistantMessageId =
          state?.error && !options?.skipErrorMessageWrite
            ? await this.resolveErrorMessageId(operationId, metadata, runOrigin)
            : undefined;
        if (assistantMessageId && state?.error && !options?.skipErrorMessageWrite) {
          // Preserve the semantic error type written by the runtime. Rebuilding
          // this as a generic AgentRuntimeError would lose UI routing data such
          // as quota context and force the client into the fallback card.
          const messageError = formatErrorForState(state.error);
          const errorMessage =
            this.extractErrorMessage(messageError) ||
            this.extractErrorMessage(state.error) ||
            String(state.error);
          try {
            await this.messageModel.update(assistantMessageId, {
              error: {
                ...messageError,
                body: messageError.body ?? { message: errorMessage },
                message: errorMessage,
              },
            });
          } catch (updateError) {
            log(
              '[%s] Failed to update assistant message with error (non-fatal): %O',
              operationId,
              updateError,
            );
          }
        }
      }
    } catch (error) {
      if (
        error instanceof CriticalHookDeliveryError ||
        error instanceof CriticalAgentInterventionPersistenceError
      ) {
        // A queue retry may run in this same process (local callback / warm
        // worker). Keep the in-memory registration until that lifecycle really
        // settles; queue mode can additionally reconstruct from host.hooks.
        shouldRetainHooksForRetry = true;
        throw error;
      }
      log('[%s] Hook dispatch error (non-fatal): %O', operationId, error);
    } finally {
      // Keep hooks registered across an async-tool park so the eventual resume
      // (same operationId) can still fire onComplete/onError. Critical delivery
      // failures likewise keep them until the provider redelivery settles.
      if (!isAsyncToolPark && !shouldRetainHooksForRetry) {
        hookDispatcher.unregister(operationId);
        // The instantiation has settled (awaited above) or this op never opted in
        // β€” drop the entry so the map doesn't grow across the service's lifetime.
        // Kept across an async-tool park: the op resumes under the same id.
        this.verifyPlanInstantiations.delete(operationId);
      }
    }
  }

  private async notifyPendingAgentIntervention(operationId: string, state: any): Promise<void> {
    return notifyPendingAgentIntervention(this.deps(), operationId, state);
  }

  private async recallUserOnCompletion(
    operationId: string,
    event: { agentId?: string; duration?: number; lastAssistantContent?: string; topicId?: string },
    runOrigin: { trigger?: unknown; userId?: string } | undefined,
  ): Promise<void> {
    return recallUserOnCompletion(this.deps(), operationId, event, runOrigin);
  }

  private async recoverLastAssistantContent(
    operationId: string,
    assistantMessageId: string | undefined,
    userId: string,
    topicId?: string,
  ): Promise<string | undefined> {
    return recoverLastAssistantContent(
      this.deps(),
      operationId,
      assistantMessageId,
      userId,
      topicId,
    );
  }

  private async resolveErrorMessageId(
    operationId: string,
    metadata: { assistantMessageId?: string } | undefined,
    runOrigin: { topicId?: string; userId?: string },
  ): Promise<string | undefined> {
    return resolveErrorMessageId(this.deps(), operationId, metadata, runOrigin);
  }

  private buildLifecycleEvent(operationId: string, state: any, reason: string) {
    return buildLifecycleEvent(this.userId, operationId, state, reason);
  }
}