bash.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import z from "zod"
  2. import { spawn } from "child_process"
  3. import { Tool } from "./tool"
  4. import DESCRIPTION from "./bash.txt"
  5. import { Permission } from "../permission"
  6. import { Filesystem } from "../util/filesystem"
  7. import { lazy } from "../util/lazy"
  8. import { Log } from "../util/log"
  9. import { Wildcard } from "../util/wildcard"
  10. import { $ } from "bun"
  11. import { Instance } from "../project/instance"
  12. import { Agent } from "../agent/agent"
  13. const MAX_OUTPUT_LENGTH = 30_000
  14. const DEFAULT_TIMEOUT = 1 * 60 * 1000
  15. const MAX_TIMEOUT = 10 * 60 * 1000
  16. const SIGKILL_TIMEOUT_MS = 200
  17. const log = Log.create({ service: "bash-tool" })
  18. const parser = lazy(async () => {
  19. try {
  20. const { default: Parser } = await import("tree-sitter")
  21. const Bash = await import("tree-sitter-bash")
  22. const p = new Parser()
  23. p.setLanguage(Bash.language as any)
  24. return p
  25. } catch (e) {
  26. const { default: Parser } = await import("web-tree-sitter")
  27. const { default: treeWasm } = await import("web-tree-sitter/tree-sitter.wasm" as string, {
  28. with: { type: "wasm" },
  29. })
  30. await Parser.init({
  31. locateFile() {
  32. return treeWasm
  33. },
  34. })
  35. const { default: bashWasm } = await import("tree-sitter-bash/tree-sitter-bash.wasm" as string, {
  36. with: { type: "wasm" },
  37. })
  38. const bashLanguage = await Parser.Language.load(bashWasm)
  39. const p = new Parser()
  40. p.setLanguage(bashLanguage)
  41. return p
  42. }
  43. })
  44. export const BashTool = Tool.define("bash", {
  45. description: DESCRIPTION,
  46. parameters: z.object({
  47. command: z.string().describe("The command to execute"),
  48. timeout: z.number().describe("Optional timeout in milliseconds").optional(),
  49. description: z
  50. .string()
  51. .describe(
  52. "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'",
  53. ),
  54. }),
  55. async execute(params, ctx) {
  56. if (params.timeout !== undefined && params.timeout < 0) {
  57. throw new Error(
  58. `Invalid timeout value: ${params.timeout}. Timeout must be a positive number.`,
  59. )
  60. }
  61. const timeout = Math.min(params.timeout ?? DEFAULT_TIMEOUT, MAX_TIMEOUT)
  62. const tree = await parser().then((p) => p.parse(params.command))
  63. const permissions = await Agent.get(ctx.agent).then((x) => x.permission.bash)
  64. const askPatterns = new Set<string>()
  65. for (const node of tree.rootNode.descendantsOfType("command")) {
  66. const command = []
  67. for (let i = 0; i < node.childCount; i++) {
  68. const child = node.child(i)
  69. if (!child) continue
  70. if (
  71. child.type !== "command_name" &&
  72. child.type !== "word" &&
  73. child.type !== "string" &&
  74. child.type !== "raw_string" &&
  75. child.type !== "concatenation"
  76. ) {
  77. continue
  78. }
  79. command.push(child.text)
  80. }
  81. // not an exhaustive list, but covers most common cases
  82. if (["cd", "rm", "cp", "mv", "mkdir", "touch", "chmod", "chown"].includes(command[0])) {
  83. for (const arg of command.slice(1)) {
  84. if (arg.startsWith("-") || (command[0] === "chmod" && arg.startsWith("+"))) continue
  85. const resolved = await $`realpath ${arg}`
  86. .quiet()
  87. .nothrow()
  88. .text()
  89. .then((x) => x.trim())
  90. log.info("resolved path", { arg, resolved })
  91. if (resolved && !Filesystem.contains(Instance.directory, resolved)) {
  92. throw new Error(
  93. `This command references paths outside of ${Instance.directory} so it is not allowed to be executed.`,
  94. )
  95. }
  96. }
  97. }
  98. // always allow cd if it passes above check
  99. if (command[0] !== "cd") {
  100. const action = Wildcard.allStructured(
  101. { head: command[0], tail: command.slice(1) },
  102. permissions,
  103. )
  104. if (action === "deny") {
  105. throw new Error(
  106. `The user has specifically restricted access to this command, you are not allowed to execute it. Here is the configuration: ${JSON.stringify(permissions)}`,
  107. )
  108. }
  109. if (action === "ask") {
  110. const pattern = (() => {
  111. if (command.length === 0) return
  112. const head = command[0]
  113. // Find first non-flag argument as subcommand
  114. const sub = command.slice(1).find((arg) => !arg.startsWith("-"))
  115. return sub ? `${head} ${sub} *` : `${head} *`
  116. })()
  117. if (pattern) {
  118. askPatterns.add(pattern)
  119. }
  120. }
  121. }
  122. }
  123. if (askPatterns.size > 0) {
  124. const patterns = Array.from(askPatterns)
  125. await Permission.ask({
  126. type: "bash",
  127. pattern: patterns,
  128. sessionID: ctx.sessionID,
  129. messageID: ctx.messageID,
  130. callID: ctx.callID,
  131. title: params.command,
  132. metadata: {
  133. command: params.command,
  134. patterns,
  135. },
  136. })
  137. }
  138. const proc = spawn(params.command, {
  139. shell: true,
  140. cwd: Instance.directory,
  141. stdio: ["ignore", "pipe", "pipe"],
  142. detached: process.platform !== "win32",
  143. })
  144. let output = ""
  145. // Initialize metadata with empty output
  146. ctx.metadata({
  147. metadata: {
  148. output: "",
  149. description: params.description,
  150. },
  151. })
  152. const append = (chunk: Buffer) => {
  153. output += chunk.toString()
  154. ctx.metadata({
  155. metadata: {
  156. output,
  157. description: params.description,
  158. },
  159. })
  160. }
  161. proc.stdout?.on("data", append)
  162. proc.stderr?.on("data", append)
  163. let timedOut = false
  164. let aborted = false
  165. let exited = false
  166. const killTree = async () => {
  167. const pid = proc.pid
  168. if (!pid || exited) {
  169. return
  170. }
  171. if (process.platform === "win32") {
  172. await new Promise<void>((resolve) => {
  173. const killer = spawn("taskkill", ["/pid", String(pid), "/f", "/t"], { stdio: "ignore" })
  174. killer.once("exit", resolve)
  175. killer.once("error", resolve)
  176. })
  177. return
  178. }
  179. try {
  180. process.kill(-pid, "SIGTERM")
  181. await Bun.sleep(SIGKILL_TIMEOUT_MS)
  182. if (!exited) {
  183. process.kill(-pid, "SIGKILL")
  184. }
  185. } catch (_e) {
  186. proc.kill("SIGTERM")
  187. await Bun.sleep(SIGKILL_TIMEOUT_MS)
  188. if (!exited) {
  189. proc.kill("SIGKILL")
  190. }
  191. }
  192. }
  193. if (ctx.abort.aborted) {
  194. aborted = true
  195. await killTree()
  196. }
  197. const abortHandler = () => {
  198. aborted = true
  199. void killTree()
  200. }
  201. ctx.abort.addEventListener("abort", abortHandler, { once: true })
  202. const timeoutTimer = setTimeout(() => {
  203. timedOut = true
  204. void killTree()
  205. }, timeout)
  206. await new Promise<void>((resolve, reject) => {
  207. const cleanup = () => {
  208. clearTimeout(timeoutTimer)
  209. ctx.abort.removeEventListener("abort", abortHandler)
  210. }
  211. proc.once("exit", () => {
  212. exited = true
  213. cleanup()
  214. resolve()
  215. })
  216. proc.once("error", (error) => {
  217. exited = true
  218. cleanup()
  219. reject(error)
  220. })
  221. })
  222. if (output.length > MAX_OUTPUT_LENGTH) {
  223. output = output.slice(0, MAX_OUTPUT_LENGTH)
  224. output += "\n\n(Output was truncated due to length limit)"
  225. }
  226. if (timedOut) {
  227. output += `\n\n(Command timed out after ${timeout} ms)`
  228. }
  229. if (aborted) {
  230. output += "\n\n(Command was aborted)"
  231. }
  232. return {
  233. title: params.command,
  234. metadata: {
  235. output,
  236. exit: proc.exitCode,
  237. description: params.description,
  238. },
  239. output,
  240. }
  241. },
  242. })