tool-grep.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { describe, expect } from "bun:test"
  4. import { Effect, Exit, Layer } from "effect"
  5. import { FSUtil } from "@opencode-ai/core/fs-util"
  6. import { Location } from "@opencode-ai/core/location"
  7. import { FileSystem } from "@opencode-ai/core/filesystem"
  8. import { Search } from "@opencode-ai/core/filesystem/search"
  9. import { Ripgrep as FileSystemRipgrep } from "@opencode-ai/core/filesystem/ripgrep"
  10. import { LocationSearch } from "@opencode-ai/core/location-search"
  11. import { PermissionV2 } from "@opencode-ai/core/permission"
  12. import { AppProcess } from "@opencode-ai/core/process"
  13. import { Ripgrep } from "@opencode-ai/core/ripgrep"
  14. import { AbsolutePath, RelativePath } from "@opencode-ai/core/schema"
  15. import { SessionV2 } from "@opencode-ai/core/session"
  16. import { GrepTool } from "@opencode-ai/core/tool/grep"
  17. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  18. import { location } from "./fixture/location"
  19. import { tmpdir } from "./fixture/tmpdir"
  20. import { it as runtimeIt } from "./lib/effect"
  21. import { testEffect } from "./lib/effect"
  22. import { toolIdentity, executeTool, settleTool, toolDefinitions } from "./lib/tool"
  23. const assertions: PermissionV2.AssertInput[] = []
  24. const searches: LocationSearch.GrepInput[] = []
  25. let allow = true
  26. let result = new LocationSearch.GrepResult({ items: [], truncated: false, partial: false })
  27. let searchFailure: Ripgrep.InvalidPatternError | undefined
  28. const search = Layer.succeed(
  29. LocationSearch.Service,
  30. LocationSearch.Service.of({
  31. files: () => Effect.die("unused"),
  32. grep: (input) =>
  33. Effect.sync(() => {
  34. searches.push(input)
  35. if (searchFailure) throw searchFailure
  36. return result
  37. }),
  38. }),
  39. )
  40. const permission = Layer.succeed(
  41. PermissionV2.Service,
  42. PermissionV2.Service.of({
  43. assert: (input) =>
  44. Effect.sync(() => {
  45. assertions.push(input)
  46. }).pipe(Effect.andThen(allow ? Effect.void : Effect.fail(new PermissionV2.DeniedError({ rules: [] })))),
  47. ask: () => Effect.die("unused"),
  48. reply: () => Effect.die("unused"),
  49. get: () => Effect.die("unused"),
  50. forSession: () => Effect.die("unused"),
  51. list: () => Effect.die("unused"),
  52. }),
  53. )
  54. const registry = ToolRegistry.defaultLayer.pipe(Layer.provide(permission))
  55. const grep = GrepTool.layer.pipe(Layer.provide(registry), Layer.provide(search), Layer.provide(permission))
  56. const it = testEffect(Layer.mergeAll(registry, search, permission, grep))
  57. const sessionID = SessionV2.ID.make("ses_grep_tool_test")
  58. const execute = (input: Record<string, unknown>) =>
  59. ToolRegistry.Service.use((registry) =>
  60. executeTool(registry, {
  61. sessionID,
  62. ...toolIdentity,
  63. call: { type: "tool-call", id: "call-grep", name: "grep", input },
  64. }),
  65. )
  66. const settle = (input: Record<string, unknown>) =>
  67. ToolRegistry.Service.use((registry) =>
  68. settleTool(registry, {
  69. sessionID,
  70. ...toolIdentity,
  71. call: { type: "tool-call", id: "call-grep", name: "grep", input },
  72. }),
  73. )
  74. const reset = () => {
  75. assertions.length = 0
  76. searches.length = 0
  77. allow = true
  78. searchFailure = undefined
  79. result = new LocationSearch.GrepResult({ items: [], truncated: false, partial: false })
  80. }
  81. function provideLive(directory: string) {
  82. const dependencies = Layer.mergeAll(
  83. FSUtil.defaultLayer,
  84. FileSystemRipgrep.defaultLayer,
  85. Search.defaultLayer,
  86. AppProcess.defaultLayer,
  87. Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))),
  88. )
  89. const filesystem = FileSystem.layer.pipe(Layer.provide(dependencies))
  90. const search = LocationSearch.layer.pipe(
  91. Layer.provide(filesystem),
  92. Layer.provide(Ripgrep.layer.pipe(Layer.provide(dependencies))),
  93. Layer.provide(FSUtil.defaultLayer),
  94. Layer.provide(dependencies),
  95. )
  96. const registry = ToolRegistry.defaultLayer.pipe(Layer.provide(permission))
  97. const grep = GrepTool.layer.pipe(
  98. Layer.provide(registry),
  99. Layer.provide(filesystem),
  100. Layer.provide(search),
  101. Layer.provide(permission),
  102. )
  103. return Layer.mergeAll(registry, filesystem, search, permission, grep)
  104. }
  105. describe("GrepTool", () => {
  106. it.effect("registers grep", () =>
  107. Effect.gen(function* () {
  108. reset()
  109. expect(yield* toolDefinitions(yield* ToolRegistry.Service)).toMatchObject([{ name: "grep" }])
  110. }),
  111. )
  112. it.effect("authorizes the regex resource and delegates an active Location grep", () =>
  113. Effect.gen(function* () {
  114. reset()
  115. const input = { pattern: "needle", path: "src", include: "*.ts", limit: 2 }
  116. expect(yield* execute(input)).toEqual({ type: "text", value: "No files found" })
  117. expect(assertions).toMatchObject([
  118. {
  119. sessionID,
  120. action: "grep",
  121. resources: ["needle"],
  122. save: ["*"],
  123. metadata: { root: "src", path: RelativePath.make("src"), include: "*.ts", limit: 2 },
  124. },
  125. ])
  126. expect(searches).toEqual([{ pattern: "needle", path: RelativePath.make("src"), include: "*.ts", limit: 2 }])
  127. }),
  128. )
  129. it.effect("does not search when permission is denied", () =>
  130. Effect.gen(function* () {
  131. reset()
  132. allow = false
  133. expect(yield* execute({ pattern: "secret" })).toEqual({ type: "error", value: "Unable to grep for secret" })
  134. expect(assertions).toHaveLength(1)
  135. expect(searches).toEqual([])
  136. }),
  137. )
  138. it.effect("keeps structured results raw while formatting bounded partial previews for models", () =>
  139. Effect.gen(function* () {
  140. reset()
  141. result = new LocationSearch.GrepResult({
  142. items: [
  143. new LocationSearch.Match({
  144. path: RelativePath.make("src/index.ts"),
  145. canonical: "/project/src/index.ts",
  146. resource: "src/index.ts",
  147. lines: "needle preview",
  148. linePreviewTruncated: true,
  149. line: 3,
  150. offset: 8,
  151. submatches: [new LocationSearch.Submatch({ text: "needle", start: 0, end: 6 })],
  152. mtime: 1,
  153. }),
  154. ],
  155. truncated: true,
  156. partial: true,
  157. })
  158. const settlement = yield* settle({ pattern: "needle" })
  159. expect(settlement.output?.structured).toEqual(result)
  160. expect(settlement.result).toEqual({
  161. type: "text",
  162. value:
  163. "Found 1 matches\nsrc/index.ts:\n Line 3: needle preview...\n\n(Results are truncated: showing first 1 matches. Consider using a more specific path or pattern.)\n\n(Some paths were inaccessible and skipped)",
  164. })
  165. }),
  166. )
  167. it.effect("preserves an unexpected search defect", () =>
  168. Effect.gen(function* () {
  169. reset()
  170. searchFailure = new Ripgrep.InvalidPatternError({
  171. pattern: "[",
  172. message: "regex parse error: unclosed character class",
  173. })
  174. expect(Exit.isFailure(yield* execute({ pattern: "[" }).pipe(Effect.exit))).toBe(true)
  175. expect(searches).toEqual([{ pattern: "[" }])
  176. }),
  177. )
  178. runtimeIt.live("greps active Location files with include globs", () =>
  179. Effect.acquireRelease(
  180. Effect.promise(() => tmpdir()),
  181. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  182. ).pipe(
  183. Effect.flatMap((tmp) => {
  184. const docs = path.join(tmp.path, "docs")
  185. return Effect.gen(function* () {
  186. reset()
  187. yield* Effect.promise(async () => {
  188. await fs.mkdir(path.join(tmp.path, "src"))
  189. await fs.writeFile(path.join(tmp.path, "src", "index.ts"), "needle ts\n")
  190. await fs.writeFile(path.join(tmp.path, "src", "notes.txt"), "needle txt\n")
  191. })
  192. expect(yield* execute({ pattern: "needle", path: "src", include: "*.ts" })).toEqual({
  193. type: "text",
  194. value: "Found 1 matches\nsrc/index.ts:\n Line 1: needle ts\n",
  195. })
  196. }).pipe(Effect.provide(provideLive(tmp.path)))
  197. }),
  198. ),
  199. )
  200. })