The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a withdrawn suggestion pill drops its phase and cancels its work
Brooklyn Nicholson685a5c95ad89
diff --git a/apps/desktop/src/app/chat/composer/suggestion-pills.test.tsx b/apps/desktop/src/app/chat/composer/suggestion-pills.test.tsxnew file mode 100644index 00000000000..e8e46b32ba8--- /dev/null+++ b/apps/desktop/src/app/chat/composer/suggestion-pills.test.tsx@@ -0,0 +1,183 @@+import { act, render } from '@testing-library/react'+import { afterEach, describe, expect, it, vi } from 'vitest'++import { $composerSuggestionsBySession, type ComposerSuggestion, offerSuggestions } from '@/store/composer-suggestions'++import { SuggestionPills } from './suggestion-pills'++/**+ * The pill strip owns phase; the bus owns existence. These cover the seam+ * between them — what has to happen to a pill's phase and its in-flight work+ * when the bus withdraws it, which is the normal way a suggestion ends.+ */++interface Flow {+ resolve: () => void+ reject: (error: unknown) => void+ /** Latest value of the invoke context's `cancelled()` poll. */+ cancelled: () => boolean+}++function pill(id: string, provider = 'mcp'): { suggestion: ComposerSuggestion; flow: Flow } {+ const flow: Partial<Flow> = {}++ const suggestion: ComposerSuggestion = {+ doneLabel: `Added ${id}`,+ doneTip: 'done',+ id,+ invoke: context =>+ new Promise<void>((resolve, reject) => {+ flow.resolve = resolve+ flow.reject = reject+ flow.cancelled = context.cancelled+ }),+ label: `Add ${id}`,+ provider,+ tip: 'because',+ workingLabel: `Connecting ${id}…`,+ workingTip: 'cancel'+ }++ return { flow: flow as Flow, suggestion }+}++const labels = (container: HTMLElement) => [...container.querySelectorAll('button')].map(button => button.textContent)++const click = async (container: HTMLElement) => {+ await act(async () => {+ container.querySelector('button')!.click()+ })+}++afterEach(() => {+ $composerSuggestionsBySession.set({})+})++describe('SuggestionPills', () => {+ it('narrates idle → working → done through one invoke', async () => {+ const { flow, suggestion } = pill('github')+ const { container } = render(<SuggestionPills sessionId="s1" />)++ act(() => offerSuggestions('s1', 'mcp', [suggestion]))+ expect(labels(container)).toEqual(['Add github'])++ await click(container)+ expect(labels(container)).toEqual(['Connecting github…'])++ await act(async () => flow.resolve())+ expect(labels(container)).toEqual(['Added github'])+ })++ it('re-offering a withdrawn key starts from idle, not a stale done', async () => {+ const first = pill('github')+ const { container } = render(<SuggestionPills sessionId="s1" />)++ act(() => offerSuggestions('s1', 'mcp', [first.suggestion]))+ await click(container)+ await act(async () => first.flow.resolve())+ expect(labels(container)).toEqual(['Added github'])++ // The provider withdraws (draft lost the trigger) and offers again later.+ act(() => offerSuggestions('s1', 'mcp', []))+ act(() => offerSuggestions('s1', 'mcp', [pill('github').suggestion]))++ expect(labels(container)).toEqual(['Add github'])+ })++ it('a fresh offer is clickable again after an earlier one completed', async () => {+ const first = pill('github')+ const second = pill('github')+ const { container } = render(<SuggestionPills sessionId="s1" />)++ act(() => offerSuggestions('s1', 'mcp', [first.suggestion]))+ await click(container)+ await act(async () => first.flow.resolve())++ act(() => offerSuggestions('s1', 'mcp', []))+ act(() => offerSuggestions('s1', 'mcp', [second.suggestion]))+ await click(container)++ // A stale `done` swallows the click (only idle invokes) — this is the+ // user-visible half of the phase leak.+ expect(labels(container)).toEqual(['Connecting github…'])+ })++ it('cancels the in-flight action when the pill is withdrawn mid-flight', async () => {+ const { flow, suggestion } = pill('github')+ const { container } = render(<SuggestionPills sessionId="s1" />)++ act(() => offerSuggestions('s1', 'mcp', [suggestion]))+ await click(container)+ expect(flow.cancelled()).toBe(false)++ // The pill is the only cancel affordance; losing it must abort the work.+ act(() => offerSuggestions('s1', 'mcp', []))++ expect(labels(container)).toEqual([])+ expect(flow.cancelled()).toBe(true)+ })++ it('cancels in-flight work when the strip unmounts', async () => {+ const { flow, suggestion } = pill('github')+ const { container, unmount } = render(<SuggestionPills sessionId="s1" />)++ act(() => offerSuggestions('s1', 'mcp', [suggestion]))+ await click(container)++ unmount()++ expect(flow.cancelled()).toBe(true)+ })++ it('does not carry phase across a session switch on one mounted composer', async () => {+ const { flow, suggestion } = pill('github')+ const { container, rerender } = render(<SuggestionPills sessionId="s1" />)++ act(() => offerSuggestions('s1', 'mcp', [suggestion]))+ await click(container)+ await act(async () => flow.resolve())+ expect(labels(container)).toEqual(['Added github'])++ act(() => offerSuggestions('s2', 'mcp', [pill('github').suggestion]))+ rerender(<SuggestionPills sessionId="s2" />)++ expect(labels(container)).toEqual(['Add github'])+ })++ it('returns to idle when the provider rejects, so it can be retried', async () => {+ const { flow, suggestion } = pill('github')+ const { container } = render(<SuggestionPills sessionId="s1" />)++ act(() => offerSuggestions('s1', 'mcp', [suggestion]))+ await click(container)++ await act(async () => flow.reject(new Error('nope')))++ expect(labels(container)).toEqual(['Add github'])+ })++ it('a second click on a working pill requests cancel', async () => {+ const { flow, suggestion } = pill('github')+ const { container } = render(<SuggestionPills sessionId="s1" />)++ act(() => offerSuggestions('s1', 'mcp', [suggestion]))+ await click(container)+ await click(container)++ expect(flow.cancelled()).toBe(true)+ expect(labels(container)).toEqual(['Connecting github…'])+ })++ it('invokes each pill independently when two are offered', async () => {+ const a = pill('github')+ const b = pill('linear')+ const invoke = vi.spyOn(b.suggestion, 'invoke')+ const { container } = render(<SuggestionPills sessionId="s1" />)++ act(() => offerSuggestions('s1', 'mcp', [a.suggestion, b.suggestion]))+ await click(container)++ expect(labels(container)).toEqual(['Connecting github…', 'Add linear'])+ expect(invoke).not.toHaveBeenCalled()+ })+})diff --git a/apps/desktop/src/app/chat/composer/suggestion-pills.tsx b/apps/desktop/src/app/chat/composer/suggestion-pills.tsxindex ce926433c1e..e4562ae057e 100644--- a/apps/desktop/src/app/chat/composer/suggestion-pills.tsx+++ b/apps/desktop/src/app/chat/composer/suggestion-pills.tsx@@ -1,4 +1,4 @@-import { useState } from 'react'+import { useEffect, useState } from 'react'import { composerFloatingPill } from '@/components/chat/composer-dock'import { Codicon } from '@/components/ui/codicon'@@ -32,7 +32,19 @@ import { $composerSuggestionsBySession, markSuggestionInvoked, suggestionKey } ftype PillPhase = 'done' | 'idle' | 'working'+/**+ * Phase belongs to the pill the user is looking at, so it lives and dies with+ * that pill — the bus owns whether a suggestion exists, this owns only how far+ * along it is. Remounting per session enforces the half of that the strip+ * can't see: one composer stays mounted across a session switch, and phase+ * keys are `provider:id`, so without this "Added GitHub" in one chat renders+ * the next chat's genuine offer as already-done and inert.+ */export function SuggestionPills({ sessionId }: { sessionId: null | string }) {+ return <SessionSuggestionPills key={sessionId ?? ''} sessionId={sessionId} />+}++function SessionSuggestionPills({ sessionId }: { sessionId: null | string }) {const suggestions = useSessionSlice($composerSuggestionsBySession, sessionId)const [phases, setPhases] = useState<Record<string, PillPhase>>({})// Cancel flags outlive renders but never trigger them (poll-boundary abort).@@ -40,6 +52,51 @@ export function SuggestionPills({ sessionId }: { sessionId: null | string }) {const setPhase = (key: string, phase: PillPhase) => setPhases(current => ({ ...current, [key]: phase }))+ // A pill that leaves the strip takes its phase with it, and cancels whatever+ // it had in flight. Both halves matter:+ //+ // - Phase, because keys repeat. A provider withdraws and re-offers the same+ // `provider:id` constantly (the draft loses and regains the trigger word,+ // a connect fails and the word is still typed), and a `done` left on file+ // paints the fresh offer as "Added GitHub" — a pill that says it already+ // worked and ignores clicks, since only `idle` invokes.+ // - Cancellation, because a withdrawn pill is the user's ONLY cancel+ // affordance. Clicking a working pill sets this flag; once the pill is+ // gone nothing can, so an OAuth flow the user walked away from would poll+ // forever, hold the server's in-progress slot against a retry, and either+ // land a server they never confirmed or roll one back with no UI to say+ // so. Withdrawal aborts it at the next poll boundary and lets the+ // provider roll back.+ //+ // `suggestions` is reference-stable while the offered set is unchanged (the+ // bus preserves identity on no-op writes), so this runs on real churn only.+ useEffect(() => {+ const live = new Set(suggestions.map(suggestionKey))++ for (const key of cancels.keys()) {+ if (!live.has(key)) {+ cancels.set(key, true)+ }+ }++ setPhases(current => {+ const kept = Object.entries(current).filter(([key]) => live.has(key))++ return kept.length === Object.keys(current).length ? current : Object.fromEntries(kept)+ })+ }, [cancels, suggestions])++ // Unmount withdraws everything at once (composer closing, session switch+ // remounting this subtree) — same rule, same reason.+ useEffect(+ () => () => {+ for (const key of cancels.keys()) {+ cancels.set(key, true)+ }+ },+ [cancels]+ )+return suggestions.map(suggestion => {const key = suggestionKey(suggestion)const brand = suggestion.brand ? brandFor(suggestion.brand) : null@@ -66,6 +123,8 @@ export function SuggestionPills({ sessionId }: { sessionId: null | string }) {// Provider owns error surfacing (and swallows its own cancels);// the pill just returns to idle so it can be tried again.setPhase(key, 'idle')+ } finally {+ cancels.delete(key)}}