03 / 零件(下)

護欄、驗證迴路與 90 行骨架

沒有護欄,就不要讓它自動跑;沒有測試,你得到的不是生產力,而是高產能的破壞者。

六個零件裡的零件 5 與零件 6,最後收束成一支約 90 行、真的能跑起來的 Python 骨架。

發光的安全閘門立在光路上,上方漂浮綠色驗證勾號與終端機視窗
護欄決定 agent「允許做什麼」,驗證器決定「怎麼判斷它做對了」。前者要在後者之後、自動化之前完成。
SECTION 01

護欄:沒有這一層,就不要讓它自動跑

這是絕大多數人跳過、然後付出代價的一層。

生產級 harness 也是這樣設計的:Cline CLI 預設就是逐步審批——每一處檔案修改與指令執行都要你點頭,diff 攤在眼前;auto-approve 可以開,但那是你主動交出的權限,不是預設值。這種「預設安全、主動放權」的順序,值得直接抄進自己的骨架。

順序很重要

護欄要在「讓它自動跑」之前做完,不是在出事之後補。這也是 Ralph 迴圈被誤解的地方——「無人值守自動完成軟體」是過度簡化的說法;實務上它仍需要人類做規格設計、code review、架構判斷與安全把關。

SECTION 02

驗證迴路:測試就是 agent 的煞車系統

每次改動後自動串成 pipeline:

格式化 → lint → type check → 單元測試 → (可選)整合測試

失敗時,把 stderr + git diff 當作下一輪的 context 餵回去。如此一來,agent 不需要「聰明」,它只需要會看紅字。

沒有測試的 agent,得到的不是生產力,而是高產能的破壞者——它會在幾分鐘內產生你花一整天才能 review 完的程式碼,而其中沒有任何一處被驗證過。

這條迴路在第 6 章會被量測

「失敗 → 讀 stderr → 修正 → 再跑」這個循環,在實測裡就是「修正次數」那一欄:27B 一次到位,8B 多繞了一圈。而 AGENTS.md 寫死的專案規則,在 11 次執行中 100% 被遵守——護欄不一定要寫在程式碼裡,寫在專案規則檔也有效。

SECTION 03

最小可跑的 Python 骨架

以下假設使用 OpenAI 相容 API(Ollama、vLLM 或雲端皆可),約 90 行就能跑起來。它已經包含前面講的六個零件中的五個:模型、工具、迴圈、context 截斷、護欄。

mini_agent.py
# mini_agent.py  —  最小 coding agent
import os, pathlib, subprocess, json
from openai import OpenAI

ROOT = pathlib.Path.cwd().resolve()
MODEL = os.getenv("MODEL", "qwen2.5-coder:7b")
MAX_STEPS, MAX_OUT = 25, 6000
BLOCK = ("rm -rf", "git push", "shutdown", "mkfs", "curl | sh")

client = OpenAI(base_url=os.getenv("BASE_URL", "http://localhost:11434/v1"),
                api_key=os.getenv("API_KEY", "not-needed"))

# ---------- 工具 ----------
def _safe(p: str) -> pathlib.Path:
    fp = (ROOT / p).resolve()
    if fp != ROOT and ROOT not in fp.parents:
        raise ValueError(f"拒絕存取 workspace 外的路徑: {p}")
    return fp

def read_file(path: str, start: int = 1, end: int = 200) -> str:
    lines = _safe(path).read_text(encoding="utf-8", errors="replace").splitlines()
    return "\n".join(f"{i}\t{l}" for i, l in enumerate(lines[start-1:end], start))

def grep(pattern: str, glob: str = "**/*") -> str:
    hits = []
    for p in ROOT.glob(glob):
        if not p.is_file() or ".git" in p.parts:
            continue
        for i, l in enumerate(p.read_text(encoding="utf-8", errors="replace").splitlines(), 1):
            if pattern in l:
                hits.append(f"{p.relative_to(ROOT)}:{i}: {l.strip()}")
    return "\n".join(hits[:200]) or "(無符合)"

