為會改動內容的工具設下關卡

閱讀約需 59 分鐘 分鐘,12 節 中共有 12 個範例。

版次 2026.11.2 範例於 2026年11 月 20 通過測試 64 個範例中有 64 個通過 變更內容

由 AI 翻譯。經人工以英文驗證。 我們如何製作書籍

譯自英文書,版次 2026.11.2。 閱讀原文

讀完本章後,你可以讓代理程式的修改一直留在它自己的副本中,直到有人點頭同意;也可以檢查這個同意是否涵蓋那個人所讀過的確切修改,並把拒絕的決定交還給代理程式,作為它的執行被中止的原因。

本書中的代理程式負責維護 unitcraft。這是一個函式庫,其中每一項修改都由維護者全權決定是否納入,而代理程式在該函式庫的暫存副本上工作。測試與編輯都在副本中進行。但仍須有機制決定,修改何時才能進入維護者所擁有的函式庫;在這個情境中,維護者就是提出該議題的人。任務 T-04 清楚說明了風險所在:它的議題要求再增加一個單位,也就是英寸,而代理程式的副本中已有一項新增該單位的編輯。這項編輯是否納入,應由人來決定,代理程式不該替人做這個決定。

本章要建構的是 harness 中負責詢問的部分。代理程式的檔案工具只會寫入該次執行的副本,只有在有人同意之後,harness 套用該次執行的 diff,函式庫才會改變。在此之上還有兩道檢查:一道是在 finish 時讀取 diff 的關卡,另一道是在每次工具呼叫前後比對函式庫位元組的防護。這道關卡與這項防護都是教學用的程式碼,屬於第二層防線,兩者都不是沙箱。對於代理程式所撰寫的程式碼,主要的防禦是容器或虛擬機器,這部分在第 7 章中說明。本章的範例都不會執行這類環境,也沒有任何紀錄展示過它。每一次工具呼叫都會被包在對案例函式庫位元組的比對之中,而這項防護採取失敗即封鎖的方式:一旦函式庫發生變化,該次執行就會以 library_changed 結束,harness 不套用任何內容,也不還原任何內容。第一節說明副本無法阻止的事情。

此部分尚未翻譯,因此以英文呈現。

The copy comes first

The agent's file tools reach files only through its copy. Each run gets its own copy, made by scratch_copy in run_task, and the tools work through a Workspace rooted at that copy. Workspace.locate resolves every path the model gives. It refuses a path that is absolute, one with a .. part, one with a NUL byte or a backslash, one that resolves through a symlink to a place outside the copy, and any write into .github/. Through these tools, a path that does not resolve inside the copy never reaches the disk.

Those checks cover the paths the tools take. They do not cover code the agent runs. run_tests runs pytest on the copy, and pytest runs whatever the model wrote there. A test module the model adds is code too. The test run imports it and runs it with the harness's own user rights and no sandbox, so it can write to any path that user can write, including the library. The runner does not load conftest.py files (run_pytest passes --noconftest), but it does collect test modules, and that is the route this section means. The gate cannot stop that write, because the gate reads only the diff at finish. The library guard, in its section, sees the write once run_tests returns, ends the run as library_changed, and restores nothing. It detects the write after it happens. It does not prevent it.

This is the open side of the harness, and it is the main limit of this chapter. Chapter 7 describes the book's teaching sandbox, which runs the agent's commands with caps and a clean environment. Its table lists what it leaves open, and writes outside the copy are on that list. A container or a virtual machine is the primary defence for running code a model wrote, and chapter 7 explains both. The checks in this chapter are teaching code, written so a reader can open them, copy them and test them. They are not a substitute for that isolation.

The library changes in one place. apply in lib/permissions.py writes new bytes into the library, and its only caller is the gate's answer to a proposal, after a yes. library_copy makes the throwaway library that the examples apply to, so the book's case folder is never written. When a run ends, Gate.settle reports what the copy still holds, and nothing in the copy is written back to the library.

A write can also fail partway through an apply. The next example gives a proposal of two files, and puts a folder where the second file goes, so the second write fails.

apply-all-or-nothing.py Python 3.12.15 · pydantic 2.14.3 通過
"""A proposal of two files where the second cannot be written, because a folder stands where it goes. The first fileis put back, the error goes back as the handoff, and the library holds the bytes it held before the finish."""from lib.case import CASEfrom lib.permissions import Gate, answer, gated, library_copyfrom lib.tasks import snapshotfrom lib.tools import V1, Workspacewith library_copy() as library, library_copy() as scratch:    (library / "unitcraft" / "extra.py").mkdir()    before = snapshot(library, ".")    (scratch / "README.md").write_text("A new line in the readme.\n", encoding="utf-8")    (scratch / "unitcraft" / "extra.py").write_text('"""Extra."""\nTOTAL = 1\n', encoding="utf-8")    gate = Gate(library, lambda p: answer(p, True))    result = gated(V1, gate).call(Workspace(scratch), "finish", {"summary": "Two files."})    print("ended:", result.end)    print("handoff:", result.question)    print("README as before:", snapshot(library, "README.md") == snapshot(CASE, "README.md"))    print("library bytes unchanged:", snapshot(library, ".") == before)    print("applied:", gate.counts["applied"], "refused:", gate.counts["refused"])    assert result.end == "ask_user" and "apply failed (IsADirectoryError)" in result.question    assert snapshot(library, ".") == before and gate.counts["applied"] == 0
輸出python apply-all-or-nothing.py
ended: ask_user
handoff: p-1 was not applied: apply failed (IsADirectoryError); library restored
README as before: True
library bytes unchanged: True
applied: 0 refused: 1

apply puts back each file it had touched and each folder it had made, and the gate holds the proposal with the error as its reason: p-1 was not applied: apply failed (IsADirectoryError); library restored. The README, which was written first, is as it was before the finish, and the library's bytes are unchanged. The gate catches an OSError as a held proposal. Any other exception also puts the files back and is then raised, and no example here shows that path.

Working rule: Give each run its own copy, refuse every path that resolves outside it, and let exactly one function write to the original, called only after a yes. Then name what the run's code can still do to the original, check that with a guard, and do not count the gate as the protection.

這個測試框架屬於哪一種模式

閘門首先需要回答一個問題:當代理停下來詢問時,由誰來回答?Claude Code 的互動模式會在工作階段中詢問使用者。Claude Agent SDK 提供數種權限模式,彼此的差異就在這一點上;測試框架必須說明自己與哪一種相近,因為這個答案決定了:在無人能夠回應時,需要核准的呼叫會有什麼結果。這個測試框架沒有終端機,也沒有工作階段。它是無頭(headless)執行,唯一能回答的對象,是呼叫端傳入的回呼函式。

廠商文件把這幾種情況區分得很清楚。Claude Code 的權限頁面指出,規則依照拒絕、詢問、允許的順序檢查,由第一個符合的規則決定結果。文件也指出,Claude Code 預設會在修改檔案之前詢問,而在工作目錄內的唯讀作業則不經提示直接執行。Agent SDK 的權限頁面描述了 dontAsk 模式:在此模式下,任何原本會提示的呼叫都會被直接拒絕,並且不會呼叫核准回呼。本章並未實際執行這兩項產品,因此關於它們的說法出自廠商,並已標明。

這個測試框架保留了它的回呼函式。需要得到許可的變更會呼叫核准器,並等待回答。廠商的 dontAsk 模式在一點上有所不同,而這一點在此很重要:它不呼叫回呼就拒絕這類呼叫,而這個測試框架一律會呼叫回呼。本書稱之為帶有回呼的無頭執行,而不是 dontAsk。無論執行時使用哪一種模型轉接器,閘門都相同,因為閘門包裝的是工具,而不是模型。第 4 章的模型協定才是隨模型不同而改變的部分。

**實用原則:**在加入閘門之前,先說明模式。當呼叫端有能夠回答的回呼函式時,讓閘門呼叫它並等待回答。沒有核准器的閘門會拒絕啟動:本章的程式碼會引發錯誤。若設計要在無人能回答時改為交接,就需要自己的路徑,而本章沒有任何範例展示這種做法。

關卡設在 finish

代理程式所做的大部分事情,都不會更動維護者所擁有的任何內容。它列出檔案、讀取檔案,並執行可見測試。它對副本所做的編輯是自己的工作,本來就是要讓它去做的。唯一要求更動函式庫的呼叫是 finish,所以關卡的檢查就設在那裡,而 finish 正是提出提案的地方。

關卡包住工具集中的每一項工具,每項工具的類別都來自 lib/permissions.py 中的同一張表。讀取類(list_files、read_file、run_tests 與 ask_user)直接通過,函式庫防護機制會監看其中每一項。run_tests 屬於讀取類,但它會執行模型所撰寫的程式碼,如副本一節所述。編輯與寫入(edit_file 與 write_file)在副本中執行,關卡會計算每一次呼叫。finish 是一項提案。它的類別來自測試框架的表格,而不是模型所撰寫的任何內容,因此無論是模型的摘要,還是工具自身的描述,都無法決定它的類別。送給模型的名稱、描述與結構描述,不論有沒有關卡都相同,所以透過受關卡管控的工具所發出的請求,與沒有關卡時發出的請求是同一個請求。

提案是副本相對於函式庫的完整差異,而不是模型所撰寫的摘要。pending 列出每個位元組內容不同的檔案,diff_text 則為每個檔案寫出一個標頭,後面接著該檔案的統一格式差異(unified diff)。提案的身分是對變更位元組所算出的雜湊值,說明見綁定一節。核准者會收到提案 ID、摘要、變更檔案清單、照原樣顯示的差異文字,以及雜湊值。它回覆同意或拒絕、一項理由,以及它所回應的 ID 與雜湊值。摘要是作為模型對該變更的主張而顯示,關卡不把它當作證據。如果副本沒有任何變更,finish 會以自己的結果通過,因此沒有改動任何內容的執行不會被詢問任何事。否則,核准者會在工具呼叫之內被呼叫,執行過程會等待答覆。當關卡處於強制執行狀態時,finish 工具自身的主體只會在同意之後才執行。

OWASP 關於過度代理權限(excessive agency)的指引要求在高影響力的動作執行之前,由人員核准,並要求代理程式只擁有它所需要的工具。那是指引,而不是量測結果,本章把它視為這項設計的理由,而不是設計確實有效的證明。

任務集是針對該案例的 20 項任務,大多數由隱藏檢查評分(任務集涵蓋全部任務)。其執行是以工具集 v1 且沒有關卡所錄製,使用的是 Qwen3-4B-Instruct-2507,也就是第 1 章所重播的模型。關卡不會改變任何一個請求,所以透過它重播時,送出的請求相同,得到的錄製回覆也相同。先從應該可行的路徑開始:重播所錄製的 T-04 執行,並由一位回答同意的人員處理。

gate-applies-only-on-yes.py Python 3.12.15 · pydantic 2.14.3 通過
"""The recorded T-04 run (add the inch) with the gate and a scripted yes: its one changed file reaches the library, andthe hidden check fails before the apply and passes after it. The second part shows two yeses that cannot apply: a copythat changed after the person looked, and an answer that names another proposal."""from lib.case import load_tasksfrom lib.permissions import Answer, Gate, gated, library_copy, replay_task, scriptedfrom lib.tasks import hidden_check_passesfrom lib.tools import V1, Workspacetask = load_tasks(only=["T-04"])[0]with library_copy() as library:    assert not hidden_check_passes(task, library)    gate = Gate(library, scripted((True, "")))    run = replay_task("T-04", gate, library)    print("ended:", run.ended)    print("proposals:", gate.counts["proposals"], "asked:", gate.counts["asked"])    print("applied:", gate.counts["applied"], "files applied:", gate.counts["files applied"])    print("visible tests after the apply:", Workspace(library).run_tests().splitlines()[0])    print("hidden check after the apply:", hidden_check_passes(task, library))    assert run.ended == "finish" and gate.counts["applied"] == 1 and hidden_check_passes(task, library)    run.close()with library_copy() as library, library_copy() as scratch:    ws = Workspace(scratch)    (scratch / "unitcraft" / "extra.py").write_text('"""Extra."""\nTOTAL = 1\n', encoding="utf-8")    def drift(proposal):  # the copy changes in the window between the person's yes and the apply        (scratch / "unitcraft" / "extra.py").write_text('"""Extra."""\nTOTAL = 2\n', encoding="utf-8")        return Answer(True, proposal.id, proposal.sha256)    late = gated(V1, Gate(library, drift)).call(ws, "finish", {"summary": "Add a constant."})    print("drifted copy, handoff:", late.question)    wrong = gated(V1, Gate(library, lambda p: Answer(True, "p-7", p.sha256)))    print("answer for another proposal, handoff:", wrong.call(ws, "finish", {"summary": "Add a constant."}).question)    assert late.end == "ask_user" and "changed after" in late.question    assert not (library / "unitcraft" / "extra.py").exists()
輸出python gate-applies-only-on-yes.py
ended: finish
proposals: 1 asked: 1
applied: 1 files applied: 1
visible tests after the apply: 8 failed, 22 passed
hidden check after the apply: True
drifted copy, handoff: p-1 was not applied: the copy changed after the answer
answer for another proposal, handoff: p-1 was not applied: the answer is to another proposal or diff

