netsky 0.2.0

netsky CLI: the viable system launcher and subcommand dispatcher
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
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
//! `netsky tick ...` — status-tick controls + ticker driver.
//!
//! - `enable <secs>`: opt agent0 into watchdog-driven status ticks.
//! - `disable`:       remove the config, ticks paused.
//! - `request`:       drop a tick-request envelope into agent0's inbox.
//! - `ticker`:        infinite sleep-loop driver, runs in a tmux session.
//! - `ticker-start`:  idempotently spawn the `netsky-ticker` tmux session.

use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use chrono::{DateTime, TimeDelta, Utc};
use fs4::fs_std::FileExt;
use netsky_core::config::owner_name;
use netsky_core::consts::{
    ENV_TICKER_INTERVAL, NETSKY_BIN, TICK_INTERVAL_CONFIG, TICK_LAST_MARKER, TICK_MIN_INTERVAL_S,
    TICKER_INTERVAL_DEFAULT_S, TICKER_LOG_PATH, TICKER_LOG_ROTATE_BYTES, TICKER_SESSION, TMUX_BIN,
};
use netsky_core::paths::{agent0_inbox_dir, resolve_netsky_dir, ticker_missing_count_file};
use netsky_core::process::run_bounded;
use netsky_sh::tmux;
use serde_json::json;

use crate::cli::TickCommand;
use crate::observability;

const TICK_REQUEST_TEXT: &str = include_str!("../../prompts/tick-request.md");
const TICK_REQUEST_FROM: &str = "agentinfinity";
const ANALYTICS_REFRESH_LOCK_PATH: &str = "/tmp/netsky-analytics-refresh.lock";
const ANALYTICS_REFRESH_HOUR_PATH: &str = "/tmp/netsky-analytics-refresh-hour";
const ANALYTICS_REFRESH_COMMIT_MSG: &str = "chore(analytics): ticker refresh";
const ANALYTICS_REFRESH_STALE_AFTER_MINUTES: i64 = 15;
const ENV_ANALYTICS_WEBSITE_DIR: &str = "NETSKY_ANALYTICS_WEBSITE_DIR";
const ANALYTICS_COMMAND_TIMEOUT_S: u64 = 60;
const GIT_OPERATION_TIMEOUT_S: u64 = 30;
const GIT_STDERR_REDACTION_TOKENS: [&str; 6] = [
    "ghp_",
    "gho_",
    "github_pat_",
    "://x-access-token:",
    "://oauth2:",
    "credential-helper",
];

pub fn run(sub: TickCommand) -> netsky_core::Result<()> {
    match sub {
        TickCommand::Enable { seconds } => enable(seconds),
        TickCommand::Disable => disable(),
        TickCommand::Request => request(),
        TickCommand::Ticker => ticker_loop(),
        TickCommand::Start => ticker_start(),
    }
}

fn enable(seconds: u64) -> netsky_core::Result<()> {
    if seconds < TICK_MIN_INTERVAL_S {
        netsky_core::bail!("interval must be >= {TICK_MIN_INTERVAL_S}s (got {seconds})");
    }
    atomic_write(Path::new(TICK_INTERVAL_CONFIG), &seconds.to_string())?;
    // Reset the last-tick marker so the next watchdog pass fires immediately
    // rather than honoring a stale interval from a prior session.
    let _ = fs::remove_file(TICK_LAST_MARKER);
    println!("[netsky tick enable] enabled at {seconds}s; next tick on next watchdog pass (~2min)");
    Ok(())
}

fn disable() -> netsky_core::Result<()> {
    if Path::new(TICK_INTERVAL_CONFIG).exists() {
        fs::remove_file(TICK_INTERVAL_CONFIG)?;
        println!("[netsky tick disable] disabled");
    } else {
        println!("[netsky tick disable] already disabled (no config file)");
    }
    Ok(())
}

