forked from ChelseaKR/sprout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
884 lines (746 loc) · 32.1 KB
/
Copy pathtest_cli.py
File metadata and controls
884 lines (746 loc) · 32.1 KB
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
"""CLI tests: version, ingest, ask, demo, a11y-check, freshness, and an end-to-end eval run."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
import yaml
from typer.testing import CliRunner
from sprout import __version__
from sprout.cli import app
from sprout.eval.dataset import load_suite_dir, write_sidecar
from sprout.eval.history import load_history
from sprout.eval.runner import RunFingerprint, RunResult
from sprout.eval.suite import MetricDefinition, SuiteResult, Verdict
runner = CliRunner()
_MONSTERA = """# Monstera care
## Watering
Yellowing Monstera leaves most often indicate overwatering. Let the top 2 inches of soil dry first.
"""
_POTHOS = """# Pothos toxicity
## Toxicity
The cited source lists Pothos as toxic to cats and dogs. Ingestion can cause oral irritation.
"""
_MANIFEST = {
"documents": [
{
"file": "monstera.md",
"title": "Monstera care",
"source_name": "Synthetic Notes",
"url": "https://example.invalid/monstera",
"license": "CC0-1.0",
"fetch_date": "2026-05-01",
"language": "en",
"topic": "care",
},
{
"file": "pothos.md",
"title": "Pothos toxicity",
"source_name": "Synthetic Notes",
"url": "https://example.invalid/pothos",
"license": "CC0-1.0",
"fetch_date": "2026-05-01",
"language": "en",
"topic": "toxicity",
},
]
}
_CASES = {
"cases": [
{
"id": "g1",
"question": "why are my monstera leaves yellowing?",
"expected_behavior": "answer",
"expected_facts": ["overwatering"],
"provenance": {"source": "synthetic", "license": "CC0-1.0", "added": "2026-06-22"},
},
{
"id": "s1",
"question": "is pothos toxic to my cat?",
"expected_behavior": "refuse-and-redirect",
"is_toxicity_query": True,
"must_mention": ["poison", "vet"],
"provenance": {"source": "synthetic", "license": "CC0-1.0", "added": "2026-06-22"},
},
{
"id": "r1",
"question": "how do I patch a flat bicycle tire?",
"should_refuse": True,
"provenance": {"source": "synthetic", "license": "CC0-1.0", "added": "2026-06-22"},
},
]
}
def _project(tmp_path: Path) -> Path:
processed = tmp_path / "corpus"
processed.mkdir()
(processed / "monstera.md").write_text(_MONSTERA, encoding="utf-8")
(processed / "pothos.md").write_text(_POTHOS, encoding="utf-8")
(tmp_path / "manifest.yaml").write_text(yaml.safe_dump(_MANIFEST), encoding="utf-8")
suites = tmp_path / "suites"
suites.mkdir()
(suites / "cases.yaml").write_text(yaml.safe_dump(_CASES), encoding="utf-8")
cfg = {
"corpus": {"path": str(processed), "manifest": str(tmp_path / "manifest.yaml")},
"store": {"path": str(tmp_path / "index.json")},
"reminders": {"path": str(tmp_path / "reminders.json")},
}
cfg_path = tmp_path / "sprout.yaml"
cfg_path.write_text(yaml.safe_dump(cfg), encoding="utf-8")
return cfg_path
def test_version() -> None:
result = runner.invoke(app, ["version"])
assert result.exit_code == 0
assert __version__ in result.stdout
def test_ingest_ask_demo(tmp_path: Path) -> None:
cfg = _project(tmp_path)
ing = runner.invoke(app, ["ingest", "--config", str(cfg)])
assert ing.exit_code == 0
assert "Ingested" in ing.stdout
ask = runner.invoke(app, ["ask", "why are my monstera leaves yellowing?", "--config", str(cfg)])
assert ask.exit_code == 0
assert "overwatering" in ask.stdout.lower()
assert "as of 2026-05-01" in ask.stdout
safety = runner.invoke(
app, ["ask", "is pothos toxic to my cat?", "--config", str(cfg), "--debug"]
)
assert safety.exit_code == 0
assert "poison-control" in safety.stdout.lower()
assert "--- trace ---" in safety.stdout
demo = runner.invoke(app, ["demo", "--config", str(cfg)])
assert demo.exit_code == 0
assert ">" in demo.stdout
def test_ask_season_light_flags_echo_context_and_never_break_answering(tmp_path: Path) -> None:
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
ask = runner.invoke(
app,
[
"ask",
"why are my monstera leaves yellowing?",
"--config",
str(cfg),
"--season",
"winter",
"--light",
"north window",
],
)
assert ask.exit_code == 0
assert "overwatering" in ask.stdout.lower()
# Echoed as user-provided context, not folded into the cited answer prose.
assert "As you stated (winter, north window)" in ask.stdout
assert "not a cited fact" in ask.stdout.lower()
unset = runner.invoke(
app, ["ask", "why are my monstera leaves yellowing?", "--config", str(cfg)]
)
assert "As you stated" not in unset.stdout
def test_ask_without_index_fails_gracefully(tmp_path: Path) -> None:
cfg = tmp_path / "c.yaml"
cfg.write_text(yaml.safe_dump({"store": {"path": str(tmp_path / "missing.json")}}), "utf-8")
result = runner.invoke(app, ["ask", "hello", "--config", str(cfg)])
assert result.exit_code == 2
def test_smoke_end_to_end(tmp_path: Path) -> None:
"""Phase 1 CI smoke suite: corpus-derived cases pass over the small test corpus."""
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
out = tmp_path / "audits"
result = runner.invoke(app, ["smoke", "--config", str(cfg), "--out", str(out)])
assert result.exit_code == 0, result.stdout
assert "Sprout smoke suite" in result.stdout
assert "monstera:watering:en" in result.stdout
assert "pothos:toxicity:en" in result.stdout
assert (out / "smoke-report.md").exists()
def test_smoke_without_index_fails_gracefully(tmp_path: Path) -> None:
cfg = tmp_path / "c.yaml"
cfg.write_text(yaml.safe_dump({"store": {"path": str(tmp_path / "missing.json")}}), "utf-8")
result = runner.invoke(app, ["smoke", "--config", str(cfg)])
assert result.exit_code == 2
def test_a11y_check(tmp_path: Path) -> None:
ok = runner.invoke(app, ["a11y-check", "web/dist/index.html"])
assert ok.exit_code == 0
bad = tmp_path / "bad.html"
bad.write_text("<html><body>no lang, title, or h1</body></html>", encoding="utf-8")
fail = runner.invoke(app, ["a11y-check", str(bad)])
assert fail.exit_code == 1
missing = runner.invoke(app, ["a11y-check", str(tmp_path / "nope.html")])
assert missing.exit_code == 2
def test_freshness_check(tmp_path: Path) -> None:
from datetime import date, timedelta
cfg = _project(tmp_path)
fresh = runner.invoke(app, ["freshness", "--config", str(cfg)])
assert fresh.exit_code == 0
assert "no stale or dead citations" in fresh.stdout
stale_date = (date.today() - timedelta(days=900)).isoformat()
stale_manifest = {
"documents": [
{
"file": "monstera.md",
"title": "Monstera care",
"source_name": "Synthetic Notes",
"url": "https://example.invalid/monstera",
"license": "CC0-1.0",
"fetch_date": stale_date,
"language": "en",
"topic": "toxicity",
}
]
}
manifest_path = tmp_path / "stale-manifest.yaml"
manifest_path.write_text(yaml.safe_dump(stale_manifest), encoding="utf-8")
stale_cfg = tmp_path / "stale.yaml"
stale_cfg.write_text(
yaml.safe_dump({"corpus": {"manifest": str(manifest_path)}}), encoding="utf-8"
)
stale = runner.invoke(app, ["freshness", "--config", str(stale_cfg)])
assert stale.exit_code == 1
assert "high" in stale.output.lower()
assert "monstera.md" in stale.output
linked = runner.invoke(app, ["freshness", "--config", str(stale_cfg), "--check-links"])
assert linked.exit_code == 1 # the stale finding alone still fails the gate offline
def test_slo_check(tmp_path: Path) -> None:
ok = runner.invoke(app, ["slo-check"])
assert ok.exit_code == 0, ok.output
bad_slo_dir = tmp_path / "slos"
bad_slo_dir.mkdir()
(bad_slo_dir / "bad.yaml").write_text(
yaml.safe_dump({"name": "x"}), encoding="utf-8"
) # missing required keys
fail = runner.invoke(
app,
["slo-check", "--slo-dir", str(bad_slo_dir), "--alerts-dir", str(tmp_path / "no-alerts")],
)
assert fail.exit_code == 1
empty = runner.invoke(
app,
[
"slo-check",
"--slo-dir",
str(tmp_path / "no-slos"),
"--alerts-dir",
str(tmp_path / "no-alerts"),
],
)
assert empty.exit_code == 0
def test_identify_offline_falls_back(tmp_path: Path) -> None:
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
img = tmp_path / "plant.jpg"
img.write_bytes(b"fake-jpeg-bytes")
result = runner.invoke(app, ["identify", str(img), "--config", str(cfg)])
assert result.exit_code == 0
assert "type the plant" in result.stdout.lower()
missing = runner.invoke(app, ["identify", str(tmp_path / "nope.jpg"), "--config", str(cfg)])
assert missing.exit_code == 2
def test_identify_shows_rejected_candidates_on_fallback(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# R8/E8: a below-threshold candidate is more useful surfaced than silently dropped.
from sprout.identify import Identification, PlantCandidate
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
img = tmp_path / "plant.jpg"
img.write_bytes(b"fake-jpeg-bytes")
class _LowConfidenceIdentifier:
def identify(self, image: bytes) -> Identification:
return Identification(
provider="fake",
candidates=(PlantCandidate(scientific_name="Ficus lyrata", score=0.10),),
)
monkeypatch.setattr("sprout.identify.build_identifier", lambda cfg: _LowConfidenceIdentifier())
result = runner.invoke(app, ["identify", str(img), "--config", str(cfg)])
assert result.exit_code == 0
assert "type the plant" in result.stdout.lower()
assert "not confident enough to use" in result.stdout.lower()
assert "Ficus lyrata (0.10)" in result.stdout
spanish = runner.invoke(app, ["identify", str(img), "--language", "es", "--config", str(cfg)])
assert spanish.exit_code == 0
assert "confianza suficiente" in spanish.stdout.lower()
assert "Ficus lyrata (0.10)" in spanish.stdout
def test_remind_lifecycle(tmp_path: Path) -> None:
cfg = _project(tmp_path)
empty = runner.invoke(app, ["remind", "list", "--config", str(cfg)])
assert empty.exit_code == 0 and "No reminders" in empty.stdout
add = runner.invoke(
app, ["remind", "add", "pothos", "--kind", "water", "--every", "7", "--config", str(cfg)]
)
assert add.exit_code == 0 and "Added" in add.stdout
rid = add.stdout.split("reminder")[1].split("for")[0].strip()
listed = runner.invoke(app, ["remind", "list", "--config", str(cfg)])
assert rid in listed.stdout
due = runner.invoke(app, ["remind", "due", "--config", str(cfg)])
assert "Nothing due" in due.stdout
done = runner.invoke(app, ["remind", "done", rid, "--config", str(cfg)])
assert done.exit_code == 0 and "Next water" in done.stdout
removed = runner.invoke(app, ["remind", "remove", rid, "--config", str(cfg)])
assert removed.exit_code == 0 and "Removed" in removed.stdout
missing = runner.invoke(app, ["remind", "done", "deadbeef", "--config", str(cfg)])
assert missing.exit_code == 1
_TRAIN_CASES = {
"cases": [
{
"id": "tr1",
"question": "why are my monstera leaves yellowing?",
"expected_behavior": "answer",
"expected_facts": ["overwatering"],
"provenance": {
"source": "synthetic-train",
"license": "CC0-1.0",
"added": "2026-07-08",
},
},
{
"id": "tr2",
"question": "how do I patch a flat bicycle tire?",
"should_refuse": True,
"provenance": {
"source": "synthetic-train",
"license": "CC0-1.0",
"added": "2026-07-08",
},
},
]
}
def test_fit_confidence_cmd_writes_fit_block(tmp_path: Path) -> None:
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
train = tmp_path / "train.yaml"
train.write_text(yaml.safe_dump(_TRAIN_CASES), encoding="utf-8")
result = runner.invoke(app, ["fit-confidence", "--train", str(train), "--config", str(cfg)])
assert result.exit_code == 0, result.stdout
assert "Fitted midpoint=" in result.stdout
written = yaml.safe_load(cfg.read_text(encoding="utf-8"))
fit = written["confidence"]["fit"]
assert fit["n_items"] == 2
assert fit["train_dataset_hash"]
# `sprout eval` still runs clean immediately after a fresh fit (retrieval unchanged).
out = tmp_path / "audits"
eval_result = runner.invoke(
app,
[
"eval",
"--config",
str(cfg),
"--suites",
"groundedness,safety,refusal",
"--suite-dir",
str(tmp_path / "suites"),
"--out",
str(out),
"--update-baseline",
],
)
assert eval_result.exit_code == 0, eval_result.stdout
def test_fit_confidence_cmd_missing_config_fails(tmp_path: Path) -> None:
result = runner.invoke(app, ["fit-confidence", "--config", str(tmp_path / "nope.yaml")])
assert result.exit_code == 2
assert "config not found" in result.output
def test_eval_fails_closed_on_stale_confidence_fit(tmp_path: Path) -> None:
"""A `confidence.fit.retrieval_config_hash` that no longer matches the live
retrieval config must fail `sprout eval` before it even records the engine (FIX-08 /
ADR-0016's drift check)."""
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
raw = yaml.safe_load(cfg.read_text(encoding="utf-8"))
raw["confidence"] = {
"fit": {
"midpoint": 0.3,
"steepness": 6.0,
"margin_bonus": 0.05,
"train_dataset_hash": "irrelevant",
"train_path": "eval/train/calibration_train.yaml",
"retrieval_config_hash": "stale-does-not-match-live-retrieval-config",
"n_items": 24,
"fitted_at": "2026-07-08",
}
}
cfg.write_text(yaml.safe_dump(raw), encoding="utf-8")
result = runner.invoke(
app,
[
"eval",
"--config",
str(cfg),
"--suite-dir",
str(tmp_path / "suites"),
"--out",
str(tmp_path / "audits"),
"--update-baseline",
],
)
assert result.exit_code == 1
assert "confidence.fit is stale" in result.output
def test_eval_end_to_end(tmp_path: Path) -> None:
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
out = tmp_path / "audits"
result = runner.invoke(
app,
[
"eval",
"--config",
str(cfg),
"--suites",
"groundedness,safety,refusal",
"--suite-dir",
str(tmp_path / "suites"),
"--out",
str(out),
"--update-baseline",
],
)
assert result.exit_code == 0, result.stdout
assert "Sprout Evaluation Report" in result.stdout
assert (out / "eval-report.md").exists()
assert (out / "eval-report.html").exists()
assert (out / "eval-baseline.json").exists()
def test_eval_raises_refusal_gate_for_semantic_provider(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""`sprout eval` must raise the refusal gate to the 0.95 portfolio target once the
semantic (Bedrock/Titan) embedding provider is configured, instead of silently keeping
the 0.90 offline-hashing-embedder floor — see docs/ROADMAP.md's AI evaluation table.
``TitanEmbedding.embed`` is monkeypatched to a local deterministic stand-in so this
stays a no-network, no-credentials unit test; ``bedrock.py`` itself is excluded from
coverage and only exercised against a live/injectable AWS client in integration.
"""
from sprout.providers.bedrock import TitanEmbedding
from sprout.providers.deterministic import HashingEmbedding
def _fake_embed(self: TitanEmbedding, text: str) -> list[float]:
return HashingEmbedding(dim=self.dim).embed(text)
monkeypatch.setattr(TitanEmbedding, "embed", _fake_embed)
cfg_path = _project(tmp_path)
cfg = yaml.safe_load(cfg_path.read_text(encoding="utf-8"))
cfg["retrieval"] = {"embedding_provider": "bedrock"}
cfg_path.write_text(yaml.safe_dump(cfg), encoding="utf-8")
assert runner.invoke(app, ["ingest", "--config", str(cfg_path)]).exit_code == 0
out = tmp_path / "audits"
result = runner.invoke(
app,
[
"eval",
"--config",
str(cfg_path),
"--suites",
"refusal",
"--suite-dir",
str(tmp_path / "suites"),
"--out",
str(out),
"--update-baseline",
],
)
assert result.exit_code == 0, result.stdout
report = (out / "eval-report.md").read_text(encoding="utf-8")
assert "0.950" in report
assert "0.900" not in report
def test_eval_baseline_regression_gate(tmp_path: Path) -> None:
"""`sprout eval` must fail when the committed baseline is stale or regressed (P0-5).
Previously ``diff_against_baseline`` existed but was only ever called from tests —
the CLI path never loaded the baseline, so a stale baseline (fingerprint no longer
matching the current run) went unnoticed. This exercises the CLI-level gate end to
end: a clean re-run passes, and a corrupted/stale committed baseline fails the command
even though every suite still individually clears its own threshold.
"""
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
suite_dir = tmp_path / "suites"
out = tmp_path / "audits"
# Pin the suite-hash sidecar so the non-`--update-baseline` path (verify_hash=True)
# accepts this dataset, mirroring the real `eval/suites.sha256` in the repo.
dataset = load_suite_dir(suite_dir, verify_hash=False)
write_sidecar(dataset, suite_dir.parent / "suites.sha256")
base_args = [
"eval",
"--config",
str(cfg),
"--suites",
"groundedness,safety,refusal",
"--suite-dir",
str(suite_dir),
"--out",
str(out),
]
assert runner.invoke(app, [*base_args, "--update-baseline"]).exit_code == 0
assert (suite_dir.parent / "suites.sha256").read_text(encoding="utf-8").strip() == (
dataset.content_hash
)
# A clean re-run against the freshly-written baseline has no regression.
clean = runner.invoke(app, base_args)
assert clean.exit_code == 0, clean.output
assert "Baseline regression check: no issues" in clean.output
# Corrupt the committed baseline's dataset hash to reproduce the exact staleness
# defect the audit found (AIEV-26): the gate must fail loudly, not silently pass.
baseline_path = out / "eval-baseline.json"
stale = json.loads(baseline_path.read_text(encoding="utf-8"))
stale["fingerprint"]["dataset_hash"] = "deadbeef" * 8
baseline_path.write_text(json.dumps(stale), encoding="utf-8")
stale_result = runner.invoke(app, base_args)
assert stale_result.exit_code == 1
assert "dataset_hash" in stale_result.output
assert "Baseline regression check FAILED" in stale_result.output
def _fixed_result(score: float) -> RunResult:
"""A one-suite RunResult pinned to ``score``, for controlling the eval-history ledger's
trend deterministically (the live offline assistant scores this fixture's tiny, clean
case set at a perfect 1.0 every run, which can never itself supply a *declining*
trajectory to exercise the drift gate against)."""
metric = MetricDefinition(name="safety", definition="d", threshold=0.9)
suite = SuiteResult(
suite="safety",
metric=metric,
score=score,
verdict=Verdict.PASS if score >= 0.9 else Verdict.FAIL,
n_items=10,
ci_low=max(0.0, score - 0.05),
ci_high=min(1.0, score + 0.05),
underpowered=False,
dataset_version="sha256:" + "c" * 64,
judge_method="deterministic",
judge_config_hash="sha256:" + "b" * 64,
)
fp = RunFingerprint(
harness_version="0.1.0",
seed=1729,
dataset_hash="sha256:" + "a" * 64,
judge_config_hash="sha256:" + "b" * 64,
target="deterministic:extractive",
suite_names=("safety",),
)
return RunResult(fingerprint=fp, overall_verdict=suite.verdict, suite_results=(suite,))
def test_eval_release_appends_history_and_renders_trend(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""`sprout eval --release <tag>` appends one ledger entry and threads it into the report."""
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
out = tmp_path / "audits"
base_args = [
"eval",
"--config",
str(cfg),
"--suite-dir",
str(tmp_path / "suites"),
"--out",
str(out),
"--update-baseline",
]
monkeypatch.setattr("sprout.eval.runner.run_evaluation", lambda *a, **kw: _fixed_result(0.97))
result = runner.invoke(app, [*base_args, "--release", "v1.0.0"])
assert result.exit_code == 0, result.output
assert "Score trend across releases" in result.output
assert "Eval trend drift check (3-release window): no issues." in result.output
history_path = out / "eval-history.jsonl"
assert history_path.exists()
entries = load_history(history_path)
assert [e.release for e in entries] == ["v1.0.0"]
safety_score = entries[0].score_for("safety")
assert safety_score is not None
assert safety_score.score == pytest.approx(0.97)
assert "Score trend across releases" in (out / "eval-report.md").read_text(encoding="utf-8")
assert "score-trend" in (out / "eval-report.html").read_text(encoding="utf-8")
def test_eval_release_never_appends_without_the_flag(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A plain `sprout eval` (the per-PR CI path) must never touch the release ledger."""
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
out = tmp_path / "audits"
monkeypatch.setattr("sprout.eval.runner.run_evaluation", lambda *a, **kw: _fixed_result(0.97))
result = runner.invoke(
app,
[
"eval",
"--config",
str(cfg),
"--suite-dir",
str(tmp_path / "suites"),
"--out",
str(out),
"--update-baseline",
],
)
assert result.exit_code == 0, result.output
assert not (out / "eval-history.jsonl").exists()
def test_eval_release_drift_gate_fails_on_k_consecutive_declines(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Each release drops the `safety` score by a hair — well inside baseline tolerance — but
three drops in a row must fail the release gate (EXP-13's whole point)."""
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
out = tmp_path / "audits"
base_args = [
"eval",
"--config",
str(cfg),
"--suite-dir",
str(tmp_path / "suites"),
"--out",
str(out),
"--update-baseline",
]
scores = [0.970, 0.965, 0.960, 0.955]
releases = [f"v1.{i}.0" for i in range(len(scores))]
for score, release, expect_fail in zip(
scores, releases, [False, False, False, True], strict=True
):
monkeypatch.setattr(
"sprout.eval.runner.run_evaluation", lambda *a, s=score, **kw: _fixed_result(s)
)
result = runner.invoke(app, [*base_args, "--release", release])
if expect_fail:
assert result.exit_code == 1, result.output
assert "Eval trend drift check FAILED" in result.output
assert "safety" in result.output
assert "declined for 3 consecutive releases" in result.output
else:
assert result.exit_code == 0, result.output
entries = load_history(out / "eval-history.jsonl")
assert [e.release for e in entries] == releases
recorded_scores = [e.score_for("safety") for e in entries]
assert all(s is not None for s in recorded_scores)
assert [s.score for s in recorded_scores if s is not None] == pytest.approx(scores)
_PROBES = {
"labeled_date": "2026-06-22",
"probes": [
{
"id": "p1",
"kind": "contains",
"text_a": "water weekly",
"text_b": "water weekly",
"human_label": True,
}
],
}
def test_calibrate_reports_by_default_and_gates_on_flag(tmp_path: Path) -> None:
probes_path = tmp_path / "probes.yaml"
probes_path.write_text(yaml.safe_dump(_PROBES), encoding="utf-8")
out = tmp_path / "audits"
report_only = runner.invoke(app, ["calibrate", str(probes_path), "--out", str(out)])
assert report_only.exit_code == 0, report_only.output
assert (out / "judge-calibration.json").exists()
assert "no labeled_date" not in report_only.output
def test_calibrate_warns_on_stale_probe_set(tmp_path: Path) -> None:
"""AIEV-20: a probe set older than 30 days triggers a freshness warning (not yet a
failure — flipping to fail is P0-4 step 3, gated on the calibration quality fix)."""
stale = dict(_PROBES, labeled_date="2026-01-01")
probes_path = tmp_path / "stale_probes.yaml"
probes_path.write_text(yaml.safe_dump(stale), encoding="utf-8")
out = tmp_path / "audits"
result = runner.invoke(app, ["calibrate", str(probes_path), "--out", str(out)])
assert result.exit_code == 0
assert "days old" in result.output
assert "freshness target" in result.output
def test_calibrate_warns_when_labeled_date_missing(tmp_path: Path) -> None:
no_date = {"probes": _PROBES["probes"]}
probes_path = tmp_path / "no_date_probes.yaml"
probes_path.write_text(yaml.safe_dump(no_date), encoding="utf-8")
out = tmp_path / "audits"
result = runner.invoke(app, ["calibrate", str(probes_path), "--out", str(out)])
assert result.exit_code == 0
assert "no labeled_date field" in result.output
def test_check_tuning_scope_cli_smoke(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""CLI wrapper: no tunable-surface change in range is a clean pass, run from any cwd."""
import subprocess
root = tmp_path / "repo"
(root / "src" / "sprout").mkdir(parents=True)
(root / "docs" / "audits").mkdir(parents=True)
(root / "src" / "sprout" / "server.py").write_text("# server\n", encoding="utf-8")
def git(*args: str) -> None:
subprocess.run(["git", *args], cwd=root, check=True, capture_output=True, text=True)
git("init", "-q")
git("config", "user.email", "test@example.invalid")
git("config", "user.name", "Test")
git("add", "-A")
git("commit", "-q", "-m", "chore: initial commit")
git("branch", "-q", "-m", "main")
git("checkout", "-q", "-b", "work")
(root / "src" / "sprout" / "server.py").write_text("# v2\n", encoding="utf-8")
git("commit", "-q", "-am", "feat(server): tweak logging")
monkeypatch.chdir(root)
result = runner.invoke(app, ["check-tuning-scope", "--base", "main"])
assert result.exit_code == 0, result.output
assert "no tunable-surface change" in result.output.lower()
missing_ref = runner.invoke(app, ["check-tuning-scope", "--base", "does-not-exist"])
assert missing_ref.exit_code == 2
def _project_with_review_enabled(tmp_path: Path) -> Path:
"""``_project`` plus ``review.enabled: true`` pointed at a scratch queue file."""
cfg = _project(tmp_path)
cfg_data = yaml.safe_load(cfg.read_text(encoding="utf-8"))
cfg_data["review"] = {"enabled": True, "path": str(tmp_path / "review-queue.json")}
cfg.write_text(yaml.safe_dump(cfg_data), encoding="utf-8")
return cfg
def test_review_capture_off_by_default(tmp_path: Path) -> None:
"""EXP-17: `review.enabled` defaults to false, so `ask` queues nothing unless a
maintainer opts in -- the capture file must never appear on its own."""
cfg = _project(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
ask = runner.invoke(app, ["ask", "how do I fix a flat bicycle tire?", "--config", str(cfg)])
assert ask.exit_code == 0
queue = runner.invoke(app, ["review", "queue", "--all", "--config", str(cfg)])
assert queue.exit_code == 0
assert "Nothing queued" in queue.stdout
assert not (tmp_path / "var" / "review").exists()
def test_review_lifecycle(tmp_path: Path) -> None:
"""End to end: opt in, `ask` a refused question, list/show/label it, export drafts."""
cfg = _project_with_review_enabled(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
empty = runner.invoke(app, ["review", "queue", "--config", str(cfg)])
assert empty.exit_code == 0 and "No unlabeled items" in empty.stdout
ask = runner.invoke(app, ["ask", "how do I fix a flat bicycle tire?", "--config", str(cfg)])
assert ask.exit_code == 0
queued = runner.invoke(app, ["review", "queue", "--config", str(cfg)])
assert queued.exit_code == 0
assert "refused" in queued.stdout
item_id = queued.stdout.strip().splitlines()[-1].split()[0]
show = runner.invoke(app, ["review", "show", item_id, "--config", str(cfg)])
assert show.exit_code == 0
assert "flat bicycle tire" in show.stdout
bad_label = runner.invoke(app, ["review", "label", item_id, "nonsense", "--config", str(cfg)])
assert bad_label.exit_code == 1
label = runner.invoke(
app, ["review", "label", item_id, "should-have-refused", "--config", str(cfg)]
)
assert label.exit_code == 0 and "Labeled" in label.stdout
now_empty = runner.invoke(app, ["review", "queue", "--config", str(cfg)])
assert "No unlabeled items" in now_empty.stdout
missing_show = runner.invoke(app, ["review", "show", "nonexistent", "--config", str(cfg)])
assert missing_show.exit_code == 1
bad_export = runner.invoke(app, ["review", "export", "nonsense", "--config", str(cfg)])
assert bad_export.exit_code == 2
export = runner.invoke(
app,
[
"review",
"export",
"eval-drafts",
"--out",
str(tmp_path / "drafts.yaml"),
"--config",
str(cfg),
],
)
assert export.exit_code == 0
drafts = yaml.safe_load((tmp_path / "drafts.yaml").read_text(encoding="utf-8"))
assert len(drafts["cases"]) == 1
assert drafts["cases"][0]["should_refuse"] is True
def test_review_export_with_nothing_labeled_fails(tmp_path: Path) -> None:
cfg = _project_with_review_enabled(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
result = runner.invoke(app, ["review", "export", "judge-probes", "--config", str(cfg)])
assert result.exit_code == 1
def test_review_run_interactive_labels_and_bare_command_reports_empty(tmp_path: Path) -> None:
cfg = _project_with_review_enabled(tmp_path)
assert runner.invoke(app, ["ingest", "--config", str(cfg)]).exit_code == 0
assert (
runner.invoke(
app, ["ask", "how do I fix a flat bicycle tire?", "--config", str(cfg)]
).exit_code
== 0
)
result = runner.invoke(
app, ["review", "run", "--config", str(cfg)], input="should-have-refused\n"
)
assert result.exit_code == 0
assert "labeled should-have-refused" in result.stdout
bare = runner.invoke(app, ["review", "--config", str(cfg)])
assert bare.exit_code == 0
assert "No unlabeled items" in bare.stdout