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.

36.8 KB
Raw Blame
import type {
  LlmCancelData,
  LlmExecuteData,
  ToolExecuteData,
} from '@lobechat/agent-gateway-client';
import type { ChatMessageError } from '@lobechat/types';
import debug from 'debug';
import urlJoin from 'url-join';

import { sanitizeVisitorError } from '@/database/models/message';

import { shouldRecordGatewayError } from './gatewayErrorRecord';
import { projectGatewayEventData, toMirroredEvent } from './gatewayEventProjection';
import {
  buildPublicEndEventData,
  buildPublicInitEventData,
  type GatewayVisitorRedaction,
  resolveRedactionFromState,
  sanitizeGatewayEventData,
} from './gatewayVisitorRedaction';
import {
  getDefaultReasonDetail,
  type StreamChunkData,
  type StreamEvent,
} from './StreamEventManager';
import type {
  IStreamEventManager,
  LlmExecuteDispatchResult,
  PublishAgentRuntimeEndParams,
} from './types';

const log = debug('lobe-server:agent-runtime:gateway-notifier');

const POST_TIMEOUT = 5000; // 5s per request
const MAX_INFLIGHT = 20; // bounded concurrency

/**
 * Op-routing fields the Gateway persists from `init` (protocol v2 Β§3.2) so the
 * per-user hub can describe an operation in `op_lifecycle` without the client
 * knowing the op up front. Order is the wire order; nothing else is sent.
 */
const GATEWAY_INIT_META_KEYS = [
  'topicId',
  'threadId',
  'agentId',
  'groupId',
  'taskId',
  'scope',
  'parentOperationId',
  'mirrorToOperationId',
  'rootOperationId',
  // A heterogeneous CLI run can go silent through a long shell command; the
  // gateway's inactivity watchdog gives it a longer window.
  'heteroType',
] as const;

export type GatewayInitMeta = Partial<Record<(typeof GATEWAY_INIT_META_KEYS)[number], string>>;

/**
 * Pick the `init` metadata from whatever the caller already passed as the
 * initial state β€” no lookups. Callers differ in what they know
 * (`AgentRuntimeCoordinator` passes the Redis op metadata, hetero dispatch
 * passes `{ agentId, topicId, mirrorToOperationId, ... }`), so only string
 * fields that are actually present make it onto the wire. Returns `undefined`
 * when nothing applies so legacy inits carry no `meta` key at all.
 */
export const pickGatewayInitMeta = (initialState: unknown): GatewayInitMeta | undefined => {
  if (!initialState || typeof initialState !== 'object') return undefined;

  const source = initialState as Record<string, unknown>;
  const meta: GatewayInitMeta = {};

  for (const key of GATEWAY_INIT_META_KEYS) {
    const value = source[key];
    if (typeof value === 'string' && value) meta[key] = value;
  }

  return Object.keys(meta).length > 0 ? meta : undefined;
};

/**
 * Decorator that wraps an IStreamEventManager and additionally pushes events
 * to the Agent Gateway via HTTP. Runtime init is an awaited ordering barrier;
 * subsequent event delivery remains best-effort and mostly fire-and-forget.
 *
 * Redis SSE remains the primary event storage / subscription mechanism.
 * The Gateway is an additional push channel for WebSocket delivery.
 */
export interface GatewayStreamNotifierOptions {
  /**
   * Let `publishStreamEvent` return before its ordering-barrier pushes
   * (`stream_end`, `message_patch`) reach the gateway. Only for a caller that
   * calls `drainPushes` at every point its invocation can be frozen or handed
   * over β€” otherwise an unawaited push can be lost with the invocation.
   */
  deferPushes?: boolean;
  /**
   * Whether the client that started `operationId` handles `member_runtime_end`
   * (from persisted op metadata). Lets a queue worker that never ran the
   * supervisor's init still rename the member terminals it mirrors. Omitted β‡’
   * in-process knowledge only; unknown β‡’ the verbatim `agent_runtime_end`.
   */
  resolveAcceptsMemberRuntimeEnd?: (operationId: string) => Promise<boolean>;
}

export class GatewayStreamNotifier implements IStreamEventManager {
  private inflight = 0;

  /**
   * The gateway answered 404 for the LLM relay routes (self-hosted Go gateway,
   * an older Worker deployment): relay events go out as plain stream events.
   */
  private llmRelayRoutesMissing = false;

