fix: identify mentioned workspace items by storage path, not title

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Guilhem Lemouel
2026-10-28 04:02:21 -07:00
co-authored by Claude Opus 5.5
parent 62c72946d6
commit dfc72ec597
3 changed files with 24 additions and 12 deletions
@@ -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]
@@ -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))
})
})
@@ -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
}