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

[Bug]: TrainingRouter keys saved call inputs by agent id, so overlapping calls on one agent train on each other's messages

Open wahllllll opened this issue 1 month ago • 0 comments

Describe the bug

TrainingRouter saves each call's input messages during PreCallEvent and reads them back during PostCallEvent, so it can hand them to the selection strategy and to the shadow run. That hand-off is keyed by agent id:

// TrainingRouter.java:79
private final Map<String, List<Msg>> callInputs = new ConcurrentHashMap<>();

// handlePreCall, :123-124
String agentId = event.getAgent().getAgentId();
callInputs.put(agentId, event.getInputMessages());

// handlePostCall, :150
List<Msg> inputs = callInputs.remove(agentId);

An agent id is shared by every concurrent call on that agent, and AgentBase#callSerializationKey only serializes calls that belong to the same session — calls in different sessions are meant to overlap. So for two overlapping calls A and B on one agent:

step state of callInputs
A's PreCall agentId -> inputsA
B's PreCall agentId -> inputsB (overwrites A)
A's PostCall removes inputsB -> A is trained on B's messages
B's PostCall removes nothing -> warns No input messages found for agent and skips training entirely

Neither path raises. The training sample is attributed to the wrong request, and one call of the pair is silently dropped from training.

To Reproduce

Install the training hook on a ReActAgent and issue two overlapping calls in different sessions (userId / sessionId): agent.stream(msgA, sessionA) and agent.stream(msgB, sessionB) concurrently. Whichever PostCallEvent arrives first consumes the other call's input messages, and the second one finds nothing to train on.

Expected behavior

Each call is matched with its own input messages — the selection strategy and the shadow run should see the messages of the call that just finished, never of a later one.

Error messages

No exception is thrown. The only trace is the losing call's warning:

No input messages found for agent: <agentId>

Environment (please complete the following information):

  • AgentScope-Java Version: 2.1.0-SNAPSHOT (main, commit 20dc107e4)
  • Java Version: 17
  • OS: Windows 11

Additional context

handlePostCall is already inside Mono.deferContextual(ctx -> ...), and the Reactor context already carries the per-call scope under AgentBase#CALL_SCOPE_KEY — the same value ReActAgent.scopeFrom reads. That value is in hand and simply unused for the keying. Keying the map by that scope, with a fallback to the agent id for agent types that publish no per-call scope, fixes both halves.

This is the anti-pattern AgentBase calls out in the javadoc for beforeAgentExecution: the per-call scope is published through the Reactor Context rather than a separate accessor precisely so that no concurrent call can overwrite a shared field between construction and capture.

One follow-on is required once the key is per-call: an entry is no longer overwritten by the next call, so a call that fails before its PostCallEvent would leak the entry for the lifetime of the JVM — ErrorEvent has to drop it.

A fix with a regression test that pins the mispairing is in #3457.

wahllllll avatar Oct 11 '26 05:10 wahllllll