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.

37.0 KB
Raw Blame
import { nanoid } from '@lobechat/utils';
import { toast } from '@lobehub/ui';
import debug from 'debug';
import { t } from 'i18next';
import { produce } from 'immer';

import { topicService } from '@/services/topic';
import { type ChatStore } from '@/store/chat/store';
import { type MessageMapKeyInput } from '@/store/chat/utils/messageMapKey';
import { messageMapKey } from '@/store/chat/utils/messageMapKey';
import { topicMapKey } from '@/store/chat/utils/topicMapKey';
import { getHomeStoreState } from '@/store/home';
import { type StoreSetter } from '@/store/types';
import { type ChatTopicStatus } from '@/types/topic';
import { setNamespace } from '@/utils/storeDebug';

import { operationSelectors } from './selectors';
import {
  type AfterCompletionCallback,
  AI_RUNTIME_OPERATION_TYPES,
  isClientHeldQueuedMessage,
  isSendableQueuedMessage,
  type Operation,
  type OperationCancelContext,
  type OperationContext,
  type OperationFilter,
  type OperationMetadata,
  type OperationStatus,
  type OperationType,
  type QueuedMessage,
} from './types';

const n = setNamespace('operation');
const MARK_READ_RETRY_BASE_MS = 1000;

/** A network failure (no HTTP status) or a 5xx may pass on retry; a 4xx will not. */
const isRetryableWriteError = (error: unknown) => {
  const status = (error as { data?: { httpStatus?: unknown } } | null)?.data?.httpStatus;
  return typeof status !== 'number' || status >= 500;
};

const log = debug('lobe-store:operation');

const isSameNullableContextValue = (left?: string | null, right?: string | null): boolean =>
  (left ?? null) === (right ?? null);

/**
 * Operation Actions
 */

type Setter = StoreSetter<ChatStore>;
export const operationActions = (set: Setter, get: () => ChatStore, _api?: unknown) =>
  new OperationActionsImpl(set, get, _api);

export class OperationActionsImpl {
  readonly #get: () => ChatStore;
  readonly #set: Setter;

  constructor(set: Setter, get: () => ChatStore, _api?: unknown) {
    void _api;
    this.#set = set;
    this.#get = get;
  }

