feat(rules): a preference updates without asking, and says what taught it (#3849 step 2)
CI & Build / Python lint (push) Successful in 5s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 1m7s
CI & Build / integration (push) Successful in 1m8s
CI & Build / Python tests (push) Successful in 1m37s
CI & Build / Build & push image (push) Successful in 33s
CI & Build / Python lint (push) Successful in 5s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 1m7s
CI & Build / integration (push) Successful in 1m8s
CI & Build / Python tests (push) Successful in 1m37s
CI & Build / Build & push image (push) Successful in 33s
The write path, and the step where a preference stops being a relabelled rule. `create_preference` / `update_preference` on the MCP surface, plus `kind` on update_rule and both HTTP doors. SEPARATE TOOLS, NOT A `kind=` ARGUMENT. create_rule's docstring IS the approval gate (#3557): propose, offer three answers, wait. That is right for a rule — the person it binds should have agreed. A preference inverts it, and one reached through create_rule would be read through that prose, so the caller would hesitate over exactly the act this kind exists to make routine. Two doors, two contracts, one table. Reads stay shared: a preference IS a rule row, and "what governs this" wants both. Two required fields, each buying something: - `when_to_apply`, because the trigger is two-thirds of the embedded document. Without one the record is written, stored, and silently never delivered — indistinguishable from one nobody wrote. - `arose_from_id`, the price of the ungated write. A corpus that drifts with no record of what taught each change cannot be audited, and the operator's veto over drift is worth exactly as much as their ability to read why it happened. The near-duplicate gate is what lets this corpus be written freely and stay small: the second preference about a thing updates the first. It is title-scoped and kind-blind, so it also catches a preference restating a rule that already binds. The asymmetry is guarded as two PRESENCE facts — the rule door still asks, the preference door still says write it — never as an absence. An absence check passes against a docstring that was deleted or rewritten into something else, which is snippet #3352's warning and would read as coverage here while proving nothing. `_plain_detail` moved to tests/helpers on its second copy, per that module's own reason for existing (#2825). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011cPyzNnegXHr5iRMzzy5KJ
This commit is contained in:
@@ -226,6 +226,12 @@ def fake_rule(**attrs) -> MagicMock:
|
||||
# `when_to_apply` and `arose_from_id` would be truthy MagicMocks and
|
||||
# rule_brief would attach both keys on every stand-in.
|
||||
"when_to_apply": None, "tier": "always_on", "arose_from_id": None,
|
||||
# Named for the same reason one line up, and it bites harder here.
|
||||
# `rule_brief` and `to_dict` both emit `kind or "rule"`, and a
|
||||
# MagicMock is truthy — so an unnamed `kind` would put a MagicMock
|
||||
# where every payload promises a force, and every stand-in rule would
|
||||
# read as neither a rule nor a preference.
|
||||
"kind": "rule",
|
||||
# Same reason, and the same trap one field further on: an unnamed
|
||||
# `verify_with` is a truthy MagicMock, so every stand-in rule would
|
||||
# claim to carry a check and rule_brief would stamp a MagicMock date
|
||||
@@ -235,6 +241,25 @@ def fake_rule(**attrs) -> MagicMock:
|
||||
}, attrs)
|
||||
|
||||
|
||||
def plain_rule_detail():
|
||||
"""Stub `rulebooks_svc.rule_detail` down to the record's own dict.
|
||||
|
||||
Every rule-tool unit test needs it and none of them wants it: the real
|
||||
`rule_detail` reads the rule's Systems and its typed edges from the
|
||||
database, which a unit test has none of. What these tests assert is that
|
||||
the TOOL forwarded the right arguments, so the seam is stubbed the same
|
||||
way the create/update calls themselves already are.
|
||||
|
||||
Consolidated here on its second copy, per this module's own reason for
|
||||
existing (#2825) — two stubs for one seam drift apart quietly, and a test
|
||||
stubbing the seam slightly differently is a test asserting something
|
||||
slightly different than it appears to.
|
||||
"""
|
||||
async def _detail(_uid, rule, _system_ids=None):
|
||||
return rule.to_dict()
|
||||
return patch("scribe.mcp.tools.rulebooks.rulebooks_svc.rule_detail", _detail)
|
||||
|
||||
|
||||
class FakeMCP:
|
||||
"""Stand-in for the FastMCP server a tool module's ``register(mcp)`` is
|
||||
handed: records the ``name=`` of every ``@mcp.tool(...)`` registration in
|
||||
|
||||
@@ -3,6 +3,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from tests.helpers import FakeMCP, fake_rule, fake_rulebook, fake_topic
|
||||
from tests.helpers import plain_rule_detail as _plain_detail
|
||||
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("_bind_user")
|
||||
@@ -48,18 +49,10 @@ async def test_get_rulebook_raises_when_not_found():
|
||||
await get_rulebook(rulebook_id=999)
|
||||
|
||||
|
||||
def _plain_detail():
|
||||
"""Stub the rule_detail seam these tool tests are not about.
|
||||
|
||||
create/update/get_rule now return through services.rulebooks.rule_detail,
|
||||
which reads the rule's areas and edges from the database. These are unit
|
||||
tests with no database, and what they assert is that the TOOL forwards the
|
||||
right arguments — so the seam is stubbed to the plain record, the same way
|
||||
they already stub the create/update calls themselves.
|
||||
"""
|
||||
async def _detail(_uid, rule, _system_ids=None):
|
||||
return rule.to_dict()
|
||||
return patch("scribe.mcp.tools.rulebooks.rulebooks_svc.rule_detail", _detail)
|
||||
# _plain_detail moved to tests/helpers on its second copy (#2825's own
|
||||
# reason for existing): two stubs for one seam drift apart quietly, and a
|
||||
# test stubbing it slightly differently asserts something slightly different
|
||||
# than it appears to.
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -230,11 +223,18 @@ def test_register_attaches_every_tool():
|
||||
|
||||
register(mcp)
|
||||
# 26 through milestone 307, +2 for the staleness sweep (milestone 312),
|
||||
# +1 for a rule's edit history (milestone 323).
|
||||
assert len(mcp.names) == 29
|
||||
# +1 for a rule's edit history (milestone 323), +2 for preferences
|
||||
# (milestone 399).
|
||||
assert len(mcp.names) == 31
|
||||
# spot-check a few names
|
||||
assert "list_rulebooks" in mcp.names
|
||||
assert "create_rule" in mcp.names
|
||||
# Preferences get their own WRITE door — create_rule's docstring is the
|
||||
# approval gate, and a preference reached through it would be read
|
||||
# through that prose. Reads stay shared deliberately, so there is no
|
||||
# get_preference to look for here.
|
||||
assert "create_preference" in mcp.names
|
||||
assert "update_preference" in mcp.names
|
||||
assert "subscribe_project_to_rulebook" in mcp.names
|
||||
assert "list_always_on_rules" in mcp.names
|
||||
# milestone 297: a project's opt-out of a whole always-on rulebook
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
"""The preference write path, and how it differs from a rule's (milestone 399).
|
||||
|
||||
WHY TWO DOORS AT ALL
|
||||
|
||||
`create_rule`'s docstring IS the approval gate (#3557): it tells its caller to
|
||||
propose, offer three answers, and wait. That is right for a rule — the person
|
||||
a rule binds should have agreed to be bound.
|
||||
|
||||
A preference inverts it. The operator's framing: *"preferences are rules that
|
||||
scribe can and should update during use."* A preference that asks every time
|
||||
never drifts, and drifting is the whole feature. Reaching one through
|
||||
`create_rule(kind=...)` would mean reading it through the gate's prose, and
|
||||
the caller would hesitate over exactly the act this kind exists to make
|
||||
routine.
|
||||
|
||||
So the asymmetry is the product, and these tests pin it.
|
||||
|
||||
TWO PRESENCE CHECKS, NEVER AN ABSENCE
|
||||
|
||||
The tempting guard is "create_preference's docstring does NOT run the approval
|
||||
loop". That is the shape snippet #3352 warns against: an absence check passes
|
||||
against a docstring that has been deleted, emptied, or rewritten into
|
||||
something else entirely, and it reads as coverage while proving nothing.
|
||||
|
||||
So the asymmetry is asserted as two PRESENCE facts — the rule door still asks,
|
||||
the preference door still says write it — and each fails if its own side is
|
||||
tidied away. Synonym families, structure not wording, the same bargain
|
||||
test_rule_creation_asks_first strikes.
|
||||
"""
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.helpers import fake_rule, plain_rule_detail as _plain_detail
|
||||
from tests.helpers import tool_doc as _doc
|
||||
|
||||
# The tool layer reads its caller from a ContextVar the HTTP transport sets.
|
||||
# With no request in flight, the module binds it itself (snippet #2836).
|
||||
pytestmark = pytest.mark.usefixtures("_bind_user")
|
||||
|
||||
MODULE = "scribe.mcp.tools.rulebooks"
|
||||
|
||||
|
||||
# ── the required fields, and why each is required ───────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_preference_without_a_trigger_is_refused():
|
||||
"""A preference with no `when_to_apply` is inert, not merely incomplete.
|
||||
|
||||
The trigger is two-thirds of the embedded document, so a record without
|
||||
one never surfaces at the moment it applies. Refusing at the tool is the
|
||||
difference between an error the writer can fix and a preference that is
|
||||
written, stored, and silently never delivered — which looks identical to
|
||||
one nobody wrote.
|
||||
"""
|
||||
create_mock = AsyncMock()
|
||||
with patch(f"{MODULE}.rulebooks_svc.create_rule", create_mock):
|
||||
from scribe.mcp.tools.rulebooks import create_preference
|
||||
with pytest.raises(ValueError, match="when_to_apply is required"):
|
||||
await create_preference(
|
||||
topic_id=10, title="t", statement="s",
|
||||
when_to_apply=" ", arose_from_id=42,
|
||||
)
|
||||
create_mock.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_preference_without_provenance_is_refused():
|
||||
"""Provenance is the price of the ungated write.
|
||||
|
||||
A preference is expected to change as the work teaches it. A corpus that
|
||||
drifts with no record of what taught each change is one nobody can audit —
|
||||
and the operator's veto over drift depends entirely on being able to read
|
||||
why it happened.
|
||||
"""
|
||||
create_mock = AsyncMock()
|
||||
with patch(f"{MODULE}.rulebooks_svc.create_rule", create_mock):
|
||||
from scribe.mcp.tools.rulebooks import create_preference
|
||||
with pytest.raises(ValueError, match="arose_from_id is required"):
|
||||
await create_preference(
|
||||
topic_id=10, title="t", statement="s",
|
||||
when_to_apply="when x", arose_from_id=0,
|
||||
)
|
||||
create_mock.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_preference_stores_the_preference_kind():
|
||||
"""The tool's one irreducible job.
|
||||
|
||||
Asserted on the kwarg reaching the service rather than on the returned
|
||||
payload: a tool that accepted the call and wrote a plain rule would
|
||||
return something that reads correctly, and the force would be wrong.
|
||||
"""
|
||||
rule = fake_rule(id=100, kind="preference")
|
||||
create_mock = AsyncMock(return_value=rule)
|
||||
with patch(f"{MODULE}.rulebooks_svc.create_rule", create_mock), _plain_detail():
|
||||
from scribe.mcp.tools.rulebooks import create_preference
|
||||
await create_preference(
|
||||
topic_id=10, title="Pace hard debugging",
|
||||
statement="One step per turn.",
|
||||
when_to_apply="during hard debugging",
|
||||
arose_from_id=42,
|
||||
)
|
||||
kwargs = create_mock.call_args.kwargs
|
||||
assert kwargs["kind"] == "preference"
|
||||
assert kwargs["arose_from_id"] == 42
|
||||
assert kwargs["when_to_apply"] == "during hard debugging"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_near_duplicate_preference_blocks():
|
||||
"""The gate is what lets this corpus be written freely and stay small.
|
||||
|
||||
The second preference about a thing must UPDATE the first. Two that
|
||||
quietly disagree are worse than none: retrieval surfaces whichever scores
|
||||
higher, and nobody learns the other exists.
|
||||
"""
|
||||
from scribe.services.dedup import DuplicateMatch
|
||||
dup = DuplicateMatch(id=47, title="Pace hard debugging", similarity=1.0, reason="title")
|
||||
create_mock = AsyncMock()
|
||||
with patch(f"{MODULE}.dedup_svc.find_duplicate_rule", AsyncMock(return_value=dup)), \
|
||||
patch(f"{MODULE}.rulebooks_svc.create_rule", create_mock):
|
||||
from scribe.mcp.tools.rulebooks import create_preference
|
||||
out = await create_preference(
|
||||
topic_id=10, title="Pace hard debugging", statement="s",
|
||||
when_to_apply="when", arose_from_id=42,
|
||||
)
|
||||
assert out["duplicate"] is True
|
||||
assert out["existing_id"] == 47
|
||||
create_mock.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_updating_a_preference_without_provenance_is_refused():
|
||||
update_mock = AsyncMock()
|
||||
with patch(f"{MODULE}.rulebooks_svc.update_rule", update_mock):
|
||||
from scribe.mcp.tools.rulebooks import update_preference
|
||||
with pytest.raises(ValueError, match="arose_from_id is required"):
|
||||
await update_preference(rule_id=5, arose_from_id=0, statement="new")
|
||||
update_mock.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_preference_forwards_what_taught_the_change():
|
||||
rule = fake_rule(id=5, kind="preference")
|
||||
update_mock = AsyncMock(return_value=rule)
|
||||
with patch(f"{MODULE}.rulebooks_svc.update_rule", update_mock), _plain_detail():
|
||||
from scribe.mcp.tools.rulebooks import update_preference
|
||||
await update_preference(
|
||||
rule_id=5, arose_from_id=99, statement="the new way",
|
||||
)
|
||||
kwargs = update_mock.call_args.kwargs
|
||||
assert kwargs["arose_from_id"] == 99
|
||||
assert kwargs["statement"] == "the new way"
|
||||
|
||||
|
||||
# ── the asymmetry, as two presence facts ────────────────────────────────
|
||||
|
||||
|
||||
def test_the_rule_door_still_asks_before_writing():
|
||||
"""Half one of the asymmetry. If this fails, the gate was tidied away and
|
||||
preferences are no longer the exception — they are just the same thing.
|
||||
"""
|
||||
doc = _doc(MODULE, "create_rule").lower()
|
||||
asks = ("approve", "propose", "ask", "question")
|
||||
assert any(w in doc for w in asks), (
|
||||
"create_rule's docstring no longer runs the propose-then-approve loop. "
|
||||
"The preference path's whole justification is that it is the exception "
|
||||
"to this; with the gate gone there is no asymmetry left to justify."
|
||||
)
|
||||
|
||||
|
||||
def test_the_preference_door_says_to_write_it():
|
||||
"""Half two. The inverting instruction has to be PRESENT, not merely
|
||||
unaccompanied by a gate.
|
||||
|
||||
An agent that has internalised #3557 will hesitate to write or rewrite a
|
||||
preference unless told plainly that this door is different. Silence here
|
||||
does not read as permission — it reads as an omission, and the caller
|
||||
falls back on the behaviour it already knows.
|
||||
"""
|
||||
for tool in ("create_preference", "update_preference"):
|
||||
doc = _doc(MODULE, tool).lower()
|
||||
permits = ("expected", "no approval", "without asking", "ordinary work",
|
||||
"write it", "not a liberty", "no proposal")
|
||||
assert any(w in doc for w in permits), (
|
||||
f"{tool}'s docstring no longer tells its caller that writing "
|
||||
"without an approval loop is expected. A caller carrying "
|
||||
"create_rule's gate will default to asking, and a preference "
|
||||
"nothing ever updates is a rule nobody enforces."
|
||||
)
|
||||
|
||||
|
||||
def test_the_preference_door_names_the_force_distinction():
|
||||
"""The routing test, stated positively (rule 165).
|
||||
|
||||
The confusion this milestone exists to fix is that a session cannot tell
|
||||
which kind it is holding. If the docstring stops drawing the line, the
|
||||
tool becomes a second way to write rules.
|
||||
"""
|
||||
doc = _doc(MODULE, "create_preference").lower()
|
||||
assert "rule" in doc and any(
|
||||
w in doc for w in ("followed", "binds", "breaks", "consistency")
|
||||
), (
|
||||
"create_preference's docstring no longer distinguishes a preference "
|
||||
"from a rule by force. Without that line the tool is a second door "
|
||||
"onto the rulebook with a weaker gate."
|
||||
)
|
||||
|
||||
|
||||
def test_the_preference_door_keeps_the_record_out_of_scope():
|
||||
"""Preferences shape HOW work is done, never WHAT is recorded.
|
||||
|
||||
Worth pinning because "record it the way I like it" is the natural next
|
||||
reach, and it would make dev-logs and issues idiosyncratic per author —
|
||||
while the record is the one thing that has to outlive the person.
|
||||
"""
|
||||
doc = _doc(MODULE, "create_preference").lower()
|
||||
assert "record" in doc, (
|
||||
"create_preference's docstring no longer says that a preference does "
|
||||
"not change what gets recorded. That boundary is the one a reader "
|
||||
"would cross without noticing."
|
||||
)
|
||||
Reference in New Issue
Block a user