def edit_file(path: str, old: str, new: str) -> str:
    fp = _safe(path); text = fp.read_text(encoding="utf-8")
    if text.count(old) != 1:
        return f"ERROR: old 必須唯一匹配,實際出現 {text.count(old)} 次,請提供更長的片段"
    fp.write_text(text.replace(old, new), encoding="utf-8")
    return f"OK: 已修改 {path}"

def run_shell(cmd: str, timeout: int = 60) -> str:
    if any(b in cmd for b in BLOCK):
        return "ERROR: 指令被安全規則攔截,需人工執行"
    try:
        r = subprocess.run(cmd, shell=True, cwd=ROOT, capture_output=True,
                           text=True, timeout=timeout)
    except subprocess.TimeoutExpired:
        return f"ERROR: 逾時 {timeout}s"
    return f"exit={r.returncode}\n{r.stdout[-MAX_OUT:]}\n{r.stderr[-MAX_OUT:]}"

REGISTRY = {"read_file": read_file, "grep": grep,
            "edit_file": edit_file, "run_shell": run_shell}

SCHEMA = [
    {"type": "function", "function": {"name": "read_file", "parameters": {
        "type": "object", "properties": {"path": {"type": "string"},
        "start": {"type": "integer"}, "end": {"type": "integer"}},
        "required": ["path"]}}},
    {"type": "function", "function": {"name": "grep", "parameters": {
        "type": "object", "properties": {"pattern": {"type": "string"}},
        "required": ["pattern"]}}},
    {"type": "function", "function": {"name": "edit_file", "parameters": {
        "type": "object", "properties": {"path": {"type": "string"},
        "old": {"type": "string"}, "new": {"type": "string"}},
        "required": ["path", "old", "new"]}}},
    {"type": "function", "function": {"name": "run_shell", "parameters": {
        "type": "object", "properties": {"cmd": {"type": "string"}},
        "required": ["cmd"]}}},
]

# ---------- 迴圈 ----------
def agent(goal: str):
    msgs = [
        {"role": "system", "content": (
            "你是 coding agent。規則:\n"
            "1. 先用 grep 定位,再 read_file 讀區間,不要整檔讀。\n"
            "2. 修改用 edit_file 做最小 patch。\n"
            "3. 改完必須執行測試驗證,看到綠燈才算完成。\n"
            "4. 不要捏造結果,只回報工具輸出的事實。")},
        {"role": "user", "content": goal},
    ]
    for step in range(1, MAX_STEPS + 1):
        r = client.chat.completions.create(model=MODEL, messages=msgs, tools=SCHEMA)
        m = r.choices[0].message
        msgs.append(m.model_dump(exclude_none=True))
        if not m.tool_calls:
            return f"[step {step}] 完成:{m.content}"
        for tc in m.tool_calls:
            fn = REGISTRY[tc.function.name]
            args = json.loads(tc.function.arguments or "{}")
            try:
                out = fn(**args)
            except Exception as e:
                out = f"ERROR: {type(e).__name__}: {e}"
            print(f"[step {step}] {tc.function.name}({args}) -> {str(out)[:200]}")
            msgs.append({"role": "tool", "tool_call_id": tc.id, "content": str(out)})
    return "已達步數上限,交回人工審查。"

if __name__ == "__main__":
    import sys
    print(agent(sys.argv[1]))

執行方式(以 Ollama 為例,需支援 tool calling 的模型):

ollama pull qwen2.5-coder:7b
$env:MODEL="qwen2.5-coder:7b"; $env:BASE_URL="http://localhost:11434/v1"
python mini_agent.py "幫我找出 utils.py 裡的 bug 並修好,修完跑 pytest 驗證"
假設與限制

這份骨架假設端點支援 OpenAI tool-calling 格式。若你的本地模型不支援 tools,就把工具描述寫進 system prompt,改成「要求模型輸出 JSON → 自行解析」的土砲做法,其餘架構完全不變。這也正好說明了一件事:模型是可替換的零件,harness 才是你的作品。

記住這四個工具

這份骨架的四個工具是 read_file、grep、edit_file、run_shell。下一章你會看到 Cline CLI 把工具層擴張成一個生態系;再下一章你會看到 Pi 的預設工具——read、bash、edit、write——幾乎就是這四個。