Skip to content

[Feature]: Promote interrupt(RuntimeContext) to the Agent interface so middlewares/tools can interrupt without casting to ReActAgent #1989

Closed

Description

@Buktal

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:

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:

  1. Cast to ReActAgent (breaks interface-oriented design)
  2. 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):

  1. Add to Agent interface (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); }
    }
    ```

  2. Override in ReActAgent with 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);
}
```

  1. 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); }
```

  1. 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:

  1. 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)
  2. Working methods not on the interface: `interrupt(RuntimeContext)` / `interrupt(userId, sessionId)` only exist on `ReActAgent`, not `Agent`
  3. No-arg version has wrong semantics: Hardcoded `defaultSessionId` causes silent failures in multi-session scenarios
  4. `HarnessAgent` incomplete delegation: Missing `interrupt(RuntimeContext)`, so casting doesn't even work through the wrapper
  5. Middleware has `ctx` but must cast: `onModelCall(Agent agent, RuntimeContext ctx, ...)` has both ingredients, but can't use them without `instanceof ReActAgent`
  6. 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.

Activity

  1. added
    enhancement
    area/core/agent
    on Jul 11, 2026
  2. moved this from Backlog to In progress in AgentScope Javaon Jul 12, 2026
  3. moved this from In progress to Done in AgentScope Javaon Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/agentenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions