如何為編碼智能體撰寫政策:可改編的淺白英文模板
Writing a policy for coding agents: a plain-English template you can adapt
Sandscale 提供一份以淺白英文撰寫的編碼智能體政策模板,把操作分為可直接執行、需人工批准及禁止三類。需人工批准的事項包括新增依賴、修改 CI、連接批准清單以外的網絡主機及執行資料庫遷移;禁止事項包括執行網絡腳本、讀取或輸出密鑰檔案,以及停用測試。文中建議以 hooks、網絡出口允許清單、憑證限制和分支保護在模型之外落實規則,並記錄每次執行及審批。
Most teams let coding agents in before anyone writes down what they may do. Then something surprising happens (a force-push, a curl | sh, a migration run against the wrong database) and the policy gets written in a hurry. It is easier to write it first, in plain English, and then make tools enforce it.
Below is a template. It is deliberately short. Copy it, cross out what does not fit, and argue about the rest with your team; the argument is most of the value.
The shape: three lists
Every rule belongs in one of three lists:
- Allowed. The agent may do it without asking. These are things that are cheap to undo or only touch the agent's own sandbox.
- Needs a person. The agent may propose it; a named human approves or denies before it happens. These are things that leave the sandbox or are hard to undo.
- Never. Refused even with approval. These are things where the cost of one mistake is larger than any convenience.
The test for each item: if the agent did this a hundred times by mistake, what would it cost? Cheap and local goes in list 1. Expensive or external goes in list 2. Unrecoverable goes in list 3.
The template
Scope
This policy applies to AI coding agents (Claude Code, Codex, Gemini CLI and similar) used on company repositories, whether run by a person or by automation. The person who starts an agent is responsible for what it does.
1. Allowed without asking
- Read any file in the repository it was given.
- Edit files in its working copy.
- Run the project's own build, lint and test commands.
- Install dependencies from the project's lockfile, from the approved registries.
- Run code it wrote, inside its sandbox.
- Create a branch and commit to it.
- Open a draft pull request.
2. Needs a person first
- Push to any branch other than its own.
- Mark a pull request ready for review, or merge one.
- Add a new dependency, or change a lockfile beyond the task's scope.
- Change CI configuration, build scripts, Git hooks or deployment files.
- Reach any network host outside the approved list.
- Use any credential beyond the read-only ones it was started with.
- Run database migrations, even against staging.
- Create, change or delete cloud resources.
- Send anything outside the company: email, chat, issue comments on public repositories.
3. Never
- Run scripts piped from the network (
curl ... | sh,wget -O- ... | bash). - Read or print secrets files (
.env, key files, credential stores) into its context. - Disable or edit tests to make a failing build pass.
- Force-push to a shared branch, or rewrite published history.
- Touch production data or production credentials.
- Approve its own request, or ask another agent to approve it.
- Change this policy or the tooling that enforces it.
Records
Every run records who started it, which agent and model, the repository and branch, each command and its result, network destinations reached and refused, and every approval with who gave it. Records are kept and reviewed after incidents.
Exceptions
Exceptions are granted per project, in writing, by the project owner, with an end date.
Why some lines are where they are
"Add a dependency" needs a person. Agents install packages that sound right. Typosquatting and dependency confusion are real; see supply-chain risk when agents install packages. A person reading the name and the package page is a cheap check.
"Change CI configuration" needs a person. CI files and Git hooks run later with more access than the agent has now. An agent that edits them can escalate its own privileges by proxy. Our agent diff review checklist lists the files to watch.
"Disable tests" is never. It is the most common way an agent "succeeds" at a task it failed, and it is invisible unless someone looks.
"Approve its own request" is never. An approval system where the requester can approve is a log, not a control. See human approvals for agent tool calls.
Making tools enforce it
A written policy that nothing enforces relies on the agent reading it, and agents can be talked out of instructions by prompt injection. Enforce the lists outside the model:
- Hooks. Claude Code supports hooks that run before a tool call and can allow it, block it, or ask a person. List 3 maps to block rules; list 2 maps to ask rules. See Anthropic's hooks documentation.
- Sandbox network policy. "Reach hosts outside the approved list" is enforced by an egress allowlist, not by asking the agent.
- Credentials. "Use credentials beyond read-only" is enforced by not giving the agent any. Scope the tokens it starts with; see Git credentials for coding agents.
- Branch protection. "Push to shared branches" and "merge" are enforced by the Git host.
How this maps to Sandscale
On Sandscale, Claude Code runs under your policy through its hooks, so each tool call is allowed, blocked or sent to a person; Codex and Gemini CLI report every step over OpenTelemetry. Human approvals are on every plan: a named approver decides, self-approval is refused, and the request and decision are recorded. Sandboxes start with the network off until your allowlist opens it, and every action lands in a hash-chained audit log. Policies for projects come with Team and Enterprise. See agent policy and approvals.
Whatever you enforce it with, keep the written version. When the next surprising thing happens, the first question will be which list it should have been on.
Originally published on the Sandscale blog.
來源:DEV Community · dev.to