Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Harness Engineering 設計模式 — 基於 Claude Code 原始碼逆向分析的 12 條可遷移原則。Use when: 設計 agent 系統架構、實作 tool orchestration、設計 context 管理策略、建構 agent loop。
.claude/skills/majiayu000-cc-harness-patterns/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-08 | ✗→✓ | ▲ Improved | 35% | 0% |
| case-01 | ✗→✓ | ▲ Improved | 20% | 0% |
| case-02 | ✗→✓ | ▲ Improved | 9% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 15% | 0% |
| case-04 | ✗→✓ | ▲ Improved | -7% | 0% |
Harness = Tools + Knowledge + Observation + Action Interfaces + Permissions
基於 Claude Code v2.1.88(92,500 行 TypeScript)逆向分析。
Claude Code 的 agent loop 用 async generator 實作:
typescriptasync function* queryModel(...): AsyncGenerator<StreamEvent | AssistantMessage> { // yield 每個 stream event,呼叫端按需消費 }
核心循環:queryModel() → 檢查 stop_reason → tool_use 時呼叫 runTools() → 結果回注 messages → 再次 queryModel()
stop_reason 分支:end_turn(結束)、tool_use(執行工具)、max_tokens(context 滿了,觸發 compaction)
typescriptfunction isConcurrencySafe(input): boolean { // 工具自己聲明是否可並行 }
partitionToolCalls() 把一批工具呼叫分成可並行組和必須串行組Schema Validation → Custom validateInput() → Input Sanitization
→ PreToolUse Hooks → Permission Resolution → Execution → PostToolUse Hooks每層只問一個問題:「能否拒絕?」拒絕即停,不往下走。執行邏輯永遠在最後一層。
Sticky Latch:影響 cache key 的欄位一旦啟用就不在 session 內關閉。
typescriptif (!fastModeHeaderLatched && isFastMode) { fastModeHeaderLatched = true // 永遠 latched }
確定性 ID:注入 context 的 ID 從穩定輸入衍���,不隨機生成。
靜態/動態邊界:SYSTEM_PROMPT_DYNAMIC_BOUNDARY 把 prompt 分成可全域快取的靜態段和 session 特定的動態段,防 2^N 快取桶爆炸。
Attachment > Inline:動態 agent 列表從 tool description 移到 attachment message,避免 MCP 連接/plugin 載入導致 tool schema cache bust。省 fleet 10.2% cache creation tokens。
原則:什麼進 context、什麼順序、何時壓縮。
normalizeMessagesForAPI:10 步處理(過濾空訊息、合併連續同角色、注入 system-reminder、truncate 過長結果...)<analysis> 草稿:讓模型在 scratchpad 思考,結果不進 contextPiiSafeString<T> 型別系統,編譯期防止 PII 進入遙測詳見 references/design-principles.md。
Other measured skills in the registry, with their headline benchmark lift.