tool-shell.test.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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("captures stderr-only and mixed stdout/stderr output", () =>
  298. Effect.acquireUseRelease(
  299. Effect.promise(() => tmpdir()),
  300. (tmp) => {
  301. reset()
  302. return withSession(tmp.path, (registry) =>
  303. Effect.gen(function* () {
  304. const stderr = yield* executeTool(registry, call({ command: stderrCommand }, "call-stderr"))
  305. expect(stderr.metadata).toMatchObject({ exit: 0, truncated: false })
  306. expect(stderr.content?.[0]).toEqual({ type: "text", text: "stderr only" })
  307. const mixed = yield* executeTool(registry, call({ command: mixedOutputCommand }, "call-mixed"))
  308. expect(mixed.metadata).toMatchObject({ exit: 0, truncated: false })
  309. const output = mixed.content?.[0]?.type === "text" ? mixed.content[0].text : ""
  310. expect(output).toContain("stdout")
  311. expect(output).toContain("stderr")
  312. }),
  313. )
  314. },
  315. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  316. ),
  317. { timeout: 15_000 },
  318. )
  319. it.live("rejects a workdir that stops being a directory during approval", () =>
  320. Effect.acquireUseRelease(
  321. Effect.promise(() => tmpdir()),
  322. (tmp) => {
  323. reset()
  324. const workdir = path.join(tmp.path, "src")
  325. afterPermission = (input) =>
  326. input.action === "shell"
  327. ? Effect.promise(async () => {
  328. await fs.rm(workdir, { recursive: true })
  329. await fs.writeFile(workdir, "not a directory")
  330. }).pipe(Effect.orDie)
  331. : Effect.void
  332. return Effect.promise(() => fs.mkdir(workdir)).pipe(
  333. Effect.andThen(
  334. withSession(tmp.path, (registry) => executeTool(registry, call({ command: cwdCommand, workdir: "src" }))),
  335. ),
  336. Effect.andThen(Effect.sync(() => expect(assertions.map((input) => input.action)).toEqual(["shell"]))),
  337. )
  338. },
  339. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  340. ),
  341. )
  342. it.live("approves an explicit external workdir before shell execution", () =>
  343. Effect.acquireUseRelease(
  344. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  345. ([active, outside]) => {
  346. reset()
  347. return withSession(active.path, (registry) =>
  348. executeTool(registry, call({ command: cwdCommand, workdir: outside.path })),
  349. ).pipe(
  350. Effect.andThen(
  351. Effect.sync(() => {
  352. expect(assertions.map((item) => item.action)).toEqual(["external_directory", "shell"])
  353. expect(assertions[0]).toMatchObject({
  354. resources: [path.join(realpathSync(outside.path), "*").replaceAll("\\", "/")],
  355. })
  356. }),
  357. ),
  358. )
  359. },
  360. ([active, outside]) =>
  361. Effect.promise(() =>
  362. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  363. ),
  364. ),
  365. )
  366. it.live("approves an external directory used by a directory-change command", () =>
  367. Effect.acquireUseRelease(
  368. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  369. ([active, outside]) => {
  370. reset()
  371. const command = isWindows
  372. ? `Set-Location -LiteralPath '${outside.path}'; (Get-Location).Path`
  373. : `cd '${outside.path}' && pwd`
  374. return withSession(active.path, (registry) => executeTool(registry, call({ command }, "call-external-cd"))).pipe(
  375. Effect.andThen(
  376. Effect.sync(() => {
  377. expect(assertions.map((item) => item.action)).toEqual(["external_directory", "shell"])
  378. expect(assertions[0]).toMatchObject({
  379. resources: [path.join(realpathSync(outside.path), "*").replaceAll("\\", "/")],
  380. })
  381. }),
  382. ),
  383. )
  384. },
  385. ([active, outside]) =>
  386. Effect.promise(() =>
  387. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  388. ),
  389. ),
  390. )
  391. it.live("approves an expanded external home directory", () =>
  392. Effect.acquireUseRelease(
  393. Effect.promise(() => tmpdir()),
  394. (tmp) => {
  395. reset()
  396. const command = isWindows ? "Set-Location $HOME; (Get-Location).Path" : "cd ~ && pwd"
  397. return withSession(tmp.path, (registry) => executeTool(registry, call({ command }, "call-external-home"))).pipe(
  398. Effect.andThen(
  399. Effect.sync(() => {
  400. expect(assertions.map((item) => item.action)).toEqual(["external_directory", "shell"])
  401. expect(assertions[0]?.resources[0]).toStartWith(os.homedir().replaceAll("\\", "/"))
  402. }),
  403. ),
  404. )
  405. },
  406. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  407. ),
  408. )
  409. it.live("does not execute after external-directory or shell denial", () =>
  410. Effect.acquireUseRelease(
  411. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  412. ([active, outside]) =>
  413. Effect.gen(function* () {
  414. reset()
  415. denyAction = "external_directory"
  416. yield* withSession(active.path, (registry) =>
  417. executeTool(registry, call({ command: cwdCommand, workdir: outside.path })),
  418. )
  419. expect(assertions.map((item) => item.action)).toEqual(["external_directory"])
  420. reset()
  421. denyAction = "shell"
  422. yield* withSession(active.path, (registry) => executeTool(registry, call({ command: cwdCommand })))
  423. expect(assertions.map((item) => item.action)).toEqual(["shell"])
  424. }),
  425. ([active, outside]) =>
  426. Effect.promise(() =>
  427. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  428. ),
  429. ),
  430. )
  431. it.live("keeps non-zero exits useful", () =>
  432. Effect.acquireUseRelease(
  433. Effect.promise(() => tmpdir()),
  434. (tmp) => {
  435. reset()
  436. return withSession(tmp.path, (registry) =>
  437. executeTool(registry, call({ command: bodyExitCommand }, "call-nonzero")),
  438. ).pipe(
  439. Effect.andThen((settled) =>
  440. Effect.sync(() => {
  441. expect(settled.status).toBe("completed")
  442. expect(settled.metadata).toMatchObject({ exit: 7, truncated: false })
  443. expect(settled.content?.[0]).toEqual({ type: "text", text: "body" })
  444. expect(settled.content?.[1]).toMatchObject({
  445. type: "text",
  446. text: expect.stringContaining("Command exited with code 7"),
  447. })
  448. }),
  449. ),
  450. )
  451. },
  452. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  453. ),
  454. )
  455. it.live("truncates the model view and points at the saved output file when output overflows", () =>
  456. Effect.acquireUseRelease(
  457. Effect.promise(() => tmpdir()),
  458. (tmp) => {
  459. reset()
  460. const bytes = ToolOutput.MAX_BYTES + 1024
  461. return withSession(tmp.path, (registry) =>
  462. executeTool(registry, call({ command: overflowCommand(bytes) }, "call-overflow")),
  463. ).pipe(
  464. Effect.andThen((settled) =>
  465. Effect.sync(() => {
  466. expect(settled.metadata).toMatchObject({ exit: 0, truncated: true })
  467. const content = settled.content?.[0]
  468. if (!content || content.type !== "text") throw new Error("Expected text content")
  469. expect(content.text.includes("output-start")).toBe(false)
  470. expect(content.text.includes("output-end")).toBe(true)
  471. expect(content).toMatchObject({
  472. type: "text",
  473. text: expect.stringContaining("output truncated; full output saved to:"),
  474. })
  475. }),
  476. ),
  477. )
  478. },
  479. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  480. ),
  481. { timeout: 15_000 },
  482. )
  483. it.live("uses configured line limits", () =>
  484. Effect.acquireUseRelease(
  485. Effect.promise(() => tmpdir()),
  486. (tmp) => {
  487. reset()
  488. return Effect.gen(function* () {
  489. yield* Effect.promise(() =>
  490. Bun.write(
  491. path.join(tmp.path, "opencode.json"),
  492. JSON.stringify({ tool_output: { max_lines: 2, max_bytes: 1_000 } }),
  493. ),
  494. )
  495. const settled = yield* withSession(tmp.path, (registry) =>
  496. executeTool(registry, call({ command: lineOverflowCommand }, "call-line-overflow")),
  497. )
  498. expect(settled.metadata).toMatchObject({ exit: 0, truncated: true })
  499. const content = settled.content?.[0]
  500. if (!content || content.type !== "text") throw new Error("Expected text content")
  501. expect(content.text).not.toContain("one")
  502. // Windows shells emit CRLF; the assertion targets line limits, not line endings.
  503. expect(content.text.replaceAll("\r\n", "\n")).toStartWith("two\nthree")
  504. expect(content.text).toContain("output truncated; full output saved to:")
  505. })
  506. },
  507. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  508. ),
  509. )
  510. it.live(
  511. "reports the shell ID for a running command",
  512. () =>
  513. Effect.acquireUseRelease(
  514. Effect.promise(() => tmpdir()),
  515. (tmp) => {
  516. reset()
  517. const release = "shell-progress-release"
  518. const releasePath = path.join(tmp.path, release)
  519. return withSession(tmp.path, (registry) =>
  520. Effect.gen(function* () {
  521. const observed = yield* Deferred.make<string>()
  522. yield* executeTool(registry, {
  523. ...call(
  524. { command: progressOverflowCommand(ToolOutput.MAX_BYTES + 1024, release) },
  525. "call-progress",
  526. ),
  527. progress: (update) =>
  528. Effect.gen(function* () {
  529. if (typeof update.shellID !== "string") return
  530. yield* Deferred.succeed(observed, update.shellID)
  531. yield* Effect.promise(() => fs.writeFile(releasePath, ""))
  532. }),
  533. })
  534. expect(yield* Deferred.await(observed)).toMatch(/^sh_/)
  535. }).pipe(Effect.ensuring(Effect.promise(() => fs.writeFile(releasePath, "")).pipe(Effect.ignore))),
  536. )
  537. },
  538. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  539. ),
  540. { timeout: 15_000 },
  541. )
  542. it.live(
  543. "does not repeat shell ID progress",
  544. () =>
  545. Effect.acquireUseRelease(
  546. Effect.promise(() => tmpdir()),
  547. (tmp) => {
  548. reset()
  549. return withSession(tmp.path, (registry) =>
  550. Effect.gen(function* () {
  551. const updates: Tool.Metadata[] = []
  552. yield* executeTool(registry, {
  553. ...call({ command: steadyProgressCommand }, "call-steady-progress"),
  554. progress: (update) => Effect.sync(() => updates.push(update)),
  555. })
  556. expect(updates).toHaveLength(1)
  557. expect(updates[0]?.shellID).toMatch(/^sh_/)
  558. }),
  559. )
  560. },
  561. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  562. ),
  563. { timeout: 10_000 },
  564. )
  565. it.live("returns a useful timeout outcome", () =>
  566. Effect.acquireUseRelease(
  567. Effect.promise(() => tmpdir()),
  568. (tmp) => {
  569. reset()
  570. return withSession(tmp.path, (registry) =>
  571. executeTool(registry, call({ command: timeoutOutputCommand, timeout: isWindows ? 3_000 : 50 })),
  572. ).pipe(
  573. Effect.andThen((settled) =>
  574. Effect.sync(() => {
  575. expect(settled.metadata).toMatchObject({ timeout: true, truncated: false })
  576. expect(settled.content?.[0]).toMatchObject({
  577. type: "text",
  578. text: expect.stringContaining("before timeout"),
  579. })
  580. expect(settled.content?.[1]).toMatchObject({
  581. type: "text",
  582. text: expect.stringContaining("Command timed out"),
  583. })
  584. }),
  585. ),
  586. )
  587. },
  588. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  589. ),
  590. { timeout: 15_000 },
  591. )
  592. it.live("returns the shell id for a background command", () =>
  593. Effect.acquireUseRelease(
  594. Effect.promise(() => tmpdir()),
  595. (tmp) => {
  596. reset()
  597. return withSession(tmp.path, (registry) =>
  598. Effect.gen(function* () {
  599. const bus = yield* Bus.Service
  600. const admitted = yield* bus.subscribe(SessionEvent.InputAdmitted).pipe(
  601. Stream.filter((event) => event.data.sessionID === sessionID && event.data.input.type === "synthetic"),
  602. Stream.runHead,
  603. Effect.forkScoped({ startImmediately: true }),
  604. )
  605. const settled = yield* executeTool(registry, call({ command: idleCommand, timeout: 50, background: true }))
  606. const shellID = typeof settled.metadata?.shellID === "string" ? settled.metadata.shellID : undefined
  607. expect(settled.metadata).toMatchObject({ truncated: false })
  608. expect(shellID).toStartWith("sh_")
  609. const shell = yield* Shell.Service
  610. if (!shellID) return
  611. const id = ShellSchema.ID.make(shellID)
  612. expect((yield* shell.list()).map((info) => info.id)).toContain(id)
  613. expect((yield* shell.wait(id)).status).toBe("timeout")
  614. expect((yield* Fiber.join(admitted)).valueOrUndefined?.data.input.data).toMatchObject({
  615. description: idleCommand,
  616. metadata: {
  617. source: "shell",
  618. state: "completed",
  619. },
  620. })
  621. }),
  622. )
  623. },
  624. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  625. ),
  626. )
  627. it.live("updates and clears a running shell timeout", () =>
  628. Effect.acquireUseRelease(
  629. Effect.promise(() => tmpdir()),
  630. (tmp) => {
  631. reset()
  632. return withSession(tmp.path, (registry) =>
  633. Effect.gen(function* () {
  634. const shell = yield* Shell.Service
  635. const timed = yield* executeTool(
  636. registry,
  637. call({ command: idleCommand, background: true }, "call-updated-timeout"),
  638. )
  639. const timedID = timed.metadata?.shellID
  640. expect(typeof timedID).toBe("string")
  641. if (typeof timedID !== "string") return
  642. const timedShellID = ShellSchema.ID.make(timedID)
  643. yield* shell.timeout(timedShellID, 50)
  644. expect((yield* shell.wait(timedShellID)).status).toBe("timeout")
  645. const cleared = yield* executeTool(
  646. registry,
  647. call({ command: idleCommand, timeout: 50, background: true }, "call-cleared-timeout"),
  648. )
  649. const clearedID = cleared.metadata?.shellID
  650. expect(typeof clearedID).toBe("string")
  651. if (typeof clearedID !== "string") return
  652. const clearedShellID = ShellSchema.ID.make(clearedID)
  653. yield* shell.timeout(clearedShellID, 0)
  654. yield* Effect.sleep(Duration.millis(100))
  655. expect((yield* shell.get(clearedShellID)).status).toBe("running")
  656. yield* shell.remove(clearedShellID)
  657. }),
  658. )
  659. },
  660. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  661. ),
  662. )
  663. it.live("backgrounds a foreground command when the session is signaled", () =>
  664. Effect.acquireUseRelease(
  665. Effect.promise(() => tmpdir()),
  666. (tmp) => {
  667. reset()
  668. return withSession(tmp.path, (registry) =>
  669. Effect.gen(function* () {
  670. const jobs = yield* Job.Service
  671. const scope = yield* Scope.Scope
  672. const waiting = yield* executeTool(
  673. registry,
  674. call({ command: idleCommand, timeout: 50 }, "call-background-signal"),
  675. ).pipe(Effect.forkIn(scope, { startImmediately: true }))
  676. const backgroundWhenReady = (remaining = 1000): Effect.Effect<Job.Info[], Error> =>
  677. Effect.gen(function* () {
  678. const backgrounded = yield* jobs.backgroundAll({ sessionID })
  679. if (backgrounded.length > 0) return backgrounded
  680. if (remaining <= 0) return yield* Effect.fail(new Error("Timed out waiting for foreground shell job"))
  681. yield* Effect.promise(() => Bun.sleep(1))
  682. return yield* backgroundWhenReady(remaining - 1)
  683. })
  684. expect(yield* backgroundWhenReady()).toMatchObject([{ id: "call-background-signal", type: "shell" }])
  685. const settled = yield* Fiber.join(waiting)
  686. const shellID = typeof settled.metadata?.shellID === "string" ? settled.metadata.shellID : undefined
  687. expect(settled.metadata).toMatchObject({ truncated: false })
  688. expect(settled.content?.[0]).toEqual({
  689. type: "text",
  690. text: "The command was moved to the background.",
  691. })
  692. expect(settled.content?.[1]).toMatchObject({
  693. type: "text",
  694. text: expect.stringContaining("DO NOT sleep, poll"),
  695. })
  696. expect(shellID).toStartWith("sh_")
  697. const shell = yield* Shell.Service
  698. if (!shellID) return
  699. const id = ShellSchema.ID.make(shellID)
  700. yield* Effect.sleep(Duration.millis(100))
  701. expect((yield* shell.get(id)).status).toBe("running")
  702. expect((yield* shell.list()).map((info) => info.id)).toContain(id)
  703. yield* shell.remove(id)
  704. }),
  705. )
  706. },
  707. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]().then(() => undefined)),
  708. ),
  709. )
  710. })