shell.test.ts 42 KB

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