Class HarnessGateway

java.lang.Object
io.agentscope.harness.agent.gateway.HarnessGateway
All Implemented Interfaces:
Gateway, WakeupDispatcher.WakeupTarget

public final class HarnessGateway extends Object implements Gateway, WakeupDispatcher.WakeupTarget
Default Gateway implementation. Routes inbound turns by MsgContext.canonicalKey(), serializes concurrent turns per session via SessionTurnGate, and dispatches to the appropriate registered HarnessAgent.

This implementation provides:

  • Multi-agent registry with fallback to a main agent
  • Stable session mapping: same MsgContext.canonicalKey() always resolves to the same session id, giving each logical conversation its own memory
  • Per-session fair mutual exclusion preventing concurrent turns from racing
  • Outbound address tracking for proactive delivery (e.g. subagent announces)
  • Method Details

    • create

      public static HarnessGateway create(ChannelManager channelManager)
      Creates a gateway with a channel manager for outbound delivery.
    • create

      public static HarnessGateway create(ChannelManager channelManager, MessageBus messageBus)
      Creates a gateway with a channel manager and message bus.
    • create

      public static HarnessGateway create()
      Creates a gateway without outbound delivery support.
    • messageBus

      public MessageBus messageBus()
      Returns the message bus, or null if not configured.
    • channelManager

      public ChannelManager channelManager()
      The channel manager for outbound delivery, or null if not configured.
    • setRuntimeContextResolver

      public void setRuntimeContextResolver(ChannelRuntimeContextResolver runtimeContextResolver)
      Sets an optional ChannelRuntimeContextResolver used when building the per-turn RuntimeContext. Passing null clears any previously configured resolver.
    • getRuntimeContextResolver

      public ChannelRuntimeContextResolver getRuntimeContextResolver()
      Returns the configured ChannelRuntimeContextResolver, or null.
    • bindMainAgent

      public void bindMainAgent(HarnessAgent agent)
      Description copied from interface: Gateway
      Binds the primary harness agent after construction. The agent is registered under its HarnessAgent.getAgentId() for routing by Gateway.run(io.agentscope.harness.agent.gateway.MsgContext, java.util.List<io.agentscope.core.message.Msg>).
      Specified by:
      bindMainAgent in interface Gateway
    • registerAgent

      public void registerAgent(String agentId, HarnessAgent agent)
      Description copied from interface: Gateway
      Registers a named harness agent for routing. Gateway.run(io.agentscope.harness.agent.gateway.MsgContext, java.util.List<io.agentscope.core.message.Msg>) will resolve the target agent by the agentId supplied in MsgContext.extra() (key "agentId"), falling back to the agent registered via Gateway.bindMainAgent(io.agentscope.harness.agent.HarnessAgent).
      Specified by:
      registerAgent in interface Gateway
    • findAgent

      public HarnessAgent findAgent(String agentId)
      Returns the agent registered under agentId, or null if not found. Exposed for introspection (e.g. enumerate an agent's skill repositories).
    • run

      public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> run(MsgContext context, List<io.agentscope.core.message.Msg> messages)
      Description copied from interface: Gateway
      Inbound turn (direct API or channel adapter).
      Specified by:
      run in interface Gateway
    • run

      public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> run(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress)
      Description copied from interface: Gateway
      Inbound turn with outbound address. The outboundAddress is recorded as the session's "last route" so that proactive outbound messages (e.g. subagent announces) can be delivered back to the originating channel/peer.
      Specified by:
      run in interface Gateway
      Parameters:
      context - routing context for session key resolution
      messages - the inbound messages
      outboundAddress - the delivery target for proactive replies; may be null
    • run

      public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> run(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext)
      Description copied from interface: Gateway
      Inbound turn with outbound address and an optional caller RuntimeContext.

      The caller context is merged into the turn; gateway identity fields take precedence. The default implementation ignores callerContext and delegates to Gateway.run(MsgContext, List).

      Specified by:
      run in interface Gateway
      Parameters:
      context - routing context for session key resolution
      messages - the inbound messages
      outboundAddress - the delivery target for proactive replies; may be null
      callerContext - optional application context to merge; may be null
    • run

      public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> run(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext, InboundMessage inboundMessage)
      Description copied from interface: Gateway
      Inbound turn with full Channel metadata for ChannelRuntimeContextResolver.
      Specified by:
      run in interface Gateway
      inboundMessage - optional inbound envelope passed to the resolver; may be null
    • runStream

      public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> runStream(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress)
      Description copied from interface: Gateway
      Specified by:
      runStream in interface Gateway
    • runStream

      public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> runStream(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext)
      Description copied from interface: Gateway
      Streaming variant of Gateway.run(MsgContext, List, OutboundAddress, RuntimeContext).

      The default signals unsupported streaming.

      Specified by:
      runStream in interface Gateway
    • runStream

      public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> runStream(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext, InboundMessage inboundMessage)
      Description copied from interface: Gateway
      Specified by:
      runStream in interface Gateway
    • deliverToSession

      public boolean deliverToSession(String sessionId, List<io.agentscope.core.message.Msg> messages)
      Delivers proactive outbound messages through the channel manager using the session's last recorded outbound address. Returns false if no route or no channel manager is available.
    • exposeSubagent

      public String exposeSubagent(String agentId, String sessionId, io.agentscope.core.agent.Agent agent, OutboundAddress replyTo)
      Exposes a spawned subagent as a user-addressable entry point. Returns the assigned subagentId.
      Parameters:
      agentId - the subagent type identifier
      sessionId - the session id assigned to the subagent
      agent - the agent instance
      replyTo - the outbound address for delivering replies; may be null
      Returns:
      the subagentId handle for direct addressing
    • revokeSubagent

      public void revokeSubagent(String subagentId)
      Revokes a previously exposed subagent, removing it from the live cache and the registry.
    • setSubagentRegistry

      public void setSubagentRegistry(SubagentRegistry registry)
      Installs the durable SubagentRegistry. Called during gateway wiring; defaults to an in-memory registry when not set.
    • setSubagentMaterializer

      public void setSubagentMaterializer(SubagentMaterializer materializer)
      Installs the SubagentMaterializer used to rebuild subagents that are not present in the local live cache (cross-node / post-restart recovery).
    • setBaseStore

      public void setBaseStore(BaseStore store)
      Installs the BaseStore used to persist session routing metadata across nodes. Called during gateway wiring when a distributed store is configured.
    • setSessionTurnGate

      public void setSessionTurnGate(SessionTurnGate gate)
      Installs the SessionTurnGate used to serialize concurrent turns per session. Defaults to LocalSessionTurnGate when not set.
      Parameters:
      gate - turn gate implementation; ignored when null
    • runSubagent

      public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> runSubagent(String subagentId, List<io.agentscope.core.message.Msg> messages)
      Description copied from interface: Gateway
      Routes messages directly to an exposed subagent session identified by subagentId, bypassing normal binding-based routing. Returns the subagent's reply.
      Specified by:
      runSubagent in interface Gateway
      Parameters:
      subagentId - the handle returned by SubagentGatewayBridge.ExposeResult.subagentId()
      messages - the messages to send to the subagent
    • runSubagentStream

      public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> runSubagentStream(String subagentId, List<io.agentscope.core.message.Msg> messages)
      Description copied from interface: Gateway
      Streaming variant of Gateway.runSubagent(String, List).
      Specified by:
      runSubagentStream in interface Gateway
    • registerExternalSession

      public void registerExternalSession(String sessionId, String gateKey)
      Adopts a control-plane-allocated session id so runWakeup(String, String) can resolve it. Used by BYO team_join; Managed sessions already use product ids that the managed turn path accepts without this mapping.
      Parameters:
      sessionId - external session id assigned by the control plane
      gateKey - stable gate key (e.g. team:{teamName}:{role})
    • isSessionRunning

      public boolean isSessionRunning(String sessionId)
      Returns true when the given session is currently inside a gated turn (a run is in progress). Used by WakeupDispatcher to skip sessions that will naturally drain their inbox on the current reasoning step.
      Specified by:
      isSessionRunning in interface WakeupDispatcher.WakeupTarget
    • runWakeup

      @Deprecated public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> runWakeup(String sessionId)
      Deprecated.
      use runWakeup(String, String) so the wakeup-driven round loads agent state from the correct (userId, sessionId) slot. This overload preserves legacy anonymous-wakeup behavior.
      Triggers a wakeup run without an owning user id.

      Delegates to runWakeup(String, String) with a null user id.

      Specified by:
      runWakeup in interface WakeupDispatcher.WakeupTarget
      Parameters:
      sessionId - the session to wake up
      Returns:
      the agent's response, or Mono.empty() if the session is unknown
    • runWakeup

      public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> runWakeup(String userId, String sessionId)
      Triggers a wakeup-driven run for an idle session, propagating the owning user id when present. Called by WakeupDispatcher when a background task completes or a team message arrives. The agent starts a reasoning round with no user input; InboxMiddleware drains the inbox and injects the pending results as context.

      When userId is non-blank it is set on the RuntimeContext, so the agent loads state from the same (userId, sessionId) slot as the original user-driven run — matching runStream. A blank or null user id preserves the legacy anonymous behavior.

      Specified by:
      runWakeup in interface WakeupDispatcher.WakeupTarget
      Parameters:
      userId - the owning user id carried by the wakeup entry; may be null or blank
      sessionId - the session to wake up
      Returns:
      the agent's response, or Mono.empty() if the session is unknown