  /**
   * Abandons unclaimed automatic recovery when a conversation changes branches.
   *
   * Use when:
   * - Either conversation surface selects another persisted reply.
   *
   * Expects:
   * - The full message context, including thread and share scopes.
   *
   * Returns:
   * - No value; unrelated conversations and explicit Retry remain unchanged.
   *
   * Call stack:
   *
   * switchMessageBranch (conversation or chat store)
   *   -> {@link OperationActionsImpl.revokeHeteroAutoRetry}
   *     -> {@link OperationActionsImpl.updateOperationMetadata}
   */
  revokeHeteroAutoRetry = (context: MessageMapKeyInput): void => {
    for (const operation of operationSelectors.getOperationsByContext(context)(this.#get())) {
      if (operation.metadata.heteroAutoRetryAvailable) {
        this.updateOperationMetadata(operation.id, { heteroAutoRetryAvailable: false });
      }
    }
  };

  internal_getConversationContext = (reference?: {
    context?: MessageMapKeyInput;
    operationId?: string;
  }): MessageMapKeyInput => {
    if (reference?.context) return reference.context;

    if (reference?.operationId) {
      const operation = this.#get().operations[reference.operationId];
      if (!operation) {
        // The op was already cleaned up (e.g. completed CC turn whose
        // runtime_end fired and was GC'd 30s later), but a late caller
        // — typically a long-lived intervention surface — still carries
        // the opId. Throwing here would tear down the optimistic write
        // and any follow-up IPC the caller was about to perform, so we
        // degrade to the global-state fallback and log loudly.
        log(
          '[internal_getConversationContext] WARNING: Operation not found, falling back to global state: %s',
          reference.operationId,
        );
        console.warn(
          '[internal_getConversationContext] operation not found, using global state:',
          reference.operationId,
        );
      } else {
        const { agentId, topicId, threadId, scope, groupId, documentId } = operation.context;
        log(
          '[internal_getConversationContext] get from operation %s: agentId=%s, topicId=%s, threadId=%s, scope=%s, groupId=%s, documentId=%s',
          reference.operationId,
          agentId,
          topicId,
          threadId,
          scope,
          groupId,
          documentId,
        );
        // Spread the whole operation context so every bucket-key field carries
        // through — notably `documentId` (page-scoped optimistic writes resolve
        // to the same `page_<agent>_<documentId>` bucket the editor reads from,
        // not `page_<agent>_new`) and `subAgentId` (group_agent scope's
        // subTopicId). Only agentId needs the non-null assertion.
        return { ...operation.context, agentId: agentId! };
      }
    }

    // Fallback to global state
    const agentId = this.#get().activeAgentId;
    const groupId = this.#get().activeGroupId;
    const topicId = this.#get().activeTopicId;
    const threadId = this.#get().activeThreadId;
    log('[internal_getConversationContext] use global state: ', {
      agentId,
      topicId,
      threadId,
      groupId,
    });
    return { agentId, topicId, threadId, groupId };
  };

  startOperation = (params: {
    context?: Partial<OperationContext>;
    description?: string;
    label?: string;
    metadata?: Partial<OperationMetadata>;
    operationId?: string;
    parentOperationId?: string;
    type: OperationType;
  }): { abortController: AbortController; operationId: string } => {
    const {
      type,
      context: partialContext,
      parentOperationId,
      label,
      description,
      metadata,
      operationId: customOperationId,
    } = params;

    const operationId = customOperationId || `op_${nanoid()}`;

    // If parent operation exists and context is not fully provided, inherit from parent
    let context: OperationContext = partialContext || {};
    const parentOp = parentOperationId ? this.#get().operations[parentOperationId] : undefined;
    const inheritedTurnStartTime = parentOp?.metadata.turnStartTime;

    if (parentOp) {
      // Inherit parent's context, allow partial override
      context = { ...parentOp.context, ...partialContext };
      log('[startOperation] inherit context from parent %s: %o', parentOperationId, context);
    }

    log('[startOperation] create operation %s (type=%s, context=%o)', operationId, type, context);

    const abortController = new AbortController();
    const now = Date.now();

    const operation: Operation = {
      id: operationId,
      type,
      status: 'running',
      context,
      abortController,
      metadata: {
        startTime: now,
        ...(inheritedTurnStartTime === undefined ? {} : { turnStartTime: inheritedTurnStartTime }),
        ...metadata,
      },
      parentOperationId,
      childOperationIds: [],
      label,
      description,
    };

    this.#set(
      produce((state: ChatStore) => {
        // Add to operations map
        state.operations[operationId] = operation;

        // Update type index
        if (!state.operationsByType[type]) {
          state.operationsByType[type] = [];
        }
        state.operationsByType[type].push(operationId);

        // Update message index (if messageId exists)
        if (context.messageId) {
          if (!state.operationsByMessage[context.messageId]) {
            state.operationsByMessage[context.messageId] = [];
          }
          state.operationsByMessage[context.messageId].push(operationId);

          // Auto-associate message with this operation (most granular)
          // This allows tools to access the correct AbortController via messageOperationMap
          state.messageOperationMap[context.messageId] = operationId;
        }

        // Update context index (if agentId exists)
        if (context.agentId) {
          const contextKey = messageMapKey(context as MessageMapKeyInput);
          if (!state.operationsByContext[contextKey]) {
            state.operationsByContext[contextKey] = [];
          }
          state.operationsByContext[contextKey].push(operationId);
        }

        // Update parent's childOperationIds
        if (parentOperationId && state.operations[parentOperationId]) {
          if (!state.operations[parentOperationId].childOperationIds) {
            state.operations[parentOperationId].childOperationIds = [];
          }
          state.operations[parentOperationId].childOperationIds!.push(operationId);
        }
      }),
      false,
      n(`startOperation/${type}/${operationId}`),
    );

    // Periodically cleanup old completed operations
    // Only cleanup for top-level operations (no parent) to avoid excessive cleanup calls
    if (!parentOperationId) {
      // Clean up operations completed more than 30 seconds ago
      this.#get().cleanupCompletedOperations(30_000);
    }

    return { operationId, abortController };
  };

