Skip to content

bug(jev): JevToolSelectionMiddleware sends the full conversation state (system prompt + memory + history) to Jev on every reasoning step #3372

Closed

Description

@amyaxy

Describe the bug

JevToolSelectionMiddleware puts the entire input.messages() list — full system
prompt, memory, and complete conversation history — into SystemOneRequest.state
and posts it to POST /v1/systemone on every reasoning step. When the tool set
exceeds the choice limit (254) and is chunked, the same full state is sent twice
per step (initial selection + rerank).

Root cause: onReasoning passes
JevSelectionSupport.messagesState(input.messages()), i.e.
Map.of("messages", List.copyOf(messages)), as the request state. The middleware
already extracts latestUserText from the messages, but uses it only as a
blank-input guard — it is never sent.

In a realistic agent setup (non-trivial system prompt + persistent memory + a few
turns of history) the serialized state is ~32K characters per request. A user turn
with S reasoning steps therefore uploads the full conversation S times (2S when
chunked), even though the decision being made is only "which of these tools fits
the current request".

To Reproduce

  1. Wrap the Jev call with a size logger and attach the middleware to a ReActAgent
    with more optional tools than maxTools:
JevToolSelectionMiddleware middleware =
        JevToolSelectionMiddleware.builder(request -> {
                    System.out.println("state bytes: "
                            + JevClient.MAPPER.writeValueAsString(request.state()).length());
                    return client.systemOne(request);
                })
                .maxTools(3)
                .build();

ReActAgent agent =
        ReActAgent.builder()
                .name("assistant")
                .model(model)
                .toolkit(toolkit)          // > 3 optional tools
                .middleware(middleware)
                .build();
  1. Run any user turn with a realistic system prompt and accumulated memory/history.
  2. Observe: every reasoning step logs a state payload equal to the complete
    serialized conversation (~32K chars in our setup), and chunked tool sets log it
    twice per step. state.messages contains everything, including the system
    prompt and memory.

Expected behavior

The middleware sends only the context the tool-selection decision needs — e.g. the
latest user request and/or a bounded recent message window — not the complete
serialized conversation on every reasoning step.

Error messages

None — this is not a crash. The middleware works as coded; the defect is the
excessive and unnecessary request payload (cost, latency, and data exposure).

Environment (please complete the following information):​

  • AgentScope-Java Version: 2.0.4-SNAPSHOT
  • Java Version: 21
  • OS: Windows 11

Additional context

  • Impact: (1) payload size and latency on a 5-second-timeout pre-check that sits in
    the hot path of every reasoning step; (2) Jev API cost, which scales with input
    size; (3) unnecessary exposure of the full system prompt, memory, and history to
    a third-party endpoint on every step.
  • Prior art in the same package: JevModelRouterMiddleware already uses the lean
    pattern state = Map.of("userRequest", userText) and makes one decision per
    agent invocation.
  • The existing test reselectsToolsForEachReasoningStepUsingFullMessages
    explicitly asserts the full-messages behavior, and both the EN and ZH docs state
    "sends the full input.messages() state to Jev" — the behavior is documented but
    the design is disproportionate to the decision being made.
  • Suggested fix: make the request context configurable on the builder, e.g. a
    ContextStrategy with a bounded recent window (default, capped by
    maxContextMessages / maxContextChars), a latest-user-message mode aligned
    with JevModelRouterMiddleware, and the current full-conversation mode kept for
    backward compatibility. I am willing to submit a PR along these lines.
  • Out of scope: JevAutoModeMiddleware also sends the full history, but risk
    assessment arguably needs broader context; that can be discussed separately.
  • Related: Wanted: Tell us your JEV ideas. #3251

