tools.spec.ts 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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, 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 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 ACP
  383. // card's title/kind and the `locations` an editor 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. it('read: generic card titled by file with the read window, read kind, location with the offset line', async () => {
  389. expect(await presentCall('read', { file_path: 'src/a.ts', offset: 12, limit: 40 })).toEqual({
  390. card: 'generic', title: 'Read src/a.ts (12 - 51)', kind: 'read',
  391. locations: [{ path: 'src/a.ts', line: 12 }],
  392. })
  393. })
  394. it('read: bare title and line-1 location when offset/limit are unset', async () => {
  395. expect(await presentCall('read', { file_path: 'a.txt' })).toEqual({
  396. card: 'generic', title: 'Read a.txt', kind: 'read', locations: [{ path: 'a.txt', line: 1 }],
  397. })
  398. })
  399. it('read: "from line N" window when only offset is set', async () => {
  400. expect(await presentCall('read', { file_path: 'a.txt', offset: 5 })).toEqual({
  401. card: 'generic', title: 'Read a.txt (from line 5)', kind: 'read', locations: [{ path: 'a.txt', line: 5 }],
  402. })
  403. })
  404. it('write: diff card (new-file style, oldText null), location', async () => {
  405. expect(await presentCall('write', { file_path: 'out.txt', content: 'hello' })).toEqual({
  406. card: 'diff', title: 'Write out.txt',
  407. diffs: [{ path: 'out.txt', oldText: null, newText: 'hello' }],
  408. locations: [{ path: 'out.txt' }],
  409. })
  410. })
  411. it('read: a limit with no offset windows from line 1', async () => {
  412. expect(await presentCall('read', { file_path: 'a.txt', limit: 10 })).toEqual({
  413. card: 'generic', title: 'Read a.txt (1 - 10)', kind: 'read', locations: [{ path: 'a.txt', line: 1 }],
  414. })
  415. })
  416. it('edit: an empty old_string maps to oldText null (a whole-file replace diff)', async () => {
  417. // presentCall runs on replay of raw logged args, which parseEditArgs does not
  418. // gate — an empty old_string must still produce a valid diff (oldText null).
  419. expect(await presentCall('edit', { file_path: 'a.txt', old_string: '', new_string: 'seed' })).toEqual({
  420. card: 'diff', title: 'Edit a.txt',
  421. diffs: [{ path: 'a.txt', oldText: null, newText: 'seed' }],
  422. locations: [{ path: 'a.txt' }],
  423. })
  424. })
  425. })
  426. describe('result-time contextual diff (meta + presentResult)', () => {
  427. // An edit records the applied contextual hunk on `tool/result` meta, and the tool's
  428. // presentResult narrows it back into a `diff` result card the bridge renders.
  429. const withContext = 'a\nb\nc\nOLD\nd\ne\nf\n'
  430. it('edit: execute attaches the applied hunk as meta { diffs }', async () => {
  431. const { ctx, fs } = await setup()
  432. const session = { header: {} }
  433. fs.files.set('key:a.txt', withContext)
  434. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  435. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'OLD', new_string: 'NEW' }, { session })
  436. expect(result.isError).toBe(false)
  437. expect(result.meta).toEqual({
  438. diffs: [{ path: 'a.txt', oldText: 'a\nb\nc\nOLD\nd\ne\nf', newText: 'a\nb\nc\nNEW\nd\ne\nf' }],
  439. })
  440. })
  441. it('edit: presentResult turns the meta into a diff result card', async () => {
  442. const { ctx, fs } = await setup()
  443. const session = { header: {} }
  444. fs.files.set('key:a.txt', withContext)
  445. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  446. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'OLD', new_string: 'NEW' }, { session })
  447. const view = ctx.tools.get('edit')?.presentResult?.({ file_path: 'a.txt', old_string: 'OLD', new_string: 'NEW' }, result)
  448. expect(view).toEqual({
  449. card: 'diff', title: 'Edit a.txt',
  450. diffs: [{ path: 'a.txt', oldText: 'a\nb\nc\nOLD\nd\ne\nf', newText: 'a\nb\nc\nNEW\nd\ne\nf' }],
  451. })
  452. })
  453. it('write OVERWRITE: execute attaches a contextual hunk; presentResult renders a diff card', async () => {
  454. const { ctx, fs } = await setup()
  455. const session = { header: {} }
  456. fs.files.set('key:a.txt', withContext)
  457. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  458. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'a\nb\nc\nNEW\nd\ne\nf\n' }, { session })
  459. expect(result.isError).toBe(false)
  460. expect(result.meta).toEqual({ diffs: [{ path: 'a.txt', oldText: 'a\nb\nc\nOLD\nd\ne\nf', newText: 'a\nb\nc\nNEW\nd\ne\nf' }] })
  461. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'x' }, result)
  462. 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' }] })
  463. })
  464. it('write CREATE: an empty applied-diff projection still falls back to the whole-file diff card', async () => {
  465. // A create has no prior content, yet the completed card must be a `diff` — an
  466. // ACP tool_call_update.content REPLACES the call's content, so a non-diff result would
  467. // clobber the pending new-file diff.
  468. const { ctx } = await setup()
  469. const session = { header: {} }
  470. const result = await call(ctx, 'write', { file_path: 'new.txt', content: 'fresh\n' }, { session })
  471. expect(result.isError).toBe(false)
  472. expect(result.meta).toEqual({ diffs: [] })
  473. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'new.txt', content: 'fresh\n' }, result)
  474. expect(view).toEqual({ card: 'diff', title: 'Write new.txt', diffs: [{ path: 'new.txt', oldText: null, newText: 'fresh\n' }] })
  475. })
  476. it('write OVERWRITE with identical content: an empty applied-diff projection falls back to a whole-file diff', async () => {
  477. const { ctx, fs } = await setup()
  478. const session = { header: {} }
  479. fs.files.set('key:a.txt', 'same\n')
  480. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  481. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'same\n' }, { session })
  482. expect(result.isError).toBe(false)
  483. expect(result.meta).toEqual({ diffs: [] })
  484. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'same\n' }, result)
  485. expect(view).toEqual({ card: 'diff', title: 'Write a.txt', diffs: [{ path: 'a.txt', oldText: null, newText: 'same\n' }] })
  486. })
  487. it('presentResult returns undefined on an error result (nothing applied)', async () => {
  488. const { ctx } = await setup()
  489. const errorResult = { content: [{ type: 'text' as const, text: 'Error: boom' }], isError: true }
  490. expect(ctx.tools.get('edit')?.presentResult?.({ file_path: 'a.txt', old_string: 'x', new_string: 'y' }, errorResult)).toBeUndefined()
  491. expect(ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'y' }, errorResult)).toBeUndefined()
  492. })
  493. it('edit presentResult returns undefined on malformed meta (defensive narrowing)', async () => {
  494. // edit has no whole-file fallback (only a literal replacement), so a malformed
  495. // meta yields the generic "updated successfully" rendering.
  496. const { ctx } = await setup()
  497. const badMeta = { content: [{ type: 'text' as const, text: 'ok' }], isError: false, meta: { diffs: 'nope' } }
  498. expect(ctx.tools.get('edit')?.presentResult?.({ file_path: 'a.txt', old_string: 'x', new_string: 'y' }, badMeta)).toBeUndefined()
  499. })
  500. it('write presentResult falls back to a whole-file diff on malformed meta (never leaks the result text)', async () => {
  501. // write always renders a diff card so the completed update can't clobber the
  502. // pending diff with the model-facing text; a malformed meta falls back to the
  503. // args-derived whole-file diff, same as a create.
  504. const { ctx } = await setup()
  505. const badMeta = { content: [{ type: 'text' as const, text: 'ok' }], isError: false, meta: { diffs: 'nope' } }
  506. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'y' }, badMeta)
  507. expect(view).toEqual({ card: 'diff', title: 'Write a.txt', diffs: [{ path: 'a.txt', oldText: null, newText: 'y' }] })
  508. })
  509. })
  510. describe('read caps are plugin config', () => {
  511. async function setupWith(config: ToolFs.Config) {
  512. const ctx = new Context()
  513. await ctx.plugin(SystemPrompt)
  514. await ctx.plugin(ToolRegistry)
  515. await ctx.plugin(FakeFs)
  516. await ctx.plugin(FsPolicy)
  517. await ctx.plugin(ToolFs, config)
  518. return { ctx, fs: ctx.fs as FakeFs }
  519. }
  520. it('a configured readLimit is both the default and the cap, and the schema names it', async () => {
  521. const { ctx, fs } = await setupWith({ readLimit: 2 })
  522. fs.files.set('key:a.txt', 'one\ntwo\nthree\nfour')
  523. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  524. expect(text(result)).toContain('(Showing lines 1-2 of 4. Use offset=3 to continue.)')
  525. const overCap = await call(ctx, 'read', { file_path: 'a.txt', limit: 3 })
  526. expect(overCap.isError).toBe(true)
  527. expect(text(overCap)).toContain('less than or equal to 2')
  528. const readSchema = ctx.tools.schemas().find(s => s.name === 'read')
  529. expect(JSON.stringify(readSchema)).toContain('Defaults to 2.')
  530. })
  531. it('a configured readMaxLineLength truncates lines at the configured length', async () => {
  532. const { ctx, fs } = await setupWith({ readMaxLineLength: 4 })
  533. fs.files.set('key:a.txt', 'abcdefgh')
  534. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  535. expect(text(result)).toContain('1: abcd... (line truncated to 4 chars)')
  536. })
  537. it('a configured readMaxBytes caps the window at the configured bytes', async () => {
  538. const { ctx, fs } = await setupWith({ readMaxBytes: 9 })
  539. fs.files.set('key:a.txt', 'aaaa\nbbbb\ncccc')
  540. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  541. expect(result.isError).toBe(false)
  542. if (result.isError) throw new Error('expected read success')
  543. expect(result.value).toMatchObject({ totalLines: 3 })
  544. expect(text(result)).toContain('Output capped.')
  545. expect(text(result)).not.toContain('cccc')
  546. })
  547. it('a configured readStreamMinSize routes smaller files to the streaming path', async () => {
  548. const { ctx, fs } = await setupWith({ readStreamMinSize: 5 })
  549. fs.files.set('key:a.txt', 'alpha\nbeta')
  550. const readSpy = vi.spyOn(fs, 'readText')
  551. const streamSpy = vi.spyOn(fs, 'streamText')
  552. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  553. expect(result.isError).toBe(false)
  554. expect(streamSpy).toHaveBeenCalled()
  555. expect(readSpy).not.toHaveBeenCalled()
  556. })
  557. it.each([
  558. ['readLimit', { readLimit: 0 }],
  559. ['readLimit', { readLimit: 2.5 }],
  560. ['readMaxLineLength', { readMaxLineLength: -1 }],
  561. ['readMaxBytes', { readMaxBytes: Number.NaN }],
  562. ['readStreamMinSize', { readStreamMinSize: 0 }],
  563. ] as const)('rejects a non-positive or fractional %s at load', async (name, config) => {
  564. const ctx = new Context()
  565. await ctx.plugin(SystemPrompt)
  566. await ctx.plugin(ToolRegistry)
  567. await ctx.plugin(FakeFs)
  568. await expect(ctx.plugin(ToolFs, config)).rejects.toThrow(new RegExp(`tool-fs: ${name} must be a positive integer`))
  569. })
  570. it('has no default export (namespace plugin export shape)', () => {
  571. expect('default' in ToolFs).toBe(false)
  572. })
  573. })
  574. describe('sandbox escalation surface (write/edit)', () => {
  575. /** A confining fake `ctx.fs`: reports a default mode, records each per-call policy, and can arm a sandbox denial. */
  576. class SandboxingFakeFs extends FakeFs {
  577. stamped: (SandboxExecutionPolicy | undefined)[] = []
  578. override get sandboxMode(): SandboxMode {
  579. return 'workspace-write'
  580. }
  581. override async writeText(
  582. target: FsTarget,
  583. content: string,
  584. expected?: FsWriteIntent,
  585. _signal?: AbortSignal,
  586. sandboxPolicy?: SandboxExecutionPolicy,
  587. ): Promise<FsWriteOutcome> {
  588. this.stamped.push(sandboxPolicy)
  589. return super.writeText(target, content, expected)
  590. }
  591. override async editText(
  592. target: FsTarget,
  593. edit: FsEditRequest,
  594. expected?: { version: FsVersion },
  595. _signal?: AbortSignal,
  596. sandboxPolicy?: SandboxExecutionPolicy,
  597. ): Promise<FsEditOutcome> {
  598. this.stamped.push(sandboxPolicy)
  599. return super.editText(target, edit, expected)
  600. }
  601. }
  602. async function setupConfining(opts: { approval?: boolean } = {}) {
  603. const ctx = new Context()
  604. await ctx.plugin(SystemPrompt)
  605. await ctx.plugin(ToolRegistry)
  606. await ctx.plugin(SandboxPolicyService, { mode: 'workspace-write' })
  607. await ctx.plugin(SandboxingFakeFs)
  608. await ctx.plugin(FsPolicy)
  609. if (opts.approval === true) await ctx.plugin(ApprovalService)
  610. await ctx.plugin(ToolFs)
  611. return { ctx, fs: ctx.fs as SandboxingFakeFs }
  612. }
  613. /** A fake agent whose session records appends (the approval audit surface), mid-turn, carrying the given events for the fold. */
  614. function escalationAgent(events: Array<{ type: string; data?: Record<string, unknown> }> = []): object {
  615. return {
  616. id: 'agent-fs-esc',
  617. session: {
  618. header: { version: 0, id: 'sess-fs-esc', createdAt: 0, cwd: '/session-project' },
  619. events: [{ type: 'turn/start' }, ...events],
  620. append: (type: string, data: Record<string, unknown>) => { events.push({ type, data }) },
  621. },
  622. }
  623. }
  624. function fsSchema(ctx: Context, name: 'write' | 'edit') {
  625. const schema = ctx.tools.schemas().find(s => s.name === name)
  626. if (!schema) throw new Error(`${name} tool not registered`)
  627. return schema as unknown as { parameters: { properties: Record<string, { enum?: string[] }> } }
  628. }
  629. it('fails load when a confining filesystem has no shared sandbox-policy resolver', async () => {
  630. const ctx = new Context()
  631. await ctx.plugin(SystemPrompt)
  632. await ctx.plugin(ToolRegistry)
  633. await ctx.plugin(SandboxingFakeFs)
  634. await expect(ctx.plugin(ToolFs)).rejects.toThrow('tool-fs: the mounted filesystem confines but ctx.sandboxPolicy is missing')
  635. })
  636. it('advertises no escalation fields under a non-confining backend', async () => {
  637. const { ctx } = await setup()
  638. expect(ctx.fs.sandboxMode).toBeUndefined()
  639. for (const name of ['write', 'edit'] as const) {
  640. const props = fsSchema(ctx, name).parameters.properties
  641. expect(props['sandbox_permissions']).toBeUndefined()
  642. expect(props['justification']).toBeUndefined()
  643. }
  644. })
  645. it('advertises the closed target vocabulary on write and edit under a confining backend', async () => {
  646. const { ctx } = await setupConfining()
  647. for (const name of ['write', 'edit'] as const) {
  648. const props = fsSchema(ctx, name).parameters.properties
  649. expect(props['sandbox_permissions']?.enum).toEqual(['workspace-write', 'danger-full-access'])
  650. expect(props['justification']).toBeDefined()
  651. }
  652. })
  653. it('a plain write stamps the default mode with the calling session root', async () => {
  654. const { ctx, fs } = await setupConfining()
  655. await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent())
  656. expect(fs.stamped).toEqual([{ mode: 'workspace-write', workspaceRoot: '/session-project' }])
  657. })
  658. it('a standing session override folds onto the stamp', async () => {
  659. const { ctx, fs } = await setupConfining()
  660. await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent([{ type: 'sandbox/mode', data: { mode: 'read-only' } }]))
  661. expect(fs.stamped).toEqual([{ mode: 'read-only', workspaceRoot: '/session-project' }])
  662. })
  663. it('a denied write maps to the shared marker plus the escalation hint (isError)', async () => {
  664. const { ctx, fs } = await setupConfining()
  665. fs.rejectWith = new FsError('denied', 'FS_SANDBOX_DENIED')
  666. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent())
  667. expect(result.isError).toBe(true)
  668. expect(text(result)).toContain('[sandbox: file access denied under workspace-write mode]')
  669. expect(text(result)).toContain('retry this exact operation once with sandbox_permissions')
  670. })
  671. it('a non-FS_SANDBOX_DENIED provider error passes through unchanged', async () => {
  672. const { ctx, fs } = await setupConfining()
  673. fs.rejectWith = new FsError('boom', 'FS_IO_ERROR')
  674. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent())
  675. expect(result.isError).toBe(true)
  676. expect(text(result)).toContain('boom')
  677. expect(text(result)).not.toContain('[sandbox:')
  678. })
  679. it('an approved escalation stamps the granted mode onto that write', async () => {
  680. const { ctx, fs } = await setupConfining({ approval: true })
  681. ctx.on('approval/request', () => Promise.resolve('allowed-once' as const))
  682. // Pass a signal so the escalation ask forwards it to the approval request
  683. // (the request rides the tool-execution abort signal).
  684. await ctx.tools.execute({
  685. callId: CallId('call-fs-esc-grant'),
  686. name: 'write',
  687. arguments: { file_path: 'a.txt', content: 'x', sandbox_permissions: 'danger-full-access', justification: 'the test needs it' },
  688. agent: escalationAgent() as never,
  689. signal: new AbortController().signal,
  690. })
  691. expect(fs.stamped).toEqual([{ mode: 'danger-full-access', workspaceRoot: '/session-project' }])
  692. })
  693. it('a rejected escalation fails closed with its own text and never mutates', async () => {
  694. const { ctx, fs } = await setupConfining({ approval: true })
  695. ctx.on('approval/request', () => Promise.resolve('rejected' as const))
  696. 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())
  697. expect(result.isError).toBe(true)
  698. expect(text(result)).toContain('the user rejected escalating this operation to "danger-full-access"')
  699. expect(fs.stamped).toEqual([])
  700. })
  701. it('escalation without an approval service fails closed', async () => {
  702. const { ctx } = await setupConfining()
  703. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'danger-full-access', justification: 'why' }, escalationAgent())
  704. expect(result.isError).toBe(true)
  705. expect(text(result)).toContain('no approval service is composed')
  706. })
  707. it('escalation with an approval service but no agent fails closed', async () => {
  708. const { ctx } = await setupConfining({ approval: true })
  709. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'danger-full-access', justification: 'why' })
  710. expect(result.isError).toBe(true)
  711. expect(text(result)).toContain('no agent to route it through')
  712. })
  713. it('rejects the escalation argument pairing (one field without the other)', async () => {
  714. const { ctx } = await setupConfining()
  715. const missing = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'workspace-write' }, escalationAgent())
  716. expect(missing.isError).toBe(true)
  717. expect(text(missing)).toContain('sandbox_permissions requires a justification')
  718. })
  719. it('sandbox_permissions under a non-confining backend fails closed (unadvertised field still reaches execute)', async () => {
  720. const { ctx } = await setup()
  721. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'workspace-write', justification: 'why' }, escalationAgent())
  722. expect(result.isError).toBe(true)
  723. expect(text(result)).toContain('not available in this composition')
  724. })
  725. })