Files
nexus/wiki/concepts/File-Watcher.md
2026-04-23 00:02:55 +08:00

56 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "File Watcher (Auto-Reindex)"
type: concept
tags: [automation, file-system, indexing, realtime]
sources: [semantic-memory-search]
last_updated: 2026-04-22
---
## Aliases
- File Watcher
- 文件监视器
- 文件监听
- Auto-Reindex
- 自动重建索引
## Definition
文件监视器是一种实时监控指定目录文件变化的机制,当文件被创建、修改或删除时,自动触发相应的索引更新操作。在语义搜索场景中,这意味着记忆文件的变更会即时反映到向量索引中,保持索引与源文档的同步,无需手动重新运行索引命令。
## How It Works
```
文件系统事件 → 检测到变更 → 触发回调:
- 文件创建 → 计算哈希Embedding存入向量数据库
- 文件修改 → 重新计算哈希,更新向量数据库记录
- 文件删除 → 从向量数据库移除对应记录
```
## Implementation Patterns
| 方式 | 工具 | 说明 |
|------|------|------|
| 轮询 | `watchdog` (Python) | 跨平台,跨语言,通用 |
| 系统事件 | inotify (Linux) / FSEvents (macOS) / ReadDirectoryChangesW (Windows) | 高效,仅 Linux/macOS/Windows 原生 |
| cron 批处理 | `*/5 * * * *` | 简单,不实时,适合低频场景 |
| Webhook | Git post-commit hook | 适合 Git 管理的文档 |
## Use Case in memsearch
memsearch 的 `memsearch watch` 命令使用文件监视器自动追踪记忆目录变化:
```bash
memsearch watch ~/path/to/your/memory/
# 持续监控,新增/修改文件自动触发增量索引
```
## Benefits
- **实时同步**:索引始终反映最新文档状态
- **零手动操作**:无需人工干预,忘记索引更新也不怕
- **节省成本**:基于 [[Content Hashing]] 的增量机制,仅处理实际变化部分
## Connections
- [[semantic-memory-search]] — 文件监视器是 memsearch 保持索引实时的核心功能
- [[memsearch]] — memsearch 内置文件监视器实现
- [[Content Hashing]] — 文件监视器的增量触发依赖内容哈希比对