Add the ResponseCache protocol, make_cache_key and InMemoryCache
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 56/100
Línea de trabajo
Start with lettucedetect/detectors/cache.py and the key construction sites in llm.py named in the issue; run uv run --extra dev pytest tests/test_llm_detector_pytest.py -q first. Implement and export the protocol, key helper, sync base, and in-memory backend in the named module, then add the reusable contract in tests/cache_contract.py and configure pytest in pyproject.toml. Done means the contract and existing suite pass, including key compatibility, TTL, async behavior, thread safety, and cancellation checks.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Parent: #144 · Epic: #126 · Blocked by: #135, #131 (for the recorded key strings)
Branch from stateless_core; open the PR against it.
Problem
Caching is welded into LLMDetector through a concrete CacheManager, with keys built inline and no model version in the key, so a redeployed model serves stale replies. There is no protocol a service can implement.
Current behavior
lettucedetect/detectors/cache.py:28_hashjoins parts with||and hashes with SHA-256.- Keys are built inline: judge
llm.py:408, native:468(extra"native"), verify:506(extra"verify"). No model version. CacheManager.get/set/deleteare sync only and have no TTL (cache.py:31-47).
Steps
- Add
lettucedetect/detectors/response_cache.pywith theResponseCacheprotocol:get(key),set(key, value, *, ttl_seconds=None),delete(key)and oneasync defpair for each:aget(key),aset(key, value, *, ttl_seconds=None),adelete(key). Each async method takes the same arguments as its sync pair and returns the same value. Keys are opaque strings, values are strings, a missing or expired key returnsNone. - Add
make_cache_key(prompt, model, model_version, temperature, kind),kindin"judge" | "native" | "verify". Withmodel_version=Noneit must equal today's_hashoutput (use the keys recorded in #131); with a version, add the version as an extra part. - Add
SyncCacheBase, a small base class for backends that only have sync storage: the backend implementsget,setanddelete, and the base suppliesaget,aset(withttl_seconds) andadeleteby running them in a worker thread (asyncio.to_thread).InMemoryCacheandFileCache(#146) build on it, so the async pairs are written once. - Add
InMemoryCachewith TTL, thread-safe, onSyncCacheBase;asethonoursttl_secondsexactly likeset. - Add
tests/cache_contract.py(value reads back, missing isNone, TTL expiry, delete, each sync method equals its async pair,asetwithttl_secondsexpires likeset, a value written bysetreads viaagetand the reverse, after anasettask is cancelled, the cache is valid: the key reads as the old value or the new one, never a partial or corrupt value, and laterget/setwork; concurrent writers lose no key from threads and fromasyncio.gatheroveraset) and run it againstInMemoryCache. - Add
asyncio_mode = "auto"under[tool.pytest.ini_options]inpyproject.toml(pytest-asynciois already in thedevextra) so the async tests are plainasync def. If #141 already added the line, leave it.
Acceptance criteria
-
SyncCacheBaseexists: a test backend that implements onlyget,setanddeleteon it passes the contract suite, async methods included, and a class implementing the protocol directly is not forced to use it. - Protocol, key function,
SyncCacheBaseandInMemoryCacheexist and are exported fromlettucedetect.detectors. -
make_cache_key(..., None, ...)equals the recorded keys for all three kinds; different versions give different keys. -
ResponseCachedeclaresaget,asetandadeleteas coroutine functions with the same arguments asget,setanddelete; a backend lacking one fails a protocol check in the suite. - The async methods of
InMemoryCacherun off the event loop's thread, and the loop keeps running while they wait (ticker test). - The contract suite passes for
InMemoryCacheand is reusable for other backends (#146 uses it). -
pyproject.tomlsetsasyncio_mode = "auto"; the async contract tests are plainasync def, and the full existing suite still passes. - No detector changes; existing tests pass; follows CONTRIBUTING "Errors and logging".
Start here
uv run --extra dev pytest tests/test_llm_detector_pytest.py -q
- Lenguaje dominante
- Python
- Estrellas
- 615
- Forks
- 60
- Merge medio
- 1 d 8 h
- PR fusionados (30 d)
- 10
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de KRLabsOrg/LettuceDetect
-
Stop-hook --min-confidence drops every --llm-model span (missing confidence treated as 0.0), reporting hallucinated answers as cleanPosiblemente ocupada @Clannad47 la tomó hace 19 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
KRLabsOrg/LettuceDetect#117 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
KRLabsOrg/LettuceDetect#108 ·
Los mantenedores suelen responder en 1 día
-
documentation
Dificultad 2/5 Medio día Aptitud para principiantes 70/100
KRLabsOrg/LettuceDetect#104 ·
Los mantenedores suelen responder en 1 día
-
ready-for-agent
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
KRLabsOrg/LettuceDetect#181 ·
Los mantenedores suelen responder en 1 día
-
ready-for-agent
Dificultad 5/5 Más de una semana Aptitud para principiantes 22/100
KRLabsOrg/LettuceDetect#180 ·
Los mantenedores suelen responder en 1 día
Todos los issues de KRLabsOrg/LettuceDetect
Issues similares
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
cunla/fakeredis-py#636 ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
opensafely-core/job-server#6180 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
area:models enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
bug needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
newrelic/newrelic-python-agent#1913 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
Los mantenedores suelen responder en 1 día