同意會套用變更,而隱藏檢查只在套用之後才通過。該次執行以 finish 結束,關卡會套用它所詢問的那項提案。變更套用後,可見測試套件回報 8 failed, 22 passed。該數字不是任務的評分依據。評分的是隱藏檢查,而範例顯示它在套用前失敗,套用後通過。

範例的後半段檢查兩種無法套用的同意。第一種,副本在核准者執行期間、也就是人員看過之後發生變動。第二種,答覆所指的是另一項提案。兩者各以自己的理由被拒絕:第一種是 p-1 was not applied: the copy changed after the answer,第二種是 p-1 was not applied: the answer is to another proposal or diff。兩種情況下函式庫都維持不變,範例也對此做了斷言。

finish 工具的主體也屬於同一條規則。

finish-body-waits-for-yes.py Python 3.12.15 · pydantic 2.14.3 通過
"""A finish that publishes does its work only after a yes. With a no the body never runs; with a yes it runs once,after the change is applied to the library."""from dataclasses import replacefrom lib.permissions import Gate, answer, gated, library_copyfrom lib.tools import V1, ToolResult, Workspacepublished = []def finish_and_publish(ws, args):    published.append(args.summary)    return ToolResult("run ended", brief="finish", end="finish", summary=args.summary)publishing = replace(V1, tools=tuple(replace(t, run=finish_and_publish) if t.name == "finish" else t                                     for t in V1.tools))def finish_with(yes: bool):    with library_copy() as library, library_copy() as scratch:        gate = Gate(library, lambda p: answer(p, yes, "decided"))        (scratch / "unitcraft" / "n.py").write_text("N = 1\n", encoding="utf-8")        result = gated(publishing, gate).call(Workspace(scratch), "finish", {"summary": "Publish."})        return result, gate, (library / "unitcraft" / "n.py").exists()no_result, _, no_added = finish_with(False)print("no: ended", no_result.end, "| publish ran:", len(published), "| file added:", no_added)yes_result, gate, yes_added = finish_with(True)print("yes: ended", yes_result.end, "| publish ran:", len(published), "| file added:", yes_added)print("applied:", gate.counts["applied"])assert no_result.end == "ask_user" and len(published) == 1 and not no_addedassert yes_result.end == "finish" and yes_added and gate.counts["applied"] == 1
輸出python finish-body-waits-for-yes.py
no: ended ask_user | publish ran: 0 | file added: False
yes: ended finish | publish ran: 1 | file added: True
applied: 1

程式碼給這次執行一個會發佈內容的 finish,並將同一項提案重播兩次。在拒絕時,發佈主體不會執行,檔案也不會被加入:ended ask_user | publish ran: 0 | file added: False。在同意時,檔案會被加入,主體只執行一次:ended finish | publish ran: 1 | file added: True。

實用原則: 從唯一能更動函式庫的那個呼叫提出副本的完整差異,並在套用之前立即再對該差異計算一次雜湊。無論答覆如何,在人員看過之後有所變動的副本都應被拒絕,因為套用所寫入的是已經過雜湊的位元組。工具自身的主體只在同意之後才執行。

一次核准只回應一項請求

只有當「是」針對的是當事人讀過的那項變更時,這個「是」才有價值。一旦同一個關卡要處理多項提案,這個問題就會浮現,例如多次執行共用同一個關卡的時候。如果沒有任何機制把兩者連結起來,針對某一項變更給出的答覆,就可能被沿用到下一項變更。

關卡用識別資訊把它們連結起來。每項提案會從該關卡的計數器取得下一個 ID,先是 p-1,接著是 p-2,依此類推,並且附上變更位元組的 SHA-256 雜湊值。這個雜湊值涵蓋每個變更檔案的確切位元組,並依路徑順序排列,而不只是當事人看到的文字。答覆必須同時指明這兩者。關卡在套用任何內容之前會先比對兩者,接著再對副本重新計算雜湊,正如關卡一節所述。Microsoft 的 Agent Framework 工具核准頁面描述了相同的模式。該頁面指出,本地的核准回應只有在符合同一工作階段中記錄的待處理核准請求時,才會授權工具呼叫,而框架會忽略未綁定的回應並記錄一則警告。它在關閉此機制的說明中提到,關閉綁定可能讓偽造或重放的回應核准具特權的呼叫。

當事人讀到的文字是逐字寫出(轉義)的,不是原始內容。diff_text 會把終端機可能據以動作的每個字元寫成可見的跳脫序列。因此,歸位字元或跳脫序列會在 diff 中以文字呈現,而不是 UTF-8 文字的檔案,則只會以其位元組的雜湊值顯示。

hidden-diff-lines-shown.py Python 3.12.15 · pydantic 2.14.3 通過
"""Two files whose text would hide a line in a terminal: a carriage return that overwrites the line before it, and anescape that erases a line. The proposal shows both written out, so every line the person reads is in the file."""from lib.permissions import Gate, answer, gated, library_copyfrom lib.tools import V1, WorkspaceCR = "HIDDEN_CR = 'not shown'\r# a comment long enough to cover the line before it\n"ESC = "HIDDEN_ESC = 'not shown'\n\x1b[1A\x1b[2K# tidy\n"seen = []def person(proposal):    seen.append(proposal)    return answer(proposal, False, "not this change")with library_copy() as library, library_copy() as scratch:    gate = Gate(library, person)    tools, ws = gated(V1, gate), Workspace(scratch)    tools.call(ws, "write_file", {"path": "unitcraft/a_cr.py", "text": CR})    tools.call(ws, "write_file", {"path": "unitcraft/b_esc.py", "text": ESC})    tools.call(ws, "finish", {"summary": "Add two comment-only modules."})    shown = seen[0].diff    for line in (ln for ln in shown.splitlines() if "HIDDEN" in ln):        print("shown:", line)    print("headers that flag control characters:", shown.count("contains control characters"))    print("raw CR or ESC in the shown text:", sum(ch in shown for ch in "\r\x1b"))    print("applied:", gate.counts["applied"], "refused:", gate.counts["refused"])    assert "\r" not in shown and "\x1b" not in shown and shown.count("contains control characters") == 2    assert gate.counts["applied"] == 0
輸出python hidden-diff-lines-shown.py
shown: +HIDDEN_CR = 'not shown'\r# a comment long enough to cover the line before it
shown: +HIDDEN_ESC = 'not shown'
headers that flag control characters: 2
raw CR or ESC in the shown text: 0
applied: 0 refused: 1

這個指令稿寫出兩個檔案,其內容在終端機中會隱藏一行。其中一個含有會覆寫前一行的歸位字元,另一個含有會清除一行的跳脫字元。當事人看到這兩行,且都已轉義。指令稿印出 2 個標示控制字元的標頭,以及 0 個出現在所顯示文字中的原始歸位字元或跳脫字元。腳本中的當事人回答「否」,因此不會套用任何內容。

