feat(8118,8119): telemetry reads take a closed window, and every enter_project records whether it carried the family line
CI & Build / TypeScript typecheck (push) Successful in 37s
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / integration (push) Failing after 2m15s
CI & Build / Python tests (push) Successful in 2m23s
CI & Build / Build & push image (push) Skipped

#8119: retrieval_telemetry and list_rules(with_usage) take `since` and
`until` (ISO date or datetime) besides `days`. Every windowed count is
bounded at both ends: sources, note/rule/system/moment usage, record
reach, judged lines and the floor-moved check. A review read after its
period can now keep a later deploy out of its counts. A bad bound is
refused rather than widened.

#8118: enter_project writes one AppLog row (family / enter_project) per
entry: the project, whether the family line was shown, its counts, and
whether the readout failed. It is spawned, so the handshake does not wait.
retrieval_telemetry gains a `family_line` block: per project, entries,
entries that carried the line, first and last shown, and the latest
counts. Review #5132's "did the project's sessions see the line" is now
answerable from this deploy on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 00:01:19 -05:00
co-authored by Claude Opus 5.5
parent 0e946ffac8
commit 577a8e8621
12 changed files with 389 additions and 29 deletions
+37 -2
View File
@@ -356,8 +356,43 @@ async def test_enter_project_fires_the_coverage_seed_on_the_owner(
))
await enter_project(project_id=5)
seed.assert_called_once_with(42, 5, cached=None)
_no_background_seed.assert_called_once()
assert _no_background_seed.call_args.kwargs["site"] == "enter_project.coverage_seed"
sites = [c.kwargs["site"] for c in _no_background_seed.call_args_list]
assert sites.count("enter_project.coverage_seed") == 1
@pytest.mark.asyncio
async def test_every_enter_records_whether_it_carried_the_family_line(
_no_family, _no_background_seed,
):
"""Review #5132 asks whether a project's sessions saw the family line.
Each enter records one entry, with the line or without it (the entries
are the denominator), and a failed readout is recorded as failed rather
than as an entry with nothing to show."""
import contextlib
line = {"line": "family canon…", "unassessed": 2, "owed": 1, "needs_recheck": 0}
async def enter():
with contextlib.ExitStack() as stack:
for cm in _enter_project_stubs(fake_project(id=5)):
stack.enter_context(cm)
record = stack.enter_context(patch(
"grimoire.mcp.tools.projects.family_adoption_svc.record_entry",
MagicMock(return_value=object()),
))
await enter_project(project_id=5)
sites = [c.kwargs["site"] for c in _no_background_seed.call_args_list]
assert "family_entry_record" in sites
return record
record = await enter()
record.assert_called_once_with(7, 5, None, failed=False)
_no_family.return_value = line
record = await enter()
record.assert_called_once_with(7, 5, line, failed=False)
_no_family.side_effect = RuntimeError("database down")
record = await enter()
record.assert_called_once_with(7, 5, None, failed=True)
@pytest.mark.asyncio
+26
View File
@@ -606,3 +606,29 @@ async def test_list_rules_carries_usage_only_when_asked():
assert usage.await_args.args[0] == [1, 2]
assert usage.await_args.kwargs["since"] is not None
lessons.assert_awaited_once_with(7, [1, 2])
@pytest.mark.asyncio
async def test_list_rules_counts_a_closed_window_when_a_review_names_one():
"""A review read after its period passes both ends, so a later deploy
stays out of the counts. A bare `until` date covers that whole day, and
an unparseable bound is refused rather than widened to the default."""
from datetime import datetime, timezone
from grimoire.mcp.tools import rulebooks as tools
usage = AsyncMock(return_value={})
with patch.object(tools.rulebooks_svc, "list_rules", AsyncMock(return_value=[fake_rule(id=1)])), \
patch.object(tools.rulebooks_svc, "list_rule_moments", AsyncMock(return_value={})), \
patch.object(tools, "usage_for_rules", usage), \
patch.object(tools, "confirmed_lesson_counts", AsyncMock(return_value={})):
await tools.list_rules(with_usage=True, since="2026-10-23", until="2026-11-24")
kw = usage.await_args.kwargs
assert kw["since"] == datetime(2026, 10, 23, tzinfo=timezone.utc)
assert kw["until"].date().isoformat() == "2026-11-24"
assert kw["until"] > datetime(2026, 11, 24, 23, 0, tzinfo=timezone.utc)
refused = await tools.list_rules(with_usage=True, since="last tuesday")
assert "error" in refused
backwards = await tools.list_rules(with_usage=True, since="2026-11-24", until="2026-10-23")
assert "error" in backwards
@@ -247,6 +247,75 @@ async def test_retrieval_summary_reads_what_the_writer_wrote(_dispose_engine):
await s.commit()
@pytest.mark.integration
@pytest.mark.asyncio
async def test_a_closed_window_leaves_out_what_came_after_it(_dispose_engine):
"""A review read late passes `until`, so events after the period under
review (a later deploy's traffic) are not counted. One surfacing inside
the window and one after it: only the first is counted, and the response
names the window it used."""
from datetime import datetime, timedelta, timezone
from sqlalchemy import delete
from grimoire.models import async_session
from grimoire.models.note_usage import NoteUsageEvent
from grimoire.services.retrieval_telemetry import retrieval_summary
UID = 990006
now = datetime.now(timezone.utc)
inside, after = now - timedelta(days=10), now - timedelta(days=2)
async with async_session() as s:
s.add_all([
NoteUsageEvent(user_id=UID, note_id=1, event="surfaced", source="auto_inject",
created_at=inside),
NoteUsageEvent(user_id=UID, note_id=2, event="surfaced", source="auto_inject",
created_at=after),
])
await s.commit()
try:
bounded = await retrieval_summary(UID, since=now - timedelta(days=14),
until=now - timedelta(days=5))
assert bounded["usage"]["surfaced"] == 1
assert bounded["until"] is not None and bounded["window_days"] == pytest.approx(9)
assert (await retrieval_summary(UID, days=14))["usage"]["surfaced"] == 2
finally:
async with async_session() as s:
await s.execute(delete(NoteUsageEvent).where(NoteUsageEvent.user_id == UID))
await s.commit()
@pytest.mark.integration
@pytest.mark.asyncio
async def test_family_line_counts_entries_and_the_ones_that_carried_the_line(_dispose_engine):
"""Review #5132's question: did a project's sessions see the family line?
Three entries to one project, two carrying the line; the readout counts
the entries as the denominator and keeps the latest counts shown."""
from sqlalchemy import delete
from grimoire.models import async_session
from grimoire.models.app_log import AppLog
from grimoire.services.family_adoption import record_entry
from grimoire.services.retrieval_telemetry import retrieval_summary
UID, PID = 990007, 990107
await record_entry(UID, PID, {"unassessed": 3, "owed": 1, "needs_recheck": 0})
await record_entry(UID, PID, None)
await record_entry(UID, PID, {"unassessed": 2, "owed": 1, "needs_recheck": 0})
try:
block = (await retrieval_summary(UID, days=1))["family_line"]
assert "family_line_failed" not in block
assert block["entries"] == 3 and block["entries_with_line"] == 2
(row,) = block["by_project"]
assert row["project_id"] == PID and row["readout_failed"] == 0
assert row["last_counts"] == {"unassessed": 2, "owed": 1, "needs_recheck": 0}
assert row["first_shown_at"] <= row["last_shown_at"]
finally:
async with async_session() as s:
await s.execute(delete(AppLog).where(AppLog.user_id == UID))
await s.commit()
@pytest.mark.integration
@pytest.mark.asyncio
async def test_retrieval_summary_is_empty_not_broken_for_a_fresh_install(_dispose_engine):