  /**
   * Gateway pushes issued for an operation that have not settled yet, and the
   * last ordering barrier among them.
   *
   * The step used to await the push of `stream_end` and `message_patch` so the
   * client could not apply a later event first. That put a gateway round trip
   * (~250ms, twice per step in production traces) on the path between two
   * steps. Ordering does not actually need the step to wait β€” it needs the
   * pushes to leave in order, which these two maps arrange:
   *
   *  - an ordinary push queues behind the last barrier, and otherwise runs
   *    concurrently with its neighbours, exactly as stream chunks always have;
   *  - a barrier push additionally waits for everything already in flight, so
   *    nothing issued before it can land after it.
   *
   * This is strictly stronger than the previous arrangement, where every push
   * that was not awaited β€” chunks included β€” could overtake one that was.
   */
  private pendingPushes = new Map<string, Set<Promise<void>>>();

  private pushBarriers = new Map<string, Promise<void>>();

  /**
   * `operationId β†’ mirrorOperationId`. When an operation declares a
   * `mirrorToOperationId` (an in-group broadcast/speak member pointing at its
   * supervisor op), every Gateway push for that operation is additionally
   * delivered to the mirror op's channel β€” so member streaming events ride down
   * the supervisor's single WebSocket instead of stranding on a per-member
   * channel nobody subscribes to (single-connection multiplexing).
   *
   * Two population paths, so this works both in-process AND across queue workers:
   *  - fast path: set at `publishAgentRuntimeInit` from the initial state (the
   *    in-memory runtime, and the process that created the op).
   *  - queue path: in `AGENT_RUNTIME_MODE=queue` the member's chunks are emitted
   *    by a QStash worker that never ran init for that op, so its map starts
   *    empty. `pushEvent` then lazily resolves the target from PERSISTED op
   *    metadata via `resolveMirrorTarget` (Redis) on the op's first event and
   *    caches it β€” converging the worker onto the same mapping.
   * Cleared at `publishAgentRuntimeEnd`.
   */
  private mirrorTargets = new Map<string, string>();
  /** Ops whose mirror target has been resolved (target found OR confirmed none). */
  private mirrorResolved = new Set<string>();
  /** In-flight resolutions, deduped per op so concurrent events share one read. */
  private mirrorResolving = new Map<string, Promise<string | undefined>>();

  /**
   * `mirror target op β†’ whether its client handles member_runtime_end`. Set from
   * the target's own init (fast path) or resolved once from persisted metadata
   * (queue path). A client released before the rename only retires a member
   * column on `agent_runtime_end`, so it keeps getting that. Cleared at the
   * target's `publishAgentRuntimeEnd`.
   */
  private memberEndTargets = new Map<string, Promise<boolean>>();

  /**
   * `operationId β†’ true` for confirmed shared-agent visitor
   * runs. `step_start` events carry neither `streamOwnerUserId` (only on
   * `agent_runtime_init`'s `initialState`) nor `finalState` (only on
   * `agent_runtime_end` / `step_complete`), so this per-operation entry is the
   * only signal `pushEvent` has for scrubbing `uiMessages` on those events.
   * Mirrors `mirrorTargets`'s two population paths:
   *  - fast path: set at `publishAgentRuntimeInit` / `publishAgentRuntimeEnd`
   *    from the state.
   *  - queue path: lazily resolved from persisted op metadata via
   *    `resolvePersistedShareVisitor`.
   * Cleared at `publishAgentRuntimeEnd`.
   */
  private shareVisitorOps = new Map<string, GatewayVisitorRedaction>();
  /** Ops whose share-visitor status has been resolved (confirmed share OR not). */
  private shareVisitorResolved = new Set<string>();
  /** In-flight resolutions, deduped per op so concurrent events share one read. */
  private shareVisitorResolving = new Map<string, Promise<GatewayVisitorRedaction>>();

  constructor(
    private inner: IStreamEventManager,
    private gatewayUrl: string,
    private serviceToken: string,
    /**
     * Resolves an op's persisted `mirrorToOperationId` (from op metadata). Lets a
     * queue worker β€” which never ran the op's init β€” still mirror its stream
     * events onto the supervisor channel. Omitted β‡’ in-process map only.
     */
    private resolveMirrorTarget?: (operationId: string) => Promise<string | undefined>,
    /**
     * Resolves whether an op is a share-visitor run (from `streamOwnerUserId`
     * on op metadata); `false` for a
     * normal run. Lets a queue worker β€” which never ran the op's init β€” still
     * scrub events for that op. Omitted β‡’ in-process map only (safe: init
     * always precedes events for the op it created).
     */
    private resolvePersistedShareVisitor?: (
      operationId: string,
    ) => Promise<GatewayVisitorRedaction>,
    private options: GatewayStreamNotifierOptions = {},
  ) {
    log('Gateway notifier initialized: %s', gatewayUrl);
  }

