1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! Watch command for file-system monitoring of AI agent session directories
//!
//! Monitors agent session storage paths for new or modified files and
//! automatically harvests them into the chasm database.
use anyhow::{Context, Result};
use colored::Colorize;
use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::mpsc;
use std::time::{Duration, Instant};
use super::run::{resolve_agent, resolve_storage_path, auto_harvest_sessions, AGENTS};
/// Run the watch command — monitor agent session directories for changes
pub fn watch_cli(
agent: Option<&str>,
path: Option<&str>,
debounce_secs: u64,
no_harvest: bool,
verbose: bool,
) -> Result<()> {
let home = dirs::home_dir().context("Cannot determine home directory")?;
// Determine which paths to watch
let mut watch_paths: Vec<(String, PathBuf)> = Vec::new();
if let Some(custom_path) = path {
// User specified an explicit path
let p = PathBuf::from(custom_path);
if !p.exists() {
anyhow::bail!("Path does not exist: {}", custom_path);
}
watch_paths.push(("custom".to_string(), p));
} else if let Some(alias) = agent {
// Watch a specific agent
let config = resolve_agent(alias).ok_or_else(|| {
anyhow::anyhow!(
"Unknown agent '{}'. Run 'chasm list agents' to see available agents.",
alias
)
})?;
if let Some(p) = resolve_storage_path(&home, config) {
if p.exists() {
watch_paths.push((config.name.to_string(), p));
} else {
println!(
"{} {} storage path does not exist yet: {}",
"[!]".yellow(),
config.name,
config.storage_hint.dimmed()
);
println!(
"{} Launch the agent first with 'chasm run {}'",
"[i]".blue(),
alias
);
anyhow::bail!("No watchable paths found");
}
}
} else {
// Watch ALL agent session directories that exist
for config in AGENTS {
if config.harvestable {
if let Some(p) = resolve_storage_path(&home, config) {
if p.exists() {
watch_paths.push((config.name.to_string(), p));
} else if verbose {
println!(
" {} {} path not found: {}",
"~".dimmed(),
config.name,
config.storage_hint.dimmed()
);
}
}
}
}
}
if watch_paths.is_empty() {
println!(
"{} No agent session directories found to watch.",
"[!]".yellow()
);
println!(
"{} Launch an agent first with 'chasm run <agent>', or specify a path with --path.",
"[i]".blue()
);
return Ok(());
}
// Header
println!("{}", "=".repeat(70).cyan());
println!(
"{} Watching {} path(s) for session changes",
"[W]".magenta().bold(),
watch_paths.len()
);
println!("{}", "=".repeat(70).cyan());
println!();
for (name, path) in &watch_paths {
println!(
" {} {} → {}",
"◉".green(),
name.bold(),
path.display().to_string().dimmed()
);
}
println!();
if no_harvest {
println!(
"{} Dry-run mode — changes detected but not harvested",
"[i]".blue()
);
}
println!(
"{} Debounce interval: {}s",
"[i]".blue(),
debounce_secs
);
println!(
"{} Press {} to stop watching",
"[i]".blue(),
"Ctrl+C".bold()
);
println!();
// Set up file watcher
let (tx, rx) = mpsc::channel::<notify::Result<Event>>();
let mut watcher: RecommendedWatcher = Watcher::new(
tx,
Config::default().with_poll_interval(Duration::from_secs(2)),
)
.context("Failed to create file system watcher")?;
// Register all paths
for (name, path) in &watch_paths {
match watcher.watch(path, RecursiveMode::Recursive) {
Ok(()) => {
if verbose {
println!(
" {} Registered watcher for {} ({})",
"+".green(),
name,
path.display()
);
}
}
Err(e) => {
println!(
"{} Failed to watch {} ({}): {}",
"[!]".yellow(),
name,
path.display(),
e
);
}
}
}
// Event processing loop with debouncing
let debounce = Duration::from_secs(debounce_secs);
let mut pending_files: HashSet<PathBuf> = HashSet::new();
let mut last_event_time: Option<Instant> = None;
let mut total_harvested: usize = 0;
println!(
"{} Watching for changes...",
"[*]".blue()
);
println!();
loop {
match rx.recv_timeout(Duration::from_millis(500)) {
Ok(Ok(event)) => {
// Filter for create/modify events on files
let dominated = matches!(
event.kind,
EventKind::Create(_) | EventKind::Modify(_)
);
if dominated {
for path in event.paths {
if path.is_file() {
// Filter out non-session files (temp files, lock files, etc.)
if should_watch_file(&path) {
if verbose {
println!(
" {} {:?} → {}",
"△".yellow(),
event.kind,
path.display().to_string().dimmed()
);
}
pending_files.insert(path);
last_event_time = Some(Instant::now());
}
}
}
}
}
Ok(Err(e)) => {
if verbose {
println!(
"{} Watcher error: {}",
"[!]".yellow(),
e
);
}
}
Err(mpsc::RecvTimeoutError::Timeout) => {
// Check if we have pending files past the debounce window
}
Err(mpsc::RecvTimeoutError::Disconnected) => {
println!(
"{} Watcher disconnected, stopping...",
"[!]".yellow()
);
break;
}
}
// Process pending files if debounce period has elapsed
if !pending_files.is_empty() {
if let Some(last) = last_event_time {
if last.elapsed() >= debounce {
let files: Vec<PathBuf> = pending_files.drain().collect();
let count = files.len();
last_event_time = None;
let now = chrono::Local::now().format("%H:%M:%S");
println!(
"{} [{}] Detected {} new/modified file(s):",
"[+]".green().bold(),
now,
count
);
for f in &files {
if let Some(name) = f.file_name() {
println!(
" {} {}",
"+".green(),
name.to_string_lossy().dimmed()
);
}
}
if !no_harvest {
match auto_harvest_sessions(&files) {
Ok(n) => {
total_harvested += n;
println!(
"{} Harvested {} session(s) (total: {})",
"[+]".green().bold(),
n,
total_harvested
);
}
Err(e) => {
println!(
"{} Harvest failed: {}",
"[!]".yellow(),
e
);
}
}
} else {
println!(
"{} Dry-run — skipping harvest",
"[i]".blue()
);
}
println!();
}
}
}
}
Ok(())
}
/// Determine if a file is a session-related file worth watching
fn should_watch_file(path: &PathBuf) -> bool {
let name = match path.file_name() {
Some(n) => n.to_string_lossy().to_lowercase(),
None => return false,
};
// Skip temp/lock files
if name.starts_with('.')
|| name.ends_with(".tmp")
|| name.ends_with(".lock")
|| name.ends_with(".swp")
|| name.ends_with(".swo")
|| name == "desktop.ini"
|| name == "thumbs.db"
{
return false;
}
// Accept session-like files
name.ends_with(".json")
|| name.ends_with(".jsonl")
|| name.ends_with(".md")
|| name.ends_with(".txt")
|| name.ends_with(".yaml")
|| name.ends_with(".yml")
|| name.ends_with(".log")
|| name.ends_with(".db")
|| name.ends_with(".sqlite")
// Claude Code uses extensionless JSONL files in some cases
|| !name.contains('.')
}