greatnessinabox/drift
GitHub: greatnessinabox/drift
drift 是一款多语言代码库实时健康监控仪表盘,结合 AI 诊断与重构建议,帮助团队在终端和 CI 中持续追踪并改善代码质量。
Stars: 3 | Forks: 0
# drift
[](https://github.com/greatnessinabox/drift/actions/workflows/ci.yml)
[](https://github.com/greatnessinabox/drift)
[](https://pkg.go.dev/github.com/greatnessinabox/drift)
[](LICENSE)
[](https://github.com/greatnessinabox/drift/releases)
**具有 GitHub Copilot CLI 集成的实时多语言代码库健康仪表盘。**
drift 实时监控你的代码库,检测代码健康状况下降,并使用 AI 诊断问题并提出修复建议。你可以把它想象成代码库的 Datadog,就运行在你的终端里。
支持 **Go**、**TypeScript/JavaScript**、**Python**、**Rust**、**Java**、**Ruby**、**PHP** 和 **C#**,具备自动语言检测功能。

```
◆ DRIFT — codebase health monitor typescript · 84 files · 312 functions
┌──────────────────────────────────────────────────────────────┐
│ ████████████████████░░░░░░░░ 72/100 ▲ +3 ▁▃▅▆▇█▇▆▅▇ │
╰──────────────────────────────────────────────────────────────╯
┌─ COMPLEXITY ─────────────────┐┌─ DEPENDENCIES ───────────────┐
│ ✗ parser.ts:47 32 ││ ✓ express 4.21 current │
│ ⚠ transform.tsx:12 28 ││ ⚠ lodash 4.17 34d │
│ main.ts:8 12 ││ ✗ axios 1.6 180d │
╰──────────────────────────────╯╰──────────────────────────────╯
┌─ BOUNDARIES ─────────────────┐┌─ ACTIVITY ───────────────────┐
│ ✗ api → db (3 hits) ││ 14:32 parser.ts modified │
│ ✓ cmd → pkg ││ 14:31 package.json updated │
╰──────────────────────────────╯╰──────────────────────────────╯
[tab] navigate [d] diagnose [r] refresh [q] quit
```
## 功能
- **🤖 AI Agent 支持** — 支持 GitHub Copilot、Claude Code、Cursor、Aider 等(详见 [AI_AGENTS.md](.github/AI_AGENTS.md))
- **🌐 多语言** — 通过项目 manifest 文件自动检测 Go、TypeScript/JS、Python、Rust、Java、Ruby、PHP 和 C#
- **🎨 实时仪表盘** — 随着你编写代码实时更新的全屏 TUI
- **📈 Sparkline 趋势** — 通过内嵌图表可视化过去 10 次提交的健康指标
- **🔧 圈复杂度** — Go 使用完整的 AST 分析;其他语言使用启发式模式匹配
- **📦 依赖新鲜度** — 根据其对应的 registry 检查依赖项(Go proxy、npm、PyPI、crates.io、Maven Central、RubyGems、Packagist、NuGet)
- **🏗️ 架构边界** — 定义 import 规则并立即捕获违规行为
- **☠️ 死代码检测** — 查找调用者为零的导出函数
- **💬 AI 诊断** — 按 `d` 键即可通过 Claude 或 GPT-4o 获取 AI 驱动的分析
- **📊 健康评分** — 带有动画过渡效果的 0-100 加权评分
- **✅ 对 CI 友好** — `drift check` + GitHub Action 可用于自动生成 PR 评论
## 支持的语言
| 语言 | Manifest | 分析方式 | 依赖 Registry |
|----------|----------|----------|---------------------|
| Go | `go.mod` | 完整 AST (`go/ast`) | Go module proxy |
| TypeScript/JS | `package.json` | 启发式 regex | npm registry |
| Python | `pyproject.toml` / `requirements.txt` | 启发式 + 缩进 | PyPI |
| Rust | `Cargo.toml` | 启发式 regex | crates.io |
| Java | `pom.xml` / `build.gradle` | 启发式 regex | Maven Central |
| Ruby | `Gemfile` | 启发式 + def/end 追踪 | RubyGems |
| PHP | `composer.json` | 启发式 regex | Packagist |
| C# | `*.csproj` | 启发式 regex | NuGet |
drift 通过检查 manifest 文件来自动检测语言。你也可以在 `.drift.yaml` 中显式设置:
```
language: typescript # or "go", "python", "rust", "java", "ruby", "php", "csharp"
```
## 安装
```
go install github.com/greatnessinabox/drift/cmd/drift@latest
```
或者从源码构建:
```
git clone https://github.com/greatnessinabox/drift.git
cd drift
go build ./cmd/drift/
```
**macOS 注意事项:** 如果 macOS 因 Gatekeeper 警告而阻止 drift,请打开 **系统设置 > 隐私与安全性** 并点击 **仍要打开**,或者运行 `xattr -d com.apple.quarantine $(which drift)`。
## 快速开始
```
# 在任何受支持的项目中运行 live dashboard
cd your-project
drift
# 生成报告
drift report
# 检查健康状态 (用于 CI)
drift check --fail-under 70
# 🆕 使用 GitHub Copilot CLI 进行交互式修复
drift fix
# 使用自定义 agent 命令
copilot --agent drift-dev "analyze src/"
```
## 🤖 GitHub Copilot CLI 集成
drift 为 [GitHub Copilot CLI 挑战赛](https://dev.to/challenges/github-2026-01-21) 展示了三种强大的 GitHub Copilot CLI 集成模式。
### 交互式修复
使用 `drift fix` 获取由 AI 驱动的重构建议:
```
$ drift fix
🔍 Analyzing codebase... (Score: 78/100)
Found 3 issue(s) to fix:
1. [🔴 HIGH] model.Update() in app.go:126 (complexity: 25)
🤖 Asking GitHub Copilot for suggestions...
[Copilot provides detailed refactoring with code examples]
Apply this suggestion? [y/N/s(kip rest)] y
```
**前提条件:**
```
brew install copilot-cli
# 或:npm install -g @github/copilot
# 或:curl -fsSL https://gh.io/copilot-install | bash
```
### 自定义 Agent
在 Copilot CLI 中使用 `@drift` 命令:
```
copilot --agent drift-dev "analyze internal/analyzer/"
copilot --agent drift-dev "suggest-refactor complexFunction()"
copilot --agent drift-dev "explain complexity"
```
有关所有命令,请参见 [`.github/agents/drift-dev.agent.md`](.github/agents/drift-dev.agent.md)。
### GitHub Action
通过由 AI 驱动的 PR 评论,将 drift 添加到你的 CI pipeline 中:
```
# .github/workflows/drift.yml
name: Code Health
on: [pull_request]
jobs:
drift:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: go install github.com/greatnessinabox/drift/cmd/drift@latest
- run: drift check --fail-under 70
```
包含 Copilot 生成的 PR 摘要的完整示例,请参见 [`.github/workflows/drift-health.yml`](.github/workflows/drift-health.yml)。
## 配置
在你的项目根目录中创建一个 `.drift.yaml`:
```
# 语言 (留空 = 从 manifest 文件自动检测)
language: ""
# 要排除的目录
exclude:
- vendor
- node_modules
- .git
- __pycache__
- target
# Metric 权重 (总和必须为 1.0)
weights:
complexity: 0.30
deps: 0.20
boundaries: 0.20
dead_code: 0.15
coverage: 0.15
# Architecture 边界规则
boundaries:
- deny: "pkg/api -> internal/db"
- deny: "cmd -> internal/tui"
# AI 诊断 (可选)
ai:
provider: anthropic # or "openai"
model: "" # uses sensible defaults
# Thresholds
thresholds:
max_complexity: 15
max_stale_days: 90
min_score: 70
```
## AI 诊断
在仪表盘中按 `d` 键触发 AI 诊断。支持:
- **Anthropic Claude** (Sonnet 3.5, 3.7, Opus, Haiku) — 设置 `ANTHROPIC_API_KEY` 环境变量
- **OpenAI GPT-4o / o1** — 设置 `OPENAI_API_KEY` 环境变量
在 `.drift.yaml` 中配置:
```
ai:
provider: anthropic # or "openai"
model: "" # uses sensible defaults
```
AI 会分析你得分最低的指标,并提供带有代码片段的具体、可操作的建议。
**超越内置诊断:** drift 可以与任何 AI 编程助手配合使用。有关与 Claude Code、Cursor、Aider 等的工作流,请参见 [.github/AI_AGENTS.md](.github/AI_AGENTS.md)。
## 键盘快捷键
| 键位 | 操作 |
|-----|--------|
| `tab` | 在面板之间导航 |
| `shift+tab` | 向后导航 |
| `d` | 运行 AI 诊断 |
| `r` | 强制进行完整重新分析 |
| `q` / `ctrl+c` | 退出 |
| `esc` | 关闭诊断浮层 |
## 工作原理
1. **语言检测** — 检查 manifest 文件(`go.mod`、`package.json`、`Cargo.toml` 等)以确定项目语言
2. **分析引擎** — Go 项目获得完整的 AST 分析;其他语言使用基于启发式 regex 的模式匹配来分析复杂度、import 和死代码
3. **依赖检查器** — 读取特定语言的 manifest,并查询相应的 registry 以获取最新版本
4. **文件监视器** — 使用 `fsnotify` 配合 200ms 的 debounce,仅监视与检测到的语言扩展名匹配的文件
5. **历史分析器** — 使用 `go-git` 遍历提交历史并生成 sparkline 趋势
6. **健康评分** — 所有指标的加权平均值,具有可配置的阈值
7. **TUI** — 使用 [Bubble Tea](https://github.com/charmbracelet/bubbletea) 和 [Lip Gloss](https://github.com/charmbracelet/lipgloss) 构建,提供精美的终端体验
## 其他 CI 选项
使用 `snapshot` 命令进行高级工作流:
```
# 使用 JSON 输出的高级 CI 集成
- name: Check codebase health
run: |
go install github.com/greatnessinabox/drift/cmd/drift@latest
SCORE=$(drift snapshot | jq '.score.total')
LANG=$(drift snapshot | jq -r '.language')
echo "Language: $LANG, Score: $SCORE"
if (( $(echo "$SCORE < 70" | bc -l) )); then
echo "Health score $SCORE is below threshold"
exit 1
fi
```
## 构建所用技术
- [Bubble Tea](https://github.com/charmbracelet/bubbletea) — TUI 框架
- [Lip Gloss](https://github.com/charmbracelet/lipgloss) — 终端样式
- [Cobra](https://github.com/spf13/cobra) — CLI 框架
- [fsnotify](https://github.com/fsnotify/fsnotify) — 文件系统通知
- [go-git](https://github.com/go-git/go-git) — 用于历史趋势的 Git 仓库访问
- [Anthropic SDK](https://github.com/anthropics/anthropic-sdk-go) — Claude AI 集成
- [OpenAI SDK](https://github.com/openai/openai-go) — GPT 集成
## 许可证
MIT
标签:AI辅助编程, EVTX分析, Go语言, SOC Prime, 代码健康, 开发工具, 日志审计, 程序破解, 终端仪表盘