agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

MemoryFlushMiddleware flush blocks the conversation completion signal

Open chcodex opened this issue 3 months ago • 1 comments

Description

MemoryFlushManager.flushMemories() performs a full LLM extraction call plus disk writes, and it currently blocks the end of the agent's reply stream.

Location

agentscope-harness/src/main/java/io/agentscope/harness/agent/middleware/MemoryFlushMiddleware.java (lines 143-152):

return next.apply(input)
        .concatWith(
                Mono.defer(() -> doFlush(agent, rc))
                        .subscribeOn(Schedulers.boundedElastic())
                        .onErrorResume(e -> { ... })
                        .then(Mono.<AgentEvent>empty()));

Problem

concatWith chains the flush strictly after the main response stream. Although subscribeOn(Schedulers.boundedElastic()) moves the flush work off the event-loop thread, it does not change the timing: the downstream onComplete signal is only emitted after flushMemories finishes, i.e. after:

  1. reading MEMORY.md + today's daily ledger,
  2. one additional LLM extraction call via model.stream(...),
  3. writing the daily *.md file.

So every call's completion is delayed by an extra synchronous LLM round-trip, adding visible tail latency to every user turn (default FlushMode.ALWAYS).

Expected behaviour

The memory flush should be fully asynchronous/fire-and-forget w.r.t. the conversation stream — e.g. dispatched on a separate scheduler without being composed into the reply Flux (possibly bounded/rate-limited to avoid unbounded pending flushes), so the response completes independently of the flush.

Environment

  • Module: agentscope-harness
  • Default config: FlushTrigger.always()

chcodex avatar Aug 19 '26 02:08 chcodex

@jujn @chickenlj

chcodex avatar Aug 19 '26 02:08 chcodex