From 46e4432af985f380f400ea7c1e86c1d114e68f67 Mon Sep 17 00:00:00 2001 From: Clannad47 Date: Mon, 19 Oct 2026 22:47:23 +0800 Subject: [PATCH] fix: keep unscored spans in the Claude Code hook --min-confidence filter The Stop hook filtered spans with (confidence or hallucination_score or 0.0) >= min_confidence, so a span without a score counted as 0.0 and was dropped once --min-confidence was above 0. In --llm-model mode neither the judge path nor the native v2 generative path produces a score, so every flagged span was dropped and the hook reported a clean answer. Keep unscored spans, the same rule as BaseDetector._filter_spans_by_confidence, and document in --help and the integration README that the threshold applies to scored spans only. Closes #117 Signed-off-by: Clannad47 --- CHANGELOG.md | 5 ++ .../integrations/claude_code/README.md | 2 +- .../integrations/claude_code/check_answer.py | 10 ++- tests/test_claude_code_hook_pytest.py | 75 +++++++++++++++++++ 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a809502..78f036e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to LettuceDetect are documented here. +## [Unreleased] + +### Fixed +- Claude Code Stop hook: `--min-confidence` above `0.0` no longer drops every span in `--llm-model` mode. Unscored spans are kept, matching `BaseDetector._filter_spans_by_confidence` (#117) + ## [0.2.3] - 2026-08-14 ### Added diff --git a/lettucedetect/integrations/claude_code/README.md b/lettucedetect/integrations/claude_code/README.md index b0c94d8..b69de55 100644 --- a/lettucedetect/integrations/claude_code/README.md +++ b/lettucedetect/integrations/claude_code/README.md @@ -60,7 +60,7 @@ found the agent receives them and revises. | `--llm-model ID` | Generative detector or LLM judge; use `KRLabsOrg/lettucedect-v2-qwen-2b` for typed spans in one pass | | `--llm-base-url URL` | OpenAI-compatible endpoint for `--llm-model` (e.g. `vllm serve KRLabsOrg/lettucedect-v2-qwen-2b`) | | `--context-file F` | Grounding passage file, repeatable; default `context.md` | -| `--min-confidence X` | Report only spans at or above this confidence | +| `--min-confidence X` | Report only spans at or above this confidence; spans the detector did not score (every span in `--llm-model` mode) are always reported | Models: `KRLabsOrg/lettucedect-base-modernbert-en-v1` for prose RAG answers; `KRLabsOrg/lettucedect-v2-mmbert-base` plus diff --git a/lettucedetect/integrations/claude_code/check_answer.py b/lettucedetect/integrations/claude_code/check_answer.py index eb6b019..24ab1a7 100644 --- a/lettucedetect/integrations/claude_code/check_answer.py +++ b/lettucedetect/integrations/claude_code/check_answer.py @@ -195,7 +195,11 @@ def main(argv: list[str] | None = None) -> int: "--min-confidence", type=float, default=0.0, - help="Report only spans at or above this confidence (default 0.0)", + help=( + "Report only spans at or above this confidence (default 0.0). " + "Applies to scored spans only; spans the detector did not score " + "(every span in --llm-model mode) are always reported." + ), ) args = parser.parse_args(argv) @@ -231,7 +235,9 @@ def main(argv: list[str] | None = None) -> int: flagged = [ s for s in spans - if (s.get("confidence", s.get("hallucination_score")) or 0.0) >= args.min_confidence + if args.min_confidence <= 0.0 + or s.get("confidence", s.get("hallucination_score")) is None + or s.get("confidence", s.get("hallucination_score")) >= args.min_confidence ] if not flagged: return 0 diff --git a/tests/test_claude_code_hook_pytest.py b/tests/test_claude_code_hook_pytest.py index ac51381..6c9e6ba 100644 --- a/tests/test_claude_code_hook_pytest.py +++ b/tests/test_claude_code_hook_pytest.py @@ -168,6 +168,81 @@ def test_min_confidence_filters(self, tmp_path): ) assert code == 0 + def test_min_confidence_keeps_unscored_spans(self, tmp_path, capsys): + """Test min confidence keeps unscored spans.""" + ctx = self.make_context(tmp_path) + code = run_main( + ["--model-path", "stub", "--context-file", ctx, "--min-confidence", "0.5"], + {"transcript_path": str(FIXTURE)}, + spans=[ + {"text": "69 million", "hallucination_score": None}, + {"text": "wrong version", "start": 0, "end": 13}, + ], + ) + assert code == 2 + captured = capsys.readouterr() + assert "69 million" in captured.err + assert "wrong version" in captured.err + + def test_min_confidence_mixes_scored_and_unscored(self, tmp_path, capsys): + """Test min confidence keeps unscored spans and drops scored ones.""" + ctx = self.make_context(tmp_path) + code = run_main( + ["--model-path", "stub", "--context-file", ctx, "--min-confidence", "0.5"], + {"transcript_path": str(FIXTURE)}, + spans=[ + {"text": "low confidence", "confidence": 0.1}, + {"text": "native typed span", "category": "unsupported_addition"}, + {"text": "high confidence", "confidence": 0.9}, + ], + ) + assert code == 2 + err = capsys.readouterr().err + assert "low confidence" not in err + assert "native typed span" in err + assert "high confidence" in err + + def test_llm_mode_unscored_spans_reported_above_threshold(self, tmp_path, capsys): + """Test llm mode unscored spans reported above threshold.""" + ctx = self.make_context(tmp_path) + seen = {} + + class StubDetector: + """Stub LLM detector returning native-path spans without confidence.""" + + def __init__(self, **kwargs): + seen.update(kwargs) + + def predict(self, **kwargs): + return [ + { + "text": "added retry logic", + "start": 0, + "end": 19, + "category": "unsupported_addition", + } + ] + + with ( + patch("sys.stdin", io.StringIO(json.dumps({"transcript_path": str(FIXTURE)}))), + patch("lettucedetect.models.inference.HallucinationDetector", StubDetector), + ): + code = main( + [ + "--llm-model", + "KRLabsOrg/lettucedect-v2-qwen-2b", + "--llm-base-url", + "http://localhost:8001/v1", + "--context-file", + ctx, + "--min-confidence", + "0.5", + ] + ) + assert seen["base_url"] == "http://localhost:8001/v1" + assert code == 2 + assert "added retry logic" in capsys.readouterr().err + def test_invalid_stdin_exits_0(self): """Test invalid stdin exits 0.""" with patch("sys.stdin", io.StringIO("not json")):