tool-bash.test.ts 16 KB

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