the ZIM fallback fires fast, and reads Russian in Russian (V-508)

Verification, as the task asked. Drove что такое фотосинтез through
/api/chat with the search reachable, with the container stopped, and with
the host blackholed. Kiwix claims the turn in both failure cases, and a
stopped container costs nothing: DNS fails and the ZIM answers inside the
same second.

The blackhole is the case that hurts. The search waited its full 8-second
budget before the ZIM was asked and the turn took 15.4s against 3.5, which
he sits through with nothing being said. So the connect phase alone is now
capped at 1.5s. A reachable instance that is merely slow keeps the whole
budget, because it is fanning out to real engines.

The RU Wikipedia ZIM is on the box (owner moved it into the kiwix zims
dir), and kiwix-serve picked it up. A Cyrillic question now searches
book_ru verbatim and skips the RU->EN rewrite: that rewriter is the
workaround for an English book, and against a Russian one it is a
translation of his own words back at him. Catalog names come from the
filename, not the <name> field — books.name=wikipedia_ru_all returns
nothing.

Measurement in docs/evals/2026-08-05-kiwix-offline-fallback.md. The RU book
answering a driven turn needs a rebuild and is not verified yet.
This commit is contained in:
2026-08-05 15:52:54 +04:00
parent 2de5a339fb
commit 1f38e71d1a
9 changed files with 234 additions and 9 deletions
+27 -6
View File
@@ -8,6 +8,7 @@ import (
"regexp"
"strings"
"time"
"unicode"
"github.com/kami/maven/internal/crawl"
"github.com/kami/maven/internal/ipc"
@@ -695,11 +696,19 @@ func (h *reactiveHandler) queryKiwix(ctx context.Context, t *queryTurn) (string,
ctxK, cancel := context.WithTimeout(ctx, kiwixTimeout)
defer cancel()
// The ZIMs are English and kiwix ranks by keyword overlap, not meaning, so
// a Russian sentence matches nothing at all. The rewriter turns it into a
// handful of English keywords with the resident model.
// A Russian question reads the Russian ZIM verbatim when there is one
// (V-508). Kiwix ranks by keyword overlap rather than meaning, so an English
// book matches a Russian sentence not at all, and the rewriter exists to
// turn the question into English keywords with the resident model. Against a
// Russian book that is a translation of his own words back at him: it costs
// a model call and drops whatever the keywords do not carry.
book, verbatim := h.kiwix.book, false
if h.kiwix.bookRU != "" && hasCyrillic(t.dec.Utterance) {
book, verbatim = h.kiwix.bookRU, true
}
pattern := t.dec.Utterance
if h.kiwix.rewriter != nil {
if h.kiwix.rewriter != nil && !verbatim {
q, err := h.kiwix.rewriter.Rewrite(ctxK, t.dec.Utterance)
if err != nil {
// Fall through to the verbatim question rather than give up. It
@@ -710,7 +719,7 @@ func (h *reactiveHandler) queryKiwix(ctx context.Context, t *queryTurn) (string,
}
}
hits, err := h.kiwix.client.Search(ctxK, pattern, h.kiwix.book, h.kiwix.max)
hits, err := h.kiwix.client.Search(ctxK, pattern, book, h.kiwix.max)
if err != nil {
log.Printf("voice: kiwix: search %q: %v", pattern, err)
return "", false
@@ -722,7 +731,7 @@ func (h *reactiveHandler) queryKiwix(ctx context.Context, t *queryTurn) (string,
// Logged on the way through, not only on failure. Without this there is no
// way to tell from the outside whether an answer came off a ZIM or out of
// the model's weights, and those are the two cases worth telling apart.
log.Printf("voice: kiwix: %q → %d hits, top %q", pattern, len(hits), top.Title)
log.Printf("voice: kiwix: %q in %q → %d hits, top %q", pattern, book, len(hits), top.Title)
// The top hit only, read as an article rather than as a snippet. Kiwix
// builds its snippet from wherever the keyword matched, which on Wikipedia
@@ -754,6 +763,18 @@ func (h *reactiveHandler) queryKiwix(ctx context.Context, t *queryTurn) (string,
return reply, true
}
// hasCyrillic reports whether the text carries a Cyrillic letter, which is the
// whole test for "he asked this in Russian". A question mixing a Latin proper
// noun into a Russian sentence is still Russian, so one letter is enough.
func hasCyrillic(s string) bool {
for _, r := range s {
if unicode.Is(unicode.Cyrillic, r) {
return true
}
}
return false
}
// queryPersonal — stop the walk on a question about him that his own data did
// not answer.
//
+44
View File
@@ -0,0 +1,44 @@
package main
import "testing"
func TestHasCyrillic(t *testing.T) {
for _, s := range []string{"что такое фотосинтез", "кто такой Elon Musk", "фотосинтез"} {
if !hasCyrillic(s) {
t.Errorf("hasCyrillic(%q) = false; it is a Russian question", s)
}
}
for _, s := range []string{"what is photosynthesis", "", "3:2"} {
if hasCyrillic(s) {
t.Errorf("hasCyrillic(%q) = true; there is no Cyrillic in it", s)
}
}
}
// The book choice and the rewrite decision are the same decision: a Russian
// book reads his question as he asked it, an English one needs it translated
// into keywords first (V-508).
func TestKiwixBookChoice(t *testing.T) {
for _, tc := range []struct {
name string
wiring kiwixWiring
utterance string
wantBook string
wantVerb bool
}{
{"a russian question reads the russian book verbatim",
kiwixWiring{book: "en", bookRU: "ru"}, "что такое фотосинтез", "ru", true},
{"an english question reads the english book",
kiwixWiring{book: "en", bookRU: "ru"}, "what is photosynthesis", "en", false},
{"no russian book configured leaves every question on the english one",
kiwixWiring{book: "en"}, "что такое фотосинтез", "en", false},
} {
book, verbatim := tc.wiring.book, false
if tc.wiring.bookRU != "" && hasCyrillic(tc.utterance) {
book, verbatim = tc.wiring.bookRU, true
}
if book != tc.wantBook || verbatim != tc.wantVerb {
t.Errorf("%s: book=%q verbatim=%v, want %q/%v", tc.name, book, verbatim, tc.wantBook, tc.wantVerb)
}
}
}
+10 -2
View File
@@ -19,8 +19,12 @@ type kiwixWiring struct {
client *kiwix.Client
rewriter *kiwix.Rewriter // nil ⇒ the question is searched verbatim
book string
max int
runes int
// bookRU — searched instead of book when the question is Cyrillic, and
// searched verbatim because it is in his language already (V-508). Empty ⇒
// every question goes to book.
bookRU string
max int
runes int
}
// wireKiwix builds the ZIM reader from the `kiwix` block, or returns nil when
@@ -38,9 +42,13 @@ func wireKiwix(cfg *config.Config, c *llm.Client) *kiwixWiring {
w := &kiwixWiring{
client: kiwix.New(kc.URL),
book: kc.Book,
bookRU: kc.BookRU,
max: kc.MaxResults,
runes: kc.SnippetRunes,
}
if kc.BookRU != "" {
log.Printf("voice: kiwix: russian questions read %q verbatim", kc.BookRU)
}
switch {
case !kc.RewriteEnabled():
log.Printf("voice: kiwix at %s (book %q, query rewriting off by config)", kc.URL, kc.Book)