fix(autocontext): add error handling to AutoContextHook.handlePreReasoning() #1317
Conversation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
compressIfNeeded() makes blocking model calls that can fail with timeout, service error, or rate limit. Without a try-catch, the exception kills the agent's reactive pipeline. Mirror handlePreCall()'s existing pattern: catch, log at WARN, and continue with uncompressed messages. Walk the cause chain for InterruptedException to preserve thread interruption semantics (Reactor's .block() wraps checked exceptions). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…pre-reasoning-error-handling # Conflicts: # agentscope-extensions/agentscope-extensions-autocontext-memory/src/main/java/io/agentscope/core/memory/autocontext/AutoContextHook.java # agentscope-extensions/agentscope-extensions-autocontext-memory/src/test/java/io/agentscope/core/memory/autocontext/AutoContextHookTest.java
|
hi, the conflicts are resolved. pls review the changes. @LearningGp |
LearningGp
left a comment
There was a problem hiding this comment.
Placing onErrorResume after .map(...) inadvertently catches exceptions thrown by buildInputMessages (even if rare) and swallows them with a misleading "Failed to compress context" log. I'd suggest chaining onErrorResume directly after compressIfNeededAsync() so exceptions inside .map can bubble up normally.
Yeah, this solution does help. BUT excptions from buildInputMessages are really rare. And if we apply the optimization, we'll re-introduce the original problem - if an exception happens inside the map, the compression disrupts the agent execution again. |
|
Close this PR because the compaction has been refactored. |



AgentScope-Java Version
1.0.12-SNAPSHOT
Description
Background
AutoContextHook.handlePreReasoning()does not have error handling. When the blocking model call insideAutoContextMemory.compressIfNeeded()(model.stream().block()) fails due to timeout, service errors, or rate limiting, the exception propagates without being handled, causing the Agent’s reactive pipeline to silently crash. The only trace is anAGENT_CALL_ENDevent in tracing with"exception":"value", preceded by an approximately 10-second blank period, which corresponds to the model call timeout window.By contrast,
handlePreCall()already has atry-catch(Exception)block and continues execution after errors.handlePreReasoning()is missing the same protection.Changes
AutoContextHook.javahandlePreReasoning()in a try-catch block, aligning it with the existing pattern inhandlePreCall().InterruptedException, where Reactor.block()wraps checked exceptions in aRuntimeException: detect it by walking the cause chain, then returnMono.error(e)to correctly propagate the interruption signal.isInterruptedException(Throwable), which walks throughgetCause()level by level to detectInterruptedException.handlePreReasoning()to document the new error-handling behavior.AutoContextHookTest.javatestPreReasoningEventHandlesCompressionError: uses aFailingModelthat throwsRuntimeException, triggers strategy-4 compression, and verifies that the hook returns the original event instead of throwing an exception.testPreReasoningEventPreservesWrappedInterruptedException: uses aFailingModelthat throwsRuntimeException(new InterruptedException(...)), and verifies that the wrapped interruption exception is correctly propagated throughMono.error().FailingModeltest helper class.How to test
Checklist
mvn spotless:applymvn test) — 183/183