Skip to content
Agent Field Notes
Course contentsExpand a topic to choose a lesson.

SYSTEMATIC LEARNING / FOUR-LEVEL COURSE

Isolate resources and capabilities when executing code

Examine files, networks, processes, credentials, and resource limits.

Learning objectives: Code execution risk depends on the capabilities the process actually possesses. Restrictions must be enforced by the execution environment over files, networks, credentials, process trees, and resources, and conventions in the prompt are not a substitute for isolation.

Content checked: 2026-10-30 · Each level has independent explanations, tasks and inspections

Choose a starting point based on your familiarity with this topic. Current level: Foundation · Understand the concepts. After completing the task, continue to the next level. Reading and self-checks alone do not establish mastery.

On this level

Review the prerequisites

Suitable for: Backend developers learning Agent applications.

Isolate resources and capabilities when executing code
Code execution risk depends on the capabilities the process actually possesses. Restrictions must be enforced by the execution environment over files, networks, credentials, process trees, and resources, and conventions in the prompt are not a substitute for isolation.

How does the mechanism work?

Step-by-step explanation
  1. What are the risks of mounting a Docker socket?

    Access to control sockets can expand a container's capabilities, so describe those capabilities explicitly.

  2. What should I do if the main process exits after the code opens a background subprocess?

    Execution lifetime limits need to cover the entire process tree.

  3. Can restricting only file paths prevent symlinks from going out of bounds?

    File access restrictions must account for path resolution and race conditions.

Foundation · Understand the concepts

Mechanism before code

Objectives of this level: manifest capability and three independent resource bounds — Derive eligibility and the exact branch order from concrete inputs.

Visual guide · Admission is before enforcement

Admission is before enforcement

The local function plans admission only. An executor must enforce mounts, network, credentials, process-tree lifetime and limits; a prompt cannot enforce them. Select the figure to open it at full size.

Mechanism before code

The after-sales analyst requests reading a return CSV and creating a chart. These application capability labels describe a proposed manifest, not Linux capabilities. A successful decision does not start an executor. Work-directory choice does not constrain all file/network/process access; Docker isolation depends on its configuration and daemon exposure. Docker security.

CPU allowance, memory allowance and artifact allowance constrain different resources. One passed bound cannot substitute for another. A tenant match also does not grant an unlisted capability. A host_socket request fails regardless of its small CPU budget. Write a branch trace for the three inputs and create one additional legal/negative input yourself before completing the function.

Starting point and evidence

Download the source-only ZIP, extract it, and work in extended-agent-practice-v1 with lab_runner.py beside cases/. Python3.9+ standard library; no model, cloud, network or business request is made. Scaffold once in a fresh directory; it refuses overwrite. Reuse your module in later tracks. The empty template must exit1 on grade; scaffold exits0, overwrite exits2. Grade exits0 only when every fixed case passes and otherwise exits1.

  • https://ai.lwkjob.com/downloads/extended-agent-practice-v1.zip
  • https://ai.lwkjob.com/lab/extended-practice/lab_runner.py
  • https://ai.lwkjob.com/lab/extended-practice/cases/coding-sandbox-boundary.json
python3 lab_runner.py scaffold --course coding-sandbox-boundary --out coding_sandbox_boundary_student.py
python3 lab_runner.py grade --course coding-sandbox-boundary --module coding_sandbox_boundary_student.py --out coding_sandbox_boundary_before.json

This course has10 public fixed cases. Each calls your sandbox_plan(payload), records input/expected/actual/error, strict JSON types, input immutability and no output-container aliases, plus module/runner/cases SHA and Python version. Inputs are complete JSON fixtures. Unknown or missing keys, null outside explicitly allowed fields, wrong types, duplicate IDs/list members where uniqueness is required raise ValueError:input_shape; booleans are not integers. Exceptions match exact class:message. Return fresh JSON values without mutating input.

Trusted identity, approvals, receipts and policy/index metadata are fixtures assembled by the exercise. In a real service the server must obtain them from authenticated/current authority sources; accepting client JSON with these labels does not authenticate it. The function is a decision over that trusted snapshot, not the adapter or race-proof enforcement.

Local function contract

