Class MemoryMaintenanceMiddleware

java.lang.Object
io.agentscope.harness.agent.middleware.MemoryMaintenanceMiddleware
All Implemented Interfaces:
io.agentscope.core.middleware.MiddlewareBase, HarnessRuntimeMiddleware

public class MemoryMaintenanceMiddleware extends Object implements HarnessRuntimeMiddleware
Middleware that performs periodic memory maintenance after each agent call.

Fires on the agent invocation completion (via onAgent doOnComplete, after MemoryFlushMiddleware) and is throttled by a configurable minimum gap so it does not run on every single call. The maintenance is fire-and-forget: the agent stream completes immediately while the maintenance runs on a background scheduler.

Maintenance steps executed in order:

  1. Expire daily memory files older than dailyFileRetentionDays by moving them to memory/archive/.
  2. Run LLM-based consolidation (MemoryConsolidator.consolidate(io.agentscope.core.agent.RuntimeContext)) if a consolidator is configured.
  3. Prune session log files older than sessionRetentionDays.

The throttle window is tracked per isolation key, which matches the memory data isolation in use:

  • Field Details

    • DEFAULT_MIN_GAP

      public static final Duration DEFAULT_MIN_GAP
      Default minimum gap between two maintenance runs.
  • Constructor Details

  • Method Details

    • onAgent

      public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> onAgent(io.agentscope.core.agent.Agent agent, io.agentscope.core.agent.RuntimeContext ctx, io.agentscope.core.middleware.AgentInput input, Function<io.agentscope.core.middleware.AgentInput,reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>> next)
      Specified by:
      onAgent in interface io.agentscope.core.middleware.MiddlewareBase