  // ─── Publish methods: delegate to inner + notify gateway ───

  async publishStreamEvent(
    operationId: string,
    event: Omit<StreamEvent, 'operationId' | 'timestamp'>,
  ): Promise<string> {
    const result = await this.inner.publishStreamEvent(operationId, event);
    const gatewayEvent = { ...event, operationId, timestamp: Date.now() };
    // `visible_output_end` may be published immediately after `stream_end`.
    // These two are ordering barriers so the client applies
    // stream_end.finalContent before visible_output_end, and the canonical
    // message patch before the following step_start / agent_runtime_end
    // revision check. Ordering comes from `issuePush`; waiting is only
    // skipped for a caller that drains before its invocation ends.
    const barrier = event.type === 'stream_end' || event.type === 'message_patch';
    const push = this.issuePush(operationId, gatewayEvent, { barrier });
    if (barrier && !this.options.deferPushes) await push;
    return result;
  }

  /** Storage-only: clients already received the removed entries live. */
  async deleteStreamEvents(operationId: string, eventIds: string[]): Promise<void> {
    return this.inner.deleteStreamEvents(operationId, eventIds);
  }

  async publishStreamChunk(
    operationId: string,
    stepIndex: number,
    chunkData: StreamChunkData,
  ): Promise<string> {
    const result = await this.inner.publishStreamChunk(operationId, stepIndex, chunkData);
    void this.issuePush(operationId, {
      data: chunkData,
      operationId,
      stepIndex,
      timestamp: Date.now(),
      type: 'stream_chunk',
    });
    return result;
  }

  async publishAgentRuntimeInit(operationId: string, initialState: any): Promise<string> {
    const result = await this.inner.publishAgentRuntimeInit(operationId, initialState);

    // Register the mirror target (if any) before the first event flows, so this
    // op's whole stream β€” including the events below β€” fans out to the
    // supervisor's channel too.
    const mirrorTo = initialState?.mirrorToOperationId;
    if (typeof mirrorTo === 'string' && mirrorTo && mirrorTo !== operationId) {
      this.mirrorTargets.set(operationId, mirrorTo);
      log('mirror registered: %s β†’ %s', operationId, mirrorTo);
    }
    if (initialState?.acceptsMemberRuntimeEnd === true) {
      this.memberEndTargets.set(operationId, Promise.resolve(true));
    }

    // Ordering barrier: a subscriber connects immediately after execAgent
    // returns and asks the Gateway for the operation's authoritative status.
    // If init is still fire-and-forget, that resume can win the race and report
    // a live heterogeneous/device run as terminal before its first producer
    // event arrives. httpPost intentionally swallows Gateway failures, so
    // awaiting it preserves best-effort semantics while preventing the normal
    // success path from exposing an operation before the Gateway knows it. Init
    // uses the non-lossy request lane: ordinary stream events may be dropped at
    // MAX_INFLIGHT, but dropping this control-plane barrier would recreate the
    // exact resume-before-init race under load.
    // Record share-visitor status up front (definitively known from
    // `initialState` here) so every later event for this op β€” including
    // `step_start`, which carries neither `streamOwnerUserId` nor `finalState`
    // β€” is sanitized without re-resolving.
    // `streamOwnerUserId` alone (without a readable `principal.actor.shareVisitor`)
    // still means a visitor is on the other end.
    if (resolveRedactionFromState(initialState)) this.shareVisitorOps.set(operationId, true);
    this.shareVisitorResolved.add(operationId);

    try {
      // The gateway DO requires the subscriber JWT's `sub` to equal the userId
      // registered here. `streamOwnerUserId` (shared-agent visitor runs) takes
      // precedence: the op executes as the creator, but only the visitor may
      // subscribe to its stream.
      // `meta` (protocol v2) lets the per-user hub describe the op in its
      // lifecycle feed; v1 gateways ignore the extra key.
      const meta = pickGatewayInitMeta(initialState);
      await this.httpPostAwait('/api/operations/init', {
        operationId,
        userId: initialState?.streamOwnerUserId || initialState?.userId || 'unknown',
        ...(meta ? { meta } : {}),
      });
    } catch (error) {
      log('Gateway /api/operations/init failed: %O', error);
    }

    void this.issuePush(
      operationId,
      {
        // Every run, not just share visitors: nothing on the other end reads this
        // event's data, while the raw `initialState` is the whole `AgentState` β€”
        // the LLM context plus the tool-set maps. See `buildPublicInitEventData`.
        data: buildPublicInitEventData(initialState),
        operationId,
        stepIndex: 0,
        timestamp: Date.now(),
        type: 'agent_runtime_init',
      },
      // The run's first event: everything else for this op queues behind it.
      { barrier: true },
    );

    return result;
  }

  async publishAgentRuntimeEnd(params: PublishAgentRuntimeEndParams): Promise<string> {
    const {
      operationId,
      stepIndex,
      finalState,
      messagePatchMode,
      messageRevision,
      reason,
      reasonDetail,
      uiMessages,
    } = params;
    const result = await this.inner.publishAgentRuntimeEnd(params);

    const endRedaction = resolveRedactionFromState(finalState);

    // `errorType`/`reasonDetail` both read `finalState.error` β€” the same
    // `formatErrorForState` shape `sanitizeVisitorError` projects for
    // `toVisitorMessage` β€” but land as SIBLINGS of `finalState` on
    // `endEventData` below, so `buildPublicEndEventData`'s wholesale
    // `finalState` drop does not touch them. For a share-visitor run, run the
    // SAME classification here rather than trusting the raw `reasonDetail` the
    // caller passed in: this Gateway push is the enforcement boundary
    // regardless of what upstream already computed from the unredacted error.
    const rawErrorType = finalState?.error?.type ?? finalState?.error?.errorType;
    const safeError = endRedaction
      ? sanitizeVisitorError(
          rawErrorType === undefined
            ? undefined
            : ({ message: finalState?.error?.message, type: rawErrorType } as ChatMessageError),
        )
      : undefined;
    const effectiveReasonDetail = endRedaction
      ? safeError?.message || getDefaultReasonDetail(undefined, reason)
      : reasonDetail || getDefaultReasonDetail(finalState, reason);
    const errorType = endRedaction ? safeError?.type : rawErrorType;

    // `finalState` already tells us definitively whether this is a share run,
    // so record it before pushing β€” covers the case where this runs in a
    // process that never saw the op's `publishAgentRuntimeInit` (queue mode)
    // without waiting on the async metadata resolver.
    if (endRedaction) this.shareVisitorOps.set(operationId, true);
    this.shareVisitorResolved.add(operationId);

    // Forward `uiMessages` to the gateway push channel so terminal-state
    // clients consuming /push-event get the canonical UIChatMessage[]
    // snapshot β€” the final step has no later step_start to carry a fresh
    // snapshot, so dropping it here would break the SoT contract.
    const endEventData = {
      errorType,
      // The gateway files errors on its board only when told to β€” the
      // user-side / board-worthy decision lives here, next to the error spec.
      ...(reason === 'error' && {
        recordError: shouldRecordGatewayError({
          error: finalState?.error,
          errorType: rawErrorType,
          provider: finalState?.modelRuntimeConfig?.provider,
        }),
      }),
      ...(!messagePatchMode && { finalState }),
      ...(messagePatchMode && { messagePatchMode: true, messageRevision }),
      reason,
      reasonDetail: effectiveReasonDetail,
      ...(uiMessages !== undefined && { uiMessages }),
    };

    // Terminal, so it is worth waiting for: the run is over and nothing else
    // will carry this event if the invocation is frozen before the push lands.
    // As a barrier it also flushes everything the run issued before it.
    await this.issuePush(
      operationId,
      {
        // Share-visitor runs must not receive the creator's raw AgentState
        // (world.userMemory / world.agent, systemRole,
        // userInterventionConfig, ...) over their WS channel β€” see
        // `buildPublicEndEventData`.
        data: endRedaction ? buildPublicEndEventData(endEventData) : endEventData,
        operationId,
        stepIndex,
        timestamp: Date.now(),
        type: 'agent_runtime_end',
      },
      { barrier: true },
    );

    // Terminal event has been forwarded (including any mirror); drop the mapping
    // so it can't leak across a reused operationId.
    this.mirrorTargets.delete(operationId);
    this.mirrorResolved.delete(operationId);
    this.mirrorResolving.delete(operationId);
    this.memberEndTargets.delete(operationId);
    this.shareVisitorOps.delete(operationId);
    this.shareVisitorResolved.delete(operationId);
    this.shareVisitorResolving.delete(operationId);

    return result;
  }

  /**
   * Request the client to execute a tool via Agent Gateway β†’ WebSocket.
   * Unlike the other push methods this is NOT fire-and-forget: callers rely
   * on the promise outcome to decide whether to block-await a result or
   * fall back to the interrupt-resume path. Rejects on HTTP error / timeout.
   */
  async sendToolExecute(operationId: string, data: ToolExecuteData): Promise<void> {
    log('sendToolExecute operation=%s toolCallId=%s', operationId, data.toolCallId);
    await this.httpPostAwait('/api/operations/tool-execute', { data, operationId });
  }

  /**
   * Tell the gateway that this operation is parked on a deferred tool, so its
   * inactivity watchdog stops reading the silence as a dead function.
   *
   * Best-effort and version-tolerant: a gateway that predates the route answers
   * 404, which resolves `false` β€” the caller then falls back to its own timeout
   * (the 30-minute watchdog is a hard ceiling there). Never throws: failing to
   * extend a watchdog must not fail the tool call that is parking.
   */
  async markOperationParked(
    operationId: string,
    params: { parkedUntil: number; reason?: string; stepIndex?: number },
  ): Promise<boolean> {
    try {
      const res = await this.httpPostResponse('/api/operations/park', {
        operationId,
        parkedUntil: params.parkedUntil,
        reason: params.reason ?? 'deferred_tool',
        stepIndex: params.stepIndex,
      });
      if (res.ok) return true;
      if (res.status === 404) {
        log('markOperationParked: gateway has no /park route (op=%s)', operationId);
        return false;
      }
      log('markOperationParked failed (op=%s): %d %s', operationId, res.status, res.body);
      return false;
    } catch (error) {
      log('markOperationParked failed (op=%s): %O', operationId, error);
      return false;
    }
  }

  /**
   * Hand one relayed LLM attempt to the user's device. The gateway delivers it
   * to the client that started the run (when connected) and keeps it
   * replayable until it is cancelled, closed or past its deadline.
   *
   * A gateway without the route (the self-hosted Go gateway, an older Worker
   * deployment) answers 404: the event then goes out as a plain stream event,
   * which those gateways broadcast to every subscriber β€” the backend's
   * per-call lease still keeps a single writer. Rejects on any other failure.
   */
  async sendLlmExecute(
    operationId: string,
    data: LlmExecuteData,
  ): Promise<LlmExecuteDispatchResult> {
    log('sendLlmExecute operation=%s callId=%s', operationId, data.callId);
    if (!this.llmRelayRoutesMissing) {
      const res = await this.httpPostResponse('/api/operations/llm-execute', { data, operationId });
      if (res.ok) {
        let delivered: number | undefined;
        try {
          delivered = (JSON.parse(res.body) as { delivered?: number }).delivered;
        } catch {
          // An older body shape; delivery is only diagnostics.
        }
        return { delivered, routed: true };
      }
      if (res.status !== 404) {
        throw new Error(`Gateway /api/operations/llm-execute returned ${res.status}: ${res.body}`);
      }
      this.llmRelayRoutesMissing = true;
    }

    await this.publishStreamEvent(operationId, {
      data,
      stepIndex: data.stepIndex,
      type: 'llm_execute',
    });
    return { routed: false };
  }

  /** Stop a relayed LLM attempt on every client (see {@link sendLlmExecute} for the fallback). */
  async sendLlmCancel(operationId: string, data: LlmCancelData & { stepIndex: number }) {
    log('sendLlmCancel operation=%s callId=%s', operationId, data.callId);
    const { stepIndex, ...cancel } = data;
    if (!this.llmRelayRoutesMissing) {
      const res = await this.httpPostResponse('/api/operations/llm-cancel', {
        data: cancel,
        operationId,
      });
      if (res.ok) return;
      if (res.status !== 404) {
        throw new Error(`Gateway /api/operations/llm-cancel returned ${res.status}`);
      }
      this.llmRelayRoutesMissing = true;
    }

    await this.publishStreamEvent(operationId, { data: cancel, stepIndex, type: 'llm_cancel' });
  }

  /**
   * The attempt is over: the gateway stops replaying its `llm_execute`.
   * Best effort β€” the gateway also drops it at the attempt's deadline.
   */
  async closeLlmCall(operationId: string, callId: string): Promise<void> {
    if (this.llmRelayRoutesMissing) return;
    try {
      const res = await this.httpPostResponse('/api/operations/llm-close', { callId, operationId });
      if (res.ok) return;
      if (res.status === 404) {
        this.llmRelayRoutesMissing = true;
        return;
      }
      log('closeLlmCall for %s (%s): gateway returned %d', operationId, callId, res.status);
    } catch (error) {
      log('closeLlmCall failed for %s (%s): %O', operationId, callId, error);
    }
  }

  /**
   * Open a one-shot relay channel (`llmcall:<userId>:…`): the gateway session a
   * single LLM call outside any run dispatches its `llm_execute` on. Only
   * `userId` may subscribe to it; the tab that makes the call already has.
   * Rejects when the gateway does not take it, so the call fails up front.
   */
  async openLlmRelayChannel(channel: string, userId: string): Promise<void> {
    await this.httpPostAwait('/api/operations/init', {
      meta: { scope: 'llm_call' },
      operationId: channel,
      userId,
    });
  }

  /** The one-shot channel's calls are over: end its session. Best effort. */
  async closeLlmRelayChannel(channel: string): Promise<void> {
    try {
      const res = await this.httpPostResponse('/api/operations/update-status', {
        operationId: channel,
        status: 'completed',
      });
      if (!res.ok) log('closeLlmRelayChannel %s: gateway returned %d', channel, res.status);
    } catch (error) {
      log('closeLlmRelayChannel failed for %s: %O', channel, error);
    }
  }

  // ─── Read / subscribe methods: delegate directly to inner ───

  async subscribeStreamEvents(
    operationId: string,
    lastEventId: string,
    onEvents: (events: StreamEvent[]) => void,
    signal?: AbortSignal,
  ): Promise<void> {
    return this.inner.subscribeStreamEvents(operationId, lastEventId, onEvents, signal);
  }

  async readEventsOnce(
    operationId: string,
    lastEventId?: string,
    blockMs?: number,
  ): Promise<{ events: StreamEvent[]; lastEventId: string }> {
    return this.inner.readEventsOnce(operationId, lastEventId, blockMs);
  }

  async getStreamHistory(operationId: string, count?: number): Promise<StreamEvent[]> {
    return this.inner.getStreamHistory(operationId, count);
  }

  async getStreamHistoryPage(operationId: string, cursor?: string, limit?: number) {
    return this.inner.getStreamHistoryPage(operationId, cursor, limit);
  }

  async cleanupOperation(operationId: string): Promise<void> {
    return this.inner.cleanupOperation(operationId);
  }

  async getActiveOperationsCount(): Promise<number> {
    return this.inner.getActiveOperationsCount();
  }

  async disconnect(): Promise<void> {
    return this.inner.disconnect();
  }

  // ─── Gateway HTTP helpers ───

  private async pushEvent(operationId: string, event: Record<string, unknown>): Promise<void> {
    // Resolve share-visitor status BEFORE building the sanitized payload β€” the
    // synchronous fast path (the common case: `publishAgentRuntimeInit` /
    // `publishAgentRuntimeEnd` always mark this resolved before calling
    // `pushEvent`) keeps this whole method's pre-`mirrorTargets.get` prefix
    // synchronous, preserving the ordering the mirror-cleanup-after-call
    // pattern in `publishAgentRuntimeEnd` relies on. Only a queue worker's
    // first event for an op it never initialized falls through to the async
    // `resolveShareVisitor` β€” see its fail-closed contract.
    const known = this.isShareVisitorKnown(operationId);
    const redaction = known === undefined ? await this.resolveShareVisitor(operationId) : known;

    // Mirror the Redis publisher's chokepoint β€” strip
    // `finalState.messages` + tool-set fields off the gateway WS push
    // payload too. The gateway forwards events verbatim to clients, and
    // downstream consumers don't read these fields, so carrying them
    // would re-introduce the same multi-megabyte serialization that
    // crashed the xadd path. step_complete state snapshots require the run
    // to opt into includeFinalState. Additionally, for a shared-agent visitor run,
    // drop `finalState` wholesale and scrub the rest β€” see
    // `sanitizeGatewayEventData`.
    const sanitizedEvent =
      event.data === undefined
        ? event
        : {
            ...event,
            data: projectGatewayEventData(
              sanitizeGatewayEventData(event.data, redaction, event.type),
              event.type,
            ),
          };
    const pushes: Promise<void>[] = [
      this.httpPost('/api/operations/push-event', {
        event: sanitizedEvent,
        operationId,
      }),
    ];

    // Single-connection multiplexing: also deliver to the mirror op's channel so
    // the event rides down that connection's WebSocket. The event payload keeps
    // its own `operationId`, which the client's event router uses to demux it
    // back to the right member column. Only the delivery channel changes.
    const mirrorTo = this.mirrorTargets.get(operationId);
    if (mirrorTo) {
      pushes.push(this.mirrorPush(mirrorTo, sanitizedEvent));
      await Promise.all(pushes);
      return;
    }
    // Queue worker: target not in the in-process map. Resolve it from persisted
    // metadata once, then mirror this (and future) events. Concurrent events for
    // the same op share one resolution and fire their mirror pushes in order.
    if (!this.mirrorResolved.has(operationId)) {
      pushes.push(
        this.resolveMirror(operationId).then(async (target) => {
          if (target) await this.mirrorPush(target, sanitizedEvent);
        }),
      );
    }

    await Promise.all(pushes);
  }

