|
| 1 | +# ContextAtlas 更新总结(2026-04-15) |
| 2 | + |
| 3 | +本轮更新围绕健康检查性能和自动化运维闭环展开: |
| 4 | + |
| 5 | +> 新增 `--quick` 健康检查模式(260x 提速)、幽灵索引目录清理命令、以及 post-checkout/post-merge git hooks 自动维护。 |
| 6 | +
|
| 7 | +## 一、为什么要更新 |
| 8 | + |
| 9 | +- `health:check` 对所有项目逐一初始化 VectorStore + 策略分析,在项目数较多时耗时显著,无法用于自动化流程 |
| 10 | +- 旧架构遗留、测试泄漏和临时仓库会导致 baseDir 下堆积无 `current` / `snapshots` 的幽灵索引目录 |
| 11 | +- `hub:cleanup-ghost` 只清理 Hub 注册表,不管文件系统层的幽灵目录 |
| 12 | +- 缺少开发者本地的自动维护机制,依赖手动执行清理命令 |
| 13 | + |
| 14 | +## 二、这次更新的目标 |
| 15 | + |
| 16 | +1. 新增 `health:check --quick` 快速模式,跳过 VectorStore / strategySummary / snapshot 扫描 |
| 17 | +2. 新增 `hub:cleanup-stale-indexes` 命令,清理无 `current` / `snapshots` 的遗留索引目录 |
| 18 | +3. 新增 post-checkout / post-merge git hooks,自动检测并修复索引健康 |
| 19 | +4. Hook 安装脚本,`pnpm install` 自动部署 |
| 20 | + |
| 21 | +## 三、如何更新 |
| 22 | + |
| 23 | +### 1. `--quick` 健康检查模式 |
| 24 | + |
| 25 | +`analyzeIndexHealth` 新增 `quick` 参数: |
| 26 | + |
| 27 | +- **无显式 projectIds**:跳过全部 snapshot 扫描,只查 queue + daemon(~0.36s) |
| 28 | +- **有显式 projectIds**:只扫描指定项目,跳过 VectorStore 和 strategySummary |
| 29 | +- CLI 新增 `--quick` 选项:`contextatlas health:check --quick --json` |
| 30 | + |
| 31 | +### 2. 幽灵索引目录清理 |
| 32 | + |
| 33 | +新增 `cleanupStaleIndexes()` 函数和 `hub:cleanup-stale-indexes` CLI 命令: |
| 34 | + |
| 35 | +- 扫描 baseDir 下所有项目目录 |
| 36 | +- 识别无 `current` 指针且无 `snapshots` 目录但有 `index.db` 的幽灵目录 |
| 37 | +- 支持 `--dry-run`(仅预览)和 `--json`(脚本消费) |
| 38 | +- 保护有 `current` 或 `snapshots` 的活跃项目 |
| 39 | + |
| 40 | +### 3. Git Hooks 自动维护 |
| 41 | + |
| 42 | +新增三层防护机制: |
| 43 | + |
| 44 | +| 层级 | 机制 | 效果 | |
| 45 | +|------|------|------| |
| 46 | +| 1 | 60s 防抖 | 快速切换分支不重复触发 | |
| 47 | +| 2 | `--quick` 模式 | health:check 仅查 queue + daemon(0.36s) | |
| 48 | +| 3 | 24h 清理节流 | 幽灵清理每天最多执行一次,先检查再清理 | |
| 49 | + |
| 50 | +Hook 行为: |
| 51 | + |
| 52 | +1. `health:check --quick --json` 检测健康状态 |
| 53 | +2. `healthy` → 跳过(0.36s 结束) |
| 54 | +3. `unhealthy` → 条件触发清理 + catalog 重建 |
| 55 | +4. 清理路径:先 `--dry-run` 检查幽灵数量,有才执行删除 |
| 56 | +5. 全程 `nohup` 后台执行,不阻塞任何 git 操作 |
| 57 | + |
| 58 | +### 4. 安装机制 |
| 59 | + |
| 60 | +- `scripts/git-hooks/post-checkout` + `scripts/git-hooks/post-merge`:hook 源文件 |
| 61 | +- `scripts/setup-hooks.sh`:安装脚本(复制 + chmod) |
| 62 | +- `package.json` → `"prepare"` 脚本:`pnpm install` 自动触发安装 |
| 63 | + |
| 64 | +## 四、解决了什么问题 |
| 65 | + |
| 66 | +| 问题 | 解决方案 | |
| 67 | +|------|----------| |
| 68 | +| health:check 全量扫描过慢 | `--quick` 模式跳过重操作,亚秒级完成 | |
| 69 | +| 幽灵索引目录堆积 | `hub:cleanup-stale-indexes` 一键清理 | |
| 70 | +| 开发者需手动维护 | post-checkout/post-merge 自动后台维护 | |
| 71 | +| 连续切换分支触发多次 | 60s 防抖 + nohup 后台 | |
| 72 | +| 清理每次都跑浪费时间 | 24h 节流 + dry-run 先检查 | |
| 73 | + |
| 74 | +## 五、边界、兼容性与当前状态 |
| 75 | + |
| 76 | +- `health:check` 不加 `--quick` 时行为不变(完整扫描) |
| 77 | +- `health:full` 不受影响 |
| 78 | +- Hook 安装幂等(覆盖写入 `.git/hooks/`) |
| 79 | +- Hook 在无 `contextatlas` CLI 的环境下静默跳过 |
| 80 | +- 新增 6 个测试(3 个 quick 模式 + 3 个 stale cleanup) |
| 81 | + |
| 82 | +## 六、涉及文件、命令与入口 |
| 83 | + |
| 84 | +新增文件: |
| 85 | + |
| 86 | +- `scripts/git-hooks/post-checkout` |
| 87 | +- `scripts/git-hooks/post-merge` |
| 88 | +- `scripts/setup-hooks.sh` |
| 89 | +- `tests/cleanup-stale-indexes.test.ts` |
| 90 | + |
| 91 | +修改文件: |
| 92 | + |
| 93 | +- `src/monitoring/indexHealth.ts`(`quick` 模式 + `cleanupStaleIndexes` 导出) |
| 94 | +- `src/cli/commands/opsHealth.ts`(`--quick` CLI 选项) |
| 95 | +- `src/cli/commands/hubProjects.ts`(`hub:cleanup-stale-indexes` 命令) |
| 96 | +- `tests/index-health.test.ts`(3 个新测试) |
| 97 | +- `package.json`(`prepare` 脚本) |
| 98 | + |
| 99 | +新 CLI 命令: |
| 100 | + |
| 101 | +```bash |
| 102 | +# 快速健康检查 |
| 103 | +contextatlas health:check --quick --json |
| 104 | + |
| 105 | +# 清理幽灵索引目录 |
| 106 | +contextatlas hub:cleanup-stale-indexes --dry-run --json # 预览 |
| 107 | +contextatlas hub:cleanup-stale-indexes --json # 执行 |
| 108 | +contextatlas hub:cleanup-stale-indexes # 文本报告 |
| 109 | +``` |
| 110 | + |
| 111 | +## 七、验证口径 |
| 112 | + |
| 113 | +```bash |
| 114 | +# 单元测试(15 个测试) |
| 115 | +node --import tsx --test tests/cleanup-stale-indexes.test.ts tests/index-health.test.ts |
| 116 | + |
| 117 | +# CLI 验证 |
| 118 | +contextatlas health:check --quick --json |
| 119 | +contextatlas hub:cleanup-stale-indexes --dry-run --json |
| 120 | + |
| 121 | +# Hook 端到端验证 |
| 122 | +git checkout -b test-branch && git checkout main && git branch -d test-branch |
| 123 | +cat ~/.cache/contextatlas/hooks.log |
| 124 | +``` |
0 commit comments