Skip to content

The agent that grows with you

fix(desktop): suggestion pills wait for a completed word and stand down on workspace homonyms

Brooklyn Nicholsonfb1ee93a6333
diff --git a/apps/desktop/src/store/suggestion-providers/mcp.test.ts b/apps/desktop/src/store/suggestion-providers/mcp.test.ts
index 9de2ad08b0c..4fd644c95d6 100644
--- a/apps/desktop/src/store/suggestion-providers/mcp.test.ts
+++ b/apps/desktop/src/store/suggestion-providers/mcp.test.ts
@@ -75,4 +75,21 @@ describe('matchSuggestions', () => {
{ keyword: 'sentry.io', server: 'sentry' }
])
})
+
+ it('a keyword still under the caret does not fire yet', () => {
+ // The debounce elapses mid-thought; the word is only intent once
+ // something follows it.
+ expect(matchSuggestions('can you check linear', INDEX)).toEqual([])
+ expect(matchSuggestions('can you check linear ', INDEX)).toEqual([{ keyword: 'linear', server: 'linear' }])
+ expect(matchSuggestions('can you check linear?', INDEX)).toEqual([{ keyword: 'linear', server: 'linear' }])
+ })
+
+ it('a pasted URL fires even as the last thing in the draft', () => {
+ // Pasting is a deliberate act — the completed-word guard is keyword-only.
+ const index = [{ hosts: ['linear.app'], keywords: ['linear'], server: 'linear' }]
+
+ expect(matchSuggestions('look at https://linear.app/team/issue/ABC-1', index)).toEqual([
+ { keyword: 'linear.app', server: 'linear' }
+ ])
+ })
})
diff --git a/apps/desktop/src/store/suggestion-providers/mcp.ts b/apps/desktop/src/store/suggestion-providers/mcp.ts
index 7048aed84fb..72676c4291e 100644
--- a/apps/desktop/src/store/suggestion-providers/mcp.ts
+++ b/apps/desktop/src/store/suggestion-providers/mcp.ts
@@ -84,11 +84,32 @@ export interface McpMatch {
const MAX_MATCHES = 2
+// Whole-word (unicode-aware) keyword hit that the user has FINISHED typing:
+// at least one character must follow the match (the lookahead already
+// guarantees it's a boundary). A hit still under the caret — "figma" as the
+// last thing typed, debounce elapsed mid-thought — is not intent yet, it's
+// eavesdropping on a word in progress; the pill waits for the space/period.
+const keywordHit = (haystack: string, candidate: string): boolean => {
+ const pattern = new RegExp(
+ `(?<![\\p{L}\\p{N}])${candidate.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?![\\p{L}\\p{N}])`,
+ 'gu'
+ )
+
+ for (const match of haystack.matchAll(pattern)) {
+ if (match.index + match[0].length < haystack.length) {
+ return true
+ }
+ }
+
+ return false
+}
+
/** Pure matcher, exported for tests: pasted-link host hits (the strongest
- * intent signal) and whole-word (unicode-aware) keyword hits against the
- * draft, capped at MAX_MATCHES. */
+ * intent signal) and completed whole-word keyword hits against the draft,
+ * capped at MAX_MATCHES. Host hits skip the completed-word guard — a paste
+ * is a deliberate act, and the URL routinely ends the draft. */
export function matchSuggestions(text: string, index: KeywordEntry[]): McpMatch[] {
- const haystack = ` ${text.toLowerCase()} `
+ const haystack = text.toLowerCase()
const hosts = draftHosts(text)
const matches: McpMatch[] = []
@@ -98,14 +119,7 @@ export function matchSuggestions(text: string, index: KeywordEntry[]): McpMatch[
// Whole-word match so "linearly" doesn't suggest Linear. Directory
// keywords are lowercase; multi-word keywords match as phrases.
- const keyword =
- host ??
- entry.keywords.find(candidate =>
- new RegExp(
- `(?<![\\p{L}\\p{N}])${candidate.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?![\\p{L}\\p{N}])`,
- 'u'
- ).test(haystack)
- )
+ const keyword = host ?? entry.keywords.find(candidate => keywordHit(haystack, candidate))
if (keyword) {
matches.push({ keyword, server: entry.server })
diff --git a/apps/desktop/src/store/suggestion-providers/skill.test.ts b/apps/desktop/src/store/suggestion-providers/skill.test.ts
index 67eef945d31..69046e1632c 100644
--- a/apps/desktop/src/store/suggestion-providers/skill.test.ts
+++ b/apps/desktop/src/store/suggestion-providers/skill.test.ts
@@ -1,28 +1,64 @@
import { describe, expect, it } from 'vitest'
-import { skillPattern } from './skill'
+import { collidesWithWorkspace, skillHit, skillPattern } from './skill'
-describe('skillPattern', () => {
- it('matches the exact name as a whole word', () => {
- expect(skillPattern('perf').test('run the perf loop')).toBe(true)
- expect(skillPattern('perf').test('performance is bad')).toBe(false)
+// skillHit is the provider's real predicate: a whole-word match that the user
+// has finished typing (at least one character follows it).
+const hits = (name: string, draft: string) => skillHit(skillPattern(name), draft.toLowerCase())
+
+describe('skillPattern + skillHit', () => {
+ it('matches the exact name as a completed whole word', () => {
+ expect(hits('perf', 'run the perf loop')).toBe(true)
+ expect(hits('perf', 'performance is bad')).toBe(false)
})
it('hyphenated names also match spaced phrasing', () => {
- expect(skillPattern('pr-ready').test('make this pr ready')).toBe(true)
- expect(skillPattern('pr-ready').test('run pr-ready on it')).toBe(true)
- expect(skillPattern('pr_ready').test('pr ready please')).toBe(true)
+ expect(hits('pr-ready', 'make this pr ready pls')).toBe(true)
+ expect(hits('pr-ready', 'run pr-ready on it')).toBe(true)
+ expect(hits('pr_ready', 'pr ready please')).toBe(true)
})
it('never matches inside other words', () => {
- expect(skillPattern('read').test('i already did')).toBe(false)
- expect(skillPattern('work').test('reworked the layout')).toBe(false)
+ expect(hits('read', 'i already did')).toBe(false)
+ expect(hits('work', 'reworked the layout')).toBe(false)
// Suffix boundary includes hyphen: "clean" must not fire inside "clean-up"
// (a different skill may own that name).
- expect(skillPattern('clean').test('do a clean-up pass')).toBe(false)
+ expect(hits('clean', 'do a clean-up pass')).toBe(false)
})
it('is case-insensitive via lowercased input', () => {
- expect(skillPattern('Clean').test('please clean this diff')).toBe(true)
+ expect(hits('Clean', 'please clean this diff')).toBe(true)
+ })
+
+ it('a name still under the caret is not a hit yet', () => {
+ // The debounce fires while the word is the last thing typed — wait for
+ // the next keystroke to call it intent.
+ expect(hits('perf', 'run perf')).toBe(false)
+ expect(hits('perf', 'run perf ')).toBe(true)
+ expect(hits('perf', 'run perf.')).toBe(true)
+ })
+
+ it('an earlier completed occurrence still counts', () => {
+ expect(hits('perf', 'perf first, then more perf')).toBe(true)
+ })
+})
+
+describe('collidesWithWorkspace', () => {
+ it('suppresses a skill named exactly like the cwd folder', () => {
+ expect(collidesWithWorkspace('hermes-agent', '/Users/b/www/hermes-agent')).toBe(true)
+ })
+
+ it('suppresses inside worktree-suffixed folders too', () => {
+ expect(collidesWithWorkspace('hermes-agent', '/Users/b/www/hermes-agent-suggest')).toBe(true)
+ })
+
+ it('does not suppress on substring-only overlap', () => {
+ // "perf" inside "perfect-app" is not a homonym of the project.
+ expect(collidesWithWorkspace('perf', '/Users/b/www/perfect-app')).toBe(false)
+ expect(collidesWithWorkspace('clean', '/Users/b/www/hermes-agent')).toBe(false)
+ })
+
+ it('never collides when detached (empty cwd)', () => {
+ expect(collidesWithWorkspace('hermes-agent', '')).toBe(false)
})
})
diff --git a/apps/desktop/src/store/suggestion-providers/skill.ts b/apps/desktop/src/store/suggestion-providers/skill.ts
index 94b84f88859..963aaca3ea5 100644
--- a/apps/desktop/src/store/suggestion-providers/skill.ts
+++ b/apps/desktop/src/store/suggestion-providers/skill.ts
@@ -2,6 +2,7 @@ import { requestComposerFocus, requestComposerInsert } from '@/app/chat/composer
import { getSkills } from '@/hermes'
import { translateNow } from '@/i18n'
import { type ComposerSuggestion, registerDraftProvider } from '@/store/composer-suggestions'
+import { $currentCwd } from '@/store/session'
/**
* Skill-match draft provider: the draft names a skill the user has, so offer
@@ -41,7 +42,34 @@ const escape = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
export function skillPattern(name: string): RegExp {
const flexible = name.toLowerCase().split(/[-_]/).map(escape).join('[-_ ]')
- return new RegExp(`(?<![\\p{L}\\p{N}])${flexible}(?![\\p{L}\\p{N}-])`, 'u')
+ return new RegExp(`(?<![\\p{L}\\p{N}])${flexible}(?![\\p{L}\\p{N}-])`, 'gu')
+}
+
+/** Completed whole-word hit, exported for tests: the match only counts once
+ * at least one character follows it. The debounce fires mid-sentence, and a
+ * name still under the caret is a word in progress, not a request — the pill
+ * waiting for the next keystroke is the difference between "offering" and
+ * "reading over your shoulder". */
+export function skillHit(pattern: RegExp, haystack: string): boolean {
+ pattern.lastIndex = 0
+
+ for (const match of haystack.matchAll(pattern)) {
+ if (match.index + match[0].length < haystack.length) {
+ return true
+ }
+ }
+
+ return false
+}
+
+/** Workspace homonym guard, exported for tests: a skill named like the
+ * working directory is the project's name, not a request for the skill.
+ * Working in `~/www/hermes-agent`, the draft says "hermes-agent" constantly
+ * — a "Use skill: hermes-agent" pill on every mention is noise (the reported
+ * annoyance that prompted this guard). Boundary containment, not exact
+ * segment equality, so worktree dirs (`hermes-agent-suggest`) suppress too. */
+export function collidesWithWorkspace(name: string, cwd: string): boolean {
+ return new RegExp(`(?<![\\p{L}\\p{N}])${escape(name.toLowerCase())}(?![\\p{L}\\p{N}])`, 'u').test(cwd.toLowerCase())
}
async function loadIndex(): Promise<SkillIndexEntry[]> {
@@ -94,7 +122,10 @@ registerDraftProvider('skill', async ({ text }) => {
}
const haystack = text.toLowerCase()
+ const cwd = $currentCwd.get()
const skills = await loadIndex()
- return skills.filter(skill => skill.pattern.test(haystack)).map(skill => toSuggestion(skill.name))
+ return skills
+ .filter(skill => skillHit(skill.pattern, haystack) && !collidesWithWorkspace(skill.name, cwd))
+ .map(skill => toSuggestion(skill.name))
})