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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
//! Grafana monitoring: bundled dashboard template, alert provisioning, and CLI commands.
//!
//! The JSON is compiled into the binary via `include_str!()` so `batty init`
//! can write it without needing a network fetch or external file.

use anyhow::{Context, Result, bail};
use serde_json::json;
use std::fs::OpenOptions;
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::process::{Command as ProcessCommand, Stdio};
use std::time::Duration;

/// Raw JSON for the Grafana dashboard template.
pub const DASHBOARD_JSON: &str = include_str!("grafana/dashboard.json");

/// Default Grafana port.
pub const DEFAULT_PORT: u16 = 3000;
/// Local webhook receiver used by the file-backed contact point.
pub const ALERT_WEBHOOK_PORT: u16 = 8787;

/// Expected row titles in the dashboard (used for validation).
pub const REQUIRED_ROWS: &[&str] = &[
    "Session Overview",
    "Pipeline Health",
    "Activity Over Time",
    "Agent Performance",
    "Event Breakdown",
    "Recent Activity",
    "Throughput Over Time",
    "Tact Engine",
    "Health Signals",
    "Board Health",
];

/// Expected alert names provisioned alongside the dashboard.
pub const REQUIRED_ALERTS: &[&str] = &[
    "Zero Activity",
    "Crash Spike",
    "Dispatch Starvation",
    "Merge Queue Depth",
    "Non-Engineer Stall SLO",
];

const ALERT_RULE_GROUP: &str = "batty-operational-anomalies";
const ALERT_FOLDER: &str = "Batty";
const ALERT_CONTACT_POINT: &str = "batty-file-log";

#[derive(Clone, Copy)]
struct AlertRuleDefinition {
    uid: &'static str,
    title: &'static str,
    severity: &'static str,
    window_secs: u64,
    for_duration: &'static str,
    threshold: f64,
    sql: &'static str,
    description: &'static str,
}

const ALERT_RULES: &[AlertRuleDefinition] = &[
    AlertRuleDefinition {
        uid: "batty_zero_activity",
        title: "Zero Activity",
        severity: "warning",
        window_secs: 30 * 60,
        for_duration: "5m",
        threshold: 0.5,
        sql: "SELECT CASE WHEN COUNT(*) = 0 THEN 1 ELSE 0 END AS value \
              FROM events \
              WHERE event_type IN ('task_assigned', 'task_completed') \
                AND timestamp BETWEEN $__from / 1000 AND $__to / 1000;",
        description: "No task assignment or completion activity in the last 30 minutes.",
    },
    AlertRuleDefinition {
        uid: "batty_crash_spike",
        title: "Crash Spike",
        severity: "critical",
        window_secs: 10 * 60,
        for_duration: "2m",
        threshold: 3.0,
        sql: "SELECT COUNT(*) AS value \
              FROM events \
              WHERE event_type = 'member_crashed' \
                AND timestamp BETWEEN $__from / 1000 AND $__to / 1000;",
        description: "More than three agent crashes in the last 10 minutes.",
    },
    AlertRuleDefinition {
        uid: "batty_dispatch_starvation",
        title: "Dispatch Starvation",
        severity: "warning",
        window_secs: 10 * 60,
        for_duration: "2m",
        threshold: 0.5,
        sql: "SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS value \
              FROM events \
              WHERE event_type = 'pipeline_starvation_detected' \
                AND timestamp BETWEEN $__from / 1000 AND $__to / 1000;",
        description: "Idle engineers outnumber runnable work and dispatch is starving the lane.",
    },
    AlertRuleDefinition {
        uid: "batty_merge_queue_depth",
        title: "Merge Queue Depth",
        severity: "warning",
        window_secs: 60 * 60,
        for_duration: "2m",
        threshold: 3.0,
        sql: "SELECT COUNT(*) AS value \
              FROM task_metrics tm \
              WHERE tm.completed_at IS NOT NULL \
                AND NOT EXISTS ( \
                    SELECT 1 \
                    FROM events e \
                    WHERE e.task_id = tm.task_id \
                      AND e.event_type IN ('task_auto_merged', 'task_manual_merged', 'task_reworked') \
                      AND e.timestamp >= tm.completed_at \
                );",
        description: "More than three completed tasks are still waiting for merge or rework.",
    },
    AlertRuleDefinition {
        uid: "batty_non_engineer_stall_slo",
        title: "Non-Engineer Stall SLO",
        severity: "warning",
        window_secs: 15 * 60,
        for_duration: "2m",
        threshold: 0.5,
        sql: "SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS value \
              FROM non_engineer_stall_metrics \
              WHERE last_seen_at BETWEEN $__from / 1000 AND $__to / 1000;",
        description: "Architect, manager, or daemon SLO stall pressure was recorded recently.",
    },
];

/// Write the bundled Grafana dashboard JSON to a file.
pub fn write_dashboard(path: &Path) -> anyhow::Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(path, DASHBOARD_JSON)?;
    Ok(())
}

/// Build the Grafana base URL from a port number.
pub fn grafana_url(port: u16) -> String {
    format!("http://localhost:{port}")
}

fn alert_webhook_url(port: u16) -> String {
    format!("http://127.0.0.1:{port}/grafana-alerts")
}

fn alerts_log_path(project_root: &Path) -> PathBuf {
    project_root.join(".batty").join("alerts.log")
}

fn orchestrator_alert_log_path(project_root: &Path) -> PathBuf {
    crate::team::orchestrator_log_path(project_root)
}

fn orchestrator_alert_ansi_log_path(project_root: &Path) -> PathBuf {
    crate::team::orchestrator_ansi_log_path(project_root)
}

fn project_alerting_dir(project_root: &Path) -> PathBuf {
    project_root.join(".batty").join("grafana").join("alerting")
}

fn grafana_provisioning_dir() -> Option<PathBuf> {
    if let Ok(dir) = std::env::var("BATTY_GRAFANA_PROVISIONING_DIR") {
        return Some(PathBuf::from(dir));
    }

    [
        "/opt/homebrew/etc/grafana/provisioning",
        "/usr/local/etc/grafana/provisioning",
        "/etc/grafana/provisioning",
    ]
    .iter()
    .map(PathBuf::from)
    .find(|path| path.exists())
}

fn provisioning_target_dir(project_root: &Path) -> PathBuf {
    grafana_provisioning_dir().unwrap_or_else(|| project_root.join(".batty").join("grafana"))
}

/// Install Grafana, provision alerting resources, and start the service.
pub fn setup(project_root: &Path, port: u16) -> Result<()> {
    println!("Installing Grafana via Homebrew...");
    run_cmd("brew", &["install", "grafana"])?;

    println!("Installing SQLite datasource plugin...");
    let plugin_result = ProcessCommand::new("grafana")
        .args([
            "cli",
            "--homepath",
            "/opt/homebrew/opt/grafana/share/grafana",
            "--pluginsDir",
            "/opt/homebrew/var/lib/grafana/plugins",
            "plugins",
            "install",
            "frser-sqlite-datasource",
        ])
        .status();
    if plugin_result.is_err() || !plugin_result.unwrap().success() {
        let _ = run_cmd(
            "grafana-cli",
            &["plugins", "install", "frser-sqlite-datasource"],
        );
    }

    println!("Provisioning alert rules and notification policy...");
    provision_alerting(project_root, ALERT_WEBHOOK_PORT)?;
    ensure_alert_webhook_running(project_root, ALERT_WEBHOOK_PORT)?;

    println!("Starting Grafana service...");
    run_cmd("brew", &["services", "start", "grafana"])?;

    println!("Waiting for Grafana to start...");
    for _ in 0..10 {
        std::thread::sleep(Duration::from_secs(1));
        if check_health(&format!("{}/api/health", grafana_url(port))).is_ok() {
            break;
        }
    }

    let _ = reload_alerting_provisioning(port);

    let db_path = project_root.join(".batty").join("telemetry.db");
    if db_path.exists() {
        provision_dashboard(project_root, port)?;
    } else {
        println!(
            "telemetry.db not found at {}. Alerting files were written, but dashboard import \
will complete after `batty start` creates the database.",
            db_path.display()
        );
    }

    println!("Grafana setup complete. Dashboard at {}", grafana_url(port));
    Ok(())
}

/// Provision a SQLite datasource and import the bundled dashboard for a project.
pub fn provision_dashboard(project_root: &Path, port: u16) -> Result<()> {
    let db_path = project_root.join(".batty").join("telemetry.db");
    if !db_path.exists() {
        bail!(
            "telemetry.db not found at {}. Run `batty start` first.",
            db_path.display()
        );
    }
    let base_url = grafana_url(port);

    let _ = ProcessCommand::new("sqlite3")
        .args([
            db_path.to_str().unwrap_or(""),
            "PRAGMA wal_checkpoint(TRUNCATE);",
        ])
        .status();

    println!("Creating SQLite datasource...");
    let ds_body = format!(
        r#"{{"name":"Batty Telemetry","uid":"batty-telemetry","type":"frser-sqlite-datasource","access":"proxy","jsonData":{{"path":"{}"}}}}"#,
        db_path.display()
    );
    let ds_result = ProcessCommand::new("curl")
        .args([
            "-sf",
            "-X",
            "POST",
            &format!("{base_url}/api/datasources"),
            "-H",
            "Content-Type: application/json",
            "-u",
            "admin:admin",
            "-d",
            &ds_body,
        ])
        .output();
    match ds_result {
        Ok(out) if out.status.success() => println!("Datasource created."),
        _ => println!("Datasource may already exist (continuing)."),
    }

    println!("Importing dashboard...");
    let dashboard_payload = format!(
        r#"{{"dashboard":{},"overwrite":true,"folderId":0}}"#,
        DASHBOARD_JSON
    );
    let tmp_file = std::env::temp_dir().join("batty-grafana-import.json");
    std::fs::write(&tmp_file, &dashboard_payload)?;
    let import_result = ProcessCommand::new("curl")
        .args([
            "-sf",
            "-X",
            "POST",
            &format!("{base_url}/api/dashboards/db"),
            "-H",
            "Content-Type: application/json",
            "-u",
            "admin:admin",
            "-d",
            &format!("@{}", tmp_file.display()),
        ])
        .output();
    let _ = std::fs::remove_file(&tmp_file);
    match import_result {
        Ok(out) if out.status.success() => {
            let body = String::from_utf8_lossy(&out.stdout);
            if body.contains("\"status\":\"success\"") {
                println!("Dashboard imported successfully.");
            } else {
                println!("Dashboard import response: {body}");
            }
        }
        _ => {
            println!("Dashboard import may have failed. Check Grafana UI.");
        }
    }

    let url = format!("{base_url}/d/batty-project");
    println!("Dashboard at: {url}");
    Ok(())
}

/// Check whether the Grafana server is reachable by hitting `/api/health`.
pub fn status(port: u16) -> Result<()> {
    let url = format!("{}/api/health", grafana_url(port));
    match check_health(&url) {
        Ok(body) => {
            println!("Grafana is running at {}", grafana_url(port));
            println!("{body}");
            Ok(())
        }
        Err(e) => bail!("Grafana is not reachable at {}: {e}", grafana_url(port)),
    }
}

/// Open the Grafana dashboard in the default browser.
pub fn open(port: u16) -> Result<()> {
    let url = grafana_url(port);
    open_browser(&url).context("failed to open browser")?;
    println!("Opened {url}");
    Ok(())
}

/// Run the local webhook receiver used by Grafana's webhook contact point.
pub fn run_alert_webhook(project_root: &Path, port: u16) -> Result<()> {
    let listener = TcpListener::bind(("127.0.0.1", port))
        .with_context(|| format!("failed to bind Grafana alert webhook on port {port}"))?;
    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                if let Err(error) = handle_alert_webhook(stream, project_root) {
                    eprintln!("grafana alert webhook error: {error}");
                }
            }
            Err(error) => eprintln!("grafana alert webhook accept failed: {error}"),
        }
    }
    Ok(())
}