pub(crate) fn request() -> netsky_core::Result<()> {
    let inbox = agent0_inbox_dir();
    fs::create_dir_all(&inbox)?;

    let now = SystemTime::now().duration_since(UNIX_EPOCH)?;
    let ts_iso = chrono::Utc::now()
        .format("%Y-%m-%dT%H:%M:%S%.6fZ")
        .to_string();
    let ts_ns = format!("{}{:09}", now.as_secs(), now.subsec_nanos());
    let path = inbox.join(format!("tick-{ts_ns}.json"));

    // The stored text is a single-line JSON string; escape newlines so the
    // envelope stays valid JSON even though the source template is multiline.
    // owner_name() consults env > netsky.toml [owner].name > OWNER_NAME_DEFAULT
    // per the config precedence brief section 4.
    let name = owner_name();
    let rendered = render_tick_request(TICK_REQUEST_TEXT, &name);
    let text_escaped = escape_json_string(rendered.trim());
    let envelope = format!(
        "{{\"from\":\"{TICK_REQUEST_FROM}\",\"text\":\"{text_escaped}\",\"ts\":\"{ts_iso}\"}}\n"
    );
    atomic_write(&path, &envelope)?;
    println!("[netsky tick request {ts_iso}] dropped {}", path.display());
    Ok(())
}

pub(crate) fn ticker_missing_count() -> u32 {
    fs::read_to_string(ticker_missing_count_file())
        .ok()
        .and_then(|s| s.trim().parse::<u32>().ok())
        .unwrap_or(0)
}

pub(crate) fn ticker_missing_record(count: u32) -> netsky_core::Result<()> {
    ticker_missing_record_at(&ticker_missing_count_file(), count)
}

pub(crate) fn ticker_missing_clear() -> netsky_core::Result<()> {
    ticker_missing_clear_at(&ticker_missing_count_file())
}

fn ticker_missing_record_at(path: &Path, count: u32) -> netsky_core::Result<()> {
    atomic_write(path, &count.to_string()).map_err(Into::into)
}

fn ticker_missing_clear_at(path: &Path) -> netsky_core::Result<()> {
    match fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(e) => Err(e.into()),
    }
}

fn ticker_loop() -> netsky_core::Result<()> {
    let interval = std::env::var(ENV_TICKER_INTERVAL)
        .ok()
        .and_then(|v| v.parse::<u64>().ok())
        .unwrap_or(TICKER_INTERVAL_DEFAULT_S);

    log_line(&format!(
        "[netsky-ticker] started at {}; interval={interval}s",
        chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ")
    ));

    loop {
        let watchdog = run_tick_subprocess(["watchdog", "tick"]);
        let cron = run_tick_subprocess(["cron", "tick"]);
        let loop_tick = run_tick_subprocess(["loop", "tick"]);
        let analytics = maybe_refresh_daily_analytics();
        observability::record_tick(
            "ticker",
            json!({
                "interval_s": interval,
                "watchdog": watchdog.detail,
                "cron": cron.detail,
                "loop": loop_tick.detail,
                "analytics": analytics,
            }),
        );
        report_tick_result("watchdog", &watchdog);
        report_tick_result("cron", &cron);
        report_tick_result("loop", &loop_tick);
        std::thread::sleep(Duration::from_secs(interval));
    }
}

struct TickSubprocessResult {
    detail: serde_json::Value,
    output: std::io::Result<std::process::Output>,
}

fn run_tick_subprocess(args: [&str; 2]) -> TickSubprocessResult {
    // Capture stdout+stderr so the unified watchdog log carries every
    // tick line, not just the ticker's own status messages.
    let output = Command::new(NETSKY_BIN).args(args).output();
    let detail = match &output {
        Ok(o) => json!({
            "status": o.status.code(),
            "ok": o.status.success(),
        }),
        Err(e) => json!({
            "status": "spawn-failed",
            "error": e.to_string(),
        }),
    };
    TickSubprocessResult { detail, output }
}

fn report_tick_result(label: &str, result: &TickSubprocessResult) {
    match &result.output {
        Ok(output) => {
            tee(&String::from_utf8_lossy(&output.stdout));
            tee(&String::from_utf8_lossy(&output.stderr));
            if !output.status.success() {
                log_line(&format!(
                    "[netsky-ticker] {label} returned non-zero at {}",
                    chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ")
                ));
            }
        }
        Err(e) => {
            log_line(&format!(
                "[netsky-ticker] {label} subprocess failed at {}: {e}",
                chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ")
            ));
        }
    }
}

fn maybe_refresh_daily_analytics() -> serde_json::Value {
    let now = Utc::now();
    let hour_key = now.format("%Y-%m-%dT%H").to_string();
    if !analytics_attempt_due(Path::new(ANALYTICS_REFRESH_HOUR_PATH), &hour_key) {
        return json!({
            "status": "skipped",
            "reason": "already-attempted-this-hour",
            "hour": hour_key,
        });
    }

    let website_dir = analytics_website_dir();
    if !website_dir.is_dir() {
        return json!({
            "status": "skipped",
            "reason": "website-dir-missing",
            "website_dir": website_dir.display().to_string(),
        });
    }

    let repo_root = match git_repo_root(&website_dir) {
        Ok(Some(root)) => root,
        Ok(None) => {
            return json!({
                "status": "skipped",
                "reason": "not-a-git-repo",
                "website_dir": website_dir.display().to_string(),
            });
        }
        Err(err) => {
            log_line(&format!(
                "[netsky-ticker] analytics refresh repo-root failed at {}: {err}",
                now.format("%Y-%m-%dT%H:%M:%SZ")
            ));
            return json!({
                "status": "error",
                "reason": "git-repo-root-failed",
                "error": err,
                "website_dir": website_dir.display().to_string(),
            });
        }
    };

    if let Err(err) = validate_analytics_repo_scope(&website_dir, &repo_root) {
        return json!({
            "status": "skipped",
            "reason": "repo-out-of-scope",
            "error": err,
            "website_dir": website_dir.display().to_string(),
            "repo_root": repo_root.display().to_string(),
        });
    };

    let date = now.date_naive();
    let page_rel = format!("website/content/analytics/{date}.md");
    let page_path = repo_root.join(&page_rel);
    let decision = analytics_refresh_decision_for_path(&page_path, now);
    if !decision.should_refresh {
        return finish_analytics_refresh(
            now,
            &hour_key,
            json!({
                "status": "skipped",
                "reason": "fresh",
                "page": page_path.display().to_string(),
                "generated_at": decision.generated_at,
            }),
        );
    }

    let _lock = match try_lock_analytics_refresh() {
        Ok(Some(lock)) => lock,
        Ok(None) => {
            return json!({
                "status": "skipped",
                "reason": "locked",
            });
        }
        Err(err) => {
            log_line(&format!(
                "[netsky-ticker] analytics refresh lock failed at {}: {err}",
                now.format("%Y-%m-%dT%H:%M:%SZ")
            ));
            return json!({
                "status": "error",
                "reason": "lock-failed",
                "error": err.to_string(),
            });
        }
    };

    let decision = analytics_refresh_decision_for_path(&page_path, Utc::now());
    if !decision.should_refresh {
        return finish_analytics_refresh(
            Utc::now(),
            &hour_key,
            json!({
                "status": "skipped",
                "reason": "fresh-after-lock",
                "page": page_path.display().to_string(),
                "generated_at": decision.generated_at,
            }),
        );
    }

    let analytics_output = match run_analytics_daily(&repo_root, &website_dir, &date.to_string()) {
        Ok(output) => output,
        Err(err) => {
            log_line(&format!(
                "[netsky-ticker] analytics refresh command failed at {}: {err}",
                Utc::now().format("%Y-%m-%dT%H:%M:%SZ")
            ));
            return json!({
                "status": "error",
                "reason": "analytics-command-failed",
                "error": err,
            });
        }
    };
    tee(&String::from_utf8_lossy(&analytics_output.stdout));
    tee(&String::from_utf8_lossy(&analytics_output.stderr));
    if !analytics_output.success() {
        log_line(&format!(
            "[netsky-ticker] analytics refresh command failed at {}",
            Utc::now().format("%Y-%m-%dT%H:%M:%SZ")
        ));
        return json!({
            "status": "error",
            "reason": "analytics-command-failed",
            "exit": analytics_output.status.and_then(|status| status.code()),
            "page": page_path.display().to_string(),
        });
    }

    let has_diff = match git_path_has_diff(&repo_root, &page_rel) {
        Ok(has_diff) => has_diff,
        Err(err) => {
            log_line(&format!(
                "[netsky-ticker] analytics refresh git status failed at {}: {err}",
                Utc::now().format("%Y-%m-%dT%H:%M:%SZ")
            ));
            return json!({
                "status": "error",
                "reason": "git-status-failed",
                "error": err,
                "page": page_path.display().to_string(),
            });
        }
    };
    if !has_diff {
        return finish_analytics_refresh(
            Utc::now(),
            &hour_key,
            json!({
                "status": "ok",
                "reason": "refreshed-no-diff",
                "page": page_path.display().to_string(),
            }),
        );
    }

    if let Err(err) = git_stage_path(&repo_root, &page_rel) {
        log_line(&format!(
            "[netsky-ticker] analytics refresh git add failed at {}: {err}",
            Utc::now().format("%Y-%m-%dT%H:%M:%SZ")
        ));
        return json!({
            "status": "error",
            "reason": "git-add-failed",
            "error": err,
        });
    }

    if let Err(err) = git_commit_refresh(&repo_root, &page_rel) {
        log_line(&format!(
            "[netsky-ticker] analytics refresh git commit failed at {}: {err}",
            Utc::now().format("%Y-%m-%dT%H:%M:%SZ")
        ));
        return json!({
            "status": "error",
            "reason": "git-commit-failed",
            "error": err,
        });
    }

    match git_push_main(&repo_root) {
        Ok(()) => finish_analytics_refresh(
            Utc::now(),
            &hour_key,
            json!({
                "status": "ok",
                "reason": "refreshed-and-pushed",
                "page": page_path.display().to_string(),
            }),
        ),
        Err(err) => {
            log_line(&format!(
                "[netsky-ticker] analytics refresh git push failed at {}: {err}",
                Utc::now().format("%Y-%m-%dT%H:%M:%SZ")
            ));
            json!({
                "status": "error",
                "reason": "git-push-failed",
                "error": err,
                "page": page_path.display().to_string(),
            })
        }
    }
}

