Files
FabledGrimoire/src/grimoire/models/retrieval_judgment.py
T
bvandeusenandClaude Opus 5.5 0f5ef0be63
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 56s
CI & Build / integration (push) Successful in 1m4s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / Build & push image (push) Successful in 44s
rename(547): the server is grimoire — package, image, DB defaults, backups
Step 2 of the Scribe -> Grimoire rename.

- src/scribe -> src/grimoire, and every import, patch target, module path,
  hypercorn entry point and `python -m grimoire.restore` with it; pyproject,
  uv.lock and the frontend package are named grimoire.
- Image git.fabledsword.com/bvandeusen/fabledgrimoire; CI's Postgres service
  runs as grimoire / grimoire_test.
- Compose files default the DB user, database and (prod) volume to grimoire,
  each overridable from .env, so an install created as Scribe keeps its data
  by setting POSTGRES_USER, POSTGRES_DB and PGDATA_VOLUME.
- Backups are written as grimoire-<ts>.tar with format grimoire-backup-tar.
  Archives named scribe-* with the scribe-backup-tar manifest still restore
  and are still counted at the destination (folder and S3), with tests.
- Exports, the MCP auth realm, the ASGI scope key and the persistent logger
  carry the new name.

Kept on purpose: the secret-sealing key label stays "scribe.secrets.v1",
because every stored secret was derived under it.

Plugin identity (plugin name, hooks, env vars, .scribe marker, skills) is
step 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:50:45 -04:00

82 lines
3.6 KiB
Python

from sqlalchemy import BigInteger, Boolean, Float, Index, Integer, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from grimoire.models import Base
from grimoire.models.base import CreatedAtMixin, iso
ON_POINT = "on_point"
ADJACENT = "adjacent"
UNRELATED = "unrelated"
VERDICTS = (ON_POINT, ADJACENT, UNRELATED)
class RetrievalJudgment(Base, CreatedAtMixin):
"""One reviewer's verdict on one candidate of one logged retrieval call.
The relevance half the usage tables cannot hold. `note_usage_events` says
whether a line was OPENED, and because every menu line carries its matched
passage, "not opened" covers an unrelated line, a line whose passage
was enough, and a line the reader already had. Only someone reading the
query beside the line can tell those apart, so this records that reading —
with its reason, because a verdict nobody can re-read is a threshold.
Keyed to the `retrieval_logs` row it judges, by id and FK-free like every
telemetry table here. `query` is COPIED rather than joined: the verdict is
only re-readable beside the words it was judged against.
`rank` and `score` are the candidate's place when the logged query was
re-run for review, which may run past the arm's budget on purpose — the
candidates just under the cut are the ones a budget change would add.
`within_budget` says which side of the cut the line was.
`opened_after` is whether the reviewer's own agent pulled the record within
an hour of the call, read from `note_usage_events`. A correlation, not a
session join — the server has no session identity (see NoteUsageEvent) —
and None when it was not measured.
"""
__tablename__ = "retrieval_judgments"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
user_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
retrieval_log_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
# The judged call's surface, copied so the readout groups without a join.
source: Mapped[str] = mapped_column(Text, nullable=False)
query: Mapped[str | None] = mapped_column(Text, nullable=True)
record_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
# 1-based, in the review's re-run.
rank: Mapped[int] = mapped_column(Integer, nullable=False)
score: Mapped[float | None] = mapped_column(Float, nullable=True)
within_budget: Mapped[bool] = mapped_column(Boolean, nullable=False)
# One of VERDICTS. Plain Text, no CHECK, like the usage tables; the
# service refuses anything else.
verdict: Mapped[str] = mapped_column(Text, nullable=False)
reason: Mapped[str] = mapped_column(Text, nullable=False)
opened_after: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
__table_args__ = (
# One verdict per reviewer per line; judging it again replaces it.
UniqueConstraint(
"retrieval_log_id", "record_id", "user_id",
name="uq_retrieval_judgment_line",
),
Index("ix_retrieval_judgments_source_created", "source", "created_at"),
)
def to_dict(self) -> dict:
return {
"id": self.id,
"created_at": iso(self.created_at),
"user_id": self.user_id,
"retrieval_log_id": self.retrieval_log_id,
"source": self.source,
"query": self.query,
"record_id": self.record_id,
"rank": self.rank,
"score": self.score,
"within_budget": self.within_budget,
"verdict": self.verdict,
"reason": self.reason,
"opened_after": self.opened_after,
}