Sandscale 如何在沙盒中傳入程式碼並取回結果:檔案、資料夾與變更清單
Getting code into a sandbox and results out: files, folders and change lists
Sandscale API 提供將程式碼傳入沙盒及取回結果的工具,文章以 TypeScript SDK 和 curl 説明如何寫入單一檔案、上載整個專案資料夾,以及讀取檔案。上載專案後,`/project/changes` 可列出上載後有變更的檔案,讓用戶只取回相關內容。文章提醒,傳入資料應排除不需要的私密資料,取回內容則須視為不可信資料。
Starting a sandbox is the easy part. The first real question in any agent integration is how to get the code in, and how to get the result out. Copying a repository one file at a time is slow and fiddly. Copying it all, including .env and node_modules, is wasteful and sometimes dangerous. And at the end you usually want one thing: which files did the agent change?
Sandscale's API has a tool for each of those steps. This post walks through them with the TypeScript SDK and curl, using only calls from the run code guide and the Sandboxes API reference.
Setup for the curl examples, as in the guide:
API=https://api.sandscale.dev/api/v1/sandbox
AUTH="Authorization: Bearer $SANDSCALE_API_KEY"
Option 1: single files
For a handful of files, such as one script the model just wrote or a config file, write them directly. The content travels base64-encoded in REST:
curl -X PUT "$API/instances/$ID/files" -H "$AUTH" -H "Content-Type: application/json" \
-d "{\"path\": \"/workspace/hello.py\", \"content\": \"$(printf 'print(\"hi\")' | base64)\"}"
The SDK does the encoding for you and accepts a string or bytes:
import { Sandscale } from '@sandscale/sdk';
const sandscale = new Sandscale(); // reads SANDSCALE_API_KEY
const box = await sandscale.sandboxes.create({ template: 'builtin:python', timeoutSeconds: 1800 });
// modelWrittenCode is the string your model produced
await sandscale.sandboxes.writeFile(box.id, '/workspace/analysis.py', modelWrittenCode, { region: box.region });
writeFile also takes a mode option for file permissions, useful for a script you want to execute directly. Each request carries one file, up to what the server accepts per request; the reference lists 413 file_too_large for anything bigger.
This is the right tool for an agent's code interpreter: the model writes code, you write it to a file, you run it.
Option 2: a whole folder as one archive
For a repository or a project directory, send everything in one request. POST /instances/{id}/project takes a gzip tar archive and unpacks it in the sandbox. An optional dir query parameter says where.
tar -czf project.tgz -C ./my-service .
curl -X POST "$API/instances/$ID/project" -H "$AUTH" \
-H "Content-Type: application/gzip" --data-binary @project.tgz
The reference does not name a content type for the body; application/gzip describes it accurately.
The answer describes what happened, per the reference's ProjectUpload type: the target dir, the number of files and bytes unpacked, a count of files that were skipped by reason, up to 100 skipped_paths with the reason for each, and the run_id of the unpack command.
That skip list is worth reading. The reference names three reasons a file is left out: it was gitignored, it was a special file, or it was denied by a rule. So files your .gitignore excludes, such as build output and dependency folders, can be left out of the upload. If a file your tests need is missing in the sandbox, skipped_paths tells you why.
A few practical notes:
- Build the archive from a clean checkout in CI, not from a developer's working directory, so you know exactly what went in.
-
Check what is not gitignored. If a
.envor credentials file is tracked or simply not ignored, it goes in with everything else. A sandbox is a place for code you are willing to have read by the code you are running. -
Handle the error codes. The reference lists
413 project_too_large,400 invalid_project_archive,422 project_extract_failed,409 instance_paused(resume first) and429 project_upload_busy(an upload is already in progress; wait and retry).
Option 3: reading results back
When the agent is done, read what you need. GET /instances/{id}/files?path=... reads a file, or lists a directory if the path is one:
curl "$API/instances/$ID/files?path=/workspace" -H "$AUTH"
In the SDK, listFiles returns entries with path, name, isDir and size, and readFile returns the bytes:
const entries = await sandscale.sandboxes.listFiles(box.id, '/workspace/out', { region: box.region });
for (const entry of entries.filter((e) => !e.isDir)) {
const bytes = await sandscale.sandboxes.readFile(box.id, entry.path, { region: box.region });
// save bytes, attach them to the PR, or pass a summary to the model
}
Which files changed?
For a coding agent the most useful question is not "what is in the sandbox" but "what did the agent change". If you uploaded with the project endpoint, GET /instances/{id}/project/changes answers it directly: the files that changed since the project was uploaded.
curl "$API/instances/$ID/project/changes" -H "$AUTH"
Per the reference's ProjectChanges type, the answer has the dir, a changed list of paths, a truncated flag if the list was cut short, and a run_id. Read only those files back, and you have the agent's work without copying the whole tree out.
A pattern for an agent that edits a repository:
- Upload the repository with the project endpoint.
- Let the agent work: run tests, edit files, run tests again.
- Ask for the change list.
- Read back only the changed files, or run
git diffin the sandbox and read its output. - Turn the result into a branch or a PR on your side, where your normal review rules apply.
- Stop the sandbox.
Step 5 deliberately happens outside the sandbox. The sandbox never needs your Git push credentials, because it never pushes. It produces a diff; your trusted code decides what to do with it.
Keep the direction of trust clear
All three options move data across the boundary between your systems and the code you are containing, so it is worth being deliberate about direction:
- In: only what the task needs. A clean checkout, minus secrets. Secrets the run genuinely needs go through Vault as environment variables, scoped to the run, not as files in the archive.
- Out: treat everything you read back as untrusted. It was written by code you did not review. Do not execute it on your side, and validate files before acting on them, the same as any input from outside.
Where to go next
- The run code guide has every step with curl and the SDK.
- For running a contributor's or an agent's pull request in a throwaway sandbox, see running untrusted pull request code.
- For long-running commands after the upload, see streaming sandbox output.
Originally published on the Sandscale blog.
來源:DEV Community · dev.to