File Watching
File Watching
grepai watch is the core daemon that maintains your codebase index. It performs an initial full scan and then monitors for file changes in real-time.
Features
- Initial indexing: Full codebase scan on startup
- Real-time updates: Monitors filesystem for changes
- Incremental sync: Only re-indexes modified files
- Symbol extraction: Builds call graph data for
grepai trace - Debounced processing: Batches rapid changes efficiently
Quick Start
# Initialize grepai in your projectgrepai init
# Start the watcher daemongrepai watchOutput:
Starting grepai watch in /path/to/projectProvider: ollama (nomic-embed-text)Backend: gob
Performing initial scan...Indexing [================] 100% (245/245) src/auth/handler.goInitial scan complete: 245 files indexed, 1842 chunks created (took 45.2s)Building symbol index...Symbol index built: 3421 symbols extracted
Watching for changes... (Press Ctrl+C to stop)How It Works
+------------------+ +------------------+ +------------------+| Initial Scan | --> | File Watcher | --> | Index Update || (full index) | | (fsnotify) | | (incremental) |+------------------+ +------------------+ +------------------+ | v +------------------+ | Debouncing | | (500ms) | +------------------+- Initial scan: Compares disk state with existing index, removes obsolete entries, indexes new files
- File watching: Monitors filesystem events (create, modify, delete, rename)
- Debouncing: Batches rapid changes to avoid redundant indexing
- Atomic updates: Prevents duplicate vectors during updates
What Gets Indexed
The watcher indexes files with these extensions:
| Language | Extensions |
|---|---|
| Go | .go |
| JavaScript/TypeScript | .js, .jsx, .ts, .tsx |
| Python | .py |
| PHP | .php |
| Rust | .rs |
| C/C++ | .c, .cpp, .h, .hpp, .cc, .cxx, .hxx |
| Zig | .zig |
| Java | .java |
| Ruby | .rb |
What Gets Skipped
Files are skipped based on:
.gitignorepatterns: Automatically respected- Config ignore patterns: From
.grepai/config.yaml - Binary files: Non-text files are excluded
- Large files: Files exceeding size limits
Default ignore patterns:
scanner: ignore: - "*.min.js" - "*.min.css" - "vendor/" - "node_modules/" - ".git/" - "target/" - ".zig-cache/" - "zig-out/"Real-Time Updates
When files change, the watcher logs updates:
[MODIFY] src/auth/handler.goIndexed src/auth/handler.go (4 chunks)Extracted 12 symbols from src/auth/handler.go
[CREATE] src/api/routes.goIndexed src/api/routes.go (3 chunks)Extracted 8 symbols from src/api/routes.go
[DELETE] src/old/deprecated.goRemoved src/old/deprecated.go from indexSymbol Indexing
The watcher also builds a symbol index for call graph analysis:
- Symbols extracted: Functions, methods, classes, types
- References tracked: Function calls and references
- Used by:
grepai tracefor call graph analysis
See Call Graph Analysis for more details.
Configuration
Configure watcher behavior in .grepai/config.yaml:
# Chunking parameterschunking: size: 512 # Tokens per chunk overlap: 50 # Overlap for context
# Additional ignore patternsscanner: ignore: - "*.generated.go" - "dist/" - "build/"Persistence
The watcher periodically saves the index:
- Auto-save: Automatic persistence during operation
- Shutdown save: Clean save on Ctrl+C or SIGTERM
- Location:
.grepai/index.gob(or PostgreSQL)
Background Daemon Mode
Run the watcher as a background daemon with built-in lifecycle management:
# Start the watcher in backgroundgrepai watch --background
# Check if the watcher is runninggrepai watch --status
# Stop the background watcher gracefullygrepai watch --stopStarting the Daemon
$ grepai watch --backgroundBackground watcher started (PID 12345)Logs: /Users/you/Library/Logs/grepai/grepai-watch.log
Use 'grepai watch --status' to check statusUse 'grepai watch --stop' to stop the watcherThe daemon waits for full initialization (embedder connection, initial scan) before returning success.
Checking Status
$ grepai watch --statusStatus: runningPID: 12345Log directory: /Users/you/Library/Logs/grepaiLog file: /Users/you/Library/Logs/grepai/grepai-watch.logStopping the Daemon
$ grepai watch --stopStopping background watcher (PID 12345)...Background watcher stoppedThe daemon performs a graceful shutdown, persisting the index before exiting.
Log Locations
Logs are stored in OS-specific directories:
| Platform | Log Directory |
|---|---|
| Linux | ~/.local/state/grepai/logs/ (or $XDG_STATE_HOME) |
| macOS | ~/Library/Logs/grepai/ |
| Windows | %LOCALAPPDATA%\grepai\logs\ |
Use --log-dir to override:
grepai watch --background --log-dir /custom/pathgrepai watch --status --log-dir /custom/pathgrepai watch --stop --log-dir /custom/pathLog Management
Logs are not rotated automatically. To prevent disk usage growth:
# Truncate log file (macOS example)echo "" > ~/Library/Logs/grepai/grepai-watch.log
# Or set up logrotate (Linux) / newsyslog (macOS)PID File Management
The daemon uses PID files with file locking to:
- Prevent multiple watchers from starting simultaneously
- Automatically clean up stale PID files from crashed processes
- Detect if a watcher is already running
Alternative: Terminal Multiplexers
You can also use traditional tools if preferred:
# Using tmuxtmux new-session -d -s grepai 'grepai watch'
# Using screenscreen -dmS grepai grepai watchTroubleshooting
| Problem | Solution |
|---|---|
| High CPU usage | Check for too many file changes, review ignore patterns |
| Missing files | Check ignore patterns and file extensions |
| Index not updating | Check file permissions and watcher limits |
| Ollama connection failed | Ensure Ollama is running with the model loaded |
System Limits (Linux)
On Linux, you may need to increase inotify watchers for large projects:
# Check current limitcat /proc/sys/fs/inotify/max_user_watches
# Increase temporarilysudo sysctl fs.inotify.max_user_watches=524288
# Increase permanentlyecho "fs.inotify.max_user_watches=524288" | sudo tee -a /etc/sysctl.confUse Cases
Development Workflow
Keep the watcher running in a terminal while you code:
# Terminal 1: Watch for changesgrepai watch
# Terminal 2: Search as you needgrepai search "current feature implementation"CI/CD Integration
For CI environments, run a one-time index:
grepai watch &sleep 60 # Wait for initial indexinggrepai search "security vulnerabilities" --json --compactCommands Reference
grepai watch- Full CLI referencegrepai status- Check index statusgrepai init- Initialize configuration