tools.spec.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /**
  2. * Consumer-surface tests over a fake provider and the real policy collaborator: schemas,
  3. * validation, formatting, typed errors, intent dispatch, and observation-driven authorization.
  4. */
  5. import { describe, expect, it, vi } from 'vitest'
  6. import { Context } from 'cordis'
  7. import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync } from 'node:fs'
  8. import { tmpdir } from 'node:os'
  9. import { join, resolve, sep } from 'node:path'
  10. import { CallId } from '@deepseek-ai/dsh-llm'
  11. import SystemPrompt, { renderPrompt } from '@deepseek-ai/dsh-system-prompt'
  12. import ToolRegistry, { type ToolResult } from '@deepseek-ai/dsh-tools'
  13. import { FileSystem, FsError, FsTargetKey, FsVersion } from '@deepseek-ai/dsh-fs'
  14. import type {
  15. FsDirEntry,
  16. FsEditOutcome,
  17. FsEditRequest,
  18. FsInfo,
  19. FsPathInfo,
  20. FsTarget,
  21. FsWriteIntent,
  22. FsWriteOutcome,
  23. } from '@deepseek-ai/dsh-fs'
  24. import * as FsPolicy from '@deepseek-ai/dsh-fs-policy'
  25. import * as ToolFs from '@deepseek-ai/dsh-tool-fs'
  26. import { STREAM_MIN_SIZE } from '../src/read.ts'
  27. import { formatReadOutput } from '../src/read-render.ts'
  28. import type { FileReadOutcome } from '../src/read-render.ts'
  29. import { sessionCwd } from '../src/session-cwd.ts'
  30. import ApprovalService from '@deepseek-ai/dsh-user-approval'
  31. import type { SandboxExecutionPolicy, SandboxMode } from '@deepseek-ai/dsh-sandbox'
  32. import SandboxPolicyService from '@deepseek-ai/dsh-sandbox-policy'
  33. const testToolSignal = new AbortController().signal
  34. /** An in-memory fake provider; a test can arm a rejection on any primitive. */
  35. class FakeFs extends FileSystem {
  36. files = new Map<string, string>()
  37. rejectWith?: FsError
  38. writeIntents: (FsWriteIntent | undefined)[] = []
  39. editIntents: ({ version: FsVersion } | undefined)[] = []
  40. private throwIfArmed(): void {
  41. if (this.rejectWith) throw this.rejectWith
  42. }
  43. override async resolve(path: string): Promise<FsTarget> {
  44. return { targetKey: FsTargetKey(`key:${path}`), displayPath: `/abs/${path}` }
  45. }
  46. override async stat(target: FsTarget): Promise<FsInfo | undefined> {
  47. this.throwIfArmed()
  48. const content = this.files.get(target.targetKey)
  49. if (content === undefined) return undefined
  50. return { version: FsVersion('v1'), type: 'file', size: content.length }
  51. }
  52. override async lstat(path: string): Promise<FsPathInfo | undefined> {
  53. const content = this.files.get(`key:${path}`)
  54. if (content === undefined) return undefined
  55. return { version: FsVersion('v1'), type: 'file', size: content.length }
  56. }
  57. override async readText(target: FsTarget): Promise<string> {
  58. return this.files.get(target.targetKey) ?? ''
  59. }
  60. override async streamText(target: FsTarget): Promise<AsyncIterable<string>> {
  61. const content = this.files.get(target.targetKey) ?? ''
  62. return (async function* () { yield content })()
  63. }
  64. override async listDir(_target: FsTarget): Promise<FsDirEntry[]> {
  65. return []
  66. }
  67. override async writeText(target: FsTarget, content: string, expected?: FsWriteIntent): Promise<FsWriteOutcome> {
  68. this.throwIfArmed()
  69. this.writeIntents.push(expected)
  70. const before = this.files.get(target.targetKey) ?? null
  71. this.files.set(target.targetKey, content)
  72. return { operation: before !== null ? 'update' : 'create', version: FsVersion('v2'), before, after: content }
  73. }
  74. override async editText(target: FsTarget, edit: FsEditRequest, expected?: { version: FsVersion }): Promise<FsEditOutcome> {
  75. this.throwIfArmed()
  76. this.editIntents.push(expected)
  77. const content = this.files.get(target.targetKey) ?? ''
  78. const after = content.split(edit.oldString).join(edit.newString)
  79. this.files.set(target.targetKey, after)
  80. return { version: FsVersion('v3'), before: content, after }
  81. }
  82. }
  83. async function setup() {
  84. const ctx = new Context()
  85. await ctx.plugin(SystemPrompt)
  86. await ctx.plugin(ToolRegistry)
  87. await ctx.plugin(FakeFs)
  88. await ctx.plugin(FsPolicy)
  89. await ctx.plugin(ToolFs)
  90. const fs = ctx.fs as FakeFs
  91. return { ctx, fs }
  92. }
  93. let callCounter = 0
  94. function call(ctx: Context, name: string, args: unknown, agent?: object) {
  95. return ctx.tools.execute({
  96. signal: testToolSignal,
  97. callId: CallId(`call-${++callCounter}`),
  98. name,
  99. arguments: args,
  100. ...agent ? { agent: agent as never } : {},
  101. })
  102. }
  103. function text(result: { content: { type: string; text?: string }[] }): string {
  104. return result.content.filter(b => b.type === 'text').map(b => b.text).join('')
  105. }
  106. describe('session cwd resolution', () => {
  107. const execution = (cwd?: string) => cwd === undefined
  108. ? {}
  109. : { agent: { session: { header: { cwd } } } }
  110. it('retains ordinary spelling but resolves the cwd before parent traversal', () => {
  111. const cwd = process.cwd()
  112. const throughParent = `${cwd}${sep}..`
  113. expect(sessionCwd(execution() as never, 'file.txt')).toBeUndefined()
  114. expect(sessionCwd(execution(cwd) as never, 'file.txt')).toBe(cwd)
  115. expect(sessionCwd(execution(throughParent) as never, 'file.txt')).toBe(realpathSync.native(throughParent))
  116. const root = mkdtempSync(join(tmpdir(), 'dsh-tool-fs-session-cwd-'))
  117. const physical = join(root, 'physical')
  118. const link = join(root, 'link')
  119. try {
  120. mkdirSync(physical)
  121. symlinkSync(physical, link, process.platform === 'win32' ? 'junction' : 'dir')
  122. expect(sessionCwd(execution(link) as never, 'child.txt')).toBe(link)
  123. expect(sessionCwd(execution(link) as never, `..${sep}parent.txt`)).toBe(realpathSync.native(link))
  124. } finally {
  125. rmSync(root, { recursive: true, force: true })
  126. }
  127. })
  128. })
  129. describe('registration', () => {
  130. it('registers read, write, and edit', async () => {
  131. const { ctx } = await setup()
  132. expect(ctx.tools.schemas().map(s => s.name).sort()).toEqual(['edit', 'read', 'write'])
  133. })
  134. it('declares read parallel-safe while write/edit remain exclusive', async () => {
  135. const { ctx } = await setup()
  136. expect(ctx.tools.executionMode({ signal: testToolSignal, callId: CallId('read-safe'), name: 'read', arguments: { file_path: 'a.txt' } }))
  137. .toEqual({ kind: 'parallel' })
  138. expect(ctx.tools.executionMode({ signal: testToolSignal, callId: CallId('write-exclusive'), name: 'write', arguments: { file_path: 'a.txt', content: 'x' } }))
  139. .toEqual({ kind: 'exclusive' })
  140. expect(ctx.tools.executionMode({ signal: testToolSignal, callId: CallId('edit-exclusive'), name: 'edit', arguments: { file_path: 'a.txt', old_string: 'x', new_string: 'y' } }))
  141. .toEqual({ kind: 'exclusive' })
  142. })
  143. it('registers prompt sections for each tool', async () => {
  144. const { ctx } = await setup()
  145. const prompt = renderPrompt(await ctx.systemPrompt.assemble())
  146. expect(prompt).toContain('Use the read tool')
  147. expect(prompt).toContain('Use the write tool')
  148. expect(prompt).toContain('Use the edit tool')
  149. })
  150. it('stays pending until ctx.fs exists (inject)', async () => {
  151. const ctx = new Context()
  152. await ctx.plugin(SystemPrompt)
  153. await ctx.plugin(ToolRegistry)
  154. await ctx.plugin(ToolFs) // no fs provider
  155. expect(ctx.tools.schemas()).toHaveLength(0)
  156. })
  157. it('unregisters everything on fiber disposal (HMR safety)', async () => {
  158. const ctx = new Context()
  159. await ctx.plugin(SystemPrompt)
  160. await ctx.plugin(ToolRegistry)
  161. await ctx.plugin(FakeFs)
  162. await ctx.plugin(FsPolicy)
  163. const fiber = await ctx.plugin(ToolFs)
  164. // Each tool contributes BOTH a schema and a prompt section; disposal must
  165. // withdraw both, not just the schemas.
  166. expect(ctx.tools.schemas()).toHaveLength(3)
  167. const sectionNames = (a: { sections: { name: string }[] }) => a.sections.map(s => s.name).sort()
  168. expect(sectionNames(await ctx.systemPrompt.assemble())).toEqual(['deployment:persona', 'harness:identity', 'tool:edit', 'tool:read', 'tool:write'])
  169. await fiber.dispose()
  170. expect(ctx.tools.schemas()).toHaveLength(0)
  171. // Only the system-prompt plugin's own built-in sections remain.
  172. expect(sectionNames(await ctx.systemPrompt.assemble())).toEqual(['deployment:persona', 'harness:identity'])
  173. })
  174. })
  175. describe('read tool', () => {
  176. it('formats line-numbered content with a footer', async () => {
  177. const { ctx, fs } = await setup()
  178. fs.files.set('key:a.txt', 'hello\nworld')
  179. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  180. expect(result.isError).toBe(false)
  181. if (result.isError) throw new Error('expected read success')
  182. expect(result.value).toEqual({
  183. path: '/abs/a.txt',
  184. offset: 1,
  185. lines: [{ number: 1, text: 'hello' }, { number: 2, text: 'world' }],
  186. totalLines: 2,
  187. })
  188. expect(text(result)).toBe(`<path>/abs/a.txt</path>
  189. <type>file</type>
  190. <content>
  191. 1: hello
  192. 2: world
  193. (End of file - total 2 lines)
  194. </content>`)
  195. })
  196. it('returns an explicit empty canonical line window for an empty file', async () => {
  197. const { ctx, fs } = await setup()
  198. fs.files.set('key:empty.txt', '')
  199. const result = await call(ctx, 'read', { file_path: 'empty.txt' })
  200. if (result.isError) throw new Error('expected empty read success')
  201. expect(result.value).toEqual({ path: '/abs/empty.txt', offset: 1, lines: [], totalLines: 0 })
  202. expect(text(result)).toContain('(End of file - total 0 lines)')
  203. })
  204. it('rejects a non-positive offset via arg validation', async () => {
  205. const { ctx } = await setup()
  206. const result = await call(ctx, 'read', { file_path: 'a.txt', offset: 0 })
  207. expect(result.isError).toBe(true)
  208. expect(text(result)).toContain('offset must be a positive integer')
  209. })
  210. it('rejects a fractional offset and a zero/negative limit', async () => {
  211. const { ctx } = await setup()
  212. for (const args of [
  213. { file_path: 'a.txt', offset: 1.5 },
  214. { file_path: 'a.txt', limit: 0 },
  215. { file_path: 'a.txt', limit: -3 },
  216. ]) {
  217. const result = await call(ctx, 'read', args)
  218. expect(result.isError, JSON.stringify(args)).toBe(true)
  219. expect(text(result)).toMatch(/must be a positive integer/)
  220. }
  221. })
  222. it('rejects a non-JSON numeric offset before tool-specific validation', async () => {
  223. const { ctx } = await setup()
  224. const result = await call(ctx, 'read', { file_path: 'a.txt', offset: Number.NaN })
  225. expect(result.isError).toBe(true)
  226. expect(text(result)).toContain('tool execution arguments must be losslessly JSON-serializable')
  227. })
  228. it('rejects a limit above the cap', async () => {
  229. const { ctx } = await setup()
  230. const result = await call(ctx, 'read', { file_path: 'a.txt', limit: 99999 })
  231. expect(result.isError).toBe(true)
  232. expect(text(result)).toContain('less than or equal to 2000')
  233. })
  234. it('rejects a blank file_path', async () => {
  235. const { ctx } = await setup()
  236. const result = await call(ctx, 'read', { file_path: ' ' })
  237. expect(result.isError).toBe(true)
  238. expect(text(result)).toContain('file_path must be a non-empty string')
  239. })
  240. it('records observed state so a follow-up edit by the same session is authorized', async () => {
  241. const { ctx, fs } = await setup()
  242. const session = { header: {} }
  243. fs.files.set('key:a.txt', 'hello')
  244. expect((await call(ctx, 'read', { file_path: 'a.txt' }, { session })).isError).toBe(false)
  245. const edited = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'hello', new_string: 'bye' }, { session })
  246. expect(edited.isError).toBe(false)
  247. expect(fs.editIntents).toEqual([{ version: 'v1' }])
  248. })
  249. it('propagates FS_NOT_FOUND for an absent file', async () => {
  250. const { ctx } = await setup()
  251. const result = await call(ctx, 'read', { file_path: 'missing.txt' })
  252. expect(result.isError).toBe(true)
  253. expect(result.error).toMatchObject({ info: { code: 'FS_NOT_FOUND' } })
  254. })
  255. it('rejects a non-regular target', async () => {
  256. const { ctx, fs } = await setup()
  257. fs.files.set('key:d', '')
  258. fs.stat = async () => ({ version: FsVersion('v1'), type: 'directory' })
  259. const result = await call(ctx, 'read', { file_path: 'd' })
  260. expect(result.isError).toBe(true)
  261. expect(result.error).toMatchObject({ info: { code: 'FS_NOT_REGULAR_FILE' } })
  262. })
  263. it('streams a large file (size at/above the cap) instead of reading whole', async () => {
  264. const { ctx, fs } = await setup()
  265. fs.files.set('key:big.txt', 'alpha\nbeta')
  266. const readSpy = vi.spyOn(fs, 'readText')
  267. const streamSpy = vi.spyOn(fs, 'streamText')
  268. fs.stat = async () => ({ version: FsVersion('v1'), type: 'file', size: STREAM_MIN_SIZE })
  269. const result = await call(ctx, 'read', { file_path: 'big.txt' })
  270. expect(result.isError).toBe(false)
  271. expect(text(result)).toContain('1: alpha')
  272. expect(streamSpy).toHaveBeenCalled()
  273. expect(readSpy).not.toHaveBeenCalled()
  274. })
  275. it('streams when the backend reports no size (never buffers a size-less file)', async () => {
  276. const { ctx, fs } = await setup()
  277. fs.files.set('key:a.txt', 'alpha')
  278. const streamSpy = vi.spyOn(fs, 'streamText')
  279. fs.stat = async () => ({ version: FsVersion('v1'), type: 'file' }) // no size
  280. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  281. expect(result.isError).toBe(false)
  282. expect(streamSpy).toHaveBeenCalled()
  283. })
  284. it('surfaces a byte-capped read as a truncated footer', async () => {
  285. const { ctx, fs } = await setup()
  286. // Many long lines so the window hits the byte cap before EOF.
  287. fs.files.set('key:big.txt', Array.from({ length: 2000 }, () => 'y'.repeat(100)).join('\n'))
  288. const result = await call(ctx, 'read', { file_path: 'big.txt' })
  289. expect(result.isError).toBe(false)
  290. expect(text(result)).toContain('Output capped.')
  291. })
  292. })
  293. describe('formatReadOutput footer variants', () => {
  294. const base: FileReadOutcome = { offset: 1, lines: [{ number: 1, text: 'x' }], totalLines: 1 }
  295. it('reports a byte-capped read', () => {
  296. const out = formatReadOutput('/f', { ...base, totalLines: 99, truncatedByBytes: true })
  297. expect(out).toContain('(Output capped. Showing lines 1-1. Use offset=2 to continue.)')
  298. })
  299. it('reports a more-remaining page', () => {
  300. const out = formatReadOutput('/f', { ...base, totalLines: 99 })
  301. expect(out).toContain('(Showing lines 1-1 of 99. Use offset=2 to continue.)')
  302. })
  303. it('reports end-of-file', () => {
  304. expect(formatReadOutput('/f', base)).toContain('(End of file - total 1 lines)')
  305. })
  306. it('renders an empty file as just the footer', () => {
  307. const out = formatReadOutput('/f', { ...base, lines: [], totalLines: 0 })
  308. expect(out).toContain('(End of file - total 0 lines)')
  309. expect(out).not.toContain(': ')
  310. })
  311. })
  312. describe('write tool', () => {
  313. it('formats a create result and uses createIfAbsent (unobserved, with the gate)', async () => {
  314. const { ctx, fs } = await setup()
  315. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'hi' }, { session: { header: {} } })
  316. expect(result.isError).toBe(false)
  317. if (result.isError) throw new Error('expected write success')
  318. expect(result.value).toEqual({ path: '/abs/a.txt', operation: 'create', before: null, after: 'hi' })
  319. expect(text(result)).toContain('Created file')
  320. expect(fs.writeIntents).toEqual([{ kind: 'createIfAbsent' }])
  321. })
  322. it('rejects a blank file_path', async () => {
  323. const { ctx } = await setup()
  324. const result = await call(ctx, 'write', { file_path: ' ', content: 'hi' })
  325. expect(result.isError).toBe(true)
  326. expect(text(result)).toContain('file_path must be a non-empty string')
  327. })
  328. it('propagates a backend FsError as an isError result carrying its code', async () => {
  329. const { ctx, fs } = await setup()
  330. fs.rejectWith = new FsError('blocked', 'FS_STALE_VERSION')
  331. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'hi' })
  332. expect(result.isError).toBe(true)
  333. expect(result.error).toMatchObject({ info: { name: 'FsError', code: 'FS_STALE_VERSION' } })
  334. })
  335. })
  336. describe('edit tool', () => {
  337. it('formats a single-replacement success after a read', async () => {
  338. const { ctx, fs } = await setup()
  339. const session = { header: {} }
  340. fs.files.set('key:a.txt', 'a')
  341. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  342. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'a', new_string: 'b' }, { session })
  343. if (result.isError) throw new Error('expected edit success')
  344. expect(result.value).toEqual({ path: '/abs/a.txt', before: 'a', after: 'b' })
  345. expect(text(result)).toBe('The file /abs/a.txt has been updated successfully.')
  346. })
  347. it('formats the replace_all success message distinctly', async () => {
  348. const { ctx, fs } = await setup()
  349. const session = { header: {} }
  350. fs.files.set('key:a.txt', 'a a a')
  351. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  352. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'a', new_string: 'b', replace_all: true }, { session })
  353. expect(text(result)).toBe('The file /abs/a.txt has been updated. All occurrences were successfully replaced.')
  354. })
  355. it('rejects identical old/new strings', async () => {
  356. const { ctx } = await setup()
  357. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'x', new_string: 'x' })
  358. expect(result.isError).toBe(true)
  359. expect(text(result)).toContain('must differ')
  360. })
  361. it('rejects an empty old_string', async () => {
  362. const { ctx } = await setup()
  363. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: '', new_string: 'x' })
  364. expect(result.isError).toBe(true)
  365. expect(text(result)).toContain('old_string must be a non-empty string')
  366. })
  367. it('rejects a blank file_path', async () => {
  368. const { ctx } = await setup()
  369. const result = await call(ctx, 'edit', { file_path: ' ', old_string: 'a', new_string: 'b' })
  370. expect(result.isError).toBe(true)
  371. expect(text(result)).toContain('file_path must be a non-empty string')
  372. })
  373. it('propagates FS_NOT_OBSERVED when the file was never read (the gate decides)', async () => {
  374. const { ctx, fs } = await setup()
  375. fs.files.set('key:a.txt', 'hello')
  376. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'a', new_string: 'b' }, { session: { header: {} } })
  377. expect(result.isError).toBe(true)
  378. expect(result.error).toMatchObject({ info: { code: 'FS_NOT_OBSERVED' } })
  379. })
  380. })
  381. describe('tool-owned presentation (pure presentCall)', () => {
  382. // presentCall is a pure display function of args (no I/O); it drives the
  383. // card's title/kind and the `locations` a UI follows along to.
  384. const presentCall = async (name: string, args: unknown) => {
  385. const { ctx } = await setup()
  386. return ctx.tools.get(name)?.presentCall?.(args)
  387. }
  388. const presentResult = async (name: string, args: unknown, result: ToolResult) => {
  389. const { ctx } = await setup()
  390. return ctx.tools.get(name)?.presentResult?.(args, result)
  391. }
  392. it('read: generic card titled by file with the read window, read kind, location with the offset line', async () => {
  393. expect(await presentCall('read', { file_path: 'src/a.ts', offset: 12, limit: 40 })).toEqual({
  394. card: 'generic', title: 'Read src/a.ts (12 - 51)', kind: 'read',
  395. locations: [{ path: 'src/a.ts', line: 12 }],
  396. })
  397. })
  398. it('read: bare title and line-1 location when offset/limit are unset', async () => {
  399. expect(await presentCall('read', { file_path: 'a.txt' })).toEqual({
  400. card: 'generic', title: 'Read a.txt', kind: 'read', locations: [{ path: 'a.txt', line: 1 }],
  401. })
  402. })
  403. it('read: completed presentation removes the model-facing XML envelope', async () => {
  404. expect(await presentResult('read', { file_path: 'a.txt' }, {
  405. content: [{ type: 'text', text: '<path>/tmp/a.txt</path>\n<type>file</type>\n<content>\n1: hello\n\n(End of file - total 1 lines)\n</content>' }],
  406. isError: false,
  407. })).toEqual({
  408. card: 'generic',
  409. content: [{ type: 'text', text: '1: hello\n\n(End of file - total 1 lines)' }],
  410. })
  411. expect(await presentResult('read', { file_path: 'a.txt' }, {
  412. content: [{ type: 'text', text: 'malformed replay' }],
  413. isError: false,
  414. })).toBeUndefined()
  415. })
  416. it('read: completed presentation declines errors and non-single-text content', async () => {
  417. const envelope = '<path>/tmp/a.txt</path>\n<type>file</type>\n<content>\nbody\n</content>'
  418. expect(await presentResult('read', { file_path: 'a.txt' }, {
  419. content: [{ type: 'text', text: envelope }],
  420. isError: true,
  421. })).toBeUndefined()
  422. expect(await presentResult('read', { file_path: 'a.txt' }, {
  423. content: [{ type: 'text', text: envelope }, { type: 'text', text: 'second' }],
  424. isError: false,
  425. })).toBeUndefined()
  426. expect(await presentResult('read', { file_path: 'a.txt' }, {
  427. content: [{ type: 'reasoning', text: envelope }],
  428. isError: false,
  429. })).toBeUndefined()
  430. })
  431. it('read: "from line N" window when only offset is set', async () => {
  432. expect(await presentCall('read', { file_path: 'a.txt', offset: 5 })).toEqual({
  433. card: 'generic', title: 'Read a.txt (from line 5)', kind: 'read', locations: [{ path: 'a.txt', line: 5 }],
  434. })
  435. })
  436. it('write: diff card (new-file style, oldText null), location', async () => {
  437. expect(await presentCall('write', { file_path: 'out.txt', content: 'hello' })).toEqual({
  438. card: 'diff', title: 'Write out.txt',
  439. diffs: [{ path: 'out.txt', oldText: null, newText: 'hello' }],
  440. locations: [{ path: 'out.txt' }],
  441. })
  442. })
  443. it('read: a limit with no offset windows from line 1', async () => {
  444. expect(await presentCall('read', { file_path: 'a.txt', limit: 10 })).toEqual({
  445. card: 'generic', title: 'Read a.txt (1 - 10)', kind: 'read', locations: [{ path: 'a.txt', line: 1 }],
  446. })
  447. })
  448. it('edit: an empty old_string maps to oldText null (a whole-file replace diff)', async () => {
  449. // presentCall runs on replay of raw logged args, which parseEditArgs does not
  450. // gate — an empty old_string must still produce a valid diff (oldText null).
  451. expect(await presentCall('edit', { file_path: 'a.txt', old_string: '', new_string: 'seed' })).toEqual({
  452. card: 'diff', title: 'Edit a.txt',
  453. diffs: [{ path: 'a.txt', oldText: null, newText: 'seed' }],
  454. locations: [{ path: 'a.txt' }],
  455. })
  456. })
  457. })
  458. describe('result-time contextual diff (meta + presentResult)', () => {
  459. // An edit records the applied contextual hunk on `tool/result` meta, and the tool's
  460. // presentResult narrows it back into a replayable `diff` result card.
  461. const withContext = 'a\nb\nc\nOLD\nd\ne\nf\n'
  462. it('edit: execute attaches the applied hunk as meta { diffs }', async () => {
  463. const { ctx, fs } = await setup()
  464. const session = { header: {} }
  465. fs.files.set('key:a.txt', withContext)
  466. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  467. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'OLD', new_string: 'NEW' }, { session })
  468. expect(result.isError).toBe(false)
  469. expect(result.meta).toEqual({
  470. diffs: [{ path: 'a.txt', oldText: 'a\nb\nc\nOLD\nd\ne\nf', newText: 'a\nb\nc\nNEW\nd\ne\nf' }],
  471. })
  472. })
  473. it('edit: presentResult turns the meta into a diff result card', async () => {
  474. const { ctx, fs } = await setup()
  475. const session = { header: {} }
  476. fs.files.set('key:a.txt', withContext)
  477. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  478. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'OLD', new_string: 'NEW' }, { session })
  479. const view = ctx.tools.get('edit')?.presentResult?.({ file_path: 'a.txt', old_string: 'OLD', new_string: 'NEW' }, result)
  480. expect(view).toEqual({
  481. card: 'diff', title: 'Edit a.txt',
  482. diffs: [{ path: 'a.txt', oldText: 'a\nb\nc\nOLD\nd\ne\nf', newText: 'a\nb\nc\nNEW\nd\ne\nf' }],
  483. })
  484. })
  485. it('write OVERWRITE: execute attaches a contextual hunk; presentResult renders a diff card', async () => {
  486. const { ctx, fs } = await setup()
  487. const session = { header: {} }
  488. fs.files.set('key:a.txt', withContext)
  489. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  490. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'a\nb\nc\nNEW\nd\ne\nf\n' }, { session })
  491. expect(result.isError).toBe(false)
  492. expect(result.meta).toEqual({ diffs: [{ path: 'a.txt', oldText: 'a\nb\nc\nOLD\nd\ne\nf', newText: 'a\nb\nc\nNEW\nd\ne\nf' }] })
  493. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'x' }, result)
  494. expect(view).toEqual({ card: 'diff', title: 'Write a.txt', diffs: [{ path: 'a.txt', oldText: 'a\nb\nc\nOLD\nd\ne\nf', newText: 'a\nb\nc\nNEW\nd\ne\nf' }] })
  495. })
  496. it('write CREATE: an empty applied-diff projection still falls back to the whole-file diff card', async () => {
  497. // A create has no prior content, yet the completed replacement view must
  498. // remain a diff instead of clobbering the pending new-file diff with text.
  499. const { ctx } = await setup()
  500. const session = { header: {} }
  501. const result = await call(ctx, 'write', { file_path: 'new.txt', content: 'fresh\n' }, { session })
  502. expect(result.isError).toBe(false)
  503. expect(result.meta).toEqual({ diffs: [] })
  504. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'new.txt', content: 'fresh\n' }, result)
  505. expect(view).toEqual({ card: 'diff', title: 'Write new.txt', diffs: [{ path: 'new.txt', oldText: null, newText: 'fresh\n' }] })
  506. })
  507. it('write OVERWRITE with identical content: an empty applied-diff projection falls back to a whole-file diff', async () => {
  508. const { ctx, fs } = await setup()
  509. const session = { header: {} }
  510. fs.files.set('key:a.txt', 'same\n')
  511. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  512. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'same\n' }, { session })
  513. expect(result.isError).toBe(false)
  514. expect(result.meta).toEqual({ diffs: [] })
  515. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'same\n' }, result)
  516. expect(view).toEqual({ card: 'diff', title: 'Write a.txt', diffs: [{ path: 'a.txt', oldText: null, newText: 'same\n' }] })
  517. })
  518. it('presentResult returns undefined on an error result (nothing applied)', async () => {
  519. const { ctx } = await setup()
  520. const errorResult = { content: [{ type: 'text' as const, text: 'Error: boom' }], isError: true }
  521. expect(ctx.tools.get('edit')?.presentResult?.({ file_path: 'a.txt', old_string: 'x', new_string: 'y' }, errorResult)).toBeUndefined()
  522. expect(ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'y' }, errorResult)).toBeUndefined()
  523. })
  524. it('edit presentResult returns undefined on malformed meta (defensive narrowing)', async () => {
  525. // edit has no whole-file fallback (only a literal replacement), so a malformed
  526. // meta yields the generic "updated successfully" rendering.
  527. const { ctx } = await setup()
  528. const badMeta = { content: [{ type: 'text' as const, text: 'ok' }], isError: false, meta: { diffs: 'nope' } }
  529. expect(ctx.tools.get('edit')?.presentResult?.({ file_path: 'a.txt', old_string: 'x', new_string: 'y' }, badMeta)).toBeUndefined()
  530. })
  531. it('write presentResult falls back to a whole-file diff on malformed meta (never leaks the result text)', async () => {
  532. // write always renders a diff card so the completed update can't clobber the
  533. // pending diff with the model-facing text; a malformed meta falls back to the
  534. // args-derived whole-file diff, same as a create.
  535. const { ctx } = await setup()
  536. const badMeta = { content: [{ type: 'text' as const, text: 'ok' }], isError: false, meta: { diffs: 'nope' } }
  537. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'y' }, badMeta)
  538. expect(view).toEqual({ card: 'diff', title: 'Write a.txt', diffs: [{ path: 'a.txt', oldText: null, newText: 'y' }] })
  539. })
  540. })
  541. describe('read caps are plugin config', () => {
  542. async function setupWith(config: ToolFs.Config) {
  543. const ctx = new Context()
  544. await ctx.plugin(SystemPrompt)
  545. await ctx.plugin(ToolRegistry)
  546. await ctx.plugin(FakeFs)
  547. await ctx.plugin(FsPolicy)
  548. await ctx.plugin(ToolFs, config)
  549. return { ctx, fs: ctx.fs as FakeFs }
  550. }
  551. it('a configured readLimit is both the default and the cap, and the schema names it', async () => {
  552. const { ctx, fs } = await setupWith({ readLimit: 2 })
  553. fs.files.set('key:a.txt', 'one\ntwo\nthree\nfour')
  554. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  555. expect(text(result)).toContain('(Showing lines 1-2 of 4. Use offset=3 to continue.)')
  556. const overCap = await call(ctx, 'read', { file_path: 'a.txt', limit: 3 })
  557. expect(overCap.isError).toBe(true)
  558. expect(text(overCap)).toContain('less than or equal to 2')
  559. const readSchema = ctx.tools.schemas().find(s => s.name === 'read')
  560. expect(JSON.stringify(readSchema)).toContain('Defaults to 2.')
  561. })
  562. it('a configured readMaxLineLength truncates lines at the configured length', async () => {
  563. const { ctx, fs } = await setupWith({ readMaxLineLength: 4 })
  564. fs.files.set('key:a.txt', 'abcdefgh')
  565. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  566. expect(text(result)).toContain('1: abcd... (line truncated to 4 chars)')
  567. })
  568. it('a configured readMaxBytes caps the window at the configured bytes', async () => {
  569. const { ctx, fs } = await setupWith({ readMaxBytes: 9 })
  570. fs.files.set('key:a.txt', 'aaaa\nbbbb\ncccc')
  571. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  572. expect(result.isError).toBe(false)
  573. if (result.isError) throw new Error('expected read success')
  574. expect(result.value).toMatchObject({ totalLines: 3 })
  575. expect(text(result)).toContain('Output capped.')
  576. expect(text(result)).not.toContain('cccc')
  577. })
  578. it('a configured readStreamMinSize routes smaller files to the streaming path', async () => {
  579. const { ctx, fs } = await setupWith({ readStreamMinSize: 5 })
  580. fs.files.set('key:a.txt', 'alpha\nbeta')
  581. const readSpy = vi.spyOn(fs, 'readText')
  582. const streamSpy = vi.spyOn(fs, 'streamText')
  583. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  584. expect(result.isError).toBe(false)
  585. expect(streamSpy).toHaveBeenCalled()
  586. expect(readSpy).not.toHaveBeenCalled()
  587. })
  588. it.each([
  589. ['readLimit', { readLimit: 0 }],
  590. ['readLimit', { readLimit: 2.5 }],
  591. ['readMaxLineLength', { readMaxLineLength: -1 }],
  592. ['readMaxBytes', { readMaxBytes: Number.NaN }],
  593. ['readStreamMinSize', { readStreamMinSize: 0 }],
  594. ] as const)('rejects a non-positive or fractional %s at load', async (name, config) => {
  595. const ctx = new Context()
  596. await ctx.plugin(SystemPrompt)
  597. await ctx.plugin(ToolRegistry)
  598. await ctx.plugin(FakeFs)
  599. await expect(ctx.plugin(ToolFs, config)).rejects.toThrow(new RegExp(`tool-fs: ${name} must be a positive integer`))
  600. })
  601. it('has no default export (namespace plugin export shape)', () => {
  602. expect('default' in ToolFs).toBe(false)
  603. })
  604. })
  605. describe('sandbox escalation surface (write/edit)', () => {
  606. /** A confining fake `ctx.fs`: reports a default mode, records each per-call policy, and can arm a sandbox denial. */
  607. class SandboxingFakeFs extends FakeFs {
  608. stamped: (SandboxExecutionPolicy | undefined)[] = []
  609. override get sandboxMode(): SandboxMode {
  610. return 'workspace-write'
  611. }
  612. override async writeText(
  613. target: FsTarget,
  614. content: string,
  615. expected?: FsWriteIntent,
  616. _signal?: AbortSignal,
  617. sandboxPolicy?: SandboxExecutionPolicy,
  618. ): Promise<FsWriteOutcome> {
  619. this.stamped.push(sandboxPolicy)
  620. return super.writeText(target, content, expected)
  621. }
  622. override async editText(
  623. target: FsTarget,
  624. edit: FsEditRequest,
  625. expected?: { version: FsVersion },
  626. _signal?: AbortSignal,
  627. sandboxPolicy?: SandboxExecutionPolicy,
  628. ): Promise<FsEditOutcome> {
  629. this.stamped.push(sandboxPolicy)
  630. return super.editText(target, edit, expected)
  631. }
  632. }
  633. async function setupConfining(opts: { approval?: boolean } = {}) {
  634. const ctx = new Context()
  635. await ctx.plugin(SystemPrompt)
  636. await ctx.plugin(ToolRegistry)
  637. await ctx.plugin(SandboxPolicyService, { mode: 'workspace-write' })
  638. await ctx.plugin(SandboxingFakeFs)
  639. await ctx.plugin(FsPolicy)
  640. if (opts.approval === true) await ctx.plugin(ApprovalService)
  641. await ctx.plugin(ToolFs)
  642. return { ctx, fs: ctx.fs as SandboxingFakeFs }
  643. }
  644. /** A fake agent whose session records appends (the approval audit surface), mid-turn, carrying the given events for the fold. */
  645. function escalationAgent(events: Array<{ type: string; data?: Record<string, unknown> }> = []): object {
  646. return {
  647. id: 'agent-fs-esc',
  648. session: {
  649. header: { version: 0, id: 'sess-fs-esc', createdAt: 0, cwd: '/session-project' },
  650. events: [{ type: 'turn/start' }, ...events],
  651. append: (type: string, data: Record<string, unknown>) => { events.push({ type, data }) },
  652. },
  653. }
  654. }
  655. function fsSchema(ctx: Context, name: 'write' | 'edit') {
  656. const schema = ctx.tools.schemas().find(s => s.name === name)
  657. if (!schema) throw new Error(`${name} tool not registered`)
  658. return schema as unknown as { parameters: { properties: Record<string, { enum?: string[] }> } }
  659. }
  660. it('fails load when a confining filesystem has no shared sandbox-policy resolver', async () => {
  661. const ctx = new Context()
  662. await ctx.plugin(SystemPrompt)
  663. await ctx.plugin(ToolRegistry)
  664. await ctx.plugin(SandboxingFakeFs)
  665. await expect(ctx.plugin(ToolFs)).rejects.toThrow('tool-fs: the mounted filesystem confines but ctx.sandboxPolicy is missing')
  666. })
  667. it('advertises no escalation fields under a non-confining backend', async () => {
  668. const { ctx } = await setup()
  669. expect(ctx.fs.sandboxMode).toBeUndefined()
  670. for (const name of ['write', 'edit'] as const) {
  671. const props = fsSchema(ctx, name).parameters.properties
  672. expect(props['sandbox_permissions']).toBeUndefined()
  673. expect(props['justification']).toBeUndefined()
  674. }
  675. })
  676. it('advertises the closed target vocabulary on write and edit under a confining backend', async () => {
  677. const { ctx } = await setupConfining()
  678. for (const name of ['write', 'edit'] as const) {
  679. const props = fsSchema(ctx, name).parameters.properties
  680. expect(props['sandbox_permissions']?.enum).toEqual(['workspace-write', 'danger-full-access'])
  681. expect(props['justification']).toBeDefined()
  682. }
  683. })
  684. it('a plain write stamps the default mode with the calling session root', async () => {
  685. const { ctx, fs } = await setupConfining()
  686. await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent())
  687. expect(fs.stamped).toEqual([{ mode: 'workspace-write', workspaceRoot: resolve('/session-project') }])
  688. })
  689. it('a standing session override folds onto the stamp', async () => {
  690. const { ctx, fs } = await setupConfining()
  691. await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent([{ type: 'sandbox/mode', data: { mode: 'read-only' } }]))
  692. expect(fs.stamped).toEqual([{ mode: 'read-only', workspaceRoot: resolve('/session-project') }])
  693. })
  694. it('a denied write maps to the shared marker plus the escalation hint (isError)', async () => {
  695. const { ctx, fs } = await setupConfining()
  696. fs.rejectWith = new FsError('denied', 'FS_SANDBOX_DENIED')
  697. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent())
  698. expect(result.isError).toBe(true)
  699. expect(text(result)).toContain('[sandbox: file access denied under workspace-write mode]')
  700. expect(text(result)).toContain('retry this exact operation once with sandbox_permissions')
  701. })
  702. it('a non-FS_SANDBOX_DENIED provider error passes through unchanged', async () => {
  703. const { ctx, fs } = await setupConfining()
  704. fs.rejectWith = new FsError('boom', 'FS_IO_ERROR')
  705. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent())
  706. expect(result.isError).toBe(true)
  707. expect(text(result)).toContain('boom')
  708. expect(text(result)).not.toContain('[sandbox:')
  709. })
  710. it('an approved escalation stamps the granted mode onto that write', async () => {
  711. const { ctx, fs } = await setupConfining({ approval: true })
  712. ctx.on('approval/request', () => Promise.resolve('allowed-once' as const))
  713. // Pass a signal so the escalation ask forwards it to the approval request
  714. // (the request rides the tool-execution abort signal).
  715. await ctx.tools.execute({
  716. callId: CallId('call-fs-esc-grant'),
  717. name: 'write',
  718. arguments: { file_path: 'a.txt', content: 'x', sandbox_permissions: 'danger-full-access', justification: 'the test needs it' },
  719. agent: escalationAgent() as never,
  720. signal: new AbortController().signal,
  721. })
  722. expect(fs.stamped).toEqual([{ mode: 'danger-full-access', workspaceRoot: resolve('/session-project') }])
  723. })
  724. it('a rejected escalation fails closed with its own text and never mutates', async () => {
  725. const { ctx, fs } = await setupConfining({ approval: true })
  726. ctx.on('approval/request', () => Promise.resolve('rejected' as const))
  727. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'x', new_string: 'y', sandbox_permissions: 'danger-full-access', justification: 'the test needs it' }, escalationAgent())
  728. expect(result.isError).toBe(true)
  729. expect(text(result)).toContain('the user rejected escalating this operation to "danger-full-access"')
  730. expect(fs.stamped).toEqual([])
  731. })
  732. it('escalation without an approval service fails closed', async () => {
  733. const { ctx } = await setupConfining()
  734. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'danger-full-access', justification: 'why' }, escalationAgent())
  735. expect(result.isError).toBe(true)
  736. expect(text(result)).toContain('no approval service is composed')
  737. })
  738. it('escalation with an approval service but no agent fails closed', async () => {
  739. const { ctx } = await setupConfining({ approval: true })
  740. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'danger-full-access', justification: 'why' })
  741. expect(result.isError).toBe(true)
  742. expect(text(result)).toContain('no agent to route it through')
  743. })
  744. it('rejects the escalation argument pairing (one field without the other)', async () => {
  745. const { ctx } = await setupConfining()
  746. const missing = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'workspace-write' }, escalationAgent())
  747. expect(missing.isError).toBe(true)
  748. expect(text(missing)).toContain('sandbox_permissions requires a justification')
  749. })
  750. it('sandbox_permissions under a non-confining backend fails closed (unadvertised field still reaches execute)', async () => {
  751. const { ctx } = await setup()
  752. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'workspace-write', justification: 'why' }, escalationAgent())
  753. expect(result.isError).toBe(true)
  754. expect(text(result)).toContain('not available in this composition')
  755. })
  756. })