fn analytics_website_dir() -> PathBuf {
    std::env::var_os(ENV_ANALYTICS_WEBSITE_DIR)
        .map(PathBuf::from)
        .unwrap_or_else(|| resolve_netsky_dir().join("website"))
}

fn analytics_attempt_due(path: &Path, hour_key: &str) -> bool {
    match fs::read_to_string(path) {
        Ok(existing) => existing.trim() != hour_key,
        Err(_) => true,
    }
}

fn analytics_refresh_decision_for_path(
    path: &Path,
    now: DateTime<Utc>,
) -> AnalyticsRefreshDecision {
    let body = match fs::read_to_string(path) {
        Ok(body) => body,
        Err(_) => {
            return AnalyticsRefreshDecision {
                should_refresh: true,
                generated_at: None,
            };
        }
    };
    analytics_refresh_decision(&body, now)
}

#[derive(Debug, PartialEq, Eq)]
struct AnalyticsRefreshDecision {
    should_refresh: bool,
    generated_at: Option<String>,
}

fn analytics_refresh_decision(markdown: &str, now: DateTime<Utc>) -> AnalyticsRefreshDecision {
    let generated_at = parse_generated_timestamp(markdown);
    let should_refresh = match generated_at.as_ref() {
        Some(ts) => {
            if *ts > now {
                true
            } else {
                now.signed_duration_since(*ts)
                    > TimeDelta::minutes(ANALYTICS_REFRESH_STALE_AFTER_MINUTES)
            }
        }
        None => true,
    };
    AnalyticsRefreshDecision {
        should_refresh,
        generated_at: generated_at.map(|ts| ts.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)),
    }
}

fn parse_generated_timestamp(markdown: &str) -> Option<DateTime<Utc>> {
    markdown.lines().find_map(|line| {
        line.trim()
            .strip_prefix("*generated ")
            .and_then(|rest| rest.strip_suffix('*'))
            .and_then(|ts| DateTime::parse_from_rfc3339(ts).ok())
            .map(|ts| ts.with_timezone(&Utc))
    })
}

fn try_lock_analytics_refresh() -> std::io::Result<Option<std::fs::File>> {
    let file = fs::OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(false)
        .open(ANALYTICS_REFRESH_LOCK_PATH)?;
    match file.try_lock_exclusive() {
        Ok(true) => Ok(Some(file)),
        Ok(false) => Ok(None),
        Err(err) => Err(err),
    }
}

