1use crate::channels::tmux;
5use crate::cli;
6use crate::cli::ndjson::{self, Formatter};
7use crate::cli::task_ref;
8use crate::home;
9use crate::store::TaskStatus;
10use crate::tmux::TmuxManager;
11use std::collections::HashMap;
12use std::fs::File;
13use std::io::{IsTerminal, Read, Seek, SeekFrom, Write};
14use std::path::{Path, PathBuf};
15use std::time::{Duration, SystemTime};
16
17use tokio::time;
18
19use std::fs;
20use std::io;
21use std::mem;
22
23const BACKLOG_BYTES: u64 = 64 * 1024;
25
26pub(crate) struct Tail {
27 path: PathBuf,
28 offset: u64,
29 partial: Vec<u8>,
30 skip_first: bool,
31 formatter: Formatter,
32}
33
34impl Tail {
35 pub(crate) fn open(path: PathBuf, color: bool) -> Self {
36 let len = fs::metadata(&path).map_or(0, |m| m.len());
37 let offset = len.saturating_sub(BACKLOG_BYTES);
38 Self {
39 path,
40 offset,
41 partial: Vec::new(),
42 skip_first: offset > 0,
43 formatter: Formatter::new(color),
44 }
45 }
46
47 pub(crate) fn open_at_end(path: PathBuf) -> Self {
49 let offset = fs::metadata(&path).map_or(0, |m| m.len());
50 Self {
51 path,
52 offset,
53 partial: Vec::new(),
54 skip_first: false,
55 formatter: Formatter::new(false),
56 }
57 }
58
59 pub(crate) fn shrank(&self) -> bool {
61 fs::metadata(&self.path).is_ok_and(|m| m.len() < self.offset)
62 }
63
64 pub(crate) fn read_lines(&mut self) -> Vec<String> {
66 let mut buf = Vec::new();
67 let read = File::open(&self.path).and_then(|mut f| {
68 f.seek(SeekFrom::Start(self.offset))?;
69 f.read_to_end(&mut buf)
70 });
71 let Ok(n) = read else {
72 return vec![];
73 };
74
75 self.offset += n as u64;
76 self.partial.extend_from_slice(&buf);
77 let Some(end) = self.partial.iter().rposition(|&b| b == b'\n') else {
78 return vec![];
79 };
80
81 let rest = self.partial.split_off(end + 1);
82 let complete = mem::replace(&mut self.partial, rest);
83 let mut lines: Vec<String> = String::from_utf8_lossy(&complete)
84 .lines()
85 .map(String::from)
86 .collect();
87 if mem::take(&mut self.skip_first) && !lines.is_empty() {
89 lines.remove(0);
90 }
91
92 lines
93 }
94}
95
96pub async fn run(task_id: Option<&str>, formatted: bool) -> anyhow::Result<()> {
98 let color = formatted && io::stdout().is_terminal();
99 let watched = if let Some(id) = task_id {
100 println!("Watching task {id}. Ctrl+C to stop");
101 Some(Watched::resolve(id).await?)
102 } else {
103 println!("Watching all running agents. Ctrl+C to stop");
104 None
105 };
106
107 let mut tails: HashMap<String, Tail> = HashMap::new();
108 let mut colors: HashMap<String, &str> = HashMap::new();
109 let mut tag_width = 0;
110 let mut said_waiting = false;
111 loop {
112 let found = match &watched {
113 Some(task) => task.sources(),
114 None => session_sources().await,
115 };
116 if let (Some(task), true, false) = (&watched, found.is_empty(), said_waiting) {
117 println!("{}", task.nothing_yet());
118 said_waiting = true;
119 }
120
121 let live: Vec<String> = found.iter().map(|(key, _)| key.clone()).collect();
122 for (key, path) in found {
123 let tail = tails
124 .entry(key)
125 .or_insert_with(|| Tail::open(path.clone(), color));
126 if tail.path != path {
127 *tail = Tail::open(path, color);
128 }
129 }
130
131 for tail in tails.values_mut() {
133 let mut entries = Vec::new();
134 for line in tail.read_lines() {
135 if formatted {
136 entries.extend(tail.formatter.format(&line));
137 } else {
138 entries.push(line);
139 }
140 }
141 if entries.is_empty() {
142 continue;
143 }
144
145 let tag = tag(&tail.path);
147 tag_width = tag_width.max(tag.chars().count());
148 let next = TAG_COLORS[colors.len() % TAG_COLORS.len()];
149 let code = *colors.entry(key_of(&tail.path)).or_insert(next);
150 let gutter = |show: bool| {
151 let text = if show { tag.as_str() } else { "" };
152 let text = format!("{text:<tag_width$} │");
153 if color {
154 format!("\x1b[{code}m{text}\x1b[0m")
155 } else {
156 text
157 }
158 };
159 for line in tag_lines(&entries, formatted, gutter) {
160 println!("{line}");
161 }
162 }
163 tails.retain(|key, _| live.contains(key));
164
165 io::stdout().flush()?;
166 time::sleep(Duration::from_secs(1)).await;
167 }
168}
169
170struct Watched {
172 id: String,
173 status: String,
174 dirs: Vec<PathBuf>,
176}
177
178impl Watched {
179 async fn resolve(id: &str) -> anyhow::Result<Self> {
180 let store = cli::init_store().await?;
181 let id = id.strip_suffix("-review").unwrap_or(id);
183 let task = task_ref::resolve(&store, id).await?;
184 Ok(Self {
185 id: task.display_spec(),
186 status: task.status.clone(),
187 dirs: task_ref::state_dirs(&task)?,
188 })
189 }
190
191 fn sources(&self) -> Vec<(String, PathBuf)> {
192 newest_output(&self.dirs)
193 .map(|path| (self.id.clone(), path))
194 .into_iter()
195 .collect()
196 }
197
198 fn nothing_yet(&self) -> String {
200 if self.status != TaskStatus::InProgress.as_str() {
201 return format!(
202 "No output for task {} yet, waiting for it to start",
203 self.id
204 );
205 }
206
207 let searched: Vec<String> = self
208 .dirs
209 .iter()
210 .map(|dir| dir.join("attempts").display().to_string())
211 .collect();
212 format!(
213 "Task {} is in_progress but no output file was found under {}",
214 self.id,
215 searched.join(" or ")
216 )
217 }
218}
219
220async fn session_sources() -> Vec<(String, PathBuf)> {
222 let tmux = TmuxManager::new();
223 tmux::list_orch_sessions()
224 .await
225 .unwrap_or_default()
226 .into_iter()
227 .filter_map(|session| {
228 let (project, task) = tmux.parse_session_name(&session)?;
229 let path = session_output(&project, &task_dir_name(&task))?;
230 Some((session, path))
231 })
232 .collect()
233}
234
235fn task_dir_name(task: &str) -> String {
237 match task.strip_prefix("internal-") {
238 Some(rest) => format!("internal:{rest}"),
239 None => task.to_string(),
240 }
241}
242
243fn session_output(project: &str, name: &str) -> Option<PathBuf> {
245 let state = home::state_dir().ok()?;
246 let dirs: Vec<PathBuf> = subdirs(&state)
247 .iter()
248 .flat_map(|owner| subdirs(owner))
249 .filter(|repo| repo.file_name() == Some(project.as_ref()))
250 .map(|repo| repo.join("tasks").join(name))
251 .collect();
252 newest_output(&dirs)
253}
254
255pub(crate) fn newest_output(dirs: &[PathBuf]) -> Option<PathBuf> {
257 let mut best: Option<(SystemTime, PathBuf)> = None;
258 for dir in dirs {
259 for attempt in subdirs(&dir.join("attempts")) {
260 let output = attempt.join("output.json");
261 let Ok(modified) = output.metadata().and_then(|m| m.modified()) else {
262 continue;
263 };
264 if best.as_ref().is_none_or(|(t, _)| modified > *t) {
265 best = Some((modified, output));
266 }
267 }
268 }
269
270 best.map(|(_, p)| p)
271}
272
273fn subdirs(dir: &Path) -> Vec<PathBuf> {
274 fs::read_dir(dir)
275 .into_iter()
276 .flatten()
277 .flatten()
278 .map(|e| e.path())
279 .filter(|p| p.is_dir())
280 .collect()
281}
282
283const TAG_COLORS: [&str; 6] = ["36", "35", "33", "34", "96", "95"];
284
285fn tag_lines(entries: &[String], formatted: bool, gutter: impl Fn(bool) -> String) -> Vec<String> {
287 let mut out = Vec::new();
288 for entry in entries {
289 let step = !formatted || !entry.starts_with(ndjson::RESULT_PREFIX);
290 if formatted && step {
291 out.push(gutter(false));
292 }
293
294 for (i, line) in entry.lines().enumerate() {
295 out.push(format!("{} {line}", gutter(step && i == 0)));
296 }
297 }
298 out
299}
300
301fn tag(path: &Path) -> String {
303 match path_parts(path).as_slice() {
304 [attempt, _, task, _, repo] => {
305 let task = task.strip_prefix("internal:").unwrap_or(task);
306 format!("{repo}#{task} a{attempt}")
307 }
308 _ => path.display().to_string(),
309 }
310}
311
312fn key_of(path: &Path) -> String {
314 path.ancestors()
315 .nth(3)
316 .map_or_else(String::new, |p| p.display().to_string())
317}
318
319fn path_parts(path: &Path) -> Vec<String> {
320 path.iter()
321 .rev()
322 .skip(1)
323 .take(5)
324 .map(|p| p.to_string_lossy().into_owned())
325 .collect()
326}
327
328#[cfg(test)]
329mod tests {
330 use std::fs::OpenOptions;
331 use std::thread;
332
333 use super::*;
334
335 #[test]
336 fn tail_returns_only_complete_lines() {
337 let dir = tempfile::tempdir().unwrap();
338 let path = dir.path().join("output.json");
339 fs::write(&path, "one\ntw").unwrap();
340 let mut tail = Tail::open(path.clone(), false);
341 assert_eq!(tail.read_lines(), vec!["one"]);
342
343 let mut f = OpenOptions::new().append(true).open(&path).unwrap();
344 f.write_all(b"o\nthree\n").unwrap();
345 assert_eq!(tail.read_lines(), vec!["two", "three"]);
346 assert_eq!(tail.read_lines(), [] as [String; 0]);
347 }
348
349 #[test]
350 fn tail_skips_cut_line_in_backlog() {
351 let dir = tempfile::tempdir().unwrap();
352 let path = dir.path().join("output.json");
353 let long = "x".repeat(usize::try_from(BACKLOG_BYTES).unwrap());
354
355 fs::write(&path, format!("{long}\nlast\n")).unwrap();
356 assert_eq!(Tail::open(path, false).read_lines(), vec!["last"]);
357 }
358
359 #[test]
360 fn task_dir_name_restores_internal_colon() {
361 assert_eq!(task_dir_name("internal-8-review"), "internal:8-review");
362 assert_eq!(task_dir_name("3601"), "3601");
363 }
364
365 #[test]
366 fn in_progress_without_output_names_the_path() {
367 let watched = Watched {
368 id: "owner/repo#5".to_string(),
369 status: TaskStatus::InProgress.as_str().to_string(),
370 dirs: vec![
371 PathBuf::from("/state/owner/repo/tasks/5-review"),
372 PathBuf::from("/state/owner/repo/tasks/5"),
373 ],
374 };
375 let msg = watched.nothing_yet();
376 assert!(msg.contains("in_progress"), "{msg}");
377 assert!(msg.contains("/state/owner/repo/tasks/5/attempts"), "{msg}");
378 assert!(
379 msg.contains("/state/owner/repo/tasks/5-review/attempts"),
380 "{msg}"
381 );
382
383 let waiting = Watched {
384 status: TaskStatus::Routed.as_str().to_string(),
385 ..watched
386 };
387 let msg = waiting.nothing_yet();
388 assert!(msg.contains("waiting for it to start"), "{msg}");
389 assert!(!msg.contains("no output file was found"), "{msg}");
390 }
391
392 #[test]
393 fn newest_output_picks_the_latest_attempt() {
394 let dir = tempfile::tempdir().unwrap();
395 let task = dir.path().join("tasks").join("5");
396 for attempt in ["1", "2"] {
397 let d = task.join("attempts").join(attempt);
398 fs::create_dir_all(&d).unwrap();
399 fs::write(d.join("output.json"), attempt).unwrap();
400 thread::sleep(Duration::from_millis(10));
402 }
403 let found = newest_output(&[task.join("5-review"), task.clone()]).unwrap();
404 assert_eq!(found, task.join("attempts").join("2").join("output.json"));
405 assert!(newest_output(&[dir.path().join("missing")]).is_none());
406 }
407
408 #[test]
409 fn tag_drops_internal_prefix() {
410 let path = Path::new("/s/owner/bean/tasks/internal:189488-review/attempts/1/output.json");
411 assert_eq!(tag(path), "bean#189488-review a1");
412 assert_eq!(key_of(path), "/s/owner/bean/tasks/internal:189488-review");
413 }
414
415 #[test]
416 fn tag_lines_label_each_step_only() {
417 let entries = vec![
418 "⏺ Read(a.rs)".to_string(),
419 " ⎿ Read 3 lines".to_string(),
420 "⏺ Fixing it.\n Then test.".to_string(),
421 ];
422 let gutter = |show: bool| if show { "t1 │" } else { " │" }.to_string();
423 assert_eq!(
424 tag_lines(&entries, true, gutter),
425 vec![
426 " │",
427 "t1 │ ⏺ Read(a.rs)",
428 " │ ⎿ Read 3 lines",
429 " │",
430 "t1 │ ⏺ Fixing it.",
431 " │ Then test.",
432 ]
433 );
434 }
435
436 #[test]
437 fn tag_lines_raw_tags_every_line() {
438 let entries = vec!["{\"a\":1}".to_string(), "{\"b\":2}".to_string()];
439 let gutter = |show: bool| if show { "t │" } else { " │" }.to_string();
440 assert_eq!(
441 tag_lines(&entries, false, gutter),
442 vec!["t │ {\"a\":1}", "t │ {\"b\":2}"]
443 );
444 }
445}