bash.ts 6.4 KB

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