fn finish_analytics_refresh(
    now: DateTime<Utc>,
    hour_key: &str,
    result: serde_json::Value,
) -> serde_json::Value {
    if !should_write_analytics_hour_marker(&result) {
        return result;
    }
    if let Err(err) = atomic_write(Path::new(ANALYTICS_REFRESH_HOUR_PATH), hour_key) {
        log_line(&format!(
            "[netsky-ticker] analytics refresh hour marker write failed at {}: {err}",
            now.format("%Y-%m-%dT%H:%M:%SZ")
        ));
        return json!({
            "status": "error",
            "reason": "hour-marker-write-failed",
            "error": err.to_string(),
        });
    }
    result
}

fn should_write_analytics_hour_marker(result: &serde_json::Value) -> bool {
    let status = result.get("status").and_then(|v| v.as_str());
    let reason = result.get("reason").and_then(|v| v.as_str());
    matches!(
        (status, reason),
        (Some("ok"), _)
            | (Some("skipped"), Some("fresh"))
            | (Some("skipped"), Some("fresh-after-lock"))
    )
}

fn git_repo_root(dir: &Path) -> Result<Option<PathBuf>, String> {
    let mut cmd = Command::new("git");
    cmd.args([
        "-C",
        &dir.display().to_string(),
        "rev-parse",
        "--show-toplevel",
    ]);
    let output = run_bounded_command(
        cmd,
        Duration::from_secs(GIT_OPERATION_TIMEOUT_S),
        "git rev-parse",
    )?;
    if !output.success() {
        return Ok(None);
    }
    let root = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if root.is_empty() {
        return Ok(None);
    }
    Ok(Some(PathBuf::from(root)))
}

fn validate_analytics_repo_scope(website_dir: &Path, repo_root: &Path) -> Result<(), String> {
    validate_analytics_repo_scope_at(&resolve_netsky_dir(), website_dir, repo_root)
}

fn validate_analytics_repo_scope_at(
    expected_repo_root: &Path,
    website_dir: &Path,
    repo_root: &Path,
) -> Result<(), String> {
    let expected_repo_root = fs::canonicalize(expected_repo_root).map_err(|_| {
        format!(
            "resolved netsky dir missing: {}",
            expected_repo_root.display()
        )
    })?;
    let expected_website_dir = expected_repo_root.join("website");
    let website_dir = fs::canonicalize(website_dir)
        .map_err(|err| format!("website dir canonicalize failed: {err}"))?;
    let repo_root = fs::canonicalize(repo_root)
        .map_err(|err| format!("repo root canonicalize failed: {err}"))?;
    if repo_root != expected_repo_root || website_dir != expected_website_dir {
        return Err(format!(
            "analytics refresh limited to {} (got repo={}, website={})",
            expected_website_dir.display(),
            repo_root.display(),
            website_dir.display(),
        ));
    }
    Ok(())
}

fn run_analytics_daily(
    repo_root: &Path,
    website_dir: &Path,
    date: &str,
) -> Result<netsky_core::process::BoundedOutput, String> {
    let mut cmd = Command::new(NETSKY_BIN);
    cmd.args([
        "analytics",
        "daily",
        "--for",
        date,
        "--website",
        &website_dir.display().to_string(),
    ])
    .current_dir(repo_root);
    run_bounded_command(
        cmd,
        Duration::from_secs(ANALYTICS_COMMAND_TIMEOUT_S),
        "netsky analytics daily",
    )
}

fn git_path_has_diff(repo_root: &Path, path: &str) -> Result<bool, String> {
    let stdout = run_git(
        repo_root,
        &["status", "--porcelain", "--", path],
        "git status",
    )?;
    Ok(!stdout.trim().is_empty())
}

fn git_stage_path(repo_root: &Path, path: &str) -> Result<(), String> {
    run_git(repo_root, &["add", "--", path], "git add").map(|_| ())
}

