Skip to content

Loop warning: count a window only when the draft counter moves in most samples - #39

Open
mmastrac wants to merge 1 commit into
AgentSparko:mainfrom
mmastrac:loop-signal-needs-moving-counters
Open

mmastrac wants to merge 1 commit into
AgentSparko:mainfrom
mmastrac:loop-signal-needs-moving-counters

Conversation

@mmastrac

@mmastrac mmastrac commented Nov 6, 2026

Copy link
Copy Markdown

The loop warning (_loop_signal) assumes the engine's speculative-decoding counters move as it decodes. gr8 adds each request's draft counters to /metrics when the request ends (kindlingai/gr8#388 changes that). That makes the warning fire on normal agent traffic:

  • An agent finishing a request every few seconds puts one burst of drafts in nearly every 5 s window.
  • Tool-call JSON accepts about 98% of its drafts.
  • So the windows read as full acceptance for 30 s and more, with nothing looping.

On a GLM-5.3-Flash serve, a 5 s poll of the counters showed vllm:generation_tokens_total rising about 50 a second while the draft counters stayed flat between request ends.

Change

  • _loop_signal counts a window only when the draft counter went up in at least loop_moving_share of its samples (new config key, default 0.8). An engine that reports counters as it decodes moves them in every sample, so the existing behaviour holds there.
  • _moving_share(logical, since): the share of samples since since in which a counter went up.
  • test_loop_warning_ignores_counters_that_arrive_per_request: one burst every 5 s at 98.7% acceptance for 60 s never warns. test_loop_warning_needs_every_draft_accepted_for_loop_warn_s still passes.

Tests

python -m pytest tests/test_sampler.py: 19 passed. The new test fails without the change. The full suite on macOS has the same 13 failures with and without this change (test_portability.py, test_portability_linux.py, test_recipes.py).

…t samples

An engine that adds a request's speculative-decoding counters when the
request ends puts each request's drafts in one sample. gr8 did that
(kindlingai/gr8#388 makes them live). Agent traffic finishing a request
every few seconds then put one burst in nearly every window, and
tool-call JSON accepts about 98% of its drafts, so finished requests
read as an exact loop.

_loop_signal now also needs the draft counter to have moved in
loop_moving_share (0.8) of the window's samples.
@AgentSparko

Copy link
Copy Markdown
Owner

Verified the premise and the tests locally, plus one cross-engine finding that changes how the guard behaves per engine.

Premise confirmed. kindlingai/gr8#388 ("spec-decode counters move while a request decodes") is the upstream fix; this box's gr8 build still emits per-request counters (g6s2shim.py line 792, "per-request counters, added once"), so the false positive is real here. LIMCARS polls at live_hz 2.0 (0.5 s) by default, loop_window_s 5, loop_warn_s 30.

Tests verified. On Linux: 19/19 pass in tests/test_sampler.py; the new test fails against main's sampler (spec_full_accept_s grows past loop_win_s, so it catches the bug); the two existing loop tests still pass; the full suite is 383 passed vs 382 on main (the +1 is the new test). The macOS failures you mention pass here.

Cross-engine finding — the moving-share default misreads the counter cadence for post-#388 gr8. The guard assumes the counter advances at poll cadence. gr8 post-#388 writes the cumulative m line at most once a second (gr8#388's change list), so at live_hz 2 the counter moves in only ~half the samples → moving share ~0.5 < 0.8 → genuine loops suppressed. Probe against the branch:

  • post-#388 gr8 (1 Hz counters, 2 Hz poll) genuine loop → 0/120 suspect frames (blind)
  • post-#388 gr8 at live_hz 1 → 35/60 (detection works)
  • live engine moving every sample at 2 Hz poll → 70/120 (detection works)
  • pre-#388 bursts at 98.7% acceptance → 0/120 (false positive fixed, as your test shows)

Lowering the default to ≤0.5 fixes it: burst-at-request-end engines still read ~0.1 (suppressed), post-#388 gr8 reads ~0.5 (detection works), live engines read ~1.0 (unchanged). live_hz is global (app/config.py line 43; EngineSampler gets the global cfg at app/sampler.py:1045), so raising it per engine isn't possible without a config change.

Engines without a drafts counter (EXL3 maps only exl3_accepted_draft_tokens_total; TensorFold maps mtp_drafted_total/mtp_accepted_total; Atlas keeps verify steps apart from draft tokens; Tarnwell maps draft_tokens/accepted_tokens; SGLang maps nothing) read drafts as None → drafts >= 10 fails → the loop warning is already blind there. Your change does not affect them.

Separate, not your scope: post-#388 gr8 with live counters, tool-call JSON at ~98% acceptance reads acc >= 0.98 * dtok on windows where the agent decodes tool-call JSON continuously — the acceptance condition false-positives regardless of the moving-share guard. The original design's risk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants