算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
circuit-breaker-pattern【设计与创意】Implement circuit breaker logic for agentic tool calls — tracking tool health, transitioning between closed/open/half-open states, reducing task scope when tools fail, routing to alternatives via capability maps, and enforcing failure budgets to prevent error accumulation. Separates orchestration (deciding what to attempt) from execution (calling too…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
工具失敗時之優雅降級。呼五工具而一壞之代理不該全敗——宜識壞者、止呼之、縮範圍至尚可達者、誠報所略。此技能以分散系統之斷路器模式編碼此邏輯,適於代理工具編排。
核心洞見出自 kirapixelads 之「廚房火患」:調度者(編排層)不宜親烹。定奪何事當試與如何為之之職責分離,令編排者不陷於壞工具之重試迴圈。
宣每一工具所供與可選之替代。此圖乃縮範圍之基——無之,工具敗令代理茫然無措。
capability_map:
- tool: Grep
provides: content search across files
alternatives:
- tool: Bash
method: "rg or grep command"
degradation: "loses Grep's built-in output formatting"
- tool: Read
method: "read suspected files directly"
degradation: "requires knowing which files to check; no broad search"
fallback: "ask the user which files to examine"
- tool: Bash
provides: command execution, build tools, git operations
alternatives: []
fallback: "report commands that need to be run manually"
- tool: Read
provides: file content inspection
alternatives:
- tool: Bash
method: "cat or head command"
degradation: "loses line numbering and truncation safety"
fallback: "ask the user to paste file contents"
- tool: Write
provides: file creation
alternatives:
- tool: Edit
method: "create via full-file edit"
degradation: "requires file to already exist for Edit"
- tool: Bash
method: "echo/cat heredoc"
degradation: "loses Write's atomic file creation"
fallback: "output file contents for the user to save manually"
- tool: WebSearch
provides: external information retrieval
alternatives: []
fallback: "state what information is needed; ask user to provide it"
每一工具記:
預期: 涵代理所用每工具之完整能力圖。每一目至少有退路,即無工具替代。圖明示慣常隱含者:何者關鍵(無替代)、何者可替。
失敗時: 若工具清單不明,由技能前言之 allowed-tools 起。若替代未定,標為 degradation: "unknown — test before relying on this route",勿省。
為每工具立狀態追蹤。所有工具始於 CLOSED 態(健康、正常運作)。
Circuit Breaker State Table:
+------------+--------+-------------------+------------------+-----------------+
| Tool | State | Consecutive Fails | Last Failure | Last Success |
+------------+--------+-------------------+------------------+-----------------+
| Grep | CLOSED | 0 | — | — |
| Bash | CLOSED | 0 | — | — |
| Read | CLOSED | 0 | — | — |
| Write | CLOSED | 0 | — | — |
| Edit | CLOSED | 0 | — | — |
| WebSearch | CLOSED | 0 | — | — |
+------------+--------+-------------------+------------------+-----------------+
Failure budget: 0 / 5 consumed
狀態定義:
狀態轉移:
預期: 所有工具以 CLOSED 態及零敗數初始化之狀態表。失敗閾值與預算已明宣。
失敗時: 若工具清單不能預舉(動態工具發現),首次用時再初始化。模式仍適——唯漸增表而已。
代理需呼工具時,循此決策序。此即調度者邏輯——定是否呼,非如何行。
BEFORE each tool call:
1. Check tool state in the circuit breaker table
2. If OPEN:
a. Check if it is time for a half-open probe
- Yes → transition to HALF-OPEN, proceed with probe call
- No → skip this tool, route to alternative (Step 4)
3. If HALF-OPEN:
a. Make one probe call
b. Success → transition to CLOSED, reset consecutive fails to 0
c. Failure → transition to OPEN, increment failure budget
4. If CLOSED:
a. Make the call normally
AFTER each tool call:
1. Success:
- Reset consecutive fails to 0
- Record last success timestamp
2. Failure:
- Increment consecutive fails
- Record last failure timestamp and error message
- Increment failure budget consumed
- If consecutive fails >= threshold:
transition to OPEN
log: "Circuit OPENED for [tool]: [failure count] consecutive failures"
- If failure budget exhausted:
PAUSE — do not continue the task
Report to user (Step 6)
調度者從不即時重試失敗之呼。其記失敗、驗閾值、續行。重試僅於後步由 HALF-OPEN 探測機制行之。
預期: 清晰之決策迴圈,代理於每次工具呼前後循之。工具健康連續追蹤。調度層永不於壞工具阻塞。
失敗時: 若跨呼追狀態不切實際(如無狀態執行),降至較簡模型:計總敗,至預算而暫停。三態斷路器為理想;敗計數器乃最低可行模式。
工具電路為 OPEN 時,查能力圖(步驟一),路由至最佳可用替代。
路由優先序:
Example routing decision:
Tool needed: Grep (circuit OPEN)
Task: find all files containing "API_KEY"
Route 1: Bash with rg command
→ Degradation: loses Grep's built-in formatting
→ Decision: ACCEPTABLE — use this route
If Bash also OPEN:
Route 2: Read suspected config files directly
→ Degradation: requires guessing which files; no broad search
→ Decision: PARTIAL — try known config paths only
If Read also OPEN:
Route 3: Ask user
→ "I need to find files containing 'API_KEY' but my search
tools are unavailable. Can you run: grep -r 'API_KEY' ."
→ Decision: FALLBACK — user provides the information
If user unavailable:
Route 4: Scope reduction
→ Remove "find API key references" from task scope
→ Document: "SKIPPED: API key search — no tools available"
預期: 工具電路開時,代理透明路由至替代或降範圍。路由決策與降級皆於任務輸出中記,令用戶知所受影響。
失敗時: 若能力圖不全(無替代列),預設降範圍並報。從不靜然略工——總記所略及因。
工具開路且替代窮盡時,縮任務至尚能以可行工具達者。此非敗——乃誠實之範圍管理。
縮範圍規約:
Scope Reduction Report:
Original scope: 5 sub-tasks
[x] 1. Read configuration files (Read: CLOSED)
[x] 2. Search for deprecated patterns (Grep: CLOSED)
[ ] 3. Run test suite (Bash: OPEN — no alternative)
[x] 4. Update documentation (Edit: CLOSED)
[ ] 5. Deploy to staging (Bash: OPEN — no alternative)
Reduced scope: 3 sub-tasks achievable
Deferred: 2 sub-tasks require Bash (circuit OPEN)
Recommendation: Complete sub-tasks 1, 2, 4 now.
Sub-tasks 3 and 5 require Bash — will probe on next cycle
or user can run commands manually.
勿試延期之子任務。勿望開路工具能成而重試。斷路器之存正為此——信其態。
預期: 任務清分為可達與延期之工。代理完成所有可達之工並報延期項附其因及解鎖之道。
失敗時: 若縮範圍除盡所有子任務(每工具皆壞),直跳步驟六——暫停並報。無可行工具之代理不該佯進。
工具返或陳舊之數據時(快取結果、過時快照、先前取得之內容),明標之,勿視為新鮮。
陳舊指標:
標籤規約:
When presenting potentially stale data:
"[STALE DATA — retrieved at {timestamp}, may not reflect current state]
File contents as of last successful Read:
..."
"[CACHED RESULT — Grep returned identical results to previous call;
filesystem may have changed since]"
"[UNVERIFIED — WebSearch result from {date}; current status unknown]"
從不靜然以陳舊數據為當前。用戶或下游代理須知數據品質以作良決。
預期: 所有或陳舊之工具輸出皆明標。新鮮數據不標(標留不定,非留確認)。
失敗時: 若陳舊不可定(無時戳、無比較基線),記其不定:「[FRESHNESS UNKNOWN — no baseline for comparison]」。新鮮性之不定本身即資訊。
跨所有工具追總敗。預算盡時代理暫停並報,勿續積錯。
Failure Budget Enforcement:
Budget: 5 failures per cycle
Current: 4 / 5 consumed
Failure 1: Bash — "permission denied" (step 3)
Failure 2: Bash — "command not found" (step 3)
Failure 3: Bash — "timeout after 120s" (step 4)
Failure 4: WebSearch — "connection refused" (step 5)
Status: 1 failure remaining before mandatory pause
→ Next tool call proceeds with heightened caution
→ If it fails: PAUSE and generate status report
預算盡時:
FAILURE BUDGET EXHAUSTED — PAUSING
Completed work:
- Sub-task 1: Read configuration files (SUCCESS)
- Sub-task 2: Search for deprecated patterns (SUCCESS)
Incomplete work:
- Sub-task 3: Run test suite (FAILED — Bash circuit OPEN)
- Sub-task 4: Update documentation (NOT ATTEMPTED — paused)
- Sub-task 5: Deploy to staging (NOT ATTEMPTED — paused)
Tool health:
Grep: CLOSED (healthy)
Read: CLOSED (healthy)
Edit: CLOSED (healthy)
Bash: OPEN (3 consecutive failures — permission/command/timeout)
WebSearch: OPEN (1 failure — connection refused)
Failures: 5 / 5 budget consumed
Recommendation:
1. Investigate Bash failures — likely environment issue
2. Check network connectivity for WebSearch
3. Resume from sub-task 4 after resolution
暫停並報之職等於電系斷路器:防害之積。代理屢呼壞工具徒耗上下文窗、以重錯惑用戶、或生不一致之部分結果。
預期: 預算盡時代理清淨止。報包括已完之工、未完之工、工具健康、可行之後續步驟。
失敗時: 若代理不能生清淨之報(如狀態追蹤已失),輸出可用之任何資訊。部分報勝於靜然續行。
驗編排邏輯(步驟二至七)與工具執行清分。
調度者(編排)為:
調度者不為:
若調度者「親烹」(為繞其他工具敗而自作工具呼),分離已破。調度者宜路由至替代工具或縮範圍——非試修壞工具。
預期: 編排決策與工具執行間之清界。調度層可描而不引具體工具 API 或錯類型。
失敗時: 若編排與執行糾纏,將決策邏輯抽為每工具呼前行之獨立步。決策步出四輸出之一:CALL、SKIP、PROBE、PAUSE。執行步據之而行。
多工具共基礎設施(網路、檔案系統、權限)時,單一根因可同絆數路。偵此相關模式,勿視每路為獨立。
連鎖失敗指標:
回應規約:
退讓倍乘: 連鎖失敗觸發時,半開探用指數退讓:第 3 步探、後第 6 步、後第 12 步。間隔上限為 20 步以防永久閉路。此防速探壓倒復原中之系統。
預期: 相關敗被偵並視為單一系統性事件,非 N 次獨立路絆。失敗預算計系統性事件一次,非 N 次。
失敗時: 若相關偵測不切實際(諸敗雖共因而錯簽各異),降為獨立每工具路。系統仍優雅降級——僅耗預算更速而已。
入斷路器迴圈(步驟三)前,選擇性驗工具可用且或成。此減可預測失敗致之無謂路絆。
呼前檢查:
| 檢查 | 方法 | 失敗時行動 |
|---|---|---|
| 工具存在 | 驗工具於 allowed-tools 列中 | 略——不試 |
| MCP 伺服器健康 | 查伺服器進程/連接狀態 | 即路由至替代 |
| 資源可用 | 驗目標檔案/URL/端點存在 | 路由或降範圍 |
決策表:
Pre-call score:
AVAILABLE → proceed to circuit breaker loop (Step 3)
DEGRADED → proceed with caution, lower the failure threshold by 1
UNAVAILABLE → skip tool, route to alternative (Step 4) without consuming budget
呼前檢查為諮,非斷。通呼前檢查之工具仍可於執行時敗。斷路器仍為主要可靠機制。
預期: 可預測失敗(缺工具、不可達伺服器)於耗失敗預算前被捕。斷路器僅處真正運行時失敗。
失敗時: 若呼前檢查不可用或增負過多,全略此步。步驟三之斷路器迴圈處所有敗——呼前選擇乃優化而非必需。
fail-early-pattern — 互補模式:fail-early 於工作始前驗輸入;circuit-breaker 管工作中失敗escalate-issues — 失敗預算盡或縮範圍大時,升至專家或人write-incident-runbook — 記屢現工具失敗模式為手冊以速診assess-context — 評當前法能否適應於多工具降級時;配縮範圍決策du-dum — 分觀察與決策之雙鐘架構;減代理迴圈觀察耗之互補模式