Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(palette): rank ⌘K results before the 50-row cap

review7084cc4556e7
diff --git a/src/renderer/components/CommandPalette.tsx b/src/renderer/components/CommandPalette.tsx
index fb410dd0c..899d27fbc 100644
--- a/src/renderer/components/CommandPalette.tsx
+++ b/src/renderer/components/CommandPalette.tsx
@@ -1,6 +1,7 @@
import { useMemo, useState, type ReactNode } from 'react'
import { createPortal } from 'react-dom'
import { rankQuickOpenFiles, type QuickOpenIndexedFile } from '../lib/quickOpenSearch'
+import { contentHit as contentHitQ, labelHit as labelHitQ, rankPaletteCommands } from '../lib/paletteSearch'
import { IconEditor } from './icons'
export interface Command {
@@ -40,19 +41,6 @@ interface CommandPaletteProps {
extraCommands?: Command[]
}
-/** Case-insensitive subsequence match — "ntr" matches "New TeRminal". */
-function matches(label: string, q: string): boolean {
- if (!q) return true
- const s = label.toLowerCase()
- let i = 0
- for (const ch of q.toLowerCase()) {
- i = s.indexOf(ch, i)
- if (i === -1) return false
- i++
- }
- return true
-}
-
/** Cmd/Ctrl+K command palette: fuzzy-filter actions and jump targets, Enter to run. */
export function CommandPalette({
commands,
@@ -66,15 +54,11 @@ export function CommandPalette({
const [query, setQuery] = useState('')
const [active, setActive] = useState(0)
- // Fuzzy-match label+hint; also substring-match the body text (e.g. terminal output).
- const contentHit = (c: Command) =>
- query.length >= 2 && !!c.content && c.content.toLowerCase().includes(query.toLowerCase())
- const labelHit = (c: Command) => matches(`${c.label} ${c.hint ?? ''}`, query)
+ // Rank (label substring > hint > fuzzy > body text) BEFORE the cap — see lib/paletteSearch.
+ const contentHit = (c: Command) => contentHitQ(c, query)
+ const labelHit = (c: Command) => labelHitQ(c, query)
- const filtered = useMemo(
- () => commands.filter((c) => labelHit(c) || contentHit(c)).slice(0, 50),
- [commands, query]
- )
+ const filtered = useMemo(() => rankPaletteCommands(commands, query, 50), [commands, query])
const fileCommands = useMemo<Command[]>(() => {
if (!fileIndex || !onOpenFile || query.trim().length < 1) return []
diff --git a/src/renderer/lib/paletteSearch.test.ts b/src/renderer/lib/paletteSearch.test.ts
new file mode 100644
index 000000000..a2ad49603
--- /dev/null
+++ b/src/renderer/lib/paletteSearch.test.ts
@@ -0,0 +1,44 @@
+import { describe, it, expect } from 'vitest'
+import { paletteScore, rankPaletteCommands } from './paletteSearch'
+
+const action = (label: string, hint?: string, section?: string) => ({ label, hint, section })
+
+describe('rankPaletteCommands', () => {
+ it('finds a node titled mid-string even behind more than `limit` scattered action hits', () => {
+ // 60 actions whose hints subsequence-match "seo" (s…e…o), declared BEFORE the node row.
+ const actions = Array.from({ length: 60 }, (_, i) =>
+ action(`Spawn a team ${i}`, 'orchestrate parallelize delegate agents conductor')
+ )
+ const node = action('Go to Ottasilver SEO değerlendirmesi', undefined, 'Opened terminals')
+ const out = rankPaletteCommands([...actions, node], 'SEO', 50)
+ expect(out[0]).toBe(node)
+ expect(out).toHaveLength(50)
+ })
+
+ it('ranks a word-start label hit above a mid-word one, and label above hint', () => {
+ const mid = action('Go to reseo')
+ const hintOnly = action('Something', 'seo')
+ const start = action('Go to SEO audit')
+ expect(rankPaletteCommands([hintOnly, mid, start], 'seo', 50)).toEqual([start, mid, hintOnly])
+ })
+
+ it('keeps a section together, ordered by its best row', () => {
+ const a1 = action('View seo one', undefined, 'View')
+ const n1 = action('Go to x', 'seo', 'Opened terminals')
+ const a2 = action('Seo two', undefined, 'View')
+ const out = rankPaletteCommands([a1, n1, a2], 'seo', 50)
+ expect(out.map((c) => c.section)).toEqual(['View', 'View', 'Opened terminals'])
+ })
+
+ it('empty query keeps declaration order', () => {
+ const a = action('b')
+ const b = action('a')
+ expect(rankPaletteCommands([a, b], ' ', 50)).toEqual([a, b])
+ })
+
+ it('still matches subsequences and content, but below substrings', () => {
+ expect(paletteScore(action('New Terminal'), 'ntr')).toBe(3)
+ expect(paletteScore({ label: 'Go to x', content: 'npm run build' }, 'run b')).toBe(5)
+ expect(paletteScore(action('Go to x'), 'zz')).toBeNull()
+ })
+})
diff --git a/src/renderer/lib/paletteSearch.ts b/src/renderer/lib/paletteSearch.ts
new file mode 100644
index 000000000..47da7df5d
--- /dev/null
+++ b/src/renderer/lib/paletteSearch.ts
@@ -0,0 +1,94 @@
+/**
+ * ⌘K palette filtering + ranking (pure).
+ *
+ * The palette used to FILTER only: a case-insensitive subsequence match over label+hint, kept in
+ * declaration order and cut at the display cap. Actions are declared before the "Go to <node>"
+ * rows, and their keyword-rich hints ("orchestrate parallelize delegate…") subsequence-match
+ * almost any short query — so typing "SEO" filled the cap with actions that merely contain an s,
+ * an e and an o, and the node actually TITLED "… SEO …" was cut off. Ranking before the cap is
+ * the fix: a contiguous hit in the label beats a scattered one, wherever the row was declared.
+ */
+
+export interface PaletteSearchable {
+ label: string
+ hint?: string
+ section?: string
+ /** Body text (e.g. a terminal's visible output) — matched by substring only. */
+ content?: string
+}
+
+/** Case-insensitive subsequence match — "ntr" matches "New TeRminal". */
+export function subsequenceMatch(text: string, q: string): boolean {
+ if (!q) return true
+ const s = text.toLowerCase()
+ let i = 0
+ for (const ch of q.toLowerCase()) {
+ i = s.indexOf(ch, i)
+ if (i === -1) return false
+ i++
+ }
+ return true
+}
+
+export function labelHit(c: PaletteSearchable, q: string): boolean {
+ return subsequenceMatch(`${c.label} ${c.hint ?? ''}`, q)
+}
+
+export function contentHit(c: PaletteSearchable, q: string): boolean {
+ return q.length >= 2 && !!c.content && c.content.toLowerCase().includes(q.toLowerCase())
+}
+
+const isWordStart = (s: string, i: number): boolean => i === 0 || /[^\p{L}\p{N}]/u.test(s[i - 1])
+
+/**
+ * Lower = better; `null` = no match. Tiers, best first: label substring at a word start, label
+ * substring anywhere, hint substring, label subsequence, label+hint subsequence, output content.
+ */
+export function paletteScore(c: PaletteSearchable, q: string): number | null {
+ const query = q.trim().toLowerCase()
+ if (!query) return 0
+ const label = c.label.toLowerCase()
+ let at = label.indexOf(query)
+ while (at !== -1) {
+ if (isWordStart(label, at)) return 0
+ at = label.indexOf(query, at + 1)
+ }
+ if (label.includes(query)) return 1
+ if ((c.hint ?? '').toLowerCase().includes(query)) return 2
+ if (subsequenceMatch(c.label, query)) return 3
+ if (labelHit(c, query)) return 4
+ if (contentHit(c, query)) return 5
+ return null
+}
+
+/**
+ * Matching commands, best first, capped at `limit`. Rows keep their declaration order within a
+ * tier, and rows of one section stay together (the palette prints a header on every section
+ * change) — sections are ordered by their best row. An empty query returns the input order.
+ */
+export function rankPaletteCommands<T extends PaletteSearchable>(
+ commands: T[],
+ q: string,
+ limit: number
+): T[] {
+ if (!q.trim()) return commands.slice(0, limit)
+ const scored: { c: T; score: number; idx: number }[] = []
+ commands.forEach((c, idx) => {
+ const score = paletteScore(c, q)
+ if (score !== null) scored.push({ c, score, idx })
+ })
+ scored.sort((a, b) => a.score - b.score || a.idx - b.idx)
+ const top = scored.slice(0, limit)
+ const sectionRank = new Map<string, number>()
+ top.forEach((r, i) => {
+ const key = r.c.section ?? ''
+ if (!sectionRank.has(key)) sectionRank.set(key, i)
+ })
+ return top
+ .map((r, i) => ({ r, i }))
+ .sort(
+ (a, b) =>
+ sectionRank.get(a.r.c.section ?? '')! - sectionRank.get(b.r.c.section ?? '')! || a.i - b.i
+ )
+ .map(({ r }) => r.c)
+}