Activity

  1. added
    bug
    on Sep 30, 2026
  2. oss-maintainer commented on Sep 30, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed (verified against main @ 01da9e6fb, 2.0.4-SNAPSHOT)

    Every code, test and doc claim in this report checks out. Paths below are relative to repo root; JEV = agentscope-extensions/agentscope-extensions-judge/agentscope-extensions-jev.

    Confirmed

    Claim Evidence
    Full input.messages() becomes the request state JevToolSelectionMiddleware.java:115 → JevSelectionSupport.messagesState(input.messages()), which is exactly Map.of("messages", List.copyOf(messages)) (JevSelectionSupport.java:52-57); packed at JevToolSelectionMiddleware.java:151-152
    POSTed to /v1/systemone per reasoning step JevClient.java:51 (endpoint), :111-116 (state serialized verbatim), :118-126 (POST body)
    Sent twice per step when chunked JevToolSelectionMiddleware.java:174 + :190-195 — the rerank request reuses the same state variable
    Threshold is 254 JevSelectionSupport.java:33-34 — MAX_CHOICE_OPTIONS = 255 (API cap enforced at JevClient.java:291-294), MAX_CANDIDATES_PER_CHOICE = 254 (254 tools + __none__ fills the budget); partitioning at :138-140
    latestUserText extracted but never sent JevToolSelectionMiddleware.java:110-113 — userText is only the blank-input guard and is never referenced again
    Test pins the behavior JevToolSelectionMiddlewareTest.java:151 (reselectsToolsForEachReasoningStepUsingFullMessages), asserting state.messages size 2 at :200-206
    EN/ZH docs state it docs/v2/en/jev/index.md:152, docs/v2/zh/jev/index.md:151
    JevModelRouterMiddleware lean prior art JevModelRouterMiddleware.java:147 — .state(Map.of("userRequest", userText)), one decision per invocation via onAgent (:86-110) + RuntimeContext (:107)
    JevAutoModeMiddleware also sends full history JevAutoModeMiddleware.java:132, :212-214

    Two additions that strengthen the case

    1. The per-reasoning-step claim is exactly right, and the engagement condition matters. ReActAgent.java:2476-2485 rebuilds and applies the onReasoning chain inside every reasoning() iteration, so the upload really is per step, not per turn. But it is skipped when the optional tool count is ≤ maxTools (:106) or the latest user text is blank (:111) — so the worst case is one full-conversation upload per step of every ReAct loop that actually has tools to select.
    2. The exposure is genuinely the whole context, with no cap anywhere. The list reaching ReasoningInput.messages is built at ReActAgent.java:2449-2451 via MessageUtils.prependSystemMessage(...) (MessageUtils.java:214-224), so the system prompt is element 0 followed by the full in-memory context (state.contextMutable(), :2433-2434) — every role and content block, tool results included. SystemOneRequest.state is an untyped Object validated only for non-null (JevClient.validateRequest, :263-266); the 2..255 limit constrains the tools dimension only. JevClient.java:48 defaults to a remote endpoint, so this is third-party transmission of the complete agent context, not an internal call.

    Assessment: the behavior is documented and test-pinned, so this is a design defect rather than an implementation mistake — the decision being made is "which of these tools fits the current request", and the payload sent to answer it is the entire conversation. Cost, latency on a hot path, and data exposure all scale with history length instead of tool count.

    On your proposed fix: a configurable context strategy on the builder (JevToolSelectionMiddleware.java:232-270 currently exposes only alwaysIncludeTools, maxTools, confidenceThreshold, failOpen) is the right shape, and keeping the full-conversation mode for backward compatibility is correct since JevToolSelectionMiddlewareTest.java:151 and both docs pin today's behavior. Two things to settle in the PR: what the default becomes (a bounded window changes what Jev sees, which is an observable behavior change), and whether JevAutoModeMiddleware (:212-214) stays as-is — we agree with you that risk assessment arguably needs the broader context, so leaving it out of scope is reasonable.

    Since you offered to submit the PR along these lines, we will not generate a competing fix proposal — please open it and we will review the diff. If you would rather have an automated proposal here first, reply /approve and we will produce one.


    Automated evaluation by github-manager-bot

  3. added
    area/ext/integration
    on Sep 30, 2026
  4. added 2 commits that reference this issue on Sep 30, 2026
    5451f84
    cc68384
  5. oss-maintainer commented on Oct 3, 2026

    @oss-maintainer
    Collaborator

    Proposal window closed — no automated PR will be generated, this proposal window here is closed

    The 72 h feedback window opened by the evaluation above (2026-09-30T10:10:10Z, comment 5909097492) reached its bound at 2026-10-03T10:10:10Z with no reply on this thread — 0 comments and 0 reactions after that evaluation as of this read — so the offer to produce an automated proposal has lapsed: we are not opening a PR for #3372 and no automated PR will be generated from this thread.

    The implementation path is already owned by the community. PR #3375 (fix(jev): bound the conversation state sent by JevToolSelectionMiddleware, authored by @amyaxy, opened 2026-09-30T14:18:09Z and declaring Fixes #3372) targets exactly the seam the evaluation describes — replacing the full input.messages() state that JevToolSelectionMiddleware.java:115 builds and packs per reasoning step with a bounded context, which is the shape the evaluation endorsed. As of this read #3375 is OPEN, not a draft, unmerged, head cc683848269f6f99c91e6b0237c3d7002d283a72, mergeable reported MERGEABLE / clean, last updated 2026-09-30T14:58:52Z. Filing a second PR against the same issue is the competing-proposal outcome this window was written to avoid, which is why the proposal is stood down rather than escalated.

    Nothing here retracts the evaluation. The defect it confirms — every reasoning step serialising the whole conversation, system prompt and tool results included, to the remote endpoint at JevClient.java:48 — was verified against main at 01da9e6fb and stands. The open work is review of #3375, and in particular the two decisions the evaluation named: what the new default context strategy becomes (a bounded window changes what Jev sees, which is an observable behavior change against JevToolSelectionMiddlewareTest.java:151 and both the EN and ZH docs), and whether JevAutoModeMiddleware.java:212-214 stays out of scope.

    If #3375 is closed without merging and an automated proposal is still wanted here, reply on this issue and the fix will be re-evaluated against whatever main looks like at that point. No action is needed from anyone else.

    Automated feedback-window maintenance by github-manager-bot

  6. added a commit that references this issue on Oct 15, 2026
    f4dd724
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/ext/integrationbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions