tools.spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { Context } from '@deepseek-ai/cordis'
  3. import { ToolCallId } from '@deepseek-ai/dsh-llm'
  4. import { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session'
  5. import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent'
  6. import type { Agent } from '@deepseek-ai/dsh-agent'
  7. import TerminalSessionService from '@deepseek-ai/dsh-terminal'
  8. import type {
  9. TerminalBackend,
  10. TerminalBackendSession,
  11. TerminalReadRequest,
  12. TerminalSendOperation,
  13. TerminalSendRequest,
  14. TerminalSessionStatus,
  15. TerminalSignal,
  16. TerminalWaitReason,
  17. } from '@deepseek-ai/dsh-terminal'
  18. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  19. import ToolRegistry from '@deepseek-ai/dsh-tools'
  20. import * as ToolPwshPersistent from '@deepseek-ai/dsh-tool-pwsh-persistent'
  21. const contexts: Context[] = []
  22. let callNumber = 0
  23. afterEach(async () => {
  24. for (const ctx of contexts.splice(0)) await ctx.fiber.dispose()
  25. })
  26. function agent(ctx: Context, cwd: string | undefined): Agent {
  27. const id = SessionId(`persistent-pwsh-owner-${callNumber}`)
  28. const scope = ctx.plugin(() => {})
  29. const session = Session.create(id, [], {
  30. version: SESSION_FORMAT_VERSION,
  31. id,
  32. createdAt: 0,
  33. isSeeded: false,
  34. ...cwd === undefined ? {} : { cwd },
  35. })
  36. const value: Agent = {
  37. id,
  38. options: {},
  39. session,
  40. inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }),
  41. status: 'idle',
  42. ctx: scope.ctx,
  43. send: () => {},
  44. followup: () => {},
  45. steer: () => ({ outcome: Promise.resolve({ status: 'rejected' as const }) }),
  46. inject: () => {},
  47. cancel() {},
  48. runMaintenance: task => task(new AbortController().signal),
  49. whenIdle: () => Promise.resolve(),
  50. }
  51. ctx.agents.register(value)
  52. return value
  53. }
  54. function text(result: { content: { type: string; text?: string }[] }): string {
  55. return result.content.filter(block => block.type === 'text').map(block => block.text).join('')
  56. }
  57. function call(
  58. ctx: Context,
  59. owner: Agent | undefined,
  60. command: string,
  61. signal = new AbortController().signal,
  62. ) {
  63. return ctx.tools.execute({
  64. signal,
  65. callId: ToolCallId(`persistent-pwsh-${++callNumber}`),
  66. name: 'pwsh',
  67. arguments: { command },
  68. ...owner === undefined ? {} : { agent: owner },
  69. })
  70. }
  71. type StubMode =
  72. | 'normal'
  73. | 'prompt-only'
  74. | 'prompt-crlf'
  75. | 'empty-read'
  76. | 'stalled-read'
  77. | 'exit'
  78. | 'signal-exit'
  79. | 'unknown-exit'
  80. | 'wait-for-abort'
  81. | 'end-on-abort'
  82. | 'idle-then-normal'
  83. | 'large'
  84. | 'nonzero'
  85. | 'torn-status'
  86. | 'finish-torn-status'
  87. | 'end-only'
  88. | 'init-exit'
  89. | 'init-timeout'
  90. | 'spawn-error'
  91. | 'send-error'
  92. | 'prompt-after-idle'
  93. | 'incremental-fallback'
  94. | 'empty-page-after-latest'
  95. | 'paged-scrollback'
  96. | 'with-echo'
  97. | 'exit-after-send'
  98. | 'prompt-collision'
  99. const START_PATTERN = /__DSH_PERSISTENT_PWSH_START_[^_]+(?:-[^_]+)*__/
  100. const END_PATTERN = /__DSH_PERSISTENT_PWSH_END_[^:]+:/
  101. class StubTerminalSession implements TerminalBackendSession {
  102. readonly motd = '__DSH_PERSISTENT_PWSH_PROMPT__ '
  103. readonly pid = 123
  104. statusValue: TerminalSessionStatus = { kind: 'running' }
  105. scrollback = this.motd
  106. closed: string[] = []
  107. mode: StubMode
  108. sends = 0
  109. pendingText = ''
  110. historyTruncated = false
  111. throwOnSend = false
  112. constructor(mode: StubMode) {
  113. this.mode = mode
  114. }
  115. startSend(request: TerminalSendRequest): TerminalSendOperation {
  116. this.sends += 1
  117. if (request.text.startsWith('function prompt')) {
  118. if (this.mode === 'init-exit') {
  119. this.statusValue = { kind: 'exited', exitCode: 1, signal: null }
  120. return this.operation(Promise.resolve(this.result('', 'session_exit')))
  121. }
  122. if (this.mode === 'init-timeout') {
  123. return this.operation(Promise.resolve(this.result('', 'timeout')))
  124. }
  125. return this.operation(Promise.resolve(this.result(this.motd, 'stdin_read')))
  126. }
  127. if (this.mode === 'send-error') throw new Error('stub send failed')
  128. if (this.throwOnSend) throw new Error('PTY session has exited')
  129. if (this.mode === 'wait-for-abort' || this.mode === 'end-on-abort') {
  130. const done = new Promise<ReturnType<StubTerminalSession['result']>>((resolve) => {
  131. request.signal?.addEventListener('abort', () => {
  132. const start = START_PATTERN.exec(request.text)?.[0]
  133. const end = END_PATTERN.exec(request.text)?.[0]
  134. const output = this.mode === 'end-on-abort'
  135. ? `${start ?? ''}\ninterrupted\n${end ?? ''}130\n${this.motd}`
  136. : 'partial output'
  137. this.scrollback += output
  138. resolve(this.result(output, 'stdin_read'))
  139. }, { once: true })
  140. })
  141. return this.operation(done)
  142. }
  143. if (this.mode === 'idle-then-normal') {
  144. this.mode = 'normal'
  145. this.pendingText = request.text
  146. return this.operation(Promise.resolve(this.result('', 'inferred_idle')))
  147. }
  148. if (this.mode === 'prompt-after-idle') {
  149. if (request.text.length > 0) {
  150. const start = START_PATTERN.exec(request.text)?.[0]
  151. const output = `${start ?? ''}\npartial syntax output\n`
  152. this.scrollback += output
  153. return this.operation(Promise.resolve(this.result(output, 'inferred_idle')))
  154. }
  155. const output = `pwsh: syntax error\n${this.motd}`
  156. this.scrollback += output
  157. return this.operation(Promise.resolve(this.result(output, 'stdin_read')))
  158. }
  159. if (this.mode === 'prompt-only' || this.mode === 'prompt-crlf') {
  160. const newline = this.mode === 'prompt-crlf' ? '\r\n' : '\n'
  161. const output = `pwsh: syntax error${newline}${this.motd}${newline}`
  162. this.scrollback += output
  163. return this.operation(Promise.resolve(this.result(output, 'stdin_read')))
  164. }
  165. const sent = request.text.length > 0 ? request.text : this.pendingText
  166. this.pendingText = ''
  167. const start = START_PATTERN.exec(sent)?.[0]
  168. const end = END_PATTERN.exec(sent)?.[0]
  169. if (this.mode === 'with-echo') {
  170. // The PSReadLine echo renders the submitted wrapper before the real
  171. // markers; the tool must strip it from the captured result.
  172. const output = `${sent}\n${start ?? ''}\nhello from stub\n${end ?? ''}0\n${this.motd}`
  173. this.scrollback += output
  174. return this.operation(Promise.resolve(this.result(output, 'stdin_read')))
  175. }
  176. if (this.mode === 'exit-after-send') {
  177. // A fast `exit` settles the send with an echoed wrapper (marker end,
  178. // no status digits) while the exit event is still in flight; the shell
  179. // flips to exited before the tool's next poll, exactly like the real
  180. // ConPTY backend. The tool must re-observe status instead of sending.
  181. const output = `${sent}\n${start ?? ''}\n`
  182. this.scrollback += output
  183. const settled = this.result(output, 'inferred_idle')
  184. this.statusValue = { kind: 'exited', exitCode: 9, signal: null }
  185. this.throwOnSend = true
  186. return this.operation(Promise.resolve(settled))
  187. }
  188. if (this.mode === 'incremental-fallback') {
  189. const incremental = `${start ?? ''}\nincrement\n${this.motd}`
  190. return this.operation(Promise.resolve(this.result(this.motd, 'stdin_read')), incremental)
  191. }
  192. if (this.mode === 'torn-status') {
  193. const output = `${start ?? ''}\nhello from stub\n${end ?? ''}`
  194. this.scrollback += output
  195. this.mode = 'finish-torn-status'
  196. return this.operation(Promise.resolve(this.result(output, 'inferred_idle')))
  197. }
  198. if (this.mode === 'finish-torn-status') {
  199. const output = `7\n${this.motd}`
  200. this.scrollback += output
  201. return this.operation(Promise.resolve(this.result(output, 'stdin_read')))
  202. }
  203. if (this.mode === 'end-only') {
  204. const output = `recovered output\n${end ?? ''}0\n${this.motd}`
  205. this.scrollback += output
  206. return this.operation(Promise.resolve(this.result(output, 'stdin_read')))
  207. }
  208. const commandOutput = this.mode === 'large'
  209. ? 'x'.repeat(100)
  210. : this.mode === 'nonzero' ? ''
  211. : this.mode === 'prompt-collision' ? this.motd
  212. : 'hello from stub'
  213. const exitCode = this.mode === 'nonzero' ? 7 : 0
  214. const output = `${start ?? ''}\n${commandOutput}\n${end ?? ''}${exitCode}\n${this.motd}`
  215. this.scrollback += output
  216. if (this.mode === 'exit' || this.mode === 'signal-exit' || this.mode === 'unknown-exit') {
  217. const exitedOutput = `${start ?? ''}\nhello from stub\n`
  218. this.scrollback = this.scrollback.slice(0, -output.length) + exitedOutput
  219. this.statusValue = this.mode === 'signal-exit'
  220. ? { kind: 'exited', exitCode: null, signal: 'SIGTERM' }
  221. : this.mode === 'exit'
  222. ? { kind: 'exited', exitCode: 9, signal: null }
  223. : { kind: 'exited', exitCode: null, signal: null }
  224. return this.operation(Promise.resolve(this.result(exitedOutput, 'session_exit')))
  225. }
  226. return this.operation(Promise.resolve(this.result(output, 'stdin_read')))
  227. }
  228. read(request: TerminalReadRequest) {
  229. if (this.mode === 'empty-read') {
  230. return { text: '', totalLines: 0, lineBegin: 0, lineEnd: 0, truncated: false }
  231. }
  232. if (this.mode === 'stalled-read') {
  233. return { text: 'stalled', totalLines: 1, lineBegin: 0, lineEnd: 0, truncated: false }
  234. }
  235. if (this.mode === 'empty-page-after-latest' && (request.offset ?? 0) > 0) {
  236. return { text: '', totalLines: 2, lineBegin: 1, lineEnd: 1, truncated: false }
  237. }
  238. const lines = this.scrollback.split('\n')
  239. if (this.mode === 'paged-scrollback') {
  240. const offset = request.offset ?? 0
  241. const end = lines.length - offset
  242. const start = Math.max(0, end - 3)
  243. const returnedLines = end - start
  244. return {
  245. text: lines.slice(start, end).join('\n'),
  246. totalLines: lines.length,
  247. lineBegin: offset,
  248. lineEnd: offset + returnedLines,
  249. truncated: this.historyTruncated,
  250. }
  251. }
  252. return {
  253. text: this.scrollback,
  254. totalLines: this.mode === 'empty-page-after-latest' ? lines.length + 1 : lines.length,
  255. lineBegin: 0,
  256. lineEnd: this.mode === 'empty-page-after-latest' ? 1 : lines.length,
  257. truncated: this.historyTruncated,
  258. }
  259. }
  260. signal(_signal: TerminalSignal) {
  261. return Promise.resolve({ delivered: true as const, targetPgid: 123 })
  262. }
  263. status() {
  264. return this.statusValue
  265. }
  266. async close(reason: string) {
  267. this.closed.push(reason)
  268. this.statusValue = { kind: 'exited', exitCode: 0, signal: null }
  269. }
  270. private result(viewport: string, waitReason: TerminalWaitReason) {
  271. return { viewport, waitReason, sessionStatus: this.statusValue, truncated: false }
  272. }
  273. private operation(done: Promise<ReturnType<StubTerminalSession['result']>>, delta = ''): TerminalSendOperation {
  274. return {
  275. done,
  276. readOutput: () => ({ delta, truncated: false }),
  277. cancel: () => false,
  278. }
  279. }
  280. }
  281. function stubBackend(initialMode: StubMode = 'normal') {
  282. const sessions: StubTerminalSession[] = []
  283. const backend: TerminalBackend = {
  284. type: 'stub',
  285. async spawn() {
  286. if (initialMode === 'spawn-error') throw new Error('stub spawn failed')
  287. const session = new StubTerminalSession(initialMode)
  288. sessions.push(session)
  289. return session
  290. },
  291. }
  292. return { backend, sessions }
  293. }
  294. async function setup(
  295. config: ToolPwshPersistent.Config = { backendType: 'stub' },
  296. initialMode: StubMode = 'normal',
  297. ) {
  298. const ctx = new Context()
  299. contexts.push(ctx)
  300. await ctx.plugin(SystemPrompt)
  301. await ctx.plugin(ToolRegistry)
  302. await ctx.plugin(AgentRegistry)
  303. await ctx.plugin(TerminalSessionService)
  304. const stub = stubBackend(initialMode)
  305. ctx.terminals.registerBackend(stub.backend)
  306. const fiber = await ctx.plugin(ToolPwshPersistent, config)
  307. return { ctx, stub, fiber, owner: agent(ctx, '/workspace') }
  308. }
  309. describe('tool-pwsh-persistent', () => {
  310. it('registers a configurable schema and reuses one owner shell', async () => {
  311. const { ctx, owner, stub, fiber } = await setup({
  312. backendType: 'stub',
  313. description: 'deployment-specific persistent shell',
  314. })
  315. const schema = ctx.tools.schemas()[0]
  316. expect(ctx.tools.schemas().map(item => item.name)).toEqual(['pwsh'])
  317. expect(schema?.description).toBe('deployment-specific persistent shell')
  318. expect(schema?.parameters).toMatchObject({
  319. required: ['command'],
  320. properties: { command: { type: 'string' } },
  321. })
  322. expect(ctx.tools.get('pwsh')?.presentCall?.({ command: 'pwd' }))
  323. .toEqual({ card: 'terminal', title: 'pwd' })
  324. expect(text(await call(ctx, owner, 'Write-Output one'))).toBe('hello from stub')
  325. expect(text(await call(ctx, owner, 'Write-Output two'))).toBe('hello from stub')
  326. expect(stub.sessions).toHaveLength(1)
  327. expect(stub.sessions[0]?.sends).toBe(3)
  328. const ownerWithoutCwd = agent(ctx, undefined)
  329. expect(text(await call(ctx, ownerWithoutCwd, 'pwd'))).toBe('hello from stub')
  330. expect(stub.sessions).toHaveLength(2)
  331. await fiber.dispose()
  332. expect(ctx.tools.schemas()).toEqual([])
  333. expect(ctx.tools.get('pwsh')).toBeUndefined()
  334. })
  335. it('strips the echoed wrapper from captured output', async () => {
  336. const { ctx, owner, stub } = await setup({ backendType: 'stub' })
  337. await call(ctx, owner, 'warm up')
  338. stub.sessions[0]!.mode = 'with-echo'
  339. const result = text(await call(ctx, owner, 'Write-Output hi'))
  340. expect(result).toBe('hello from stub')
  341. expect(result).not.toContain('__DSH_PERSISTENT_PWSH_START_')
  342. expect(result).not.toContain('__DSH_PERSISTENT_PWSH_END_')
  343. expect(result).not.toContain('Invoke-Expression')
  344. })
  345. it('preserves command output that equals the private shell prompt', async () => {
  346. const { ctx, owner, stub } = await setup({ backendType: 'stub' })
  347. await call(ctx, owner, 'warm up')
  348. const session = stub.sessions[0]!
  349. session.mode = 'prompt-collision'
  350. expect(text(await call(ctx, owner, 'complete prompt collision'))).toBe(session.motd)
  351. })
  352. it('reports the exit path when the shell exits between send settlement and the next poll', async () => {
  353. const { ctx, owner, stub } = await setup({ backendType: 'stub' })
  354. await call(ctx, owner, 'warm up')
  355. const session = stub.sessions[0]!
  356. session.mode = 'exit-after-send'
  357. const result = text(await call(ctx, owner, 'exit'))
  358. expect(result).toContain('[shell exited: code 9]')
  359. expect(result).toContain('next pwsh call starts from the workspace')
  360. expect(session.closed).toContain('persistent pwsh shell exited')
  361. expect(text(await call(ctx, owner, 'Write-Output "$PWD"'))).toBe('hello from stub')
  362. expect(stub.sessions).toHaveLength(2)
  363. })
  364. it('handles inferred idle, prompt fallback, shell exit, clipping, and cleanup', async () => {
  365. const { ctx, owner, stub, fiber } = await setup({
  366. backendType: 'stub',
  367. maxOutputChars: 10,
  368. })
  369. await call(ctx, owner, 'warm up')
  370. const session = stub.sessions[0]!
  371. session.mode = 'idle-then-normal'
  372. expect(text(await call(ctx, owner, 'silent then complete'))).toContain('hello from')
  373. session.mode = 'incremental-fallback'
  374. session.scrollback = ''
  375. expect(text(await call(ctx, owner, 'incremental fallback'))).toBe('increment')
  376. session.mode = 'prompt-only'
  377. const promptFallback = text(await call(ctx, owner, 'bad {'))
  378. expect(promptFallback).toContain('pwsh: synt')
  379. expect(promptFallback).not.toContain('DSH_PERSISTENT_PWSH_PROMPT')
  380. session.mode = 'prompt-crlf'
  381. session.scrollback = ''
  382. const crlfPromptFallback = text(await call(ctx, owner, 'bad {'))
  383. expect(crlfPromptFallback).toContain('pwsh: synt')
  384. expect(crlfPromptFallback).not.toContain('DSH_PERSISTENT_PWSH_PROMPT')
  385. session.mode = 'end-only'
  386. session.scrollback = ''
  387. const missingStart = text(await call(ctx, owner, 'recover marker'))
  388. expect(missingStart).toContain('recovered')
  389. expect(missingStart).toContain('beginning of this command output was dropped')
  390. expect(missingStart).toContain('<response clipped>')
  391. session.mode = 'large'
  392. expect(text(await call(ctx, owner, 'large'))).toContain('<response clipped>')
  393. session.mode = 'nonzero'
  394. expect(text(await call(ctx, owner, 'false'))).toBe('[exit code: 7]')
  395. session.mode = 'exit'
  396. const exited = text(await call(ctx, owner, 'exit'))
  397. expect(exited).toContain('hello from')
  398. expect(exited).toContain('[shell exited: code 9]')
  399. expect(exited).not.toContain('[exit code: 9]')
  400. expect(exited).toContain('next pwsh call starts from the workspace')
  401. expect(session.closed).toContain('persistent pwsh shell exited')
  402. await call(ctx, owner, 'new shell')
  403. expect(stub.sessions).toHaveLength(2)
  404. const replacement = stub.sessions[1]!
  405. replacement.mode = 'signal-exit'
  406. expect(text(await call(ctx, owner, 'kill shell')))
  407. .toContain('[shell killed by signal: SIGTERM]')
  408. await call(ctx, owner, 'another shell')
  409. expect(stub.sessions).toHaveLength(3)
  410. const externallyClosed = ctx.terminals.list(owner)[0]?.sessionId
  411. expect(externallyClosed).toBeDefined()
  412. await ctx.terminals.kill(owner, externallyClosed!, 'external cleanup')
  413. await fiber.dispose()
  414. expect(stub.sessions[2]?.closed).toEqual(['external cleanup'])
  415. })
  416. it('waits for status digits after a torn completion marker', async () => {
  417. const { ctx, owner, stub } = await setup({ backendType: 'stub', maxOutputChars: 1_000 })
  418. await call(ctx, owner, 'warm up')
  419. stub.sessions[0]!.mode = 'torn-status'
  420. stub.sessions[0]!.scrollback = ''
  421. expect(text(await call(ctx, owner, 'torn status'))).toBe('hello from stub\n[exit code: 7]')
  422. })
  423. it('reports a shell exit when the backend has no code or signal', async () => {
  424. const { ctx, owner, stub } = await setup({ backendType: 'stub' })
  425. await call(ctx, owner, 'warm up')
  426. stub.sessions[0]!.mode = 'unknown-exit'
  427. expect(text(await call(ctx, owner, 'exit without status'))).toContain('[shell exited]')
  428. })
  429. it('marks a short missing-prefix result and tolerates exhausted scrollback pages', async () => {
  430. const { ctx, owner, stub } = await setup({ backendType: 'stub', maxOutputChars: 1_000 })
  431. await call(ctx, owner, 'warm up')
  432. const session = stub.sessions[0]!
  433. session.mode = 'end-only'
  434. session.scrollback = ''
  435. expect(text(await call(ctx, owner, 'missing start')))
  436. .toContain('beginning of this command output was dropped')
  437. session.mode = 'empty-read'
  438. expect(text(await call(ctx, owner, 'empty page'))).toContain('hello from stub')
  439. session.mode = 'stalled-read'
  440. expect(text(await call(ctx, owner, 'stalled page'))).toContain('hello from stub')
  441. session.mode = 'empty-page-after-latest'
  442. expect(text(await call(ctx, owner, 'empty continuation page'))).toContain('hello from stub')
  443. })
  444. it('assembles retained output across backward scrollback pages', async () => {
  445. const { ctx, owner, stub } = await setup({ backendType: 'stub', maxOutputChars: 1_000 })
  446. await call(ctx, owner, 'warm up')
  447. const session = stub.sessions[0]!
  448. session.mode = 'paged-scrollback'
  449. session.scrollback = 'older one\nolder two\nolder three\nolder four\n'
  450. expect(text(await call(ctx, owner, 'paged output'))).toBe('hello from stub')
  451. })
  452. it('sanitizes a prompt fallback reached after multiple polling rounds', async () => {
  453. const { ctx, owner, stub } = await setup({ backendType: 'stub', maxOutputChars: 1_000 })
  454. await call(ctx, owner, 'warm up')
  455. const session = stub.sessions[0]!
  456. session.mode = 'prompt-after-idle'
  457. session.scrollback = ''
  458. const result = text(await call(ctx, owner, 'bad {'))
  459. expect(result).toContain('partial syntax output')
  460. expect(result).toContain('pwsh: syntax error')
  461. expect(result).not.toContain('DSH_PERSISTENT_PWSH_PROMPT')
  462. expect(result).not.toContain('DSH_PERSISTENT_PWSH_START')
  463. })
  464. it('does not attribute old scrollback truncation to a complete current command', async () => {
  465. const { ctx, owner, stub } = await setup({ backendType: 'stub', maxOutputChars: 1_000 })
  466. await call(ctx, owner, 'warm up')
  467. stub.sessions[0]!.historyTruncated = true
  468. const result = text(await call(ctx, owner, 'short command'))
  469. expect(result).toBe('hello from stub')
  470. expect(result).not.toContain('<response clipped>')
  471. expect(result).not.toContain('beginning of this command output was dropped')
  472. })
  473. it('closes a timed-out shell and reports bounded partial output', async () => {
  474. const { ctx, owner, stub } = await setup({ backendType: 'stub', timeoutMs: 10 })
  475. await call(ctx, owner, 'warm up')
  476. stub.sessions[0]!.mode = 'wait-for-abort'
  477. const result = await call(ctx, owner, 'hang')
  478. expect(text(result)).toContain('timed out after 0 seconds or experienced an OOM error')
  479. expect(text(result)).toContain('partial output')
  480. expect(text(result)).toContain('next pwsh call starts from the workspace')
  481. expect(stub.sessions[0]?.closed).toContain('persistent pwsh command timed out')
  482. })
  483. it.each(['wait-for-abort', 'end-on-abort'] as const)(
  484. 'cancels %s work, resets the shell, and releases a queued call',
  485. async (mode) => {
  486. const { ctx, owner, stub } = await setup({ backendType: 'stub', timeoutMs: 5_000 })
  487. await call(ctx, owner, 'warm up')
  488. stub.sessions[0]!.mode = mode
  489. const controller = new AbortController()
  490. const cancelled = call(ctx, owner, 'hang', controller.signal)
  491. const queued = call(ctx, owner, 'after cancellation')
  492. setTimeout(() => {
  493. controller.abort(new Error('caller stopped'))
  494. }, 5)
  495. expect((await cancelled).isError).toBe(true)
  496. expect(text(await queued)).toBe('hello from stub')
  497. expect(stub.sessions[0]?.closed).toContain('persistent pwsh command aborted')
  498. expect(stub.sessions).toHaveLength(2)
  499. },
  500. )
  501. it.each(['init-exit', 'init-timeout'] as const)(
  502. 'fails initialization and closes the unusable shell for %s',
  503. async (mode) => {
  504. const { ctx, owner, stub } = await setup({ backendType: 'stub' }, mode)
  505. expect((await call(ctx, owner, 'pwd')).isError).toBe(true)
  506. expect(stub.sessions[0]?.closed).toContain('persistent pwsh initialization failed')
  507. },
  508. )
  509. it('clears a failed spawn without trying to close an unpublished shell', async () => {
  510. const { ctx, owner, stub } = await setup({ backendType: 'stub' }, 'spawn-error')
  511. expect((await call(ctx, owner, 'pwd')).isError).toBe(true)
  512. expect(stub.sessions).toHaveLength(0)
  513. })
  514. it('resets a cached shell after startSend fails', async () => {
  515. const { ctx, owner, stub } = await setup()
  516. await call(ctx, owner, 'warm up')
  517. stub.sessions[0]!.mode = 'send-error'
  518. expect((await call(ctx, owner, 'fails')).isError).toBe(true)
  519. expect(stub.sessions[0]?.closed).toContain('persistent pwsh send failed')
  520. expect(text(await call(ctx, owner, 'recovers'))).toBe('hello from stub')
  521. expect(stub.sessions).toHaveLength(2)
  522. })
  523. it('cancels and awaits a pending shell spawn when the plugin is disposed', async () => {
  524. const ctx = new Context()
  525. contexts.push(ctx)
  526. await ctx.plugin(SystemPrompt)
  527. await ctx.plugin(ToolRegistry)
  528. await ctx.plugin(AgentRegistry)
  529. await ctx.plugin(TerminalSessionService)
  530. const spawnStarted = Promise.withResolvers<undefined>()
  531. const spawnAborted = Promise.withResolvers<undefined>()
  532. ctx.terminals.registerBackend({
  533. type: 'slow',
  534. spawn: spec => new Promise((_resolve, reject) => {
  535. spawnStarted.resolve(undefined)
  536. spec.signal?.addEventListener('abort', () => {
  537. spawnAborted.resolve(undefined)
  538. const reason: unknown = spec.signal?.reason
  539. reject(reason instanceof Error
  540. ? reason
  541. : new Error('slow PTY spawn aborted', { cause: reason }))
  542. }, { once: true })
  543. }),
  544. })
  545. const fiber = await ctx.plugin(ToolPwshPersistent, { backendType: 'slow' })
  546. const owner = agent(ctx, '/workspace')
  547. const running = call(ctx, owner, 'pwd')
  548. await spawnStarted.promise
  549. await fiber.dispose()
  550. await spawnAborted.promise
  551. expect((await running).isError).toBe(true)
  552. expect(ctx.terminals.list(owner)).toEqual([])
  553. })
  554. it('rejects invalid config and invalid calls', async () => {
  555. const { ctx, owner, stub } = await setup()
  556. expect((await call(ctx, undefined, 'pwd')).isError).toBe(true)
  557. expect(text(await call(ctx, owner, ' '))).toContain('command must be a non-empty string')
  558. const controller = new AbortController()
  559. controller.abort(new Error('caller stopped'))
  560. expect((await call(ctx, owner, 'pwd', controller.signal)).isError).toBe(true)
  561. expect(stub.sessions).toHaveLength(0)
  562. expect(() => {
  563. ToolPwshPersistent.apply(new Context(), { backendType: '' })
  564. }).toThrow('backendType must be non-empty')
  565. expect(() => {
  566. ToolPwshPersistent.apply(new Context(), { timeoutMs: 0 })
  567. }).toThrow('timeoutMs must be a positive safe integer')
  568. expect(() => {
  569. ToolPwshPersistent.apply(new Context(), { maxOutputChars: 0 })
  570. }).toThrow('maxOutputChars must be a positive safe integer')
  571. expect(() => {
  572. ToolPwshPersistent.apply(new Context(), { description: ' ' })
  573. }).toThrow('description must be non-empty')
  574. })
  575. })