Skip to content

fix(think): wrap stream-resume ACK fallback in sendIfOpen#1463

Merged
whoiskatrin merged 1 commit intomainfrom
devin/1777988047-think-replay-sendifopen
May 5, 2026
Merged

fix(think): wrap stream-resume ACK fallback in sendIfOpen#1463
whoiskatrin merged 1 commit intomainfrom
devin/1777988047-think-replay-sendifopen

Conversation

@whoiskatrin
Copy link
Copy Markdown
Contributor

@whoiskatrin whoiskatrin commented May 5, 2026

Summary

Follow-up to #1376. That PR hardened agents/chat and @cloudflare/ai-chat against the closed-WebSocket replay race but intentionally left @cloudflare/think for a separate PR. This is that PR.

ResumableStream.replayCompletedChunksByRequestId now returns false both when no completed stream is found and when a sendIfOpen call fails because the socket closed mid-replay. The matching fallback in packages/ai-chat/src/index.ts was updated to use sendIfOpen, but the identical pattern in packages/think/src/think.ts:_handleStreamResumeAck still used a bare connection.send() — so on the close-mid-replay path it would throw TypeError: WebSocket send() after close up through onMessage, which is exactly the failure mode #1376 set out to prevent.

This PR mirrors the sendIfOpen helper from @cloudflare/ai-chat into @cloudflare/think and routes the fallback done: true send through it.

Review & Testing Checklist for Human

  • Confirm copying sendIfOpen / isWebSocketClosedSendError into think.ts is preferred over exporting a shared helper from agents/chat (the same duplication already exists in @cloudflare/ai-chat and agents/chat/resumable-stream.ts).
  • Sanity check the changeset entry — @cloudflare/think patch — matches the release process you'd expect for this kind of resilience fix.

Notes

  • No behaviour change on the happy path: sendIfOpen only swallows TypeError: "WebSocket send() after close" and re-throws everything else.
  • Local verification: npm run check, npm run lint, and npx nx run @cloudflare/think:test (289 tests) all pass.

Link to Devin session: https://app.devin.ai/sessions/303193c332f24e08b4c8525adb58e285
Requested by: @whoiskatrin


Open in Devin Review

Mirror the sendIfOpen pattern that ai-chat already uses for the
stream-resume ACK fallback. Without this, when the WebSocket closes
during ResumableStream.replayCompletedChunksByRequestId the method
now returns false (since #1376), and the fallback bare connection.send()
in _handleStreamResumeAck throws TypeError: WebSocket send() after close
up through onMessage.
@devin-ai-integration
Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR that start with 'DevinAI' or '@devin'.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 5, 2026

🦋 Changeset detected

Latest commit: 0c5e260

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

This PR includes changesets to release 1 package
Name Type
@cloudflare/think Patch

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

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 5, 2026

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1463

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1463

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1463

hono-agents

npm i https://pkg.pr.new/hono-agents@1463

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1463

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1463

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1463

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1463

commit: 0c5e260

@whoiskatrin whoiskatrin merged commit ab2b1db into main May 5, 2026
4 checks passed
@whoiskatrin whoiskatrin deleted the devin/1777988047-think-replay-sendifopen branch May 5, 2026 13:52
@github-actions github-actions Bot mentioned this pull request May 5, 2026
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.

1 participant