-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(chat): share thinking and preserve natural activity labels #7810
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9ca79fa
fix(chat): use shared thinking and natural completion labels
waleedlatif1 6c79f92
fix(chat): share thinking across pending agent lanes
waleedlatif1 0cb037c
fix(chat): retain quiet gaps during streamed narration
waleedlatif1 d39bdb1
docs(chat): explain pending activity ownership
waleedlatif1 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
44 changes: 44 additions & 0 deletions
44
...orkspaceId]/home/components/message-content/components/agent-group/agent-group-content.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,44 @@ | ||
| import type { | ||
| AgentGroupItem, | ||
| NestedAgentGroup, | ||
| } from '@/app/workspace/[workspaceId]/home/components/message-content/components/agent-group/agent-group-view' | ||
| import type { ToolCallData } from '@/app/workspace/[workspaceId]/home/types' | ||
|
|
||
| /** Empty agent lanes share the turn's thinking indicator until they have output. */ | ||
| export function hasAgentGroupItemContent(item: AgentGroupItem): boolean { | ||
| switch (item.type) { | ||
| case 'tool': | ||
| return true | ||
| case 'text': | ||
| return item.content.trim().length > 0 | ||
| case 'agent_group': | ||
| return item.group.items.some(hasAgentGroupItemContent) | ||
| } | ||
| } | ||
|
|
||
| /** Finds empty live lanes at any depth whose wait belongs to the turn indicator. */ | ||
| export function hasPendingAgentGroup( | ||
| group: Pick<NestedAgentGroup, 'items' | 'isOpen' | 'isDelegating'> | ||
| ): boolean { | ||
| return ( | ||
| ((group.isOpen || group.isDelegating) && !group.items.some(hasAgentGroupItemContent)) || | ||
| group.items.some((item) => item.type === 'agent_group' && hasPendingAgentGroup(item.group)) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Every tool in a group, in stream order, including those run by nested | ||
| * agents. A parent's status line speaks for the whole subtree it delegated, | ||
| * so a grandchild's work is what surfaces while the parent itself waits. | ||
| */ | ||
| export function collectGroupTools(items: AgentGroupItem[]): ToolCallData[] { | ||
| const tools: ToolCallData[] = [] | ||
| const walk = (list: AgentGroupItem[]) => { | ||
| for (const item of list) { | ||
| if (item.type === 'tool') tools.push(item.data) | ||
| else if (item.type === 'agent_group') walk(item.group.items) | ||
| } | ||
| } | ||
| walk(items) | ||
| return tools | ||
| } | ||
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
200 changes: 200 additions & 0 deletions
200
...workspace/[workspaceId]/home/components/message-content/message-content-thinking.test.tsx
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,200 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { act } from 'react' | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
| import { createRoot, type Root } from 'react-dom/client' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { MessageContent } from '@/app/workspace/[workspaceId]/home/components/message-content/message-content' | ||
| import type { ContentBlock, ToolCallStatus } from '@/app/workspace/[workspaceId]/home/types' | ||
|
|
||
| vi.mock('@/lib/auth/auth-client', () => ({ | ||
| useSession: vi.fn(() => ({ data: null, isPending: false })), | ||
| })) | ||
|
|
||
| function start(name: string, parentSpanId = 'main'): ContentBlock { | ||
| return { type: 'subagent', content: name, spanId: name, parentSpanId, timestamp: 1 } | ||
| } | ||
|
|
||
| function tool(spanId: string, status: ToolCallStatus = 'executing'): ContentBlock { | ||
| return { | ||
| type: 'tool_call', | ||
| spanId, | ||
| toolCall: { | ||
| id: `${spanId}-read`, | ||
| name: 'read', | ||
| calledBy: spanId, | ||
| status, | ||
| activityDescription: `Reading ${spanId} notes`, | ||
| }, | ||
| timestamp: 2, | ||
| } | ||
| } | ||
|
|
||
| describe('MessageContent shared thinking indicator', () => { | ||
| let queryClient: QueryClient | ||
| let root: Root | ||
| let container: HTMLDivElement | ||
|
|
||
| beforeEach(() => { | ||
| queryClient = new QueryClient() | ||
| vi.useFakeTimers() | ||
| vi.stubGlobal('matchMedia', vi.fn().mockReturnValue({ matches: false })) | ||
| ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true | ||
| container = document.createElement('div') | ||
| document.body.appendChild(container) | ||
| root = createRoot(container) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| act(() => root.unmount()) | ||
| container.remove() | ||
| queryClient.clear() | ||
| vi.useRealTimers() | ||
| vi.unstubAllGlobals() | ||
| }) | ||
|
|
||
| const render = (blocks: ContentBlock[], isStreaming = true) => | ||
| act(() => { | ||
| root.render( | ||
| <QueryClientProvider client={queryClient}> | ||
| <MessageContent blocks={blocks} fallbackContent='' isStreaming={isStreaming} isLast /> | ||
| </QueryClientProvider> | ||
| ) | ||
| }) | ||
| const thinking = () => container.querySelectorAll('[aria-hidden="false"] svg') | ||
| const groups = () => container.querySelectorAll('[data-agent-group]') | ||
|
|
||
| it('shares one indicator across parallel and nested empty agents', () => { | ||
| render([start('workflow'), start('browser'), start('deploy', 'workflow')]) | ||
| expect(thinking()).toHaveLength(1) | ||
| expect(groups()).toHaveLength(0) | ||
| expect(container.querySelector('[role="status"]')).toBeNull() | ||
| }) | ||
|
|
||
| it('hands off immediately to meaningful activity without remounting existing rows', () => { | ||
| const blocks = [start('workflow'), start('browser')] | ||
| render(blocks) | ||
| expect(thinking()).toHaveLength(1) | ||
| render([...blocks, tool('workflow')]) | ||
| expect(thinking()).toHaveLength(0) | ||
| expect(groups()).toHaveLength(1) | ||
| const firstRow = container.querySelector('[role="status"]') | ||
| expect(firstRow?.textContent).toBe('Reading workflow notes') | ||
| render([...blocks, tool('workflow'), tool('browser')]) | ||
| expect(groups()).toHaveLength(2) | ||
| expect(container.querySelector('[role="status"]')).toBe(firstRow) | ||
| expect(thinking()).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('shows the pending indicator after the only visible agent finishes', () => { | ||
| const blocks = [start('workflow'), start('browser'), tool('workflow', 'success')] | ||
| render(blocks) | ||
| expect(thinking()).toHaveLength(0) | ||
| render([...blocks, { type: 'subagent_end', spanId: 'workflow', timestamp: 3 }]) | ||
| expect(thinking()).toHaveLength(1) | ||
| expect(groups()).toHaveLength(1) | ||
| }) | ||
|
|
||
| it('keeps a singleton action flat when its nested agent has no output', () => { | ||
| render([start('workflow'), tool('workflow'), start('deploy', 'workflow')]) | ||
| expect(groups()).toHaveLength(1) | ||
| expect(container.querySelectorAll('[role="status"]')).toHaveLength(1) | ||
| expect(container.querySelector('[role="button"]')).toBeNull() | ||
| expect(thinking()).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('retains nested activity while another child is empty', () => { | ||
| render([ | ||
| start('workflow'), | ||
| start('deploy', 'workflow'), | ||
| start('browser', 'workflow'), | ||
| tool('deploy'), | ||
| ]) | ||
| const trigger = container.querySelector<HTMLElement>('[role="button"]')! | ||
| act(() => trigger.click()) | ||
| expect(container.querySelector('[data-state="open"]')?.textContent).toContain( | ||
| 'Reading deploy notes' | ||
| ) | ||
| expect(container.querySelectorAll('[role="status"]')).toHaveLength(2) | ||
| expect(thinking()).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('preserves narration and skips whitespace-only output', () => { | ||
| const blocks: ContentBlock[] = [ | ||
| start('workflow'), | ||
| { type: 'subagent_text', spanId: 'workflow', content: ' ', timestamp: 2 }, | ||
| ] | ||
| render(blocks) | ||
| expect(groups()).toHaveLength(0) | ||
| expect(thinking()).toHaveLength(1) | ||
| render( | ||
| [ | ||
| ...blocks, | ||
| { type: 'subagent_text', spanId: 'workflow', content: 'Checking the setup.', timestamp: 3 }, | ||
| ], | ||
| false | ||
| ) | ||
| expect(groups()).toHaveLength(1) | ||
| act(() => container.querySelector<HTMLElement>('[role="button"]')!.click()) | ||
| expect(container.querySelector('[data-state="open"]')?.textContent).toContain( | ||
| 'Checking the setup.' | ||
| ) | ||
| }) | ||
|
|
||
| it.each(['awaiting_approval', 'error', 'cancelled', 'rejected'] as const)( | ||
| 'keeps %s tool rows visible while another agent is pending', | ||
| (status) => { | ||
| render([start('workflow'), start('browser'), tool('workflow', status)]) | ||
| expect(groups()).toHaveLength(1) | ||
| expect(container.querySelector('[role="status"]')?.textContent).toContain('workflow notes') | ||
| expect(thinking()).toHaveLength(1) | ||
| } | ||
| ) | ||
|
|
||
| it('keeps thinking hidden while prose streams and finishes revealing', () => { | ||
| const blocks: ContentBlock[] = [ | ||
| start('browser'), | ||
| { | ||
| type: 'text', | ||
| content: 'Here is the result of reviewing the project and checking its configuration.', | ||
| timestamp: 3, | ||
| }, | ||
| ] | ||
| render([start('browser'), { type: 'text', content: 'Here is', timestamp: 3 }]) | ||
| render(blocks) | ||
| act(() => vi.advanceTimersByTime(100)) | ||
| expect(thinking()).toHaveLength(0) | ||
| render(blocks, false) | ||
| expect(thinking()).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('waits for the normal quiet period between prose chunks while an agent is pending', () => { | ||
| const prose = 'Here is the result of reviewing the project and checking its configuration.' | ||
| const blocks: ContentBlock[] = [ | ||
| start('browser'), | ||
| { type: 'text', content: prose, timestamp: 3 }, | ||
| ] | ||
| render(blocks) | ||
| expect(thinking()).toHaveLength(0) | ||
| act(() => vi.advanceTimersByTime(1_499)) | ||
| expect(thinking()).toHaveLength(0) | ||
| act(() => vi.advanceTimersByTime(1)) | ||
| expect(thinking()).toHaveLength(1) | ||
| render([...blocks, { type: 'text', content: ' The configuration is valid.', timestamp: 4 }]) | ||
| expect(thinking()).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('removes thinking when a turn stops or finishes without agent output', () => { | ||
| const blocks = [start('workflow'), start('browser')] | ||
| render(blocks) | ||
| expect(thinking()).toHaveLength(1) | ||
| render([...blocks, { type: 'stopped', timestamp: 3 }], false) | ||
| expect(thinking()).toHaveLength(0) | ||
| expect(groups()).toHaveLength(0) | ||
| expect(container.textContent).toContain('Stopped') | ||
| render(blocks, false) | ||
| expect(thinking()).toHaveLength(0) | ||
| expect(groups()).toHaveLength(0) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.