bash.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import z from "zod/v4"
  2. import { exec } 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 log = Log.create({ service: "bash-tool" })
  17. const parser = lazy(async () => {
  18. try {
  19. const { default: Parser } = await import("tree-sitter")
  20. const Bash = await import("tree-sitter-bash")
  21. const p = new Parser()
  22. p.setLanguage(Bash.language as any)
  23. return p
  24. } catch (e) {
  25. const { default: Parser } = await import("web-tree-sitter")
  26. const { default: treeWasm } = await import("web-tree-sitter/tree-sitter.wasm" as string, { with: { type: "wasm" } })
  27. await Parser.init({
  28. locateFile() {
  29. return treeWasm
  30. },
  31. })
  32. const { default: bashWasm } = await import("tree-sitter-bash/tree-sitter-bash.wasm" as string, {
  33. with: { type: "wasm" },
  34. })
  35. const bashLanguage = await Parser.Language.load(bashWasm)
  36. const p = new Parser()
  37. p.setLanguage(bashLanguage)
  38. return p
  39. }
  40. })
  41. export const BashTool = Tool.define("bash", {
  42. description: DESCRIPTION,
  43. parameters: z.object({
  44. command: z.string().describe("The command to execute"),
  45. timeout: z.number().describe("Optional timeout in milliseconds").optional(),
  46. description: z
  47. .string()
  48. .describe(
  49. "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'",
  50. ),
  51. }),
  52. async execute(params, ctx) {
  53. const timeout = Math.min(params.timeout ?? DEFAULT_TIMEOUT, MAX_TIMEOUT)
  54. const tree = await parser().then((p) => p.parse(params.command))
  55. const permissions = await Agent.get(ctx.agent).then((x) => x.permission.bash)
  56. const askPatterns = new Set<string>()
  57. for (const node of tree.rootNode.descendantsOfType("command")) {
  58. const command = []
  59. for (let i = 0; i < node.childCount; i++) {
  60. const child = node.child(i)
  61. if (!child) continue
  62. if (
  63. child.type !== "command_name" &&
  64. child.type !== "word" &&
  65. child.type !== "string" &&
  66. child.type !== "raw_string" &&
  67. child.type !== "concatenation"
  68. ) {
  69. continue
  70. }
  71. command.push(child.text)
  72. }
  73. // not an exhaustive list, but covers most common cases
  74. if (["cd", "rm", "cp", "mv", "mkdir", "touch", "chmod", "chown"].includes(command[0])) {
  75. for (const arg of command.slice(1)) {
  76. if (arg.startsWith("-") || (command[0] === "chmod" && arg.startsWith("+"))) continue
  77. const resolved = await $`realpath ${arg}`
  78. .quiet()
  79. .nothrow()
  80. .text()
  81. .then((x) => x.trim())
  82. log.info("resolved path", { arg, resolved })
  83. if (resolved && !Filesystem.contains(Instance.directory, resolved)) {
  84. throw new Error(
  85. `This command references paths outside of ${Instance.directory} so it is not allowed to be executed.`,
  86. )
  87. }
  88. }
  89. }
  90. // always allow cd if it passes above check
  91. if (command[0] !== "cd") {
  92. const action = Wildcard.all(node.text, permissions)
  93. if (action === "deny") {
  94. throw new Error(
  95. `The user has specifically restricted access to this command, you are not allowed to execute it. Here is the configuration: ${JSON.stringify(permissions)}`,
  96. )
  97. }
  98. if (action === "ask") {
  99. const pattern = (() => {
  100. let head = ""
  101. let sub: string | undefined
  102. for (let i = 0; i < node.childCount; i++) {
  103. const child = node.child(i)
  104. if (!child) continue
  105. if (child.type === "command_name") {
  106. if (!head) {
  107. head = child.text
  108. }
  109. continue
  110. }
  111. if (!sub && child.type === "word") {
  112. if (!child.text.startsWith("-")) sub = child.text
  113. }
  114. }
  115. if (!head) return
  116. return sub ? `${head} ${sub} *` : `${head} *`
  117. })()
  118. if (pattern) {
  119. askPatterns.add(pattern)
  120. }
  121. }
  122. }
  123. }
  124. if (askPatterns.size > 0) {
  125. const patterns = Array.from(askPatterns)
  126. await Permission.ask({
  127. type: "bash",
  128. pattern: patterns,
  129. sessionID: ctx.sessionID,
  130. messageID: ctx.messageID,
  131. callID: ctx.callID,
  132. title: params.command,
  133. metadata: {
  134. command: params.command,
  135. patterns,
  136. },
  137. })
  138. }
  139. const process = exec(params.command, {
  140. cwd: Instance.directory,
  141. signal: ctx.abort,
  142. timeout,
  143. })
  144. let output = ""
  145. // Initialize metadata with empty output
  146. ctx.metadata({
  147. metadata: {
  148. output: "",
  149. description: params.description,
  150. },
  151. })
  152. process.stdout?.on("data", (chunk) => {
  153. output += chunk.toString()
  154. ctx.metadata({
  155. metadata: {
  156. output: output,
  157. description: params.description,
  158. },
  159. })
  160. })
  161. process.stderr?.on("data", (chunk) => {
  162. output += chunk.toString()
  163. ctx.metadata({
  164. metadata: {
  165. output: output,
  166. description: params.description,
  167. },
  168. })
  169. })
  170. await new Promise<void>((resolve) => {
  171. process.on("close", () => {
  172. resolve()
  173. })
  174. })
  175. ctx.metadata({
  176. metadata: {
  177. output: output,
  178. exit: process.exitCode,
  179. description: params.description,
  180. },
  181. })
  182. if (output.length > MAX_OUTPUT_LENGTH) {
  183. output = output.slice(0, MAX_OUTPUT_LENGTH)
  184. output += "\n\n(Output was truncated due to length limit)"
  185. }
  186. if (process.signalCode === "SIGTERM" && params.timeout) {
  187. output += `\n\n(Command timed out after ${timeout} ms)`
  188. }
  189. return {
  190. title: params.command,
  191. metadata: {
  192. output,
  193. exit: process.exitCode,
  194. description: params.description,
  195. },
  196. output,
  197. }
  198. },
  199. })