GracefulShutdownMiddleware
public final class GracefulShutdownMiddleware implements MiddlewareBase
io/agentscope/core/shutdown/GracefulShutdownMiddleware.java:53–96Graceful Shutdown 中间件,在智能体执行流程中提供横切扩展。位于 io.agentscope.core.shutdown 包,实现 MiddlewareBase。核心成员包括 onReasoning、onActing、GracefulShutdownMiddleware。
System middleware that integrates graceful shutdown into the agent lifecycle.
Per-call request registration and unregistration are handled by AgentBase.call():
each call registers a distinct requestId (carried on the Reactor Context under io.agentscope.core.agent.AgentBase#SHUTDOWN_REQUEST_ID_KEY) and unregisters it via
doFinally, guaranteeing that every registered request is always unregistered regardless
of success, error, or cancel — and that concurrent calls on one agent instance are tracked
independently.
This middleware is responsible for:
-
onAgent— deduplicate input if resuming from a shutdown-interrupted session -
onReasoning(doOnComplete) — checkpoint after reasoning -
onActing(doOnComplete) — checkpoint after acting
Shutdown checkpoints — when the system is in SHUTTING_DOWN state, the agent is interrupted at these safe points (after the current phase has fully completed), so reasoning/acting are allowed to complete before the interrupt is issued — output tokens are not wasted. Only when the global shutdown timeout is reached will the agent be force-interrupted mid-phase (handled by GracefulShutdownManager#enforceTimeoutAndInterrupt).
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public GracefulShutdownMiddleware(GracefulShutdownManager manager) | 初始化 `GracefulShutdownMiddleware` 实例。 Creates a new instance. |
方法摘要Method Summary
构造器详情Constructor Detail
GracefulShutdownMiddleware
public GracefulShutdownMiddleware(GracefulShutdownManager manager)
初始化 GracefulShutdownMiddleware 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
manager | GracefulShutdownManager | 管理器(类型 `GracefulShutdownManager`)。The manager. |
io/agentscope/core/shutdown/GracefulShutdownMiddleware.java:59–61方法详情Method Detail
onReasoning
public Flux<AgentEvent> onReasoning(Agent agent, RuntimeContext ctx, ReasoningInput input, Function<ReasoningInput, Flux<AgentEvent>> next)
处理推理事件。
Performs the on reasoning operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | Agent | 智能体(类型 `Agent`)。The agent. |
ctx | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The ctx. |
input | ReasoningInput | 输入消息(类型 `ReasoningInput`)。The input. |
next | Function<ReasoningInput, Flux<AgentEvent>> | 下一个(类型 `Function >`)。The next. |
Flux<AgentEvent>io/agentscope/core/shutdown/GracefulShutdownMiddleware.java:63–76onActing
public Flux<AgentEvent> onActing(Agent agent, RuntimeContext ctx, ActingInput input, Function<ActingInput, Flux<AgentEvent>> next)
处理执行事件。
Performs the on acting operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | Agent | 智能体(类型 `Agent`)。The agent. |
ctx | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The ctx. |
input | ActingInput | 输入消息(类型 `ActingInput`)。The input. |
next | Function<ActingInput, Flux<AgentEvent>> | 下一个(类型 `Function >`)。The next. |
Flux<AgentEvent>io/agentscope/core/shutdown/GracefulShutdownMiddleware.java:78–91