Skip to content

Enhance MemoryConversation with async initialization and fire-and-for…#116

Merged
OhYee merged 1 commit into
mainfrom
dev-0609
Jun 9, 2026
Merged

Enhance MemoryConversation with async initialization and fire-and-for…#116
OhYee merged 1 commit into
mainfrom
dev-0609

Conversation

@penghuima

Copy link
Copy Markdown
Collaborator

…get session handling

Change-Id: I4e3ce626e51d4b9b3a77fddcdbfa32b05a30959f

Thank you for creating a pull request to contribute to Serverless Devs agentrun-sdk-python code! Before you open the request please answer the following questions to help it be more easily integrated. Please check the boxes "[ ]" with "[x]" when done too.
Please select one of the PR types below to complete


Fix bugs

Bug detail

The specific manifestation of the bug or the associated issue.

Pull request tasks

  • Add test cases for the changes
  • Passed the CI test

Update docs

Reason for update

Why do you need to update your documentation?

Pull request tasks

  • Update Chinese documentation
  • Update English documentation

Add contributor

Contributed content

  • Code
  • Document

Content detail

if content_type == 'code' || content_type == 'document':
    please tell us `PR url`,like: https://github.com/Serverless-Devs/agentrun-sdk-python/pull/1
else:
    please describe your contribution in detail

Others

Reason for update

Why do you need to update your documentation?

…get session handling

Change-Id: I4e3ce626e51d4b9b3a77fddcdbfa32b05a30959f
Signed-off-by: 久氢 <mapenghui.mph@alibaba-inc.com>

Copilot AI 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.

Pull request overview

This PR updates MemoryConversation to be safer under concurrent async usage by preventing duplicate memory-store initialization, and to reduce streaming latency by persisting session/message data in background tasks rather than blocking the response stream.

Changes:

  • Add a per-instance async lock and split memory-store initialization into a locked init path to avoid concurrent double-initialization.
  • Switch session + conversation persistence to fire-and-forget background tasks (asyncio.create_task) to avoid blocking stream completion.
  • Update unit tests to account for background persistence timing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
agentrun/memory_collection/memory_conversation.py Adds async init locking and moves persistence to background tasks to avoid blocking streaming.
tests/unittests/memory_collection/test_memory_conversation.py Adds a helper to wait before assertions so background persistence tasks can complete.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +397 to +402
async def _put_session_bg():
try:
await memory_store.put_session(session)
except Exception as e:
logger.error(f"Failed to save session: {e}", exc_info=True)

Comment on lines +516 to 534
try:
await ms.put_message(msg)
sess.update_time = microseconds_timestamp()
await ms.update_session(sess)
logger.debug(
"Saved conversation: %d messages,"
" text length: %d chars,"
" tool_calls: %d, tool_results: %d",
n_msgs,
text_len,
n_tc,
n_tr,
)
except Exception as e:
logger.error(
"Failed to save conversation: %s",
e,
exc_info=True,
)
except Exception as e:
logger.error(f"Failed to save session: {e}", exc_info=True)

asyncio.create_task(_put_session_bg())
Comment on lines +12 to +15
async def _flush_bg_tasks():
"""Let fire-and-forget background tasks complete before assertions."""
await asyncio.sleep(0.05)

@OhYee OhYee merged commit bc76f73 into main Jun 9, 2026
3 of 4 checks passed
@OhYee OhYee deleted the dev-0609 branch June 9, 2026 08:58
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.

3 participants