Skip to content

fix(jev): bound the conversation state sent by JevToolSelectionMiddleware - #3375

Merged
jujn merged 2 commits into
agentscope-ai:mainfrom
amyaxy:fix/jev-tool-selection-lean-state
Oct 15, 2026
Merged

jujn merged 2 commits into
agentscope-ai:mainfrom
amyaxy:fix/jev-tool-selection-lean-state

Conversation

@amyaxy

@amyaxy amyaxy commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #3372.

Bound the conversation state sent by JevToolSelectionMiddleware on each reasoning step. Updated against current main while preserving its independent per-tool relevance scoring and JevExecution OFF/SHADOW/ENFORCE modes.

Behavior

  • When enabled, RECENT_WINDOW is the default context strategy: the latest 8 nonblank text messages, projected as {role, text}, capped at 8000 total text characters.
  • maxContextMessages and maxContextChars configure positive window limits. Blank messages do not consume slots; truncation preserves UTF-16 surrogate pairs.
  • LATEST_USER_MESSAGE sends only the latest user text as userRequest.
  • FULL_CONVERSATION sends the complete original message list, restoring the previous context scope.
  • All 64-tool relevance batches reuse the same context state. Context construction remains inside JevExecution, so errors are observed and restore the original tool list.
  • Default OFF mode, SHADOW observation, ENFORCE filtering, independent Noul questions, uncertainty handling, and valid NONE_APPLICABLE decisions remain unchanged from main. The former Choice chunk/rerank implementation is not restored.

The bounded window can omit earlier tool hints in long conversations; tune the limits or choose FULL_CONVERSATION for applications that need that history.

Validation

  • JevSelectionSupportTest and JevToolSelectionMiddlewareTest: 51 tests passed, including main's selection/mode regressions and context strategy, batch budget, invalid-context fallback, OFF-mode laziness, and builder validation.
  • Full JEV module suite: 128 tests, 0 failures, 0 errors, 1 skipped.
  • Spotless formatting/checks passed for the changed module.
  • Documentation tests, strict build validation and broken-link checks passed; English/Chinese landing pages and the current tool-selection guide describe the configuration.
  • CI follow-up: fixed the inherited ReActAgent.Builder.copy() omission by copying configured agentId; the existing ReActAgentTest suite passes all 26 tests. The inherited HarnessAgentIdTest now explicitly enables legacy session history before asserting legacy state directories, preserving its existing assertions under main's event-log default. Both Core and Harness ID regressions pass. GitHub Ubuntu clean verification, documentation validation, license/module sync and patch coverage checks pass. Windows CI is still running. Local full clean verification passed Core, Harness and JEV, then hit two Session Chat timing timeouts; all 8 ChatSessionsTest cases passed unchanged on a standalone rerun.

@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.07692% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ensions/judge/jev/example/JevSelectionSupport.java 96.42% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

JevToolSelectionMiddleware previously shipped the complete input.messages() list — system prompt, memory and full history — to Jev on every reasoning step, and split it across chunks when candidates exceeded 254, so each selection call carried an unbounded payload on long-running agents. This PR introduces ContextStrategy with a bounded recent window as the default. Reviewed both sources, the changed tests and the EN/ZH docs; the engine ran against this checkout and the suites are green.

Verdict: approve. The bound is real, the legacy behaviour stays opt-in, and no correctness, thread-safety or credential issue surfaced. Three non-blocking notes inline.

Verified

  • Hard bound: recentWindowState enforces both limits — a entries.size() < maxMessages loop guard and a remaining char budget decremented per message (JevSelectionSupport.java:80-104). A single oversized message is truncated surrogate-safely to the remaining budget and skipped if the cut empties it (:91-103), so the payload is bounded in both message count and characters.
  • Compatibility: contextStrategy(FULL_CONVERSATION) keeps the old shape reachable (JevToolSelectionMiddleware.java:355, dispatched at :184); the builder API is only extended. The default does flip to RECENT_WINDOW (:317), so upgraded users can get different tool selections from the same agent — intentional per #3372 and documented in docs/v2/en/jev/index.md + docs/v2/zh/jev/index.md.
  • Thread safety: all fields final (:82-89), alwaysIncludeTools immutable via Set.copyOf (:93), and onReasoning/stateFor/selectTools work only on locals.
  • No leakage: no new logging; the only new exception message carries tool names, not message text (:167-171); state-building errors are swallowed by fail-open (:150-158).
  • Tests: truncation is covered at both levels (oldest-kept cut, surrogate cut, skip-when-empty, drop-beyond-budget, blank-skip/backfill, role lowercasing — JevSelectionSupportTest.java:144-247, JevToolSelectionMiddlewareTest.java:322, :365), plus the 255-tool two-chunk rerank path (:901).

Notes

  • Inline [Warning] on JevSelectionSupport.java:88: an assistant message carrying only tool calls has blank text and is skipped, so a surviving TOOL result can appear unpaired in the judge window. The primary model contract is unaffected (input.messages() is passed to next unchanged, JevToolSelectionMiddleware.java:175-177), but this is exactly the turns tool selection depends on.
  • Inline [Info] on :317: a one-line migration note on pinning the old behaviour would make the upgrade less surprising.
  • Inline [Info] on tests: the exact 254-candidate single-partition boundary and a window fixture containing TOOL/assistant-tool-call messages are the two gaps.

Review scope

Static review plus running the affected test suites in this PR-head checkout; no CI was triggered from this review and no build of unrelated modules was attempted.


Automated review by github-manager-bot

continue;
}
String text = message.getTextContent();
if (text == null || text.isBlank()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Warning] Inside the bounded window, an assistant message that carries only tool calls contributes blank text (Msg joins TextBlocks only, Msg.java:544-550), so it is skipped here — which can leave a TOOL result in the window with no preceding assistant tool call. This does not break the primary model contract (input.messages() is passed to next untouched at JevToolSelectionMiddleware.java:175-177), but it degrades the judge's context quality exactly on the turns where tool selection matters most. Consider keeping the assistant tool-call line (its name or a placeholder) when its tool result survives the window. Not blocking.

private int maxTools = 3;
private double confidenceThreshold = 0.5;
private boolean failOpen = true;
private ContextStrategy contextStrategy = ContextStrategy.RECENT_WINDOW;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Info] Confirming the blast radius of the new default: RECENT_WINDOW now applies to every existing user of the middleware, so the Jev input — and therefore potentially the selected tools — changes silently after an upgrade. FULL_CONVERSATION stays reachable via contextStrategy(...) (:355, dispatched at :184) and both EN/ZH docs are updated, so this is acceptable as the point of #3372. A one-line migration note telling users how to pin the old behaviour would make the upgrade less surprising.

}

@Test
void chunksAndReranksLargeToolSets() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Info] Coverage is genuinely strong — oldest-kept truncation, surrogate-aware cut, skip-when-empty, drop-beyond-budget, blank-skip/backfill, role lowercasing, and the 255-tool two-chunk rerank path are all asserted. Two gaps: the exact single-partition boundary at 254 candidates (:1086 only exercises 3 tools), and no window fixture containing TOOL-role or assistant-tool-call messages (all fixtures are user messages), which is the case the warning above is about.

@amyaxy
amyaxy force-pushed the fix/jev-tool-selection-lean-state branch from 92034c5 to c1a1de6 Compare October 14, 2026 07:46
@jujn
jujn merged commit f4dd724 into agentscope-ai:main Oct 15, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants