[Bug]:CompactionMiddleware.onReasoning onErrorResume swallows model errors and replays reasoning -> ReAct loop never terminates on QPM/API error
Version: agentscope-java 2.0.1 (agentscope-harness)
Problem
When a reasoning model call fails after exhausting retries (e.g. HTTP 510 QPM limit / empty response / API error), the error is swallowed by CompactionMiddleware instead of propagating, and the whole reasoning step is replayed. The ReAct loop therefore never exits even though the model failed.
Root cause
CompactionMiddleware.onReasoning (agentscope-harness/src/main/java/io/agentscope/harness/agent/middleware/CompactionMiddleware.java ~L109-L141):
Flux<agentevent> downstream = next.apply(input); // reasoning, errors must propagate return compactor .compactIfNeeded(...) .onErrorResume(e -> { log.warn("Compaction failed, continuing without compaction: {}", e.getMessage()); return downstream; // do NOT re-invoke reasoning here }) .flatMapMany(optResult -> optResult.isEmpty() ? downstream : next.apply(new ReasoningInput(...)));</agentevent>
Because next.apply(input) (the real reasoning/model call) executes lazily inside flatMapMany, any model error emitted there propagates into the broad onErrorResume. The handler mistakes it for a compaction failure and replays next.apply(input), issuing a brand-new model call. On the next QPM the pattern repeats indefinitely.
Observed behavior
Within one user turn the ReAct loop kept running across failures:
- model call #15 QPM -> exhausted 3 retries ->
ChatModelException-> swallowed + reasoning replayed - model call #16 succeeded (tool call) -> loop continued
- model call #18 / #22 same as #15, each followed by a successful replay
- The terminal
.onError/doFinallyat the top-level streamEvents never fired; no top-level failure was ever surfaced.
Attached fragment (callSeq=15 terms): after the model logs 重试耗尽 it is immediately followed by CompactionMiddleware: Compaction failed, continuing without compaction and a new request with the same messageCount is built.
Expected
Errors from the reasoning (next.apply) should propagate up (terminating the ReAct loop), and only failures of the compaction sub-stream itself should be caught by onErrorResume.
Suggested fix (upstream)
Scope onErrorResume to the compaction step only, and let the reasoning stream (next.apply) errors pass through uncaught. For example, wrap only the compactIfNeeded(...)/summary subscription in error recovery:
Flux<agentevent> downstream = next.apply(input); // reasoning, errors must propagate return compactor .compactIfNeeded(...) .onErrorResume(e -> { log.warn("Compaction failed, continuing without compaction: {}", e.getMessage()); return downstream; // do NOT re-invoke reasoning here }) .flatMapMany(optResult -> optResult.isEmpty() ? downstream : next.apply(new ReasoningInput(...)));</agentevent>
Impact
- Any app that enables
.compaction(...)never terminates its agent turn when the model returns QPM/empty/API errors, causing runaway tool calls and unbounded cost.