diff --git a/cli/src/modules/common/handlers/ripgrep.ts b/cli/src/modules/common/handlers/ripgrep.ts index 5b14a5b5..397739ac 100644 --- a/cli/src/modules/common/handlers/ripgrep.ts +++ b/cli/src/modules/common/handlers/ripgrep.ts @@ -1,13 +1,14 @@ import { logger } from '@/ui/logger' import { RPC_METHODS } from '@hapi/protocol/rpcMethods' import type { RpcHandlerManager } from '@/api/rpc/RpcHandlerManager' -import { run as runRipgrep } from '@/modules/ripgrep/index' +import { run as runRipgrep, runFileSearch, type FileSearchOptions } from '@/modules/ripgrep/index' import { validatePath } from '../pathSecurity' import { getErrorMessage, rpcError } from '../rpcResponses' interface RipgrepRequest { args: string[] cwd?: string + fileSearch?: FileSearchOptions } interface RipgrepResponse { @@ -30,7 +31,9 @@ export function registerRipgrepHandlers(rpcHandlerManager: RpcHandlerManager, wo } try { - const result = await runRipgrep(data.args, { cwd: data.cwd }) + const result = data.fileSearch + ? await runFileSearch(data.args, { ...data.fileSearch, cwd: data.cwd }) + : await runRipgrep(data.args, { cwd: data.cwd }) return { success: true, exitCode: result.exitCode, diff --git a/cli/src/modules/ripgrep/index.test.ts b/cli/src/modules/ripgrep/index.test.ts index be400bca..c7cd3cff 100644 --- a/cli/src/modules/ripgrep/index.test.ts +++ b/cli/src/modules/ripgrep/index.test.ts @@ -3,7 +3,7 @@ */ import { describe, it, expect } from 'vitest' -import { run } from './index' +import { matchesFileSearchPath, run, runFileSearch, selectFileSearchPaths } from './index' describe('ripgrep low-level wrapper', () => { it('should get version', async () => { @@ -39,4 +39,25 @@ describe('ripgrep low-level wrapper', () => { expect(result.exitCode).toBe(0) expect(result.stdout).toContain('describe') }) -}) \ No newline at end of file + + it('should apply shared wildcard semantics before enforcing the result limit', () => { + expect(selectFileSearchPaths([ + 'src/file.ts', + 'other.ts', + 'src/deep/file.ts' + ], 'src*.ts', 2)).toEqual([ + 'src/file.ts', + 'src/deep/file.ts' + ]) + }) + + it('should bound runner-side wildcard file-search output', async () => { + const query = 'src/modules/ripgrep/*.ts' + const result = await runFileSearch(['--files'], { query, limit: 1 }) + const paths = result.stdout.trim().split(/\r?\n/).filter(Boolean) + + expect(result.exitCode).toBe(0) + expect(paths).toHaveLength(1) + expect(matchesFileSearchPath(paths[0], query)).toBe(true) + }) +}) diff --git a/cli/src/modules/ripgrep/index.ts b/cli/src/modules/ripgrep/index.ts index ab137304..23433d65 100644 --- a/cli/src/modules/ripgrep/index.ts +++ b/cli/src/modules/ripgrep/index.ts @@ -3,8 +3,10 @@ */ import { spawn } from 'child_process'; +import { createInterface } from 'readline'; import { join, resolve } from 'path'; import { platform } from 'os'; +import { matchesSearchQuery } from '@hapi/protocol'; import { runtimePath } from '@/projectPath'; import { withBunRuntimeEnv } from '@/utils/bunRuntime'; @@ -18,6 +20,12 @@ export interface RipgrepOptions { cwd?: string } +export interface FileSearchOptions { + cwd?: string + query: string + limit: number +} + function getBinaryPath(): string { const platformName = platform(); const binaryName = platformName === 'win32' ? 'rg.exe' : 'rg'; @@ -58,3 +66,70 @@ export function run(args: string[], options?: RipgrepOptions): Promise, query: string, limit: number): string[] { + const matches: string[] = [] + const boundedLimit = Math.max(1, limit) + for (const path of paths) { + if (matchesFileSearchPath(path, query)) { + matches.push(path) + if (matches.length >= boundedLimit) break + } + } + return matches +} + +export function runFileSearch(args: string[], options: FileSearchOptions): Promise { + const binaryPath = getBinaryPath(); + const limit = Math.max(1, options.limit) + return new Promise((resolve, reject) => { + const child = spawn(binaryPath, args, { + stdio: ['pipe', 'pipe', 'pipe'], + cwd: options.cwd, + env: withBunRuntimeEnv(), + windowsHide: process.platform === 'win32' + }); + + const lines = createInterface({ input: child.stdout }); + const matchedPaths: string[] = []; + let stderr = ''; + let settled = false; + + child.stderr.on('data', (data) => { + stderr += data.toString(); + }); + + lines.on('line', (line) => { + if (matchedPaths.length >= limit) return; + if (matchesFileSearchPath(line, options.query)) { + matchedPaths.push(line); + if (matchedPaths.length >= limit) { + child.kill(); + } + } + }); + + child.on('close', (code) => { + if (settled) return; + settled = true; + lines.close(); + resolve({ + exitCode: code || 0, + stdout: matchedPaths.length > 0 ? `${matchedPaths.join('\n')}\n` : '', + stderr + }); + }); + + child.on('error', (err) => { + if (settled) return; + settled = true; + lines.close(); + reject(err); + }); + }); +} diff --git a/hub/src/sync/rpcGateway.ts b/hub/src/sync/rpcGateway.ts index 45412656..aa776676 100644 --- a/hub/src/sync/rpcGateway.ts +++ b/hub/src/sync/rpcGateway.ts @@ -61,6 +61,10 @@ export class RpcTargetMissingError extends Error { } export type RpcCommandResponse = CommandResponse +export type FileSearchOptions = { + query: string + limit: number +} export type RpcReadFileResponse = FileReadResponse export type RpcGeneratedImageResponse = GeneratedImageResponse export type RpcUploadFileResponse = UploadFileResponse @@ -319,8 +323,8 @@ export class RpcGateway { return await this.sessionRpc(sessionId, RPC_METHODS.DeleteUpload, { sessionId, path }) as RpcDeleteUploadResponse } - async runRipgrep(sessionId: string, args: string[], cwd?: string): Promise { - return await this.sessionRpc(sessionId, RPC_METHODS.Ripgrep, { args, cwd }) as RpcCommandResponse + async runRipgrep(sessionId: string, args: string[], cwd?: string, fileSearch?: FileSearchOptions): Promise { + return await this.sessionRpc(sessionId, RPC_METHODS.Ripgrep, { args, cwd, fileSearch }) as RpcCommandResponse } async listSlashCommands(sessionId: string, agent: string): Promise { diff --git a/hub/src/sync/syncEngine.ts b/hub/src/sync/syncEngine.ts index 6f1f610d..741d095f 100644 --- a/hub/src/sync/syncEngine.ts +++ b/hub/src/sync/syncEngine.ts @@ -33,6 +33,7 @@ import { selectForkTranscriptPrefix } from './forkTranscript' import { RpcGateway, RpcTargetMissingError, + type FileSearchOptions, type RpcCodexModel, type RpcCommandResponse, type RpcDeleteUploadResponse, @@ -3868,8 +3869,8 @@ export class SyncEngine { return await this.rpcGateway.deleteUploadFile(sessionId, path) } - async runRipgrep(sessionId: string, args: string[], cwd?: string): Promise { - return await this.rpcGateway.runRipgrep(sessionId, args, cwd) + async runRipgrep(sessionId: string, args: string[], cwd?: string, fileSearch?: FileSearchOptions): Promise { + return await this.rpcGateway.runRipgrep(sessionId, args, cwd, fileSearch) } async listSlashCommands(sessionId: string, agent: string): Promise { diff --git a/hub/src/web/routes/git.test.ts b/hub/src/web/routes/git.test.ts index fc278a0e..a368c3ad 100644 --- a/hub/src/web/routes/git.test.ts +++ b/hub/src/web/routes/git.test.ts @@ -92,10 +92,12 @@ describe('file search route', () => { metadata: { path: 'C:\\project' } } as unknown as Session let ripgrepArgs: string[] = [] + let fileSearchQuery: string | undefined const engine = { resolveSessionAccess: () => ({ ok: true as const, sessionId: 'session-1', session }), - runRipgrep: async (_sessionId: string, args: string[]) => { + runRipgrep: async (_sessionId: string, args: string[], _cwd: string, fileSearch?: { query: string }) => { ripgrepArgs = args + fileSearchQuery = fileSearch?.query return { success: true, stdout: 'src/nested/file.ts\n' } }, statFiles: async (_sessionId: string, paths: string[]) => ({ @@ -109,6 +111,7 @@ describe('file search route', () => { expect(response.status).toBe(200) expect(ripgrepArgs).toEqual(['--files', '--iglob', '*src/nested/file.ts*']) + expect(fileSearchQuery).toBe('src/nested/file.ts') }) it('preserves backslashes in POSIX search queries', async () => { @@ -135,7 +138,76 @@ describe('file search route', () => { const response = await buildApp(engine).request(`/api/sessions/session-1/files?${query}`) expect(response.status).toBe(200) - expect(ripgrepArgs).toEqual(['--files', '--iglob', '*src\\file\\name.ts*']) + expect(ripgrepArgs).toEqual(['--files', '--iglob', '*src\\\\file\\\\name.ts*']) + }) + + it('uses shared matching semantics for plain and wildcard queries', async () => { + const session = { + id: 'session-1', + namespace: 'default', + active: true, + metadata: { path: '/project' } + } as unknown as Session + const ripgrepArgs: string[][] = [] + const fileSearchOptions: Array<{ query: string; limit: number }> = [] + const stdout = [ + 'src/file.ts', + 'other.ts', + 'test-AB', + '!literal.ts', + '[ab]literal.ts', + '{a,b}literal.ts', + 'notes.txt' + ].join('\n') + const engine = { + resolveSessionAccess: () => ({ ok: true as const, sessionId: 'session-1', session }), + runRipgrep: async (_sessionId: string, args: string[], _cwd: string, fileSearch?: { query: string; limit: number }) => { + ripgrepArgs.push(args) + if (fileSearch) fileSearchOptions.push(fileSearch) + return { success: true, stdout } + }, + statFiles: async (_sessionId: string, paths: string[]) => ({ + success: true, + entries: paths.map((path) => ({ path, size: 1, modified: 1 })) + }) + } as unknown as Partial + + const app = buildApp(engine) + const queries: Array<[string, string[]]> = [ + ['.txt', ['notes.txt']], + ['*.ts', ['src/file.ts', 'other.ts', '!literal.ts', '[ab]literal.ts', '{a,b}literal.ts']], + ['test-%3F%3F', ['test-AB']], + ['%21*.ts', ['!literal.ts']], + ['%5Bab%5D*.ts', ['[ab]literal.ts']], + ['%7Ba%2Cb%7D*.ts', ['{a,b}literal.ts']], + ['src*.ts', ['src/file.ts']] + ] + + for (const [query, expected] of queries) { + const response = await app.request(`/api/sessions/session-1/files?query=${query}`) + expect(response.status).toBe(200) + const body = await response.json() as { files: Array<{ fullPath: string }> } + expect(body.files.map((file) => file.fullPath)).toEqual(expected) + } + + expect(ripgrepArgs).toEqual([ + ['--files', '--iglob', '*.txt*'], + ['--files'], + ['--files'], + ['--files'], + ['--files'], + ['--files'], + ['--files'] + ]) + expect(fileSearchOptions).toEqual([ + { query: '.txt', limit: 200 }, + { query: '*.ts', limit: 200 }, + { query: 'test-??', limit: 200 }, + { query: '!*.ts', limit: 200 }, + { query: '[ab]*.ts', limit: 200 }, + { query: '{a,b}*.ts', limit: 200 }, + { query: 'src*.ts', limit: 200 } + ]) }) it('adds size and modification metadata to search results', async () => { diff --git a/hub/src/web/routes/git.ts b/hub/src/web/routes/git.ts index 31c905e7..24dde9c5 100644 --- a/hub/src/web/routes/git.ts +++ b/hub/src/web/routes/git.ts @@ -1,4 +1,5 @@ import { Hono } from 'hono' +import { isWildcardSearch, matchesSearchQuery, toSearchGlob } from '@hapi/protocol' import { z } from 'zod' import type { SyncEngine } from '../../sync/syncEngine' import type { WebAppEnv } from '../middleware/auth' @@ -235,11 +236,16 @@ export function createGitRoutes(getSyncEngine: () => SyncEngine | null): Hono engine.runRipgrep(sessionResult.sessionId, args, sessionPath)) + const result = await runRpc(() => engine.runRipgrep( + sessionResult.sessionId, + args, + sessionPath, + { query: normalizedQuery, limit } + )) if (!result.success) { return c.json({ success: false, error: result.error ?? 'Failed to list files' }) } @@ -253,6 +259,7 @@ export function createGitRoutes(getSyncEngine: () => SyncEngine | null): Hono line.trim()) .filter((line) => line.length > 0) .map(normalizePath) + .filter((path) => !normalizedQuery || matchesSearchQuery(path, normalizedQuery)) .slice(0, limit) const metadataResult = await runRpc(() => engine.statFiles(sessionResult.sessionId, paths)) diff --git a/shared/src/index.ts b/shared/src/index.ts index 824e0d55..23590297 100644 --- a/shared/src/index.ts +++ b/shared/src/index.ts @@ -12,6 +12,7 @@ export * from './modes' export * from './resume' export * from './rpcMethods' export * from './runnerCapabilities' +export * from './search' export * from './socket' export * from './sessionSummary' export * from './sessionCitation' diff --git a/shared/src/search.test.ts b/shared/src/search.test.ts new file mode 100644 index 00000000..c1a5a6a5 --- /dev/null +++ b/shared/src/search.test.ts @@ -0,0 +1,45 @@ +import { describe, expect, it } from 'bun:test' +import { isWildcardSearch, matchesSearchQuery, toSearchGlob } from './search' + +describe('search wildcard helpers', () => { + it('only enables wildcard mode for star and question mark patterns', () => { + expect(isWildcardSearch('feature-*')).toBe(true) + expect(isWildcardSearch('file-??')).toBe(true) + expect(isWildcardSearch('feature-[ab]')).toBe(false) + expect(isWildcardSearch('feature')).toBe(false) + }) + + it('keeps plain text matching as a case-insensitive substring search', () => { + expect(matchesSearchQuery('Fix Bot Review', 'bot review')).toBe(true) + expect(matchesSearchQuery('Fix Bot Review', 'BOT')).toBe(true) + expect(matchesSearchQuery('Fix Bot Review', 'reviewed')).toBe(false) + }) + + it('matches complete wildcard patterns with star and question mark', () => { + expect(matchesSearchQuery('feature-sidebar-search', 'feature-*')).toBe(true) + expect(matchesSearchQuery('feature-sidebar-search', 'feature-????')).toBe(false) + expect(matchesSearchQuery('file-01.ts', 'file-??.ts')).toBe(true) + expect(matchesSearchQuery('file-001.ts', 'file-??.ts')).toBe(false) + expect(matchesSearchQuery('src/file.ts', '*.ts')).toBe(true) + expect(matchesSearchQuery('src/file.ts.bak', '*.ts')).toBe(false) + }) + + it('treats non-wildcard characters as literals', () => { + expect(matchesSearchQuery('file1.ts', 'file?.ts')).toBe(true) + expect(matchesSearchQuery('file+.ts', 'file+.ts')).toBe(true) + expect(matchesSearchQuery('fileX.ts', 'file+.ts')).toBe(false) + }) + + it('handles adversarial wildcard patterns without regex backtracking', () => { + expect(matchesSearchQuery('a'.repeat(100), '*a*a*a*a*a*a*b')).toBe(false) + }) + + it('keeps ripgrep prefilters literal outside the supported wildcard operators', () => { + expect(toSearchGlob(' .ts ')).toBe('*.ts*') + expect(toSearchGlob('*.ts')).toBe('*.ts') + expect(toSearchGlob('!*.ts')).toBe('\\!*.ts') + expect(toSearchGlob('[ab]*.ts')).toBe('\\[ab\\]*.ts') + expect(toSearchGlob('{a,b}*.ts')).toBe('\\{a,b\\}*.ts') + }) + +}) diff --git a/shared/src/search.ts b/shared/src/search.ts new file mode 100644 index 00000000..f22f95ae --- /dev/null +++ b/shared/src/search.ts @@ -0,0 +1,67 @@ +/** Whether a search query should be interpreted as a wildcard pattern. */ +export function isWildcardSearch(query: string): boolean { + return query.includes('*') || query.includes('?') +} + +function matchesWildcard(value: string, pattern: string): boolean { + const input = Array.from(value.toLowerCase()) + const tokens = Array.from(pattern.toLowerCase()) + let inputIndex = 0 + let tokenIndex = 0 + let starIndex = -1 + let retryInputIndex = 0 + + while (inputIndex < input.length) { + const token = tokens[tokenIndex] + if (token !== undefined && (token === '?' || token === input[inputIndex])) { + inputIndex++ + tokenIndex++ + } else if (token === '*') { + starIndex = tokenIndex++ + retryInputIndex = inputIndex + } else if (starIndex >= 0) { + tokenIndex = starIndex + 1 + inputIndex = ++retryInputIndex + } else { + return false + } + } + + while (tokens[tokenIndex] === '*') { + tokenIndex++ + } + return tokenIndex === tokens.length +} + +/** Match a value using the existing substring behavior or supported wildcards. */ +export function matchesSearchQuery(value: string, query: string): boolean { + const normalizedQuery = query.trim() + if (!normalizedQuery) return true + + if (!isWildcardSearch(normalizedQuery)) { + return value.toLowerCase().includes(normalizedQuery.toLowerCase()) + } + + return matchesWildcard(value, normalizedQuery) +} + +function escapeRipgrepGlobSyntax(pattern: string): string { + let escaped = '' + for (const [index, character] of Array.from(pattern).entries()) { + if (character === '\\' || character === '[' || character === ']' || character === '{' || character === '}') { + escaped += `\\${character}` + } else if (index === 0 && character === '!') { + escaped += '\\!' + } else { + escaped += character + } + } + return escaped +} + +/** Build a conservative ripgrep glob for plain-text file-search prefiltering. */ +export function toSearchGlob(query: string): string { + const normalizedQuery = query.trim() + const pattern = isWildcardSearch(normalizedQuery) ? normalizedQuery : `*${normalizedQuery}*` + return escapeRipgrepGlobSyntax(pattern) +} diff --git a/web/src/components/AssistantChat/HappyThread.test.tsx b/web/src/components/AssistantChat/HappyThread.test.tsx index 4a6b6f1b..b31e06e4 100644 --- a/web/src/components/AssistantChat/HappyThread.test.tsx +++ b/web/src/components/AssistantChat/HappyThread.test.tsx @@ -198,6 +198,48 @@ describe('ConversationOutlinePanel', () => { expect(onLoadMore).toHaveBeenCalledTimes(1) }) + it('supports wildcard patterns in outline search', () => { + renderPanel() + + const searchbox = screen.getByRole('searchbox', { name: 'Search outline items' }) + fireEvent.change(searchbox, { target: { value: 'Implement*' } }) + + expect(screen.getByText('Implement the panel')).toBeInTheDocument() + expect(screen.queryByText('Second user prompt')).not.toBeInTheDocument() + + fireEvent.change(searchbox, { target: { value: 'Second user p?????' } }) + + expect(screen.queryByText('Implement the panel')).not.toBeInTheDocument() + expect(screen.getByText('Second user prompt')).toBeInTheDocument() + }) + + it('lets the shared matcher normalize outline queries consistently', () => { + const toLocaleLowerCase = vi.spyOn(String.prototype, 'toLocaleLowerCase').mockImplementation(function (this: string) { + return this.toString() === 'I' ? 'ı' : this.toLowerCase() + }) + try { + renderPanel({ + items: [ + ...outlineItems, + { + id: 'outline:user-text:m3', + targetMessageId: 'user-text:m3', + kind: 'user', + label: 'Istanbul deployment', + createdAt: 3000 + } + ] + }) + fireEvent.change(screen.getByRole('searchbox', { name: 'Search outline items' }), { + target: { value: 'I' } + }) + + expect(screen.getByText('Istanbul deployment')).toBeInTheDocument() + } finally { + toLocaleLowerCase.mockRestore() + } + }) + it('shows a search-specific empty state', () => { renderPanel() diff --git a/web/src/components/AssistantChat/HappyThread.tsx b/web/src/components/AssistantChat/HappyThread.tsx index e095af7e..79bd4d1f 100644 --- a/web/src/components/AssistantChat/HappyThread.tsx +++ b/web/src/components/AssistantChat/HappyThread.tsx @@ -34,6 +34,7 @@ import { formatSessionHeaderTimestamp } from '@/lib/sessionHeaderTimestamp' import { getShareTurnReasoningLabel, selectShareTurnMetadata } from '@/lib/shareTurnMetadata' import { useMinuteTick } from '@/hooks/useMinuteTick' import { queryKeys } from '@/lib/query-keys' +import { matchesSearchQuery } from '@hapi/protocol' type ScrollAnchor = { id: string @@ -338,13 +339,13 @@ export function ConversationOutlinePanel(props: { }) { const { t, locale } = useTranslation() const [searchQuery, setSearchQuery] = useState('') - const normalizedSearchQuery = searchQuery.trim().toLocaleLowerCase() + const normalizedSearchQuery = searchQuery.trim() const filteredItems = useMemo(() => { if (normalizedSearchQuery.length === 0) { return props.items } return props.items.filter((item) => ( - item.label.toLocaleLowerCase().includes(normalizedSearchQuery) + matchesSearchQuery(item.label, normalizedSearchQuery) )) }, [normalizedSearchQuery, props.items]) diff --git a/web/src/components/SessionList.test.ts b/web/src/components/SessionList.test.ts index 623ab87b..c516981b 100644 --- a/web/src/components/SessionList.test.ts +++ b/web/src/components/SessionList.test.ts @@ -403,6 +403,23 @@ describe('session list search helpers', () => { expect(sessionMatchesQuery(session, normalizeSearch('sidebar-search'), 'desktop')).toBe(true) expect(sessionMatchesQuery(session, normalizeSearch('hapi-worktrees'), 'desktop')).toBe(true) }) + + it('supports complete wildcard patterns without changing plain text matching', () => { + const session = makeSession({ + id: 'session-123', + metadata: { + path: '/work/hapi', + name: 'Fix Bot Review', + flavor: 'codex' + } + }) + + expect(sessionMatchesQuery(session, normalizeSearch('*bot*'), 'desktop')).toBe(true) + expect(sessionMatchesQuery(session, normalizeSearch('Fix*Review'), 'desktop')).toBe(true) + expect(sessionMatchesQuery(session, normalizeSearch('session-???'), 'desktop')).toBe(true) + expect(sessionMatchesQuery(session, normalizeSearch('bot*review'), 'desktop')).toBe(false) + expect(sessionMatchesQuery(session, normalizeSearch('bot review'), 'desktop')).toBe(true) + }) }) describe('session list time filter helpers', () => { diff --git a/web/src/components/SessionList.tsx b/web/src/components/SessionList.tsx index 900ee7da..b30031b7 100644 --- a/web/src/components/SessionList.tsx +++ b/web/src/components/SessionList.tsx @@ -1,5 +1,6 @@ import { useEffect, useMemo, useRef, useState } from 'react' import type { SessionSummary } from '@/types/api' +import { isWildcardSearch, matchesSearchQuery } from '@hapi/protocol' import type { ApiClient } from '@/api/client' import { useLongPress } from '@/hooks/useLongPress' import { usePlatform } from '@/hooks/usePlatform' @@ -518,7 +519,7 @@ export function normalizeSearch(value: string | null | undefined): string { export function sessionMatchesQuery(session: SessionSummary, query: string, machineLabel: string): boolean { if (!query) return true - const searchable = [ + const searchableParts = [ getSessionTitle(session), getWorktreeSessionLabel(session), session.id, @@ -531,9 +532,10 @@ export function sessionMatchesQuery(session: SessionSummary, query: string, mach machineLabel, ] .filter((part): part is string => typeof part === 'string' && part.length > 0) - .join('\n') - .toLowerCase() - return searchable.includes(query) + if (isWildcardSearch(query)) { + return searchableParts.some((part) => matchesSearchQuery(part, query)) + } + return searchableParts.join('\n').toLowerCase().includes(query) }