pub fn render_alert_rules_yaml() -> Result<String> {
    let rules = ALERT_RULES
        .iter()
        .map(|rule| {
            json!({
                "uid": rule.uid,
                "title": rule.title,
                "condition": "B",
                "data": [
                    {
                        "refId": "A",
                        "datasourceUid": "batty-telemetry",
                        "relativeTimeRange": {
                            "from": rule.window_secs,
                            "to": 0
                        },
                        "model": {
                            "datasource": {
                                "type": "frser-sqlite-datasource",
                                "uid": "batty-telemetry"
                            },
                            "intervalMs": 1000,
                            "maxDataPoints": 43200,
                            "queryText": rule.sql,
                            "queryType": "table",
                            "rawQueryText": rule.sql,
                            "rawSql": rule.sql,
                            "refId": "A"
                        }
                    },
                    {
                        "refId": "B",
                        "datasourceUid": "__expr__",
                        "relativeTimeRange": {
                            "from": rule.window_secs,
                            "to": 0
                        },
                        "model": {
                            "conditions": [
                                {
                                    "evaluator": {
                                        "params": [rule.threshold],
                                        "type": "gt"
                                    },
                                    "operator": {
                                        "type": "and"
                                    },
                                    "query": {
                                        "params": ["A"]
                                    },
                                    "reducer": {
                                        "type": "last"
                                    },
                                    "type": "query"
                                }
                            ],
                            "datasource": {
                                "type": "__expr__",
                                "uid": "__expr__"
                            },
                            "expression": "A",
                            "intervalMs": 1000,
                            "maxDataPoints": 43200,
                            "refId": "B",
                            "type": "classic_conditions"
                        }
                    }
                ],
                "noDataState": "OK",
                "execErrState": "Alerting",
                "for": rule.for_duration,
                "annotations": {
                    "summary": rule.description
                },
                "labels": {
                    "severity": rule.severity,
                    "team": "batty"
                }
            })
        })
        .collect::<Vec<_>>();

    serde_yaml::to_string(&json!({
        "apiVersion": 1,
        "groups": [
            {
                "orgId": 1,
                "name": ALERT_RULE_GROUP,
                "folder": ALERT_FOLDER,
                "interval": "60s",
                "rules": rules
            }
        ]
    }))
    .context("failed to render Grafana alert rules YAML")
}

fn render_contact_points_yaml(port: u16) -> Result<String> {
    serde_yaml::to_string(&json!({
        "apiVersion": 1,
        "contactPoints": [
            {
                "orgId": 1,
                "name": ALERT_CONTACT_POINT,
                "receivers": [
                    {
                        "uid": "batty_file_log",
                        "type": "webhook",
                        "disableResolveMessage": false,
                        "settings": {
                            "url": alert_webhook_url(port),
                            "httpMethod": "POST",
                            "title": "{{ template \"default.title\" . }}",
                            "message": "{{ template \"default.message\" . }}"
                        }
                    }
                ]
            }
        ]
    }))
    .context("failed to render Grafana contact points YAML")
}

fn render_notification_policies_yaml() -> Result<String> {
    serde_yaml::to_string(&json!({
        "apiVersion": 1,
        "policies": [
            {
                "orgId": 1,
                "receiver": ALERT_CONTACT_POINT,
                "group_by": ["alertname", "severity"],
                "group_wait": "30s",
                "group_interval": "5m",
                "repeat_interval": "4h"
            }
        ]
    }))
    .context("failed to render Grafana notification policy YAML")
}

fn provision_alerting(project_root: &Path, port: u16) -> Result<()> {
    let project_dir = project_alerting_dir(project_root);
    std::fs::create_dir_all(&project_dir)?;
    if let Some(parent) = alerts_log_path(project_root).parent() {
        std::fs::create_dir_all(parent)?;
    }
    let _ = OpenOptions::new()
        .create(true)
        .append(true)
        .open(alerts_log_path(project_root))?;

    let rules_yaml = render_alert_rules_yaml()?;
    let contact_points_yaml = render_contact_points_yaml(port)?;
    let policies_yaml = render_notification_policies_yaml()?;

    write_alerting_file(&project_dir.join("rules.yaml"), &rules_yaml)?;
    write_alerting_file(
        &project_dir.join("contact-points.yaml"),
        &contact_points_yaml,
    )?;
    write_alerting_file(&project_dir.join("policies.yaml"), &policies_yaml)?;

    let target_dir = provisioning_target_dir(project_root).join("alerting");
    std::fs::create_dir_all(&target_dir)?;
    write_alerting_file(&target_dir.join("batty-rules.yaml"), &rules_yaml)?;
    write_alerting_file(
        &target_dir.join("batty-contact-points.yaml"),
        &contact_points_yaml,
    )?;
    write_alerting_file(&target_dir.join("batty-policies.yaml"), &policies_yaml)?;
    Ok(())
}

fn write_alerting_file(path: &Path, content: &str) -> Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(path, content)
        .with_context(|| format!("failed to write alerting file {}", path.display()))
}