  /**
   * Hand a push to the gateway without making the caller wait for it. Returns a
   * promise that never rejects, so a failed push cannot poison the order it was
   * issued in.
   */
  private issuePush(
    operationId: string,
    event: Record<string, unknown>,
    options?: { barrier?: boolean },
  ): Promise<void> {
    const barrier = this.pushBarriers.get(operationId);
    const pending = this.pendingPushes.get(operationId) ?? new Set<Promise<void>>();
    const waitFor = options?.barrier
      ? Promise.allSettled([...pending, ...(barrier ? [barrier] : [])]).then(() => {})
      : (barrier ?? Promise.resolve());

    const settled = waitFor.then(() =>
      this.pushEvent(operationId, event).catch((error) => {
        log('Gateway push failed for %s (%s): %O', operationId, event.type, error);
      }),
    );

    pending.add(settled);
    this.pendingPushes.set(operationId, pending);
    void settled.then(() => {
      pending.delete(settled);
      if (pending.size === 0 && this.pendingPushes.get(operationId) === pending) {
        this.pendingPushes.delete(operationId);
      }
      if (this.pushBarriers.get(operationId) === settled) this.pushBarriers.delete(operationId);
    });

    if (options?.barrier) this.pushBarriers.set(operationId, settled);

    return settled;
  }

  /**
   * Wait for an operation's issued pushes to reach the gateway. The invocation
   * that produced them calls this before it can be frozen or handed over β€”
   * nothing else guarantees an unawaited push survives the end of a request.
   */
  async drainPushes(operationId: string): Promise<void> {
    // A settling barrier releases pushes queued behind it, so draining takes a
    // few rounds; bounded so a pathological producer cannot spin here.
    for (let round = 0; round < 5; round += 1) {
      const pending = this.pendingPushes.get(operationId);
      if (!pending?.size) return;
      await Promise.allSettled(pending);
    }
  }

  private async mirrorPush(mirrorTo: string, event: Record<string, unknown>): Promise<void> {
    const mirrored =
      event.type === 'agent_runtime_end' && (await this.acceptsMemberRuntimeEnd(mirrorTo))
        ? toMirroredEvent(event)
        : event;
    return this.httpPost('/api/operations/push-event', {
      event: mirrored,
      operationId: mirrorTo,
    });
  }

  private acceptsMemberRuntimeEnd(target: string): Promise<boolean> {
    let accepted = this.memberEndTargets.get(target);
    if (!accepted) {
      const resolve = this.options.resolveAcceptsMemberRuntimeEnd;
      accepted = resolve ? resolve(target).catch(() => false) : Promise.resolve(false);
      this.memberEndTargets.set(target, accepted);
    }
    return accepted;
  }

  /**
   * Resolve and cache an op's mirror target from persisted metadata. Returns the
   * target (cached in `mirrorTargets`) or undefined when the op has none. Deduped
   * so many concurrent events trigger a single metadata read.
   */
  private resolveMirror(operationId: string): Promise<string | undefined> {
    const cached = this.mirrorTargets.get(operationId);
    if (cached) return Promise.resolve(cached);
    if (this.mirrorResolved.has(operationId) || !this.resolveMirrorTarget) {
      return Promise.resolve(undefined);
    }
    let pending = this.mirrorResolving.get(operationId);
    if (!pending) {
      pending = this.resolveMirrorTarget(operationId)
        .then((target) => {
          this.mirrorResolved.add(operationId);
          this.mirrorResolving.delete(operationId);
          if (target && target !== operationId) {
            this.mirrorTargets.set(operationId, target);
            return target;
          }
          return undefined;
        })
        .catch(() => {
          this.mirrorResolving.delete(operationId);
          return undefined;
        });
      this.mirrorResolving.set(operationId, pending);
    }
    return pending;
  }

