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:
@@ -29,6 +29,7 @@ from sqlalchemy import (
|
||||
ForeignKey,
|
||||
Index,
|
||||
Integer,
|
||||
LargeBinary,
|
||||
String,
|
||||
Table,
|
||||
Text,
|
||||
@@ -286,3 +287,69 @@ class Share(UUIDPrimaryKey, Timestamps, Base):
|
||||
|
||||
Index("ix_shares_resource", Share.resource_type, Share.resource_id)
|
||||
Index("ix_shares_principal", Share.principal_type, Share.principal_id)
|
||||
|
||||
|
||||
# --- Semantic index -----------------------------------------------------------
|
||||
# What a chunk belongs to. Strings rather than a foreign key per store, because
|
||||
# one table serving four of them is what stops the chunking, the scoring and the
|
||||
# rebuild being written four times and drifting three ways.
|
||||
CHUNK_DOCUMENT = "document"
|
||||
CHUNK_NOTE = "note"
|
||||
CHUNK_SKILL = "skill"
|
||||
CHUNK_REPORT = "report"
|
||||
|
||||
CHUNK_KINDS = (CHUNK_DOCUMENT, CHUNK_NOTE, CHUNK_SKILL, CHUNK_REPORT)
|
||||
|
||||
|
||||
class Chunk(UUIDPrimaryKey, Timestamps, Base):
|
||||
"""A piece of one library record, and its embedding.
|
||||
|
||||
**Additive, so `sync_schema` creates it at startup with no manual step**, and
|
||||
absent-means-nothing: an instance with no embedding model chosen never writes
|
||||
a row here and the search behaves exactly as it always did.
|
||||
|
||||
`owner_id` is denormalised off the resource. It is not used for
|
||||
authorisation -- `services/sharing.py` is still the only definition of who
|
||||
may see what, and scoring happens before that filter exactly as the
|
||||
full-text path does -- but it is what makes "rebuild this person's index"
|
||||
and "drop everything of theirs" one indexed query rather than four joins.
|
||||
|
||||
No foreign key on `resource_id`, for the reason `Share.principal_id` has
|
||||
none: the column points at one of four tables depending on `resource_type`,
|
||||
which SQLite cannot express. `indexing.forget_resource` deletes the rows.
|
||||
"""
|
||||
|
||||
__tablename__ = "chunks"
|
||||
|
||||
owner_id: Mapped[str] = mapped_column(
|
||||
String(32), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
|
||||
)
|
||||
resource_type: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
resource_id: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
# Where in the record this piece came from, so a set can be rebuilt in order
|
||||
# and a hit can say which part matched.
|
||||
ordinal: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
text: Mapped[str] = mapped_column(Text, default="")
|
||||
|
||||
# float32, little-endian, packed. A BLOB rather than JSON because a 1024
|
||||
# dimension vector is 4KB packed and about 20KB as text, and every one of
|
||||
# them is read on every semantic search.
|
||||
vector: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
|
||||
# How many floats are in it. Stored rather than derived from the length so a
|
||||
# mismatch is a comparison this code refuses rather than one it gets wrong:
|
||||
# changing the embedding model changes the space, and vectors from two
|
||||
# spaces score against each other perfectly happily and mean nothing.
|
||||
dims: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
# Which model wrote it, for the same reason. A rebuild is what reconciles
|
||||
# them; until then the odd ones out are ignored rather than trusted.
|
||||
model_id: Mapped[str] = mapped_column(String(300), default="")
|
||||
# A hash of the text this set was built from. What makes re-indexing an
|
||||
# unchanged record free, and what makes "is this index current?" answerable
|
||||
# without re-embedding anything.
|
||||
source_hash: Mapped[str] = mapped_column(String(64), default="")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Chunk {self.resource_type}:{self.resource_id}#{self.ordinal}>"
|
||||
|
||||
|
||||
Index("ix_chunks_resource", Chunk.resource_type, Chunk.resource_id)
|
||||
|
||||
Reference in New Issue
Block a user