batty-cli 0.11.63

Supervised agent execution for software teams
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! Task time estimation from telemetry — median cycle time by tag set.
//!
//! Queries completed task durations from the telemetry database, groups them
//! by tag set (from the board), and computes median cycle times. For in-progress
//! tasks, finds the best-matching tag set to estimate remaining time.

use std::collections::HashMap;
use std::path::Path;

use anyhow::Result;
use rusqlite::Connection;
use tracing::warn;

/// A completed task's cycle time (seconds) and associated tags.
#[derive(Debug, Clone)]
pub(crate) struct CompletedTaskSample {
    pub duration_secs: u64,
    pub tags: Vec<String>,
}

/// Estimated time for a single in-progress task.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum TaskEstimate {
    /// Estimated remaining seconds and total estimated seconds.
    Remaining {
        remaining_secs: i64,
        total_secs: u64,
    },
    /// No historical data to estimate from.
    NoData,
}

/// Load completed task cycle times from the telemetry database.
///
/// Returns tasks that have both `started_at` and `completed_at` set.
pub(crate) fn load_completed_samples(conn: &Connection) -> Result<Vec<(String, u64)>> {
    let mut stmt = conn.prepare(
        "SELECT task_id, completed_at - started_at
         FROM task_metrics
         WHERE started_at IS NOT NULL AND completed_at IS NOT NULL
           AND completed_at > started_at",
    )?;
    let rows = stmt
        .query_map([], |row| {
            let task_id: String = row.get(0)?;
            let duration: i64 = row.get(1)?;
            Ok((task_id, duration as u64))
        })?
        .collect::<std::result::Result<Vec<_>, _>>()?;
    Ok(rows)
}

/// Build completed task samples by joining telemetry durations with board tags.
///
/// `tag_map` maps task ID strings to their tag lists (from the board).
pub(crate) fn build_samples(
    durations: &[(String, u64)],
    tag_map: &HashMap<String, Vec<String>>,
) -> Vec<CompletedTaskSample> {
    durations
        .iter()
        .map(|(task_id, duration)| CompletedTaskSample {
            duration_secs: *duration,
            tags: tag_map.get(task_id).cloned().unwrap_or_default(),
        })
        .collect()
}

/// Compute median cycle time (seconds) for each unique tag set.
///
/// Tags are sorted and joined with "," as the key. Tasks with no tags
/// use the key `""` (empty string).
pub(crate) fn median_by_tag_set(samples: &[CompletedTaskSample]) -> HashMap<String, u64> {
    let mut grouped: HashMap<String, Vec<u64>> = HashMap::new();
    for sample in samples {
        let key = tag_set_key(&sample.tags);
        grouped.entry(key).or_default().push(sample.duration_secs);
    }

    let mut result = HashMap::new();
    for (key, mut durations) in grouped {
        durations.sort_unstable();
        let median = compute_median(&durations);
        result.insert(key, median);
    }
    result
}

/// Compute the global median across all samples (fallback when no tag match).
pub(crate) fn global_median(samples: &[CompletedTaskSample]) -> Option<u64> {
    if samples.is_empty() {
        return None;
    }
    let mut durations: Vec<u64> = samples.iter().map(|s| s.duration_secs).collect();
    durations.sort_unstable();
    Some(compute_median(&durations))
}

/// Estimate remaining time for an in-progress task.
///
/// Strategy: find median for exact tag set match, then fall back to global median.
/// `elapsed_secs` is how long the task has been running.
pub(crate) fn estimate_task(
    task_tags: &[String],
    elapsed_secs: u64,
    medians: &HashMap<String, u64>,
    fallback_median: Option<u64>,
) -> TaskEstimate {
    let key = tag_set_key(task_tags);

    // Try exact tag set match first.
    if let Some(&median) = medians.get(&key) {
        return TaskEstimate::Remaining {
            remaining_secs: median as i64 - elapsed_secs as i64,
            total_secs: median,
        };
    }

    // Fall back to global median.
    if let Some(median) = fallback_median {
        return TaskEstimate::Remaining {
            remaining_secs: median as i64 - elapsed_secs as i64,
            total_secs: median,
        };
    }

    TaskEstimate::NoData
}

/// Format a task estimate for display in the status table.
pub(crate) fn format_estimate(estimate: &TaskEstimate) -> String {
    match estimate {
        TaskEstimate::NoData => "n/a".to_string(),
        TaskEstimate::Remaining {
            remaining_secs,
            total_secs: _,
        } => {
            if *remaining_secs < 0 {
                let overdue = (-remaining_secs) as u64;
                format!("overdue +{}", format_duration(overdue))
            } else {
                format!("~{}", format_duration(*remaining_secs as u64))
            }
        }
    }
}

/// Build a tag-set-key map from loaded board tasks.
pub(crate) fn build_tag_map(project_root: &Path) -> HashMap<String, Vec<String>> {
    let tasks_dir = project_root
        .join(".batty")
        .join("team_config")
        .join("board")
        .join("tasks");
    if !tasks_dir.is_dir() {
        return HashMap::new();
    }

    let tasks = match crate::task::load_tasks_from_dir(&tasks_dir) {
        Ok(tasks) => tasks,
        Err(error) => {
            warn!(error = %error, "failed to load board tasks for estimation");
            return HashMap::new();
        }
    };

    tasks
        .into_iter()
        .map(|task| (task.id.to_string(), task.tags))
        .collect()
}

/// Compute ETA strings for a set of in-progress tasks.
///
/// Returns a map from task_id to formatted ETA string.
pub(crate) fn compute_etas(
    project_root: &Path,
    active_task_ids: &[(u32, u64)], // (task_id, elapsed_secs)
) -> HashMap<u32, String> {
    if active_task_ids.is_empty() {
        return HashMap::new();
    }

    let conn = match super::telemetry_db::open_readonly(project_root) {
        Ok(Some(conn)) => conn,
        Ok(None) | Err(_) => {
            return active_task_ids
                .iter()
                .map(|(id, _)| (*id, "n/a".to_string()))
                .collect();
        }
    };

    let durations = match load_completed_samples(&conn) {
        Ok(d) => d,
        Err(error) => {
            warn!(error = %error, "failed to load completed samples for estimation");
            return active_task_ids
                .iter()
                .map(|(id, _)| (*id, "n/a".to_string()))
                .collect();
        }
    };

    let tag_map = build_tag_map(project_root);
    let samples = build_samples(&durations, &tag_map);
    let medians = median_by_tag_set(&samples);
    let fallback = global_median(&samples);

    active_task_ids
        .iter()
        .map(|(task_id, elapsed)| {
            let tags = tag_map
                .get(&task_id.to_string())
                .cloned()
                .unwrap_or_default();
            let estimate = estimate_task(&tags, *elapsed, &medians, fallback);
            (*task_id, format_estimate(&estimate))
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

fn tag_set_key(tags: &[String]) -> String {
    let mut sorted = tags.to_vec();
    sorted.sort();
    sorted.join(",")
}

fn compute_median(sorted: &[u64]) -> u64 {
    let len = sorted.len();
    if len == 0 {
        return 0;
    }
    if len % 2 == 0 {
        (sorted[len / 2 - 1] + sorted[len / 2]) / 2
    } else {
        sorted[len / 2]
    }
}

fn format_duration(secs: u64) -> String {
    if secs < 60 {
        format!("{secs}s")
    } else if secs < 3600 {
        format!("{}m", secs / 60)
    } else {
        let hours = secs / 3600;
        let mins = (secs % 3600) / 60;
        if mins == 0 {
            format!("{hours}h")
        } else {
            format!("{hours}h{mins}m")
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn median_of_odd_count() {
        assert_eq!(compute_median(&[100, 200, 300]), 200);
    }

    #[test]
    fn median_of_even_count() {
        assert_eq!(compute_median(&[100, 200, 300, 400]), 250);
    }

    #[test]
    fn median_of_single() {
        assert_eq!(compute_median(&[42]), 42);
    }

    #[test]
    fn median_of_empty() {
        assert_eq!(compute_median(&[]), 0);
    }

    #[test]
    fn tag_set_key_sorts() {
        let tags = vec!["feature".into(), "daemon".into(), "bugfix".into()];
        assert_eq!(tag_set_key(&tags), "bugfix,daemon,feature");
    }

    #[test]
    fn tag_set_key_empty() {
        assert_eq!(tag_set_key(&[]), "");
    }

    #[test]
    fn format_duration_seconds() {
        assert_eq!(format_duration(45), "45s");
    }

    #[test]
    fn format_duration_minutes() {
        assert_eq!(format_duration(300), "5m");
    }

    #[test]
    fn format_duration_hours_and_minutes() {
        assert_eq!(format_duration(5400), "1h30m");
    }

    #[test]
    fn format_duration_exact_hours() {
        assert_eq!(format_duration(7200), "2h");
    }

    #[test]
    fn format_estimate_no_data() {
        assert_eq!(format_estimate(&TaskEstimate::NoData), "n/a");
    }

    #[test]
    fn format_estimate_remaining() {
        let est = TaskEstimate::Remaining {
            remaining_secs: 1800,
            total_secs: 3600,
        };
        assert_eq!(format_estimate(&est), "~30m");
    }

    #[test]
    fn format_estimate_overdue() {
        let est = TaskEstimate::Remaining {
            remaining_secs: -600,
            total_secs: 3600,
        };
        assert_eq!(format_estimate(&est), "overdue +10m");
    }

    #[test]
    fn estimate_task_exact_tag_match() {
        let mut medians = HashMap::new();
        medians.insert("bugfix,daemon".to_string(), 3600);
        let tags = vec!["daemon".to_string(), "bugfix".to_string()];
        let result = estimate_task(&tags, 1800, &medians, Some(7200));
        assert_eq!(
            result,
            TaskEstimate::Remaining {
                remaining_secs: 1800,
                total_secs: 3600,
            }
        );
    }

    #[test]
    fn estimate_task_falls_back_to_global() {
        let medians = HashMap::new(); // no match
        let tags = vec!["newfeature".to_string()];
        let result = estimate_task(&tags, 300, &medians, Some(1800));
        assert_eq!(
            result,
            TaskEstimate::Remaining {
                remaining_secs: 1500,
                total_secs: 1800,
            }
        );
    }

    #[test]
    fn estimate_task_no_data() {
        let medians = HashMap::new();
        let tags = vec!["newfeature".to_string()];
        let result = estimate_task(&tags, 300, &medians, None);
        assert_eq!(result, TaskEstimate::NoData);
    }

    #[test]
    fn estimate_task_overdue() {
        let mut medians = HashMap::new();
        medians.insert("bugfix".to_string(), 1000);
        let tags = vec!["bugfix".to_string()];
        let result = estimate_task(&tags, 2000, &medians, None);
        assert_eq!(
            result,
            TaskEstimate::Remaining {
                remaining_secs: -1000,
                total_secs: 1000,
            }
        );
    }

    #[test]
    fn build_samples_joins_tags() {
        let durations = vec![
            ("1".to_string(), 100),
            ("2".to_string(), 200),
            ("3".to_string(), 300),
        ];
        let mut tag_map = HashMap::new();
        tag_map.insert("1".to_string(), vec!["bugfix".into()]);
        tag_map.insert("2".to_string(), vec!["feature".into(), "daemon".into()]);
        // task 3 has no tags in the map

        let samples = build_samples(&durations, &tag_map);
        assert_eq!(samples.len(), 3);
        assert_eq!(samples[0].tags, vec!["bugfix"]);
        assert_eq!(samples[1].tags, vec!["feature", "daemon"]);
        assert!(samples[2].tags.is_empty());
    }

    #[test]
    fn median_by_tag_set_groups_correctly() {
        let samples = vec![
            CompletedTaskSample {
                duration_secs: 100,
                tags: vec!["bugfix".into()],
            },
            CompletedTaskSample {
                duration_secs: 300,
                tags: vec!["bugfix".into()],
            },
            CompletedTaskSample {
                duration_secs: 200,
                tags: vec!["bugfix".into()],
            },
            CompletedTaskSample {
                duration_secs: 1000,
                tags: vec!["feature".into()],
            },
        ];
        let medians = median_by_tag_set(&samples);
        assert_eq!(medians["bugfix"], 200); // median of [100, 200, 300]
        assert_eq!(medians["feature"], 1000); // single value
    }

    #[test]
    fn global_median_across_all_samples() {
        let samples = vec![
            CompletedTaskSample {
                duration_secs: 100,
                tags: vec![],
            },
            CompletedTaskSample {
                duration_secs: 500,
                tags: vec![],
            },
            CompletedTaskSample {
                duration_secs: 300,
                tags: vec![],
            },
        ];
        assert_eq!(global_median(&samples), Some(300));
    }

    #[test]
    fn global_median_empty() {
        assert_eq!(global_median(&[]), None);
    }

    #[test]
    fn load_completed_samples_from_telemetry() {
        let conn = super::super::telemetry_db::open_in_memory().unwrap();

        // Insert task_assigned and task_completed events to populate task_metrics.
        let mut assign = crate::team::events::TeamEvent::task_assigned("eng-1", "10");
        assign.ts = 1000;
        super::super::telemetry_db::insert_event(&conn, &assign).unwrap();

        let mut complete = crate::team::events::TeamEvent::task_completed("eng-1", Some("10"));
        complete.ts = 1600;
        super::super::telemetry_db::insert_event(&conn, &complete).unwrap();

        let samples = load_completed_samples(&conn).unwrap();
        assert_eq!(samples.len(), 1);
        assert_eq!(samples[0].0, "10");
        assert_eq!(samples[0].1, 600);
    }

    #[test]
    fn load_completed_samples_skips_incomplete() {
        let conn = super::super::telemetry_db::open_in_memory().unwrap();

        // Only assigned, never completed.
        let assign = crate::team::events::TeamEvent::task_assigned("eng-1", "11");
        super::super::telemetry_db::insert_event(&conn, &assign).unwrap();

        let samples = load_completed_samples(&conn).unwrap();
        assert!(samples.is_empty());
    }

    #[test]
    fn format_estimate_zero_remaining() {
        let est = TaskEstimate::Remaining {
            remaining_secs: 0,
            total_secs: 3600,
        };
        // Exactly on time — show ~0s.
        assert_eq!(format_estimate(&est), "~0s");
    }

    #[test]
    fn median_by_tag_set_empty_tags_use_empty_key() {
        let samples = vec![
            CompletedTaskSample {
                duration_secs: 500,
                tags: vec![],
            },
            CompletedTaskSample {
                duration_secs: 700,
                tags: vec![],
            },
        ];
        let medians = median_by_tag_set(&samples);
        assert_eq!(medians[""], 600); // median of [500, 700]
    }
}