AI Coding Agent Security Architecture: Containment First vs. Analyze First
Once a large language model is wired into a real development workflow, the engineering focus shifts quickly from model capability to execution boundaries. This article compares the two approaches the industry has converged on for answering a single upstream question: when the model emits a command, what decides whether it is allowed to run? Containment first enforces deterministic limits in the OS kernel or runtime; analyze first inserts probabilistic review layers before execution. The article walks through each mechanism, a three-tier permission model, the strengths and costs of each route, the three attack surfaces of a review layer, how the security choice dictates whether the tool system ends up small and general or large and specialized, a selection checklist, and a defense-in-depth combination of semantic pre-review, kernel containment, and audit with replay.
Introduction: One Foundational Choice, Not Seven or Eight Isolated Decisions
Once you connect a large language model to a real development workflow, the engineering focus moves fast — from “model capability” to “execution boundaries.” The reason is simple: an agent is not a code completion tool. It decides its own next action and acts directly on the file system, processes, and the network. At that point, the thing that needs justification is not a feature list but a prior question: when the model emits a command, what decides whether it may be executed?
The industry’s answers have converged into two routes:
| Route | Interception point | Trust assumption | Nature of the decision |
|---|---|---|---|
| Containment first | OS kernel / runtime | Do not trust model output; trust only environment constraints | Deterministic |
| Analyze first | Semantic review model + rule engine | Do not fully trust the model; review with another layer of intelligence | Probabilistic |
This choice matters because it is not a parallel technical option — it is an upstream decision. Once you fix whether interception happens in the kernel or in a review layer, the number of tools, the context budget, cross-platform cost, and failure observability all follow from it.
1. Containment First: Subtract Capability
Mechanism
The core idea of containment first is “narrow the capability first, then talk about intelligence.” The agent runs in a restricted execution environment whose externally exposed resources are explicitly trimmed:
- The file system mounts only the current workspace; all other paths are invisible or read-only;
- Networking is off by default and explicitly allowed per domain/port when needed;
- Processes, memory, CPU, and runtime duration are capped by quota;
- All writes land in a disposable temporary layer and are reclaimed on exit.
In implementation terms, this usually relies on kernel-provided access control and isolation primitives, a lightweight container runtime, or a platform’s least-privilege token mechanism — with separate adaptation required per operating system.
A Three-Tier Permission Model
In practice, a binary switch is rare; three escalating modes are offered instead:
# Example: execution environment policymode: workspace-write # read-only | workspace-write | unrestrictedworkspace: /srv/projectnetwork: falseallow_write:- /srv/project/**deny_read:- /etc/**- ~/.ssh/**resource:cpu: 2memory: 4Gitimeout: 120s
read-only suits code review and Q&A; workspace-write is the daily workhorse; unrestricted should be used only in a throwaway container or a trusted directory.
Strengths and Costs
The strength is immunity to whole classes of attack. No matter how the upstream text tries to steer the model, out-of-bounds actions are rejected directly by the kernel, with no dependence on the accuracy of any model judgment.
The costs are equally clear:
- Silent failure: when the environment rejects an operation without clear feedback, the model misreads “forbidden” as “tool defect” and retries repeatedly, wasting context;
- Cross-platform cost: every operating system needs its own implementation and validation;
- Dependency on the host environment: policy correctness is affected by the user’s machine configuration;
- Weak observability: commands are a black box and hard to audit in a structured way.
The key to mitigating silent failure is to report environment rejection and command failure back to the model as two distinct signals:
{ "status": "denied_by_policy","reason": "path outside workspace","path": "/etc/hosts","hint": "Use a relative path inside the workspace instead" }
2. Analyze First: Add Judgment
Mechanism
Analyze first does not build a wall at the environment layer; it inserts multiple review stages before execution:
- Path rules: read/write allowlists and denylists;
- Command parsing: split out the executable and its arguments, match against high-risk patterns;
- Semantic scoring: a classification model judges intent risk and returns a score;
- Interactive approval: above the threshold, show the user an explanation and wait for confirmation.
Strengths
One body of logic works across platforms; rejections are explainable, so the user experience is “a dialog explaining why” rather than a silent failure; permission granularity can go down to a single tool or a single parameter; and structured results are naturally suited to logging and auditing.
Three Attack Surfaces
The risk in a review layer concentrates in three places:
- Prompt injection: external content (web pages, dependency source, issue text) carries instructions that steer the model into generating a malicious command that “looks ordinary”;
- Command obfuscation: encoding variants, environment-variable concatenation, and indirect interpreter invocation defeat string matching;
- Out-of-distribution attacks: new patterns the review model never saw in training, whose scores land in the safe zone.
The root problem: when the review layer and the execution layer both derive from model capability, they share common failure modes — the same class of semantic blind spot fools generation and review at once.
3. The Knock-On Effect: Two Shapes of Tool System
Once the security route is fixed, tool design is almost automatically derived.
Containment first → few and general: roughly a dozen tools. Search is left to the model assembling commands in a shell; editing uses one patch tool that supports multiple files and transactional application; network access goes through a command-line client; parallel work is dispatched via a batch task list. The design philosophy is “hand the model a Swiss army knife.”
Analyze first → many and specialized: dozens of tools, each with explicit inputs and outputs — a standalone structured search tool (with bundled binary dependencies), a precise editing tool (one location at a time), a separate command execution tool (going through the permission check), dedicated web-fetch and search tools, a sub-agent tool, and a code-understanding tool built on language service interfaces. The design philosophy is “hand the model a professional toolbox.”
Cost comparison:
| Dimension | Few and general | Many and specialized |
|---|---|---|
| Flexibility | High | Low |
| Reliability | Depends on the user’s system environment | Bundled dependencies, stable |
| Token cost | Low | High (tool schemas consume context) |
| Observability | Low | High (structured results) |
| Permission granularity | Coarse (shell black box) | Fine (authorized per tool) |
4. Selection Checklist
- Is the execution environment trusted, and is it shared by multiple tenants? For shared scenarios, prefer containment.
- Must the experience be identical across platforms? Prefer analysis.
- Are task chains long, and is after-the-fact auditing required? Prefer specialized tools plus structured logs.
- Is the context budget tight? General tools are cheaper.
- Does the team have the capacity to maintain kernel policies and images? If not, start with the analysis route.
5. Recommended Combination: Defense in Depth
The two routes are not mutually exclusive; mature solutions typically stack three layers:
- Upstream semantic review: reduces risk and provides explanations, covering the part that needs to “understand”;
- Kernel containment as the backstop: deterministic interception, covering the part that needs to “hold”;
- Audit and replay: record every tool call, policy decision, and result, supporting after-the-fact tracing.
In a cloud deployment, a container platform can host the isolated execution environment, object storage can hold patches and artifacts, and a logging service with monitoring and alerting can aggregate policy-denial events — with an alert on “sudden spikes in denial rate,” which is often an early signal of an injection attack or a policy misconfiguration.
Conclusion
The disagreement between containment first and analyze first is essentially about putting determinism in the environment or putting flexibility in the review layer. The former trades implementation cost for strong guarantees; the latter trades probabilistic risk for cross-platform reach and explainability. Understanding this chain of consequences is worth more than memorizing any feature comparison table: fix the interception point first, and the rest of the design converges on its own.
