tool-bash.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. import fs from "fs/promises"
  2. import { realpathSync } from "node:fs"
  3. import path from "path"
  4. import { describe, expect, test } from "bun:test"
  5. import { Effect, Layer } from "effect"
  6. import { ChildProcess } from "effect/unstable/process"
  7. import { FSUtil } from "@opencode-ai/core/fs-util"
  8. import { Config } from "@opencode-ai/core/config"
  9. import { Location } from "@opencode-ai/core/location"
  10. import { LocationMutation } from "@opencode-ai/core/location-mutation"
  11. import { PermissionV2 } from "@opencode-ai/core/permission"
  12. import { AppProcess } from "@opencode-ai/core/process"
  13. import { AbsolutePath } from "@opencode-ai/core/schema"
  14. import { SessionV2 } from "@opencode-ai/core/session"
  15. import { BashTool } from "@opencode-ai/core/tool/bash"
  16. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  17. import { location } from "./fixture/location"
  18. import { tmpdir } from "./fixture/tmpdir"
  19. import { testEffect } from "./lib/effect"
  20. import { toolIdentity, executeTool, settleTool, toolDefinitions } from "./lib/tool"
  21. const sessionID = SessionV2.ID.make("ses_bash_tool_test")
  22. const assertions: PermissionV2.AssertInput[] = []
  23. const runs: Array<{
  24. readonly command: string
  25. readonly cwd?: string
  26. readonly shell?: string | boolean
  27. readonly options?: AppProcess.RunOptions
  28. }> = []
  29. let denyAction: string | undefined
  30. let result: AppProcess.RunResult = {
  31. command: "mock",
  32. exitCode: 0,
  33. stdout: Buffer.from("hello\n"),
  34. stderr: Buffer.alloc(0),
  35. stdoutTruncated: false,
  36. stderrTruncated: false,
  37. }
  38. let runFailure: AppProcess.AppProcessError | undefined
  39. let afterPermission = (_input: PermissionV2.AssertInput): Effect.Effect<void> => Effect.void
  40. const permission = Layer.succeed(
  41. PermissionV2.Service,
  42. PermissionV2.Service.of({
  43. assert: (input) =>
  44. Effect.sync(() => assertions.push(input)).pipe(
  45. Effect.andThen(Effect.suspend(() => afterPermission(input))),
  46. Effect.andThen(
  47. input.action === denyAction ? Effect.fail(new PermissionV2.DeniedError({ rules: [] })) : Effect.void,
  48. ),
  49. ),
  50. ask: () => Effect.die("unused"),
  51. reply: () => Effect.die("unused"),
  52. get: () => Effect.die("unused"),
  53. forSession: () => Effect.die("unused"),
  54. list: () => Effect.die("unused"),
  55. }),
  56. )
  57. const appProcess = Layer.succeed(
  58. AppProcess.Service,
  59. AppProcess.Service.of({
  60. run: (command: ChildProcess.Command, options?: AppProcess.RunOptions) =>
  61. Effect.suspend(() => {
  62. if (command._tag !== "StandardCommand") throw new Error("expected standard command")
  63. runs.push({ command: command.command, cwd: command.options.cwd, shell: command.options.shell, options })
  64. return runFailure ? Effect.fail(runFailure) : Effect.succeed(result)
  65. }),
  66. } as unknown as AppProcess.Interface),
  67. )
  68. const config = Layer.succeed(
  69. Config.Service,
  70. Config.Service.of({
  71. entries: () => Effect.succeed([]),
  72. }),
  73. )
  74. const reset = () => {
  75. assertions.length = 0
  76. runs.length = 0
  77. denyAction = undefined
  78. runFailure = undefined
  79. afterPermission = () => Effect.void
  80. result = {
  81. command: "mock",
  82. exitCode: 0,
  83. stdout: Buffer.from("hello\n"),
  84. stderr: Buffer.alloc(0),
  85. stdoutTruncated: false,
  86. stderrTruncated: false,
  87. }
  88. }
  89. const withTool = <A, E, R>(
  90. directory: string,
  91. body: (registry: ToolRegistry.Interface) => Effect.Effect<A, E, R>,
  92. processLayer: Layer.Layer<AppProcess.Service> = appProcess,
  93. ) => {
  94. const filesystem = FSUtil.defaultLayer
  95. const activeLocation = Layer.succeed(
  96. Location.Service,
  97. Location.Service.of(location({ directory: AbsolutePath.make(directory) })),
  98. )
  99. const mutation = LocationMutation.layer.pipe(Layer.provide(filesystem), Layer.provide(activeLocation))
  100. const registry = ToolRegistry.defaultLayer.pipe(Layer.provide(permission))
  101. const bash = BashTool.layer.pipe(
  102. Layer.provide(registry),
  103. Layer.provide(permission),
  104. Layer.provide(mutation),
  105. Layer.provide(filesystem),
  106. Layer.provide(processLayer),
  107. Layer.provide(config),
  108. )
  109. return Effect.gen(function* () {
  110. return yield* body(yield* ToolRegistry.Service)
  111. }).pipe(Effect.provide(Layer.mergeAll(registry, bash)))
  112. }
  113. const call = (input: typeof BashTool.Input.Type, id = "call-bash") => ({
  114. sessionID,
  115. ...toolIdentity,
  116. call: { type: "tool-call" as const, id, name: "bash", input },
  117. })
  118. const it = testEffect(Layer.empty)
  119. describe("BashTool", () => {
  120. it.live("registers and returns structured successful output from the active Location", () =>
  121. Effect.acquireUseRelease(
  122. Effect.promise(() => tmpdir()),
  123. (tmp) => {
  124. reset()
  125. return withTool(tmp.path, (registry) =>
  126. Effect.gen(function* () {
  127. const definitions = yield* toolDefinitions(registry)
  128. expect(definitions.map((tool) => tool.name)).toEqual(["bash"])
  129. expect(definitions[0]?.inputSchema).not.toHaveProperty("properties.background")
  130. expect(definitions[0]?.inputSchema).not.toHaveProperty("properties.description")
  131. expect(yield* toolDefinitions(registry, [{ action: "bash", resource: "*", effect: "deny" }])).toEqual([])
  132. expect(yield* settleTool(registry, call({ command: "pwd" }))).toEqual({
  133. result: { type: "text", value: "hello\n\n\nCommand exited with code 0." },
  134. output: {
  135. structured: {
  136. command: "pwd",
  137. cwd: realpathSync(tmp.path),
  138. exitCode: 0,
  139. output: "hello\n",
  140. truncated: false,
  141. },
  142. content: [{ type: "text", text: "hello\n\n\nCommand exited with code 0." }],
  143. },
  144. })
  145. expect(runs).toMatchObject([{ command: "pwd", cwd: realpathSync(tmp.path) }])
  146. expect(runs[0]?.options).toMatchObject({
  147. maxOutputBytes: BashTool.MAX_CAPTURE_BYTES,
  148. maxErrorBytes: BashTool.MAX_CAPTURE_BYTES,
  149. })
  150. expect(assertions).toMatchObject([{ sessionID, action: "bash", resources: ["pwd"], save: ["pwd"] }])
  151. }),
  152. )
  153. },
  154. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  155. ),
  156. )
  157. it.live("resolves a relative workdir from the active Location", () =>
  158. Effect.acquireUseRelease(
  159. Effect.promise(() => tmpdir()),
  160. (tmp) => {
  161. reset()
  162. return Effect.promise(() => fs.mkdir(path.join(tmp.path, "src"))).pipe(
  163. Effect.andThen(
  164. withTool(tmp.path, (registry) => executeTool(registry, call({ command: "pwd", workdir: "src" }))),
  165. ),
  166. Effect.andThen(
  167. Effect.sync(() => expect(runs).toMatchObject([{ cwd: realpathSync(path.join(tmp.path, "src")) }])),
  168. ),
  169. )
  170. },
  171. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  172. ),
  173. )
  174. it.live("rejects a workdir that stops being a directory during approval", () =>
  175. Effect.acquireUseRelease(
  176. Effect.promise(() => tmpdir()),
  177. (tmp) => {
  178. reset()
  179. const workdir = path.join(tmp.path, "src")
  180. afterPermission = (input) =>
  181. input.action === "bash"
  182. ? Effect.promise(async () => {
  183. await fs.rm(workdir, { recursive: true })
  184. await fs.writeFile(workdir, "not a directory")
  185. }).pipe(Effect.orDie)
  186. : Effect.void
  187. return Effect.promise(() => fs.mkdir(workdir)).pipe(
  188. Effect.andThen(
  189. withTool(tmp.path, (registry) => executeTool(registry, call({ command: "pwd", workdir: "src" }))),
  190. ),
  191. Effect.andThen(
  192. Effect.sync(() => {
  193. expect(runs).toEqual([])
  194. expect(assertions.map((input) => input.action)).toEqual(["bash"])
  195. }),
  196. ),
  197. )
  198. },
  199. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  200. ),
  201. )
  202. if (process.platform !== "win32") {
  203. it.live("executes a real shell command through AppProcess", () =>
  204. Effect.acquireUseRelease(
  205. Effect.promise(() => tmpdir()),
  206. (tmp) => {
  207. reset()
  208. return withTool(
  209. tmp.path,
  210. (registry) => settleTool(registry, call({ command: "printf core-bash" })),
  211. AppProcess.defaultLayer,
  212. ).pipe(
  213. Effect.andThen((settled) =>
  214. Effect.sync(() => {
  215. expect(settled.result).toEqual({ type: "text", value: "core-bash\n\nCommand exited with code 0." })
  216. expect(settled.output?.structured).toMatchObject({
  217. command: "printf core-bash",
  218. cwd: realpathSync(tmp.path),
  219. exitCode: 0,
  220. output: "core-bash",
  221. })
  222. }),
  223. ),
  224. )
  225. },
  226. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  227. ),
  228. )
  229. }
  230. it.live("approves an explicit external workdir before bash execution", () =>
  231. Effect.acquireUseRelease(
  232. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  233. ([active, outside]) => {
  234. reset()
  235. return withTool(active.path, (registry) =>
  236. executeTool(registry, call({ command: "pwd", workdir: outside.path })),
  237. ).pipe(
  238. Effect.andThen(
  239. Effect.sync(() => {
  240. expect(assertions.map((item) => item.action)).toEqual(["external_directory", "bash"])
  241. expect(assertions[0]).toMatchObject({
  242. resources: [path.join(realpathSync(outside.path), "*").replaceAll("\\", "/")],
  243. })
  244. expect(runs).toHaveLength(1)
  245. }),
  246. ),
  247. )
  248. },
  249. ([active, outside]) =>
  250. Effect.promise(() =>
  251. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  252. ),
  253. ),
  254. )
  255. it.live("does not execute after external-directory or bash denial", () =>
  256. Effect.acquireUseRelease(
  257. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  258. ([active, outside]) =>
  259. Effect.gen(function* () {
  260. reset()
  261. denyAction = "external_directory"
  262. yield* withTool(active.path, (registry) =>
  263. executeTool(registry, call({ command: "pwd", workdir: outside.path })),
  264. )
  265. expect(assertions.map((item) => item.action)).toEqual(["external_directory"])
  266. expect(runs).toEqual([])
  267. reset()
  268. denyAction = "bash"
  269. yield* withTool(active.path, (registry) => executeTool(registry, call({ command: "pwd" })))
  270. expect(assertions.map((item) => item.action)).toEqual(["bash"])
  271. expect(runs).toEqual([])
  272. }),
  273. ([active, outside]) =>
  274. Effect.promise(() =>
  275. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  276. ),
  277. ),
  278. )
  279. it.live("reports external command arguments as advisory warnings without enforcing approval", () =>
  280. Effect.acquireUseRelease(
  281. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  282. ([active, outside]) => {
  283. reset()
  284. denyAction = "external_directory"
  285. const target = path.join(outside.path, "secret.txt")
  286. return withTool(active.path, (registry) => settleTool(registry, call({ command: `cat ${target}` }))).pipe(
  287. Effect.andThen((settled) =>
  288. Effect.sync(() => {
  289. expect(assertions.map((item) => item.action)).toEqual(["bash"])
  290. expect(runs).toHaveLength(1)
  291. expect(settled.output?.structured).toMatchObject({
  292. warnings: [
  293. `Command argument references external directory ${path.join(realpathSync(outside.path), "*").replaceAll("\\", "/")}. Bash runs with host-user filesystem, process, and network authority; this scan is advisory only.`,
  294. ],
  295. })
  296. expect(settled.result).toMatchObject({ type: "text", value: expect.stringContaining("Warnings:") })
  297. }),
  298. ),
  299. )
  300. },
  301. ([active, outside]) =>
  302. Effect.promise(() =>
  303. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  304. ),
  305. ),
  306. )
  307. it.live("keeps non-zero exits useful", () =>
  308. Effect.acquireUseRelease(
  309. Effect.promise(() => tmpdir()),
  310. (tmp) => {
  311. reset()
  312. result = { ...result, exitCode: 7, stdout: Buffer.from("HEAD full output TAIL") }
  313. return withTool(tmp.path, (registry) => settleTool(registry, call({ command: "false" }, "call-overflow"))).pipe(
  314. Effect.andThen((settled) =>
  315. Effect.sync(() => {
  316. expect(settled.result).toMatchObject({
  317. type: "text",
  318. value: expect.stringContaining("Command exited with code 7"),
  319. })
  320. expect(settled.output?.structured).toMatchObject({
  321. command: "false",
  322. cwd: realpathSync(tmp.path),
  323. exitCode: 7,
  324. output: "HEAD full output TAIL",
  325. truncated: false,
  326. })
  327. }),
  328. ),
  329. )
  330. },
  331. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  332. ),
  333. )
  334. it.live("surfaces bounded process-capture truncation", () =>
  335. Effect.acquireUseRelease(
  336. Effect.promise(() => tmpdir()),
  337. (tmp) => {
  338. reset()
  339. result = { ...result, stdoutTruncated: true }
  340. return withTool(tmp.path, (registry) => settleTool(registry, call({ command: "verbose" }))).pipe(
  341. Effect.andThen((settled) =>
  342. Effect.sync(() => {
  343. expect(settled.output?.structured).toMatchObject({ truncated: true, stdoutTruncated: true })
  344. expect(settled.result).toMatchObject({
  345. type: "text",
  346. value: expect.stringContaining("stdout capture truncated"),
  347. })
  348. expect(settled.output?.structured).not.toHaveProperty("resource")
  349. }),
  350. ),
  351. )
  352. },
  353. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  354. ),
  355. )
  356. it.live("returns a useful timeout settlement", () =>
  357. Effect.acquireUseRelease(
  358. Effect.promise(() => tmpdir()),
  359. (tmp) => {
  360. reset()
  361. runFailure = new AppProcess.AppProcessError({ command: "sleep", cause: new Error("Timed out") })
  362. return withTool(tmp.path, (registry) => settleTool(registry, call({ command: "sleep 60", timeout: 10 }))).pipe(
  363. Effect.andThen((settled) =>
  364. Effect.sync(() => {
  365. expect(settled.result).toMatchObject({
  366. type: "text",
  367. value: expect.stringContaining("Command timed out"),
  368. })
  369. expect(settled.output?.structured).toMatchObject({
  370. command: "sleep 60",
  371. timedOut: true,
  372. truncated: false,
  373. })
  374. }),
  375. ),
  376. )
  377. },
  378. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  379. ),
  380. )
  381. })
  382. test("keeps locked deferred parity TODOs visible", async () => {
  383. const source = await fs.readFile(new URL("../src/tool/bash.ts", import.meta.url), "utf8")
  384. for (const todo of [
  385. "Port tree-sitter bash / PowerShell parser-based approval reduction.",
  386. "Port BashArity reusable command-prefix approvals.",
  387. "Replace token-based command-argument external-directory advisories with parser-based detection.",
  388. "Restore PowerShell and cmd-specific invocation/path handling on Windows.",
  389. "Add plugin shell.env environment augmentation once V2 plugin hooks exist.",
  390. "Add durable/live progress metadata streaming for long-running commands once V2 tool invocation progress context is wired.",
  391. "Persist background job status and define restart recovery before exposing remote observation.",
  392. "Revisit process-group cleanup and platform coverage with shell-specific tests if current AppProcess semantics do not fully cover it.",
  393. "Revisit binary output handling if stdout/stderr decoding is text-only.",
  394. "Stream full shell output into managed storage while retaining only a bounded in-memory preview.",
  395. ]) {
  396. expect(source).toContain(`TODO: ${todo}`)
  397. }
  398. })