fn ensure_alert_webhook_running(project_root: &Path, port: u16) -> Result<()> {
    if TcpStream::connect(("127.0.0.1", port)).is_ok() {
        return Ok(());
    }

    let current_exe = std::env::current_exe().context("failed to resolve current batty binary")?;
    ProcessCommand::new(current_exe)
        .args([
            "grafana-webhook",
            "--project-root",
            &project_root.display().to_string(),
            "--port",
            &port.to_string(),
        ])
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .context("failed to start Grafana webhook receiver")?;
    std::thread::sleep(Duration::from_millis(250));
    Ok(())
}

fn handle_alert_webhook(mut stream: TcpStream, project_root: &Path) -> Result<()> {
    stream.set_read_timeout(Some(Duration::from_secs(2)))?;
    let mut buf = Vec::new();
    let _ = stream.read_to_end(&mut buf);
    let request = String::from_utf8_lossy(&buf);
    let body = request
        .split_once("\r\n\r\n")
        .map(|(_, body)| body)
        .unwrap_or_default();
    let line = format_alert_notification(body);
    append_alert_logs(project_root, &line)?;
    if let Err(error) = maybe_send_telegram_alert(&line) {
        eprintln!("grafana alert telegram delivery failed: {error}");
    }
    stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok")?;
    Ok(())
}

fn append_alert_logs(project_root: &Path, line: &str) -> Result<()> {
    if let Some(parent) = alerts_log_path(project_root).parent() {
        std::fs::create_dir_all(parent)?;
    }
    let mut alerts = OpenOptions::new()
        .create(true)
        .append(true)
        .open(alerts_log_path(project_root))
        .with_context(|| {
            format!(
                "failed to open alert log at {}",
                alerts_log_path(project_root).display()
            )
        })?;
    writeln!(alerts, "{line}")?;

    let orchestrator_line = format!("alert: {line}");
    let plain_path = orchestrator_alert_log_path(project_root);
    let ansi_path = orchestrator_alert_ansi_log_path(project_root);
    let mut plain = crate::team::open_log_for_append(&plain_path)?;
    writeln!(plain, "{orchestrator_line}")?;
    let mut ansi = crate::team::open_log_for_append(&ansi_path)?;
    writeln!(ansi, "{orchestrator_line}")?;
    Ok(())
}

fn maybe_send_telegram_alert(message: &str) -> Result<()> {
    let bot_token = match std::env::var("BATTY_TELEGRAM_BOT_TOKEN") {
        Ok(token) => token,
        Err(_) => return Ok(()),
    };
    let chat_id = match std::env::var("BATTY_GRAFANA_ALERT_CHAT_ID")
        .or_else(|_| std::env::var("BATTY_TELEGRAM_ALERT_CHAT_ID"))
    {
        Ok(chat_id) => chat_id,
        Err(_) => return Ok(()),
    };

    let bot = crate::team::telegram::TelegramBot::new(bot_token, Vec::new());
    bot.send_message(&chat_id, message)
        .context("failed to send Grafana alert to Telegram")
}

fn format_alert_notification(body: &str) -> String {
    let ts = crate::team::now_unix();
    if let Ok(payload) = serde_json::from_str::<serde_json::Value>(body) {
        let status = payload
            .get("status")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown");
        let alerts = payload
            .get("alerts")
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default();
        let names = alerts
            .iter()
            .filter_map(|alert| {
                alert
                    .get("labels")
                    .and_then(|labels| labels.get("alertname"))
                    .and_then(|name| name.as_str())
            })
            .collect::<Vec<_>>();
        if !names.is_empty() {
            return format!(
                "[{ts}] status={status} alerts={} raw={}",
                names.join(","),
                body.trim()
            );
        }
    }
    format!("[{ts}] raw={}", body.trim())
}

fn reload_alerting_provisioning(port: u16) -> Result<()> {
    let output = ProcessCommand::new("curl")
        .args([
            "-sf",
            "-X",
            "POST",
            &format!(
                "{}/api/admin/provisioning/alerting/reload",
                grafana_url(port)
            ),
            "-u",
            "admin:admin",
        ])
        .output()
        .context("failed to run curl for alerting reload")?;
    if !output.status.success() {
        bail!(
            "Grafana alerting reload failed with status {}",
            output.status
        );
    }
    Ok(())
}

