bash.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import z from "zod"
  2. import { spawn } from "child_process"
  3. import { Tool } from "./tool"
  4. import DESCRIPTION from "./bash.txt"
  5. import { Log } from "../util/log"
  6. import { Instance } from "../project/instance"
  7. import { lazy } from "@/util/lazy"
  8. import { Language } from "web-tree-sitter"
  9. import { Agent } from "@/agent/agent"
  10. import { $ } from "bun"
  11. import { Filesystem } from "@/util/filesystem"
  12. import { Wildcard } from "@/util/wildcard"
  13. import { Permission } from "@/permission"
  14. import { fileURLToPath } from "url"
  15. import { Flag } from "@/flag/flag.ts"
  16. import path from "path"
  17. import { iife } from "@/util/iife"
  18. const DEFAULT_MAX_OUTPUT_LENGTH = 30_000
  19. const MAX_OUTPUT_LENGTH = (() => {
  20. const parsed = Number(Flag.OPENCODE_EXPERIMENTAL_BASH_MAX_OUTPUT_LENGTH)
  21. return Number.isInteger(parsed) && parsed > 0 ? parsed : DEFAULT_MAX_OUTPUT_LENGTH
  22. })()
  23. const DEFAULT_TIMEOUT = 2 * 60 * 1000
  24. const SIGKILL_TIMEOUT_MS = 200
  25. export const log = Log.create({ service: "bash-tool" })
  26. const resolveWasm = (asset: string) => {
  27. if (asset.startsWith("file://")) return fileURLToPath(asset)
  28. if (asset.startsWith("/") || /^[a-z]:/i.test(asset)) return asset
  29. const url = new URL(asset, import.meta.url)
  30. return fileURLToPath(url)
  31. }
  32. const parser = lazy(async () => {
  33. const { Parser } = await import("web-tree-sitter")
  34. const { default: treeWasm } = await import("web-tree-sitter/tree-sitter.wasm" as string, {
  35. with: { type: "wasm" },
  36. })
  37. const treePath = resolveWasm(treeWasm)
  38. await Parser.init({
  39. locateFile() {
  40. return treePath
  41. },
  42. })
  43. const { default: bashWasm } = await import("tree-sitter-bash/tree-sitter-bash.wasm" as string, {
  44. with: { type: "wasm" },
  45. })
  46. const bashPath = resolveWasm(bashWasm)
  47. const bashLanguage = await Language.load(bashPath)
  48. const p = new Parser()
  49. p.setLanguage(bashLanguage)
  50. return p
  51. })
  52. // TODO: we may wanna rename this tool so it works better on other shells
  53. export const BashTool = Tool.define("bash", async () => {
  54. const shell = iife(() => {
  55. const s = process.env.SHELL
  56. if (s) {
  57. if (!new Set(["/bin/fish", "/bin/nu", "/usr/bin/fish", "/usr/bin/nu"]).has(s)) {
  58. return s
  59. }
  60. }
  61. if (process.platform === "darwin") {
  62. return "/bin/zsh"
  63. }
  64. if (process.platform === "win32") {
  65. // Let Bun / Node pick COMSPEC (usually cmd.exe)
  66. // or explicitly:
  67. return process.env.COMSPEC || true
  68. }
  69. const bash = Bun.which("bash")
  70. if (bash) {
  71. return bash
  72. }
  73. return true
  74. })
  75. log.info("bash tool using shell", { shell })
  76. return {
  77. description: DESCRIPTION,
  78. parameters: z.object({
  79. command: z.string().describe("The command to execute"),
  80. timeout: z.number().describe("Optional timeout in milliseconds").optional(),
  81. workdir: z
  82. .string()
  83. .describe(
  84. `The working directory to run the command in. Defaults to ${Instance.directory}. Use this instead of 'cd' commands.`,
  85. )
  86. .optional(),
  87. description: z
  88. .string()
  89. .describe(
  90. "Clear, concise description of what this command does in 5-10 words. Examples:\nInput: ls\nOutput: Lists files in current directory\n\nInput: git status\nOutput: Shows working tree status\n\nInput: npm install\nOutput: Installs package dependencies\n\nInput: mkdir foo\nOutput: Creates directory 'foo'",
  91. ),
  92. }),
  93. async execute(params, ctx) {
  94. const cwd = params.workdir || Instance.directory
  95. if (params.timeout !== undefined && params.timeout < 0) {
  96. throw new Error(`Invalid timeout value: ${params.timeout}. Timeout must be a positive number.`)
  97. }
  98. const timeout = params.timeout ?? DEFAULT_TIMEOUT
  99. const tree = await parser().then((p) => p.parse(params.command))
  100. if (!tree) {
  101. throw new Error("Failed to parse command")
  102. }
  103. const agent = await Agent.get(ctx.agent)
  104. const checkExternalDirectory = async (dir: string) => {
  105. if (Filesystem.contains(Instance.directory, dir)) return
  106. const title = `This command references paths outside of ${Instance.directory}`
  107. if (agent.permission.external_directory === "ask") {
  108. await Permission.ask({
  109. type: "external_directory",
  110. pattern: [dir, path.join(dir, "*")],
  111. sessionID: ctx.sessionID,
  112. messageID: ctx.messageID,
  113. callID: ctx.callID,
  114. title,
  115. metadata: {
  116. command: params.command,
  117. },
  118. })
  119. } else if (agent.permission.external_directory === "deny") {
  120. throw new Permission.RejectedError(
  121. ctx.sessionID,
  122. "external_directory",
  123. ctx.callID,
  124. {
  125. command: params.command,
  126. },
  127. `${title} so this command is not allowed to be executed.`,
  128. )
  129. }
  130. }
  131. await checkExternalDirectory(cwd)
  132. const permissions = agent.permission.bash
  133. const askPatterns = new Set<string>()
  134. for (const node of tree.rootNode.descendantsOfType("command")) {
  135. if (!node) continue
  136. const command = []
  137. for (let i = 0; i < node.childCount; i++) {
  138. const child = node.child(i)
  139. if (!child) continue
  140. if (
  141. child.type !== "command_name" &&
  142. child.type !== "word" &&
  143. child.type !== "string" &&
  144. child.type !== "raw_string" &&
  145. child.type !== "concatenation"
  146. ) {
  147. continue
  148. }
  149. command.push(child.text)
  150. }
  151. // not an exhaustive list, but covers most common cases
  152. if (["cd", "rm", "cp", "mv", "mkdir", "touch", "chmod", "chown"].includes(command[0])) {
  153. for (const arg of command.slice(1)) {
  154. if (arg.startsWith("-") || (command[0] === "chmod" && arg.startsWith("+"))) continue
  155. const resolved = await $`realpath ${arg}`
  156. .quiet()
  157. .nothrow()
  158. .text()
  159. .then((x) => x.trim())
  160. log.info("resolved path", { arg, resolved })
  161. if (resolved) {
  162. // Git Bash on Windows returns Unix-style paths like /c/Users/...
  163. const normalized =
  164. process.platform === "win32" && resolved.match(/^\/[a-z]\//)
  165. ? resolved.replace(/^\/([a-z])\//, (_, drive) => `${drive.toUpperCase()}:\\`).replace(/\//g, "\\")
  166. : resolved
  167. await checkExternalDirectory(normalized)
  168. }
  169. }
  170. }
  171. // always allow cd if it passes above check
  172. if (command[0] !== "cd") {
  173. const action = Wildcard.allStructured({ head: command[0], tail: command.slice(1) }, permissions)
  174. if (action === "deny") {
  175. throw new Error(
  176. `The user has specifically restricted access to this command, you are not allowed to execute it. Here is the configuration: ${JSON.stringify(permissions)}`,
  177. )
  178. }
  179. if (action === "ask") {
  180. const pattern = (() => {
  181. if (command.length === 0) return
  182. const head = command[0]
  183. // Find first non-flag argument as subcommand
  184. const sub = command.slice(1).find((arg) => !arg.startsWith("-"))
  185. return sub ? `${head} ${sub} *` : `${head} *`
  186. })()
  187. if (pattern) {
  188. askPatterns.add(pattern)
  189. }
  190. }
  191. }
  192. }
  193. if (askPatterns.size > 0) {
  194. const patterns = Array.from(askPatterns)
  195. await Permission.ask({
  196. type: "bash",
  197. pattern: patterns,
  198. sessionID: ctx.sessionID,
  199. messageID: ctx.messageID,
  200. callID: ctx.callID,
  201. title: params.command,
  202. metadata: {
  203. command: params.command,
  204. patterns,
  205. },
  206. })
  207. }
  208. const proc = spawn(params.command, {
  209. shell,
  210. cwd,
  211. env: {
  212. ...process.env,
  213. },
  214. stdio: ["ignore", "pipe", "pipe"],
  215. detached: process.platform !== "win32",
  216. })
  217. let output = ""
  218. // Initialize metadata with empty output
  219. ctx.metadata({
  220. metadata: {
  221. output: "",
  222. description: params.description,
  223. },
  224. })
  225. const append = (chunk: Buffer) => {
  226. if (output.length <= MAX_OUTPUT_LENGTH) {
  227. output += chunk.toString()
  228. ctx.metadata({
  229. metadata: {
  230. output,
  231. description: params.description,
  232. },
  233. })
  234. }
  235. }
  236. proc.stdout?.on("data", append)
  237. proc.stderr?.on("data", append)
  238. let timedOut = false
  239. let aborted = false
  240. let exited = false
  241. const killTree = async () => {
  242. const pid = proc.pid
  243. if (!pid || exited) {
  244. return
  245. }
  246. if (process.platform === "win32") {
  247. await new Promise<void>((resolve) => {
  248. const killer = spawn("taskkill", ["/pid", String(pid), "/f", "/t"], { stdio: "ignore" })
  249. killer.once("exit", resolve)
  250. killer.once("error", resolve)
  251. })
  252. return
  253. }
  254. try {
  255. process.kill(-pid, "SIGTERM")
  256. await Bun.sleep(SIGKILL_TIMEOUT_MS)
  257. if (!exited) {
  258. process.kill(-pid, "SIGKILL")
  259. }
  260. } catch (_e) {
  261. proc.kill("SIGTERM")
  262. await Bun.sleep(SIGKILL_TIMEOUT_MS)
  263. if (!exited) {
  264. proc.kill("SIGKILL")
  265. }
  266. }
  267. }
  268. if (ctx.abort.aborted) {
  269. aborted = true
  270. await killTree()
  271. }
  272. const abortHandler = () => {
  273. aborted = true
  274. void killTree()
  275. }
  276. ctx.abort.addEventListener("abort", abortHandler, { once: true })
  277. const timeoutTimer = setTimeout(() => {
  278. timedOut = true
  279. void killTree()
  280. }, timeout + 100)
  281. await new Promise<void>((resolve, reject) => {
  282. const cleanup = () => {
  283. clearTimeout(timeoutTimer)
  284. ctx.abort.removeEventListener("abort", abortHandler)
  285. }
  286. proc.once("exit", () => {
  287. exited = true
  288. cleanup()
  289. resolve()
  290. })
  291. proc.once("error", (error) => {
  292. exited = true
  293. cleanup()
  294. reject(error)
  295. })
  296. })
  297. let resultMetadata: String[] = ["<bash_metadata>"]
  298. if (output.length > MAX_OUTPUT_LENGTH) {
  299. output = output.slice(0, MAX_OUTPUT_LENGTH)
  300. resultMetadata.push(`bash tool truncated output as it exceeded ${MAX_OUTPUT_LENGTH} char limit`)
  301. }
  302. if (timedOut) {
  303. resultMetadata.push(`bash tool terminated commmand after exceeding timeout ${timeout} ms`)
  304. }
  305. if (aborted) {
  306. resultMetadata.push("User aborted the command")
  307. }
  308. if (resultMetadata.length > 1) {
  309. resultMetadata.push("</bash_metadata>")
  310. output += "\n\n" + resultMetadata.join("\n")
  311. }
  312. return {
  313. title: params.description,
  314. metadata: {
  315. output,
  316. exit: proc.exitCode,
  317. description: params.description,
  318. },
  319. output,
  320. }
  321. },
  322. }
  323. })