quietset-cli 0.14.0

CLI for quietset — filter datasets by label stability
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
use std::process::Output;

use assert_cmd::Command;

fn run(args: &[&str]) -> Output {
    Command::cargo_bin("quietset")
        .unwrap()
        .args(args)
        .output()
        .unwrap()
}

fn stdout(out: &Output) -> String {
    String::from_utf8_lossy(&out.stdout).into_owned()
}

fn stderr(out: &Output) -> String {
    String::from_utf8_lossy(&out.stderr).into_owned()
}

fn write_temp(name: &str, contents: &str) -> std::path::PathBuf {
    let path =
        std::env::temp_dir().join(format!("quietset_cli_test_{name}_{}", std::process::id()));
    std::fs::write(&path, contents).unwrap();
    path
}

#[test]
fn version_flag_reports_cargo_package_version() {
    let out = run(&["--version"]);
    assert!(out.status.success());
    assert!(stdout(&out).contains(env!("CARGO_PKG_VERSION")));
}

#[test]
fn score_jsonl_succeeds_on_valid_fixture() {
    let out = run(&["score", "../../tests/fixtures/simple.jsonl"]);
    assert!(out.status.success());
    assert!(stdout(&out).contains("\"sample_id\":\"a\""));
}

#[test]
fn score_invalid_jsonl_without_skip_invalid_fails() {
    let path = write_temp(
        "bad.jsonl",
        "{\"sample_id\":\"a\",\"score\":0.5}\nNOT JSON\n",
    );
    let out = run(&["score", path.to_str().unwrap()]);
    assert!(!out.status.success());
}

#[test]
fn score_invalid_jsonl_with_skip_invalid_succeeds_and_warns() {
    let path = write_temp(
        "bad_skip.jsonl",
        "{\"sample_id\":\"a\",\"score\":0.5}\nNOT JSON\n",
    );
    let out = run(&["score", path.to_str().unwrap(), "--skip-invalid"]);
    assert!(out.status.success());
    assert!(stderr(&out).contains("warning: skipping"));
}

// Regression test for the bug fixed in this session: `--skip-invalid` used to be
// silently ignored for `--format csv`, so a single bad row failed the whole run
// regardless of the flag.
#[test]
fn score_csv_skip_invalid_skips_bad_row() {
    let path = write_temp(
        "bad.csv",
        "sample_id,label,score\na,win,0.9\nb,win,not_a_number\nc,win,0.8\n",
    );

    let failing = run(&["score", path.to_str().unwrap(), "--format", "csv"]);
    assert!(!failing.status.success());

    let ok = run(&[
        "score",
        path.to_str().unwrap(),
        "--format",
        "csv",
        "--skip-invalid",
    ]);
    assert!(ok.status.success());
    assert!(stderr(&ok).contains("warning: skipping row 2"));
}

// Regression test for the bug fixed in this session: `explain` used to swallow
// JSONL parse errors with `.ok()`, so a malformed line anywhere in the file made
// unrelated, later sample_ids report a misleading "not found" instead of the
// real parse error.
#[test]
fn explain_reports_parse_error_not_misleading_not_found() {
    let scored = run(&["score", "../../tests/fixtures/simple.jsonl"]);
    assert!(scored.status.success());
    let scored_text = stdout(&scored);
    let lines: Vec<&str> = scored_text.lines().collect();
    assert!(lines.len() >= 2, "fixture should score at least 2 samples");

    let corrupted = format!("{}\nNOT JSON\n{}\n", lines[0], lines[1]);
    let path = write_temp("explain_corrupted.jsonl", &corrupted);

    // sample_id on line 1 is found before the corrupted line is ever read.
    let found_before_corruption = run(&["explain", path.to_str().unwrap(), "--sample-id", "a"]);
    assert!(found_before_corruption.status.success());

    // sample_id on line 3 requires scanning past the corrupted line 2 — this
    // must surface the parse error, not "sample_id 'b' not found".
    let must_scan_past_corruption = run(&["explain", path.to_str().unwrap(), "--sample-id", "b"]);
    assert!(!must_scan_past_corruption.status.success());
    assert!(stderr(&must_scan_past_corruption).contains("parsing line 2"));
}

#[test]
fn explain_missing_sample_id_without_corruption_reports_not_found() {
    let scored = run(&["score", "../../tests/fixtures/simple.jsonl"]);
    assert!(scored.status.success());
    let path = write_temp("scored_ok.jsonl", &stdout(&scored));

    let out = run(&["explain", path.to_str().unwrap(), "--sample-id", "zzz"]);
    assert!(!out.status.success());
    assert!(stderr(&out).contains("not found"));
}

#[test]
fn score_then_filter_pipeline_via_files() {
    let scored = run(&["score", "../../tests/fixtures/simple.jsonl"]);
    assert!(scored.status.success());
    let path = write_temp("pipeline.jsonl", &stdout(&scored));

    let out = run(&["filter", path.to_str().unwrap(), "--decision", "keep"]);
    assert!(out.status.success());
}

// Regression test for the bug fixed in this session: `filter` had no empty-result
// guard, so if every input row was invalid and dropped via `--skip-invalid`, it
// silently produced 0 lines of output with exit code 0 instead of erroring like
// `summary`/`audit`/`select`/`recommend` already do.
#[test]
fn filter_all_invalid_with_skip_invalid_fails_instead_of_silently_succeeding() {
    let path = write_temp("filter_all_bad.jsonl", "NOT JSON\nALSO NOT JSON\n");
    let out = run(&["filter", path.to_str().unwrap(), "--skip-invalid"]);
    assert!(!out.status.success());
    assert!(stderr(&out).contains("no records found"));
}

#[test]
fn game_ai_profile_keeps_stable_signed_scores() {
    let path = write_temp(
        "game_ai_stable_signed.jsonl",
        "{\"sample_id\":\"stable_signed\",\"budget\":10.0,\"seed\":1,\"evaluator_id\":\"e1\",\"score\":0.35}\n\
         {\"sample_id\":\"stable_signed\",\"budget\":20.0,\"seed\":2,\"evaluator_id\":\"e2\",\"score\":0.52}\n\
         {\"sample_id\":\"stable_signed\",\"budget\":10.0,\"seed\":2,\"evaluator_id\":\"e1\",\"score\":0.41}\n\
         {\"sample_id\":\"stable_signed\",\"budget\":20.0,\"seed\":1,\"evaluator_id\":\"e2\",\"score\":0.44}\n",
    );
    let out = run(&["score", path.to_str().unwrap(), "--profile", "game-ai"]);
    assert!(out.status.success());
    let value: serde_json::Value =
        serde_json::from_str(stdout(&out).lines().next().unwrap()).unwrap();
    assert_eq!(value["decision"], "keep");
}

// game-ai weights score_sign at 2.0 because a shogi-style evaluation whose sign flips
// under small search perturbations is a worse training signal than one whose magnitude
// merely varies. Same budget/seed/evaluator spread as stable_signed above -- only the
// score-sign pattern differs.
#[test]
fn game_ai_profile_flags_sign_flip_instability() {
    let path = write_temp(
        "game_ai_sign_flip.jsonl",
        "{\"sample_id\":\"sign_flip\",\"budget\":10.0,\"seed\":1,\"evaluator_id\":\"e1\",\"score\":0.12}\n\
         {\"sample_id\":\"sign_flip\",\"budget\":20.0,\"seed\":2,\"evaluator_id\":\"e2\",\"score\":-0.08}\n\
         {\"sample_id\":\"sign_flip\",\"budget\":10.0,\"seed\":2,\"evaluator_id\":\"e1\",\"score\":0.05}\n\
         {\"sample_id\":\"sign_flip\",\"budget\":20.0,\"seed\":1,\"evaluator_id\":\"e2\",\"score\":-0.03}\n",
    );
    let out = run(&["score", path.to_str().unwrap(), "--profile", "game-ai"]);
    assert!(out.status.success());
    let value: serde_json::Value =
        serde_json::from_str(stdout(&out).lines().next().unwrap()).unwrap();
    assert_ne!(value["decision"], "keep");
}

// budget swing correlates with the sign flip while seed is assigned orthogonally to it,
// isolating budget_stability as the dominant weighted deficit rather than seed noise.
#[test]
fn game_ai_profile_flags_budget_flip_instability() {
    let path = write_temp(
        "game_ai_budget_flip.jsonl",
        "{\"sample_id\":\"budget_flip\",\"budget\":10.0,\"seed\":1,\"evaluator_id\":\"e1\",\"score\":0.40}\n\
         {\"sample_id\":\"budget_flip\",\"budget\":10.0,\"seed\":2,\"evaluator_id\":\"e2\",\"score\":0.38}\n\
         {\"sample_id\":\"budget_flip\",\"budget\":100.0,\"seed\":1,\"evaluator_id\":\"e2\",\"score\":-0.42}\n\
         {\"sample_id\":\"budget_flip\",\"budget\":100.0,\"seed\":2,\"evaluator_id\":\"e1\",\"score\":-0.36}\n",
    );
    let out = run(&["score", path.to_str().unwrap(), "--profile", "game-ai"]);
    assert!(out.status.success());
    let value: serde_json::Value =
        serde_json::from_str(stdout(&out).lines().next().unwrap()).unwrap();
    assert_ne!(value["decision"], "keep");
}

// Same seed-alternating pattern as active_review_observations_populates_target_seed_for_add_seed_action
// below, with a fixed budget and two evaluators added so it also clears game-ai's other
// minimums -- confirms seed instability alone demotes a sample under this stricter profile,
// and that the resulting active-review recommendation still targets add_seed.
#[test]
fn game_ai_profile_flags_seed_sensitivity_and_targets_add_seed() {
    let obs_text = "{\"sample_id\":\"pricey\",\"label\":\"win\",\"budget\":4.0,\"seed\":0,\"evaluator_id\":\"e1\",\"score\":0.9}\n\
         {\"sample_id\":\"pricey\",\"label\":\"win\",\"budget\":4.0,\"seed\":1,\"evaluator_id\":\"e2\",\"score\":0.1}\n\
         {\"sample_id\":\"pricey\",\"label\":\"win\",\"budget\":4.0,\"seed\":0,\"evaluator_id\":\"e2\",\"score\":0.9}\n\
         {\"sample_id\":\"pricey\",\"label\":\"win\",\"budget\":4.0,\"seed\":1,\"evaluator_id\":\"e1\",\"score\":0.1}\n";
    let obs_path = write_temp("game_ai_seed_sensitive.jsonl", obs_text);

    let scored = run(&["score", obs_path.to_str().unwrap(), "--profile", "game-ai"]);
    assert!(scored.status.success());
    let scored_text = stdout(&scored);
    let value: serde_json::Value =
        serde_json::from_str(scored_text.lines().next().unwrap()).unwrap();
    assert_ne!(value["decision"], "keep");

    let scored_path = write_temp("game_ai_seed_sensitive_scored.jsonl", &scored_text);
    let out = run(&[
        "active-review",
        scored_path.to_str().unwrap(),
        "--observations",
        obs_path.to_str().unwrap(),
    ]);
    assert!(out.status.success());
    let review_value: serde_json::Value =
        serde_json::from_str(stdout(&out).lines().next().unwrap()).unwrap();
    assert_eq!(review_value["suggested_action"], "add_seed");
    assert_eq!(review_value["target_seed"], 2);
}

// Single-engine, multi-depth data: one distinct evaluator_id. --profile game-ai's
// min-evaluators-keep floor of 2 can never be satisfied by this shape, silently
// demoting an otherwise-stable sample to review; --profile game-ai-single-engine
// drops that floor to 0 since depth (budget), not evaluator identity, is what varies.
#[test]
fn game_ai_single_engine_profile_keeps_what_game_ai_profile_wrongly_demotes() {
    let path = write_temp(
        "game_ai_single_engine.jsonl",
        "{\"sample_id\":\"one_engine\",\"budget\":10.0,\"seed\":1,\"evaluator_id\":\"sekirei\",\"score\":0.35}\n\
         {\"sample_id\":\"one_engine\",\"budget\":20.0,\"seed\":2,\"evaluator_id\":\"sekirei\",\"score\":0.52}\n\
         {\"sample_id\":\"one_engine\",\"budget\":10.0,\"seed\":2,\"evaluator_id\":\"sekirei\",\"score\":0.41}\n\
         {\"sample_id\":\"one_engine\",\"budget\":20.0,\"seed\":1,\"evaluator_id\":\"sekirei\",\"score\":0.44}\n",
    );

    let under_game_ai = run(&["score", path.to_str().unwrap(), "--profile", "game-ai"]);
    assert!(under_game_ai.status.success());
    let value: serde_json::Value =
        serde_json::from_str(stdout(&under_game_ai).lines().next().unwrap()).unwrap();
    assert_ne!(value["decision"], "keep");

    let under_single_engine = run(&[
        "score",
        path.to_str().unwrap(),
        "--profile",
        "game-ai-single-engine",
    ]);
    assert!(under_single_engine.status.success());
    let value_single: serde_json::Value =
        serde_json::from_str(stdout(&under_single_engine).lines().next().unwrap()).unwrap();
    assert_eq!(value_single["decision"], "keep");
}

fn gold_labeled_fixture() -> std::path::PathBuf {
    write_temp(
        "gold_labeled.jsonl",
        "{\"sample_id\":\"a\",\"label\":\"win\",\"score\":0.9,\"gold_label\":\"win\"}\n\
         {\"sample_id\":\"a\",\"label\":\"win\",\"score\":0.9,\"gold_label\":\"win\"}\n\
         {\"sample_id\":\"a\",\"label\":\"win\",\"score\":0.9,\"gold_label\":\"win\"}\n\
         {\"sample_id\":\"b\",\"label\":\"win\",\"score\":0.9,\"gold_label\":\"win\"}\n\
         {\"sample_id\":\"b\",\"label\":\"win\",\"score\":0.9,\"gold_label\":\"win\"}\n\
         {\"sample_id\":\"b\",\"label\":\"win\",\"score\":0.9,\"gold_label\":\"win\"}\n",
    )
}

#[test]
fn calibrate_csv_output_has_header_and_one_data_row() {
    let path = gold_labeled_fixture();
    let out = run(&[
        "calibrate",
        path.to_str().unwrap(),
        "--target-precision",
        "0.9",
        "--output-format",
        "csv",
    ]);
    assert!(out.status.success());
    let text = stdout(&out);
    let mut lines = text.lines();
    let header = lines.next().expect("header row");
    assert_eq!(
        header,
        "decision_score,keep_threshold,drop_threshold,achieved_precision,precision_ci_low,precision_ci_high,coverage,n_keep,n_total,train_precision,validated_on_heldout,note"
    );
    let data = lines.next().expect("one data row");
    assert_eq!(data.split(',').count(), 12);
    assert!(lines.next().is_none(), "should be exactly one data row");
}

#[test]
fn policy_csv_output_has_header_and_fixed_columns() {
    let path = gold_labeled_fixture();
    let out = run(&["policy", path.to_str().unwrap(), "--output-format", "csv"]);
    assert!(out.status.success());
    let text = stdout(&out);
    let mut lines = text.lines();
    let header = lines.next().expect("header row");
    assert_eq!(
        header,
        "threshold,n_keep,coverage,precision,stable_wrong_rate,best"
    );
    let data_rows: Vec<&str> = lines.collect();
    assert_eq!(data_rows.len(), 50, "one row per swept threshold");
    for row in &data_rows {
        assert_eq!(row.split(',').count(), 6);
    }
}

#[test]
fn active_review_output_has_expected_value_fields_and_renamed_actions() {
    let path = write_temp(
        "active_review_input.jsonl",
        "{\"sample_id\":\"a\",\"label\":\"win\"}\n\
         {\"sample_id\":\"a\",\"label\":\"loss\"}\n",
    );
    let scored = run(&["score", path.to_str().unwrap()]);
    assert!(scored.status.success());
    let scored_path = write_temp("active_review_scored.jsonl", &stdout(&scored));

    let out = run(&["active-review", scored_path.to_str().unwrap()]);
    assert!(out.status.success());
    let text = stdout(&out);
    let line = text.lines().next().expect("one output line");
    let value: serde_json::Value = serde_json::from_str(line).unwrap();

    for key in [
        "sample_id",
        "urgency_score",
        "primary_reason",
        "suggested_action",
        "expected_coverage_gain",
        "expected_risk_reduction",
        "cost",
        "utility",
    ] {
        assert!(value.get(key).is_some(), "missing field: {key}");
    }
    let action = value["suggested_action"].as_str().unwrap();
    assert!(
        [
            "request_gold_label",
            "add_evaluator",
            "add_model",
            "increase_budget",
            "add_seed"
        ]
        .contains(&action),
        "suggested_action should use the renamed action set, got {action}"
    );
}

#[test]
fn calibrate_fail_below_target_exits_nonzero_but_still_prints_output() {
    // Training: 9/10 correct (0.90 precision) -> a threshold clears --target-precision 0.80.
    // Held-out: 2/10 correct (0.20 precision) -> falls well short of the target.
    let mut train = String::new();
    for i in 0..9 {
        train.push_str(&format!(
            "{{\"sample_id\":\"t{i}\",\"label\":\"win\",\"gold_label\":\"win\",\"evaluator_id\":\"e0\"}}\n"
        ));
    }
    train.push_str(
        "{\"sample_id\":\"tw\",\"label\":\"loss\",\"gold_label\":\"win\",\"evaluator_id\":\"e0\"}\n",
    );
    let train_path = write_temp("calibrate_fail_train.jsonl", &train);

    let mut heldout = String::new();
    for i in 0..2 {
        heldout.push_str(&format!(
            "{{\"sample_id\":\"h{i}\",\"label\":\"win\",\"gold_label\":\"win\",\"evaluator_id\":\"e0\"}}\n"
        ));
    }
    for i in 0..8 {
        heldout.push_str(&format!(
            "{{\"sample_id\":\"hw{i}\",\"label\":\"loss\",\"gold_label\":\"win\",\"evaluator_id\":\"e0\"}}\n"
        ));
    }
    let heldout_path = write_temp("calibrate_fail_heldout.jsonl", &heldout);

    let out = run(&[
        "calibrate",
        train_path.to_str().unwrap(),
        "--target-precision",
        "0.80",
        "--heldout",
        heldout_path.to_str().unwrap(),
        "--fail-below-target",
    ]);
    assert!(
        !out.status.success(),
        "should exit non-zero when held-out precision falls below target"
    );
    assert!(
        stdout(&out).contains("\"achieved_precision\""),
        "the calibration result should still be printed to stdout before the non-zero exit"
    );
    assert!(stderr(&out).contains("below --target-precision"));
}

#[test]
fn active_review_plan_respects_budget_and_defaults_to_unbounded() {
    let path = write_temp(
        "plan_input.jsonl",
        "{\"sample_id\":\"cheap\",\"label\":\"win\"}\n\
         {\"sample_id\":\"cheap\",\"label\":\"win\"}\n\
         {\"sample_id\":\"cheap\",\"label\":\"win\"}\n\
         {\"sample_id\":\"cheap\",\"label\":\"win\"}\n\
         {\"sample_id\":\"cheap\",\"label\":\"win\"}\n\
         {\"sample_id\":\"cheap\",\"label\":\"win\"}\n\
         {\"sample_id\":\"cheap\",\"label\":\"win\"}\n\
         {\"sample_id\":\"cheap\",\"label\":\"win\"}\n\
         {\"sample_id\":\"cheap\",\"label\":\"win\"}\n\
         {\"sample_id\":\"cheap\",\"label\":\"loss\"}\n\
         {\"sample_id\":\"pricey\",\"label\":\"win\",\"seed\":0,\"score\":0.9}\n\
         {\"sample_id\":\"pricey\",\"label\":\"win\",\"seed\":1,\"score\":0.1}\n\
         {\"sample_id\":\"pricey\",\"label\":\"win\",\"seed\":0,\"score\":0.9}\n\
         {\"sample_id\":\"pricey\",\"label\":\"win\",\"seed\":1,\"score\":0.1}\n",
    );
    let scored = run(&["score", path.to_str().unwrap()]);
    assert!(scored.status.success());
    let scored_path = write_temp("plan_scored.jsonl", &stdout(&scored));

    let plan_path =
        std::env::temp_dir().join(format!("quietset_cli_test_plan_out_{}", std::process::id()));

    let out = run(&[
        "active-review",
        scored_path.to_str().unwrap(),
        "--plan",
        plan_path.to_str().unwrap(),
        "--budget",
        "1.0",
    ]);
    assert!(out.status.success());
    let plan_text = std::fs::read_to_string(&plan_path).unwrap();
    let plan_lines: Vec<&str> = plan_text.lines().collect();
    assert_eq!(
        plan_lines.len(),
        1,
        "budget 1.0 should only afford the single highest-utility (cost 1.0) entry"
    );
    assert!(plan_lines[0].contains("\"sample_id\":\"cheap\""));

    let out_unbounded = run(&[
        "active-review",
        scored_path.to_str().unwrap(),
        "--plan",
        plan_path.to_str().unwrap(),
    ]);
    assert!(out_unbounded.status.success());
    let unbounded_text = std::fs::read_to_string(&plan_path).unwrap();
    assert_eq!(
        unbounded_text.lines().count(),
        2,
        "without --budget, --plan should write every entry"
    );

    std::fs::remove_file(&plan_path).ok();
}

#[test]
fn active_review_observations_populates_target_seed_for_add_seed_action() {
    // seed alternates 0/1 with score flipping 0.9/0.1 -> high seed_sensitivity dominates,
    // so suggested_action is deterministically "add_seed"; max observed seed is 1.
    let obs_text = "{\"sample_id\":\"pricey\",\"label\":\"win\",\"seed\":0,\"score\":0.9}\n\
         {\"sample_id\":\"pricey\",\"label\":\"win\",\"seed\":1,\"score\":0.1}\n\
         {\"sample_id\":\"pricey\",\"label\":\"win\",\"seed\":0,\"score\":0.9}\n\
         {\"sample_id\":\"pricey\",\"label\":\"win\",\"seed\":1,\"score\":0.1}\n";
    let obs_path = write_temp("active_review_targets_obs.jsonl", obs_text);

    let scored = run(&["score", obs_path.to_str().unwrap()]);
    assert!(scored.status.success());
    let scored_path = write_temp("active_review_targets_scored.jsonl", &stdout(&scored));

    let out = run(&["active-review", scored_path.to_str().unwrap()]);
    assert!(out.status.success());
    let value: serde_json::Value =
        serde_json::from_str(stdout(&out).lines().next().unwrap()).unwrap();
    assert_eq!(value["suggested_action"], "add_seed");
    assert!(
        value.get("target_seed").is_none(),
        "target_seed should be absent without --observations"
    );

    let out_with_obs = run(&[
        "active-review",
        scored_path.to_str().unwrap(),
        "--observations",
        obs_path.to_str().unwrap(),
    ]);
    assert!(out_with_obs.status.success());
    let value_with_obs: serde_json::Value =
        serde_json::from_str(stdout(&out_with_obs).lines().next().unwrap()).unwrap();
    assert_eq!(
        value_with_obs["target_seed"], 2,
        "target_seed should be max observed seed (1) + 1"
    );
    assert!(value_with_obs.get("target_budget").is_none());
    assert!(value_with_obs.get("target_evaluator_slot").is_none());
    assert!(value_with_obs.get("target_model_slot").is_none());
}

#[test]
fn active_review_observations_populates_target_budget_for_increase_budget_action() {
    // budget swing (0.1 at budget 2 vs 0.9 at budget 4) makes budget_sensitivity the
    // dominant signal over the unavoidable competing score_mad signal.
    let obs_text = "{\"sample_id\":\"s_budget\",\"score\":0.1,\"budget\":2.0}\n\
         {\"sample_id\":\"s_budget\",\"score\":0.9,\"budget\":4.0}\n";
    let obs_path = write_temp("active_review_targets_budget_obs.jsonl", obs_text);

    let scored = run(&["score", obs_path.to_str().unwrap()]);
    assert!(scored.status.success());
    let scored_path = write_temp(
        "active_review_targets_budget_scored.jsonl",
        &stdout(&scored),
    );

    let out = run(&[
        "active-review",
        scored_path.to_str().unwrap(),
        "--observations",
        obs_path.to_str().unwrap(),
    ]);
    assert!(out.status.success());
    let value: serde_json::Value =
        serde_json::from_str(stdout(&out).lines().next().unwrap()).unwrap();
    assert_eq!(value["suggested_action"], "increase_budget");
    assert_eq!(
        value["target_budget"], 8.0,
        "target_budget should be max observed budget (4.0) x 2"
    );
}

#[test]
fn active_review_observations_populates_target_evaluator_slot_for_add_evaluator_action() {
    // 50/50 label split across two evaluators maximizes label_entropy, which dominates
    // over label_agreement_lcb here.
    let obs_text = "{\"sample_id\":\"s_evaluator\",\"label\":\"win\",\"evaluator_id\":\"e1\"}\n\
         {\"sample_id\":\"s_evaluator\",\"label\":\"win\",\"evaluator_id\":\"e1\"}\n\
         {\"sample_id\":\"s_evaluator\",\"label\":\"loss\",\"evaluator_id\":\"e2\"}\n\
         {\"sample_id\":\"s_evaluator\",\"label\":\"loss\",\"evaluator_id\":\"e2\"}\n";
    let obs_path = write_temp("active_review_targets_evaluator_obs.jsonl", obs_text);

    let scored = run(&["score", obs_path.to_str().unwrap()]);
    assert!(scored.status.success());
    let scored_path = write_temp(
        "active_review_targets_evaluator_scored.jsonl",
        &stdout(&scored),
    );

    let out = run(&[
        "active-review",
        scored_path.to_str().unwrap(),
        "--observations",
        obs_path.to_str().unwrap(),
    ]);
    assert!(out.status.success());
    let value: serde_json::Value =
        serde_json::from_str(stdout(&out).lines().next().unwrap()).unwrap();
    assert_eq!(value["suggested_action"], "add_evaluator");
    assert_eq!(
        value["target_evaluator_slot"], 3,
        "target_evaluator_slot should be distinct evaluator count (2) + 1"
    );
}

