fix(8192): a gated arm's declines include the calls its cue gate withheld
CI & Build / TypeScript typecheck (push) Successful in 37s
CI & Build / Plugin hooks (push) Successful in 15s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Python tests (push) Successful in 2m10s
CI & Build / integration (push) Successful in 2m15s
CI & Build / Build & push image (push) Successful in 21s

#8192 "The cannot_decline warning reads the result count from before the cue
gate, so a gated arm can never show a decline", step 3 of milestone 669.

auto_inject logs its ranked results before the cue gate decides, so a call
the gate silenced still reads as a hit, and cannot_decline fired on an arm
whose gate withholds most calls (since 11-23, gate_kept = 0 on 55 of 69,
40 of 44 and 71 of 84 calls a day, with result_count never 0).

The readout gains a `gate` block per source (gated_calls, declined_by_gate),
null where no row carried a gate decision, and cannot_decline counts
zero_result_calls plus declined_by_gate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-28 16:15:53 -05:00
co-authored by Claude Opus 5.5
parent deb991b2cc
commit 163d32859f
4 changed files with 99 additions and 13 deletions
+41 -5
View File
@@ -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,