fn git_commit_refresh(repo_root: &Path, path: &str) -> Result<(), String> {
    let staged_paths = git_cached_paths(repo_root)?;
    if staged_paths != [path] {
        return Err(format!(
            "unexpected staged paths before analytics commit: {}",
            staged_paths.join(", ")
        ));
    }
    run_git(
        repo_root,
        &["commit", "-m", ANALYTICS_REFRESH_COMMIT_MSG, "--", path],
        "git commit",
    )
    .map(|_| ())
}

fn git_push_main(repo_root: &Path) -> Result<(), String> {
    run_git(repo_root, &["push", "origin", "main"], "git push").map(|_| ())
}

fn git_cached_paths(repo_root: &Path) -> Result<Vec<String>, String> {
    let stdout = run_git(
        repo_root,
        &["diff", "--cached", "--name-only"],
        "git diff --cached",
    )?;
    Ok(stdout
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty())
        .map(ToOwned::to_owned)
        .collect())
}

fn run_git(repo_root: &Path, args: &[&str], label: &str) -> Result<String, String> {
    let mut cmd = Command::new("git");
    cmd.args(["-C", &repo_root.display().to_string()])
        .args(args);
    let output = run_bounded_command(cmd, Duration::from_secs(GIT_OPERATION_TIMEOUT_S), label)?;
    let stdout = redact_sensitive_git_output(&String::from_utf8_lossy(&output.stdout));
    let stderr = redact_sensitive_git_output(&String::from_utf8_lossy(&output.stderr));
    if output.success() {
        return Ok(stdout.trim().to_string());
    }
    if !stderr.is_empty() {
        return Err(stderr.trim().to_string());
    }
    if !stdout.is_empty() {
        return Err(stdout.trim().to_string());
    }
    Err(format!(
        "{label} exited {:?}",
        output.status.and_then(|status| status.code())
    ))
}

fn run_bounded_command(
    cmd: Command,
    timeout: Duration,
    label: &str,
) -> Result<netsky_core::process::BoundedOutput, String> {
    let output = run_bounded(cmd, timeout).map_err(|err| err.to_string())?;
    if output.timed_out {
        return Err(format!("{label} timed out after {}s", timeout.as_secs()));
    }
    Ok(output)
}