answer-binds-bytes.py Python 3.12.15 · pydantic 2.14.3 通過
"""A file that is not UTF-8 text is shown by the hash of its bytes, and the yes binds to those bytes. When the copy'sbytes change after the yes, the apply is refused, even though both versions decode to the same replacement characters."""from lib.permissions import Change, Gate, answer, digest_changes, gated, library_copyfrom lib.tools import V1, Workspacewith library_copy() as library, library_copy() as scratch:    (scratch / "unitcraft" / "blob.bin").write_bytes(b"DATA \xff\n")    def swap_then_yes(proposal):        print("shown as:", proposal.diff.split(";")[0])        (scratch / "unitcraft" / "blob.bin").write_bytes(b"DATA \xfe\n")        return answer(proposal, True)    gate = Gate(library, swap_then_yes)    result = gated(V1, gate).call(Workspace(scratch), "finish", {"summary": "Add a blob."})    print("ended:", result.end)    print("handoff:", result.question)    print("applied:", gate.counts["applied"], "| blob in library:", (library / "unitcraft" / "blob.bin").exists())    one = digest_changes([Change("x.bin", None, b"\xff")])    two = digest_changes([Change("x.bin", None, b"\xfe")])    print("hashes differ for \\xff and \\xfe:", one != two)    assert result.end == "ask_user" and "changed after" in result.question and gate.counts["applied"] == 0    assert one != two
輸出python answer-binds-bytes.py
shown as: == unitcraft/blob.bin: added, not UTF-8 text
ended: ask_user
handoff: p-1 was not applied: the copy changed after the answer
applied: 0 | blob in library: False
hashes differ for \xff and \xfe: True

這個指令稿展示了另一半。它新增一個位元組不是有效 UTF-8 的檔案,而核准者看到的是 == unitcraft/blob.bin: added, not UTF-8 text。在回答「是」之後,副本的位元組從一個位元組被換成另一個位元組,而兩者解碼後都是同一個替代字元。套用被拒絕:p-1 was not applied: the copy changed after the answer。指令稿也斷言這兩個位元組字串的雜湊值不同,所以這個雜湊值不是解碼後文字的雜湊值。

兩次執行可以在同一個範例中共用一個關卡。

approval-in-transcript.py Python 3.12.15 · pydantic 2.14.3 通過
"""Two proposals in one transcript, through one gate: the recorded T-04 run is answered yes and its inch reaches thelibrary; a stored T-09 run (fix the Celsius offset) is answered no and handed back. Each answer is logged with theproposal ID and the hash of the diff it answers."""from lib.case import CASEfrom lib.permissions import Gate, library_copy, replay_task, scripted, stored_taskfrom lib.tasks import snapshotwith library_copy() as library:    gate = Gate(library, scripted((True, ""), (False, "not in this change")))    first = replay_task("T-04", gate, library)    second = stored_task("T-09", gate, library)    print("T-04 ended:", first.ended, "| T-09 ended:", second.ended)    print("transcript, T-04:", first.trace.lines()[-1])    print("transcript, T-09:", second.trace.lines()[-1])    for line in gate.log:        print("gate:", line)    print("applied:", gate.counts["applied"], "refused:", gate.counts["refused"])    assert first.ended == "finish" and second.ended == "ask_user" and gate.counts["applied"] == 1    assert snapshot(library, "unitcraft/units.py") != snapshot(CASE, "unitcraft/units.py")    assert snapshot(library, "unitcraft/temperature.py") == snapshot(CASE, "unitcraft/temperature.py")    first.close()    second.close()
輸出python approval-in-transcript.py
T-04 ended: finish | T-09 ended: ask_user
transcript, T-04: step 5: finish: p-1 yes, applied 1 files
transcript, T-09: step 6: finish: p-2 was not applied: the person said no: not in this change
gate: p-1 1 files, diff 06ee73fd984f: asked
gate: p-1 answer yes, bound to p-1 and 06ee73fd984f
gate: p-1 applied: 1 files
gate: p-2 1 files, diff 95fb833675dd: asked
gate: p-2 answer no, bound to p-2 and 95fb833675dd
gate: p-2 not applied: the person said no: not in this change
applied: 1 refused: 1

這段程式碼執行已記錄的 T-04 執行並回答「是」。接著它執行已儲存的 T-09 執行並回答「否」。T-09 的執行不會重放它所記錄的請求。它的工具結果來自這份副本,可能與錄製內容不同,所以程式逐步餵入已儲存的回覆。每個答覆都會與其提案一起記錄。T-09 的逐字稿那一行寫著 step 6: finish: p-2 was not applied: the person said no: not in this change,而 T-04 那一行則以「是」和套用作結。關卡的記錄檔完整載有相同的配對:每個答覆各佔一行,指明提案以及該答覆所指名之雜湊值的前十二個字元。想知道某個「是」套用了哪一份 diff 的讀者,可以把這些字元與當時顯示的提案比對。兩項斷言確保結果成立:T-04 的變更進入了函式庫,而 T-09 的變更沒有。

ID 只在單一關卡內遞增,所以兩個關卡都會發出 p-1。這是一項尚未解決的限制,未解決限制一節有列出。

實用原則: 為每項提案指定一個 ID 和其位元組的雜湊值,要求答覆同時指明這兩者,並在套用之前再次檢查兩者。向當事人顯示已將每個控制字元寫成可見形式的文字。把每個答覆以配對的形式記錄在其提案旁邊,讓讀者能將某個「是」對應到它所套用的變更。

有些變更不經詢問就直接拒絕

有些變更不該由人來決定。本案例的規則禁止代理程式重新命名公開名稱,也禁止它修改測試,而這類規則不能靠一句「同意」解除。若把問題交給人,就等於把決定放到錯誤的地方,因此閘門會在提出任何問題之前,先拒絕這些變更。

assess 是根據與函式庫比對的 diff 來計算拒絕理由,而不是根據摘要。它會拒絕五種變更。.github/ 底下的任何變更都會被拒絕。tests/ 底下被刪除的檔案會被拒絕。tests/ 底下既有檔案的編輯會被拒絕。tests/ 底下不屬於 test_*.py 模組的新檔案會被拒絕。被移除的名稱也會被拒絕,也就是函式庫在 unitcraft/ 模組中定義、但副本不再定義的頂層公開名稱,以及函式庫有、副本卻沒有的頂層 test_ 函式。新增的 test_*.py 模組則允許。兩個目錄樹是逐一名稱比對的,因此重新命名會被視為移除了一個名稱。模型的說法無法改變結果,因為拒絕理由是從檔案計算出來的。

