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
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:
@@ -837,7 +837,7 @@ def _silent_surfaces(sources: dict, usage: dict, rule_usage: dict,
|
||||
SYSTEM_USAGE_TOP = 20
|
||||
|
||||
|
||||
async def _system_usage(user_id: int | None, since) -> dict:
|
||||
async def _system_usage(user_id: int | None, since, until) -> dict:
|
||||
"""The rulings arm's own block: were an area's rulings shown, and
|
||||
was the area then opened.
|
||||
|
||||
@@ -871,6 +871,7 @@ async def _system_usage(user_id: int | None, since) -> dict:
|
||||
async with async_session() as session:
|
||||
where = (
|
||||
SystemUsageEvent.created_at >= since,
|
||||
SystemUsageEvent.created_at < until,
|
||||
SystemUsageEvent.user_id == user_id,
|
||||
)
|
||||
per_source = (
|
||||
@@ -943,7 +944,7 @@ async def _system_usage(user_id: int | None, since) -> dict:
|
||||
|
||||
|
||||
|
||||
async def moment_usage(user_id: int | None, since) -> dict:
|
||||
async def moment_usage(user_id: int | None, since, until=None) -> dict:
|
||||
"""Deliveries and opens per moment.
|
||||
|
||||
A mounted rule's delivery is a `moment_rule` surfacing whose `detail` is
|
||||
@@ -973,6 +974,8 @@ async def moment_usage(user_id: int | None, since) -> dict:
|
||||
"""
|
||||
from grimoire.services.retrieval_pipeline import MOMENT_RULE_SOURCE
|
||||
|
||||
if until is None:
|
||||
until = datetime.now(timezone.utc)
|
||||
block: dict = {"by_moment": {}}
|
||||
try:
|
||||
async with async_session() as session:
|
||||
@@ -986,6 +989,7 @@ async def moment_usage(user_id: int | None, since) -> dict:
|
||||
)
|
||||
.where(
|
||||
RuleUsageEvent.created_at >= since,
|
||||
RuleUsageEvent.created_at < until,
|
||||
RuleUsageEvent.user_id == user_id,
|
||||
RuleUsageEvent.event == RULE_SURFACED,
|
||||
RuleUsageEvent.source == MOMENT_RULE_SOURCE,
|
||||
@@ -1002,6 +1006,7 @@ async def moment_usage(user_id: int | None, since) -> dict:
|
||||
# autoescape: `_` is a LIKE wildcard (see the note block).
|
||||
pull.source.startswith("mcp_", autoescape=True),
|
||||
pull.created_at >= per_rule.c.first_at,
|
||||
pull.created_at < until,
|
||||
)
|
||||
# One row per (moment, rule); the moment's totals are summed from
|
||||
# these below, so `by_rule` and the totals cannot disagree.
|
||||
@@ -1049,8 +1054,40 @@ async def moment_usage(user_id: int | None, since) -> dict:
|
||||
block.update(_coverage(complete.get("*"), since))
|
||||
return block
|
||||
|
||||
def resolve_window(days: int = 30, since=None, until=None) -> tuple[datetime, datetime]:
|
||||
"""The [since, until) window a telemetry read counts over.
|
||||
|
||||
`days` counts back from `until`, and `until` defaults to now. An explicit
|
||||
`since` replaces the count. A review read after the period it describes
|
||||
passes both bounds, so a deploy that landed after the period stays out of
|
||||
the counts. `since` and `until` are aware datetimes, or ISO date/datetime
|
||||
strings: a bare date as `until` covers the whole of that day. An
|
||||
unparseable value is refused rather than silently widened to the default,
|
||||
because a window the caller did not ask for reads exactly like the one
|
||||
they did.
|
||||
"""
|
||||
from grimoire.services.logging import parse_filter_datetime
|
||||
|
||||
def _bound(value, *, end_of_day: bool):
|
||||
if value is None or value == "":
|
||||
return None
|
||||
if isinstance(value, datetime):
|
||||
return value if value.tzinfo else value.replace(tzinfo=timezone.utc)
|
||||
parsed = parse_filter_datetime(str(value), end_of_day=end_of_day)
|
||||
if parsed is None:
|
||||
raise ValueError(f"not an ISO date or datetime: {value!r}")
|
||||
return parsed
|
||||
|
||||
end = _bound(until, end_of_day=True) or datetime.now(timezone.utc)
|
||||
start = _bound(since, end_of_day=False) or end - timedelta(days=max(1, int(days or 30)))
|
||||
if start >= end:
|
||||
raise ValueError(f"since ({start.isoformat()}) must be before until ({end.isoformat()})")
|
||||
return start, end
|
||||
|
||||
|
||||
async def retrieval_summary(
|
||||
user_id: int | None, *, days: int = 30, near_miss_samples: int = 0,
|
||||
since=None, until=None,
|
||||
) -> dict:
|
||||
"""What the retrieval telemetry says, per surface, over a window.
|
||||
|
||||
@@ -1080,10 +1117,11 @@ async def retrieval_summary(
|
||||
no readout. It does distinguish "no rows" from "the read failed", because
|
||||
a read outage that looks like a quiet window can go unnoticed for weeks.
|
||||
"""
|
||||
since = datetime.now(timezone.utc) - timedelta(days=max(1, int(days)))
|
||||
since, until = resolve_window(days, since, until)
|
||||
out: dict = {
|
||||
"window_days": int(days),
|
||||
"window_days": round((until - since).total_seconds() / 86400, 2),
|
||||
"since": iso(since),
|
||||
"until": iso(until),
|
||||
"sources": {},
|
||||
"usage": {},
|
||||
"rule_usage": {},
|
||||
@@ -1181,6 +1219,7 @@ async def retrieval_summary(
|
||||
)
|
||||
.where(
|
||||
RetrievalLog.created_at >= since,
|
||||
RetrievalLog.created_at < until,
|
||||
RetrievalLog.user_id == user_id,
|
||||
)
|
||||
.group_by(RetrievalLog.source)
|
||||
@@ -1239,6 +1278,7 @@ async def retrieval_summary(
|
||||
.where(
|
||||
declined,
|
||||
RetrievalLog.created_at >= since,
|
||||
RetrievalLog.created_at < until,
|
||||
RetrievalLog.user_id == user_id,
|
||||
RetrievalLog.best_available_id.isnot(None),
|
||||
)
|
||||
@@ -1282,6 +1322,7 @@ async def retrieval_summary(
|
||||
)
|
||||
.where(
|
||||
NoteUsageEvent.created_at >= since,
|
||||
NoteUsageEvent.created_at < until,
|
||||
NoteUsageEvent.user_id == user_id,
|
||||
)
|
||||
.group_by(NoteUsageEvent.event, NoteUsageEvent.source)
|
||||
@@ -1300,6 +1341,7 @@ async def retrieval_summary(
|
||||
await session.execute(
|
||||
select(func.count(func.distinct(NoteUsageEvent.note_id))).where(
|
||||
NoteUsageEvent.created_at >= since,
|
||||
NoteUsageEvent.created_at < until,
|
||||
NoteUsageEvent.user_id == user_id,
|
||||
NoteUsageEvent.event == SURFACED,
|
||||
NoteUsageEvent.source.notin_(_AMB),
|
||||
@@ -1310,6 +1352,7 @@ async def retrieval_summary(
|
||||
await session.execute(
|
||||
select(func.count(func.distinct(NoteUsageEvent.note_id))).where(
|
||||
NoteUsageEvent.created_at >= since,
|
||||
NoteUsageEvent.created_at < until,
|
||||
NoteUsageEvent.user_id == user_id,
|
||||
NoteUsageEvent.event == PULLED,
|
||||
)
|
||||
@@ -1341,6 +1384,7 @@ async def retrieval_summary(
|
||||
select(NoteUsageEvent.note_id)
|
||||
.where(
|
||||
NoteUsageEvent.created_at >= since,
|
||||
NoteUsageEvent.created_at < until,
|
||||
NoteUsageEvent.user_id == user_id,
|
||||
NoteUsageEvent.event == PULLED,
|
||||
# autoescape because `_` is a LIKE wildcard: a bare
|
||||
@@ -1356,6 +1400,7 @@ async def retrieval_summary(
|
||||
select(NoteUsageEvent.source, NoteUsageEvent.note_id)
|
||||
.where(
|
||||
NoteUsageEvent.created_at >= since,
|
||||
NoteUsageEvent.created_at < until,
|
||||
NoteUsageEvent.user_id == user_id,
|
||||
NoteUsageEvent.event == SURFACED,
|
||||
)
|
||||
@@ -1409,6 +1454,7 @@ async def retrieval_summary(
|
||||
)
|
||||
.where(
|
||||
RuleUsageEvent.created_at >= since,
|
||||
RuleUsageEvent.created_at < until,
|
||||
RuleUsageEvent.user_id == user_id,
|
||||
)
|
||||
.group_by(RuleUsageEvent.event, RuleUsageEvent.source)
|
||||
@@ -1431,6 +1477,7 @@ async def retrieval_summary(
|
||||
select(func.count(func.distinct(RuleUsageEvent.rule_id)))
|
||||
.where(
|
||||
RuleUsageEvent.created_at >= since,
|
||||
RuleUsageEvent.created_at < until,
|
||||
RuleUsageEvent.user_id == user_id,
|
||||
RuleUsageEvent.event == RULE_SURFACED,
|
||||
)
|
||||
@@ -1441,6 +1488,7 @@ async def retrieval_summary(
|
||||
select(func.count(func.distinct(RuleUsageEvent.rule_id)))
|
||||
.where(
|
||||
RuleUsageEvent.created_at >= since,
|
||||
RuleUsageEvent.created_at < until,
|
||||
RuleUsageEvent.user_id == user_id,
|
||||
RuleUsageEvent.event == RULE_PULLED,
|
||||
)
|
||||
@@ -1456,6 +1504,7 @@ async def retrieval_summary(
|
||||
select(func.count(func.distinct(RuleUsageEvent.rule_id)))
|
||||
.where(
|
||||
RuleUsageEvent.created_at >= since,
|
||||
RuleUsageEvent.created_at < until,
|
||||
RuleUsageEvent.user_id == user_id,
|
||||
RuleUsageEvent.event.in_(RULE_OUTCOMES),
|
||||
)
|
||||
@@ -1477,6 +1526,7 @@ async def retrieval_summary(
|
||||
select(RuleUsageEvent.rule_id)
|
||||
.where(
|
||||
RuleUsageEvent.created_at >= since,
|
||||
RuleUsageEvent.created_at < until,
|
||||
RuleUsageEvent.user_id == user_id,
|
||||
RuleUsageEvent.event == RULE_PULLED,
|
||||
RuleUsageEvent.source.startswith("mcp_", autoescape=True),
|
||||
@@ -1488,6 +1538,7 @@ async def retrieval_summary(
|
||||
select(RuleUsageEvent.source, RuleUsageEvent.rule_id)
|
||||
.where(
|
||||
RuleUsageEvent.created_at >= since,
|
||||
RuleUsageEvent.created_at < until,
|
||||
RuleUsageEvent.user_id == user_id,
|
||||
RuleUsageEvent.event == RULE_SURFACED,
|
||||
)
|
||||
@@ -1716,14 +1767,14 @@ async def retrieval_summary(
|
||||
}
|
||||
rule_usage.update(_coverage((rule_complete or {}).get("*"), since))
|
||||
out["rule_usage"] = rule_usage
|
||||
out["system_usage"] = await _system_usage(user_id, since)
|
||||
out["moment_usage"] = await moment_usage(user_id, since)
|
||||
out["system_usage"] = await _system_usage(user_id, since, until)
|
||||
out["moment_usage"] = await moment_usage(user_id, since, until)
|
||||
# How each non-rule record reached a session: pushed by a hook, returned
|
||||
# by a search the session ran, or opened with nothing delivering it — and
|
||||
# how many were opened each way. Its own guard, so a failure flags itself.
|
||||
try:
|
||||
from grimoire.services import record_deliveries
|
||||
out["record_reach"] = (await record_deliveries.summary(user_id, days=days)
|
||||
out["record_reach"] = (await record_deliveries.summary(user_id, since=since, until=until)
|
||||
if user_id else None)
|
||||
except Exception:
|
||||
logger.exception("record reach read failed")
|
||||
@@ -1737,11 +1788,22 @@ async def retrieval_summary(
|
||||
# `_system_usage`'s reason — a failure flags itself rather than printing
|
||||
# an empty block, which reads as "nothing judged".
|
||||
try:
|
||||
out["judged"] = await judged_block(user_id, since)
|
||||
out["judged"] = await judged_block(user_id, since, until)
|
||||
except Exception:
|
||||
logger.warning("judged block read failed", exc_info=True)
|
||||
out["judged"] = {"judged_failed": True}
|
||||
|
||||
# ── The family line ──────────────────────────────────────────────────
|
||||
#
|
||||
# How often each project was entered, and how often the entry carried
|
||||
# the family line. Its own guard, for the judged block's reason.
|
||||
try:
|
||||
from grimoire.services.family_adoption import entry_summary
|
||||
out["family_line"] = await entry_summary(user_id, since, until)
|
||||
except Exception:
|
||||
logger.warning("family line read failed", exc_info=True)
|
||||
out["family_line"] = {"family_line_failed": True}
|
||||
|
||||
# ── What is wrong ────────────────────────────────────────────────────
|
||||
#
|
||||
# Computed LAST, over the blocks above rather than over the database, so
|
||||
@@ -1791,7 +1853,7 @@ async def retrieval_summary(
|
||||
# account.
|
||||
floor_moves: dict[str, str] = {}
|
||||
try:
|
||||
floor_moves = await floor_moves_since(since)
|
||||
floor_moves = await floor_moves_since(since, until)
|
||||
except Exception: # pragma: no cover - telemetry never raises
|
||||
logger.warning("could not read floor changes", exc_info=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user