  updateOperationMetadata = (operationId: string, metadata: Partial<OperationMetadata>): void => {
    const operation = this.#get().operations[operationId];
    if (metadata.isAborting) {
      log(
        '[updateOperationMetadata] Setting isAborting=true for operation %s (type=%s)',
        operationId,
        operation?.type,
      );
    }

    this.#set(
      produce((state: ChatStore) => {
        const operation = state.operations[operationId];
        if (!operation) return;

        operation.metadata = {
          ...operation.metadata,
          ...metadata,
        };
      }),
      false,
      n(`updateOperationMetadata/${operationId}`),
    );
  };

  updateOperationStatus = (
    operationId: string,
    status: OperationStatus,
    metadata?: Partial<OperationMetadata>,
  ): void => {
    this.#set(
      produce((state: ChatStore) => {
        const operation = state.operations[operationId];
        if (!operation) return;

        operation.status = status;

        if (metadata) {
          operation.metadata = {
            ...operation.metadata,
            ...metadata,
          };
        }
      }),
      false,
      n(`updateOperationStatus/${operationId}/${status}`),
    );
  };

  updateOperationProgress = (operationId: string, current: number, total?: number): void => {
    this.#set(
      produce((state: ChatStore) => {
        const operation = state.operations[operationId];
        if (!operation) return;

        operation.metadata.progress = {
          current,
          total: total ?? operation.metadata.progress?.total ?? current,
          percentage: total ? Math.round((current / total) * 100) : undefined,
        };
      }),
      false,
      n(`updateOperationProgress/${operationId}`),
    );
  };

  completeOperation = (operationId: string, metadata?: Partial<OperationMetadata>): void => {
    const operation = this.#get().operations[operationId];
    // Late stream/session callbacks cannot overwrite the persisted terminal
    // outcome (or reset its duration) after snapshot reconciliation.
    if (operation?.metadata.terminalReconciled) return;
    if (operation) {
      log(
        '[completeOperation] operation %s (type=%s) completed, duration=%dms',
        operationId,
        operation.type,
        Date.now() - operation.metadata.startTime,
      );
    }

    this.#set(
      produce((state: ChatStore) => {
        const operation = state.operations[operationId];
        if (!operation) return;

        const now = Date.now();

        // Don't override cancelled status - preserve user interruption state
        if (operation.status !== 'cancelled') {
          operation.status = 'completed';
        }

        operation.metadata.endTime = now;
        operation.metadata.duration = now - operation.metadata.startTime;

        if (metadata) {
          operation.metadata = {
            ...operation.metadata,
            ...metadata,
          };
        }
      }),
      false,
      n(`completeOperation/${operationId}`),
    );

    if (
      operation &&
      AI_RUNTIME_OPERATION_TYPES.includes(operation.type) &&
      operation.status !== 'completed'
    ) {
      const state = this.#get();
      const { agentId, topicId } = operation.context;
      if (!agentId || !topicId) return;
      // A run that finishes after the user switched away must still persist to
      // its own conversation, so rebuild the operation's context with a definite
      // agent/topic: `OperationContext` exposes them as optional.
      state.internal_settleTranscript({ ...operation.context, agentId, topicId });
    }
  };

  getOperationAbortSignal = (operationId: string): AbortSignal => {
    const operation = this.#get().operations[operationId];
    if (!operation) {
      throw new Error(`[getOperationAbortSignal] Operation not found: ${operationId}`);
    }
    return operation.abortController.signal;
  };

  onOperationCancel = (
    operationId: string,
    handler: (context: OperationCancelContext) => void | Promise<void>,
  ): void => {
    this.#set(
      produce((state: ChatStore) => {
        const operation = state.operations[operationId];
        if (!operation) {
          log('[onOperationCancel] WARNING: Operation not found: %s', operationId);
          return;
        }

        operation.onCancelHandler = handler;
        log(
          '[onOperationCancel] registered cancel handler for %s (type=%s)',
          operationId,
          operation.type,
        );
      }),
      false,
      n(`onOperationCancel/${operationId}`),
    );
  };

  registerAfterCompletionCallback = (
    operationId: string,
    callback: AfterCompletionCallback,
  ): void => {
    this.#set(
      produce((state: ChatStore) => {
        const operation = state.operations[operationId];
        if (!operation) {
          log('[registerAfterCompletionCallback] WARNING: Operation not found: %s', operationId);
          return;
        }

        // Initialize runtimeHooks if not exists
        if (!operation.metadata.runtimeHooks) {
          operation.metadata.runtimeHooks = {};
        }

        // Initialize afterCompletionCallbacks array if not exists
        if (!operation.metadata.runtimeHooks.afterCompletionCallbacks) {
          operation.metadata.runtimeHooks.afterCompletionCallbacks = [];
        }

        // Add callback to array
        operation.metadata.runtimeHooks.afterCompletionCallbacks.push(callback);

        log(
          '[registerAfterCompletionCallback] registered callback for %s (type=%s), total callbacks: %d',
          operationId,
          operation.type,
          operation.metadata.runtimeHooks.afterCompletionCallbacks.length,
        );
      }),
      false,
      n(`registerAfterCompletionCallback/${operationId}`),
    );
  };

  /**
   * Cancels an operation and reports whether its transport shutdown was confirmed.
   *
   * Use when:
   * - A caller needs to stop an operation and its child operations.
   * - A replacement task must not start after a failed transport cancellation.
   *
   * Expects:
   * - `operationId` may already be absent or terminal; those states are safe no-ops.
   * - Registered cancel handlers reject when their underlying transport remains active.
   *
   * Returns:
   * - `true` when every cancellation handler settled successfully, otherwise `false`.
   */
  cancelOperation = async (
    operationId: string,
    reason: string = 'User cancelled',
  ): Promise<boolean> => {
    const operation = this.#get().operations[operationId];
    if (!operation) {
      log('[cancelOperation] operation not found: %s', operationId);
      return true;
    }

    // Skip if already cancelled or completed
    if (operation.status === 'cancelled' || operation.status === 'completed') {
      log('[cancelOperation] operation %s already %s, skipping', operationId, operation.status);
      return true;
    }

    log(
      '[cancelOperation] cancelling operation %s (type=%s), reason: %s',
      operationId,
      operation.type,
      reason,
    );

    // 1. Abort the operation (triggers AbortSignal for all async operations)
    try {
      operation.abortController.abort(reason);
    } catch {
      // Ignore abort errors
    }

    // 2. Set isAborting flag immediately for agent-runtime operations.
    // This ensures UI (loading button) responds instantly to user cancellation.
    // Applies to all AI runtime operation types so the UI transitions out of
    // loading right away without waiting for the process to fully terminate.
    if (AI_RUNTIME_OPERATION_TYPES.includes(operation.type)) {
      this.#get().updateOperationMetadata(operationId, { isAborting: true });
    }

    // 3. Call cancel handler if registered. The local state transition below
    // remains synchronous, while callers such as Send now can await the returned
    // promise before starting a replacement native writer.
    let cancelHandler = Promise.resolve(true);
    if (operation.onCancelHandler) {
      log('[cancelOperation] calling cancel handler for %s (type=%s)', operationId, operation.type);

      const cancelContext: OperationCancelContext = {
        operationId,
        type: operation.type,
        reason,
        metadata: operation.metadata,
      };

      // Start the handler without delaying the synchronous local state transition.
      // The returned cancellation promise still waits for this work so replacement
      // operations can coordinate with the underlying transport shutdown.
      try {
        cancelHandler = Promise.resolve(operation.onCancelHandler(cancelContext)).then(
          () => true,
          (err) => {
            log('[cancelOperation] cancel handler error for %s: %O', operationId, err);
            return false;
          },
        );
      } catch (err) {
        // Handle synchronous errors from handler
        log('[cancelOperation] cancel handler synchronous error for %s: %O', operationId, err);
        cancelHandler = Promise.resolve(false);
      }
    }

    // 4. Update status
    this.#set(
      produce((state: ChatStore) => {
        const op = state.operations[operationId];
        if (!op) return;

        const now = Date.now();
        op.status = 'cancelled';
        op.metadata.endTime = now;
        op.metadata.duration = now - op.metadata.startTime;
        op.metadata.cancelReason = reason;
      }),
      false,
      n(`cancelOperation/${operationId}`),
    );

    // 5. Cancel all child operations recursively
    let childCancellations: Promise<boolean>[] = [];
    if (operation.childOperationIds && operation.childOperationIds.length > 0) {
      log('[cancelOperation] cancelling %d child operations', operation.childOperationIds.length);
      childCancellations = operation.childOperationIds.map((childId) =>
        this.#get().cancelOperation(childId, 'Parent operation cancelled'),
      );
    }

    const confirmations = await Promise.all([cancelHandler, ...childCancellations]);
    const cancellationConfirmed = confirmations.every(Boolean);
    if (cancellationConfirmed) return true;

    // The native transport may still own resources even though the optimistic
    // UI transition above marked the operation cancelled. Restore the blocker
    // so another send cannot mistake an unconfirmed shutdown for an idle chat.
    this.#set(
      produce((state: ChatStore) => {
        const op = state.operations[operationId];
        if (!op || op.status !== 'cancelled' || op.metadata.cancelReason !== reason) return;

        op.status = 'running';
        delete op.metadata.isAborting;
        delete op.metadata.cancelReason;
        delete op.metadata.duration;
        delete op.metadata.endTime;
      }),
      false,
      n(`cancelOperationFailed/${operationId}`),
    );

    return false;
  };

  failOperation = (
    operationId: string,
    error: { code?: string; details?: any; message: string; type: string },
  ): void => {
    const operation = this.#get().operations[operationId];
    if (operation?.metadata.terminalReconciled) return;
    if (operation) {
      log(
        '[failOperation] operation %s (type=%s) failed: %s',
        operationId,
        operation.type,
        error.message,
      );
    }

    this.#set(
      produce((state: ChatStore) => {
        const operation = state.operations[operationId];
        if (!operation) return;

        const now = Date.now();
        operation.status = 'failed';
        operation.metadata.endTime = now;
        operation.metadata.duration = now - operation.metadata.startTime;
        operation.metadata.error = error;
      }),
      false,
      n(`failOperation/${operationId}`),
    );
  };

  cancelOperations = (filter: OperationFilter, reason: string = 'Batch cancelled'): string[] => {
    const operations = Object.values(this.#get().operations);
    const matchedIds: string[] = [];

    operations.forEach((op) => {
      if (op.status !== 'running') return;

      let matches = true;

      // Type filter
      if (filter.type) {
        const types = Array.isArray(filter.type) ? filter.type : [filter.type];
        matches = matches && types.includes(op.type);
      }

      // Status filter
      if (filter.status) {
        const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
        matches = matches && statuses.includes(op.status);
      }

      // Context filters
      if (filter.agentId !== undefined) {
        matches = matches && op.context.agentId === filter.agentId;
      }
      if (filter.topicId !== undefined) {
        matches = matches && isSameNullableContextValue(op.context.topicId, filter.topicId);
      }
      if (filter.messageId !== undefined) {
        matches = matches && op.context.messageId === filter.messageId;
      }
      if (filter.threadId !== undefined) {
        matches = matches && isSameNullableContextValue(op.context.threadId, filter.threadId);
      }
      if (filter.groupId !== undefined) {
        matches = matches && isSameNullableContextValue(op.context.groupId, filter.groupId);
      }
      if (filter.scope !== undefined) {
        // Match message-map scope inference, while keeping explicit sub-agent
        // scopes separate: sharing a display bucket does not authorize Stop.
        const scope =
          op.context.scope ??
          (op.context.groupId ? 'group' : op.context.threadId ? 'thread' : 'main');
        matches = matches && scope === filter.scope;
      }
      if (filter.isNew !== undefined) {
        matches = matches && (op.context.isNew ?? false) === filter.isNew;
      }

      if (matches) {
        matchedIds.push(op.id);
      }
    });

    // Cancel all matched operations
    void Promise.all(matchedIds.map((id) => this.#get().cancelOperation(id, reason)))
      .then((confirmations) => {
        if (confirmations.some((confirmed) => !confirmed)) {
          toast.error(t('backgroundActivity.stopFailed', { ns: 'chat' }));
        }
      })
      .catch((error: unknown) => {
        log('[cancelOperations] cancellation error: %O', error);
        toast.error(t('backgroundActivity.stopFailed', { ns: 'chat' }));
      });

    return matchedIds;
  };

  cancelAllOperations = (reason: string = 'Cancel all operations'): void => {
    const operations = Object.values(this.#get().operations);

    operations.forEach((op) => {
      if (op.status === 'running') {
        this.#get().cancelOperation(op.id, reason);
      }
    });
  };

  cleanupCompletedOperations = (olderThan: number = 60_000): number => {
    // Default: cleanup operations completed more than 1 minute ago
    const now = Date.now();

    // Collect operations to delete first
    const operationsToDelete: string[] = [];
    Object.values(this.#get().operations).forEach((op) => {
      const isCompleted =
        op.status === 'completed' || op.status === 'cancelled' || op.status === 'failed';
      const isOld = op.metadata.endTime && now - op.metadata.endTime > olderThan;

      if (isCompleted && isOld) {
        operationsToDelete.push(op.id);
      }
    });

    if (operationsToDelete.length === 0) return 0;

    this.#set(
      produce((state: ChatStore) => {
        // Delete operations and update indexes
        operationsToDelete.forEach((operationId) => {
          const op = state.operations[operationId];
          if (!op) return;

          // Remove from operations map
          delete state.operations[operationId];

          // Remove from type index
          const typeIndex = state.operationsByType[op.type];
          if (typeIndex) {
            state.operationsByType[op.type] = typeIndex.filter((id) => id !== operationId);
          }

          // Remove from message index
          if (op.context.messageId) {
            const msgIndex = state.operationsByMessage[op.context.messageId];
            if (msgIndex) {
              state.operationsByMessage[op.context.messageId] = msgIndex.filter(
                (id) => id !== operationId,
              );
            }
          }

          // Remove from context index
          if (op.context.agentId) {
            const contextKey = messageMapKey(op.context as MessageMapKeyInput);
            const contextIndex = state.operationsByContext[contextKey];
            if (contextIndex) {
              state.operationsByContext[contextKey] = contextIndex.filter(
                (id) => id !== operationId,
              );
            }
          }

          // Remove from parent's childOperationIds
          if (op.parentOperationId && state.operations[op.parentOperationId]) {
            const parent = state.operations[op.parentOperationId];
            if (parent.childOperationIds) {
              parent.childOperationIds = parent.childOperationIds.filter(
                (id) => id !== operationId,
              );
            }
          }

          // Remove EVERY messageOperationMap entry pointing to this opId.
          // Assistant + tool messages from the same turn often map to the
          // same operation; the previous `find` + single-delete left
          // dangling references behind, which `submitHeteroIntervention`
          // later read back as a stale opId and threw on lookup.
          for (const [messageId, opId] of Object.entries(state.messageOperationMap)) {
            if (opId === operationId) {
              delete state.messageOperationMap[messageId];
            }
          }
        });
      }),
      false,
      n(`cleanupCompletedOperations/count=${operationsToDelete.length}`),
    );

    log('[cleanupCompletedOperations] cleaned up %d operations', operationsToDelete.length);
    return operationsToDelete.length;
  };

  associateMessageWithOperation = (messageId: string, operationId: string): void => {
    this.#set(
      produce((state: ChatStore) => {
        // Update messageOperationMap (for single operation lookup)
        state.messageOperationMap[messageId] = operationId;

        // Update operationsByMessage index (for multiple operations lookup)
        if (!state.operationsByMessage[messageId]) {
          state.operationsByMessage[messageId] = [];
        }
        if (!state.operationsByMessage[messageId].includes(operationId)) {
          state.operationsByMessage[messageId].push(operationId);
        }
      }),
      false,
      n(`associateMessageWithOperation/${messageId}/${operationId}`),
    );
  };

  /**
   * Mark a topic as having an unread completed generation by persisting
   * `status: 'unread'`. Skipped when the user is already viewing the topic, or
   * for the default (no-topic) conversation which has no persisted row.
   *
   * The write goes through `updateTopicStatus`, which optimistically patches the
   * in-memory topic map (so the sidebar dot lights up instantly for the active
   * agent) and persists fire-and-forget. After it persists we refresh the home
   * sidebar list so the cross-agent unread badge updates even for agents whose
   * topics aren't loaded on this client.
   */
  markTopicUnread = ({
    agentId,
    groupId,
    topicId,
  }: {
    agentId?: string;
    groupId?: string | null;
    topicId?: string | null;
  }): void => {
    if (!topicId) return;
    if (this.#get().activeTopicId === topicId) return;

    void this.#get()
      .updateTopicStatus?.({
        agentId,
        groupId: groupId ?? undefined,
        status: 'unread',
        topicId,
      })
      ?.then(() => {
        void getHomeStoreState().refreshAgentList?.();
      });
  };

  /**
   * Clear a topic's unread mark by flipping `status: 'unread'` back to 'active'.
   * Invoked when the user opens the topic.
   *
   * `status` is the caller's own copy of the topic's status, for surfaces that
   * list topics outside `topicDataMap` (the project sidebar); without it the
   * status is read from the loaded topic buckets. That copy may be stale (a
   * persisted list, another tab), so the server write is conditional on the row
   * still being unread, and the local mirror never outranks a newer transition —
   * a run started since keeps its `running` status. A transient failure is
   * retried while the topic stays open, since the callers fire once per open.
   *
   * When the server reports the row is no longer unread, the local copy is the
   * stale one (e.g. a run's terminal `active` write landed after its `unread`
   * one). The row's actual status is read back and mirrored, because surfaces
   * like the project page never refetch the agent's topic list to correct it.
   */
  markTopicRead = async ({
    agentId,
    groupId,
    status,
    topicId,
  }: {
    agentId?: string;
    groupId?: string | null;
    status?: ChatTopicStatus | null;
    topicId?: string | null;
  }): Promise<void> => {
    if (!topicId) return;

    const key = topicMapKey({
      agentId: agentId ?? this.#get().activeAgentId,
      groupId: groupId ?? this.#get().activeGroupId,
    });
    const knownStatus =
      status === undefined
        ? this.#get().topicDataMap[key]?.items?.find((t) => t.id === topicId)?.status
        : status;
    if (knownStatus !== 'unread') return;

    const cleared = await this.#clearUnread(topicId);
    if (cleared === undefined) return;

    if (cleared === null) {
      const current = await topicService.getTopicDetail(topicId).catch(() => null);
      if (current?.status && current.status !== 'unread')
        this.#get().internal_settleTopicRead?.({ status: current.status, topicId });
      return;
    }

    this.#get().internal_settleTopicRead?.({ topicId });
    void getHomeStoreState().refreshAgentList?.();
  };

  /**
   * The conditional server clear, retried with backoff while the topic stays
   * open. A 4xx will not change on retry, so it ends the attempt. Resolves to
   * the cleared row, `null` when the server row was no longer unread, or
   * `undefined` when the write failed.
   */
  #clearUnread = async (topicId: string) => {
    for (let attempt = 0; ; attempt += 1) {
      try {
        return await topicService.markTopicRead(topicId);
      } catch (error) {
        if (!isRetryableWriteError(error)) {
          console.error('[markTopicRead] persist failed:', error);
          return undefined;
        }
        await new Promise((resolve) =>
          setTimeout(resolve, Math.min(MARK_READ_RETRY_BASE_MS * 2 ** attempt, 30_000)),
        );
        if (this.#get().activeTopicId !== topicId) return undefined;
      }
    }
  };
  // ━━━ Message Queue Actions ━━━

  /**
   * Enqueue a message for a conversation context.
   * If a hard interrupt, also cancel the running operation.
   */
  enqueueMessage = (
    contextKey: string,
    message: QueuedMessage,
    runningOperationId?: string,
  ): void => {
    log(
      '[enqueueMessage] contextKey=%s, messageId=%s, mode=%s',
      contextKey,
      message.id,
      message.interruptMode,
    );

    this.#set(
      produce((state: ChatStore) => {
        const queue = state.queuedMessages[contextKey] ?? [];
        queue.push(message);
        state.queuedMessages[contextKey] = queue;
      }),
      false,
      n(`enqueueMessage/${contextKey}`),
    );

    // Soft: a running Gateway run hands its turn back at the next step boundary
    // instead of finishing every remaining step before the follow-up starts.
    // One handed to the server queue is the server's to join or start.
    if (message.interruptMode === 'soft' && isClientHeldQueuedMessage(message))
      this.#get().internal_syncQueuedMessagesFlag(contextKey);

    // Hard interrupt: cancel the running operation
    if (message.interruptMode === 'hard' && runningOperationId) {
      const op = this.#get().operations[runningOperationId];
      if (op?.status === 'running') {
        log('[enqueueMessage] Hard interrupt, cancelling operation %s', runningOperationId);
        this.#get().cancelOperation(runningOperationId, 'hard_interrupt');
      }
    }
  };

  /**
   * Drain all queued messages for a context (atomic take-all).
   *
   * Takes nothing while the server holds any of them: it delivers those
   * itself, and the ones queued after them must not overtake them.
   */
  /**
   * Take the queue to send it. `limit` takes only that many from its head (a
   * queue sent one message per turn); the rest stays queued.
   */
  drainQueuedMessages = (contextKey: string, options?: { limit?: number }): QueuedMessage[] => {
    const queue = this.#get().queuedMessages[contextKey];
    if (!queue || queue.length === 0) return [];
    if (!queue.every(isClientHeldQueuedMessage)) return [];

    // A steer that may already be with the agent is never sent on its own.
    const sendable = queue.filter(isSendableQueuedMessage);
    const messages = options?.limit ? sendable.slice(0, options.limit) : sendable;
    if (messages.length === 0) return [];

    this.#set(
      produce((state: ChatStore) => {
        state.queuedMessages[contextKey] = queue.filter((message) => !messages.includes(message));
      }),
      false,
      n(`drainQueuedMessages/${contextKey}`),
    );

    log('[drainQueuedMessages] contextKey=%s, drained %d', contextKey, messages.length);
    return messages;
  };

  /** Put messages taken from a queue back at its head, in their order (a failed hand-over). */
  requeueQueuedMessages = (contextKey: string, messages: QueuedMessage[]): void => {
    if (messages.length === 0) return;

    this.#set(
      produce((state: ChatStore) => {
        state.queuedMessages[contextKey] = [
          ...messages,
          ...(state.queuedMessages[contextKey] ?? []),
        ];
      }),
      false,
      n(`requeueQueuedMessages/${contextKey}`),
    );
  };

  moveQueuedMessages = (fromContextKey: string, toContextKey: string): void => {
    if (fromContextKey === toContextKey) return;

    const queue = this.#get().queuedMessages[fromContextKey];
    if (!queue || queue.length === 0) return;

    this.#set(
      produce((state: ChatStore) => {
        const fromQueue = state.queuedMessages[fromContextKey];
        if (!fromQueue || fromQueue.length === 0) return;

        const toQueue = state.queuedMessages[toContextKey] ?? [];
        state.queuedMessages[toContextKey] = [...toQueue, ...fromQueue];
        state.queuedMessages[fromContextKey] = [];
      }),
      false,
      n(`moveQueuedMessages/${fromContextKey}/${toContextKey}`),
    );

    log(
      '[moveQueuedMessages] fromContextKey=%s, toContextKey=%s, moved %d',
      fromContextKey,
      toContextKey,
      queue.length,
    );
  };

  /** Put a queued message back where it was (e.g. after a failed Send now), keeping the order. */
  restoreQueuedMessage = (contextKey: string, message: QueuedMessage, index: number): void => {
    this.#set(
      produce((state: ChatStore) => {
        const queue = state.queuedMessages[contextKey] ?? [];
        queue.splice(Math.max(0, Math.min(index, queue.length)), 0, message);
        state.queuedMessages[contextKey] = queue;
      }),
      false,
      n(`restoreQueuedMessage/${contextKey}/${message.id}`),
    );
    if (isClientHeldQueuedMessage(message)) this.#get().internal_syncQueuedMessagesFlag(contextKey);
  };

  updateQueuedMessage = (
    contextKey: string,
    messageId: string,
    patch: Partial<
      Pick<
        QueuedMessage,
        | 'heldBehindOperationId'
        | 'localSteer'
        | 'serverDelivering'
        | 'serverDelivery'
        | 'serverRequest'
        | 'serverUnconfirmed'
      >
    >,
  ): void => {
    this.#set(
      produce((state: ChatStore) => {
        const message = state.queuedMessages[contextKey]?.find((m) => m.id === messageId);
        if (message) Object.assign(message, patch);
      }),
      false,
      n(`updateQueuedMessage/${contextKey}/${messageId}`),
    );
  };

  removeQueuedMessage = (contextKey: string, messageId: string): void => {
    this.#set(
      produce((state: ChatStore) => {
        const queue = state.queuedMessages[contextKey];
        if (!queue) return;
        const idx = queue.findIndex((m) => m.id === messageId);
        if (idx >= 0) queue.splice(idx, 1);
      }),
      false,
      n(`removeQueuedMessage/${contextKey}/${messageId}`),
    );

    // The last message only this client holds is gone: let the run finish its
    // turn normally.
    if (!this.#get().queuedMessages[contextKey]?.some(isClientHeldQueuedMessage))
      this.#get().internal_syncQueuedMessagesFlag(contextKey);
  };

  clearMessageQueue = (contextKey: string): void => {
    this.#set(
      produce((state: ChatStore) => {
        delete state.queuedMessages[contextKey];
      }),
      false,
      n(`clearMessageQueue/${contextKey}`),
    );

    this.#get().internal_syncQueuedMessagesFlag(contextKey);
  };
}

export type OperationActions = Pick<OperationActionsImpl, keyof OperationActionsImpl>;