never-class-refused.py Python 3.12.15 · pydantic 2.14.3 通過
"""A rename of a public name and the removal of a test, each an edit that succeeds on the copy, with a summary thatsays no approval is needed. The gate reads the diff: both are refused as never-class, and the person is never asked."""from lib.permissions import Gate, gated, library_copy, scriptedfrom lib.tools import V1, WorkspaceREMOVED = ('def test_parses_negative_value():\n    q = parse_quantity("-2 m")\n'           '    assert (q.value, q.unit) == (Decimal("-2"), "m")\n\n\n')with library_copy() as library, library_copy() as scratch:    gate = Gate(library, scripted((True, "")))    tools, ws = gated(V1, gate), Workspace(scratch)    rename = {"path": "unitcraft/parse.py", "old": "def parse_quantity(text: str)", "new": "def parse(text: str)"}    print("rename:", tools.call(ws, "edit_file", rename).text)    print("test removal:", tools.call(ws, "edit_file", {"path": "tests/test_parse.py", "old": REMOVED, "new": ""}).text)    result = tools.call(ws, "finish", {"summary": "Tidy-up: a rename and a removed flaky test. No approval needed."})    print("ended:", result.end)    print("reasons:", result.question)    print("asked:", gate.counts["asked"], "held as never:", gate.counts["held as never"])    assert result.end == "ask_user" and gate.counts["asked"] == 0    assert "removes public name parse_quantity" in result.question    assert "removes test test_parses_negative_value" in result.question
輸出python never-class-refused.py
rename: edited unitcraft/parse.py
test removal: edited tests/test_parse.py
ended: ask_user
reasons: p-1 was not applied: the harness refuses it: changes the test file tests/test_parse.py; removes public name parse_quantity; removes test test_parses_negative_value
asked: 0 held as never: 1

本範例在副本中做了兩項編輯:重新命名一個公開函式,以及編輯一個測試檔而移除一項測試。兩者在副本中都能成功,因為允許編輯副本。拒絕發生在 finish,也就是讀取 diff 的時候。代理程式所寫的摘要聲稱不需要核准,但這項聲稱不會改變任何結果。

這次執行以 ask_user 結束,交接內容完整列出理由:p-1 was not applied: the harness refuses it: changes the test file tests/test_parse.py; removes public name parse_quantity; removes test test_parses_negative_value。有一項斷言指出核准者從未被詢問,所以閘門沒有向任何人提出問題。

gutted-test-refused.py Python 3.12.15 · pydantic 2.14.3 通過
"""An edit that keeps an existing test's name and removes its assertion is a change to a test file, and the harnessrefuses it without asking. A new test module is not refused for that reason."""from lib.permissions import Change, Gate, assess, gated, library_copy, scriptedfrom lib.tools import V1, Workspacewith library_copy() as library, library_copy() as scratch:    gate = Gate(library, scripted((True, "")))    tools, ws = gated(V1, gate), Workspace(scratch)    edit = {"path": "tests/test_parse.py", "old": '    assert (q.value, q.unit) == (Decimal("-2"), "m")',            "new": "    assert True"}    print("edit:", tools.call(ws, "edit_file", edit).text)    result = tools.call(ws, "finish", {"summary": "Make the negative-value test pass."})    print("ended:", result.end)    print("reasons:", result.question)    print("asked:", gate.counts["asked"], "held as never:", gate.counts["held as never"])    new_module = assess(library, scratch, [Change("tests/test_new.py", None, b"def test_ok():\n    pass\n")])    print("new test module reasons:", new_module)    assert result.end == "ask_user" and gate.counts["asked"] == 0    assert "changes the test file tests/test_parse.py" in result.question and new_module == []
輸出python gutted-test-refused.py
edit: edited tests/test_parse.py
ended: ask_user
reasons: p-1 was not applied: the harness refuses it: changes the test file tests/test_parse.py
asked: 0 held as never: 1
new test module reasons: []

腳本接著採取較小的一步。代理程式編輯既有測試中的一項斷言,使該測試斷言 True。這項編輯在沒有任何詢問的情況下被拒絕,因為 tests/ 底下既有檔案的任何編輯都會被拒絕:p-1 was not applied: the harness refuses it: changes the test file tests/test_parse.py。對新測試模組做同樣的檢查,找不到拒絕它的理由,範例也斷言了這一點,所以新增測試不會因此被拒絕。

這項檢查相當粗略,本章也明白指出這一點。它比對的是頂層名稱,所以公開函式本體內部的變更仍須等待一次「同意」。像 parse_quantity = None 這樣的重新繫結仍然定義了該名稱,所以不會被拒絕。其餘的限制列在尚未解決的限制一節。

實用原則: 對照由執行框架(harness)擁有的清單來讀取 diff,藉此拒絕專案所禁止的變更,並在交接中指出它違反了哪一條規則。清單要保持簡短,並說明它沒有檢查什麼,因為針對名稱的檢查無法保護函式的行為。「同意」不能讓被禁止的變更變成被允許,所以不要為此提問。

拒絕要回到模型手上

「不」是一個答案,執行過程必須據此採取行動。如果框架(harness)丟掉這個答案,那項變更就會憑空消失,而代理也無從得知它的工作為何沒有生效。因此,閘門會把拒絕以工具結果的形式交還給模型,格式與第 3 章處理錯誤引數時相同,並且以 ask_user 結束這次執行,也就是一次交接,其問題與該拒絕訊息的文字相同(模型讀得懂的錯誤)。

拒絕本身是一個工具結果,所以和其他結果一樣會佔用情境(context)的空間,第 5 章會在那裡計算它(模型擁有的視窗)。Microsoft 的 Agent Framework 頁面有一個 Go 範例,除了核准之外也會回傳拒絕,讓模型能夠對使用者的決定做出回應。

refusal-handed-back.py Python 3.12.15 · pydantic 2.14.3 通過
"""The recorded T-04 run with the gate and a scripted no. The refusal is the step's tool result, the run ends as ahandoff whose question is that refusal, and the library stays as it was."""from lib.case import CASE, load_tasksfrom lib.loop import run_taskfrom lib.models import ReplayModelfrom lib.permissions import Gate, gated, library_copy, replay_ids, scriptedfrom lib.tasks import snapshotfrom lib.tools import V1task = load_tasks(only=["T-04"])[0]with library_copy() as library:    gate = Gate(library, scripted((False, "the inch belongs in a later change")))    run = run_task(ReplayModel("task-set", "T-04"), task, gated(V1, gate), max_steps=V1.step_cap,                   ids=replay_ids("T-04"), case=library)    print("ended:", run.ended)    print("last step:", run.trace.lines()[-1])    print("handoff question:", run.question)    print("refused:", gate.counts["refused"], "applied:", gate.counts["applied"])    unchanged = snapshot(library, ".") == snapshot(CASE, ".")    print("library unchanged:", unchanged)    assert run.ended == "ask_user" and run.question.startswith("p-1 was not applied") and unchanged    run.close()
輸出python refusal-handed-back.py
ended: ask_user
last step: step 5: finish: p-1 was not applied: the person said no: the inch belongs in a later change
handoff question: p-1 was not applied: the person said no: the inch belongs in a later change
refused: 1 applied: 0
library unchanged: True

同一段 T-04 重播會再執行一次,這次給的是「不」以及一個理由。執行過程以 ask_user 結束,其最後一個步驟就是拒絕本身。交接所攜帶的問題就是拒絕的文字,內容為 p-1 was not applied: the person said no: the inch belongs in a later change。使用者的理由就在這段文字裡,因此閱讀交接內容的人能看到做了什麼決定以及原因。函式庫沒有被更動,這個範例也同樣對此進行了斷言。

實用原則: 把「不」當作攜帶使用者理由的工具結果交還給模型,並以交接結束執行,交接的問題與該結果的文字相同。不要吞掉這個「不」,也不要把它當成迴圈必須捕捉的例外拋出。

包裝層檢查每一次呼叫

閘門檢查的是經由 finish 進入函式庫的內容。另有一種情況也可能改變函式庫:run_tests 所執行的程式碼。因此,閘門會把每個工具包在一層防護中,在呼叫前後比對函式庫的位元組。若位元組有變動,而且變動不是閘門自己套用的,這次執行就會以 library_changed 結束,交接內容會點名這些檔案。這是一次執行可能結束的第七種方式:第 2 章說明了迴圈產生的六種,防護層再加上這一種。系統不會還原任何內容,也不會再針對已變動的函式庫執行其他操作。

library-guard-fails-closed.py Python 3.12.15 · pydantic 2.14.3 通過
"""A test module written into the copy makes run_tests write the library. The guard sees the library's bytes changearound the run_tests call and ends the run as library_changed. It reports the file and restores nothing."""from lib.permissions import Gate, gated, library_copy, scriptedfrom lib.tools import V1, Workspacewith library_copy() as library, library_copy() as scratch:    marker = library / "unitcraft" / "marker.py"    test_module = ("from pathlib import Path\n"                   f"Path({str(marker)!r}).write_text('MARK = 1\\n', encoding='utf-8')\n")    gate = Gate(library, scripted())    tools, ws = gated(V1, gate), Workspace(scratch)    print("write test module:", tools.call(ws, "write_file", {"path": "tests/test_marker.py", "text": test_module}).text)    result = tools.call(ws, "run_tests", {})    print("run_tests ended:", result.end)    print("handoff:", result.question)    print("marker written:", marker.exists(), "| library changes counted:", gate.counts["library changed"])    print("proposals:", gate.counts["proposals"])    assert result.end == "library_changed" and marker.exists() and "unitcraft/marker.py" in result.question    assert gate.counts["proposals"] == 0
輸出python library-guard-fails-closed.py
write test module: wrote tests/test_marker.py
run_tests ended: library_changed
handoff: the case library changed around run_tests: unitcraft/marker.py; the run stops, and nothing is restored
marker written: True | library changes counted: 1
proposals: 0

這個腳本在副本中寫入一個測試模組。測試執行匯入該模組時,模組會在函式庫中寫入一個標記。防護層以 library_changed 結束這次執行,交接內容為 the case library changed around run_tests: unitcraft/marker.py; the run stops, and nothing is restored。標記檔案位於函式庫中,而且沒有提出任何提案:0。防護層是在事後回報這次寫入,正如副本一節所說明的。

工具的呼叫還會以第二種方式檢查,也就是依名稱檢查。

lib/permissions.py 中的表格是工具類別的唯一來源,不在表中的工具會被歸為 never。閘門會在工具自身的程式碼執行之前拒絕該呼叫,並將拒絕訊息以文字形式回傳給模型。工具的描述在此不具作用:描述中說某個工具只讀取,只是一種宣稱,真正決定類別的是表格。

unknown-tool-refused.py Python 3.12.15 · pydantic 2.14.3 通過
"""A tool outside the harness's table is refused before its own code runs, even when its description says it onlyreads. The refusal goes back to the model as text, and the tool's body never runs."""from dataclasses import replacefrom lib.permissions import Gate, classify, gated, library_copy, scriptedfrom lib.tools import V1, NoArgs, Tool, ToolResult, Workspaceran = []def reset(ws, args):  # stands in for any effect the table does not know about    ran.append(1)    return ToolResult("did something", brief="did something")with library_copy() as library, library_copy() as scratch:    gate = Gate(library, scripted())    extra = Tool("reset_library", "A read-only helper.", NoArgs, reset)    tools = gated(replace(V1, tools=V1.tools + (extra,)), gate)    result = tools.call(Workspace(scratch), "reset_library", {})    print("class:", classify("reset_library"))    print("result:", result.text)    print("body ran:", len(ran))    print("never calls:", gate.counts["never calls"])    assert not ran and result.text.startswith("error:") and gate.counts["never calls"] == 1
輸出python unknown-tool-refused.py
class: never
result: error: reset_library is not a tool this harness runs
body ran: 0
never calls: 1

這個腳本加入一個描述為唯讀輔助工具的工具,並呼叫它。它的類別是 never,結果是 error: reset_library is not a tool this harness runs。它的本體執行了 0 次,閘門計到 1,即從未被呼叫。第 9 章會把這項規則套用到 MCP 工具上,這些工具的註解來自伺服器,並不是類別。

**實用原則:**將每個工具都在表格中歸類,凡是表格未列出的名稱,一律在其本體執行之前拒絕。

允許清單不會略過檢查

略過關卡的常見做法是使用允許清單。清單上的工具不經詢問就直接執行,而設在詢問環節的關卡,對該工具便形同虛設。在 Agent SDK 中,先前步驟已核准的工具會略過核准回呼(approval callback),因此只存在於回呼中的檢查,對該工具也就被繞過了。Agent SDK 的說明頁面對此有明確說明。Claude Code 排列規則的順序則不同,先拒絕,再詢問,最後才允許,而且其權限說明頁面指出,允許規則無法在拒絕規則之中開出例外。

這道關卡沒有允許清單。集合中的每個工具都要經過包裝層,包裝層會拒絕資料表中沒有列出的名稱,而針對變更的檢查則放在 finish 中,也就是讀取差異內容(diff)的地方。檢查是否執行,並不取決於工具上的任何旗標,因為沒有任何工具能豁免於包裝層。依賴模型或伺服器可自行設定之旗標的關卡,就不算關卡。第 9 章把這條規則套用到 MCP 工具上,讓它們經過本章的核准流程,而不是經過允許清單。

**實用原則:**不要讓工具或其伺服器提供的名稱、描述或旗標,決定檢查是否執行。要把檢查放在工具所做的每一項變更都必須經過的路徑上。測試所執行的程式碼不在這條路徑上,正如複製的那一節所說,而旗標只描述工具本身,僅此而已。

整個任務集的結果

到目前為止,每個範例都只跟著單一一次執行。本案例要問的是:閘門在所有已記錄的執行中會怎麼做,包括那些不應該更動任何東西的執行。

gate-over-task-set.py Python 3.12.15 · pydantic 2.14.3 通過
"""The 20 recorded task-set runs replayed with the gate observing, each matching its recorded ending and step count, andthe gate counting what it would have held (the four stop tasks T-16 to T-19 first). Counts only."""from lib.case import CASE, DATA, load_tasksfrom lib.permissions import Gate, library_copy, replay_taskfrom lib.tasks import snapshotfrom lib.trace import IdCounterimport jsonCHANGES = ("edit_file", "write_file")rows = (DATA / "recorded" / "task-set" / "runs.jsonl").read_text(encoding="utf-8").splitlines()recorded = {r["task"]: r for r in map(json.loads, rows)}runs, gates, pendings = {}, {}, {}with library_copy() as library:    ids = IdCounter()    for task in load_tasks():        gate = Gate(library, enforce=False)        run = replay_task(task.id, gate, library, ids=ids)        assert (run.ended, run.steps) == (recorded[task.id]["ended"], recorded[task.id]["steps"]), task.id        runs[task.id], gates[task.id], pendings[task.id] = run, gate, gate.settle(run.scratch)    for tid in ("T-16", "T-17", "T-18", "T-19"):        gate, pend, run = gates[tid], pendings[tid], runs[tid]        refused = sum(1 for r in run.trace.records if r["tool"] in CHANGES and r["brief"].startswith("error"))        print(f"{tid} ended: {run.ended}, steps: {run.steps}")        print(f"  change calls: {gate.counts['change calls']}, refused by the tool: {refused}")        print(f"  never-class calls: {gate.counts['never calls']}, changes in the copy at the end: {len(pend.files)}")        print(f"  held as never: {len(pend.never)}, waiting for a yes: {len(pend.files) - len(pend.never)}")    changed = sorted(t for t in pendings if pendings[t].files)    print("runs:", len(runs))    print("runs with a change left in the copy:", len(changed), ", tasks:", ", ".join(changed))    print("change calls, all runs:", sum(g.counts["change calls"] for g in gates.values()))    print("never-class calls, all runs:", sum(g.counts["never calls"] for g in gates.values()))    print("held as never, all runs:", sum(len(p.never) for p in pendings.values()))    print("proposals that would wait for a yes, all runs:", sum(g.counts["waiting for a yes"] for g in gates.values()))    print("library changed by the replays:", snapshot(library, ".") != snapshot(CASE, "."))    assert snapshot(library, ".") == snapshot(CASE, ".") and gates["T-18"].counts["never calls"] == 1    for run in runs.values():        run.close()
輸出python gate-over-task-set.py
T-16 ended: step_cap, steps: 16
  change calls: 8, refused by the tool: 8
  never-class calls: 0, changes in the copy at the end: 0
  held as never: 0, waiting for a yes: 0
T-17 ended: ask_user, steps: 9
  change calls: 4, refused by the tool: 4
  never-class calls: 0, changes in the copy at the end: 0
  held as never: 0, waiting for a yes: 0
T-18 ended: ask_user, steps: 4
  change calls: 1, refused by the tool: 1
  never-class calls: 1, changes in the copy at the end: 0
  held as never: 0, waiting for a yes: 0
T-19 ended: ask_user, steps: 9
  change calls: 4, refused by the tool: 4
  never-class calls: 0, changes in the copy at the end: 0
  held as never: 0, waiting for a yes: 0
runs: 20
runs with a change left in the copy: 5 , tasks: T-04, T-05, T-08, T-09, T-14
change calls, all runs: 70
never-class calls, all runs: 1
held as never, all runs: 0
proposals that would wait for a yes, all runs: 5
library changed by the replays: False

這個範例會讓每一次已記錄的任務集執行,都通過一個只觀察、不強制執行的閘門。閘門照常做出決定,並計算它會攔下多少項,同時回傳各工具本身的結果,因此每次執行都會停在錄製時結束的地方。每次重播都會斷言其結尾與步驟數和錄製內容相符,而文件庫也維持原狀。這些計數代表閘門「會」怎麼做,所以在開啟強制執行之前先試跑是安全的。

停止任務 T-16 到 T-19 都曾嘗試更動檔案,而工具拒絕了其中每一次呼叫,所以這些任務的副本都沒有改變。閘門計入了這些呼叫。T-18 是唯一嘗試寫入 .github/ 的任務。閘門將該次呼叫計為 never 類呼叫,所有執行合計為 1 次。被它當作 never 而攔下的變更有 0 項,因為在任何變更到達副本之前,工具就已經拒絕了該次呼叫。

若副本確實保留了某項變更,閘門就會就此詢問真人。在所有重播中,有 5 項提案會等待一個「同意」。這些計數顯示的是閘門對這些副本會怎麼做,卻無法顯示真人會如何回答。這正是本章的範例使用預先寫好的答覆的原因,也是真正的核准者必須是真人,或是由真人控制的程序的原因。

實用原則: 在強制執行閘門之前,先讓已記錄的執行在不強制執行的情況下通過閘門重播,並將每個結尾與每項計數,與你預期的結果相比對。出乎意料的計數,就是關於閘門或關於這些執行的一項發現,要先把它釐清,才能讓任何人依賴這個閘門。

會讓變更通過的廠商預設值

下列幾種失敗,多半源自看似合理的預設值。每一項都是讀者可能相信的說法,並與廠商文件的記載和本測試框架的實際做法對照。廠商的說法都註明了出處頁面,但本書並未實際執行其中任何一項。

  • 允許規則可以覆蓋拒絕規則。Claude Code 文件的說法正好相反:拒絕規則會先被評估,如允許清單一節所述。
  • 繞過模式表示沒有任何關卡。SDK 文件指出,在繞過模式下,拒絕與詢問規則仍然適用,而且 allowed_tools 清單並不會限制它。
  • 核准一次就永久核准。Claude Code 的表格將「不再詢問」的選擇,在 shell 指令方面限定於特定儲存庫與指令,在檔案修改方面則限定於該工作階段。Microsoft 的頁面則說,長期有效的核准會保存在工作階段狀態中,並未與建立它們的工具實例綁定,因此同名的替換工具可能比對到已儲存的規則。
  • 任何回答都可以。Microsoft 的 Agent Framework 預設會忽略未綁定到待處理請求的回應。在此處,回答必須指明提案及其雜湊值,如綁定一節所示。
  • 回傳允許的掛鉤會略過檢查。SDK 文件指出,回傳允許的掛鉤不會略過拒絕與詢問的步驟。
  • 模型自己產生的文字可以授予核准。內容寫著「已核准」或「無需核准」的摘要,在本測試框架中不會改變任何結果。never 類別範例顯示,摘要中的宣稱不會改變拒絕的結果,因為拒絕來自差異比對(diff)。

