CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 9s
CI & Build / integration (push) Failing after 33s
CI & Build / Python tests (push) Failing after 37s
CI & Build / TypeScript typecheck (push) Successful in 54s
CI & Build / Build & push image (push) Skipped
`when_to_apply` is not metadata. `rule_document` embeds a rule as
`{title} — {trigger}` / `When to apply: {trigger}\n\n{statement}`, the trigger
appearing twice so purpose dominates a short vector — the shape note 2485
measured on snippets (a 0.153 top-to-second gap against 0.010–0.023 for
everything else). Without one the document silently becomes title + statement:
a DIFFERENT shape, ranked against a corpus it does not match, with nothing to
report it. Every bar and every rank in the system assumes one shape.
`create_preference` has refused an empty trigger since it shipped. The two rule
creators defaulted it to "" — so the shape was enforced for the record kind
that guides and optional for the kind that binds.
The guard lives in the SERVICE, because both doors reach it: the MCP tools and
the frontend's fast path in routes/rulebooks.py. Written in either alone, the
other could still create a rule that never fires. The route keeps a matching
check for the STATUS CODE only (400, not the 404 it maps ValueError to).
update_rule refuses to EMPTY an existing trigger, checked after the mutation so
it covers `clear=[...]`, an emptied form input, and any route added later.
Deliberately asked as "did this edit remove one" rather than "does one exist":
a rule predating the guard has none, and refusing to save it would freeze
precisely the unreachable records that most need fixing.
Deliberately not following arose_from_id, which the human door exempts itself
from because provenance is about auditing what the AGENT changed. That reasoning
does not reach this field — a missing trigger is not a missing explanation, it
is a rule that does not work, and it fails an operator as badly as a session.
15 test fixtures across 6 files were creating rules with no trigger. They now
pass one; that they did not is the point — curation is not a guarantee.
Step 1 of milestone 416 "Retrieval stops guessing a bar". First because every
later step assumes one document shape, and it is much cheaper to guarantee
before a corpus grows than to backfill after.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01821k5B3Ysecp9fNYs92Kuy
434 lines
17 KiB
Python
434 lines
17 KiB
Python
"""Rulebook / topic REST endpoints.
|
|
|
|
Wraps services/rulebooks.py. Standard Scribe auth: get_current_user_id() is the
|
|
authenticated owner; the service enforces ownership scoping.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from quart import Blueprint, jsonify, request
|
|
|
|
from scribe.auth import get_current_user_id, login_required
|
|
import scribe.services.rulebooks as rulebooks_svc
|
|
from scribe.services.trash import delete as trash_delete
|
|
from scribe.services.rule_usage import (
|
|
empty_rule_usage, record_rule_pulled, usage_for_rules,
|
|
)
|
|
|
|
rulebooks_bp = Blueprint("rulebooks", __name__, url_prefix="/api")
|
|
|
|
|
|
|
|
# ── Rulebooks ───────────────────────────────────────────────────────────
|
|
|
|
@rulebooks_bp.get("/rulebooks")
|
|
@login_required
|
|
async def list_rulebooks():
|
|
rows = await rulebooks_svc.list_rulebooks(get_current_user_id())
|
|
return jsonify({"rulebooks": [rb.to_dict() for rb in rows]})
|
|
|
|
|
|
@rulebooks_bp.post("/rulebooks")
|
|
@login_required
|
|
async def create_rulebook():
|
|
data = await request.get_json() or {}
|
|
title = (data.get("title") or "").strip()
|
|
if not title:
|
|
return jsonify({"error": "title is required"}), 400
|
|
rb = await rulebooks_svc.create_rulebook(
|
|
user_id=get_current_user_id(),
|
|
title=title,
|
|
description=data.get("description", ""),
|
|
)
|
|
return jsonify(rb.to_dict()), 201
|
|
|
|
|
|
@rulebooks_bp.get("/rulebooks/<int:rulebook_id>")
|
|
@login_required
|
|
async def get_rulebook(rulebook_id: int):
|
|
rb = await rulebooks_svc.get_rulebook(rulebook_id, get_current_user_id())
|
|
if rb is None:
|
|
return jsonify({"error": "rulebook not found"}), 404
|
|
return jsonify(rb.to_dict())
|
|
|
|
|
|
@rulebooks_bp.patch("/rulebooks/<int:rulebook_id>")
|
|
@login_required
|
|
async def update_rulebook(rulebook_id: int):
|
|
data = await request.get_json() or {}
|
|
fields = {k: v for k, v in data.items() if k in ("title", "description")}
|
|
rb = await rulebooks_svc.update_rulebook(rulebook_id, get_current_user_id(), **fields)
|
|
if rb is None:
|
|
return jsonify({"error": "rulebook not found"}), 404
|
|
return jsonify(rb.to_dict())
|
|
|
|
|
|
@rulebooks_bp.delete("/rulebooks/<int:rulebook_id>")
|
|
@login_required
|
|
async def delete_rulebook(rulebook_id: int):
|
|
await trash_delete(get_current_user_id(), "rulebook", rulebook_id)
|
|
return "", 204
|
|
|
|
|
|
# ── Topics ──────────────────────────────────────────────────────────────
|
|
|
|
@rulebooks_bp.get("/rulebooks/<int:rulebook_id>/topics")
|
|
@login_required
|
|
async def list_topics(rulebook_id: int):
|
|
try:
|
|
rows = await rulebooks_svc.list_topics(rulebook_id, get_current_user_id())
|
|
except ValueError as exc:
|
|
return jsonify({"error": str(exc)}), 404
|
|
return jsonify({"topics": [t.to_dict() for t in rows]})
|
|
|
|
|
|
@rulebooks_bp.post("/rulebooks/<int:rulebook_id>/topics")
|
|
@login_required
|
|
async def create_topic(rulebook_id: int):
|
|
data = await request.get_json() or {}
|
|
title = (data.get("title") or "").strip()
|
|
if not title:
|
|
return jsonify({"error": "title is required"}), 400
|
|
try:
|
|
topic = await rulebooks_svc.create_topic(
|
|
rulebook_id=rulebook_id,
|
|
user_id=get_current_user_id(),
|
|
title=title,
|
|
description=data.get("description", ""),
|
|
order_index=data.get("order_index", 0),
|
|
)
|
|
except ValueError as exc:
|
|
return jsonify({"error": str(exc)}), 404
|
|
return jsonify(topic.to_dict()), 201
|
|
|
|
|
|
@rulebooks_bp.patch("/rulebook-topics/<int:topic_id>")
|
|
@login_required
|
|
async def update_topic(topic_id: int):
|
|
data = await request.get_json() or {}
|
|
fields = {
|
|
k: v for k, v in data.items()
|
|
if k in ("title", "description", "order_index")
|
|
}
|
|
topic = await rulebooks_svc.update_topic(topic_id, get_current_user_id(), **fields)
|
|
if topic is None:
|
|
return jsonify({"error": "topic not found"}), 404
|
|
return jsonify(topic.to_dict())
|
|
|
|
|
|
@rulebooks_bp.delete("/rulebook-topics/<int:topic_id>")
|
|
@login_required
|
|
async def delete_topic(topic_id: int):
|
|
if await trash_delete(get_current_user_id(), "topic", topic_id) is None:
|
|
return jsonify({"error": "topic not found"}), 404
|
|
return "", 204
|
|
|
|
|
|
# ── Rules ───────────────────────────────────────────────────────────────
|
|
|
|
@rulebooks_bp.get("/rules")
|
|
@login_required
|
|
async def list_rules():
|
|
def _opt_int(name):
|
|
raw = request.args.get(name)
|
|
return int(raw) if raw else None
|
|
|
|
try:
|
|
rulebook_id = _opt_int("rulebook_id")
|
|
topic_id = _opt_int("topic_id")
|
|
project_id = _opt_int("project_id")
|
|
except ValueError:
|
|
return jsonify({"error": "rulebook_id, topic_id, project_id must be integers"}), 400
|
|
|
|
uid = get_current_user_id()
|
|
rows = await rulebooks_svc.list_rules(
|
|
user_id=uid,
|
|
rulebook_id=rulebook_id,
|
|
topic_id=topic_id,
|
|
project_id=project_id,
|
|
)
|
|
items = [r.to_dict() for r in rows]
|
|
# One aggregate for the whole page — a per-row lookup here would be N+1 by
|
|
# construction, the same reason the snippet list does it this way. Every
|
|
# row gets the key, zero-filled, so the UI renders "never surfaced" rather
|
|
# than having to treat a missing field as a state. That matters more here
|
|
# than for snippets: every rule on every install predates this table, so
|
|
# for a while the zero-filled shape IS the common case.
|
|
usage = await usage_for_rules([int(it["id"]) for it in items])
|
|
for it in items:
|
|
it["usage"] = usage.get(int(it["id"]), empty_rule_usage())
|
|
return jsonify({"rules": items})
|
|
|
|
|
|
@rulebooks_bp.post("/rulebook-topics/<int:topic_id>/rules")
|
|
@login_required
|
|
async def create_rule(topic_id: int):
|
|
data = await request.get_json() or {}
|
|
title = (data.get("title") or "").strip()
|
|
statement = (data.get("statement") or "").strip()
|
|
if not title or not statement:
|
|
return jsonify({"error": "title and statement are required"}), 400
|
|
try:
|
|
rule = await rulebooks_svc.create_rule(
|
|
topic_id=topic_id,
|
|
user_id=get_current_user_id(),
|
|
title=title,
|
|
statement=statement,
|
|
why=data.get("why", ""),
|
|
how_to_apply=data.get("how_to_apply", ""),
|
|
order_index=data.get("order_index", 0),
|
|
when_to_apply=data.get("when_to_apply", ""),
|
|
# The human door carries `kind` too, and without the MCP door's
|
|
# required provenance: an operator editing their own preference
|
|
# owes nobody an explanation. That requirement is about auditing
|
|
# what the AGENT changed, not what they did themselves.
|
|
kind=data.get("kind", "rule"),
|
|
arose_from_id=data.get("arose_from_id", 0) or 0,
|
|
verify_with=data.get("verify_with", ""),
|
|
expires_when=data.get("expires_when", ""),
|
|
)
|
|
except ValueError as exc:
|
|
return jsonify({"error": str(exc)}), 404
|
|
return jsonify(await rulebooks_svc.rule_detail(
|
|
get_current_user_id(), rule, data.get("system_ids"),
|
|
)), 201
|
|
|
|
|
|
@rulebooks_bp.get("/rules/<int:rule_id>")
|
|
@login_required
|
|
async def get_rule(rule_id: int):
|
|
uid = get_current_user_id()
|
|
rule = await rulebooks_svc.get_rule(rule_id, uid)
|
|
if rule is None:
|
|
return jsonify({"error": "rule not found"}), 404
|
|
# `rest_` rather than `mcp_`, and the prefix is load-bearing: "is this rule
|
|
# dead weight?" is served by any pull, but "did that injected hint land?"
|
|
# — the question this arm exists to answer — is served by AGENT pulls only.
|
|
# A person clicking through the rule list says nothing about the hint.
|
|
record_rule_pulled(user_id=uid, rule_id=int(rule.id), source="rest_rule")
|
|
return jsonify(await rulebooks_svc.rule_detail(uid, rule))
|
|
|
|
|
|
@rulebooks_bp.patch("/rules/<int:rule_id>")
|
|
@login_required
|
|
async def update_rule(rule_id: int):
|
|
data = await request.get_json() or {}
|
|
uid = get_current_user_id()
|
|
fields = {
|
|
k: v for k, v in data.items()
|
|
if k in ("title", "statement", "why", "how_to_apply", "order_index",
|
|
"when_to_apply", "kind", "arose_from_id",
|
|
"verify_with", "expires_when")
|
|
}
|
|
# No clear_fields here: a form sends "" for an emptied input, and the
|
|
# service normalises "" to NULL for every nullable text column. The MCP
|
|
# door needs the explicit list only because "" already means "unchanged"
|
|
# there — two idioms, one outcome.
|
|
rule = await rulebooks_svc.update_rule(rule_id, uid, **fields)
|
|
if rule is None:
|
|
return jsonify({"error": "rule not found"}), 404
|
|
return jsonify(await rulebooks_svc.rule_detail(uid, rule, data.get("system_ids")))
|
|
|
|
|
|
@rulebooks_bp.get("/rules/<int:rule_id>/versions")
|
|
@login_required
|
|
async def list_rule_versions(rule_id: int):
|
|
"""A rule's edit history, newest first.
|
|
|
|
Listing form only — a rule's `statement` and `why` run to thousands of
|
|
characters, so a history list carrying every field would be unreadable
|
|
and expensive to send. Open one for the text.
|
|
"""
|
|
uid = get_current_user_id()
|
|
versions = await rulebooks_svc.list_rule_versions(rule_id, uid)
|
|
if versions is None:
|
|
return jsonify({"error": "rule not found"}), 404
|
|
return jsonify({
|
|
"versions": [v.to_dict(include_text=False) for v in versions],
|
|
})
|
|
|
|
|
|
@rulebooks_bp.get("/rules/<int:rule_id>/versions/<int:version_id>")
|
|
@login_required
|
|
async def get_rule_version(rule_id: int, version_id: int):
|
|
"""One snapshot in full — what the rule said before that edit."""
|
|
uid = get_current_user_id()
|
|
version = await rulebooks_svc.get_rule_version(rule_id, version_id, uid)
|
|
if version is None:
|
|
return jsonify({"error": "version not found"}), 404
|
|
return jsonify(version.to_dict(include_text=True))
|
|
|
|
|
|
# NO restore route, deliberately (milestone 323). A note version can be
|
|
# restored; a binding instruction should not be revertible in one click.
|
|
# Putting a rewrite back goes through update_rule, which takes its own
|
|
# snapshot and leaves the undo in the history like any other edit — a silent
|
|
# revert would erase the only record of why the rewrite happened.
|
|
|
|
|
|
@rulebooks_bp.post("/rules/<int:rule_id>/relations")
|
|
@login_required
|
|
async def relate_rules(rule_id: int):
|
|
"""Draw a typed edge FROM this rule to another.
|
|
|
|
Body: {"to_rule_id": N, "kind": "co_surfaces"|"overrides"|"elaborates",
|
|
"note": "..."}. Idempotent — re-drawing an edge returns the existing one.
|
|
"""
|
|
data = await request.get_json() or {}
|
|
to_rule_id = data.get("to_rule_id")
|
|
if not isinstance(to_rule_id, int):
|
|
return jsonify({"error": "to_rule_id is required"}), 400
|
|
try:
|
|
relation = await rulebooks_svc.add_rule_relation(
|
|
get_current_user_id(), rule_id, to_rule_id,
|
|
data.get("kind", ""), data.get("note", ""),
|
|
)
|
|
except ValueError as exc:
|
|
return jsonify({"error": str(exc)}), 400
|
|
if relation is None:
|
|
return jsonify({"error": "rule not found"}), 404
|
|
return jsonify(relation.to_dict()), 201
|
|
|
|
|
|
@rulebooks_bp.delete("/rule-relations/<int:relation_id>")
|
|
@login_required
|
|
async def unrelate_rules(relation_id: int):
|
|
if not await rulebooks_svc.remove_rule_relation(get_current_user_id(), relation_id):
|
|
return jsonify({"error": "relation not found"}), 404
|
|
return "", 204
|
|
|
|
|
|
@rulebooks_bp.post("/rules/<int:rule_id>/move")
|
|
@login_required
|
|
async def move_rule(rule_id: int):
|
|
"""Give a rule a new home: {topic_id} makes it global, {project_id} makes
|
|
it that project's. The MCP twin is move_rule (rule 33: same names)."""
|
|
data = await request.get_json() or {}
|
|
uid = get_current_user_id()
|
|
try:
|
|
rule = await rulebooks_svc.move_rule(
|
|
rule_id, uid,
|
|
topic_id=int(data.get("topic_id") or 0),
|
|
project_id=int(data.get("project_id") or 0),
|
|
)
|
|
except (TypeError, ValueError) as exc:
|
|
msg = str(exc)
|
|
return jsonify({"error": msg}), 404 if "not found" in msg else 400
|
|
if rule is None:
|
|
return jsonify({"error": "rule not found"}), 404
|
|
return jsonify(await rulebooks_svc.rule_detail(uid, rule))
|
|
|
|
|
|
@rulebooks_bp.delete("/rules/<int:rule_id>")
|
|
@login_required
|
|
async def delete_rule(rule_id: int):
|
|
if await trash_delete(get_current_user_id(), "rule", rule_id) is None:
|
|
return jsonify({"error": "rule not found"}), 404
|
|
return "", 204
|
|
|
|
|
|
# ── A project's rule listing ───────────────────────────────────────────
|
|
|
|
@rulebooks_bp.get("/projects/<int:project_id>/rules")
|
|
@login_required
|
|
async def get_project_rules(project_id: int):
|
|
result = await rulebooks_svc.get_applicable_rules(
|
|
project_id=project_id, user_id=get_current_user_id(),
|
|
)
|
|
return jsonify(result)
|
|
|
|
|
|
@rulebooks_bp.post("/projects/<int:project_id>/rules")
|
|
@login_required
|
|
async def create_project_rule(project_id: int):
|
|
"""Create a rule scoped to a single project. Frontend fast path."""
|
|
data = await request.get_json() or {}
|
|
statement = (data.get("statement") or "").strip()
|
|
if not statement:
|
|
return jsonify({"error": "statement is required"}), 400
|
|
# Checked here as well as in the service, only for the STATUS CODE: the
|
|
# service raises ValueError, which this route maps to 404 for "project not
|
|
# found", and a missing trigger is a 400. The service stays the guard —
|
|
# this is the door telling the truth about whose mistake it was.
|
|
if not (data.get("when_to_apply") or "").strip():
|
|
return jsonify({
|
|
"error": "when_to_apply is required: a rule with no trigger never "
|
|
"surfaces at the moment it applies."
|
|
}), 400
|
|
title = (data.get("title") or "").strip() or statement.split(".")[0][:50]
|
|
try:
|
|
rule = await rulebooks_svc.create_project_rule(
|
|
project_id=project_id,
|
|
user_id=get_current_user_id(),
|
|
title=title,
|
|
statement=statement,
|
|
why=data.get("why", ""),
|
|
how_to_apply=data.get("how_to_apply", ""),
|
|
order_index=data.get("order_index", 0),
|
|
when_to_apply=data.get("when_to_apply", ""),
|
|
# The human door carries `kind` too, and without the MCP door's
|
|
# required provenance: an operator editing their own preference
|
|
# owes nobody an explanation. That requirement is about auditing
|
|
# what the AGENT changed, not what they did themselves.
|
|
kind=data.get("kind", "rule"),
|
|
arose_from_id=data.get("arose_from_id", 0) or 0,
|
|
verify_with=data.get("verify_with", ""),
|
|
expires_when=data.get("expires_when", ""),
|
|
)
|
|
except ValueError as exc:
|
|
return jsonify({"error": str(exc)}), 404
|
|
return jsonify(await rulebooks_svc.rule_detail(
|
|
get_current_user_id(), rule, data.get("system_ids"),
|
|
)), 201
|
|
|
|
|
|
# ── The staleness sweep (milestone 312) ────────────────────────────────
|
|
|
|
@rulebooks_bp.get("/rules-due-for-verification")
|
|
@login_required
|
|
async def rules_due_for_verification():
|
|
"""Rules that carry a check, oldest verification first, never-checked top.
|
|
|
|
Query params: older_than_days, never_only. A rule with no
|
|
`verify_with` never appears — it is a decision, not a fact.
|
|
"""
|
|
uid = get_current_user_id()
|
|
args = request.args
|
|
try:
|
|
older = int(args.get("older_than_days", 0) or 0)
|
|
except ValueError:
|
|
return jsonify({"error": "older_than_days must be an integer"}), 400
|
|
try:
|
|
rules = await rulebooks_svc.rules_due_for_verification(
|
|
uid,
|
|
older_than_days=older,
|
|
never_only=args.get("never_only", "").lower() in ("1", "true", "yes"),
|
|
)
|
|
except ValueError as exc:
|
|
# An unrecognised tier is a 400, not a silently narrowed result set:
|
|
# a filter that quietly answers a different question is the failure
|
|
# this whole surface exists to catch.
|
|
return jsonify({"error": str(exc)}), 400
|
|
return jsonify({
|
|
"rules": [rulebooks_svc.verification_row(r) for r in rules],
|
|
"total": len(rules),
|
|
})
|
|
|
|
|
|
@rulebooks_bp.post("/rules/<int:rule_id>/verify")
|
|
@login_required
|
|
async def mark_rule_verified(rule_id: int):
|
|
"""Record that the rule's check was run. Body: {"still_true": bool}.
|
|
|
|
`still_true: false` writes nothing — a rule whose check failed is wrong,
|
|
not in a recordable state — so it stays at the top of the sweep.
|
|
"""
|
|
data = await request.get_json() or {}
|
|
uid = get_current_user_id()
|
|
still_true = data.get("still_true", True)
|
|
rule = await rulebooks_svc.mark_rule_verified(rule_id, uid, bool(still_true))
|
|
if rule is None:
|
|
return jsonify({"error": "rule not found, or carries no verify_with"}), 404
|
|
payload = await rulebooks_svc.rule_detail(uid, rule)
|
|
payload["verified"] = bool(still_true)
|
|
return jsonify(payload)
|