#[test]
fn active_review_observations_populates_target_model_slot_for_add_model_action() {
    // no label/budget/seed fields at all, so score_mad is the only live signal.
    let obs_text = "{\"sample_id\":\"s_model\",\"score\":0.1,\"model_id\":\"m1\"}\n\
         {\"sample_id\":\"s_model\",\"score\":0.9,\"model_id\":\"m2\"}\n";
    let obs_path = write_temp("active_review_targets_model_obs.jsonl", obs_text);

    let scored = run(&["score", obs_path.to_str().unwrap()]);
    assert!(scored.status.success());
    let scored_path = write_temp("active_review_targets_model_scored.jsonl", &stdout(&scored));

    let out = run(&[
        "active-review",
        scored_path.to_str().unwrap(),
        "--observations",
        obs_path.to_str().unwrap(),
    ]);
    assert!(out.status.success());
    let value: serde_json::Value =
        serde_json::from_str(stdout(&out).lines().next().unwrap()).unwrap();
    assert_eq!(value["suggested_action"], "add_model");
    assert_eq!(
        value["target_model_slot"], 3,
        "target_model_slot should be distinct model count (2) + 1"
    );
}

#[test]
fn stable_wrong_risk_breakdown_off_by_default() {
    let path = gold_labeled_fixture();
    let out = run(&["stable-wrong-risk", path.to_str().unwrap()]);
    assert!(out.status.success());
    let text = stdout(&out);
    assert!(!text.contains("by_evaluator"));
    assert!(!text.contains("by_model"));
    assert!(!text.contains("by_budget"));
}

#[test]
fn stable_wrong_risk_breakdown_computes_real_evaluator_rate() {
    // "right": unanimous "win", gold "win", evaluator e1 -> Keep, matches gold.
    // "wrong": unanimous "loss", gold "win", evaluator e1 -> Keep, disagrees with gold.
    // e1 touches both Keep samples -> n_keep=2, n_stable_wrong=1, rate=0.5.
    let path = write_temp(
        "stable_wrong_breakdown.jsonl",
        "{\"sample_id\":\"right\",\"label\":\"win\",\"gold_label\":\"win\",\"evaluator_id\":\"e1\"}\n\
         {\"sample_id\":\"right\",\"label\":\"win\",\"gold_label\":\"win\",\"evaluator_id\":\"e1\"}\n\
         {\"sample_id\":\"right\",\"label\":\"win\",\"gold_label\":\"win\",\"evaluator_id\":\"e1\"}\n\
         {\"sample_id\":\"wrong\",\"label\":\"loss\",\"gold_label\":\"win\",\"evaluator_id\":\"e1\"}\n\
         {\"sample_id\":\"wrong\",\"label\":\"loss\",\"gold_label\":\"win\",\"evaluator_id\":\"e1\"}\n\
         {\"sample_id\":\"wrong\",\"label\":\"loss\",\"gold_label\":\"win\",\"evaluator_id\":\"e1\"}\n",
    );
    let out = run(&["stable-wrong-risk", path.to_str().unwrap(), "--breakdown"]);
    assert!(out.status.success());
    let value: serde_json::Value = serde_json::from_str(&stdout(&out)).unwrap();
    let by_evaluator = value["by_evaluator"].as_array().unwrap();
    assert_eq!(by_evaluator.len(), 1);
    assert_eq!(by_evaluator[0]["evaluator_id"], "e1");
    assert_eq!(by_evaluator[0]["n_keep"], 2);
    assert_eq!(by_evaluator[0]["n_stable_wrong"], 1);
    assert!((by_evaluator[0]["stable_wrong_rate"].as_f64().unwrap() - 0.5).abs() < 1e-9);
}