fn run_cmd(program: &str, args: &[&str]) -> Result<()> {
    let status = ProcessCommand::new(program)
        .args(args)
        .status()
        .with_context(|| format!("failed to run {program} — is it installed?"))?;
    if !status.success() {
        bail!("{program} exited with status {status}");
    }
    Ok(())
}

fn check_health(url: &str) -> Result<String> {
    let output = ProcessCommand::new("curl")
        .args(["-sf", url])
        .output()
        .context("failed to run curl")?;
    if !output.status.success() {
        bail!("HTTP request failed (status {})", output.status);
    }
    Ok(String::from_utf8_lossy(&output.stdout).to_string())
}

fn open_browser(url: &str) -> Result<()> {
    #[cfg(target_os = "macos")]
    let program = "open";
    #[cfg(target_os = "linux")]
    let program = "xdg-open";
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    let program = "open";

    run_cmd(program, &[url])
}

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

    #[test]
    fn dashboard_json_valid() {
        let parsed: serde_json::Value =
            serde_json::from_str(DASHBOARD_JSON).expect("dashboard.json must be valid JSON");
        assert!(parsed.is_object(), "root must be an object");
        assert!(parsed["panels"].is_array(), "panels must be an array");
    }

    #[test]
    fn dashboard_has_all_rows() {
        let parsed: serde_json::Value = serde_json::from_str(DASHBOARD_JSON).unwrap();
        let panels = parsed["panels"].as_array().unwrap();
        let row_titles: Vec<&str> = panels
            .iter()
            .filter(|p| p["type"].as_str() == Some("row"))
            .filter_map(|p| p["title"].as_str())
            .collect();

        for expected in REQUIRED_ROWS {
            assert!(
                row_titles.contains(expected),
                "missing row: {expected}. Found: {row_titles:?}"
            );
        }
    }

    #[test]
    fn required_alerts_match_rule_definitions() {
        let titles: Vec<&str> = ALERT_RULES.iter().map(|rule| rule.title).collect();
        for expected in REQUIRED_ALERTS {
            assert!(titles.contains(expected), "missing alert rule {expected}");
        }
        assert_eq!(titles.len(), REQUIRED_ALERTS.len());
    }

    #[test]
    fn alert_rules_yaml_contains_expected_rules() {
        let yaml = render_alert_rules_yaml().unwrap();
        for expected in REQUIRED_ALERTS {
            assert!(yaml.contains(expected), "missing {expected} from YAML");
        }
        assert!(yaml.contains("pipeline_starvation_detected"));
        assert!(yaml.contains("task_auto_merged"));
        assert!(yaml.contains("task_manual_merged"));
        assert!(yaml.contains("task_reworked"));
    }

    #[test]
    fn alert_contact_points_yaml_uses_local_webhook() {
        let yaml = render_contact_points_yaml(ALERT_WEBHOOK_PORT).unwrap();
        assert!(yaml.contains("type: webhook"));
        assert!(yaml.contains("http://127.0.0.1:8787/grafana-alerts"));
        assert!(yaml.contains(ALERT_CONTACT_POINT));
    }

    #[test]
    fn notification_formatting_extracts_alert_names() {
        let body = r#"{"status":"firing","alerts":[{"labels":{"alertname":"Crash Spike"}},{"labels":{"alertname":"Zero Activity"}}]}"#;
        let formatted = format_alert_notification(body);
        assert!(formatted.contains("status=firing"));
        assert!(formatted.contains("Crash Spike,Zero Activity"));
    }

    #[test]
    fn provision_alerting_writes_expected_files() {
        let tmp = tempfile::tempdir().unwrap();
        provision_alerting(tmp.path(), 9911).unwrap();

        assert!(tmp.path().join(".batty/alerts.log").exists());
        assert!(
            tmp.path()
                .join(".batty/grafana/alerting/rules.yaml")
                .exists()
        );
        assert!(
            tmp.path()
                .join(".batty/grafana/alerting/contact-points.yaml")
                .exists()
        );
        assert!(
            tmp.path()
                .join(".batty/grafana/alerting/policies.yaml")
                .exists()
        );
    }

    #[test]
    fn append_alert_logs_writes_alert_and_orchestrator_logs() {
        let tmp = tempfile::tempdir().unwrap();

        append_alert_logs(tmp.path(), "[1] status=firing alerts=Crash Spike").unwrap();

        let alerts = std::fs::read_to_string(tmp.path().join(".batty/alerts.log")).unwrap();
        assert!(alerts.contains("Crash Spike"));

        let orchestrator =
            std::fs::read_to_string(tmp.path().join(".batty/orchestrator.log")).unwrap();
        assert!(orchestrator.contains("alert: [1] status=firing alerts=Crash Spike"));

        let orchestrator_ansi =
            std::fs::read_to_string(tmp.path().join(".batty/orchestrator.ansi.log")).unwrap();
        assert!(orchestrator_ansi.contains("alert: [1] status=firing alerts=Crash Spike"));
    }

    #[test]
    fn dashboard_has_required_panels() {
        let parsed: serde_json::Value = serde_json::from_str(DASHBOARD_JSON).unwrap();
        let panels = parsed["panels"].as_array().unwrap();
        let titles: Vec<&str> = panels.iter().filter_map(|p| p["title"].as_str()).collect();

        let required = [
            "Total Events",
            "Tasks Completed",
            "Auto-Merged",
            "Discord Events Sent",
            "Merge Queue Depth",
            "Notification Delivery Latency",
            "Verification Pass Rate Over Time",
            "Agent Metrics",
            "Event Type Distribution",
            "Recent Completions",
            "Events Per Hour",
            "Average Cycle and Lead Time Per Hour",
            "Tasks Completed Per Hour",
            "Narration Rejection Rate Per Hour (%)",
            "Planning Cycles Triggered Per Hour",
            "Tasks Created Per Planning Cycle",
            "Planning Cycle Latency",
            "Planning Cycle Success vs Failure",
            "Narration Events Detected Per Agent",
            "Context Pressure Warnings Per Agent",
            "Board Task Count by Status Over Time",
            "Tasks Archived Per Hour",
            "Review Queue Depth",
            "Oldest Review Age",
            "Review Over SLO",
            "Review Disposition Latency",
        ];
        for expected in required {
            assert!(
                titles.contains(&expected),
                "missing panel: {expected}. Found: {titles:?}"
            );
        }
    }

    #[test]
    fn dashboard_uses_consistent_datasource_uid() {
        let parsed: serde_json::Value = serde_json::from_str(DASHBOARD_JSON).unwrap();
        let panels = parsed["panels"].as_array().unwrap();
        for panel in panels {
            if let Some(uid) = panel["datasource"]["uid"].as_str() {
                assert_eq!(
                    uid,
                    "batty-telemetry",
                    "panel '{}' has unexpected datasource uid: {uid}",
                    panel["title"].as_str().unwrap_or("?")
                );
            }
        }
    }

    #[test]
    fn dashboard_alert_count_matches_expected() {
        assert_eq!(REQUIRED_ALERTS.len(), ALERT_RULES.len());
    }

    #[test]
    fn dashboard_panel_count() {
        let parsed: serde_json::Value = serde_json::from_str(DASHBOARD_JSON).unwrap();
        let panels = parsed["panels"].as_array().unwrap();
        let non_row_panels: Vec<_> = panels
            .iter()
            .filter(|p| p["type"].as_str() != Some("row"))
            .collect();
        assert!(
            non_row_panels.len() >= 28,
            "expected at least 28 data panels, got {}",
            non_row_panels.len()
        );
    }

    #[test]
    fn write_dashboard_creates_file() {
        let dir = std::env::temp_dir().join("batty_grafana_test");
        let path = dir.join("dashboard.json");
        let _ = std::fs::remove_dir_all(&dir);

        write_dashboard(&path).expect("write_dashboard should succeed");
        assert!(path.exists());

        let content = std::fs::read_to_string(&path).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&content).unwrap();
        assert!(
            parsed["title"].as_str().is_some(),
            "dashboard must have a title"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn grafana_url_default_port() {
        assert_eq!(grafana_url(3000), "http://localhost:3000");
    }

    #[test]
    fn grafana_url_custom_port() {
        assert_eq!(grafana_url(9090), "http://localhost:9090");
    }

    #[test]
    fn default_port_is_3000() {
        assert_eq!(DEFAULT_PORT, 3000);
    }

    #[test]
    fn check_health_unreachable() {
        let result = check_health("http://localhost:1/api/health");
        assert!(result.is_err());
    }
}