`ai-sandbox-vercel`: `fs.write` runs one `exec` per 32 KiB chunk instead of using `sandbox.writeFiles` #2204
Send to my agentTanStack AI version#
1.7.0
Framework/Library version#
@tanstack/ai-sandbox-vercel 1.0.1
Describe the bug and the steps to reproduce it#
TanStack AI version: @tanstack/ai-sandbox-vercel 1.0.1
Describe the problem
fs.write in packages/ai-sandbox-vercel/src/handle.ts goes through fsWriteCommands:
write: async (p, data, o) => {
o?.signal?.throwIfAborted()
for (const command of fsWriteCommands(this.abs(p), data)) {
const r = await this.exec(command)
if (r.exitCode !== 0)
throw new Error(`write failed: ${r.stderr.trim()}`)
}
},
fsWriteCommands emits one mkdir -p … && : > file command, then one printf … | base64 -d >> file command per 32 KiB of payload, to stay under MAX_ARG_STRLEN. Each command is a separate round trip to the Vercel Sandbox API, run sequentially. A 1 KiB file costs two round trips, and a 1 MiB file costs 33. The write is also not atomic: a failure or abort midway leaves a truncated file.
@vercel/sandbox already exposes sandbox.writeFiles, which uploads the bytes in a single call with no argv size limit.
Minimal reproduction
The script drives the real VercelHandle over a stand-in for the @vercel/sandbox object and counts the calls fs.write makes. No sandbox or API key needed. Save both files, then npm install && npm start.
package.json
{
"name": "repro-vercel-fs-write-round-trips",
"private": true,
"type": "module",
"scripts": { "start": "node repro.mjs" },
"dependencies": { "@tanstack/ai-sandbox-vercel": "1.0.1" }
}
repro.mjs
import { VercelHandle } from '@tanstack/ai-sandbox-vercel'
// Stand-in for the `@vercel/sandbox` object: every method call is one HTTP
// round trip to the Vercel Sandbox API, so counting calls counts round trips.
const calls = { runCommand: 0, writeFiles: 0 }
const sandbox = {
name: 'fake',
runCommand: async () => {
calls.runCommand += 1
return { exitCode: 0, output: async () => '' }
},
writeFiles: async () => {
calls.writeFiles += 1
},
}
const handle = new VercelHandle({ sandbox, workdir: '/vercel/sandbox', ports: [] })
for (const [label, bytes] of [
['empty file', 0],
['1 KiB', 1024],
['100 KiB', 100 * 1024],
['1 MiB', 1024 * 1024],
]) {
calls.runCommand = 0
calls.writeFiles = 0
await handle.fs.write('/vercel/sandbox/file.bin', new Uint8Array(bytes))
console.log(
`${label.padEnd(10)} -> ${calls.runCommand} runCommand, ${calls.writeFiles} writeFiles`,
)
}
Output on 1.0.1:
empty file -> 1 runCommand, 0 writeFiles
1 KiB -> 2 runCommand, 0 writeFiles
100 KiB -> 5 runCommand, 0 writeFiles
1 MiB -> 33 runCommand, 0 writeFiles
With the change below, every line reads 0 runCommand, 1 writeFiles.
Suggested change
write: async (p, data, o) => {
o?.signal?.throwIfAborted()
const content =
typeof data === 'string' ? Buffer.from(data, 'utf8') : Buffer.from(data)
const target = this.abs(p)
const path = target.startsWith('/') ? target : `${this.workdir}/${target}`
await this.sandbox.writeFiles(
[{ path, content }],
o?.signal ? { signal: o.signal } : undefined,
)
},
This also forwards the caller's signal to the upload, where the current loop only checks it once before starting.
The current implementation creates missing parent directories with mkdir -p, and writeFiles keeps that behaviour. Checked on a live sandbox with @vercel/sandbox 3.9.1: writing /tmp/probe-a/b/c/absolute.txt and /vercel/sandbox/probe-d/e/workdir.txt, with none of the intermediate directories existing, succeeds and creates them. The Vercel docs suggest calling mkDir() first, but it is not required.
We have been running this change as a local patch on 1.0.1.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
Typescript
Screenshots or Videos (Optional)#
No response
Do you intend to try to help solve this bug with your own PR?#
None
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
tanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) assignedtombeckenham
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
Sign in to join this conversation.