Skip to content

fix(harness): support asynchronous memory flush - #2833

Open
guslegend0510 wants to merge 2 commits into
agentscope-ai:mainfrom
guslegend0510:codex/fix-2821-harness-async-memory-flush
Open

fix(harness): support asynchronous memory flush#2833
guslegend0510 wants to merge 2 commits into
agentscope-ai:mainfrom
guslegend0510:codex/fix-2821-harness-async-memory-flush

Conversation

@guslegend0510

Copy link
Copy Markdown
Contributor

Summary

  • Add an opt-in MemoryConfig.asyncFlush(boolean) option for HarnessAgent
  • Preserve the existing synchronous behavior by default
  • Capture a snapshot of completed-call messages before starting the background flush
  • Run asynchronous flushes on a bounded scheduler to prevent an unbounded backlog
  • Isolate background flush failures from completed agent responses
  • Add English and Chinese documentation for the new option

Motivation

HarnessAgent currently waits for the per-call memory flush to complete before completing the response stream.

When the memory model responds slowly, this can significantly delay the agent response. The deprecated ReActAgent.longTermMemoryAsyncRecord(...) option does not apply to HarnessAgent.

This change provides an equivalent opt-in asynchronous mode:

MemoryConfig.builder()
    .asyncFlush(true)
    .build()

The default remains false, preserving the existing completion and persistence semantics.

Behavior

When asyncFlush is enabled:

  • The agent response completes without waiting for the memory model
  • Messages are copied when the response completes, preventing later calls from changing the flush input
  • Failures are logged without failing the completed response
  • A dedicated scheduler runs one flush at a time and queues up to three additional tasks
  • Saturated tasks are rejected and logged instead of creating an unbounded backlog

Asynchronous flushes are fire-and-forget. In-flight flushes are not awaited during HarnessAgent.close(), so applications that exit immediately may stop before persistence completes.

Testing

mvn -pl agentscope-harness -am test

Results:

  • Core: 2294 tests, 0 failures, 9 skipped
  • Harness: 839 tests, 0 failures, 3 skipped
  • Spotless passed

Fixes #2821

@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 15 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...arness/agent/middleware/MemoryFlushMiddleware.java 62.50% 13 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

开启长期记忆后,每次对话完成,会同步卡在那里等待,能不能做成异步的

2 participants