Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

whoiskatrin
Copy link
Collaborator

this fixes #420

so previously when we switch from threadId-1 to threadId-2, we would use agentUrlString as the cache key, which in this cause would be identical for both agents and the cache would return the msgs for thread1, instead of thread2

wrote tests for the initial bug, failed as expected, added agent.name to the cacheKey and now all is passing

Copy link

changeset-bot bot commented Oct 1, 2025

🦋 Changeset detected

Latest commit: 53ed507

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

This PR includes changesets to release 1 package
Name Type
agents 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

Update the cache key in useAgentChat to include the agent name, addressing issue #420.
Copy link

pkg-pr-new bot commented Oct 1, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@531

commit: 53ed507

@threepointone
Copy link
Collaborator

That's odd. Doesn't agentUrlString already include the name and id?

@whoiskatrin
Copy link
Collaborator Author

whoiskatrin commented Oct 1, 2025

That's odd. Doesn't agentUrlString already include the name and id?

so here is my theory: when you call useAgent with a new name, it just mutates the same socket. It updates agent.name = newName, but the socket itself is still hanging on to the old _pkurl until it reconnects and PartySocket runs setWSProperties again. During that gap, if you check the URL, you’ll still see the old one (including the old room ID), so it doesn’t line up with the new thread. That stale URL is what the cache picks up, which is why you end up serving the previous conversation.

On the other hand, if you unmount and remount, the socket gets rebuilt from scratch, so _pkurl is set with the new room right away, that's why it's not always happening, I think.

I re-wrote the tests (initial ones had wrong URLs), tests these one again and I think this is the fix

That's my main theory, correct me if I'm wrong

@whoiskatrin whoiskatrin marked this pull request as ready for review October 1, 2025 13:02
@whoiskatrin
Copy link
Collaborator Author

switching this to draft for now, gonna go dig into partysocket to see what's happening in there

@whoiskatrin whoiskatrin marked this pull request as draft October 3, 2025 13:40
@whoiskatrin whoiskatrin marked this pull request as ready for review October 11, 2025 17:41
Copy link
Collaborator

@threepointone threepointone left a comment

Choose a reason for hiding this comment

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

lgtm!

@whoiskatrin whoiskatrin merged commit cdfc590 into main Oct 11, 2025
3 checks passed
@whoiskatrin whoiskatrin deleted the fix-use-agent-chat branch October 11, 2025 18:42
@threepointone threepointone mentioned this pull request Oct 11, 2025
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.

useAgentChat, when re-render with a different agent instance, does not update messages to the new instance

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.