tools.spec.ts 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /**
  2. * Consumer-surface tests for the search tools over a FAKE subprocess service
  3. * and a FAKE spill backend, exercised through `ctx.tools.execute()` so nothing
  4. * bypasses the tool registry. The fake service makes every seam outcome
  5. * scriptable — spawn failure, truncated stdout with/without a raw spill path,
  6. * abort/timeout kills, signal kills, ripgrep exit codes — so these tests
  7. * verify schemas, argument validation, argv construction, workdir derivation,
  8. * signal forwarding, `SEARCH_*` error classification, retention,
  9. * formatted-result spill handoff, and the no-background-task invariant.
  10. * Real-`rg` behavior is pinned separately in integration.spec.ts.
  11. */
  12. import { describe, expect, it } from 'vitest'
  13. import { Context } from 'cordis'
  14. import { join, sep } from 'node:path'
  15. import { createUserMessage, CallId } from '@deepseek-ai/dsh-llm'
  16. import SystemPrompt, { renderPrompt } from '@deepseek-ai/dsh-system-prompt'
  17. import ToolRegistry, { TOOL_ABORTED_BEFORE_DISPATCH, type ToolExecution, type ToolExecutionToken } from '@deepseek-ai/dsh-tools'
  18. import { SubprocessService } from '@deepseek-ai/dsh-subprocess'
  19. import type { SubprocessCollectedOutputs, SubprocessHandle, SubprocessOutcome, SubprocessOutputRead, SubprocessOutputReader, SubprocessSpawnSpec } from '@deepseek-ai/dsh-subprocess'
  20. import { rgPath } from '@vscode/ripgrep'
  21. import { SpillLocator, SpillStore } from '@deepseek-ai/dsh-spill'
  22. import type { SaveTextSpill, SpillRef } from '@deepseek-ai/dsh-spill'
  23. import * as ToolFsSearch from '@deepseek-ai/dsh-tool-fs-search'
  24. import {
  25. buildGlobCommand,
  26. buildGrepCommand,
  27. formatGrepMatches,
  28. parseGrepMatches,
  29. presentGlobCall,
  30. presentGlobResult,
  31. presentGrepCall,
  32. presentGrepResult,
  33. previewLine,
  34. resolveRgPath,
  35. runRipgrep,
  36. sampleAcrossTopLevel,
  37. toWorkdirRelative,
  38. } from '@deepseek-ai/dsh-tool-fs-search'
  39. const testToolSignal = new AbortController().signal
  40. /**
  41. * Normalize a POSIX-style test path to the platform separator: the sampler and
  42. * the workdir-relative display conversion group by `node:path.sep`, so
  43. * `/`-literal paths would collapse into per-path groups on Windows.
  44. */
  45. const w = (path: string): string => path.replaceAll('/', sep)
  46. /** One scripted collect-mode stream, returned by `readFrom(0)` after settlement. */
  47. interface ScriptedStream {
  48. text: string
  49. lossy?: boolean
  50. spillPath?: string
  51. }
  52. /** One scripted spawn: exit facts plus the collected streams the tool reads. */
  53. interface ScriptedRun {
  54. outcome: SubprocessOutcome
  55. stdout: ScriptedStream
  56. stderr: ScriptedStream
  57. }
  58. /** A successful run over the given stdout; overrides script the failure shapes. */
  59. function runResult(
  60. stdout: string,
  61. overrides?: Partial<SubprocessOutcome> & { stdout?: Partial<ScriptedStream>; stderr?: ScriptedStream },
  62. ): ScriptedRun {
  63. const { stdout: stdoutOverrides, stderr: stderrOverrides, ...outcome } = overrides ?? {}
  64. return {
  65. outcome: { exitCode: 0, signal: null, ...outcome },
  66. stdout: { text: stdout, ...stdoutOverrides },
  67. stderr: { text: '', ...stderrOverrides },
  68. }
  69. }
  70. /** A fixed-response collect-mode reader: the tools read each stream once, from 0, after settlement. */
  71. class FakeReader implements SubprocessOutputReader {
  72. constructor(private readonly read: ScriptedStream) {}
  73. readFrom(_fromByte: number): SubprocessOutputRead {
  74. return {
  75. text: this.read.text,
  76. nextOffset: 0,
  77. lossy: this.read.lossy ?? false,
  78. ...this.read.spillPath !== undefined ? { spillPath: this.read.spillPath } : {},
  79. }
  80. }
  81. }
  82. /**
  83. * A scriptable subprocess handle: `done` resolves with the scripted outcome
  84. * (or rejects with the scripted error), `terminate()` records the call, and
  85. * the spec's abort signal marks the handle terminated — mirroring the seam's
  86. * abort→terminate escalation.
  87. */
  88. class FakeHandle implements SubprocessHandle {
  89. readonly pid = 4242
  90. readonly stdin = undefined
  91. readonly stdout = undefined
  92. readonly stderr = undefined
  93. readonly collected: SubprocessCollectedOutputs
  94. readonly done: Promise<SubprocessOutcome>
  95. /** True once `done` settled — the search tools must never leave a spawn running. */
  96. settled = false
  97. /** True when the handle's termination path ran (abort signal or explicit terminate). */
  98. terminated = false
  99. /** Scripted handle that drops one requested collect reader (the defensive branch). */
  100. readonly dropReaders: boolean
  101. constructor(spec: SubprocessSpawnSpec, script: () => ScriptedRun | { reject: Error }, dropReaders = false) {
  102. this.dropReaders = dropReaders
  103. // The abort listener attaches BEFORE the scripted run resolves, mirroring
  104. // a real spawn: the escalation is armed when the process starts.
  105. spec.signal?.addEventListener('abort', () => { this.terminated = true }, { once: true })
  106. const scripted = script()
  107. if ('reject' in scripted) {
  108. // A spawn failure produces no process output, so no readers exist.
  109. this.collected = {}
  110. this.done = Promise.reject(scripted.reject)
  111. } else {
  112. this.collected = {
  113. ...dropReaders ? {} : { stdout: new FakeReader(scripted.stdout), stderr: new FakeReader(scripted.stderr) },
  114. }
  115. this.done = Promise.resolve(scripted.outcome)
  116. }
  117. this.done.then(
  118. () => { this.settled = true },
  119. () => { this.settled = true },
  120. )
  121. }
  122. terminate(): void {
  123. this.terminated = true
  124. }
  125. waitForExit(_signal?: AbortSignal): Promise<boolean> {
  126. return Promise.resolve(true)
  127. }
  128. }
  129. /**
  130. * A scriptable fake subprocess service: `spawn()` records every spec and
  131. * returns a handle scripted by the armed `handler`. The search tools must
  132. * never spawn outside a single awaited foreground call, so every test can
  133. * assert on the exact spawn specs and settled handles.
  134. */
  135. class FakeSubprocess extends SubprocessService {
  136. spawns: SubprocessSpawnSpec[] = []
  137. handles: FakeHandle[] = []
  138. /** Arms the per-spawn script; a `{ reject }` return scripts a spawn-level failure. */
  139. handler: (spec: SubprocessSpawnSpec) => ScriptedRun | { reject: Error } = () => runResult('')
  140. /** When true, spawned handles drop their collect readers (the defensive branch). */
  141. dropReaders = false
  142. override spawn(spec: SubprocessSpawnSpec): SubprocessHandle {
  143. this.spawns.push(spec)
  144. const handle = new FakeHandle(spec, () => this.handler(spec), this.dropReaders)
  145. this.handles.push(handle)
  146. return handle
  147. }
  148. }
  149. /** A recording spill backend; arm `failWith` to script a storage failure. */
  150. class FakeSpill extends SpillStore {
  151. saves: SaveTextSpill[] = []
  152. failWith?: Error
  153. override saveText(input: SaveTextSpill): Promise<SpillRef> {
  154. if (this.failWith) return Promise.reject(this.failWith)
  155. this.saves.push(input)
  156. return Promise.resolve({
  157. locator: SpillLocator(`/spill/${input.suggestedName}`),
  158. bytes: Buffer.byteLength(input.content, 'utf8'),
  159. retrievalHint: 'Use the fake retrieval hint.',
  160. })
  161. }
  162. }
  163. interface SetupOptions {
  164. config?: Partial<ToolFsSearch.Config>
  165. spill?: boolean
  166. }
  167. const DEFAULT_CONFIG = { sampleOverCapGlobResults: true } satisfies ToolFsSearch.Config
  168. async function setup(options: SetupOptions = {}) {
  169. const ctx = new Context()
  170. const warnings: string[] = []
  171. ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn
  172. await ctx.plugin(SystemPrompt)
  173. await ctx.plugin(ToolRegistry)
  174. await ctx.plugin(FakeSubprocess)
  175. const subprocess = ctx.subprocess as FakeSubprocess
  176. if (options.spill === true) await ctx.plugin(FakeSpill)
  177. const fiber = await ctx.plugin(ToolFsSearch, { ...DEFAULT_CONFIG, ...options.config })
  178. const spill = options.spill === true ? ctx.get('spillStore') as FakeSpill : undefined
  179. return { ctx, subprocess, spill, fiber, warnings }
  180. }
  181. /** A stand-in agent whose session header carries the given cwd (and a stable id). */
  182. const agent = (cwd?: string) => ({ session: { header: { id: 'session-1', ...cwd !== undefined ? { cwd } : {} } } })
  183. let callCounter = 0
  184. function call(
  185. ctx: Context,
  186. name: string,
  187. args: unknown,
  188. options: { agent?: object; signal?: AbortSignal; parent?: ToolExecutionToken } = {},
  189. ) {
  190. return ctx.tools.execute({
  191. signal: testToolSignal,
  192. callId: CallId(`call-${++callCounter}`),
  193. name,
  194. arguments: args,
  195. ...options.agent ? { agent: options.agent as never } : {},
  196. ...options.signal ? { signal: options.signal } : {},
  197. ...options.parent ? { parent: options.parent } : {},
  198. })
  199. }
  200. function text(result: { content: { type: string; text?: string }[] }): string {
  201. return result.content.filter(b => b.type === 'text').map(b => b.text).join('')
  202. }
  203. /** One rg --json match record line. */
  204. function matchLine(path: string, lineNumber: number, lineText: string): string {
  205. return JSON.stringify({ type: 'match', data: { path: { text: path }, lines: { text: lineText }, line_number: lineNumber, absolute_offset: 0, submatches: [] } })
  206. }
  207. describe('registration', () => {
  208. it('registers glob and grep unconditionally with their prompt sections', async () => {
  209. const { ctx, subprocess } = await setup()
  210. // Registration performs NO load-time probe: the packaged binary is always
  211. // available, so nothing spawns until a tool call.
  212. expect(subprocess.spawns).toHaveLength(0)
  213. expect(ctx.tools.schemas().map(s => s.name).sort()).toEqual(['glob', 'grep'])
  214. const prompt = renderPrompt(await ctx.systemPrompt.assemble())
  215. expect(prompt).toContain('Use the glob tool')
  216. expect(prompt).toContain('Use the grep tool')
  217. expect(prompt).toContain('sampled across top-level entries')
  218. expect(prompt).not.toContain('sampled across top-level directories')
  219. const glob = ctx.tools.schemas().find(schema => schema.name === 'glob')
  220. expect(glob?.description).toContain('sampled across top-level entries')
  221. })
  222. it('stays pending until ctx.subprocess exists (inject)', async () => {
  223. const ctx = new Context()
  224. await ctx.plugin(SystemPrompt)
  225. await ctx.plugin(ToolRegistry)
  226. await ctx.plugin(ToolFsSearch, DEFAULT_CONFIG) // no subprocess service
  227. expect(ctx.tools.schemas()).toHaveLength(0)
  228. })
  229. it('unregisters everything on fiber disposal (HMR safety)', async () => {
  230. const { ctx, fiber } = await setup()
  231. expect(ctx.tools.schemas()).toHaveLength(2)
  232. await fiber.dispose()
  233. expect(ctx.tools.schemas()).toHaveLength(0)
  234. const sections = (await ctx.systemPrompt.assemble()).sections.map(s => s.name)
  235. expect(sections).not.toContain('tool:glob')
  236. expect(sections).not.toContain('tool:grep')
  237. })
  238. it('attaches the configured timeoutMs to both tool definitions', async () => {
  239. const { ctx } = await setup({ config: { timeoutMs: 5000 } })
  240. expect(ctx.tools.get('glob')?.timeoutMs).toBe(5000)
  241. expect(ctx.tools.get('grep')?.timeoutMs).toBe(5000)
  242. })
  243. it('defaults the timeout budget to 30 seconds', async () => {
  244. const { ctx } = await setup()
  245. expect(ctx.tools.get('glob')?.timeoutMs).toBe(30_000)
  246. expect(ctx.tools.get('grep')?.timeoutMs).toBe(30_000)
  247. })
  248. it('describes the modification-time head when over-cap sampling is disabled', async () => {
  249. const { ctx } = await setup({ config: { sampleOverCapGlobResults: false } })
  250. const prompt = renderPrompt(await ctx.systemPrompt.assemble())
  251. expect(prompt).toContain('a larger one keeps the modification-time-ordered head')
  252. expect(prompt).not.toContain('sampled across top-level entries')
  253. const glob = ctx.tools.schemas().find(schema => schema.name === 'glob')
  254. expect(glob?.description).toContain('a larger result returns the first 100 paths in modification-time order')
  255. expect(glob?.description).not.toContain('sampled across top-level entries')
  256. })
  257. })
  258. describe('config validation', () => {
  259. it('requires an explicit over-cap glob sampling choice', () => {
  260. expect(() => new ToolFsSearch.Config()).toThrow(/sampleOverCapGlobResults/)
  261. expect(new ToolFsSearch.Config({ sampleOverCapGlobResults: false })).toMatchObject({
  262. sampleOverCapGlobResults: false,
  263. globMaxResults: 100,
  264. })
  265. })
  266. it.each([
  267. ['globMaxResults', { globMaxResults: 0 }],
  268. ['grepMaxMatches', { grepMaxMatches: -1 }],
  269. ['grepMaxLineBytes', { grepMaxLineBytes: 1.5 }],
  270. ['rawOutputMaxBytes', { rawOutputMaxBytes: 0 }],
  271. ['graceMs', { graceMs: 0 }],
  272. ['stderrMaxBytes', { stderrMaxBytes: -1 }],
  273. ['timeoutMs', { timeoutMs: -100 }],
  274. ] as const)('rejects a non-positive or fractional %s at load', async (name, config) => {
  275. const ctx = new Context()
  276. await ctx.plugin(SystemPrompt)
  277. await ctx.plugin(ToolRegistry)
  278. await ctx.plugin(FakeSubprocess)
  279. await expect(ctx.plugin(ToolFsSearch, { ...DEFAULT_CONFIG, ...config })).rejects.toThrow(new RegExp(`tool-fs-search: ${name} must be a positive integer`))
  280. })
  281. })
  282. describe('command construction (plain argv)', () => {
  283. it('glob: fixed rg --files argv with the pattern and paired VCS excludes', () => {
  284. expect(buildGlobCommand({ pattern: '**/*.ts' })).toEqual([
  285. '--files',
  286. '--glob=**/*.ts',
  287. '--sort=modified',
  288. '--no-ignore',
  289. '--hidden',
  290. '--glob=!**/.git', '--glob=!**/.git/**',
  291. '--glob=!**/.svn', '--glob=!**/.svn/**',
  292. '--glob=!**/.hg', '--glob=!**/.hg/**',
  293. '--glob=!**/.bzr', '--glob=!**/.bzr/**',
  294. '--glob=!**/.jj', '--glob=!**/.jj/**',
  295. '--glob=!**/.sl', '--glob=!**/.sl/**',
  296. ])
  297. })
  298. it('glob: the search root rides behind -- as a plain element', () => {
  299. expect(buildGlobCommand({ pattern: '*.md', path: 'docs dir' })).toEqual(['--files', '--glob=*.md', '--sort=modified', '--no-ignore', '--hidden',
  300. '--glob=!**/.git', '--glob=!**/.git/**',
  301. '--glob=!**/.svn', '--glob=!**/.svn/**',
  302. '--glob=!**/.hg', '--glob=!**/.hg/**',
  303. '--glob=!**/.bzr', '--glob=!**/.bzr/**',
  304. '--glob=!**/.jj', '--glob=!**/.jj/**',
  305. '--glob=!**/.sl', '--glob=!**/.sl/**',
  306. '--', 'docs dir'])
  307. })
  308. it('grep: fixed rg --json argv with the pattern in --regexp= form', () => {
  309. expect(buildGrepCommand({ pattern: 'foo.*bar' })).toEqual(['--json', '--regexp=foo.*bar'])
  310. })
  311. it('grep: include in --glob= form, path behind --, both plain elements', () => {
  312. expect(buildGrepCommand({ pattern: 'x', path: '-leading-dash', include: '*.{ts,tsx}' }))
  313. .toEqual(['--json', '--regexp=x', '--glob=*.{ts,tsx}', '--', '-leading-dash'])
  314. })
  315. it.each([
  316. ['a command-substitution pattern', '$(rm -rf /)'],
  317. ['a backtick pattern', '`touch pwned`'],
  318. ['a pattern with double quotes and spaces', 'say "hi there"'],
  319. ['a pattern with single quotes', "it's"],
  320. ['a pattern with newlines', 'a\nb'],
  321. ['a leading-dash pattern', '--flag'],
  322. ['glob metacharacters', '*?[a-z]{x,y}'],
  323. ])('keeps %s as ONE inert argv element (no shell layer to escape)', (_label, raw) => {
  324. // The argv vector is handed to rg verbatim: hostile text cannot break out
  325. // of its argument because there is no shell between the vector and rg.
  326. expect(buildGrepCommand({ pattern: raw })).toEqual(['--json', `--regexp=${raw}`])
  327. expect(buildGlobCommand({ pattern: raw })[1]).toBe(`--glob=${raw}`)
  328. })
  329. })
  330. describe('workdir derivation and signal forwarding', () => {
  331. it('forwards the session cwd as the spawn cwd', async () => {
  332. const { ctx, subprocess } = await setup()
  333. subprocess.handler = () => runResult('a.ts\n')
  334. await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/sessions/s1') })
  335. expect(subprocess.spawns[0]?.cwd).toBe('/sessions/s1')
  336. })
  337. it('defaults the spawn cwd to process.cwd() without a session cwd', async () => {
  338. const { ctx, subprocess } = await setup()
  339. subprocess.handler = () => runResult('a.ts\n')
  340. await call(ctx, 'glob', { pattern: '*' }, { agent: agent() })
  341. expect(subprocess.spawns[0]?.cwd).toBe(process.cwd())
  342. // A non-agent caller takes the same default.
  343. await call(ctx, 'grep', { pattern: 'x' })
  344. expect(subprocess.spawns[1]?.cwd).toBe(process.cwd())
  345. })
  346. it('spawns the packaged ripgrep binary with --no-config, the fixed argv, and budgeted collect streams', async () => {
  347. const { ctx, subprocess } = await setup({
  348. config: { rawOutputMaxBytes: 1234, graceMs: 5000, stderrMaxBytes: 4096 },
  349. })
  350. subprocess.handler = () => runResult('', { exitCode: 1 })
  351. await call(ctx, 'grep', { pattern: 'needle' })
  352. const spec = subprocess.spawns[0]
  353. // --no-config keeps a host RIPGREP_CONFIG_PATH from injecting a
  354. // preprocessor into this unconfined spawn.
  355. expect(spec?.argv).toEqual([rgPath, '--no-config', '--json', '--regexp=needle'])
  356. expect(spec?.stdio.stdin).toBe('ignore')
  357. // stdout gets the tool's parse budget; stderr is a diagnostic excerpt;
  358. // both are the seam's diagnostic-tail shape (no spill files requested).
  359. expect((spec?.stdio.stdout as { maxBytes: number }).maxBytes).toBe(1234)
  360. expect((spec?.stdio.stderr as { maxBytes: number }).maxBytes).toBe(4096)
  361. expect(spec?.graceMs).toBe(5_000)
  362. })
  363. it('defaults the stderr tail budget and grace period when the config omits them', async () => {
  364. const { ctx, subprocess } = await setup()
  365. subprocess.handler = () => runResult('', { exitCode: 1 })
  366. await call(ctx, 'grep', { pattern: 'needle' })
  367. const spec = subprocess.spawns[0]
  368. expect((spec?.stdio.stderr as { maxBytes: number }).maxBytes).toBe(64 * 1024)
  369. expect(spec?.graceMs).toBe(3_000)
  370. })
  371. it('forwards exec.signal into the spawn spec', async () => {
  372. const { ctx, subprocess } = await setup()
  373. const controller = new AbortController()
  374. subprocess.handler = () => runResult('')
  375. const result = await call(ctx, 'grep', { pattern: 'x' }, { signal: controller.signal })
  376. expect(subprocess.spawns[0]?.signal).toBe(controller.signal)
  377. expect(result.isError).toBe(false)
  378. })
  379. it('reports an abort fired during the run as SEARCH_ABORTED', async () => {
  380. // The cooperative tool timeout or caller cancellation aborts exec.signal;
  381. // the subprocess seam then kills the process tree. The tool classifies
  382. // the first cause it owns: the abort.
  383. const { ctx, subprocess } = await setup()
  384. const controller = new AbortController()
  385. subprocess.handler = () => {
  386. controller.abort('timeout')
  387. return runResult('', { exitCode: null, signal: 'SIGTERM' })
  388. }
  389. const result = await call(ctx, 'glob', { pattern: '*' }, { signal: controller.signal })
  390. expect(result.isError).toBe(true)
  391. expect(result.error).toMatchObject({ info: { code: 'SEARCH_ABORTED' } })
  392. expect(text(result)).toContain('aborted before completion')
  393. expect(subprocess.handles[0]?.terminated).toBe(true)
  394. })
  395. it('skips a pre-aborted registry call before spawn()', async () => {
  396. const { ctx, subprocess } = await setup()
  397. const controller = new AbortController()
  398. controller.abort()
  399. subprocess.handler = () => { throw new Error('aborted before spawn') }
  400. const result = await call(ctx, 'grep', { pattern: 'x' }, { signal: controller.signal })
  401. expect(result.isError).toBe(true)
  402. expect(result.error).toMatchObject({ info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } })
  403. expect(subprocess.spawns).toHaveLength(0)
  404. })
  405. it('fails a pre-aborted exec.signal before spawn with SEARCH_ABORTED', async () => {
  406. // Direct unit check of runRipgrep's own pre-spawn guard: the registry
  407. // intercepts most pre-aborted calls, but a signal that aborts between the
  408. // registry check and execute reaches this branch.
  409. const { ctx } = await setup()
  410. const controller = new AbortController()
  411. controller.abort()
  412. const exec = { signal: controller.signal, name: 'glob', callId: CallId('direct-pre-abort') } as unknown as ToolExecution
  413. await expect(runRipgrep(ctx, exec, 'glob', ['--files'], 1_000_000, 3_000, 64 * 1024)).rejects
  414. .toMatchObject({ name: 'SearchError', code: 'SEARCH_ABORTED' })
  415. })
  416. it('translates a spawn rejection into SEARCH_FAILED even when the signal aborts concurrently', async () => {
  417. // The seam rejects only for infrastructure failures (unusable workdir,
  418. // missing binary); the abort happened after dispatch, so the launch
  419. // failure is the reportable cause with the original error chained.
  420. const { ctx, subprocess } = await setup()
  421. const controller = new AbortController()
  422. subprocess.handler = () => {
  423. controller.abort('cancel search')
  424. return { reject: new Error('spawn ENOENT') }
  425. }
  426. const result = await call(ctx, 'grep', { pattern: 'x' }, { signal: controller.signal })
  427. expect(result.isError).toBe(true)
  428. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_FAILED' } })
  429. expect(text(result)).toContain('could not start')
  430. })
  431. it('classifies a synchronous spawn-creation throw as SEARCH_FAILED', async () => {
  432. // Node's spawn() throws synchronously for a NUL in argv, and the local
  433. // impl can throw synchronously for other invalid specs. Creation-time
  434. // failures must join the error vocabulary instead of escaping raw.
  435. const { ctx, subprocess } = await setup()
  436. subprocess.handler = () => { throw new Error('spawn ERR_INVALID_ARG_VALUE') }
  437. const result = await call(ctx, 'grep', { pattern: 'x' })
  438. expect(result.isError).toBe(true)
  439. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_FAILED' } })
  440. expect(text(result)).toContain('could not start')
  441. })
  442. it('classifies a synchronous spawn-creation throw after an abort as SEARCH_ABORTED', async () => {
  443. // The local impl can throw synchronously when the signal aborts between
  444. // the pre-spawn check and the spawn call; no process was launched, so the
  445. // abort is the reportable cause.
  446. const { ctx, subprocess } = await setup()
  447. const controller = new AbortController()
  448. subprocess.handler = () => {
  449. controller.abort('timeout')
  450. throw new Error('aborted during spawn')
  451. }
  452. const result = await call(ctx, 'glob', { pattern: '*' }, { signal: controller.signal })
  453. expect(result.isError).toBe(true)
  454. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_ABORTED' } })
  455. expect(text(result)).toContain('aborted before completion')
  456. })
  457. it('resolves the packaged ripgrep path lazily, once per process', async () => {
  458. // The module must not touch @vscode/ripgrep at load (a missing platform
  459. // package would otherwise fail the whole composition), and repeated
  460. // resolution reuses the first result. The resolution-failure path is
  461. // pinned separately in rg-path.spec.ts.
  462. await setup()
  463. expect(await resolveRgPath()).toBe(rgPath)
  464. expect(resolveRgPath()).toBe(resolveRgPath())
  465. })
  466. it('rejects when the subprocess implementation drops a requested collect stream', async () => {
  467. const { ctx, subprocess } = await setup()
  468. subprocess.dropReaders = true
  469. const result = await call(ctx, 'glob', { pattern: '*' })
  470. expect(result.isError).toBe(true)
  471. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_FAILED' } })
  472. expect(text(result)).toContain('no collected output streams')
  473. })
  474. })
  475. describe('exit semantics and failure classification', () => {
  476. it('exit 1 is a successful empty search', async () => {
  477. const { ctx, subprocess } = await setup()
  478. subprocess.handler = () => runResult('', { exitCode: 1 })
  479. const glob = await call(ctx, 'glob', { pattern: '*.nope' })
  480. expect(glob.isError).toBe(false)
  481. expect(text(glob)).toBe('No files found')
  482. const grep = await call(ctx, 'grep', { pattern: 'nope' })
  483. expect(grep.isError).toBe(false)
  484. expect(text(grep)).toBe('No matches found')
  485. })
  486. it('a regex parse error classifies as SEARCH_INVALID_PATTERN', async () => {
  487. const { ctx, subprocess } = await setup()
  488. subprocess.handler = () => runResult('', { exitCode: 2, stderr: { text: 'rg: regex parse error:\n (\nerror: unclosed group' } })
  489. const result = await call(ctx, 'grep', { pattern: '(' })
  490. expect(result.error).toMatchObject({ info: { code: 'SEARCH_INVALID_PATTERN' } })
  491. expect(text(result)).toContain('regex parse error')
  492. })
  493. it('a glob parse error classifies as SEARCH_INVALID_PATTERN', async () => {
  494. const { ctx, subprocess } = await setup()
  495. subprocess.handler = () => runResult('', { exitCode: 2, stderr: { text: 'rg: error parsing glob \'[\': unclosed character class' } })
  496. const result = await call(ctx, 'glob', { pattern: '[' })
  497. expect(result.error).toMatchObject({ info: { code: 'SEARCH_INVALID_PATTERN' } })
  498. })
  499. it('other nonzero exits are SEARCH_FAILED carrying the stderr excerpt', async () => {
  500. const { ctx, subprocess } = await setup()
  501. subprocess.handler = () => runResult('', { exitCode: 2, stderr: { text: 'rg: missing.dir: IO error: no such file or directory' } })
  502. const result = await call(ctx, 'grep', { pattern: 'x', path: 'missing.dir' })
  503. expect(result.error).toMatchObject({ info: { code: 'SEARCH_FAILED' } })
  504. expect(text(result)).toContain('IO error')
  505. })
  506. it('a nonzero exit with EMPTY stderr still reports the exit code', async () => {
  507. const { ctx, subprocess } = await setup()
  508. subprocess.handler = () => runResult('', { exitCode: 3 })
  509. const result = await call(ctx, 'glob', { pattern: '*' })
  510. expect(result.error).toMatchObject({ info: { code: 'SEARCH_FAILED' } })
  511. expect(text(result)).toContain('exit 3')
  512. })
  513. it('truncated stderr gains a truncation note and stderr.spillPath is never read', async () => {
  514. const { ctx, subprocess } = await setup()
  515. subprocess.handler = () => runResult('', {
  516. exitCode: 2,
  517. stderr: { text: 'tail of diagnostics', lossy: true, spillPath: '/does/not/exist-and-never-read' },
  518. })
  519. const result = await call(ctx, 'grep', { pattern: 'x' })
  520. expect(text(result)).toContain('tail of diagnostics [stderr truncated]')
  521. })
  522. it('a signal kill (not timeout, not abort) is SEARCH_FAILED', async () => {
  523. const { ctx, subprocess } = await setup()
  524. subprocess.handler = () => runResult('', { exitCode: null, signal: 'SIGKILL' })
  525. const result = await call(ctx, 'grep', { pattern: 'x' })
  526. expect(result.error).toMatchObject({ info: { code: 'SEARCH_FAILED' } })
  527. expect(text(result)).toContain('SIGKILL')
  528. })
  529. it('a null exit with no signal (defensive) is SEARCH_FAILED', async () => {
  530. const { ctx, subprocess } = await setup()
  531. subprocess.handler = () => runResult('', { exitCode: null, signal: null })
  532. const result = await call(ctx, 'glob', { pattern: '*' })
  533. expect(result.error).toMatchObject({ info: { code: 'SEARCH_FAILED' } })
  534. expect(text(result)).toContain('killed by signal (unknown)')
  535. })
  536. })
  537. describe('raw output acquisition', () => {
  538. it('fails with SEARCH_RAW_OUTPUT_OVERFLOW when truncated stdout has a raw spill path', async () => {
  539. const { ctx, subprocess } = await setup({ config: { rawOutputMaxBytes: 16 } })
  540. subprocess.handler = () => runResult('', { stdout: { text: 'x', lossy: true, spillPath: '/does/not/get-read' } })
  541. const result = await call(ctx, 'glob', { pattern: '*' })
  542. expect(result.error).toMatchObject({ info: { code: 'SEARCH_RAW_OUTPUT_OVERFLOW' } })
  543. expect(text(result)).toContain('narrow pattern, path, or include')
  544. })
  545. it('fails with SEARCH_RAW_OUTPUT_OVERFLOW when UNTRUNCATED inline stdout exceeds the cap', async () => {
  546. // A subprocess implementation retaining more inline than this package's
  547. // cap (or a deployment lowering rawOutputMaxBytes below the retention
  548. // budget) must not smuggle an over-cap parse through the untruncated path.
  549. const { ctx, subprocess } = await setup({ config: { rawOutputMaxBytes: 16 } })
  550. subprocess.handler = () => runResult(`${'x'.repeat(64)}\n`)
  551. const result = await call(ctx, 'grep', { pattern: 'x' })
  552. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_RAW_OUTPUT_OVERFLOW' } })
  553. expect(text(result)).toContain('narrow pattern, path, or include')
  554. })
  555. it('fails with SEARCH_RAW_OUTPUT_OVERFLOW when truncated stdout has no spill path', async () => {
  556. const { ctx, subprocess } = await setup()
  557. subprocess.handler = () => runResult('', { stdout: { text: 'partial', lossy: true } })
  558. const result = await call(ctx, 'grep', { pattern: 'x' })
  559. expect(result.error).toMatchObject({ info: { code: 'SEARCH_RAW_OUTPUT_OVERFLOW' } })
  560. })
  561. })
  562. describe('cross-directory sampling', () => {
  563. it('gives every top-level entry a slot before any entry gets a second', () => {
  564. const paths = ['v/a', 'v/b', 'v/c', 'v/d', 'src/e', 'guide/f'].map(w)
  565. // The head of 3 would be all `v/`; the sample reaches all three entries.
  566. expect(sampleAcrossTopLevel(paths, 3)).toEqual({ items: ['v/a', 'src/e', 'guide/f'].map(w), shown: 3, total: 3 })
  567. // Extra slots go round again — to the only entry with paths left — and the
  568. // page stays grouped by entry rather than interleaved.
  569. expect(sampleAcrossTopLevel(paths, 5)).toEqual({ items: ['v/a', 'v/b', 'v/c', 'src/e', 'guide/f'].map(w), shown: 3, total: 3 })
  570. })
  571. it('hands an exhausted entry the remaining slots go to entries that still have paths', () => {
  572. const paths = ['solo/a', 'many/b', 'many/c', 'many/d'].map(w)
  573. expect(sampleAcrossTopLevel(paths, 3)).toEqual({ items: ['solo/a', 'many/b', 'many/c'].map(w), shown: 2, total: 2 })
  574. })
  575. it('does not rescan exhausted entries while filling a skewed page', () => {
  576. const singletonCount = 12_500
  577. const paths = [
  578. ...Array.from({ length: singletonCount }, (_, index) => `group-${index}${sep}only`),
  579. ...Array.from({ length: singletonCount }, (_, index) => `late${sep}${index}`),
  580. ]
  581. expect(sampleAcrossTopLevel(paths, paths.length - 1)).toMatchObject({
  582. shown: singletonCount + 1,
  583. total: singletonCount + 1,
  584. items: { length: paths.length - 1 },
  585. })
  586. }, 500)
  587. it('reports the entries it could not reach when the page is smaller than the top level', () => {
  588. const paths = ['a/1', 'b/1', 'c/1', 'd/1'].map(w)
  589. expect(sampleAcrossTopLevel(paths, 2)).toEqual({ items: ['a/1', 'b/1'].map(w), shown: 2, total: 4 })
  590. })
  591. it('groups an absolute path by its first real name, not by its empty root segment', () => {
  592. // Paths outside the workdir stay absolute; without stripping the leading
  593. // separator every one of them would collapse into a single empty group.
  594. expect(sampleAcrossTopLevel(['/out/a', '/out/b', '/away/c', '/away/d'].map(w), 2))
  595. .toEqual({ items: ['/out/a', '/away/c'].map(w), shown: 2, total: 2 })
  596. })
  597. it('reproduces the modification-time-ordered head for a flat result', () => {
  598. expect(sampleAcrossTopLevel(['a.ts', 'b.ts', 'c.ts'], 2)).toEqual({ items: ['a.ts', 'b.ts'], shown: 2, total: 3 })
  599. })
  600. it('groups paths relative to an explicit search root', () => {
  601. expect(sampleAcrossTopLevel([
  602. 'workspace/vendor/a.ts',
  603. 'workspace/vendor/b.ts',
  604. 'workspace/source/c.ts',
  605. 'workspace/guides/d.md',
  606. ].map(w), 3, 'workspace')).toEqual({
  607. items: ['workspace/vendor/a.ts', 'workspace/source/c.ts', 'workspace/guides/d.md'].map(w),
  608. shown: 3,
  609. total: 3,
  610. })
  611. expect(sampleAcrossTopLevel(['./vendor/a.ts', './src/b.ts'].map(w), 2, '.'))
  612. .toEqual({ items: ['./vendor/a.ts', './src/b.ts'].map(w), shown: 2, total: 2 })
  613. expect(sampleAcrossTopLevel(['/vendor/a.ts', '/src/b.ts'].map(w), 2, w('/')))
  614. .toEqual({ items: ['/vendor/a.ts', '/src/b.ts'].map(w), shown: 2, total: 2 })
  615. const rooted = [
  616. ['root', 'a', 'one'].join(sep),
  617. ['root', 'a', 'two'].join(sep),
  618. ['root', 'b', 'three'].join(sep),
  619. ]
  620. expect(sampleAcrossTopLevel(rooted, 2, 'root'))
  621. .toEqual({ items: [rooted[0], rooted[2]], shown: 2, total: 2 })
  622. expect(sampleAcrossTopLevel(['other/a.ts'].map(w), 1, 'src'))
  623. .toEqual({ items: ['other/a.ts'].map(w), shown: 1, total: 1 })
  624. expect(sampleAcrossTopLevel(['src'], 1, 'src'))
  625. .toEqual({ items: ['src'], shown: 1, total: 1 })
  626. })
  627. it.skipIf(process.platform === 'win32')('treats POSIX backslashes as filename characters', () => {
  628. const paths = ['old\\one', 'old\\two', 'src/a']
  629. expect(sampleAcrossTopLevel(paths, 2)).toEqual({
  630. items: ['old\\one', 'old\\two'],
  631. shown: 2,
  632. total: 3,
  633. })
  634. })
  635. it('handles more top-level groups than the JavaScript argument limit', () => {
  636. const paths = Array.from({ length: 125_000 }, (_, index) => `dir-${index}/file.txt`)
  637. expect(sampleAcrossTopLevel(paths, 100)).toMatchObject({ shown: 100, total: 125_000 })
  638. })
  639. })
  640. describe('glob results', () => {
  641. it('lists workdir-relative paths (absolute output under the workdir is relativized)', async () => {
  642. const { ctx, subprocess } = await setup()
  643. subprocess.handler = () => runResult('/sessions/s1/src/a.ts\n/elsewhere/b.ts\nrel/c.ts\n')
  644. const result = await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/sessions/s1') })
  645. if (result.isError) throw new Error('expected glob success')
  646. expect(result.value).toEqual({ root: '.', paths: [join('src', 'a.ts'), '/elsewhere/b.ts', 'rel/c.ts'] })
  647. expect(text(result)).toBe(`${join('src', 'a.ts')}\n/elsewhere/b.ts\nrel/c.ts`)
  648. })
  649. it('validates arguments (blank pattern, blank path)', async () => {
  650. const { ctx } = await setup()
  651. expect(text(await call(ctx, 'glob', { pattern: ' ' }))).toContain('pattern must be a non-empty string')
  652. expect(text(await call(ctx, 'glob', { pattern: '*', path: ' ' }))).toContain('path must be a non-empty string')
  653. })
  654. it('threads a valid path through to the spawn as the plain search root element', async () => {
  655. const { ctx, subprocess } = await setup()
  656. subprocess.handler = () => runResult('sub/a.ts\n')
  657. const result = await call(ctx, 'glob', { pattern: '*.ts', path: 'sub' })
  658. expect(result.isError).toBe(false)
  659. expect(subprocess.spawns[0]?.argv).toEqual([rgPath, '--no-config', '--files', '--glob=*.ts', '--sort=modified', '--no-ignore', '--hidden',
  660. '--glob=!**/.git', '--glob=!**/.git/**',
  661. '--glob=!**/.svn', '--glob=!**/.svn/**',
  662. '--glob=!**/.hg', '--glob=!**/.hg/**',
  663. '--glob=!**/.bzr', '--glob=!**/.bzr/**',
  664. '--glob=!**/.jj', '--glob=!**/.jj/**',
  665. '--glob=!**/.sl', '--glob=!**/.sl/**',
  666. '--', 'sub'])
  667. })
  668. it('caps at globMaxResults and saves the FULL sorted list through spillStore', async () => {
  669. const { ctx, subprocess, spill } = await setup({ config: { globMaxResults: 2 }, spill: true })
  670. ctx.on('tools/post-execute', async () => ({
  671. kind: 'accept',
  672. additionalContexts: [createUserMessage({
  673. content: [{ type: 'text', text: 'glob context' }], source: { kind: 'plugin', plugin: 'test' },
  674. })],
  675. }))
  676. subprocess.handler = () => runResult('a.ts\nb.ts\nc.ts\nd.ts\n')
  677. const result = await call(ctx, 'glob', { pattern: '*.ts' }, { agent: agent('/w') })
  678. expect(result.isError).toBe(false)
  679. if (result.isError) throw new Error('expected glob success')
  680. expect(result.value).toEqual({ root: '.', paths: ['a.ts', 'b.ts', 'c.ts', 'd.ts'] })
  681. expect(text(result)).toBe('a.ts\nb.ts\n\n(Showing 2 of 4 paths. Full sorted result stored at: /spill/glob-results.txt. Use the fake retrieval hint.)')
  682. expect(spill?.saves).toHaveLength(1)
  683. expect(spill?.saves[0]).toMatchObject({
  684. owner: { sessionId: 'session-1' },
  685. source: { toolName: 'glob', label: 'result' },
  686. suggestedName: 'glob-results.txt',
  687. content: 'a.ts\nb.ts\nc.ts\nd.ts',
  688. })
  689. expect(spill?.saves[0]?.source.callId).toBeDefined()
  690. expect(result.additionalContexts?.[0]?.content).toEqual([{ type: 'text', text: 'glob context' }])
  691. })
  692. it('samples an over-cap result across top-level entries instead of taking its head', async () => {
  693. // The shipped failure: `*` matches the whole tree, mtime order puts one
  694. // freshly-unpacked subtree first, and a head-of-3 reads like the entire
  695. // workspace. The sample reaches every top-level entry instead.
  696. const { ctx, subprocess } = await setup({ config: { globMaxResults: 3 } })
  697. subprocess.handler = () => runResult(['vendor/a.ts', 'vendor/b.ts', 'vendor/c.ts', 'src/d.ts', 'guide/e.md', 'top.txt'].map(w).join('\n'))
  698. const result = await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/w') })
  699. expect(text(result)).toBe(['vendor/a.ts', 'src/d.ts', 'guide/e.md'].map(w).join('\n') + '\n\n'
  700. + '(Showing 3 of 6 paths, sampled across 3 of the 4 top-level entries this pattern matched '
  701. + 'instead of taken in modification-time order. Narrow path to inspect a specific subtree. '
  702. + 'The complete result could not be saved; narrow pattern or path to see more.)')
  703. })
  704. it('keeps the modification-time head when over-cap sampling is disabled', async () => {
  705. const { ctx, subprocess } = await setup({
  706. config: { globMaxResults: 3, sampleOverCapGlobResults: false },
  707. })
  708. subprocess.handler = () => runResult(['vendor/a.ts', 'vendor/b.ts', 'vendor/c.ts', 'src/d.ts', 'guide/e.md'].join('\n'))
  709. expect(text(await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/w') })))
  710. .toBe('vendor/a.ts\nvendor/b.ts\nvendor/c.ts\n\n'
  711. + '(Showing 3 of 5 paths. The complete result could not be saved; narrow pattern or path to see more.)')
  712. })
  713. it('samples relative to the explicit search root instead of its workdir prefix', async () => {
  714. const { ctx, subprocess } = await setup({ config: { globMaxResults: 3 } })
  715. subprocess.handler = () => runResult([
  716. 'workspace/vendor/a.ts',
  717. 'workspace/vendor/b.ts',
  718. 'workspace/source/c.ts',
  719. 'workspace/guides/d.md',
  720. ].map(w).join('\n'))
  721. const result = await call(ctx, 'glob', { pattern: '*', path: w('workspace') }, { agent: agent('/w') })
  722. expect(text(result)).toContain(['workspace/vendor/a.ts', 'workspace/source/c.ts', 'workspace/guides/d.md'].map(w).join('\n'))
  723. expect(text(result)).toContain('sampled across 3 of the 3 top-level entries')
  724. })
  725. it('samples relative to an absolute search root after workdir display conversion', async () => {
  726. const { ctx, subprocess } = await setup({ config: { globMaxResults: 3 } })
  727. subprocess.handler = () => runResult([
  728. '/w/workspace/vendor/a.ts',
  729. '/w/workspace/vendor/b.ts',
  730. '/w/workspace/source/c.ts',
  731. '/w/workspace/guides/d.md',
  732. ].map(w).join('\n'))
  733. const result = await call(ctx, 'glob', { pattern: '*', path: w('/w/workspace') }, { agent: agent(w('/w')) })
  734. expect(text(result)).toContain(['workspace/vendor/a.ts', 'workspace/source/c.ts', 'workspace/guides/d.md'].map(w).join('\n'))
  735. expect(text(result)).toContain('sampled across 3 of the 3 top-level entries')
  736. })
  737. it('drops the narrowing hint when the sample reaches every top-level entry', async () => {
  738. const { ctx, subprocess } = await setup({ config: { globMaxResults: 3 } })
  739. subprocess.handler = () => runResult(['vendor/a.ts', 'vendor/b.ts', 'vendor/c.ts', 'src/d.ts'].map(w).join('\n'))
  740. expect(text(await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/w') })))
  741. .toBe(['vendor/a.ts', 'vendor/b.ts', 'src/d.ts'].map(w).join('\n') + '\n\n'
  742. + '(Showing 3 of 4 paths, sampled across 2 of the 2 top-level entries this pattern matched '
  743. + 'instead of taken in modification-time order. '
  744. + 'The complete result could not be saved; narrow pattern or path to see more.)')
  745. })
  746. it('keeps modification-time order untouched when the whole result fits', async () => {
  747. const { ctx, subprocess } = await setup({ config: { globMaxResults: 4 } })
  748. subprocess.handler = () => runResult('vendor/a.ts\nvendor/b.ts\nsrc/c.ts\n')
  749. expect(text(await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/w') })))
  750. .toBe('vendor/a.ts\nvendor/b.ts\nsrc/c.ts')
  751. })
  752. it('keeps the plain footer for a flat result, where the sample is the modification-time head', async () => {
  753. const { ctx, subprocess } = await setup({ config: { globMaxResults: 2 } })
  754. subprocess.handler = () => runResult('a.ts\nb.ts\nc.ts\n')
  755. expect(text(await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/w') })))
  756. .toBe('a.ts\nb.ts\n\n(Showing 2 of 3 paths. The complete result could not be saved; narrow pattern or path to see more.)')
  757. })
  758. it('does not create a spill file when the result fits inline', async () => {
  759. const { ctx, subprocess, spill } = await setup({ spill: true })
  760. subprocess.handler = () => runResult('a.ts\nb.ts\n')
  761. const result = await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/w') })
  762. expect(text(result)).toBe('a.ts\nb.ts')
  763. expect(spill?.saves).toHaveLength(0)
  764. })
  765. it('preserves a downstream canonical value replacement instead of spilling the old value', async () => {
  766. const { ctx, subprocess, spill } = await setup({ config: { globMaxResults: 1 }, spill: true })
  767. ctx.on('tools/post-execute', async () => ({
  768. kind: 'accept' as const,
  769. value: { root: '.', paths: ['replacement-a.ts', 'replacement-b.ts'] },
  770. }))
  771. subprocess.handler = () => runResult('old-a.ts\nold-b.ts\n')
  772. const result = await call(ctx, 'glob', { pattern: '*.ts' }, { agent: agent('/w') })
  773. if (result.isError) throw new Error('expected glob replacement success')
  774. expect(result.value).toEqual({ root: '.', paths: ['replacement-a.ts', 'replacement-b.ts'] })
  775. expect(text(result)).toContain('replacement-a.ts')
  776. expect(text(result)).not.toContain('old-a.ts')
  777. expect(spill?.saves).toHaveLength(0)
  778. })
  779. it('keeps the full nested Code value without creating a surface spill', async () => {
  780. const { ctx, subprocess, spill } = await setup({ config: { globMaxResults: 2 }, spill: true })
  781. subprocess.handler = () => runResult('a.ts\nb.ts\nc.ts\nd.ts\n')
  782. const result = await call(ctx, 'glob', { pattern: '*.ts' }, {
  783. agent: agent('/w'),
  784. parent: Symbol('run_code') as ToolExecutionToken,
  785. })
  786. if (result.isError) throw new Error('expected glob success')
  787. expect(result.value).toEqual({ root: '.', paths: ['a.ts', 'b.ts', 'c.ts', 'd.ts'] })
  788. expect(text(result)).toBe('a.ts\nb.ts\n\n(Showing 2 of 4 paths. The complete result could not be saved; narrow pattern or path to see more.)')
  789. expect(spill?.saves).toHaveLength(0)
  790. })
  791. it.each([
  792. ['no spill backend loaded', { fail: false, spill: false, ownerless: false }],
  793. ['saveText fails', { fail: true, spill: true, ownerless: false }],
  794. ['no session owner', { fail: false, spill: true, ownerless: true }],
  795. ])('keeps the inline page and reports the unsaved remainder when %s', async (_label, mode) => {
  796. const { ctx, subprocess, spill } = await setup({ config: { globMaxResults: 1 }, spill: mode.spill })
  797. if (mode.fail && spill) spill.failWith = new Error('disk full')
  798. subprocess.handler = () => runResult('a.ts\nb.ts\n')
  799. const result = await call(ctx, 'glob', { pattern: '*' }, mode.ownerless ? {} : { agent: agent('/w') })
  800. expect(result.isError).toBe(false) // spill unavailability never fails the search
  801. expect(text(result)).toBe('a.ts\n\n(Showing 1 of 2 paths. The complete result could not be saved; narrow pattern or path to see more.)')
  802. })
  803. })
  804. describe('grep results', () => {
  805. it('groups matches by file with line numbers', async () => {
  806. const { ctx, subprocess } = await setup()
  807. subprocess.handler = () => runResult([
  808. JSON.stringify({ type: 'begin', data: { path: { text: 'a.ts' } } }),
  809. matchLine('a.ts', 3, 'const x = 1\n'),
  810. matchLine('a.ts', 9, 'const y = 2\n'),
  811. JSON.stringify({ type: 'end', data: { path: { text: 'a.ts' } } }),
  812. matchLine('b.ts', 1, 'const z = 3'),
  813. JSON.stringify({ type: 'summary', data: {} }),
  814. '',
  815. ].join('\n'))
  816. const result = await call(ctx, 'grep', { pattern: 'const' })
  817. expect(result.isError).toBe(false)
  818. if (result.isError) throw new Error('expected grep success')
  819. expect(result.value).toEqual({
  820. matches: [
  821. { path: 'a.ts', lineNumber: 3, line: 'const x = 1' },
  822. { path: 'a.ts', lineNumber: 9, line: 'const y = 2' },
  823. { path: 'b.ts', lineNumber: 1, line: 'const z = 3' },
  824. ],
  825. })
  826. expect(text(result)).toBe('Found 3 matches\n\na.ts\nLine 3: const x = 1\nLine 9: const y = 2\n\nb.ts\nLine 1: const z = 3')
  827. })
  828. it('reports a single match in the singular', async () => {
  829. const { ctx, subprocess } = await setup()
  830. subprocess.handler = () => runResult(`${matchLine('a.ts', 1, 'hit')}\n`)
  831. expect(text(await call(ctx, 'grep', { pattern: 'hit' }))).toBe('Found 1 match\n\na.ts\nLine 1: hit')
  832. })
  833. it('relativizes absolute match paths against the resolved workdir', async () => {
  834. const { ctx, subprocess } = await setup()
  835. subprocess.handler = () => runResult(`${matchLine('/sessions/s1/deep/a.ts', 2, 'hit')}\n`)
  836. const result = await call(ctx, 'grep', { pattern: 'hit', path: '/sessions/s1' }, { agent: agent('/sessions/s1') })
  837. expect(text(result)).toContain(`${join('deep', 'a.ts')}\nLine 2: hit`)
  838. })
  839. it('previews a long matched line at grepMaxLineBytes preserving UTF-8', async () => {
  840. const { ctx, subprocess } = await setup({ config: { grepMaxLineBytes: 7 } })
  841. // 'héllo wörld' cut at 7 bytes lands mid-'é'? h(1)é(2)l(1)l(1)o(1)=6, space=7 → clean cut at 7.
  842. // Use a multibyte straddle instead: 'aé' repeated — cut at 7 bytes: a(1)é(2)a(1)é(2)=6 +a(1)=7 → next é straddles: trimmed.
  843. subprocess.handler = () => runResult(`${matchLine('a.txt', 1, 'aéaéaéaé')}\n`)
  844. const result = await call(ctx, 'grep', { pattern: 'a' })
  845. if (result.isError) throw new Error('expected grep success')
  846. expect(result.value).toEqual({ matches: [{ path: 'a.txt', lineNumber: 1, line: 'aéaéaéaé' }] })
  847. expect(text(result)).toContain('Line 1: aéaéa (line truncated)')
  848. })
  849. it('renders a non-UTF-8 line (rg bytes form) as a placeholder instead of failing', async () => {
  850. const { ctx, subprocess } = await setup()
  851. const record = JSON.stringify({ type: 'match', data: { path: { text: 'bin.dat' }, lines: { bytes: 'AAECww==' }, line_number: 4 } })
  852. subprocess.handler = () => runResult(`${record}\n`)
  853. expect(text(await call(ctx, 'grep', { pattern: 'x' }))).toContain('Line 4: (line is not valid UTF-8)')
  854. })
  855. it('strips a CRLF terminator from the matched line text', () => {
  856. const matches = parseGrepMatches(`${matchLine('a.txt', 1, 'windows line\r\n')}\n`)
  857. expect(matches[0]?.line).toBe('windows line')
  858. })
  859. it('caps at grepMaxMatches and spills the full formatted match list', async () => {
  860. const { ctx, subprocess, spill } = await setup({ config: { grepMaxMatches: 2 }, spill: true })
  861. ctx.on('tools/post-execute', async () => ({
  862. kind: 'accept',
  863. additionalContexts: [createUserMessage({
  864. content: [{ type: 'text', text: 'grep context' }], source: { kind: 'plugin', plugin: 'test' },
  865. })],
  866. }))
  867. subprocess.handler = () => runResult([
  868. matchLine('a.ts', 1, 'one'),
  869. matchLine('a.ts', 2, 'two'),
  870. matchLine('b.ts', 3, 'three'),
  871. '',
  872. ].join('\n'))
  873. const result = await call(ctx, 'grep', { pattern: 'e' }, { agent: agent('/w') })
  874. if (result.isError) throw new Error('expected grep success')
  875. expect(result.value).toEqual({
  876. matches: [
  877. { path: 'a.ts', lineNumber: 1, line: 'one' },
  878. { path: 'a.ts', lineNumber: 2, line: 'two' },
  879. { path: 'b.ts', lineNumber: 3, line: 'three' },
  880. ],
  881. })
  882. expect(text(result)).toBe('Found 2 of 3 matches\n\na.ts\nLine 1: one\nLine 2: two\n\n(Full grep result stored at: /spill/grep-results.txt. Use the fake retrieval hint.)')
  883. expect(spill?.saves[0]).toMatchObject({
  884. source: { toolName: 'grep', label: 'result' },
  885. suggestedName: 'grep-results.txt',
  886. content: 'Found 3 matches\n\na.ts\nLine 1: one\nLine 2: two\n\nb.ts\nLine 3: three',
  887. })
  888. expect(result.additionalContexts?.[0]?.content).toEqual([{ type: 'text', text: 'grep context' }])
  889. })
  890. it('preserves a downstream canonical value replacement instead of spilling the old matches', async () => {
  891. const { ctx, subprocess, spill } = await setup({ config: { grepMaxMatches: 1 }, spill: true })
  892. ctx.on('tools/post-execute', async () => ({
  893. kind: 'accept' as const,
  894. value: {
  895. matches: [
  896. { path: 'replacement.ts', lineNumber: 7, line: 'first' },
  897. { path: 'replacement.ts', lineNumber: 8, line: 'second' },
  898. ],
  899. },
  900. }))
  901. subprocess.handler = () => runResult(`${matchLine('old.ts', 1, 'old')}\n`)
  902. const result = await call(ctx, 'grep', { pattern: 'old' }, { agent: agent('/w') })
  903. if (result.isError) throw new Error('expected grep replacement success')
  904. expect(result.value).toEqual({
  905. matches: [
  906. { path: 'replacement.ts', lineNumber: 7, line: 'first' },
  907. { path: 'replacement.ts', lineNumber: 8, line: 'second' },
  908. ],
  909. })
  910. expect(text(result)).toContain('replacement.ts')
  911. expect(text(result)).not.toContain('old.ts')
  912. expect(spill?.saves).toHaveLength(0)
  913. })
  914. it('keeps every nested Code match in the value without creating a surface spill', async () => {
  915. const { ctx, subprocess, spill } = await setup({ config: { grepMaxMatches: 1 }, spill: true })
  916. subprocess.handler = () => runResult(`${matchLine('a.ts', 1, 'one')}\n${matchLine('b.ts', 2, 'two')}\n`)
  917. const result = await call(ctx, 'grep', { pattern: 'o' }, {
  918. agent: agent('/w'),
  919. parent: Symbol('run_code') as ToolExecutionToken,
  920. })
  921. if (result.isError) throw new Error('expected grep success')
  922. expect(result.value).toEqual({
  923. matches: [
  924. { path: 'a.ts', lineNumber: 1, line: 'one' },
  925. { path: 'b.ts', lineNumber: 2, line: 'two' },
  926. ],
  927. })
  928. expect(text(result)).toBe('Found 1 of 2 matches\n\na.ts\nLine 1: one\n\n(The complete result could not be saved; narrow pattern, path, or include to see more.)')
  929. expect(spill?.saves).toHaveLength(0)
  930. })
  931. it('reports the unsaved remainder when capped with no spill backend', async () => {
  932. const { ctx, subprocess } = await setup({ config: { grepMaxMatches: 1 } })
  933. subprocess.handler = () => runResult(`${matchLine('a.ts', 1, 'one')}\n${matchLine('a.ts', 2, 'two')}\n`)
  934. const result = await call(ctx, 'grep', { pattern: 'o' }, { agent: agent('/w') })
  935. expect(result.isError).toBe(false)
  936. expect(text(result)).toBe('Found 1 of 2 matches\n\na.ts\nLine 1: one\n\n(The complete result could not be saved; narrow pattern, path, or include to see more.)')
  937. })
  938. it('validates arguments (empty pattern, blank path, bad include)', async () => {
  939. const { ctx } = await setup()
  940. expect(text(await call(ctx, 'grep', { pattern: '' }))).toContain('pattern must be a non-empty string')
  941. expect(text(await call(ctx, 'grep', { pattern: 'x', path: ' ' }))).toContain('path must be a non-empty string')
  942. expect(text(await call(ctx, 'grep', { pattern: 'x', include: ' ' }))).toContain('include must be a non-empty glob')
  943. expect(text(await call(ctx, 'grep', { pattern: 'x', include: '!*.ts' }))).toContain('negated patterns')
  944. expect(text(await call(ctx, 'grep', { pattern: 'x', include: '*.ts,*.js' }))).toContain('comma-separated list')
  945. })
  946. it('accepts a whitespace-only pattern (a legitimate regex) and brace alternation in include', async () => {
  947. const { ctx, subprocess } = await setup()
  948. subprocess.handler = () => runResult('', { exitCode: 1 })
  949. const result = await call(ctx, 'grep', { pattern: ' ', include: '*.{ts,tsx}' })
  950. expect(result.isError).toBe(false)
  951. })
  952. })
  953. describe('rg --json transport failures (SEARCH_FAILED)', () => {
  954. it.each([
  955. ['a non-JSON line', 'not json at all'],
  956. ['a non-object record', '42'],
  957. ['a match record with no data', JSON.stringify({ type: 'match' })],
  958. ['a match record with no path text', JSON.stringify({ type: 'match', data: { path: {}, lines: { text: 'x' }, line_number: 1 } })],
  959. ['a match record with a non-object path', JSON.stringify({ type: 'match', data: { path: 'a.ts', lines: { text: 'x' }, line_number: 1 } })],
  960. ['a match record with no line number', JSON.stringify({ type: 'match', data: { path: { text: 'a.ts' }, lines: { text: 'x' } } })],
  961. ['a match record with no line content', JSON.stringify({ type: 'match', data: { path: { text: 'a.ts' }, line_number: 1 } })],
  962. ['a match record with neither text nor bytes', JSON.stringify({ type: 'match', data: { path: { text: 'a.ts' }, lines: {}, line_number: 1 } })],
  963. ])('%s fails the search', async (_label, line) => {
  964. const { ctx, subprocess } = await setup()
  965. subprocess.handler = () => runResult(`${line}\n`)
  966. const result = await call(ctx, 'grep', { pattern: 'x' })
  967. expect(result.isError).toBe(true)
  968. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_FAILED' } })
  969. })
  970. })
  971. describe('the no-background-task invariant', () => {
  972. it('settles every spawned search handle across successful and failed searches', async () => {
  973. const { ctx, subprocess } = await setup()
  974. subprocess.handler = () => runResult('a.ts\n')
  975. await call(ctx, 'glob', { pattern: '*' })
  976. subprocess.handler = () => runResult('', { exitCode: 2, stderr: { text: 'boom' } })
  977. await call(ctx, 'grep', { pattern: 'x' })
  978. // One foreground spawn per call, each awaited to settlement before the
  979. // tool returns — the searches never leave a background handle running.
  980. expect(subprocess.spawns).toHaveLength(2)
  981. expect(subprocess.handles.every(handle => handle.settled)).toBe(true)
  982. })
  983. })
  984. describe('presentation', () => {
  985. it('glob titles carry the pattern and optional root', () => {
  986. expect(presentGlobCall({ pattern: '**/*.ts' })).toMatchObject({ card: 'generic', title: 'Glob **/*.ts', kind: 'search' })
  987. expect(presentGlobCall({ pattern: '*.md', path: 'docs' }).title).toBe('Glob *.md in docs')
  988. })
  989. it('grep titles carry the pattern, target, and include filter', () => {
  990. expect(presentGrepCall({ pattern: 'todo' })).toMatchObject({ card: 'generic', title: 'Grep todo', kind: 'search' })
  991. expect(presentGrepCall({ pattern: 'todo', path: 'src', include: '*.ts' }).title).toBe('Grep todo in src (*.ts)')
  992. })
  993. it('grep projects a search card from a real execute, grouped by file with total and truncation', async () => {
  994. const { ctx, subprocess } = await setup({ config: { grepMaxMatches: 2 } })
  995. subprocess.handler = () => runResult([
  996. matchLine('a.ts', 1, 'one'),
  997. matchLine('a.ts', 2, 'two'),
  998. matchLine('b.ts', 3, 'three'),
  999. '',
  1000. ].join('\n'))
  1001. const result = await call(ctx, 'grep', { pattern: 'e' }, { agent: agent('/w') })
  1002. if (result.isError) throw new Error('expected grep success')
  1003. // The presentationMeta projection rides the result meta (a surface call).
  1004. expect(result.meta).toEqual({
  1005. shape: 'matches',
  1006. files: [{ path: 'a.ts', matches: [{ lineNumber: 1, line: 'one' }, { lineNumber: 2, line: 'two' }] }],
  1007. truncated: true,
  1008. total: 3,
  1009. })
  1010. const view = presentGrepResult({ pattern: 'e' }, result)
  1011. expect(view).toEqual({
  1012. card: 'search',
  1013. shape: 'matches',
  1014. files: [{ path: 'a.ts', matches: [{ lineNumber: 1, line: 'one' }, { lineNumber: 2, line: 'two' }] }],
  1015. truncated: true,
  1016. total: 3,
  1017. })
  1018. })
  1019. it('glob projects a search card from a real execute, a flat path list with total and truncation', async () => {
  1020. const { ctx, subprocess } = await setup({ config: { globMaxResults: 2 } })
  1021. subprocess.handler = () => runResult('a.ts\nb.ts\nc.ts\n')
  1022. const result = await call(ctx, 'glob', { pattern: '*.ts' }, { agent: agent('/w') })
  1023. if (result.isError) throw new Error('expected glob success')
  1024. expect(result.meta).toEqual({ shape: 'paths', paths: ['a.ts', 'b.ts'], truncated: true, total: 3 })
  1025. const view = presentGlobResult({ pattern: '*.ts' }, result)
  1026. expect(view).toEqual({ card: 'search', shape: 'paths', paths: ['a.ts', 'b.ts'], truncated: true, total: 3 })
  1027. })
  1028. it('nested Code dispatch computes no meta, so presentResult falls back to the generic card', async () => {
  1029. const { ctx, subprocess } = await setup()
  1030. subprocess.handler = () => runResult(`${matchLine('a.ts', 1, 'one')}\n`)
  1031. const result = await call(ctx, 'grep', { pattern: 'o' }, {
  1032. agent: agent('/w'),
  1033. parent: Symbol('run_code') as ToolExecutionToken,
  1034. })
  1035. if (result.isError) throw new Error('expected grep success')
  1036. expect(result.meta).toBeUndefined()
  1037. expect(presentGrepResult({ pattern: 'o' }, result)).toBeUndefined()
  1038. })
  1039. it('presentResult returns undefined for a failed result and for the other tool’s meta shape', () => {
  1040. const errorResult = { content: [{ type: 'text' as const, text: 'boom' }], isError: true }
  1041. expect(presentGrepResult({ pattern: 'x' }, errorResult)).toBeUndefined()
  1042. expect(presentGlobResult({ pattern: '*' }, errorResult)).toBeUndefined()
  1043. // A grep result carrying a paths-shaped meta (and vice versa) is not this
  1044. // tool's shape: each presenter narrows to its own shape and otherwise falls back.
  1045. const pathsResult = { content: [], isError: false, meta: { shape: 'paths', paths: ['a.ts'], truncated: false, total: 1 } }
  1046. const matchesResult = { content: [], isError: false, meta: { shape: 'matches', files: [], truncated: false, total: 0 } }
  1047. expect(presentGrepResult({ pattern: 'x' }, pathsResult)).toBeUndefined()
  1048. expect(presentGlobResult({ pattern: '*' }, matchesResult)).toBeUndefined()
  1049. })
  1050. it('presentResult falls back to the generic card on malformed replayed meta', () => {
  1051. const malformed = { content: [], isError: false, meta: { shape: 'matches', files: 'nope', truncated: false, total: 0 } }
  1052. expect(presentGrepResult({ pattern: 'x' }, malformed)).toBeUndefined()
  1053. expect(presentGlobResult({ pattern: '*' }, { content: [], isError: false, meta: 42 })).toBeUndefined()
  1054. })
  1055. })
  1056. describe('helpers', () => {
  1057. it('toWorkdirRelative maps inside-workdir absolutes and passes everything else through', () => {
  1058. expect(toWorkdirRelative('/w/a/b.ts', '/w')).toBe(join('a', 'b.ts'))
  1059. expect(toWorkdirRelative('/w', '/w')).toBe('.')
  1060. expect(toWorkdirRelative('/other/b.ts', '/w')).toBe('/other/b.ts')
  1061. expect(toWorkdirRelative('/w-sibling/b.ts', '/w')).toBe('/w-sibling/b.ts')
  1062. expect(toWorkdirRelative('rel/b.ts', '/w')).toBe('rel/b.ts')
  1063. // Normalization makes this land OUTSIDE the workdir → original path kept.
  1064. expect(toWorkdirRelative('/w/../up.ts', '/w')).toBe('/w/../up.ts')
  1065. })
  1066. it('previewLine keeps a within-budget line untouched', () => {
  1067. expect(previewLine('short', 100)).toBe('short')
  1068. })
  1069. it('formatGrepMatches groups by first-seen file order', () => {
  1070. const grouped = formatGrepMatches([
  1071. { path: 'b.ts', lineNumber: 2, line: 'x' },
  1072. { path: 'a.ts', lineNumber: 1, line: 'y' },
  1073. { path: 'b.ts', lineNumber: 5, line: 'z' },
  1074. ])
  1075. expect(grouped).toBe('b.ts\nLine 2: x\nLine 5: z\n\na.ts\nLine 1: y')
  1076. })
  1077. })