diff --git a/app/sampler.py b/app/sampler.py index 46450fb..28e4ec0 100644 --- a/app/sampler.py +++ b/app/sampler.py @@ -98,6 +98,9 @@ def __init__(self, cfg: dict, eng: dict, broker: Broker, store=None): # loop warning: every MTP draft accepted, step after step, for loop_warn_s self.loop_win_s = float(cfg.get("loop_window_s", 5.0)) self.loop_warn_s = float(cfg.get("loop_warn_s", 30.0)) + # ...and only while the draft counter moves in most samples of the window: an engine that adds a request's + # counters when it ends puts them in one sample + self.loop_moving_share = float(cfg.get("loop_moving_share", 0.8)) # served-model regex -> seconds: the KV tile shows the highest reading of that window self.kv_peak_models = cfg.get("kv_peak_models", DEFAULT_KV_PEAK_MODELS) self._kv_hist: deque = deque() @@ -797,12 +800,18 @@ def _loop_signal(self, now: float, row: dict) -> None: acceptance pins at the maximum: on DSV4.1 (K=5) a real 9-minute Hermes reasoning loop read 6.00 tokens/step every minute, normal reasoning 2-3. Judged over loop_window_s of steps so one lucky stretch does not count. + + A window counts only when the draft counter moved in at least loop_moving_share of + its samples. gr8 added each request's counters when the request ended: agent traffic + then put one burst in nearly every window, and tool-call JSON accepts about 98% of + its drafts, so finished requests read as a loop while nothing looped. """ since = now - self.loop_win_s drafts = self._win_sum("drafts", since) or 0 dtok = self._win_sum("draft_tokens", since) or 0 acc = self._win_sum("accepted_tokens", since) or 0 - if drafts >= 10 and dtok > 0 and acc >= 0.98 * dtok: + if (drafts >= 10 and dtok > 0 and acc >= 0.98 * dtok + and self._moving_share("drafts", since) >= self.loop_moving_share): if self._full_accept_since is None: self._full_accept_since = since else: @@ -811,6 +820,11 @@ def _loop_signal(self, now: float, row: dict) -> None: row["spec_full_accept_s"] = round(secs, 1) row["loop_suspect"] = secs >= self.loop_warn_s + def _moving_share(self, logical: str, since: float) -> float: + """The share of the samples since SINCE in which the counter went up.""" + deltas = [d for t, d, _ in self._counter_win.get(logical) or [] if t >= since] + return sum(1 for d in deltas if d > 0) / len(deltas) if deltas else 0.0 + def _gen_active_rate(self, now: float) -> float | None: """Tokens per second of the ticks that generated, over decode_avg_window_s.""" floor_t = now - self.decode_avg_win_s diff --git a/tests/test_sampler.py b/tests/test_sampler.py index 8ae9162..9ffca05 100644 --- a/tests/test_sampler.py +++ b/tests/test_sampler.py @@ -246,6 +246,22 @@ def tick(t, acc_ratio): assert r["loop_suspect"] is False +def test_loop_warning_ignores_counters_that_arrive_per_request(): + """gr8 added a request's draft counters when it ended. Agent traffic finishing a request + every 5 s, each at 98.7% acceptance (tool-call JSON), must not read as a loop: the counter + moved in one sample of each window, not in most of them.""" + s = _mk() + t0 = time.time() - 100 + for i in range(120): # 60 s of 0.5 s samples + t = t0 + i * 0.5 + burst = i % 10 == 0 + for lg, d in (("drafts", 300), ("draft_tokens", 300), ("accepted_tokens", 296)): + s._win_delta(lg, 1000 + (d if burst else 0), 1000, t, 0.5) + row = {} + s._loop_signal(t, row) + assert row["loop_suspect"] is False and row["spec_full_accept_s"] <= s.loop_win_s, (i, row) + + def test_kv_tile_holds_the_peak_only_for_listed_models(): """DSV4.1 flips 0.70 <-> 1.02 % for ~1 s every 128 tokens: its tile shows the 10 s peak, every other model keeps the raw reading."""