tool-shell.test.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. import fs from "fs/promises"
  2. import { realpathSync } from "node:fs"
  3. import os from "os"
  4. import path from "path"
  5. import { describe, expect } from "bun:test"
  6. import { DateTime, Deferred, Duration, Effect, Fiber, Layer, Scope, Stream } from "effect"
  7. import { Money } from "@opencode-ai/schema/money"
  8. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  9. import { LayerNode } from "@opencode-ai/util/effect/layer-node"
  10. import { makeGlobalNode } from "@opencode-ai/util/effect/app-node"
  11. import { filesystem } from "@opencode-ai/util/effect/app-node-platform"
  12. import { Database } from "@opencode-ai/core/database/database"
  13. import { Bus } from "@opencode-ai/core/bus"
  14. import { FSUtil } from "@opencode-ai/util/fs-util"
  15. import { Global } from "@opencode-ai/util/global"
  16. import { Location } from "@opencode-ai/core/location"
  17. import { LocationServiceMap } from "@opencode-ai/core/location-service-map"
  18. import { Model } from "@opencode-ai/core/model"
  19. import { Provider } from "@opencode-ai/core/provider"
  20. import { AbsolutePath } from "@opencode-ai/core/schema"
  21. import { Agent } from "@opencode-ai/core/agent"
  22. import { Job } from "@opencode-ai/core/job"
  23. import { Session } from "@opencode-ai/core/session"
  24. import { SessionEvent } from "@opencode-ai/core/session/event"
  25. import { SessionExecution } from "@opencode-ai/core/session/execution"
  26. import { SessionMessage } from "@opencode-ai/core/session/message"
  27. import { SessionStore } from "@opencode-ai/core/session/store"
  28. import { Permission } from "@opencode-ai/core/permission"
  29. import { PluginRuntime } from "@opencode-ai/core/plugin/runtime"
  30. import { Shell } from "@opencode-ai/core/shell"
  31. import { Shell as ShellSchema } from "@opencode-ai/schema/shell"
  32. import { ShellTool } from "@opencode-ai/core/tool/plugin/shell"
  33. import { ToolOutput } from "@opencode-ai/core/tool-output"
  34. import { Tool } from "@opencode-ai/core/tool"
  35. import { tmpdir } from "./fixture/tmpdir"
  36. import { testEffect } from "./lib/effect"
  37. import { toolIdentity, executeTool, toolDefinitions, waitForTool } from "./lib/tool"
  38. const sessionID = Session.ID.make("ses_shell_tool_test")
  39. const sessionModel = Model.Ref.make({ id: Model.ID.make("test"), providerID: Provider.ID.make("test") })
  40. const assertions: Permission.AssertInput[] = []
  41. let denyAction: string | undefined
  42. let afterPermission = (_input: Permission.AssertInput): Effect.Effect<void> => Effect.void
  43. const permission = Layer.succeed(
  44. Permission.Service,
  45. Permission.Service.of({
  46. assert: (input) =>
  47. Effect.sync(() => assertions.push(input)).pipe(
  48. Effect.andThen(Effect.suspend(() => afterPermission(input))),
  49. Effect.andThen(
  50. input.action === denyAction
  51. ? Effect.fail(
  52. new Permission.BlockedError({
  53. rules: [],
  54. permission: input.action,
  55. resources: input.resources,
  56. }),
  57. )
  58. : Effect.void,
  59. ),
  60. ),
  61. ask: () => Effect.die("unused"),
  62. reply: () => Effect.die("unused"),
  63. get: () => Effect.die("unused"),
  64. forSession: () => Effect.die("unused"),
  65. list: () => Effect.die("unused"),
  66. }),
  67. )
  68. const reset = () => {
  69. assertions.length = 0
  70. denyAction = undefined
  71. afterPermission = () => Effect.void
  72. }
  73. const executionNode = makeGlobalNode({
  74. service: SessionExecution.Service,
  75. layer: Layer.effect(
  76. SessionExecution.Service,
  77. Effect.gen(function* () {
  78. const bus = yield* Bus.Service
  79. const store = yield* SessionStore.Service
  80. const complete = Effect.fn("ShellTest.complete")(function* (id: Session.ID) {
  81. const session = yield* store.get(id)
  82. if (!session) return
  83. const assistantMessageID = SessionMessage.ID.create()
  84. yield* bus.publish(SessionEvent.Step.Started, {
  85. sessionID: id,
  86. assistantMessageID,
  87. agent: session.agent ?? Agent.ID.make("code"),
  88. model: sessionModel,
  89. })
  90. yield* bus.publish(SessionEvent.Text.Started, {
  91. sessionID: id,
  92. assistantMessageID,
  93. ordinal: 0,
  94. })
  95. yield* bus.publish(SessionEvent.Text.Ended, {
  96. sessionID: id,
  97. assistantMessageID,
  98. ordinal: 0,
  99. text: "ok",
  100. })
  101. yield* bus.publish(SessionEvent.Step.Ended, {
  102. sessionID: id,
  103. assistantMessageID,
  104. finish: "stop",
  105. cost: Money.USD.zero,
  106. tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
  107. })
  108. })
  109. return SessionExecution.Service.of({
  110. active: Effect.succeed(new Set()),
  111. resume: complete,
  112. wake: () => Effect.void,
  113. interrupt: () => Effect.void,
  114. awaitIdle: (id) => complete(id).pipe(Effect.exit, Effect.asVoid),
  115. })
  116. }),
  117. ),
  118. deps: [Bus.node, SessionStore.node],
  119. })
  120. const layer = AppNodeBuilder.build(
  121. LayerNode.group([
  122. Database.node,
  123. Bus.node,
  124. Job.node,
  125. Session.node,
  126. SessionExecution.node,
  127. PluginRuntime.providerNode,
  128. LocationServiceMap.node,
  129. filesystem,
  130. FSUtil.node,
  131. Global.node,
  132. ]),
  133. [
  134. [SessionExecution.node, executionNode],
  135. [Permission.node, permission],
  136. ],
  137. )
  138. const it = testEffect(layer)
  139. const call = (input: typeof ShellTool.Input.Type, id = "call-shell") => ({
  140. sessionID,
  141. ...toolIdentity,
  142. call: { type: "tool-call" as const, id, name: "shell", input },
  143. })
  144. const isWindows = process.platform === "win32"
  145. const cwdCommand = isWindows ? "(Get-Location).Path; Start-Sleep -Milliseconds 100" : "pwd"
  146. const helloCommand = isWindows ? "[Console]::Out.Write('hello'); Start-Sleep -Milliseconds 100" : "printf hello"
  147. const stderrCommand = isWindows
  148. ? "[Console]::Error.Write('stderr only'); Start-Sleep -Milliseconds 100"
  149. : "printf 'stderr only' >&2"
  150. const mixedOutputCommand = isWindows
  151. ? "[Console]::Out.Write('stdout'); Start-Sleep -Milliseconds 50; [Console]::Error.Write('stderr'); Start-Sleep -Milliseconds 100"
  152. : "printf stdout; sleep 0.05; printf stderr >&2"
  153. const idleCommand = isWindows ? "Start-Sleep -Seconds 60" : "sleep 60"
  154. const timeoutOutputCommand = isWindows
  155. ? "[Console]::Out.Write('before timeout'); Start-Sleep -Seconds 60"
  156. : "printf 'before timeout'; sleep 60"
  157. const steadyProgressCommand = isWindows
  158. ? "[Console]::Out.Write('steady'); Start-Sleep -Milliseconds 3400"
  159. : "printf steady; sleep 3.4"
  160. const bodyExitCommand = isWindows
  161. ? "[Console]::Out.Write('body'); Start-Sleep -Milliseconds 100; exit 7"
  162. : "printf body && exit 7"
  163. const overflowCommand = (bytes: number) =>
  164. isWindows
  165. ? `[Console]::Out.Write('output-start' + ('x' * ${bytes}) + 'output-end'); Start-Sleep -Milliseconds 100`
  166. : `printf output-start; head -c ${bytes} /dev/zero | tr '\\0' 'x'; printf output-end`
  167. const lineOverflowCommand = isWindows
  168. ? "[Console]::Out.Write('one' + [Environment]::NewLine + 'two' + [Environment]::NewLine + 'three')"
  169. : "printf 'one\\ntwo\\nthree'"
  170. const progressOverflowCommand = (bytes: number, release: string) =>
  171. isWindows
  172. ? `[Console]::Out.Write(('x' * ${bytes})); while (!(Test-Path -LiteralPath '${release}')) { Start-Sleep -Milliseconds 50 }`
  173. : `head -c ${bytes} /dev/zero | tr '\\0' 'x'; while [ ! -e '${release}' ]; do sleep 0.05; done`
  174. const withSession = <A, E, R>(directory: string, body: (registry: Tool.Interface) => Effect.Effect<A, E, R>) =>
  175. Effect.gen(function* () {
  176. const sessions = yield* Session.Service
  177. const location = Location.Ref.make({ directory: AbsolutePath.make(directory) })
  178. yield* sessions.create({
  179. id: sessionID,
  180. title: "shell test",
  181. location,
  182. model: sessionModel,
  183. })
  184. const locations = yield* LocationServiceMap.Service
  185. const locationLayer = locations.get(location)
  186. return yield* Effect.gen(function* () {
  187. const registry = yield* Tool.Service
  188. yield* waitForTool(registry, ShellTool.name)
  189. return yield* body(registry)
  190. }).pipe(Effect.provide(locationLayer), Effect.ensuring(locations.invalidate(location)))
  191. })
  192. describe("ShellTool", () => {
  193. it.live("registers and returns real successful output from the active Location", () =>
  194. Effect.acquireUseRelease(
  195. Effect.promise(() => tmpdir()),
  196. (tmp) => {
  197. reset()
  198. return withSession(tmp.path, (registry) =>
  199. Effect.gen(function* () {
  200. const definitions = yield* toolDefinitions(registry)
  201. const definition = definitions.find((tool) => tool.name === "shell")
  202. expect(definition?.description).toStartWith("Execute a shell command and return its output.")
  203. expect(definition?.inputSchema).not.toHaveProperty("properties.timeout.maximum")
  204. // Code Mode receives the declared output schema, including the command output text.
  205. expect(definition?.outputSchema).toHaveProperty("properties.output")
  206. expect(
  207. (yield* toolDefinitions(registry, [{ action: "shell", resource: "*", effect: "deny" }])).map(
  208. (tool) => tool.name,
  209. ),
  210. ).not.toContain("shell")
  211. const settled = yield* executeTool(registry, call({ command: helloCommand }))
  212. expect(settled.status).toBe("completed")
  213. expect(settled.metadata).toMatchObject({ exit: 0, truncated: false })
  214. expect(settled.content?.[0]).toEqual({ type: "text", text: "hello" })
  215. expect(settled.content?.[1]).toMatchObject({
  216. type: "text",
  217. text: expect.stringContaining("Command exited with code 0."),
  218. })
  219. expect(assertions).toMatchObject([
  220. { sessionID, action: "shell", resources: [isWindows ? "Start-Sleep -Milliseconds 100" : helloCommand] },
  221. ])
  222. expect(assertions[0]?.save).toEqual([isWindows ? "Start-Sleep *" : "printf *"])
  223. }),
  224. )
  225. },
  226. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  227. ),
  228. )
  229. it.live("resolves a relative workdir from the active Location", () =>
  230. Effect.acquireUseRelease(
  231. Effect.promise(() => tmpdir()),
  232. (tmp) => {
  233. reset()
  234. return Effect.promise(() => fs.mkdir(path.join(tmp.path, "src"))).pipe(
  235. Effect.andThen(
  236. withSession(tmp.path, (registry) => executeTool(registry, call({ command: cwdCommand, workdir: "src" }))),
  237. ),
  238. Effect.andThen((settled) =>
  239. Effect.sync(() =>
  240. expect(settled.content?.[0]).toMatchObject({
  241. type: "text",
  242. text: expect.stringContaining(realpathSync(path.join(tmp.path, "src"))),
  243. }),
  244. ),
  245. ),
  246. )
  247. },
  248. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  249. ),
  250. )
  251. it.live("reports a missing workdir", () =>
  252. Effect.acquireUseRelease(
  253. Effect.promise(() => tmpdir()),
  254. (tmp) => {
  255. reset()
  256. return withSession(tmp.path, (registry) =>
  257. executeTool(registry, call({ command: cwdCommand, workdir: "missing" })),
  258. ).pipe(
  259. Effect.andThen((settled) =>
  260. Effect.sync(() =>
  261. expect(settled).toEqual({
  262. status: "error",
  263. error: {
  264. type: "unknown",
  265. message: `Working directory does not exist: ${path.join(tmp.path, "missing")}`,
  266. },
  267. }),
  268. ),
  269. ),
  270. )
  271. },
  272. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  273. ),
  274. )
  275. it.live("permissions compound commands separately", () =>
  276. Effect.acquireUseRelease(
  277. Effect.promise(() => tmpdir()),
  278. (tmp) => {
  279. reset()
  280. return withSession(tmp.path, (registry) =>
  281. executeTool(registry, call({ command: "printf one && printf two" }, "call-compound")),
  282. ).pipe(
  283. Effect.andThen(
  284. Effect.sync(() => {
  285. expect(assertions).toHaveLength(1)
  286. expect(assertions[0]).toMatchObject({
  287. resources: ["printf one", "printf two"],
  288. save: ["printf *", "printf *"],
  289. })
  290. }),
  291. ),
  292. )
  293. },
  294. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  295. ),
  296. )
  297. it.live(
  298. "captures stderr-only and mixed stdout/stderr output",
  299. () =>
  300. Effect.acquireUseRelease(
  301. Effect.promise(() => tmpdir()),
  302. (tmp) => {
  303. reset()
  304. return withSession(tmp.path, (registry) =>
  305. Effect.gen(function* () {
  306. const stderr = yield* executeTool(registry, call({ command: stderrCommand }, "call-stderr"))
  307. expect(stderr.metadata).toMatchObject({ exit: 0, truncated: false })
  308. expect(stderr.content?.[0]).toEqual({ type: "text", text: "stderr only" })
  309. const mixed = yield* executeTool(registry, call({ command: mixedOutputCommand }, "call-mixed"))
  310. expect(mixed.metadata).toMatchObject({ exit: 0, truncated: false })
  311. const output = mixed.content?.[0]?.type === "text" ? mixed.content[0].text : ""
  312. expect(output).toContain("stdout")
  313. expect(output).toContain("stderr")
  314. }),
  315. )
  316. },
  317. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  318. ),
  319. { timeout: 15_000 },
  320. )
  321. it.live("rejects a workdir that stops being a directory during approval", () =>
  322. Effect.acquireUseRelease(
  323. Effect.promise(() => tmpdir()),
  324. (tmp) => {
  325. reset()
  326. const workdir = path.join(tmp.path, "src")
  327. afterPermission = (input) =>
  328. input.action === "shell"
  329. ? Effect.promise(async () => {
  330. await fs.rm(workdir, { recursive: true })
  331. await fs.writeFile(workdir, "not a directory")
  332. }).pipe(Effect.orDie)
  333. : Effect.void
  334. return Effect.promise(() => fs.mkdir(workdir)).pipe(
  335. Effect.andThen(
  336. withSession(tmp.path, (registry) => executeTool(registry, call({ command: cwdCommand, workdir: "src" }))),
  337. ),
  338. Effect.andThen(Effect.sync(() => expect(assertions.map((input) => input.action)).toEqual(["shell"]))),
  339. )
  340. },
  341. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  342. ),
  343. )
  344. it.live("approves an explicit external workdir before shell execution", () =>
  345. Effect.acquireUseRelease(
  346. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  347. ([active, outside]) => {
  348. reset()
  349. return withSession(active.path, (registry) =>
  350. executeTool(registry, call({ command: cwdCommand, workdir: outside.path })),
  351. ).pipe(
  352. Effect.andThen(
  353. Effect.sync(() => {
  354. expect(assertions.map((item) => item.action)).toEqual(["external_directory", "shell"])
  355. expect(assertions[0]).toMatchObject({
  356. resources: [path.join(realpathSync(outside.path), "*").replaceAll("\\", "/")],
  357. })
  358. }),
  359. ),
  360. )
  361. },
  362. ([active, outside]) =>
  363. Effect.promise(() =>
  364. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  365. ),
  366. ),
  367. )
  368. it.live("approves an external directory used by a directory-change command", () =>
  369. Effect.acquireUseRelease(
  370. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  371. ([active, outside]) => {
  372. reset()
  373. const command = isWindows
  374. ? `Set-Location -LiteralPath '${outside.path}'; (Get-Location).Path`
  375. : `cd '${outside.path}' && pwd`
  376. return withSession(active.path, (registry) =>
  377. executeTool(registry, call({ command }, "call-external-cd")),
  378. ).pipe(
  379. Effect.andThen(
  380. Effect.sync(() => {
  381. expect(assertions.map((item) => item.action)).toEqual(["external_directory", "shell"])
  382. expect(assertions[0]).toMatchObject({
  383. resources: [path.join(realpathSync(outside.path), "*").replaceAll("\\", "/")],
  384. })
  385. }),
  386. ),
  387. )
  388. },
  389. ([active, outside]) =>
  390. Effect.promise(() =>
  391. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  392. ),
  393. ),
  394. )
  395. it.live("approves an expanded external home directory", () =>
  396. Effect.acquireUseRelease(
  397. Effect.promise(() => tmpdir()),
  398. (tmp) => {
  399. reset()
  400. const command = isWindows ? "Set-Location $HOME; (Get-Location).Path" : "cd ~ && pwd"
  401. return withSession(tmp.path, (registry) => executeTool(registry, call({ command }, "call-external-home"))).pipe(
  402. Effect.andThen(
  403. Effect.sync(() => {
  404. expect(assertions.map((item) => item.action)).toEqual(["external_directory", "shell"])
  405. expect(assertions[0]?.resources[0]).toStartWith(os.homedir().replaceAll("\\", "/"))
  406. }),
  407. ),
  408. )
  409. },
  410. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  411. ),
  412. )
  413. it.live("does not execute after external-directory or shell denial", () =>
  414. Effect.acquireUseRelease(
  415. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  416. ([active, outside]) =>
  417. Effect.gen(function* () {
  418. reset()
  419. denyAction = "external_directory"
  420. yield* withSession(active.path, (registry) =>
  421. executeTool(registry, call({ command: cwdCommand, workdir: outside.path })),
  422. )
  423. expect(assertions.map((item) => item.action)).toEqual(["external_directory"])
  424. reset()
  425. denyAction = "shell"
  426. yield* withSession(active.path, (registry) => executeTool(registry, call({ command: cwdCommand })))
  427. expect(assertions.map((item) => item.action)).toEqual(["shell"])
  428. }),
  429. ([active, outside]) =>
  430. Effect.promise(() =>
  431. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  432. ),
  433. ),
  434. )
  435. it.live("keeps non-zero exits useful", () =>
  436. Effect.acquireUseRelease(
  437. Effect.promise(() => tmpdir()),
  438. (tmp) => {
  439. reset()
  440. return withSession(tmp.path, (registry) =>
  441. executeTool(registry, call({ command: bodyExitCommand }, "call-nonzero")),
  442. ).pipe(
  443. Effect.andThen((settled) =>
  444. Effect.sync(() => {
  445. expect(settled.status).toBe("completed")
  446. expect(settled.metadata).toMatchObject({ exit: 7, truncated: false })
  447. expect(settled.content?.[0]).toEqual({ type: "text", text: "body" })
  448. expect(settled.content?.[1]).toMatchObject({
  449. type: "text",
  450. text: expect.stringContaining("Command exited with code 7"),
  451. })
  452. }),
  453. ),
  454. )
  455. },
  456. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  457. ),
  458. )
  459. it.live(
  460. "truncates the model view and points at the saved output file when output overflows",
  461. () =>
  462. Effect.acquireUseRelease(
  463. Effect.promise(() => tmpdir()),
  464. (tmp) => {
  465. reset()
  466. const bytes = ToolOutput.MAX_BYTES + 1024
  467. return withSession(tmp.path, (registry) =>
  468. executeTool(registry, call({ command: overflowCommand(bytes) }, "call-overflow")),
  469. ).pipe(
  470. Effect.andThen((settled) =>
  471. Effect.sync(() => {
  472. expect(settled.metadata).toMatchObject({ exit: 0, truncated: true })
  473. const content = settled.content?.[0]
  474. if (!content || content.type !== "text") throw new Error("Expected text content")
  475. expect(content.text.includes("output-start")).toBe(false)
  476. expect(content.text.includes("output-end")).toBe(true)
  477. expect(content).toMatchObject({
  478. type: "text",
  479. text: expect.stringContaining("output truncated; full output saved to:"),
  480. })
  481. }),
  482. ),
  483. )
  484. },
  485. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  486. ),
  487. { timeout: 15_000 },
  488. )
  489. it.live("uses configured line limits", () =>
  490. Effect.acquireUseRelease(
  491. Effect.promise(() => tmpdir()),
  492. (tmp) => {
  493. reset()
  494. return Effect.gen(function* () {
  495. yield* Effect.promise(() =>
  496. Bun.write(
  497. path.join(tmp.path, "opencode.json"),
  498. JSON.stringify({ tool_output: { max_lines: 2, max_bytes: 1_000 } }),
  499. ),
  500. )
  501. const settled = yield* withSession(tmp.path, (registry) =>
  502. executeTool(registry, call({ command: lineOverflowCommand }, "call-line-overflow")),
  503. )
  504. expect(settled.metadata).toMatchObject({ exit: 0, truncated: true })
  505. const content = settled.content?.[0]
  506. if (!content || content.type !== "text") throw new Error("Expected text content")
  507. expect(content.text).not.toContain("one")
  508. // Windows shells emit CRLF; the assertion targets line limits, not line endings.
  509. expect(content.text.replaceAll("\r\n", "\n")).toStartWith("two\nthree")
  510. expect(content.text).toContain("output truncated; full output saved to:")
  511. })
  512. },
  513. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  514. ),
  515. )
  516. it.live(
  517. "reports the shell ID for a running command",
  518. () =>
  519. Effect.acquireUseRelease(
  520. Effect.promise(() => tmpdir()),
  521. (tmp) => {
  522. reset()
  523. const release = "shell-progress-release"
  524. const releasePath = path.join(tmp.path, release)
  525. return withSession(tmp.path, (registry) =>
  526. Effect.gen(function* () {
  527. const observed = yield* Deferred.make<string>()
  528. yield* executeTool(registry, {
  529. ...call({ command: progressOverflowCommand(ToolOutput.MAX_BYTES + 1024, release) }, "call-progress"),
  530. progress: (update) =>
  531. Effect.gen(function* () {
  532. if (typeof update.shellID !== "string") return
  533. yield* Deferred.succeed(observed, update.shellID)
  534. yield* Effect.promise(() => fs.writeFile(releasePath, ""))
  535. }),
  536. })
  537. expect(yield* Deferred.await(observed)).toMatch(/^sh_/)
  538. }).pipe(Effect.ensuring(Effect.promise(() => fs.writeFile(releasePath, "")).pipe(Effect.ignore))),
  539. )
  540. },
  541. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  542. ),
  543. { timeout: 15_000 },
  544. )
  545. it.live(
  546. "does not repeat shell ID progress",
  547. () =>
  548. Effect.acquireUseRelease(
  549. Effect.promise(() => tmpdir()),
  550. (tmp) => {
  551. reset()
  552. return withSession(tmp.path, (registry) =>
  553. Effect.gen(function* () {
  554. const updates: Tool.Metadata[] = []
  555. yield* executeTool(registry, {
  556. ...call({ command: steadyProgressCommand }, "call-steady-progress"),
  557. progress: (update) => Effect.sync(() => updates.push(update)),
  558. })
  559. expect(updates).toHaveLength(1)
  560. expect(updates[0]?.shellID).toMatch(/^sh_/)
  561. }),
  562. )
  563. },
  564. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  565. ),
  566. { timeout: 10_000 },
  567. )
  568. it.live(
  569. "returns a useful timeout outcome",
  570. () =>
  571. Effect.acquireUseRelease(
  572. Effect.promise(() => tmpdir()),
  573. (tmp) => {
  574. reset()
  575. return withSession(tmp.path, (registry) =>
  576. executeTool(registry, call({ command: timeoutOutputCommand, timeout: isWindows ? 3_000 : 50 })),
  577. ).pipe(
  578. Effect.andThen((settled) =>
  579. Effect.sync(() => {
  580. expect(settled.metadata).toMatchObject({ timeout: true, truncated: false })
  581. expect(settled.content?.[0]).toMatchObject({
  582. type: "text",
  583. text: expect.stringContaining("before timeout"),
  584. })
  585. expect(settled.content?.[1]).toMatchObject({
  586. type: "text",
  587. text: expect.stringContaining("Command timed out"),
  588. })
  589. }),
  590. ),
  591. )
  592. },
  593. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  594. ),
  595. { timeout: 15_000 },
  596. )
  597. it.live("returns the shell id for a background command", () =>
  598. Effect.acquireUseRelease(
  599. Effect.promise(() => tmpdir()),
  600. (tmp) => {
  601. reset()
  602. return withSession(tmp.path, (registry) =>
  603. Effect.gen(function* () {
  604. const bus = yield* Bus.Service
  605. const admitted = yield* bus.subscribe(SessionEvent.InputAdmitted).pipe(
  606. Stream.filter((event) => event.data.sessionID === sessionID && event.data.input.type === "synthetic"),
  607. Stream.runHead,
  608. Effect.forkScoped({ startImmediately: true }),
  609. )
  610. const settled = yield* executeTool(registry, call({ command: idleCommand, timeout: 50, background: true }))
  611. const shellID = typeof settled.metadata?.shellID === "string" ? settled.metadata.shellID : undefined
  612. expect(settled.metadata).toMatchObject({ truncated: false })
  613. expect(shellID).toStartWith("sh_")
  614. const shell = yield* Shell.Service
  615. if (!shellID) return
  616. const id = ShellSchema.ID.make(shellID)
  617. expect((yield* shell.list()).map((info) => info.id)).toContain(id)
  618. expect((yield* shell.wait(id)).status).toBe("timeout")
  619. expect((yield* Fiber.join(admitted)).valueOrUndefined?.data.input.data).toMatchObject({
  620. description: idleCommand,
  621. metadata: {
  622. source: "shell",
  623. state: "completed",
  624. },
  625. })
  626. }),
  627. )
  628. },
  629. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  630. ),
  631. )
  632. it.live("updates and clears a running shell timeout", () =>
  633. Effect.acquireUseRelease(
  634. Effect.promise(() => tmpdir()),
  635. (tmp) => {
  636. reset()
  637. return withSession(tmp.path, (registry) =>
  638. Effect.gen(function* () {
  639. const shell = yield* Shell.Service
  640. const timed = yield* executeTool(
  641. registry,
  642. call({ command: idleCommand, background: true }, "call-updated-timeout"),
  643. )
  644. const timedID = timed.metadata?.shellID
  645. expect(typeof timedID).toBe("string")
  646. if (typeof timedID !== "string") return
  647. const timedShellID = ShellSchema.ID.make(timedID)
  648. yield* shell.timeout(timedShellID, 50)
  649. expect((yield* shell.wait(timedShellID)).status).toBe("timeout")
  650. const cleared = yield* executeTool(
  651. registry,
  652. call({ command: idleCommand, timeout: 50, background: true }, "call-cleared-timeout"),
  653. )
  654. const clearedID = cleared.metadata?.shellID
  655. expect(typeof clearedID).toBe("string")
  656. if (typeof clearedID !== "string") return
  657. const clearedShellID = ShellSchema.ID.make(clearedID)
  658. yield* shell.timeout(clearedShellID, 0)
  659. yield* Effect.sleep(Duration.millis(100))
  660. expect((yield* shell.get(clearedShellID)).status).toBe("running")
  661. yield* shell.remove(clearedShellID)
  662. }),
  663. )
  664. },
  665. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  666. ),
  667. )
  668. it.live("backgrounds a foreground command when the session is signaled", () =>
  669. Effect.acquireUseRelease(
  670. Effect.promise(() => tmpdir()),
  671. (tmp) => {
  672. reset()
  673. return withSession(tmp.path, (registry) =>
  674. Effect.gen(function* () {
  675. const jobs = yield* Job.Service
  676. const scope = yield* Scope.Scope
  677. const waiting = yield* executeTool(
  678. registry,
  679. call({ command: idleCommand, timeout: 50 }, "call-background-signal"),
  680. ).pipe(Effect.forkIn(scope, { startImmediately: true }))
  681. const backgroundWhenReady = (remaining = 1000): Effect.Effect<Job.Info[], Error> =>
  682. Effect.gen(function* () {
  683. const backgrounded = yield* jobs.backgroundAll({ sessionID })
  684. if (backgrounded.length > 0) return backgrounded
  685. if (remaining <= 0) return yield* Effect.fail(new Error("Timed out waiting for foreground shell job"))
  686. yield* Effect.promise(() => Bun.sleep(1))
  687. return yield* backgroundWhenReady(remaining - 1)
  688. })
  689. expect(yield* backgroundWhenReady()).toMatchObject([{ id: "call-background-signal", type: "shell" }])
  690. const settled = yield* Fiber.join(waiting)
  691. const shellID = typeof settled.metadata?.shellID === "string" ? settled.metadata.shellID : undefined
  692. expect(settled.metadata).toMatchObject({ truncated: false })
  693. expect(settled.content?.[0]).toEqual({
  694. type: "text",
  695. text: "The command was moved to the background.",
  696. })
  697. expect(settled.content?.[1]).toMatchObject({
  698. type: "text",
  699. text: expect.stringContaining("DO NOT sleep, poll"),
  700. })
  701. expect(shellID).toStartWith("sh_")
  702. const shell = yield* Shell.Service
  703. if (!shellID) return
  704. const id = ShellSchema.ID.make(shellID)
  705. yield* Effect.sleep(Duration.millis(100))
  706. expect((yield* shell.get(id)).status).toBe("running")
  707. expect((yield* shell.list()).map((info) => info.id)).toContain(id)
  708. yield* shell.remove(id)
  709. }),
  710. )
  711. },
  712. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  713. ),
  714. )
  715. })