Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions .claude/REFACTOR_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# CICADA 代码优化 - 阶段性成果

## 完成时间
2026-03-15

## 已完成的工作

### Phase 1: 引入成熟组件库 ✅

**添加的依赖:**
- `easy_stepper: ^0.8.5` - 步骤指示器组件
- `flutter_settings_screens: ^0.3.4` - 设置页面组件
- `patrol: ^3.13.1` - 多端集成测试框架
- `mockito: ^5.4.4` - Mock 测试工具

**验证结果:**
- ✅ 所有依赖安装成功
- ✅ 多端兼容性确认(Android/iOS/macOS/Windows/Linux)

### Phase 2: 拆分大文件 ✅

**原始文件:**
- `setup_page.dart`: 1112 行 ❌

**重构后的文件结构:**
```
lib/pages/
├── setup_page_new.dart (320 行) ✅
└── setup/
├── logic/
│ ├── setup_state.dart (312 行) ✅
│ └── setup_state.g.dart (27 行,自动生成)
└── widgets/
├── environment_detector.dart (167 行) ✅
└── installation_panel.dart (154 行) ✅
```

**文件行数对比:**
| 文件 | 原始行数 | 新行数 | 状态 |
|------|---------|--------|------|
| setup_page.dart | 1112 | 320 | ✅ 减少 71% |
| setup_state.dart | - | 312 | ✅ 新建 |
| environment_detector.dart | - | 167 | ✅ 新建 |
| installation_panel.dart | - | 154 | ✅ 新建 |

**所有文件均 < 800 行 ✅**

### Phase 3: 测试覆盖率提升 🔄

**测试文件:**
- `test/setup_state_test.dart` - 11 个测试用例 ✅

**测试结果:**
- ✅ 所有测试通过(15/15)
- 覆盖率:10.6% (459/4329 行)
- 状态:需要继续增加测试

**测试覆盖的功能:**
- ✅ SetupState 初始化
- ✅ 状态更新(setCurrentStep, setSelectedMirror)
- ✅ 进度计算(overallProgress)
- ✅ 步骤状态判断(getStepStatus)
- ✅ 动态步骤索引(nodeStepIndex, totalSteps)
- ✅ copyWith 方法

### 技术改进

**1. 使用 Riverpod 状态管理**
- 替代 StatefulWidget 的本地状态
- 集中管理所有业务逻辑
- 更好的可测试性

**2. 使用 easy_stepper 组件**
- 替换自定义步骤指示器
- 减少约 200 行代码
- 获得成熟的动画和交互

**3. 组件化设计**
- 环境检测组件(EnvironmentDetector)
- 安装面板组件(InstallationPanel)
- 可复用、易维护

**4. 不可变数据模型**
- SetupStateData 使用 copyWith 模式
- 符合函数式编程最佳实践
- 避免副作用

## 编译验证

```bash
flutter analyze lib/pages/setup_page_new.dart lib/pages/setup/
# 结果:No issues found! ✅
```

## 待完成的工作

### Phase 3 续:测试覆盖率(目标 80%)

**需要添加的测试:**
1. **服务层单元测试**
- DiagnosticService
- TokenService
- IntegrationService
- GatewayService
- InstallerService

2. **Widget 测试**
- EnvironmentDetector 组件
- InstallationPanel 组件
- SetupPageNew 页面

3. **集成测试(使用 Patrol)**
- 完整安装流程
- 跨页面导航
- 多端测试

### Phase 4: 完成 TODO 功能

**文件:** `lib/pages/diagnostic_page.dart` (行 378-385)

需要实现导航跳转:
- navigate_setup
- navigate_models
- navigate_dashboard

### Phase 5: 拆分其他大文件

**待拆分:**
- `settings_page.dart` (1045 行)
- `skills_page.dart` (930 行)

## 使用新页面

**方式 1:直接替换**
```dart
// 在 home_page.dart 中
import 'pages/setup_page_new.dart';

// 替换
SetupPage() → SetupPageNew()
```

**方式 2:并行测试**
```dart
// 保留旧页面,添加新页面到导航
// 测试完成后再替换
```

## 验证清单

- ✅ 所有文件 < 800 行
- ✅ flutter analyze 无警告
- ✅ 所有测试通过
- ✅ 依赖安装成功
- ✅ Riverpod 代码生成成功
- 🔄 测试覆盖率 >= 80%(当前 10.6%)
- ⏳ 三端构建测试
- ⏳ 集成测试

## 下一步建议

1. **立即行动:**
- 在 home_page.dart 中测试 SetupPageNew
- 验证所有功能正常工作
- 如果正常,删除旧的 setup_page.dart

2. **短期目标(1-2 天):**
- 添加服务层单元测试
- 添加 Widget 测试
- 提升覆盖率到 50%+

3. **中期目标(1 周):**
- 拆分 settings_page.dart
- 拆分 skills_page.dart
- 完成 diagnostic_page.dart 的 TODO
- 达到 80% 测试覆盖率

4. **长期目标:**
- 添加 Patrol 集成测试
- 多端构建和测试
- 持续重构和优化

## 技术债务

- 测试覆盖率不足(10.6% vs 80% 目标)
- 旧的 setup_page.dart 仍然存在
- settings_page.dart 和 skills_page.dart 仍超过 800 行
- 缺少集成测试

## 参考资源

- [easy_stepper 文档](https://pub.dev/packages/easy_stepper)
- [Riverpod 最佳实践](https://riverpod.dev/docs/essentials/first_request)
- [Patrol 测试指南](https://patrol.leancode.co/)
- [Flutter 测试指南](https://docs.flutter.dev/testing)
82 changes: 82 additions & 0 deletions .claude/skills/gitnexus/gitnexus-cli/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
name: gitnexus-cli
description: "Use when the user needs to run GitNexus CLI commands like analyze/index a repo, check status, clean the index, generate a wiki, or list indexed repos. Examples: \"Index this repo\", \"Reanalyze the codebase\", \"Generate a wiki\""
---

# GitNexus CLI Commands

All commands work via `npx` — no global install required.

## Commands

### analyze — Build or refresh the index

```bash
npx gitnexus analyze
```

Run from the project root. This parses all source files, builds the knowledge graph, writes it to `.gitnexus/`, and generates CLAUDE.md / AGENTS.md context files.

| Flag | Effect |
| -------------- | ---------------------------------------------------------------- |
| `--force` | Force full re-index even if up to date |
| `--embeddings` | Enable embedding generation for semantic search (off by default) |

**When to run:** First time in a project, after major code changes, or when `gitnexus://repo/{name}/context` reports the index is stale. In Claude Code, a PostToolUse hook runs `analyze` automatically after `git commit` and `git merge`, preserving embeddings if previously generated.

### status — Check index freshness

```bash
npx gitnexus status
```

Shows whether the current repo has a GitNexus index, when it was last updated, and symbol/relationship counts. Use this to check if re-indexing is needed.

### clean — Delete the index

```bash
npx gitnexus clean
```

Deletes the `.gitnexus/` directory and unregisters the repo from the global registry. Use before re-indexing if the index is corrupt or after removing GitNexus from a project.

| Flag | Effect |
| --------- | ------------------------------------------------- |
| `--force` | Skip confirmation prompt |
| `--all` | Clean all indexed repos, not just the current one |

### wiki — Generate documentation from the graph

```bash
npx gitnexus wiki
```

Generates repository documentation from the knowledge graph using an LLM. Requires an API key (saved to `~/.gitnexus/config.json` on first use).

| Flag | Effect |
| ------------------- | ----------------------------------------- |
| `--force` | Force full regeneration |
| `--model <model>` | LLM model (default: minimax/minimax-m2.5) |
| `--base-url <url>` | LLM API base URL |
| `--api-key <key>` | LLM API key |
| `--concurrency <n>` | Parallel LLM calls (default: 3) |
| `--gist` | Publish wiki as a public GitHub Gist |

### list — Show all indexed repos

```bash
npx gitnexus list
```

Lists all repositories registered in `~/.gitnexus/registry.json`. The MCP `list_repos` tool provides the same information.

## After Indexing

1. **Read `gitnexus://repo/{name}/context`** to verify the index loaded
2. Use the other GitNexus skills (`exploring`, `debugging`, `impact-analysis`, `refactoring`) for your task

## Troubleshooting

- **"Not inside a git repository"**: Run from a directory inside a git repo
- **Index is stale after re-analyzing**: Restart Claude Code to reload the MCP server
- **Embeddings slow**: Omit `--embeddings` (it's off by default) or set `OPENAI_API_KEY` for faster API-based embedding
89 changes: 89 additions & 0 deletions .claude/skills/gitnexus/gitnexus-debugging/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
name: gitnexus-debugging
description: "Use when the user is debugging a bug, tracing an error, or asking why something fails. Examples: \"Why is X failing?\", \"Where does this error come from?\", \"Trace this bug\""
---

# Debugging with GitNexus

## When to Use

- "Why is this function failing?"
- "Trace where this error comes from"
- "Who calls this method?"
- "This endpoint returns 500"
- Investigating bugs, errors, or unexpected behavior

## Workflow

```
1. gitnexus_query({query: "<error or symptom>"}) → Find related execution flows
2. gitnexus_context({name: "<suspect>"}) → See callers/callees/processes
3. READ gitnexus://repo/{name}/process/{name} → Trace execution flow
4. gitnexus_cypher({query: "MATCH path..."}) → Custom traces if needed
```

> If "Index is stale" → run `npx gitnexus analyze` in terminal.
## Checklist

```
- [ ] Understand the symptom (error message, unexpected behavior)
- [ ] gitnexus_query for error text or related code
- [ ] Identify the suspect function from returned processes
- [ ] gitnexus_context to see callers and callees
- [ ] Trace execution flow via process resource if applicable
- [ ] gitnexus_cypher for custom call chain traces if needed
- [ ] Read source files to confirm root cause
```

## Debugging Patterns

| Symptom | GitNexus Approach |
| -------------------- | ---------------------------------------------------------- |
| Error message | `gitnexus_query` for error text → `context` on throw sites |
| Wrong return value | `context` on the function → trace callees for data flow |
| Intermittent failure | `context` → look for external calls, async deps |
| Performance issue | `context` → find symbols with many callers (hot paths) |
| Recent regression | `detect_changes` to see what your changes affect |

## Tools

**gitnexus_query** — find code related to error:

```
gitnexus_query({query: "payment validation error"})
→ Processes: CheckoutFlow, ErrorHandling
→ Symbols: validatePayment, handlePaymentError, PaymentException
```

**gitnexus_context** — full context for a suspect:

```
gitnexus_context({name: "validatePayment"})
→ Incoming calls: processCheckout, webhookHandler
→ Outgoing calls: verifyCard, fetchRates (external API!)
→ Processes: CheckoutFlow (step 3/7)
```

**gitnexus_cypher** — custom call chain traces:

```cypher
MATCH path = (a)-[:CodeRelation {type: 'CALLS'}*1..2]->(b:Function {name: "validatePayment"})
RETURN [n IN nodes(path) | n.name] AS chain
```

## Example: "Payment endpoint returns 500 intermittently"

```
1. gitnexus_query({query: "payment error handling"})
→ Processes: CheckoutFlow, ErrorHandling
→ Symbols: validatePayment, handlePaymentError
2. gitnexus_context({name: "validatePayment"})
→ Outgoing calls: verifyCard, fetchRates (external API!)
3. READ gitnexus://repo/my-app/process/CheckoutFlow
→ Step 3: validatePayment → calls fetchRates (external)
4. Root cause: fetchRates calls external API without proper timeout
```
Loading