agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

HarnessGateway.runWakeup throws NPE for external session ids when no outbound route is recorded (route registry gap)

Open ly19911114 opened this issue 2 weeks ago • 0 comments

Summary

HarnessGateway.runWakeup(String userId, String sessionId) throws a NullPointerException during RuntimeContext assembly when the given session id has a resolvable gate key but no outbound route — which is exactly the situation for external (application-assigned) session ids driven through ChatUiChannel, e.g. by WakeupDispatcher after a background task completes.

Environment

  • AgentScope Java 2.0.4 (io.agentscope:agentscope-harness:2.0.4, verified against the 2.0.4 sources jar)

Analysis (2.0.4 sources)

  1. runWakeup resolves the gate key first and safely skips unknown sessions:
// HarnessGateway.java:641-646
String gateKey = resolveSessionGateKey(sessionId);
if (gateKey == null) {
    log.debug("runWakeup: unknown sessionId={}, skipping", sessionId);
    return Mono.empty();
}
  1. But when the gate key resolves, the outbound address is put into the RuntimeContext unconditionally:
// HarnessGateway.java:651-655
RuntimeContext.Builder rtcBuilder =
        RuntimeContext.builder()
                .sessionId(sessionId)
                .put("gateKey", gateKey)
                .put("outboundAddress", resolveRoute(sessionId));   // <-- may be null
  1. RuntimeContext.Builder.put stores into a ConcurrentHashMap, which rejects null values → NPE at assembly time (the wakeup round never starts):
// RuntimeContext.java:375-380
public Builder put(String key, Object value) {
    ...
    this.stringExtras.put(key, value);   // ConcurrentHashMap
}
  1. Why the route is missing for external ids: routes are only recorded when runStream delivers to a channel, under the gateway's own generated session id (gw-...), in lastRouteBySession / the ROUTE_NAMESPACE store (resolveRoute, HarnessGateway.java:778-803). An external session id only ever gets a gate key:
    • via registerExternalSession (HarnessGateway.java:588-597), which writes only the SESSION_NAMESPACE mapping — no route, and
    • there is no public API to register a route for an external session id.

So for application-driven sessions: gateKey ✔, route ✘ → step 2 NPEs.

Reproduction

  1. Build a GatewayBootstrap with a HarnessAgent and drive turns through chatUiChannel.sendStream(SendOptions.of(userId, businessSessionId), msgs) (the channel resolves its own internal gw-... session id internally).
  2. Register the external session: gateway.registerExternalSession(businessSessionId, gateKey).
  3. Enqueue a wakeup for it on the shared bus: bus.enqueueWakeup(userId, businessSessionId, agentId).
  4. WakeupDispatcher drains the queue and calls HarnessGateway.runWakeup(userId, businessSessionId) → NPE (ConcurrentHashMap.put(key, null)), the wakeup round never runs.

Expected behavior

One of:

  • Tolerate a missing route the same way an unknown session is tolerated — skip the outboundAddress entry (or the whole wakeup) with a debug log;
  • or let registerExternalSession (a new overload) also record the outbound route for the external session id.

The unknown sessionId, skipping branch suggests "skip when not resolvable" is already the intended semantics — the route lookup just wasn't given the same treatment.

Workaround we currently ship

A custom WakeupDispatcher.WakeupTarget that never calls HarnessGateway.runWakeup: it drains the session inbox itself and drives the round via channel.sendStream(SendOptions.of(userId, sessionId), ...) — functionally equivalent push-back without touching the route registry.

ly19911114 avatar Nov 14 '26 11:11 ly19911114