Keep every file inside the rules the repo already wrote down
The 500-line rule and the one-line-comment rule were enforced only from
memory, so they drifted. bench/filesize_check.py now checks the budget on
every tracked source file, and it passes.
Split, with behaviour proven unchanged by comparing each moved function body
against HEAD byte for byte (53 training bodies, 21 benchmark bodies, 0
differences):
db.py 503 -> 396 store_search.py 105
bench/run.py 609 -> 351 query_set.py 80, report.py 211
benchmarks.py 619 -> 424 benchmark_scoring.py 262
training.py 1630 -> 94 training_{common,encode,metrics,datasets,lora,retrieval}
TrainingService stays as a thin facade for the CLI; the facade's parameter
lists are pinned against the module functions by inspect, which is what
catches a leaked self.
Deleted the eight top-level functions no file called, after fixing the scan
that had reported 201 false positives by skipping the defining file. The
v1/v2/v3 sweep found nothing to delete and says so: every remaining version
tag is an on-disk format marker, a migration step, a fixture name, or a
third-party model name.
Collapsed 39 multi-line comment blocks (86 lines) to one line each, keeping
the measured facts and error strings that justify the code.
269 tests pass; ruff clean; mypy 24 errors in 9 files against a 24-in-6
baseline, net zero; pyright 4, the same four HEAD measures.
This commit is contained in:
1 parent
56b94cd58e
commit
9cb46c18d3
47 files changed
+2678
-2272
No files matched your search
+1
-108
@@ -3,7 +3,6 @@ from __future__ import annotations
|
||||
import sqlite3
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
@@ -12,16 +11,7 @@ from .config import Settings, get_settings
|
||||
from .migrations import apply as apply_migrations
|
||||
from .row_codec import atom_params, from_json, row_to_atom, row_to_source, to_json
|
||||
from .schemas import EvidenceAtom, SourceRecord, utc_now
|
||||
from .text_index import fts_expression, fts_terms, index_text, rare_terms
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class FtsPage:
|
||||
"""One page of BM25 hits plus whether the corpus holds more: the cursor is an offset, and a
|
||||
ranking this small re-reads rather than keeps a server-side position."""
|
||||
|
||||
rows: list[tuple[str, float]]
|
||||
has_more: bool
|
||||
from .text_index import index_text
|
||||
|
||||
|
||||
class Database:
|
||||
@@ -173,103 +163,6 @@ class Database:
|
||||
by_id = {row["atom_id"]: row_to_atom(row) for row in rows}
|
||||
return [by_id[atom_id] for atom_id in atom_ids if atom_id in by_id]
|
||||
|
||||
def search_fts(self, query: str, limit: int) -> list[tuple[str, float]]:
|
||||
"""Best BM25 matches over the whole corpus, no filters: the ranking the product needs most days."""
|
||||
return self.search_atoms(query, limit=limit).rows
|
||||
|
||||
def search_atoms(
|
||||
self,
|
||||
query: str,
|
||||
*,
|
||||
limit: int,
|
||||
offset: int = 0,
|
||||
holder: str | None = None,
|
||||
scenario: str | None = None,
|
||||
source_id: str | None = None,
|
||||
modality: str | None = None,
|
||||
since: datetime | None = None,
|
||||
until: datetime | None = None,
|
||||
min_trust: float | None = None,
|
||||
cartridge_ids: list[str] | None = None,
|
||||
as_holder: str | None = None,
|
||||
) -> FtsPage:
|
||||
"""BM25 over the FTS index joined to memories that are open right now.
|
||||
|
||||
Every filter is a `? IS NULL OR` predicate in one literal statement: the ranking never
|
||||
depends on string-built SQL, and the planner sees one shape. A memory with no event time
|
||||
cannot answer a time filter, so it drops out when one is asked for.
|
||||
|
||||
`as_holder` applies the sharing rule: a memory is readable by its owner, by anyone named
|
||||
in its visible_to list, by everyone when it is public, and by anyone when nobody owns it.
|
||||
A memory with no holder cannot be private to a holder that does not exist. Without an
|
||||
asker the query runs over the whole library, which is what an operator debugging wants.
|
||||
"""
|
||||
with self.connect() as connection:
|
||||
expression = fts_expression(rare_terms(connection, fts_terms(query)))
|
||||
rows = connection.execute(
|
||||
"""
|
||||
SELECT f.atom_id, bm25(atom_fts) AS score
|
||||
FROM atom_fts AS f
|
||||
JOIN v_atoms_current AS a ON a.atom_id = f.atom_id
|
||||
WHERE atom_fts MATCH ?
|
||||
AND (? IS NULL OR a.holder_id = ?)
|
||||
AND (? IS NULL OR a.scenario = ?)
|
||||
AND (? IS NULL OR a.source_id = ?)
|
||||
AND (? IS NULL OR a.modality = ?)
|
||||
AND (? IS NULL OR (a.event_time IS NOT NULL AND a.event_time >= ?))
|
||||
AND (? IS NULL OR (a.event_time IS NOT NULL AND a.event_time <= ?))
|
||||
AND (? IS NULL OR (
|
||||
a.trust_json IS NOT NULL
|
||||
AND min(
|
||||
json_extract(a.trust_json, '$.parser'),
|
||||
json_extract(a.trust_json, '$.ocr'),
|
||||
json_extract(a.trust_json, '$.model')
|
||||
) >= ?
|
||||
))
|
||||
AND (? IS NULL OR EXISTS (
|
||||
SELECT 1 FROM cartridge_atoms AS ca
|
||||
WHERE ca.atom_id = f.atom_id
|
||||
AND ca.cartridge_id IN (SELECT value FROM json_each(?))
|
||||
))
|
||||
AND (? IS NULL OR a.holder_id = ''
|
||||
OR json_extract(a.visibility_json, '$.scope') = 'everyone'
|
||||
OR a.holder_id = ?
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM json_each(json_extract(a.visibility_json, '$.holders'))
|
||||
WHERE value = ?
|
||||
))
|
||||
ORDER BY score
|
||||
LIMIT ? OFFSET ?
|
||||
""",
|
||||
(
|
||||
expression,
|
||||
holder,
|
||||
holder,
|
||||
scenario,
|
||||
scenario,
|
||||
source_id,
|
||||
source_id,
|
||||
modality,
|
||||
modality,
|
||||
None if since is None else since.isoformat(),
|
||||
None if since is None else since.isoformat(),
|
||||
None if until is None else until.isoformat(),
|
||||
None if until is None else until.isoformat(),
|
||||
min_trust,
|
||||
min_trust,
|
||||
None if cartridge_ids is None else to_json(cartridge_ids),
|
||||
None if cartridge_ids is None else to_json(cartridge_ids),
|
||||
as_holder,
|
||||
as_holder,
|
||||
as_holder,
|
||||
limit + 1,
|
||||
offset,
|
||||
),
|
||||
).fetchall()
|
||||
matched = [(str(row["atom_id"]), -float(row["score"])) for row in rows[:limit]]
|
||||
return FtsPage(rows=matched, has_more=len(rows) > limit)
|
||||
|
||||
|
||||
def source_id_of_atom(self, atom_id: str) -> str | None:
|
||||
with self.connect() as connection:
|
||||
row = connection.execute("SELECT source_id FROM atoms WHERE atom_id = ?", (atom_id,)).fetchone()
|
||||
|
||||
Reference in new issue
Block a user