payload has exactly principal/request/policy. principal={tenant:nonempty str,enabled:strict bool}. request={tenant:nonempty str,capabilities:unique nonempty str list,cpu_ms:positive strict int,memory_mb:positive strict int,artifact_bytes:nonnegative strict int}. policy={capabilities:unique nonempty str list,max_cpu_ms:positive strict int,max_memory_mb:positive strict int,max_artifact_bytes:nonnegative strict int}. Empty capability lists are legal. Validate all nested shapes first. Then deny in this order: tenant_mismatch, principal_disabled, capability (requested set is not a subset), cpu_budget, memory_budget, artifact_budget. Equality with a maximum is allowed. Return exactly decision and reason: deny plus the first reason, or allow/bounded_manifest. No execution or cleanup occurs.

def sandbox_plan(payload):
    raise NotImplementedError("Implement the public local contract")

Three reviewable inputs

at-all-limits

{
  "principal": {
    "tenant": "acme",
    "enabled": true
  },
  "request": {
    "tenant": "acme",
    "capabilities": [
      "read_csv",
      "write_chart"
    ],
    "cpu_ms": 100,
    "memory_mb": 64,
    "artifact_bytes": 1000
  },
  "policy": {
    "capabilities": [
      "read_csv",
      "write_chart"
    ],
    "max_cpu_ms": 100,
    "max_memory_mb": 64,
    "max_artifact_bytes": 1000
  }
}

Expected / 预期: {"decision": "allow", "reason": "bounded_manifest"}

cpu-over

{
  "principal": {
    "tenant": "acme",
    "enabled": true
  },
  "request": {
    "tenant": "acme",
    "capabilities": [
      "read_csv",
      "write_chart"
    ],
    "cpu_ms": 101,
    "memory_mb": 64,
    "artifact_bytes": 1000
  },
  "policy": {
    "capabilities": [
      "read_csv",
      "write_chart"
    ],
    "max_cpu_ms": 100,
    "max_memory_mb": 64,
    "max_artifact_bytes": 1000
  }
}

Expected / 预期: {"decision": "deny", "reason": "cpu_budget"}

host-socket

{
  "principal": {
    "tenant": "acme",
    "enabled": true
  },
  "request": {
    "tenant": "acme",
    "capabilities": [
      "host_socket"
    ],
    "cpu_ms": 100,
    "memory_mb": 64,
    "artifact_bytes": 1000
  },
  "policy": {
    "capabilities": [
      "read_csv",
      "write_chart"
    ],
    "max_cpu_ms": 100,
    "max_memory_mb": 64,
    "max_artifact_bytes": 1000
  }
}

Expected / 预期: {"decision": "deny", "reason": "capability"}

Responsibility and limits

Junior delivers a strict manifest decision and boundary tests. Intermediate connects a proposed admission adapter to the chart stage with observable rejection and per-task cleanup ownership. Senior compares process/container/VM boundaries and specifies host control-plane isolation, resource reservations and artifact failure windows. Only the pure decision is implemented here; actual OS isolation remains unverified. The four tracks are learning activities, not job ranks. Fixed authored examples and known repairs cannot prove independent diagnosis, real-world security or hiring readiness. Keep source, initial failure, personal diff, same-input rerun and a declared changed-condition result. A reviewer-new complete contract and observed personal work are separate; disclose assistance. The five-second worker guard is not a performance SLO or hostile-code sandbox.

code example

Mechanism before code — at-all-limits

Additional review: explain why this case follows the contract and which single-field change would alter the branch. Public case: at-all-limits Expected / 预期: {"decision": "allow", "reason": "bounded_manifest"} https://ai.lwkjob.com/lab/extended-practice/cases/coding-sandbox-boundary.json

Acceptance task for this level

manifest capability and three independent resource bounds — Submit three branch traces and two personal payloads; distinguish trusted fixture fields from client/model data.

Acceptance self-checks

Reading this browser’s record…

Hide the answer and check your understanding

Why is cpu_ms=True invalid even under100?

Further explanations and practice

When encountering unfamiliar principles, first read the implementation, continuous questioning and migration cases, and then independently explain the premise and boundaries. Answers and notes are saved to the original account record.

All linked explanations and exercises (1 )

Sources and verification scope

The principles are based on public information; the numbers, cases and tasks are the teaching design of this website. Offline experiments verify the range noted on this page, and the learning effect still needs to be judged through independent tasks and feedback.