Repository navigation
Conversation
TrainingRouter keeps the PreCall inputs in a map so PostCall can hand them to the selection strategy and to the shadow run. The map was keyed by the agent id, which an agent shares with every concurrent call on it: AgentBase# callSerializationKey only serializes calls that belong to the same session, so two calls on one agent in different sessions overlap by design. Overlapping calls then raced on that single entry. Whichever PreCall ran last won, so the earlier call's PostCall handed the later call's messages to the selection strategy -- training data attributed to the wrong request -- while the later call's own PostCall found nothing and logged "No input messages found", dropping that call from training entirely. Neither path raises. Key the map by the per-call scope the framework publishes on the Reactor Context under AgentBase#CALL_SCOPE_KEY (the same value ReActAgent.scopeFrom reads), falling back to the agent id for agent types that keep no scope. Contexts that yield no key at all -- only reachable with an agent that has no id -- skip the map instead of failing the hook, since a ConcurrentHashMap rejects a null key. Because a per-call key is never overwritten by a later call, an entry would now outlive a call that fails before PostCall fires, so ErrorEvent is handled to drop it. All changes are confined to agentscope-extensions-training.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…llback Pin the two behaviours the previous commit added without tests: an ErrorEvent drops the inputs saved by PreCall so a late PostCall has nothing to train on, and an agent that exposes no id is tolerated rather than failing the hook on a null map key.
|
@jujn — #3457 is the oldest thing I have open (30 days) and has never been looked at, so I am not sure whether it is queued or whether the shape is wrong. It fixes #3499: If it needs a different shape I would rather redo it now than leave it sitting for another month, and I am happy to do that work. If it is simply queued, a word that it is on the list would be enough — I will stop bumping it. |
Symptom
TrainingRoutersaves each call's input messages duringPreCallEventand reads them back duringPostCallEvent, so it can hand them to the selection strategy and to the shadow run. That hand-off is keyed by agent id:An agent id is shared by every concurrent call on that agent, and
AgentBase#callSerializationKeyonly serializes calls that belong to the same session — calls in different sessions overlap by design (that is the stated intent of that method: "ReActAgent returns its(userId, sessionId)slot key so same-session calls do not corrupt shared conversation state while distinct sessions run in parallel").So for two overlapping calls A and B on one agent:
callInputsagentId -> inputsAagentId -> inputsB(overwrites)inputsB→ A is trained on B's messagesNo input messages found for agentand skips trainingNeither path raises. The training data is attributed to the wrong request, and one call in the pair is silently dropped.
This is the exact anti-pattern
AgentBasewarns about in its own javadoc forbeforeAgentExecution: returning the per-call scope through the Reactor Context rather than a separate accessor exists "to avoid any window in which a concurrent call could overwrite a shared field between construction and capture".Fix
Key the map by the per-call scope the framework already publishes on the Reactor Context under
AgentBase#CALL_SCOPE_KEY— the same valueReActAgent.scopeFromreads.handlePostCallwas already insideMono.deferContextual(ctx -> …), so the context was in hand and simply unused.Agent types that keep no per-call scope (
beforeAgentExecutionreturnsnull) fall back to the agent id, so their behaviour is unchanged. When neither is available — only reachable with an agent that has no id at all, e.g. a test double — the key isnulland the map operation is skipped, since aConcurrentHashMapcannot hold a null key.One follow-on is needed. With a per-call key, an entry is no longer overwritten by the next call, so a call that fails before
PostCallEventfires would leave its entry behind for the life of the JVM.ErrorEventis now handled to drop it. (AgentCloneronly supportsReActAgent, so the scope is always present on the path this module actually uses.)Tests
Added
shouldTrainEachCallWithItsOwnInputs: twoPreCallEvents on the same agent with different call scopes (the two-session overlap above), then the first call'sPostCallEvent, asserting the selection strategy receives the first call's messages.Verified the test pins the behaviour — with the production change reverted it fails exactly on the wrong pairing:
With the fix the same class is green (8/8), and
spotless:applyreports no further changes.Note on the integration path
This module integrates through the
Hook/HookEventAPI, which is@Deprecated(forRemoval = true, since = "2.0.0")—MiddlewareBasereplaces it. I deliberately kept this a minimal, mechanical fix to the shipped behaviour rather than porting the router to Middleware: the bug is live and affects data correctness today, and the port is a design decision (which middleware hook points map toPreCallEvent/PostCallEvent, where the shadow run belongs) that deserves its own discussion. Happy to do that port separately if you would prefer the module moved onto Middleware rather than patched.No database migration and no configuration change.
Fixes #3499