diff --git a/src/grimoire/mcp/tools/search.py b/src/grimoire/mcp/tools/search.py index be29efbc..4b256de2 100644 --- a/src/grimoire/mcp/tools/search.py +++ b/src/grimoire/mcp/tools/search.py @@ -439,6 +439,12 @@ async def retrieval_telemetry( Read a null as "unknown", never as a zero: an artifact read as a measurement mis-scopes the surface. + `gate` is the cue gate's share, on an arm that has one: `gated_calls` + carried a gate decision and `declined_by_gate` returned ranked results + the gate then withheld. Such an arm logs `result_count` before the gate + decides, so its declines are `zero_result_calls` plus `declined_by_gate`. + `null` when no row in the window carried a gate decision. + `usage` — NOTES ONLY, from `note_usage_events`, at the per-note grain `retrieval_logs` cannot be indexed at: `surfaced` (ranked surfacings — a scored surface CHOSE the record), `ambient` (the rest), `pulled` split into @@ -614,7 +620,9 @@ async def retrieval_telemetry( Only ever raised for unbidden arms known to log unconditionally: a search returning a list every time is doing its job, and an arm whose zeros were never written would flag a LOGGING bug while pointing you at - a threshold. + a threshold. A gated arm's declines include the calls its cue gate + withheld (`gate.declined_by_gate`), since it logs its ranked results + before the gate decides. - `band_hugs_floor` — the weakest tenth of what an arm returns sits on its floor. The bar is doing the selecting and the score is not, so moving that floor changes how MUCH you get, not how good it is. diff --git a/src/grimoire/services/retrieval_telemetry.py b/src/grimoire/services/retrieval_telemetry.py index 058e25bc..ae7e8769 100644 --- a/src/grimoire/services/retrieval_telemetry.py +++ b/src/grimoire/services/retrieval_telemetry.py @@ -332,7 +332,8 @@ def _bucket(rows: list) -> dict: """A score readout a human can act on, from one aggregate row.""" (calls, zero, p10, p50, p90, lo, hi, avg_n, dur, measured, supp_calls, supp_zero, - miss_calls, miss_p50, miss_p90, miss_max) = rows + miss_calls, miss_p50, miss_p90, miss_max, + gated, gate_declined) = rows return { "calls": int(calls or 0), # A call that returned nothing is not a low-scoring call — it is a @@ -388,6 +389,21 @@ def _bucket(rows: list) -> dict: "max": _round(miss_max), } ), + # WHAT THE CUE GATE TURNED AWAY. A gated arm logs its ranked results + # before the gate decides, so its `result_count` is what cleared the + # floor, and a call the gate silenced still reads as a hit there. + # `declined_by_gate` counts those calls, which returned something + # from the ranker and were withheld by the gate; a gated arm's + # declines are these plus `zero_result_calls`. + # + # None when no row in the window carried a gate decision: an ungated + # arm, or rows older than the gate. + "gate": ( + None if not int(gated or 0) else { + "gated_calls": int(gated or 0), + "declined_by_gate": int(gate_declined or 0), + } + ), "avg_result_count": _round(avg_n), "p90_duration_ms": _round(dur, 1), } @@ -397,11 +413,11 @@ def _bucket(rows: list) -> dict: # the window: nothing counted, nothing scored. Positional, matching the SELECT # `_bucket` unpacks — calls, zero, p10, p50, p90, min, max, avg_n, # dur, measured, supp_calls, supp_zero, miss_calls, miss_p50, miss_p90, -# miss_max. The counts are 0 because zero calls is a real observation; +# miss_max, gated, gate_declined. The counts are 0 because zero calls is a real observation; # everything else is None because a distribution nobody sampled has no value, # and rendering it as 0.0 would state one. _NO_ROWS_IN_WINDOW = [0, 0, None, None, None, None, None, None, None, - 0, 0, 0, 0, None, None, None] + 0, 0, 0, 0, None, None, None, 0, 0] def _round(v, places: int = 4): @@ -591,6 +607,12 @@ def _compute_warnings(sources: dict, usage: dict, rule_usage: dict, # it is stuck — either its bar is beneath everything or it is not # applying one. # + # A gated arm stays quiet in two places: the floor, which shows as a + # zero-result call, and the cue gate after it, which logs the ranked + # results and then withholds them (`gate.declined_by_gate`). Both are + # declines; counting the first alone flags an arm whose gate is + # silencing most of its calls. + # # THREE GUARDS BEFORE TRUSTING THIS, and each prevents a false # alarm. Asked surfaces are exempt: a search returning a list every # time is a search doing its job, and flagging `mcp_search` would @@ -600,20 +622,24 @@ def _compute_warnings(sources: dict, usage: dict, rule_usage: dict, # exempt: an arm that records only its hits has a decline count that # is structurally zero, and this warning would fire on a LOGGING # defect while pointing the reader at the threshold. + gate = b.get("gate") or {} + declines = (b.get("zero_result_calls") or 0) + (gate.get("declined_by_gate") or 0) if ( calls >= min_calls - and (b.get("zero_result_calls") or 0) == 0 + and declines == 0 and point is not None and point.kind == UNBIDDEN and point.logs_unconditionally ): out.append(_warn( "cannot_decline", - f"{calls} calls, 0 of them returned nothing. An arm that fires " + f"{calls} calls, 0 of them returned nothing" + f"{' or were withheld by its cue gate' if gate else ''}. An arm that fires " f"unasked has to be able to say nothing; this one never has. " f"Check that it applies its floor at all before reading any " f"score below as evidence.", source=name, calls=calls, zero_result_calls=0, + **({"declined_by_gate": 0} if gate else {}), )) # ── Band hugs its floor ────────────────────────────────────────── @@ -1181,6 +1207,14 @@ async def retrieval_summary( ((RetrievalLog.result_count == 0) & (RetrievalLog.suppressed_count > 0), 1), else_=0, ) + # A gate decision is on the row only for a gated arm. A call the gate + # silenced after the ranker returned something: `result_count > 0`, so it + # is never also one of the zero-result calls above. + gated = case((RetrievalLog.gate_kept.isnot(None), 1), else_=0) + gate_declined = case( + ((RetrievalLog.gate_kept == 0) & (RetrievalLog.result_count > 0), 1), + else_=0, + ) def pct(p: float): return func.percentile_cont(p).within_group(RetrievalLog.top_score.asc()) @@ -1216,6 +1250,8 @@ async def retrieval_summary( func.percentile_cont(0.5).within_group(miss_score.asc()), func.percentile_cont(0.9).within_group(miss_score.asc()), func.max(miss_score), + func.sum(gated).label("gated"), + func.sum(gate_declined).label("gate_declined"), ) .where( RetrievalLog.created_at >= since, diff --git a/tests/test_retrieval_warnings.py b/tests/test_retrieval_warnings.py index 04889bb2..6feb728c 100644 --- a/tests/test_retrieval_warnings.py +++ b/tests/test_retrieval_warnings.py @@ -97,6 +97,31 @@ def test_an_arm_that_does_decline_is_not_flagged() -> None: assert "cannot_decline" not in codes(ws) +def test_a_gated_arm_whose_gate_declines_is_not_flagged() -> None: + """A gated arm logs its ranked results before the cue gate decides, so a + call the gate silenced still reads as a hit in `zero_result_calls`. Those + withheld calls are declines too.""" + gated = src(calls=300, zero_result_calls=0, + gate={"gated_calls": 300, "declined_by_gate": 240}) + assert "cannot_decline" not in codes(warn({"auto_inject": gated})) + + +def test_counting_zero_results_alone_would_flag_that_arm() -> None: + """The same arm with its gate block dropped, which is how the check read it + before: the warning fires, so the test above is not passing by accident.""" + assert "cannot_decline" in codes( + warn({"auto_inject": src(calls=300, zero_result_calls=0)})) + + +def test_a_gated_arm_that_never_declines_anywhere_is_flagged() -> None: + gated = src(calls=300, zero_result_calls=0, + gate={"gated_calls": 300, "declined_by_gate": 0}) + ws = warn({"auto_inject": gated}) + w = next(w for w in ws if w["code"] == "cannot_decline") + assert w["numbers"] == {"calls": 300, "zero_result_calls": 0, "declined_by_gate": 0} + assert "cue gate" in w["detail"] + + def test_an_arm_not_known_to_log_unconditionally_is_exempt(monkeypatch) -> None: """A structurally-zero decline count is a logging bug, not a floor. diff --git a/tests/test_services_retrieval_telemetry.py b/tests/test_services_retrieval_telemetry.py index 239524a8..5c434221 100644 --- a/tests/test_services_retrieval_telemetry.py +++ b/tests/test_services_retrieval_telemetry.py @@ -103,13 +103,14 @@ def test_the_readout_reports_unmeasured_suppression_as_none(): from grimoire.services.retrieval_telemetry import _bucket # calls, zero, p10, p50, p90, min, max, avg_n, dur, - # measured, supp_calls, supp_zero, miss_calls, miss_p50, miss_p90, miss_max + # measured, supp_calls, supp_zero, miss_calls, miss_p50, miss_p90, miss_max, + # gated, gate_declined unmeasured = _bucket([326, 114, 0.6, 0.68, 0.77, 0.55, 0.85, 1.7, 130.9, - 0, 0, 0, 0, None, None, None]) + 0, 0, 0, 0, None, None, None, 0, 0]) assert unmeasured["suppression"] is None measured = _bucket([35, 34, 0.75, 0.75, 0.75, 0.75, 0.75, 0.03, 51.9, - 35, 9, 9, 0, None, None, None]) + 35, 9, 9, 0, None, None, None, 0, 0]) assert measured["suppression"] == { "measured_calls": 35, "calls_with_suppression": 9, @@ -1124,13 +1125,14 @@ def test_the_readout_reports_unmeasured_near_misses_as_none(): from grimoire.services.retrieval_telemetry import _bucket # calls, zero, p10, p50, p90, min, max, avg_n, dur, - # measured, supp_calls, supp_zero, miss_calls, miss_p50, miss_p90, miss_max + # measured, supp_calls, supp_zero, miss_calls, miss_p50, miss_p90, miss_max, + # gated, gate_declined none_measured = _bucket([326, 114, 0.6, 0.68, 0.77, 0.55, 0.85, 1.7, 130.9, - 0, 0, 0, 0, None, None, None]) + 0, 0, 0, 0, None, None, None, 0, 0]) assert none_measured["near_misses"] is None measured = _bucket([326, 114, 0.6, 0.68, 0.77, 0.55, 0.85, 1.7, 130.9, - 0, 0, 0, 114, 0.61, 0.7104, 0.7189]) + 0, 0, 0, 114, 0.61, 0.7104, 0.7189, 0, 0]) assert measured["near_misses"] == { "measured_calls": 114, "p50": 0.61, @@ -1139,6 +1141,21 @@ def test_the_readout_reports_unmeasured_near_misses_as_none(): } +def test_the_readout_reports_the_cue_gate_only_where_rows_carried_one(): + """An ungated arm, or a window older than the gate, has no gate block: + a zeroed one would claim the gate declined nothing.""" + from grimoire.services.retrieval_telemetry import _bucket + + ungated = _bucket([326, 0, 0.6, 0.68, 0.77, 0.55, 0.85, 3.9, 130.9, + 0, 0, 0, 0, None, None, None, 0, 0]) + assert ungated["gate"] is None + + gated = _bucket([69, 0, 0.77, 0.8, 0.86, 0.7, 0.9, 3.9, 1065.0, + 0, 0, 0, 0, None, None, None, 69, 55]) + assert gated["gate"] == {"gated_calls": 69, "declined_by_gate": 55} + assert gated["zero_result_calls"] == 0 + + def test_the_readout_carries_no_field_derivable_from_its_neighbours(): """No readout field is a sum of the two beside it. @@ -1150,7 +1167,7 @@ def test_the_readout_carries_no_field_derivable_from_its_neighbours(): from grimoire.services.retrieval_telemetry import _bucket b = _bucket([326, 114, 0.6, 0.68, 0.77, 0.55, 0.85, 1.7, 130.9, - 0, 0, 0, 114, 0.61, 0.71, 0.72]) + 0, 0, 0, 114, 0.61, 0.71, 0.72, 0, 0]) derivable = { k for k, v in b.items() if isinstance(v, int) and not isinstance(v, bool)