Translation cache key ignores service, model and system prompt, entries never expire

Aperta Adatta ai principianti
#480 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php
Ambito
backend

Direzione di ricerca

Il rapporto individua TranslateService::translate() come il punto in cui viene creata e memorizzata la chiave della cache. Leggi prima questo metodo e l’uso della cache che lo circonda; verifica che i test di traduzione esistenti coprano il comportamento del servizio, del modello, del prompt e della scadenza. Il lavoro è completato quando le traduzioni con configurazioni diverse non riutilizzano una voce incompatibile e le voci memorizzate nella cache hanno una durata limitata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Steps to reproduce
  1. Configure a distributed memory cache in config.php (e.g. APCu or Redis) (e.g. 'memcache.distributed' => '\OC\Memcache\Redis'), as recommended for production instances.
  2. Configure two different translation services, or a single service whose model you can change in the settings.
  3. Translate the same text with the same source and target languages using the first service or model, then using the second one.
Expected behaviour

The second translation is actually performed by the second service or model, since different providers and models produce different translations of the same input.

Actual behaviour

The second translation is served from the cache entry written by the first one. The provider is never called again.

The cache key is built in TranslateService::translate() as:

$cacheKey = $sourceLanguageCode . '/' . $targetLanguageCode . '/' . md5($chunk);

The key contains neither the service, nor the model, nor the system prompt, so a cache entry written through one configuration is necessarily hit by any other configuration translating the same text between the same languages. Entries are also stored without a TTL ($cache->set($cacheKey, ...)), so once written, a translation is served forever, even after the underlying model or service configuration is changed or fixed.

This also means the cache silently loses most of its value as soon as an instance uses more than one translation service, since hits become essentially random across configurations.

Side observation: on instances without a distributed memory cache configured, the distributed cache falls back to a per-request backend, so the translation cache never hits at all and every translation is recomputed. If that fallback is considered acceptable for small instances it may be worth documenting; if not, the cache could use a different backend.

Suggested direction

Include at least the service id and the model in the cache key (and ideally the system prompt, which is part of the translation behaviour), and store entries with a finite TTL so that configuration or model changes eventually invalidate stale translations.

Reproduction on a live instance

Reproduced end-to-end on a live Nextcloud 35.0.3 instance with APCu as the distributed cache, using a local mock provider that echoes what it receives and logs every request. Two distinct translation services were configured, both pointing at the mock, and the same text was translated three times in a row:

Call Service Model Provider called?
1 A model-aaa yes
2 A model-bbb no — served from cache
3 B model-ccc no — served from cache

Only the first of the three translations reached the provider, although each used a different model or an entirely different service.

Assisted by Devin:SWE-2.1

Lingua principale
PHP
Stelle
78
Fork
42
Merge medio
2g 21h
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nextcloud/integration_openai

Tutte le issue di nextcloud/integration_openai

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.