DEV Community

Sandscale
Sandscale

Posted on Edited on Originally published at sandscale.dev

Watching what an agent's sandbox is doing: CPU, memory, disk and run history

Short answer: a handful of CLI commands answer most "what is my agent doing in there?" questions. sandscale sandbox list shows which sandboxes exist, sandscale sandbox instances-metrics-list --id ID shows a sandbox's allocated CPUs and memory and its usage samples, sandscale sandbox runs-list --id ID lists the commands that ran with their status and exit codes, and sandscale sandbox run-get --id RUN_ID gives one run's exit code, duration and cost.

Everything below is checked against the help text of CLI 1.3.0 (sandscale help sandbox <command>) and the sandboxes API guide.

Which sandboxes are running?

sandscale sandbox list
sandscale sandbox get --id SANDBOX_ID
Enter fullscreen mode Exit fullscreen mode

list shows the sandboxes you can use, newest first, with their status, template and image. It takes --limit N and --cursor C for paging, --all-pages to walk every page, and --status; every command takes --json for scripts. get shows one sandbox's status, size, template, image, network mode and timestamps. If your agents put labels on the sandboxes they start (--labels KEY=VALUE on sandscale sandbox create, up to 16, or labels in the SDK), you can tie each one back to a task or pull request.

A sandbox your agent forgot to terminate still ends when its time limit runs out, but it counts against your plan's vCPU-hours until then. list is the quickest way to spot one, and sandscale sandbox terminate --id ID --yes ends it.

How hard is it working?

sandscale sandbox instances-metrics-list --id SANDBOX_ID --json
Enter fullscreen mode Exit fullscreen mode

The answer has the sandbox's allocated CPUs and memory. Usage samples are listed when the sandbox's host reports them; otherwise metrics is empty and code says why. --start takes an RFC 3339 time and returns only samples from then on. The command has no follow mode, so to watch a sandbox, poll it from a loop and pipe the JSON into jq or a log shipper.

What to look for in the samples you get:

  • CPU pinned for a long time on a task that should be quick: a loop, or a test suite that hangs waiting for something it cannot reach.
  • Memory climbing toward the box's size: the next command may come back with oom_killed. Each vCPU comes with 4 GiB; see sizing sandboxes.
  • Disk filling: an agent writing logs or build output without cleaning up. See resource exhaustion in agent sandboxes.

What did it run?

sandscale sandbox runs-list --id SANDBOX_ID
sandscale sandbox run-get --id RUN_ID
sandscale sandbox run-output --id RUN_ID
Enter fullscreen mode Exit fullscreen mode

runs-list lists the commands run in the sandbox with their status and exit codes. run-get gives one run's status, exit code, duration and cost. run-output reads a run's stdout and stderr, but only for a command started with exec and wait false, and only while the output is kept: a command you waited for handed its output to its caller, and run-output then reports output_available false with the reason not_kept. For a run in progress, sandscale sandbox logs --id SANDBOX_ID --run-id RUN_ID streams its output as server-sent events, and --from-chunk resumes after a disconnect.

This is useful after the fact: the agent's transcript tells you what it meant to do, and the run history tells you what actually executed.

From code: the run record

Every command run through the API is recorded. The exec answer includes a run_id (runId in the TypeScript SDK), and:

curl "https://api.sandscale.dev/api/v1/sandbox/runs/$RUN_ID" -H "Authorization: Bearer $SANDSCALE_API_KEY"
Enter fullscreen mode Exit fullscreen mode

returns that run's details and cost; sandscale sandbox run-get --id RUN_ID is the same call from the CLI. Storing the run ID next to the agent step that caused it gives you a per-step cost without estimating. The exec answer itself already has exit_code, duration_ms, and timed_out and oom_killed flags.

Live output while it runs

For your own code rather than a terminal, the TypeScript SDK's run accepts onStdout and onStderr callbacks that receive output in order as the server releases it, and the result's streamed field says whether they got live output or the final output at the end. Streaming sandbox output in TypeScript covers the details.

Who did what

The CLI shows what a sandbox did. Who started it, stopped it and ran which commands is in your organization's hash-chained audit log, which every plan can read in the console and Team and Enterprise can export. Together they cover "what happened" and "who did it". More on how each Sandscale sandbox is isolated is on the product page.

FAQ

How do I see a Sandscale sandbox's CPU and memory use?

Run sandscale sandbox instances-metrics-list --id ID. It returns the allocated CPUs and memory, and usage samples when the sandbox's host reports them. Add --start to return only samples from a given time and --json for the server's answer as it came.

How do I see what commands ran in a sandbox?

sandscale sandbox runs-list --id ID lists the sandbox's runs with their status and exit codes, and sandscale sandbox run-get --id RUN_ID shows one run. sandscale sandbox logs --id ID --run-id RUN_ID streams a run's output.

Can I get the cost of a single command?

Yes. Every run through the API is recorded, and GET /api/v1/sandbox/runs/{id} with the run ID returns its details and cost.

Do forgotten sandboxes keep running forever?

No. A sandbox stops by itself when its time limit runs out. Until then it counts against your plan's vCPU-hours, so list your sandboxes and terminate ones you no longer need.

Related

Originally published on the Sandscale blog.

Top comments (0)