Skip to content
Source
Sign inSign up
TanStack/ai

`ai-claude-code`: the adapter writes and removes its temp files one by one, adding sequential sandbox round trips to every turn #2205

Send to my agent
Openjbpionnier(GitHub profile, opens in a new tab) opened this issue 0 comments

TanStack AI version

1.7.0

Framework/Library version

@tanstack/ai-claude-code 1.0.3

Describe the bug and the steps to reproduce it

TanStack AI version: @tanstack/ai-claude-code 1.0.3

Describe the problem

Before spawning the runner, ClaudeCodeTextAdapter in packages/ai-claude-code/src/adapters/text.ts writes up to five independent files, each behind its own await sandbox.fs.write(...):

  • the MCP bridge config (when a bridge is present),
  • the output schema (when outputSchema is set),
  • the runner source,
  • the argv file,
  • the prompt file (when the sandbox has no writable stdin).

None of these writes depends on another, but they run back to back. The finally block then removes the same files in a sequential loop:

for (const path of tempFiles) {
  try {
    await cleanupSandbox.fs.remove(path)
  } catch {}
}

On a remote sandbox provider each fs.write and fs.remove is at least one network round trip, so every turn pays up to five sequential round trips before the agent starts and up to five more after it ends.

Minimal reproduction

The script runs the real adapter over a fake sandbox whose fs calls each take 200 ms, and logs when each call starts. The spawn is stubbed out to fail, so the run goes straight from the writes to the cleanup. No sandbox or API key needed. Save both files, then npm install && npm start.

package.json

{
  "name": "repro-claude-code-sequential-temp-files",
  "private": true,
  "type": "module",
  "scripts": { "start": "node repro.mjs" },
  "dependencies": {
    "@tanstack/ai-claude-code": "1.0.3",
    "@tanstack/ai-sandbox": "1.4.0"
  }
}

repro.mjs

import { claudeCodeText } from '@tanstack/ai-claude-code'
import { SandboxCapability } from '@tanstack/ai-sandbox'

// A remote sandbox: every fs call is one network round trip of LATENCY_MS.
const LATENCY_MS = 200
const start = Date.now()
const log = (operation, path) =>
  console.log(`${String(Date.now() - start).padStart(5)} ms  ${operation} ${path}`)
const roundTrip = () => new Promise((resolve) => setTimeout(resolve, LATENCY_MS))

const sandbox = {
  id: 'fake',
  provider: 'fake-remote',
  workspaceRoot: '/workspace',
  capabilities: { writableStdin: false },
  fs: {
    write: async (path) => {
      log('write ', path)
      await roundTrip()
    },
    remove: async (path) => {
      log('remove', path)
      await roundTrip()
    },
  },
  process: {
    // The agent itself is out of scope: fail the spawn so the run goes straight
    // from the temp-file writes to the cleanup.
    spawn: async () => {
      log('spawn ', '(agent)')
      throw new Error('spawn stubbed out')
    },
  },
}

const [, provideSandbox] = SandboxCapability
const context = { capabilities: { markProvided: () => {}, has: () => true } }
provideSandbox(context, sandbox)

const noop = () => {}
const logger = { request: noop, provider: noop, errors: noop, agentLoop: noop, warnings: noop, warn: noop, debug: noop }

const stream = claudeCodeText('haiku').chatStream({
  model: 'haiku',
  messages: [{ role: 'user', content: 'ping' }],
  logger,
  capabilities: context,
})
for await (const _chunk of stream) {
  // drain: the stubbed spawn surfaces as a RUN_ERROR chunk, which is expected
}
console.log(`${String(Date.now() - start).padStart(5)} ms  done`)

Output on 1.0.3 (run id shortened):

    1 ms  write  /workspace/tanstack-claude-run-<run>.mjs
  202 ms  write  /workspace/tanstack-claude-argv-<run>.json
  403 ms  write  /tmp/tanstack-claude-prompt-<run>
  605 ms  spawn  (agent)
  605 ms  remove /workspace/tanstack-claude-run-<run>.mjs
  806 ms  remove /workspace/tanstack-claude-argv-<run>.json
 1007 ms  remove /tmp/tanstack-claude-prompt-<run>
 1207 ms  done

Three files here; a bridge and an output schema add two more. With the change below, the same script gives:

    1 ms  write  /workspace/tanstack-claude-run-<run>.mjs
    2 ms  write  /workspace/tanstack-claude-argv-<run>.json
    2 ms  write  /tmp/tanstack-claude-prompt-<run>
  202 ms  spawn  (agent)
  203 ms  remove /workspace/tanstack-claude-run-<run>.mjs
  203 ms  remove /workspace/tanstack-claude-argv-<run>.json
  203 ms  remove /tmp/tanstack-claude-prompt-<run>
  404 ms  done

Suggested change

Collect the write promises and await them together before the spawn, and settle the removals together:

const pendingWrites: Array<Promise<void>> = []
// ...
pendingWrites.push(sandbox.fs.write(mcpConfigPath, bridgeToMcpConfig(bridge)))
// ... same for the schema, runner, argv and prompt files
await Promise.all(pendingWrites)
if (cleanupSandbox) {
  await Promise.allSettled(
    tempFiles.map((path) => cleanupSandbox.fs.remove(path)),
  )
}

Promise.allSettled keeps the current behaviour of ignoring a failed removal. The files all have distinct paths, so the writes cannot conflict.

We have been running this change as a local patch on 1.0.2 and 1.0.3.

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.
  1. tanstack-source added labelsbug

  2. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

Sign in to join this conversation.