filesystem.spec.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. import { Buffer } from 'node:buffer'
  2. import { dirname, posix } from 'node:path'
  3. import { Context } from '@deepseek-ai/cordis'
  4. import {
  5. CommandExitError,
  6. FileNotFoundError,
  7. FileType,
  8. type EntryInfo,
  9. type Sandbox,
  10. } from '@deepseek-ai/dsh-e2b'
  11. import type E2BSandboxService from '@deepseek-ai/dsh-e2b'
  12. import { FsTargetKey, FsVersion } from '@deepseek-ai/dsh-fs'
  13. import E2BFileSystem from '@deepseek-ai/dsh-fs-e2b'
  14. import * as E2BFsInvariant from '../src/invariant.ts'
  15. import InvariantService from '@deepseek-ai/dsh-invariants'
  16. import { describe, expect, it, vi } from 'vitest'
  17. interface RemoteNode {
  18. type: FileType
  19. data: Uint8Array
  20. mode: number
  21. modified: number
  22. metadata?: Record<string, string>
  23. symlinkTarget?: string
  24. }
  25. function bytes(value: string | readonly number[]): Uint8Array {
  26. return typeof value === 'string' ? new TextEncoder().encode(value) : Uint8Array.from(value)
  27. }
  28. function commandError(exitCode: number, stderr = ''): CommandExitError {
  29. return new CommandExitError({ exitCode, stdout: '', stderr, error: stderr })
  30. }
  31. class FakeRemote {
  32. readonly nodes = new Map<string, RemoteNode>()
  33. readonly writes: Array<{ path: string; data: string; metadata?: Record<string, string> }> = []
  34. readonly writeParentModes: number[] = []
  35. readonly renames: Array<{ from: string; to: string }> = []
  36. readonly links: Array<{ from: string; to: string }> = []
  37. readonly removals: string[] = []
  38. readonly commands: string[] = []
  39. readonly reads: Array<{ path: string; format: 'bytes' | 'stream' }> = []
  40. streamChunks: Uint8Array[] | undefined
  41. streamKeepOpen = false
  42. readonly streamCancel = vi.fn()
  43. nextCommandError: unknown
  44. nextMakeDirResult: boolean | undefined
  45. nextInfoError: unknown
  46. nextListError: unknown
  47. nextReadError: unknown
  48. nextRenameError: unknown
  49. nextRemoveError: unknown
  50. canonicalOutput: string | undefined
  51. abortAfterRename: AbortController | undefined
  52. competitorBeforeLink:
  53. | { path: string; kind: 'file'; data: string }
  54. | { path: string; kind: 'directory' }
  55. | undefined
  56. guardedLinkOutput: string | undefined
  57. disappearOnInfo = new Set<string>()
  58. private clock = 1
  59. constructor() {
  60. this.dir('/')
  61. this.dir('/workspace')
  62. }
  63. dir(path: string): void {
  64. this.nodes.set(path, { type: FileType.DIR, data: bytes(''), mode: 0o755, modified: this.clock++ })
  65. }
  66. file(path: string, data: string | readonly number[], mode = 0o644): void {
  67. this.nodes.set(path, { type: FileType.FILE, data: bytes(data), mode, modified: this.clock++ })
  68. }
  69. other(path: string): void {
  70. this.nodes.set(path, { type: 'other' as FileType, data: bytes(''), mode: 0o600, modified: this.clock++ })
  71. }
  72. symlink(path: string, target: string): void {
  73. this.nodes.set(path, {
  74. type: FileType.FILE,
  75. data: bytes(''),
  76. mode: 0o777,
  77. modified: this.clock++,
  78. symlinkTarget: target,
  79. })
  80. }
  81. mutate(path: string, data: string): void {
  82. const node = this.required(path)
  83. node.data = bytes(data)
  84. node.modified = this.clock++
  85. }
  86. private required(path: string): RemoteNode {
  87. const node = this.nodes.get(path)
  88. if (node === undefined) throw new FileNotFoundError(`missing: ${path}`)
  89. return node
  90. }
  91. private followed(path: string): { path: string; node: RemoteNode; link?: RemoteNode } {
  92. const node = this.required(path)
  93. if (node.symlinkTarget === undefined) return { path, node }
  94. return { path: node.symlinkTarget, node: this.required(node.symlinkTarget), link: node }
  95. }
  96. private info(path: string): EntryInfo {
  97. if (this.disappearOnInfo.delete(path)) throw new FileNotFoundError(`missing: ${path}`)
  98. return this.rawInfo(path)
  99. }
  100. private rawInfo(path: string): EntryInfo {
  101. const followed = this.followed(path)
  102. const node = followed.node
  103. return {
  104. name: posix.basename(path),
  105. path,
  106. type: node.type,
  107. size: node.data.byteLength,
  108. mode: node.mode,
  109. permissions: 'rw-------',
  110. owner: 'user',
  111. group: 'user',
  112. modifiedTime: new Date(node.modified),
  113. ...(node.metadata !== undefined ? { metadata: { ...node.metadata } } : {}),
  114. ...(followed.link?.symlinkTarget !== undefined ? { symlinkTarget: followed.link.symlinkTarget } : {}),
  115. }
  116. }
  117. private checkAbort(options: { signal?: AbortSignal } | undefined): void {
  118. if (options?.signal?.aborted === true) throw new DOMException('aborted', 'AbortError')
  119. }
  120. readonly sandbox = {
  121. sandboxId: 'fake',
  122. files: {
  123. makeDir: async (path: string, options?: { signal?: AbortSignal }): Promise<boolean> => {
  124. this.checkAbort(options)
  125. if (this.nextMakeDirResult !== undefined) {
  126. const result = this.nextMakeDirResult
  127. this.nextMakeDirResult = undefined
  128. return result
  129. }
  130. if (this.nodes.has(path)) return false
  131. this.dir(path)
  132. return true
  133. },
  134. getInfo: async (path: string, options?: { signal?: AbortSignal }): Promise<EntryInfo> => {
  135. this.checkAbort(options)
  136. if (this.nextInfoError !== undefined) {
  137. const error = this.nextInfoError
  138. this.nextInfoError = undefined
  139. throw error
  140. }
  141. return this.info(path)
  142. },
  143. read: async (path: string, options: { format: 'bytes' | 'stream'; signal?: AbortSignal }): Promise<Uint8Array | ReadableStream<Uint8Array> | string> => {
  144. this.checkAbort(options)
  145. this.reads.push({ path, format: options.format })
  146. if (this.nextReadError !== undefined) {
  147. const error = this.nextReadError
  148. this.nextReadError = undefined
  149. throw error
  150. }
  151. const data = this.followed(path).node.data
  152. if (options.format === 'bytes') return data.slice()
  153. // Pinned-SDK fidelity: a content-length-0 response returns '' even in stream format.
  154. if (data.length === 0 && this.streamChunks === undefined) return ''
  155. const chunks = this.streamChunks ?? [data.slice()]
  156. return new ReadableStream<Uint8Array>({
  157. start: (controller) => {
  158. for (const chunk of chunks) controller.enqueue(chunk)
  159. if (!this.streamKeepOpen) controller.close()
  160. },
  161. cancel: () => { this.streamCancel() },
  162. })
  163. },
  164. list: async (path: string, options?: { depth?: number; signal?: AbortSignal }): Promise<EntryInfo[]> => {
  165. this.checkAbort(options)
  166. if (this.nextListError !== undefined) {
  167. const error = this.nextListError
  168. this.nextListError = undefined
  169. throw error
  170. }
  171. this.required(path)
  172. return [...this.nodes.keys()]
  173. .filter(candidate => candidate !== path && dirname(candidate) === path)
  174. .map(candidate => this.rawInfo(candidate))
  175. },
  176. write: async (path: string, data: string, options?: { metadata?: Record<string, string>; signal?: AbortSignal }): Promise<object> => {
  177. this.checkAbort(options)
  178. const parent = dirname(path)
  179. if (!this.nodes.has(parent)) this.dir(parent)
  180. this.writeParentModes.push(this.required(parent).mode)
  181. this.nodes.set(path, {
  182. type: FileType.FILE,
  183. data: bytes(data),
  184. mode: 0o644,
  185. modified: this.clock++,
  186. ...(options?.metadata !== undefined ? { metadata: { ...options.metadata } } : {}),
  187. })
  188. this.writes.push({ path, data, ...(options?.metadata !== undefined ? { metadata: options.metadata } : {}) })
  189. return {}
  190. },
  191. rename: async (from: string, to: string, options?: { signal?: AbortSignal }): Promise<EntryInfo> => {
  192. this.checkAbort(options)
  193. if (this.nextRenameError !== undefined) {
  194. const error = this.nextRenameError
  195. this.nextRenameError = undefined
  196. throw error
  197. }
  198. const node = this.required(from)
  199. this.nodes.delete(from)
  200. this.nodes.set(to, node)
  201. this.renames.push({ from, to })
  202. this.abortAfterRename?.abort('after commit')
  203. this.checkAbort(options)
  204. return this.info(to)
  205. },
  206. remove: async (path: string): Promise<void> => {
  207. this.removals.push(path)
  208. if (this.nextRemoveError !== undefined) {
  209. const error = this.nextRemoveError
  210. this.nextRemoveError = undefined
  211. throw error
  212. }
  213. for (const candidate of this.nodes.keys()) {
  214. if (candidate === path || candidate.startsWith(`${path}/`)) this.nodes.delete(candidate)
  215. }
  216. },
  217. },
  218. commands: {
  219. run: async (
  220. command: string,
  221. options?: { envs?: Record<string, string>; signal?: AbortSignal },
  222. ): Promise<{ exitCode: number; stdout: string; stderr: string }> => {
  223. this.checkAbort(options)
  224. const home = options?.envs?.HOME
  225. expect(home).toMatch(/^\/\.dsh-e2b-control-/)
  226. expect(options?.envs).toEqual({ HOME: home })
  227. this.commands.push(command)
  228. if (this.nextCommandError !== undefined) {
  229. const error = this.nextCommandError
  230. this.nextCommandError = undefined
  231. throw error
  232. }
  233. const realpathPrefix = 'set -o pipefail; realpath -mz -- '
  234. const realpathSuffix = ' | base64 -w0'
  235. if (command.startsWith(realpathPrefix) && command.endsWith(realpathSuffix)) {
  236. const quoted = command.slice(realpathPrefix.length, -realpathSuffix.length)
  237. const input = quoted.slice(1, -1).replaceAll(String.raw`'"'"'`, '\'')
  238. const node = this.nodes.get(input)
  239. const canonical = `${node?.symlinkTarget ?? input}\0`
  240. return {
  241. exitCode: 0,
  242. stdout: this.canonicalOutput ?? Buffer.from(canonical).toString('base64'),
  243. stderr: '',
  244. }
  245. }
  246. const chmod = /^chmod ([0-7]+) -- '([^']+)'$/.exec(command)
  247. if (chmod !== null) this.required(chmod[2]!).mode = Number.parseInt(chmod[1]!, 8)
  248. const guardedLink = new RegExp(
  249. "^if ln -T -- '([^']+)' '([^']+)'; then printf created; "
  250. + "elif test -e '[^']+' \\|\\| test -L '[^']+'; then printf exists; else exit 1; fi$",
  251. ).exec(command)
  252. if (guardedLink !== null) {
  253. const from = guardedLink[1]!
  254. const to = guardedLink[2]!
  255. if (this.guardedLinkOutput !== undefined) {
  256. const stdout = this.guardedLinkOutput
  257. this.guardedLinkOutput = undefined
  258. return { exitCode: 0, stdout, stderr: '' }
  259. }
  260. if (this.competitorBeforeLink?.path === to) {
  261. if (this.competitorBeforeLink.kind === 'directory') this.dir(to)
  262. else this.file(to, this.competitorBeforeLink.data)
  263. this.competitorBeforeLink = undefined
  264. }
  265. if (this.nodes.has(to)) return { exitCode: 0, stdout: 'exists', stderr: '' }
  266. this.nodes.set(to, this.required(from))
  267. this.links.push({ from, to })
  268. this.abortAfterRename?.abort('after commit')
  269. return { exitCode: 0, stdout: 'created', stderr: '' }
  270. }
  271. const move = /^mv -f -- '([^']+)' '([^']+)'$/.exec(command)
  272. if (move !== null) {
  273. if (this.nextRenameError !== undefined) {
  274. const error = this.nextRenameError
  275. this.nextRenameError = undefined
  276. throw error
  277. }
  278. const node = this.required(move[1]!)
  279. this.nodes.delete(move[1]!)
  280. this.nodes.set(move[2]!, node)
  281. this.renames.push({ from: move[1]!, to: move[2]! })
  282. this.abortAfterRename?.abort('after commit')
  283. }
  284. return { exitCode: 0, stdout: '', stderr: '' }
  285. },
  286. },
  287. } as unknown as Sandbox
  288. }
  289. async function setup(remote = new FakeRemote()): Promise<{ ctx: Context; fs: E2BFileSystem; remote: FakeRemote }> {
  290. const ctx = new Context()
  291. const runtime = {
  292. cwd: '/workspace',
  293. runtimeRoot: '/workspace/.dsh-e2b',
  294. getSandbox: async () => remote.sandbox,
  295. } as unknown as E2BSandboxService
  296. ctx.provide('e2b', runtime)
  297. await ctx.plugin(E2BFileSystem)
  298. return { ctx, fs: ctx.fs as E2BFileSystem, remote }
  299. }
  300. async function expectCode(promise: Promise<unknown>, code: string): Promise<void> {
  301. await expect(promise).rejects.toMatchObject({ code })
  302. }
  303. describe('E2BFileSystem identity, metadata, and reads', () => {
  304. it('resolves remote paths, reports symlinks, and lists direct children in stable order', async () => {
  305. const remote = new FakeRemote()
  306. remote.file('/workspace/z.txt', 'z')
  307. remote.file('/workspace/a.txt', 'a')
  308. remote.dir('/workspace/dir')
  309. remote.other('/workspace/special')
  310. remote.file('/workspace/dir/nested.txt', 'nested')
  311. remote.symlink('/workspace/link.txt', '/workspace/a.txt')
  312. const { fs } = await setup(remote)
  313. const link = await fs.resolve('link.txt')
  314. expect(link).toEqual({ targetKey: '/workspace/a.txt', displayPath: '/workspace/link.txt' })
  315. await expect(fs.lstat('link.txt')).resolves.toMatchObject({ type: 'symlink', size: 1 })
  316. await expect(fs.lstat('a.txt')).resolves.toMatchObject({ type: 'file', size: 1 })
  317. await expect(fs.lstat('dir')).resolves.toEqual(expect.objectContaining({ type: 'directory' }))
  318. await expect(fs.lstat('special')).resolves.toEqual(expect.objectContaining({ type: 'other' }))
  319. await expect(fs.lstat('missing')).resolves.toBeUndefined()
  320. await expect(fs.stat(link)).resolves.toMatchObject({ type: 'file', size: 1 })
  321. const directory = await fs.resolve('.')
  322. const listed = await fs.listDir(directory)
  323. expect(listed.map(entry => entry.name)).toEqual(['a.txt', 'dir', 'link.txt', 'special', 'z.txt'])
  324. expect(listed.find(entry => entry.name === 'dir')).toMatchObject({ type: 'directory' })
  325. expect(listed.find(entry => entry.name === 'link.txt')).toMatchObject({
  326. type: 'file',
  327. target: { targetKey: '/workspace/a.txt', displayPath: '/workspace/link.txt' },
  328. })
  329. expect(listed.some(entry => entry.name === 'nested.txt')).toBe(false)
  330. })
  331. it('projects canonical process paths, file URLs, and containment', async () => {
  332. const remote = new FakeRemote()
  333. remote.dir('/workspace/nested')
  334. remote.file('/workspace/nested/multibyte # file.ts', 'text')
  335. remote.file('/outside.ts', 'outside')
  336. const { fs } = await setup(remote)
  337. const workspace = await fs.resolve('/workspace')
  338. const nested = await fs.resolve('/workspace/nested/multibyte # file.ts')
  339. const outside = await fs.resolve('/outside.ts')
  340. expect(fs.processPath(nested)).toBe('/workspace/nested/multibyte # file.ts')
  341. expect(fs.fileUrl(nested)).toBe('file:///workspace/nested/multibyte%20%23%20file.ts')
  342. expect(fs.contains(workspace, workspace)).toBe(true)
  343. expect(fs.contains(workspace, nested)).toBe(true)
  344. expect(fs.contains(nested, workspace)).toBe(false)
  345. expect(fs.contains(workspace, outside)).toBe(false)
  346. expect(() => fs.fileUrl({ targetKey: FsTargetKey('relative'), displayPath: 'relative' }))
  347. .toThrow('expected an absolute process path')
  348. })
  349. it('preserves newline and multibyte canonical paths through strict ASCII framing', async () => {
  350. const remote = new FakeRemote()
  351. const path = '/workspace/你好\nfile.ts'
  352. remote.file(path, 'text')
  353. const { fs } = await setup(remote)
  354. await expect(fs.resolve(path)).resolves.toEqual({ targetKey: path, displayPath: path })
  355. })
  356. it.each([
  357. ['invalid base64', '!!!!'],
  358. ['missing terminator', Buffer.from('/workspace/file').toString('base64')],
  359. ['multiple records', Buffer.from('/workspace/file\0/other\0').toString('base64')],
  360. ['invalid UTF-8', Buffer.from([47, 0xff, 0]).toString('base64')],
  361. ['relative path', Buffer.from('workspace/file\0').toString('base64')],
  362. ])('rejects %s from canonical path transport', async (_label, output) => {
  363. const remote = new FakeRemote()
  364. remote.canonicalOutput = output
  365. const { fs } = await setup(remote)
  366. await expectCode(fs.resolve('file'), 'FS_IO_ERROR')
  367. })
  368. it('reads whole and streamed UTF-8 across chunk boundaries', async () => {
  369. const remote = new FakeRemote()
  370. remote.file('/workspace/text.txt', 'A€B')
  371. remote.streamChunks = [bytes([65, 0xe2]), bytes([0x82, 0xac, 66])]
  372. const { fs } = await setup(remote)
  373. const target = await fs.resolve('text.txt')
  374. await expect(fs.readText(target)).resolves.toBe('A€B')
  375. let streamed = ''
  376. for await (const chunk of await fs.streamText(target)) streamed += chunk
  377. expect(streamed).toBe('A€B')
  378. remote.streamChunks = [bytes([0xe2]), bytes([0x82, 0xac])]
  379. let initiallyBuffered = ''
  380. for await (const chunk of await fs.streamText(target)) initiallyBuffered += chunk
  381. expect(initiallyBuffered).toBe('€')
  382. })
  383. it('streams an empty file even though the pinned SDK returns a non-stream value', async () => {
  384. const remote = new FakeRemote()
  385. remote.file('/workspace/empty.txt', '')
  386. const { fs } = await setup(remote)
  387. let streamed = ''
  388. for await (const chunk of await fs.streamText(await fs.resolve('empty.txt'))) streamed += chunk
  389. expect(streamed).toBe('')
  390. })
  391. it('cancels a remote stream when its consumer stops early', async () => {
  392. const remote = new FakeRemote()
  393. remote.file('/workspace/text.txt', 'ab')
  394. remote.streamChunks = [bytes('a'), bytes('b')]
  395. remote.streamKeepOpen = true
  396. const { fs } = await setup(remote)
  397. const stream = await fs.streamText(await fs.resolve('text.txt'))
  398. for await (const chunk of stream) {
  399. expect(chunk).toBe('a')
  400. break
  401. }
  402. expect(remote.streamCancel).toHaveBeenCalledOnce()
  403. })
  404. it('matches local binary sampling while edits still reject any NUL byte', async () => {
  405. const remote = new FakeRemote()
  406. remote.file('/workspace/late-nul.txt', `${'a'.repeat(8192)}\0tail`)
  407. const { fs } = await setup(remote)
  408. const target = await fs.resolve('late-nul.txt')
  409. await expect(fs.readText(target)).resolves.toContain('\0tail')
  410. remote.streamChunks = [bytes('a'.repeat(8192)), bytes([0, 116])]
  411. let streamed = ''
  412. for await (const chunk of await fs.streamText(target)) streamed += chunk
  413. expect(streamed).toBe(`${'a'.repeat(8192)}\0t`)
  414. await expectCode(fs.editText(target, { oldString: 'tail', newString: 'end', replaceAll: false }), 'FS_NOT_TEXT')
  415. })
  416. it('maps binary, invalid UTF-8, missing, and non-regular read failures', async () => {
  417. const remote = new FakeRemote()
  418. remote.file('/workspace/binary', [0, 1])
  419. remote.file('/workspace/invalid', [0xff])
  420. remote.dir('/workspace/directory')
  421. const { fs } = await setup(remote)
  422. await expectCode(fs.readText(await fs.resolve('binary')), 'FS_NOT_TEXT')
  423. await expectCode(fs.readText(await fs.resolve('invalid')), 'FS_NOT_TEXT')
  424. await expectCode(fs.readText(await fs.resolve('missing')), 'FS_NOT_FOUND')
  425. await expectCode(fs.readText(await fs.resolve('directory')), 'FS_NOT_REGULAR_FILE')
  426. remote.streamChunks = [bytes([0xff])]
  427. const invalid = await fs.streamText(await fs.resolve('invalid'))
  428. await expect((async () => { for await (const _chunk of invalid) void _chunk })()).rejects.toMatchObject({ code: 'FS_NOT_TEXT' })
  429. remote.streamChunks = [bytes([0])]
  430. const binary = await fs.streamText(await fs.resolve('binary'))
  431. await expect((async () => { for await (const _chunk of binary) void _chunk })()).rejects.toMatchObject({ code: 'FS_NOT_TEXT' })
  432. remote.streamChunks = [bytes([0xe2])]
  433. const incomplete = await fs.streamText(await fs.resolve('invalid'))
  434. await expect((async () => { for await (const _chunk of incomplete) void _chunk })()).rejects.toMatchObject({ code: 'FS_NOT_TEXT' })
  435. const raced = await fs.resolve('invalid')
  436. remote.nextReadError = new FileNotFoundError('gone after stat')
  437. await expectCode(fs.streamText(raced), 'FS_NOT_FOUND')
  438. })
  439. it('readBytes returns raw content, enforces the byte cap, and maps failures', async () => {
  440. const remote = new FakeRemote()
  441. remote.file('/workspace/img.bin', [0x89, 0, 0xff, 0x47])
  442. remote.dir('/workspace/directory')
  443. const { fs } = await setup(remote)
  444. const target = await fs.resolve('img.bin')
  445. expect(Array.from(await fs.readBytes(target, undefined, 4))).toEqual([0x89, 0, 0xff, 0x47])
  446. expect(remote.reads).toEqual([{ path: '/workspace/img.bin', format: 'stream' }])
  447. remote.reads.length = 0
  448. await expectCode(fs.readBytes(target, undefined, 3), 'FS_TOO_LARGE')
  449. expect(remote.reads).toEqual([])
  450. await expectCode(fs.readBytes(await fs.resolve('missing'), undefined, 4), 'FS_NOT_FOUND')
  451. await expectCode(fs.readBytes(await fs.resolve('directory'), undefined, 4), 'FS_NOT_REGULAR_FILE')
  452. const live = new AbortController()
  453. expect((await fs.readBytes(target, live.signal, 4)).byteLength).toBe(4)
  454. remote.nextReadError = new DOMException('aborted', 'AbortError')
  455. await expectCode(fs.readBytes(target, undefined, 4), 'FS_ABORTED')
  456. })
  457. it('readBytes bounds a post-stat grower mid-stream and reads an empty file through the SDK quirk', async () => {
  458. const remote = new FakeRemote()
  459. remote.file('/workspace/grow.bin', [1, 1, 1, 1])
  460. remote.file('/workspace/empty.bin', '')
  461. const { fs } = await setup(remote)
  462. remote.streamChunks = [bytes([1, 1, 1]), bytes([1, 2, 2])]
  463. remote.streamKeepOpen = true
  464. await expectCode(fs.readBytes(await fs.resolve('grow.bin'), undefined, 4), 'FS_TOO_LARGE')
  465. expect(remote.streamCancel).toHaveBeenCalledOnce()
  466. remote.streamChunks = undefined
  467. remote.streamKeepOpen = false
  468. expect((await fs.readBytes(await fs.resolve('empty.bin'), undefined, 4)).byteLength).toBe(0)
  469. })
  470. it('honors aborts before and during remote reads', async () => {
  471. const remote = new FakeRemote()
  472. remote.file('/workspace/a', 'a')
  473. const { fs } = await setup(remote)
  474. await expectCode(fs.resolve('a', { signal: AbortSignal.abort() }), 'FS_ABORTED')
  475. await expectCode(fs.lstat('a', undefined, AbortSignal.abort()), 'FS_ABORTED')
  476. await expectCode(fs.stat(await fs.resolve('a'), AbortSignal.abort()), 'FS_ABORTED')
  477. remote.nextReadError = new DOMException('aborted', 'AbortError')
  478. await expectCode(fs.readText(await fs.resolve('a')), 'FS_ABORTED')
  479. })
  480. it('rejects empty paths and directory-listing type errors', async () => {
  481. const remote = new FakeRemote()
  482. remote.file('/workspace/file', 'x')
  483. const { fs } = await setup(remote)
  484. await expectCode(fs.resolve(' '), 'FS_NOT_FOUND')
  485. await expectCode(fs.lstat(''), 'FS_NOT_FOUND')
  486. await expectCode(fs.listDir(await fs.resolve('missing')), 'FS_NOT_FOUND')
  487. await expectCode(fs.listDir(await fs.resolve('/workspace/file')), 'FS_NOT_DIRECTORY')
  488. remote.nextListError = new Error('listing transport failed')
  489. await expectCode(fs.listDir(await fs.resolve('/workspace')), 'FS_IO_ERROR')
  490. })
  491. })
  492. describe('E2BFileSystem atomic writes and edits', () => {
  493. it('creates owner-only files and returns metadata after the committed move', async () => {
  494. const { fs, remote } = await setup()
  495. const target = await fs.resolve('new.txt')
  496. const outcome = await fs.writeText(target, 'one\r\ntwo\rthree', { kind: 'createIfAbsent' })
  497. expect(outcome).toMatchObject({ operation: 'create', before: null, after: 'one\ntwo\rthree' })
  498. expect(remote.nodes.get('/workspace/new.txt')?.mode).toBe(0o600)
  499. expect(remote.nodes.get('/workspace/new.txt')?.metadata?.['dsh-version']).toBeDefined()
  500. expect(remote.writeParentModes).toEqual([0o700])
  501. expect(remote.links).toHaveLength(1)
  502. const stagingDirectory = posix.dirname(remote.writes[0]!.path)
  503. expect(posix.dirname(stagingDirectory)).toBe('/workspace')
  504. expect(remote.removals).toContain(stagingDirectory)
  505. await expect(fs.stat(target)).resolves.toMatchObject({ version: outcome.version, size: 14 })
  506. })
  507. it('preserves replacement mode, normalizes only CRLF for diffs, and changes version on external writes', async () => {
  508. const remote = new FakeRemote()
  509. remote.file('/workspace/file.txt', 'old\r\nline\rlone', 0o640)
  510. const { fs } = await setup(remote)
  511. const target = await fs.resolve('file.txt')
  512. const before = (await fs.stat(target))!.version
  513. const outcome = await fs.writeText(target, 'new', { kind: 'replaceIfVersion', version: before })
  514. expect(outcome).toMatchObject({ operation: 'update', before: 'old\nline\rlone', after: 'new' })
  515. expect(remote.nodes.get('/workspace/file.txt')?.mode).toBe(0o640)
  516. const committed = outcome.version
  517. remote.mutate('/workspace/file.txt', 'external')
  518. expect((await fs.stat(target))!.version).not.toBe(committed)
  519. })
  520. it('returns null as the overwrite diff basis for binary or invalid prior content', async () => {
  521. const remote = new FakeRemote()
  522. remote.file('/workspace/file.txt', [0xff])
  523. const { fs } = await setup(remote)
  524. const target = await fs.resolve('file.txt')
  525. await expect(fs.writeText(target, 'valid')).resolves.toMatchObject({ before: null, after: 'valid' })
  526. })
  527. it('fails an overwrite when reading its text diff basis fails for another reason', async () => {
  528. const remote = new FakeRemote()
  529. remote.file('/workspace/file.txt', 'prior')
  530. const { fs } = await setup(remote)
  531. const target = await fs.resolve('file.txt')
  532. remote.nextReadError = new Error('read transport failed')
  533. await expectCode(fs.writeText(target, 'replacement'), 'FS_IO_ERROR')
  534. expect(new TextDecoder().decode(remote.nodes.get('/workspace/file.txt')?.data)).toBe('prior')
  535. })
  536. it('enforces create and version intents before publication', async () => {
  537. const remote = new FakeRemote()
  538. remote.file('/workspace/file.txt', 'v1')
  539. const { fs } = await setup(remote)
  540. const target = await fs.resolve('file.txt')
  541. const version = (await fs.stat(target))!.version
  542. await expectCode(fs.writeText(target, 'blind', { kind: 'createIfAbsent' }), 'FS_NOT_OBSERVED')
  543. remote.mutate('/workspace/file.txt', 'v2')
  544. await expectCode(fs.writeText(target, 'stale', { kind: 'replaceIfVersion', version }), 'FS_STALE_VERSION')
  545. await expectCode(fs.writeText(await fs.resolve('missing'), 'stale', { kind: 'replaceIfVersion', version }), 'FS_STALE_VERSION')
  546. remote.dir('/workspace/dir')
  547. await expectCode(fs.writeText(await fs.resolve('dir'), 'x'), 'FS_NOT_REGULAR_FILE')
  548. })
  549. it('preserves a competitor created after the guarded-create probe', async () => {
  550. const remote = new FakeRemote()
  551. remote.competitorBeforeLink = { path: '/workspace/race.txt', kind: 'file', data: 'competitor' }
  552. const { fs } = await setup(remote)
  553. await expectCode(
  554. fs.writeText(await fs.resolve('race.txt'), 'ours', { kind: 'createIfAbsent' }),
  555. 'FS_NOT_OBSERVED',
  556. )
  557. expect(new TextDecoder().decode(remote.nodes.get('/workspace/race.txt')?.data)).toBe('competitor')
  558. expect(remote.links).toHaveLength(0)
  559. expect(remote.removals).toHaveLength(1)
  560. })
  561. it('preserves a competing directory during guarded-create publication', async () => {
  562. const remote = new FakeRemote()
  563. remote.competitorBeforeLink = { path: '/workspace/race-dir', kind: 'directory' }
  564. const { fs } = await setup(remote)
  565. await expectCode(
  566. fs.writeText(await fs.resolve('race-dir'), 'ours', { kind: 'createIfAbsent' }),
  567. 'FS_NOT_OBSERVED',
  568. )
  569. expect(remote.nodes.get('/workspace/race-dir')?.type).toBe(FileType.DIR)
  570. expect(remote.nodes.has('/workspace/race-dir/content')).toBe(false)
  571. expect(remote.links).toHaveLength(0)
  572. expect(remote.removals).toHaveLength(1)
  573. })
  574. it('rejects an invalid guarded-create publication response before claiming success', async () => {
  575. const remote = new FakeRemote()
  576. remote.guardedLinkOutput = 'unexpected'
  577. const { fs } = await setup(remote)
  578. await expectCode(
  579. fs.writeText(await fs.resolve('invalid.txt'), 'ours', { kind: 'createIfAbsent' }),
  580. 'FS_IO_ERROR',
  581. )
  582. expect(remote.nodes.has('/workspace/invalid.txt')).toBe(false)
  583. expect(remote.removals).toHaveLength(1)
  584. })
  585. it('does not turn an abort observed after a successful move into a failed write', async () => {
  586. const remote = new FakeRemote()
  587. const controller = new AbortController()
  588. remote.abortAfterRename = controller
  589. const { fs } = await setup(remote)
  590. await expect(fs.writeText(await fs.resolve('committed'), 'yes', undefined, controller.signal))
  591. .resolves.toMatchObject({ operation: 'create' })
  592. expect(controller.signal.aborted).toBe(true)
  593. })
  594. it('does not turn an abort observed after a guarded create into a failed write', async () => {
  595. const remote = new FakeRemote()
  596. const controller = new AbortController()
  597. remote.abortAfterRename = controller
  598. const { fs } = await setup(remote)
  599. await expect(fs.writeText(
  600. await fs.resolve('committed-create'),
  601. 'yes',
  602. { kind: 'createIfAbsent' },
  603. controller.signal,
  604. )).resolves.toMatchObject({ operation: 'create' })
  605. expect(controller.signal.aborted).toBe(true)
  606. })
  607. it('does not turn post-commit staging cleanup failure into a failed write', async () => {
  608. const remote = new FakeRemote()
  609. remote.nextRemoveError = new Error('empty staging cleanup failed')
  610. const { fs } = await setup(remote)
  611. await expect(fs.writeText(await fs.resolve('committed'), 'yes'))
  612. .resolves.toMatchObject({ operation: 'create' })
  613. expect(new TextDecoder().decode(remote.nodes.get('/workspace/committed')?.data)).toBe('yes')
  614. })
  615. it('returns committed rename metadata without a fallible post-commit lookup', async () => {
  616. const remote = new FakeRemote()
  617. const getInfo = vi.spyOn(remote.sandbox.files, 'getInfo')
  618. const { fs } = await setup(remote)
  619. await expect(fs.writeText(await fs.resolve('committed'), 'yes'))
  620. .resolves.toMatchObject({ operation: 'create' })
  621. expect(getInfo).toHaveBeenCalledTimes(1)
  622. expect(remote.renames).toHaveLength(1)
  623. })
  624. it('cleans staging files and maps command, permission, and abort failures', async () => {
  625. const remote = new FakeRemote()
  626. const { fs } = await setup(remote)
  627. const commandTarget = await fs.resolve('command')
  628. remote.nextCommandError = commandError(1, 'chmod failed')
  629. await expectCode(fs.writeText(commandTarget, 'x'), 'FS_IO_ERROR')
  630. expect(remote.removals).toHaveLength(1)
  631. remote.nextRenameError = new Error('permission denied')
  632. await expectCode(fs.writeText(await fs.resolve('permission'), 'x'), 'FS_PERMISSION_DENIED')
  633. remote.nextRemoveError = new Error('cleanup also failed')
  634. remote.nextRenameError = new DOMException('aborted', 'AbortError')
  635. await expectCode(fs.writeText(await fs.resolve('abort'), 'x'), 'FS_ABORTED')
  636. const removalsBeforeCollision = remote.removals.length
  637. remote.nextMakeDirResult = false
  638. await expectCode(fs.writeText(await fs.resolve('collision'), 'x'), 'FS_IO_ERROR')
  639. expect(remote.removals).toHaveLength(removalsBeforeCollision)
  640. })
  641. it('applies literal edits atomically and restores the detected CRLF style', async () => {
  642. const remote = new FakeRemote()
  643. remote.file('/workspace/file.txt', 'one\r\ntwo\r\nthree\n')
  644. const { fs } = await setup(remote)
  645. const target = await fs.resolve('file.txt')
  646. const version = (await fs.stat(target))!.version
  647. const outcome = await fs.editText(
  648. target,
  649. { oldString: 'two\r\n', newString: 'TWO\r\n', replaceAll: false },
  650. { version },
  651. )
  652. expect(outcome).toMatchObject({ before: 'one\ntwo\nthree\n', after: 'one\nTWO\nthree\n' })
  653. expect(new TextDecoder().decode(remote.nodes.get('/workspace/file.txt')?.data)).toBe('one\r\nTWO\r\nthree\r\n')
  654. })
  655. it('reports stale and literal-match failures with stable codes', async () => {
  656. const remote = new FakeRemote()
  657. remote.file('/workspace/file.txt', 'a a')
  658. remote.dir('/workspace/dir')
  659. const { fs } = await setup(remote)
  660. const target = await fs.resolve('file.txt')
  661. await expectCode(fs.editText(target, { oldString: '', newString: 'x', replaceAll: false }), 'FS_EDIT_NOT_FOUND')
  662. await expectCode(fs.editText(target, { oldString: 'z', newString: 'x', replaceAll: false }), 'FS_EDIT_NOT_FOUND')
  663. await expectCode(fs.editText(target, { oldString: 'a', newString: 'x', replaceAll: false }), 'FS_AMBIGUOUS_EDIT')
  664. await expect(fs.editText(target, { oldString: 'a', newString: 'x', replaceAll: true }))
  665. .resolves.toMatchObject({ after: 'x x' })
  666. await expectCode(fs.editText(target, { oldString: 'x', newString: 'y', replaceAll: false }, { version: FsVersion('stale') }), 'FS_STALE_VERSION')
  667. await expectCode(fs.editText(await fs.resolve('missing'), { oldString: 'x', newString: 'y', replaceAll: false }), 'FS_STALE_VERSION')
  668. await expectCode(fs.editText(await fs.resolve('dir'), { oldString: 'x', newString: 'y', replaceAll: false }), 'FS_NOT_REGULAR_FILE')
  669. })
  670. it('serializes guarded mutations so only one stale version can win', async () => {
  671. const remote = new FakeRemote()
  672. remote.file('/workspace/file.txt', 'base')
  673. const { fs } = await setup(remote)
  674. const target = await fs.resolve('file.txt')
  675. const version = (await fs.stat(target))!.version
  676. const results = await Promise.allSettled([
  677. fs.writeText(target, 'one', { kind: 'replaceIfVersion', version }),
  678. fs.editText(target, { oldString: 'base', newString: 'two', replaceAll: false }, { version }),
  679. ])
  680. expect(results.filter(result => result.status === 'fulfilled')).toHaveLength(1)
  681. expect(results.filter(result => result.status === 'rejected')).toHaveLength(1)
  682. })
  683. })
  684. describe('E2B filesystem adapter integration edges', () => {
  685. it('maps canonicalization, permission, and generic provider failures', async () => {
  686. const remote = new FakeRemote()
  687. const { fs } = await setup(remote)
  688. remote.nextCommandError = commandError(1, 'not a directory')
  689. await expectCode(fs.resolve('bad'), 'FS_IO_ERROR')
  690. remote.nextCommandError = commandError(1)
  691. await expectCode(fs.resolve('bad-again'), 'FS_IO_ERROR')
  692. remote.nextCommandError = new Error('canonical transport failed')
  693. await expectCode(fs.resolve('bad-transport'), 'FS_IO_ERROR')
  694. remote.file('/workspace/a', 'a')
  695. const target = await fs.resolve('a')
  696. remote.nextInfoError = new Error('metadata transport failed')
  697. await expectCode(fs.stat(target), 'FS_IO_ERROR')
  698. remote.nextReadError = new Error('operation not permitted')
  699. await expectCode(fs.readText(target), 'FS_PERMISSION_DENIED')
  700. remote.nextReadError = 'transport vanished'
  701. await expectCode(fs.readText(target), 'FS_IO_ERROR')
  702. })
  703. it('uses listing metadata directly and canonicalizes only symbolic links', async () => {
  704. const remote = new FakeRemote()
  705. remote.file('/workspace/a', 'a')
  706. remote.file('/workspace/target', 'target')
  707. remote.file('/workspace/gone', 'gone')
  708. remote.symlink('/workspace/link', '/workspace/target')
  709. remote.symlink('/workspace/vanished-link', '/workspace/gone')
  710. remote.disappearOnInfo.add('/workspace/gone')
  711. const { fs } = await setup(remote)
  712. const directory = await fs.resolve('/workspace')
  713. const commandsBefore = remote.commands.length
  714. const getInfo = vi.spyOn(remote.sandbox.files, 'getInfo')
  715. const listed = await fs.listDir(directory)
  716. expect(listed.find(entry => entry.name === 'a')).toMatchObject({
  717. type: 'file', target: { targetKey: '/workspace/a' }, size: 1,
  718. })
  719. expect(listed.find(entry => entry.name === 'link')).toMatchObject({
  720. type: 'file', target: { targetKey: '/workspace/target' }, size: 6,
  721. })
  722. expect(listed.find(entry => entry.name === 'vanished-link')).toEqual({
  723. name: 'vanished-link',
  724. type: 'other',
  725. target: { targetKey: '/workspace/gone', displayPath: '/workspace/vanished-link' },
  726. })
  727. expect(remote.commands.slice(commandsBefore)).toHaveLength(2)
  728. expect(getInfo).toHaveBeenCalledTimes(3)
  729. })
  730. it('registers the package-owned empty invariant installer', async () => {
  731. const ctx = new Context()
  732. await ctx.plugin(InvariantService, { enabled: true })
  733. const fiber = await ctx.plugin(E2BFsInvariant).await()
  734. await fiber.dispose()
  735. })
  736. })