Class MemoryFlushMiddleware

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

public class MemoryFlushMiddleware extends Object implements HarnessRuntimeMiddleware
Middleware that triggers memory flush and message offload at the end of each agent call.

Runs in onAgent(io.agentscope.core.agent.Agent, io.agentscope.core.agent.RuntimeContext, io.agentscope.core.middleware.AgentInput, java.util.function.Function<io.agentscope.core.middleware.AgentInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>>)'s doOnComplete so long-term memories are extracted and persisted after every call, even when conversation compaction was not triggered during that call. The flush is fire-and-forget: the agent stream completes immediately while the extraction runs on a background scheduler. When CompactionMiddleware is active, it handles flush/offload for the messages it summarizes; this middleware covers the remaining tail of messages that were kept verbatim.

Flush is gated by a MemoryConfig.FlushTrigger:

Session transcript append is not handled here — see TranscriptMiddleware, which runs independently of memory flush so history stays complete even when flush is disabled.

Concurrent flushes for the same isolation key are serialised: at most one flush runs per key at a time, and pending flushes of the same conversation coalesce into a single queued task, because a queued task reads the conversation state only when it executes and the newest task therefore subsumes the ones it replaces. Flushes of distinct conversations under the same key are queued separately.

The class is thread-safe: instances hold only configuration, and the per-key queues live in a static map and are synchronised internally.

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

  • 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