Finding a thing that does not use your words

Three pieces, and the first one is that they are all optional.

Extraction stops being constants. Upload size, image edge, JPEG quality, PDF
pages, extracted characters, orphan age and the text-extension list are settings
now, read through a process-level snapshot rather than a session -- `prepare` and
everything under it are called from routes, tool runners and the startup sweep,
and several of those have no session in hand. Two things deliberately stayed
constants: the decompression-bomb guard, which is a guard and not a preference,
and ORPHAN_AGE, which would have been evaluated at import if it stayed in the
signature and pinned the shipped 24 hours whatever anybody set.

An embedding model is picked from the models an administrator flagged for it, and
one that has since lost its flag is *named* rather than dropped from the picker:
a setting that vanishes is one nobody can tell from a setting never made. Nothing
here is required. Choosing none means no chunk rows, no requests, and
retrieval.search returning exactly what fts.search_ids returns in exactly that
order -- asserted, because it is what makes this safe to land on an instance that
never asked for it.

The two rankings are fused by reciprocal rank fusion: ranks and not scores,
because bm25 is a corpus-dependent negative and cosine is 0..1, and normalising
them onto one scale means picking a constant nobody can tune without a labelled
set they do not have. RRF's one constant is famously insensitive and degrades to
whichever list is non-empty -- which is what turns "no embedding model" into a
branch that does not exist.

A record scores as its best chunk rather than its average, or a long document
about something else outranks a short one that says the thing. Width and model
are stored beside every vector and a mismatch is skipped, because vectors from
two spaces score against each other perfectly happily and mean nothing -- a
search that works and is wrong is the worst failure this can have, and a model
change now leaves stale rows ignored rather than trusted.

Indexing is fired and forgotten, and how a change is noticed is a session event
rather than a call in each of the ten library writers. That is a departure from
this codebase's taste for explicit seams, for the reason tool_label is a Jinja
global: a step every writer has to remember is one that gets forgotten, and here
forgetting is silent -- the record saves, keyword search still finds it, and only
its recall goes stale. Chunks are embedded before anything is deleted, so a
failure leaves the old index rather than half a new one.

Also: `embeddings` joins the model capabilities, and the three tool flags that
had shipped with no checkbox -- canvas, scheduling and helpers -- have one.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jaroslav Beneš
2026-08-06 16:15:21 +02:00
parent 78e5717f77
commit 20bb569b00
27 changed files with 2563 additions and 55 deletions
+87 -3
View File
@@ -188,12 +188,15 @@ def test_empty_files_are_rejected():
def test_oversized_files_are_rejected():
with pytest.raises(files_service.FileError) as caught:
files_service.prepare(b"x" * (files_service.MAX_UPLOAD_BYTES + 1), "huge.txt")
files_service.prepare(b"x" * (files_service.limits().max_upload_bytes + 1), "huge.txt")
assert "MB" in str(caught.value)
def test_extracted_text_is_capped(monkeypatch):
monkeypatch.setattr(files_service, "MAX_EXTRACTED_CHARS", 50)
"""Through the setting rather than the constant. The constant is only the
default now; what `prepare` reads is the snapshot, which is the thing that
would have gone on returning 120,000 if the wiring were wrong."""
monkeypatch.setattr(files_service, "_LIMITS", files_service.Limits(max_extracted_chars=50))
prepared = files_service.prepare(b"x" * 500, "long.txt")
assert len(prepared.extracted_text) == 50
assert prepared.truncated is True
@@ -446,7 +449,7 @@ def test_documents_reach_a_model_without_vision(client: TestClient, db, chat_wit
def test_truncation_is_declared_to_the_model(client: TestClient, db, chat_with_model, monkeypatch):
"""A model asked about page 400 should be able to say it did not see it."""
monkeypatch.setattr(files_service, "MAX_EXTRACTED_CHARS", 20)
monkeypatch.setattr(files_service, "_LIMITS", files_service.Limits(max_extracted_chars=20))
client.post("/api/files", files={"file": ("big.txt", b"y" * 200, "text/plain")})
attachment = db.scalar(select(Attachment))
client.post(
@@ -600,3 +603,84 @@ def test_a_browser_serialising_the_form_actually_sends_the_attachment(
content = turns(chat_service.build_request(db, chat))[0]["content"]
assert isinstance(content, list), "the image never reached the model"
assert any(p.get("type") == "image_url" for p in content)
# --- The extraction settings ---------------------------------------------------
# The constants above are defaults now, and what `prepare` actually reads is a
# process-level snapshot. Every one of these failures would be silent: a limit
# that looks configured and is not.
def test_a_saved_limit_reaches_the_snapshot(db, client, registered):
from lembas.services import settings_store
client.post(
"/admin/extraction",
data={
"max_upload_mb": "5",
"max_image_edge": "800",
"jpeg_quality": "70",
"max_pdf_pages": "10",
"max_extracted_chars": "2000",
"orphan_hours": "3",
"extra_text_extensions": "env\n.conf",
},
follow_redirects=False,
)
bounds = files_service.limits()
assert bounds.max_upload_bytes == 5 * 1024 * 1024
assert bounds.max_extracted_chars == 2000
assert bounds.max_image_edge == 800
assert settings_store.extraction(db)["extra_text_extensions"] == ["env", ".conf"]
def test_an_extra_extension_gets_a_leading_dot(db, client, registered):
"""Typed both ways by different people, and a mapping somebody has to get
right twice is one they get wrong once."""
client.post(
"/admin/extraction", data={"extra_text_extensions": "env"}, follow_redirects=False
)
assert files_service.limits().media_type_for(".env") == "text/plain"
def test_an_unknown_extension_is_still_stored_as_text(db):
"""Decodability is what decides. The list only picks a media type, which is
why an unlisted extension has always worked and must go on working."""
prepared = files_service.prepare(b"hello there", "notes.wat")
assert prepared.kind == "text"
assert prepared.extension == ".txt"
def test_a_number_out_of_range_is_clamped(db, client, registered):
client.post(
"/admin/extraction",
data={"max_upload_mb": "99999", "jpeg_quality": "1"},
follow_redirects=False,
)
bounds = files_service.limits()
assert bounds.max_upload_bytes == 512 * 1024 * 1024
assert bounds.jpeg_quality == 30
def test_the_snapshot_is_dropped_when_the_page_saves(db, client, registered):
"""Read once per process. A save that did not drop it would take effect at
the next restart, which is the failure this codebase keeps cataloguing."""
assert files_service.limits().max_upload_bytes == 20 * 1024 * 1024
client.post("/admin/extraction", data={"max_upload_mb": "1"}, follow_redirects=False)
assert files_service.limits().max_upload_bytes == 1024 * 1024
def test_only_an_administrator_may_change_extraction(client, registered):
client.post("/auth/logout", follow_redirects=False)
client.post(
"/auth/register",
data={"name": "Sam", "email": "sam@shire.test", "password": "potatoes-po-ta-toes"},
follow_redirects=False,
)
assert client.post("/admin/extraction", data={"max_upload_mb": "1"}).status_code == 403
assert client.post("/admin/extraction/search", data={}).status_code == 403
assert client.post("/admin/extraction/rebuild", data={}).status_code == 403