feat(7833): each moment declares which kinds of record its search may consider, editable in Settings > Moments
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 15s
CI & Build / TypeScript typecheck (push) Successful in 34s
CI & Build / Python tests (push) Failing after 1m35s
CI & Build / integration (push) Successful in 1m58s
CI & Build / Build & push image (push) Skipped
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 15s
CI & Build / TypeScript typecheck (push) Successful in 34s
CI & Build / Python tests (push) Failing after 1m35s
CI & Build / integration (push) Successful in 1m58s
CI & Build / Build & push image (push) Skipped
The kind x moment table (services/record_kinds.py): - every record search fires at a door: the operator message, or a catalog moment (work.change for the write path); - each door considers a set of kinds (snippet, note, lesson, process, task:issue/work/spike/plan); a kind left out is never searched there; - the shipped defaults are today's behaviour; a user's change is stored per door, and putting a door back removes it. Both note arms read it, replacing their fixed kind lists, and a reserved slot whose kind is turned off is not reserved. The review re-run searches the same kinds. Settings > Moments shows and edits the table, with each cell's lines shown and opens from the record ledger. list_moments returns it as record_kinds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -367,6 +367,27 @@ def _default_payload_budget(request):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _default_record_kinds(request):
|
||||
"""Stub the settings read behind the kind×moment table.
|
||||
|
||||
Both record searches read the user's kinds per door
|
||||
(`record_kinds.kinds_for`); stubbed to the shipped default so a unit test
|
||||
of a door needs no database. The integration lane, where the table's own
|
||||
tests live, reads the real setting.
|
||||
"""
|
||||
if request.node.get_closest_marker("integration"):
|
||||
yield
|
||||
return
|
||||
from grimoire.services import record_kinds
|
||||
|
||||
async def _default(_user_id, door):
|
||||
return record_kinds.DOORS[door].default
|
||||
|
||||
with patch.object(record_kinds, "kinds_for", AsyncMock(side_effect=_default)):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _ungated_doors(request):
|
||||
"""Leave the plugin doors' cue-swap gate off unless a test asks for it.
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
"""Real-Postgres tests for the kind×moment table (`record_kinds`): the
|
||||
shipped defaults, a user's change and its reset, the refusals, the SQL clause
|
||||
matching exactly the kinds named, and each cell's evidence from the ledger."""
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import delete, select
|
||||
|
||||
from grimoire.models import async_session
|
||||
from grimoire.models.note import Note
|
||||
from grimoire.models.record_delivery import RecordDelivery
|
||||
from grimoire.models.setting import Setting
|
||||
from grimoire.services import record_kinds
|
||||
from tests.helpers import ensure_user
|
||||
|
||||
pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")]
|
||||
|
||||
|
||||
async def _user(name: str = "record_kinds_owner") -> int:
|
||||
async with async_session() as s:
|
||||
uid = (await ensure_user(s, name)).id
|
||||
await s.execute(delete(Setting).where(
|
||||
Setting.user_id == uid, Setting.key == record_kinds.SETTING_KEY))
|
||||
await s.execute(delete(RecordDelivery).where(RecordDelivery.user_id == uid))
|
||||
await s.commit()
|
||||
return uid
|
||||
|
||||
|
||||
async def test_a_door_follows_its_default_until_the_user_changes_it():
|
||||
uid = await _user()
|
||||
assert await record_kinds.kinds_for(uid, "work.change") == record_kinds.DOORS["work.change"].default
|
||||
await record_kinds.set_kinds(uid, "work.change", ["snippet", "task:issue"])
|
||||
assert await record_kinds.kinds_for(uid, "work.change") == {"snippet", "task:issue"}
|
||||
# Another door is untouched.
|
||||
assert await record_kinds.kinds_for(uid, "message") == frozenset(record_kinds.KINDS)
|
||||
table = await record_kinds.set_kinds(uid, "work.change", None)
|
||||
row = next(d for d in table["doors"] if d["door"] == "work.change")
|
||||
assert row["changed"] is False and row["kinds"] == row["default"]
|
||||
|
||||
|
||||
async def test_setting_the_default_stores_no_change():
|
||||
uid = await _user()
|
||||
default = sorted(record_kinds.DOORS["work.change"].default)
|
||||
table = await record_kinds.set_kinds(uid, "work.change", default)
|
||||
assert not any(d["changed"] for d in table["doors"])
|
||||
|
||||
|
||||
async def test_an_unknown_door_or_kind_is_refused_and_nothing_is_stored():
|
||||
uid = await _user()
|
||||
with pytest.raises(ValueError, match="unknown door"):
|
||||
await record_kinds.set_kinds(uid, "work.nowhere", ["snippet"])
|
||||
with pytest.raises(ValueError, match="unknown kind"):
|
||||
await record_kinds.set_kinds(uid, "message", ["snippet", "rule"])
|
||||
assert await record_kinds.kinds_for(uid, "message") == frozenset(record_kinds.KINDS)
|
||||
|
||||
|
||||
async def test_the_clause_matches_exactly_the_kinds_named():
|
||||
uid = await _user()
|
||||
async with async_session() as s:
|
||||
made = {
|
||||
"snippet": Note(user_id=uid, title="s", note_type="snippet"),
|
||||
"note": Note(user_id=uid, title="n", note_type="note"),
|
||||
"task:issue": Note(user_id=uid, title="i", status="todo", task_kind="issue"),
|
||||
"task:work": Note(user_id=uid, title="w", status="todo", task_kind="work"),
|
||||
}
|
||||
s.add_all(made.values())
|
||||
await s.commit()
|
||||
ids = {k: n.id for k, n in made.items()}
|
||||
for kinds in (["snippet", "task:issue"], ["note"], ["task:work"], []):
|
||||
got = set((await s.execute(
|
||||
select(Note.id).where(Note.id.in_(ids.values()), record_kinds.kinds_clause(kinds))
|
||||
)).scalars())
|
||||
assert got == {ids[k] for k in kinds}, kinds
|
||||
|
||||
|
||||
async def test_each_cell_carries_the_lines_its_door_pushed_and_the_opens():
|
||||
uid = await _user()
|
||||
async with async_session() as s:
|
||||
s.add_all([
|
||||
RecordDelivery(user_id=uid, session_id="s", note_id=1, kind="snippet", way="pushed",
|
||||
source="write_path_semantic", plugin="p",
|
||||
opened_at=datetime.now(timezone.utc)),
|
||||
RecordDelivery(user_id=uid, session_id="s", note_id=2, kind="snippet", way="pushed",
|
||||
source="write_path_semantic", plugin="p"),
|
||||
RecordDelivery(user_id=uid, session_id="s", note_id=3, kind="task:issue", way="pushed",
|
||||
source="auto_inject", plugin="p"),
|
||||
])
|
||||
await s.commit()
|
||||
table = await record_kinds.table(uid, days=7)
|
||||
cells = {d["door"]: d["evidence"] for d in table["doors"]}
|
||||
assert cells["work.change"] == {"snippet": {"delivered": 2, "opened": 1}}
|
||||
assert cells["message"] == {"task:issue": {"delivered": 1, "opened": 0}}
|
||||
assert table["evidence_failed"] is False
|
||||
@@ -91,14 +91,18 @@ async def test_the_tool_returns_the_catalog_with_this_installs_actions():
|
||||
from grimoire.mcp.tools import moments as tool
|
||||
|
||||
by_moment = {"actions": {"work.change": []}, "removed_defaults": []}
|
||||
table = {"doors": [{"door": "message", "kinds": ["snippet"]}]}
|
||||
with patch.object(tool, "current_user_id", lambda: 7), \
|
||||
patch.object(tool.actions_svc, "actions_by_moment",
|
||||
AsyncMock(return_value=by_moment)) as read:
|
||||
AsyncMock(return_value=by_moment)) as read, \
|
||||
patch.object(tool.record_kinds_svc, "table", AsyncMock(return_value=table)) as kinds:
|
||||
out = await tool.list_moments()
|
||||
read.assert_awaited_once_with(7)
|
||||
kinds.assert_awaited_once_with(7)
|
||||
assert out["moments"] == moments.catalog()["moments"]
|
||||
assert out["actions"] == by_moment["actions"]
|
||||
assert out["removed_defaults"] == []
|
||||
assert out["record_kinds"] == table
|
||||
|
||||
|
||||
def test_the_tools_are_registered_and_classified():
|
||||
|
||||
@@ -267,11 +267,41 @@ def test_the_pipelines_fan_out_sites_declare_every_notes_source():
|
||||
"grimoire/services/plugin_context.py::record_surfaced(source=arm)"]
|
||||
|
||||
|
||||
def test_the_filters_say_which_kinds_and_whose_records():
|
||||
assert rp.note_search_filters(rp.AUTO_INJECT) == {
|
||||
"include_global_kinds": True, "scope": "browse",
|
||||
}
|
||||
assert rp.note_search_filters(rp.WRITE_PATH) == {
|
||||
"note_type": ("snippet", "note", LESSON_NOTE_TYPE), "task_kind": "issue",
|
||||
"include_global_kinds": True, "scope": "browse",
|
||||
}
|
||||
def test_the_filters_say_whose_records_and_the_table_says_which_kinds():
|
||||
for arm in (rp.AUTO_INJECT, rp.WRITE_PATH):
|
||||
assert rp.note_search_filters(arm) == {"include_global_kinds": True, "scope": "browse"}
|
||||
assert rp.AUTO_INJECT.door == "message" and rp.WRITE_PATH.door == "work.change"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_an_arm_searches_only_the_kinds_its_door_allows():
|
||||
# The shipped defaults: the message door considers every kind, so its
|
||||
# search is unfiltered; the write path narrows to its row.
|
||||
_r, _io_, calls = await _run(rp.AUTO_INJECT, lambda kw: [])
|
||||
assert calls[0]["record_kinds"] is None
|
||||
_r, _io_, calls = await _run(rp.WRITE_PATH, lambda kw: [])
|
||||
assert calls[0]["record_kinds"] == ["lesson", "note", "snippet", "task:issue"]
|
||||
# A user's own row replaces the default.
|
||||
_r, _io_, calls = await _run(rp.WRITE_PATH, lambda kw: [], kinds=frozenset({"snippet"}))
|
||||
assert calls[0]["record_kinds"] == ["snippet"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_door_with_every_kind_off_searches_nothing_and_logs_nothing():
|
||||
result, io, calls = await _run(rp.AUTO_INJECT, lambda kw: [], kinds=frozenset())
|
||||
assert calls == [] and result.menu == []
|
||||
io.record_retrieval.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_slot_whose_kind_the_door_turned_off_is_not_reserved():
|
||||
main = [_note(1, 0.70), _note(2, 0.69), _note(3, 0.68)]
|
||||
reuse = [_note(9, 0.60, note_type="snippet")]
|
||||
lesson = [_note(42, 0.58, note_type=LESSON_NOTE_TYPE)]
|
||||
result, _io_, calls = await _run(
|
||||
rp.AUTO_INJECT, _routed(main, reuse, lesson), budget=3,
|
||||
kinds=frozenset({"note", "process"}),
|
||||
)
|
||||
# The reuse slot narrows to processes; the lesson slot does not run.
|
||||
assert [c.get("note_type") for c in calls] == [None, ("process",)]
|
||||
assert "lesson_slot" not in result.slot_ids
|
||||
|
||||
@@ -51,7 +51,7 @@ async def test_an_empty_verdict_list_is_refused():
|
||||
# ── the re-run replays the arm ───────────────────────────────────────────
|
||||
|
||||
|
||||
_FILTERS = ("note_type", "task_kind", "include_global_kinds", "scope")
|
||||
_FILTERS = ("note_type", "task_kind", "record_kinds", "include_global_kinds", "scope")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -42,6 +42,7 @@ def test_every_endpoint_is_reachable_on_the_app():
|
||||
"/api/retrieval/surfaces/<surface>",
|
||||
"/api/retrieval/adherence",
|
||||
"/api/retrieval/record-reach",
|
||||
"/api/retrieval/record-kinds",
|
||||
"/api/retrieval/gate",
|
||||
"/api/retrieval/tuning-history",
|
||||
"/api/retrieval/moments",
|
||||
|
||||
Reference in New Issue
Block a user