tool-bash.test.ts 16 KB

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