From dfc72ec5972c3eb444fba61e3d1bc759bf9f06d4 Mon Sep 17 00:00:00 2001 From: Guilhem Lemouel Date: Wed, 28 Oct 2026 04:02:21 -0700 Subject: [PATCH] fix: identify mentioned workspace items by storage path, not title Co-Authored-By: Claude Opus 5.5 --- .../lib/components/copilot/chat/AIChatInput.svelte | 14 ++++---------- .../lib/components/copilot/chat/context.test.ts | 11 +++++++++++ .../src/lib/components/copilot/chat/context.ts | 11 +++++++++-- 3 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 frontend/src/lib/components/copilot/chat/context.test.ts diff --git a/frontend/src/lib/components/copilot/chat/AIChatInput.svelte b/frontend/src/lib/components/copilot/chat/AIChatInput.svelte index ac6f21844e..14065bdbe4 100644 --- a/frontend/src/lib/components/copilot/chat/AIChatInput.svelte +++ b/frontend/src/lib/components/copilot/chat/AIChatInput.svelte @@ -6,6 +6,7 @@ import { contextElementKey, createAttachedFileContextElement, + isMentionContext, isSameContextElement, type AppDomSelectorElement, type ContextElement @@ -856,17 +857,12 @@ export async function addContextToSelection(contextElement: ContextElement) { if (!selectedContext || !availableContext) return - const alreadySelected = selectedContext.find( - (c) => c.type === contextElement.type && c.title === contextElement.title - ) + const alreadySelected = selectedContext.find((c) => isSameContextElement(c, contextElement)) if (alreadySelected) return // Workspace items are fetched on-demand and not in availableContext, // so skip the availableContext check for them - const isWorkspaceItem = - contextElement.type === 'workspace_script' || - contextElement.type === 'workspace_flow' || - contextElement.type === 'workspace_app' + const isWorkspaceItem = isMentionContext(contextElement) if ( !isWorkspaceItem && !availableContext.find( @@ -912,9 +908,7 @@ } } - const duplicateAfterAwait = selectedContext.find( - (c) => c.type === contextToAdd.type && c.title === contextToAdd.title - ) + const duplicateAfterAwait = selectedContext.find((c) => isSameContextElement(c, contextToAdd)) if (duplicateAfterAwait) return selectedContext = [...selectedContext, contextToAdd] diff --git a/frontend/src/lib/components/copilot/chat/context.test.ts b/frontend/src/lib/components/copilot/chat/context.test.ts new file mode 100644 index 0000000000..a0736ca1db --- /dev/null +++ b/frontend/src/lib/components/copilot/chat/context.test.ts @@ -0,0 +1,11 @@ +import { describe, expect, it } from 'vitest' +import { contextElementKey, isSameContextElement, type ContextElement } from './context' + +describe('mentioned workspace item identity', () => { + it('tells apart two drafts titled by the same draft path', () => { + const a: ContextElement = { type: 'workspace_flow', path: 'u/me/draft_a', title: 'u/me/report' } + const b: ContextElement = { type: 'workspace_flow', path: 'u/me/draft_b', title: 'u/me/report' } + expect(isSameContextElement(a, b)).toBe(false) + expect(contextElementKey(a)).not.toBe(contextElementKey(b)) + }) +}) diff --git a/frontend/src/lib/components/copilot/chat/context.ts b/frontend/src/lib/components/copilot/chat/context.ts index f57862c179..14ca8923c4 100644 --- a/frontend/src/lib/components/copilot/chat/context.ts +++ b/frontend/src/lib/components/copilot/chat/context.ts @@ -326,9 +326,13 @@ export type ContextElement = ( // DOM selector chips can share a display title (two `button.btn` from repeated // elements), so identify them by (appPath, selector) — keying or removing by // title would collide, giving repeated chips one Svelte key and deleting them -// together. Other context types stay identified by (type, title). +// together. Mentioned workspace items are titled by their draft path when draft-only, +// which two drafts can share, so they are identified by their storage path. Other +// context types stay identified by (type, title). export function contextElementKey(c: ContextElement): string { - return c.type === 'app_dom_selector' ? `dom:${c.appPath}:${c.selector}` : `${c.type}:${c.title}` + if (c.type === 'app_dom_selector') return `dom:${c.appPath}:${c.selector}` + if (isMentionContext(c)) return `${c.type}:${(c as WorkspaceScriptElement).path}` + return `${c.type}:${c.title}` } /** An `@`-mentioned workspace item, attached to one message the way a DOM pick @@ -343,5 +347,8 @@ export function isSameContextElement(a: ContextElement, b: ContextElement): bool if (a.type === 'app_dom_selector' && b.type === 'app_dom_selector') { return a.selector === b.selector && a.appPath === b.appPath } + if (isMentionContext(a) && isMentionContext(b)) { + return (a as WorkspaceScriptElement).path === (b as WorkspaceScriptElement).path + } return a.title === b.title }