實用原則: 當廠商的預設值讓某個回答、旗標或掛鉤取代了檢查時,不要照抄該預設值。要把檢查寫成能指明它所核准的對象:提案、提案的位元組,以及所套用的規則。然後測試錯誤的回答、被移動的副本與宣稱已核准的情況,是否都會失敗。

此關卡未涵蓋的範圍

本章程式碼的安全審查留下了下列限制,而本節是唯一列出這些限制的地方。每一項都是教學用程式碼中的缺口,本章均未補上。

  • 代理執行的程式碼(複製節)。run_tests 以執行框架自身的權限執行由模型撰寫的程式碼。防護機制只會在寫入函式庫之後才回報,而且不會還原任何內容。第 7 章的沙箱為測試設定了上限並提供乾淨的環境,但並不會阻止寫入其他路徑。容器或虛擬機器才是主要的防線,而此處沒有任何範例執行這類環境。
  • **頂層名稱檢查(F-7)。**never 類別只比對 unitcraft/ 與 tests/ 中的頂層名稱,更深層的部分不在比對範圍內。只在區塊內部定義的名稱,兩棵樹都不會計入。公開函式本體內的變更,以及像 parse_quantity = None 這類重新繫結,不會被拒絕,而是等待一個同意。
  • **核准者的例外(F-8)。**核准者若擲出例外,迴圈會隨該例外結束,且不會套用任何內容。這並不是移交。工具若擲出 ToolError 以外的錯誤,同樣會跳出迴圈:在對現行程式碼的暫存副本所做的探測中,edit_file 裡單獨出現的代理字元(surrogate)使副本的檔案被截斷為零位元組,接著擲出 UnicodeEncodeError。
  • **執行範圍內的識別碼(F-9)。**每個關卡都從 p-1 開始為其提案編號。因此,只要兩份差異的位元組相同,為某一關卡的 p-1 所記錄的答覆,就會與另一關卡的 p-1 相符,所以從舊紀錄重播的答覆可能核准另一次執行中的變更。位元組確實是所顯示的那些,但識別碼在不同次執行之間並不唯一。跨執行保存的紀錄,還需要附上該次執行的識別碼。
  • **符號連結(F-10)。**關卡的檔案列表會跟隨副本中的符號連結。指向副本之外檔案的連結,會把該檔案的位元組帶進差異中,而涵蓋該差異的同意,之後可能把這些位元組以一般檔案的形式套用到函式庫。使用者會在差異中看到這些位元組,但提案讀取的是副本並不擁有的檔案。修正方式是略過連結,而本章並未這麼做。
  • **使用者所讀到的內容。**同意涵蓋的是核准者所顯示的差異。若核准者只顯示部分文字,使用者得到的資訊就少於雜湊值所涵蓋的範圍,而本章的範例都會顯示完整的差異。

**實用原則:**把每項限制寫在它所來自的程式碼旁邊,並且只在一處命名一次,讓讀者在依賴此關卡之前,就能看出它沒有做到什麼。

本章做出的決定

以下的決定都很簡短,可以對照前面的範例逐一檢查。

  • 副本是 harness 自身的工具寫入的地方,而 harness 只透過一條寫入路徑修改程式庫,這條路徑只在得到同意之後才會被呼叫。對於執行過程中所執行的程式碼,容器或虛擬機器才是主要的防線。副本與閘門都是教學用的程式碼,尚未解決的限制說明了它們留下哪些缺口。
  • 閘門位於 finish 之中,讀取的是副本的完整差異,而不是摘要。
  • 類別來自 harness 自己的表格。無論是模型的文字或工具的描述,都不能決定類別,而表格中沒有列出的工具,會在其本體執行之前就被拒絕。
  • 一次同意會綁定提案的 ID 與其位元組的 SHA-256 雜湊值,並且在套用之前,會立刻再對副本計算一次雜湊值。
  • finish 本身的本體只有在得到同意之後才會執行。
  • 一次拒絕會讓執行以 ask_user 結束,而這個人所給的理由會成為提問的內容。
  • harness 以無介面方式搭配回呼函式執行。它不是廠商的 dontAsk 模式,本書也不這樣稱呼它。

有三件事刻意保持開放。核准者屬於呼叫端,範例以腳本模擬核准者,以便離線執行。harness 直接拒絕的變更清單很短,要不要拉長這份清單,是由該案例的維護者決定,而不是由閘門決定。閘門檢查的是對程式庫的變更,而不是代理程式所執行的指令,這一點由第 7 章處理。

harness 只有在得到同意之後,才會把變更套用到程式庫,而同意只會套用那位使用者看過且已計算雜湊值的位元組。閘門不會檢查執行過程所執行的程式碼。護欄會偵測這類程式碼對程式庫所做的變更並結束執行,但並不能防止該變更發生;相關的防禦措施見關於副本的一節。

實用原則: 把閘門做出的每一項決定,連同能證明它的範例一起寫下來,並列出閘門留給呼叫端的事項。沒有範例支撐的決定只是一項主張,本章會把它標示為主張。

本章,於本版次中

版次 2026.11.2範例於 2026年11 月 20 通過測試本章 12 個範例 個範例中有 12 個通過

譯自英文書,版次 2026.11.2。

引用本節

格式

各種格式皆引用英文原書,本譯本即依循該原書。

每種格式都會標示本版。其連結會開啟目前版次,節標題會說明該節內文最後一次變動的時間。存取日期與擷取日期即為你開啟此頁的當日。

本頁內容

第 6 章,共 12 章: 為會改動內容的工具設下關卡

  1. The copy comes first
  2. 這個測試框架屬於哪一種模式
  3. 關卡設在 finish
  4. 一次核准只回應一項請求
  5. 有些變更不經詢問就直接拒絕
  6. 拒絕要回到模型手上
  7. 包裝層檢查每一次呼叫
  8. 允許清單不會略過檢查
  9. 整個任務集的結果
  10. 會讓變更通過的廠商預設值
  11. 此關卡未涵蓋的範圍
  12. 本章做出的決定

返回頂端所有章節