Skip to content

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

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

guslegend0510 wants to merge 16 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!

@xiaoyuuuuuupeng xiaoyuuuuuupeng left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — the approach looks good, and the tests cover the core #2821 scenario.

Two things I’d like to confirm:

ASYNC_FLUSH_SCHEDULER is static, so the whole process shares one worker and a queue of three. Different agents / workspaces don’t share the same MEMORY.md, so serializing writes doesn’t seem like a reason to share this queue across agents. I’m more worried about a slow flush blocking or rejecting another agent’s flush. Is the process-wide limit intentional, or would you consider splitting schedulers by isolation key / instance later?

Also, in memory.md under “When flush fires”, flush is described as fire-and-forget and not blocking call(), which doesn’t match the code — the default is still to wait synchronously for flush. Now that asyncFlush exists, it’d be good to fix that sentence.

@guslegend0510

Copy link
Copy Markdown
Contributor Author

ASYNC_FLUSH_SCHEDULER 是静态的,因此整个进程共享一个工作线程和一个包含三个线程的队列。不同的代理/工作区不共享同一个 MEMORY.md 文件,所以串行写入似乎不是跨代理共享此队列的理由。我更担心的是,缓慢的刷新操作会阻塞或拒绝其他代理的刷新操作。这种进程范围的限制是有意为之吗?或者您是否考虑过以后按隔离键/

感谢仔细审阅,这两个问题都很有价值。
进程级共享调度器是这个初始实现中有意采用的保守限制,主要用于控制 fire-and-forget 任务的总体数量,并不是出于文件写入安全的考虑。你说得对,这种设计确实可能造成不同 agent 之间的队头阻塞,队列饱和时还可能拒绝其他 agent 的 flush。
我倾向于后续通过按 isolation key 调度来解决:同一 isolation key 的 flush 保持顺序执行,不同 agent、workspace 或 isolation key 之间可以并行,同时补充完整的调度器生命周期管理。
至于文档描述,你指出的问题是正确的。我会在这个 PR 中同时修正中英文文档,明确说明:默认情况下,per-call flush 仍会同步等待;只有启用 asyncFlush(true) 后,它才会以 fire-and-forget 方式在后台执行。

@oss-maintainer

Copy link
Copy Markdown
Collaborator

⚠️ Merge conflict detected

This PR has conflicts with the main branch and cannot be merged. Please rebase or merge main into your branch and resolve the conflicts:

git fetch origin
git checkout codex/fix-2821-harness-async-memory-flush
git rebase origin/main
# resolve conflicts, then:
git push --force-with-lease

This is a one-time reminder. Feel free to @mention me for a re-review after conflicts are resolved.


Automated notification by github-manager-bot

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.

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

4 participants