Repository navigation
Description
AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.
Is your feature request related to a problem? Please describe.
MiddlewareBase callbacks (e.g., onModelCall, onReasoning, onActing) receive both Agent agent and RuntimeContext ctx, but the Agent interface only provides:
void interrupt()— hardcoded to the agent'sdefaultSessionId(see Bug [Bug]: agent.interrupt() is a silent no-op in MiddlewareBase callbacks when the call carries a RuntimeContext #1988)void interrupt(Msg)— same slot-mismatch issue
The session-aware methods that actually work (interrupt(RuntimeContext) / interrupt(String userId, String sessionId, Msg)) are only on ReActAgent, not on the Agent interface. This forces middleware/tool authors to:
- Cast to
ReActAgent(breaks interface-oriented design) - Live with silent failures when
agent.interrupt()targets the wrong session
Evidence: SubAgentTool.java:200-229 already does this workaround:
```java
private void interruptAgent(Agent agent, RuntimeContext ctx) {
if (agent instanceof ReActAgent ra) {
ra.interrupt(ctx); // ← can't call this via Agent interface
}
}
```
Additionally, HarnessAgent does not delegate interrupt(RuntimeContext):
```java
// HarnessAgent.java:482-489
@OverRide public void interrupt() { delegate.interrupt(); }
@OverRide public void interrupt(Msg msg) { delegate.interrupt(msg); }
// ❌ missing: interrupt(RuntimeContext ctx)
```
So even if a middleware casts to ReActAgent, it won't work through a HarnessAgent wrapper.
Describe the solution you'd like
Proposed API (backward-compatible):
-
Add to
Agentinterface (with default delegation to preserve compatibility):
```java
// Agent.java
public interface Agent ... {
@deprecated void interrupt();
@deprecated void interrupt(Msg msg);// NEW — default delegates to legacy for compatibility
default void interrupt(RuntimeContext ctx) { interrupt(); }
default void interrupt(RuntimeContext ctx, Msg msg) { interrupt(msg); }
}
``` -
Override in
ReActAgentwith real per-session logic:
```java
// ReActAgent.java
@OverRide
public void interrupt(RuntimeContext ctx) {
interrupt(ctx, null);
}
@OverRide
public void interrupt(RuntimeContext ctx, Msg msg) {
String uid = ctx != null ? ctx.getUserId() : null;
String sid = ctx != null ? ctx.getSessionId() : null;
if (sid == null || sid.isBlank()) {
sid = defaultSessionId;
}
getAgentState(uid, sid).interruptControl().trigger(InterruptSource.USER, msg);
}
```
- Add missing delegate in
HarnessAgent:
```java
// HarnessAgent.java
@OverRide
public void interrupt(RuntimeContext ctx) { delegate.interrupt(ctx); }
@OverRide
public void interrupt(RuntimeContext ctx, Msg msg) { delegate.interrupt(ctx, msg); }
```
- Clean up (separate PR or follow-up):
- Mark legacy `interrupt()` / `interrupt(Msg)` as `@Deprecated(forRemoval = true)`
- Remove other deprecated interrupt-related methods (`interrupt(InterruptSource)`, `checkInterruptedAsync`, `resetInterruptFlag`, etc.)
- Update Javadoc to point users at the new methods
Result: Middlewares can write `agent.interrupt(ctx)` without casting, and it works correctly for all agent types.
Describe alternatives you've considered
Alternative A: New sub-interface `InterruptibleAgent`
```java
public interface InterruptibleAgent extends Agent {
void interrupt(RuntimeContext ctx);
void interrupt(RuntimeContext ctx, Msg msg);
}
```
- Pro: Clean separation of concerns (SOLID)
- Con: Middleware usage still requires `instanceof InterruptibleAgent` checks (better than casting, but still a branch)
- Con: Adds a new interface to the API surface
Alternative B: Bypass the `Agent` interface entirely
```java
// Direct access to the per-session interrupt signal
ctx.getAgentState().interruptControl().trigger(InterruptSource.USER, msg);
```
- Pro: Zero interface changes
- Con: Exposes `InterruptControl` internals (leaky abstraction)
- Con: Doesn't fix the misleading `Agent` contract (users will still think `interrupt()` works)
Why we prefer the proposed solution:
- Backward-compatible (default methods preserve existing behavior)
- Eliminates casting (middlewares use `agent.interrupt(ctx)` directly)
- Works uniformly across `ReActAgent` and `HarnessAgent`
- Minimal API churn (just adds overloads, doesn't break anything)
Additional context
The six ways the current interrupt API is messy:
- Interface contract vs. implementation mismatch: `Agent.interrupt()` Javadoc promises sets an interrupt flag that will be checked at appropriate checkpoints, but `AgentBase` implements it as `@Deprecated {}` (empty method)
- Working methods not on the interface: `interrupt(RuntimeContext)` / `interrupt(userId, sessionId)` only exist on `ReActAgent`, not `Agent`
- No-arg version has wrong semantics: Hardcoded `defaultSessionId` causes silent failures in multi-session scenarios
- `HarnessAgent` incomplete delegation: Missing `interrupt(RuntimeContext)`, so casting doesn't even work through the wrapper
- Middleware has `ctx` but must cast: `onModelCall(Agent agent, RuntimeContext ctx, ...)` has both ingredients, but can't use them without `instanceof ReActAgent`
- Deprecated method pile-up: `interrupt()`, `interrupt(Msg)`, `interrupt(InterruptSource)`, `checkInterruptedAsync`, `resetInterruptFlag`, `getInterruptFlag`, `getInterruptSource` — all deprecated, high cognitive load
Related: See Bug #1988 for a concrete reproduction of the silent-failure symptom.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity