tracevault-cli 0.29.9

CLI tool for Visdom Trace - AI code tracing and attribution
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
//! VIS-1108 end to end: Claude Code's `cost-state` line, written at exit
//! after `Stop`, reaches the server through the `SessionEnd` hook
//! (`tracevault stream --event session-end`) or, when that hook did not send
//! it, through the catch-up sweep in `tracevault session-start`. `stop` keeps
//! ending the session for every agent.
//!
//! Runs the hook binary (via `CARGO_BIN_EXE_*`) against a raw-TCP mock server,
//! isolated to a tempdir like `subagent_stream_e2e_test.rs`.
use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpListener;
use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};
use std::sync::mpsc;
use std::thread;
use std::time::{Duration, Instant};
use tracevault_protocol::streaming::{StreamEventRequest, StreamEventType};

const REPO_ID: &str = "11111111-1111-1111-1111-111111111111";
const SESSION_ID: &str = "vis1108-session";

const COST_STATE: &str = r#"{"type":"cost-state","sessionId":"vis1108-session","totalCostUSD":0.1249792,"modelUsage":{"claude-opus-5-5":{"outputTokens":4,"costUSD":0.1249792}},"hasUnknownModelCost":false}"#;

/// How long the mock server waits for a connection that never arrives
/// before its thread gives up.
const ACCEPT_DEADLINE: Duration = Duration::from_secs(5);

fn accept_with_deadline(listener: &TcpListener) -> Option<std::net::TcpStream> {
    let deadline = Instant::now() + ACCEPT_DEADLINE;
    loop {
        match listener.accept() {
            Ok((stream, _)) => {
                let _ = stream.set_nonblocking(false);
                return Some(stream);
            }
            Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                if Instant::now() >= deadline {
                    return None;
                }
                thread::sleep(Duration::from_millis(20));
            }
            Err(_) => return None,
        }
    }
}

/// Accept every request until no new connection arrives for
/// `ACCEPT_DEADLINE`. A stream POST is answered as accepted and its body
/// captured; anything else (`session-start`'s policy and binding lookups)
/// gets a 404.
fn spawn_server() -> (String, mpsc::Receiver<StreamEventRequest>) {
    spawn_server_with(false)
}

/// [`spawn_server`], but with `failing` every stream POST is captured and
/// then answered with a 503.
fn spawn_server_with(failing: bool) -> (String, mpsc::Receiver<StreamEventRequest>) {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    listener.set_nonblocking(true).unwrap();
    let addr = listener.local_addr().unwrap();
    let (tx, rx) = mpsc::channel();
    let body = serde_json::to_string(&tracevault_protocol::streaming::StreamEventResponse {
        session_db_id: uuid::Uuid::nil(),
        event_db_id: Some(uuid::Uuid::nil()),
        status: "accepted".to_string(),
    })
    .unwrap();
    let response = format!(
        "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
        body.len()
    );
    thread::spawn(move || {
        while let Some(stream) = accept_with_deadline(&listener) {
            let mut reader = BufReader::new(stream);
            let mut request_line = String::new();
            let _ = reader.read_line(&mut request_line);
            let mut content_length = 0usize;
            loop {
                let mut header = String::new();
                if reader.read_line(&mut header).unwrap_or(0) == 0 {
                    break;
                }
                let header = header.trim_end();
                if header.is_empty() {
                    break;
                }
                if let Some((name, value)) = header.split_once(':') {
                    if name.eq_ignore_ascii_case("content-length") {
                        content_length = value.trim().parse().unwrap_or(0);
                    }
                }
            }
            let mut body = vec![0u8; content_length];
            let _ = reader.read_exact(&mut body);
            let mut stream = reader.into_inner();
            // Repo- or project-scoped.
            if !(request_line.starts_with("POST /api/v1/") && request_line.contains("/stream")) {
                let _ = stream.write_all(
                    b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
                );
                continue;
            }
            if let Ok(req) = serde_json::from_slice(&body) {
                let _ = tx.send(req);
            }
            if failing {
                let _ = stream.write_all(
                    b"HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
                );
                continue;
            }
            let _ = stream.write_all(response.as_bytes());
            let _ = stream.flush();
        }
    });
    (format!("http://{addr}"), rx)
}

/// A URL nothing listens on: every send fails.
fn dead_server() -> String {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    format!("http://{}", listener.local_addr().unwrap())
}

/// A server that accepts every connection and never answers, for
/// `ACCEPT_DEADLINE` after the last one.
fn stalled_server() -> String {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    listener.set_nonblocking(true).unwrap();
    let addr = listener.local_addr().unwrap();
    thread::spawn(move || {
        let mut held = Vec::new();
        while let Some(stream) = accept_with_deadline(&listener) {
            held.push(stream);
        }
    });
    format!("http://{addr}")
}

/// A temp git repo bound to `REPO_ID`, isolated home/config/state dirs, and a
/// Claude Code project dir for the session transcripts.
struct Fixture {
    _tmp: tempfile::TempDir,
    home: PathBuf,
    state: PathBuf,
    repo: PathBuf,
    project: PathBuf,
}

impl Fixture {
    fn new() -> Self {
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path().join("home");
        let state = tmp.path().join("state");
        let repo = tmp.path().join("repo");
        let project = tmp.path().join("claude-project");
        std::fs::create_dir_all(&home).unwrap();
        std::fs::create_dir_all(&repo).unwrap();
        std::fs::create_dir_all(&project).unwrap();
        for args in [
            vec!["init", "-q"],
            vec!["config", "user.email", "test@example.com"],
            vec!["config", "user.name", "Test"],
            vec!["commit", "-q", "--allow-empty", "-m", "init"],
        ] {
            let ok = Command::new("git")
                .arg("-C")
                .arg(&repo)
                .args(&args)
                .status()
                .expect("git failed to run")
                .success();
            assert!(ok, "git {args:?} must succeed");
        }
        std::fs::create_dir_all(repo.join(".visdom")).unwrap();
        std::fs::write(
            repo.join(".visdom").join("tracing.toml"),
            format!("repo_id = \"{REPO_ID}\"\n"),
        )
        .unwrap();
        Fixture {
            _tmp: tmp,
            home,
            state,
            repo,
            project,
        }
    }

    fn transcript(&self, session: &str) -> PathBuf {
        self.project.join(format!("{session}.jsonl"))
    }

    fn session_dir(&self, session: &str) -> PathBuf {
        self.repo.join(".visdom").join("sessions").join(session)
    }

    fn line(&self, session: &str, line: &str) {
        append(&self.transcript(session), &format!("{line}\n"));
    }

    fn assistant(&self, session: &str, n: usize) {
        self.line(
            session,
            &serde_json::json!({"type": "assistant", "n": n}).to_string(),
        );
    }

    fn command(&self, base: &str) -> Command {
        let mut cmd = Command::new(env!("CARGO_BIN_EXE_tracevault"));
        cmd.current_dir(&self.repo)
            .env("HOME", &self.home)
            .env("XDG_CONFIG_HOME", self.home.join(".config"))
            .env("XDG_STATE_HOME", &self.state)
            .env("TRACEVAULT_SERVER_URL", base)
            .env("TRACEVAULT_API_KEY", "tvk_test")
            .env_remove("CLAUDE_ENV_FILE")
            .env_remove("TRACEVAULT_SESSION_ID")
            .env_remove("AMP_CURRENT_THREAD_ID")
            .env_remove("TRACEVAULT_PROJECT")
            .env_remove("TRACEVAULT_PROJECT_ATTRIBUTION");
        cmd
    }

    fn run(&self, base: &str, args: &[&str], payload: serde_json::Value) -> Output {
        let mut child = self
            .command(base)
            .args(args)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("failed to run the tracevault binary");
        child
            .stdin
            .take()
            .unwrap()
            .write_all(payload.to_string().as_bytes())
            .unwrap();
        let out = child.wait_with_output().unwrap();
        assert!(out.status.success(), "{out:?}");
        out
    }

    /// Run `tracevault stream --event <event>` for `session`.
    fn hook(&self, base: &str, session: &str, event: &str, hook_event_name: &str) -> Output {
        self.run(
            base,
            &["stream", "--event", event],
            serde_json::json!({
                "session_id": session,
                "transcript_path": self.transcript(session).to_string_lossy(),
                "cwd": self.repo.to_string_lossy(),
                "hook_event_name": hook_event_name,
            }),
        )
    }

    /// Run `tracevault session-start` for `session`.
    fn session_start(&self, base: &str, session: &str) -> Output {
        self.run(
            base,
            &["session-start"],
            serde_json::json!({
                "session_id": session,
                "transcript_path": self.transcript(session).to_string_lossy(),
                "cwd": self.repo.to_string_lossy(),
                "hook_event_name": "SessionStart",
                "source": "startup",
            }),
        )
    }

    fn offset(&self, session: &str) -> Option<u64> {
        std::fs::read_to_string(self.session_dir(session).join(".stream_offset"))
            .ok()
            .map(|s| s.trim().parse().unwrap())
    }

    fn transcript_len(&self, session: &str) -> u64 {
        std::fs::metadata(self.transcript(session)).unwrap().len()
    }
}

fn append(path: &Path, text: &str) {
    let mut f = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
        .unwrap();
    f.write_all(text.as_bytes()).unwrap();
}

/// Everything the server received. Called after the process exited, so every
/// request it made is already in the channel.
fn captured(rx: &mpsc::Receiver<StreamEventRequest>) -> Vec<StreamEventRequest> {
    rx.try_iter().collect()
}

fn lines(req: &StreamEventRequest) -> Vec<serde_json::Value> {
    req.transcript_lines.clone().unwrap_or_default()
}

fn types(req: &StreamEventRequest) -> Vec<String> {
    lines(req)
        .iter()
        .map(|l| l["type"].as_str().unwrap_or_default().to_string())
        .collect()
}

/// A session whose turn `Stop` already sent, then the `cost-state` Claude
/// Code writes at exit.
fn ended_session(fx: &Fixture, base: &str, session: &str) {
    fx.assistant(session, 1);
    fx.hook(base, session, "stop", "Stop");
    fx.line(session, COST_STATE);
}

#[test]
fn session_end_sends_the_final_cost_state_without_ending_the_session_again() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();

    fx.assistant(SESSION_ID, 1);
    fx.hook(&base, SESSION_ID, "stop", "Stop");
    let stop = captured(&rx);
    assert_eq!(stop.len(), 1);
    assert_eq!(stop[0].event_type, StreamEventType::SessionEnd);
    let after_stop = fx.offset(SESSION_ID).unwrap();

    fx.line(SESSION_ID, COST_STATE);
    let out = fx.hook(&base, SESSION_ID, "session-end", "SessionEnd");
    let sent = captured(&rx);
    assert_eq!(sent.len(), 1, "{sent:?}");
    assert_eq!(sent[0].event_type, StreamEventType::Transcript);
    assert_eq!(sent[0].hook_event_name.as_deref(), Some("SessionEnd"));
    assert_eq!(sent[0].tool.as_deref(), Some("claude-code"));
    assert_eq!(types(&sent[0]), ["cost-state"]);
    assert_eq!(lines(&sent[0])[0]["totalCostUSD"], 0.1249792);
    assert_eq!(sent[0].transcript_offset, Some(after_stop as i64));
    assert_eq!(fx.offset(SESSION_ID), Some(fx.transcript_len(SESSION_ID)));
    let response: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert!(response.is_object(), "the hook answers like every hook");
}

#[test]
fn session_end_sends_every_new_line_from_the_offset() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    fx.assistant(SESSION_ID, 1);
    fx.hook(&base, SESSION_ID, "post-tool-use", "PostToolUse");
    captured(&rx);

    // Lines `Stop` did not send, and one after the report (2 of 40 sampled
    // transcripts have lines after it).
    fx.assistant(SESSION_ID, 2);
    fx.line(SESSION_ID, COST_STATE);
    fx.assistant(SESSION_ID, 3);
    fx.hook(&base, SESSION_ID, "session-end", "SessionEnd");

    let sent = captured(&rx);
    assert_eq!(sent.len(), 1);
    assert_eq!(types(&sent[0]), ["assistant", "cost-state", "assistant"]);
    assert_eq!(fx.offset(SESSION_ID), Some(fx.transcript_len(SESSION_ID)));
}

#[test]
fn session_end_without_a_new_line_sends_nothing() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    fx.assistant(SESSION_ID, 1);
    fx.hook(&base, SESSION_ID, "stop", "Stop");
    captured(&rx);
    let offset = fx.offset(SESSION_ID);

    fx.hook(&base, SESSION_ID, "session-end", "SessionEnd");
    assert!(captured(&rx).is_empty());
    assert_eq!(fx.offset(SESSION_ID), offset);
}

#[test]
fn session_end_that_cannot_send_leaves_the_offset_for_a_retry() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    ended_session(&fx, &base, SESSION_ID);
    captured(&rx);
    let offset = fx.offset(SESSION_ID);

    fx.hook(&dead_server(), SESSION_ID, "session-end", "SessionEnd");
    assert_eq!(fx.offset(SESSION_ID), offset, "the sweep can still find it");
}

#[test]
fn stop_still_ends_the_session_for_every_agent() {
    for agent in ["claude-code", "codex", "gsd", "opencode"] {
        let fx = Fixture::new();
        let (base, rx) = spawn_server();
        fx.assistant(SESSION_ID, 1);
        let mut payload = serde_json::json!({
            "session_id": SESSION_ID,
            "transcript_path": fx.transcript(SESSION_ID).to_string_lossy(),
            "cwd": fx.repo.to_string_lossy(),
            "hook_event_name": "Stop",
        });
        if agent == "opencode" {
            payload["transcript_path"] = "".into();
            payload["transcript_records"] = serde_json::json!([]);
        }
        fx.run(
            &base,
            &["stream", "--event", "stop", "--agent", agent],
            payload,
        );
        let sent = captured(&rx);
        assert_eq!(sent.len(), 1, "{agent}: {sent:?}");
        assert_eq!(sent[0].event_type, StreamEventType::SessionEnd, "{agent}");
        assert_eq!(sent[0].tool.as_deref(), Some(agent), "{agent}");
    }
}

#[test]
fn session_start_sends_what_an_earlier_session_end_left_unsent() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    // Ended, but its SessionEnd hook never ran (failed, or ended before the
    // upgrade that installs it).
    ended_session(&fx, &base, "ended");
    // Still running in another terminal: unsent lines but no report.
    fx.assistant("running", 1);
    fx.hook(&base, "running", "post-tool-use", "PostToolUse");
    fx.assistant("running", 2);
    let running_offset = fx.offset("running");
    captured(&rx);

    let out = fx.session_start(&base, "new-session");

    let sent = captured(&rx);
    assert_eq!(sent.len(), 1, "{sent:?}");
    assert_eq!(sent[0].session_id, "ended");
    assert_eq!(sent[0].event_type, StreamEventType::Transcript);
    assert_eq!(sent[0].hook_event_name.as_deref(), Some("SessionEnd"));
    assert_eq!(types(&sent[0]), ["cost-state"]);
    assert_eq!(
        sent[0].billing_mode, None,
        "this process's credentials say nothing about that session's"
    );
    assert_eq!(fx.offset("ended"), Some(fx.transcript_len("ended")));
    assert_eq!(
        fx.offset("running"),
        running_offset,
        "left to its own hooks"
    );
    let response: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert!(response.is_object(), "session-start still answers");

    // Sent once: the next session start finds nothing to do.
    fx.session_start(&base, "another-session");
    assert!(captured(&rx).is_empty());
}

#[test]
fn session_start_stops_at_the_first_session_the_server_refuses() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    ended_session(&fx, &base, "older");
    ended_session(&fx, &base, "newer");
    let now = std::time::SystemTime::now();
    for (session, age) in [("older", 120), ("newer", 60)] {
        std::fs::File::options()
            .write(true)
            .open(fx.transcript(session))
            .unwrap()
            .set_modified(now - Duration::from_secs(age))
            .unwrap();
    }
    captured(&rx);
    let offsets = (fx.offset("older"), fx.offset("newer"));

    let (failing, rx) = spawn_server_with(true);
    fx.session_start(&failing, "new-session");

    let tried = captured(&rx);
    assert_eq!(tried.len(), 1, "the older session is not tried: {tried:?}");
    assert_eq!(tried[0].session_id, "newer", "most recent first");
    assert_eq!((fx.offset("older"), fx.offset("newer")), offsets);
    // The refused lines are not queued as well: they are in the transcript,
    // and the offset still points at them.
    for session in ["older", "newer"] {
        assert!(!fx
            .session_dir(session)
            .join(format!("pending-{REPO_ID}.jsonl"))
            .exists());
    }

    // So the next session start, with the server back, sends them.
    let (base, rx) = spawn_server();
    fx.session_start(&base, "later-session");
    let sent = captured(&rx);
    let mut sessions: Vec<&str> = sent.iter().map(|r| r.session_id.as_str()).collect();
    sessions.sort_unstable();
    assert_eq!(sessions, ["newer", "older"]);
    assert!(sent.iter().all(|r| types(r) == ["cost-state"]));
}

#[test]
fn session_start_with_a_process_scoped_project_leaves_other_sessions_alone() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    ended_session(&fx, &base, "ended");
    captured(&rx);
    let offset = fx.offset("ended");

    let mut child = fx
        .command(&base)
        .env("TRACEVAULT_PROJECT", "22222222-2222-2222-2222-222222222222")
        .arg("session-start")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    child
        .stdin
        .take()
        .unwrap()
        .write_all(
            serde_json::json!({
                "session_id": "new-session",
                "cwd": fx.repo.to_string_lossy(),
                "hook_event_name": "SessionStart",
            })
            .to_string()
            .as_bytes(),
        )
        .unwrap();
    assert!(child.wait_with_output().unwrap().status.success());

    assert!(captured(&rx).is_empty());
    assert_eq!(fx.offset("ended"), offset);
}

/// Set (or, with `None`, remove) the attribution `session`'s hooks recorded.
fn set_recorded_attribution(fx: &Fixture, session: &str, attribution: Option<serde_json::Value>) {
    let path = fx.session_dir(session).join("metadata.json");
    let mut meta: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
    match attribution {
        Some(a) => meta["attribution"] = a,
        None => {
            meta.as_object_mut().unwrap().remove("attribution");
        }
    }
    std::fs::write(&path, meta.to_string()).unwrap();
}

#[test]
fn hooks_record_the_attribution_they_send_under() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    ended_session(&fx, &base, "ended");
    captured(&rx);
    let meta: serde_json::Value = serde_json::from_str(
        &std::fs::read_to_string(fx.session_dir("ended").join("metadata.json")).unwrap(),
    )
    .unwrap();
    assert_eq!(
        meta["attribution"],
        serde_json::json!({"repo_id": REPO_ID, "project_id": null})
    );
}

#[test]
fn session_start_leaves_a_session_recorded_under_another_attribution() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    ended_session(&fx, &base, "ended");
    captured(&rx);
    // Its hooks sent under a project that no longer resolves for it (the
    // default project was switched since).
    set_recorded_attribution(
        &fx,
        "ended",
        Some(serde_json::json!({
            "repo_id": REPO_ID,
            "project_id": "22222222-2222-2222-2222-222222222222",
        })),
    );
    let offset = fx.offset("ended");

    fx.session_start(&base, "new-session");
    assert!(captured(&rx).is_empty());
    assert_eq!(fx.offset("ended"), offset);
}

#[test]
fn session_start_sends_a_repo_session_an_older_cli_recorded() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    ended_session(&fx, &base, "ended");
    captured(&rx);
    set_recorded_attribution(&fx, "ended", None);

    fx.session_start(&base, "new-session");
    let sent = captured(&rx);
    assert_eq!(sent.len(), 1, "{sent:?}");
    assert_eq!(types(&sent[0]), ["cost-state"]);
}

/// Set the transcript of `session` as last written `age_secs` ago.
fn age(fx: &Fixture, session: &str, age_secs: u64) {
    std::fs::File::options()
        .write(true)
        .open(fx.transcript(session))
        .unwrap()
        .set_modified(std::time::SystemTime::now() - Duration::from_secs(age_secs))
        .unwrap();
}

#[test]
fn sessions_skipped_for_their_attribution_do_not_hold_back_older_ones() {
    // As many newer sessions as one run sends, each recorded under a project
    // that no longer resolves, and an older one that can be sent.
    const SENT_PER_RUN: usize = 10;
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    ended_session(&fx, &base, "older");
    age(&fx, "older", 600);
    for i in 0..SENT_PER_RUN {
        let id = format!("moved-{i}");
        ended_session(&fx, &base, &id);
        age(&fx, &id, 60 + i as u64);
        set_recorded_attribution(
            &fx,
            &id,
            Some(serde_json::json!({
                "repo_id": REPO_ID,
                "project_id": "22222222-2222-2222-2222-222222222222",
            })),
        );
    }
    captured(&rx);

    fx.session_start(&base, "new-session");
    let sent = captured(&rx);
    assert_eq!(sent.len(), 1, "{sent:?}");
    assert_eq!(sent[0].session_id, "older");
}

#[test]
fn one_session_start_sends_at_most_ten_sessions() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    for i in 0..12 {
        let id = format!("s{i:02}");
        ended_session(&fx, &base, &id);
        age(&fx, &id, 1000 - i as u64);
    }
    captured(&rx);

    fx.session_start(&base, "new-session");
    let mut sent: Vec<String> = captured(&rx).into_iter().map(|r| r.session_id).collect();
    sent.sort();
    let expected: Vec<String> = (2..12).map(|i| format!("s{i:02}")).collect();
    assert_eq!(sent, expected, "the 10 most recent");

    fx.session_start(&base, "next-session");
    let sent: Vec<String> = captured(&rx).into_iter().map(|r| r.session_id).collect();
    assert_eq!(sent, ["s01", "s00"], "the rest next time");
}

#[test]
fn session_start_stays_inside_its_timeout_when_the_server_stalls() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    ended_session(&fx, &base, "ended");
    captured(&rx);
    let offset = fx.offset("ended");

    let started = Instant::now();
    let out = fx.session_start(&stalled_server(), "new-session");
    let took = started.elapsed();
    assert!(took < Duration::from_secs(10), "took {took:?}");
    let response: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert!(response.is_object());
    assert_eq!(fx.offset("ended"), offset);
}

#[test]
fn session_start_never_fails_on_a_dead_server() {
    let fx = Fixture::new();
    let (base, rx) = spawn_server();
    ended_session(&fx, &base, "ended");
    captured(&rx);
    let offset = fx.offset("ended");

    let started = Instant::now();
    let out = fx.session_start(&dead_server(), "new-session");
    assert!(
        started.elapsed() < Duration::from_secs(9),
        "within the hook timeout"
    );
    let response: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert!(response.is_object());
    assert_eq!(
        fx.offset("ended"),
        offset,
        "retried by a later session start"
    );
}