fn redact_sensitive_git_output(text: &str) -> String {
    text.lines()
        .filter(|line| {
            !GIT_STDERR_REDACTION_TOKENS
                .iter()
                .any(|needle| line.contains(needle))
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn tee(text: &str) {
    for line in text.lines() {
        if line.is_empty() {
            continue;
        }
        log_line(line);
    }
}

pub(crate) fn ticker_start() -> netsky_core::Result<()> {
    if tmux::has_session(TICKER_SESSION) {
        let _ = ticker_missing_clear();
        println!("[ticker-start] session '{TICKER_SESSION}' already up; skipping spawn");
        return Ok(());
    }
    // Invoke our own binary from the session, not a bash script.
    let cmd = format!("{NETSKY_BIN} tick ticker");
    let status = Command::new(TMUX_BIN)
        .args(["new-session", "-d", "-s", TICKER_SESSION, &cmd])
        .status()?;
    if !status.success() {
        netsky_core::bail!("tmux new-session failed for '{TICKER_SESSION}'");
    }
    let _ = ticker_missing_clear();
    let interval = std::env::var(ENV_TICKER_INTERVAL)
        .unwrap_or_else(|_| TICKER_INTERVAL_DEFAULT_S.to_string());
    println!(
        "[ticker-start] spawned '{TICKER_SESSION}' — watchdog ticks every {interval}s (bash sleep-loop)"
    );
    Ok(())
}

fn atomic_write(target: &Path, content: &str) -> std::io::Result<()> {
    let tmp = target.with_extension("tmp");
    fs::write(&tmp, content)?;
    fs::rename(&tmp, target)
}

fn log_line(line: &str) {
    println!("{line}");
    maybe_rotate_log();
    // Best-effort append to the shared watchdog log.
    if let Ok(mut f) = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(TICKER_LOG_PATH)
    {
        let _ = writeln!(f, "{line}");
    }
}

/// Rotate TICKER_LOG_PATH to `.1` once it exceeds the size threshold.
/// The ticker emits a handful of lines per pass (1-3), so the check is
/// cheap. Best-effort: errors are swallowed — logging is advisory.
fn maybe_rotate_log() {
    if let Ok(md) = fs::metadata(TICKER_LOG_PATH)
        && md.len() > TICKER_LOG_ROTATE_BYTES
    {
        let rotated = format!("{TICKER_LOG_PATH}.1");
        let _ = fs::rename(TICKER_LOG_PATH, &rotated);
    }
}

/// Substitute the `{{ owner_name }}` placeholder in the tick-request
/// template. Tolerates the two canonical spellings (`{{ owner_name }}`
/// and `{{owner_name}}`) the same way `prompt::apply_bindings` does, so
/// the template author can use either form without surprise.
fn render_tick_request(template: &str, owner_name: &str) -> String {
    template
        .replace("{{ owner_name }}", owner_name)
        .replace("{{owner_name}}", owner_name)
}

fn escape_json_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out
}

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

    #[test]
    fn json_escape_handles_specials() {
        assert_eq!(escape_json_string("a\"b"), "a\\\"b");
        assert_eq!(escape_json_string("a\\b"), "a\\\\b");
        assert_eq!(escape_json_string("line1\nline2"), "line1\\nline2");
    }

    #[test]
    fn render_substitutes_owner_name() {
        let out = render_tick_request("ping {{ owner_name }} now", "Cody");
        assert_eq!(out, "ping Cody now");
        assert!(!out.contains("{{"));
    }

    #[test]
    fn render_tolerates_no_inner_whitespace() {
        let out = render_tick_request("ping {{owner_name}} now", "Cody");
        assert_eq!(out, "ping Cody now");
    }

    #[test]
    fn render_default_keeps_baked_template_owner_neutral() {
        // The shipped template MUST carry the placeholder, not a baked
        // name — the whole point of the parameterization. If someone
        // ever inlines a name back into prompts/tick-request.md this
        // test fails loudly.
        assert!(
            TICK_REQUEST_TEXT.contains("{{ owner_name }}"),
            "tick-request.md lost its owner_name placeholder"
        );
        let rendered =
            render_tick_request(TICK_REQUEST_TEXT, netsky_core::consts::OWNER_NAME_DEFAULT);
        assert!(rendered.contains(netsky_core::consts::OWNER_NAME_DEFAULT));
        assert!(!rendered.contains("{{"));
    }

    #[test]
    fn ticker_missing_count_roundtrips() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("netsky-ticker-missing-count");
        ticker_missing_record_at(&path, 1).unwrap();
        assert_eq!(fs::read_to_string(&path).unwrap(), "1");
    }

    #[test]
    fn ticker_missing_clear_removes_state() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("netsky-ticker-missing-count");
        std::fs::write(&path, "2").unwrap();
        assert!(path.exists());
        ticker_missing_clear_at(&path).unwrap();
        assert!(!path.exists());
    }

    #[test]
    fn analytics_refresh_decision_skips_fresh_page() {
        let now = DateTime::parse_from_rfc3339("2026-04-18T19:00:00Z")
            .unwrap()
            .with_timezone(&Utc);
        let markdown = "+++\n+++\n\n*generated 2026-04-18T18:50:30Z*\n";

        let decision = analytics_refresh_decision(markdown, now);

        assert_eq!(
            decision,
            AnalyticsRefreshDecision {
                should_refresh: false,
                generated_at: Some("2026-04-18T18:50:30Z".to_string()),
            }
        );
    }

    #[test]
    fn analytics_refresh_decision_refreshes_stale_page() {
        let now = DateTime::parse_from_rfc3339("2026-04-18T19:00:00Z")
            .unwrap()
            .with_timezone(&Utc);
        let markdown = "+++\n+++\n\n*generated 2026-04-18T18:40:00Z*\n";

        let decision = analytics_refresh_decision(markdown, now);

        assert_eq!(
            decision,
            AnalyticsRefreshDecision {
                should_refresh: true,
                generated_at: Some("2026-04-18T18:40:00Z".to_string()),
            }
        );
    }

    #[test]
    fn analytics_refresh_decision_refreshes_missing_timestamp() {
        let now = DateTime::parse_from_rfc3339("2026-04-18T19:00:00Z")
            .unwrap()
            .with_timezone(&Utc);

        let decision = analytics_refresh_decision("+++\n+++\n\n", now);

        assert_eq!(
            decision,
            AnalyticsRefreshDecision {
                should_refresh: true,
                generated_at: None,
            }
        );
    }

    #[test]
    fn analytics_refresh_decision_refreshes_future_timestamp() {
        let now = DateTime::parse_from_rfc3339("2026-04-18T19:00:00Z")
            .unwrap()
            .with_timezone(&Utc);
        let markdown = "+++\n+++\n\n*generated 2099-01-01T00:00:00Z*\n";

        let decision = analytics_refresh_decision(markdown, now);

        assert_eq!(
            decision,
            AnalyticsRefreshDecision {
                should_refresh: true,
                generated_at: Some("2099-01-01T00:00:00Z".to_string()),
            }
        );
    }

    #[test]
    fn analytics_commit_rejects_unrelated_staged_paths() {
        let dir = tempdir().unwrap();
        init_git_repo(dir.path());
        let page_rel = "website/content/analytics/2026-04-18.md";
        let page_path = dir.path().join(page_rel);
        let other_path = dir.path().join("src/leak.rs");
        fs::create_dir_all(page_path.parent().unwrap()).unwrap();
        fs::create_dir_all(other_path.parent().unwrap()).unwrap();
        fs::write(&page_path, "*generated 2026-04-18T19:00:00Z*\n").unwrap();
        fs::write(&other_path, "fn leak() {}\n").unwrap();
        git(dir.path(), &["add", "--", "src/leak.rs"]);

        let err = git_commit_refresh(dir.path(), page_rel).unwrap_err();
        let staged = git_stdout(dir.path(), &["diff", "--cached", "--name-only"]);
        let head = git_stdout(dir.path(), &["rev-list", "--count", "HEAD"]);

        assert!(err.contains("unexpected staged paths"));
        assert!(staged.lines().any(|line| line.trim() == "src/leak.rs"));
        assert_eq!(head.trim(), "1");
    }

    #[test]
    fn analytics_repo_scope_rejects_redirect_repo() {
        let expected = tempdir().unwrap();
        let rogue = tempdir().unwrap();
        init_git_repo(expected.path());
        init_git_repo(rogue.path());
        let rogue_website = rogue.path().join("website");
        fs::create_dir_all(&rogue_website).unwrap();

        let err = validate_analytics_repo_scope_at(expected.path(), &rogue_website, rogue.path())
            .unwrap_err();

        assert!(err.contains("analytics refresh limited to"));
    }

    #[test]
    fn analytics_hour_marker_waits_for_success_or_fresh() {
        let marker = tempdir().unwrap().path().join("hour");
        let error = json!({
            "status": "error",
            "reason": "git-push-failed",
        });
        let fresh = json!({
            "status": "skipped",
            "reason": "fresh",
        });

        assert!(!should_write_analytics_hour_marker(&error));
        assert!(should_write_analytics_hour_marker(&fresh));
        assert!(!marker.exists());
    }

    #[test]
    fn redact_sensitive_git_output_drops_token_lines() {
        let redacted = redact_sensitive_git_output(
            "ok\nremote: credential-helper says hi\nauth=ghp_secret\nsafe line",
        );
        assert_eq!(redacted, "ok\nsafe line");
    }

    fn init_git_repo(path: &Path) {
        git(path, &["init", "-b", "main"]);
        git(path, &["config", "user.name", "netsky"]);
        git(path, &["config", "user.email", "netsky@example.com"]);
        fs::write(path.join(".gitignore"), "target/\n").unwrap();
        git(path, &["add", "--", ".gitignore"]);
        git(path, &["commit", "-m", "init"]);
    }

    fn git(path: &Path, args: &[&str]) {
        let status = Command::new("git")
            .arg("-C")
            .arg(path)
            .args(args)
            .status()
            .unwrap();
        assert!(status.success(), "git {:?} failed", args);
    }

    fn git_stdout(path: &Path, args: &[&str]) -> String {
        let output = Command::new("git")
            .arg("-C")
            .arg(path)
            .args(args)
            .output()
            .unwrap();
        assert!(output.status.success(), "git {:?} failed", args);
        String::from_utf8_lossy(&output.stdout).trim().to_string()
    }
}