关于 grepai-watch-daemon
这份技能指南介绍 `grepai watch` 命令及其守护进程,用于索引项目代码并保持索引更新。内容涵盖前台与后台运行、状态检查、配置和故障排查,适合管理代码索引的开发者。
主要功能
- 01扫描并索引项目源文件
- 02监控文件变更并更新索引
- 03以前台或后台模式运行
- 04查看状态、停止守护进程并检查日志
- 05配置防抖及工作区监控
适用场景
- 01创建初始代码索引
- 02在文件变更时保持索引更新
- 03管理多个项目的后台索引
- 04排查守护进程或文件监控问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ yoanbernabeu/ grepai-skills --skill grepai-watch-daemon安装后在 Claude Code 中输入/grepai-watch-daemon确认已加载。
详细介绍
GrepAI Watch 守护进程
本技能介绍 grepai watch 命令以及如何管理守护进程,以实现实时代码索引。
何时使用本技能
- 开始初始代码索引
- 设置实时文件监控
- 在后台运行守护进程
- 排查索引问题
Watch 守护进程的功能
Watch 守护进程会:
- 扫描项目中的所有源文件
- 分块,将代码切分为多个片段(约 512 个 token)
- 生成嵌入向量,使用你配置的提供商
- 存储向量,使用你配置的后端
- 监控文件的实时变化
- 更新文件发生变化时的索引
基本用法
前台启动监控
cd /your/project
grepai watch
输出:
🔍 GrepAI Watch
Scanning files...
Found 245 files
Processing chunks...
████████████████████████████████ 100%
Indexed 1,234 chunks
Watching for changes...
按 Ctrl+C 停止。
在后台启动
grepai watch --background
输出:
🔍 GrepAI Watch (background)
Daemon started with PID 12345
Log file: ~/.grepai/daemon.log
检查守护进程状态
grepai watch --status
输出:
✅ GrepAI Daemon Running
PID: 12345
Started: 2025-01-28 10:30:00
Project: /path/to/project
Statistics:
- Files indexed: 245
- Chunks: 1,234
- Last update: 2 minutes ago
停止守护进程
grepai watch --stop
输出:
✅ Daemon stopped (PID 12345)
命令参考
| 命令 | 描述 |
|---|---|
grepai watch | 在前台启动守护进程 |
grepai watch --background | 在后台启动守护进程 |
grepai watch --status | 检查守护进程状态 |
grepai watch --stop | 停止正在运行的守护进程 |
配置
Watch 设置
# .grepai/config.yaml
watch:
# Debounce delay in milliseconds
# Groups rapid file changes together
debounce_ms: 500
防抖说明
保存文件时,编辑器通常会在短时间内多次写入。防抖会等待变更稳定下来:
| 值 | 行为 |
|---|---|
100 | 响应更快,但重新索引次数更多 |
500 | 平衡(默认值) |
1000 | 响应较慢,但重新索引次数更少 |
初始索引
索引内容
守护进程会为符合以下条件的文件建立索引:
- 扩展名受支持(.go、.js、.ts、.py 等)
- 不在忽略模式中(node_modules、.git 等)
- 遵循
.gitignore
索引进度
大型代码库会显示进度:
Scanning files...
Found 10,245 files
Processing chunks...
████████████████░░░░░░░░░░░░░░░░ 50% (5,122/10,245)
索引耗时估算
| 代码库 | 文件数 | 耗时(Ollama) | 耗时(OpenAI) |
|---|---|---|---|
| 小型 | 100 | ~30s | ~10s |
| 中型 | 1,000 | ~5min | ~1min |
| 大型 | 10,000 | ~30min | ~5min |
实时监控
完成初始索引后,守护进程会监控以下变化:
- 文件创建
- 文件修改
- 文件删除
- 文件重命名
文件变化检测
使用操作系统原生的文件监控机制:
- macOS: FSEvents
- Linux: inotify
- Windows: ReadDirectoryChangesW
触发重新索引的操作
| 操作 | 结果 |
|---|---|
| 保存现有文件 | 重新嵌入文件片段 |
| 创建新文件 | 为新片段建立索引 |
| 删除文件 | 从索引中移除 |
| 重命名文件 | 更新路径,保留向量 |
后台守护进程管理
设置为系统启动时运行
macOS(launchd)
创建 ~/Library/LaunchAgents/com.grepai.watch.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.grepai.watch</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/grepai</string>
<string>watch</string>
</array>
<key>WorkingDirectory</key>
<string>/path/to/your/project</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
加载:
launchctl load ~/Library/LaunchAgents/com.grepai.watch.plist
Linux(systemd)
创建 ~/.config/systemd/user/grepai-watch.service:
[Unit]
Description=GrepAI Watch Daemon
After=network.target
[Service]
Type=simple
WorkingDirectory=/path/to/your/project
ExecStart=/usr/local/bin/grepai watch
Restart=always
[Install]
WantedBy=default.target
启用:
systemctl --user enable grepai-watch
systemctl --user start grepai-watch
检查日志
# Background daemon logs
tail -f ~/.grepai/daemon.log
# Or with systemd
journalctl --user -u grepai-watch -f
多个项目
每个项目运行一个守护进程
为每个项目分别运行守护进程:
# Terminal 1: Project A
cd /path/to/project-a
grepai watch --background
# Terminal 2: Project B
cd /path/to/project-b
grepai watch --background
使用工作区
适用于多项目设置:
grepai workspace create my-workspace
grepai workspace add my-workspace /path/to/project-a
grepai workspace add my-workspace /path/to/project-b
grepai watch --workspace my-workspace
故障排除
守护进程无法启动
❌ 问题:“另一个守护进程已在运行” ✅ 解决方案:
grepai watch --stop
grepai watch --background
❌ 问题:“找不到配置” ✅ **解决方案:**先初始化:
grepai init
grepai watch
❌ 问题:“嵌入器连接失败” ✅ **解决方案:**启动你的嵌入提供商:
ollama serve # For Ollama
索引问题
❌ **问题:**文件未建立索引 ✅ **解决方案:**检查配置中的忽略模式,并确认文件扩展名受支持
❌ **问题:**索引速度非常慢 ✅ 解决方案:
- 使用 OpenAI 以更快地生成云端嵌入向量
- 添加更多忽略模式
- 增大分块大小
❌ **问题:**索引似乎已过时 ✅ **解决方案:**清除并重新索引:
rm .grepai/index.gob
grepai watch
文件监控问题
❌ **问题:**未检测到变化 ✅ 解决方案:
- 减小 debounce_ms
- 检查 inotify 限制(Linux):
echo 65536 | sudo tee /proc/sys/fs/inotify/max_user_watches
最佳实践
- 在后台运行: 用于持续监控
- 在 monorepo 中使用工作区: 便于更好地组织
- 设置自动启动: launchd 或 systemd
- 定期检查日志: 监控错误
- 配置更改后重新索引: 尤其是在更改嵌入模型后
状态检查
定期健康检查:
grepai status
输出:
✅ GrepAI Status
Project: /path/to/project
Config: .grepai/config.yaml
Embedder: Ollama (nomic-embed-text)
Storage: GOB (.grepai/index.gob)
Index:
- Files: 245
- Chunks: 1,234
- Size: 12.5 MB
- Last updated: 2025-01-28 10:30:00
Daemon: Running (PID 12345)
输出格式
监控守护进程状态:
✅ Watch Daemon Active
Mode: Background
PID: 12345
Project: /path/to/project
Initial Index:
- Files scanned: 245
- Chunks created: 1,234
- Duration: 45s
Real-time Monitor:
- Debounce: 500ms
- Events processed: 23
- Last event: 5 minutes ago
Next steps:
- Run 'grepai search "query"' to search
- Run 'grepai watch --status' to check status
- Run 'grepai watch --stop' to stop daemon
评论
支持 Markdown