@figo5/diffguard

中文文档
0.1.3发布于 2026/08/08MITTypeScriptES Module近 30 天下载 6083

Audit code changes for security issues and unintended behavior changes using your own Anthropic API key.

原始使用文档
文档目录11 节
  1. Install
  2. Setup
  3. Usage
  4. In coding agents
  5. Exit codes
  6. Example
  7. Configuration
  8. How it works
  9. Privacy
  10. Roadmap
  11. License

DiffGuard

Audit code changes for security issues and unintended behavior changes using your own Anthropic API key. DiffGuard reads a git diff, sends it to Claude for review, and prints structured findings — or exits cleanly when nothing's wrong. Built for the terminal: works on any machine with git and Node, drops straight into CI and pre-commit hooks.

Install

npm install -g @figo5/diffguard

Setup

diffguard config

diffguard config walks you through choosing a provider (Anthropic, OpenAI, OpenRouter, Groq, Ollama, or a custom OpenAI-compatible base URL), picking a model, and storing your API key in the platform keychain. Afterwards, plain diffguard just works — no env vars needed. Non-interactive setup is available too:

diffguard config --provider=ollama --model=gemma4:e2b
diffguard config --show    # print the effective config (never the key)
diffguard config --reset   # clear the saved config

Note: diffguard config --key=... puts the key on the command line, where it's visible in shell history and to anyone who can list processes on the machine. Prefer the interactive wizard (diffguard config) or diffguard key set, which read the key from a hidden prompt or stdin instead.

You can still configure everything with env vars instead — see the Configuration table. Env vars always override the saved config file.

Usage

diffguard            # audit the working-tree diff
diffguard --staged   # audit the staged diff (pre-commit)
diffguard --json     # machine-readable output
diffguard config    # one-time setup: provider, model, and key

In coding agents

DiffGuard is available as an on-demand audit tool inside coding agents:

  • Pi — install the extension at ~/.pi/agent/extensions/pi.ts (or .pi/extensions/pi.ts).
  • MCP-capable agents (Claude Code, Codex, Cursor, Windsurf, Copilot, Cline) — run the diffguard-mcp stdio server and add it as an MCP server.

The agent calls the diffguard_audit tool with a scope argument:

ScopeAudits
working (default)uncommitted changes
stagedthe git index (pre-commit)
file:<path>a single file
repothe whole repository (security only)

The tool returns a text report plus structured findings. The API key is resolved from the agent's own credential (Pi), ANTHROPIC_API_KEY, or the platform keychain (diffguard key set). The key never enters the agent's context.

The agent layer reads the same saved config file as the CLI, so the provider and model you configure with diffguard config apply to Pi and MCP too. (Local providers like Ollama still need a key in the agent's environment — set ANTHROPIC_API_KEY to any value.)

Exit codes

CodeMeaning
0Clean — no findings
1Findings found
2Error (missing key, not a git repo, audit failure)

Use it as a pre-commit gate:

# .git/hooks/pre-commit, or via a pre-commit framework
diffguard --staged || exit 1

Example

$ diffguard
[security] high  src/auth.ts:42
  Credentials compared with == instead of a constant-time comparison.
  Fix: use crypto.timingSafeEqual.

2 findings (1 security, 1 behavior)

Configuration

Env varDefaultMeaning
ANTHROPIC_API_KEY(unset)API key. Checked first; falls back to the platform keychain. Not needed for local providers like Ollama.
DIFFGUARD_MODELclaude-sonnet-5Model used for audits
DIFFGUARD_SEVERITY_THRESHOLDmediumMinimum severity shown in human output (critical/high/medium/low/info)
DIFFGUARD_SECURITY_ENABLEDtrueAudit for security issues
DIFFGUARD_BEHAVIOR_ENABLEDtrueAudit for behavior changes
DIFFGUARD_MAX_DIFF_TOKENS8000Max diff size before truncation (audit becomes partial)
DIFFGUARD_PROVIDERanthropicProvider: anthropic or openai (OpenAI-compatible)
DIFFGUARD_BASE_URL(unset)Base URL for OpenAI-compatible providers (e.g. http://localhost:11434/v1)
~/.config/diffguard/config.json(unset)Saved settings from diffguard config (provider, model, base URL, needsKey). Env vars override it. Set XDG_CONFIG_HOME to relocate.

Precedence: env var > config file > default. The API key is never stored in the config file — it lives in the platform keychain (diffguard key set / diffguard config) or the ANTHROPIC_API_KEY env var. The CLI checks the env var first, then the keychain.

How it works

  1. DiffGuard collects the git diff and some lightweight repo context.
  2. It builds a prompt instructing Claude to return findings as strict JSON, and streams the response (cancel with Ctrl-C).
  3. The response is parsed and validated — malformed findings are dropped and counted, never silently accepted.
  4. Findings print as a grouped report, or as raw JSON with --json.

If the diff is empty, DiffGuard exits 0 without calling the API. If the diff is too large, it's truncated to DIFFGUARD_MAX_DIFF_TOKENS and the audit is marked partial.

Privacy

  • The diff you audit is sent to Anthropic to generate findings. Don't audit code you aren't comfortable sending to a third-party API.
  • DiffGuard makes no other network calls and collects no telemetry.

Roadmap

  • Pre-commit hook installer (diffguard install).
  • --path filtering, --fail-on <severity>.
  • Test-gap and license-compliance checks alongside security/behavior findings.

License

MIT — see LICENSE.

版本与发布记录

@figo5/diffguard 共发布 4 个版本,首次发布于 2026/08/07;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.1.3latest2026/08/083 个月前
0.1.22026/08/083 个月前
0.1.12026/08/083 个月前
0.1.02026/08/073 个月前

关于 @figo5/diffguard 的常见问题

@figo5/diffguard 是什么?

Audit code changes for security issues and unintended behavior changes using your own Anthropic API key.

如何安装 @figo5/diffguard?

在项目目录运行 npm install @figo5/diffguard,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 diffguard、diffguard-mcp,可以用 npx diffguard 直接运行。

@figo5/diffguard 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@figo5/diffguard 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@figo5/diffguard 可以商用吗?

@figo5/diffguard 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@figo5/diffguard 的下载量有多少?

根据 npm 官方下载统计,@figo5/diffguard 在 2026/10/15 – 2026/11/13 这 30 天内被下载 6,083 次,日均约 203 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@figo5/diffguard 还在维护吗?

最新版本 0.1.3 发布于 2026/08/08(3 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 4 个;首次发布于 2026/08/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。