Skip to content

Conversation

@yilinglu
Copy link

@yilinglu yilinglu commented Jul 25, 2025

This PR fixes a bug where streaming responses in Agent Squad don't save conversation history with the configured MAX_MESSAGE_PAIRS_PER_AGENT limit, potentially causing unlimited memory growth.

This fix ensures that conversation history is properly limited in streaming scenarios, preventing potential memory issues from unlimited message accumulation.

Issue Link (REQUIRED)

Fixes #365

Summary

The processStreamInBackground method was not passing the MAX_MESSAGE_PAIRS_PER_AGENT parameter to saveConversationExchange, while the non-streaming code path correctly included it. This inconsistency could lead to unlimited message history growth in streaming scenarios.

Changes

  • Add missing maxHistorySize parameter to saveConversationExchange call in processStreamInBackground
  • Add unit test to verify streaming responses save conversation history with correct maxHistorySize
  • Test ensures the bug is fixed and prevents regression

In the processStreamInBackground method of orchestrator.ts, the call to saveConversationExchange was missing the maxHistorySize parameter:

// Before (line 487-489)
await saveConversationExchange(
  userInput,
  fullResponse,
  this.storage,
  userId,
  sessionId,
  agent.id
  // Missing: this.config.MAX_MESSAGE_PAIRS_PER_AGENT
);

The non-streaming code path correctly includes this parameter, creating an inconsistency that could lead to:

  • Unlimited conversation history growth for streaming responses
  • Memory issues in long-running sessions
  • Inconsistent behavior between streaming and non-streaming modes

Solution

This PR adds the missing parameter to ensure consistent behavior:

// After
await saveConversationExchange(
  userInput,
  fullResponse,
  this.storage,
  userId,
  sessionId,
  agent.id,
  this.config.MAX_MESSAGE_PAIRS_PER_AGENT
);

User experience

After the fix, the user having long conversations with the AI app can successfully maintain conversation context (remember past conversation details) across multiple conversation turns while not having the risk of memory leak on the server to crash the server.
The operator of the agent squad backend won't get alarms on memory leak.

Checklist

If your change doesn't seem to apply, please leave them unchecked.

  • I have performed a self-review of this change
  • Changes have been tested
  • Changes are documented
  • I have linked this PR to an existing issue (required)
Is this a breaking change? This is not a breaking change

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

The processStreamInBackground method was not passing the MAX_MESSAGE_PAIRS_PER_AGENT
parameter to saveConversationExchange, while the non-streaming code path correctly
included it. This inconsistency could lead to unlimited message history growth in
streaming scenarios.

Changes:
- Add missing maxHistorySize parameter to saveConversationExchange call in processStreamInBackground
- Add unit test to verify streaming responses save conversation history with correct maxHistorySize
- Test ensures the bug is fixed and prevents regression

This fix ensures that conversation history is properly limited in streaming scenarios,
preventing potential memory issues from unlimited message accumulation.
@yilinglu yilinglu force-pushed the fix/streaming-conversation-save branch from 74c333f to a830588 Compare July 25, 2025 08:42
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.

Streaming responses don't respect MAX_MESSAGE_PAIRS_PER_AGENT configuration

1 participant