Skip to content

Restart streams on error in Agent SDK#1759

Merged
bennycode merged 18 commits intomainfrom
bennycode-toxiproxy-tests
Mar 14, 2026
Merged

Restart streams on error in Agent SDK#1759
bennycode merged 18 commits intomainfrom
bennycode-toxiproxy-tests

Conversation

@bennycode
Copy link
Copy Markdown
Contributor

@bennycode bennycode commented Mar 13, 2026

Summary

Test plan

  • Observe CI results — reconnect tests should fail since Agent.ts has no auto-restart logic on this branch

Note

Add automatic stream restart with exponential backoff to Agent SDK

  • Agent now retries failed stream setup using exponential backoff (10 retries, 1s–30s delay) via ts-retry-promise, re-emitting 'start' on successful recovery.
  • A new #retryStreams method and reworked #handleStreamError coordinate restart state, guarding against concurrent restarts and respecting stop() calls.
  • Calling stop() sets a #stopped flag that prevents any pending or future automatic restarts from proceeding.
  • Adds a toxiproxy-based local test setup to simulate mid-stream disconnects and validate reconnect behavior.
  • Behavioral Change: stream errors that were previously unrecoverable now trigger retries; start() failures also schedule retries instead of failing silently.

Changes since #1759 opened

  • Removed early-return guard in Agent.conversations.streamAllMessages onValue handler [8e64c9e]
  • Changed DecodedMessage import to type-only import in Agent module [971e41a]
  • Reordered type imports in @xmtp/node-sdk import statement [3c24761]

Macroscope summarized 4f21526.

…nt fixes

Adds Toxiproxy to the dev Docker setup and reconnect/restart tests
to verify whether CI fails without the corresponding Agent.ts changes.
@bennycode bennycode requested a review from a team as a code owner March 13, 2026 08:00
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 13, 2026

🦋 Changeset detected

Latest commit: 3c24761

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@xmtp/agent-sdk Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
xmtp-chat-api-service Skipped Skipped Mar 14, 2026 1:28am
xmtp-js-xmtp-chat Skipped Skipped Mar 14, 2026 1:28am

Request Review

macroscopeapp[bot]
macroscopeapp bot previously approved these changes Mar 13, 2026
@macroscopeapp
Copy link
Copy Markdown

macroscopeapp bot commented Mar 13, 2026

Approvability

Verdict: Needs human review

This PR introduces significant runtime behavior changes - automatic stream reconnection with exponential backoff retry logic. While the author owns nearly all changed files and the implementation appears reasonable, the change modifies core error handling behavior that could affect applications using the SDK. The open review comment is a minor cleanup suggestion.

You can customize Macroscope's approvability policy. Learn more.

@vercel vercel bot temporarily deployed to Preview – xmtp-chat-api-service March 13, 2026 22:36 Inactive
@vercel vercel bot temporarily deployed to Preview – xmtp-js-xmtp-chat March 13, 2026 22:36 Inactive
@macroscopeapp macroscopeapp bot dismissed their stale review March 13, 2026 22:36

Dismissing prior approval to re-evaluate adfaea5

macroscopeapp[bot]
macroscopeapp bot previously approved these changes Mar 13, 2026
@vercel vercel bot temporarily deployed to Preview – xmtp-chat-api-service March 13, 2026 22:50 Inactive
@vercel vercel bot temporarily deployed to Preview – xmtp-js-xmtp-chat March 13, 2026 22:50 Inactive
@macroscopeapp macroscopeapp bot dismissed their stale review March 13, 2026 22:50

Dismissing prior approval to re-evaluate aef8c77

macroscopeapp[bot]
macroscopeapp bot previously approved these changes Mar 13, 2026
@vercel vercel bot temporarily deployed to Preview – xmtp-js-xmtp-chat March 13, 2026 22:58 Inactive
@vercel vercel bot temporarily deployed to Preview – xmtp-chat-api-service March 13, 2026 22:58 Inactive
@macroscopeapp macroscopeapp bot dismissed their stale review March 13, 2026 22:59

Dismissing prior approval to re-evaluate 22077bc

macroscopeapp[bot]
macroscopeapp bot previously approved these changes Mar 13, 2026
@bennycode bennycode changed the title test(agent-sdk): add reconnect tests without Agent fixes Restart streams on error in Agent SDK Mar 13, 2026
@vercel vercel bot temporarily deployed to Preview – xmtp-chat-api-service March 13, 2026 23:11 Inactive
@vercel vercel bot temporarily deployed to Preview – xmtp-js-xmtp-chat March 13, 2026 23:11 Inactive
@macroscopeapp macroscopeapp bot dismissed their stale review March 13, 2026 23:11

Dismissing prior approval to re-evaluate 423d196

macroscopeapp[bot]
macroscopeapp bot previously approved these changes Mar 13, 2026
…ry fails or agent is (#1760)

Fix agent lock not being released when stream recovery fails or agent is stopped

Co-authored-by: macroscopeapp[bot] <170038800+macroscopeapp[bot]@users.noreply.github.com>
@vercel vercel bot temporarily deployed to Preview – xmtp-js-xmtp-chat March 14, 2026 00:44 Inactive
@vercel vercel bot temporarily deployed to Preview – xmtp-chat-api-service March 14, 2026 00:44 Inactive
… exhausted retry err (#1761)

Remove try-catch around #retryStreams() to propagate exhausted retry errors

Co-authored-by: macroscopeapp[bot] <170038800+macroscopeapp[bot]@users.noreply.github.com>
@vercel vercel bot temporarily deployed to Preview – xmtp-chat-api-service March 14, 2026 00:54 Inactive
@vercel vercel bot temporarily deployed to Preview – xmtp-js-xmtp-chat March 14, 2026 00:54 Inactive
Updated the version of @xmtp/agent-sdk to minor and added restart on error functionality for streams.
// but we must handle it for proper types
if (!(message instanceof DecodedMessage)) {
return;
}
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.

this if statement is no longer necessary

@vercel vercel bot temporarily deployed to Preview – xmtp-chat-api-service March 14, 2026 01:22 Inactive
@vercel vercel bot temporarily deployed to Preview – xmtp-js-xmtp-chat March 14, 2026 01:22 Inactive
@bennycode bennycode enabled auto-merge (squash) March 14, 2026 01:23
@vercel vercel bot temporarily deployed to Preview – xmtp-js-xmtp-chat March 14, 2026 01:27 Inactive
@vercel vercel bot temporarily deployed to Preview – xmtp-chat-api-service March 14, 2026 01:28 Inactive
@vercel vercel bot temporarily deployed to Preview – xmtp-js-xmtp-chat March 14, 2026 01:28 Inactive
@vercel vercel bot temporarily deployed to Preview – xmtp-chat-api-service March 14, 2026 01:28 Inactive
@bennycode bennycode merged commit 3557ccb into main Mar 14, 2026
33 checks passed
@bennycode bennycode deleted the bennycode-toxiproxy-tests branch March 14, 2026 01:30
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