Skip to content

test: record golden fixtures for today's detector API (#127) - #182

Merged
hslev merged 4 commits into
stateless_corefrom
127-record-golden-fixtures-for-todays-detector-api
Nov 5, 2026
Merged

hslev merged 4 commits into
stateless_corefrom
127-record-golden-fixtures-for-todays-detector-api

Conversation

@hslev

@hslev hslev commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Characterization ("golden") tests that pin today's output of every public detector entry point the detectors epic (#126) rebuilds or deprecates, so each later adapter can prove it keeps today's shape. Tests and fixtures only: no production code and no existing test changed.

tests/
├── golden.py                                  # assert_golden(name, data); UPDATE_GOLDEN=1 rewrites; unified diff on failure
├── test_golden_transformer_pytest.py          # #128  stub model + local tokenizer
├── test_golden_llm_pytest.py                  # #129  fake LLMClient
├── test_golden_rag_fact_checker_pytest.py     # #130  fake fact checker
├── test_golden_factory_cache_pytest.py        # #131  make_detector, HallucinationDetector, CacheManager
└── fixtures/golden/*.json                     # 56 checked-in recordings

One commit per sub-ticket (#128, #129, #130, #131). Recordings include today's defects (llm_defect_*, rag_merge_overlap_text_shorter_than_offsets_*); the PR that fixes one updates its fixture as an intended change.

Notes for review:

  • assert_golden rounds floats to 6 places on both sides, so cross-platform softmax noise cannot flake the suite.
  • Quirks recorded as-is: token output ends with the trailing [SEP]; at prob == 0.5 the single-group path (argmax) and multi-group path (>= 0.5) disagree; LLMDetector defaults to "spans" while the facade defaults to "tokens".
  • The RAG 0.7 "single word" confidence is only reachable with a 4th triplet element; the fixture uses one.
  • The recorded cache keys hash the full prompt, so editing a prompt template or few-shot file will fail cache_llm_detector_keys (and invalidates users' caches): re-record on purpose.

Related issue

Closes #127, #128, #129, #130, #131. Parent: #126.

Base is stateless_core, not main, so GitHub will not close these automatically; close them by hand after the merge.

Type of change

  • Bug fix
  • Feature
  • Documentation
  • Tests
  • Refactor or maintenance

Testing

Before: the new tests do not exist and a changed detector output goes unnoticed. After, a changed output fails with a diff. Each recording was checked by temporarily changing a production constant (transformer.py >= 0.5, llm.py _SUPPORTED_TOKEN_PROB, rag_fact_checker.py 0.7, cache.py "||"); the matching golden tests failed, then the change was reverted.

  • ruff format --check lettucedetect/ lettucedetect_api/ tests/ (74 files already formatted)
  • ruff check lettucedetect/ lettucedetect_api/ tests/ --extend-exclude lettucedetect/integrations/ (all checks passed)
  • HF_HUB_OFFLINE=1 OPENAI_API_KEY='' uv run --extra dev pytest (265 passed, run with HF_HUB_OFFLINE=1 and no OPENAI_API_KEY)

Checklist

  • I kept the PR focused on one change.
  • I added or updated tests/docs when needed.
  • I checked that no secrets, API keys, or credentials are included.

Rights & sign-off (required)

  • I certify that I have the right to submit this code and that it may be
    distributed under the repository's MIT license
    (see CONTRIBUTING).

Merge Danger

Door: two-way. Test-only; reverting removes the files.

Blast Radius: tests. Every later detector change that alters recorded output now fails until its fixture is updated (UPDATE_GOLDEN=1).

hslev and others added 4 commits November 5, 2026 15:37
Add tests/golden.py (assert_golden, UPDATE_GOLDEN=1 to rewrite, readable
unified diff, floats rounded to 6 places) and record predict, predict_prompt
and predict_prompt_batch in "tokens" and "spans" format on a stub model with
the local tokenizer, plus a multi-group context and spans typed by a stub typer.

Recorded as-is: "tokens" output ends with the trailing [SEP], and the
multi-group path flags a token at prob >= 0.5 while the single-group path
uses argmax.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Record predict, predict_prompt and predict_prompt_batch in "tokens" and
"spans" format through a fake LLMClient: the judge with defaults, zero_shot,
include_reasoning, each include_taxonomy form (True, list, flat dict, nested
dict), verify, and native mode with and without include_reasoning.

Also record today's behaviour in the cases the epic changes (llm_defect_*):
a reply without hallucination_list, an unparseable reply, an item that is
not found in the answer, and spans without confidence.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…130)

Record "tokens", "spans" and "detailed" output of predict, predict_prompt
and predict_prompt_batch through a fake fact checker, a triplet matched
only by the single-word fallback (confidence 0.7), and the merge defect
where a merged span's text is shorter than its offsets
(rag_merge_overlap_text_shorter_than_offsets_*, for the fix PR to update).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ior (#131)

Record the class make_detector returns for "transformer", "llm" and
"rag_fact_checker" and the error for an unknown method; assert and record
that HallucinationDetector.predict, predict_prompt and predict_prompt_batch
return what the detector alone returns (transformer, LLM, RAG); and pin the
CacheManager file after set/delete calls, the _hash strings, and the judge,
native and verify keys LLMDetector writes for fixed prompts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@hslev hslev closed this Nov 5, 2026
@hslev hslev reopened this Nov 5, 2026
@hslev
hslev marked this pull request as ready for review November 5, 2026 15:02
@hslev
hslev merged commit 52bad80 into stateless_core Nov 5, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant