tool-bash.test.ts 17 KB

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