-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cloudflare): Rotate agent conversation id on chat clear #22720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JPeer264
merged 2 commits into
jp/cloudflare-instrument-agents
from
jp/cloudflare-instrument-reset-conv-id
Jul 28, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
packages/cloudflare/test/instrumentChatAgentConversation.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| import { getCurrentScope } from '@sentry/core'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import { instrumentChatAgentConversation } from '../src/instrumentations/agents/instrumentChatAgentConversation'; | ||
| import type { AgentInternals } from '../src/instrumentations/agents/types'; | ||
|
|
||
| describe('instrumentChatAgentConversation', () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('does not set conversation id when agent name is empty string', () => { | ||
| const setConversationIdSpy = vi.spyOn(getCurrentScope(), 'setConversationId').mockImplementation(() => {}); | ||
|
|
||
| const obj: AgentInternals = { | ||
| name: '', | ||
| onChatMessage() { | ||
| return 'response'; | ||
| }, | ||
| }; | ||
|
|
||
| instrumentChatAgentConversation(obj); | ||
|
|
||
| obj.onChatMessage!(() => {}, {}); | ||
|
|
||
| expect(setConversationIdSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not set conversation id when agent name is undefined', () => { | ||
| const setConversationIdSpy = vi.spyOn(getCurrentScope(), 'setConversationId').mockImplementation(() => {}); | ||
|
|
||
| const obj: AgentInternals = { | ||
| onChatMessage() { | ||
| return 'response'; | ||
| }, | ||
| }; | ||
|
|
||
| instrumentChatAgentConversation(obj); | ||
|
|
||
| obj.onChatMessage!(() => {}, {}); | ||
|
|
||
| expect(setConversationIdSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('sets conversation id when agent name is present', () => { | ||
| const setConversationId = vi.fn(); | ||
| vi.spyOn(getCurrentScope(), 'setConversationId').mockImplementation(setConversationId); | ||
|
|
||
| const obj: AgentInternals = { | ||
| name: 'conversation-42', | ||
| onChatMessage() { | ||
| return 'response'; | ||
| }, | ||
| }; | ||
|
|
||
| instrumentChatAgentConversation(obj); | ||
|
|
||
| obj.onChatMessage!(() => {}, {}); | ||
|
|
||
| expect(setConversationId).toHaveBeenCalledWith('conversation-42'); | ||
| }); | ||
|
|
||
| it('forwards the return value from the original onChatMessage', () => { | ||
| const obj: AgentInternals = { | ||
| name: 'convo-1', | ||
| onChatMessage() { | ||
| return { output: 'hello' }; | ||
| }, | ||
| }; | ||
|
|
||
| instrumentChatAgentConversation(obj); | ||
|
|
||
| const result = obj.onChatMessage!(() => {}, {}); | ||
|
|
||
| expect(result).toEqual({ output: 'hello' }); | ||
| }); | ||
|
|
||
| it('leaves the agent untouched when onChatMessage is not defined', () => { | ||
| const obj: AgentInternals = { name: 'agent-1' }; | ||
|
|
||
| instrumentChatAgentConversation(obj); | ||
|
|
||
| expect('onChatMessage' in obj).toBe(false); | ||
| }); | ||
|
|
||
| it('uses the instance name as the conversation id before any clear', () => { | ||
| const setConversationId = vi.fn(); | ||
| vi.spyOn(getCurrentScope(), 'setConversationId').mockImplementation(setConversationId); | ||
|
|
||
| const obj: AgentInternals = { | ||
| name: 'session-7', | ||
| _emit() { | ||
| return undefined; | ||
| }, | ||
| onChatMessage() { | ||
| return 'response'; | ||
| }, | ||
| }; | ||
|
|
||
| instrumentChatAgentConversation(obj); | ||
|
|
||
| obj.onChatMessage!(() => {}, {}); | ||
|
|
||
| expect(setConversationId).toHaveBeenCalledWith('session-7'); | ||
| }); | ||
|
|
||
| it('rotates the conversation id on the message:clear observability event (fresh id, not the instance name)', () => { | ||
| const setConversationId = vi.fn(); | ||
| vi.spyOn(getCurrentScope(), 'setConversationId').mockImplementation(setConversationId); | ||
|
|
||
| const obj: AgentInternals = { | ||
| name: 'session-7', | ||
| _emit() { | ||
| return undefined; | ||
| }, | ||
| onChatMessage() { | ||
| return 'response'; | ||
| }, | ||
| }; | ||
|
|
||
| instrumentChatAgentConversation(obj); | ||
|
|
||
| // Simulate the SDK emitting the chat-clear observability event. | ||
| obj._emit!('message:clear'); | ||
|
|
||
| obj.onChatMessage!(() => {}, {}); | ||
|
|
||
| expect(setConversationId).toHaveBeenCalledTimes(1); | ||
| const rotated = setConversationId.mock.calls[0]?.[0] as string; | ||
| expect(typeof rotated).toBe('string'); | ||
| expect(rotated).not.toBe('session-7'); | ||
| expect(obj.__sentryConversationId).toBe(rotated); | ||
| }); | ||
|
|
||
| it('forwards message:clear to the original _emit', () => { | ||
| const received: Array<{ type: string; payload: unknown }> = []; | ||
| const obj: AgentInternals = { | ||
| name: 'session-7', | ||
| _emit(type: string, payload?: Record<string, unknown>) { | ||
| received.push({ type, payload }); | ||
| return undefined; | ||
| }, | ||
| onChatMessage() { | ||
| return 'response'; | ||
| }, | ||
| }; | ||
|
|
||
| instrumentChatAgentConversation(obj); | ||
|
|
||
| obj._emit!('message:clear', { source: 'user' }); | ||
| expect(received).toEqual([{ type: 'message:clear', payload: { source: 'user' } }]); | ||
| }); | ||
|
|
||
| it('does not rotate the conversation id for other observability events', () => { | ||
| const obj: AgentInternals = { | ||
| name: 'session-7', | ||
| _emit() { | ||
| return undefined; | ||
| }, | ||
| onChatMessage() { | ||
| return 'response'; | ||
| }, | ||
| }; | ||
|
|
||
| instrumentChatAgentConversation(obj); | ||
|
|
||
| obj._emit!('message:request'); | ||
| obj._emit!('rpc', { method: 'greet' }); | ||
|
|
||
| expect(obj.__sentryConversationId).toBeUndefined(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.