Repository navigation
Description
Describe the bug
When an interrupt is signalled between the reasoning step (the model has just emitted ToolUseBlocks) and the acting step (tool execution), ReActAgent.handleInterrupt appends its recovery message but does not clean up the assistant message that already contains the unfulfilled ToolUseBlock. The persisted AgentState context ends up inconsistent:
[..., user, assistant(ToolUseBlock), assistant("I noticed that you have interrupted me. What can I do for you?")]
^^^ no matching ToolResultBlock ^^^
On the next run that resumes from this saved state, the model is fed an assistant ToolUseBlock with no matching ToolResultBlock, immediately followed by the recovery message. With an OpenAI-compatible model (observed on MiniMax-M3) the model returns an empty response (completion_tokens = 0, choices: []), so the agent produces no output — the user-facing symptom is "the agent stopped replying after I resumed".
This affects any middleware that triggers an interrupt after the model call completes but before tools execute — e.g. a post-generation quota/budget/permission check (our case: a points-balance middleware that deducts on ModelCallEndEvent and interrupts on exhaustion).
Root cause (source references)
One reasoning iteration in ReActAgent:
reasoningStreamruns the model through theonModelCallmiddleware chain. A post-call middleware observesModelCallEndEventand callsreActAgent.interrupt(ctx)— this only sets the flag onInterruptControl; it does not stop the in-flight step.- After the stream completes,
runPostReasoningPipelinewrites the reasoning result into the context:ReActAgent.java:2075→state.contextMutable().add(eventMsg)— the assistant message with theToolUseBlockis now in context.
- It then proceeds to acting:
ReActAgent.java:2103→return checkInterrupted().then(acting(iter))— the flag is already set, socheckInterrupted()throwsInterruptedExceptionandactingis skipped (noToolResultBlockis ever produced).
- The
InterruptedExceptionis caught inAgentBase(AgentBase.java:497-499) and routed tohandleInterrupt:ReActAgent.java:3572→scope.state.contextMutable().add(recoveryMsg)— appends the recovery message.ReActAgent.java:3573→saveStateToSession(scope)— persists the now-corrupted context.
The gap: the checkInterrupted checkpoint at :2103 sits after the reasoning result is written to context (:2075) but before acting. An interrupt therefore cannot prevent the ToolUseBlock from being persisted, and handleInterrupt does not reconcile it.
To Reproduce
A minimal middleware that interrupts right after the first model call (simulating post-generation quota exhaustion — the model has emitted tool_calls, but we "can't afford" to run them):
public class InterruptAfterModelCallMiddleware implements MiddlewareBase {
private final AtomicBoolean fired = new AtomicBoolean();
@Override
public Flux<AgentEvent> onModelCall(Agent agent, RuntimeContext ctx, ModelCallInput input,
Function<ModelCallInput, Flux<AgentEvent>> next) {
return next.apply(input).concatMap(event -> {
if (event instanceof ModelCallEndEvent && fired.compareAndSet(false, true)) {
if (agent instanceof ReActAgent r) {
r.interrupt(ctx);
} else {
agent.interrupt();
}
}
return Flux.just(event);
});
}
}
Steps:
1. Build a ReActAgent with at least one tool, a persistent AgentStateStore (e.g. the Redis extension), and the middleware above.
2. Run a prompt that triggers tool use, so the model emits a ToolUseBlock.
3. The first run is interrupted as expected; the AgentState is persisted.
4. Inspect the persisted context → it contains assistant(ToolUseBlock) immediately followed by assistant("I noticed that you have interrupted me..."), with no ToolResultBlock for the tool call.
5. Start a new run on the same session and send "continue". The model receives the inconsistent context and returns an empty response (completion_tokens = 0); the agent emits no content.
Expected behavior
After an interrupt, handleInterrupt should leave the context self-consistent. Either:
- remove the unfulfilled ToolUseBlock (or the whole trailing assistant message containing it) before appending the recovery message; or
- emit a cancelled/error ToolResultBlock for each pending ToolUseBlock, preserving the tool-calling protocol invariant (every assistant tool_call is followed by a matching tool result).
Either way, the next resumed run must not inherit a broken context.
Error messages
None — this is the silent part. No exception is thrown; the provider simply returns an empty completion. The corruption is only visible by inspecting the persisted AgentState context.
Additional context
- The OpenAI/Anthropic tool-calling protocol requires every assistant tool_call to be followed by a corresponding tool result message. A dangling ToolUseBlock violates this; providers react differently (MiniMax-M3 returns empty; others may 4xx). The framework should not let an interrupt persist such a state.
- Suggested fix location: ReActAgent.handleInterrupt (:3550) is the natural place to reconcile the trailing context before appending the recovery message — the most contained fix. An alternative is to move the checkInterrupted checkpoint (:2103) to before the reasoning result is written (:2075), but that changes "what gets persisted on interrupt" semantics for all interrupt sources, so it is riskier.
- Discovered in production via Langfuse context dumps; happy to attach the traced message sequence if useful.
Activity
fix(core): reconcile dangling tool_use on interrupt before persisting…
fix(agentscope-ai#2409): reconcile dangling ToolUseBlock on agent int…
oss-maintainer commentedon Jul 28, 2026
fix(core): reconcile dangling tool_use on interrupt before persisting…
fix(core): reconcile dangling tool_use on interrupt before persisting…