簡介
什麼是PRECC?
PRECC (Claude Code 預測性錯誤糾正) 是一個Rust工具,通過官方的PreToolUse鉤子機制攔截Claude Code的bash命令。它在錯誤發生之前修復它們,節省token並消除重試循環。
對社區用戶免費。
問題
Claude Code在可預防的錯誤上浪費大量token:
- 目錄錯誤 – 在沒有
Cargo.toml的父目錄中運行cargo build,然後在讀取錯誤後重試。 - 重試循環 – 失敗的命令產生冗長的輸出,Claude讀取、推理並重試。每個循環消耗數百個token。
- 冗長輸出 –
find或ls -R等命令輸出數千行,Claude必須處理這些內容。
四大支柱
上下文修復 (cd-prepend)
檢測到 cargo build 或 npm test 等命令在錯誤的目錄中運行時,在執行前添加 cd /正確/路徑 &&。
GDB調試
檢測附加GDB進行更深入調試的機會,提供結構化的調試信息而不是原始的核心轉儲。
會話挖掘
挖掘Claude Code會話日誌中的失敗-修復對。當同樣的錯誤再次發生時,PRECC已經知道修復方法並自動應用。
自動化技能
內置和挖掘技能庫,匹配命令模式並重寫它們。技能定義爲TOML文件或SQLite行,便於檢查、編輯和共享。
工作原理(30秒版本)
- Claude Code即將運行一個bash命令。
- PreToolUse鉤子將命令作爲JSON通過stdin發送給
precc-hook。 precc-hook在3毫秒內通過管道(技能、目錄修正、壓縮)處理命令。- 修正後的命令作爲JSON通過stdout返回。
- Claude Code執行修正後的命令。
Claude永遠看不到錯誤。沒有token浪費。
自適應壓縮
如果命令在壓縮後失敗,PRECC會自動在重試時跳過壓縮,以便Claude獲得完整的未壓縮輸出來除錯。
實時使用統計
| 指標 | 值 |
|---|---|
| 鉤子調用次數 | – |
| 節省的token | – |
| 節省比率 | –% |
| RTK重寫 | – |
| CD修正 | – |
| 鉤子延遲 | – ms (p50) |
數字為估算值。每次預防的失敗避免了完整的重試循環:錯誤輸出、模型推理和重試命令。 這些數字會從匿名遙測數據自動更新。
鏈接
- GitHub: https://github.com/peria-ai/precc-cc
- 網站: https://peria.ai
- 文檔: https://precc.cc
安裝
快速安裝 (Linux / macOS)
curl -fsSL https://raw.githubusercontent.com/peria-ai/precc-cc/main/scripts/install.sh | bash
這會下載適用於您平臺的最新版本二進制文件,驗證SHA256校驗和,並將其放置在 ~/.local/bin/ 中。
安裝後,初始化PRECC:
precc init
precc init 在Claude Code中註冊PreToolUse鉤子,創建數據目錄,並初始化技能數據庫。
安裝選項
SHA256驗證
默認情況下,安裝程序會根據已發佈的SHA256校驗和驗證二進制文件。要跳過驗證(不推薦):
curl -fsSL https://raw.githubusercontent.com/peria-ai/precc-cc/main/scripts/install.sh | bash -s -- --no-verify
自定義安裝前綴
安裝到自定義位置:
curl -fsSL https://raw.githubusercontent.com/peria-ai/precc-cc/main/scripts/install.sh | bash -s -- --prefix /opt/precc
附加工具 (–extras)
PRECC附帶可選的附加工具。使用 --extras 安裝它們:
curl -fsSL https://raw.githubusercontent.com/peria-ai/precc-cc/main/scripts/install.sh | bash -s -- --extras
這將安裝:
| 工具 | 用途 |
|---|---|
| RTK | 命令重寫工具包 |
| lean-ctx | CLAUDE.md和提示文件的上下文壓縮 |
| nushell | 用於高級管道的結構化Shell |
| cocoindex-code | 代碼索引以加快上下文解析 |
Windows (PowerShell)
irm https://raw.githubusercontent.com/peria-ai/precc-cc/main/scripts/install.ps1 | iex
然後初始化:
precc init
手動安裝
- 從 GitHub Releases 下載適用於您平臺的發佈二進制文件。
- 根據版本中的
.sha256文件驗證SHA256校驗和。 - 將二進制文件放置在
PATH中的目錄中(例如~/.local/bin/)。 - 運行
precc init。
更新
precc update
強制更新到特定版本:
precc update --force --version 0.3.0
啓用自動更新:
precc update --auto
驗證安裝
$ precc --version
precc 0.3.0
$ precc savings
Session savings: 0 tokens (no commands intercepted yet)
如果找不到 precc,請確保 ~/.local/bin 在您的 PATH 中。
快速入門
5分鐘內啓動PRECC。
步驟1:安裝
curl -fsSL https://raw.githubusercontent.com/peria-ai/precc-cc/main/scripts/install.sh | bash
步驟2:初始化
$ precc init
[precc] Hook registered with Claude Code
[precc] Created ~/.local/share/precc/
[precc] Initialized heuristics.db with 8 built-in skills
[precc] Ready.
步驟3:驗證Hook已激活
$ precc skills list
# Name Type Triggers
1 cargo-wrong-dir built-in cargo build/test/clippy outside Rust project
2 git-wrong-dir built-in git * outside a repo
3 go-wrong-dir built-in go build/test outside Go module
4 make-wrong-dir built-in make without Makefile in cwd
5 npm-wrong-dir built-in npm/npx/pnpm/yarn outside Node project
6 python-wrong-dir built-in python/pytest/pip outside Python project
7 jj-translate built-in git * in jj-colocated repo
8 asciinema-gif built-in asciinema rec
步驟4:正常使用Claude Code
打開Claude Code並照常工作。PRECC在後臺靜默運行。當Claude發出一個會失敗的命令時,PRECC會在執行前修正它。
示例:錯誤目錄的Cargo Build
假設你的項目在 ~/projects/myapp/,Claude發出:
cargo build
從 ~/projects/(高了一級,那裏沒有 Cargo.toml)。
沒有PRECC: Claude收到錯誤 could not find Cargo.toml in /home/user/projects or any parent directory,讀取、推理,然後用 cd myapp && cargo build 重試。代價:浪費約2,000個token。
使用PRECC: Hook檢測到缺失的 Cargo.toml,在 myapp/ 中找到它,並將命令重寫爲:
cd /home/user/projects/myapp && cargo build
Claude永遠看不到錯誤。零token浪費。
步驟5:查看節省情況
會話結束後,查看PRECC節省了多少token:
$ precc savings
Session Token Savings
=====================
Total estimated savings: 4,312 tokens
Breakdown:
Pillar 1 (cd prepends): 2,104 tokens (3 corrections)
Pillar 4 (skill activations): 980 tokens (2 activations)
RTK rewrites: 1,228 tokens (5 rewrites)
後續步驟
許可證
PRECC提供兩個層級:Community(免費)和Pro。
Community層(免費)
Community層包括:
- 所有內置技能(錯誤目錄修正、jj翻譯等)
- 支持完整Pillar 1和Pillar 4的Hook管道
- 基本的
precc savings摘要 - 使用
precc ingest進行會話挖掘 - 無限本地使用
Pro層
Pro解鎖額外功能:
- 詳細節省分析 –
precc savings --all逐命令分析 - GIF錄製 –
precc gif用於創建終端動畫GIF - IP地理圍欄合規 – 適用於受監管的環境
- 電子郵件報告 –
precc mail report發送分析報告 - GitHub Actions分析 –
precc gha用於調試失敗的工作流 - 上下文壓縮 –
precc compress用於CLAUDE.md優化 - 優先支持
激活許可證
$ precc license activate XXXX-XXXX-XXXX-XXXX --email you@example.com
[precc] License activated for you@example.com
[precc] Plan: Pro
[precc] Expires: 2027-04-03
檢查許可證狀態
$ precc license status
License: Pro
Email: you@example.com
Expires: 2027-04-03
Status: Active
GitHub Sponsors激活
如果您通過GitHub Sponsors贊助PRECC,您的許可證將通過您的GitHub郵箱自動激活。無需密鑰——只需確保您的贊助者郵箱匹配:
$ precc license status
License: Pro (GitHub Sponsors)
Email: you@example.com
Status: Active (auto-renewed)
設備指紋
每個許可證都綁定到設備指紋。使用以下命令查看:
$ precc license fingerprint
Fingerprint: a1b2c3d4e5f6...
如果需要將許可證轉移到新機器,請先停用:
precc license deactivate
然後在新機器上激活。
許可證過期?
當Pro許可證到期時,PRECC會恢復到Community層。所有內置技能和核心功能繼續工作。只有Pro特有功能變爲不可用。詳情請參閱FAQ。
鉤子管道
precc-hook 二進制文件是PRECC的核心。它位於Claude Code和shell之間,在5毫秒內處理每個bash命令。
Claude Code如何調用鉤子
Claude Code支持PreToolUse鉤子——可以在執行前檢查和修改工具輸入的外部程序。當Claude即將運行bash命令時,它通過stdin將JSON發送給 precc-hook 並從stdout讀取響應。
管道階段
Claude Code
|
v
+---------------------------+
| 1. Parse JSON stdin | Read the command from Claude Code
+---------------------------+
|
v
+---------------------------+
| 2. Skill matching | Query heuristics.db for matching skills (Pillar 4)
+---------------------------+
|
v
+---------------------------+
| 3. Directory correction | Resolve correct working directory (Pillar 1)
+---------------------------+
|
v
+---------------------------+
| 4. GDB check | Detect debug opportunities (Pillar 2)
+---------------------------+
|
v
+---------------------------+
| 5. RTK rewriting | Apply command rewrites for token savings
+---------------------------+
|
v
+---------------------------+
| 6. Emit JSON stdout | Return modified command to Claude Code
+---------------------------+
|
v
Shell executes corrected command
示例:JSON輸入和輸出
輸入(來自Claude Code)
{
"tool_input": {
"command": "cargo build"
}
}
PRECC檢測到當前目錄沒有 Cargo.toml,但 ./myapp/Cargo.toml 存在。
輸出(到Claude Code)
{
"hookSpecificOutput": {
"updatedInput": {
"command": "cd /home/user/projects/myapp && cargo build"
}
}
}
如果不需要修改,updatedInput.command 爲空,Claude Code使用原始命令。
階段詳情
階段1:解析JSON
從stdin讀取完整的JSON對象。提取 tool_input.command。如果解析失敗,鉤子立即退出,Claude Code使用原始命令(fail-open設計)。
階段2:技能匹配
查詢SQLite啓發式數據庫,尋找觸發模式與命令匹配的技能。技能按優先級順序檢查。內置TOML技能和挖掘的技能都會被評估。
階段3:目錄修正
對於構建命令(cargo、go、make、npm、python 等),檢查預期的項目文件是否存在於當前目錄中。如果不存在,掃描附近目錄尋找最近匹配並添加 cd <dir> && 前綴。
目錄掃描使用緩存的文件系統索引,TTL爲5秒,以保持高速。
階段4:GDB檢查
如果命令可能產生崩潰(例如運行調試二進制文件),PRECC可以建議或注入GDB包裝器來捕獲結構化的調試輸出,而不是原始崩潰日誌。
階段5:RTK重寫
應用RTK(重寫工具包)規則,縮短冗長命令、抑制嘈雜輸出或重構命令以提高token效率。
階段6:輸出JSON
將修改後的命令序列化回JSON並寫入stdout。如果沒有更改,輸出信號Claude Code使用原始命令。
性能
整個管道在5毫秒(p99)內完成。關鍵優化:
- SQLite使用WAL模式實現無鎖併發讀取
- 預編譯的正則表達式模式用於技能匹配
- 緩存的文件系統掃描(5秒TTL)
- 熱路徑中無網絡調用
- Fail-open:任何錯誤都回退到原始命令
手動測試鉤子
你可以直接調用鉤子:
$ echo '{"tool_input":{"command":"cargo build"}}' | precc-hook
{"hookSpecificOutput":{"updatedInput":{"command":"cd /home/user/myapp && cargo build"}}}
技能
技能是PRECC用來檢測和糾正命令的模式匹配規則。它們可以是內置的(作爲TOML文件分發)或從會話日誌中挖掘的。
內置技能
| 技能 | 觸發條件 | 動作 |
|---|---|---|
cargo-wrong-dir | 在Rust項目外運行 cargo build/test/clippy | 在命令前添加 cd 到最近的 Cargo.toml 目錄 |
git-wrong-dir | 在git倉庫外運行 git * | 在命令前添加 cd 到最近的 .git 目錄 |
go-wrong-dir | 在Go模塊外運行 go build/test | 在命令前添加 cd 到最近的 go.mod 目錄 |
make-wrong-dir | 當前目錄沒有Makefile時運行 make | 在命令前添加 cd 到最近的Makefile目錄 |
npm-wrong-dir | 在Node項目外運行 npm/npx/pnpm/yarn | 在命令前添加 cd 到最近的 package.json 目錄 |
python-wrong-dir | 在Python項目外運行 python/pytest/pip | 在命令前添加 cd 到最近的Python項目 |
jj-translate | 在jj共存倉庫中運行 git * | 重寫爲等效的 jj 命令 |
asciinema-gif | asciinema rec | 重寫爲 precc gif |
列出技能
$ precc skills list
# Name Type Triggers
1 cargo-wrong-dir built-in cargo build/test/clippy outside Rust project
2 git-wrong-dir built-in git * outside a repo
3 go-wrong-dir built-in go build/test outside Go module
4 make-wrong-dir built-in make without Makefile in cwd
5 npm-wrong-dir built-in npm/npx/pnpm/yarn outside Node project
6 python-wrong-dir built-in python/pytest/pip outside Python project
7 jj-translate built-in git * in jj-colocated repo
8 asciinema-gif built-in asciinema rec
9 fix-pytest-path mined pytest with wrong test path
顯示技能詳情
$ precc skills show cargo-wrong-dir
Name: cargo-wrong-dir
Type: built-in
Source: skills/builtin/cargo-wrong-dir.toml
Description: Detects cargo commands run outside a Rust project and prepends
cd to the directory containing the nearest Cargo.toml.
Trigger: ^cargo\s+(build|test|clippy|run|check|bench|doc)
Action: prepend_cd
Marker: Cargo.toml
Activations: 12
將技能導出爲TOML
$ precc skills export cargo-wrong-dir
[skill]
name = "cargo-wrong-dir"
description = "Prepend cd for cargo commands outside a Rust project"
trigger = "^cargo\\s+(build|test|clippy|run|check|bench|doc)"
action = "prepend_cd"
marker = "Cargo.toml"
priority = 10
編輯技能
$ precc skills edit cargo-wrong-dir
這將在您的 $EDITOR 中打開技能定義。保存後,技能會自動重新加載。
Advise 命令
precc skills advise 分析您最近的會話,並根據重複模式建議新技能:
$ precc skills advise
Analyzed 47 commands from the last session.
Suggested skills:
1. docker-wrong-dir: You ran `docker compose up` outside the project root 3 times.
Suggested trigger: ^docker\s+compose
Suggested marker: docker-compose.yml
2. terraform-wrong-dir: You ran `terraform plan` outside the infra directory 2 times.
Suggested trigger: ^terraform\s+(plan|apply|init)
Suggested marker: main.tf
Accept suggestion [1/2/skip]?
聚類技能
$ precc skills cluster
將相似的挖掘技能分組,幫助識別冗餘或重疊的模式。
挖掘技能與內置技能
內置技能隨PRECC一起分發,定義在 skills/builtin/*.toml 中。它們涵蓋了最常見的目錄錯誤。
挖掘技能由 precc ingest 或 precc-learner 守護進程從您的會話日誌創建。它們存儲在 ~/.local/share/precc/heuristics.db 中,特定於您的工作流程。詳情請參閱挖掘。
節省
PRECC追蹤每次攔截的估計token節省。使用 precc savings 查看PRECC阻止了多少浪費。
快速摘要
$ precc savings
Session Token Savings
=====================
Total estimated savings: <span data-stat="session_tokens_saved">8,741</span> tokens
Breakdown:
Pillar 1 (cd prepends): <span data-stat="session_p1_tokens">3,204</span> tokens (<span data-stat="session_p1_count">6</span> corrections)
Pillar 4 (skill activations): <span data-stat="session_p4_tokens">1,560</span> tokens (<span data-stat="session_p4_count">4</span> activations)
RTK rewrites: <span data-stat="session_rtk_tokens">2,749</span> tokens (<span data-stat="session_rtk_count">11</span> rewrites)
Lean-ctx wraps: <span data-stat="session_lean_tokens">1,228</span> tokens (<span data-stat="session_lean_count">2</span> wraps)
詳細分類(Pro)
$ precc savings --all
Session Token Savings (Detailed)
================================
Total estimated savings: <span data-stat="session_tokens_saved">8,741</span> tokens
Command-by-command:
# Time Command Saving Source
1 09:12 cargo build 534 tk cd prepend (cargo-wrong-dir)
2 09:14 cargo test 534 tk cd prepend (cargo-wrong-dir)
3 09:15 git status 412 tk cd prepend (git-wrong-dir)
4 09:18 npm install 824 tk cd prepend (npm-wrong-dir)
5 09:22 find . -name "*.rs" 387 tk RTK rewrite (output truncation)
6 09:25 cat src/main.rs 249 tk RTK rewrite (lean-ctx wrap)
7 09:31 cargo clippy 534 tk cd prepend (cargo-wrong-dir)
...
Pillar Breakdown:
Pillar 1 (context resolution): <span data-stat="session_p1_tokens">3,204</span> tokens <span data-stat="session_p1_pct">36.6</span>%
Pillar 2 (GDB debugging): 0 tokens 0.0%
Pillar 3 (mined preventions): 0 tokens 0.0%
Pillar 4 (automation skills): <span data-stat="session_p4_tokens">1,560</span> tokens <span data-stat="session_p4_pct">17.8</span>%
RTK rewrites: <span data-stat="session_rtk_tokens">2,749</span> tokens <span data-stat="session_rtk_pct">31.5</span>%
Lean-ctx wraps: <span data-stat="session_lean_tokens">1,228</span> tokens <span data-stat="session_lean_pct">14.1</span>%
如何估算節省
每種修正類型都有基於沒有PRECC時會發生什麼的估計token成本:
| 修正類型 | 估計節省 | 原因 |
|---|---|---|
| cd prepend | ~500 tokens | 錯誤輸出 + Claude推理 + 重試 |
| 技能激活 | ~400 tokens | 錯誤輸出 + Claude推理 + 重試 |
| RTK rewrite | ~250 tokens | Claude需要閱讀的冗長輸出 |
| Lean-ctx wrap | ~600 tokens | 大文件內容被壓縮 |
| 挖掘預防 | ~500 tokens | 已知的失敗模式被避免 |
這些是保守估計。實際節省通常更高,因爲Claude對錯誤的推理可能很冗長。
累計節省
節省數據在PRECC數據庫中跨會話持久化。隨着時間推移,您可以跟蹤總體影響:
$ precc savings
Session Token Savings
=====================
Total estimated savings: <span data-stat="session_tokens_saved">8,741</span> tokens
Lifetime savings: <span data-stat="total_tokens_saved">142,389</span> tokens across <span data-stat="total_sessions">47</span> sessions
壓縮
precc compress 縮小 CLAUDE.md 和其他上下文文件,以減少 Claude Code 加載時的 token 使用量。這是 Pro 功能。
基本用法
$ precc compress .
[precc] Scanning directory: .
[precc] Found 3 context files:
CLAUDE.md (2,847 tokens -> 1,203 tokens, -57.7%)
ARCHITECTURE.md (4,112 tokens -> 2,044 tokens, -50.3%)
ALTERNATIVES.md (3,891 tokens -> 1,967 tokens, -49.5%)
[precc] Total: 10,850 tokens -> 5,214 tokens (-51.9%)
[precc] Files compressed. Use --revert to restore originals.
試運行
預覽將要更改的內容而不修改文件:
$ precc compress . --dry-run
[precc] Dry run -- no files will be modified.
[precc] CLAUDE.md: 2,847 tokens -> 1,203 tokens (-57.7%)
[precc] ARCHITECTURE.md: 4,112 tokens -> 2,044 tokens (-50.3%)
[precc] ALTERNATIVES.md: 3,891 tokens -> 1,967 tokens (-49.5%)
[precc] Total: 10,850 tokens -> 5,214 tokens (-51.9%)
還原
原始文件會自動備份。要恢復它們:
$ precc compress --revert
[precc] Restored 3 files from backups.
壓縮了什麼
壓縮器應用多種轉換:
- 刪除冗餘空白和空行
- 縮短冗長的措辭同時保留含義
- 壓縮表格和列表
- 去除註釋和裝飾性格式
- 保留所有代碼塊、路徑和技術標識符
壓縮後的輸出仍然是人類可讀的——它不是壓縮化或混淆的。
針對特定文件
$ precc compress CLAUDE.md
[precc] CLAUDE.md: 2,847 tokens -> 1,203 tokens (-57.7%)
報告
precc report 生成一個分析儀表板,總結 PRECC 活動和 token 節省情況。
生成報告
$ precc report
PRECC Report -- 2026-04-03
==========================
Sessions analyzed: 12
Commands intercepted: 87
Total token savings: 42,389
Top skills by activation:
1. cargo-wrong-dir 34 activations 17,204 tokens saved
2. npm-wrong-dir 18 activations 9,360 tokens saved
3. git-wrong-dir 12 activations 4,944 tokens saved
4. RTK rewrite 15 activations 3,750 tokens saved
5. python-wrong-dir 8 activations 4,131 tokens saved
Savings by pillar:
Pillar 1 (context resolution): 28,639 tokens 67.6%
Pillar 4 (automation skills): 7,000 tokens 16.5%
RTK rewrites: 3,750 tokens 8.8%
Lean-ctx wraps: 3,000 tokens 7.1%
Recent corrections:
2026-04-03 09:12 cargo build -> cd myapp && cargo build
2026-04-03 09:18 npm test -> cd frontend && npm test
2026-04-03 10:05 git status -> cd repo && git status
...
通過電子郵件發送報告
將報告發送到電子郵件地址(需要郵件設置,見 Email):
$ precc report --email
[precc] Report sent to you@example.com
收件人地址從 ~/.config/precc/mail.toml 讀取。您也可以使用 precc mail report EMAIL 發送到特定地址。
報告數據
報告從本地 PRECC 數據庫 ~/.local/share/precc/history.db 生成。除非您明確通過電子郵件發送報告,否則沒有數據離開您的機器。
挖掘
PRECC挖掘Claude Code會話日誌以學習失敗-修復模式。當它再次看到同樣的錯誤時,會自動應用修復。
導入會話日誌
導入單個文件
$ precc ingest ~/.claude/logs/session-2026-04-03.jsonl
[precc] Parsing session-2026-04-03.jsonl...
[precc] Found 142 commands, 8 failure-fix pairs
[precc] Stored 8 patterns in history.db
[precc] 2 new skill candidates identified
導入所有日誌
$ precc ingest --all
[precc] Scanning ~/.claude/logs/...
[precc] Found 23 session files (14 new, 9 already ingested)
[precc] Parsing 14 new files...
[precc] Found 47 failure-fix pairs across 14 sessions
[precc] Stored 47 patterns in history.db
[precc] 5 new skill candidates identified
強制重新導入
要重新處理已導入的文件:
$ precc ingest --all --force
[precc] Re-ingesting all 23 session files...
挖掘的工作原理
- PRECC讀取會話JSONL日誌文件。
- 它識別命令對,其中第一個命令失敗,第二個是糾正後的重試。
- 它提取模式(出了什麼問題)和修復(Claude做了什麼不同的事)。
- 模式存儲在
~/.local/share/precc/history.db中。 - 當模式達到置信閾值(多次出現)時,它成爲
heuristics.db中的挖掘技能。
示例模式
Failure: pytest tests/test_auth.py
Error: ModuleNotFoundError: No module named 'myapp'
Fix: cd /home/user/myapp && pytest tests/test_auth.py
Pattern: pytest outside project root -> prepend cd
precc-learner 守護進程
precc-learner 守護進程在後臺運行,自動監視新的會話日誌:
$ precc-learner &
[precc-learner] Watching ~/.claude/logs/ for new sessions...
[precc-learner] Processing session-2026-04-03-1412.jsonl... 3 new patterns
守護進程使用文件系統通知(Linux上的inotify,macOS上的FSEvents),因此在會話結束時立即做出反應。
從模式到技能
挖掘的模式在滿足以下條件時升級爲技能:
- 跨會話至少出現3次
- 一致的修復模式(每次相同類型的糾正)
- 未檢測到誤報
您可以通過以下方式查看技能候選:
$ precc skills advise
有關管理技能的詳細信息,請參見 Skills。
數據存儲
- 失敗-修復對:
~/.local/share/precc/history.db - 升級的技能:
~/.local/share/precc/heuristics.db
兩者都是WAL模式的SQLite數據庫,用於安全的併發訪問。
電子郵件
PRECC可以通過電子郵件發送報告和文件。這需要一次性的SMTP設置。
設置
$ precc mail setup
SMTP host: smtp.gmail.com
SMTP port [587]: 587
Username: you@gmail.com
Password: ********
From address [you@gmail.com]: you@gmail.com
[precc] Mail configuration saved to ~/.config/precc/mail.toml
[precc] Sending test email to you@gmail.com...
[precc] Test email sent successfully.
配置文件
配置存儲在 ~/.config/precc/mail.toml:
[smtp]
host = "smtp.gmail.com"
port = 587
username = "you@gmail.com"
password = "app-password-here"
from = "you@gmail.com"
tls = true
您可以直接編輯此文件:
$EDITOR ~/.config/precc/mail.toml
對於Gmail,請使用應用密碼而不是您的賬戶密碼。
發送報告
$ precc mail report team@example.com
[precc] Generating report...
[precc] Sending to team@example.com...
[precc] Report sent.
發送文件
$ precc mail send colleague@example.com output.log
[precc] Sending output.log to colleague@example.com...
[precc] Sent (14.2 KB).
SSH中繼支持
如果您的機器無法直接訪問SMTP服務器(例如,在企業防火牆後面),PRECC支持通過SSH隧道中繼:
[smtp]
host = "localhost"
port = 2525
[ssh_relay]
host = "relay.example.com"
user = "you"
remote_port = 587
local_port = 2525
PRECC將在發送前自動建立SSH隧道。
GIF錄製
precc gif 從bash腳本創建終端會話的動畫GIF錄製。這是Pro功能。
基本用法
$ precc gif script.sh 30s
[precc] Recording script.sh (max 30s)...
[precc] Running: echo "Hello, world!"
[precc] Running: cargo build --release
[precc] Running: cargo test
[precc] Recording complete.
[precc] Output: script.gif (1.2 MB, 24s)
第一個參數是包含要運行的命令的bash腳本。第二個參數是最大錄製時長。
腳本格式
腳本是標準的bash文件:
#!/bin/bash
echo "Building project..."
cargo build --release
echo "Running tests..."
cargo test
echo "Done!"
輸入模擬
對於交互式命令,提供輸入值作爲額外參數:
$ precc gif interactive-demo.sh 60s "yes" "my-project" "3"
每個額外參數在腳本提示輸入時作爲一行stdin輸入。
輸出選項
輸出文件默認以腳本命名(script.gif)。GIF使用深色終端主題,標準80x24尺寸。
爲什麼使用GIF而不是asciinema?
內置技能 asciinema-gif 自動將 asciinema rec 重寫爲 precc gif。GIF文件更具可移植性——它們可以在GitHub README、Slack和電子郵件中內聯顯示,無需播放器。
GitHub Actions 分析
precc gha 分析失敗的GitHub Actions運行並建議修復方案。這是Pro功能。
用法
傳入失敗的GitHub Actions運行的URL:
$ precc gha https://github.com/myorg/myrepo/actions/runs/12345678
[precc] Fetching run 12345678...
[precc] Run: CI / build (ubuntu-latest)
[precc] Status: failure
[precc] Failed step: Run cargo test
[precc] Log analysis:
Error: test result: FAILED. 2 passed; 1 failed
Failed test: tests::integration::test_database_connection
Cause: thread 'tests::integration::test_database_connection' panicked at
'called Result::unwrap() on an Err value: Connection refused'
[precc] Suggested fix:
The test requires a database connection but the CI environment does not
start a database service. Add a services block to your workflow:
services:
postgres:
image: postgres:15
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: test
功能說明
- 解析GitHub Actions運行URL以提取所有者、倉庫和運行ID。
- 通過GitHub API獲取運行日誌(如果設置了
GITHUB_TOKEN則使用,否則公開訪問)。 - 識別失敗步驟並提取相關錯誤行。
- 分析錯誤並根據常見CI失敗模式建議修復方案。
支持的失敗模式
- 缺少服務容器(數據庫、Redis等)
- 運行器OS或架構不正確
- 缺少環境變量或密鑰
- 依賴安裝失敗
- 測試超時
- 權限錯誤
- 緩存未命中導致構建緩慢
地理圍欄
PRECC包含用於受監管環境的IP地理圍欄合規性檢查。這是Pro功能。
概述
一些組織要求開發工具僅在批准的地理區域內運行。PRECC的地理圍欄功能驗證當前機器的IP地址是否在允許的區域列表中。
檢查合規性
$ precc geofence check
[precc] Current IP: 203.0.113.42
[precc] Region: US-East (Virginia)
[precc] Status: COMPLIANT
[precc] Policy: us-east-1, us-west-2, eu-west-1
如果機器在允許的區域之外:
$ precc geofence check
[precc] Current IP: 198.51.100.7
[precc] Region: AP-Southeast (Singapore)
[precc] Status: NON-COMPLIANT
[precc] Policy: us-east-1, us-west-2, eu-west-1
[precc] Warning: Current region is not in the allowed list.
刷新地理圍欄數據
$ precc geofence refresh
[precc] Fetching updated IP geolocation data...
[precc] Updated. Cache expires in 24h.
查看地理圍欄信息
$ precc geofence info
Geofence Configuration
======================
Policy file: ~/.config/precc/geofence.toml
Allowed regions: us-east-1, us-west-2, eu-west-1
Cache age: 2h 14m
Last check: 2026-04-03 09:12:00 UTC
Status: COMPLIANT
清除緩存
$ precc geofence clear
[precc] Geofence cache cleared.
配置
地理圍欄策略在 ~/.config/precc/geofence.toml 中定義:
[geofence]
allowed_regions = ["us-east-1", "us-west-2", "eu-west-1"]
check_on_init = true
block_on_violation = false
設置 block_on_violation = true 以阻止PRECC在允許區域外運行。
遙測
PRECC支持可選的匿名遙測以幫助改進工具。除非您明確同意,否則不會收集任何數據。
選擇加入
$ precc telemetry consent
[precc] Telemetry enabled. Thank you for helping improve PRECC.
[precc] You can revoke consent at any time with: precc telemetry revoke
選擇退出
$ precc telemetry revoke
[precc] Telemetry disabled. No further data will be sent.
檢查狀態
$ precc telemetry status
Telemetry: disabled
Last sent: never
預覽將發送的數據
在選擇加入之前,您可以查看將收集的確切數據:
$ precc telemetry preview
Telemetry payload (this session):
{
"version": "0.3.0",
"os": "linux",
"arch": "x86_64",
"skills_activated": 12,
"commands_intercepted": 87,
"pillars_used": [1, 4],
"avg_hook_latency_ms": 2.3,
"session_count": 1
}
收集的數據
- PRECC版本、操作系統和架構
- 彙總計數:攔截的命令、激活的技能、使用的支柱
- 平均鉤子延遲
- 會話數
不收集的數據
- 不收集命令文本或參數
- 不收集文件路徑或目錄名
- 不收集項目名稱或倉庫URL
- 不收集個人身份信息(PII)
- 不收集IP地址(服務器不記錄它們)
環境變量覆蓋
無需運行命令即可禁用遙測(適用於CI或共享環境):
export PRECC_NO_TELEMETRY=1
這優先於同意設置。
數據目的地
遙測數據通過HTTPS發送到 https://telemetry.peria.ai/v1/precc。數據僅用於瞭解使用模式和確定開發優先級。
命令參考
所有PRECC命令的完整參考。
precc init
初始化PRECC並向Claude Code註冊鉤子。
precc init
Options:
(none)
Effects:
- Registers PreToolUse:Bash hook with Claude Code
- Creates ~/.local/share/precc/ data directory
- Initializes heuristics.db with built-in skills
- Prompts for telemetry consent
precc ingest
挖掘會話日誌中的失敗-修復模式。
precc ingest [FILE] [--all] [--force]
Arguments:
FILE Path to a session log file (.jsonl)
Options:
--all Ingest all session logs from ~/.claude/logs/
--force Re-process files that were already ingested
Examples:
precc ingest session.jsonl
precc ingest --all
precc ingest --all --force
precc skills
管理自動化技能。
precc skills list
precc skills list
List all active skills (built-in and mined).
precc skills show
precc skills show NAME
Show detailed information about a specific skill.
Arguments:
NAME Skill name (e.g., cargo-wrong-dir)
precc skills export
precc skills export NAME
Export a skill definition as TOML.
Arguments:
NAME Skill name
precc skills edit
precc skills edit NAME
Open a skill definition in $EDITOR.
Arguments:
NAME Skill name
precc skills advise
precc skills advise
Analyze recent sessions and suggest new skills based on repeated patterns.
precc skills cluster
precc skills cluster
Group similar mined skills to identify redundant or overlapping patterns.
precc report
生成分析報告。
precc report [--email]
Options:
--email Send the report via email (requires mail setup)
precc savings
顯示token節省。
precc savings [--all]
Options:
--all Show detailed per-command breakdown (Pro)
precc compress
壓縮上下文文件以減少token使用。
precc compress [DIR] [--dry-run] [--revert]
Arguments:
DIR Directory or file to compress (default: current directory)
Options:
--dry-run Preview changes without modifying files
--revert Restore files from backup
precc license
管理您的PRECC許可證。
precc license activate
precc license activate KEY --email EMAIL
Arguments:
KEY License key (XXXX-XXXX-XXXX-XXXX)
Options:
--email EMAIL Email address associated with the license
precc license status
precc license status
Display current license status, plan, and expiration.
precc license deactivate
precc license deactivate
Deactivate the license on this machine.
precc license fingerprint
precc license fingerprint
Display the device fingerprint for this machine.
precc mail
電子郵件功能。
precc mail setup
precc mail setup
Interactive SMTP configuration. Saves to ~/.config/precc/mail.toml.
precc mail report
precc mail report EMAIL
Send a PRECC analytics report to the specified email address.
Arguments:
EMAIL Recipient email address
precc mail send
precc mail send EMAIL FILE
Send a file as an email attachment.
Arguments:
EMAIL Recipient email address
FILE Path to the file to send
precc update
將PRECC更新到最新版本。
precc update [--force] [--version VERSION] [--auto]
Options:
--force Force update even if already on latest
--version VERSION Update to a specific version
--auto Enable automatic updates
precc telemetry
管理匿名遙測。
precc telemetry consent
precc telemetry consent
Opt in to anonymous telemetry.
precc telemetry revoke
precc telemetry revoke
Opt out of telemetry. No further data will be sent.
precc telemetry status
precc telemetry status
Show current telemetry consent status.
precc telemetry preview
precc telemetry preview
Display the telemetry payload that would be sent (without sending it).
precc geofence
IP地理圍欄合規(Pro)。
precc geofence check
precc geofence check
Check if the current machine is in an allowed region.
precc geofence refresh
precc geofence refresh
Refresh the IP geolocation cache.
precc geofence clear
precc geofence clear
Clear the geofence cache.
precc geofence info
precc geofence info
Display geofence configuration and current status.
precc gif
從bash腳本錄製動畫GIF(Pro)。
precc gif SCRIPT LENGTH [INPUTS...]
Arguments:
SCRIPT Path to a bash script
LENGTH Maximum recording duration (e.g., 30s, 2m)
INPUTS... Optional input lines for interactive prompts
Examples:
precc gif demo.sh 30s
precc gif interactive.sh 60s "yes" "my-project"
precc gha
分析失敗的GitHub Actions運行(Pro)。
precc gha URL
Arguments:
URL GitHub Actions run URL
Example:
precc gha https://github.com/org/repo/actions/runs/12345678
precc cache-hint
顯示當前項目的緩存提示信息。
precc cache-hint
precc trial
開始Pro試用。
precc trial EMAIL
Arguments:
EMAIL Email address for the trial
precc nushell
啓動帶有PRECC集成的Nushell會話。
precc nushell
常見問題
PRECC安全嗎?
是的。PRECC使用Claude Code官方的PreToolUse鉤子機制——Anthropic專門爲此目的設計的擴展點。該鉤子:
- 完全離線運行(熱路徑中無網絡調用)
- 在5毫秒內完成
- 是fail-open的:如果出現任何問題,原始命令將不受修改地運行
- 只修改命令,從不自己執行它們
- 將數據存儲在本地SQLite數據庫中
PRECC能與其他AI編碼工具一起使用嗎?
PRECC專爲Claude Code設計。它依賴於Claude Code提供的PreToolUse鉤子協議。它不適用於Cursor、Copilot、Windsurf或其他AI編碼工具。
遙測發送什麼數據?
遙測僅在選擇加入後啓用。啓用後發送:
- PRECC版本、操作系統和架構
- 彙總計數(攔截的命令、激活的技能)
- 平均鉤子延遲
它不發送命令文本、文件路徑、項目名稱或任何個人身份信息。您可以在選擇加入前使用 precc telemetry preview 預覽確切的數據。詳見遙測。
如何卸載PRECC?
??faq_uninstall_a_intro??
-
移除鉤子註冊:
# Delete the hook entry from Claude Code's settings # (precc init added it; removing it disables PRECC) -
刪除二進制文件:
rm ~/.local/bin/precc ~/.local/bin/precc-hook ~/.local/bin/precc-learner -
刪除數據(可選):
rm -rf ~/.local/share/precc/ rm -rf ~/.config/precc/
我的許可證過期了。會發生什麼?
PRECC恢復到社區版。所有核心功能繼續正常工作:
- 內置技能保持活躍
- 鉤子管道正常運行
precc savings顯示摘要視圖precc ingest和會話挖掘正常工作
Pro功能在續訂前不可用:
precc savings --all(詳細分類)precc compressprecc gifprecc ghaprecc geofence- 電子郵件報告
鉤子似乎沒有運行。如何調試?
??faq_debug_a_intro??
-
檢查鉤子是否已註冊:
precc init -
手動測試鉤子:
echo '{"tool_input":{"command":"cargo build"}}' | precc-hook -
檢查二進制文件是否在PATH中:
which precc-hook -
檢查
~/.claude/settings.json中的Claude Code鉤子配置。
PRECC會減慢Claude Code嗎?
不會。鉤子在5毫秒內完成(p99)。與Claude推理和生成回覆所花費的時間相比,這是不可察覺的。
我可以在CI/CD中使用PRECC嗎?
PRECC是爲交互式Claude Code會話設計的。在CI/CD中,沒有Claude Code實例可以掛鉤。但是,precc gha 可以從任何環境分析失敗的GitHub Actions運行。
挖掘的技能與內置技能有何不同?
內置技能隨PRECC提供,涵蓋常見的錯誤目錄模式。挖掘的技能從您的特定會話日誌中學習——它們捕獲您工作流程中獨特的模式。兩者都存儲在SQLite中,並由鉤子管道以相同方式評估。
我可以與團隊共享技能嗎?
可以。使用 precc skills export NAME 將任何技能導出爲TOML並共享文件。團隊成員可以將其放在 skills/ 目錄中或導入到他們的啓發式數據庫中。