tools.spec.ts 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /**
  2. * Consumer API 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 '@deepseek-ai/cordis'
  7. import { CodeRuntime } from '@deepseek-ai/dsh-code-runtime'
  8. import { createScope, type Scope } from '@deepseek-ai/dsh-scope'
  9. import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync } from 'node:fs'
  10. import { tmpdir } from 'node:os'
  11. import { join, resolve, sep } from 'node:path'
  12. import { turnBoundaryProjectionDefinition } from '@deepseek-ai/dsh-agent-loop'
  13. import { ToolCallId } from '@deepseek-ai/dsh-llm'
  14. import SystemPrompt, { renderPrompt } from '@deepseek-ai/dsh-system-prompt'
  15. import ToolRuntime, { type ToolResult } from '@deepseek-ai/dsh-tools'
  16. import { FileSystem, FsError, FsTargetKey, FsVersion } from '@deepseek-ai/dsh-fs'
  17. import type {
  18. FsDirEntry,
  19. FsEditOutcome,
  20. FsEditRequest,
  21. FsInfo,
  22. FsPathInfo,
  23. FsTarget,
  24. FsWriteIntent,
  25. FsWriteOutcome,
  26. } from '@deepseek-ai/dsh-fs'
  27. import * as FsPolicy from '@deepseek-ai/dsh-fs-observation-policy'
  28. import * as ToolFs from '@deepseek-ai/dsh-tool-fs'
  29. import { STREAM_MIN_SIZE } from '../src/read.ts'
  30. import { formatReadOutput } from '../src/read-render.ts'
  31. import type { FileReadOutcome } from '../src/read-render.ts'
  32. import { sessionCwd } from '../src/session-cwd.ts'
  33. import ApprovalService from '@deepseek-ai/dsh-user-approval'
  34. import type { SandboxExecutionPolicy, SandboxMode } from '@deepseek-ai/dsh-sandbox'
  35. import SandboxPolicyService from '@deepseek-ai/dsh-sandbox-policy'
  36. import { SessionId, SessionLogOffset, SessionSeq } from '@deepseek-ai/dsh-session'
  37. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  38. const testToolSignal = new AbortController().signal
  39. /** An in-memory fake provider; a test can arm a rejection on any primitive. */
  40. class FakeFs extends FileSystem {
  41. files = new Map<string, string>()
  42. rejectWith?: FsError
  43. writeIntents: (FsWriteIntent | undefined)[] = []
  44. editIntents: ({ version: FsVersion } | undefined)[] = []
  45. private throwIfArmed(): void {
  46. if (this.rejectWith) throw this.rejectWith
  47. }
  48. override async resolve(path: string): Promise<FsTarget> {
  49. return { targetKey: FsTargetKey(`key:${path}`), displayPath: `/abs/${path}` }
  50. }
  51. override processPath(target: FsTarget): string { return String(target.targetKey) }
  52. override fileUrl(target: FsTarget): string { return `file://${target.targetKey}` }
  53. override contains(parent: FsTarget, child: FsTarget): boolean {
  54. return child.targetKey === parent.targetKey || String(child.targetKey).startsWith(`${parent.targetKey}/`)
  55. }
  56. override async stat(target: FsTarget): Promise<FsInfo | undefined> {
  57. this.throwIfArmed()
  58. const content = this.files.get(target.targetKey)
  59. if (content === undefined) return undefined
  60. return { version: FsVersion('v1'), type: 'file', size: content.length }
  61. }
  62. override async lstat(path: string): Promise<FsPathInfo | undefined> {
  63. const content = this.files.get(`key:${path}`)
  64. if (content === undefined) return undefined
  65. return { version: FsVersion('v1'), type: 'file', size: content.length }
  66. }
  67. override async readText(target: FsTarget): Promise<string> {
  68. return this.files.get(target.targetKey) ?? ''
  69. }
  70. override async streamText(target: FsTarget): Promise<AsyncIterable<string>> {
  71. const content = this.files.get(target.targetKey) ?? ''
  72. return (async function* () { yield content })()
  73. }
  74. override async readBytes(target: FsTarget, _signal: AbortSignal | undefined, maxBytes: number): Promise<Uint8Array> {
  75. const bytes = new TextEncoder().encode(this.files.get(target.targetKey) ?? '')
  76. if (bytes.length > maxBytes) {
  77. throw new FsError(`too large: ${target.displayPath}`, 'FS_TOO_LARGE')
  78. }
  79. return bytes
  80. }
  81. override async readByteRange(target: FsTarget, range: { offset: number; length: number }): Promise<Uint8Array> {
  82. return new TextEncoder().encode(this.files.get(target.targetKey) ?? '').subarray(range.offset, range.offset + range.length)
  83. }
  84. override async listDir(_target: FsTarget): Promise<FsDirEntry[]> {
  85. return []
  86. }
  87. override async writeText(target: FsTarget, content: string, expected?: FsWriteIntent): Promise<FsWriteOutcome> {
  88. this.throwIfArmed()
  89. this.writeIntents.push(expected)
  90. const before = this.files.get(target.targetKey) ?? null
  91. this.files.set(target.targetKey, content)
  92. return { operation: before !== null ? 'update' : 'create', version: FsVersion('v2'), before, after: content }
  93. }
  94. override async editText(target: FsTarget, edit: FsEditRequest, expected?: { version: FsVersion }): Promise<FsEditOutcome> {
  95. this.throwIfArmed()
  96. this.editIntents.push(expected)
  97. const content = this.files.get(target.targetKey) ?? ''
  98. const after = content.split(edit.oldString).join(edit.newString)
  99. this.files.set(target.targetKey, after)
  100. return { version: FsVersion('v3'), before: content, after }
  101. }
  102. }
  103. async function setup() {
  104. const ctx = new Context()
  105. await ctx.plugin(SystemPrompt)
  106. await ctx.plugin(ToolRuntime)
  107. await ctx.plugin(FakeFs)
  108. await ctx.plugin(FsPolicy)
  109. await ctx.plugin(ToolFs)
  110. const fs = ctx.fs as FakeFs
  111. return { ctx, fs }
  112. }
  113. let callCounter = 0
  114. function call(ctx: Context, name: string, args: unknown, agent?: object) {
  115. return ctx.tools.execute({
  116. signal: testToolSignal,
  117. callId: ToolCallId(`call-${++callCounter}`),
  118. name,
  119. arguments: args,
  120. ...agent ? { agent: agent as never } : {},
  121. })
  122. }
  123. function text(result: { content: { type: string; text?: string }[] }): string {
  124. return result.content.filter(b => b.type === 'text').map(b => b.text).join('')
  125. }
  126. describe('session cwd resolution', () => {
  127. const execution = (cwd?: string) => cwd === undefined
  128. ? {}
  129. : { agent: { session: { header: { cwd } } } }
  130. it('retains ordinary spelling but resolves the cwd before parent traversal', () => {
  131. const cwd = process.cwd()
  132. const throughParent = `${cwd}${sep}..`
  133. expect(sessionCwd(execution() as never, 'file.txt')).toBeUndefined()
  134. expect(sessionCwd(execution(cwd) as never, 'file.txt')).toBe(cwd)
  135. expect(sessionCwd(execution(throughParent) as never, 'file.txt')).toBe(realpathSync.native(throughParent))
  136. const root = mkdtempSync(join(tmpdir(), 'dsh-tool-fs-session-cwd-'))
  137. const physical = join(root, 'physical')
  138. const link = join(root, 'link')
  139. try {
  140. mkdirSync(physical)
  141. symlinkSync(physical, link, process.platform === 'win32' ? 'junction' : 'dir')
  142. expect(sessionCwd(execution(link) as never, 'child.txt')).toBe(link)
  143. expect(sessionCwd(execution(link) as never, `..${sep}parent.txt`)).toBe(realpathSync.native(link))
  144. } finally {
  145. rmSync(root, { recursive: true, force: true })
  146. }
  147. })
  148. })
  149. describe('registration', () => {
  150. it('registers read, write, and edit', async () => {
  151. const { ctx } = await setup()
  152. expect(ctx.tools.schemas().map(s => s.name).sort()).toEqual(['edit', 'read', 'write'])
  153. })
  154. it('declares read parallel-safe while write/edit remain exclusive', async () => {
  155. const { ctx } = await setup()
  156. expect(ctx.tools.executionMode({ signal: testToolSignal, callId: ToolCallId('read-safe'), name: 'read', arguments: { file_path: 'a.txt' } }))
  157. .toEqual({ kind: 'parallel' })
  158. expect(ctx.tools.executionMode({ signal: testToolSignal, callId: ToolCallId('write-exclusive'), name: 'write', arguments: { file_path: 'a.txt', content: 'x' } }))
  159. .toEqual({ kind: 'exclusive' })
  160. expect(ctx.tools.executionMode({ signal: testToolSignal, callId: ToolCallId('edit-exclusive'), name: 'edit', arguments: { file_path: 'a.txt', old_string: 'x', new_string: 'y' } }))
  161. .toEqual({ kind: 'exclusive' })
  162. })
  163. it('registers prompt sections for each tool', async () => {
  164. const { ctx } = await setup()
  165. const prompt = renderPrompt(await ctx.systemPrompt.assemble())
  166. expect(prompt).toContain('Use the read tool')
  167. expect(prompt).toContain('Use the write tool')
  168. expect(prompt).toContain('Use the edit tool')
  169. })
  170. it('stays pending until ctx.fs exists (inject)', async () => {
  171. const ctx = new Context()
  172. await ctx.plugin(SystemPrompt)
  173. await ctx.plugin(ToolRuntime)
  174. await ctx.plugin(ToolFs) // no fs provider
  175. expect(ctx.tools.schemas()).toHaveLength(0)
  176. })
  177. it('unregisters everything on fiber disposal (HMR safety)', async () => {
  178. const ctx = new Context()
  179. await ctx.plugin(SystemPrompt)
  180. await ctx.plugin(ToolRuntime)
  181. await ctx.plugin(FakeFs)
  182. await ctx.plugin(FsPolicy)
  183. const fiber = await ctx.plugin(ToolFs)
  184. // Each tool contributes BOTH a schema and a prompt section; disposal must
  185. // withdraw both, not just the schemas.
  186. expect(ctx.tools.schemas()).toHaveLength(3)
  187. const sectionNames = (a: { sections: { name: string }[] }) => a.sections.map(s => s.name).sort()
  188. expect(sectionNames(await ctx.systemPrompt.assemble())).toEqual(['deployment:persona-prefix', 'deployment:persona-suffix', 'harness:identity', 'tool:edit', 'tool:read', 'tool:write'])
  189. await fiber.dispose()
  190. expect(ctx.tools.schemas()).toHaveLength(0)
  191. // Only the system-prompt plugin's own built-in sections remain.
  192. expect(sectionNames(await ctx.systemPrompt.assemble())).toEqual(['deployment:persona-prefix', 'deployment:persona-suffix', 'harness:identity'])
  193. })
  194. })
  195. describe('read tool', () => {
  196. it('formats line-numbered content with a footer', async () => {
  197. const { ctx, fs } = await setup()
  198. fs.files.set('key:a.txt', 'hello\nworld')
  199. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  200. expect(result.isError).toBe(false)
  201. if (result.isError) throw new Error('expected read success')
  202. expect(result.value).toEqual({
  203. path: '/abs/a.txt',
  204. offset: 1,
  205. lines: [{ number: 1, text: 'hello' }, { number: 2, text: 'world' }],
  206. totalLines: 2,
  207. })
  208. expect(text(result)).toBe(`<path>/abs/a.txt</path>
  209. <type>file</type>
  210. <content>
  211. 1: hello
  212. 2: world
  213. (End of file - total 2 lines)
  214. </content>`)
  215. })
  216. it('returns an explicit empty canonical line window for an empty file', async () => {
  217. const { ctx, fs } = await setup()
  218. fs.files.set('key:empty.txt', '')
  219. const result = await call(ctx, 'read', { file_path: 'empty.txt' })
  220. if (result.isError) throw new Error('expected empty read success')
  221. expect(result.value).toEqual({ path: '/abs/empty.txt', offset: 1, lines: [], totalLines: 0 })
  222. expect(text(result)).toContain('(End of file - total 0 lines)')
  223. })
  224. it('rejects a non-positive offset via arg validation', async () => {
  225. const { ctx } = await setup()
  226. const result = await call(ctx, 'read', { file_path: 'a.txt', offset: 0 })
  227. expect(result.isError).toBe(true)
  228. expect(text(result)).toContain('offset must be a positive integer')
  229. })
  230. it('rejects a fractional offset and a zero/negative limit', async () => {
  231. const { ctx } = await setup()
  232. for (const args of [
  233. { file_path: 'a.txt', offset: 1.5 },
  234. { file_path: 'a.txt', limit: 0 },
  235. { file_path: 'a.txt', limit: -3 },
  236. ]) {
  237. const result = await call(ctx, 'read', args)
  238. expect(result.isError, JSON.stringify(args)).toBe(true)
  239. expect(text(result)).toMatch(/must be a positive integer/)
  240. }
  241. })
  242. it('rejects a non-JSON numeric offset before tool-specific validation', async () => {
  243. const { ctx } = await setup()
  244. const result = await call(ctx, 'read', { file_path: 'a.txt', offset: Number.NaN })
  245. expect(result.isError).toBe(true)
  246. expect(text(result)).toContain('tool execution arguments must be losslessly JSON-serializable')
  247. })
  248. it('rejects a limit above the cap', async () => {
  249. const { ctx } = await setup()
  250. const result = await call(ctx, 'read', { file_path: 'a.txt', limit: 99999 })
  251. expect(result.isError).toBe(true)
  252. expect(text(result)).toContain('less than or equal to 2000')
  253. })
  254. it('rejects a blank file_path', async () => {
  255. const { ctx } = await setup()
  256. const result = await call(ctx, 'read', { file_path: ' ' })
  257. expect(result.isError).toBe(true)
  258. expect(text(result)).toContain('file_path must be a non-empty string')
  259. })
  260. it('records observed state so a follow-up edit by the same session is authorized', async () => {
  261. const { ctx, fs } = await setup()
  262. const session = { header: {} }
  263. fs.files.set('key:a.txt', 'hello')
  264. expect((await call(ctx, 'read', { file_path: 'a.txt' }, { session })).isError).toBe(false)
  265. const edited = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'hello', new_string: 'bye' }, { session })
  266. expect(edited.isError).toBe(false)
  267. expect(fs.editIntents).toEqual([{ version: 'v1' }])
  268. })
  269. it('propagates FS_NOT_FOUND for an absent file', async () => {
  270. const { ctx } = await setup()
  271. const result = await call(ctx, 'read', { file_path: 'missing.txt' })
  272. expect(result.isError).toBe(true)
  273. expect(result.error).toMatchObject({ info: { code: 'FS_NOT_FOUND' } })
  274. })
  275. it('rejects a non-regular target', async () => {
  276. const { ctx, fs } = await setup()
  277. fs.files.set('key:d', '')
  278. fs.stat = async () => ({ version: FsVersion('v1'), type: 'directory' })
  279. const result = await call(ctx, 'read', { file_path: 'd' })
  280. expect(result.isError).toBe(true)
  281. expect(result.error).toMatchObject({ info: { code: 'FS_NOT_REGULAR_FILE' } })
  282. })
  283. it('streams a large file (size at/above the cap) instead of reading whole', async () => {
  284. const { ctx, fs } = await setup()
  285. fs.files.set('key:big.txt', 'alpha\nbeta')
  286. const readSpy = vi.spyOn(fs, 'readText')
  287. const streamSpy = vi.spyOn(fs, 'streamText')
  288. fs.stat = async () => ({ version: FsVersion('v1'), type: 'file', size: STREAM_MIN_SIZE })
  289. const result = await call(ctx, 'read', { file_path: 'big.txt' })
  290. expect(result.isError).toBe(false)
  291. expect(text(result)).toContain('1: alpha')
  292. expect(streamSpy).toHaveBeenCalled()
  293. expect(readSpy).not.toHaveBeenCalled()
  294. })
  295. it('streams when the backend reports no size (never buffers a size-less file)', async () => {
  296. const { ctx, fs } = await setup()
  297. fs.files.set('key:a.txt', 'alpha')
  298. const streamSpy = vi.spyOn(fs, 'streamText')
  299. fs.stat = async () => ({ version: FsVersion('v1'), type: 'file' }) // no size
  300. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  301. expect(result.isError).toBe(false)
  302. expect(streamSpy).toHaveBeenCalled()
  303. })
  304. it('surfaces a byte-capped read as a truncated footer', async () => {
  305. const { ctx, fs } = await setup()
  306. // Many long lines so the window hits the byte cap before EOF.
  307. fs.files.set('key:big.txt', Array.from({ length: 2000 }, () => 'y'.repeat(100)).join('\n'))
  308. const result = await call(ctx, 'read', { file_path: 'big.txt' })
  309. expect(result.isError).toBe(false)
  310. expect(text(result)).toContain('Output capped.')
  311. })
  312. it('attaches the structured window as presentation meta, and presentResult narrows it into a read card', async () => {
  313. const { ctx, fs } = await setup()
  314. fs.files.set('key:a.ts', 'const x = 1\nconst y = 2')
  315. const result = await call(ctx, 'read', { file_path: 'a.ts' })
  316. expect(result.isError).toBe(false)
  317. if (result.isError) throw new Error('expected read success')
  318. // The extension drives the lang hint; the window rides on persisted meta.
  319. expect(result.meta).toEqual({
  320. path: '/abs/a.ts',
  321. offset: 1,
  322. lines: [{ number: 1, text: 'const x = 1' }, { number: 2, text: 'const y = 2' }],
  323. totalLines: 2,
  324. lang: 'ts',
  325. })
  326. const view = ctx.tools.get('read')?.presentResult?.({ file_path: 'a.ts' }, result)
  327. expect(view).toEqual({
  328. card: 'read',
  329. path: '/abs/a.ts',
  330. offset: 1,
  331. lines: [{ number: 1, text: 'const x = 1' }, { number: 2, text: 'const y = 2' }],
  332. totalLines: 2,
  333. lang: 'ts',
  334. content: [{ type: 'text', text: '1: const x = 1\n2: const y = 2\n\n(End of file - total 2 lines)' }],
  335. })
  336. })
  337. it('omits the lang hint in meta for an extension that maps to no language', async () => {
  338. const { ctx, fs } = await setup()
  339. fs.files.set('key:notes', 'plain')
  340. const result = await call(ctx, 'read', { file_path: 'notes' })
  341. if (result.isError) throw new Error('expected read success')
  342. expect(result.meta).toEqual({ path: '/abs/notes', offset: 1, lines: [{ number: 1, text: 'plain' }], totalLines: 1 })
  343. })
  344. })
  345. describe('formatReadOutput footer variants', () => {
  346. const base: FileReadOutcome = { offset: 1, lines: [{ number: 1, text: 'x' }], totalLines: 1 }
  347. it('reports a byte-capped read', () => {
  348. const out = formatReadOutput('/f', { ...base, totalLines: 99, truncatedByBytes: true })
  349. expect(out).toContain('(Output capped. Showing lines 1-1. Use offset=2 to continue.)')
  350. })
  351. it('reports a more-remaining page', () => {
  352. const out = formatReadOutput('/f', { ...base, totalLines: 99 })
  353. expect(out).toContain('(Showing lines 1-1 of 99. Use offset=2 to continue.)')
  354. })
  355. it('reports end-of-file', () => {
  356. expect(formatReadOutput('/f', base)).toContain('(End of file - total 1 lines)')
  357. })
  358. it('renders an empty file as just the footer', () => {
  359. const out = formatReadOutput('/f', { ...base, lines: [], totalLines: 0 })
  360. expect(out).toContain('(End of file - total 0 lines)')
  361. expect(out).not.toContain(': ')
  362. })
  363. })
  364. describe('write tool', () => {
  365. it('formats a create result and uses createIfAbsent (unobserved, with the gate)', async () => {
  366. const { ctx, fs } = await setup()
  367. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'hi' }, { session: { header: {} } })
  368. expect(result.isError).toBe(false)
  369. if (result.isError) throw new Error('expected write success')
  370. expect(result.value).toEqual({ path: '/abs/a.txt', operation: 'create', before: null, after: 'hi' })
  371. expect(text(result)).toContain('Created file')
  372. expect(fs.writeIntents).toEqual([{ kind: 'createIfAbsent' }])
  373. })
  374. it('rejects a blank file_path', async () => {
  375. const { ctx } = await setup()
  376. const result = await call(ctx, 'write', { file_path: ' ', content: 'hi' })
  377. expect(result.isError).toBe(true)
  378. expect(text(result)).toContain('file_path must be a non-empty string')
  379. })
  380. it('propagates a backend FsError as an isError result carrying its code and remedy', async () => {
  381. const { ctx, fs } = await setup()
  382. fs.rejectWith = new FsError('blocked', 'FS_STALE_VERSION')
  383. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'hi' })
  384. expect(result.isError).toBe(true)
  385. expect(result.error).toMatchObject({ info: { name: 'FsError', code: 'FS_STALE_VERSION' } })
  386. expect(text(result)).toContain('re-read the file, then retry')
  387. })
  388. })
  389. describe('edit tool', () => {
  390. it('formats a single-replacement success after a read', async () => {
  391. const { ctx, fs } = await setup()
  392. const session = { header: {} }
  393. fs.files.set('key:a.txt', 'a')
  394. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  395. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'a', new_string: 'b' }, { session })
  396. if (result.isError) throw new Error('expected edit success')
  397. expect(result.value).toEqual({ path: '/abs/a.txt', before: 'a', after: 'b' })
  398. expect(text(result)).toBe('The file /abs/a.txt has been updated successfully.')
  399. })
  400. it('formats the replace_all success message distinctly', async () => {
  401. const { ctx, fs } = await setup()
  402. const session = { header: {} }
  403. fs.files.set('key:a.txt', 'a a a')
  404. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  405. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'a', new_string: 'b', replace_all: true }, { session })
  406. expect(text(result)).toBe('The file /abs/a.txt has been updated. All occurrences were successfully replaced.')
  407. })
  408. it('rejects identical old/new strings', async () => {
  409. const { ctx } = await setup()
  410. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'x', new_string: 'x' })
  411. expect(result.isError).toBe(true)
  412. expect(text(result)).toContain('must differ')
  413. })
  414. it('rejects an empty old_string', async () => {
  415. const { ctx } = await setup()
  416. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: '', new_string: 'x' })
  417. expect(result.isError).toBe(true)
  418. expect(text(result)).toContain('old_string must be a non-empty string')
  419. })
  420. it('rejects a blank file_path', async () => {
  421. const { ctx } = await setup()
  422. const result = await call(ctx, 'edit', { file_path: ' ', old_string: 'a', new_string: 'b' })
  423. expect(result.isError).toBe(true)
  424. expect(text(result)).toContain('file_path must be a non-empty string')
  425. })
  426. it('propagates FS_NOT_OBSERVED when the file was never read (the gate decides)', async () => {
  427. const { ctx, fs } = await setup()
  428. fs.files.set('key:a.txt', 'hello')
  429. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'a', new_string: 'b' }, { session: { header: {} } })
  430. expect(result.isError).toBe(true)
  431. expect(result.error).toMatchObject({ info: { code: 'FS_NOT_OBSERVED' } })
  432. })
  433. })
  434. describe('tool-owned presentation (pure presentCall)', () => {
  435. // presentCall is a pure display function of args (no I/O); it drives the
  436. // card's title/kind and the `locations` a UI follows along to.
  437. const presentCall = async (name: string, args: unknown) => {
  438. const { ctx } = await setup()
  439. return ctx.tools.get(name)?.presentCall?.(args)
  440. }
  441. const presentResult = async (name: string, args: unknown, result: ToolResult) => {
  442. const { ctx } = await setup()
  443. return ctx.tools.get(name)?.presentResult?.(args, result)
  444. }
  445. it('read: generic card titled by file with the read window, read kind, location with the offset line', async () => {
  446. expect(await presentCall('read', { file_path: 'src/a.ts', offset: 12, limit: 40 })).toEqual({
  447. card: 'generic', title: 'Read src/a.ts (12 - 51)', kind: 'read',
  448. locations: [{ path: 'src/a.ts', line: 12 }],
  449. })
  450. })
  451. it('read: bare title and line-1 location when offset/limit are unset', async () => {
  452. expect(await presentCall('read', { file_path: 'a.txt' })).toEqual({
  453. card: 'generic', title: 'Read a.txt', kind: 'read', locations: [{ path: 'a.txt', line: 1 }],
  454. })
  455. })
  456. it('read: completed presentation is a read card carrying the structured window with the envelope stripped', async () => {
  457. // The structured line data rides on persisted meta (the raw output object is
  458. // not on the wire); presentResult narrows it and appends the stripped text as
  459. // the no-capability `content` fallback.
  460. const meta = { path: '/tmp/a.ts', offset: 1, lines: [{ number: 1, text: 'hello' }], totalLines: 1, lang: 'ts' }
  461. expect(await presentResult('read', { file_path: 'a.ts' }, {
  462. content: [{ type: 'text', text: '<path>/tmp/a.ts</path>\n<type>file</type>\n<content>\n1: hello\n\n(End of file - total 1 lines)\n</content>' }],
  463. isError: false,
  464. meta,
  465. })).toEqual({
  466. card: 'read',
  467. path: '/tmp/a.ts',
  468. offset: 1,
  469. lines: [{ number: 1, text: 'hello' }],
  470. totalLines: 1,
  471. lang: 'ts',
  472. content: [{ type: 'text', text: '1: hello\n\n(End of file - total 1 lines)' }],
  473. })
  474. // A window whose extension maps to no language omits `lang` from the card.
  475. expect(await presentResult('read', { file_path: 'notes' }, {
  476. content: [{ type: 'text', text: '<path>/tmp/notes</path>\n<type>file</type>\n<content>\nbody\n</content>' }],
  477. isError: false,
  478. meta: { path: '/tmp/notes', offset: 1, lines: [{ number: 1, text: 'body' }], totalLines: 1 },
  479. })).toEqual({
  480. card: 'read',
  481. path: '/tmp/notes',
  482. offset: 1,
  483. lines: [{ number: 1, text: 'body' }],
  484. totalLines: 1,
  485. content: [{ type: 'text', text: 'body' }],
  486. })
  487. // Malformed envelope text with valid meta still declines (the fallback text is unavailable).
  488. expect(await presentResult('read', { file_path: 'a.ts' }, {
  489. content: [{ type: 'text', text: 'malformed replay' }],
  490. isError: false,
  491. meta,
  492. })).toBeUndefined()
  493. // Valid envelope but absent/malformed meta declines to the generic fallback.
  494. expect(await presentResult('read', { file_path: 'a.ts' }, {
  495. content: [{ type: 'text', text: '<path>/tmp/a.ts</path>\n<type>file</type>\n<content>\n1: hello\n</content>' }],
  496. isError: false,
  497. })).toBeUndefined()
  498. expect(await presentResult('read', { file_path: 'a.ts' }, {
  499. content: [{ type: 'text', text: '<path>/tmp/a.ts</path>\n<type>file</type>\n<content>\n1: hello\n</content>' }],
  500. isError: false,
  501. meta: { path: '/tmp/a.ts', lines: 'nope', totalLines: 1 },
  502. })).toBeUndefined()
  503. })
  504. it('read: completed presentation declines errors and non-single-text content', async () => {
  505. const envelope = '<path>/tmp/a.txt</path>\n<type>file</type>\n<content>\nbody\n</content>'
  506. const meta = { path: '/tmp/a.txt', offset: 1, lines: [{ number: 1, text: 'body' }], totalLines: 1 }
  507. expect(await presentResult('read', { file_path: 'a.txt' }, {
  508. content: [{ type: 'text', text: envelope }],
  509. isError: true,
  510. meta,
  511. })).toBeUndefined()
  512. expect(await presentResult('read', { file_path: 'a.txt' }, {
  513. content: [{ type: 'text', text: envelope }, { type: 'text', text: 'second' }],
  514. isError: false,
  515. meta,
  516. })).toBeUndefined()
  517. expect(await presentResult('read', { file_path: 'a.txt' }, {
  518. content: [{ type: 'reasoning', text: envelope }],
  519. isError: false,
  520. meta,
  521. })).toBeUndefined()
  522. })
  523. it('read: "from line N" window when only offset is set', async () => {
  524. expect(await presentCall('read', { file_path: 'a.txt', offset: 5 })).toEqual({
  525. card: 'generic', title: 'Read a.txt (from line 5)', kind: 'read', locations: [{ path: 'a.txt', line: 5 }],
  526. })
  527. })
  528. it('write: diff card (new-file style, oldText null), location', async () => {
  529. expect(await presentCall('write', { file_path: 'out.txt', content: 'hello' })).toEqual({
  530. card: 'diff', title: 'Write out.txt',
  531. diffs: [{ path: 'out.txt', oldText: null, newText: 'hello' }],
  532. locations: [{ path: 'out.txt' }],
  533. })
  534. })
  535. it('read: a limit with no offset windows from line 1', async () => {
  536. expect(await presentCall('read', { file_path: 'a.txt', limit: 10 })).toEqual({
  537. card: 'generic', title: 'Read a.txt (1 - 10)', kind: 'read', locations: [{ path: 'a.txt', line: 1 }],
  538. })
  539. })
  540. it('edit: an empty old_string maps to oldText null (a whole-file replace diff)', async () => {
  541. // presentCall runs on replay of raw logged args, which parseEditArgs does not
  542. // gate — an empty old_string must still produce a valid diff (oldText null).
  543. expect(await presentCall('edit', { file_path: 'a.txt', old_string: '', new_string: 'seed' })).toEqual({
  544. card: 'diff', title: 'Edit a.txt',
  545. diffs: [{ path: 'a.txt', oldText: null, newText: 'seed' }],
  546. locations: [{ path: 'a.txt' }],
  547. })
  548. })
  549. })
  550. describe('result-time contextual diff (meta + presentResult)', () => {
  551. // An edit records the applied contextual hunk on `tool/result` meta, and the tool's
  552. // presentResult narrows it back into a replayable `diff` result card.
  553. const withContext = 'a\nb\nc\nOLD\nd\ne\nf\n'
  554. it('edit: execute attaches the applied hunk as meta { diffs }', async () => {
  555. const { ctx, fs } = await setup()
  556. const session = { header: {} }
  557. fs.files.set('key:a.txt', withContext)
  558. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  559. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'OLD', new_string: 'NEW' }, { session })
  560. expect(result.isError).toBe(false)
  561. expect(result.meta).toEqual({
  562. diffs: [{ path: 'a.txt', oldText: 'a\nb\nc\nOLD\nd\ne\nf', newText: 'a\nb\nc\nNEW\nd\ne\nf' }],
  563. })
  564. })
  565. it('edit: presentResult turns the meta into a diff result card', async () => {
  566. const { ctx, fs } = await setup()
  567. const session = { header: {} }
  568. fs.files.set('key:a.txt', withContext)
  569. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  570. const result = await call(ctx, 'edit', { file_path: 'a.txt', old_string: 'OLD', new_string: 'NEW' }, { session })
  571. const view = ctx.tools.get('edit')?.presentResult?.({ file_path: 'a.txt', old_string: 'OLD', new_string: 'NEW' }, result)
  572. expect(view).toEqual({
  573. card: 'diff', title: 'Edit a.txt',
  574. diffs: [{ path: 'a.txt', oldText: 'a\nb\nc\nOLD\nd\ne\nf', newText: 'a\nb\nc\nNEW\nd\ne\nf' }],
  575. })
  576. })
  577. it('write OVERWRITE: execute attaches a contextual hunk; presentResult renders a diff card', async () => {
  578. const { ctx, fs } = await setup()
  579. const session = { header: {} }
  580. fs.files.set('key:a.txt', withContext)
  581. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  582. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'a\nb\nc\nNEW\nd\ne\nf\n' }, { session })
  583. expect(result.isError).toBe(false)
  584. expect(result.meta).toEqual({ diffs: [{ path: 'a.txt', oldText: 'a\nb\nc\nOLD\nd\ne\nf', newText: 'a\nb\nc\nNEW\nd\ne\nf' }] })
  585. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'x' }, result)
  586. 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' }] })
  587. })
  588. it('write CREATE: an empty applied-diff projection still falls back to the whole-file diff card', async () => {
  589. // A create has no prior content, yet the completed replacement view must
  590. // remain a diff instead of clobbering the pending new-file diff with text.
  591. const { ctx } = await setup()
  592. const session = { header: {} }
  593. const result = await call(ctx, 'write', { file_path: 'new.txt', content: 'fresh\n' }, { session })
  594. expect(result.isError).toBe(false)
  595. expect(result.meta).toEqual({ diffs: [] })
  596. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'new.txt', content: 'fresh\n' }, result)
  597. expect(view).toEqual({ card: 'diff', title: 'Write new.txt', diffs: [{ path: 'new.txt', oldText: null, newText: 'fresh\n' }] })
  598. })
  599. it('write OVERWRITE with identical content: an empty applied-diff projection falls back to a whole-file diff', async () => {
  600. const { ctx, fs } = await setup()
  601. const session = { header: {} }
  602. fs.files.set('key:a.txt', 'same\n')
  603. await call(ctx, 'read', { file_path: 'a.txt' }, { session })
  604. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'same\n' }, { session })
  605. expect(result.isError).toBe(false)
  606. expect(result.meta).toEqual({ diffs: [] })
  607. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'same\n' }, result)
  608. expect(view).toEqual({ card: 'diff', title: 'Write a.txt', diffs: [{ path: 'a.txt', oldText: null, newText: 'same\n' }] })
  609. })
  610. it('presentResult returns undefined on an error result (nothing applied)', async () => {
  611. const { ctx } = await setup()
  612. const errorResult = { content: [{ type: 'text' as const, text: 'Error: boom' }], isError: true }
  613. expect(ctx.tools.get('edit')?.presentResult?.({ file_path: 'a.txt', old_string: 'x', new_string: 'y' }, errorResult)).toBeUndefined()
  614. expect(ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'y' }, errorResult)).toBeUndefined()
  615. })
  616. it('edit presentResult returns undefined on malformed meta (defensive narrowing)', async () => {
  617. // edit has no whole-file fallback (only a literal replacement), so a malformed
  618. // meta yields the generic "updated successfully" rendering.
  619. const { ctx } = await setup()
  620. const badMeta = { content: [{ type: 'text' as const, text: 'ok' }], isError: false, meta: { diffs: 'nope' } }
  621. expect(ctx.tools.get('edit')?.presentResult?.({ file_path: 'a.txt', old_string: 'x', new_string: 'y' }, badMeta)).toBeUndefined()
  622. })
  623. it('write presentResult falls back to a whole-file diff on malformed meta (never leaks the result text)', async () => {
  624. // write always renders a diff card so the completed update can't clobber the
  625. // pending diff with the model-facing text; a malformed meta falls back to the
  626. // args-derived whole-file diff, same as a create.
  627. const { ctx } = await setup()
  628. const badMeta = { content: [{ type: 'text' as const, text: 'ok' }], isError: false, meta: { diffs: 'nope' } }
  629. const view = ctx.tools.get('write')?.presentResult?.({ file_path: 'a.txt', content: 'y' }, badMeta)
  630. expect(view).toEqual({ card: 'diff', title: 'Write a.txt', diffs: [{ path: 'a.txt', oldText: null, newText: 'y' }] })
  631. })
  632. })
  633. describe('read caps are plugin config', () => {
  634. async function setupWith(config: ToolFs.Config) {
  635. const ctx = new Context()
  636. await ctx.plugin(SystemPrompt)
  637. await ctx.plugin(ToolRuntime)
  638. await ctx.plugin(FakeFs)
  639. await ctx.plugin(FsPolicy)
  640. await ctx.plugin(ToolFs, config)
  641. return { ctx, fs: ctx.fs as FakeFs }
  642. }
  643. it('a configured readLimit is both the default and the cap, and the schema names it', async () => {
  644. const { ctx, fs } = await setupWith({ readLimit: 2 })
  645. fs.files.set('key:a.txt', 'one\ntwo\nthree\nfour')
  646. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  647. expect(text(result)).toContain('(Showing lines 1-2 of 4. Use offset=3 to continue.)')
  648. const overCap = await call(ctx, 'read', { file_path: 'a.txt', limit: 3 })
  649. expect(overCap.isError).toBe(true)
  650. expect(text(overCap)).toContain('less than or equal to 2')
  651. const readSchema = ctx.tools.schemas().find(s => s.name === 'read')
  652. expect(JSON.stringify(readSchema)).toContain('Defaults to 2.')
  653. })
  654. it('a configured readMaxLineLength truncates lines at the configured length', async () => {
  655. const { ctx, fs } = await setupWith({ readMaxLineLength: 4 })
  656. fs.files.set('key:a.txt', 'abcdefgh')
  657. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  658. expect(text(result)).toContain('1: abcd... (line truncated to 4 chars)')
  659. })
  660. it('a configured readMaxBytes caps the window at the configured bytes', async () => {
  661. const { ctx, fs } = await setupWith({ readMaxBytes: 9 })
  662. fs.files.set('key:a.txt', 'aaaa\nbbbb\ncccc')
  663. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  664. expect(result.isError).toBe(false)
  665. if (result.isError) throw new Error('expected read success')
  666. expect(result.value).toMatchObject({ totalLines: 3 })
  667. expect(text(result)).toContain('Output capped.')
  668. expect(text(result)).not.toContain('cccc')
  669. })
  670. it('a configured readStreamMinSize routes smaller files to the streaming path', async () => {
  671. const { ctx, fs } = await setupWith({ readStreamMinSize: 5 })
  672. fs.files.set('key:a.txt', 'alpha\nbeta')
  673. const readSpy = vi.spyOn(fs, 'readText')
  674. const streamSpy = vi.spyOn(fs, 'streamText')
  675. const result = await call(ctx, 'read', { file_path: 'a.txt' })
  676. expect(result.isError).toBe(false)
  677. expect(streamSpy).toHaveBeenCalled()
  678. expect(readSpy).not.toHaveBeenCalled()
  679. })
  680. it.each([
  681. ['readLimit', { readLimit: 0 }],
  682. ['readLimit', { readLimit: 2.5 }],
  683. ['readMaxLineLength', { readMaxLineLength: -1 }],
  684. ['readMaxBytes', { readMaxBytes: Number.NaN }],
  685. ['readStreamMinSize', { readStreamMinSize: 0 }],
  686. ] as const)('rejects a non-positive or fractional %s at load', async (name, config) => {
  687. const ctx = new Context()
  688. await ctx.plugin(SystemPrompt)
  689. await ctx.plugin(ToolRuntime)
  690. await ctx.plugin(FakeFs)
  691. await expect(ctx.plugin(ToolFs, config)).rejects.toThrow(new RegExp(`tool-fs: ${name} must be a positive integer`))
  692. })
  693. it('has no default export (namespace plugin export shape)', () => {
  694. expect('default' in ToolFs).toBe(false)
  695. })
  696. })
  697. describe('sandbox escalation API (write/edit)', () => {
  698. /** A confining fake `ctx.fs`: reports a default mode, records each per-call policy, and can arm a sandbox denial. */
  699. class SandboxingFakeFs extends FakeFs {
  700. stamped: (SandboxExecutionPolicy | undefined)[] = []
  701. override get sandboxMode(): SandboxMode {
  702. return 'workspace-write'
  703. }
  704. override async writeText(
  705. target: FsTarget,
  706. content: string,
  707. expected?: FsWriteIntent,
  708. _signal?: AbortSignal,
  709. sandboxPolicy?: SandboxExecutionPolicy,
  710. ): Promise<FsWriteOutcome> {
  711. this.stamped.push(sandboxPolicy)
  712. return super.writeText(target, content, expected)
  713. }
  714. override async editText(
  715. target: FsTarget,
  716. edit: FsEditRequest,
  717. expected?: { version: FsVersion },
  718. _signal?: AbortSignal,
  719. sandboxPolicy?: SandboxExecutionPolicy,
  720. ): Promise<FsEditOutcome> {
  721. this.stamped.push(sandboxPolicy)
  722. return super.editText(target, edit, expected)
  723. }
  724. }
  725. async function setupConfining(opts: { approval?: boolean } = {}) {
  726. const ctx = new Context()
  727. await ctx.plugin(SystemPrompt)
  728. await ctx.plugin(ToolRuntime)
  729. await ctx.plugin(SessionProjectionRegistry)
  730. ctx.sessionProjections.register(turnBoundaryProjectionDefinition)
  731. await ctx.plugin(SandboxPolicyService, { mode: 'workspace-write' })
  732. await ctx.plugin(SandboxingFakeFs)
  733. await ctx.plugin(FsPolicy)
  734. if (opts.approval === true) await ctx.plugin(ApprovalService)
  735. await ctx.plugin(ToolFs)
  736. return { ctx, fs: ctx.fs as SandboxingFakeFs }
  737. }
  738. /** A fake agent whose session records appends (the approval audit trail), mid-turn, carrying the given events for the fold. */
  739. function escalationAgent(records: Array<{ type: string; data?: Record<string, unknown> }> = []): object {
  740. const id = SessionId('sess-fs-esc')
  741. const events: Array<{
  742. type: string
  743. seq: ReturnType<typeof SessionSeq>
  744. time: number
  745. data: Record<string, unknown>
  746. }> = [
  747. { type: 'turn/start', seq: SessionSeq(0), time: 0, data: { turn: 1 } },
  748. ...records.map((record, index) => ({
  749. type: record.type,
  750. seq: SessionSeq(index + 1),
  751. time: index + 1,
  752. data: record.data ?? {},
  753. })),
  754. ]
  755. return {
  756. id,
  757. session: {
  758. id,
  759. header: { version: 0, id, createdAt: 0, cwd: '/session-project', isSeeded: false },
  760. inheritedEventCount: SessionLogOffset(0),
  761. firstLiveSeq: SessionLogOffset(0),
  762. get seq() { return SessionLogOffset(events.length) },
  763. eventAt: (seq: ReturnType<typeof SessionSeq>) => events[seq],
  764. snapshotEvents: (
  765. fromSeq = SessionLogOffset(0),
  766. toSeqExclusive = SessionLogOffset(events.length),
  767. ) => events.slice(fromSeq, toSeqExclusive),
  768. append: (type: string, data: Record<string, unknown>) => {
  769. const event = {
  770. type,
  771. seq: SessionSeq(events.length),
  772. time: events.length,
  773. data,
  774. }
  775. events.push(event)
  776. return event
  777. },
  778. },
  779. }
  780. }
  781. function fsSchema(ctx: Context, name: 'write' | 'edit') {
  782. const schema = ctx.tools.schemas().find(s => s.name === name)
  783. if (!schema) throw new Error(`${name} tool not registered`)
  784. return schema as unknown as { parameters: { properties: Record<string, { enum?: string[] }> } }
  785. }
  786. it('fails load when a confining filesystem has no shared sandbox-policy resolver', async () => {
  787. const ctx = new Context()
  788. await ctx.plugin(SystemPrompt)
  789. await ctx.plugin(ToolRuntime)
  790. await ctx.plugin(SandboxingFakeFs)
  791. await expect(ctx.plugin(ToolFs)).rejects.toThrow('tool-fs: the mounted filesystem confines but ctx.sandboxPolicy is missing')
  792. })
  793. it('advertises no escalation fields under a non-confining backend', async () => {
  794. const { ctx } = await setup()
  795. expect(ctx.fs.sandboxMode).toBeUndefined()
  796. for (const name of ['write', 'edit'] as const) {
  797. const props = fsSchema(ctx, name).parameters.properties
  798. expect(props['sandbox_permissions']).toBeUndefined()
  799. expect(props['justification']).toBeUndefined()
  800. }
  801. })
  802. it('advertises the closed target vocabulary on write and edit under a confining backend', async () => {
  803. const { ctx } = await setupConfining()
  804. for (const name of ['write', 'edit'] as const) {
  805. const props = fsSchema(ctx, name).parameters.properties
  806. expect(props['sandbox_permissions']?.enum).toEqual(['workspace-write', 'danger-full-access'])
  807. expect(props['justification']).toBeDefined()
  808. }
  809. })
  810. it('a plain write stamps the default mode with the calling session root', async () => {
  811. const { ctx, fs } = await setupConfining()
  812. await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent())
  813. expect(fs.stamped).toEqual([{
  814. mode: 'workspace-write',
  815. workspaceRoot: resolve('/session-project'),
  816. sessionId: SessionId('sess-fs-esc'),
  817. }])
  818. })
  819. it('a standing session override folds onto the stamp', async () => {
  820. const { ctx, fs } = await setupConfining()
  821. await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent([{ type: 'sandbox/mode', data: { mode: 'read-only' } }]))
  822. expect(fs.stamped).toEqual([{
  823. mode: 'read-only',
  824. workspaceRoot: resolve('/session-project'),
  825. sessionId: SessionId('sess-fs-esc'),
  826. }])
  827. })
  828. it('a denied write maps to the shared marker plus the escalation hint (isError)', async () => {
  829. const { ctx, fs } = await setupConfining()
  830. fs.rejectWith = new FsError('denied', 'FS_SANDBOX_DENIED')
  831. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent())
  832. expect(result.isError).toBe(true)
  833. expect(text(result)).toContain('[sandbox: file access denied under workspace-write mode]')
  834. expect(text(result)).toContain('retry this exact operation once with sandbox_permissions')
  835. })
  836. it('a non-FS_SANDBOX_DENIED provider error passes through unchanged', async () => {
  837. const { ctx, fs } = await setupConfining()
  838. fs.rejectWith = new FsError('boom', 'FS_IO_ERROR')
  839. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x' }, escalationAgent())
  840. expect(result.isError).toBe(true)
  841. expect(text(result)).toContain('boom')
  842. expect(text(result)).not.toContain('[sandbox:')
  843. })
  844. it('an approved escalation stamps the granted mode onto that write', async () => {
  845. const { ctx, fs } = await setupConfining({ approval: true })
  846. ctx.on('approval/request', () => Promise.resolve('allowed-once' as const))
  847. // Pass a signal so the escalation ask forwards it to the approval request
  848. // (the request rides the tool-execution abort signal).
  849. await ctx.tools.execute({
  850. callId: ToolCallId('call-fs-esc-grant'),
  851. name: 'write',
  852. arguments: { file_path: 'a.txt', content: 'x', sandbox_permissions: 'danger-full-access', justification: 'the test needs it' },
  853. agent: escalationAgent() as never,
  854. signal: new AbortController().signal,
  855. })
  856. expect(fs.stamped).toEqual([{
  857. mode: 'danger-full-access',
  858. workspaceRoot: resolve('/session-project'),
  859. sessionId: SessionId('sess-fs-esc'),
  860. }])
  861. })
  862. it('a rejected escalation fails closed with its own text and never mutates', async () => {
  863. const { ctx, fs } = await setupConfining({ approval: true })
  864. ctx.on('approval/request', () => Promise.resolve('rejected' as const))
  865. 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())
  866. expect(result.isError).toBe(true)
  867. expect(text(result)).toContain('the user rejected escalating this operation to "danger-full-access"')
  868. expect(fs.stamped).toEqual([])
  869. })
  870. it('escalation without an approval service fails closed', async () => {
  871. const { ctx } = await setupConfining()
  872. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'danger-full-access', justification: 'why' }, escalationAgent())
  873. expect(result.isError).toBe(true)
  874. expect(text(result)).toContain('no approval service is composed')
  875. })
  876. it('escalation with an approval service but no agent fails closed', async () => {
  877. const { ctx } = await setupConfining({ approval: true })
  878. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'danger-full-access', justification: 'why' })
  879. expect(result.isError).toBe(true)
  880. expect(text(result)).toContain('no agent to route it through')
  881. })
  882. it('rejects the escalation argument pairing (one field without the other)', async () => {
  883. const { ctx } = await setupConfining()
  884. const missing = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'workspace-write' }, escalationAgent())
  885. expect(missing.isError).toBe(true)
  886. expect(text(missing)).toContain('sandbox_permissions requires a justification')
  887. })
  888. it('sandbox_permissions under a non-confining backend fails closed (unadvertised field still reaches execute)', async () => {
  889. const { ctx } = await setup()
  890. const result = await call(ctx, 'write', { file_path: 'a.txt', content: 'x', sandbox_permissions: 'workspace-write', justification: 'why' }, escalationAgent())
  891. expect(result.isError).toBe(true)
  892. expect(text(result)).toContain('not available in this composition')
  893. })
  894. })
  895. /** Create a real per-agent scope over the mounted tool plugins. */
  896. async function guidanceScope(ctx: Context) {
  897. const key = {}
  898. let scope!: Scope
  899. await ctx.plugin(Object.assign((inner: Context) => { scope = createScope(inner, key) },
  900. { inject: ['tools', 'systemPrompt'] }))
  901. return { key, scope }
  902. }
  903. const originalGuidance = {
  904. read: 'Use the read tool — not shell commands like cat — to inspect text files. Results include line numbers. Use offset and limit to continue reading large files.',
  905. write: 'Use the write tool to create files or completely replace file contents. Existing files are overwritten, so read an existing file first (the default fs-observation-policy requires it) and prefer edit for targeted changes.',
  906. edit: 'Use the edit tool for targeted changes to existing UTF-8 text files. It replaces literal old_string with new_string; by default old_string must appear exactly once. If old_string appears multiple times, provide a more specific old_string or set replace_all to true. Read the file first (the default fs-observation-policy requires it), unless you just created or edited it in this session.',
  907. }
  908. describe('scope-aware filesystem guidance', () => {
  909. it.each(Array.from({ length: 8 }, (_, mask) => mask))('preserves exact text for visible tools (mask %i)', async (mask) => {
  910. const { ctx } = await setup()
  911. const { key, scope } = await guidanceScope(ctx)
  912. const names = ['read', 'write', 'edit'] as const
  913. const allow = names.filter((_, index) => (mask & (1 << index)) !== 0)
  914. const baseline = withPersona(...names.map(name => originalGuidance[name]))
  915. expect(renderPrompt(await ctx.systemPrompt.assemble())).toBe(baseline)
  916. const release = scope.ctx.tools.restrict({ allow })
  917. try {
  918. const assembly = await ctx.systemPrompt.assemble({ scope: key })
  919. expect(assembly.tools.map(tool => tool.name)).toEqual([...allow].sort())
  920. const expected = withPersona(...allow.map(name => name === 'write' && !allow.includes('edit')
  921. ? originalGuidance.write.replace(' and prefer edit for targeted changes', '')
  922. : originalGuidance[name]))
  923. expect(renderPrompt(assembly)).toBe(expected)
  924. expect(renderPrompt(await ctx.systemPrompt.assemble())).toBe(baseline)
  925. release()
  926. expect(renderPrompt(await ctx.systemPrompt.assemble({ scope: key }))).toBe(baseline)
  927. } finally {
  928. await scope.dispose()
  929. }
  930. })
  931. it('honors deny filters and the existing exemption for own-scope tools', async () => {
  932. const { ctx } = await setup()
  933. const { key, scope } = await guidanceScope(ctx)
  934. const write = ctx.tools.get('write')!
  935. scope.ctx.tools.restrict({ deny: ['write', 'edit'] })
  936. try {
  937. expect(renderPrompt(await ctx.systemPrompt.assemble({ scope: key }))).toBe(withPersona(originalGuidance.read))
  938. const denied = await call(ctx, 'write', { file_path: '/blocked', content: 'blocked' }, key)
  939. expect(denied.isError).toBe(true)
  940. expect(text(denied)).toContain('unknown tool "write"')
  941. scope.ctx.tools.register(write)
  942. const assembly = await ctx.systemPrompt.assemble({ scope: key })
  943. expect(assembly.tools.map(tool => tool.name)).toEqual(['read', 'write'])
  944. expect(renderPrompt(assembly)).toBe(withPersona(originalGuidance.read,
  945. originalGuidance.write.replace(' and prefer edit for targeted changes', '')))
  946. } finally {
  947. await scope.dispose()
  948. }
  949. })
  950. })
  951. /** Preserve the default persona and exact section separators in the oracle. */
  952. function withPersona(...sections: string[]): string {
  953. return ['You are an AI agent powered by DeepSeek Harness.', ...sections].join('\n\n')
  954. }
  955. /** Schema assembly only: these cases never execute user code. */
  956. class GuidanceCodeRuntime extends CodeRuntime {
  957. readonly language = 'typescript'
  958. readonly isolation = 'fake'
  959. run() { return Promise.resolve({ logs: [] }) }
  960. }
  961. describe('scope-aware PTC guidance', () => {
  962. it.each(['ptc', 'both'] as const)('uses capability visibility in %s mode', async (mode) => {
  963. const ctx = new Context()
  964. await ctx.plugin(SystemPrompt)
  965. await ctx.plugin(GuidanceCodeRuntime)
  966. await ctx.plugin(ToolRuntime, { mode })
  967. await ctx.plugin(FakeFs)
  968. await ctx.plugin(ToolFs)
  969. const { key, scope } = await guidanceScope(ctx)
  970. try {
  971. const baseline = renderPrompt(await ctx.systemPrompt.assemble({ scope: key }))
  972. const release = scope.ctx.tools.restrict({ allow: ['read'] })
  973. const assembly = await ctx.systemPrompt.assemble({ scope: key })
  974. expect(assembly.tools.map(tool => tool.name)).toEqual(mode === 'ptc' ? ['run_code'] : ['read', 'run_code'])
  975. expect(assembly.sections.filter(section => ['tool:read', 'tool:write', 'tool:edit'].includes(section.name))
  976. .map(section => section.text).filter(Boolean)).toEqual([originalGuidance.read])
  977. expect(renderPrompt(assembly)).toContain(originalGuidance.read)
  978. expect(renderPrompt(assembly)).not.toContain(originalGuidance.write)
  979. expect(renderPrompt(assembly)).not.toContain(originalGuidance.edit)
  980. release()
  981. expect(renderPrompt(await ctx.systemPrompt.assemble({ scope: key }))).toBe(baseline)
  982. } finally {
  983. await scope.dispose()
  984. }
  985. })
  986. })