tool-shell.test.ts 30 KB

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