Sandbox / Code Execution — контролируемая среда для запуска кода, команд, преобразований и потенциально опасных вычислений так, чтобы ошибка или враждебный input не получили прямой доступ к host OS, секретам, пользовательским данным, сети и production-инфраструктуре.
№29 Code as a Reasoning Tool объясняет, зачем агенту исполнять код как когнитивный инструмент; №53 отвечает, где и с какими ограничениями этот код запускается. №51 Permissions & Secrets управляет credentials/capabilities; sandbox не должен получать их автоматически. №52 Agent Security задаёт threat model; sandbox — один из execution controls. №48 Guardrails решает, можно ли запускать конкретный класс execution. №50 Contracts задаёт structured run request/result. №70 Model Serving запускает ML inference, а не arbitrary user/generated code.
Prerequisites: №12 Tools, №29 Code as Reasoning Tool, №46 Observability, №48 Policies, №50 Contracts, №51 Permissions, №52 Security. Forward references: №57 Queues/Workers, №60 Artifact Store, №63 Fallbacks/Retry/Circuit Breakers, №64 Budgets, №69 Distributed Reliability, №74 Computer Use.
REQUEST-TIME: CONDITIONAL — только если конкретный task требует execution. CONTROL PLANE: YES — images/profiles/limits/allowlists/runtime versions. DATA PLANE: YES — code, input files, stdout/stderr, produced artifacts. OFFLINE: YES — security tests, image scanning, dependency refresh, stress/failure tests.
Success: job завершается в рамках policy и возвращает sanitized structured result/artifacts. Retryable: transient worker/runtime startup failure. Permanent: forbidden syscall/resource/network/path, invalid package, policy deny, deterministic code error. Idempotency: repeated job must not duplicate external effects; sandbox ideally has no direct external side effects. Persist: run config/hash, runtime image, limits, code/artifact refs, exit code, logs, policy result. Trace: lifecycle + resources + violations.
№53 не владеет general tool orchestration, prompt-injection detection, permissions store, secret manager, package repository, job queue, artifact store или full container platform. Она владеет CONTAINED EXECUTION ENVIRONMENT AND ITS SECURITY/RESOURCE CONTRACT.
Генерирует Python/JS/shell-like instructions или вызывает code tool.
Ошибка здесь — bad proposal.
Ограничивает filesystem, network, process tree, time, CPU, RAM, credentials, syscalls.
Ошибка здесь должна остаться contained.
Получает structured result/artifacts. Любые внешние side effects проходят отдельные tools/policy gates.
Код пытается читать ~/.ssh, configs, user files или менять host filesystem.
Process видит API keys, cloud creds, tokens или metadata endpoints.
Код отправляет данные наружу или загружает произвольный payload.
Infinite loop, fork bomb, giant files, huge allocations.
Попытка выйти из containment или использовать dangerous host interfaces.
Динамическая установка пакетов расширяет attack surface и reproducibility risk.
Один job оставляет файлы/процессы, которые влияют на следующий task.
Код напрямую вызывает production API, обходя Tool/Policy layer.
Gigantic stdout, malformed files, path traversal, unexpected binaries.
{
"contract": "sandbox.run.v1",
"run_id": "RUN-...",
"profile": "CALCULATE",
"runtime": "python-3.x-fixed",
"code_ref": "artifact://code/...",
"input_refs": [
"artifact://input/table.csv"
],
"limits": {
"wall_time_ms": 15000,
"cpu_ms": 10000,
"memory_mb": 512,
"disk_mb": 256,
"max_processes": 16,
"max_output_mb": 10
},
"network": {
"mode": "DENY"
},
"outputs": {
"allowed_globs": ["out/*"]
}
}Модель может предложить code и нужный profile, но host/policy определяет:
Runtime image ideally immutable/read-only where practical.
Только listed artifacts; read-only unless transformation requires copy.
Ограниченный tmp/work directory с quota.
Не монтировать home, Docker socket, host root, SSH, cloud configs.
Collect только конкретную output directory/pattern.
Reject traversal, symlink escape, unexpected device paths.
Maximum total/file size, file count, archive expansion limits.
Удалить workspace после artifact collection.
| Mode | Когда | Контроль |
|---|---|---|
| DENY | Calculations, local transforms, most tests. | No outbound/inbound network. |
| ALLOWLIST | Нужен конкретный package/data/API endpoint. | Exact domains/IP/service proxies + method restrictions where possible. |
| PROXY | Нужно контролировать/логировать egress. | All traffic via policy-aware proxy. |
| OPEN | Редкий доверенный internal profile. | Не использовать для arbitrary generated code без отдельной threat justification. |
Не передавать все env vars parent process. Собрать минимальный clean env.
Sandbox не должен автоматически получать host/cloud workload credentials.
Если code нужен ограниченный доступ к данным, лучше дать narrow broker/tool, чем raw credential.
Maximum elapsed time; hard kill after deadline.
CPU quota/time protects shared host.
OOM limit; prevent host swap/degradation.
Workspace quota + max individual artifact.
Limit process/thread count; mitigate fork storms.
Bound stdout/stderr bytes and line rate.
Prevent millions of tiny files / archive explosion.
Per-user/tenant/global active sandbox limits.
Обычный subprocess с отдельным working dir подходит для trusted local scripts, но не считается strong hostile-code sandbox.
Namespaces/cgroups/rootless/non-root/seccomp/capability drop/read-only FS. Требует правильной конфигурации.
Более сильная boundary для truly untrusted multi-tenant code, ценой startup/ops complexity.
| Pattern | Риск | Лучше |
|---|---|---|
| pip/npm install anything | Supply chain, network, nondeterminism, arbitrary install scripts. | Prebuilt images / curated packages / lockfiles. |
| Latest versions | Run today != run tomorrow. | Pinned/locked versions + image digest. |
| Unknown binary download | Executes uncontrolled code. | Artifact allowlist + verified source/hash. |
| Model chooses repository | Can be steered to malicious source. | Host-controlled package sources. |
Exit code / timeout / killed / policy_violation.
Capture truncated/sanitized logs, not unlimited streams.
Allowed path/type/size/hash before promotion to Artifact Store.
Produced binary/script does not gain automatic execution authority.
{
"contract": "sandbox.result.v1",
"run_id": "RUN-...",
"status": "SUCCESS",
"exit_code": 0,
"runtime": {
"image_digest": "sha256:...",
"profile": "CALCULATE"
},
"usage": {
"wall_ms": 1240,
"cpu_ms": 830,
"peak_memory_mb": 92,
"disk_written_mb": 1.8
},
"stdout_ref": "artifact://log/...",
"stderr_ref": null,
"artifacts": [
{
"ref": "artifact://out/result.csv",
"sha256": "...",
"bytes": 18422
}
],
"violations": []
}Нужно знать:
Это позволяет evals и debugging отличить code bug от infrastructure failure.
Generated script получает production API token и сам вызывает external endpoint. Policy/tool trace больше не контролирует side effect.
Sandbox генерирует artifact/structured intent. Затем обычный Tool/Action Engine отдельно проверяет permissions, policy, approval и выполняет external action.
| Status | Meaning | Typical response |
|---|---|---|
| SUCCESS | Process exited normally and required outputs validated. | Continue / verify semantics. |
| CODE_ERROR | Program exception/nonzero deterministic exit. | Targeted code repair if budget allows. |
| TIMEOUT | Wall/CPU deadline exceeded. | Kill process tree; maybe simplify/retry with same hard max. |
| RESOURCE_LIMIT | RAM/disk/PID/output cap hit. | Reformulate; don't blindly raise limits. |
| POLICY_DENIED | Profile/network/path/action not permitted. | Do not retry to bypass. |
| SANDBOX_FAILURE | Runtime/worker/container infrastructure failed. | Retry/fallback on clean environment. |
| ARTIFACT_REJECTED | Output failed file/type/size/path checks. | Do not promote; repair/terminate. |
user/task/agent/tenant/profile.
image digest, language/runtime version, code hash.
time/CPU/RAM/disk/PID/network policy.
wall/cpu/peak memory/files/output bytes.
network/path/syscall/resource policy hits.
status/exit code/error class/artifact refs.
workspace/process cleanup completed.
Link sandbox span to agent/tool/eval run.
Attempt access outside allowed mounts must fail.
Host env/secrets/cloud metadata unavailable.
DENY profile cannot egress; allowlist allows only intended endpoints.
Infinite loop/fork/large allocation/file spam contained.
Traversal/symlink/oversize/unexpected output rejected.
No process/workspace contamination between jobs.
Repeated infra retry does not create external effect.
Legitimate workloads still succeed within realistic limits.
Unauthorized host/network/secret access. Target: zero.
% runs killed by wall/CPU budget.
Memory/disk/PID/output limit hits by profile.
Time to create isolated environment.
% legitimate jobs completing under current limits.
Outputs rejected by path/type/size policy.
Blocked network attempts by profile/task.
CPU/RAM/time cost per successful sandbox job.
sandbox/
├── profiles.yaml
├── runner.py
├── limits.py
├── filesystem.py
├── network.py
├── artifacts.py
├── cleanup.py
└── tests/
profiles:
calculate:
network: deny
memory_mb: 512
wall_time_s: 15
disk_mb: 256
pids: 16
image: python-fixed@sha256:...
document:
network: deny
read_only_inputs: true
output_dir: /work/out
image: document-tools@sha256:...
default:
secrets: none
root: non-root
workspace: ephemeralЕсли позже появится multi-tenant hostile-code execution, усилить boundary до специализированного sandbox/microVM layer.
| Вопрос | Ответ |
|---|---|
| Стоит ли реализовывать? | Только если системе реально нужно исполнять code/scripts. Для purely textual/retrieval system — нет. |
| Separate Component? | YES. Execution boundary должна быть отдельной logical/runtime capability внутри Tool / Action Engine. |
| Минимум 80% ценности? | Ephemeral isolated profile, no secrets, network deny, filesystem boundary, resource limits, cleanup, structured output. |
| Когда overkill? | Строить microVM fleet, kernel-level isolation orchestration и multi-region workers для single-user local calculator script. |
| Trigger? | Need to run generated/user code, tests, transformations, simulations or arbitrary executable logic. |
| Как измерить uplift? | Containment escapes, resource incidents, reproducibility, sandbox success rate, latency/cost, failures prevented. |
| Можно ли rule/tool/code вместо LLM-agent? | Это и должен быть code/runtime layer. LLM не является sandbox. |
Происхождение от вашей модели не делает execution trusted.
Не давать code execution там, где tool/schema решают задачу дешевле.
Sandbox стартует с clean env и без host credentials.
Добавлять egress только для доказанного use case.
Только нужные inputs и ephemeral output workspace.
CPU/RAM/time/disk/PIDs/output/concurrency имеют caps.
Sandbox produces result; production tool performs external side effect.
Persistence — explicit exception, not default.
Artifacts/logs проходят validation before promotion/use.
MODEL / USER / WORKFLOW
↓
EXECUTION INTENT
↓
DO WE NEED CODE EXECUTION?
├─ NO → USE NORMAL TOOL / RULE / TRANSFORM
│
└─ YES
↓
SELECT ALLOWED SANDBOX PROFILE
↓
POLICY / PERMISSION CHECK
↓
PREPARE:
code_ref
explicit input_refs
fixed runtime
hard limits
network policy
no secrets by default
↓
CREATE EPHEMERAL ISOLATION
↓
RUN AS NON-PRIVILEGED WORKLOAD
↓
ENFORCE:
filesystem
network
CPU
RAM
disk
PIDs
time
output size
↓
CAPTURE:
exit
stdout/stderr refs
usage
violations
artifacts
↓
VALIDATE ARTIFACTS
↓
DESTROY WORKSPACE + PROCESS TREE
↓
RETURN STRUCTURED RESULT
↓
IF EXTERNAL EFFECT NEEDED:
NORMAL TOOL ENGINE
→ POLICY
→ PERMISSION
→ HITL
→ EXECUTE
CORE PRINCIPLE:
SANDBOX IS NOT
"LET THE AGENT DO ANYTHING SAFELY".
SANDBOX IS
"A SMALL, EXPLICIT BOX
IN WHICH FAILURE IS EXPECTED
AND DAMAGE IS BOUNDED".
A. Related ECC ideas. Context-as-cache, scoped memory, lifecycle hooks, selective capabilities, feature flags, deterministic enforcement, provider-neutral adapters and eval-gated learning are applied only where relevant to №53 Sandbox / Code Execution.
B–E. Existing boundary and placement. The existing conceptual boundary, class SPECIALIZED, default OFF and owner Tool / Action Engine remain authoritative. Runtime/control/data/offline placement is unchanged; durable state stays outside model context.
F–H. Hooks and contracts. Use bounded PRE_MODEL/POST_MODEL, PRE_TOOL/POST_TOOL, CHECKPOINT and TASK_COMPLETED events as applicable. Illustrative fields and canonical contracts are defined in NEW_CONTRACTS_SPEC.md; no universal schema is implied.
I–J. Security and evaluation. Host-side schema, permission, secret, budget, idempotency and audit checks take precedence over LLM output. Optional mechanisms require a feature flag and WITH/WITHOUT ablation; measure quality, acceptance, correction, latency, cost, escalations and severe errors.
K–L. Task profiles and cross-references. A TaskProfile selects the relevant skill, tool/context slice, memory scope and enforcement profile independently from FAST/STANDARD/DEEP. See cross-reference map, hook spec and ablation plan. Provider adapters remain outside the core.