Safeguard
AI Security•

Should an agent sandbox be fresh for every task or reused?

A fresh sandbox per task limits leftover state and cross-task leakage. When reuse is acceptable, and what to wipe if you do reuse an agent environment.

Safeguard Security Team
6 min read

Fresh per task is the safer default. A new environment cannot carry credentials, files, or poisoned state from the last run. Reuse can be acceptable for speed if you wipe the workspace and credentials between tasks, keep it within one repository and one trust level, and cache only content you can rebuild and verify.

Documented examples

One hosted platform says that each session gets a fresh VM, and that after a few minutes without activity a session's VM pauses with its files saved, then the next message restores the same VM. That is reuse inside one session, not across tasks. It also caches installed dependencies so they exist at the start of each new session, and states that the cache stores files only, not running processes.

Another platform recommends ephemeral, single-use runners for self-hosted setups.

So the common pattern is a fresh machine per task, with an optional cache for installed packages.

What leftover state can hold

ItemRisk if reused
Environment variables and tokensNext task inherits them
Shell history and logsContain commands and sometimes secrets
Files from earlier tasksMay include data from another repository
Installed packagesA bad install persists
Agent memory or notesInstructions planted earlier can steer later work
Git config and credentials helpersMay authorize unintended pushes

Memory deserves its own mention. If an agent keeps notes between runs, injected text from one task can influence the next. Treat any persisted agent state like untrusted input.

Rules for a reuse policy

  1. Reuse only within one repository and one team.
  2. Wipe the workspace, home directory, temporary files, and shell history between tasks.
  3. Rotate or remove task credentials every time.
  4. Cache only dependency downloads from a lockfile, and verify them.
  5. Rebuild the base image on a schedule and when advisories apply.
  6. Never share an environment between tasks of different sensitivity.
  7. Set a maximum lifetime, such as a day.

Cost versus safety

Fresh environments cost start time and some compute. A cache cuts most of that without keeping task state. If start time is the complaint, solve it with a prebuilt image or cache of dependencies, not by keeping the whole machine alive.

A quick test

Run two tasks back to back in the same environment. In the second, ask the agent to list the files it can see, print its environment variable names, and show recent shell history. If anything from the first task appears, your cleanup is incomplete.

Worked example: what a reused environment can leak

An illustrative sequence in a single reused environment.

Task 1. An agent works on a repository for the billing team. It reads a test fixture containing realistic customer records, and a developer pastes a temporary token into the prompt to debug an integration. The agent writes notes to a scratch file to keep its place.

Task 2. The same environment is reused for a different team's task. The agent lists the working directory, finds the scratch file, and reads the fixture path in its notes. Shell history still shows the earlier commands. The environment variables still hold the token. A planted instruction in the earlier task's content, saved in the notes, now sits in context for the new task.

None of this required an escape or an exploit. It required reuse.

With a fresh environment per task, none of those artifacts exist for Task 2.

Cleanup checklist if you must reuse

AreaWhat to remove
WorkspaceAll files, including hidden ones, and the git directory
Home directoryShell history, editor state, tool caches that hold content
Temp directoriesEverything
EnvironmentUnset task variables, restart the process tree
CredentialsRevoke task tokens, clear credential helpers
Network stateClose connections, flush caches that retain names or tokens
Agent stateClear memory, notes, and any persisted context
LogsShip out, then clear local copies

Verify by running the quick test from earlier: ask a fresh agent to list files, variable names, and history, and confirm nothing from the last task appears.

Speed without reuse

If the issue is start time, there are options that do not keep task state.

  1. Prebuilt images that include the toolchain and dependencies for a given repository.
  2. Dependency caches keyed to the lockfile hash, rebuilt on change.
  3. Warm pools of clean, unused environments waiting for assignment, discarded after one use.
  4. Smaller base images that start faster and expose less.

These keep the benefit of speed while preserving per-task isolation.

Lifetime and idle behavior

One platform pauses an idle session's VM and restores it on the next message. That is acceptable within a single session, but set a maximum lifetime so a paused session does not become a long lived environment. End the session when the task ends, and archive or delete the record according to your retention policy.

A rule of thumb

If you cannot name every piece of state that survives from one task to the next, assume something sensitive does. Prefer designs where the list is empty.

Where Safeguard fits

Safeguard does not manage sandbox lifecycles. Its asset discovery and agent identity features help you track which agents exist, and dependency scanning covers what cached images contain.

Try it yourself

Click through it at your own pace, or open it full screen.

Click through it and notice how state accumulates and changes over time, which is the same drift a reused environment suffers.

Quick check

Is pausing and resuming a VM the same as reuse?

Within one session it keeps that session's state, so treat it as one trust boundary and end it when the task ends.

Can I cache the whole environment?

Cache what you can rebuild and verify, such as packages. Avoid caching credentials or task files.

Is a warm pool of clean environments the same as reuse?

No. A warm pool holds environments that have never run a task, and each is used once and discarded. Reuse means running a second task in an environment that already ran one.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.