  /**
   * Synchronous share-visitor lookup. Returns `undefined` only when a queue
   * worker resolver is configured AND this op's status hasn't been resolved yet
   * (its first event, in a process that never ran its init) β€” the caller must
   * then fall back to the async {@link resolveShareVisitor}.
   *
   * Without a configured resolver (non-queue mode), an unresolved op is treated
   * as a normal run synchronously rather than going through the async path at
   * all β€” this keeps `pushEvent`'s common-case prefix fully synchronous,
   * matching `mirrorTargets.get`'s behavior and the `stream_end` await-ordering
   * contract on `publishStreamEvent`. It's also the correct default: without a
   * resolver this notifier only ever sees events for ops it initialized itself,
   * and init marks resolved synchronously before any event is pushed.
   */
  private isShareVisitorKnown(operationId: string): GatewayVisitorRedaction | undefined {
    const cached = this.shareVisitorOps.get(operationId);
    if (cached) return cached;
    if (this.shareVisitorResolved.has(operationId)) return false;
    if (!this.resolvePersistedShareVisitor) return false;
    return undefined;
  }

  /**
   * Resolve and cache whether an op is a shared-agent visitor run, from
   * persisted metadata (`streamOwnerUserId`). Only reached when a resolver is
   * configured and the op is still unresolved (see {@link isShareVisitorKnown}).
   * Deduped so concurrent events for the same unresolved op share one metadata
   * read.
   *
   * Fails closed: a resolution error treats the op as a share run rather than risk
   * leaking the creator's identity, and deliberately does NOT cache that
   * outcome as resolved, so the next event retries once the transient failure
   * clears.
   */
  private resolveShareVisitor(operationId: string): Promise<GatewayVisitorRedaction> {
    if (!this.resolvePersistedShareVisitor) return Promise.resolve(false);

    let pending = this.shareVisitorResolving.get(operationId);
    if (!pending) {
      pending = this.resolvePersistedShareVisitor(operationId)
        .then((resolved) => {
          this.shareVisitorResolved.add(operationId);
          this.shareVisitorResolving.delete(operationId);
          if (!resolved) return false;
          this.shareVisitorOps.set(operationId, true);
          return true;
        })
        .catch((error) => {
          this.shareVisitorResolving.delete(operationId);
          log('[%s] Share visitor resolution failed, failing closed: %O', operationId, error);
          return true;
        });
      this.shareVisitorResolving.set(operationId, pending);
    }
    return pending;
  }

  /**
   * POST that surfaces errors back to the caller (no swallow). Used for
   * request-response style pushes like tool_execute where the caller needs
   * to know whether the gateway accepted the request.
   */
  private async httpPostAwait(path: string, body: Record<string, unknown>): Promise<void> {
    const res = await this.httpPostResponse(path, body);
    if (!res.ok) {
      throw new Error(`Gateway ${path} returned ${res.status}: ${res.body}`);
    }
  }

  /**
   * POST and hand back the status and body, whatever the status. The body is
   * read under the same timeout as the request, so a gateway that sends
   * headers and then stalls cannot hang the caller. Rejects on network errors
   * and timeout.
   */
  private async httpPostResponse(
    path: string,
    body: Record<string, unknown>,
  ): Promise<{ body: string; ok: boolean; status: number }> {
    const controller = new AbortController();
    const timer = setTimeout(() => controller.abort(), POST_TIMEOUT);

    try {
      const res = await fetch(urlJoin(this.gatewayUrl, path), {
        body: JSON.stringify(body),
        headers: {
          'Authorization': `Bearer ${this.serviceToken}`,
          'Content-Type': 'application/json',
        },
        method: 'POST',
        signal: controller.signal,
      });
      const text = await res.text().catch((error) => {
        if (controller.signal.aborted) throw error;
        return '';
      });
      return { body: text, ok: res.ok, status: res.status };
    } finally {
      clearTimeout(timer);
    }
  }

  private async httpPost(path: string, body: Record<string, unknown>): Promise<void> {
    if (this.inflight >= MAX_INFLIGHT) {
      log('Gateway %s dropped: max inflight (%d) reached', path, MAX_INFLIGHT);
      return;
    }

    this.inflight++;
    const controller = new AbortController();
    const timer = setTimeout(() => controller.abort(), POST_TIMEOUT);

    try {
      const res = await fetch(urlJoin(this.gatewayUrl, path), {
        body: JSON.stringify(body),
        headers: {
          'Authorization': `Bearer ${this.serviceToken}`,
          'Content-Type': 'application/json',
        },
        method: 'POST',
        signal: controller.signal,
      });

      if (!res.ok) {
        log('Gateway %s returned %d: %s', path, res.status, await res.text());
      }
    } catch (error) {
      log('Gateway %s failed: %O', path, error);
    } finally {
      clearTimeout(timer);
      this.inflight--;
    }
  }
}