tools.spec.ts 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /**
  2. * Consumer-surface tests for the search tools over a FAKE bash executor and a
  3. * FAKE spill backend, exercised through `ctx.tools.execute()` so nothing
  4. * bypasses the tool registry. The fake executor makes every seam outcome
  5. * scriptable — registration-time `rg` probing, truncated stdout with/without a
  6. * raw spill path, abort/timeout, signal kills, ripgrep exit codes — so these
  7. * tests verify schemas, argument validation, shell-safe command construction,
  8. * workdir derivation, signal forwarding, `SEARCH_*` error classification,
  9. * retention, formatted-result spill handoff, and the no-background-task
  10. * invariant. 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 } from 'node:path'
  15. import { CallId } from '@deepseek-ai/dsh-llm'
  16. import SystemPrompt, { renderPrompt } from '@deepseek-ai/dsh-system-prompt'
  17. import ToolRegistry, { TOOL_ABORTED_BEFORE_DISPATCH, type ToolExecutionToken } from '@deepseek-ai/dsh-tools'
  18. import { BashExecutor } from '@deepseek-ai/dsh-bash'
  19. import type { BashExecRequest, BashExecSpec, BashProcess, BashRunResult } from '@deepseek-ai/dsh-bash'
  20. import { SpillLocator, SpillStore } from '@deepseek-ai/dsh-spill'
  21. import type { SaveTextSpill, SpillRef } from '@deepseek-ai/dsh-spill'
  22. import * as ToolFsSearch from '@deepseek-ai/dsh-tool-fs-search'
  23. import {
  24. buildGlobCommand,
  25. buildGrepCommand,
  26. formatGrepMatches,
  27. parseGrepMatches,
  28. presentGlobCall,
  29. presentGrepCall,
  30. previewLine,
  31. toWorkdirRelative,
  32. } from '@deepseek-ai/dsh-tool-fs-search'
  33. const testToolSignal = new AbortController().signal
  34. const RG_PROBE_COMMAND = 'command -v rg >/dev/null 2>&1'
  35. /** A successful run result over the given stdout; overrides script the failure shapes. */
  36. function runResult(stdout: string, overrides?: Partial<BashRunResult>): BashRunResult {
  37. return {
  38. exitCode: 0,
  39. signal: null,
  40. timedOut: false,
  41. aborted: false,
  42. timeoutMs: 60_000,
  43. stdout: { text: stdout, truncated: false },
  44. stderr: { text: '', truncated: false },
  45. ...overrides,
  46. }
  47. }
  48. /**
  49. * A scriptable fake executor: `resolve()` mirrors the real request→spec
  50. * defaulting (workdir falls back to `/work`), `run()` returns whatever the
  51. * test armed via `handler`, and `start()` throws — the search tools must NEVER
  52. * create a background task.
  53. */
  54. class FakeBash extends BashExecutor {
  55. probeRequests: BashExecRequest[] = []
  56. probeSpecs: BashExecSpec[] = []
  57. requests: BashExecRequest[] = []
  58. specs: BashExecSpec[] = []
  59. startCalls = 0
  60. forwardSignal = true
  61. probeResult: BashRunResult = runResult('')
  62. probeError?: Error
  63. handler: (spec: BashExecSpec) => BashRunResult = () => runResult('')
  64. override resolve(request: BashExecRequest): BashExecSpec {
  65. if (request.command === RG_PROBE_COMMAND) this.probeRequests.push(request)
  66. else this.requests.push(request)
  67. return {
  68. command: request.command,
  69. workdir: request.workdir ?? '/work',
  70. timeoutMs: request.timeoutMs ?? 60_000,
  71. stdoutMaxBytes: request.stdoutMaxBytes ?? 64_000,
  72. ...this.forwardSignal ? { signal: request.signal } : {},
  73. sandboxPolicy: request.sandboxPolicy,
  74. }
  75. }
  76. override async run(spec: BashExecSpec): Promise<BashRunResult> {
  77. if (spec.command === RG_PROBE_COMMAND) {
  78. this.probeSpecs.push(spec)
  79. if (this.probeError) throw this.probeError
  80. return this.probeResult
  81. }
  82. this.specs.push(spec)
  83. return this.handler(spec)
  84. }
  85. override start(): BashProcess {
  86. this.startCalls++
  87. throw new Error('search tools must never start a background task')
  88. }
  89. }
  90. /** A recording spill backend; arm `failWith` to script a storage failure. */
  91. class FakeSpill extends SpillStore {
  92. saves: SaveTextSpill[] = []
  93. failWith?: Error
  94. override saveText(input: SaveTextSpill): Promise<SpillRef> {
  95. if (this.failWith) return Promise.reject(this.failWith)
  96. this.saves.push(input)
  97. return Promise.resolve({
  98. locator: SpillLocator(`/spill/${input.suggestedName}`),
  99. bytes: Buffer.byteLength(input.content, 'utf8'),
  100. retrievalHint: 'Use the fake retrieval hint.',
  101. })
  102. }
  103. }
  104. interface SetupOptions {
  105. config?: ToolFsSearch.Config
  106. spill?: boolean
  107. probeError?: Error
  108. probeResult?: BashRunResult
  109. }
  110. async function setup(options: SetupOptions = {}) {
  111. const ctx = new Context()
  112. const warnings: string[] = []
  113. ctx.logger.warn = ((message: unknown) => { warnings.push(String(message)) }) as typeof ctx.logger.warn
  114. await ctx.plugin(SystemPrompt)
  115. await ctx.plugin(ToolRegistry)
  116. await ctx.plugin(FakeBash)
  117. const bash = ctx.bash as FakeBash
  118. if (options.probeResult) bash.probeResult = options.probeResult
  119. if (options.probeError) bash.probeError = options.probeError
  120. if (options.spill === true) await ctx.plugin(FakeSpill)
  121. const fiber = await ctx.plugin(ToolFsSearch, options.config)
  122. const spill = options.spill === true ? ctx.get('spillStore') as FakeSpill : undefined
  123. return { ctx, bash, spill, fiber, warnings }
  124. }
  125. /** Assert plugin setup rejects without letting Vitest pretty-print a live Context on failure. */
  126. async function expectSetupRejects(options: SetupOptions, message: RegExp): Promise<void> {
  127. let thrown: string | undefined
  128. try {
  129. const loaded = await setup(options)
  130. await loaded.fiber.dispose()
  131. } catch (error: unknown) {
  132. thrown = error instanceof Error ? error.message : String(error)
  133. }
  134. expect(thrown).toMatch(message)
  135. }
  136. /** A stand-in agent whose session header carries the given cwd (and a stable id). */
  137. const agent = (cwd?: string) => ({ session: { header: { id: 'session-1', ...cwd !== undefined ? { cwd } : {} } } })
  138. let callCounter = 0
  139. function call(
  140. ctx: Context,
  141. name: string,
  142. args: unknown,
  143. options: { agent?: object; signal?: AbortSignal; parent?: ToolExecutionToken } = {},
  144. ) {
  145. return ctx.tools.execute({
  146. signal: testToolSignal,
  147. callId: CallId(`call-${++callCounter}`),
  148. name,
  149. arguments: args,
  150. ...options.agent ? { agent: options.agent as never } : {},
  151. ...options.signal ? { signal: options.signal } : {},
  152. ...options.parent ? { parent: options.parent } : {},
  153. })
  154. }
  155. function text(result: { content: { type: string; text?: string }[] }): string {
  156. return result.content.filter(b => b.type === 'text').map(b => b.text).join('')
  157. }
  158. /** One rg --json match record line. */
  159. function matchLine(path: string, lineNumber: number, lineText: string): string {
  160. return JSON.stringify({ type: 'match', data: { path: { text: path }, lines: { text: lineText }, line_number: lineNumber, absolute_offset: 0, submatches: [] } })
  161. }
  162. describe('registration', () => {
  163. it('registers glob and grep with their prompt sections', async () => {
  164. const { ctx, bash } = await setup()
  165. expect(bash.probeRequests).toHaveLength(1)
  166. expect(bash.probeRequests[0]?.command).toBe(RG_PROBE_COMMAND)
  167. expect(bash.probeRequests[0]).not.toHaveProperty('workdir')
  168. expect(ctx.tools.schemas().map(s => s.name).sort()).toEqual(['glob', 'grep'])
  169. const prompt = renderPrompt(await ctx.systemPrompt.assemble())
  170. expect(prompt).toContain('Use the glob tool')
  171. expect(prompt).toContain('Use the grep tool')
  172. })
  173. it('does not register glob or grep when the bash executor cannot find rg', async () => {
  174. const { ctx, warnings } = await setup({ probeResult: runResult('', { exitCode: 1 }) })
  175. expect(ctx.tools.schemas()).toHaveLength(0)
  176. const sections = (await ctx.systemPrompt.assemble()).sections.map(s => s.name)
  177. expect(sections).not.toContain('tool:glob')
  178. expect(sections).not.toContain('tool:grep')
  179. expect(warnings).toEqual([
  180. 'tool-fs-search: ripgrep (rg) not found on the bash executor PATH; glob/grep tools not registered',
  181. ])
  182. })
  183. it('rejects plugin load when the rg availability probe cannot run', async () => {
  184. await expectSetupRejects({ probeError: new Error('spawn bash ENOENT') }, /spawn bash ENOENT/)
  185. })
  186. it('rejects plugin load when the rg availability probe is aborted or killed', async () => {
  187. await expectSetupRejects({
  188. probeResult: runResult('', { aborted: true, exitCode: null, signal: 'SIGTERM' }),
  189. }, /tool-fs-search: ripgrep availability probe did not complete/)
  190. })
  191. it('stays pending until ctx.bash exists (inject)', async () => {
  192. const ctx = new Context()
  193. await ctx.plugin(SystemPrompt)
  194. await ctx.plugin(ToolRegistry)
  195. await ctx.plugin(ToolFsSearch) // no bash executor
  196. expect(ctx.tools.schemas()).toHaveLength(0)
  197. })
  198. it('unregisters everything on fiber disposal (HMR safety)', async () => {
  199. const { ctx, fiber } = await setup()
  200. expect(ctx.tools.schemas()).toHaveLength(2)
  201. await fiber.dispose()
  202. expect(ctx.tools.schemas()).toHaveLength(0)
  203. const sections = (await ctx.systemPrompt.assemble()).sections.map(s => s.name)
  204. expect(sections).not.toContain('tool:glob')
  205. expect(sections).not.toContain('tool:grep')
  206. })
  207. it('attaches the configured timeoutMs to both tool definitions', async () => {
  208. const { ctx } = await setup({ config: { timeoutMs: 5000 } })
  209. expect(ctx.tools.get('glob')?.timeoutMs).toBe(5000)
  210. expect(ctx.tools.get('grep')?.timeoutMs).toBe(5000)
  211. })
  212. it('defaults the timeout budget to 30 seconds', async () => {
  213. const { ctx } = await setup()
  214. expect(ctx.tools.get('glob')?.timeoutMs).toBe(30_000)
  215. expect(ctx.tools.get('grep')?.timeoutMs).toBe(30_000)
  216. })
  217. })
  218. describe('config validation', () => {
  219. it.each([
  220. ['globMaxResults', { globMaxResults: 0 }],
  221. ['grepMaxMatches', { grepMaxMatches: -1 }],
  222. ['grepMaxLineBytes', { grepMaxLineBytes: 1.5 }],
  223. ['rawOutputMaxBytes', { rawOutputMaxBytes: 0 }],
  224. ['timeoutMs', { timeoutMs: -100 }],
  225. ] as const)('rejects a non-positive or fractional %s at load', async (name, config) => {
  226. const ctx = new Context()
  227. await ctx.plugin(SystemPrompt)
  228. await ctx.plugin(ToolRegistry)
  229. await ctx.plugin(FakeBash)
  230. await expect(ctx.plugin(ToolFsSearch, config)).rejects.toThrow(new RegExp(`tool-fs-search: ${name} must be a positive integer`))
  231. })
  232. })
  233. describe('command construction (shell-safe)', () => {
  234. it('glob: fixed rg --files template with quoted pattern and paired VCS excludes', () => {
  235. const command = buildGlobCommand({ pattern: '**/*.ts' })
  236. expect(command).toBe(
  237. "rg --files --glob='**/*.ts' --sort=modified --no-ignore --hidden "
  238. + "--glob='!**/.git' --glob='!**/.git/**' --glob='!**/.svn' --glob='!**/.svn/**' "
  239. + "--glob='!**/.hg' --glob='!**/.hg/**' --glob='!**/.bzr' --glob='!**/.bzr/**' "
  240. + "--glob='!**/.jj' --glob='!**/.jj/**' --glob='!**/.sl' --glob='!**/.sl/**'",
  241. )
  242. })
  243. it('glob: the search root rides behind -- and is quoted', () => {
  244. const command = buildGlobCommand({ pattern: '*.md', path: 'docs dir' })
  245. expect(command).toContain("-- 'docs dir'")
  246. })
  247. it('grep: fixed rg --json template with the pattern in --regexp= form', () => {
  248. expect(buildGrepCommand({ pattern: 'foo.*bar' })).toBe("rg --json --regexp='foo.*bar'")
  249. })
  250. it('grep: include and path are quoted, include in --glob= form, path behind --', () => {
  251. const command = buildGrepCommand({ pattern: 'x', path: '-leading-dash', include: '*.{ts,tsx}' })
  252. expect(command).toBe("rg --json --regexp='x' --glob='*.{ts,tsx}' -- '-leading-dash'")
  253. })
  254. it.each([
  255. ['a command-substitution pattern', '$(rm -rf /)', "'$(rm -rf /)'"],
  256. ['a backtick pattern', '`touch pwned`', "'`touch pwned`'"],
  257. ['a pattern with double quotes and spaces', 'say "hi there"', '\'say "hi there"\''],
  258. ['a pattern with single quotes', "it's", '\'it\'\\\'\'s\''],
  259. ['a pattern with newlines', 'a\nb', "'a\nb'"],
  260. ['a leading-dash pattern', '--flag', "'--flag'"],
  261. ['glob metacharacters', '*?[a-z]{x,y}', "'*?[a-z]{x,y}'"],
  262. ])('quotes %s into one inert shell word', (_label, raw, quoted) => {
  263. expect(buildGrepCommand({ pattern: raw })).toBe(`rg --json --regexp=${quoted}`)
  264. })
  265. })
  266. describe('workdir derivation and signal forwarding', () => {
  267. it('forwards the session cwd as the request workdir', async () => {
  268. const { ctx, bash } = await setup()
  269. bash.handler = () => runResult('a.ts\n')
  270. await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/sessions/s1') })
  271. expect(bash.requests[0]?.workdir).toBe('/sessions/s1')
  272. expect(bash.specs[0]?.workdir).toBe('/sessions/s1')
  273. })
  274. it('omits the request workdir without a session cwd so resolve() defaults apply', async () => {
  275. const { ctx, bash } = await setup()
  276. bash.handler = () => runResult('a.ts\n')
  277. await call(ctx, 'glob', { pattern: '*' }, { agent: agent() })
  278. expect(bash.requests[0]).not.toHaveProperty('workdir')
  279. expect(bash.specs[0]?.workdir).toBe('/work')
  280. // A non-agent caller takes the same default path.
  281. await call(ctx, 'grep', { pattern: 'x' })
  282. expect(bash.requests[1]).not.toHaveProperty('workdir')
  283. })
  284. it('forwards exec.signal into the bash spec', async () => {
  285. const { ctx, bash } = await setup()
  286. const controller = new AbortController()
  287. bash.handler = () => runResult('')
  288. const result = await call(ctx, 'grep', { pattern: 'x' }, { signal: controller.signal })
  289. expect(bash.specs[0]?.signal).toBe(controller.signal)
  290. expect(result.isError).toBe(false)
  291. })
  292. it('reports the bash executor timeout as SEARCH_ABORTED with the budget', async () => {
  293. const { ctx, bash } = await setup()
  294. bash.handler = () => runResult('', { timedOut: true, timeoutMs: 1234, exitCode: null, signal: 'SIGTERM' })
  295. const result = await call(ctx, 'glob', { pattern: '*' })
  296. expect(result.isError).toBe(true)
  297. expect(result.error).toMatchObject({ info: { code: 'SEARCH_ABORTED' } })
  298. expect(text(result)).toContain('timed out after 1234ms')
  299. })
  300. it('skips a pre-aborted registry call before run()', async () => {
  301. const { ctx, bash } = await setup()
  302. const controller = new AbortController()
  303. controller.abort()
  304. bash.handler = () => { throw new Error('aborted before spawn') }
  305. const result = await call(ctx, 'grep', { pattern: 'x' }, { signal: controller.signal })
  306. expect(result.isError).toBe(true)
  307. expect(result.error).toMatchObject({ info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } })
  308. expect(bash.specs).toHaveLength(0)
  309. })
  310. it('translates a run() rejection after the forwarded signal aborts', async () => {
  311. const { ctx, bash } = await setup()
  312. const controller = new AbortController()
  313. bash.handler = () => {
  314. controller.abort('cancel search')
  315. throw new Error('executor stopped on abort')
  316. }
  317. const result = await call(ctx, 'grep', { pattern: 'x' }, { signal: controller.signal })
  318. expect(result.isError).toBe(true)
  319. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_ABORTED' } })
  320. expect(text(result)).toContain('aborted before completion')
  321. })
  322. it('translates an aborted executor result after dispatch starts', async () => {
  323. const { ctx, bash } = await setup()
  324. bash.handler = () => runResult('', { aborted: true, exitCode: null })
  325. const result = await call(ctx, 'glob', { pattern: '*' })
  326. expect(result.isError).toBe(true)
  327. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_ABORTED' } })
  328. expect(text(result)).toContain('aborted before completion')
  329. })
  330. it('translates a run() rejection without an abort (unusable workdir) into SEARCH_FAILED', async () => {
  331. const { ctx, bash } = await setup()
  332. bash.forwardSignal = false
  333. bash.handler = () => { throw new Error('spawn bash ENOENT') }
  334. const result = await call(ctx, 'glob', { pattern: '*' })
  335. expect(result.isError).toBe(true)
  336. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_FAILED' } })
  337. expect(text(result)).toContain('could not start')
  338. })
  339. })
  340. describe('exit semantics and failure classification', () => {
  341. it('exit 1 is a successful empty search', async () => {
  342. const { ctx, bash } = await setup()
  343. bash.handler = () => runResult('', { exitCode: 1 })
  344. const glob = await call(ctx, 'glob', { pattern: '*.nope' })
  345. expect(glob.isError).toBe(false)
  346. expect(text(glob)).toBe('No files found')
  347. const grep = await call(ctx, 'grep', { pattern: 'nope' })
  348. expect(grep.isError).toBe(false)
  349. expect(text(grep)).toBe('No matches found')
  350. })
  351. it('a regex parse error classifies as SEARCH_INVALID_PATTERN', async () => {
  352. const { ctx, bash } = await setup()
  353. bash.handler = () => runResult('', { exitCode: 2, stderr: { text: 'rg: regex parse error:\n (\nerror: unclosed group', truncated: false } })
  354. const result = await call(ctx, 'grep', { pattern: '(' })
  355. expect(result.error).toMatchObject({ info: { code: 'SEARCH_INVALID_PATTERN' } })
  356. expect(text(result)).toContain('regex parse error')
  357. })
  358. it('a glob parse error classifies as SEARCH_INVALID_PATTERN', async () => {
  359. const { ctx, bash } = await setup()
  360. bash.handler = () => runResult('', { exitCode: 2, stderr: { text: 'rg: error parsing glob \'[\': unclosed character class', truncated: false } })
  361. const result = await call(ctx, 'glob', { pattern: '[' })
  362. expect(result.error).toMatchObject({ info: { code: 'SEARCH_INVALID_PATTERN' } })
  363. })
  364. it('a missing rg binary classifies as SEARCH_FAILED naming ripgrep', async () => {
  365. const { ctx, bash } = await setup()
  366. bash.handler = () => runResult('', { exitCode: 127, stderr: { text: 'bash: line 1: rg: command not found', truncated: false } })
  367. const result = await call(ctx, 'glob', { pattern: '*' })
  368. expect(result.error).toMatchObject({ info: { code: 'SEARCH_FAILED' } })
  369. expect(text(result)).toContain('requires ripgrep (rg)')
  370. // The same classification holds from either evidence alone: the 127 exit
  371. // with silent stderr, or a shell's command-not-found text on another exit.
  372. bash.handler = () => runResult('', { exitCode: 127 })
  373. expect(text(await call(ctx, 'glob', { pattern: '*' }))).toContain('requires ripgrep (rg)')
  374. bash.handler = () => runResult('', { exitCode: 2, stderr: { text: 'sh: rg: command not found', truncated: false } })
  375. expect(text(await call(ctx, 'grep', { pattern: 'x' }))).toContain('requires ripgrep (rg)')
  376. })
  377. it('other nonzero exits are SEARCH_FAILED carrying the stderr excerpt', async () => {
  378. const { ctx, bash } = await setup()
  379. bash.handler = () => runResult('', { exitCode: 2, stderr: { text: 'rg: missing.dir: IO error: no such file or directory', truncated: false } })
  380. const result = await call(ctx, 'grep', { pattern: 'x', path: 'missing.dir' })
  381. expect(result.error).toMatchObject({ info: { code: 'SEARCH_FAILED' } })
  382. expect(text(result)).toContain('IO error')
  383. })
  384. it('a nonzero exit with EMPTY stderr still reports the exit code', async () => {
  385. const { ctx, bash } = await setup()
  386. bash.handler = () => runResult('', { exitCode: 3 })
  387. const result = await call(ctx, 'glob', { pattern: '*' })
  388. expect(result.error).toMatchObject({ info: { code: 'SEARCH_FAILED' } })
  389. expect(text(result)).toContain('exit 3')
  390. })
  391. it('truncated stderr gains a truncation note and stderr.spillPath is never read', async () => {
  392. const { ctx, bash } = await setup()
  393. bash.handler = () => runResult('', {
  394. exitCode: 2,
  395. stderr: { text: 'tail of diagnostics', truncated: true, spillPath: '/does/not/exist-and-never-read' },
  396. })
  397. const result = await call(ctx, 'grep', { pattern: 'x' })
  398. expect(text(result)).toContain('tail of diagnostics [stderr truncated]')
  399. })
  400. it('a signal kill (not timeout, not abort) is SEARCH_FAILED', async () => {
  401. const { ctx, bash } = await setup()
  402. bash.handler = () => runResult('', { exitCode: null, signal: 'SIGKILL' })
  403. const result = await call(ctx, 'grep', { pattern: 'x' })
  404. expect(result.error).toMatchObject({ info: { code: 'SEARCH_FAILED' } })
  405. expect(text(result)).toContain('SIGKILL')
  406. })
  407. it('a null exit with no signal (defensive) is SEARCH_FAILED', async () => {
  408. const { ctx, bash } = await setup()
  409. bash.handler = () => runResult('', { exitCode: null, signal: null })
  410. const result = await call(ctx, 'glob', { pattern: '*' })
  411. expect(result.error).toMatchObject({ info: { code: 'SEARCH_FAILED' } })
  412. })
  413. })
  414. describe('raw output acquisition', () => {
  415. it('passes rawOutputMaxBytes to bash as the stdout capture budget', async () => {
  416. const { ctx, bash } = await setup({ config: { rawOutputMaxBytes: 1234 } })
  417. bash.handler = () => runResult('', { exitCode: 1 })
  418. await call(ctx, 'glob', { pattern: '*.ts' })
  419. await call(ctx, 'grep', { pattern: 'needle' })
  420. expect(bash.requests.map(request => request.stdoutMaxBytes)).toEqual([1234, 1234])
  421. expect(bash.specs.map(spec => spec.stdoutMaxBytes)).toEqual([1234, 1234])
  422. })
  423. it('fails with SEARCH_RAW_OUTPUT_OVERFLOW when truncated stdout has a raw spill path', async () => {
  424. const { ctx, bash } = await setup({ config: { rawOutputMaxBytes: 16 } })
  425. bash.handler = () => runResult('', { stdout: { text: 'x', truncated: true, spillPath: '/does/not/get-read' } })
  426. const result = await call(ctx, 'glob', { pattern: '*' })
  427. expect(result.error).toMatchObject({ info: { code: 'SEARCH_RAW_OUTPUT_OVERFLOW' } })
  428. expect(text(result)).toContain('narrow pattern, path, or include')
  429. })
  430. it('fails with SEARCH_RAW_OUTPUT_OVERFLOW when UNTRUNCATED inline stdout exceeds the cap', async () => {
  431. // An executor retaining more inline than this package's cap (or a
  432. // deployment lowering rawOutputMaxBytes below the bash retention) must not
  433. // smuggle an over-cap parse through the untruncated path.
  434. const { ctx, bash } = await setup({ config: { rawOutputMaxBytes: 16 } })
  435. bash.handler = () => runResult(`${'x'.repeat(64)}\n`)
  436. const result = await call(ctx, 'grep', { pattern: 'x' })
  437. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_RAW_OUTPUT_OVERFLOW' } })
  438. expect(text(result)).toContain('narrow pattern, path, or include')
  439. })
  440. it('fails with SEARCH_RAW_OUTPUT_OVERFLOW when truncated stdout has no spill path', async () => {
  441. const { ctx, bash } = await setup()
  442. bash.handler = () => runResult('', { stdout: { text: 'partial', truncated: true } })
  443. const result = await call(ctx, 'grep', { pattern: 'x' })
  444. expect(result.error).toMatchObject({ info: { code: 'SEARCH_RAW_OUTPUT_OVERFLOW' } })
  445. })
  446. })
  447. describe('glob results', () => {
  448. it('lists workdir-relative paths (absolute output under the workdir is relativized)', async () => {
  449. const { ctx, bash } = await setup()
  450. bash.handler = () => runResult('/sessions/s1/src/a.ts\n/elsewhere/b.ts\nrel/c.ts\n')
  451. const result = await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/sessions/s1') })
  452. if (result.isError) throw new Error('expected glob success')
  453. expect(result.value).toEqual({ paths: [join('src', 'a.ts'), '/elsewhere/b.ts', 'rel/c.ts'] })
  454. expect(text(result)).toBe(`${join('src', 'a.ts')}\n/elsewhere/b.ts\nrel/c.ts`)
  455. })
  456. it('validates arguments (blank pattern, blank path)', async () => {
  457. const { ctx } = await setup()
  458. expect(text(await call(ctx, 'glob', { pattern: ' ' }))).toContain('pattern must be a non-empty string')
  459. expect(text(await call(ctx, 'glob', { pattern: '*', path: ' ' }))).toContain('path must be a non-empty string')
  460. })
  461. it('threads a valid path through to the command as the quoted search root', async () => {
  462. const { ctx, bash } = await setup()
  463. bash.handler = () => runResult('sub/a.ts\n')
  464. const result = await call(ctx, 'glob', { pattern: '*.ts', path: 'sub' })
  465. expect(result.isError).toBe(false)
  466. expect(bash.specs[0]?.command).toContain("-- 'sub'")
  467. })
  468. it('caps at globMaxResults and saves the FULL sorted list through spillStore', async () => {
  469. const { ctx, bash, spill } = await setup({ config: { globMaxResults: 2 }, spill: true })
  470. ctx.on('tools/post-execute', async () => ({
  471. kind: 'accept',
  472. additionalContexts: [{ content: [{ type: 'text', text: 'glob context' }], source: { kind: 'plugin', plugin: 'test' } }],
  473. }))
  474. bash.handler = () => runResult('a.ts\nb.ts\nc.ts\nd.ts\n')
  475. const result = await call(ctx, 'glob', { pattern: '*.ts' }, { agent: agent('/w') })
  476. expect(result.isError).toBe(false)
  477. if (result.isError) throw new Error('expected glob success')
  478. expect(result.value).toEqual({ paths: ['a.ts', 'b.ts', 'c.ts', 'd.ts'] })
  479. 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.)')
  480. expect(spill?.saves).toHaveLength(1)
  481. expect(spill?.saves[0]).toMatchObject({
  482. owner: { sessionId: 'session-1' },
  483. source: { toolName: 'glob', label: 'result' },
  484. suggestedName: 'glob-results.txt',
  485. content: 'a.ts\nb.ts\nc.ts\nd.ts',
  486. })
  487. expect(spill?.saves[0]?.source.callId).toBeDefined()
  488. expect(result.additionalContexts?.[0]?.content).toEqual([{ type: 'text', text: 'glob context' }])
  489. })
  490. it('does not create a spill file when the result fits inline', async () => {
  491. const { ctx, bash, spill } = await setup({ spill: true })
  492. bash.handler = () => runResult('a.ts\nb.ts\n')
  493. const result = await call(ctx, 'glob', { pattern: '*' }, { agent: agent('/w') })
  494. expect(text(result)).toBe('a.ts\nb.ts')
  495. expect(spill?.saves).toHaveLength(0)
  496. })
  497. it('preserves a downstream canonical value replacement instead of spilling the old value', async () => {
  498. const { ctx, bash, spill } = await setup({ config: { globMaxResults: 1 }, spill: true })
  499. ctx.on('tools/post-execute', async () => ({
  500. kind: 'accept' as const,
  501. value: { paths: ['replacement-a.ts', 'replacement-b.ts'] },
  502. }))
  503. bash.handler = () => runResult('old-a.ts\nold-b.ts\n')
  504. const result = await call(ctx, 'glob', { pattern: '*.ts' }, { agent: agent('/w') })
  505. if (result.isError) throw new Error('expected glob replacement success')
  506. expect(result.value).toEqual({ paths: ['replacement-a.ts', 'replacement-b.ts'] })
  507. expect(text(result)).toContain('replacement-a.ts')
  508. expect(text(result)).not.toContain('old-a.ts')
  509. expect(spill?.saves).toHaveLength(0)
  510. })
  511. it('keeps the full nested Code value without creating a surface spill', async () => {
  512. const { ctx, bash, spill } = await setup({ config: { globMaxResults: 2 }, spill: true })
  513. bash.handler = () => runResult('a.ts\nb.ts\nc.ts\nd.ts\n')
  514. const result = await call(ctx, 'glob', { pattern: '*.ts' }, {
  515. agent: agent('/w'),
  516. parent: Symbol('run_code') as ToolExecutionToken,
  517. })
  518. if (result.isError) throw new Error('expected glob success')
  519. expect(result.value).toEqual({ paths: ['a.ts', 'b.ts', 'c.ts', 'd.ts'] })
  520. 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.)')
  521. expect(spill?.saves).toHaveLength(0)
  522. })
  523. it.each([
  524. ['no spill backend loaded', { fail: false, spill: false, ownerless: false }],
  525. ['saveText fails', { fail: true, spill: true, ownerless: false }],
  526. ['no session owner', { fail: false, spill: true, ownerless: true }],
  527. ])('keeps the inline page and reports the unsaved remainder when %s', async (_label, mode) => {
  528. const { ctx, bash, spill } = await setup({ config: { globMaxResults: 1 }, spill: mode.spill })
  529. if (mode.fail && spill) spill.failWith = new Error('disk full')
  530. bash.handler = () => runResult('a.ts\nb.ts\n')
  531. const result = await call(ctx, 'glob', { pattern: '*' }, mode.ownerless ? {} : { agent: agent('/w') })
  532. expect(result.isError).toBe(false) // spill unavailability never fails the search
  533. 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.)')
  534. })
  535. })
  536. describe('grep results', () => {
  537. it('groups matches by file with line numbers', async () => {
  538. const { ctx, bash } = await setup()
  539. bash.handler = () => runResult([
  540. JSON.stringify({ type: 'begin', data: { path: { text: 'a.ts' } } }),
  541. matchLine('a.ts', 3, 'const x = 1\n'),
  542. matchLine('a.ts', 9, 'const y = 2\n'),
  543. JSON.stringify({ type: 'end', data: { path: { text: 'a.ts' } } }),
  544. matchLine('b.ts', 1, 'const z = 3'),
  545. JSON.stringify({ type: 'summary', data: {} }),
  546. '',
  547. ].join('\n'))
  548. const result = await call(ctx, 'grep', { pattern: 'const' })
  549. expect(result.isError).toBe(false)
  550. if (result.isError) throw new Error('expected grep success')
  551. expect(result.value).toEqual({
  552. matches: [
  553. { path: 'a.ts', lineNumber: 3, line: 'const x = 1' },
  554. { path: 'a.ts', lineNumber: 9, line: 'const y = 2' },
  555. { path: 'b.ts', lineNumber: 1, line: 'const z = 3' },
  556. ],
  557. })
  558. 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')
  559. })
  560. it('reports a single match in the singular', async () => {
  561. const { ctx, bash } = await setup()
  562. bash.handler = () => runResult(`${matchLine('a.ts', 1, 'hit')}\n`)
  563. expect(text(await call(ctx, 'grep', { pattern: 'hit' }))).toBe('Found 1 match\n\na.ts\nLine 1: hit')
  564. })
  565. it('relativizes absolute match paths against the resolved workdir', async () => {
  566. const { ctx, bash } = await setup()
  567. bash.handler = () => runResult(`${matchLine('/sessions/s1/deep/a.ts', 2, 'hit')}\n`)
  568. const result = await call(ctx, 'grep', { pattern: 'hit', path: '/sessions/s1' }, { agent: agent('/sessions/s1') })
  569. expect(text(result)).toContain(`${join('deep', 'a.ts')}\nLine 2: hit`)
  570. })
  571. it('previews a long matched line at grepMaxLineBytes preserving UTF-8', async () => {
  572. const { ctx, bash } = await setup({ config: { grepMaxLineBytes: 7 } })
  573. // '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.
  574. // Use a multibyte straddle instead: 'aé' repeated — cut at 7 bytes: a(1)é(2)a(1)é(2)=6 +a(1)=7 → next é straddles: trimmed.
  575. bash.handler = () => runResult(`${matchLine('a.txt', 1, 'aéaéaéaé')}\n`)
  576. const result = await call(ctx, 'grep', { pattern: 'a' })
  577. if (result.isError) throw new Error('expected grep success')
  578. expect(result.value).toEqual({ matches: [{ path: 'a.txt', lineNumber: 1, line: 'aéaéaéaé' }] })
  579. expect(text(result)).toContain('Line 1: aéaéa (line truncated)')
  580. })
  581. it('renders a non-UTF-8 line (rg bytes form) as a placeholder instead of failing', async () => {
  582. const { ctx, bash } = await setup()
  583. const record = JSON.stringify({ type: 'match', data: { path: { text: 'bin.dat' }, lines: { bytes: 'AAECww==' }, line_number: 4 } })
  584. bash.handler = () => runResult(`${record}\n`)
  585. expect(text(await call(ctx, 'grep', { pattern: 'x' }))).toContain('Line 4: (line is not valid UTF-8)')
  586. })
  587. it('strips a CRLF terminator from the matched line text', () => {
  588. const matches = parseGrepMatches(`${matchLine('a.txt', 1, 'windows line\r\n')}\n`)
  589. expect(matches[0]?.line).toBe('windows line')
  590. })
  591. it('caps at grepMaxMatches and spills the full formatted match list', async () => {
  592. const { ctx, bash, spill } = await setup({ config: { grepMaxMatches: 2 }, spill: true })
  593. ctx.on('tools/post-execute', async () => ({
  594. kind: 'accept',
  595. additionalContexts: [{ content: [{ type: 'text', text: 'grep context' }], source: { kind: 'plugin', plugin: 'test' } }],
  596. }))
  597. bash.handler = () => runResult([
  598. matchLine('a.ts', 1, 'one'),
  599. matchLine('a.ts', 2, 'two'),
  600. matchLine('b.ts', 3, 'three'),
  601. '',
  602. ].join('\n'))
  603. const result = await call(ctx, 'grep', { pattern: 'e' }, { agent: agent('/w') })
  604. if (result.isError) throw new Error('expected grep success')
  605. expect(result.value).toEqual({
  606. matches: [
  607. { path: 'a.ts', lineNumber: 1, line: 'one' },
  608. { path: 'a.ts', lineNumber: 2, line: 'two' },
  609. { path: 'b.ts', lineNumber: 3, line: 'three' },
  610. ],
  611. })
  612. 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.)')
  613. expect(spill?.saves[0]).toMatchObject({
  614. source: { toolName: 'grep', label: 'result' },
  615. suggestedName: 'grep-results.txt',
  616. content: 'Found 3 matches\n\na.ts\nLine 1: one\nLine 2: two\n\nb.ts\nLine 3: three',
  617. })
  618. expect(result.additionalContexts?.[0]?.content).toEqual([{ type: 'text', text: 'grep context' }])
  619. })
  620. it('preserves a downstream canonical value replacement instead of spilling the old matches', async () => {
  621. const { ctx, bash, spill } = await setup({ config: { grepMaxMatches: 1 }, spill: true })
  622. ctx.on('tools/post-execute', async () => ({
  623. kind: 'accept' as const,
  624. value: {
  625. matches: [
  626. { path: 'replacement.ts', lineNumber: 7, line: 'first' },
  627. { path: 'replacement.ts', lineNumber: 8, line: 'second' },
  628. ],
  629. },
  630. }))
  631. bash.handler = () => runResult(`${matchLine('old.ts', 1, 'old')}\n`)
  632. const result = await call(ctx, 'grep', { pattern: 'old' }, { agent: agent('/w') })
  633. if (result.isError) throw new Error('expected grep replacement success')
  634. expect(result.value).toEqual({
  635. matches: [
  636. { path: 'replacement.ts', lineNumber: 7, line: 'first' },
  637. { path: 'replacement.ts', lineNumber: 8, line: 'second' },
  638. ],
  639. })
  640. expect(text(result)).toContain('replacement.ts')
  641. expect(text(result)).not.toContain('old.ts')
  642. expect(spill?.saves).toHaveLength(0)
  643. })
  644. it('keeps every nested Code match in the value without creating a surface spill', async () => {
  645. const { ctx, bash, spill } = await setup({ config: { grepMaxMatches: 1 }, spill: true })
  646. bash.handler = () => runResult(`${matchLine('a.ts', 1, 'one')}\n${matchLine('b.ts', 2, 'two')}\n`)
  647. const result = await call(ctx, 'grep', { pattern: 'o' }, {
  648. agent: agent('/w'),
  649. parent: Symbol('run_code') as ToolExecutionToken,
  650. })
  651. if (result.isError) throw new Error('expected grep success')
  652. expect(result.value).toEqual({
  653. matches: [
  654. { path: 'a.ts', lineNumber: 1, line: 'one' },
  655. { path: 'b.ts', lineNumber: 2, line: 'two' },
  656. ],
  657. })
  658. 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.)')
  659. expect(spill?.saves).toHaveLength(0)
  660. })
  661. it('reports the unsaved remainder when capped with no spill backend', async () => {
  662. const { ctx, bash } = await setup({ config: { grepMaxMatches: 1 } })
  663. bash.handler = () => runResult(`${matchLine('a.ts', 1, 'one')}\n${matchLine('a.ts', 2, 'two')}\n`)
  664. const result = await call(ctx, 'grep', { pattern: 'o' }, { agent: agent('/w') })
  665. expect(result.isError).toBe(false)
  666. 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.)')
  667. })
  668. it('validates arguments (empty pattern, blank path, bad include)', async () => {
  669. const { ctx } = await setup()
  670. expect(text(await call(ctx, 'grep', { pattern: '' }))).toContain('pattern must be a non-empty string')
  671. expect(text(await call(ctx, 'grep', { pattern: 'x', path: ' ' }))).toContain('path must be a non-empty string')
  672. expect(text(await call(ctx, 'grep', { pattern: 'x', include: ' ' }))).toContain('include must be a non-empty glob')
  673. expect(text(await call(ctx, 'grep', { pattern: 'x', include: '!*.ts' }))).toContain('negated patterns')
  674. expect(text(await call(ctx, 'grep', { pattern: 'x', include: '*.ts,*.js' }))).toContain('comma-separated list')
  675. })
  676. it('accepts a whitespace-only pattern (a legitimate regex) and brace alternation in include', async () => {
  677. const { ctx, bash } = await setup()
  678. bash.handler = () => runResult('', { exitCode: 1 })
  679. const result = await call(ctx, 'grep', { pattern: ' ', include: '*.{ts,tsx}' })
  680. expect(result.isError).toBe(false)
  681. })
  682. })
  683. describe('rg --json transport failures (SEARCH_FAILED)', () => {
  684. it.each([
  685. ['a non-JSON line', 'not json at all'],
  686. ['a non-object record', '42'],
  687. ['a match record with no data', JSON.stringify({ type: 'match' })],
  688. ['a match record with no path text', JSON.stringify({ type: 'match', data: { path: {}, lines: { text: 'x' }, line_number: 1 } })],
  689. ['a match record with a non-object path', JSON.stringify({ type: 'match', data: { path: 'a.ts', lines: { text: 'x' }, line_number: 1 } })],
  690. ['a match record with no line number', JSON.stringify({ type: 'match', data: { path: { text: 'a.ts' }, lines: { text: 'x' } } })],
  691. ['a match record with no line content', JSON.stringify({ type: 'match', data: { path: { text: 'a.ts' }, line_number: 1 } })],
  692. ['a match record with neither text nor bytes', JSON.stringify({ type: 'match', data: { path: { text: 'a.ts' }, lines: {}, line_number: 1 } })],
  693. ])('%s fails the search', async (_label, line) => {
  694. const { ctx, bash } = await setup()
  695. bash.handler = () => runResult(`${line}\n`)
  696. const result = await call(ctx, 'grep', { pattern: 'x' })
  697. expect(result.isError).toBe(true)
  698. expect(result.error).toMatchObject({ info: { name: 'SearchError', code: 'SEARCH_FAILED' } })
  699. })
  700. })
  701. describe('the no-background-task invariant', () => {
  702. it('never calls ctx.bash.start() across successful and failed searches', async () => {
  703. const { ctx, bash } = await setup()
  704. bash.handler = () => runResult('a.ts\n')
  705. await call(ctx, 'glob', { pattern: '*' })
  706. bash.handler = () => runResult('', { exitCode: 2, stderr: { text: 'boom', truncated: false } })
  707. await call(ctx, 'grep', { pattern: 'x' })
  708. expect(bash.startCalls).toBe(0)
  709. })
  710. })
  711. describe('presentation', () => {
  712. it('glob titles carry the pattern and optional root', () => {
  713. expect(presentGlobCall({ pattern: '**/*.ts' })).toMatchObject({ card: 'generic', title: 'Glob **/*.ts', kind: 'search' })
  714. expect(presentGlobCall({ pattern: '*.md', path: 'docs' }).title).toBe('Glob *.md in docs')
  715. })
  716. it('grep titles carry the pattern, target, and include filter', () => {
  717. expect(presentGrepCall({ pattern: 'todo' })).toMatchObject({ card: 'generic', title: 'Grep todo', kind: 'search' })
  718. expect(presentGrepCall({ pattern: 'todo', path: 'src', include: '*.ts' }).title).toBe('Grep todo in src (*.ts)')
  719. })
  720. })
  721. describe('helpers', () => {
  722. it('toWorkdirRelative maps inside-workdir absolutes and passes everything else through', () => {
  723. expect(toWorkdirRelative('/w/a/b.ts', '/w')).toBe(join('a', 'b.ts'))
  724. expect(toWorkdirRelative('/w', '/w')).toBe('.')
  725. expect(toWorkdirRelative('/other/b.ts', '/w')).toBe('/other/b.ts')
  726. expect(toWorkdirRelative('/w-sibling/b.ts', '/w')).toBe('/w-sibling/b.ts')
  727. expect(toWorkdirRelative('rel/b.ts', '/w')).toBe('rel/b.ts')
  728. // Normalization makes this land OUTSIDE the workdir → original path kept.
  729. expect(toWorkdirRelative('/w/../up.ts', '/w')).toBe('/w/../up.ts')
  730. })
  731. it('previewLine keeps a within-budget line untouched', () => {
  732. expect(previewLine('short', 100)).toBe('short')
  733. })
  734. it('formatGrepMatches groups by first-seen file order', () => {
  735. const grouped = formatGrepMatches([
  736. { path: 'b.ts', lineNumber: 2, line: 'x' },
  737. { path: 'a.ts', lineNumber: 1, line: 'y' },
  738. { path: 'b.ts', lineNumber: 5, line: 'z' },
  739. ])
  740. expect(grouped).toBe('b.ts\nLine 2: x\nLine 5: z\n\na.ts\nLine 1: y')
  741. })
  742. })