bash.test.ts 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. import { describe, expect, test } from "bun:test"
  2. import { Effect, Layer, ManagedRuntime } from "effect"
  3. import os from "os"
  4. import path from "path"
  5. import { Shell } from "../../src/shell/shell"
  6. import { BashTool } from "../../src/tool/bash"
  7. import { Instance } from "../../src/project/instance"
  8. import { Filesystem } from "../../src/util/filesystem"
  9. import { tmpdir } from "../fixture/fixture"
  10. import type { Permission } from "../../src/permission"
  11. import { Truncate } from "../../src/tool/truncate"
  12. import { SessionID, MessageID } from "../../src/session/schema"
  13. import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
  14. import { AppFileSystem } from "../../src/filesystem"
  15. import { Plugin } from "../../src/plugin"
  16. const runtime = ManagedRuntime.make(
  17. Layer.mergeAll(CrossSpawnSpawner.defaultLayer, AppFileSystem.defaultLayer, Plugin.defaultLayer),
  18. )
  19. function initBash() {
  20. return runtime.runPromise(BashTool.pipe(Effect.flatMap((info) => Effect.promise(() => info.init()))))
  21. }
  22. const ctx = {
  23. sessionID: SessionID.make("ses_test"),
  24. messageID: MessageID.make(""),
  25. callID: "",
  26. agent: "build",
  27. abort: AbortSignal.any([]),
  28. messages: [],
  29. metadata: () => {},
  30. ask: async () => {},
  31. }
  32. Shell.acceptable.reset()
  33. const quote = (text: string) => `"${text}"`
  34. const squote = (text: string) => `'${text}'`
  35. const projectRoot = path.join(__dirname, "../..")
  36. const bin = quote(process.execPath.replaceAll("\\", "/"))
  37. const bash = (() => {
  38. const shell = Shell.acceptable()
  39. if (Shell.name(shell) === "bash") return shell
  40. return Shell.gitbash()
  41. })()
  42. const shells = (() => {
  43. if (process.platform !== "win32") {
  44. const shell = Shell.acceptable()
  45. return [{ label: Shell.name(shell), shell }]
  46. }
  47. const list = [bash, Bun.which("pwsh"), Bun.which("powershell"), process.env.COMSPEC || Bun.which("cmd.exe")]
  48. .filter((shell): shell is string => Boolean(shell))
  49. .map((shell) => ({ label: Shell.name(shell), shell }))
  50. return list.filter(
  51. (item, i) => list.findIndex((other) => other.shell.toLowerCase() === item.shell.toLowerCase()) === i,
  52. )
  53. })()
  54. const PS = new Set(["pwsh", "powershell"])
  55. const ps = shells.filter((item) => PS.has(item.label))
  56. const sh = () => Shell.name(Shell.acceptable())
  57. const evalarg = (text: string) => (sh() === "cmd" ? quote(text) : squote(text))
  58. const fill = (mode: "lines" | "bytes", n: number) => {
  59. const code =
  60. mode === "lines"
  61. ? "console.log(Array.from({length:Number(Bun.argv[1])},(_,i)=>i+1).join(String.fromCharCode(10)))"
  62. : "process.stdout.write(String.fromCharCode(97).repeat(Number(Bun.argv[1])))"
  63. const text = `${bin} -e ${evalarg(code)} ${n}`
  64. if (PS.has(sh())) return `& ${text}`
  65. return text
  66. }
  67. const glob = (p: string) =>
  68. process.platform === "win32" ? Filesystem.normalizePathPattern(p) : p.replaceAll("\\", "/")
  69. const forms = (dir: string) => {
  70. if (process.platform !== "win32") return [dir]
  71. const full = Filesystem.normalizePath(dir)
  72. const slash = full.replaceAll("\\", "/")
  73. const root = slash.replace(/^[A-Za-z]:/, "")
  74. return Array.from(new Set([full, slash, root, root.toLowerCase()]))
  75. }
  76. const withShell = (item: { label: string; shell: string }, fn: () => Promise<void>) => async () => {
  77. const prev = process.env.SHELL
  78. process.env.SHELL = item.shell
  79. Shell.acceptable.reset()
  80. Shell.preferred.reset()
  81. try {
  82. await fn()
  83. } finally {
  84. if (prev === undefined) delete process.env.SHELL
  85. else process.env.SHELL = prev
  86. Shell.acceptable.reset()
  87. Shell.preferred.reset()
  88. }
  89. }
  90. const each = (name: string, fn: (item: { label: string; shell: string }) => Promise<void>) => {
  91. for (const item of shells) {
  92. test(
  93. `${name} [${item.label}]`,
  94. withShell(item, () => fn(item)),
  95. )
  96. }
  97. }
  98. const capture = (requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">>, stop?: Error) => ({
  99. ...ctx,
  100. ask: async (req: Omit<Permission.Request, "id" | "sessionID" | "tool">) => {
  101. requests.push(req)
  102. if (stop) throw stop
  103. },
  104. })
  105. const mustTruncate = (result: {
  106. metadata: { truncated?: boolean; exit?: number | null } & Record<string, unknown>
  107. output: string
  108. }) => {
  109. if (result.metadata.truncated) return
  110. throw new Error(
  111. [`shell: ${process.env.SHELL || ""}`, `exit: ${String(result.metadata.exit)}`, "output:", result.output].join("\n"),
  112. )
  113. }
  114. describe("tool.bash", () => {
  115. each("basic", async () => {
  116. await Instance.provide({
  117. directory: projectRoot,
  118. fn: async () => {
  119. const bash = await initBash()
  120. const result = await bash.execute(
  121. {
  122. command: "echo test",
  123. description: "Echo test message",
  124. },
  125. ctx,
  126. )
  127. expect(result.metadata.exit).toBe(0)
  128. expect(result.metadata.output).toContain("test")
  129. },
  130. })
  131. })
  132. })
  133. describe("tool.bash permissions", () => {
  134. each("asks for bash permission with correct pattern", async () => {
  135. await using tmp = await tmpdir()
  136. await Instance.provide({
  137. directory: tmp.path,
  138. fn: async () => {
  139. const bash = await initBash()
  140. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  141. await bash.execute(
  142. {
  143. command: "echo hello",
  144. description: "Echo hello",
  145. },
  146. capture(requests),
  147. )
  148. expect(requests.length).toBe(1)
  149. expect(requests[0].permission).toBe("bash")
  150. expect(requests[0].patterns).toContain("echo hello")
  151. },
  152. })
  153. })
  154. each("asks for bash permission with multiple commands", async () => {
  155. await using tmp = await tmpdir()
  156. await Instance.provide({
  157. directory: tmp.path,
  158. fn: async () => {
  159. const bash = await initBash()
  160. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  161. await bash.execute(
  162. {
  163. command: "echo foo && echo bar",
  164. description: "Echo twice",
  165. },
  166. capture(requests),
  167. )
  168. expect(requests.length).toBe(1)
  169. expect(requests[0].permission).toBe("bash")
  170. expect(requests[0].patterns).toContain("echo foo")
  171. expect(requests[0].patterns).toContain("echo bar")
  172. },
  173. })
  174. })
  175. for (const item of ps) {
  176. test(
  177. `parses PowerShell conditionals for permission prompts [${item.label}]`,
  178. withShell(item, async () => {
  179. await Instance.provide({
  180. directory: projectRoot,
  181. fn: async () => {
  182. const bash = await initBash()
  183. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  184. await bash.execute(
  185. {
  186. command: "Write-Host foo; if ($?) { Write-Host bar }",
  187. description: "Check PowerShell conditional",
  188. },
  189. capture(requests),
  190. )
  191. const bashReq = requests.find((r) => r.permission === "bash")
  192. expect(bashReq).toBeDefined()
  193. expect(bashReq!.patterns).toContain("Write-Host foo")
  194. expect(bashReq!.patterns).toContain("Write-Host bar")
  195. expect(bashReq!.always).toContain("Write-Host *")
  196. },
  197. })
  198. }),
  199. )
  200. }
  201. each("asks for external_directory permission for wildcard external paths", async () => {
  202. await Instance.provide({
  203. directory: projectRoot,
  204. fn: async () => {
  205. const bash = await initBash()
  206. const err = new Error("stop after permission")
  207. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  208. const file = process.platform === "win32" ? `${process.env.WINDIR!.replaceAll("\\", "/")}/*` : "/etc/*"
  209. const want = process.platform === "win32" ? glob(path.join(process.env.WINDIR!, "*")) : "/etc/*"
  210. await expect(
  211. bash.execute(
  212. {
  213. command: `cat ${file}`,
  214. description: "Read wildcard path",
  215. },
  216. capture(requests, err),
  217. ),
  218. ).rejects.toThrow(err.message)
  219. const extDirReq = requests.find((r) => r.permission === "external_directory")
  220. expect(extDirReq).toBeDefined()
  221. expect(extDirReq!.patterns).toContain(want)
  222. },
  223. })
  224. })
  225. if (process.platform === "win32") {
  226. if (bash) {
  227. test(
  228. "asks for nested bash command permissions [bash]",
  229. withShell({ label: "bash", shell: bash }, async () => {
  230. await using outerTmp = await tmpdir({
  231. init: async (dir) => {
  232. await Bun.write(path.join(dir, "outside.txt"), "x")
  233. },
  234. })
  235. await Instance.provide({
  236. directory: projectRoot,
  237. fn: async () => {
  238. const bash = await initBash()
  239. const file = path.join(outerTmp.path, "outside.txt").replaceAll("\\", "/")
  240. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  241. await bash.execute(
  242. {
  243. command: `echo $(cat "${file}")`,
  244. description: "Read nested bash file",
  245. },
  246. capture(requests),
  247. )
  248. const extDirReq = requests.find((r) => r.permission === "external_directory")
  249. const bashReq = requests.find((r) => r.permission === "bash")
  250. expect(extDirReq).toBeDefined()
  251. expect(extDirReq!.patterns).toContain(glob(path.join(outerTmp.path, "*")))
  252. expect(bashReq).toBeDefined()
  253. expect(bashReq!.patterns).toContain(`cat "${file}"`)
  254. },
  255. })
  256. }),
  257. )
  258. }
  259. }
  260. if (process.platform === "win32") {
  261. for (const item of ps) {
  262. test(
  263. `asks for external_directory permission for PowerShell paths after switches [${item.label}]`,
  264. withShell(item, async () => {
  265. await Instance.provide({
  266. directory: projectRoot,
  267. fn: async () => {
  268. const bash = await initBash()
  269. const err = new Error("stop after permission")
  270. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  271. await expect(
  272. bash.execute(
  273. {
  274. command: `Copy-Item -PassThru "${process.env.WINDIR!.replaceAll("\\", "/")}/win.ini" ./out`,
  275. description: "Copy Windows ini",
  276. },
  277. capture(requests, err),
  278. ),
  279. ).rejects.toThrow(err.message)
  280. const extDirReq = requests.find((r) => r.permission === "external_directory")
  281. expect(extDirReq).toBeDefined()
  282. expect(extDirReq!.patterns).toContain(glob(path.join(process.env.WINDIR!, "*")))
  283. },
  284. })
  285. }),
  286. )
  287. }
  288. for (const item of ps) {
  289. test(
  290. `asks for nested PowerShell command permissions [${item.label}]`,
  291. withShell(item, async () => {
  292. await Instance.provide({
  293. directory: projectRoot,
  294. fn: async () => {
  295. const bash = await initBash()
  296. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  297. const file = `${process.env.WINDIR!.replaceAll("\\", "/")}/win.ini`
  298. await bash.execute(
  299. {
  300. command: `Write-Output $(Get-Content ${file})`,
  301. description: "Read nested PowerShell file",
  302. },
  303. capture(requests),
  304. )
  305. const extDirReq = requests.find((r) => r.permission === "external_directory")
  306. const bashReq = requests.find((r) => r.permission === "bash")
  307. expect(extDirReq).toBeDefined()
  308. expect(extDirReq!.patterns).toContain(glob(path.join(process.env.WINDIR!, "*")))
  309. expect(bashReq).toBeDefined()
  310. expect(bashReq!.patterns).toContain(`Get-Content ${file}`)
  311. },
  312. })
  313. }),
  314. )
  315. }
  316. for (const item of ps) {
  317. test(
  318. `asks for external_directory permission for drive-relative PowerShell paths [${item.label}]`,
  319. withShell(item, async () => {
  320. await using tmp = await tmpdir()
  321. await Instance.provide({
  322. directory: tmp.path,
  323. fn: async () => {
  324. const bash = await initBash()
  325. const err = new Error("stop after permission")
  326. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  327. await expect(
  328. bash.execute(
  329. {
  330. command: 'Get-Content "C:../outside.txt"',
  331. description: "Read drive-relative file",
  332. },
  333. capture(requests, err),
  334. ),
  335. ).rejects.toThrow(err.message)
  336. expect(requests[0]?.permission).toBe("external_directory")
  337. if (requests[0]?.permission !== "external_directory") return
  338. expect(requests[0].patterns).toContain(glob(path.join(path.dirname(tmp.path), "*")))
  339. },
  340. })
  341. }),
  342. )
  343. }
  344. for (const item of ps) {
  345. test(
  346. `asks for external_directory permission for $HOME PowerShell paths [${item.label}]`,
  347. withShell(item, async () => {
  348. await Instance.provide({
  349. directory: projectRoot,
  350. fn: async () => {
  351. const bash = await initBash()
  352. const err = new Error("stop after permission")
  353. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  354. await expect(
  355. bash.execute(
  356. {
  357. command: 'Get-Content "$HOME/.ssh/config"',
  358. description: "Read home config",
  359. },
  360. capture(requests, err),
  361. ),
  362. ).rejects.toThrow(err.message)
  363. expect(requests[0]?.permission).toBe("external_directory")
  364. if (requests[0]?.permission !== "external_directory") return
  365. expect(requests[0].patterns).toContain(glob(path.join(os.homedir(), ".ssh", "*")))
  366. },
  367. })
  368. }),
  369. )
  370. }
  371. for (const item of ps) {
  372. test(
  373. `asks for external_directory permission for $PWD PowerShell paths [${item.label}]`,
  374. withShell(item, async () => {
  375. await using tmp = await tmpdir()
  376. await Instance.provide({
  377. directory: tmp.path,
  378. fn: async () => {
  379. const bash = await initBash()
  380. const err = new Error("stop after permission")
  381. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  382. await expect(
  383. bash.execute(
  384. {
  385. command: 'Get-Content "$PWD/../outside.txt"',
  386. description: "Read pwd-relative file",
  387. },
  388. capture(requests, err),
  389. ),
  390. ).rejects.toThrow(err.message)
  391. expect(requests[0]?.permission).toBe("external_directory")
  392. if (requests[0]?.permission !== "external_directory") return
  393. expect(requests[0].patterns).toContain(glob(path.join(path.dirname(tmp.path), "*")))
  394. },
  395. })
  396. }),
  397. )
  398. }
  399. for (const item of ps) {
  400. test(
  401. `asks for external_directory permission for $PSHOME PowerShell paths [${item.label}]`,
  402. withShell(item, async () => {
  403. await Instance.provide({
  404. directory: projectRoot,
  405. fn: async () => {
  406. const bash = await initBash()
  407. const err = new Error("stop after permission")
  408. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  409. await expect(
  410. bash.execute(
  411. {
  412. command: 'Get-Content "$PSHOME/outside.txt"',
  413. description: "Read pshome file",
  414. },
  415. capture(requests, err),
  416. ),
  417. ).rejects.toThrow(err.message)
  418. expect(requests[0]?.permission).toBe("external_directory")
  419. if (requests[0]?.permission !== "external_directory") return
  420. expect(requests[0].patterns).toContain(glob(path.join(path.dirname(item.shell), "*")))
  421. },
  422. })
  423. }),
  424. )
  425. }
  426. for (const item of ps) {
  427. test(
  428. `asks for external_directory permission for missing PowerShell env paths [${item.label}]`,
  429. withShell(item, async () => {
  430. const key = "OPENCODE_TEST_MISSING"
  431. const prev = process.env[key]
  432. delete process.env[key]
  433. try {
  434. await Instance.provide({
  435. directory: projectRoot,
  436. fn: async () => {
  437. const bash = await initBash()
  438. const err = new Error("stop after permission")
  439. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  440. const root = path.parse(process.env.WINDIR!).root.replace(/[\\/]+$/, "")
  441. await expect(
  442. bash.execute(
  443. {
  444. command: `Get-Content -Path "${root}$env:${key}\\Windows\\win.ini"`,
  445. description: "Read Windows ini with missing env",
  446. },
  447. capture(requests, err),
  448. ),
  449. ).rejects.toThrow(err.message)
  450. const extDirReq = requests.find((r) => r.permission === "external_directory")
  451. expect(extDirReq).toBeDefined()
  452. expect(extDirReq!.patterns).toContain(glob(path.join(process.env.WINDIR!, "*")))
  453. },
  454. })
  455. } finally {
  456. if (prev === undefined) delete process.env[key]
  457. else process.env[key] = prev
  458. }
  459. }),
  460. )
  461. }
  462. for (const item of ps) {
  463. test(
  464. `asks for external_directory permission for PowerShell env paths [${item.label}]`,
  465. withShell(item, async () => {
  466. await Instance.provide({
  467. directory: projectRoot,
  468. fn: async () => {
  469. const bash = await initBash()
  470. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  471. await bash.execute(
  472. {
  473. command: "Get-Content $env:WINDIR/win.ini",
  474. description: "Read Windows ini from env",
  475. },
  476. capture(requests),
  477. )
  478. const extDirReq = requests.find((r) => r.permission === "external_directory")
  479. expect(extDirReq).toBeDefined()
  480. expect(extDirReq!.patterns).toContain(
  481. Filesystem.normalizePathPattern(path.join(process.env.WINDIR!, "*")),
  482. )
  483. },
  484. })
  485. }),
  486. )
  487. }
  488. for (const item of ps) {
  489. test(
  490. `asks for external_directory permission for PowerShell FileSystem paths [${item.label}]`,
  491. withShell(item, async () => {
  492. await Instance.provide({
  493. directory: projectRoot,
  494. fn: async () => {
  495. const bash = await initBash()
  496. const err = new Error("stop after permission")
  497. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  498. await expect(
  499. bash.execute(
  500. {
  501. command: `Get-Content -Path FileSystem::${process.env.WINDIR!.replaceAll("\\", "/")}/win.ini`,
  502. description: "Read Windows ini from FileSystem provider",
  503. },
  504. capture(requests, err),
  505. ),
  506. ).rejects.toThrow(err.message)
  507. expect(requests[0]?.permission).toBe("external_directory")
  508. if (requests[0]?.permission !== "external_directory") return
  509. expect(requests[0].patterns).toContain(
  510. Filesystem.normalizePathPattern(path.join(process.env.WINDIR!, "*")),
  511. )
  512. },
  513. })
  514. }),
  515. )
  516. }
  517. for (const item of ps) {
  518. test(
  519. `asks for external_directory permission for braced PowerShell env paths [${item.label}]`,
  520. withShell(item, async () => {
  521. await Instance.provide({
  522. directory: projectRoot,
  523. fn: async () => {
  524. const bash = await initBash()
  525. const err = new Error("stop after permission")
  526. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  527. await expect(
  528. bash.execute(
  529. {
  530. command: "Get-Content ${env:WINDIR}/win.ini",
  531. description: "Read Windows ini from braced env",
  532. },
  533. capture(requests, err),
  534. ),
  535. ).rejects.toThrow(err.message)
  536. expect(requests[0]?.permission).toBe("external_directory")
  537. if (requests[0]?.permission !== "external_directory") return
  538. expect(requests[0].patterns).toContain(
  539. Filesystem.normalizePathPattern(path.join(process.env.WINDIR!, "*")),
  540. )
  541. },
  542. })
  543. }),
  544. )
  545. }
  546. for (const item of ps) {
  547. test(
  548. `treats Set-Location like cd for permissions [${item.label}]`,
  549. withShell(item, async () => {
  550. await Instance.provide({
  551. directory: projectRoot,
  552. fn: async () => {
  553. const bash = await initBash()
  554. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  555. await bash.execute(
  556. {
  557. command: "Set-Location C:/Windows",
  558. description: "Change location",
  559. },
  560. capture(requests),
  561. )
  562. const extDirReq = requests.find((r) => r.permission === "external_directory")
  563. const bashReq = requests.find((r) => r.permission === "bash")
  564. expect(extDirReq).toBeDefined()
  565. expect(extDirReq!.patterns).toContain(
  566. Filesystem.normalizePathPattern(path.join(process.env.WINDIR!, "*")),
  567. )
  568. expect(bashReq).toBeUndefined()
  569. },
  570. })
  571. }),
  572. )
  573. }
  574. for (const item of ps) {
  575. test(
  576. `does not add nested PowerShell expressions to permission prompts [${item.label}]`,
  577. withShell(item, async () => {
  578. await Instance.provide({
  579. directory: projectRoot,
  580. fn: async () => {
  581. const bash = await initBash()
  582. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  583. await bash.execute(
  584. {
  585. command: "Write-Output ('a' * 3)",
  586. description: "Write repeated text",
  587. },
  588. capture(requests),
  589. )
  590. const bashReq = requests.find((r) => r.permission === "bash")
  591. expect(bashReq).toBeDefined()
  592. expect(bashReq!.patterns).not.toContain("a * 3")
  593. expect(bashReq!.always).not.toContain("a *")
  594. },
  595. })
  596. }),
  597. )
  598. }
  599. }
  600. each("asks for external_directory permission when cd to parent", async () => {
  601. await using tmp = await tmpdir()
  602. await Instance.provide({
  603. directory: tmp.path,
  604. fn: async () => {
  605. const bash = await initBash()
  606. const err = new Error("stop after permission")
  607. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  608. await expect(
  609. bash.execute(
  610. {
  611. command: "cd ../",
  612. description: "Change to parent directory",
  613. },
  614. capture(requests, err),
  615. ),
  616. ).rejects.toThrow(err.message)
  617. const extDirReq = requests.find((r) => r.permission === "external_directory")
  618. expect(extDirReq).toBeDefined()
  619. },
  620. })
  621. })
  622. each("asks for external_directory permission when workdir is outside project", async () => {
  623. await using tmp = await tmpdir()
  624. await Instance.provide({
  625. directory: tmp.path,
  626. fn: async () => {
  627. const bash = await initBash()
  628. const err = new Error("stop after permission")
  629. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  630. await expect(
  631. bash.execute(
  632. {
  633. command: "echo ok",
  634. workdir: os.tmpdir(),
  635. description: "Echo from temp dir",
  636. },
  637. capture(requests, err),
  638. ),
  639. ).rejects.toThrow(err.message)
  640. const extDirReq = requests.find((r) => r.permission === "external_directory")
  641. expect(extDirReq).toBeDefined()
  642. expect(extDirReq!.patterns).toContain(glob(path.join(os.tmpdir(), "*")))
  643. },
  644. })
  645. })
  646. if (process.platform === "win32") {
  647. test("normalizes external_directory workdir variants on Windows", async () => {
  648. const err = new Error("stop after permission")
  649. await using outerTmp = await tmpdir()
  650. await using tmp = await tmpdir()
  651. await Instance.provide({
  652. directory: tmp.path,
  653. fn: async () => {
  654. const bash = await initBash()
  655. const want = Filesystem.normalizePathPattern(path.join(outerTmp.path, "*"))
  656. for (const dir of forms(outerTmp.path)) {
  657. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  658. await expect(
  659. bash.execute(
  660. {
  661. command: "echo ok",
  662. workdir: dir,
  663. description: "Echo from external dir",
  664. },
  665. capture(requests, err),
  666. ),
  667. ).rejects.toThrow(err.message)
  668. const extDirReq = requests.find((r) => r.permission === "external_directory")
  669. expect({ dir, patterns: extDirReq?.patterns, always: extDirReq?.always }).toEqual({
  670. dir,
  671. patterns: [want],
  672. always: [want],
  673. })
  674. }
  675. },
  676. })
  677. })
  678. if (bash) {
  679. test(
  680. "uses Git Bash /tmp semantics for external workdir",
  681. withShell({ label: "bash", shell: bash }, async () => {
  682. await Instance.provide({
  683. directory: projectRoot,
  684. fn: async () => {
  685. const bash = await initBash()
  686. const err = new Error("stop after permission")
  687. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  688. const want = glob(path.join(os.tmpdir(), "*"))
  689. await expect(
  690. bash.execute(
  691. {
  692. command: "echo ok",
  693. workdir: "/tmp",
  694. description: "Echo from Git Bash tmp",
  695. },
  696. capture(requests, err),
  697. ),
  698. ).rejects.toThrow(err.message)
  699. expect(requests[0]).toMatchObject({
  700. permission: "external_directory",
  701. patterns: [want],
  702. always: [want],
  703. })
  704. },
  705. })
  706. }),
  707. )
  708. test(
  709. "uses Git Bash /tmp semantics for external file paths",
  710. withShell({ label: "bash", shell: bash }, async () => {
  711. await Instance.provide({
  712. directory: projectRoot,
  713. fn: async () => {
  714. const bash = await initBash()
  715. const err = new Error("stop after permission")
  716. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  717. const want = glob(path.join(os.tmpdir(), "*"))
  718. await expect(
  719. bash.execute(
  720. {
  721. command: "cat /tmp/opencode-does-not-exist",
  722. description: "Read Git Bash tmp file",
  723. },
  724. capture(requests, err),
  725. ),
  726. ).rejects.toThrow(err.message)
  727. expect(requests[0]).toMatchObject({
  728. permission: "external_directory",
  729. patterns: [want],
  730. always: [want],
  731. })
  732. },
  733. })
  734. }),
  735. )
  736. }
  737. }
  738. each("asks for external_directory permission when file arg is outside project", async () => {
  739. await using outerTmp = await tmpdir({
  740. init: async (dir) => {
  741. await Bun.write(path.join(dir, "outside.txt"), "x")
  742. },
  743. })
  744. await using tmp = await tmpdir()
  745. await Instance.provide({
  746. directory: tmp.path,
  747. fn: async () => {
  748. const bash = await initBash()
  749. const err = new Error("stop after permission")
  750. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  751. const filepath = path.join(outerTmp.path, "outside.txt")
  752. await expect(
  753. bash.execute(
  754. {
  755. command: `cat ${filepath}`,
  756. description: "Read external file",
  757. },
  758. capture(requests, err),
  759. ),
  760. ).rejects.toThrow(err.message)
  761. const extDirReq = requests.find((r) => r.permission === "external_directory")
  762. const expected = glob(path.join(outerTmp.path, "*"))
  763. expect(extDirReq).toBeDefined()
  764. expect(extDirReq!.patterns).toContain(expected)
  765. expect(extDirReq!.always).toContain(expected)
  766. },
  767. })
  768. })
  769. each("does not ask for external_directory permission when rm inside project", async () => {
  770. await using tmp = await tmpdir({
  771. init: async (dir) => {
  772. await Bun.write(path.join(dir, "tmpfile"), "x")
  773. },
  774. })
  775. await Instance.provide({
  776. directory: tmp.path,
  777. fn: async () => {
  778. const bash = await initBash()
  779. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  780. await bash.execute(
  781. {
  782. command: `rm -rf ${path.join(tmp.path, "nested")}`,
  783. description: "Remove nested dir",
  784. },
  785. capture(requests),
  786. )
  787. const extDirReq = requests.find((r) => r.permission === "external_directory")
  788. expect(extDirReq).toBeUndefined()
  789. },
  790. })
  791. })
  792. each("includes always patterns for auto-approval", async () => {
  793. await using tmp = await tmpdir()
  794. await Instance.provide({
  795. directory: tmp.path,
  796. fn: async () => {
  797. const bash = await initBash()
  798. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  799. await bash.execute(
  800. {
  801. command: "git log --oneline -5",
  802. description: "Git log",
  803. },
  804. capture(requests),
  805. )
  806. expect(requests.length).toBe(1)
  807. expect(requests[0].always.length).toBeGreaterThan(0)
  808. expect(requests[0].always.some((item) => item.endsWith("*"))).toBe(true)
  809. },
  810. })
  811. })
  812. each("does not ask for bash permission when command is cd only", async () => {
  813. await using tmp = await tmpdir()
  814. await Instance.provide({
  815. directory: tmp.path,
  816. fn: async () => {
  817. const bash = await initBash()
  818. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  819. await bash.execute(
  820. {
  821. command: "cd .",
  822. description: "Stay in current directory",
  823. },
  824. capture(requests),
  825. )
  826. const bashReq = requests.find((r) => r.permission === "bash")
  827. expect(bashReq).toBeUndefined()
  828. },
  829. })
  830. })
  831. each("matches redirects in permission pattern", async () => {
  832. await using tmp = await tmpdir()
  833. await Instance.provide({
  834. directory: tmp.path,
  835. fn: async () => {
  836. const bash = await initBash()
  837. const err = new Error("stop after permission")
  838. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  839. await expect(
  840. bash.execute(
  841. { command: "echo test > output.txt", description: "Redirect test output" },
  842. capture(requests, err),
  843. ),
  844. ).rejects.toThrow(err.message)
  845. const bashReq = requests.find((r) => r.permission === "bash")
  846. expect(bashReq).toBeDefined()
  847. expect(bashReq!.patterns).toContain("echo test > output.txt")
  848. },
  849. })
  850. })
  851. each("always pattern has space before wildcard to not include different commands", async () => {
  852. await using tmp = await tmpdir()
  853. await Instance.provide({
  854. directory: tmp.path,
  855. fn: async () => {
  856. const bash = await initBash()
  857. const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
  858. await bash.execute({ command: "ls -la", description: "List" }, capture(requests))
  859. const bashReq = requests.find((r) => r.permission === "bash")
  860. expect(bashReq).toBeDefined()
  861. expect(bashReq!.always[0]).toBe("ls *")
  862. },
  863. })
  864. })
  865. })
  866. describe("tool.bash abort", () => {
  867. test("preserves output when aborted", async () => {
  868. await Instance.provide({
  869. directory: projectRoot,
  870. fn: async () => {
  871. const bash = await initBash()
  872. const controller = new AbortController()
  873. const collected: string[] = []
  874. const result = bash.execute(
  875. {
  876. command: `echo before && sleep 30`,
  877. description: "Long running command",
  878. },
  879. {
  880. ...ctx,
  881. abort: controller.signal,
  882. metadata: (input) => {
  883. const output = (input.metadata as { output?: string })?.output
  884. if (output && output.includes("before") && !controller.signal.aborted) {
  885. collected.push(output)
  886. controller.abort()
  887. }
  888. },
  889. },
  890. )
  891. const res = await result
  892. expect(res.output).toContain("before")
  893. expect(res.output).toContain("User aborted the command")
  894. expect(collected.length).toBeGreaterThan(0)
  895. },
  896. })
  897. }, 15_000)
  898. test("terminates command on timeout", async () => {
  899. await Instance.provide({
  900. directory: projectRoot,
  901. fn: async () => {
  902. const bash = await initBash()
  903. const result = await bash.execute(
  904. {
  905. command: `echo started && sleep 60`,
  906. description: "Timeout test",
  907. timeout: 500,
  908. },
  909. ctx,
  910. )
  911. expect(result.output).toContain("started")
  912. expect(result.output).toContain("bash tool terminated command after exceeding timeout")
  913. },
  914. })
  915. }, 15_000)
  916. test.skipIf(process.platform === "win32")("captures stderr in output", async () => {
  917. await Instance.provide({
  918. directory: projectRoot,
  919. fn: async () => {
  920. const bash = await initBash()
  921. const result = await bash.execute(
  922. {
  923. command: `echo stdout_msg && echo stderr_msg >&2`,
  924. description: "Stderr test",
  925. },
  926. ctx,
  927. )
  928. expect(result.output).toContain("stdout_msg")
  929. expect(result.output).toContain("stderr_msg")
  930. expect(result.metadata.exit).toBe(0)
  931. },
  932. })
  933. })
  934. test("returns non-zero exit code", async () => {
  935. await Instance.provide({
  936. directory: projectRoot,
  937. fn: async () => {
  938. const bash = await initBash()
  939. const result = await bash.execute(
  940. {
  941. command: `exit 42`,
  942. description: "Non-zero exit",
  943. },
  944. ctx,
  945. )
  946. expect(result.metadata.exit).toBe(42)
  947. },
  948. })
  949. })
  950. test("streams metadata updates progressively", async () => {
  951. await Instance.provide({
  952. directory: projectRoot,
  953. fn: async () => {
  954. const bash = await initBash()
  955. const updates: string[] = []
  956. const result = await bash.execute(
  957. {
  958. command: `echo first && sleep 0.1 && echo second`,
  959. description: "Streaming test",
  960. },
  961. {
  962. ...ctx,
  963. metadata: (input) => {
  964. const output = (input.metadata as { output?: string })?.output
  965. if (output) updates.push(output)
  966. },
  967. },
  968. )
  969. expect(result.output).toContain("first")
  970. expect(result.output).toContain("second")
  971. expect(updates.length).toBeGreaterThan(1)
  972. },
  973. })
  974. })
  975. })
  976. describe("tool.bash truncation", () => {
  977. test("truncates output exceeding line limit", async () => {
  978. await Instance.provide({
  979. directory: projectRoot,
  980. fn: async () => {
  981. const bash = await initBash()
  982. const lineCount = Truncate.MAX_LINES + 500
  983. const result = await bash.execute(
  984. {
  985. command: fill("lines", lineCount),
  986. description: "Generate lines exceeding limit",
  987. },
  988. ctx,
  989. )
  990. mustTruncate(result)
  991. expect(result.output).toContain("truncated")
  992. expect(result.output).toContain("The tool call succeeded but the output was truncated")
  993. },
  994. })
  995. })
  996. test("truncates output exceeding byte limit", async () => {
  997. await Instance.provide({
  998. directory: projectRoot,
  999. fn: async () => {
  1000. const bash = await initBash()
  1001. const byteCount = Truncate.MAX_BYTES + 10000
  1002. const result = await bash.execute(
  1003. {
  1004. command: fill("bytes", byteCount),
  1005. description: "Generate bytes exceeding limit",
  1006. },
  1007. ctx,
  1008. )
  1009. mustTruncate(result)
  1010. expect(result.output).toContain("truncated")
  1011. expect(result.output).toContain("The tool call succeeded but the output was truncated")
  1012. },
  1013. })
  1014. })
  1015. test("does not truncate small output", async () => {
  1016. await Instance.provide({
  1017. directory: projectRoot,
  1018. fn: async () => {
  1019. const bash = await initBash()
  1020. const result = await bash.execute(
  1021. {
  1022. command: "echo hello",
  1023. description: "Echo hello",
  1024. },
  1025. ctx,
  1026. )
  1027. expect((result.metadata as { truncated?: boolean }).truncated).toBe(false)
  1028. expect(result.output).toContain("hello")
  1029. },
  1030. })
  1031. })
  1032. test("full output is saved to file when truncated", async () => {
  1033. await Instance.provide({
  1034. directory: projectRoot,
  1035. fn: async () => {
  1036. const bash = await initBash()
  1037. const lineCount = Truncate.MAX_LINES + 100
  1038. const result = await bash.execute(
  1039. {
  1040. command: fill("lines", lineCount),
  1041. description: "Generate lines for file check",
  1042. },
  1043. ctx,
  1044. )
  1045. mustTruncate(result)
  1046. const filepath = (result.metadata as { outputPath?: string }).outputPath
  1047. expect(filepath).toBeTruthy()
  1048. const saved = await Filesystem.readText(filepath!)
  1049. const lines = saved.trim().split(/\r?\n/)
  1050. expect(lines.length).toBe(lineCount)
  1051. expect(lines[0]).toBe("1")
  1052. expect(lines[lineCount - 1]).toBe(String(lineCount))
  1053. },
  1054. })
  1055. })
  1056. })